pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix parse_cte.c's failure to examine sub-WITHs in DML statements
Date: Sat, 05 Apr 2025 19:01:59 +0000
Message-ID: <E1u18mF-002qny-2g@gemulon.postgresql.org> (raw)

Fix parse_cte.c's failure to examine sub-WITHs in DML statements.

makeDependencyGraphWalker thought that only SelectStmt nodes could
contain a WithClause.  Which was true in our original implementation
of WITH, but astonishingly we missed updating this code when we added
the ability to attach WITH to INSERT/UPDATE/DELETE (and later MERGE).
Moreover, since it was coded to deliberately block recursion to a
WithClause, even updating raw_expression_tree_walker didn't save it.

The upshot of this was that we didn't see references to outer CTE
names appearing within an inner WITH, and would neither complain about
disallowed recursion nor account for such references when sorting CTEs
into a usable order.  The lack of complaints about this is perhaps not
so surprising, because typical usage of WITH wouldn't hit either case.
Still, it's pretty broken; failing to detect recursion here leads to
assert failures or worse later on.

Fix by factoring out the processing of sub-WITHs into a new function
WalkInnerWith, and invoking that for all the statement types that
can have WITH.

Bug: #18878
Reported-by: Yu Liang <luy70@psu.edu>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/18878-a26fa5ab6be2f2cf@postgresql.org
Backpatch-through: 13

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/754a3d82d79cc3d7040cfadaf9c8cd1579684ee6

Modified Files
--------------
src/backend/parser/parse_cte.c     | 139 +++++++++++++++++++++++++------------
src/test/regress/expected/with.out |   8 +++
src/test/regress/sql/with.sql      |   7 ++
3 files changed, 111 insertions(+), 43 deletions(-)



view thread (6+ messages)  latest in thread

Message-ID: <E1u18mF-002qny-2g@gemulon.postgresql.org>
Permalink:  ../E1u18mF-002qny-2g@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1u18mF-002qny-2g@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 using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Fix parse_cte.c's failure to examine sub-WITHs in DML statements
  In-Reply-To: <E1u18mF-002qny-2g@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