From e2c88a66d7cde58aeab2317b041f1d04c60b0c09 Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Tue, 1 Dec 2026 14:28:43 +0900 Subject: [PATCH v1] Seek a spilled tuplestore from its start or end when nearer. In file states, tuplestore_seek() moves only by stepping over records one at a time from the read pointer's current position. It walks all the way to reach the end of the file, and walks back to reach a row near its start instead of going back to the start. The rows at both ends of the file have known positions: the file starts with row rowstrimmed, and the write position is just past the last row. So go straight to the write position when the target is the end or EOF, and otherwise start from the start of the file or its end when either is nearer than the current position. Stepping back from the end needs the extra length words written for backward scans. This mainly helps WindowAgg once a partition spills, for aggregates that are recomputed for each row by reading back to the frame head. With 4000 rows and work_mem = 64kB, max() over ROWS BETWEEN 2000 PRECEDING AND CURRENT ROW went from 1689 ms to 153 ms, and over ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING from 3278 ms to 995 ms. --- src/backend/utils/sort/tuplestore.c | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/tuplestore.c index d8a4d669727..36f31451ce2 100644 --- a/src/backend/utils/sort/tuplestore.c +++ b/src/backend/utils/sort/tuplestore.c @@ -1358,6 +1358,21 @@ tuplestore_seek(Tuplestorestate *state, int64 row) pg_fallthrough; case TSS_READFILE: + /* The end of the stored rows is the write position, so go there */ + if (row >= state->tuples) + { + if (!readptr->eof_reached && + BufFileSeek(state->myfile, + state->writepos_file, state->writepos_offset, + SEEK_SET) != 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not seek in tuplestore temporary file"))); + readptr->eof_reached = (row > state->tuples); + readptr->row = state->tuples; + return true; + } + /* We must seek the file to the correct tuple */ if (readptr->eof_reached) { @@ -1366,6 +1381,32 @@ tuplestore_seek(Tuplestorestate *state, int64 row) readptr->row = state->tuples; } + /* + * Start walking from the start of the file or its end instead, if + * either is nearer. Stepping back from the end needs the extra + * length words written for backward scans. + */ + if (row < readptr->row && + row - state->rowstrimmed < readptr->row - row) + { + if (BufFileSeek(state->myfile, 0, 0, SEEK_SET) != 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not seek in tuplestore temporary file"))); + readptr->row = state->rowstrimmed; + } + else if (row > readptr->row && state->backward && + state->tuples - row < row - readptr->row) + { + if (BufFileSeek(state->myfile, + state->writepos_file, state->writepos_offset, + SEEK_SET) != 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not seek in tuplestore temporary file"))); + readptr->row = state->tuples; + } + while (readptr->row < row) { /* This should fail only when we're advancing to "at EOF" */ -- 2.54.0 (Apple Git-157)