pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Use tuplestore_clear instead of tuplestore_end in nodeMaterial.c
Date: Mon, 05 Oct 2026 00:42:44 +0000
Message-ID: <E1xDWmy-00000000KfA-0N8r@gemulon.postgresql.org> (raw)

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(-)



view thread (2+ messages)  latest in thread

Message-ID: <E1xDWmy-00000000KfA-0N8r@gemulon.postgresql.org>
Permalink:  ../E1xDWmy-00000000KfA-0N8r@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xDWmy-00000000KfA-0N8r@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: drowley@postgresql.org
  Subject: Re: pgsql: Use tuplestore_clear instead of tuplestore_end in nodeMaterial.c
  In-Reply-To: <E1xDWmy-00000000KfA-0N8r@gemulon.postgresql.org>
  References: <E1xDWmy-00000000KfA-0N8r@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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