From fa9fe13e903b8df099d30fd166e9e389063ed110 Mon Sep 17 00:00:00 2001 From: Alexander Korotkov Date: Sun, 31 Aug 2025 02:08:45 +0300 Subject: [PATCH v2] Fix the operation order in get_relation_constraints() Must do ChangeVarNodes() before eval_const_expressions(), which which might access the corresponding RTEs by their varno's. Reported-by: Alexander Lakhin Discussion: https://postgr.es/m/9b10b847-067e-45cf-bbf7-7802b6303fc0%40gmail.com Backpatch-through: 13 --- src/backend/optimizer/util/plancat.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c index 6ce4efea118..73fd57a316b 100644 --- a/src/backend/optimizer/util/plancat.c +++ b/src/backend/optimizer/util/plancat.c @@ -1406,6 +1406,14 @@ get_relation_constraints(PlannerInfo *root, cexpr = stringToNode(constr->check[i].ccbin); + /* + * Fix Vars to have the desired varno. Must do this before + * eval_const_expressions(), which might access the corresponding + * RTEs. + */ + if (varno != 1) + ChangeVarNodes(cexpr, 1, varno, 0); + /* * Run each expression through const-simplification and * canonicalization. This is not just an optimization, but is @@ -1420,10 +1428,6 @@ get_relation_constraints(PlannerInfo *root, cexpr = (Node *) canonicalize_qual((Expr *) cexpr, true); - /* Fix Vars to have the desired varno */ - if (varno != 1) - ChangeVarNodes(cexpr, 1, varno, 0); - /* * Finally, convert to implicit-AND format (that is, a List) and * append the resulting item(s) to our output list. -- 2.39.5 (Apple Git-154)