pgsql: Fix transient memory leak for SRFs in FROM. - Mailing list pgsql-committers

From Andres Freund
Subject pgsql: Fix transient memory leak for SRFs in FROM.
Date
Msg-id E1jRS5w-00036K-Ji@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix transient memory leak for SRFs in FROM.

In a9c35cf85ca I changed ExecMakeTableFunctionResult() to dynamically
allocate the FunctionCallInfo used to call the SRF. Unfortunately I
did not account for the fact that the surrounding memory context has
query lifetime, leading to a leak till the end of the query.

In most cases the leak is fairly inconsequential, but if the
FunctionScan is done many times in the query, the leak can add
up. This happens e.g. if the function scan is on the inner side of a
nested loop, due to a lateral join.
EXPLAIN SELECT sum(f) FROM generate_series(1, 100000000) g(i), generate_series(i, i+1) f;
quickly shows the leak.

Instead of explicitly freeing the FunctionCallInfo it seems better to
make sure all the per-set temporary state in
ExecMakeTableFunctionResult() is cleaned up wholesale. Currently
that's probably just the FunctionCallInfo allocation, but since
there's some initialization work, and since there's already an
appropriate context, this seems like a more robust approach.

Bug: #16112
Reported-By: Ben Cornett
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/16112-4448bbf55a404189%40postgresql.org
Backpatch: 12, a9c35cf85ca

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/299298bc873374ed49fa2f39944c09ac62bd75e3

Modified Files
--------------
src/backend/executor/execSRF.c | 50 ++++++++++++++++++++++++------------------
1 file changed, 29 insertions(+), 21 deletions(-)


pgsql-committers by date:

Previous
From: Fujii Masao
Date:
Subject: pgsql: Fix option related issues in pg_verifybackup.
Next
From: Andres Freund
Date:
Subject: pgsql: Fix transient memory leak for SRFs in FROM.