pgsql: Fix self-join removal to update bare Var references in join clau

From: Alexander Korotkov <akorotkov(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix self-join removal to update bare Var references in join clau
Date: 2026-03-20 13:46:40
Message-ID: E1w3aBT-000eJ1-1z@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix self-join removal to update bare Var references in join clauses

Self-join removal failed to update Var nodes when the join clause was a
bare Var (e.g., ON t1.bool_col) rather than an expression containing
Vars. ChangeVarNodesWalkExpression() used expression_tree_walker(),
which descends into child nodes but does not process the top-level node
itself. When a bare Var referencing the removed relation appeared as
the clause, its varno was left unchanged, leading to "no relation entry
for relid N" errors.

Fix by calling ChangeVarNodes_walker() directly instead of
expression_tree_walker(), so the top-level node is also processed.

Bug: #19435
Reported-by: Hang Ammmkilo <ammmkilo(at)163(dot)com>
Author: Andrei Lepikhov <lepihov(at)gmail(dot)com>
Co-authored-by: Tender Wang <tndrwang(at)gmail(dot)com>
Co-authored-by: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Reviewed-by: Kirill Reshke <reshkekirill(at)gmail(dot)com>
Discussion: https://www.postgresql.org/message-id/flat/19435-3cc1a87f291129f1%40postgresql.org
Backpatch-through: 18

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/07b7a964d368e0618e1e1c788f3b998b4e571427

Modified Files
--------------
src/backend/rewrite/rewriteManip.c | 4 +---
src/test/regress/expected/join.out | 16 ++++++++++++++++
src/test/regress/sql/join.sql | 6 ++++++
3 files changed, 23 insertions(+), 3 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alexander Korotkov 2026-03-20 13:47:20 pgsql: Fix self-join removal to update bare Var references in join clau
Previous Message Álvaro Herrera 2026-03-20 13:42:40 pgsql: SET NOT NULL: Call object-alter hook only after the catalog chan