PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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. Also add its new RT
568 * index to the set of relations to be potentially accessed during
569 * execution.
570 *
571 * We do this even though the RTE might be unreferenced in the plan tree;
572 * this would correspond to cases such as views that were expanded, child
573 * tables that were eliminated by constraint exclusion, etc. Schema
574 * invalidation on such a rel must still force rebuilding of the plan.
575 *
576 * Note we don't bother to avoid making duplicate list entries. We could,
577 * but it would probably cost more cycles than it would save.
578 */
579 if (newrte->rtekind == RTE_RELATION ||
580 (newrte->rtekind == RTE_SUBQUERY && OidIsValid(newrte->relid)))
581 {
582 glob->relationOids = lappend_oid(glob->relationOids, newrte->relid);
583 glob->allRelids = bms_add_member(glob->allRelids,
584 list_length(glob->finalrtable));
585 }
586
587 /*
588 * Add a copy of the RTEPermissionInfo, if any, corresponding to this RTE
589 * to the flattened global list.
590 */
591 if (rte->perminfoindex > 0)
592 {
593 RTEPermissionInfo *perminfo;
594 RTEPermissionInfo *newperminfo;
595
596 /* Get the existing one from this query's rteperminfos. */
597 perminfo = getRTEPermissionInfo(rteperminfos, newrte);
598
599 /*
600 * Add a new one to finalrteperminfos and copy the contents of the
601 * existing one into it. Note that addRTEPermissionInfo() also
602 * updates newrte->perminfoindex to point to newperminfo in
603 * finalrteperminfos.
604 */
605 newrte->perminfoindex = 0; /* expected by addRTEPermissionInfo() */
606 newperminfo = addRTEPermissionInfo(&glob->finalrteperminfos, newrte);
607 memcpy(newperminfo, perminfo, sizeof(RTEPermissionInfo));
608 }
609}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
#define OidIsValid(objectId)
Definition: c.h:746
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
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
Bitmapset * allRelids
Definition: pathnodes.h:123
List * relationOids
Definition: pathnodes.h:151
List * finalrteperminfos
Definition: pathnodes.h:133
List * finalrtable
Definition: pathnodes.h:117
TableFunc * tablefunc
Definition: parsenodes.h:1198
struct TableSampleClause * tablesample
Definition: parsenodes.h:1112
Query * subquery
Definition: parsenodes.h:1118
List * values_lists
Definition: parsenodes.h:1204
List * functions
Definition: parsenodes.h:1191
RTEKind rtekind
Definition: parsenodes.h:1061

References addRTEPermissionInfo(), PlannerGlobal::allRelids, bms_add_member(), PlannerGlobal::finalrtable, PlannerGlobal::finalrteperminfos, RangeTblEntry::functions, getRTEPermissionInfo(), lappend(), lappend_oid(), list_length(), NIL, OidIsValid, palloc(), PlannerGlobal::relationOids, 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}
unsigned int Index
Definition: c.h:585
Assert(PointerIsAligned(start, uint64))
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1985
@ 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:945
PlannerInfo * subroot
Definition: pathnodes.h:980

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, 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 2757 of file setrefs.c.

2758{
2759 indexed_tlist *itlist;
2760 tlist_vinfo *vinfo;
2761 ListCell *l;
2762
2763 /* Create data structure with enough slots for all tlist entries */
2764 itlist = (indexed_tlist *)
2765 palloc(offsetof(indexed_tlist, vars) +
2766 list_length(tlist) * sizeof(tlist_vinfo));
2767
2768 itlist->tlist = tlist;
2769 itlist->has_ph_vars = false;
2770 itlist->has_non_vars = false;
2771
2772 /* Find the Vars and fill in the index array */
2773 vinfo = itlist->vars;
2774 foreach(l, tlist)
2775 {
2776 TargetEntry *tle = (TargetEntry *) lfirst(l);
2777
2778 if (tle->expr && IsA(tle->expr, Var))
2779 {
2780 Var *var = (Var *) tle->expr;
2781
2782 vinfo->varno = var->varno;
2783 vinfo->varattno = var->varattno;
2784 vinfo->resno = tle->resno;
2785 vinfo->varnullingrels = var->varnullingrels;
2786 vinfo++;
2787 }
2788 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2789 itlist->has_ph_vars = true;
2790 else
2791 itlist->has_non_vars = true;
2792 }
2793
2794 itlist->num_vars = (vinfo - itlist->vars);
2795
2796 return itlist;
2797}
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
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 2808 of file setrefs.c.

2809{
2810 indexed_tlist *itlist;
2811 tlist_vinfo *vinfo;
2812 ListCell *l;
2813
2814 /* Create data structure with enough slots for all tlist entries */
2815 itlist = (indexed_tlist *)
2816 palloc(offsetof(indexed_tlist, vars) +
2817 list_length(tlist) * sizeof(tlist_vinfo));
2818
2819 itlist->tlist = tlist;
2820 itlist->has_ph_vars = false;
2821 itlist->has_non_vars = false;
2822
2823 /* Find the desired Vars and fill in the index array */
2824 vinfo = itlist->vars;
2825 foreach(l, tlist)
2826 {
2827 TargetEntry *tle = (TargetEntry *) lfirst(l);
2828
2829 if (tle->expr && IsA(tle->expr, Var))
2830 {
2831 Var *var = (Var *) tle->expr;
2832
2833 if (var->varno != ignore_rel)
2834 {
2835 vinfo->varno = var->varno;
2836 vinfo->varattno = var->varattno;
2837 vinfo->resno = tle->resno;
2838 vinfo->varnullingrels = var->varnullingrels;
2839 vinfo++;
2840 }
2841 }
2842 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2843 itlist->has_ph_vars = true;
2844 }
2845
2846 itlist->num_vars = (vinfo - itlist->vars);
2847
2848 return itlist;
2849}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:78

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 1544 of file setrefs.c.

1545{
1546 /*
1547 * We have to be sure we don't lose any initplans, so move any that were
1548 * attached to the parent plan to the child. If any are parallel-unsafe,
1549 * the child is no longer parallel-safe. As a cosmetic matter, also add
1550 * the initplans' run costs to the child's costs.
1551 */
1552 if (parent->initPlan)
1553 {
1554 Cost initplan_cost;
1555 bool unsafe_initplans;
1556
1558 &initplan_cost, &unsafe_initplans);
1559 child->startup_cost += initplan_cost;
1560 child->total_cost += initplan_cost;
1561 if (unsafe_initplans)
1562 child->parallel_safe = false;
1563
1564 /*
1565 * Attach plans this way so that parent's initplans are processed
1566 * before any pre-existing initplans of the child. Probably doesn't
1567 * matter, but let's preserve the ordering just in case.
1568 */
1569 child->initPlan = list_concat(parent->initPlan,
1570 child->initPlan);
1571 }
1572
1573 /*
1574 * We also have to transfer the parent's column labeling info into the
1575 * child, else columns sent to client will be improperly labeled if this
1576 * is the topmost plan level. resjunk and so on may be important too.
1577 */
1579
1580 return child;
1581}
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
double Cost
Definition: nodes.h:257
Cost total_cost
Definition: plannodes.h:179
Cost startup_cost
Definition: plannodes.h:177
bool parallel_safe
Definition: plannodes.h:195
List * targetlist
Definition: plannodes.h:209
List * initPlan
Definition: plannodes.h:216
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 2622 of file setrefs.c.

2623{
2624 if (node == NULL)
2625 return NULL;
2626 if (IsA(node, Aggref))
2627 {
2628 Aggref *orig_agg = (Aggref *) node;
2629 Aggref *child_agg;
2630 Aggref *parent_agg;
2631
2632 /* Assert we've not chosen to partial-ize any unsupported cases */
2633 Assert(orig_agg->aggorder == NIL);
2634 Assert(orig_agg->aggdistinct == NIL);
2635
2636 /*
2637 * Since aggregate calls can't be nested, we needn't recurse into the
2638 * arguments. But for safety, flat-copy the Aggref node itself rather
2639 * than modifying it in-place.
2640 */
2641 child_agg = makeNode(Aggref);
2642 memcpy(child_agg, orig_agg, sizeof(Aggref));
2643
2644 /*
2645 * For the parent Aggref, we want to copy all the fields of the
2646 * original aggregate *except* the args list, which we'll replace
2647 * below, and the aggfilter expression, which should be applied only
2648 * by the child not the parent. Rather than explicitly knowing about
2649 * all the other fields here, we can momentarily modify child_agg to
2650 * provide a suitable source for copyObject.
2651 */
2652 child_agg->args = NIL;
2653 child_agg->aggfilter = NULL;
2654 parent_agg = copyObject(child_agg);
2655 child_agg->args = orig_agg->args;
2656 child_agg->aggfilter = orig_agg->aggfilter;
2657
2658 /*
2659 * Now, set up child_agg to represent the first phase of partial
2660 * aggregation. For now, assume serialization is required.
2661 */
2663
2664 /*
2665 * And set up parent_agg to represent the second phase.
2666 */
2667 parent_agg->args = list_make1(makeTargetEntry((Expr *) child_agg,
2668 1, NULL, false));
2670
2671 return (Node *) parent_agg;
2672 }
2674}
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:289
#define expression_tree_mutator(n, m, c)
Definition: nodeFuncs.h:155
#define copyObject(obj)
Definition: nodes.h:230
@ AGGSPLIT_FINAL_DESERIAL
Definition: nodes.h:387
@ AGGSPLIT_INITIAL_SERIAL
Definition: nodes.h:385
#define makeNode(_type_)
Definition: nodes.h:161
#define list_make1(x1)
Definition: pg_list.h:212
void mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
Definition: planner.c:5644
static Node * convert_combining_aggrefs(Node *node, void *context)
Definition: setrefs.c:2622
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:135

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 2006 of file setrefs.c.

2007{
2008 Var *newvar = (Var *) palloc(sizeof(Var));
2009
2010 *newvar = *var;
2011 return newvar;
2012}

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 3633 of file setrefs.c.

3637{
3638 PlannerGlobal glob;
3640
3641 /* Make up dummy planner state so we can use this module's machinery */
3642 MemSet(&glob, 0, sizeof(glob));
3643 glob.type = T_PlannerGlobal;
3644 glob.relationOids = NIL;
3645 glob.invalItems = NIL;
3646 /* Hack: we use glob.dependsOnRole to collect hasRowSecurity flags */
3647 glob.dependsOnRole = false;
3648
3649 MemSet(&root, 0, sizeof(root));
3650 root.type = T_PlannerInfo;
3651 root.glob = &glob;
3652
3654
3655 *relationOids = glob.relationOids;
3656 *invalItems = glob.invalItems;
3657 *hasRowSecurity = glob.dependsOnRole;
3658}
#define MemSet(start, val, len)
Definition: c.h:991
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:3669
bool dependsOnRole
Definition: pathnodes.h:172
List * invalItems
Definition: pathnodes.h:154

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 3669 of file setrefs.c.

3670{
3671 if (node == NULL)
3672 return false;
3673 Assert(!IsA(node, PlaceHolderVar));
3674 if (IsA(node, Query))
3675 {
3676 Query *query = (Query *) node;
3677 ListCell *lc;
3678
3679 if (query->commandType == CMD_UTILITY)
3680 {
3681 /*
3682 * This logic must handle any utility command for which parse
3683 * analysis was nontrivial (cf. stmt_requires_parse_analysis).
3684 *
3685 * Notably, CALL requires its own processing.
3686 */
3687 if (IsA(query->utilityStmt, CallStmt))
3688 {
3689 CallStmt *callstmt = (CallStmt *) query->utilityStmt;
3690
3691 /* We need not examine funccall, just the transformed exprs */
3692 (void) extract_query_dependencies_walker((Node *) callstmt->funcexpr,
3693 context);
3694 (void) extract_query_dependencies_walker((Node *) callstmt->outargs,
3695 context);
3696 return false;
3697 }
3698
3699 /*
3700 * Ignore other utility statements, except those (such as EXPLAIN)
3701 * that contain a parsed-but-not-planned query. For those, we
3702 * just need to transfer our attention to the contained query.
3703 */
3704 query = UtilityContainsQuery(query->utilityStmt);
3705 if (query == NULL)
3706 return false;
3707 }
3708
3709 /* Remember if any Query has RLS quals applied by rewriter */
3710 if (query->hasRowSecurity)
3711 context->glob->dependsOnRole = true;
3712
3713 /* Collect relation OIDs in this Query's rtable */
3714 foreach(lc, query->rtable)
3715 {
3716 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3717
3718 if (rte->rtekind == RTE_RELATION ||
3719 (rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)) ||
3720 (rte->rtekind == RTE_NAMEDTUPLESTORE && OidIsValid(rte->relid)))
3721 context->glob->relationOids =
3722 lappend_oid(context->glob->relationOids, rte->relid);
3723 }
3724
3725 /* And recurse into the query's subexpressions */
3727 context, 0);
3728 }
3729 /* Extract function dependencies and check for regclass Consts */
3730 fix_expr_common(context, node);
3732 context);
3733}
#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:276
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1033
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:2028
FuncExpr * funcexpr
Definition: parsenodes.h:3602
List * outargs
Definition: parsenodes.h:3604
PlannerGlobal * glob
Definition: pathnodes.h:229
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, 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 3519 of file setrefs.c.

3520{
3521 if (root->minmax_aggs != NIL &&
3522 list_length(aggref->args) == 1)
3523 {
3524 TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
3525 ListCell *lc;
3526
3527 foreach(lc, root->minmax_aggs)
3528 {
3529 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
3530
3531 if (mminfo->aggfnoid == aggref->aggfnoid &&
3532 equal(mminfo->target, curTarget->expr))
3533 return mminfo->param;
3534 }
3535 }
3536 return NULL;
3537}
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:3171
Expr * target
Definition: pathnodes.h:3156

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 2154 of file setrefs.c.

2156{
2157 SubPlan *bestplan = NULL;
2158 Cost bestcost = 0;
2159 ListCell *lc;
2160
2161 /*
2162 * Compute the estimated cost of each subplan assuming num_exec
2163 * executions, and keep the cheapest one. In event of exact equality of
2164 * estimates, we prefer the later plan; this is a bit arbitrary, but in
2165 * current usage it biases us to break ties against fast-start subplans.
2166 */
2167 Assert(asplan->subplans != NIL);
2168
2169 foreach(lc, asplan->subplans)
2170 {
2171 SubPlan *curplan = (SubPlan *) lfirst(lc);
2172 Cost curcost;
2173
2174 curcost = curplan->startup_cost + num_exec * curplan->per_call_cost;
2175 if (bestplan == NULL || curcost <= bestcost)
2176 {
2177 bestplan = curplan;
2178 bestcost = curcost;
2179 }
2180
2181 /* Also mark all subplans that are in AlternativeSubPlans */
2182 root->isAltSubplan[curplan->plan_id - 1] = true;
2183 }
2184
2185 /* Mark the subplan we selected */
2186 root->isUsedSubplan[bestplan->plan_id - 1] = true;
2187
2188 return (Node *) bestplan;
2189}
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 2028 of file setrefs.c.

2029{
2030 /* We assume callers won't call us on a NULL pointer */
2031 if (IsA(node, Aggref))
2032 {
2034 ((Aggref *) node)->aggfnoid);
2035 }
2036 else if (IsA(node, WindowFunc))
2037 {
2039 ((WindowFunc *) node)->winfnoid);
2040 }
2041 else if (IsA(node, FuncExpr))
2042 {
2044 ((FuncExpr *) node)->funcid);
2045 }
2046 else if (IsA(node, OpExpr))
2047 {
2048 set_opfuncid((OpExpr *) node);
2050 ((OpExpr *) node)->opfuncid);
2051 }
2052 else if (IsA(node, DistinctExpr))
2053 {
2054 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
2056 ((DistinctExpr *) node)->opfuncid);
2057 }
2058 else if (IsA(node, NullIfExpr))
2059 {
2060 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
2062 ((NullIfExpr *) node)->opfuncid);
2063 }
2064 else if (IsA(node, ScalarArrayOpExpr))
2065 {
2066 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
2067
2068 set_sa_opfuncid(saop);
2069 record_plan_function_dependency(root, saop->opfuncid);
2070
2071 if (OidIsValid(saop->hashfuncid))
2072 record_plan_function_dependency(root, saop->hashfuncid);
2073
2074 if (OidIsValid(saop->negfuncid))
2075 record_plan_function_dependency(root, saop->negfuncid);
2076 }
2077 else if (IsA(node, Const))
2078 {
2079 Const *con = (Const *) node;
2080
2081 /* Check for regclass reference */
2082 if (ISREGCLASSCONST(con))
2083 root->glob->relationOids =
2084 lappend_oid(root->glob->relationOids,
2085 DatumGetObjectId(con->constvalue));
2086 }
2087 else if (IsA(node, GroupingFunc))
2088 {
2089 GroupingFunc *g = (GroupingFunc *) node;
2090 AttrNumber *grouping_map = root->grouping_map;
2091
2092 /* If there are no grouping sets, we don't need this. */
2093
2094 Assert(grouping_map || g->cols == NIL);
2095
2096 if (grouping_map)
2097 {
2098 ListCell *lc;
2099 List *cols = NIL;
2100
2101 foreach(lc, g->refs)
2102 {
2103 cols = lappend_int(cols, grouping_map[lfirst_int(lc)]);
2104 }
2105
2106 Assert(!g->cols || equal(cols, g->cols));
2107
2108 if (!g->cols)
2109 g->cols = cols;
2110 }
2111 }
2112}
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:3552
#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 3102 of file setrefs.c.

3110{
3111 fix_join_expr_context context;
3112
3113 context.root = root;
3114 context.outer_itlist = outer_itlist;
3115 context.inner_itlist = inner_itlist;
3116 context.acceptable_rel = acceptable_rel;
3117 context.rtoffset = rtoffset;
3118 context.nrm_match = nrm_match;
3119 context.num_exec = num_exec;
3120 return (List *) fix_join_expr_mutator((Node *) clauses, &context);
3121}
static Node * fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
Definition: setrefs.c:3124
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 3124 of file setrefs.c.

3125{
3126 Var *newvar;
3127
3128 if (node == NULL)
3129 return NULL;
3130 if (IsA(node, Var))
3131 {
3132 Var *var = (Var *) node;
3133
3134 /*
3135 * Verify that Vars with non-default varreturningtype only appear in
3136 * the RETURNING list, and refer to the target relation.
3137 */
3139 {
3140 if (context->inner_itlist != NULL ||
3141 context->outer_itlist == NULL ||
3142 context->acceptable_rel == 0)
3143 elog(ERROR, "variable returning old/new found outside RETURNING list");
3144 if (var->varno != context->acceptable_rel)
3145 elog(ERROR, "wrong varno %d (expected %d) for variable returning old/new",
3146 var->varno, context->acceptable_rel);
3147 }
3148
3149 /* Look for the var in the input tlists, first in the outer */
3150 if (context->outer_itlist)
3151 {
3152 newvar = search_indexed_tlist_for_var(var,
3153 context->outer_itlist,
3154 OUTER_VAR,
3155 context->rtoffset,
3156 context->nrm_match);
3157 if (newvar)
3158 return (Node *) newvar;
3159 }
3160
3161 /* then in the inner. */
3162 if (context->inner_itlist)
3163 {
3164 newvar = search_indexed_tlist_for_var(var,
3165 context->inner_itlist,
3166 INNER_VAR,
3167 context->rtoffset,
3168 context->nrm_match);
3169 if (newvar)
3170 return (Node *) newvar;
3171 }
3172
3173 /* If it's for acceptable_rel, adjust and return it */
3174 if (var->varno == context->acceptable_rel)
3175 {
3176 var = copyVar(var);
3177 var->varno += context->rtoffset;
3178 if (var->varnosyn > 0)
3179 var->varnosyn += context->rtoffset;
3180 return (Node *) var;
3181 }
3182
3183 /* No referent found for Var */
3184 elog(ERROR, "variable not found in subplan target lists");
3185 }
3186 if (IsA(node, PlaceHolderVar))
3187 {
3188 PlaceHolderVar *phv = (PlaceHolderVar *) node;
3189
3190 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
3191 if (context->outer_itlist && context->outer_itlist->has_ph_vars)
3192 {
3193 newvar = search_indexed_tlist_for_phv(phv,
3194 context->outer_itlist,
3195 OUTER_VAR,
3196 context->nrm_match);
3197 if (newvar)
3198 return (Node *) newvar;
3199 }
3200 if (context->inner_itlist && context->inner_itlist->has_ph_vars)
3201 {
3202 newvar = search_indexed_tlist_for_phv(phv,
3203 context->inner_itlist,
3204 INNER_VAR,
3205 context->nrm_match);
3206 if (newvar)
3207 return (Node *) newvar;
3208 }
3209
3210 /* If not supplied by input plans, evaluate the contained expr */
3211 /* XXX can we assert something about phnullingrels? */
3212 return fix_join_expr_mutator((Node *) phv->phexpr, context);
3213 }
3214 /* Try matching more complex expressions too, if tlists have any */
3215 if (context->outer_itlist && context->outer_itlist->has_non_vars)
3216 {
3217 newvar = search_indexed_tlist_for_non_var((Expr *) node,
3218 context->outer_itlist,
3219 OUTER_VAR);
3220 if (newvar)
3221 return (Node *) newvar;
3222 }
3223 if (context->inner_itlist && context->inner_itlist->has_non_vars)
3224 {
3225 newvar = search_indexed_tlist_for_non_var((Expr *) node,
3226 context->inner_itlist,
3227 INNER_VAR);
3228 if (newvar)
3229 return (Node *) newvar;
3230 }
3231 /* Special cases (apply only AFTER failing to match to lower tlist) */
3232 if (IsA(node, Param))
3233 return fix_param_node(context->root, (Param *) node);
3234 if (IsA(node, AlternativeSubPlan))
3236 (AlternativeSubPlan *) node,
3237 context->num_exec),
3238 context);
3239 fix_expr_common(context->root, node);
3240 return expression_tree_mutator(node, fix_join_expr_mutator, context);
3241}
#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:2123
static Var * search_indexed_tlist_for_non_var(Expr *node, indexed_tlist *itlist, int newvarno)
Definition: setrefs.c:2984
static Var * search_indexed_tlist_for_phv(PlaceHolderVar *phv, indexed_tlist *itlist, int newvarno, NullingRelsMatch nrm_match)
Definition: setrefs.c:2931
static Var * copyVar(Var *var)
Definition: setrefs.c:2006
static Node * fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
Definition: setrefs.c:2154
static Var * search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, int newvarno, int rtoffset, NullingRelsMatch nrm_match)
Definition: setrefs.c:2866
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 2123 of file setrefs.c.

2124{
2125 if (p->paramkind == PARAM_MULTIEXPR)
2126 {
2127 int subqueryid = p->paramid >> 16;
2128 int colno = p->paramid & 0xFFFF;
2129 List *params;
2130
2131 if (subqueryid <= 0 ||
2132 subqueryid > list_length(root->multiexpr_params))
2133 elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
2134 params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
2135 if (colno <= 0 || colno > list_length(params))
2136 elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
2137 return copyObject(list_nth(params, colno - 1));
2138 }
2139 return (Node *) copyObject(p);
2140}
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 2210 of file setrefs.c.

2211{
2212 fix_scan_expr_context context;
2213
2214 context.root = root;
2215 context.rtoffset = rtoffset;
2216 context.num_exec = num_exec;
2217
2218 if (rtoffset != 0 ||
2219 root->multiexpr_params != NIL ||
2220 root->glob->lastPHId != 0 ||
2221 root->minmax_aggs != NIL ||
2222 root->hasAlternativeSubPlans)
2223 {
2224 return fix_scan_expr_mutator(node, &context);
2225 }
2226 else
2227 {
2228 /*
2229 * If rtoffset == 0, we don't need to change any Vars, and if there
2230 * are no MULTIEXPR subqueries then we don't need to replace
2231 * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
2232 * we won't need to remove them, and if there are no minmax Aggrefs we
2233 * won't need to replace them, and if there are no AlternativeSubPlans
2234 * we won't need to remove them. Then it's OK to just scribble on the
2235 * input node tree instead of copying (since the only change, filling
2236 * in any unset opfuncid fields, is harmless). This saves just enough
2237 * cycles to be noticeable on trivial queries.
2238 */
2239 (void) fix_scan_expr_walker(node, &context);
2240 return node;
2241 }
2242}
static Node * fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:2245
static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:2310
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 2245 of file setrefs.c.

2246{
2247 if (node == NULL)
2248 return NULL;
2249 if (IsA(node, Var))
2250 {
2251 Var *var = copyVar((Var *) node);
2252
2253 Assert(var->varlevelsup == 0);
2254
2255 /*
2256 * We should not see Vars marked INNER_VAR, OUTER_VAR, or ROWID_VAR.
2257 * But an indexqual expression could contain INDEX_VAR Vars.
2258 */
2259 Assert(var->varno != INNER_VAR);
2260 Assert(var->varno != OUTER_VAR);
2261 Assert(var->varno != ROWID_VAR);
2262 if (!IS_SPECIAL_VARNO(var->varno))
2263 var->varno += context->rtoffset;
2264 if (var->varnosyn > 0)
2265 var->varnosyn += context->rtoffset;
2266 return (Node *) var;
2267 }
2268 if (IsA(node, Param))
2269 return fix_param_node(context->root, (Param *) node);
2270 if (IsA(node, Aggref))
2271 {
2272 Aggref *aggref = (Aggref *) node;
2273 Param *aggparam;
2274
2275 /* See if the Aggref should be replaced by a Param */
2276 aggparam = find_minmax_agg_replacement_param(context->root, aggref);
2277 if (aggparam != NULL)
2278 {
2279 /* Make a copy of the Param for paranoia's sake */
2280 return (Node *) copyObject(aggparam);
2281 }
2282 /* If no match, just fall through to process it normally */
2283 }
2284 if (IsA(node, CurrentOfExpr))
2285 {
2286 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
2287
2288 Assert(!IS_SPECIAL_VARNO(cexpr->cvarno));
2289 cexpr->cvarno += context->rtoffset;
2290 return (Node *) cexpr;
2291 }
2292 if (IsA(node, PlaceHolderVar))
2293 {
2294 /* At scan level, we should always just evaluate the contained expr */
2295 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2296
2297 /* XXX can we assert something about phnullingrels? */
2298 return fix_scan_expr_mutator((Node *) phv->phexpr, context);
2299 }
2300 if (IsA(node, AlternativeSubPlan))
2302 (AlternativeSubPlan *) node,
2303 context->num_exec),
2304 context);
2305 fix_expr_common(context->root, node);
2306 return expression_tree_mutator(node, fix_scan_expr_mutator, context);
2307}
#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:3519
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 2310 of file setrefs.c.

2311{
2312 if (node == NULL)
2313 return false;
2314 Assert(!(IsA(node, Var) && ((Var *) node)->varno == ROWID_VAR));
2315 Assert(!IsA(node, PlaceHolderVar));
2316 Assert(!IsA(node, AlternativeSubPlan));
2317 fix_expr_common(context->root, node);
2318 return expression_tree_walker(node, fix_scan_expr_walker, context);
2319}

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 3276 of file setrefs.c.

3283{
3284 fix_upper_expr_context context;
3285
3286 context.root = root;
3287 context.subplan_itlist = subplan_itlist;
3288 context.newvarno = newvarno;
3289 context.rtoffset = rtoffset;
3290 context.nrm_match = nrm_match;
3291 context.num_exec = num_exec;
3292 return fix_upper_expr_mutator(node, &context);
3293}
static Node * fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
Definition: setrefs.c:3296
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 3296 of file setrefs.c.

3297{
3298 Var *newvar;
3299
3300 if (node == NULL)
3301 return NULL;
3302 if (IsA(node, Var))
3303 {
3304 Var *var = (Var *) node;
3305
3306 newvar = search_indexed_tlist_for_var(var,
3307 context->subplan_itlist,
3308 context->newvarno,
3309 context->rtoffset,
3310 context->nrm_match);
3311 if (!newvar)
3312 elog(ERROR, "variable not found in subplan target list");
3313 return (Node *) newvar;
3314 }
3315 if (IsA(node, PlaceHolderVar))
3316 {
3317 PlaceHolderVar *phv = (PlaceHolderVar *) node;
3318
3319 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
3320 if (context->subplan_itlist->has_ph_vars)
3321 {
3322 newvar = search_indexed_tlist_for_phv(phv,
3323 context->subplan_itlist,
3324 context->newvarno,
3325 context->nrm_match);
3326 if (newvar)
3327 return (Node *) newvar;
3328 }
3329 /* If not supplied by input plan, evaluate the contained expr */
3330 /* XXX can we assert something about phnullingrels? */
3331 return fix_upper_expr_mutator((Node *) phv->phexpr, context);
3332 }
3333 /* Try matching more complex expressions too, if tlist has any */
3334 if (context->subplan_itlist->has_non_vars)
3335 {
3336 newvar = search_indexed_tlist_for_non_var((Expr *) node,
3337 context->subplan_itlist,
3338 context->newvarno);
3339 if (newvar)
3340 return (Node *) newvar;
3341 }
3342 /* Special cases (apply only AFTER failing to match to lower tlist) */
3343 if (IsA(node, Param))
3344 return fix_param_node(context->root, (Param *) node);
3345 if (IsA(node, Aggref))
3346 {
3347 Aggref *aggref = (Aggref *) node;
3348 Param *aggparam;
3349
3350 /* See if the Aggref should be replaced by a Param */
3351 aggparam = find_minmax_agg_replacement_param(context->root, aggref);
3352 if (aggparam != NULL)
3353 {
3354 /* Make a copy of the Param for paranoia's sake */
3355 return (Node *) copyObject(aggparam);
3356 }
3357 /* If no match, just fall through to process it normally */
3358 }
3359 if (IsA(node, AlternativeSubPlan))
3361 (AlternativeSubPlan *) node,
3362 context->num_exec),
3363 context);
3364 fix_expr_common(context->root, node);
3365 return expression_tree_mutator(node, fix_upper_expr_mutator, context);
3366}

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 3471 of file setrefs.c.

3474{
3476
3477 context.root = root;
3478 context.subplan_itlist = subplan_itlist;
3479 context.newvarno = 0;
3480
3481 return (List *) fix_windowagg_condition_expr_mutator((Node *) runcondition,
3482 &context);
3483}
static Node * fix_windowagg_condition_expr_mutator(Node *node, fix_windowagg_cond_context *context)
Definition: setrefs.c:3441
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 3441 of file setrefs.c.

3443{
3444 if (node == NULL)
3445 return NULL;
3446
3447 if (IsA(node, WindowFunc))
3448 {
3449 Var *newvar;
3450
3451 newvar = search_indexed_tlist_for_non_var((Expr *) node,
3452 context->subplan_itlist,
3453 context->newvarno);
3454 if (newvar)
3455 return (Node *) newvar;
3456 elog(ERROR, "WindowFunc not found in subplan target lists");
3457 }
3458
3459 return expression_tree_mutator(node,
3461 context);
3462}

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, 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 1984 of file setrefs.c.

1985{
1986 Relids result = NULL;
1987 int rtindex;
1988
1989 /* If there's no offset to apply, we needn't recompute the value */
1990 if (rtoffset == 0)
1991 return relids;
1992 rtindex = -1;
1993 while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1994 result = bms_add_member(result, rtindex + rtoffset);
1995 return result;
1996}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306

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 3552 of file setrefs.c.

3553{
3554 /*
3555 * For performance reasons, we don't bother to track built-in functions;
3556 * we just assume they'll never change (or at least not in ways that'd
3557 * invalidate plans using them). For this purpose we can consider a
3558 * built-in function to be one with OID less than FirstUnpinnedObjectId.
3559 * Note that the OID generator guarantees never to generate such an OID
3560 * after startup, even at OID wraparound.
3561 */
3562 if (funcid >= (Oid) FirstUnpinnedObjectId)
3563 {
3564 PlanInvalItem *inval_item = makeNode(PlanInvalItem);
3565
3566 /*
3567 * It would work to use any syscache on pg_proc, but the easiest is
3568 * PROCOID since we already have the function's OID at hand. Note
3569 * that plancache.c knows we use PROCOID.
3570 */
3571 inval_item->cacheId = PROCOID;
3572 inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
3573 ObjectIdGetDatum(funcid));
3574
3575 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3576 }
3577}
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
unsigned int Oid
Definition: postgres_ext.h:30
uint32 hashValue
Definition: plannodes.h:1754
#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 3592 of file setrefs.c.

3593{
3594 /*
3595 * As in record_plan_function_dependency, ignore the possibility that
3596 * someone would change a built-in domain.
3597 */
3598 if (typid >= (Oid) FirstUnpinnedObjectId)
3599 {
3600 PlanInvalItem *inval_item = makeNode(PlanInvalItem);
3601
3602 /*
3603 * It would work to use any syscache on pg_type, but the easiest is
3604 * TYPEOID since we already have the type's OID at hand. Note that
3605 * plancache.c knows we use TYPEOID.
3606 */
3607 inval_item->cacheId = TYPEOID;
3608 inval_item->hashValue = GetSysCacheHashValue1(TYPEOID,
3609 ObjectIdGetDatum(typid));
3610
3611 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3612 }
3613}

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 1758 of file setrefs.c.

1759{
1760 PlannerGlobal *glob = root->glob;
1761 PartitionPruneInfo *pinfo;
1762 ListCell *l;
1763
1764 Assert(part_prune_index >= 0 &&
1765 part_prune_index < list_length(root->partPruneInfos));
1766 pinfo = list_nth_node(PartitionPruneInfo, root->partPruneInfos,
1767 part_prune_index);
1768
1769 pinfo->relids = offset_relid_set(pinfo->relids, rtoffset);
1770 foreach(l, pinfo->prune_infos)
1771 {
1772 List *prune_infos = lfirst(l);
1773 ListCell *l2;
1774
1775 foreach(l2, prune_infos)
1776 {
1777 PartitionedRelPruneInfo *prelinfo = lfirst(l2);
1778 int i;
1779
1780 prelinfo->rtindex += rtoffset;
1781 prelinfo->initial_pruning_steps =
1783 rtoffset, 1);
1784 prelinfo->exec_pruning_steps =
1786 rtoffset, 1);
1787
1788 for (i = 0; i < prelinfo->nparts; i++)
1789 {
1790 /*
1791 * Non-leaf partitions and partitions that do not have a
1792 * subplan are not included in this map as mentioned in
1793 * make_partitionedrel_pruneinfo().
1794 */
1795 if (prelinfo->leafpart_rti_map[i])
1796 {
1797 prelinfo->leafpart_rti_map[i] += rtoffset;
1798 if (prelinfo->initial_pruning_steps)
1800 prelinfo->leafpart_rti_map[i]);
1801 }
1802 }
1803 }
1804 }
1805
1806 glob->partPruneInfos = lappend(glob->partPruneInfos, pinfo);
1807
1808 return list_length(glob->partPruneInfos) - 1;
1809}
int i
Definition: isn.c:74
#define list_nth_node(type, list, n)
Definition: pg_list.h:327
static Relids offset_relid_set(Relids relids, int rtoffset)
Definition: setrefs.c:1984
#define fix_scan_list(root, lst, rtoffset, num_exec)
Definition: setrefs.c:130
Bitmapset * relids
Definition: plannodes.h:1597
Bitmapset * prunableRelids
Definition: pathnodes.h:130
List * partPruneInfos
Definition: pathnodes.h:148

References Assert(), bms_add_member(), PartitionedRelPruneInfo::exec_pruning_steps, fix_scan_list, i, PartitionedRelPruneInfo::initial_pruning_steps, lappend(), lfirst, list_length(), list_nth_node, PartitionedRelPruneInfo::nparts, offset_relid_set(), PlannerGlobal::partPruneInfos, PlannerGlobal::prunableRelids, 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 2984 of file setrefs.c.

2986{
2987 TargetEntry *tle;
2988
2989 /*
2990 * If it's a simple Const, replacing it with a Var is silly, even if there
2991 * happens to be an identical Const below; a Var is more expensive to
2992 * execute than a Const. What's more, replacing it could confuse some
2993 * places in the executor that expect to see simple Consts for, eg,
2994 * dropped columns.
2995 */
2996 if (IsA(node, Const))
2997 return NULL;
2998
2999 tle = tlist_member(node, itlist->tlist);
3000 if (tle)
3001 {
3002 /* Found a matching subplan output expression */
3003 Var *newvar;
3004
3005 newvar = makeVarFromTargetEntry(newvarno, tle);
3006 newvar->varnosyn = 0; /* wasn't ever a plain Var */
3007 newvar->varattnosyn = 0;
3008 return newvar;
3009 }
3010 return NULL; /* no match */
3011}
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 2931 of file setrefs.c.

2934{
2935 ListCell *lc;
2936
2937 foreach(lc, itlist->tlist)
2938 {
2939 TargetEntry *tle = (TargetEntry *) lfirst(lc);
2940
2941 if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2942 {
2943 PlaceHolderVar *subphv = (PlaceHolderVar *) tle->expr;
2944 Var *newvar;
2945
2946 /*
2947 * Analogously to search_indexed_tlist_for_var, we match on phid
2948 * only. We don't use equal(), partially for speed but mostly
2949 * because phnullingrels might not be exactly equal.
2950 */
2951 if (phv->phid != subphv->phid)
2952 continue;
2953
2954 /* Verify that we kept all the nullingrels machinations straight */
2955 if (!(nrm_match == NRM_SUBSET ?
2957 nrm_match == NRM_SUPERSET ?
2959 bms_equal(subphv->phnullingrels, phv->phnullingrels)))
2960 elog(ERROR, "wrong phnullingrels %s (expected %s) for PlaceHolderVar %d",
2961 bmsToString(phv->phnullingrels),
2962 bmsToString(subphv->phnullingrels),
2963 phv->phid);
2964
2965 /* Found a matching subplan output expression */
2966 newvar = makeVarFromTargetEntry(newvarno, tle);
2967 newvar->varnosyn = 0; /* wasn't ever a plain Var */
2968 newvar->varattnosyn = 0;
2969 return newvar;
2970 }
2971 }
2972 return NULL; /* no match */
2973}
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:2829

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 3024 of file setrefs.c.

3028{
3029 ListCell *lc;
3030
3031 foreach(lc, itlist->tlist)
3032 {
3033 TargetEntry *tle = (TargetEntry *) lfirst(lc);
3034
3035 /*
3036 * Usually the equal() check is redundant, but in setop plans it may
3037 * not be, since prepunion.c assigns ressortgroupref equal to the
3038 * column resno without regard to whether that matches the topmost
3039 * level's sortgrouprefs and without regard to whether any implicit
3040 * coercions are added in the setop tree. We might have to clean that
3041 * up someday; but for now, just ignore any false matches.
3042 */
3043 if (tle->ressortgroupref == sortgroupref &&
3044 equal(node, tle->expr))
3045 {
3046 /* Found a matching subplan output expression */
3047 Var *newvar;
3048
3049 newvar = makeVarFromTargetEntry(newvarno, tle);
3050 newvar->varnosyn = 0; /* wasn't ever a plain Var */
3051 newvar->varattnosyn = 0;
3052 return newvar;
3053 }
3054 }
3055 return NULL; /* no match */
3056}
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 2866 of file setrefs.c.

2869{
2870 int varno = var->varno;
2871 AttrNumber varattno = var->varattno;
2872 tlist_vinfo *vinfo;
2873 int i;
2874
2875 vinfo = itlist->vars;
2876 i = itlist->num_vars;
2877 while (i-- > 0)
2878 {
2879 if (vinfo->varno == varno && vinfo->varattno == varattno)
2880 {
2881 /* Found a match */
2882 Var *newvar = copyVar(var);
2883
2884 /*
2885 * Verify that we kept all the nullingrels machinations straight.
2886 *
2887 * XXX we skip the check for system columns and whole-row Vars.
2888 * That's because such Vars might be row identity Vars, which are
2889 * generated without any varnullingrels. It'd be hard to do
2890 * otherwise, since they're normally made very early in planning,
2891 * when we haven't looked at the jointree yet and don't know which
2892 * joins might null such Vars. Doesn't seem worth the expense to
2893 * make them fully valid. (While it's slightly annoying that we
2894 * thereby lose checking for user-written references to such
2895 * columns, it seems unlikely that a bug in nullingrels logic
2896 * would affect only system columns.)
2897 */
2898 if (!(varattno <= 0 ||
2899 (nrm_match == NRM_SUBSET ?
2900 bms_is_subset(var->varnullingrels, vinfo->varnullingrels) :
2901 nrm_match == NRM_SUPERSET ?
2902 bms_is_subset(vinfo->varnullingrels, var->varnullingrels) :
2903 bms_equal(vinfo->varnullingrels, var->varnullingrels))))
2904 elog(ERROR, "wrong varnullingrels %s (expected %s) for Var %d/%d",
2905 bmsToString(var->varnullingrels),
2907 varno, varattno);
2908
2909 newvar->varno = newvarno;
2910 newvar->varattno = vinfo->resno;
2911 if (newvar->varnosyn > 0)
2912 newvar->varnosyn += rtoffset;
2913 return newvar;
2914 }
2915 vinfo++;
2916 }
2917 return NULL; /* no match */
2918}

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 1819 of file setrefs.c.

1822{
1823 ListCell *l;
1824
1825 /*
1826 * Append, like Sort et al, doesn't actually evaluate its targetlist or
1827 * check quals. If it's got exactly one child plan, then it's not doing
1828 * anything useful at all, and we can strip it out.
1829 */
1830 Assert(aplan->plan.qual == NIL);
1831
1832 /* First, we gotta recurse on the children */
1833 foreach(l, aplan->appendplans)
1834 {
1835 lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1836 }
1837
1838 /*
1839 * See if it's safe to get rid of the Append entirely. For this to be
1840 * safe, there must be only one child plan and that child plan's parallel
1841 * awareness must match the Append's. The reason for the latter is that
1842 * if the Append is parallel aware and the child is not, then the calling
1843 * plan may execute the non-parallel aware child multiple times. (If you
1844 * change these rules, update create_append_path to match.)
1845 */
1846 if (list_length(aplan->appendplans) == 1)
1847 {
1848 Plan *p = (Plan *) linitial(aplan->appendplans);
1849
1850 if (p->parallel_aware == aplan->plan.parallel_aware)
1851 return clean_up_removed_plan_level((Plan *) aplan, p);
1852 }
1853
1854 /*
1855 * Otherwise, clean up the Append as needed. It's okay to do this after
1856 * recursing to the children, because set_dummy_tlist_references doesn't
1857 * look at those.
1858 */
1859 set_dummy_tlist_references((Plan *) aplan, rtoffset);
1860
1861 aplan->apprelids = offset_relid_set(aplan->apprelids, rtoffset);
1862
1863 /*
1864 * Add PartitionPruneInfo, if any, to PlannerGlobal and update the index.
1865 * Also update the RT indexes present in it to add the offset.
1866 */
1867 if (aplan->part_prune_index >= 0)
1868 aplan->part_prune_index =
1869 register_partpruneinfo(root, aplan->part_prune_index, rtoffset);
1870
1871 /* We don't need to recurse to lefttree or righttree ... */
1872 Assert(aplan->plan.lefttree == NULL);
1873 Assert(aplan->plan.righttree == NULL);
1874
1875 return (Plan *) aplan;
1876}
static void set_dummy_tlist_references(Plan *plan, int rtoffset)
Definition: setrefs.c:2690
static int register_partpruneinfo(PlannerInfo *root, int part_prune_index, int rtoffset)
Definition: setrefs.c:1758
static Plan * clean_up_removed_plan_level(Plan *parent, Plan *child)
Definition: setrefs.c:1544
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:615
int part_prune_index
Definition: plannodes.h:368
Bitmapset * apprelids
Definition: plannodes.h:352
Plan plan
Definition: plannodes.h:350
List * appendplans
Definition: plannodes.h:353
struct Plan * lefttree
Definition: plannodes.h:213
struct Plan * righttree
Definition: plannodes.h:214
bool parallel_aware
Definition: plannodes.h:193
List * qual
Definition: plannodes.h:211

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 1675 of file setrefs.c.

1678{
1679 ListCell *lc;
1680
1681 /* Adjust scanrelid if it's valid */
1682 if (cscan->scan.scanrelid > 0)
1683 cscan->scan.scanrelid += rtoffset;
1684
1685 if (cscan->custom_scan_tlist != NIL || cscan->scan.scanrelid == 0)
1686 {
1687 /* Adjust tlist, qual, custom_exprs to reference custom scan tuple */
1689
1690 cscan->scan.plan.targetlist = (List *)
1692 (Node *) cscan->scan.plan.targetlist,
1693 itlist,
1694 INDEX_VAR,
1695 rtoffset,
1696 NRM_EQUAL,
1697 NUM_EXEC_TLIST((Plan *) cscan));
1698 cscan->scan.plan.qual = (List *)
1700 (Node *) cscan->scan.plan.qual,
1701 itlist,
1702 INDEX_VAR,
1703 rtoffset,
1704 NRM_EQUAL,
1705 NUM_EXEC_QUAL((Plan *) cscan));
1706 cscan->custom_exprs = (List *)
1708 (Node *) cscan->custom_exprs,
1709 itlist,
1710 INDEX_VAR,
1711 rtoffset,
1712 NRM_EQUAL,
1713 NUM_EXEC_QUAL((Plan *) cscan));
1714 pfree(itlist);
1715 /* custom_scan_tlist itself just needs fix_scan_list() adjustments */
1716 cscan->custom_scan_tlist =
1718 rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1719 }
1720 else
1721 {
1722 /* Adjust tlist, qual, custom_exprs in the standard way */
1723 cscan->scan.plan.targetlist =
1724 fix_scan_list(root, cscan->scan.plan.targetlist,
1725 rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1726 cscan->scan.plan.qual =
1727 fix_scan_list(root, cscan->scan.plan.qual,
1728 rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1729 cscan->custom_exprs =
1731 rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1732 }
1733
1734 /* Adjust child plan-nodes recursively, if needed */
1735 foreach(lc, cscan->custom_plans)
1736 {
1737 lfirst(lc) = set_plan_refs(root, (Plan *) lfirst(lc), rtoffset);
1738 }
1739
1740 cscan->custom_relids = offset_relid_set(cscan->custom_relids, rtoffset);
1741}
void pfree(void *pointer)
Definition: mcxt.c:1524
#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:2757
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:3276
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:116
List * custom_scan_tlist
Definition: plannodes.h:883
Scan scan
Definition: plannodes.h:873
Bitmapset * custom_relids
Definition: plannodes.h:885
List * custom_exprs
Definition: plannodes.h:879
List * custom_plans
Definition: plannodes.h:877
Index scanrelid
Definition: plannodes.h:483

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 2690 of file setrefs.c.

2691{
2692 List *output_targetlist;
2693 ListCell *l;
2694
2695 output_targetlist = NIL;
2696 foreach(l, plan->targetlist)
2697 {
2698 TargetEntry *tle = (TargetEntry *) lfirst(l);
2699 Var *oldvar = (Var *) tle->expr;
2700 Var *newvar;
2701
2702 /*
2703 * As in search_indexed_tlist_for_non_var(), we prefer to keep Consts
2704 * as Consts, not Vars referencing Consts. Here, there's no speed
2705 * advantage to be had, but it makes EXPLAIN output look cleaner, and
2706 * again it avoids confusing the executor.
2707 */
2708 if (IsA(oldvar, Const))
2709 {
2710 /* just reuse the existing TLE node */
2711 output_targetlist = lappend(output_targetlist, tle);
2712 continue;
2713 }
2714
2715 newvar = makeVar(OUTER_VAR,
2716 tle->resno,
2717 exprType((Node *) oldvar),
2718 exprTypmod((Node *) oldvar),
2719 exprCollation((Node *) oldvar),
2720 0);
2721 if (IsA(oldvar, Var) &&
2722 oldvar->varnosyn > 0)
2723 {
2724 newvar->varnosyn = oldvar->varnosyn + rtoffset;
2725 newvar->varattnosyn = oldvar->varattnosyn;
2726 }
2727 else
2728 {
2729 newvar->varnosyn = 0; /* wasn't ever a plain Var */
2730 newvar->varattnosyn = 0;
2731 }
2732
2733 tle = flatCopyTargetEntry(tle);
2734 tle->expr = (Expr *) newvar;
2735 output_targetlist = lappend(output_targetlist, tle);
2736 }
2737 plan->targetlist = output_targetlist;
2738
2739 /* We don't touch plan->qual here */
2740}
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:322
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 1588 of file setrefs.c.

1591{
1592 /* Adjust scanrelid if it's valid */
1593 if (fscan->scan.scanrelid > 0)
1594 fscan->scan.scanrelid += rtoffset;
1595
1596 if (fscan->fdw_scan_tlist != NIL || fscan->scan.scanrelid == 0)
1597 {
1598 /*
1599 * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals to reference
1600 * foreign scan tuple
1601 */
1603
1604 fscan->scan.plan.targetlist = (List *)
1606 (Node *) fscan->scan.plan.targetlist,
1607 itlist,
1608 INDEX_VAR,
1609 rtoffset,
1610 NRM_EQUAL,
1611 NUM_EXEC_TLIST((Plan *) fscan));
1612 fscan->scan.plan.qual = (List *)
1614 (Node *) fscan->scan.plan.qual,
1615 itlist,
1616 INDEX_VAR,
1617 rtoffset,
1618 NRM_EQUAL,
1619 NUM_EXEC_QUAL((Plan *) fscan));
1620 fscan->fdw_exprs = (List *)
1622 (Node *) fscan->fdw_exprs,
1623 itlist,
1624 INDEX_VAR,
1625 rtoffset,
1626 NRM_EQUAL,
1627 NUM_EXEC_QUAL((Plan *) fscan));
1628 fscan->fdw_recheck_quals = (List *)
1630 (Node *) fscan->fdw_recheck_quals,
1631 itlist,
1632 INDEX_VAR,
1633 rtoffset,
1634 NRM_EQUAL,
1635 NUM_EXEC_QUAL((Plan *) fscan));
1636 pfree(itlist);
1637 /* fdw_scan_tlist itself just needs fix_scan_list() adjustments */
1638 fscan->fdw_scan_tlist =
1640 rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1641 }
1642 else
1643 {
1644 /*
1645 * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals in the standard
1646 * way
1647 */
1648 fscan->scan.plan.targetlist =
1649 fix_scan_list(root, fscan->scan.plan.targetlist,
1650 rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1651 fscan->scan.plan.qual =
1652 fix_scan_list(root, fscan->scan.plan.qual,
1653 rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1654 fscan->fdw_exprs =
1656 rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1657 fscan->fdw_recheck_quals =
1659 rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1660 }
1661
1662 fscan->fs_relids = offset_relid_set(fscan->fs_relids, rtoffset);
1663 fscan->fs_base_relids = offset_relid_set(fscan->fs_base_relids, rtoffset);
1664
1665 /* Adjust resultRelation if it's valid */
1666 if (fscan->resultRelation > 0)
1667 fscan->resultRelation += rtoffset;
1668}
List * fdw_exprs
Definition: plannodes.h:841
Bitmapset * fs_relids
Definition: plannodes.h:849
Bitmapset * fs_base_relids
Definition: plannodes.h:851
Index resultRelation
Definition: plannodes.h:835
List * fdw_recheck_quals
Definition: plannodes.h:847
List * fdw_scan_tlist
Definition: plannodes.h:845

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 1951 of file setrefs.c.

1952{
1953 Hash *hplan = (Hash *) plan;
1954 Plan *outer_plan = plan->lefttree;
1955 indexed_tlist *outer_itlist;
1956
1957 /*
1958 * Hash's hashkeys are used when feeding tuples into the hashtable,
1959 * therefore have them reference Hash's outer plan (which itself is the
1960 * inner plan of the HashJoin).
1961 */
1962 outer_itlist = build_tlist_index(outer_plan->targetlist);
1963 hplan->hashkeys = (List *)
1965 (Node *) hplan->hashkeys,
1966 outer_itlist,
1967 OUTER_VAR,
1968 rtoffset,
1969 NRM_EQUAL,
1971
1972 /* Hash doesn't project */
1974
1975 /* Hash nodes don't have their own quals */
1976 Assert(plan->qual == NIL);
1977}
List * hashkeys
Definition: plannodes.h:1359

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 1331 of file setrefs.c.

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

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 2330 of file setrefs.c.

2331{
2332 Plan *outer_plan = join->plan.lefttree;
2333 Plan *inner_plan = join->plan.righttree;
2334 indexed_tlist *outer_itlist;
2335 indexed_tlist *inner_itlist;
2336
2337 outer_itlist = build_tlist_index(outer_plan->targetlist);
2338 inner_itlist = build_tlist_index(inner_plan->targetlist);
2339
2340 /*
2341 * First process the joinquals (including merge or hash clauses). These
2342 * are logically below the join so they can always use all values
2343 * available from the input tlists. It's okay to also handle
2344 * NestLoopParams now, because those couldn't refer to nullable
2345 * subexpressions.
2346 */
2347 join->joinqual = fix_join_expr(root,
2348 join->joinqual,
2349 outer_itlist,
2350 inner_itlist,
2351 (Index) 0,
2352 rtoffset,
2353 NRM_EQUAL,
2354 NUM_EXEC_QUAL((Plan *) join));
2355
2356 /* Now do join-type-specific stuff */
2357 if (IsA(join, NestLoop))
2358 {
2359 NestLoop *nl = (NestLoop *) join;
2360 ListCell *lc;
2361
2362 foreach(lc, nl->nestParams)
2363 {
2364 NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
2365
2366 /*
2367 * Because we don't reparameterize parameterized paths to match
2368 * the outer-join level at which they are used, Vars seen in the
2369 * NestLoopParam expression may have nullingrels that are just a
2370 * subset of those in the Vars actually available from the outer
2371 * side. (Lateral references can also cause this, as explained in
2372 * the comments for identify_current_nestloop_params.) Not
2373 * checking this exactly is a bit grotty, but the work needed to
2374 * make things match up perfectly seems well out of proportion to
2375 * the value.
2376 */
2377 nlp->paramval = (Var *) fix_upper_expr(root,
2378 (Node *) nlp->paramval,
2379 outer_itlist,
2380 OUTER_VAR,
2381 rtoffset,
2382 NRM_SUBSET,
2383 NUM_EXEC_TLIST(outer_plan));
2384 /* Check we replaced any PlaceHolderVar with simple Var */
2385 if (!(IsA(nlp->paramval, Var) &&
2386 nlp->paramval->varno == OUTER_VAR))
2387 elog(ERROR, "NestLoopParam was not reduced to a simple Var");
2388 }
2389 }
2390 else if (IsA(join, MergeJoin))
2391 {
2392 MergeJoin *mj = (MergeJoin *) join;
2393
2395 mj->mergeclauses,
2396 outer_itlist,
2397 inner_itlist,
2398 (Index) 0,
2399 rtoffset,
2400 NRM_EQUAL,
2401 NUM_EXEC_QUAL((Plan *) join));
2402 }
2403 else if (IsA(join, HashJoin))
2404 {
2405 HashJoin *hj = (HashJoin *) join;
2406
2408 hj->hashclauses,
2409 outer_itlist,
2410 inner_itlist,
2411 (Index) 0,
2412 rtoffset,
2413 NRM_EQUAL,
2414 NUM_EXEC_QUAL((Plan *) join));
2415
2416 /*
2417 * HashJoin's hashkeys are used to look for matching tuples from its
2418 * outer plan (not the Hash node!) in the hashtable.
2419 */
2420 hj->hashkeys = (List *) fix_upper_expr(root,
2421 (Node *) hj->hashkeys,
2422 outer_itlist,
2423 OUTER_VAR,
2424 rtoffset,
2425 NRM_EQUAL,
2426 NUM_EXEC_QUAL((Plan *) join));
2427 }
2428
2429 /*
2430 * Now we need to fix up the targetlist and qpqual, which are logically
2431 * above the join. This means that, if it's not an inner join, any Vars
2432 * and PHVs appearing here should have nullingrels that include the
2433 * effects of the outer join, ie they will have nullingrels equal to the
2434 * input Vars' nullingrels plus the bit added by the outer join. We don't
2435 * currently have enough info available here to identify what that should
2436 * be, so we just tell fix_join_expr to accept superset nullingrels
2437 * matches instead of exact ones.
2438 */
2439 join->plan.targetlist = fix_join_expr(root,
2440 join->plan.targetlist,
2441 outer_itlist,
2442 inner_itlist,
2443 (Index) 0,
2444 rtoffset,
2446 NUM_EXEC_TLIST((Plan *) join));
2447 join->plan.qual = fix_join_expr(root,
2448 join->plan.qual,
2449 outer_itlist,
2450 inner_itlist,
2451 (Index) 0,
2452 rtoffset,
2454 NUM_EXEC_QUAL((Plan *) join));
2455
2456 pfree(outer_itlist);
2457 pfree(inner_itlist);
2458}
@ JOIN_INNER
Definition: nodes.h:299
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:3102
List * hashclauses
Definition: plannodes.h:1006
List * hashkeys
Definition: plannodes.h:1014
List * joinqual
Definition: plannodes.h:931
JoinType jointype
Definition: plannodes.h:928
List * mergeclauses
Definition: plannodes.h:982
Var * paramval
Definition: plannodes.h:960
List * nestParams
Definition: plannodes.h:949

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 1886 of file setrefs.c.

1889{
1890 ListCell *l;
1891
1892 /*
1893 * MergeAppend, like Sort et al, doesn't actually evaluate its targetlist
1894 * or check quals. If it's got exactly one child plan, then it's not
1895 * doing anything useful at all, and we can strip it out.
1896 */
1897 Assert(mplan->plan.qual == NIL);
1898
1899 /* First, we gotta recurse on the children */
1900 foreach(l, mplan->mergeplans)
1901 {
1902 lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1903 }
1904
1905 /*
1906 * See if it's safe to get rid of the MergeAppend entirely. For this to
1907 * be safe, there must be only one child plan and that child plan's
1908 * parallel awareness must match the MergeAppend's. The reason for the
1909 * latter is that if the MergeAppend is parallel aware and the child is
1910 * not, then the calling plan may execute the non-parallel aware child
1911 * multiple times. (If you change these rules, update
1912 * create_merge_append_path to match.)
1913 */
1914 if (list_length(mplan->mergeplans) == 1)
1915 {
1916 Plan *p = (Plan *) linitial(mplan->mergeplans);
1917
1918 if (p->parallel_aware == mplan->plan.parallel_aware)
1919 return clean_up_removed_plan_level((Plan *) mplan, p);
1920 }
1921
1922 /*
1923 * Otherwise, clean up the MergeAppend as needed. It's okay to do this
1924 * after recursing to the children, because set_dummy_tlist_references
1925 * doesn't look at those.
1926 */
1927 set_dummy_tlist_references((Plan *) mplan, rtoffset);
1928
1929 mplan->apprelids = offset_relid_set(mplan->apprelids, rtoffset);
1930
1931 /*
1932 * Add PartitionPruneInfo, if any, to PlannerGlobal and update the index.
1933 * Also update the RT indexes present in it to add the offset.
1934 */
1935 if (mplan->part_prune_index >= 0)
1936 mplan->part_prune_index =
1937 register_partpruneinfo(root, mplan->part_prune_index, rtoffset);
1938
1939 /* We don't need to recurse to lefttree or righttree ... */
1940 Assert(mplan->plan.lefttree == NULL);
1941 Assert(mplan->plan.righttree == NULL);
1942
1943 return (Plan *) mplan;
1944}
int part_prune_index
Definition: plannodes.h:407
Bitmapset * apprelids
Definition: plannodes.h:381
List * mergeplans
Definition: plannodes.h:383

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 2567 of file setrefs.c.

2568{
2570
2571 if (plan->lefttree->extParam)
2572 {
2573 PlannerInfo *proot;
2574 Bitmapset *initSetParam = NULL;
2575 ListCell *l;
2576
2577 for (proot = root; proot != NULL; proot = proot->parent_root)
2578 {
2579 foreach(l, proot->init_plans)
2580 {
2581 SubPlan *initsubplan = (SubPlan *) lfirst(l);
2582 ListCell *l2;
2583
2584 foreach(l2, initsubplan->setParam)
2585 {
2586 initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
2587 }
2588 }
2589 }
2590
2591 /*
2592 * Remember the list of all external initplan params that are used by
2593 * the children of Gather or Gather merge node.
2594 */
2595 if (IsA(plan, Gather))
2596 ((Gather *) plan)->initParam =
2597 bms_intersect(plan->lefttree->extParam, initSetParam);
2598 else
2599 ((GatherMerge *) plan)->initParam =
2600 bms_intersect(plan->lefttree->extParam, initSetParam);
2601 }
2602}
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
List * init_plans
Definition: pathnodes.h:323
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:3008
List * translated_vars
Definition: pathnodes.h:3035
Index parent_relid
Definition: pathnodes.h:3007
Index prti
Definition: plannodes.h:1542
List * subplans
Definition: pathnodes.h:105
List * appendRelations
Definition: pathnodes.h:145
List * finalrowmarks
Definition: pathnodes.h:136

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 615 of file setrefs.c.

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

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 3397 of file setrefs.c.

3402{
3403 indexed_tlist *itlist;
3404
3405 /*
3406 * We can perform the desired Var fixup by abusing the fix_join_expr
3407 * machinery that formerly handled inner indexscan fixup. We search the
3408 * top plan's targetlist for Vars of non-result relations, and use
3409 * fix_join_expr to convert RETURNING Vars into references to those tlist
3410 * entries, while leaving result-rel Vars as-is.
3411 *
3412 * PlaceHolderVars will also be sought in the targetlist, but no
3413 * more-complex expressions will be. Note that it is not possible for a
3414 * PlaceHolderVar to refer to the result relation, since the result is
3415 * never below an outer join. If that case could happen, we'd have to be
3416 * prepared to pick apart the PlaceHolderVar and evaluate its contained
3417 * expression instead.
3418 */
3419 itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
3420
3421 rlist = fix_join_expr(root,
3422 rlist,
3423 itlist,
3424 NULL,
3425 resultRelation,
3426 rtoffset,
3427 NRM_EQUAL,
3428 NUM_EXEC_TLIST(topplan));
3429
3430 pfree(itlist);
3431
3432 return rlist;
3433}
static indexed_tlist * build_tlist_index_other_vars(List *tlist, int ignore_rel)
Definition: setrefs.c:2808

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 1405 of file setrefs.c.

1408{
1409 RelOptInfo *rel;
1410 Plan *result;
1411
1412 /* Need to look up the subquery's RelOptInfo, since we need its subroot */
1413 rel = find_base_rel(root, plan->scan.scanrelid);
1414
1415 /* Recursively process the subplan */
1416 plan->subplan = set_plan_references(rel->subroot, plan->subplan);
1417
1419 {
1420 /*
1421 * We can omit the SubqueryScan node and just pull up the subplan.
1422 */
1423 result = clean_up_removed_plan_level((Plan *) plan, plan->subplan);
1424 }
1425 else
1426 {
1427 /*
1428 * Keep the SubqueryScan node. We have to do the processing that
1429 * set_plan_references would otherwise have done on it. Notice we do
1430 * not do set_upper_references() here, because a SubqueryScan will
1431 * always have been created with correct references to its subplan's
1432 * outputs to begin with.
1433 */
1434 plan->scan.scanrelid += rtoffset;
1435 plan->scan.plan.targetlist =
1436 fix_scan_list(root, plan->scan.plan.targetlist,
1437 rtoffset, NUM_EXEC_TLIST((Plan *) plan));
1438 plan->scan.plan.qual =
1439 fix_scan_list(root, plan->scan.plan.qual,
1440 rtoffset, NUM_EXEC_QUAL((Plan *) plan));
1441
1442 result = (Plan *) plan;
1443 }
1444
1445 return result;
1446}
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:1474

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 2479 of file setrefs.c.

2480{
2481 Plan *subplan = plan->lefttree;
2482 indexed_tlist *subplan_itlist;
2483 List *output_targetlist;
2484 ListCell *l;
2485
2486 subplan_itlist = build_tlist_index(subplan->targetlist);
2487
2488 /*
2489 * If it's a grouping node with grouping sets, any Vars and PHVs appearing
2490 * in the targetlist and quals should have nullingrels that include the
2491 * effects of the grouping step, ie they will have nullingrels equal to
2492 * the input Vars/PHVs' nullingrels plus the RT index of the grouping
2493 * step. In order to perform exact nullingrels matches, we remove the RT
2494 * index of the grouping step first.
2495 */
2496 if (IsA(plan, Agg) &&
2497 root->group_rtindex > 0 &&
2498 ((Agg *) plan)->groupingSets)
2499 {
2500 plan->targetlist = (List *)
2501 remove_nulling_relids((Node *) plan->targetlist,
2502 bms_make_singleton(root->group_rtindex),
2503 NULL);
2504 plan->qual = (List *)
2506 bms_make_singleton(root->group_rtindex),
2507 NULL);
2508 }
2509
2510 output_targetlist = NIL;
2511 foreach(l, plan->targetlist)
2512 {
2513 TargetEntry *tle = (TargetEntry *) lfirst(l);
2514 Node *newexpr;
2515
2516 /* If it's a sort/group item, first try to match by sortref */
2517 if (tle->ressortgroupref != 0)
2518 {
2519 newexpr = (Node *)
2521 tle->ressortgroupref,
2522 subplan_itlist,
2523 OUTER_VAR);
2524 if (!newexpr)
2525 newexpr = fix_upper_expr(root,
2526 (Node *) tle->expr,
2527 subplan_itlist,
2528 OUTER_VAR,
2529 rtoffset,
2530 NRM_EQUAL,
2532 }
2533 else
2534 newexpr = fix_upper_expr(root,
2535 (Node *) tle->expr,
2536 subplan_itlist,
2537 OUTER_VAR,
2538 rtoffset,
2539 NRM_EQUAL,
2541 tle = flatCopyTargetEntry(tle);
2542 tle->expr = (Expr *) newexpr;
2543 output_targetlist = lappend(output_targetlist, tle);
2544 }
2545 plan->targetlist = output_targetlist;
2546
2547 plan->qual = (List *)
2549 (Node *) plan->qual,
2550 subplan_itlist,
2551 OUTER_VAR,
2552 rtoffset,
2553 NRM_EQUAL,
2555
2556 pfree(subplan_itlist);
2557}
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:3024

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 3492 of file setrefs.c.

3495{
3496 List *newlist;
3497 indexed_tlist *itlist;
3498
3499 itlist = build_tlist_index(plan->targetlist);
3500
3501 newlist = fix_windowagg_condition_expr(root, runcondition, itlist);
3502
3503 pfree(itlist);
3504
3505 return newlist;
3506}
static List * fix_windowagg_condition_expr(PlannerInfo *root, List *runcondition, indexed_tlist *subplan_itlist)
Definition: setrefs.c:3471

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 1474 of file setrefs.c.

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

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().