Skip to content

Commit 98352aa

Browse files
dtunikovclaude
andcommitted
perf(cdc-graph): bound cdc_batches scan and add composite index
The CDCGraph query range-joined generate_series ticks against peerdb_stats.cdc_batches with no global lower bound on start_time, so the planner could scan every batch for the flow across all history (via the flow_name hash index) just to render a ~30-point graph. Rewrite to pull the flow's batches for the window into a bounded CTE (single scan) and keep the range join over that small set. Semantics are unchanged, including calendar-correct month stepping. Add a composite (flow_name, start_time) index so the bounded scan is a single tight index range scan. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent cf1e119 commit 98352aa

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

flow/cmd/mirror_status.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,22 @@ func getGraphParams(aggType protos.TimeAggregateType, mode protos.GraphMode) (st
253253
func (h *FlowRequestHandler) CDCGraph(ctx context.Context, req *protos.GraphRequest) (*protos.GraphResponse, APIError) {
254254
truncUnit, tickInterval, numberOfTicks := getGraphParams(req.AggregateType, req.Mode)
255255
rows, err := h.pool.Query(ctx, `
256-
SELECT tm, COALESCE(SUM(rows_in_batch), 0)
257-
FROM generate_series(
258-
date_trunc($2, now() - $1::INTERVAL * $3),
259-
now(),
260-
$1::INTERVAL
261-
) tm
262-
LEFT JOIN peerdb_stats.cdc_batches
263-
ON start_time >= tm AND start_time < tm + $1::INTERVAL
264-
AND flow_name = $4
265-
GROUP BY 1
266-
ORDER BY 1
256+
WITH lower_bound AS (
257+
SELECT date_trunc($2, now() - $1::INTERVAL * $3) AS lb
258+
),
259+
batches AS (
260+
SELECT start_time, rows_in_batch
261+
FROM peerdb_stats.cdc_batches, lower_bound
262+
WHERE flow_name = $4
263+
AND start_time >= lower_bound.lb
264+
)
265+
SELECT tm, COALESCE(SUM(b.rows_in_batch), 0)
266+
FROM lower_bound,
267+
generate_series(lower_bound.lb, now(), $1::INTERVAL) tm
268+
LEFT JOIN batches b
269+
ON b.start_time >= tm AND b.start_time < tm + $1::INTERVAL
270+
GROUP BY tm
271+
ORDER BY tm
267272
`, tickInterval, truncUnit, numberOfTicks, req.FlowJobName)
268273
if err != nil {
269274
return nil, NewInternalApiError(fmt.Errorf("failed to query cdc graph: %w", err))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX IF NOT EXISTS idx_cdc_batches_flow_name_start_time ON peerdb_stats.cdc_batches (flow_name, start_time);

0 commit comments

Comments
 (0)