Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1707caa
feat(store): give the space freed by compressing blocks back to the disk
MoonBoi9001 Dec 3, 2026
7ba5255
docs(graphman): describe shrinking the blocks table
MoonBoi9001 Dec 3, 2026
20139f4
fix(store): move blocks as they are stored when shrinking by default
MoonBoi9001 Dec 3, 2026
c98210d
fix(store): keep the map that shrinking uses in a temporary table
MoonBoi9001 Dec 3, 2026
4049653
test(store): shrink blocks as a plain owner and beside a writing node
MoonBoi9001 Dec 3, 2026
0f7e3c4
fix(store): tell a non-owner at once that it can not shrink the table
MoonBoi9001 Dec 4, 2026
d1d626e
test(store): shrink as a plain owner down to VACUUM and check it shrank
MoonBoi9001 Dec 4, 2026
52683ec
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 4, 2026
d88d850
fix(store): end a shrink batch once its blocks take up 32 MiB
MoonBoi9001 Dec 4, 2026
18e12ef
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 4, 2026
6c70d1b
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 4, 2026
9a6bf7e
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 4, 2026
9f07179
fix(store): stop shrinking once moved blocks no longer fit in front
MoonBoi9001 Dec 5, 2026
a987670
fix(store): wait for sessions that keep deleted rows before shrinking
MoonBoi9001 Dec 5, 2026
cdd2cc3
fix(store): count the WAL of the first VACUUM of a shrink
MoonBoi9001 Dec 5, 2026
c91b49b
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 5, 2026
6e01ae8
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 5, 2026
bccd9b6
test(store): count all the WAL a shrink writes, written out or not
MoonBoi9001 Dec 5, 2026
5ce94e1
test(store): pack the table before adding the blocks a shrink deletes
MoonBoi9001 Dec 5, 2026
bbcfaa3
test(store): store pglz blocks in the test of moves that no longer fit
MoonBoi9001 Dec 5, 2026
864f431
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 5, 2026
fd2ebe0
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 5, 2026
337abca
fix(store): let owners shrink without writing the progress table
MoonBoi9001 Dec 5, 2026
fe743b7
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 5, 2026
dc84864
Merge branch 'mb9/pause-compressing-blocks-when-unsafe' into mb9/give…
MoonBoi9001 Dec 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions docs/graphman.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ Inspect all blocks after block `13000000`:
SUBCOMMANDS:
run Rewrite the blocks with the given compression, or continue a run that did not finish
status Show how far the last run got
shrink Give the space that rewriting blocks freed back to the disk (Postgres 17 or later)

### DESCRIPTION

Expand Down Expand Up @@ -418,12 +419,23 @@ resolved continues where it stopped. That check needs Postgres 15 or later; on o
when it starts that it can not check. The files of the table do not get smaller, but the space inside them is
reused for new blocks.

`shrink` gives the freed space back to the disk: it moves the blocks stored at the end of the table's TOAST table
into free space nearer its start, as they are stored unless `--compression` says otherwise, and lets `VACUUM`
cut off the empty end. It repeats that until the TOAST table is within about 10% of the size of the block data in
it, stops getting smaller, or the blocks it moves no longer fit into free space in front of the others. It needs
Postgres 17 or later,
and needs to read the TOAST table, which a user who is not a superuser can only do after a superuser runs
`grant usage on schema pg_toast to <user>`. While it works, it keeps a map from the data in the TOAST table to
the blocks it belongs to in a temporary table, which goes away when it ends. The map takes up about 180 bytes
of disk per block, measured on test data, and twice that for a block with both an uncompressed and a compressed
value in the TOAST table. It pauses for the same reasons as `run`.

To limit the load on the database, the command limits how much WAL the server writes per second while it works,
and pauses while replicas fall behind. That limit counts all WAL the server writes, not only the command's own, and
holds for all its connections together. Besides its `--jobs` connections, the command uses 1 connection of its own
and 1 for `VACUUM`, all from the connection pool of the chain's shard; `graphman` raises `--pool-size` to make
room for them, and with `--pool-size 0` the command stops before it starts if the shard's `pool_size` in the
configuration is too small.
configuration is too small. `shrink` works on 1 connection of its own and 1 for `VACUUM`.
The `VACUUM` it runs stops when another session waits more than a second for a lock on the table, and tries
again once nobody waits. Writing a batch never waits for a block that a node has locked, and the command stops
with an error rather than wait more than 2 seconds for a lock on the table, since everybody who needs a
Expand Down Expand Up @@ -462,12 +474,14 @@ says so.
table.
- Filesystem snapshots and incremental backups keep the old blocks, so the space on their disks is only freed
once they expire.
- Shrinking truncates the table, which needs a short exclusive lock on it. Replicas replay that lock, which can
delay queries running on them, or cancel them.

### UNDO

Set `block_compression = "none"` on every node so that new blocks are stored uncompressed, then run
`run --compression none`, which writes compressed blocks back uncompressed. That needs room for the uncompressed
blocks again.
blocks again. `shrink` then gives back to the disk whatever space that leaves free in the table.

### OPTIONS

Expand All @@ -483,11 +497,18 @@ blocks again.
WAL. The default is 8
- `--jobs`: how many batches to write at once, each on a connection of its own. The default is 4

`shrink` has these options:

- `--compression`: store the blocks that move this way, so that `zstd:1` compresses uncompressed blocks and
`none` uncompresses compressed ones. Without it, blocks move as they are stored
- `--max-rate` and `--max-wal-backlog`: as for `run`, with the same defaults

### EXAMPLES

Compress all blocks for mainnet:
Compress all blocks for mainnet, and then give the space that freed back to the disk:

graphman --config config.toml chain compress-blocks mainnet run
graphman --config config.toml chain compress-blocks mainnet shrink

Compress them with 8 connections, letting the server write up to 100 MB of WAL a second:

Expand Down
23 changes: 22 additions & 1 deletion node/src/bin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,22 @@ pub enum CompressBlocksCommand {
},
/// Show how far the last run got
Status,
/// Give the space that rewriting blocks freed back to the disk by
/// moving the blocks at the end of the table forward and truncating it.
/// Needs Postgres 17 or later
Shrink {
/// How to store the blocks that move; without it, blocks move as
/// they are stored
#[clap(long)]
compression: Option<BlockCompression>,
/// How many MB of WAL to write per second at most
#[clap(long, default_value = "20", value_parser = clap::value_parser!(u64).range(1..))]
max_rate: u64,
/// Pause while a replication slot or the WAL archiver holds back
/// more than this many GB of WAL
#[clap(long, default_value = "8", value_parser = clap::value_parser!(u64).range(1..))]
max_wal_backlog: u64,
},
}

#[derive(Clone, Debug, Subcommand)]
Expand Down Expand Up @@ -1597,7 +1613,7 @@ async fn main() -> anyhow::Result<()> {
}
CompressBlocks { chain_name, method } => {
use CompressBlocksCommand::*;
use commands::compress_blocks::{run, status};
use commands::compress_blocks::{run, shrink, status};
let chain_store = ctx.chain_store(&chain_name).await?;
match method {
Run {
Expand All @@ -1618,6 +1634,11 @@ async fn main() -> anyhow::Result<()> {
.await
}
Status => status(chain_store).await,
Shrink {
compression,
max_rate,
max_wal_backlog,
} => shrink(chain_store, compression, max_rate, max_wal_backlog).await,
}
}
Truncate { chain_name, force } => {
Expand Down
37 changes: 36 additions & 1 deletion node/src/manager/commands/compress_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::{Duration, Instant};
use graph::prelude::anyhow::Error;
use graph_store_postgres::{
BlockCompression, ChainStore, CompressBlocksProgress, CompressBlocksReporter,
CompressBlocksRequest, CompressBlocksStatus,
CompressBlocksRequest, CompressBlocksStatus, ShrinkBlocksRequest,
};

const MB: u64 = 1_000_000;
Expand Down Expand Up @@ -94,6 +94,41 @@ pub async fn run(
Ok(())
}

pub async fn shrink(
chain_store: Arc<ChainStore>,
compression: Option<BlockCompression>,
max_rate: u64,
max_wal_backlog: u64,
) -> Result<(), Error> {
let req = ShrinkBlocksRequest {
compression,
max_wal_rate: max_rate * MB,
max_wal_backlog: max_wal_backlog * GB,
window: None,
};
let report = chain_store
.shrink_blocks(&req, &mut Reporter::new())
.await?;
println!(
"the TOAST table of the blocks for {} went from {} MB to {} MB in {} rounds that moved \
{} blocks and wrote {} MB of WAL; it holds {} MB of blocks",
chain_store.chain,
report.toast_before / MB,
report.toast_after / MB,
report.rounds,
report.blocks,
report.wal_bytes / MB,
report.live / MB
);
if !report.done {
println!(
"it stopped shrinking before it got close to the size of the blocks in it; \
the messages above say what got in the way"
);
}
Ok(())
}

pub async fn status(chain_store: Arc<ChainStore>) -> Result<(), Error> {
match chain_store.compress_blocks_status().await? {
CompressBlocksStatus::NotStarted => println!("not started"),
Expand Down
28 changes: 27 additions & 1 deletion store/postgres/src/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::AsyncPgConnection;
use crate::block_compression::BlockCompression;
use crate::compress_blocks::{
BlocksCompressor, CompressBlocksProgress, CompressBlocksReporter, CompressBlocksRequest,
CompressBlocksStatus,
CompressBlocksStatus, ShrinkBlocksReport, ShrinkBlocksRequest,
};
use crate::vid_batcher::AdaptiveBatchSize;
use crate::{chain_head_listener::ChainHeadUpdateSender, pool::ConnectionPool};
Expand Down Expand Up @@ -3332,6 +3332,32 @@ impl ChainStore {
compressor.run_as_for_tests(conn, role, req, reporter).await
}

/// Give the space that compressing blocks freed inside the blocks table
/// back to the disk
pub async fn shrink_blocks(
&self,
req: &ShrinkBlocksRequest,
reporter: &mut dyn CompressBlocksReporter,
) -> Result<ShrinkBlocksReport, StoreError> {
let (compressor, conn) = self.blocks_compressor().await?;
compressor.shrink(conn, req, reporter).await
}

/// Shrink the blocks table as `role` to see what users without some
/// privileges get
#[cfg(debug_assertions)]
pub async fn shrink_blocks_as_for_tests(
&self,
role: &str,
req: &ShrinkBlocksRequest,
reporter: &mut dyn CompressBlocksReporter,
) -> Result<ShrinkBlocksReport, StoreError> {
let (compressor, conn) = self.blocks_compressor().await?;
compressor
.shrink_as_for_tests(conn, role, req, reporter)
.await
}

pub async fn compress_blocks_status(&self) -> Result<CompressBlocksStatus, StoreError> {
let (compressor, mut conn) = self.blocks_compressor().await?;
compressor.status(&mut conn).await
Expand Down
Loading
Loading