Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewind tuple store to fix scrollable with hold cursor fetches #7014

Merged
merged 1 commit into from Jun 19, 2023

Conversation

aykut-bozkurt
Copy link
Contributor

@aykut-bozkurt aykut-bozkurt commented Jun 19, 2023

We need to rewind the tuplestorestate's tuple index to get correct results on fetching scrollable with hold cursors.

PersistHoldablePortal is responsible for persisting out tuplestorestate inside a scrollable with hold cursor before commiting a transaction.

It rewinds the scroll like below (ExecutorRewindcalls calls rescan):

if (portal->cursorOptions & CURSOR_OPT_SCROLL)
{
  ExecutorRewind(queryDesc);
}

At the end, it adjusts tuple index for holdStore in the portal properly.

if (portal->cursorOptions & CURSOR_OPT_SCROLL)
{
         if (!tuplestore_skiptuples(portal->holdStore,
	                                         portal->portalPos,
	                                         true))
	    elog(ERROR, "unexpected end of tuple stream");
}
  • Add tests with different cursor options and verify results.

DESCRIPTION: Fixes incorrect results on fetching scrollable with hold cursors.

Fixes #7010

@aykut-bozkurt aykut-bozkurt marked this pull request as draft June 19, 2023 13:43
@aykut-bozkurt aykut-bozkurt force-pushed the scrollable-cursor-with-hold-fix branch from 7b15608 to c89a559 Compare June 19, 2023 14:16
@codecov
Copy link

codecov bot commented Jun 19, 2023

Codecov Report

Merging #7014 (62fcde8) into main (58da877) will decrease coverage by 0.24%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #7014      +/-   ##
==========================================
- Coverage   93.33%   93.10%   -0.24%     
==========================================
  Files         273      273              
  Lines       58469    58474       +5     
==========================================
- Hits        54572    54441     -131     
- Misses       3897     4033     +136     

@aykut-bozkurt aykut-bozkurt force-pushed the scrollable-cursor-with-hold-fix branch from c89a559 to 5f60985 Compare June 19, 2023 14:47
@aykut-bozkurt aykut-bozkurt marked this pull request as ready for review June 19, 2023 14:54
{ }
{
CitusScanState *scanState = (CitusScanState *) node;
if (scanState->tuplestorestate)
Copy link
Member

Choose a reason for hiding this comment

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

other, similar scan nodes in PG also have

    if (node->ss.ps.ps_ResultTupleSlot)
        ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);

    ExecScanReScan(&node->ss);

do we need that there too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. We need to free slot for the heap tuple. And ExecScanReScan must be called according to the comments on the function. (refer function's comments)

@aykut-bozkurt aykut-bozkurt force-pushed the scrollable-cursor-with-hold-fix branch from 5f60985 to 9b0f451 Compare June 19, 2023 16:44
@aykut-bozkurt aykut-bozkurt force-pushed the scrollable-cursor-with-hold-fix branch from 9b0f451 to 62fcde8 Compare June 19, 2023 19:46
@aykut-bozkurt aykut-bozkurt merged commit f667f14 into main Jun 19, 2023
77 checks passed
@aykut-bozkurt aykut-bozkurt deleted the scrollable-cursor-with-hold-fix branch June 19, 2023 20:00
aykut-bozkurt added a commit that referenced this pull request Jun 21, 2023
We need to rewind the tuplestorestate's tuple index to get correct
results on fetching scrollable with hold cursors.

`PersistHoldablePortal` is responsible for persisting out
tuplestorestate inside a with hold cursor before commiting a
transaction.

It rewinds the cursor like below (`ExecutorRewindcalls` calls `rescan`):
```c
if (portal->cursorOptions & CURSOR_OPT_SCROLL)
{
  ExecutorRewind(queryDesc);
}
```

At the end, it adjusts tuple index for holdStore in the portal properly.
```c
if (portal->cursorOptions & CURSOR_OPT_SCROLL)
{
         if (!tuplestore_skiptuples(portal->holdStore,
	                                         portal->portalPos,
	                                         true))
	    elog(ERROR, "unexpected end of tuple stream");
}
```

DESCRIPTION: Fixes incorrect results on fetching scrollable with hold
cursors.

Fixes #7010

(cherry picked from commit f667f14)
aykut-bozkurt added a commit that referenced this pull request Jun 21, 2023
We need to rewind the tuplestorestate's tuple index to get correct
results on fetching scrollable with hold cursors.

`PersistHoldablePortal` is responsible for persisting out
tuplestorestate inside a with hold cursor before commiting a
transaction.

It rewinds the cursor like below (`ExecutorRewindcalls` calls `rescan`):
```c
if (portal->cursorOptions & CURSOR_OPT_SCROLL)
{
  ExecutorRewind(queryDesc);
}
```

At the end, it adjusts tuple index for holdStore in the portal properly.
```c
if (portal->cursorOptions & CURSOR_OPT_SCROLL)
{
         if (!tuplestore_skiptuples(portal->holdStore,
	                                         portal->portalPos,
	                                         true))
	    elog(ERROR, "unexpected end of tuple stream");
}
```

DESCRIPTION: Fixes incorrect results on fetching scrollable with hold
cursors.

Fixes #7010

(cherry picked from commit f667f14)
aykut-bozkurt added a commit that referenced this pull request Jun 21, 2023
We need to rewind the tuplestorestate's tuple index to get correct
results on fetching scrollable with hold cursors.

`PersistHoldablePortal` is responsible for persisting out
tuplestorestate inside a with hold cursor before commiting a
transaction.

It rewinds the cursor like below (`ExecutorRewindcalls` calls `rescan`):
```c
if (portal->cursorOptions & CURSOR_OPT_SCROLL)
{
  ExecutorRewind(queryDesc);
}
```

At the end, it adjusts tuple index for holdStore in the portal properly.
```c
if (portal->cursorOptions & CURSOR_OPT_SCROLL)
{
         if (!tuplestore_skiptuples(portal->holdStore,
	                                         portal->portalPos,
	                                         true))
	    elog(ERROR, "unexpected end of tuple stream");
}
```

DESCRIPTION: Fixes incorrect results on fetching scrollable with hold
cursors.

Fixes #7010

(cherry picked from commit f667f14)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WITH HOLD cursors can give incorrect results
2 participants