From 910c85cf16e50cccd9c76e7ac4a847c6a522008e Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Tue, 1 Dec 2026 23:54:27 +0900 Subject: [PATCH v2 2/3] Skip rows without fetching them when moving a held cursor. MOVE, and FETCH ABSOLUTE or RELATIVE on their way to the requested row, run a portal with DestNone. When the portal's result is held in a tuplestore, RunFromStore() still fetched every skipped row from the store and threw it away. Moving backward through a spilled store is especially slow that way, since each step backs up over the record's length words before reading it. Use tuplestore_skiptuples() instead, which moves the read position without fetching anything. With the position index from the previous commit, that costs about one seek even after the store has spilled. The count returned is the number of rows the fetch loop would have returned: running out of rows costs the loop one failed fetch when moving backward, but none when moving forward. Discussion: https://postgr.es/m/CAAAe_zD5Ti_C-8LEK6=v3QDUu9YYWMmM+y1CBk3pOyYEm=jZSw@mail.gmail.com --- src/backend/tcop/pquery.c | 34 ++++++++++++++ src/test/regress/expected/portals.out | 66 +++++++++++++++++++++++++++ src/test/regress/sql/portals.sql | 41 +++++++++++++++++ 3 files changed, 141 insertions(+) diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c index 98205aafb05..fa72c295ca8 100644 --- a/src/backend/tcop/pquery.c +++ b/src/backend/tcop/pquery.c @@ -1170,6 +1170,40 @@ RunFromStore(Portal portal, ScanDirection direction, uint64 count, { /* do nothing except start/stop the destination */ } + else if (dest->mydest == DestNone) + { + /* + * Nothing to send, so move the read position without fetching rows, + * and count the rows a fetch loop would have returned. + */ + bool forward = ScanDirectionIsForward(direction); + MemoryContext oldcontext; + int64 total; + int64 before; + bool more; + + oldcontext = MemoryContextSwitchTo(portal->holdContext); + total = tuplestore_tuple_count(portal->holdStore); + before = tuplestore_tell(portal->holdStore); + more = tuplestore_skiptuples(portal->holdStore, + count ? (int64) count : PG_INT64_MAX, + forward); + MemoryContextSwitchTo(oldcontext); + + if (more) + current_tuple_count = count; + else if (forward) + { + /* ran to EOF, which tell() reports as total + 1 */ + current_tuple_count = total - Min(before, total); + } + else + { + /* ran back to the start; the last step returned no row */ + Assert(tuplestore_tell(portal->holdStore) == 0); + current_tuple_count = Max(before - 1, 0); + } + } else { bool forward = ScanDirectionIsForward(direction); diff --git a/src/test/regress/expected/portals.out b/src/test/regress/expected/portals.out index d521c50aeef..d3b94c2adc1 100644 --- a/src/test/regress/expected/portals.out +++ b/src/test/regress/expected/portals.out @@ -818,6 +818,72 @@ SELECT name, statement, is_holdable, is_binary, is_scrollable FROM pg_cursors; (1 row) CLOSE foo25ns; +-- MOVE and FETCH positioning in a holdable cursor, including past either end +BEGIN; +DECLARE foo27 SCROLL CURSOR WITH HOLD FOR SELECT i FROM generate_series(1, 10) i; +COMMIT; +MOVE FORWARD 3 IN foo27; +FETCH FROM foo27; + i +--- + 4 +(1 row) + +MOVE FORWARD 20 IN foo27; +\echo :ROW_COUNT +6 +FETCH BACKWARD FROM foo27; + i +---- + 10 +(1 row) + +MOVE BACKWARD 4 IN foo27; +FETCH FROM foo27; + i +--- + 7 +(1 row) + +MOVE BACKWARD 20 IN foo27; +\echo :ROW_COUNT +6 +FETCH FROM foo27; + i +--- + 1 +(1 row) + +MOVE FORWARD ALL IN foo27; +\echo :ROW_COUNT +9 +MOVE BACKWARD ALL IN foo27; +\echo :ROW_COUNT +10 +FETCH ABSOLUTE 7 FROM foo27; + i +--- + 7 +(1 row) + +FETCH RELATIVE -5 FROM foo27; + i +--- + 2 +(1 row) + +FETCH ABSOLUTE 20 FROM foo27; + i +--- +(0 rows) + +FETCH RELATIVE -3 FROM foo27; + i +--- + 8 +(1 row) + +CLOSE foo27; -- -- ROLLBACK should close holdable cursors -- diff --git a/src/test/regress/sql/portals.sql b/src/test/regress/sql/portals.sql index 7d29e9de319..be86ccc401c 100644 --- a/src/test/regress/sql/portals.sql +++ b/src/test/regress/sql/portals.sql @@ -249,6 +249,47 @@ SELECT name, statement, is_holdable, is_binary, is_scrollable FROM pg_cursors; CLOSE foo25ns; +-- MOVE and FETCH positioning in a holdable cursor, including past either end +BEGIN; + +DECLARE foo27 SCROLL CURSOR WITH HOLD FOR SELECT i FROM generate_series(1, 10) i; + +COMMIT; + +MOVE FORWARD 3 IN foo27; + +FETCH FROM foo27; + +MOVE FORWARD 20 IN foo27; +\echo :ROW_COUNT + +FETCH BACKWARD FROM foo27; + +MOVE BACKWARD 4 IN foo27; + +FETCH FROM foo27; + +MOVE BACKWARD 20 IN foo27; +\echo :ROW_COUNT + +FETCH FROM foo27; + +MOVE FORWARD ALL IN foo27; +\echo :ROW_COUNT + +MOVE BACKWARD ALL IN foo27; +\echo :ROW_COUNT + +FETCH ABSOLUTE 7 FROM foo27; + +FETCH RELATIVE -5 FROM foo27; + +FETCH ABSOLUTE 20 FROM foo27; + +FETCH RELATIVE -3 FROM foo27; + +CLOSE foo27; + -- -- ROLLBACK should close holdable cursors -- -- 2.54.0 (Apple Git-157)