400: updated pg compaction bounds #407
Open
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.
Context
modules/module-postgres-storage/src/storage/PostgresCompactor.ts.feature/pg-compaction-bounds.Current Issue
The compaction loop queries
bucket_datain reverse PK orderORDER BY bucket_name DESC, op_id DESCwith this filter:Because of the OR, PostgreSQL cannot traverse the
(group_id, bucket_name, op_id)PRIMARY KEY efficiently and instead runsIndex Scan Backward using unique_idwhile filtering millions of rows (example plan reports ~4.3M rows scanned for a 10k LIMIT).Result: high latency and IO for compaction jobs, especially when buckets contain large histories.
Change Overview
groupParam,bucketLowerParam,bucketUpperParam,upperOpParam,limitParam).upperOpIdLimit === BIGINT_MAX): keep binary comparisonbucket_name < bucketUpper COLLATE "C".ROW(bucket_name COLLATE "C", op_id) < ROW(bucketUpper, upperOpIdLimit); this follows the composite index ordering exactly.bucketLowerhandling andMAX_CHARsentinel unchanged.bucketUpper/upperOpIdLimitfrom the last row in each batch.as consttagging on parameter objects prevents lint errors and clarifies Postgres types.Why This Works
(bucketUpper, upperOpIdLimit)cursor, so scanned rows ≈ requested LIMIT.COLLATE "C") consistently to match the original comparison semantics.Testing / Verification Plan
EXPLAIN (ANALYZE, BUFFERS)for the old query to document ~4.3M scanned rows.