| From: | Henson Choi <assam258(at)gmail(dot)com> |
|---|---|
| To: | Nitin Motiani <nitinmotiani(at)google(dot)com> |
| Cc: | pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
| Subject: | Re: Speed up tuplestore_seek() by starting from the nearest known position |
| Date: | 2026-12-03 16:46:04 |
| Message-ID: | CAAAe_zCqxF+h0QQg1KWi-Ohp9nZqkNJj-bAfJVFLcfb7dB=9Lg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Nitin,
Thanks for looking at this.
> EXPLAIN ANALYZE wouldn't show this extra memory. Is that ok? Or should
> we count these in the stats?
As the v2 mail said, v2 goes past work_mem during the dump. v3 avoids
that, so the memory EXPLAIN reports at the spill stays the peak, and
the stats need no change. On 64-bit builds the index takes over the
memtuples array and writes each position into the slot of a tuple the
dump has already written and freed. On 32-bit builds a 4-byte pointer
slot cannot hold a 64-bit position, so the index gets a small array of
its own and may go over by about 64 bytes, once, for the first entry,
but few people build for 32 bits these days.
> Would it be simpler to change the cost function to only pick the
> backward walk when it benefits?
That would fix the choice inside the seek, but the cost would stay
for other backward reads, such as a scrollable cursor fetching
backward. v3 adds a new function, BufFilePeekBackward(), which reads
the bytes before the position and refills the buffer with the block
that ends there, so reading backward through rows smaller than a
block reads the file once per block, as reading forward does.
Backward steps then cost about as much as forward ones, so counting
steps, as v2 already does, is enough. Along with that, v3 improves
how BufFile does its I/O and separates its read and write buffers.
I am about to post v3. This is how tuplestore and BufFile change:
- tuplestore keeps the position of every N-th row in memory, charged
to work_mem, and the seek starts from the nearest known position.
- BufFile reads backward through its buffer, and reads of a block or
more go straight into the caller's memory.
- BufFile gets separate buffers for reading and writing, so reads and
writes that alternate on one file no longer drop each other's block.
A file that is both read and written holds two buffers; one only
written or only read keeps one.
- Buffered writes never cross a block boundary, so after a seek only
the first one is partial, and writes of two blocks or more at a
block boundary skip the buffer.
- A write after seeking past the end leaves a hole that reads as
zeros, even when it starts in a later segment.
- New functions take or return a position as one 64-bit offset; the
old ones remain as wrappers.
On top of that, the callers change:
- logtape.c leaves the holes instead of writing zero blocks into them,
which about halves the temporary blocks a spilled hash aggregate
writes.
- Hash join ends writing its batch files before reading them back, so
each keeps only one buffer.
- The workaround for interleaved reads and writes in nodeWindowAgg.c
goes.
- MOVE and FETCH ABSOLUTE or RELATIVE on a held cursor skip rows
instead of fetching each one.
- The callers in core use the 64-bit position functions.
A test module, run only through PG_TEST_EXTRA, checks BufFile against
an in-memory model and a spilled tuplestore against one that stays in
memory, and times most of the cases above, so each step can be
measured against master.
Best regards,
Henson
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Ayoub Kazar | 2026-12-03 16:44:37 | Should we retain spilled hash tables across rescans? |