Skip to content

Use PostgreSQL tuple cursor for reverse history node pagination - #11778

Open
Lakshaymiddha wants to merge 7 commits into
temporalio:mainfrom
Lakshaymiddha:lakshay/postgres-tuple-cursor-reverse-nodes
Open

Lakshaymiddha wants to merge 7 commits into
temporalio:mainfrom
Lakshaymiddha:lakshay/postgres-tuple-cursor-reverse-nodes

Conversation

@Lakshaymiddha

@Lakshaymiddha Lakshaymiddha commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

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.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 because:

  • On the first page, MaxTxnID = math.MinInt64 + 1 (sentinel), so node_id = <huge negative> never matched any real row.
  • On subsequent pages, MaxTxnID = lastRow.TxnID (small positive int), so node_id = <txn_id> would only accidentally match if a node_id happened to equal a TxnID.

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_node table (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):

Cursor position Cursor form Rows removed by filter Shared buffers Execution time Speedup
Page 1 (start of reverse) OR expression 10 10 0.308 ms
Page 1 (start of reverse) (node_id, txn_id) < ($5, $6) 0 7 0.069 ms 4×
25% into reverse OR expression 250,010 17,055 95.421 ms
25% into reverse (node_id, txn_id) < ($5, $6) 0 11 0.121 ms 789×
50% into reverse OR expression 500,010 34,099 154.028 ms
50% into reverse (node_id, txn_id) < ($5, $6) 0 10 0.148 ms 1,041×
75% into reverse OR expression 750,010 51,147 167.345 ms
75% into reverse (node_id, txn_id) < ($5, $6) 0 12 0.135 ms 1,240×
99% into reverse OR expression 990,010 67,510 197.640 ms
99% into reverse (node_id, txn_id) < ($5, $6) 0 10 0.068 ms 2,907×

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?

  • built
  • run locally and tested manually
  • covered by existing tests (full PostgreSQL persistence suite — TestPQ and TestPGX)
  • added new persistence integration test(s)
go test ./common/persistence/tests -run '^TestPQ$/^TestPostgreSQLHistoryNodeSuite$' -count=1
go test ./common/persistence/tests -run '^TestPGX$/^TestPostgreSQLHistoryNodeSuite$' -count=1

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 gaps
  • TestInsertSelectReverse_AcrossNodes — verifies multi-node multi-txn reverse pagination returns all rows

Both 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.

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.
@Lakshaymiddha
Lakshaymiddha marked this pull request as ready for review August 25, 2026 15:58
@Lakshaymiddha
Lakshaymiddha requested a review from a team August 25, 2026 15:58
@Lakshaymiddha
Lakshaymiddha requested review from a team as code owners August 25, 2026 15:58

@Abhinav0905 Abhinav0905 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@Lakshaymiddha

Copy link
Copy Markdown
Contributor Author

Good catch @Abhinav0905
Addressed in 36442c8. Both tests now assert uniqueness and set equality on (NodeID, TxnID) pairs via a shared assertUniqueAndExpectedKeys helper, so a duplicate replacing a skipped row will fail both checks.

Lakshaymiddha and others added 2 commits September 3, 2026 12:28
- staticcheck ST1003: rename TestInsertSelectReverse_{CursorBoundary,MultiPageSameNode,AcrossNodes}
- testifylint: replace s.Equal(n, len(...)) with s.Len(..., n)
@Lakshaymiddha
Lakshaymiddha force-pushed the lakshay/postgres-tuple-cursor-reverse-nodes branch from 9a8a7c4 to f7436b6 Compare September 3, 2026 11:19
Lakshaymiddha and others added 3 commits September 16, 2026 10:49
…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.
@Lakshaymiddha

Copy link
Copy Markdown
Contributor Author

@ratdaddy Would appreciate a review when you get a chance.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants