Repository navigation
Use PostgreSQL tuple cursor for reverse history node pagination - #11778
Lakshaymiddha wants to merge 7 commits into
Conversation
Replace the OR-based composite cursor in getHistoryNodesReverseQuery with a row-value comparison. This fixes two issues: 1. Bug: the reverse query arg list passed filter.MaxTxnID for the node_id = $5 comparison instead of filter.MaxNodeID, comparing node_id against a txn_id value. The first OR branch was effectively dead code in most cases. 2. Optimization: the OR expression prevents PostgreSQL from using the composite index as an upper bound, causing every page to scan and filter all following index entries. Row-value comparison fixes both: it uses the correct MaxNodeID value and allows PostgreSQL to seek directly to the cursor position.
Abhinav0905
left a comment
There was a problem hiding this comment.
Thanks for splitting this out. The tuple comparison and corrected argument binding look right to me.
One test gap: TestInsertSelectReverse_MultiPageSameNode and TestInsertSelectReverse_AcrossNodes only assert len(allRows). A duplicate row replacing a skipped row would still satisfy those assertions. Could you compare the exact ordered (NodeID, TxnID) pairs, or assert uniqueness plus set equality, so the tests prove there are no gaps or duplicates across page boundaries?
Strengthen TestInsertSelectReverse_MultiPageSameNode and TestInsertSelectReverse_AcrossNodes to verify that returned (NodeID, TxnID) pairs are unique and match the expected set, not just the count.
|
Good catch @Abhinav0905 |
- staticcheck ST1003: rename TestInsertSelectReverse_{CursorBoundary,MultiPageSameNode,AcrossNodes}
- testifylint: replace s.Equal(n, len(...)) with s.Len(..., n)
9a8a7c4 to
f7436b6
Compare
…ueries Same fix as PostgreSQL: replace OR-based composite cursor with (node_id, txn_id) < (?, ?) and fix the argument binding that passed MaxTxnID for the node_id comparison.
|
@ratdaddy Would appreciate a review when you get a chance. |
Part of #11743.
What changed?
PostgreSQL reverse pagination for history nodes now uses a row-value comparison instead of an OR-based composite cursor expression. This also fixes a pre-existing bug in the argument binding.
Why?
Bug fix: The reverse query arg list passed
filter.MaxTxnIDfor thenode_id = $5comparison instead offilter.MaxNodeID— comparingnode_idagainst a
txn_idvalue. The first OR branch was effectively dead code because:MaxTxnID = math.MinInt64 + 1(sentinel), sonode_id = <huge negative>never matched any real row.MaxTxnID = lastRow.TxnID(small positive int), sonode_id = <txn_id>would only accidentally match if anode_idhappened to equal aTxnID.Optimization: The OR expression prevents PostgreSQL from using the composite index as an upper bound. Later pages in reverse pagination scan and filter all following index entries. Row-value comparison allows PostgreSQL to seek directly to the cursor position.
On PostgreSQL 13.5 with a synthetic 1,000,000-row
history_nodetable (100k node_ids × 10 txn_ids), using the default page size of 100 (used across NDC, replication, and archival callers — ndc/constants.go#L4, sync_state_retriever.go#L34):(node_id, txn_id) < ($5, $6)(node_id, txn_id) < ($5, $6)(node_id, txn_id) < ($5, $6)(node_id, txn_id) < ($5, $6)(node_id, txn_id) < ($5, $6)The OR expression degrades linearly with pagination depth — every page rescans all rows between the cursor and the end of the index. The tuple cursor maintains constant ~10 shared buffers and sub-millisecond execution regardless of position.
How did you test it?
New test coverage for reverse cursor boundary conditions:
TestInsertSelectReverse_CursorBoundary— verifies strict < semantics (cursor row excluded, prior rows included)TestInsertSelectReverse_MultiPageSameNode— verifies page boundary splits within same node_id produce no duplicates or gapsTestInsertSelectReverse_AcrossNodes— verifies multi-node multi-txn reverse pagination returns all rowsBoth PostgreSQL driver suites (PQ and PGX) passed against PostgreSQL 13.5.
Potential risks
The query change is PostgreSQL-specific. MySQL and SQLite queries are unchanged.
The bug fix changes effective query behavior for the reverse path — previously the
txn_id boundary at the cursor node was not applied, now it is.