From 0d36978783a4dc642b42c394a6348eab28e4a3dc Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Mon, 23 Nov 2026 17:59:54 +0900 Subject: [PATCH v7 5/9] 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. The reimplementation on top of tuplestore_seek() broke it in three ways: * Backward from EOF, the first step goes from EOF onto the last tuple, as a backward gettuple at EOF returns the last tuple. The reimplementation did not count that step, so it went one tuple too far. A scroll cursor over Material or CteScan that turned around at EOF could return wrong rows. * It left EOF by clearing eof_reached by hand. In WRITEFILE state the file position of a pointer at EOF is not maintained, so the next seek could start from a stale position and fail an assertion or raise "could not seek in tuplestore temporary file". Let tuplestore_seek() leave EOF instead. * Running out of tuples returned true when ntuples was exactly one more than the tuples available, in either direction. The original code returned true only if all ntuples steps landed on a tuple. Forward, this made PersistHoldablePortal() miss a short re-execution. Also restore the EXEC_FLAG_BACKWARD assertion that the reimplementation dropped. --- src/backend/utils/sort/tuplestore.c | 34 +++---- src/test/regress/expected/portals.out | 130 ++++++++++++++++++++++++++ src/test/regress/sql/portals.sql | 68 ++++++++++++++ 3 files changed, 215 insertions(+), 17 deletions(-) diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/tuplestore.c index 7cabd0245b2..a1609f67b38 100644 --- a/src/backend/utils/sort/tuplestore.c +++ b/src/backend/utils/sort/tuplestore.c @@ -83,10 +83,11 @@ typedef enum * eflags shows the capability flags required for this pointer (which can * be different from one pointer to another). * - * eof_reached is set true when a forward read fails due to reaching EOF. - * While it is true, the pointer's read position is implicitly equal to the - * write position, and we do not maintain row/current/file/offset. This - * rule saves us from having to update read pointers each time we write. + * eof_reached is set true when the pointer is moved to EOF, as by a + * forward read that fails there. While it is true, the pointer's read + * position is implicitly equal to the write position, and we do not + * maintain row/current/file/offset. This rule saves us from having to + * update read pointers each time we write. * * row is the absolute row number of the next record to be read by this * pointer, counting from 0 as the first record inserted after tuplestore @@ -1246,6 +1247,8 @@ tuplestore_skiptuples(Tuplestorestate *state, int64 ntuples, bool forward) TSReadPointer *readptr = &state->readptrs[state->activeptr]; int64 limit; + Assert(forward || (readptr->eflags & EXEC_FLAG_BACKWARD)); + if (ntuples <= 0) return true; @@ -1253,30 +1256,27 @@ tuplestore_skiptuples(Tuplestorestate *state, int64 ntuples, bool 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); - } + /* limit is the distance to EOF; reaching EOF means running out */ + limit = (state->tuples + 1) - readptr->row; + if (!tuplestore_seek(state, readptr->row + Min(limit, ntuples))) + return false; + return (ntuples < limit); } else { if (readptr->eof_reached) { - /* Return to the state of being just past the last tuple */ - readptr->eof_reached = false; - readptr->row = state->tuples; + /* The first step goes from EOF onto the last tuple */ + (void) tuplestore_seek(state, state->tuples); + ntuples--; } + /* limit is the distance to the start; reaching it means running out */ limit = readptr->row - 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); + return (ntuples < limit); } } diff --git a/src/test/regress/expected/portals.out b/src/test/regress/expected/portals.out index a66f27e3694..3aa6e0dd389 100644 --- a/src/test/regress/expected/portals.out +++ b/src/test/regress/expected/portals.out @@ -1547,6 +1547,136 @@ 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) + +-- Back onto the start of the file +move absolute 1 in c4; +fetch backward 1 in c4; + g +--- +(0 rows) + +fetch 1 in c4; + g +--- + 1 +(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; +-- Same with the hold store spilled to a file +create sequence portal_hold_seq2; +begin; +set local work_mem = '64kB'; +declare c7 scroll cursor with hold for + select g from generate_series(1,5000) g where nextval('portal_hold_seq2') <= 6000; +move 3000 in c7; +commit; +fetch backward 1 in c7; + g +------ + 2999 +(1 row) + +fetch 1 in c7; + g +------ + 3000 +(1 row) + +fetch 1 in c7; + g +--- +(0 rows) + +close c7; +alter sequence portal_hold_seq2 restart; +begin; +set local work_mem = '64kB'; +declare c8 scroll cursor with hold for + select g from generate_series(1,5000) g where nextval('portal_hold_seq2') <= 5999; +move 3000 in c8; +commit; +ERROR: unexpected end of tuple stream +drop sequence portal_hold_seq2; -- 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..611fb684304 100644 --- a/src/test/regress/sql/portals.sql +++ b/src/test/regress/sql/portals.sql @@ -587,6 +587,74 @@ 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; +-- Back onto the start of the file +move absolute 1 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; + +-- Same with the hold store spilled to a file +create sequence portal_hold_seq2; +begin; +set local work_mem = '64kB'; +declare c7 scroll cursor with hold for + select g from generate_series(1,5000) g where nextval('portal_hold_seq2') <= 6000; +move 3000 in c7; +commit; +fetch backward 1 in c7; +fetch 1 in c7; +fetch 1 in c7; +close c7; +alter sequence portal_hold_seq2 restart; +begin; +set local work_mem = '64kB'; +declare c8 scroll cursor with hold for + select g from generate_series(1,5000) g where nextval('portal_hold_seq2') <= 5999; +move 3000 in c8; +commit; +drop sequence portal_hold_seq2; + -- Check fetching of toasted datums via cursors. begin;