From f68446e82ea66ad99c059c5238e9e23d64ee157f Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Mon, 23 Nov 2026 17:59:54 +0900 Subject: [PATCH v5 5/7] Fix tuplestore_skiptuples() at the boundaries. The contract of tuplestore_skiptuples() is that it is the same as ntuples calls to tuplestore_gettuple(), and Material, CteScan and PersistHoldablePortal() rely on it. EOF is a virtual position past the last tuple; a backward gettuple at EOF steps from it onto the last tuple and returns that tuple. In terms of tuplestore_tell(), that step goes from tuples + 1 to tuples. The reimplementation on top of tuplestore_seek() broke this contract. Backward, it started as if EOF were already on the last tuple, so it went one tuple too far, and it started from current and the file position, which are not maintained at EOF. A scroll cursor over Material or CteScan that turned around at EOF could then return wrong rows, fail an assertion, or raise "could not seek in tuplestore temporary file". Forward, a skip of one more than the remaining tuples returned true, so PersistHoldablePortal() missed a short re-execution. Start from tuplestore_tell(), count as the contract says, and leave the EOF state to tuplestore_seek(). When there are not enough tuples, go to EOF or to the lowest position tuplestore_seek() accepts, and return false, as before. Bug: #19911 Author: Henson Choi Discussion: https://postgr.es/m/19911-991c431cc0ba50d1@postgresql.org Backpatch-through: 15 --- src/backend/utils/sort/tuplestore.c | 47 ++++++++------- src/test/regress/expected/portals.out | 82 +++++++++++++++++++++++++++ src/test/regress/sql/portals.sql | 43 ++++++++++++++ 3 files changed, 151 insertions(+), 21 deletions(-) diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/tuplestore.c index 8eaddcf8d93..c49e00346c8 100644 --- a/src/backend/utils/sort/tuplestore.c +++ b/src/backend/utils/sort/tuplestore.c @@ -1232,39 +1232,44 @@ bool tuplestore_skiptuples(Tuplestorestate *state, int64 ntuples, bool forward) { TSReadPointer *readptr = &state->readptrs[state->activeptr]; - int64 limit; + int64 cur; + int64 lowest; + + Assert(forward || (readptr->eflags & EXEC_FLAG_BACKWARD)); if (ntuples <= 0) return true; + /* + * Act like ntuples calls of tuplestore_gettuple(). EOF is tell() = + * tuples + 1, and the first backward step goes from it onto the last + * tuple. + */ + cur = tuplestore_tell(state); + if (forward) { if (readptr->eof_reached) return false; - else - { - limit = state->tuples - readptr->row + 1; - /* If ntuples > limit, return false after advancing to EOF */ - if (!tuplestore_seek(state, readptr->row + Min(limit, ntuples))) - return false; - return (ntuples <= limit); - } + if (ntuples <= state->tuples - cur) + return tuplestore_seek(state, cur + ntuples); + /* Ran out of tuples: go to EOF */ + (void) tuplestore_seek(state, state->tuples + 1); + return false; } else { - if (readptr->eof_reached) - { - /* Return to the state of being just past the last tuple */ - readptr->eof_reached = false; - readptr->row = state->tuples; - } - limit = readptr->row - state->rowstrimmed; + lowest = state->rowstrimmed; if (state->status == TSS_INMEM) - limit -= state->memtupdeleted; - /* If ntuples > limit, return false after moving to start */ - if (!tuplestore_seek(state, readptr->row - Min(limit, ntuples))) - return false; - return (ntuples <= limit); + lowest += state->memtupdeleted; + if (ntuples < cur - lowest) + return tuplestore_seek(state, cur - ntuples); + /* Ran out of tuples: go to the lowest position seek accepts */ + Assert(!state->truncated); + if (state->status == TSS_INMEM && state->truncated) + lowest++; + (void) tuplestore_seek(state, lowest); + return false; } } diff --git a/src/test/regress/expected/portals.out b/src/test/regress/expected/portals.out index a66f27e3694..990148c363f 100644 --- a/src/test/regress/expected/portals.out +++ b/src/test/regress/expected/portals.out @@ -1547,6 +1547,88 @@ fetch backward all in c2; (3 rows) rollback; +-- Turn around at tuplestore EOF before the Materialize input is done +begin; +declare c3 scroll cursor for select generate_series(1,5) as g; +fetch 3 in c3; + g +--- + 1 + 2 + 3 +(3 rows) + +fetch backward 1 in c3; + g +--- + 2 +(1 row) + +fetch backward 1 in c3; + g +--- + 1 +(1 row) + +fetch 1 in c3; + g +--- + 2 +(1 row) + +rollback; +-- Same with the tuplestore spilled to a file while it is written +begin; +set local work_mem = '64kB'; +declare c4 scroll cursor for select generate_series(1,5000) as g; +move 3000 in c4; +fetch backward 1 in c4; + g +------ + 2999 +(1 row) + +fetch 1 in c4; + g +------ + 3000 +(1 row) + +rollback; +-- Same through a CTE reader whose file position was saved for a SubPlan reader +begin; +set local work_mem = '64kB'; +declare c5 scroll cursor for + with c as materialized + (select g, repeat('x', 1000) as pad from generate_series(1, 200) g) + select x.g, + case when x.g <= 50 then + (select y.g from c y where y.g = x.g + 1 limit 1) + end as ahead + from c x; +move 100 in c5; +fetch backward 1 in c5; + g | ahead +----+------- + 99 | +(1 row) + +fetch 1 in c5; + g | ahead +-----+------- + 100 | +(1 row) + +rollback; +-- A held cursor whose re-execution returns one row fewer than its position +create sequence portal_hold_seq; +begin; +declare c6 scroll cursor with hold for + select g from generate_series(1,10) g where nextval('portal_hold_seq') <= 7; +move 4 in c6; +commit; +ERROR: unexpected end of tuple stream +drop sequence portal_hold_seq; -- Check fetching of toasted datums via cursors. begin; -- Other compression algorithms may cause the compressed data to be stored diff --git a/src/test/regress/sql/portals.sql b/src/test/regress/sql/portals.sql index 196b862c756..ef4edd9b06c 100644 --- a/src/test/regress/sql/portals.sql +++ b/src/test/regress/sql/portals.sql @@ -587,6 +587,49 @@ fetch all in c2; fetch backward all in c2; rollback; +-- Turn around at tuplestore EOF before the Materialize input is done +begin; +declare c3 scroll cursor for select generate_series(1,5) as g; +fetch 3 in c3; +fetch backward 1 in c3; +fetch backward 1 in c3; +fetch 1 in c3; +rollback; + +-- Same with the tuplestore spilled to a file while it is written +begin; +set local work_mem = '64kB'; +declare c4 scroll cursor for select generate_series(1,5000) as g; +move 3000 in c4; +fetch backward 1 in c4; +fetch 1 in c4; +rollback; + +-- Same through a CTE reader whose file position was saved for a SubPlan reader +begin; +set local work_mem = '64kB'; +declare c5 scroll cursor for + with c as materialized + (select g, repeat('x', 1000) as pad from generate_series(1, 200) g) + select x.g, + case when x.g <= 50 then + (select y.g from c y where y.g = x.g + 1 limit 1) + end as ahead + from c x; +move 100 in c5; +fetch backward 1 in c5; +fetch 1 in c5; +rollback; + +-- A held cursor whose re-execution returns one row fewer than its position +create sequence portal_hold_seq; +begin; +declare c6 scroll cursor with hold for + select g from generate_series(1,10) g where nextval('portal_hold_seq') <= 7; +move 4 in c6; +commit; +drop sequence portal_hold_seq; + -- Check fetching of toasted datums via cursors. begin; -- 2.54.0 (Apple Git-157)