Repository navigation
feat(node,store): shrink the blocks table after compressing - #6861
Draft
MoonBoi9001 wants to merge 25 commits into
Draft
MoonBoi9001 wants to merge 25 commits into
MoonBoi9001 wants to merge 25 commits into
Conversation
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.
…-compressed-block-space-back-to-disk
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
…-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
…-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.
…-compressed-block-space-back-to-disk
…-compressed-block-space-back-to-disk
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
…-compressed-block-space-back-to-disk # Conflicts: # store/postgres/src/compress_blocks.rs
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, plusgrant usage on schema pg_toastfor a user that is not a superuser.