Skip to content

feat(node,store): shrink the blocks table after compressing - #6861

Draft
MoonBoi9001 wants to merge 25 commits into
mb9/pause-compressing-blocks-when-unsafefrom
mb9/give-compressed-block-space-back-to-disk
Draft

MoonBoi9001 wants to merge 25 commits into
mb9/pause-compressing-blocks-when-unsafefrom
mb9/give-compressed-block-space-back-to-disk

Conversation

@MoonBoi9001

Copy link
Copy Markdown
Member

Adds graphman chain compress-blocks <chain> shrink, which gives the space freed by compressing blocks back to the disk. It moves the last blocks in the TOAST table into free space nearer the start, so VACUUM can truncate the end. It needs Postgres 17 or later, plus grant usage on schema pg_toast for a user that is not a superuser.

Rewriting blocks frees space inside the table, but its files stay as big. Add a shrink command
for Postgres 17 and later that moves the blocks at the end of the table into that free space
and lets VACUUM cut off the empty end, a slice at a time, with the same pacing and pauses.
Explain how compress-blocks shrink gives the space that rewriting blocks freed back to the disk,
what it needs, the map it keeps while it works, and what its truncation costs replicas.
Shrinking the blocks table compressed every uncompressed block it moved unless told otherwise,
which changed how blocks are stored without being asked. Move blocks as they are by default,
and change how they are stored only when --compression is given.
Shrinking kept its map from TOAST values to blocks in an unlogged table in the chain's schema,
which an interrupted run left behind. Keep it in a temporary table of the connection that
shrinks, and close that connection afterwards, so the map goes away however shrinking ends.
Check that an owner who is no superuser can shrink the blocks table once it may read TOAST
tables, and that blocks a node changes or adds while shrinking runs keep what the node wrote.
Shrinking added the missing data_zstd column before it checked that the user owns the blocks
table, so a user who does not own it tried to add the column for about a minute. Check first.
The test that shrinks the blocks table as an owner who is no superuser ran VACUUM as the
superuser and never checked the result. Run that VACUUM as the owner and check the table shrank.
@MoonBoi9001
MoonBoi9001 added this pull request to stack #6852 December 4, 2026 01:03
Shrinking moves as many blocks at once as compressing does, which is now up to 10,000, so a
batch of big blocks could take GBs of memory. Its batches now also end at 32 MiB of blocks.
…-compressed-block-space-back-to-disk

# Conflicts:
#	store/postgres/src/compress_blocks.rs
…-compressed-block-space-back-to-disk

# Conflicts:
#	store/postgres/src/compress_blocks.rs
A packed table of small blocks swung between 2 sizes, as each round moved every block to the end
and back. The run now stops after a batch whose blocks kept their size yet landed past the end,
and runs VACUUM first so that rows deleted before it count as free space.
A session with an old snapshot kept the first VACUUM from freeing rows deleted before shrink
began, so the first batch went past the end and the run stopped with most of the table free.
Shrink now waits for such sessions first and names them, as it does after its own writes.
Shrink took its first WAL reading only after its first VACUUM, so the WAL that VACUUM wrote was
neither counted nor paced. It now takes a reading before it.
…-compressed-block-space-back-to-disk

# Conflicts:
#	store/postgres/src/compress_blocks.rs
pg_current_wal_lsn() is how far the server has written WAL out to disk, which trails the WAL a
VACUUM has just inserted. The test now takes the shrink's total from pg_current_wal_insert_lsn.
VACUUM FULL keeps rows in the order they sit in the table, and new rows fill space that earlier
tests freed, so some deleted blocks could stay in front of the kept ones and need moving.
Packing the table first appends the new blocks in order. CI on Postgres 19 hit this.
Postgres 19 compresses with lz4 by default, which fits these blocks of about 2 KB in 1 TOAST
chunk each, so the packed table came within 10% of their size and the test's setup check failed.
The test now sets pglz, the default up to Postgres 18, on the column while it runs.
Shrinking runs VACUUM through the same code as a compress run, which now also saved the run's
count of bytes since VACUUM in the progress table, so an owner without rights on that table
could no longer shrink. A compress run now saves that count itself after each of its VACUUMs.
…-compressed-block-space-back-to-disk

# Conflicts:
#	store/postgres/src/compress_blocks.rs

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