From: Tom Lane Date: Mon, 23 Mar 2026 15:14:24 +0000 (-0400) Subject: Improve commentary about ChangeVarNodesWalkExpression(). X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=360dd6f7b4c522d9a48d4621feb6da0a91bd7594;p=thirdparty%2Fpostgresql.git Improve commentary about ChangeVarNodesWalkExpression(). IMO the proximate cause of the bug fixed in commit 07b7a964d was sloppy thinking about what ChangeVarNodesWalkExpression() is to be used for. Flesh out its header comment to try to improve that situation. Author: Tom Lane Discussion: https://postgr.es/m/1607553.1774017006@sss.pgh.pa.us Backpatch-through: 18 --- diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c index 7249ffbfb36..dc803a17037 100644 --- a/src/backend/rewrite/rewriteManip.c +++ b/src/backend/rewrite/rewriteManip.c @@ -672,7 +672,7 @@ ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context) * value indicating if the given node should be skipped from further processing * by ChangeVarNodes_walker. The callback is called only for expressions and * other children nodes of a Query processed by a walker. Initial processing - * of the root Query doesn't involve the callback. + * of the root Query node doesn't invoke the callback. */ void ChangeVarNodesExtended(Node *node, int rt_index, int new_index, @@ -737,9 +737,16 @@ ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up) } /* - * ChangeVarNodesWalkExpression - process expression within the custom - * callback provided to the - * ChangeVarNodesExtended. + * ChangeVarNodesWalkExpression - process subexpression within a callback + * function passed to ChangeVarNodesExtended. + * + * This is intended to be used by a callback that needs to recursively + * process subexpressions of some node being visited by an outer + * ChangeVarNodesExtended call (not letting ChangeVarNodes_walker do that). + * Hence, we invoke ChangeVarNodes_walker directly. This means that if + * the passed Node is a Query node, it will be treated as a sub-Query, + * so sublevels_up will be incremented immediately. Do not apply this + * to a top-level Query node, or you'll likely get wrong results. */ bool ChangeVarNodesWalkExpression(Node *node, ChangeVarNodes_context *context)