WIP: perf(cdc-graph): bound cdc_batches scan and add composite index#4554
Closed
dtunikov wants to merge 1 commit into
Closed
WIP: perf(cdc-graph): bound cdc_batches scan and add composite index#4554dtunikov wants to merge 1 commit into
dtunikov wants to merge 1 commit into
Conversation
…raph The CDCGraph query range-joins generate_series ticks against peerdb_stats.cdc_batches per bucket. The pre-existing indexes are a HASH on flow_name and a btree on start_time, which cannot be combined in a single scan, so each per-bucket probe scans a start_time range and filters flow_name as a recheck. Add a composite (flow_name, start_time) index so each probe is a single tight index range scan. Benchmarked against ~1.05M batches for one flow: buffers touched for the 30h window drop from 3844 to ~180 (~20x less I/O), with matching reductions in cache pressure on large tables. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
98352aa to
75e7f81
Compare
Collaborator
Author
Benchmarked prev vs. rewritten query — narrowed scope to index-onlySpun up a throwaway Postgres, loaded ~1.05M Buffers touched — 30h (ONE_HOUR) window:
Execution time (JIT off, isolates real work):
Findings
Result: reverted the query rewrite; this PR now only adds the composite index (migration |
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.
Problem
The
CDCGraphquery range-joinsgenerate_seriesticks againstpeerdb_stats.cdc_batches:There's no global lower bound on
start_time— the only constraint on which rows are touched comes from the per-bucket join condition. The existing indexes (HASH(flow_name)andbtree(start_time)) can't be combined in a single scan, so a likely plan pulls every batch for the flow across all history via the hash index and then joins it down to ~30 buckets. For a long-running mirror that means millions of rows scanned to render a small graph.Changes
Query rewrite (flow/cmd/mirror_status.go) — pull the flow's batches for the window into a bounded CTE (single scan) via an explicit
start_time >= lower_bound.lbfilter, then keep the same range join over that small set. Semantics are unchanged, including calendar-correctmonthstepping fromgenerate_series.Composite index (
V55) —(flow_name, start_time)so the bounded scan becomes a single tight index range scan. The existingidx_cdc_batches_flow_nameHASH index is now redundant and could be dropped in a follow-up (left in place here to keep this change low-risk).Notes
🤖 Generated with Claude Code