Repository navigation
Use PostgreSQL tuple cursor for scheduled task pagination - #11746
Open
Lakshaymiddha wants to merge 7 commits into
Open
Lakshaymiddha wants to merge 7 commits into
Lakshaymiddha wants to merge 7 commits into
Conversation
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.
Verifies no rows are lost or duplicated when a page boundary falls in the middle of tasks sharing the same visibility timestamp.
Lakshaymiddha
force-pushed
the
lakshay/postgres-tuple-cursor-scheduled-tasks
branch
from
August 24, 2026 17:40
0556b80 to
0e7db21
Compare
2 tasks
Lakshaymiddha
marked this pull request as ready for review
August 25, 2026 15:57
Abhinav0905
approved these changes
Aug 26, 2026
Abhinav0905
left a comment
There was a problem hiding this comment.
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.
- staticcheck ST1003: rename TestInsertSelect_{CursorRowIncluded,CursorExcludesPriorRows,UpperBoundExclusive,CrossTimestampCursor,MultiplePages,PageBoundarySplitsSameTimestamp}
- testifylint: replace s.Equal(n, len(...)) with s.Len(..., n)
Contributor
Author
|
@ratdaddy Would appreciate a review when you get a chance. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
getHistoryScheduledTasksQueryis 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_taskstable, using the default page size of 100 (TimerTaskBatchSize, constants.go#L2159):(visibility_timestamp, task_id) >= ($3, $4)(visibility_timestamp, task_id) >= ($3, $4)(visibility_timestamp, task_id) >= ($3, $4)(visibility_timestamp, task_id) >= ($3, $4)(visibility_timestamp, task_id) >= ($3, $4)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?
New test coverage for cursor boundary conditions:
TestInsertSelect_CursorRowIncluded— verifies >= semantics (cursor row is returned)TestInsertSelect_CursorExcludesPriorRows— verifies prior rows at same timestamp are excludedTestInsertSelect_UpperBoundExclusive— verifies upper bound uses strict <TestInsertSelect_CrossTimestampCursor— verifies later timestamps include all taskIDsTestInsertSelect_MultiplePages— verifies no rows lost or duplicated across pagesTestInsertSelect_PageBoundarySplitsSameTimestamp— verifies page boundary within same timestampBoth 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.