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

Поиск
Список
Период
Сортировка
Искать

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

От:
Induja 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

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

От:
Eduard Stepanov <crtxcz@gmail.com>
Дата:
Вложения:
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




On Tue, 10 Mar 2026 at 23:22, Induja Sreekanthan  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

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

От:
Feike Steenbergen <feikesteenbergen@gmail.com>
Дата:
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

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

От:
Feike Steenbergen <feikesteenbergen@gmail.com>
Дата:


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

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

От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
Hi Induja,

On Fri, Feb 27, 2026 at 7:55 PM Induja Sreekanthan  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.

> 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] https://postgr.es/m/97529f5a-ec10-46b1-ab50-4653126c6889%40gmail.com

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


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

От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Fri, Jul 3, 2026 at 7:50 AM Xuneng Zhou  wrote:
>
> Hi Induja,
>
> On Fri, Feb 27, 2026 at 7:55 PM Induja Sreekanthan  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.


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

От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Wed, Aug 5, 2026 at 11:52 PM Melanie Plageman
 wrote:
>
> On Fri, Jul 24, 2026 at 1:40 AM Xuneng Zhou  wrote:
> >
> > On Thu, Jul 23, 2026 at 11:08 PM Melanie Plageman
> >  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.


FAQ