pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Use tuplestore_clear instead of tuplestore_end in nodeMaterial.c
2+ messages / 1 participants
[nested] [flat]

* pgsql: Use tuplestore_clear instead of tuplestore_end in nodeMaterial.c
@ 2026-10-05 00:42  David Rowley <drowley@postgresql.org>
  0 siblings, 0 replies; 2+ messages in thread

From: David Rowley @ 2026-10-05 00:42 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Use tuplestore_clear instead of tuplestore_end in nodeMaterial.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 Material node.  1eff8279d added additional EXPLAIN output for Material
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 Material node uses
less storage than some previous rescan.  One example is:

EXPLAIN ANALYZE
SELECT count(*) FROM (VALUES(10000),(1)) v1(r1)
LEFT JOIN LATERAL (
        SELECT * FROM generate_series(1, 2) gs0
        LEFT JOIN LATERAL (
                SELECT * FROM generate_series(1, v1.r1) gs1
                FULL JOIN generate_series(1, v1.r1) gs4 ON false
        ) ss on true
) q on true;

Without this fix, the reported Material storage is for the v1.r1 = 1 case,
whereas it should consider how much was used with the v1.r1 = 10000 case
and show the maximum of each.

Technically this issue does exist in v18, but no backpatch here as
parameterized Material nodes are not that common and there have been no
bug reports about the incorrect memory reporting, so it's probably not
worth risking backpatching the change.

Author: David Rowley <dgrowleyml@gmail.com>
Reviewed-by: Tatsuo Ishii <ishii@postgresql.org>
Reviewed-by: Tatsuya Kawata <kawatatatsuya0913@gmail.com>
Discussion: https://postgr.es/m/CAApHDvoa55vcRth05Ozu5be4FawgTH-aCsZ5=Z+_UXTUzUxdQg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/03985e1e52672c70297b38a3c658fefb6faa57cf

Modified Files
--------------
src/backend/executor/nodeMaterial.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* pgsql: Use tuplestore_clear instead of tuplestore_end in nodeTableFuncs
@ 2026-10-05 02:59  David Rowley <drowley@postgresql.org>
  0 siblings, 0 replies; 2+ messages in thread

From: David Rowley @ 2026-10-05 02:59 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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@gmail.com>
Reviewed-by: David Rowley <dgrowleyml@gmail.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(-)



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-10-05 02:59 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-05 00:42 pgsql: Use tuplestore_clear instead of tuplestore_end in nodeMaterial.c David Rowley <drowley@postgresql.org>
2026-10-05 02:59 pgsql: Use tuplestore_clear instead of tuplestore_end in nodeTableFuncs David Rowley <drowley@postgresql.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox