| From: | Henson Choi <assam258(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers <pgsql-hackers(at)postgresql(dot)org> |
| Subject: | Speed up tuplestore_seek() by starting from the nearest known position |
| Date: | 2026-12-01 06:05:03 |
| Message-ID: | CAAAe_zD5Ti_C-8LEK6=v3QDUu9YYWMmM+y1CBk3pOyYEm=jZSw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
The fix for BUG #19911 made tuplestore track absolute row numbers and
added tuplestore_seek() [1]. In file states, though,
tuplestore_seek() still moves only by stepping one record at a time
from the 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. The attached patch goes straight to the write position
when the target is the end or EOF, and otherwise starts walking from
the start or the end of the file when either is nearer than the
current position.
This mainly helps aggregates that are recomputed for each row by
reading back to the frame head once the partition spills. For
example:
SET work_mem = '64kB';
SELECT count(x) FROM
(SELECT max(g) OVER (ORDER BY g ROWS BETWEEN 2000 PRECEDING
AND CURRENT ROW) AS x
FROM generate_series(1, 4000) g) s;
takes 1689 ms before and 153 ms after, on release builds (median of
three runs). Other max() frames that read far back into the spilled
partition improved by 2.6 to 11 times. Other functions and frames
showed no difference in time, and all results were the same as
without the patch.
The index of block-starting tuples Tom mentioned in the #19911
review would cut the walking down much further [2]. This patch is a
smaller step that needs no change to the storage format, and the
index can be built on top of it. Since seek already goes to the
nearest known position and walks from there, the index need not
cover every tuple; keeping positions for only some of them is enough,
which keeps the index small.
[1] https://postgr.es/m/19911-991c431cc0ba50d1@postgresql.org
[2] https://postgr.es/m/1016045.1795541071@sss.pgh.pa.us
Best regards,
Henson
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Seek-a-spilled-tuplestore-from-its-start-or-end-w.patch | application/octet-stream | 3.3 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Shlok Kyal | 2026-12-01 06:21:59 | Re: Preserve conflict log table during upgrade |
| Previous Message | Chao Li | 2026-12-01 05:51:18 | Re: Should we add timing output to pg_upgrade? |