| From: | Henson Choi <assam258(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers <pgsql-hackers(at)postgresql(dot)org> |
| Cc: | 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-02 22:01:37 |
| Message-ID: | CAAAe_zAD+6ZR9S1Z0Jw5dWy9Jhjb_Egjymf7+JUtKvEjemW76Q@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
I found a problem in v2 while testing it further.
> - If the file has the length words for backward scans, it jumps to
> the next entry instead when walking back from it takes fewer steps.
...
> - Counting the jump as one step, it walks from where the read pointer
> already is when that takes no more steps. A backward walk loses a
> tie, as its steps cost more.
"Cost more" understates it by far. A seek to before the start of
the BufFile buffer refills it from there, so every backward step
reads a whole buffer, while forward steps stay in it. With the file
in the OS cache, a backward step cost about 40 times a forward one.
record [len|body|len] the last len is for backward scans
forward
buffer [ r100 | r101 | r102 | ... | r259 ] one read
-> -> -> -> in memory
backward
file ... | r98 |t| r99 |t| r100 | r101 | ...
^ r99's trailing len
buffer [ r100 | r101 | ...
step 1 refill from t: [t| r100 | r101 | ... read 1
then to r99's start, before the buffer again
step 2 [t| r99 |t| r100 | ... read 2
and so on, one read per step
A backward walk thus gets no benefit from the buffer at all, and
only forces extra reads that are not needed.
So walking back from the next entry does not pay off as v2 assumes.
What is needed is to make a backward step cheap: when a backward
read falls before the buffer, refill it with the block that ends at
the position, not the one that starts there:
file ... | r97 |t| r98 |t| r99 |t| r100 | ...
buffer [.. | r97 |t| r98 |t| r99 |t] one read
steps <- <- <- in memory
The steps after the refill then stay in the buffer, as forward steps
do, and a seek can keep walking back from the next entry. This would
also help a scrollable cursor fetching backward through a result that
has spilled to disk, which today reads the buffer twice per row.
There are two more issues on the same read path, separate from the
above.
Tuples larger than the buffer pass through it one block at a time.
They could use the part already in the buffer, and when the rest is
larger than the buffer, be read straight into the caller's memory.
When reads and writes alternate on the same file, they share the one
buffer and lose it at each switch: a read loads a block, the write
that follows seeks to the end and drops it, and the next read loads
the same block again.
I think these are worth fixing too. Thoughts?
Best regards,
Henson
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Masahiko Sawada | 2026-12-02 21:59:57 | Re: Postmaster PANIC on a standby with slot synchronization enabled |