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-29 14:24:15
Message-ID: 483f910920ce16a1dff5567025a4f477@postgrespro.ru
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Alexander Pyhalov писал(а) 2026-01-23 09:18:
> 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.

The issue which worried me is that we could change behavior for cursors,
which don't expect this (for example, if these cursors are used in
functions, which are executed via FDW). So I added new cursor option and
used it in postgres_fdw.

Also rearranged patches. While working with test cases I found that we
should avoid batch insert for big tuples, as it has significant
overhead. Switching from (limited) batch insert to foreign insert
allowed to decrease memory consumption in 3 times in some tests.
--
Best regards,
Alexander Pyhalov,
Postgres Professional

Attachment Content-Type Size
v5-0001-Introduce-function-to-estimate-tuple-size.patch text/x-diff 3.0 KB
v5-0002-Limit-batch_size-for-foreign-insert-with-work_mem.patch text/x-diff 11.1 KB
v5-0003-Introduce-memory-limited-cursors-and-use-them-in-pos.patch text/x-diff 30.7 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2026-01-29 14:33:17 Re: logical apply worker's lock waits in subscriber can stall checkpointer in publisher
Previous Message Tom Lane 2026-01-29 14:18:52 Re: ABI Compliance Checker GSoC Project