From 07b7a964d368e0618e1e1c788f3b998b4e571427 Mon Sep 17 00:00:00 2001 From: Alexander Korotkov Date: Fri, 20 Mar 2026 15:32:52 +0200 Subject: [PATCH] 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 Author: Andrei Lepikhov Co-authored-by: Tender Wang Co-authored-by: Alexander Korotkov Reviewed-by: Kirill Reshke Discussion: https://www.postgresql.org/message-id/flat/19435-3cc1a87f291129f1%40postgresql.org Backpatch-through: 18 --- 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(-) diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c index 5282f60e531..7249ffbfb36 100644 --- a/src/backend/rewrite/rewriteManip.c +++ b/src/backend/rewrite/rewriteManip.c @@ -744,9 +744,7 @@ ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up) bool ChangeVarNodesWalkExpression(Node *node, ChangeVarNodes_context *context) { - return expression_tree_walker(node, - ChangeVarNodes_walker, - (void *) context); + return ChangeVarNodes_walker(node, context); } /* diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index a2bd5d20b63..250b17d092f 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -8092,6 +8092,22 @@ WHERE q0.a = 1; -> Seq Scan on sj n1 (7 rows) +-- Do not forget to replace relid in bare Var join clause (bug #19435) +ALTER TABLE sl ADD COLUMN bool_col boolean; +EXPLAIN (COSTS OFF) +SELECT 1 AS c1 FROM sl sl1 LEFT JOIN (sl AS sl2 NATURAL JOIN sl AS sl3) + ON sl2.bool_col LEFT JOIN sl AS sl4 ON sl2.bool_col; + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------- + Nested Loop Left Join + -> Seq Scan on sl sl1 + -> Nested Loop Left Join + Join Filter: sl3.bool_col + -> Seq Scan on sl sl3 + Filter: (bool_col AND (a IS NOT NULL) AND (b IS NOT NULL) AND (c IS NOT NULL) AND (bool_col IS NOT NULL)) + -> Seq Scan on sl sl4 +(7 rows) + -- Check optimization disabling if it will violate special join conditions. -- Two identical joined relations satisfies self join removal conditions but -- stay in different special join infos. diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql index 7f3449e2c84..a81c5fd011f 100644 --- a/src/test/regress/sql/join.sql +++ b/src/test/regress/sql/join.sql @@ -3156,6 +3156,12 @@ SELECT * FROM (SELECT n2.a FROM sj n1, sj n2 WHERE n1.a <> n2.a) q0, sl WHERE q0.a = 1; +-- Do not forget to replace relid in bare Var join clause (bug #19435) +ALTER TABLE sl ADD COLUMN bool_col boolean; +EXPLAIN (COSTS OFF) +SELECT 1 AS c1 FROM sl sl1 LEFT JOIN (sl AS sl2 NATURAL JOIN sl AS sl3) + ON sl2.bool_col LEFT JOIN sl AS sl4 ON sl2.bool_col; + -- Check optimization disabling if it will violate special join conditions. -- Two identical joined relations satisfies self join removal conditions but -- stay in different special join infos.