perf(inkless:control-plane): index and rewrite list_offsets_v1 timestamp lookups [KC-537] #799
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #799"
general
weight: 0.35
jeqo
The concrete-timestamp branch of
list_offsets_v1(theoffsetsForTimespath) picks its batch withORDER BY batch_id LIMIT 1.batches_pkeyyields that order, so the planner can walk the primary key across the wholebatchestable with the partition and timestamp as filters, and inside plpgsql it does so on every call because the generic plan cannot see the timestamp's selectivity. With a largebatchesbacklog, every ListOffsets then runs intostatement_timeout. MAX_TIMESTAMP has a related problem: no index coversbatch_timestamp(...), so it evaluates the function for every retained batch. Two Flyway migrations fix this and must ship together: V30 adds the index, and V31 rewrites the lookups so the planner has no ordered-walk plan left to pick. See the commit message for the planner reasoning.Flyway migrations
V30__Batches_by_timestamp_index.sql: createsbatches_by_timestamp_idxon(topic_id, partition, batch_timestamp(...)) INCLUDE (base_offset), then runsANALYZE batches. Flyway runs it in a transaction, so the build holds aSHARElock onbatchesand blockscommit_fileuntil it finishes, during broker startup. Measured build rate is about 200k rows/s single-threaded: about 6 minutes at 70M rows and over an hour at the 1e9 rows V24 designs for. At that scale, run theCREATE INDEX CONCURRENTLY IF NOT EXISTSstatement from the migration header before upgrading. The migration then only runsANALYZE.batch_timestampmust stayLANGUAGE plpgsql, otherwise queries stop matching the stored index expression and the index goes unused. The index adds about 4 GB per 70M rows and a plpgsql call per inserted batch.V31__List_offsets_min_offset_lookup.sql:CREATE OR REPLACE FUNCTION list_offsets_v1, no table lock. The twobatcheslookups becomeMIN(base_offset)over the timestamp predicate, followed by a point fetch by offset. A concrete-timestamp request first checks the oldest retained batch and skips the scan when it already qualifies. The function body is the V28 one and readslogs.deleted_at, so it cannot be hand-applied to a schema older than V28.V30: new index
V31:
list_offsets_v1changesOnly the two
batcheslookups change (MAX_TIMESTAMP and the concrete timestamp). Thelogs-only branches are untouched. The diff is against the V28 definition, the last one before this PR.<details>
<summary>Diff of <code>list_offsets_v1</code>, V28 to V31</summary>
</details>
Testing
ListOffsetsJobTest.timestampLookupFollowsOffsetOrderNotTimestampOrder: out-of-order timestamps across three batches. It pins that the lookup returns the first batch by offset, not the smallest qualifying timestamp. The existing MAX_TIMESTAMP and concrete-timestamp contract tests run against V31 throughPostgresControlPlaneTest.ListOffsetsJobTest.maxTimestampTieReturnsFirstBatchByOffset,timestampBeforeOldestBatchReturnsOldestBatch, andtimestampLookupIsClampedToLogStartOffset: pin the tie case, both branches of the oldest-batch check, and thelog_start_offsetclamp.ListOffsetsTimestampScanBenchmarkTest(@Tag("benchmark"), not run in CI): runs one seeded workload against the baseline schema (V29), V30, and V31, each in its own database through a Flyway target version. It prints per-call latency, write cost, index sizes, and EXPLAIN output.How to read the benchmark: each schema prints three tables, one row per depth (25k to 400k batches in one partition). Compare schemas at the same depth, and look at how a column changes with depth more than at any single figure. In the read table, a flat column means the query ignores partition size, and a column that doubles when the depth doubles means it reads the whole partition. About 500 to 1,000 microseconds is the fixed cost of the job and the round trip, so values near that floor are "constant", not "fast". Compare V29 with V30 to price the index, since V30 and V31 share a write path. Run-to-run noise is about 10% on most figures and up to 30% on a single one. Plans come from standalone SQL and timings go through the plpgsql function, so the two can differ.
Benchmark results
One partition, batch
icarries timestampbase + i, statistics refreshed after each seed step, local Postgres 17 container. Microseconds perlist_offsets_v1call throughListOffsetsJobat 400k retained batches.ts@startmatches the first row,ts@90%matches the last 10%,ts@end+1matches nothing.| schema | MAX_TIMESTAMP | ts@start | ts@90% | ts@end+1 |
| -------- | ------------- | -------- | ------- | -------- |
| baseline | 209,704 | 781 | 90,562 | 101,981 |
| V30 | 592 | 590 | 95,251 | 103,372 |
| V31 | 681 | 643 | 3,999 | 469 |
batches_pkeyfor the lookup.ts@startis fast only because the first row matches.batches_pkey, so the two production-shaped cases barely move.ts@90%reads 40k entries andts@end+1reads none.ts@startwould read all 400k entries, so V31 first checks the oldest retained batch and skips the scan when it already qualifies.Storage and write cost
Same benchmark, 400k batches in one partition, local Postgres 17 container, one run per schema.
batchessizes in MB, and the index adds 59 bytes per row (22.5 MB at 400k rows, about 4.1 GB at 70M rows).| index or table | baseline (V29) | V30 / V31 |
| ------------------------------------- | -------------- | --------- |
| heap | 48.1 | 48.1 |
|
batches_pkey| 8.6 | 8.6 ||
batches_by_file| 3.8 | 3.8 ||
batches_by_last_offset_covering_idx| 36.7 | 36.7 ||
batches_by_timestamp_idx| - | 22.5 |The new index is 46% of the existing index total (49.1 MB) and 23% of the whole relation.
| write path | baseline (V29) | V30 / V31 |
| ----------------------------------- | -------------- | --------- |
| bulk commit, microseconds per batch | 45-51 | 44-45 |
| bulk commit, WAL bytes per batch | 456 | 559 |
| 10-batch commit, WAL bytes | 24,575 | 25,586 |
| 10-batch commit, milliseconds | 1.7-2.3 | 1.5-2.3 |
pg_indexes_size('batches')before and after on a staging copy.Related
Broker-side hardening for slow control-plane queries (lock removal and bounded pools) is #798. Neither PR depends on the other.