Re: Limit memory usage by postgres_fdw batches

From: Alexander Pyhalov <a(dot)pyhalov(at)postgrespro(dot)ru>
To: Tomas Vondra <tomas(at)vondra(dot)me>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Limit memory usage by postgres_fdw batches
Date: 2026-01-23 06:18:29
Message-ID: e39d964cb5ed91ede13a87109376a463@postgrespro.ru
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Alexander Pyhalov писал(а) 2026-01-13 13:44:
> For now I start thinking we need some form of FETCH, which stops
> fetching data based on batch size...

Hi.

To limit memory consumption, we actually have to retreive less data. And
we can do it only on the side of the foreign server. I've rewritten the
third patch. We introduce a new parameter - cursor_fetch_limit, which is
set by postgres_fdw. When it is set, fetching limited count of records
from the cursor is also limited by memory consumed by the records. Of
course, record size is some estimation (for example, we don't know what
out function will do).

This works as expected - in my tests with tables of large records,
backends, executing selects, were always restricted by about 2 GB of RAM
overall (without patch memory consumption easily grows up to 8 GB).
However, now when we got less tuples from executor, than expected, we
should recheck, if these are all tuples we can get. I've introduced
es_eof EState field to signal that there's no more tuples. Don't know if
it's the best way.

--
Best regards,
Alexander Pyhalov,
Postgres Professional

Attachment Content-Type Size
v4-0001-Limit-batch_size-for-foreign-insert-with-work_mem.patch text/x-diff 2.7 KB
v4-0002-Fall-back-to-foreign-insert-when-tuple-is-too-big.patch text/x-diff 10.2 KB
v4-0003-Introduce-cursor_fetch_limit-parameter-and-use-it-in.patch text/x-diff 26.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message VASUKI M 2026-01-23 06:33:27 Re: Optional skipping of unchanged relations during ANALYZE?
Previous Message Peter Smith 2026-01-23 05:49:26 Re: DOCS - "\d mytable" also shows any publications that publish mytable