PostgreSQL Source Code git master
setrefs.c File Reference
#include "postgres.h"
#include "access/transam.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/planmain.h"
#include "optimizer/planner.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
#include "parser/parse_relation.h"
#include "rewrite/rewriteManip.h"
#include "tcop/utility.h"
#include "utils/syscache.h"
Include dependency graph for setrefs.c:

Go to the source code of this file.

Data Structures

struct  tlist_vinfo
 
struct  indexed_tlist
 
struct  fix_scan_expr_context
 
struct  fix_join_expr_context
 
struct  fix_upper_expr_context
 
struct  fix_windowagg_cond_context
 
struct  flatten_rtes_walker_context
 

Macros

#define NUM_EXEC_TLIST(parentplan)   ((parentplan)->plan_rows)
 
#define NUM_EXEC_QUAL(parentplan)   ((parentplan)->plan_rows * 2.0)
 
#define ISREGCLASSCONST(con)
 
#define fix_scan_list(root, lst, rtoffset, num_exec)    ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset, num_exec))
 

Enumerations

enum  NullingRelsMatch { NRM_EQUAL , NRM_SUBSET , NRM_SUPERSET }
 

Functions

static void add_rtes_to_flat_rtable (PlannerInfo *root, bool recursing)
 
static void flatten_unplanned_rtes (PlannerGlobal *glob, RangeTblEntry *rte)
 
static bool flatten_rtes_walker (Node *node, flatten_rtes_walker_context *cxt)
 
static void add_rte_to_flat_rtable (PlannerGlobal *glob, List *rteperminfos, RangeTblEntry *rte)
 
static Planset_plan_refs (PlannerInfo *root, Plan *plan, int rtoffset)
 
static Planset_indexonlyscan_references (PlannerInfo *root, IndexOnlyScan *plan, int rtoffset)
 
static Planset_subqueryscan_references (PlannerInfo *root, SubqueryScan *plan, int rtoffset)
 
static Planclean_up_removed_plan_level (Plan *parent, Plan *child)
 
static void set_foreignscan_references (PlannerInfo *root, ForeignScan *fscan, int rtoffset)
 
static void set_customscan_references (PlannerInfo *root, CustomScan *cscan, int rtoffset)
 
static Planset_append_references (PlannerInfo *root, Append *aplan, int rtoffset)
 
static Planset_mergeappend_references (PlannerInfo *root, MergeAppend *mplan, int rtoffset)
 
static void set_hash_references (PlannerInfo *root, Plan *plan, int rtoffset)
 
static Relids offset_relid_set (Relids relids, int rtoffset)
 
static Nodefix_scan_expr (PlannerInfo *root, Node *node, int rtoffset, double num_exec)
 
static Nodefix_scan_expr_mutator (Node *node, fix_scan_expr_context *context)
 
static bool fix_scan_expr_walker (Node *node, fix_scan_expr_context *context)
 
static void set_join_references (PlannerInfo *root, Join *join, int rtoffset)
 
static void set_upper_references (PlannerInfo *root, Plan *plan, int rtoffset)
 
static void set_param_references (PlannerInfo *root, Plan *plan)
 
static Nodeconvert_combining_aggrefs (Node *node, void *context)
 
static void set_dummy_tlist_references (Plan *plan, int rtoffset)
 
static indexed_tlistbuild_tlist_index (List *tlist)
 
static Varsearch_indexed_tlist_for_var (Var *var, indexed_tlist *itlist, int newvarno, int rtoffset, NullingRelsMatch nrm_match)
 
static Varsearch_indexed_tlist_for_phv (PlaceHolderVar *phv, indexed_tlist *itlist, int newvarno, NullingRelsMatch nrm_match)
 
static Varsearch_indexed_tlist_for_non_var (Expr *node, indexed_tlist *itlist, int newvarno)
 
static Varsearch_indexed_tlist_for_sortgroupref (Expr *node, Index sortgroupref, indexed_tlist *itlist, int newvarno)
 
static Listfix_join_expr (PlannerInfo *root, List *clauses, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel, int rtoffset, NullingRelsMatch nrm_match, double num_exec)
 
static Nodefix_join_expr_mutator (Node *node, fix_join_expr_context *context)
 
static Nodefix_upper_expr (PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, int newvarno, int rtoffset, NullingRelsMatch nrm_match, double num_exec)
 
static Nodefix_upper_expr_mutator (Node *node, fix_upper_expr_context *context)
 
static Listset_returning_clause_references (PlannerInfo *root, List *rlist, Plan *topplan, Index resultRelation, int rtoffset)
 
static Listset_windowagg_runcondition_references (PlannerInfo *root, List *runcondition, Plan *plan)
 
Planset_plan_references (PlannerInfo *root, Plan *plan)
 
bool trivial_subqueryscan (SubqueryScan *plan)
 
static int register_partpruneinfo (PlannerInfo *root, int part_prune_index, int rtoffset)
 
static VarcopyVar (Var *var)
 
static void fix_expr_common (PlannerInfo *root, Node *node)
 
static Nodefix_param_node (PlannerInfo *root, Param *p)
 
static Nodefix_alternative_subplan (PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
 
static indexed_tlistbuild_tlist_index_other_vars (List *tlist, int ignore_rel)
 
static Nodefix_windowagg_condition_expr_mutator (Node *node, fix_windowagg_cond_context *context)
 
static Listfix_windowagg_condition_expr (PlannerInfo *root, List *runcondition, indexed_tlist *subplan_itlist)
 
Paramfind_minmax_agg_replacement_param (PlannerInfo *root, Aggref *aggref)
 
void record_plan_function_dependency (PlannerInfo *root, Oid funcid)
 
void record_plan_type_dependency (PlannerInfo *root, Oid typid)
 
void extract_query_dependencies (Node *query, List **relationOids, List **invalItems, bool *hasRowSecurity)
 
bool extract_query_dependencies_walker (Node *node, PlannerInfo *context)
 

Macro Definition Documentation

◆ fix_scan_list

#define fix_scan_list (   root,
  lst,
  rtoffset,
  num_exec 
)     ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset, num_exec))

Definition at line 130 of file setrefs.c.

◆ ISREGCLASSCONST

#define ISREGCLASSCONST (   con)
Value:
(((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \
!(con)->constisnull)

Definition at line 126 of file setrefs.c.

◆ NUM_EXEC_QUAL

#define NUM_EXEC_QUAL (   parentplan)    ((parentplan)->plan_rows * 2.0)

Definition at line 117 of file setrefs.c.

◆ NUM_EXEC_TLIST

#define NUM_EXEC_TLIST (   parentplan)    ((parentplan)->plan_rows)

Definition at line 116 of file setrefs.c.

Enumeration Type Documentation

◆ NullingRelsMatch

Enumerator
NRM_EQUAL 
NRM_SUBSET 
NRM_SUPERSET 

Definition at line 34 of file setrefs.c.

35{
36 NRM_EQUAL, /* expect exact match of nullingrels */
37 NRM_SUBSET, /* actual Var may have a subset of input */
38 NRM_SUPERSET, /* actual Var may have a superset of input */
NullingRelsMatch
Definition: setrefs.c:35
@ NRM_EQUAL
Definition: setrefs.c:36
@ NRM_SUPERSET
Definition: setrefs.c:38
@ NRM_SUBSET
Definition: setrefs.c:37

Function Documentation

◆ add_rte_to_flat_rtable()

static void add_rte_to_flat_rtable ( PlannerGlobal glob,
List rteperminfos,
RangeTblEntry rte 
)
static

Definition at line 538 of file setrefs.c.

540{
541 RangeTblEntry *newrte;
542
543 /* flat copy to duplicate all the scalar fields */
544 newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
545 memcpy(newrte, rte, sizeof(RangeTblEntry));
546
547 /* zap unneeded sub-structure */
548 newrte->tablesample = NULL;
549 newrte->subquery = NULL;
550 newrte->joinaliasvars = NIL;
551 newrte->joinleftcols = NIL;
552 newrte->joinrightcols = NIL;
553 newrte->join_using_alias = NULL;
554 newrte->functions = NIL;
555 newrte->tablefunc = NULL;
556 newrte->values_lists = NIL;
557 newrte->coltypes = NIL;
558 newrte->coltypmods = NIL;
559 newrte->colcollations = NIL;
560 newrte->groupexprs = NIL;
561 newrte->securityQuals = NIL;
562
563 glob->finalrtable = lappend(glob->finalrtable, newrte);
564
565 /*
566 * If it's a plain relation RTE (or a subquery that was once a view
567 * reference), add the relation OID to relationOids.
568 *
569 * We do this even though the RTE might be unreferenced in the plan tree;
570 * this would correspond to cases such as views that were expanded, child
571 * tables that were eliminated by constraint exclusion, etc. Schema
572 * invalidation on such a rel must still force rebuilding of the plan.
573 *
574 * Note we don't bother to avoid making duplicate list entries. We could,
575 * but it would probably cost more cycles than it would save.
576 */
577 if (newrte->rtekind == RTE_RELATION ||
578 (newrte->rtekind == RTE_SUBQUERY && OidIsValid(newrte->relid)))
579 glob->relationOids = lappend_oid(glob->relationOids, newrte->relid);
580
581 /*
582 * Add a copy of the RTEPermissionInfo, if any, corresponding to this RTE
583 * to the flattened global list.
584 */
585 if (rte->perminfoindex > 0)
586 {
587 RTEPermissionInfo *perminfo;
588 RTEPermissionInfo *newperminfo;
589
590 /* Get the existing one from this query's rteperminfos. */
591 perminfo = getRTEPermissionInfo(rteperminfos, newrte);
592
593 /*
594 * Add a new one to finalrteperminfos and copy the contents of the
595 * existing one into it. Note that addRTEPermissionInfo() also
596 * updates newrte->perminfoindex to point to newperminfo in
597 * finalrteperminfos.
598 */
599 newrte->perminfoindex = 0; /* expected by addRTEPermissionInfo() */
600 newperminfo = addRTEPermissionInfo(&glob->finalrteperminfos, newrte);
601 memcpy(newperminfo, perminfo, sizeof(RTEPermissionInfo));
602 }
603}
#define OidIsValid(objectId)
Definition: c.h:732
List * lappend(List *list, void *datum)
Definition: list.c:339
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
void * palloc(Size size)
Definition: mcxt.c:1317
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
@ RTE_SUBQUERY
Definition: parsenodes.h:1027
@ RTE_RELATION
Definition: parsenodes.h:1026
#define NIL
Definition: pg_list.h:68
List * relationOids
Definition: pathnodes.h:135
List * finalrteperminfos
Definition: pathnodes.h:120
List * finalrtable
Definition: pathnodes.h:117
TableFunc * tablefunc
Definition: parsenodes.h:1193
struct TableSampleClause * tablesample
Definition: parsenodes.h:1107
Query * subquery
Definition: parsenodes.h:1113
List * values_lists
Definition: parsenodes.h:1199
List * functions
Definition: parsenodes.h:1186
RTEKind rtekind
Definition: parsenodes.h:1056

References addRTEPermissionInfo(), PlannerGlobal::finalrtable, PlannerGlobal::finalrteperminfos, RangeTblEntry::functions, getRTEPermissionInfo(), lappend(), lappend_oid(), NIL, OidIsValid, palloc(), PlannerGlobal::relationOids, RangeTblEntry::relid, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblEntry::subquery, RangeTblEntry::tablefunc, RangeTblEntry::tablesample, and RangeTblEntry::values_lists.

Referenced by add_rtes_to_flat_rtable(), and flatten_rtes_walker().

◆ add_rtes_to_flat_rtable()

static void add_rtes_to_flat_rtable ( PlannerInfo root,
bool  recursing 
)
static

Definition at line 392 of file setrefs.c.

393{
394 PlannerGlobal *glob = root->glob;
395 Index rti;
396 ListCell *lc;
397
398 /*
399 * Add the query's own RTEs to the flattened rangetable.
400 *
401 * At top level, we must add all RTEs so that their indexes in the
402 * flattened rangetable match up with their original indexes. When
403 * recursing, we only care about extracting relation RTEs (and subquery
404 * RTEs that were once relation RTEs).
405 */
406 foreach(lc, root->parse->rtable)
407 {
408 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
409
410 if (!recursing || rte->rtekind == RTE_RELATION ||
411 (rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)))
412 add_rte_to_flat_rtable(glob, root->parse->rteperminfos, rte);
413 }
414
415 /*
416 * If there are any dead subqueries, they are not referenced in the Plan
417 * tree, so we must add RTEs contained in them to the flattened rtable
418 * separately. (If we failed to do this, the executor would not perform
419 * expected permission checks for tables mentioned in such subqueries.)
420 *
421 * Note: this pass over the rangetable can't be combined with the previous
422 * one, because that would mess up the numbering of the live RTEs in the
423 * flattened rangetable.
424 */
425 rti = 1;
426 foreach(lc, root->parse->rtable)
427 {
428 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
429
430 /*
431 * We should ignore inheritance-parent RTEs: their contents have been
432 * pulled up into our rangetable already. Also ignore any subquery
433 * RTEs without matching RelOptInfos, as they likewise have been
434 * pulled up.
435 */
436 if (rte->rtekind == RTE_SUBQUERY && !rte->inh &&
437 rti < root->simple_rel_array_size)
438 {
439 RelOptInfo *rel = root->simple_rel_array[rti];
440
441 if (rel != NULL)
442 {
443 Assert(rel->relid == rti); /* sanity check on array */
444
445 /*
446 * The subquery might never have been planned at all, if it
447 * was excluded on the basis of self-contradictory constraints
448 * in our query level. In this case apply
449 * flatten_unplanned_rtes.
450 *
451 * If it was planned but the result rel is dummy, we assume
452 * that it has been omitted from our plan tree (see
453 * set_subquery_pathlist), and recurse to pull up its RTEs.
454 *
455 * Otherwise, it should be represented by a SubqueryScan node
456 * somewhere in our plan tree, and we'll pull up its RTEs when
457 * we process that plan node.
458 *
459 * However, if we're recursing, then we should pull up RTEs
460 * whether the subquery is dummy or not, because we've found
461 * that some upper query level is treating this one as dummy,
462 * and so we won't scan this level's plan tree at all.
463 */
464 if (rel->subroot == NULL)
465 flatten_unplanned_rtes(glob, rte);
466 else if (recursing ||
468 UPPERREL_FINAL, NULL)))
470 }
471 }
472 rti++;
473 }
474}
#define Assert(condition)
Definition: c.h:815
unsigned int Index
Definition: c.h:571
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1964
@ UPPERREL_FINAL
Definition: pathnodes.h:79
#define lfirst(lc)
Definition: pg_list.h:172
tree ctl root
Definition: radixtree.h:1857
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1458
static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
Definition: setrefs.c:392
static void add_rte_to_flat_rtable(PlannerGlobal *glob, List *rteperminfos, RangeTblEntry *rte)
Definition: setrefs.c:538
static void flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte)
Definition: setrefs.c:481
Index relid
Definition: pathnodes.h:924
PlannerInfo * subroot
Definition: pathnodes.h:959

References add_rte_to_flat_rtable(), add_rtes_to_flat_rtable(), Assert, fetch_upper_rel(), flatten_unplanned_rtes(), RangeTblEntry::inh, IS_DUMMY_REL, lfirst, OidIsValid, RangeTblEntry::relid, RelOptInfo::relid, root, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, RelOptInfo::subroot, and UPPERREL_FINAL.

Referenced by add_rtes_to_flat_rtable(), and set_plan_references().

◆ build_tlist_index()

static indexed_tlist * build_tlist_index ( List tlist)
static

Definition at line 2727 of file setrefs.c.

2728{
2729 indexed_tlist *itlist;
2730 tlist_vinfo *vinfo;
2731 ListCell *l;
2732
2733 /* Create data structure with enough slots for all tlist entries */
2734 itlist = (indexed_tlist *)
2735 palloc(offsetof(indexed_tlist, vars) +
2736 list_length(tlist) * sizeof(tlist_vinfo));
2737
2738 itlist->tlist = tlist;
2739 itlist->has_ph_vars = false;
2740 itlist->has_non_vars = false;
2741
2742 /* Find the Vars and fill in the index array */
2743 vinfo = itlist->vars;
2744 foreach(l, tlist)
2745 {
2746 TargetEntry *tle = (TargetEntry *) lfirst(l);
2747
2748 if (tle->expr && IsA(tle->expr, Var))
2749 {
2750 Var *var = (Var *) tle->expr;
2751
2752 vinfo->varno = var->varno;
2753 vinfo->varattno = var->varattno;
2754 vinfo->resno = tle->resno;
2755 vinfo->varnullingrels = var->varnullingrels;
2756 vinfo++;
2757 }
2758 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2759 itlist->has_ph_vars = true;
2760 else
2761 itlist->has_non_vars = true;
2762 }
2763
2764 itlist->num_vars = (vinfo - itlist->vars);
2765
2766 return itlist;
2767}
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
static int list_length(const List *l)
Definition: pg_list.h:152
Expr * expr
Definition: primnodes.h:2219
AttrNumber resno
Definition: primnodes.h:2221
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269
tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]
Definition: setrefs.c:55
bool has_ph_vars
Definition: setrefs.c:53
bool has_non_vars
Definition: setrefs.c:54
int num_vars
Definition: setrefs.c:52
List * tlist
Definition: setrefs.c:51
AttrNumber resno
Definition: setrefs.c:45
Bitmapset * varnullingrels
Definition: setrefs.c:46
int varno
Definition: setrefs.c:43
AttrNumber varattno
Definition: setrefs.c:44
Definition: regcomp.c:282

References TargetEntry::expr, indexed_tlist::has_non_vars, indexed_tlist::has_ph_vars, IsA, lfirst, list_length(), indexed_tlist::num_vars, palloc(), tlist_vinfo::resno, TargetEntry::resno, indexed_tlist::tlist, tlist_vinfo::varattno, Var::varattno, tlist_vinfo::varno, Var::varno, tlist_vinfo::varnullingrels, and indexed_tlist::vars.

Referenced by set_customscan_references(), set_foreignscan_references(), set_hash_references(), set_indexonlyscan_references(), set_join_references(), set_plan_refs(), set_upper_references(), and set_windowagg_runcondition_references().

◆ build_tlist_index_other_vars()

static indexed_tlist * build_tlist_index_other_vars ( List tlist,
int  ignore_rel 
)
static

Definition at line 2778 of file setrefs.c.

2779{
2780 indexed_tlist *itlist;
2781 tlist_vinfo *vinfo;
2782 ListCell *l;
2783
2784 /* Create data structure with enough slots for all tlist entries */
2785 itlist = (indexed_tlist *)
2786 palloc(offsetof(indexed_tlist, vars) +
2787 list_length(tlist) * sizeof(tlist_vinfo));
2788
2789 itlist->tlist = tlist;
2790 itlist->has_ph_vars = false;
2791 itlist->has_non_vars = false;
2792
2793 /* Find the desired Vars and fill in the index array */
2794 vinfo = itlist->vars;
2795 foreach(l, tlist)
2796 {
2797 TargetEntry *tle = (TargetEntry *) lfirst(l);
2798
2799 if (tle->expr && IsA(tle->expr, Var))
2800 {
2801 Var *var = (Var *) tle->expr;
2802
2803 if (var->varno != ignore_rel)
2804 {
2805 vinfo->varno = var->varno;
2806 vinfo->varattno = var->varattno;
2807 vinfo->resno = tle->resno;
2808 vinfo->varnullingrels = var->varnullingrels;
2809 vinfo++;
2810 }
2811 }
2812 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2813 itlist->has_ph_vars = true;
2814 }
2815
2816 itlist->num_vars = (vinfo - itlist->vars);
2817
2818 return itlist;
2819}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76

References TargetEntry::expr, indexed_tlist::has_non_vars, indexed_tlist::has_ph_vars, if(), IsA, lfirst, list_length(), indexed_tlist::num_vars, palloc(), tlist_vinfo::resno, TargetEntry::resno, indexed_tlist::tlist, tlist_vinfo::varattno, Var::varattno, tlist_vinfo::varno, Var::varno, tlist_vinfo::varnullingrels, and indexed_tlist::vars.

Referenced by set_returning_clause_references().

◆ clean_up_removed_plan_level()

static Plan * clean_up_removed_plan_level ( Plan parent,
Plan child 
)
static

Definition at line 1535 of file setrefs.c.

1536{
1537 /*
1538 * We have to be sure we don't lose any initplans, so move any that were
1539 * attached to the parent plan to the child. If any are parallel-unsafe,
1540 * the child is no longer parallel-safe. As a cosmetic matter, also add
1541 * the initplans' run costs to the child's costs.
1542 */
1543 if (parent->initPlan)
1544 {
1545 Cost initplan_cost;
1546 bool unsafe_initplans;
1547
1549 &initplan_cost, &unsafe_initplans);
1550 child->startup_cost += initplan_cost;
1551 child->total_cost += initplan_cost;
1552 if (unsafe_initplans)
1553 child->parallel_safe = false;
1554
1555 /*
1556 * Attach plans this way so that parent's initplans are processed
1557 * before any pre-existing initplans of the child. Probably doesn't
1558 * matter, but let's preserve the ordering just in case.
1559 */
1560 child->initPlan = list_concat(parent->initPlan,
1561 child->initPlan);
1562 }
1563
1564 /*
1565 * We also have to transfer the parent's column labeling info into the
1566 * child, else columns sent to client will be improperly labeled if this
1567 * is the topmost plan level. resjunk and so on may be important too.
1568 */
1570
1571 return child;
1572}
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
double Cost
Definition: nodes.h:251
Cost total_cost
Definition: plannodes.h:133
Cost startup_cost
Definition: plannodes.h:132
bool parallel_safe
Definition: plannodes.h:145
List * targetlist
Definition: plannodes.h:156
List * initPlan
Definition: plannodes.h:160
void SS_compute_initplan_cost(List *init_plans, Cost *initplan_cost_p, bool *unsafe_initplans_p)
Definition: subselect.c:2232
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:318

References apply_tlist_labeling(), Plan::initPlan, list_concat(), Plan::parallel_safe, SS_compute_initplan_cost(), Plan::startup_cost, Plan::targetlist, and Plan::total_cost.

Referenced by set_append_references(), set_mergeappend_references(), and set_subqueryscan_references().

◆ convert_combining_aggrefs()

static Node * convert_combining_aggrefs ( Node node,
void *  context 
)
static

Definition at line 2592 of file setrefs.c.

2593{
2594 if (node == NULL)
2595 return NULL;
2596 if (IsA(node, Aggref))
2597 {
2598 Aggref *orig_agg = (Aggref *) node;
2599 Aggref *child_agg;
2600 Aggref *parent_agg;
2601
2602 /* Assert we've not chosen to partial-ize any unsupported cases */
2603 Assert(orig_agg->aggorder == NIL);
2604 Assert(orig_agg->aggdistinct == NIL);
2605
2606 /*
2607 * Since aggregate calls can't be nested, we needn't recurse into the
2608 * arguments. But for safety, flat-copy the Aggref node itself rather
2609 * than modifying it in-place.
2610 */
2611 child_agg = makeNode(Aggref);
2612 memcpy(child_agg, orig_agg, sizeof(Aggref));
2613
2614 /*
2615 * For the parent Aggref, we want to copy all the fields of the
2616 * original aggregate *except* the args list, which we'll replace
2617 * below, and the aggfilter expression, which should be applied only
2618 * by the child not the parent. Rather than explicitly knowing about
2619 * all the other fields here, we can momentarily modify child_agg to
2620 * provide a suitable source for copyObject.
2621 */
2622 child_agg->args = NIL;
2623 child_agg->aggfilter = NULL;
2624 parent_agg = copyObject(child_agg);
2625 child_agg->args = orig_agg->args;
2626 child_agg->aggfilter = orig_agg->aggfilter;
2627
2628 /*
2629 * Now, set up child_agg to represent the first phase of partial
2630 * aggregation. For now, assume serialization is required.
2631 */
2633
2634 /*
2635 * And set up parent_agg to represent the second phase.
2636 */
2637 parent_agg->args = list_make1(makeTargetEntry((Expr *) child_agg,
2638 1, NULL, false));
2640
2641 return (Node *) parent_agg;
2642 }
2644}
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:242
#define expression_tree_mutator(n, m, c)
Definition: nodeFuncs.h:155
#define copyObject(obj)
Definition: nodes.h:224
@ AGGSPLIT_FINAL_DESERIAL
Definition: nodes.h:381
@ AGGSPLIT_INITIAL_SERIAL
Definition: nodes.h:379
#define makeNode(_type_)
Definition: nodes.h:155
#define list_make1(x1)
Definition: pg_list.h:212
void mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
Definition: planner.c:5624
static Node * convert_combining_aggrefs(Node *node, void *context)
Definition: setrefs.c:2592
List * aggdistinct
Definition: primnodes.h:491
List * args
Definition: primnodes.h:485
Expr * aggfilter
Definition: primnodes.h:494
List * aggorder
Definition: primnodes.h:488
Definition: nodes.h:129

References Aggref::aggdistinct, Aggref::aggfilter, Aggref::aggorder, AGGSPLIT_FINAL_DESERIAL, AGGSPLIT_INITIAL_SERIAL, Aggref::args, Assert, convert_combining_aggrefs(), copyObject, expression_tree_mutator, IsA, list_make1, makeNode, makeTargetEntry(), mark_partial_aggref(), and NIL.

Referenced by convert_combining_aggrefs(), and set_plan_refs().

◆ copyVar()

static Var * copyVar ( Var var)
inlinestatic

Definition at line 1976 of file setrefs.c.

1977{
1978 Var *newvar = (Var *) palloc(sizeof(Var));
1979
1980 *newvar = *var;
1981 return newvar;
1982}

References palloc().

Referenced by fix_join_expr_mutator(), fix_scan_expr_mutator(), and search_indexed_tlist_for_var().

◆ extract_query_dependencies()

void extract_query_dependencies ( Node query,
List **  relationOids,
List **  invalItems,
bool *  hasRowSecurity 
)

Definition at line 3603 of file setrefs.c.

3607{
3608 PlannerGlobal glob;
3610
3611 /* Make up dummy planner state so we can use this module's machinery */
3612 MemSet(&glob, 0, sizeof(glob));
3613 glob.type = T_PlannerGlobal;
3614 glob.relationOids = NIL;
3615 glob.invalItems = NIL;
3616 /* Hack: we use glob.dependsOnRole to collect hasRowSecurity flags */
3617 glob.dependsOnRole = false;
3618
3619 MemSet(&root, 0, sizeof(root));
3620 root.type = T_PlannerInfo;
3621 root.glob = &glob;
3622
3624
3625 *relationOids = glob.relationOids;
3626 *invalItems = glob.invalItems;
3627 *hasRowSecurity = glob.dependsOnRole;
3628}
#define MemSet(start, val, len)
Definition: c.h:977
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:3639
bool dependsOnRole
Definition: pathnodes.h:156
List * invalItems
Definition: pathnodes.h:138

References PlannerGlobal::dependsOnRole, extract_query_dependencies_walker(), PlannerGlobal::invalItems, MemSet, NIL, PlannerGlobal::relationOids, and root.

Referenced by CompleteCachedPlan(), and RevalidateCachedQuery().

◆ extract_query_dependencies_walker()

bool extract_query_dependencies_walker ( Node node,
PlannerInfo context 
)

Definition at line 3639 of file setrefs.c.

3640{
3641 if (node == NULL)
3642 return false;
3643 Assert(!IsA(node, PlaceHolderVar));
3644 if (IsA(node, Query))
3645 {
3646 Query *query = (Query *) node;
3647 ListCell *lc;
3648
3649 if (query->commandType == CMD_UTILITY)
3650 {
3651 /*
3652 * This logic must handle any utility command for which parse
3653 * analysis was nontrivial (cf. stmt_requires_parse_analysis).
3654 *
3655 * Notably, CALL requires its own processing.
3656 */
3657 if (IsA(query->utilityStmt, CallStmt))
3658 {
3659 CallStmt *callstmt = (CallStmt *) query->utilityStmt;
3660
3661 /* We need not examine funccall, just the transformed exprs */
3662 (void) extract_query_dependencies_walker((Node *) callstmt->funcexpr,
3663 context);
3664 (void) extract_query_dependencies_walker((Node *) callstmt->outargs,
3665 context);
3666 return false;
3667 }
3668
3669 /*
3670 * Ignore other utility statements, except those (such as EXPLAIN)
3671 * that contain a parsed-but-not-planned query. For those, we
3672 * just need to transfer our attention to the contained query.
3673 */
3674 query = UtilityContainsQuery(query->utilityStmt);
3675 if (query == NULL)
3676 return false;
3677 }
3678
3679 /* Remember if any Query has RLS quals applied by rewriter */
3680 if (query->hasRowSecurity)
3681 context->glob->dependsOnRole = true;
3682
3683 /* Collect relation OIDs in this Query's rtable */
3684 foreach(lc, query->rtable)
3685 {
3686 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3687
3688 if (rte->rtekind == RTE_RELATION ||
3689 (rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)) ||
3690 (rte->rtekind == RTE_NAMEDTUPLESTORE && OidIsValid(rte->relid)))
3691 context->glob->relationOids =
3692 lappend_oid(context->glob->relationOids, rte->relid);
3693 }
3694
3695 /* And recurse into the query's subexpressions */
3697 context, 0);
3698 }
3699 /* Extract function dependencies and check for regclass Consts */
3700 fix_expr_common(context, node);
3702 context);
3703}
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:158
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:153
@ CMD_UTILITY
Definition: nodes.h:270
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1033
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1998
FuncExpr * funcexpr
Definition: parsenodes.h:3583
List * outargs
Definition: parsenodes.h:3585
PlannerGlobal * glob
Definition: pathnodes.h:208
List * rtable
Definition: parsenodes.h:170
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:136
Query * UtilityContainsQuery(Node *parsetree)
Definition: utility.c:2179

References Assert, CMD_UTILITY, Query::commandType, PlannerGlobal::dependsOnRole, expression_tree_walker, extract_query_dependencies_walker(), fix_expr_common(), CallStmt::funcexpr, PlannerInfo::glob, IsA, lappend_oid(), lfirst, OidIsValid, CallStmt::outargs, query_tree_walker, PlannerGlobal::relationOids, RangeTblEntry::relid, Query::rtable, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, UtilityContainsQuery(), and Query::utilityStmt.

Referenced by expression_planner_with_deps(), extract_query_dependencies(), and extract_query_dependencies_walker().

◆ find_minmax_agg_replacement_param()

Param * find_minmax_agg_replacement_param ( PlannerInfo root,
Aggref aggref 
)

Definition at line 3489 of file setrefs.c.

3490{
3491 if (root->minmax_aggs != NIL &&
3492 list_length(aggref->args) == 1)
3493 {
3494 TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
3495 ListCell *lc;
3496
3497 foreach(lc, root->minmax_aggs)
3498 {
3499 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
3500
3501 if (mminfo->aggfnoid == aggref->aggfnoid &&
3502 equal(mminfo->target, curTarget->expr))
3503 return mminfo->param;
3504 }
3505 }
3506 return NULL;
3507}
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
#define linitial(l)
Definition: pg_list.h:178
Oid aggfnoid
Definition: primnodes.h:461
Param * param
Definition: pathnodes.h:3150
Expr * target
Definition: pathnodes.h:3135

References MinMaxAggInfo::aggfnoid, Aggref::aggfnoid, Aggref::args, equal(), TargetEntry::expr, lfirst, linitial, list_length(), NIL, MinMaxAggInfo::param, root, and MinMaxAggInfo::target.

Referenced by finalize_primnode(), fix_scan_expr_mutator(), and fix_upper_expr_mutator().

◆ fix_alternative_subplan()

static Node * fix_alternative_subplan ( PlannerInfo root,
AlternativeSubPlan asplan,
double  num_exec 
)
static

Definition at line 2124 of file setrefs.c.

2126{
2127 SubPlan *bestplan = NULL;
2128 Cost bestcost = 0;
2129 ListCell *lc;
2130
2131 /*
2132 * Compute the estimated cost of each subplan assuming num_exec
2133 * executions, and keep the cheapest one. In event of exact equality of
2134 * estimates, we prefer the later plan; this is a bit arbitrary, but in
2135 * current usage it biases us to break ties against fast-start subplans.
2136 */
2137 Assert(asplan->subplans != NIL);
2138
2139 foreach(lc, asplan->subplans)
2140 {
2141 SubPlan *curplan = (SubPlan *) lfirst(lc);
2142 Cost curcost;
2143
2144 curcost = curplan->startup_cost + num_exec * curplan->per_call_cost;
2145 if (bestplan == NULL || curcost <= bestcost)
2146 {
2147 bestplan = curplan;
2148 bestcost = curcost;
2149 }
2150
2151 /* Also mark all subplans that are in AlternativeSubPlans */
2152 root->isAltSubplan[curplan->plan_id - 1] = true;
2153 }
2154
2155 /* Mark the subplan we selected */
2156 root->isUsedSubplan[bestplan->plan_id - 1] = true;
2157
2158 return (Node *) bestplan;
2159}
int plan_id
Definition: primnodes.h:1087
Cost startup_cost
Definition: primnodes.h:1110
Cost per_call_cost
Definition: primnodes.h:1111

References Assert, lfirst, NIL, SubPlan::per_call_cost, SubPlan::plan_id, root, SubPlan::startup_cost, and AlternativeSubPlan::subplans.

Referenced by fix_join_expr_mutator(), fix_scan_expr_mutator(), and fix_upper_expr_mutator().

◆ fix_expr_common()

static void fix_expr_common ( PlannerInfo root,
Node node 
)
static

Definition at line 1998 of file setrefs.c.

1999{
2000 /* We assume callers won't call us on a NULL pointer */
2001 if (IsA(node, Aggref))
2002 {
2004 ((Aggref *) node)->aggfnoid);
2005 }
2006 else if (IsA(node, WindowFunc))
2007 {
2009 ((WindowFunc *) node)->winfnoid);
2010 }
2011 else if (IsA(node, FuncExpr))
2012 {
2014 ((FuncExpr *) node)->funcid);
2015 }
2016 else if (IsA(node, OpExpr))
2017 {
2018 set_opfuncid((OpExpr *) node);
2020 ((OpExpr *) node)->opfuncid);
2021 }
2022 else if (IsA(node, DistinctExpr))
2023 {
2024 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
2026 ((DistinctExpr *) node)->opfuncid);
2027 }
2028 else if (IsA(node, NullIfExpr))
2029 {
2030 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
2032 ((NullIfExpr *) node)->opfuncid);
2033 }
2034 else if (IsA(node, ScalarArrayOpExpr))
2035 {
2036 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
2037
2038 set_sa_opfuncid(saop);
2039 record_plan_function_dependency(root, saop->opfuncid);
2040
2041 if (OidIsValid(saop->hashfuncid))
2042 record_plan_function_dependency(root, saop->hashfuncid);
2043
2044 if (OidIsValid(saop->negfuncid))
2045 record_plan_function_dependency(root, saop->negfuncid);
2046 }
2047 else if (IsA(node, Const))
2048 {
2049 Const *con = (Const *) node;
2050
2051 /* Check for regclass reference */
2052 if (ISREGCLASSCONST(con))
2053 root->glob->relationOids =
2054 lappend_oid(root->glob->relationOids,
2055 DatumGetObjectId(con->constvalue));
2056 }
2057 else if (IsA(node, GroupingFunc))
2058 {
2059 GroupingFunc *g = (GroupingFunc *) node;
2060 AttrNumber *grouping_map = root->grouping_map;
2061
2062 /* If there are no grouping sets, we don't need this. */
2063
2064 Assert(grouping_map || g->cols == NIL);
2065
2066 if (grouping_map)
2067 {
2068 ListCell *lc;
2069 List *cols = NIL;
2070
2071 foreach(lc, g->refs)
2072 {
2073 cols = lappend_int(cols, grouping_map[lfirst_int(lc)]);
2074 }
2075
2076 Assert(!g->cols || equal(cols, g->cols));
2077
2078 if (!g->cols)
2079 g->cols = cols;
2080 }
2081 }
2082}
int16 AttrNumber
Definition: attnum.h:21
List * lappend_int(List *list, int datum)
Definition: list.c:357
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1883
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1872
#define lfirst_int(lc)
Definition: pg_list.h:173
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:247
void record_plan_function_dependency(PlannerInfo *root, Oid funcid)
Definition: setrefs.c:3522
#define ISREGCLASSCONST(con)
Definition: setrefs.c:126
Definition: pg_list.h:54

References Assert, DatumGetObjectId(), equal(), IsA, ISREGCLASSCONST, lappend_int(), lappend_oid(), lfirst_int, NIL, OidIsValid, record_plan_function_dependency(), root, set_opfuncid(), and set_sa_opfuncid().

Referenced by extract_query_dependencies_walker(), fix_join_expr_mutator(), fix_scan_expr_mutator(), fix_scan_expr_walker(), and fix_upper_expr_mutator().

◆ fix_join_expr()

static List * fix_join_expr ( PlannerInfo root,
List clauses,
indexed_tlist outer_itlist,
indexed_tlist inner_itlist,
Index  acceptable_rel,
int  rtoffset,
NullingRelsMatch  nrm_match,
double  num_exec 
)
static

Definition at line 3072 of file setrefs.c.

3080{
3081 fix_join_expr_context context;
3082
3083 context.root = root;
3084 context.outer_itlist = outer_itlist;
3085 context.inner_itlist = inner_itlist;
3086 context.acceptable_rel = acceptable_rel;
3087 context.rtoffset = rtoffset;
3088 context.nrm_match = nrm_match;
3089 context.num_exec = num_exec;
3090 return (List *) fix_join_expr_mutator((Node *) clauses, &context);
3091}
static Node * fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
Definition: setrefs.c:3094
NullingRelsMatch nrm_match
Definition: setrefs.c:72
indexed_tlist * outer_itlist
Definition: setrefs.c:68
PlannerInfo * root
Definition: setrefs.c:67
indexed_tlist * inner_itlist
Definition: setrefs.c:69

References fix_join_expr_context::acceptable_rel, fix_join_expr_mutator(), fix_join_expr_context::inner_itlist, fix_join_expr_context::nrm_match, fix_join_expr_context::num_exec, fix_join_expr_context::outer_itlist, fix_join_expr_context::root, root, and fix_join_expr_context::rtoffset.

Referenced by set_join_references(), set_plan_refs(), and set_returning_clause_references().

◆ fix_join_expr_mutator()

static Node * fix_join_expr_mutator ( Node node,
fix_join_expr_context context 
)
static

Definition at line 3094 of file setrefs.c.

3095{
3096 Var *newvar;
3097
3098 if (node == NULL)
3099 return NULL;
3100 if (IsA(node, Var))
3101 {
3102 Var *var = (Var *) node;
3103
3104 /*
3105 * Verify that Vars with non-default varreturningtype only appear in
3106 * the RETURNING list, and refer to the target relation.
3107 */
3109 {
3110 if (context->inner_itlist != NULL ||
3111 context->outer_itlist == NULL ||
3112 context->acceptable_rel == 0)
3113 elog(ERROR, "variable returning old/new found outside RETURNING list");
3114 if (var->varno != context->acceptable_rel)
3115 elog(ERROR, "wrong varno %d (expected %d) for variable returning old/new",
3116 var->varno, context->acceptable_rel);
3117 }
3118
3119 /* Look for the var in the input tlists, first in the outer */
3120 if (context->outer_itlist)
3121 {
3122 newvar = search_indexed_tlist_for_var(var,
3123 context->outer_itlist,
3124 OUTER_VAR,
3125 context->rtoffset,
3126 context->nrm_match);
3127 if (newvar)
3128 return (Node *) newvar;
3129 }
3130
3131 /* then in the inner. */
3132 if (context->inner_itlist)
3133 {
3134 newvar = search_indexed_tlist_for_var(var,
3135 context->inner_itlist,
3136 INNER_VAR,
3137 context->rtoffset,
3138 context->nrm_match);
3139 if (newvar)
3140 return (Node *) newvar;
3141 }
3142
3143 /* If it's for acceptable_rel, adjust and return it */
3144 if (var->varno == context->acceptable_rel)
3145 {
3146 var = copyVar(var);
3147 var->varno += context->rtoffset;
3148 if (var->varnosyn > 0)
3149 var->varnosyn += context->rtoffset;
3150 return (Node *) var;
3151 }
3152
3153 /* No referent found for Var */
3154 elog(ERROR, "variable not found in subplan target lists");
3155 }
3156 if (IsA(node, PlaceHolderVar))
3157 {
3158 PlaceHolderVar *phv = (PlaceHolderVar *) node;
3159
3160 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
3161 if (context->outer_itlist && context->outer_itlist->has_ph_vars)
3162 {
3163 newvar = search_indexed_tlist_for_phv(phv,
3164 context->outer_itlist,
3165 OUTER_VAR,
3166 context->nrm_match);
3167 if (newvar)
3168 return (Node *) newvar;
3169 }
3170 if (context->inner_itlist && context->inner_itlist->has_ph_vars)
3171 {
3172 newvar = search_indexed_tlist_for_phv(phv,
3173 context->inner_itlist,
3174 INNER_VAR,
3175 context->nrm_match);
3176 if (newvar)
3177 return (Node *) newvar;
3178 }
3179
3180 /* If not supplied by input plans, evaluate the contained expr */
3181 /* XXX can we assert something about phnullingrels? */
3182 return fix_join_expr_mutator((Node *) phv->phexpr, context);
3183 }
3184 /* Try matching more complex expressions too, if tlists have any */
3185 if (context->outer_itlist && context->outer_itlist->has_non_vars)
3186 {
3187 newvar = search_indexed_tlist_for_non_var((Expr *) node,
3188 context->outer_itlist,
3189 OUTER_VAR);
3190 if (newvar)
3191 return (Node *) newvar;
3192 }
3193 if (context->inner_itlist && context->inner_itlist->has_non_vars)
3194 {
3195 newvar = search_indexed_tlist_for_non_var((Expr *) node,
3196 context->inner_itlist,
3197 INNER_VAR);
3198 if (newvar)
3199 return (Node *) newvar;
3200 }
3201 /* Special cases (apply only AFTER failing to match to lower tlist) */
3202 if (IsA(node, Param))
3203 return fix_param_node(context->root, (Param *) node);
3204 if (IsA(node, AlternativeSubPlan))
3206 (AlternativeSubPlan *) node,
3207 context->num_exec),
3208 context);
3209 fix_expr_common(context->root, node);
3210 return expression_tree_mutator(node, fix_join_expr_mutator, context);
3211}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
@ VAR_RETURNING_DEFAULT
Definition: primnodes.h:256
#define OUTER_VAR
Definition: primnodes.h:243
#define INNER_VAR
Definition: primnodes.h:242
static Node * fix_param_node(PlannerInfo *root, Param *p)
Definition: setrefs.c:2093
static Var * search_indexed_tlist_for_non_var(Expr *node, indexed_tlist *itlist, int newvarno)
Definition: setrefs.c:2954
static Var * search_indexed_tlist_for_phv(PlaceHolderVar *phv, indexed_tlist *itlist, int newvarno, NullingRelsMatch nrm_match)
Definition: setrefs.c:2901
static Var * copyVar(Var *var)
Definition: setrefs.c:1976
static Node * fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
Definition: setrefs.c:2124
static Var * search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, int newvarno, int rtoffset, NullingRelsMatch nrm_match)
Definition: setrefs.c:2836
VarReturningType varreturningtype
Definition: primnodes.h:297

References fix_join_expr_context::acceptable_rel, copyVar(), elog, ERROR, expression_tree_mutator, fix_alternative_subplan(), fix_expr_common(), fix_join_expr_mutator(), fix_param_node(), indexed_tlist::has_non_vars, indexed_tlist::has_ph_vars, fix_join_expr_context::inner_itlist, INNER_VAR, IsA, fix_join_expr_context::nrm_match, fix_join_expr_context::num_exec, fix_join_expr_context::outer_itlist, OUTER_VAR, fix_join_expr_context::root, fix_join_expr_context::rtoffset, search_indexed_tlist_for_non_var(), search_indexed_tlist_for_phv(), search_indexed_tlist_for_var(), VAR_RETURNING_DEFAULT, Var::varno, and Var::varreturningtype.

Referenced by fix_join_expr(), and fix_join_expr_mutator().

◆ fix_param_node()

static Node * fix_param_node ( PlannerInfo root,
Param p 
)
static

Definition at line 2093 of file setrefs.c.

2094{
2095 if (p->paramkind == PARAM_MULTIEXPR)
2096 {
2097 int subqueryid = p->paramid >> 16;
2098 int colno = p->paramid & 0xFFFF;
2099 List *params;
2100
2101 if (subqueryid <= 0 ||
2102 subqueryid > list_length(root->multiexpr_params))
2103 elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
2104 params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
2105 if (colno <= 0 || colno > list_length(params))
2106 elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
2107 return copyObject(list_nth(params, colno - 1));
2108 }
2109 return (Node *) copyObject(p);
2110}
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
@ PARAM_MULTIEXPR
Definition: primnodes.h:387
int paramid
Definition: primnodes.h:394
ParamKind paramkind
Definition: primnodes.h:393

References copyObject, elog, ERROR, list_length(), list_nth(), PARAM_MULTIEXPR, Param::paramid, Param::paramkind, and root.

Referenced by fix_join_expr_mutator(), fix_scan_expr_mutator(), and fix_upper_expr_mutator().

◆ fix_scan_expr()

static Node * fix_scan_expr ( PlannerInfo root,
Node node,
int  rtoffset,
double  num_exec 
)
static

Definition at line 2180 of file setrefs.c.

2181{
2182 fix_scan_expr_context context;
2183
2184 context.root = root;
2185 context.rtoffset = rtoffset;
2186 context.num_exec = num_exec;
2187
2188 if (rtoffset != 0 ||
2189 root->multiexpr_params != NIL ||
2190 root->glob->lastPHId != 0 ||
2191 root->minmax_aggs != NIL ||
2192 root->hasAlternativeSubPlans)
2193 {
2194 return fix_scan_expr_mutator(node, &context);
2195 }
2196 else
2197 {
2198 /*
2199 * If rtoffset == 0, we don't need to change any Vars, and if there
2200 * are no MULTIEXPR subqueries then we don't need to replace
2201 * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
2202 * we won't need to remove them, and if there are no minmax Aggrefs we
2203 * won't need to replace them, and if there are no AlternativeSubPlans
2204 * we won't need to remove them. Then it's OK to just scribble on the
2205 * input node tree instead of copying (since the only change, filling
2206 * in any unset opfuncid fields, is harmless). This saves just enough
2207 * cycles to be noticeable on trivial queries.
2208 */
2209 (void) fix_scan_expr_walker(node, &context);
2210 return node;
2211 }
2212}
static Node * fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:2215
static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:2280
PlannerInfo * root
Definition: setrefs.c:60

References fix_scan_expr_mutator(), fix_scan_expr_walker(), NIL, fix_scan_expr_context::num_exec, fix_scan_expr_context::root, root, and fix_scan_expr_context::rtoffset.

Referenced by set_plan_refs().

◆ fix_scan_expr_mutator()

static Node * fix_scan_expr_mutator ( Node node,
fix_scan_expr_context context 
)
static

Definition at line 2215 of file setrefs.c.

2216{
2217 if (node == NULL)
2218 return NULL;
2219 if (IsA(node, Var))
2220 {
2221 Var *var = copyVar((Var *) node);
2222
2223 Assert(var->varlevelsup == 0);
2224
2225 /*
2226 * We should not see Vars marked INNER_VAR, OUTER_VAR, or ROWID_VAR.
2227 * But an indexqual expression could contain INDEX_VAR Vars.
2228 */
2229 Assert(var->varno != INNER_VAR);
2230 Assert(var->varno != OUTER_VAR);
2231 Assert(var->varno != ROWID_VAR);
2232 if (!IS_SPECIAL_VARNO(var->varno))
2233 var->varno += context->rtoffset;
2234 if (var->varnosyn > 0)
2235 var->varnosyn += context->rtoffset;
2236 return (Node *) var;
2237 }
2238 if (IsA(node, Param))
2239 return fix_param_node(context->root, (Param *) node);
2240 if (IsA(node, Aggref))
2241 {
2242 Aggref *aggref = (Aggref *) node;
2243 Param *aggparam;
2244
2245 /* See if the Aggref should be replaced by a Param */
2246 aggparam = find_minmax_agg_replacement_param(context->root, aggref);
2247 if (aggparam != NULL)
2248 {
2249 /* Make a copy of the Param for paranoia's sake */
2250 return (Node *) copyObject(aggparam);
2251 }
2252 /* If no match, just fall through to process it normally */
2253 }
2254 if (IsA(node, CurrentOfExpr))
2255 {
2256 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
2257
2258 Assert(!IS_SPECIAL_VARNO(cexpr->cvarno));
2259 cexpr->cvarno += context->rtoffset;
2260 return (Node *) cexpr;
2261 }
2262 if (IsA(node, PlaceHolderVar))
2263 {
2264 /* At scan level, we should always just evaluate the contained expr */
2265 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2266
2267 /* XXX can we assert something about phnullingrels? */
2268 return fix_scan_expr_mutator((Node *) phv->phexpr, context);
2269 }
2270 if (IsA(node, AlternativeSubPlan))
2272 (AlternativeSubPlan *) node,
2273 context->num_exec),
2274 context);
2275 fix_expr_common(context->root, node);
2276 return expression_tree_mutator(node, fix_scan_expr_mutator, context);
2277}
#define ROWID_VAR
Definition: primnodes.h:245
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:247
Param * find_minmax_agg_replacement_param(PlannerInfo *root, Aggref *aggref)
Definition: setrefs.c:3489
Index varlevelsup
Definition: primnodes.h:294

References Assert, copyObject, copyVar(), CurrentOfExpr::cvarno, expression_tree_mutator, find_minmax_agg_replacement_param(), fix_alternative_subplan(), fix_expr_common(), fix_param_node(), fix_scan_expr_mutator(), INNER_VAR, IS_SPECIAL_VARNO, IsA, fix_scan_expr_context::num_exec, OUTER_VAR, fix_scan_expr_context::root, ROWID_VAR, fix_scan_expr_context::rtoffset, Var::varlevelsup, and Var::varno.

Referenced by fix_scan_expr(), and fix_scan_expr_mutator().

◆ fix_scan_expr_walker()

static bool fix_scan_expr_walker ( Node node,
fix_scan_expr_context context 
)
static

Definition at line 2280 of file setrefs.c.

2281{
2282 if (node == NULL)
2283 return false;
2284 Assert(!(IsA(node, Var) && ((Var *) node)->varno == ROWID_VAR));
2285 Assert(!IsA(node, PlaceHolderVar));
2286 Assert(!IsA(node, AlternativeSubPlan));
2287 fix_expr_common(context->root, node);
2288 return expression_tree_walker(node, fix_scan_expr_walker, context);
2289}

References Assert, expression_tree_walker, fix_expr_common(), fix_scan_expr_walker(), IsA, fix_scan_expr_context::root, and ROWID_VAR.

Referenced by fix_scan_expr(), and fix_scan_expr_walker().

◆ fix_upper_expr()

static Node * fix_upper_expr ( PlannerInfo root,
Node node,
indexed_tlist subplan_itlist,
int  newvarno,
int  rtoffset,
NullingRelsMatch  nrm_match,
double  num_exec 
)
static

Definition at line 3246 of file setrefs.c.

3253{
3254 fix_upper_expr_context context;
3255
3256 context.root = root;
3257 context.subplan_itlist = subplan_itlist;
3258 context.newvarno = newvarno;
3259 context.rtoffset = rtoffset;
3260 context.nrm_match = nrm_match;
3261 context.num_exec = num_exec;
3262 return fix_upper_expr_mutator(node, &context);
3263}
static Node * fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
Definition: setrefs.c:3266
indexed_tlist * subplan_itlist
Definition: setrefs.c:79
PlannerInfo * root
Definition: setrefs.c:78
NullingRelsMatch nrm_match
Definition: setrefs.c:82

References fix_upper_expr_mutator(), fix_upper_expr_context::newvarno, fix_upper_expr_context::nrm_match, fix_upper_expr_context::num_exec, fix_upper_expr_context::root, root, fix_upper_expr_context::rtoffset, and fix_upper_expr_context::subplan_itlist.

Referenced by set_customscan_references(), set_foreignscan_references(), set_hash_references(), set_indexonlyscan_references(), set_join_references(), and set_upper_references().

◆ fix_upper_expr_mutator()

static Node * fix_upper_expr_mutator ( Node node,
fix_upper_expr_context context 
)
static

Definition at line 3266 of file setrefs.c.

3267{
3268 Var *newvar;
3269
3270 if (node == NULL)
3271 return NULL;
3272 if (IsA(node, Var))
3273 {
3274 Var *var = (Var *) node;
3275
3276 newvar = search_indexed_tlist_for_var(var,
3277 context->subplan_itlist,
3278 context->newvarno,
3279 context->rtoffset,
3280 context->nrm_match);
3281 if (!newvar)
3282 elog(ERROR, "variable not found in subplan target list");
3283 return (Node *) newvar;
3284 }
3285 if (IsA(node, PlaceHolderVar))
3286 {
3287 PlaceHolderVar *phv = (PlaceHolderVar *) node;
3288
3289 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
3290 if (context->subplan_itlist->has_ph_vars)
3291 {
3292 newvar = search_indexed_tlist_for_phv(phv,
3293 context->subplan_itlist,
3294 context->newvarno,
3295 context->nrm_match);
3296 if (newvar)
3297 return (Node *) newvar;
3298 }
3299 /* If not supplied by input plan, evaluate the contained expr */
3300 /* XXX can we assert something about phnullingrels? */
3301 return fix_upper_expr_mutator((Node *) phv->phexpr, context);
3302 }
3303 /* Try matching more complex expressions too, if tlist has any */
3304 if (context->subplan_itlist->has_non_vars)
3305 {
3306 newvar = search_indexed_tlist_for_non_var((Expr *) node,
3307 context->subplan_itlist,
3308 context->newvarno);
3309 if (newvar)
3310 return (Node *) newvar;
3311 }
3312 /* Special cases (apply only AFTER failing to match to lower tlist) */
3313 if (IsA(node, Param))
3314 return fix_param_node(context->root, (Param *) node);
3315 if (IsA(node, Aggref))
3316 {
3317 Aggref *aggref = (Aggref *) node;
3318 Param *aggparam;
3319
3320 /* See if the Aggref should be replaced by a Param */
3321 aggparam = find_minmax_agg_replacement_param(context->root, aggref);
3322 if (aggparam != NULL)
3323 {
3324 /* Make a copy of the Param for paranoia's sake */
3325 return (Node *) copyObject(aggparam);
3326 }
3327 /* If no match, just fall through to process it normally */
3328 }
3329 if (IsA(node, AlternativeSubPlan))
3331 (AlternativeSubPlan *) node,
3332 context->num_exec),
3333 context);
3334 fix_expr_common(context->root, node);
3335 return expression_tree_mutator(node, fix_upper_expr_mutator, context);
3336}

References copyObject, elog, ERROR, expression_tree_mutator, find_minmax_agg_replacement_param(), fix_alternative_subplan(), fix_expr_common(), fix_param_node(), fix_upper_expr_mutator(), indexed_tlist::has_non_vars, indexed_tlist::has_ph_vars, IsA, fix_upper_expr_context::newvarno, fix_upper_expr_context::nrm_match, fix_upper_expr_context::num_exec, fix_upper_expr_context::root, fix_upper_expr_context::rtoffset, search_indexed_tlist_for_non_var(), search_indexed_tlist_for_phv(), search_indexed_tlist_for_var(), and fix_upper_expr_context::subplan_itlist.

Referenced by fix_upper_expr(), and fix_upper_expr_mutator().

◆ fix_windowagg_condition_expr()

static List * fix_windowagg_condition_expr ( PlannerInfo root,
List runcondition,
indexed_tlist subplan_itlist 
)
static

Definition at line 3441 of file setrefs.c.

3444{
3446
3447 context.root = root;
3448 context.subplan_itlist = subplan_itlist;
3449 context.newvarno = 0;
3450
3451 return (List *) fix_windowagg_condition_expr_mutator((Node *) runcondition,
3452 &context);
3453}
static Node * fix_windowagg_condition_expr_mutator(Node *node, fix_windowagg_cond_context *context)
Definition: setrefs.c:3411
indexed_tlist * subplan_itlist
Definition: setrefs.c:89
PlannerInfo * root
Definition: setrefs.c:88

References fix_windowagg_condition_expr_mutator(), fix_windowagg_cond_context::newvarno, fix_windowagg_cond_context::root, root, and fix_windowagg_cond_context::subplan_itlist.

Referenced by set_windowagg_runcondition_references().

◆ fix_windowagg_condition_expr_mutator()

static Node * fix_windowagg_condition_expr_mutator ( Node node,
fix_windowagg_cond_context context 
)
static

Definition at line 3411 of file setrefs.c.

3413{
3414 if (node == NULL)
3415 return NULL;
3416
3417 if (IsA(node, WindowFunc))
3418 {
3419 Var *newvar;
3420
3421 newvar = search_indexed_tlist_for_non_var((Expr *) node,
3422 context->subplan_itlist,
3423 context->newvarno);
3424 if (newvar)
3425 return (Node *) newvar;
3426 elog(ERROR, "WindowFunc not found in subplan target lists");
3427 }
3428
3429 return expression_tree_mutator(node,
3431 context);
3432}

References elog, ERROR, expression_tree_mutator, fix_windowagg_condition_expr_mutator(), IsA, fix_windowagg_cond_context::newvarno, search_indexed_tlist_for_non_var(), and fix_windowagg_cond_context::subplan_itlist.

Referenced by fix_windowagg_condition_expr(), and fix_windowagg_condition_expr_mutator().

◆ flatten_rtes_walker()

static bool flatten_rtes_walker ( Node node,
flatten_rtes_walker_context cxt 
)
static

Definition at line 493 of file setrefs.c.

494{
495 if (node == NULL)
496 return false;
497 if (IsA(node, RangeTblEntry))
498 {
499 RangeTblEntry *rte = (RangeTblEntry *) node;
500
501 /* As above, we need only save relation RTEs and former relations */
502 if (rte->rtekind == RTE_RELATION ||
503 (rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)))
504 add_rte_to_flat_rtable(cxt->glob, cxt->query->rteperminfos, rte);
505 return false;
506 }
507 if (IsA(node, Query))
508 {
509 /*
510 * Recurse into subselects. Must update cxt->query to this query so
511 * that the rtable and rteperminfos correspond with each other.
512 */
513 Query *save_query = cxt->query;
514 bool result;
515
516 cxt->query = (Query *) node;
517 result = query_tree_walker((Query *) node,
519 cxt,
521 cxt->query = save_query;
522 return result;
523 }
525}
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:27
static bool flatten_rtes_walker(Node *node, flatten_rtes_walker_context *cxt)
Definition: setrefs.c:493
PlannerGlobal * glob
Definition: setrefs.c:96

References add_rte_to_flat_rtable(), expression_tree_walker, flatten_rtes_walker(), flatten_rtes_walker_context::glob, IsA, OidIsValid, QTW_EXAMINE_RTES_BEFORE, flatten_rtes_walker_context::query, query_tree_walker, RangeTblEntry::relid, RTE_RELATION, RTE_SUBQUERY, and RangeTblEntry::rtekind.

Referenced by flatten_rtes_walker(), and flatten_unplanned_rtes().

◆ flatten_unplanned_rtes()

static void flatten_unplanned_rtes ( PlannerGlobal glob,
RangeTblEntry rte 
)
static

Definition at line 481 of file setrefs.c.

482{
483 flatten_rtes_walker_context cxt = {glob, rte->subquery};
484
485 /* Use query_tree_walker to find all RTEs in the parse tree */
486 (void) query_tree_walker(rte->subquery,
488 &cxt,
490}

References flatten_rtes_walker(), QTW_EXAMINE_RTES_BEFORE, query_tree_walker, and RangeTblEntry::subquery.

Referenced by add_rtes_to_flat_rtable().

◆ offset_relid_set()

static Relids offset_relid_set ( Relids  relids,
int  rtoffset 
)
static

Definition at line 1954 of file setrefs.c.

1955{
1956 Relids result = NULL;
1957 int rtindex;
1958
1959 /* If there's no offset to apply, we needn't recompute the value */
1960 if (rtoffset == 0)
1961 return relids;
1962 rtindex = -1;
1963 while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1964 result = bms_add_member(result, rtindex + rtoffset);
1965 return result;
1966}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815

References bms_add_member(), and bms_next_member().

Referenced by register_partpruneinfo(), set_append_references(), set_customscan_references(), set_foreignscan_references(), and set_mergeappend_references().

◆ record_plan_function_dependency()

void record_plan_function_dependency ( PlannerInfo root,
Oid  funcid 
)

Definition at line 3522 of file setrefs.c.

3523{
3524 /*
3525 * For performance reasons, we don't bother to track built-in functions;
3526 * we just assume they'll never change (or at least not in ways that'd
3527 * invalidate plans using them). For this purpose we can consider a
3528 * built-in function to be one with OID less than FirstUnpinnedObjectId.
3529 * Note that the OID generator guarantees never to generate such an OID
3530 * after startup, even at OID wraparound.
3531 */
3532 if (funcid >= (Oid) FirstUnpinnedObjectId)
3533 {
3534 PlanInvalItem *inval_item = makeNode(PlanInvalItem);
3535
3536 /*
3537 * It would work to use any syscache on pg_proc, but the easiest is
3538 * PROCOID since we already have the function's OID at hand. Note
3539 * that plancache.c knows we use PROCOID.
3540 */
3541 inval_item->cacheId = PROCOID;
3542 inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
3543 ObjectIdGetDatum(funcid));
3544
3545 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3546 }
3547}
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
unsigned int Oid
Definition: postgres_ext.h:32
uint32 hashValue
Definition: plannodes.h:1589
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:118
#define FirstUnpinnedObjectId
Definition: transam.h:196

References PlanInvalItem::cacheId, FirstUnpinnedObjectId, GetSysCacheHashValue1, PlanInvalItem::hashValue, lappend(), makeNode, ObjectIdGetDatum(), and root.

Referenced by fix_expr_common(), inline_function(), and inline_set_returning_function().

◆ record_plan_type_dependency()

void record_plan_type_dependency ( PlannerInfo root,
Oid  typid 
)

Definition at line 3562 of file setrefs.c.

3563{
3564 /*
3565 * As in record_plan_function_dependency, ignore the possibility that
3566 * someone would change a built-in domain.
3567 */
3568 if (typid >= (Oid) FirstUnpinnedObjectId)
3569 {
3570 PlanInvalItem *inval_item = makeNode(PlanInvalItem);
3571
3572 /*
3573 * It would work to use any syscache on pg_type, but the easiest is
3574 * TYPEOID since we already have the type's OID at hand. Note that
3575 * plancache.c knows we use TYPEOID.
3576 */
3577 inval_item->cacheId = TYPEOID;
3578 inval_item->hashValue = GetSysCacheHashValue1(TYPEOID,
3579 ObjectIdGetDatum(typid));
3580
3581 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3582 }
3583}

References PlanInvalItem::cacheId, FirstUnpinnedObjectId, GetSysCacheHashValue1, PlanInvalItem::hashValue, lappend(), makeNode, ObjectIdGetDatum(), and root.

Referenced by eval_const_expressions_mutator().

◆ register_partpruneinfo()

static int register_partpruneinfo ( PlannerInfo root,
int  part_prune_index,
int  rtoffset 
)
static

Definition at line 1745 of file setrefs.c.

1746{
1747 PlannerGlobal *glob = root->glob;
1748 PartitionPruneInfo *pinfo;
1749 ListCell *l;
1750
1751 Assert(part_prune_index >= 0 &&
1752 part_prune_index < list_length(root->partPruneInfos));
1753 pinfo = list_nth_node(PartitionPruneInfo, root->partPruneInfos,
1754 part_prune_index);
1755
1756 pinfo->relids = offset_relid_set(pinfo->relids, rtoffset);
1757 foreach(l, pinfo->prune_infos)
1758 {
1759 List *prune_infos = lfirst(l);
1760 ListCell *l2;
1761
1762 foreach(l2, prune_infos)
1763 {
1764 PartitionedRelPruneInfo *prelinfo = lfirst(l2);
1765
1766 prelinfo->rtindex += rtoffset;
1767 prelinfo->initial_pruning_steps =
1769 rtoffset, 1);
1770 prelinfo->exec_pruning_steps =
1772 rtoffset, 1);
1773 }
1774 }
1775
1776 glob->partPruneInfos = lappend(glob->partPruneInfos, pinfo);
1777
1778 return list_length(glob->partPruneInfos) - 1;
1779}
#define list_nth_node(type, list, n)
Definition: pg_list.h:327
static Relids offset_relid_set(Relids relids, int rtoffset)
Definition: setrefs.c:1954
#define fix_scan_list(root, lst, rtoffset, num_exec)
Definition: setrefs.c:130
Bitmapset * relids
Definition: plannodes.h:1443
List * partPruneInfos
Definition: pathnodes.h:132

References Assert, PartitionedRelPruneInfo::exec_pruning_steps, fix_scan_list, PartitionedRelPruneInfo::initial_pruning_steps, lappend(), lfirst, list_length(), list_nth_node, offset_relid_set(), PlannerGlobal::partPruneInfos, PartitionPruneInfo::prune_infos, PartitionPruneInfo::relids, root, and PartitionedRelPruneInfo::rtindex.

Referenced by set_append_references(), and set_mergeappend_references().

◆ search_indexed_tlist_for_non_var()

static Var * search_indexed_tlist_for_non_var ( Expr node,
indexed_tlist itlist,
int  newvarno 
)
static

Definition at line 2954 of file setrefs.c.

2956{
2957 TargetEntry *tle;
2958
2959 /*
2960 * If it's a simple Const, replacing it with a Var is silly, even if there
2961 * happens to be an identical Const below; a Var is more expensive to
2962 * execute than a Const. What's more, replacing it could confuse some
2963 * places in the executor that expect to see simple Consts for, eg,
2964 * dropped columns.
2965 */
2966 if (IsA(node, Const))
2967 return NULL;
2968
2969 tle = tlist_member(node, itlist->tlist);
2970 if (tle)
2971 {
2972 /* Found a matching subplan output expression */
2973 Var *newvar;
2974
2975 newvar = makeVarFromTargetEntry(newvarno, tle);
2976 newvar->varnosyn = 0; /* wasn't ever a plain Var */
2977 newvar->varattnosyn = 0;
2978 return newvar;
2979 }
2980 return NULL; /* no match */
2981}
Var * makeVarFromTargetEntry(int varno, TargetEntry *tle)
Definition: makefuncs.c:107
TargetEntry * tlist_member(Expr *node, List *targetlist)
Definition: tlist.c:79

References IsA, makeVarFromTargetEntry(), indexed_tlist::tlist, and tlist_member().

Referenced by fix_join_expr_mutator(), fix_upper_expr_mutator(), and fix_windowagg_condition_expr_mutator().

◆ search_indexed_tlist_for_phv()

static Var * search_indexed_tlist_for_phv ( PlaceHolderVar phv,
indexed_tlist itlist,
int  newvarno,
NullingRelsMatch  nrm_match 
)
static

Definition at line 2901 of file setrefs.c.

2904{
2905 ListCell *lc;
2906
2907 foreach(lc, itlist->tlist)
2908 {
2909 TargetEntry *tle = (TargetEntry *) lfirst(lc);
2910
2911 if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2912 {
2913 PlaceHolderVar *subphv = (PlaceHolderVar *) tle->expr;
2914 Var *newvar;
2915
2916 /*
2917 * Analogously to search_indexed_tlist_for_var, we match on phid
2918 * only. We don't use equal(), partially for speed but mostly
2919 * because phnullingrels might not be exactly equal.
2920 */
2921 if (phv->phid != subphv->phid)
2922 continue;
2923
2924 /* Verify that we kept all the nullingrels machinations straight */
2925 if (!(nrm_match == NRM_SUBSET ?
2927 nrm_match == NRM_SUPERSET ?
2929 bms_equal(subphv->phnullingrels, phv->phnullingrels)))
2930 elog(ERROR, "wrong phnullingrels %s (expected %s) for PlaceHolderVar %d",
2931 bmsToString(phv->phnullingrels),
2932 bmsToString(subphv->phnullingrels),
2933 phv->phid);
2934
2935 /* Found a matching subplan output expression */
2936 newvar = makeVarFromTargetEntry(newvarno, tle);
2937 newvar->varnosyn = 0; /* wasn't ever a plain Var */
2938 newvar->varattnosyn = 0;
2939 return newvar;
2940 }
2941 }
2942 return NULL; /* no match */
2943}
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
char * bmsToString(const Bitmapset *bms)
Definition: outfuncs.c:811
Relids phnullingrels
Definition: pathnodes.h:2808

References bms_equal(), bms_is_subset(), bmsToString(), elog, ERROR, TargetEntry::expr, if(), IsA, lfirst, makeVarFromTargetEntry(), NRM_SUBSET, NRM_SUPERSET, PlaceHolderVar::phid, PlaceHolderVar::phnullingrels, and indexed_tlist::tlist.

Referenced by fix_join_expr_mutator(), and fix_upper_expr_mutator().

◆ search_indexed_tlist_for_sortgroupref()

static Var * search_indexed_tlist_for_sortgroupref ( Expr node,
Index  sortgroupref,
indexed_tlist itlist,
int  newvarno 
)
static

Definition at line 2994 of file setrefs.c.

2998{
2999 ListCell *lc;
3000
3001 foreach(lc, itlist->tlist)
3002 {
3003 TargetEntry *tle = (TargetEntry *) lfirst(lc);
3004
3005 /*
3006 * Usually the equal() check is redundant, but in setop plans it may
3007 * not be, since prepunion.c assigns ressortgroupref equal to the
3008 * column resno without regard to whether that matches the topmost
3009 * level's sortgrouprefs and without regard to whether any implicit
3010 * coercions are added in the setop tree. We might have to clean that
3011 * up someday; but for now, just ignore any false matches.
3012 */
3013 if (tle->ressortgroupref == sortgroupref &&
3014 equal(node, tle->expr))
3015 {
3016 /* Found a matching subplan output expression */
3017 Var *newvar;
3018
3019 newvar = makeVarFromTargetEntry(newvarno, tle);
3020 newvar->varnosyn = 0; /* wasn't ever a plain Var */
3021 newvar->varattnosyn = 0;
3022 return newvar;
3023 }
3024 }
3025 return NULL; /* no match */
3026}
Index ressortgroupref
Definition: primnodes.h:2225

References equal(), TargetEntry::expr, lfirst, makeVarFromTargetEntry(), TargetEntry::ressortgroupref, and indexed_tlist::tlist.

Referenced by set_upper_references().

◆ search_indexed_tlist_for_var()

static Var * search_indexed_tlist_for_var ( Var var,
indexed_tlist itlist,
int  newvarno,
int  rtoffset,
NullingRelsMatch  nrm_match 
)
static

Definition at line 2836 of file setrefs.c.

2839{
2840 int varno = var->varno;
2841 AttrNumber varattno = var->varattno;
2842 tlist_vinfo *vinfo;
2843 int i;
2844
2845 vinfo = itlist->vars;
2846 i = itlist->num_vars;
2847 while (i-- > 0)
2848 {
2849 if (vinfo->varno == varno && vinfo->varattno == varattno)
2850 {
2851 /* Found a match */
2852 Var *newvar = copyVar(var);
2853
2854 /*
2855 * Verify that we kept all the nullingrels machinations straight.
2856 *
2857 * XXX we skip the check for system columns and whole-row Vars.
2858 * That's because such Vars might be row identity Vars, which are
2859 * generated without any varnullingrels. It'd be hard to do
2860 * otherwise, since they're normally made very early in planning,
2861 * when we haven't looked at the jointree yet and don't know which
2862 * joins might null such Vars. Doesn't seem worth the expense to
2863 * make them fully valid. (While it's slightly annoying that we
2864 * thereby lose checking for user-written references to such
2865 * columns, it seems unlikely that a bug in nullingrels logic
2866 * would affect only system columns.)
2867 */
2868 if (!(varattno <= 0 ||
2869 (nrm_match == NRM_SUBSET ?
2870 bms_is_subset(var->varnullingrels, vinfo->varnullingrels) :
2871 nrm_match == NRM_SUPERSET ?
2872 bms_is_subset(vinfo->varnullingrels, var->varnullingrels) :
2873 bms_equal(vinfo->varnullingrels, var->varnullingrels))))
2874 elog(ERROR, "wrong varnullingrels %s (expected %s) for Var %d/%d",
2875 bmsToString(var->varnullingrels),
2877 varno, varattno);
2878
2879 newvar->varno = newvarno;
2880 newvar->varattno = vinfo->resno;
2881 if (newvar->varnosyn > 0)
2882 newvar->varnosyn += rtoffset;
2883 return newvar;
2884 }
2885 vinfo++;
2886 }
2887 return NULL; /* no match */
2888}
int i
Definition: isn.c:72

References bms_equal(), bms_is_subset(), bmsToString(), copyVar(), elog, ERROR, i, NRM_SUBSET, NRM_SUPERSET, indexed_tlist::num_vars, tlist_vinfo::resno, tlist_vinfo::varattno, Var::varattno, tlist_vinfo::varno, Var::varno, tlist_vinfo::varnullingrels, and indexed_tlist::vars.

Referenced by fix_join_expr_mutator(), and fix_upper_expr_mutator().

◆ set_append_references()

static Plan * set_append_references ( PlannerInfo root,
Append aplan,
int  rtoffset 
)
static

Definition at line 1789 of file setrefs.c.

1792{
1793 ListCell *l;
1794
1795 /*
1796 * Append, like Sort et al, doesn't actually evaluate its targetlist or
1797 * check quals. If it's got exactly one child plan, then it's not doing
1798 * anything useful at all, and we can strip it out.
1799 */
1800 Assert(aplan->plan.qual == NIL);
1801
1802 /* First, we gotta recurse on the children */
1803 foreach(l, aplan->appendplans)
1804 {
1805 lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1806 }
1807
1808 /*
1809 * See if it's safe to get rid of the Append entirely. For this to be
1810 * safe, there must be only one child plan and that child plan's parallel
1811 * awareness must match the Append's. The reason for the latter is that
1812 * if the Append is parallel aware and the child is not, then the calling
1813 * plan may execute the non-parallel aware child multiple times. (If you
1814 * change these rules, update create_append_path to match.)
1815 */
1816 if (list_length(aplan->appendplans) == 1)
1817 {
1818 Plan *p = (Plan *) linitial(aplan->appendplans);
1819
1820 if (p->parallel_aware == aplan->plan.parallel_aware)
1821 return clean_up_removed_plan_level((Plan *) aplan, p);
1822 }
1823
1824 /*
1825 * Otherwise, clean up the Append as needed. It's okay to do this after
1826 * recursing to the children, because set_dummy_tlist_references doesn't
1827 * look at those.
1828 */
1829 set_dummy_tlist_references((Plan *) aplan, rtoffset);
1830
1831 aplan->apprelids = offset_relid_set(aplan->apprelids, rtoffset);
1832
1833 /*
1834 * Add PartitionPruneInfo, if any, to PlannerGlobal and update the index.
1835 * Also update the RT indexes present in it to add the offset.
1836 */
1837 if (aplan->part_prune_index >= 0)
1838 aplan->part_prune_index =
1839 register_partpruneinfo(root, aplan->part_prune_index, rtoffset);
1840
1841 /* We don't need to recurse to lefttree or righttree ... */
1842 Assert(aplan->plan.lefttree == NULL);
1843 Assert(aplan->plan.righttree == NULL);
1844
1845 return (Plan *) aplan;
1846}
static void set_dummy_tlist_references(Plan *plan, int rtoffset)
Definition: setrefs.c:2660
static int register_partpruneinfo(PlannerInfo *root, int part_prune_index, int rtoffset)
Definition: setrefs.c:1745
static Plan * clean_up_removed_plan_level(Plan *parent, Plan *child)
Definition: setrefs.c:1535
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:609
int part_prune_index
Definition: plannodes.h:289
Bitmapset * apprelids
Definition: plannodes.h:274
Plan plan
Definition: plannodes.h:273
List * appendplans
Definition: plannodes.h:275
struct Plan * lefttree
Definition: plannodes.h:158
struct Plan * righttree
Definition: plannodes.h:159
bool parallel_aware
Definition: plannodes.h:144
List * qual
Definition: plannodes.h:157

References Append::appendplans, Append::apprelids, Assert, clean_up_removed_plan_level(), Plan::lefttree, lfirst, linitial, list_length(), NIL, offset_relid_set(), Plan::parallel_aware, Append::part_prune_index, Append::plan, Plan::qual, register_partpruneinfo(), Plan::righttree, root, set_dummy_tlist_references(), and set_plan_refs().

Referenced by set_plan_refs().

◆ set_customscan_references()

static void set_customscan_references ( PlannerInfo root,
CustomScan cscan,
int  rtoffset 
)
static

Definition at line 1666 of file setrefs.c.

1669{
1670 ListCell *lc;
1671
1672 /* Adjust scanrelid if it's valid */
1673 if (cscan->scan.scanrelid > 0)
1674 cscan->scan.scanrelid += rtoffset;
1675
1676 if (cscan->custom_scan_tlist != NIL || cscan->scan.scanrelid == 0)
1677 {
1678 /* Adjust tlist, qual, custom_exprs to reference custom scan tuple */
1680
1681 cscan->scan.plan.targetlist = (List *)
1683 (Node *) cscan->scan.plan.targetlist,
1684 itlist,
1685 INDEX_VAR,
1686 rtoffset,
1687 NRM_EQUAL,
1688 NUM_EXEC_TLIST((Plan *) cscan));
1689 cscan->scan.plan.qual = (List *)
1691 (Node *) cscan->scan.plan.qual,
1692 itlist,
1693 INDEX_VAR,
1694 rtoffset,
1695 NRM_EQUAL,
1696 NUM_EXEC_QUAL((Plan *) cscan));
1697 cscan->custom_exprs = (List *)
1699 (Node *) cscan->custom_exprs,
1700 itlist,
1701 INDEX_VAR,
1702 rtoffset,
1703 NRM_EQUAL,
1704 NUM_EXEC_QUAL((Plan *) cscan));
1705 pfree(itlist);
1706 /* custom_scan_tlist itself just needs fix_scan_list() adjustments */
1707 cscan->custom_scan_tlist =
1709 rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1710 }
1711 else
1712 {
1713 /* Adjust tlist, qual, custom_exprs in the standard way */
1714 cscan->scan.plan.targetlist =
1715 fix_scan_list(root, cscan->scan.plan.targetlist,
1716 rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1717 cscan->scan.plan.qual =
1718 fix_scan_list(root, cscan->scan.plan.qual,
1719 rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1720 cscan->custom_exprs =
1722 rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1723 }
1724
1725 /* Adjust child plan-nodes recursively, if needed */
1726 foreach(lc, cscan->custom_plans)
1727 {
1728 lfirst(lc) = set_plan_refs(root, (Plan *) lfirst(lc), rtoffset);
1729 }
1730
1731 cscan->custom_relids = offset_relid_set(cscan->custom_relids, rtoffset);
1732}
void pfree(void *pointer)
Definition: mcxt.c:1521
#define INDEX_VAR
Definition: primnodes.h:244
#define NUM_EXEC_QUAL(parentplan)
Definition: setrefs.c:117
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2727
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, int newvarno, int rtoffset, NullingRelsMatch nrm_match, double num_exec)
Definition: setrefs.c:3246
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:116
List * custom_scan_tlist
Definition: plannodes.h:761
Scan scan
Definition: plannodes.h:755
Bitmapset * custom_relids
Definition: plannodes.h:762
List * custom_exprs
Definition: plannodes.h:759
List * custom_plans
Definition: plannodes.h:758
Index scanrelid
Definition: plannodes.h:403

References build_tlist_index(), CustomScan::custom_plans, CustomScan::custom_relids, CustomScan::custom_scan_tlist, fix_scan_list, fix_upper_expr(), INDEX_VAR, lfirst, NIL, NRM_EQUAL, NUM_EXEC_QUAL, NUM_EXEC_TLIST, offset_relid_set(), pfree(), Plan::qual, root, CustomScan::scan, Scan::scanrelid, and set_plan_refs().

Referenced by set_plan_refs().

◆ set_dummy_tlist_references()

static void set_dummy_tlist_references ( Plan plan,
int  rtoffset 
)
static

Definition at line 2660 of file setrefs.c.

2661{
2662 List *output_targetlist;
2663 ListCell *l;
2664
2665 output_targetlist = NIL;
2666 foreach(l, plan->targetlist)
2667 {
2668 TargetEntry *tle = (TargetEntry *) lfirst(l);
2669 Var *oldvar = (Var *) tle->expr;
2670 Var *newvar;
2671
2672 /*
2673 * As in search_indexed_tlist_for_non_var(), we prefer to keep Consts
2674 * as Consts, not Vars referencing Consts. Here, there's no speed
2675 * advantage to be had, but it makes EXPLAIN output look cleaner, and
2676 * again it avoids confusing the executor.
2677 */
2678 if (IsA(oldvar, Const))
2679 {
2680 /* just reuse the existing TLE node */
2681 output_targetlist = lappend(output_targetlist, tle);
2682 continue;
2683 }
2684
2685 newvar = makeVar(OUTER_VAR,
2686 tle->resno,
2687 exprType((Node *) oldvar),
2688 exprTypmod((Node *) oldvar),
2689 exprCollation((Node *) oldvar),
2690 0);
2691 if (IsA(oldvar, Var) &&
2692 oldvar->varnosyn > 0)
2693 {
2694 newvar->varnosyn = oldvar->varnosyn + rtoffset;
2695 newvar->varattnosyn = oldvar->varattnosyn;
2696 }
2697 else
2698 {
2699 newvar->varnosyn = 0; /* wasn't ever a plain Var */
2700 newvar->varattnosyn = 0;
2701 }
2702
2703 tle = flatCopyTargetEntry(tle);
2704 tle->expr = (Expr *) newvar;
2705 output_targetlist = lappend(output_targetlist, tle);
2706 }
2707 plan->targetlist = output_targetlist;
2708
2709 /* We don't touch plan->qual here */
2710}
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:275
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:301
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
#define plan(x)
Definition: pg_regress.c:161

References TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), flatCopyTargetEntry(), if(), IsA, lappend(), lfirst, makeVar(), NIL, OUTER_VAR, plan, and TargetEntry::resno.

Referenced by set_append_references(), set_hash_references(), set_mergeappend_references(), and set_plan_refs().

◆ set_foreignscan_references()

static void set_foreignscan_references ( PlannerInfo root,
ForeignScan fscan,
int  rtoffset 
)
static

Definition at line 1579 of file setrefs.c.

1582{
1583 /* Adjust scanrelid if it's valid */
1584 if (fscan->scan.scanrelid > 0)
1585 fscan->scan.scanrelid += rtoffset;
1586
1587 if (fscan->fdw_scan_tlist != NIL || fscan->scan.scanrelid == 0)
1588 {
1589 /*
1590 * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals to reference
1591 * foreign scan tuple
1592 */
1594
1595 fscan->scan.plan.targetlist = (List *)
1597 (Node *) fscan->scan.plan.targetlist,
1598 itlist,
1599 INDEX_VAR,
1600 rtoffset,
1601 NRM_EQUAL,
1602 NUM_EXEC_TLIST((Plan *) fscan));
1603 fscan->scan.plan.qual = (List *)
1605 (Node *) fscan->scan.plan.qual,
1606 itlist,
1607 INDEX_VAR,
1608 rtoffset,
1609 NRM_EQUAL,
1610 NUM_EXEC_QUAL((Plan *) fscan));
1611 fscan->fdw_exprs = (List *)
1613 (Node *) fscan->fdw_exprs,
1614 itlist,
1615 INDEX_VAR,
1616 rtoffset,
1617 NRM_EQUAL,
1618 NUM_EXEC_QUAL((Plan *) fscan));
1619 fscan->fdw_recheck_quals = (List *)
1621 (Node *) fscan->fdw_recheck_quals,
1622 itlist,
1623 INDEX_VAR,
1624 rtoffset,
1625 NRM_EQUAL,
1626 NUM_EXEC_QUAL((Plan *) fscan));
1627 pfree(itlist);
1628 /* fdw_scan_tlist itself just needs fix_scan_list() adjustments */
1629 fscan->fdw_scan_tlist =
1631 rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1632 }
1633 else
1634 {
1635 /*
1636 * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals in the standard
1637 * way
1638 */
1639 fscan->scan.plan.targetlist =
1640 fix_scan_list(root, fscan->scan.plan.targetlist,
1641 rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1642 fscan->scan.plan.qual =
1643 fix_scan_list(root, fscan->scan.plan.qual,
1644 rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1645 fscan->fdw_exprs =
1647 rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1648 fscan->fdw_recheck_quals =
1650 rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1651 }
1652
1653 fscan->fs_relids = offset_relid_set(fscan->fs_relids, rtoffset);
1654 fscan->fs_base_relids = offset_relid_set(fscan->fs_base_relids, rtoffset);
1655
1656 /* Adjust resultRelation if it's valid */
1657 if (fscan->resultRelation > 0)
1658 fscan->resultRelation += rtoffset;
1659}
List * fdw_exprs
Definition: plannodes.h:729
Bitmapset * fs_relids
Definition: plannodes.h:733
Bitmapset * fs_base_relids
Definition: plannodes.h:734
Index resultRelation
Definition: plannodes.h:725
List * fdw_recheck_quals
Definition: plannodes.h:732
List * fdw_scan_tlist
Definition: plannodes.h:731

References build_tlist_index(), ForeignScan::fdw_scan_tlist, fix_scan_list, fix_upper_expr(), ForeignScan::fs_base_relids, ForeignScan::fs_relids, INDEX_VAR, NIL, NRM_EQUAL, NUM_EXEC_QUAL, NUM_EXEC_TLIST, offset_relid_set(), pfree(), Plan::qual, ForeignScan::resultRelation, root, ForeignScan::scan, and Scan::scanrelid.

Referenced by set_plan_refs().

◆ set_hash_references()

static void set_hash_references ( PlannerInfo root,
Plan plan,
int  rtoffset 
)
static

Definition at line 1921 of file setrefs.c.

1922{
1923 Hash *hplan = (Hash *) plan;
1924 Plan *outer_plan = plan->lefttree;
1925 indexed_tlist *outer_itlist;
1926
1927 /*
1928 * Hash's hashkeys are used when feeding tuples into the hashtable,
1929 * therefore have them reference Hash's outer plan (which itself is the
1930 * inner plan of the HashJoin).
1931 */
1932 outer_itlist = build_tlist_index(outer_plan->targetlist);
1933 hplan->hashkeys = (List *)
1935 (Node *) hplan->hashkeys,
1936 outer_itlist,
1937 OUTER_VAR,
1938 rtoffset,
1939 NRM_EQUAL,
1941
1942 /* Hash doesn't project */
1944
1945 /* Hash nodes don't have their own quals */
1946 Assert(plan->qual == NIL);
1947}
List * hashkeys
Definition: plannodes.h:1219

References Assert, build_tlist_index(), fix_upper_expr(), Hash::hashkeys, NIL, NRM_EQUAL, NUM_EXEC_QUAL, OUTER_VAR, plan, root, set_dummy_tlist_references(), and Plan::targetlist.

Referenced by set_plan_refs().

◆ set_indexonlyscan_references()

static Plan * set_indexonlyscan_references ( PlannerInfo root,
IndexOnlyScan plan,
int  rtoffset 
)
static

Definition at line 1322 of file setrefs.c.

1325{
1326 indexed_tlist *index_itlist;
1327 List *stripped_indextlist;
1328 ListCell *lc;
1329
1330 /*
1331 * Vars in the plan node's targetlist, qual, and recheckqual must only
1332 * reference columns that the index AM can actually return. To ensure
1333 * this, remove non-returnable columns (which are marked as resjunk) from
1334 * the indexed tlist. We can just drop them because the indexed_tlist
1335 * machinery pays attention to TLE resnos, not physical list position.
1336 */
1337 stripped_indextlist = NIL;
1338 foreach(lc, plan->indextlist)
1339 {
1340 TargetEntry *indextle = (TargetEntry *) lfirst(lc);
1341
1342 if (!indextle->resjunk)
1343 stripped_indextlist = lappend(stripped_indextlist, indextle);
1344 }
1345
1346 index_itlist = build_tlist_index(stripped_indextlist);
1347
1348 plan->scan.scanrelid += rtoffset;
1349 plan->scan.plan.targetlist = (List *)
1351 (Node *) plan->scan.plan.targetlist,
1352 index_itlist,
1353 INDEX_VAR,
1354 rtoffset,
1355 NRM_EQUAL,
1356 NUM_EXEC_TLIST((Plan *) plan));
1357 plan->scan.plan.qual = (List *)
1359 (Node *) plan->scan.plan.qual,
1360 index_itlist,
1361 INDEX_VAR,
1362 rtoffset,
1363 NRM_EQUAL,
1364 NUM_EXEC_QUAL((Plan *) plan));
1365 plan->recheckqual = (List *)
1367 (Node *) plan->recheckqual,
1368 index_itlist,
1369 INDEX_VAR,
1370 rtoffset,
1371 NRM_EQUAL,
1372 NUM_EXEC_QUAL((Plan *) plan));
1373 /* indexqual is already transformed to reference index columns */
1374 plan->indexqual = fix_scan_list(root, plan->indexqual,
1375 rtoffset, 1);
1376 /* indexorderby is already transformed to reference index columns */
1377 plan->indexorderby = fix_scan_list(root, plan->indexorderby,
1378 rtoffset, 1);
1379 /* indextlist must NOT be transformed to reference index columns */
1380 plan->indextlist = fix_scan_list(root, plan->indextlist,
1381 rtoffset, NUM_EXEC_TLIST((Plan *) plan));
1382
1383 pfree(index_itlist);
1384
1385 return (Plan *) plan;
1386}

References build_tlist_index(), fix_scan_list, fix_upper_expr(), INDEX_VAR, lappend(), lfirst, NIL, NRM_EQUAL, NUM_EXEC_QUAL, NUM_EXEC_TLIST, pfree(), plan, and root.

Referenced by set_plan_refs().

◆ set_join_references()

static void set_join_references ( PlannerInfo root,
Join join,
int  rtoffset 
)
static

Definition at line 2300 of file setrefs.c.

2301{
2302 Plan *outer_plan = join->plan.lefttree;
2303 Plan *inner_plan = join->plan.righttree;
2304 indexed_tlist *outer_itlist;
2305 indexed_tlist *inner_itlist;
2306
2307 outer_itlist = build_tlist_index(outer_plan->targetlist);
2308 inner_itlist = build_tlist_index(inner_plan->targetlist);
2309
2310 /*
2311 * First process the joinquals (including merge or hash clauses). These
2312 * are logically below the join so they can always use all values
2313 * available from the input tlists. It's okay to also handle
2314 * NestLoopParams now, because those couldn't refer to nullable
2315 * subexpressions.
2316 */
2317 join->joinqual = fix_join_expr(root,
2318 join->joinqual,
2319 outer_itlist,
2320 inner_itlist,
2321 (Index) 0,
2322 rtoffset,
2323 NRM_EQUAL,
2324 NUM_EXEC_QUAL((Plan *) join));
2325
2326 /* Now do join-type-specific stuff */
2327 if (IsA(join, NestLoop))
2328 {
2329 NestLoop *nl = (NestLoop *) join;
2330 ListCell *lc;
2331
2332 foreach(lc, nl->nestParams)
2333 {
2334 NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
2335
2336 /*
2337 * Because we don't reparameterize parameterized paths to match
2338 * the outer-join level at which they are used, Vars seen in the
2339 * NestLoopParam expression may have nullingrels that are just a
2340 * subset of those in the Vars actually available from the outer
2341 * side. (Lateral references can also cause this, as explained in
2342 * the comments for identify_current_nestloop_params.) Not
2343 * checking this exactly is a bit grotty, but the work needed to
2344 * make things match up perfectly seems well out of proportion to
2345 * the value.
2346 */
2347 nlp->paramval = (Var *) fix_upper_expr(root,
2348 (Node *) nlp->paramval,
2349 outer_itlist,
2350 OUTER_VAR,
2351 rtoffset,
2352 NRM_SUBSET,
2353 NUM_EXEC_TLIST(outer_plan));
2354 /* Check we replaced any PlaceHolderVar with simple Var */
2355 if (!(IsA(nlp->paramval, Var) &&
2356 nlp->paramval->varno == OUTER_VAR))
2357 elog(ERROR, "NestLoopParam was not reduced to a simple Var");
2358 }
2359 }
2360 else if (IsA(join, MergeJoin))
2361 {
2362 MergeJoin *mj = (MergeJoin *) join;
2363
2365 mj->mergeclauses,
2366 outer_itlist,
2367 inner_itlist,
2368 (Index) 0,
2369 rtoffset,
2370 NRM_EQUAL,
2371 NUM_EXEC_QUAL((Plan *) join));
2372 }
2373 else if (IsA(join, HashJoin))
2374 {
2375 HashJoin *hj = (HashJoin *) join;
2376
2378 hj->hashclauses,
2379 outer_itlist,
2380 inner_itlist,
2381 (Index) 0,
2382 rtoffset,
2383 NRM_EQUAL,
2384 NUM_EXEC_QUAL((Plan *) join));
2385
2386 /*
2387 * HashJoin's hashkeys are used to look for matching tuples from its
2388 * outer plan (not the Hash node!) in the hashtable.
2389 */
2390 hj->hashkeys = (List *) fix_upper_expr(root,
2391 (Node *) hj->hashkeys,
2392 outer_itlist,
2393 OUTER_VAR,
2394 rtoffset,
2395 NRM_EQUAL,
2396 NUM_EXEC_QUAL((Plan *) join));
2397 }
2398
2399 /*
2400 * Now we need to fix up the targetlist and qpqual, which are logically
2401 * above the join. This means that, if it's not an inner join, any Vars
2402 * and PHVs appearing here should have nullingrels that include the
2403 * effects of the outer join, ie they will have nullingrels equal to the
2404 * input Vars' nullingrels plus the bit added by the outer join. We don't
2405 * currently have enough info available here to identify what that should
2406 * be, so we just tell fix_join_expr to accept superset nullingrels
2407 * matches instead of exact ones.
2408 */
2409 join->plan.targetlist = fix_join_expr(root,
2410 join->plan.targetlist,
2411 outer_itlist,
2412 inner_itlist,
2413 (Index) 0,
2414 rtoffset,
2416 NUM_EXEC_TLIST((Plan *) join));
2417 join->plan.qual = fix_join_expr(root,
2418 join->plan.qual,
2419 outer_itlist,
2420 inner_itlist,
2421 (Index) 0,
2422 rtoffset,
2424 NUM_EXEC_QUAL((Plan *) join));
2425
2426 pfree(outer_itlist);
2427 pfree(inner_itlist);
2428}
@ JOIN_INNER
Definition: nodes.h:293
static List * fix_join_expr(PlannerInfo *root, List *clauses, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel, int rtoffset, NullingRelsMatch nrm_match, double num_exec)
Definition: setrefs.c:3072
List * hashclauses
Definition: plannodes.h:879
List * hashkeys
Definition: plannodes.h:887
List * joinqual
Definition: plannodes.h:807
JoinType jointype
Definition: plannodes.h:805
List * mergeclauses
Definition: plannodes.h:855
Var * paramval
Definition: plannodes.h:833
List * nestParams
Definition: plannodes.h:824

References build_tlist_index(), elog, ERROR, fix_join_expr(), fix_upper_expr(), HashJoin::hashclauses, HashJoin::hashkeys, IsA, JOIN_INNER, Join::joinqual, Join::jointype, lfirst, MergeJoin::mergeclauses, NestLoop::nestParams, NRM_EQUAL, NRM_SUBSET, NRM_SUPERSET, NUM_EXEC_QUAL, NUM_EXEC_TLIST, OUTER_VAR, NestLoopParam::paramval, pfree(), root, Plan::targetlist, and Var::varno.

Referenced by set_plan_refs().

◆ set_mergeappend_references()

static Plan * set_mergeappend_references ( PlannerInfo root,
MergeAppend mplan,
int  rtoffset 
)
static

Definition at line 1856 of file setrefs.c.

1859{
1860 ListCell *l;
1861
1862 /*
1863 * MergeAppend, like Sort et al, doesn't actually evaluate its targetlist
1864 * or check quals. If it's got exactly one child plan, then it's not
1865 * doing anything useful at all, and we can strip it out.
1866 */
1867 Assert(mplan->plan.qual == NIL);
1868
1869 /* First, we gotta recurse on the children */
1870 foreach(l, mplan->mergeplans)
1871 {
1872 lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1873 }
1874
1875 /*
1876 * See if it's safe to get rid of the MergeAppend entirely. For this to
1877 * be safe, there must be only one child plan and that child plan's
1878 * parallel awareness must match the MergeAppend's. The reason for the
1879 * latter is that if the MergeAppend is parallel aware and the child is
1880 * not, then the calling plan may execute the non-parallel aware child
1881 * multiple times. (If you change these rules, update
1882 * create_merge_append_path to match.)
1883 */
1884 if (list_length(mplan->mergeplans) == 1)
1885 {
1886 Plan *p = (Plan *) linitial(mplan->mergeplans);
1887
1888 if (p->parallel_aware == mplan->plan.parallel_aware)
1889 return clean_up_removed_plan_level((Plan *) mplan, p);
1890 }
1891
1892 /*
1893 * Otherwise, clean up the MergeAppend as needed. It's okay to do this
1894 * after recursing to the children, because set_dummy_tlist_references
1895 * doesn't look at those.
1896 */
1897 set_dummy_tlist_references((Plan *) mplan, rtoffset);
1898
1899 mplan->apprelids = offset_relid_set(mplan->apprelids, rtoffset);
1900
1901 /*
1902 * Add PartitionPruneInfo, if any, to PlannerGlobal and update the index.
1903 * Also update the RT indexes present in it to add the offset.
1904 */
1905 if (mplan->part_prune_index >= 0)
1906 mplan->part_prune_index =
1907 register_partpruneinfo(root, mplan->part_prune_index, rtoffset);
1908
1909 /* We don't need to recurse to lefttree or righttree ... */
1910 Assert(mplan->plan.lefttree == NULL);
1911 Assert(mplan->plan.righttree == NULL);
1912
1913 return (Plan *) mplan;
1914}
int part_prune_index
Definition: plannodes.h:328
Bitmapset * apprelids
Definition: plannodes.h:302
List * mergeplans
Definition: plannodes.h:304

References MergeAppend::apprelids, Assert, clean_up_removed_plan_level(), Plan::lefttree, lfirst, linitial, list_length(), MergeAppend::mergeplans, NIL, offset_relid_set(), Plan::parallel_aware, MergeAppend::part_prune_index, MergeAppend::plan, Plan::qual, register_partpruneinfo(), Plan::righttree, root, set_dummy_tlist_references(), and set_plan_refs().

Referenced by set_plan_refs().

◆ set_param_references()

static void set_param_references ( PlannerInfo root,
Plan plan 
)
static

Definition at line 2537 of file setrefs.c.

2538{
2540
2541 if (plan->lefttree->extParam)
2542 {
2543 PlannerInfo *proot;
2544 Bitmapset *initSetParam = NULL;
2545 ListCell *l;
2546
2547 for (proot = root; proot != NULL; proot = proot->parent_root)
2548 {
2549 foreach(l, proot->init_plans)
2550 {
2551 SubPlan *initsubplan = (SubPlan *) lfirst(l);
2552 ListCell *l2;
2553
2554 foreach(l2, initsubplan->setParam)
2555 {
2556 initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
2557 }
2558 }
2559 }
2560
2561 /*
2562 * Remember the list of all external initplan params that are used by
2563 * the children of Gather or Gather merge node.
2564 */
2565 if (IsA(plan, Gather))
2566 ((Gather *) plan)->initParam =
2567 bms_intersect(plan->lefttree->extParam, initSetParam);
2568 else
2569 ((GatherMerge *) plan)->initParam =
2570 bms_intersect(plan->lefttree->extParam, initSetParam);
2571 }
2572}
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
List * init_plans
Definition: pathnodes.h:302
List * setParam
Definition: primnodes.h:1105

References Assert, bms_add_member(), bms_intersect(), PlannerInfo::init_plans, IsA, lfirst, lfirst_int, plan, root, and SubPlan::setParam.

Referenced by set_plan_refs().

◆ set_plan_references()

Plan * set_plan_references ( PlannerInfo root,
Plan plan 
)

Definition at line 288 of file setrefs.c.

289{
290 Plan *result;
291 PlannerGlobal *glob = root->glob;
292 int rtoffset = list_length(glob->finalrtable);
293 ListCell *lc;
294
295 /*
296 * Add all the query's RTEs to the flattened rangetable. The live ones
297 * will have their rangetable indexes increased by rtoffset. (Additional
298 * RTEs, not referenced by the Plan tree, might get added after those.)
299 */
301
302 /*
303 * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
304 */
305 foreach(lc, root->rowMarks)
306 {
308 PlanRowMark *newrc;
309
310 /* flat copy is enough since all fields are scalars */
311 newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark));
312 memcpy(newrc, rc, sizeof(PlanRowMark));
313
314 /* adjust indexes ... but *not* the rowmarkId */
315 newrc->rti += rtoffset;
316 newrc->prti += rtoffset;
317
318 glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
319 }
320
321 /*
322 * Adjust RT indexes of AppendRelInfos and add to final appendrels list.
323 * We assume the AppendRelInfos were built during planning and don't need
324 * to be copied.
325 */
326 foreach(lc, root->append_rel_list)
327 {
329
330 /* adjust RT indexes */
331 appinfo->parent_relid += rtoffset;
332 appinfo->child_relid += rtoffset;
333
334 /*
335 * Rather than adjust the translated_vars entries, just drop 'em.
336 * Neither the executor nor EXPLAIN currently need that data.
337 */
338 appinfo->translated_vars = NIL;
339
340 glob->appendRelations = lappend(glob->appendRelations, appinfo);
341 }
342
343 /* If needed, create workspace for processing AlternativeSubPlans */
344 if (root->hasAlternativeSubPlans)
345 {
346 root->isAltSubplan = (bool *)
347 palloc0(list_length(glob->subplans) * sizeof(bool));
348 root->isUsedSubplan = (bool *)
349 palloc0(list_length(glob->subplans) * sizeof(bool));
350 }
351
352 /* Now fix the Plan tree */
353 result = set_plan_refs(root, plan, rtoffset);
354
355 /*
356 * If we have AlternativeSubPlans, it is likely that we now have some
357 * unreferenced subplans in glob->subplans. To avoid expending cycles on
358 * those subplans later, get rid of them by setting those list entries to
359 * NULL. (Note: we can't do this immediately upon processing an
360 * AlternativeSubPlan, because there may be multiple copies of the
361 * AlternativeSubPlan, and they can get resolved differently.)
362 */
363 if (root->hasAlternativeSubPlans)
364 {
365 foreach(lc, glob->subplans)
366 {
367 int ndx = foreach_current_index(lc);
368
369 /*
370 * If it was used by some AlternativeSubPlan in this query level,
371 * but wasn't selected as best by any AlternativeSubPlan, then we
372 * don't need it. Do not touch subplans that aren't parts of
373 * AlternativeSubPlans.
374 */
375 if (root->isAltSubplan[ndx] && !root->isUsedSubplan[ndx])
376 lfirst(lc) = NULL;
377 }
378 }
379
380 return result;
381}
void * palloc0(Size size)
Definition: mcxt.c:1347
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define foreach_current_index(var_or_cell)
Definition: pg_list.h:403
Index child_relid
Definition: pathnodes.h:2987
List * translated_vars
Definition: pathnodes.h:3014
Index parent_relid
Definition: pathnodes.h:2986
Index prti
Definition: plannodes.h:1394
List * subplans
Definition: pathnodes.h:105
List * appendRelations
Definition: pathnodes.h:129
List * finalrowmarks
Definition: pathnodes.h:123

References add_rtes_to_flat_rtable(), PlannerGlobal::appendRelations, AppendRelInfo::child_relid, PlannerGlobal::finalrowmarks, PlannerGlobal::finalrtable, foreach_current_index, lappend(), lfirst, lfirst_node, list_length(), NIL, palloc(), palloc0(), AppendRelInfo::parent_relid, plan, PlanRowMark::prti, root, PlanRowMark::rti, set_plan_refs(), PlannerGlobal::subplans, and AppendRelInfo::translated_vars.

Referenced by set_subqueryscan_references(), and standard_planner().

◆ set_plan_refs()

static Plan * set_plan_refs ( PlannerInfo root,
Plan plan,
int  rtoffset 
)
static

Definition at line 609 of file setrefs.c.

610{
611 ListCell *l;
612
613 if (plan == NULL)
614 return NULL;
615
616 /* Assign this node a unique ID. */
617 plan->plan_node_id = root->glob->lastPlanNodeId++;
618
619 /*
620 * Plan-type-specific fixes
621 */
622 switch (nodeTag(plan))
623 {
624 case T_SeqScan:
625 {
626 SeqScan *splan = (SeqScan *) plan;
627
628 splan->scan.scanrelid += rtoffset;
629 splan->scan.plan.targetlist =
630 fix_scan_list(root, splan->scan.plan.targetlist,
631 rtoffset, NUM_EXEC_TLIST(plan));
632 splan->scan.plan.qual =
633 fix_scan_list(root, splan->scan.plan.qual,
634 rtoffset, NUM_EXEC_QUAL(plan));
635 }
636 break;
637 case T_SampleScan:
638 {
640
641 splan->scan.scanrelid += rtoffset;
642 splan->scan.plan.targetlist =
643 fix_scan_list(root, splan->scan.plan.targetlist,
644 rtoffset, NUM_EXEC_TLIST(plan));
645 splan->scan.plan.qual =
646 fix_scan_list(root, splan->scan.plan.qual,
647 rtoffset, NUM_EXEC_QUAL(plan));
648 splan->tablesample = (TableSampleClause *)
649 fix_scan_expr(root, (Node *) splan->tablesample,
650 rtoffset, 1);
651 }
652 break;
653 case T_IndexScan:
654 {
656
657 splan->scan.scanrelid += rtoffset;
658 splan->scan.plan.targetlist =
659 fix_scan_list(root, splan->scan.plan.targetlist,
660 rtoffset, NUM_EXEC_TLIST(plan));
661 splan->scan.plan.qual =
662 fix_scan_list(root, splan->scan.plan.qual,
663 rtoffset, NUM_EXEC_QUAL(plan));
664 splan->indexqual =
665 fix_scan_list(root, splan->indexqual,
666 rtoffset, 1);
667 splan->indexqualorig =
668 fix_scan_list(root, splan->indexqualorig,
669 rtoffset, NUM_EXEC_QUAL(plan));
670 splan->indexorderby =
671 fix_scan_list(root, splan->indexorderby,
672 rtoffset, 1);
673 splan->indexorderbyorig =
674 fix_scan_list(root, splan->indexorderbyorig,
675 rtoffset, NUM_EXEC_QUAL(plan));
676 }
677 break;
678 case T_IndexOnlyScan:
679 {
681
682 return set_indexonlyscan_references(root, splan, rtoffset);
683 }
684 break;
685 case T_BitmapIndexScan:
686 {
688
689 splan->scan.scanrelid += rtoffset;
690 /* no need to fix targetlist and qual */
691 Assert(splan->scan.plan.targetlist == NIL);
692 Assert(splan->scan.plan.qual == NIL);
693 splan->indexqual =
694 fix_scan_list(root, splan->indexqual, rtoffset, 1);
695 splan->indexqualorig =
696 fix_scan_list(root, splan->indexqualorig,
697 rtoffset, NUM_EXEC_QUAL(plan));
698 }
699 break;
700 case T_BitmapHeapScan:
701 {
703
704 splan->scan.scanrelid += rtoffset;
705 splan->scan.plan.targetlist =
706 fix_scan_list(root, splan->scan.plan.targetlist,
707 rtoffset, NUM_EXEC_TLIST(plan));
708 splan->scan.plan.qual =
709 fix_scan_list(root, splan->scan.plan.qual,
710 rtoffset, NUM_EXEC_QUAL(plan));
711 splan->bitmapqualorig =
712 fix_scan_list(root, splan->bitmapqualorig,
713 rtoffset, NUM_EXEC_QUAL(plan));
714 }
715 break;
716 case T_TidScan:
717 {
718 TidScan *splan = (TidScan *) plan;
719
720 splan->scan.scanrelid += rtoffset;
721 splan->scan.plan.targetlist =
722 fix_scan_list(root, splan->scan.plan.targetlist,
723 rtoffset, NUM_EXEC_TLIST(plan));
724 splan->scan.plan.qual =
725 fix_scan_list(root, splan->scan.plan.qual,
726 rtoffset, NUM_EXEC_QUAL(plan));
727 splan->tidquals =
728 fix_scan_list(root, splan->tidquals,
729 rtoffset, 1);
730 }
731 break;
732 case T_TidRangeScan:
733 {
735
736 splan->scan.scanrelid += rtoffset;
737 splan->scan.plan.targetlist =
738 fix_scan_list(root, splan->scan.plan.targetlist,
739 rtoffset, NUM_EXEC_TLIST(plan));
740 splan->scan.plan.qual =
741 fix_scan_list(root, splan->scan.plan.qual,
742 rtoffset, NUM_EXEC_QUAL(plan));
743 splan->tidrangequals =
744 fix_scan_list(root, splan->tidrangequals,
745 rtoffset, 1);
746 }
747 break;
748 case T_SubqueryScan:
749 /* Needs special treatment, see comments below */
751 (SubqueryScan *) plan,
752 rtoffset);
753 case T_FunctionScan:
754 {
756
757 splan->scan.scanrelid += rtoffset;
758 splan->scan.plan.targetlist =
759 fix_scan_list(root, splan->scan.plan.targetlist,
760 rtoffset, NUM_EXEC_TLIST(plan));
761 splan->scan.plan.qual =
762 fix_scan_list(root, splan->scan.plan.qual,
763 rtoffset, NUM_EXEC_QUAL(plan));
764 splan->functions =
765 fix_scan_list(root, splan->functions, rtoffset, 1);
766 }
767 break;
768 case T_TableFuncScan:
769 {
771
772 splan->scan.scanrelid += rtoffset;
773 splan->scan.plan.targetlist =
774 fix_scan_list(root, splan->scan.plan.targetlist,
775 rtoffset, NUM_EXEC_TLIST(plan));
776 splan->scan.plan.qual =
777 fix_scan_list(root, splan->scan.plan.qual,
778 rtoffset, NUM_EXEC_QUAL(plan));
779 splan->tablefunc = (TableFunc *)
780 fix_scan_expr(root, (Node *) splan->tablefunc,
781 rtoffset, 1);
782 }
783 break;
784 case T_ValuesScan:
785 {
787
788 splan->scan.scanrelid += rtoffset;
789 splan->scan.plan.targetlist =
790 fix_scan_list(root, splan->scan.plan.targetlist,
791 rtoffset, NUM_EXEC_TLIST(plan));
792 splan->scan.plan.qual =
793 fix_scan_list(root, splan->scan.plan.qual,
794 rtoffset, NUM_EXEC_QUAL(plan));
795 splan->values_lists =
796 fix_scan_list(root, splan->values_lists,
797 rtoffset, 1);
798 }
799 break;
800 case T_CteScan:
801 {
802 CteScan *splan = (CteScan *) plan;
803
804 splan->scan.scanrelid += rtoffset;
805 splan->scan.plan.targetlist =
806 fix_scan_list(root, splan->scan.plan.targetlist,
807 rtoffset, NUM_EXEC_TLIST(plan));
808 splan->scan.plan.qual =
809 fix_scan_list(root, splan->scan.plan.qual,
810 rtoffset, NUM_EXEC_QUAL(plan));
811 }
812 break;
813 case T_NamedTuplestoreScan:
814 {
816
817 splan->scan.scanrelid += rtoffset;
818 splan->scan.plan.targetlist =
819 fix_scan_list(root, splan->scan.plan.targetlist,
820 rtoffset, NUM_EXEC_TLIST(plan));
821 splan->scan.plan.qual =
822 fix_scan_list(root, splan->scan.plan.qual,
823 rtoffset, NUM_EXEC_QUAL(plan));
824 }
825 break;
826 case T_WorkTableScan:
827 {
829
830 splan->scan.scanrelid += rtoffset;
831 splan->scan.plan.targetlist =
832 fix_scan_list(root, splan->scan.plan.targetlist,
833 rtoffset, NUM_EXEC_TLIST(plan));
834 splan->scan.plan.qual =
835 fix_scan_list(root, splan->scan.plan.qual,
836 rtoffset, NUM_EXEC_QUAL(plan));
837 }
838 break;
839 case T_ForeignScan:
841 break;
842 case T_CustomScan:
844 break;
845
846 case T_NestLoop:
847 case T_MergeJoin:
848 case T_HashJoin:
849 set_join_references(root, (Join *) plan, rtoffset);
850 break;
851
852 case T_Gather:
853 case T_GatherMerge:
854 {
855 set_upper_references(root, plan, rtoffset);
857 }
858 break;
859
860 case T_Hash:
861 set_hash_references(root, plan, rtoffset);
862 break;
863
864 case T_Memoize:
865 {
866 Memoize *mplan = (Memoize *) plan;
867
868 /*
869 * Memoize does not evaluate its targetlist. It just uses the
870 * same targetlist from its outer subnode.
871 */
873
874 mplan->param_exprs = fix_scan_list(root, mplan->param_exprs,
875 rtoffset,
877 break;
878 }
879
880 case T_Material:
881 case T_Sort:
882 case T_IncrementalSort:
883 case T_Unique:
884 case T_SetOp:
885
886 /*
887 * These plan types don't actually bother to evaluate their
888 * targetlists, because they just return their unmodified input
889 * tuples. Even though the targetlist won't be used by the
890 * executor, we fix it up for possible use by EXPLAIN (not to
891 * mention ease of debugging --- wrong varnos are very confusing).
892 */
894
895 /*
896 * Since these plan types don't check quals either, we should not
897 * find any qual expression attached to them.
898 */
899 Assert(plan->qual == NIL);
900 break;
901 case T_LockRows:
902 {
904
905 /*
906 * Like the plan types above, LockRows doesn't evaluate its
907 * tlist or quals. But we have to fix up the RT indexes in
908 * its rowmarks.
909 */
911 Assert(splan->plan.qual == NIL);
912
913 foreach(l, splan->rowMarks)
914 {
915 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
916
917 rc->rti += rtoffset;
918 rc->prti += rtoffset;
919 }
920 }
921 break;
922 case T_Limit:
923 {
924 Limit *splan = (Limit *) plan;
925
926 /*
927 * Like the plan types above, Limit doesn't evaluate its tlist
928 * or quals. It does have live expressions for limit/offset,
929 * however; and those cannot contain subplan variable refs, so
930 * fix_scan_expr works for them.
931 */
933 Assert(splan->plan.qual == NIL);
934
935 splan->limitOffset =
936 fix_scan_expr(root, splan->limitOffset, rtoffset, 1);
937 splan->limitCount =
938 fix_scan_expr(root, splan->limitCount, rtoffset, 1);
939 }
940 break;
941 case T_Agg:
942 {
943 Agg *agg = (Agg *) plan;
944
945 /*
946 * If this node is combining partial-aggregation results, we
947 * must convert its Aggrefs to contain references to the
948 * partial-aggregate subexpressions that will be available
949 * from the child plan node.
950 */
952 {
953 plan->targetlist = (List *)
954 convert_combining_aggrefs((Node *) plan->targetlist,
955 NULL);
956 plan->qual = (List *)
958 NULL);
959 }
960
961 set_upper_references(root, plan, rtoffset);
962 }
963 break;
964 case T_Group:
965 set_upper_references(root, plan, rtoffset);
966 break;
967 case T_WindowAgg:
968 {
969 WindowAgg *wplan = (WindowAgg *) plan;
970
971 /*
972 * Adjust the WindowAgg's run conditions by swapping the
973 * WindowFuncs references out to instead reference the Var in
974 * the scan slot so that when the executor evaluates the
975 * runCondition, it receives the WindowFunc's value from the
976 * slot that the result has just been stored into rather than
977 * evaluating the WindowFunc all over again.
978 */
980 wplan->runCondition,
981 (Plan *) wplan);
982
983 set_upper_references(root, plan, rtoffset);
984
985 /*
986 * Like Limit node limit/offset expressions, WindowAgg has
987 * frame offset expressions, which cannot contain subplan
988 * variable refs, so fix_scan_expr works for them.
989 */
990 wplan->startOffset =
991 fix_scan_expr(root, wplan->startOffset, rtoffset, 1);
992 wplan->endOffset =
993 fix_scan_expr(root, wplan->endOffset, rtoffset, 1);
995 wplan->runCondition,
996 rtoffset,
999 wplan->runConditionOrig,
1000 rtoffset,
1002 }
1003 break;
1004 case T_Result:
1005 {
1006 Result *splan = (Result *) plan;
1007
1008 /*
1009 * Result may or may not have a subplan; if not, it's more
1010 * like a scan node than an upper node.
1011 */
1012 if (splan->plan.lefttree != NULL)
1013 set_upper_references(root, plan, rtoffset);
1014 else
1015 {
1016 /*
1017 * The tlist of a childless Result could contain
1018 * unresolved ROWID_VAR Vars, in case it's representing a
1019 * target relation which is completely empty because of
1020 * constraint exclusion. Replace any such Vars by null
1021 * constants, as though they'd been resolved for a leaf
1022 * scan node that doesn't support them. We could have
1023 * fix_scan_expr do this, but since the case is only
1024 * expected to occur here, it seems safer to special-case
1025 * it here and keep the assertions that ROWID_VARs
1026 * shouldn't be seen by fix_scan_expr.
1027 */
1028 foreach(l, splan->plan.targetlist)
1029 {
1030 TargetEntry *tle = (TargetEntry *) lfirst(l);
1031 Var *var = (Var *) tle->expr;
1032
1033 if (var && IsA(var, Var) && var->varno == ROWID_VAR)
1034 tle->expr = (Expr *) makeNullConst(var->vartype,
1035 var->vartypmod,
1036 var->varcollid);
1037 }
1038
1039 splan->plan.targetlist =
1040 fix_scan_list(root, splan->plan.targetlist,
1041 rtoffset, NUM_EXEC_TLIST(plan));
1042 splan->plan.qual =
1043 fix_scan_list(root, splan->plan.qual,
1044 rtoffset, NUM_EXEC_QUAL(plan));
1045 }
1046 /* resconstantqual can't contain any subplan variable refs */
1047 splan->resconstantqual =
1048 fix_scan_expr(root, splan->resconstantqual, rtoffset, 1);
1049 }
1050 break;
1051 case T_ProjectSet:
1052 set_upper_references(root, plan, rtoffset);
1053 break;
1054 case T_ModifyTable:
1055 {
1057 Plan *subplan = outerPlan(splan);
1058
1059 Assert(splan->plan.targetlist == NIL);
1060 Assert(splan->plan.qual == NIL);
1061
1062 splan->withCheckOptionLists =
1063 fix_scan_list(root, splan->withCheckOptionLists,
1064 rtoffset, 1);
1065
1066 if (splan->returningLists)
1067 {
1068 List *newRL = NIL;
1069 ListCell *lcrl,
1070 *lcrr;
1071
1072 /*
1073 * Pass each per-resultrel returningList through
1074 * set_returning_clause_references().
1075 */
1076 Assert(list_length(splan->returningLists) == list_length(splan->resultRelations));
1077 forboth(lcrl, splan->returningLists,
1078 lcrr, splan->resultRelations)
1079 {
1080 List *rlist = (List *) lfirst(lcrl);
1081 Index resultrel = lfirst_int(lcrr);
1082
1084 rlist,
1085 subplan,
1086 resultrel,
1087 rtoffset);
1088 newRL = lappend(newRL, rlist);
1089 }
1090 splan->returningLists = newRL;
1091
1092 /*
1093 * Set up the visible plan targetlist as being the same as
1094 * the first RETURNING list. This is for the use of
1095 * EXPLAIN; the executor won't pay any attention to the
1096 * targetlist. We postpone this step until here so that
1097 * we don't have to do set_returning_clause_references()
1098 * twice on identical targetlists.
1099 */
1100 splan->plan.targetlist = copyObject(linitial(newRL));
1101 }
1102
1103 /*
1104 * We treat ModifyTable with ON CONFLICT as a form of 'pseudo
1105 * join', where the inner side is the EXCLUDED tuple.
1106 * Therefore use fix_join_expr to setup the relevant variables
1107 * to INNER_VAR. We explicitly don't create any OUTER_VARs as
1108 * those are already used by RETURNING and it seems better to
1109 * be non-conflicting.
1110 */
1111 if (splan->onConflictSet)
1112 {
1113 indexed_tlist *itlist;
1114
1115 itlist = build_tlist_index(splan->exclRelTlist);
1116
1117 splan->onConflictSet =
1118 fix_join_expr(root, splan->onConflictSet,
1119 NULL, itlist,
1120 linitial_int(splan->resultRelations),
1121 rtoffset, NRM_EQUAL, NUM_EXEC_QUAL(plan));
1122
1123 splan->onConflictWhere = (Node *)
1124 fix_join_expr(root, (List *) splan->onConflictWhere,
1125 NULL, itlist,
1126 linitial_int(splan->resultRelations),
1127 rtoffset, NRM_EQUAL, NUM_EXEC_QUAL(plan));
1128
1129 pfree(itlist);
1130
1131 splan->exclRelTlist =
1132 fix_scan_list(root, splan->exclRelTlist, rtoffset, 1);
1133 }
1134
1135 /*
1136 * The MERGE statement produces the target rows by performing
1137 * a right join between the target relation and the source
1138 * relation (which could be a plain relation or a subquery).
1139 * The INSERT and UPDATE actions of the MERGE statement
1140 * require access to the columns from the source relation. We
1141 * arrange things so that the source relation attributes are
1142 * available as INNER_VAR and the target relation attributes
1143 * are available from the scan tuple.
1144 */
1145 if (splan->mergeActionLists != NIL)
1146 {
1147 List *newMJC = NIL;
1148 ListCell *lca,
1149 *lcj,
1150 *lcr;
1151
1152 /*
1153 * Fix the targetList of individual action nodes so that
1154 * the so-called "source relation" Vars are referenced as
1155 * INNER_VAR. Note that for this to work correctly during
1156 * execution, the ecxt_innertuple must be set to the tuple
1157 * obtained by executing the subplan, which is what
1158 * constitutes the "source relation".
1159 *
1160 * We leave the Vars from the result relation (i.e. the
1161 * target relation) unchanged i.e. those Vars would be
1162 * picked from the scan slot. So during execution, we must
1163 * ensure that ecxt_scantuple is setup correctly to refer
1164 * to the tuple from the target relation.
1165 */
1166 indexed_tlist *itlist;
1167
1168 itlist = build_tlist_index(subplan->targetlist);
1169
1170 forthree(lca, splan->mergeActionLists,
1171 lcj, splan->mergeJoinConditions,
1172 lcr, splan->resultRelations)
1173 {
1174 List *mergeActionList = lfirst(lca);
1175 Node *mergeJoinCondition = lfirst(lcj);
1176 Index resultrel = lfirst_int(lcr);
1177
1178 foreach(l, mergeActionList)
1179 {
1181
1182 /* Fix targetList of each action. */
1183 action->targetList = fix_join_expr(root,
1184 action->targetList,
1185 NULL, itlist,
1186 resultrel,
1187 rtoffset,
1188 NRM_EQUAL,
1190
1191 /* Fix quals too. */
1192 action->qual = (Node *) fix_join_expr(root,
1193 (List *) action->qual,
1194 NULL, itlist,
1195 resultrel,
1196 rtoffset,
1197 NRM_EQUAL,
1199 }
1200
1201 /* Fix join condition too. */
1202 mergeJoinCondition = (Node *)
1204 (List *) mergeJoinCondition,
1205 NULL, itlist,
1206 resultrel,
1207 rtoffset,
1208 NRM_EQUAL,
1210 newMJC = lappend(newMJC, mergeJoinCondition);
1211 }
1212 splan->mergeJoinConditions = newMJC;
1213 }
1214
1215 splan->nominalRelation += rtoffset;
1216 if (splan->rootRelation)
1217 splan->rootRelation += rtoffset;
1218 splan->exclRelRTI += rtoffset;
1219
1220 foreach(l, splan->resultRelations)
1221 {
1222 lfirst_int(l) += rtoffset;
1223 }
1224 foreach(l, splan->rowMarks)
1225 {
1226 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
1227
1228 rc->rti += rtoffset;
1229 rc->prti += rtoffset;
1230 }
1231
1232 /*
1233 * Append this ModifyTable node's final result relation RT
1234 * index(es) to the global list for the plan.
1235 */
1236 root->glob->resultRelations =
1237 list_concat(root->glob->resultRelations,
1238 splan->resultRelations);
1239 if (splan->rootRelation)
1240 {
1241 root->glob->resultRelations =
1242 lappend_int(root->glob->resultRelations,
1243 splan->rootRelation);
1244 }
1245 }
1246 break;
1247 case T_Append:
1248 /* Needs special treatment, see comments below */
1250 (Append *) plan,
1251 rtoffset);
1252 case T_MergeAppend:
1253 /* Needs special treatment, see comments below */
1255 (MergeAppend *) plan,
1256 rtoffset);
1257 case T_RecursiveUnion:
1258 /* This doesn't evaluate targetlist or check quals either */
1260 Assert(plan->qual == NIL);
1261 break;
1262 case T_BitmapAnd:
1263 {
1265
1266 /* BitmapAnd works like Append, but has no tlist */
1267 Assert(splan->plan.targetlist == NIL);
1268 Assert(splan->plan.qual == NIL);
1269 foreach(l, splan->bitmapplans)
1270 {
1272 (Plan *) lfirst(l),
1273 rtoffset);
1274 }
1275 }
1276 break;
1277 case T_BitmapOr:
1278 {
1279 BitmapOr *splan = (BitmapOr *) plan;
1280
1281 /* BitmapOr works like Append, but has no tlist */
1282 Assert(splan->plan.targetlist == NIL);
1283 Assert(splan->plan.qual == NIL);
1284 foreach(l, splan->bitmapplans)
1285 {
1287 (Plan *) lfirst(l),
1288 rtoffset);
1289 }
1290 }
1291 break;
1292 default:
1293 elog(ERROR, "unrecognized node type: %d",
1294 (int) nodeTag(plan));
1295 break;
1296 }
1297
1298 /*
1299 * Now recurse into child plans, if any
1300 *
1301 * NOTE: it is essential that we recurse into child plans AFTER we set
1302 * subplan references in this plan's tlist and quals. If we did the
1303 * reference-adjustments bottom-up, then we would fail to match this
1304 * plan's var nodes against the already-modified nodes of the children.
1305 */
1306 plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
1307 plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
1308
1309 return plan;
1310}
Datum lca(PG_FUNCTION_ARGS)
Definition: ltree_op.c:568
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:341
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:385
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define linitial_int(l)
Definition: pg_list.h:179
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:563
#define outerPlan(node)
Definition: plannodes.h:186
static SPIPlanPtr splan
Definition: regress.c:267
static void set_hash_references(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:1921
static Plan * set_append_references(PlannerInfo *root, Append *aplan, int rtoffset)
Definition: setrefs.c:1789
static Plan * set_mergeappend_references(PlannerInfo *root, MergeAppend *mplan, int rtoffset)
Definition: setrefs.c:1856
static List * set_returning_clause_references(PlannerInfo *root, List *rlist, Plan *topplan, Index resultRelation, int rtoffset)
Definition: setrefs.c:3367
static List * set_windowagg_runcondition_references(PlannerInfo *root, List *runcondition, Plan *plan)
Definition: setrefs.c:3462
static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:2449
static void set_param_references(PlannerInfo *root, Plan *plan)
Definition: setrefs.c:2537
static void set_foreignscan_references(PlannerInfo *root, ForeignScan *fscan, int rtoffset)
Definition: setrefs.c:1579
static Plan * set_subqueryscan_references(PlannerInfo *root, SubqueryScan *plan, int rtoffset)
Definition: setrefs.c:1396
static Plan * set_indexonlyscan_references(PlannerInfo *root, IndexOnlyScan *plan, int rtoffset)
Definition: setrefs.c:1322
static void set_customscan_references(PlannerInfo *root, CustomScan *cscan, int rtoffset)
Definition: setrefs.c:1666
static void set_join_references(PlannerInfo *root, Join *join, int rtoffset)
Definition: setrefs.c:2300
static Node * fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset, double num_exec)
Definition: setrefs.c:2180
AggSplit aggsplit
Definition: plannodes.h:1018
List * param_exprs
Definition: plannodes.h:917
Node * endOffset
Definition: plannodes.h:1090
List * runConditionOrig
Definition: plannodes.h:1096
Node * startOffset
Definition: plannodes.h:1087
List * runCondition
Definition: plannodes.h:1093

References generate_unaccent_rules::action, Agg::aggsplit, Assert, build_tlist_index(), convert_combining_aggrefs(), copyObject, DO_AGGSPLIT_COMBINE, elog, WindowAgg::endOffset, ERROR, TargetEntry::expr, fix_join_expr(), fix_scan_expr(), fix_scan_list, forboth, forthree, if(), IsA, lappend(), lappend_int(), lca(), lfirst, lfirst_int, linitial, linitial_int, list_concat(), list_length(), makeNullConst(), NIL, nodeTag, NRM_EQUAL, NUM_EXEC_QUAL, NUM_EXEC_TLIST, outerPlan, Memoize::param_exprs, pfree(), plan, PlanRowMark::prti, root, ROWID_VAR, PlanRowMark::rti, WindowAgg::runCondition, WindowAgg::runConditionOrig, set_append_references(), set_customscan_references(), set_dummy_tlist_references(), set_foreignscan_references(), set_hash_references(), set_indexonlyscan_references(), set_join_references(), set_mergeappend_references(), set_param_references(), set_plan_refs(), set_returning_clause_references(), set_subqueryscan_references(), set_upper_references(), set_windowagg_runcondition_references(), splan, WindowAgg::startOffset, Plan::targetlist, and Var::varno.

Referenced by set_append_references(), set_customscan_references(), set_mergeappend_references(), set_plan_references(), and set_plan_refs().

◆ set_returning_clause_references()

static List * set_returning_clause_references ( PlannerInfo root,
List rlist,
Plan topplan,
Index  resultRelation,
int  rtoffset 
)
static

Definition at line 3367 of file setrefs.c.

3372{
3373 indexed_tlist *itlist;
3374
3375 /*
3376 * We can perform the desired Var fixup by abusing the fix_join_expr
3377 * machinery that formerly handled inner indexscan fixup. We search the
3378 * top plan's targetlist for Vars of non-result relations, and use
3379 * fix_join_expr to convert RETURNING Vars into references to those tlist
3380 * entries, while leaving result-rel Vars as-is.
3381 *
3382 * PlaceHolderVars will also be sought in the targetlist, but no
3383 * more-complex expressions will be. Note that it is not possible for a
3384 * PlaceHolderVar to refer to the result relation, since the result is
3385 * never below an outer join. If that case could happen, we'd have to be
3386 * prepared to pick apart the PlaceHolderVar and evaluate its contained
3387 * expression instead.
3388 */
3389 itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
3390
3391 rlist = fix_join_expr(root,
3392 rlist,
3393 itlist,
3394 NULL,
3395 resultRelation,
3396 rtoffset,
3397 NRM_EQUAL,
3398 NUM_EXEC_TLIST(topplan));
3399
3400 pfree(itlist);
3401
3402 return rlist;
3403}
static indexed_tlist * build_tlist_index_other_vars(List *tlist, int ignore_rel)
Definition: setrefs.c:2778

References build_tlist_index_other_vars(), fix_join_expr(), NRM_EQUAL, NUM_EXEC_TLIST, pfree(), root, and Plan::targetlist.

Referenced by set_plan_refs().

◆ set_subqueryscan_references()

static Plan * set_subqueryscan_references ( PlannerInfo root,
SubqueryScan plan,
int  rtoffset 
)
static

Definition at line 1396 of file setrefs.c.

1399{
1400 RelOptInfo *rel;
1401 Plan *result;
1402
1403 /* Need to look up the subquery's RelOptInfo, since we need its subroot */
1404 rel = find_base_rel(root, plan->scan.scanrelid);
1405
1406 /* Recursively process the subplan */
1407 plan->subplan = set_plan_references(rel->subroot, plan->subplan);
1408
1410 {
1411 /*
1412 * We can omit the SubqueryScan node and just pull up the subplan.
1413 */
1414 result = clean_up_removed_plan_level((Plan *) plan, plan->subplan);
1415 }
1416 else
1417 {
1418 /*
1419 * Keep the SubqueryScan node. We have to do the processing that
1420 * set_plan_references would otherwise have done on it. Notice we do
1421 * not do set_upper_references() here, because a SubqueryScan will
1422 * always have been created with correct references to its subplan's
1423 * outputs to begin with.
1424 */
1425 plan->scan.scanrelid += rtoffset;
1426 plan->scan.plan.targetlist =
1427 fix_scan_list(root, plan->scan.plan.targetlist,
1428 rtoffset, NUM_EXEC_TLIST((Plan *) plan));
1429 plan->scan.plan.qual =
1430 fix_scan_list(root, plan->scan.plan.qual,
1431 rtoffset, NUM_EXEC_QUAL((Plan *) plan));
1432
1433 result = (Plan *) plan;
1434 }
1435
1436 return result;
1437}
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:414
Plan * set_plan_references(PlannerInfo *root, Plan *plan)
Definition: setrefs.c:288
bool trivial_subqueryscan(SubqueryScan *plan)
Definition: setrefs.c:1465

References clean_up_removed_plan_level(), find_base_rel(), fix_scan_list, NUM_EXEC_QUAL, NUM_EXEC_TLIST, plan, root, set_plan_references(), RelOptInfo::subroot, and trivial_subqueryscan().

Referenced by set_plan_refs().

◆ set_upper_references()

static void set_upper_references ( PlannerInfo root,
Plan plan,
int  rtoffset 
)
static

Definition at line 2449 of file setrefs.c.

2450{
2451 Plan *subplan = plan->lefttree;
2452 indexed_tlist *subplan_itlist;
2453 List *output_targetlist;
2454 ListCell *l;
2455
2456 subplan_itlist = build_tlist_index(subplan->targetlist);
2457
2458 /*
2459 * If it's a grouping node with grouping sets, any Vars and PHVs appearing
2460 * in the targetlist and quals should have nullingrels that include the
2461 * effects of the grouping step, ie they will have nullingrels equal to
2462 * the input Vars/PHVs' nullingrels plus the RT index of the grouping
2463 * step. In order to perform exact nullingrels matches, we remove the RT
2464 * index of the grouping step first.
2465 */
2466 if (IsA(plan, Agg) &&
2467 root->group_rtindex > 0 &&
2468 ((Agg *) plan)->groupingSets)
2469 {
2470 plan->targetlist = (List *)
2471 remove_nulling_relids((Node *) plan->targetlist,
2472 bms_make_singleton(root->group_rtindex),
2473 NULL);
2474 plan->qual = (List *)
2476 bms_make_singleton(root->group_rtindex),
2477 NULL);
2478 }
2479
2480 output_targetlist = NIL;
2481 foreach(l, plan->targetlist)
2482 {
2483 TargetEntry *tle = (TargetEntry *) lfirst(l);
2484 Node *newexpr;
2485
2486 /* If it's a sort/group item, first try to match by sortref */
2487 if (tle->ressortgroupref != 0)
2488 {
2489 newexpr = (Node *)
2491 tle->ressortgroupref,
2492 subplan_itlist,
2493 OUTER_VAR);
2494 if (!newexpr)
2495 newexpr = fix_upper_expr(root,
2496 (Node *) tle->expr,
2497 subplan_itlist,
2498 OUTER_VAR,
2499 rtoffset,
2500 NRM_EQUAL,
2502 }
2503 else
2504 newexpr = fix_upper_expr(root,
2505 (Node *) tle->expr,
2506 subplan_itlist,
2507 OUTER_VAR,
2508 rtoffset,
2509 NRM_EQUAL,
2511 tle = flatCopyTargetEntry(tle);
2512 tle->expr = (Expr *) newexpr;
2513 output_targetlist = lappend(output_targetlist, tle);
2514 }
2515 plan->targetlist = output_targetlist;
2516
2517 plan->qual = (List *)
2519 (Node *) plan->qual,
2520 subplan_itlist,
2521 OUTER_VAR,
2522 rtoffset,
2523 NRM_EQUAL,
2525
2526 pfree(subplan_itlist);
2527}
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
static Var * search_indexed_tlist_for_sortgroupref(Expr *node, Index sortgroupref, indexed_tlist *itlist, int newvarno)
Definition: setrefs.c:2994

References bms_make_singleton(), build_tlist_index(), TargetEntry::expr, fix_upper_expr(), flatCopyTargetEntry(), IsA, lappend(), lfirst, NIL, NRM_EQUAL, NUM_EXEC_QUAL, NUM_EXEC_TLIST, OUTER_VAR, pfree(), plan, remove_nulling_relids(), TargetEntry::ressortgroupref, root, search_indexed_tlist_for_sortgroupref(), and Plan::targetlist.

Referenced by set_plan_refs().

◆ set_windowagg_runcondition_references()

static List * set_windowagg_runcondition_references ( PlannerInfo root,
List runcondition,
Plan plan 
)
static

Definition at line 3462 of file setrefs.c.

3465{
3466 List *newlist;
3467 indexed_tlist *itlist;
3468
3469 itlist = build_tlist_index(plan->targetlist);
3470
3471 newlist = fix_windowagg_condition_expr(root, runcondition, itlist);
3472
3473 pfree(itlist);
3474
3475 return newlist;
3476}
static List * fix_windowagg_condition_expr(PlannerInfo *root, List *runcondition, indexed_tlist *subplan_itlist)
Definition: setrefs.c:3441

References build_tlist_index(), fix_windowagg_condition_expr(), pfree(), plan, and root.

Referenced by set_plan_refs().

◆ trivial_subqueryscan()

bool trivial_subqueryscan ( SubqueryScan plan)

Definition at line 1465 of file setrefs.c.

1466{
1467 int attrno;
1468 ListCell *lp,
1469 *lc;
1470
1471 /* We might have detected this already; in which case reuse the result */
1472 if (plan->scanstatus == SUBQUERY_SCAN_TRIVIAL)
1473 return true;
1474 if (plan->scanstatus == SUBQUERY_SCAN_NONTRIVIAL)
1475 return false;
1476 Assert(plan->scanstatus == SUBQUERY_SCAN_UNKNOWN);
1477 /* Initially, mark the SubqueryScan as non-deletable from the plan tree */
1478 plan->scanstatus = SUBQUERY_SCAN_NONTRIVIAL;
1479
1480 if (plan->scan.plan.qual != NIL)
1481 return false;
1482
1483 if (list_length(plan->scan.plan.targetlist) !=
1484 list_length(plan->subplan->targetlist))
1485 return false; /* tlists not same length */
1486
1487 attrno = 1;
1488 forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
1489 {
1490 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1491 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1492
1493 if (ptle->resjunk != ctle->resjunk)
1494 return false; /* tlist doesn't match junk status */
1495
1496 /*
1497 * We accept either a Var referencing the corresponding element of the
1498 * subplan tlist, or a Const equaling the subplan element. See
1499 * generate_setop_tlist() for motivation.
1500 */
1501 if (ptle->expr && IsA(ptle->expr, Var))
1502 {
1503 Var *var = (Var *) ptle->expr;
1504
1505 Assert(var->varno == plan->scan.scanrelid);
1506 Assert(var->varlevelsup == 0);
1507 if (var->varattno != attrno)
1508 return false; /* out of order */
1509 }
1510 else if (ptle->expr && IsA(ptle->expr, Const))
1511 {
1512 if (!equal(ptle->expr, ctle->expr))
1513 return false;
1514 }
1515 else
1516 return false;
1517
1518 attrno++;
1519 }
1520
1521 /* Re-mark the SubqueryScan as deletable from the plan tree */
1522 plan->scanstatus = SUBQUERY_SCAN_TRIVIAL;
1523
1524 return true;
1525}
@ SUBQUERY_SCAN_NONTRIVIAL
Definition: plannodes.h:609
@ SUBQUERY_SCAN_UNKNOWN
Definition: plannodes.h:607
@ SUBQUERY_SCAN_TRIVIAL
Definition: plannodes.h:608

References Assert, equal(), TargetEntry::expr, forboth, IsA, lfirst, list_length(), NIL, plan, SUBQUERY_SCAN_NONTRIVIAL, SUBQUERY_SCAN_TRIVIAL, SUBQUERY_SCAN_UNKNOWN, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by mark_async_capable_plan(), and set_subqueryscan_references().