| From: | Tynan Couture-Rashid <tynan(at)awardedsoftware(dot)com> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | Memory leak in InitMaterializedSRF when a set-returning function is called once per row (15+) |
| Date: | 2026-12-03 18:17:23 |
| Message-ID: | CACngnGtv59g0vudLB+Z3UwHgVteBZ8mer0g5K8XQpZJt6+HERw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
Hi,
I tracked down a per-call memory leak in
InitMaterializedSRF(). It shows up via jsonb_each() and similar
set-returning functions. We had a bad query in production and the plan
called jsonb_each() about 87 million times, exacerbating this leak and
causing the server to run out of memory.
The write-up below and the attached patches were drafted with AI assistance
with significant involvement from me, and I reproduced the leak and
the fix myself. It's a small fix that may be backported into all
postgres versions 15+, since that's when the issue was introduced.
I'm happy to answer any questions. This is my first time attempting to
contribute to postgres or via any mailing list, so any feedback is
welcome. The rest of this email is machine-written.
Thanks, Tynan
---
Summary:
A set-returning function that resolves its result type through
get_call_result_type() leaks memory into the per-query context on every
call. When such a function is called once per outer row, for example
from a correlated subquery, the backend's memory grows until the query
ends. work_mem doesn't limit it.
This took down a production server (16.13, 64 GB RAM). The plan rescanned
a correlated subquery calling jsonb_each() about 87 million times. Each
session grew to roughly 26 GB, and three concurrent sessions exhausted
memory.
Reproduce:
SET jit = off;
SET max_parallel_workers_per_gather = 0;
CREATE TABLE t AS SELECT g AS id FROM generate_series(1, 2000000) g;
SELECT sum((SELECT count(*) FROM jsonb_each(jsonb_build_object('k', t.id))))
FROM t;
The backend reaches about 590 MB of private memory (RssAnon) before the
query finishes. pg_log_backend_memory_contexts() from another session
shows the growth in ExecutorState. jsonb_array_elements() called the
same way stays at about 4 MB.
Versions tested: 16.14, 16.16, 17.11, 18.7, 19.0 and master at
1e5ccbddf0d. All leak at roughly 300 bytes per call.
Cause:
InitMaterializedSRF() switches to the per-query context before building
the result descriptor. With MAT_SRF_USE_EXPECTED_DESC it copies
rsinfo->expectedDesc. Without it, it calls get_call_result_type(), which
reaches build_function_result_tupdesc_d(). That function deconstructs
proargnames and allocates the output type and name arrays and name
strings, all in the per-query context. ExecMakeTableFunctionResult()
frees the returned TupleDesc, but nothing frees the scratch allocations.
About 40 call sites in core pass 0 or MAT_SRF_BLESS and take this path,
including json_each(), jsonb_each() and jsonb_each_text(). The leak
grows with the number of output columns: pg_stat_get_activity() called
with an argument that varies per row leaks more than 1 kB per call.
This is a regression from 9e98583898c, which added the helper
in 15. Before it, each_worker_jsonb() called get_call_result_type() in
the caller's context, then switched to the per-query context and copied
the descriptor. The helper does the lookup after the switch. That
conclusion comes from reading the history; 14 wasn't tested.
Patches:
0001 is the fix, proposed for back-patching to 15. It resolves the
result type in the caller's context, which the executor resets between
calls, and copies the finished descriptor into the per-query context
with CreateTupleDescCopyConstr() so nothing it returned is lost. This
restores the pre-15 behavior for every caller. If depending on the
caller's context is undesirable, a temporary context created and
deleted inside InitMaterializedSRF() also works, at a small cost per
call.
0002 is optional, for master. It passes MAT_SRF_USE_EXPECTED_DESC in
each_worker_jsonb() and each_worker(), as elements_worker_jsonb()
already does, which saves a catalog lookup per call.
Peak memory for one query on master (2.1M calls each, 212k for
pg_stat_get_activity), as master / with 0001 / with 0002 alone:
- jsonb_each in a correlated subquery: 620 MB / 2 MB / 2 MB
- jsonb_each_text: 618 MB / 2 MB / 2 MB
- jsonb_each in a SELECT list: 641 MB / 2 MB / 2 MB
- json_each: 643 MB / 4 MB / 4 MB
- pg_stat_get_activity with a per-row argument: 272 MB / 4 MB / 265 MB
- jsonb_array_elements (control, doesn't leak): 4 MB / 4 MB / 4 MB
Both patches apply cleanly to current master. With both applied,
meson test passes: regress, isolation, ecpg and the contrib suites,
92 tests. The TAP tests were skipped because IPC::Run wasn't installed
in the build environment.
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Fix-per-call-memory-leak-in-InitMaterializedSRF.patch | application/octet-stream | 2.7 KB |
| v1-0002-Use-the-expected-tuple-descriptor-in-json-b-_each.patch | application/octet-stream | 1.6 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2026-12-03 18:24:43 | Re: BUG #19965: btree_gist index on timetz returns too few rows for <, > and <> |
| Previous Message | Kirill Reshke | 2026-12-03 18:16:23 | Re: btree_gist: <-> fails on same-sign infinite intervals |