BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

Started by Induja Sreekanthan9 months ago17 messageshackers
Jump to latest
#1Induja Sreekanthan
indujas@google.com

Hi,

I found an issue where Postgres (with *effective_io_concurrency* of 64 or
higher) runs out of local buffers during a sequential scan on a temporary
table with TOAST data.

The issue occurs because the ReadStream look-ahead pins all the local
buffers. This results in the TOAST index look-up and TOAST page read being
unable to find any available local buffers. The ReadStream's
max_pinned_buffers can be as high as the num_temp_buffers, depending on the
effective_io_concurrency.
Here is a reproduction of the issue using the default temp_buffers setting
and effective_io_concurrency=128:

docker run --name my-postgres -e POSTGRES_PASSWORD=my-password -p 5432:5432
-d postgres:18 -c effective_io_concurrency=128

postgres=# CREATE TEMPORARY TABLE tmp_tbl1 (
s_suppkey NUMERIC NOT NULL,
s_nationkey NUMERIC,
s_comment VARCHAR(256),
s_name CHAR(256),
s_address VARCHAR(256),
s_phone TEXT,
s_acctbal NUMERIC,
CONSTRAINT supplier_pk PRIMARY KEY (s_suppkey)
);
CREATE TABLE
postgres=# INSERT INTO tmp_tbl1 (s_suppkey, s_nationkey, s_comment, s_name,
s_address, s_phone, s_acctbal)
SELECT
('1' || repeat('0', 2000) || i::text)::NUMERIC AS s_suppkey,
('5' || repeat('0', 2000) || floor(random() * 25)::text)::NUMERIC AS
s_nationkey,
md5(random()::text) || ' some comment' AS s_comment,
'Supplier#' || LPAD(i::text, 9, '0') AS s_name,
'Address-' || md5(i::text) AS s_address,
repeat('P', 4096) || '-' || i::text || repeat('P', 2048) || 'fwoiefrr'
||
repeat('fejwfelwkmfP', 4096) || '-' || i::text ||
repeat('fnwekjfmelkwf', 2048) AS s_phone,
('9' || repeat('9', 2000) || '.' || floor(random()*100)::text)::NUMERIC
AS s_acctbal
FROM generate_series(1, 8000) AS i;
INSERT 0 8000
postgres=# SELECT * FROM tmp_tbl1;
ERROR: no empty local buffer available

Attached is a patch that addresses this by limiting ReadStream's
max_pinned_buffers for temp tables to 75% of the available local buffers.
It also introduces a cap on max_ios for temp tables to
DEFAULT_EFFECTIVE_IO_CONCURRENCY, to account for multiple sequential scan
look-aheads happening simultaneously.

Regards,
Induja Sreekanthan

Attachments:

diff.patchapplication/x-patch; name=diff.patchDownload+30-4
#2Eduard Stepanov
crtxcz@gmail.com
In reply to: Induja Sreekanthan (#1)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

Hi Induja!

I've encountered the same issue but in a different context, so I came
up with an alternative solution: dynamic buffer_limit check in
read_stream_look_ahead()

In read_stream_look_ahead(), I dynamically check the actual remaining
pin budget via GetAdditionalLocalPinLimit(). This adds a third
condition to the main while loop and re-checks after each pin
operation. Additionally, when the budget is exhausted, the pending
read is started immediately rather than waiting for io_combine_limit
accumulation.

The attached patch includes changes to
src/backend/storage/aio/read_stream.c and adds two additional tests to
temp.sql.

Best regards,
Eduard Stepanov
Tantor Labs LLC

Show quoted text

On Tue, 10 Mar 2026 at 23:22, Induja Sreekanthan <indujas@google.com> wrote:

Hi,

I found an issue where Postgres (with effective_io_concurrency of 64 or higher) runs out of local buffers during a sequential scan on a temporary table with TOAST data.

The issue occurs because the ReadStream look-ahead pins all the local buffers. This results in the TOAST index look-up and TOAST page read being unable to find any available local buffers. The ReadStream's max_pinned_buffers can be as high as the num_temp_buffers, depending on the effective_io_concurrency.

Here is a reproduction of the issue using the default temp_buffers setting and effective_io_concurrency=128:

docker run --name my-postgres -e POSTGRES_PASSWORD=my-password -p 5432:5432 -d postgres:18 -c effective_io_concurrency=128

postgres=# CREATE TEMPORARY TABLE tmp_tbl1 (
s_suppkey NUMERIC NOT NULL,
s_nationkey NUMERIC,
s_comment VARCHAR(256),
s_name CHAR(256),
s_address VARCHAR(256),
s_phone TEXT,
s_acctbal NUMERIC,
CONSTRAINT supplier_pk PRIMARY KEY (s_suppkey)
);
CREATE TABLE
postgres=# INSERT INTO tmp_tbl1 (s_suppkey, s_nationkey, s_comment, s_name, s_address, s_phone, s_acctbal)
SELECT
('1' || repeat('0', 2000) || i::text)::NUMERIC AS s_suppkey,
('5' || repeat('0', 2000) || floor(random() * 25)::text)::NUMERIC AS s_nationkey,
md5(random()::text) || ' some comment' AS s_comment,
'Supplier#' || LPAD(i::text, 9, '0') AS s_name,
'Address-' || md5(i::text) AS s_address,
repeat('P', 4096) || '-' || i::text || repeat('P', 2048) || 'fwoiefrr' ||
repeat('fejwfelwkmfP', 4096) || '-' || i::text || repeat('fnwekjfmelkwf', 2048) AS s_phone,
('9' || repeat('9', 2000) || '.' || floor(random()*100)::text)::NUMERIC AS s_acctbal
FROM generate_series(1, 8000) AS i;
INSERT 0 8000
postgres=# SELECT * FROM tmp_tbl1;
ERROR: no empty local buffer available

Attached is a patch that addresses this by limiting ReadStream's max_pinned_buffers for temp tables to 75% of the available local buffers. It also introduces a cap on max_ios for temp tables to DEFAULT_EFFECTIVE_IO_CONCURRENCY, to account for multiple sequential scan look-aheads happening simultaneously.

Regards,
Induja Sreekanthan

Attachments:

t53419_2
0001-Throttle-read-stream-look-ahead-against-local-buffer.patchapplication/octet-stream; name=0001-Throttle-read-stream-look-ahead-against-local-buffer.patchDownload+201-3
#3Feike Steenbergen
feikesteenbergen@gmail.com
In reply to: Eduard Stepanov (#2)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

Hi, all,

On Tue, 10 Mar 2026 at 23:22, Induja Sreekanthan <indujas@google.com>

wrote:

Hi,

I found an issue where Postgres (with effective_io_concurrency of 64 or

higher) runs out of local buffers during a sequential scan on a temporary
table with TOAST data.

Just to confirm, I have a production db (20TB, high concurrency, lot's of
updates/merges) that
is a heavy user of temp tables running into this as well with
io_method='io_uring'.

This started after having upgraded PostgreSQL 18 (from 17).
For now, the problem disappears again when switching to io_method='worker'

Thank you for providing the patches,

kind regards,

Feike Steenbergen

#4Feike Steenbergen
feikesteenbergen@gmail.com
In reply to: Feike Steenbergen (#3)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Wed, 6 May 2026 at 14:24, Feike Steenbergen <feikesteenbergen@gmail.com>
wrote:

For now, the problem disappears again when switching to io_method='worker'

I spoke too soon it seems. It happens less frequently, but we do still have
the issue even with io_method='worker'.
Reverting to io_method='sync' as I would expect that will solve the issue.

Feike

#5Xuneng Zhou
xunengzhou@gmail.com
In reply to: Induja Sreekanthan (#1)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

Hi Induja,

On Fri, Feb 27, 2026 at 7:55 PM Induja Sreekanthan <indujas@google.com> wrote:

Hi,

I found an issue where Postgres (with effective_io_concurrency of 64 or higher) runs out of local buffers during a sequential scan on a temporary table with TOAST data.

The issue occurs because the ReadStream look-ahead pins all the local buffers. This results in the TOAST index look-up and TOAST page read being unable to find any available local buffers. The ReadStream's max_pinned_buffers can be as high as the num_temp_buffers, depending on the effective_io_concurrency.

Here is a reproduction of the issue using the default temp_buffers setting and effective_io_concurrency=128:

docker run --name my-postgres -e POSTGRES_PASSWORD=my-password -p 5432:5432 -d postgres:18 -c effective_io_concurrency=128

postgres=# CREATE TEMPORARY TABLE tmp_tbl1 (
s_suppkey NUMERIC NOT NULL,
s_nationkey NUMERIC,
s_comment VARCHAR(256),
s_name CHAR(256),
s_address VARCHAR(256),
s_phone TEXT,
s_acctbal NUMERIC,
CONSTRAINT supplier_pk PRIMARY KEY (s_suppkey)
);
CREATE TABLE
postgres=# INSERT INTO tmp_tbl1 (s_suppkey, s_nationkey, s_comment, s_name, s_address, s_phone, s_acctbal)
SELECT
('1' || repeat('0', 2000) || i::text)::NUMERIC AS s_suppkey,
('5' || repeat('0', 2000) || floor(random() * 25)::text)::NUMERIC AS s_nationkey,
md5(random()::text) || ' some comment' AS s_comment,
'Supplier#' || LPAD(i::text, 9, '0') AS s_name,
'Address-' || md5(i::text) AS s_address,
repeat('P', 4096) || '-' || i::text || repeat('P', 2048) || 'fwoiefrr' ||
repeat('fejwfelwkmfP', 4096) || '-' || i::text || repeat('fnwekjfmelkwf', 2048) AS s_phone,
('9' || repeat('9', 2000) || '.' || floor(random()*100)::text)::NUMERIC AS s_acctbal
FROM generate_series(1, 8000) AS i;
INSERT 0 8000
postgres=# SELECT * FROM tmp_tbl1;
ERROR: no empty local buffer available

Thanks for reporting this issue. It smells similar to the bug reported
by Alexander earlier. [1]/messages/by-id/97529f5a-ec10-46b1-ab50-4653126c6889@gmail.com The root cause of them seems the same: we
give read stream too much budget for local buffer pins. The
fix(da6874635db by Melanie) is to cut the budget to 1/4.

Attached is a patch that addresses this by limiting ReadStream's max_pinned_buffers for temp tables to 75% of the available local buffers. It also introduces a cap on max_ios for temp tables to DEFAULT_EFFECTIVE_IO_CONCURRENCY, to account for multiple sequential scan look-aheads happening simultaneously.

If that's the case, I'm wondering whether it makes sense to backpatch
this fix to 18. I tried to do this for the local tree and the
reproducer passed. That said, it might not be safe to do so for a
stable version. It would be helpful to hear Melanie's and Andres's
thoughts on this.

[1]: /messages/by-id/97529f5a-ec10-46b1-ab50-4653126c6889@gmail.com

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#6Xuneng Zhou
xunengzhou@gmail.com
In reply to: Xuneng Zhou (#5)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Fri, Jul 3, 2026 at 7:50 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:

Hi Induja,

On Fri, Feb 27, 2026 at 7:55 PM Induja Sreekanthan <indujas@google.com> wrote:

Hi,

I found an issue where Postgres (with effective_io_concurrency of 64 or higher) runs out of local buffers during a sequential scan on a temporary table with TOAST data.

The issue occurs because the ReadStream look-ahead pins all the local buffers. This results in the TOAST index look-up and TOAST page read being unable to find any available local buffers. The ReadStream's max_pinned_buffers can be as high as the num_temp_buffers, depending on the effective_io_concurrency.

Here is a reproduction of the issue using the default temp_buffers setting and effective_io_concurrency=128:

docker run --name my-postgres -e POSTGRES_PASSWORD=my-password -p 5432:5432 -d postgres:18 -c effective_io_concurrency=128

postgres=# CREATE TEMPORARY TABLE tmp_tbl1 (
s_suppkey NUMERIC NOT NULL,
s_nationkey NUMERIC,
s_comment VARCHAR(256),
s_name CHAR(256),
s_address VARCHAR(256),
s_phone TEXT,
s_acctbal NUMERIC,
CONSTRAINT supplier_pk PRIMARY KEY (s_suppkey)
);
CREATE TABLE
postgres=# INSERT INTO tmp_tbl1 (s_suppkey, s_nationkey, s_comment, s_name, s_address, s_phone, s_acctbal)
SELECT
('1' || repeat('0', 2000) || i::text)::NUMERIC AS s_suppkey,
('5' || repeat('0', 2000) || floor(random() * 25)::text)::NUMERIC AS s_nationkey,
md5(random()::text) || ' some comment' AS s_comment,
'Supplier#' || LPAD(i::text, 9, '0') AS s_name,
'Address-' || md5(i::text) AS s_address,
repeat('P', 4096) || '-' || i::text || repeat('P', 2048) || 'fwoiefrr' ||
repeat('fejwfelwkmfP', 4096) || '-' || i::text || repeat('fnwekjfmelkwf', 2048) AS s_phone,
('9' || repeat('9', 2000) || '.' || floor(random()*100)::text)::NUMERIC AS s_acctbal
FROM generate_series(1, 8000) AS i;
INSERT 0 8000
postgres=# SELECT * FROM tmp_tbl1;
ERROR: no empty local buffer available

Thanks for reporting this issue. It smells similar to the bug reported
by Alexander earlier. [1] The root cause of them seems the same: we
give read stream too much budget for local buffer pins. The
fix(da6874635db by Melanie) is to cut the budget to 1/4.

The issue is gone in pg19 in further testing.

Attached is a patch that addresses this by limiting ReadStream's max_pinned_buffers for temp tables to 75% of the available local buffers. It also introduces a cap on max_ios for temp tables to DEFAULT_EFFECTIVE_IO_CONCURRENCY, to account for multiple sequential scan look-aheads happening simultaneously.

If that's the case, I'm wondering whether it makes sense to backpatch
this fix to 18. I tried to do this for the local tree and the
reproducer passed. That said, it might not be safe to do so for a
stable version. It would be helpful to hear Melanie's and Andres's
thoughts on this.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#7Xuneng Zhou
xunengzhou@gmail.com
In reply to: Feike Steenbergen (#4)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

Hi Feike,

On Wed, May 6, 2026 at 8:48 PM Feike Steenbergen
<feikesteenbergen@gmail.com> wrote:

On Wed, 6 May 2026 at 14:24, Feike Steenbergen <feikesteenbergen@gmail.com> wrote:

For now, the problem disappears again when switching to io_method='worker'

I spoke too soon it seems. It happens less frequently, but we do still have the issue even with io_method='worker'.
Reverting to io_method='sync' as I would expect that will solve the issue.

Interestingly, I wonder why this variance would occur in production.
This is the more interesting part of this thread. I did some
investigation & experiment, pg 19 passed the reproducer even if
da6874635db is removed. A series of improvements made in April may
contribute to this. Will return soon to share the findings..

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#8Xuneng Zhou
xunengzhou@gmail.com
In reply to: Xuneng Zhou (#7)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Fri, Jul 3, 2026 at 11:54 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:

Hi Feike,

On Wed, May 6, 2026 at 8:48 PM Feike Steenbergen
<feikesteenbergen@gmail.com> wrote:

On Wed, 6 May 2026 at 14:24, Feike Steenbergen <feikesteenbergen@gmail.com> wrote:

For now, the problem disappears again when switching to io_method='worker'

I spoke too soon it seems. It happens less frequently, but we do still have the issue even with io_method='worker'.
Reverting to io_method='sync' as I would expect that will solve the issue.

Interestingly, I wonder why this variance would occur in production.
This is the more interesting part of this thread. I did some
investigation & experiment, pg 19 passed the reproducer even if
da6874635db is removed. A series of improvements made in April may
contribute to this. Will return soon to share the findings..

I think we cannot expect that different I/O methods will expose the
bug differently since the root cause of this failure stays the same
regardless of the I/O methods being selected. That's what happened in
my pg18 testing with the producer -- it failed deterministically with
all three methods.

1) Separate but not independent

Currently, the I/O stack has two moving pieces that matter here:

- The read stream decides what to read next and how far ahead: it
tries to pin a window of buffers ahead of its consumer via lookahead
heuristics, so that by the time the caller wants a block, the read has
long been issued or even completed, and the buffer can be returned
with less waiting.
- The AIO submission layer decides who would execute the read: the
backend itself (sync), a fleet of dedicated I/O worker processes, or
the kernel via io_uring.

These two mechanisms are separate -- but not independent. The coupling
of them matters. The look-ahead window size is not fixed; it's
auto-tuned with feedback. The size starts at one block and adjusts
based on the signal/status of the consumed buffer: if the buffer was a
cache hit, the size decays by one; if I/O needed, the size doubles, up
to a ceiling. The signal that drives the controller is generated by
the fill machinery -- the very subsystem io_metod selects. The signal
seems deceptively simple at first glance, but it becomes complicated
as we look into it -- varies across versions/io methods/buffer types.
So better look at the simple and coherent part first -- how to
encounter this bug.

As said, we have a ceiling for the look-ahead window, it is set as:
max_pinned_buffers = (effective_io_concurrency + 1) x
io_combine_limit, and being clamped several times after. For shared
buffers, the clamping strategy is effective as we let seqscan uses a
small ring and one backend gets only a fair share of shared_buffers.
However, for temp Buffers, the pin max_pinned_buffers is capped only
by GetLocalPinLimit() which returns num_temp_buffers. All of it, which
is over-generous in a dangerous way. Let's say with the defaults
(temp_buffers=1024, io_combine_limit=16), any effective_io_concurrency
of 64 or more makes the formula exceed 1024, so the clamp cap is
exactly on the whole pool. That means a single sequential scan's
look-ahead is allowed to pin every local buffer the backend has. It
will be a problem if extra buffer needs from the backend arrives
simultaneously.

This is how the tragedy of the reproducer happened, 'tmp_tbl1'
occupies about 1,333 heap blocks, larger than the default
temp_buffers' pool of 1,024 blocks, which is ample for driving the
lookahead window to its ceiling in a cold-miss run. The "something
else" arrives on every single row -- each tuple carries a TOASTed
column that must be detoasted for output, and the TOAST fetch goes
through plain ReadBuffer, which has no pin-limit awareness and no
fallback; it just errors. The collision was caught in a backtrace:
printtup --> detoast_attr --> toast_fetch_datum --> … -->
GetLocalVictimBuffer, at the precise moment the added log recorded
NLocalPinnedBuffers=1024, num_temp_buffers=1024. Every buffer pinned;
the 1025th request was fatal.

This coincidence condition here is why production and lab could behave
differently. The reproducer failed deterministically in cold runs of
pg18 since it ensures both conditions are true -- the look ahead
window ramp-up to its ceiling and the extra buffer needs is there at
that timing. However, those two conditions can be hard to guarantee in
a changing production workload.

2) Three engines, two pools

To be continued...

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#9Feike Steenbergen
feikesteenbergen@gmail.com
In reply to: Xuneng Zhou (#8)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Fri, 3 Jul 2026 at 05:54, Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Wed, 6 May 2026 at 14:24, Feike Steenbergen <

feikesteenbergen@gmail.com> wrote:

For now, the problem disappears again when switching to

io_method='worker'

I spoke too soon it seems. It happens less frequently, but we do still

have the issue even with io_method='worker'.

Reverting to io_method='sync' as I would expect that will solve the

issue.

Interestingly, I wonder why this variance would occur in production.
This is the more interesting part of this thread. I did some
investigation & experiment, pg 19 passed the reproducer even if
da6874635db is removed. A series of improvements made in April may
contribute to this. Will return soon to share the findings..

Indeed, we see the problem occur at *all* settings of io_method. As it is
slightly unpredictable,
a retry of a transaction may succeed afterwards, so our production
application is mostly
fine. We did observe these errors right after upgrading to pg19, then we
didn't observe the error
for a full 3 weeks, but now we observe the error dozens of times every hour.

Our application has a bit of a cadence though, it only does its work 5/6
hours, and the error
is only showing when the application is actually doing work.

2) Three engines, two pools

To be continued...

On our end, we're trying to reduce connections (batch more, queue work
outside the db more),
however, it would be great if this problem can be fully identified and
resolved in pg
itself.

Kind regards,

Feike

#10Xuneng Zhou
xunengzhou@gmail.com
In reply to: Feike Steenbergen (#9)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Thu, Jul 23, 2026 at 5:17 PM Feike Steenbergen
<feikesteenbergen@gmail.com> wrote:

On Fri, 3 Jul 2026 at 05:54, Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Wed, 6 May 2026 at 14:24, Feike Steenbergen <feikesteenbergen@gmail.com> wrote:

For now, the problem disappears again when switching to io_method='worker'

I spoke too soon it seems. It happens less frequently, but we do still have the issue even with io_method='worker'.
Reverting to io_method='sync' as I would expect that will solve the issue.

Interestingly, I wonder why this variance would occur in production.
This is the more interesting part of this thread. I did some
investigation & experiment, pg 19 passed the reproducer even if
da6874635db is removed. A series of improvements made in April may
contribute to this. Will return soon to share the findings..

Indeed, we see the problem occur at *all* settings of io_method. As it is slightly unpredictable,
a retry of a transaction may succeed afterwards, so our production application is mostly
fine. We did observe these errors right after upgrading to pg19, then we didn't observe the error
for a full 3 weeks, but now we observe the error dozens of times every hour.

Our application has a bit of a cadence though, it only does its work 5/6 hours, and the error
is only showing when the application is actually doing work.

That seems strange. Can you share some failure logs and describe the
workload a bit more? There are possible workloads that keep squeezing
out the temp buffers. It would also be helpful to do some quick
diagnoses like:

1) ALTER SYSTEM SET backtrace_functions = 'GetLocalVictimBuffer';
SELECT pg_reload_conf();

2) SELECT version();

3) Settings: temp_buffers, effective_io_concurrency, io_combine_limit

2) Three engines, two pools

To be continued...

On our end, we're trying to reduce connections (batch more, queue work outside the db more),
however, it would be great if this problem can be fully identified and resolved in pg
itself.

If this same issue/failure persists, I'm afraid this move would not
mitigate it or could be counterproductive since the temp-pressure per
backend is bigger. It might be more helpful to up the temp_buffers &
down the effective_io_concurrency if the arithmetic applies here.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#11Melanie Plageman
melanieplageman@gmail.com
In reply to: Xuneng Zhou (#5)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Thu, Jul 2, 2026 at 7:50 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

Thanks for reporting this issue. It smells similar to the bug reported
by Alexander earlier. [1] The root cause of them seems the same: we
give read stream too much budget for local buffer pins. The
fix(da6874635db by Melanie) is to cut the budget to 1/4.

Attached is a patch that addresses this by limiting ReadStream's max_pinned_buffers for temp tables to 75% of the available local buffers. It also introduces a cap on max_ios for temp tables to DEFAULT_EFFECTIVE_IO_CONCURRENCY, to account for multiple sequential scan look-aheads happening simultaneously.

If that's the case, I'm wondering whether it makes sense to backpatch
this fix to 18. I tried to do this for the local tree and the
reproducer passed. That said, it might not be safe to do so for a
stable version. It would be helpful to hear Melanie's and Andres's
thoughts on this.

da6874635db fixes the originally reported bug in this thread on 18. It
makes sense to backpatch it to 18. I hadn't thought of the TOAST case.
I'm thinking about whether backpatching to 17 makes sense or not. It
would be much more difficult to hit an issue in 17, but I'll need to
think more about it.

I wonder if in 20, we should move toward Andres' suggested formula
Max(1, (num_temp_buffers − NLocalPinnedBuffers) / 4)
instead of the existing
num_temp_buffers / 4 − NLocalPinnedBuffers

Which would mean we got a quarter of available buffers instead of a
hard cap of a quarter of total buffers.
This wouldn't have been any better at solving the issue in this
thread, but I'm wondering if it is just a better formula in general
going forward (not for backpatching).

- Melanie

#12Xuneng Zhou
xunengzhou@gmail.com
In reply to: Melanie Plageman (#11)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

Hi Melanie,

On Thu, Jul 23, 2026 at 11:08 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:

On Thu, Jul 2, 2026 at 7:50 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

Thanks for reporting this issue. It smells similar to the bug reported
by Alexander earlier. [1] The root cause of them seems the same: we
give read stream too much budget for local buffer pins. The
fix(da6874635db by Melanie) is to cut the budget to 1/4.

Attached is a patch that addresses this by limiting ReadStream's max_pinned_buffers for temp tables to 75% of the available local buffers. It also introduces a cap on max_ios for temp tables to DEFAULT_EFFECTIVE_IO_CONCURRENCY, to account for multiple sequential scan look-aheads happening simultaneously.

If that's the case, I'm wondering whether it makes sense to backpatch
this fix to 18. I tried to do this for the local tree and the
reproducer passed. That said, it might not be safe to do so for a
stable version. It would be helpful to hear Melanie's and Andres's
thoughts on this.

da6874635db fixes the originally reported bug in this thread on 18. It
makes sense to backpatch it to 18. I hadn't thought of the TOAST case.
I'm thinking about whether backpatching to 17 makes sense or not. It
would be much more difficult to hit an issue in 17, but I'll need to
think more about it.
I wonder if in 20, we should move toward Andres' suggested formula
Max(1, (num_temp_buffers − NLocalPinnedBuffers) / 4)
instead of the existing
num_temp_buffers / 4 − NLocalPinnedBuffers

Which would mean we got a quarter of available buffers instead of a
hard cap of a quarter of total buffers.
This wouldn't have been any better at solving the issue in this
thread, but I'm wondering if it is just a better formula in general
going forward (not for backpatching).

I am not sure about that. In the existing formula, we set a hard-cap
for the read streams at backend level. For the suggested one, the cap
is removed so multiple successive streams could push the water level
of pins toward the ceiling of temp buffers. I am unsure of the
consequences of this. Adjusting the cap limit alone seems not resolve
the competing nature between read stream and other needs. Reclaiming
some pinned buffers might help, but it is a more complex mechanism
which might not be a good fit in the bigger picture.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#13Melanie Plageman
melanieplageman@gmail.com
In reply to: Melanie Plageman (#11)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Thu, Jul 23, 2026 at 11:07 AM Melanie Plageman
<melanieplageman@gmail.com> wrote:

On Thu, Jul 2, 2026 at 7:50 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

If that's the case, I'm wondering whether it makes sense to backpatch
this fix to 18. I tried to do this for the local tree and the
reproducer passed. That said, it might not be safe to do so for a
stable version. It would be helpful to hear Melanie's and Andres's
thoughts on this.

da6874635db fixes the originally reported bug in this thread on 18. It
makes sense to backpatch it to 18. I hadn't thought of the TOAST case.
I'm thinking about whether backpatching to 17 makes sense or not. It
would be much more difficult to hit an issue in 17, but I'll need to
think more about it.

I've backpatched the fix to pg 18. After some analysis, it seems we
can not hit the issue with any non-contrived example in 17 because in
practice sequential scans won't bump the lookahead distance past
io_combine_limit which can't exceed the minimum temp_buffers value.
There may be some contrived way to hit it with analyze but it seems
far-fetched. Also on 17, it's not a clean cherry-pick because we don't
have GetAdditionalLocalPinLimit(), and there seem to be more
implications to changing LimitAdditionalLocalPins(). So, for now,
let's just patch 18.

- Melanie

#14Melanie Plageman
melanieplageman@gmail.com
In reply to: Xuneng Zhou (#12)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Fri, Jul 24, 2026 at 1:40 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Thu, Jul 23, 2026 at 11:08 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:

I wonder if in 20, we should move toward Andres' suggested formula
Max(1, (num_temp_buffers − NLocalPinnedBuffers) / 4)
instead of the existing
num_temp_buffers / 4 − NLocalPinnedBuffers

Which would mean we got a quarter of available buffers instead of a
hard cap of a quarter of total buffers.
This wouldn't have been any better at solving the issue in this
thread, but I'm wondering if it is just a better formula in general
going forward (not for backpatching).

I am not sure about that. In the existing formula, we set a hard-cap
for the read streams at backend level. For the suggested one, the cap
is removed so multiple successive streams could push the water level
of pins toward the ceiling of temp buffers. I am unsure of the
consequences of this. Adjusting the cap limit alone seems not resolve
the competing nature between read stream and other needs. Reclaiming
some pinned buffers might help, but it is a more complex mechanism
which might not be a good fit in the bigger picture.

I think the right approach is to drive out a change like this with
specific use cases or after seeing bottlenecks/problems in workloads.
Since we're not encountering those now, I think investigating how to
change this falls far down the priority list.

- Melanie

#15Xuneng Zhou
xunengzhou@gmail.com
In reply to: Melanie Plageman (#13)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Wed, Aug 5, 2026 at 11:51 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:

On Thu, Jul 23, 2026 at 11:07 AM Melanie Plageman
<melanieplageman@gmail.com> wrote:

On Thu, Jul 2, 2026 at 7:50 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

If that's the case, I'm wondering whether it makes sense to backpatch
this fix to 18. I tried to do this for the local tree and the
reproducer passed. That said, it might not be safe to do so for a
stable version. It would be helpful to hear Melanie's and Andres's
thoughts on this.

da6874635db fixes the originally reported bug in this thread on 18. It
makes sense to backpatch it to 18. I hadn't thought of the TOAST case.
I'm thinking about whether backpatching to 17 makes sense or not. It
would be much more difficult to hit an issue in 17, but I'll need to
think more about it.

I've backpatched the fix to pg 18. After some analysis, it seems we
can not hit the issue with any non-contrived example in 17 because in
practice sequential scans won't bump the lookahead distance past
io_combine_limit which can't exceed the minimum temp_buffers value.
There may be some contrived way to hit it with analyze but it seems
far-fetched. Also on 17, it's not a clean cherry-pick because we don't
have GetAdditionalLocalPinLimit(), and there seem to be more
implications to changing LimitAdditionalLocalPins(). So, for now,
let's just patch 18.

Thanks for dealing with it. The backpatching decision also makes sense to me.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#16Xuneng Zhou
xunengzhou@gmail.com
In reply to: Melanie Plageman (#14)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Wed, Aug 5, 2026 at 11:52 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:

On Fri, Jul 24, 2026 at 1:40 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Thu, Jul 23, 2026 at 11:08 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:

I wonder if in 20, we should move toward Andres' suggested formula
Max(1, (num_temp_buffers − NLocalPinnedBuffers) / 4)
instead of the existing
num_temp_buffers / 4 − NLocalPinnedBuffers

Which would mean we got a quarter of available buffers instead of a
hard cap of a quarter of total buffers.
This wouldn't have been any better at solving the issue in this
thread, but I'm wondering if it is just a better formula in general
going forward (not for backpatching).

I am not sure about that. In the existing formula, we set a hard-cap
for the read streams at backend level. For the suggested one, the cap
is removed so multiple successive streams could push the water level
of pins toward the ceiling of temp buffers. I am unsure of the
consequences of this. Adjusting the cap limit alone seems not resolve
the competing nature between read stream and other needs. Reclaiming
some pinned buffers might help, but it is a more complex mechanism
which might not be a good fit in the bigger picture.

I think the right approach is to drive out a change like this with
specific use cases or after seeing bottlenecks/problems in workloads.
Since we're not encountering those now, I think investigating how to
change this falls far down the priority list.

I agree with adjusting heuristic better based on real workloads. BTW,
I wanna consult a potential nearby performance optimization. In the
process of paired code reading with Sol before, it suggested lowering
the cap of lookahead distance for synchronous mode. The suggestion
somehow makes sense to me and it showed small consistent
improvement(2%) in a preliminary benchmarking. Currently, the
heuristic of lookahead distance is unified for three io modes. The
distance can grow up to max_pinned_buffers, which may be pretty larger
than io_combine_limit. For io_ worker and io_uring, that setting could
make sense since it allows overlapping of multiple i/o operations to
hide latency. However, for synchronous mode, that may seems not help a
lot since reads are done by the consuming backend, so i/o cannot
overlap with query execution. Once that distance is approaching
io_combine_limit, the benefit of growing it further becomes marginal
at the cost of the pinning and bookkeeping overhead. This is the case
io_combine_limit for pg17, where the aio framework hasn't been
introduced. After 18 is the status quo - max_pinned_buffers. Is there
something missing in my understanding for this setting or is it a
relatively low-priority micro-optimization? If it is the latter, do
you think this is viable/worthwhile? I plan to start a new dedicated
thread for this topic when the working thread of my mind gets
preempted from other tasks and its context gets switched properly.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#17Xuneng Zhou
xunengzhou@gmail.com
In reply to: Xuneng Zhou (#16)
Re: BUG: ReadStream look-ahead exhausts local buffers when effective_io_concurrency>=64

On Thu, Aug 6, 2026 at 11:34 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Wed, Aug 5, 2026 at 11:52 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:

On Fri, Jul 24, 2026 at 1:40 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Thu, Jul 23, 2026 at 11:08 PM Melanie Plageman
<melanieplageman@gmail.com> wrote:

I wonder if in 20, we should move toward Andres' suggested formula
Max(1, (num_temp_buffers − NLocalPinnedBuffers) / 4)
instead of the existing
num_temp_buffers / 4 − NLocalPinnedBuffers

Which would mean we got a quarter of available buffers instead of a
hard cap of a quarter of total buffers.
This wouldn't have been any better at solving the issue in this
thread, but I'm wondering if it is just a better formula in general
going forward (not for backpatching).

I am not sure about that. In the existing formula, we set a hard-cap
for the read streams at backend level. For the suggested one, the cap
is removed so multiple successive streams could push the water level
of pins toward the ceiling of temp buffers. I am unsure of the
consequences of this. Adjusting the cap limit alone seems not resolve
the competing nature between read stream and other needs. Reclaiming
some pinned buffers might help, but it is a more complex mechanism
which might not be a good fit in the bigger picture.

I think the right approach is to drive out a change like this with
specific use cases or after seeing bottlenecks/problems in workloads.
Since we're not encountering those now, I think investigating how to
change this falls far down the priority list.

I agree with adjusting heuristic better based on real workloads. BTW,
I wanna consult a potential nearby performance optimization. In the
process of paired code reading with Sol before, it suggested lowering
the cap of lookahead distance for synchronous mode. The suggestion
somehow makes sense to me and it showed small consistent
improvement(2%) in a preliminary benchmarking. Currently, the
heuristic of lookahead distance is unified for three io modes. The
distance can grow up to max_pinned_buffers, which may be pretty larger

Brain fart, this is inaccurate even for temp buffers after the stricter budget.

than io_combine_limit. For io_ worker and io_uring, that setting could
make sense since it allows overlapping of multiple i/o operations to
hide latency. However, for synchronous mode, that may seems not help a
lot since reads are done by the consuming backend, so i/o cannot
overlap with query execution. Once that distance is approaching
io_combine_limit, the benefit of growing it further becomes marginal
at the cost of the pinning and bookkeeping overhead. This is the case
io_combine_limit for pg17, where the aio framework hasn't been
introduced. After 18 is the status quo - max_pinned_buffers. Is there
something missing in my understanding for this setting or is it a
relatively low-priority micro-optimization? If it is the latter, do
you think this is viable/worthwhile? I plan to start a new dedicated
thread for this topic when the working thread of my mind gets
preempted from other tasks and its context gets switched properly.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.