Skip to content

feat(node,store): compress existing cached blocks - #6853

Draft
MoonBoi9001 wants to merge 30 commits into
mb9/compress-new-blocksfrom
mb9/compress-existing-blocks
Draft

MoonBoi9001 wants to merge 30 commits into
mb9/compress-new-blocksfrom
mb9/compress-existing-blocks

Conversation

@MoonBoi9001

@MoonBoi9001 MoonBoi9001 commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

TL;DR

Adds graphman chain compress-blocks <chain> run, which compresses the blocks already in a chain's block cache in place while the chain stays in use, and status, which shows how far the last run got.

Motivation

#6851 only compresses blocks as they are written, so an existing cache keeps its uncompressed blocks until they are deleted. Truncating the cache and letting graph-node fetch blocks again costs an RPC call for every block a subgraph needs and slows indexing while the cache refills. Rewriting in place needs no RPC calls.

Summary

  • run rewrites blocks where they are stored, in batches in block order on --jobs connections (4 by default), and checks that every compressed block uncompresses to the original before writing it.
  • It limits the WAL the server writes to --max-rate (20 MB/s by default) and runs VACUUM after every --vacuum-every GB of blocks rewritten (50 by default), so later writes reuse the freed space.
  • A stopped run continues where it left off, keeping its count toward the next VACUUM. Progress is kept in a compress_blocks_progress table that a migration creates.
  • A block a node changes or locks during the run keeps what the node wrote and is tried again at the end.
  • The run stops with an error when something keeps VACUUM from cleaning up, rather than letting the table grow.
  • --compression none uncompresses blocks again.

Important

Every node that uses the chain must run a version that reads compressed blocks before a run starts. The table can grow by up to twice --vacuum-every during a run, or 3 times for blocks of a few KB.

@MoonBoi9001
MoonBoi9001 added this pull request to stack #6852 December 1, 2026 10:28
@MoonBoi9001
MoonBoi9001 marked this pull request as ready for review December 1, 2026 10:48
@MoonBoi9001
MoonBoi9001 marked this pull request as draft December 1, 2026 10:57
@MoonBoi9001
MoonBoi9001 marked this pull request as ready for review December 1, 2026 16:56
@MoonBoi9001
MoonBoi9001 force-pushed the mb9/compress-existing-blocks branch from 880cfc3 to 297477c Compare December 1, 2026 17:24
@MoonBoi9001
MoonBoi9001 marked this pull request as draft December 1, 2026 17:48
@MoonBoi9001
MoonBoi9001 force-pushed the mb9/compress-existing-blocks branch from 297477c to 0c67d4d Compare December 2, 2026 17:20
Nodes can store new blocks compressed, but blocks already in the cache stay as they were. Add
graphman chain compress-blocks run and status, which rewrite them in place in batches while the
chain is in use, keep any block a node changes meanwhile, and resume where a run stopped.
Rewritten blocks leave old versions behind, and autovacuum waits until a fifth of a table is
dead. Run VACUUM every 1 GiB or so on its own connection, stop it when anyone waits over a
second for a lock on the table, and pause if the table still grows more than that allows.
The block_compression setting only affects new blocks. Explain how compress-blocks run and
status rewrite the cached ones, what they need, what they cost in WAL, time and disk, and how
to undo them.
Compressing cached blocks had its own copy of the code that adds the column for compressed
blocks. Use the chain store's instead, so both share one lock timeout, backoff and column state.
Each batch locked the table first and read why a lock failed through a server-side trick, and a
run waited for anti-wraparound autovacuum. Give every statement a 2 second lock timeout instead,
so the run stops with an error rather than make others queue behind it, and drop the rest.
A run that could not convert some blocks, or gave up on blocks that stayed locked, still ended
as if it succeeded. Count both in its progress, show them in status, and end with an error
that says how many blocks were left and why.
A run created a table for its progress in the chain's schema when it started. Create one table
for all chains in a database migration instead, and say how to apply it when graphman finds it
missing, since graphman does not run migrations itself.
The pause for a table that grows too much allowed only twice what is rewritten between VACUUMs,
so uncompressing blocks always paused. Also allow the bytes new values add over old ones, read
after Postgres stores them, and count a batch that runs past the VACUUM interval.
A compress run stopped by Ctrl-C, an error while watching its VACUUM or a dead process left that
VACUUM running on the server. Cancel it in each case, have the server check that we are still
there, and close a dropped run's connections so that its lock goes with them.
A limit on the table's size stood in for this, its pause never ended, and it tripped on undos,
small blocks and busy chains. After each VACUUM, stop with an error when the dead rows it left
outnumber the blocks rewritten since the VACUUM before; running the command again continues.
Adding the column reused the node's code, which hides errors and backs off for up to an hour, so
a run by a user who does not own the table paused forever. The run now checks the owner first,
and gets the real error back, trying for about a minute before it stops with it.
After Ctrl-C, cancelling the VACUUM and releasing the lock had no time limit, and later presses
were ignored, so a dead database left graphman hanging for about 15 minutes. Cleaning up now
stops after 30s or on a second Ctrl-C, closing the connection so the server releases the lock.
When VACUUM gave way to others and cancelling it failed, the run returned at once and left the
VACUUM to the server's client check. It now waits up to 10s for it, as the other paths do.
The disk room that compress-blocks needs is given in multiples of --vacuum-every, so say what it
is by default: the larger of 1 GiB and the size of the TOAST table's index.
@MoonBoi9001
MoonBoi9001 force-pushed the mb9/compress-existing-blocks branch from 0c67d4d to d2375e5 Compare December 4, 2026 01:02
A batch covered at most 100 block numbers, so where numbers are sparse it held about 1 block and
a run rewrote about 90 blocks a second. Batches now take the next blocks by count, up to 10,000
and 32 MiB as stored, and always take every block of the numbers they cover.
A run read, compressed and wrote each batch in turn on 1 connection, waiting on the disk each
time. Workers now write batches at once on connections of their own, and the saved progress
stops before the first batch not yet written. The WAL limit now covers the server's WAL overall.
graphman ran compress-blocks on 1 connection. --jobs sets how many connections write batches at
once; graphman raises --pool-size to fit them, and the docs explain both.
graphman raises --pool-size for compress-blocks run so that every job gets a connection. That
choice now sits in a function of its own, and a test checks it for several option values.
With several jobs a batch ends after later ones went out at other sizes, so one timing changed
the size several times over and batches swung between tiny and huge. The next size now starts
from the size of the batch that ended.
A worker waits after reading its batch until the run answers, which can take long while the run
pauses for the WAL limit or for replicas. Counting that wait shrank batches for no reason; the
time of a batch now covers only its own work.
Progress lines count WAL only up to the batch before them, so the WAL of the last batches and
the last VACUUM never showed. The run now counts it at the end and prints the total.
When several workers fail at once, the run ended with the first error and said nothing about the
others. It still ends with the first, and now prints each of the others as it arrives.
Once a run has rewritten --vacuum-every of blocks, the batches other jobs are writing still
finish before VACUUM runs, so the table can grow by up to --jobs minus 1 batches more.
The comment on the pool check said a run waits without end for its connections. It waits without
end only for a permit, and then gives up after GRAPH_STORE_CONNECTION_TIMEOUT.
With small blocks every VACUUM visits most of the TOAST table however much was rewritten, so
running it after the TOAST index size made cleanup take 650 s per GB rewritten in a test, and
after 50 GB it took 48 s per GB. The cost is up to about 100 GB more disk while a run lasts.
pg_current_wal_lsn() leaves out WAL not yet written to disk, and the WAL writer writes that of a
VACUUM on its next round, up to 200 ms later, so the total missed the last VACUUM. Read
pg_current_wal_insert_lsn() instead, and test with enough blocks that a miss shows.
A run that stopped forgot how many bytes it rewrote since its last VACUUM, so with the 50 GB
default a stop and a second run could rewrite about 99 GB before the next VACUUM. A new
migration adds that count to the progress table, and the next run, continued or new, carries it.
The sentence on disk room read as if the default --vacuum-every rose by half for blocks of a few
KB. It is the room that rises: twice --vacuum-every, or 3 times it for such blocks, so 100 GB or
150 GB at 50 GB. The default now has a point of its own.
No release has run the migration that creates the progress table yet, so the 2 columns that
count rewrites since the last VACUUM go in it rather than in a second migration.
When something held back VACUUM's cleanup, the run stopped and asked to be run again, but first
reset its count of bytes rewritten since VACUUM, so the next run waited a whole interval before
its first VACUUM. The count now resets only after a VACUUM that cleaned up.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant