Skip to content

Use PostgreSQL tuple cursor for scheduled task pagination - #11746

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

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

Conversation

@Lakshaymiddha

@Lakshaymiddha Lakshaymiddha commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Part of #11743.

What changed?

PostgreSQL forward pagination for history scheduled tasks now uses a row-value comparison instead of an OR-based composite cursor expression.

Why?

The existing OR expression in getHistoryScheduledTasksQuery is logically correct but PostgreSQL does not recognize it as a composite index lower bound. Later pages rescan and filter all rows preceding the cursor. Row-value comparisons preserve the same page boundaries while allowing PostgreSQL to start the index scan at the cursor.

On PostgreSQL 13.5 with a synthetic 1,000,000-row history_scheduled_tasks table, using the default page size of 100 (TimerTaskBatchSize, constants.go#L2159):

Cursor position Cursor form Rows removed by filter Shared buffers Execution time Speedup
Page 1 (start) OR expression 0 5 0.187 ms
Page 1 (start) (visibility_timestamp, task_id) >= ($3, $4) 0 5 0.057 ms 3×
25% OR expression 250,000 4,808 148.864 ms
25% (visibility_timestamp, task_id) >= ($3, $4) 0 6 0.159 ms 936×
50% OR expression 500,000 9,611 99.958 ms
50% (visibility_timestamp, task_id) >= ($3, $4) 0 6 0.140 ms 714×
75% OR expression 750,000 14,414 150.051 ms
75% (visibility_timestamp, task_id) >= ($3, $4) 0 6 0.147 ms 1,021×
99% (near end) OR expression 989,600 19,017 128.000 ms
99% (near end) (visibility_timestamp, task_id) >= ($3, $4) 0 6 0.118 ms 1,085×

The OR expression degrades linearly with cursor depth — every page rescans all preceding rows. The tuple cursor maintains constant ~6 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$/^TestPostgreSQLHistoryScheduledTaskSuite$' -count=1
go test ./common/persistence/tests -run '^TestPGX$/^TestPostgreSQLHistoryScheduledTaskSuite$' -count=1

New test coverage for cursor boundary conditions:

  • TestInsertSelect_CursorRowIncluded — verifies >= semantics (cursor row is returned)
  • TestInsertSelect_CursorExcludesPriorRows — verifies prior rows at same timestamp are excluded
  • TestInsertSelect_UpperBoundExclusive — verifies upper bound uses strict <
  • TestInsertSelect_CrossTimestampCursor — verifies later timestamps include all taskIDs
  • TestInsertSelect_MultiplePages — verifies no rows lost or duplicated across pages
  • TestInsertSelect_PageBoundarySplitsSameTimestamp — verifies page boundary within same timestamp

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.

Replace the OR-based composite cursor in getHistoryScheduledTasksQuery
with a row-value comparison. The OR expression is logically correct but
PostgreSQL does not recognize it as a composite index bound, causing
every page to rescan all preceding index entries.

Row-value comparison allows PostgreSQL to seek directly to the cursor
position in the B-tree index.
@CLAassistant

CLAassistant commented Aug 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Verifies no rows are lost or duplicated when a page boundary falls
in the middle of tasks sharing the same visibility timestamp.
@Lakshaymiddha
Lakshaymiddha force-pushed the lakshay/postgres-tuple-cursor-scheduled-tasks branch from 0556b80 to 0e7db21 Compare August 24, 2026 17:40
@Lakshaymiddha
Lakshaymiddha marked this pull request as ready for review August 25, 2026 15:57
@Lakshaymiddha
Lakshaymiddha requested a review from a team August 25, 2026 15:57
@Lakshaymiddha
Lakshaymiddha requested review from a team as code owners August 25, 2026 15:57

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

I reviewed the PostgreSQL query change, parameter renumbering, and the new boundary tests. The tuple comparison preserves the inclusive cursor semantics, the six arguments line up with the updated placeholders, and the multi-page cases compare the exact ordered rows across same-timestamp boundaries. I did not find a code issue in this patch.

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

4 participants