pgsql: Use tuplestore_clear instead of tuplestore_end in nodeTableFuncs

From: David Rowley <drowley(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Use tuplestore_clear instead of tuplestore_end in nodeTableFuncs
Date: 2026-10-05 02:59:04
Message-ID: E1xDYuu-00000000Lfa-05NX@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Use tuplestore_clear instead of tuplestore_end in nodeTableFuncscan.c

Clearing the tuplestore rather than ending it avoids the need for
various memory allocations, so is slightly more efficient. However, the
main reason to do this is to correctly track the maximum storage used by
the Table Function Scan node. 40708acd65 added additional EXPLAIN
output for Table Function Scan nodes and that output does claim to be
showing "Maximum Storage", which is not true, as the maximums could be
lost after tuplestore_end() is called during a rescan.

This causes misreporting when the final rescan of a Table Function Scan
node uses less storage than some previous rescan. One example is:

EXPLAIN ANALYZE
SELECT count(*)
FROM (VALUES ((SELECT jsonb_agg(g) FROM generate_series(1, 100000) g)),
('[1]'::jsonb)) v(j),
LATERAL JSON_TABLE(v.j, '$[*]' COLUMNS (x int PATH '$')) jt;

Without this fix, the reported Table Function Scan storage is for the
'[1]'::jsonb case, whereas it should consider how much was used with the
larger document and show the maximum of all rescans.

TableFuncNext() used a NULL tupstore to tell that the rows still had to
be fetched, so add a tupstore_filled flag for that, similar to what
908a96861 did for WindowAgg.

Technically, this issue does exist in v18, but no backpatch as this is a
fairly minor issue which was only noticed during a review of a similar
feature being proposed for Function Scans.

Author: Tatsuya Kawata <kawatatatsuya0913(at)gmail(dot)com>
Reviewed-by: David Rowley <dgrowleyml(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAHza6qdXjcqXv2aGU7OCakG0OmEsy9-jfP0Gpe2EL+k-AidOhw@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a7f8cd811d65d1a51be87bdb890d7ebf7d49aa45

Modified Files
--------------
src/backend/executor/nodeTableFuncscan.c | 29 +++++++++++++++++------------
src/include/nodes/execnodes.h | 1 +
2 files changed, 18 insertions(+), 12 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2026-10-05 03:48:13 pgsql: Reject CR and LF characters in backup labels
Previous Message Michael Paquier 2026-10-05 02:12:28 pgsql: Skip isolation tests that terminate other backends on Windows