Re: Speed up tuplestore_seek() by starting from the nearest known position

From: Nitin Motiani <nitinmotiani(at)google(dot)com>
To: assam258(at)gmail(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 15:05:51
Message-ID: CAH5HC9474RkkvzxiWZFa5P2D8CryaoxveQWm6XrT2Hj3B5T=8Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Henson,

I looked at this. Didn't understand all the details yet. But here are
a couple of comments.

> One cost to point out: the index is allocated before the dump frees
> the tuples, so while the dump runs the store holds up to 16 bytes
> per row on top of work_mem. For 100-byte rows that is about 10%,
> and only rows with no columns at all get near 50%. It has to be
> that way round, since entries for the rows being dumped cannot be
> added after the fact, and once the dump finishes the index lives in
> the space the tuples gave back.

EXPLAIN ANALYZE wouldn't show this extra memory. Is that ok? Or should
we count these in the stats?

>
> "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:
>

Would it be simpler to change the cost function to only pick the
backward walk when it benefits? I thought initially that the
backward_cost could be a multiple of the forward cost but perhaps that
would be inaccurate and might not pick the backward walk at all. I
used an LLM to analyze this further, and this is rough pseudocode for
how these costs could be changed.

/* forward: one load covers BLCKSZ / row_bytes rows (~75) */
loads_forward(nrows)
return nrows * row_bytes / BLCKSZ
/* backward: today every step reloads, so one load per row */
loads_backward(nrows)
return nrows
cost_cur = target >= cur ? loads_forward(target - cur)
: loads_backward(max(0, cur - target - cached))
cost_prev = 1 + loads_forward(target - prev) /* 1 = load after
the jump */
cost_next = 1 + loads_backward(next - target) /* 1 = load after
the jump */
start from whichever is cheapest (the same three starts v2 uses)

row_bytes is the average on-disk row size, e.g. from the offsets of
the entries around the target. cached is the number of rows still in
the buffer behind cur; BufFile doesn't expose that today, so it would
be 0 for now.

Let me know if I missed something.

Thanks & Regards,
Nitin Motiani
Google

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Matthias van de Meent 2026-12-03 15:23:47 Re: [WIP] Adding a { LOCK ROWS | LOCK KEY INDEX } clause to FK definitions
Previous Message Andrew Dunstan 2026-12-03 14:57:00 Re: Table AM callback for logical decoding support (REPACK CONCURRENTLY)