Skip to content

Commit 18c0da8

Browse files
committed
Split QTW_EXAMINE_RTES flag into QTW_EXAMINE_RTES_BEFORE/_AFTER.
This change allows callers of query_tree_walker() to choose whether to visit an RTE before or after visiting the contents of the RTE (i.e., prefix or postfix tree order). All existing users of QTW_EXAMINE_RTES want the QTW_EXAMINE_RTES_BEFORE behavior, but an upcoming patch will want QTW_EXAMINE_RTES_AFTER, and it seems like a potentially useful change on its own. Andreas Karlsson (extracted from CTE inlining patch) Discussion: https://postgr.es/m/[email protected]
1 parent ff750ce commit 18c0da8

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

src/backend/nodes/nodeFuncs.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,7 @@ expression_tree_walker(Node *node,
22552255
* Some callers want to suppress visitation of certain items in the sub-Query,
22562256
* typically because they need to process them specially, or don't actually
22572257
* want to recurse into subqueries. This is supported by the flags argument,
2258-
* which is the bitwise OR of flag values to suppress visitation of
2258+
* which is the bitwise OR of flag values to add or suppress visitation of
22592259
* indicated items. (More flag bits may be added as needed.)
22602260
*/
22612261
bool
@@ -2314,8 +2314,12 @@ range_table_walker(List *rtable,
23142314
{
23152315
RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
23162316

2317-
/* For historical reasons, visiting RTEs is not the default */
2318-
if (flags & QTW_EXAMINE_RTES)
2317+
/*
2318+
* Walkers might need to examine the RTE node itself either before or
2319+
* after visiting its contents (or, conceivably, both). Note that if
2320+
* you specify neither flag, the walker won't visit the RTE at all.
2321+
*/
2322+
if (flags & QTW_EXAMINE_RTES_BEFORE)
23192323
if (walker(rte, context))
23202324
return true;
23212325

@@ -2355,6 +2359,10 @@ range_table_walker(List *rtable,
23552359

23562360
if (walker(rte->securityQuals, context))
23572361
return true;
2362+
2363+
if (flags & QTW_EXAMINE_RTES_AFTER)
2364+
if (walker(rte, context))
2365+
return true;
23582366
}
23592367
return false;
23602368
}

src/backend/optimizer/plan/setrefs.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte)
340340
(void) query_tree_walker(rte->subquery,
341341
flatten_rtes_walker,
342342
(void *) glob,
343-
QTW_EXAMINE_RTES);
343+
QTW_EXAMINE_RTES_BEFORE);
344344
}
345345

346346
static bool
@@ -363,7 +363,7 @@ flatten_rtes_walker(Node *node, PlannerGlobal *glob)
363363
return query_tree_walker((Query *) node,
364364
flatten_rtes_walker,
365365
(void *) glob,
366-
QTW_EXAMINE_RTES);
366+
QTW_EXAMINE_RTES_BEFORE);
367367
}
368368
return expression_tree_walker(node, flatten_rtes_walker,
369369
(void *) glob);

src/backend/rewrite/rewriteManip.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ IncrementVarSublevelsUp_walker(Node *node,
761761
result = query_tree_walker((Query *) node,
762762
IncrementVarSublevelsUp_walker,
763763
(void *) context,
764-
QTW_EXAMINE_RTES);
764+
QTW_EXAMINE_RTES_BEFORE);
765765
context->min_sublevels_up--;
766766
return result;
767767
}
@@ -785,7 +785,7 @@ IncrementVarSublevelsUp(Node *node, int delta_sublevels_up,
785785
query_or_expression_tree_walker(node,
786786
IncrementVarSublevelsUp_walker,
787787
(void *) &context,
788-
QTW_EXAMINE_RTES);
788+
QTW_EXAMINE_RTES_BEFORE);
789789
}
790790

791791
/*
@@ -804,7 +804,7 @@ IncrementVarSublevelsUp_rtable(List *rtable, int delta_sublevels_up,
804804
range_table_walker(rtable,
805805
IncrementVarSublevelsUp_walker,
806806
(void *) &context,
807-
QTW_EXAMINE_RTES);
807+
QTW_EXAMINE_RTES_BEFORE);
808808
}
809809

810810

src/include/nodes/nodeFuncs.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
#define QTW_IGNORE_RC_SUBQUERIES 0x03 /* both of above */
2323
#define QTW_IGNORE_JOINALIASES 0x04 /* JOIN alias var lists */
2424
#define QTW_IGNORE_RANGE_TABLE 0x08 /* skip rangetable entirely */
25-
#define QTW_EXAMINE_RTES 0x10 /* examine RTEs */
26-
#define QTW_DONT_COPY_QUERY 0x20 /* do not copy top Query */
25+
#define QTW_EXAMINE_RTES_BEFORE 0x10 /* examine RTE nodes before their
26+
* contents */
27+
#define QTW_EXAMINE_RTES_AFTER 0x20 /* examine RTE nodes after their
28+
* contents */
29+
#define QTW_DONT_COPY_QUERY 0x40 /* do not copy top Query */
2730

2831
/* callback function for check_functions_in_node */
2932
typedef bool (*check_function_callback) (Oid func_id, void *context);

0 commit comments

Comments
 (0)