public inbox for [email protected]  
help / color / mirror / Atom feed
pgsql: Fix unsafe RTE_GROUP removal in simplify_EXISTS_query
2+ messages / 1 participants
[nested] [flat]

* pgsql: Fix unsafe RTE_GROUP removal in simplify_EXISTS_query
@ 2026-02-25 02:16  Richard Guo <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Richard Guo @ 2026-02-25 02:16 UTC (permalink / raw)
  To: [email protected]

Fix unsafe RTE_GROUP removal in simplify_EXISTS_query

When simplify_EXISTS_query removes the GROUP BY clauses from an EXISTS
subquery, it previously deleted the RTE_GROUP RTE directly from the
subquery's range table.

This approach is dangerous because deleting an RTE from the middle of
the rtable list shifts the index of any subsequent RTE, which can
silently corrupt any Var nodes in the query tree that reference those
later relations.  (Currently, this direct removal has not caused
problems because the RTE_GROUP RTE happens to always be the last entry
in the rtable list.  However, relying on that is extremely fragile and
seems like trouble waiting to happen.)

Instead of deleting the RTE_GROUP RTE, this patch converts it in-place
to be RTE_RESULT type and clears its groupexprs list.  This preserves
the length and indexing of the rtable list, ensuring all Var
references remain intact.

Reported-by: Tom Lane <[email protected]>
Author: Richard Guo <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Backpatch-through: 18

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/1c7358099cbe77bc622bc817ec4e9d919ca91fcf

Modified Files
--------------
src/backend/optimizer/plan/subselect.c | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)



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

* pgsql: Fix unsafe RTE_GROUP removal in simplify_EXISTS_query
@ 2026-02-25 02:16  Richard Guo <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Richard Guo @ 2026-02-25 02:16 UTC (permalink / raw)
  To: [email protected]

Fix unsafe RTE_GROUP removal in simplify_EXISTS_query

When simplify_EXISTS_query removes the GROUP BY clauses from an EXISTS
subquery, it previously deleted the RTE_GROUP RTE directly from the
subquery's range table.

This approach is dangerous because deleting an RTE from the middle of
the rtable list shifts the index of any subsequent RTE, which can
silently corrupt any Var nodes in the query tree that reference those
later relations.  (Currently, this direct removal has not caused
problems because the RTE_GROUP RTE happens to always be the last entry
in the rtable list.  However, relying on that is extremely fragile and
seems like trouble waiting to happen.)

Instead of deleting the RTE_GROUP RTE, this patch converts it in-place
to be RTE_RESULT type and clears its groupexprs list.  This preserves
the length and indexing of the rtable list, ensuring all Var
references remain intact.

Reported-by: Tom Lane <[email protected]>
Author: Richard Guo <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Backpatch-through: 18

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/77c7a17a6e5fefcd55edb6b47fc462a059b983dc

Modified Files
--------------
src/backend/optimizer/plan/subselect.c | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)



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


end of thread, other threads:[~2026-02-25 02:16 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-02-25 02:16 pgsql: Fix unsafe RTE_GROUP removal in simplify_EXISTS_query Richard Guo <[email protected]>
2026-02-25 02:16 pgsql: Fix unsafe RTE_GROUP removal in simplify_EXISTS_query Richard Guo <[email protected]>

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