1 /*-------------------------------------------------------------------------
4 * Routines to plan set-operation queries. The filename is a leftover
5 * from a time when only UNIONs were implemented.
7 * There are two code paths in the planner for set-operation queries.
8 * If a subquery consists entirely of simple UNION ALL operations, it
9 * is converted into an "append relation". Otherwise, it is handled
10 * by the general code in this module (plan_set_operations and its
11 * subroutines). There is some support code here for the append-relation
12 * case, but most of the heavy lifting for that is done elsewhere,
13 * notably in prepjointree.c and allpaths.c.
15 * There is also some code here to support planning of queries that use
16 * inheritance (SELECT FROM foo*). Inheritance trees are converted into
17 * append relations, and thenceforth share code with the UNION ALL case.
20 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
21 * Portions Copyright (c) 1994, Regents of the University of California
25 * $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.138 2007/02/19 07:03:30 tgl Exp $
27 *-------------------------------------------------------------------------
32 #include "access/heapam.h"
33 #include "catalog/namespace.h"
34 #include "catalog/pg_type.h"
35 #include "nodes/makefuncs.h"
36 #include "optimizer/clauses.h"
37 #include "optimizer/plancat.h"
38 #include "optimizer/planmain.h"
39 #include "optimizer/planner.h"
40 #include "optimizer/prep.h"
41 #include "optimizer/tlist.h"
42 #include "parser/parse_clause.h"
43 #include "parser/parse_coerce.h"
44 #include "parser/parse_expr.h"
45 #include "parser/parsetree.h"
46 #include "utils/lsyscache.h"
49 static Plan *recurse_set_operations(Node *setOp, PlannerInfo *root,
50 double tuple_fraction,
51 List *colTypes, bool junkOK,
52 int flag, List *refnames_tlist,
54 static Plan *generate_union_plan(SetOperationStmt *op, PlannerInfo *root,
55 double tuple_fraction,
56 List *refnames_tlist, List **sortClauses);
57 static Plan *generate_nonunion_plan(SetOperationStmt *op, PlannerInfo *root,
58 List *refnames_tlist, List **sortClauses);
59 static List *recurse_union_children(Node *setOp, PlannerInfo *root,
60 double tuple_fraction,
61 SetOperationStmt *top_union,
62 List *refnames_tlist);
63 static List *generate_setop_tlist(List *colTypes, int flag,
67 List *refnames_tlist);
68 static List *generate_append_tlist(List *colTypes, bool flag,
70 List *refnames_tlist);
71 static void expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte,
73 static void make_inh_translation_lists(Relation oldrelation,
77 List **translated_vars);
78 static Node *adjust_appendrel_attrs_mutator(Node *node,
79 AppendRelInfo *context);
80 static Relids adjust_relid_set(Relids relids, Index oldrelid, Index newrelid);
81 static List *adjust_inherited_tlist(List *tlist,
82 AppendRelInfo *context);
88 * Plans the queries for a tree of set operations (UNION/INTERSECT/EXCEPT)
90 * This routine only deals with the setOperations tree of the given query.
91 * Any top-level ORDER BY requested in root->parse->sortClause will be added
92 * when we return to grouping_planner.
94 * tuple_fraction is the fraction of tuples we expect will be retrieved.
95 * tuple_fraction is interpreted as for grouping_planner(); in particular,
96 * zero means "all the tuples will be fetched". Any LIMIT present at the
97 * top level has already been factored into tuple_fraction.
99 * *sortClauses is an output argument: it is set to a list of SortClauses
100 * representing the result ordering of the topmost set operation.
103 plan_set_operations(PlannerInfo *root, double tuple_fraction,
106 Query *parse = root->parse;
107 SetOperationStmt *topop = (SetOperationStmt *) parse->setOperations;
109 Query *leftmostQuery;
111 Assert(topop && IsA(topop, SetOperationStmt));
113 /* check for unsupported stuff */
114 Assert(parse->utilityStmt == NULL);
115 Assert(parse->jointree->fromlist == NIL);
116 Assert(parse->jointree->quals == NULL);
117 Assert(parse->groupClause == NIL);
118 Assert(parse->havingQual == NULL);
119 Assert(parse->distinctClause == NIL);
122 * Find the leftmost component Query. We need to use its column names for
123 * all generated tlists (else SELECT INTO won't work right).
126 while (node && IsA(node, SetOperationStmt))
127 node = ((SetOperationStmt *) node)->larg;
128 Assert(node && IsA(node, RangeTblRef));
129 leftmostQuery = rt_fetch(((RangeTblRef *) node)->rtindex,
130 parse->rtable)->subquery;
131 Assert(leftmostQuery != NULL);
134 * Recurse on setOperations tree to generate plans for set ops. The final
135 * output plan should have just the column types shown as the output from
136 * the top-level node, plus possibly resjunk working columns (we can rely
137 * on upper-level nodes to deal with that).
139 return recurse_set_operations((Node *) topop, root, tuple_fraction,
140 topop->colTypes, true, -1,
141 leftmostQuery->targetList,
146 * recurse_set_operations
147 * Recursively handle one step in a tree of set operations
149 * tuple_fraction: fraction of tuples we expect to retrieve from node
150 * colTypes: list of type OIDs of expected output columns
151 * junkOK: if true, child resjunk columns may be left in the result
152 * flag: if >= 0, add a resjunk output column indicating value of flag
153 * refnames_tlist: targetlist to take column names from
154 * *sortClauses: receives list of SortClauses for result plan, if any
156 * We don't have to care about typmods here: the only allowed difference
157 * between set-op input and output typmods is input is a specific typmod
158 * and output is -1, and that does not require a coercion.
161 recurse_set_operations(Node *setOp, PlannerInfo *root,
162 double tuple_fraction,
163 List *colTypes, bool junkOK,
164 int flag, List *refnames_tlist,
167 if (IsA(setOp, RangeTblRef))
169 RangeTblRef *rtr = (RangeTblRef *) setOp;
170 RangeTblEntry *rte = rt_fetch(rtr->rtindex, root->parse->rtable);
171 Query *subquery = rte->subquery;
175 Assert(subquery != NULL);
178 * Generate plan for primitive subquery
180 subplan = subquery_planner(root->glob, subquery,
181 root->query_level + 1,
186 * Add a SubqueryScan with the caller-requested targetlist
189 make_subqueryscan(generate_setop_tlist(colTypes, flag,
199 * We don't bother to determine the subquery's output ordering since
200 * it won't be reflected in the set-op result anyhow.
206 else if (IsA(setOp, SetOperationStmt))
208 SetOperationStmt *op = (SetOperationStmt *) setOp;
211 /* UNIONs are much different from INTERSECT/EXCEPT */
212 if (op->op == SETOP_UNION)
213 plan = generate_union_plan(op, root, tuple_fraction,
217 plan = generate_nonunion_plan(op, root,
222 * If necessary, add a Result node to project the caller-requested
225 * XXX you don't really want to know about this: setrefs.c will apply
226 * replace_vars_with_subplan_refs() to the Result node's tlist. This
227 * would fail if the Vars generated by generate_setop_tlist() were not
228 * exactly equal() to the corresponding tlist entries of the subplan.
229 * However, since the subplan was generated by generate_union_plan()
230 * or generate_nonunion_plan(), and hence its tlist was generated by
231 * generate_append_tlist(), this will work. We just tell
232 * generate_setop_tlist() to use varno 0.
235 !tlist_same_datatypes(plan->targetlist, colTypes, junkOK))
238 make_result(generate_setop_tlist(colTypes, flag,
250 elog(ERROR, "unrecognized node type: %d",
251 (int) nodeTag(setOp));
252 return NULL; /* keep compiler quiet */
257 * Generate plan for a UNION or UNION ALL node
260 generate_union_plan(SetOperationStmt *op, PlannerInfo *root,
261 double tuple_fraction,
262 List *refnames_tlist,
270 * If plain UNION, tell children to fetch all tuples.
272 * Note: in UNION ALL, we pass the top-level tuple_fraction unmodified to
273 * each arm of the UNION ALL. One could make a case for reducing the
274 * tuple fraction for later arms (discounting by the expected size of the
275 * earlier arms' results) but it seems not worth the trouble. The normal
276 * case where tuple_fraction isn't already zero is a LIMIT at top level,
277 * and passing it down as-is is usually enough to get the desired result
278 * of preferring fast-start plans.
281 tuple_fraction = 0.0;
284 * If any of my children are identical UNION nodes (same op, all-flag, and
285 * colTypes) then they can be merged into this node so that we generate
286 * only one Append and Sort for the lot. Recurse to find such nodes and
287 * compute their children's plans.
289 planlist = list_concat(recurse_union_children(op->larg, root,
292 recurse_union_children(op->rarg, root,
294 op, refnames_tlist));
297 * Generate tlist for Append plan node.
299 * The tlist for an Append plan isn't important as far as the Append is
300 * concerned, but we must make it look real anyway for the benefit of the
301 * next plan level up.
303 tlist = generate_append_tlist(op->colTypes, false,
304 planlist, refnames_tlist);
307 * Append the child results together.
309 plan = (Plan *) make_append(planlist, false, tlist);
312 * For UNION ALL, we just need the Append plan. For UNION, need to add
313 * Sort and Unique nodes to produce unique output.
319 sortList = addAllTargetsToSortList(NULL, NIL, tlist, false);
322 plan = (Plan *) make_sort_from_sortclauses(root, sortList, plan);
323 plan = (Plan *) make_unique(plan, sortList);
325 *sortClauses = sortList;
334 * Generate plan for an INTERSECT, INTERSECT ALL, EXCEPT, or EXCEPT ALL node
337 generate_nonunion_plan(SetOperationStmt *op, PlannerInfo *root,
338 List *refnames_tlist,
350 /* Recurse on children, ensuring their outputs are marked */
351 lplan = recurse_set_operations(op->larg, root,
352 0.0 /* all tuples needed */ ,
353 op->colTypes, false, 0,
356 rplan = recurse_set_operations(op->rarg, root,
357 0.0 /* all tuples needed */ ,
358 op->colTypes, false, 1,
361 planlist = list_make2(lplan, rplan);
364 * Generate tlist for Append plan node.
366 * The tlist for an Append plan isn't important as far as the Append is
367 * concerned, but we must make it look real anyway for the benefit of the
368 * next plan level up. In fact, it has to be real enough that the flag
369 * column is shown as a variable not a constant, else setrefs.c will get
372 tlist = generate_append_tlist(op->colTypes, true,
373 planlist, refnames_tlist);
376 * Append the child results together.
378 plan = (Plan *) make_append(planlist, false, tlist);
381 * Sort the child results, then add a SetOp plan node to generate the
384 sortList = addAllTargetsToSortList(NULL, NIL, tlist, false);
386 if (sortList == NIL) /* nothing to sort on? */
392 plan = (Plan *) make_sort_from_sortclauses(root, sortList, plan);
395 case SETOP_INTERSECT:
396 cmd = op->all ? SETOPCMD_INTERSECT_ALL : SETOPCMD_INTERSECT;
399 cmd = op->all ? SETOPCMD_EXCEPT_ALL : SETOPCMD_EXCEPT;
402 elog(ERROR, "unrecognized set op: %d",
404 cmd = SETOPCMD_INTERSECT; /* keep compiler quiet */
407 plan = (Plan *) make_setop(cmd, plan, sortList, list_length(op->colTypes) + 1);
409 *sortClauses = sortList;
415 * Pull up children of a UNION node that are identically-propertied UNIONs.
417 * NOTE: we can also pull a UNION ALL up into a UNION, since the distinct
418 * output rows will be lost anyway.
421 recurse_union_children(Node *setOp, PlannerInfo *root,
422 double tuple_fraction,
423 SetOperationStmt *top_union,
424 List *refnames_tlist)
426 List *child_sortclauses;
428 if (IsA(setOp, SetOperationStmt))
430 SetOperationStmt *op = (SetOperationStmt *) setOp;
432 if (op->op == top_union->op &&
433 (op->all == top_union->all || op->all) &&
434 equal(op->colTypes, top_union->colTypes))
436 /* Same UNION, so fold children into parent's subplan list */
437 return list_concat(recurse_union_children(op->larg, root,
441 recurse_union_children(op->rarg, root,
449 * Not same, so plan this child separately.
451 * Note we disallow any resjunk columns in child results. This is
452 * necessary since the Append node that implements the union won't do any
453 * projection, and upper levels will get confused if some of our output
454 * tuples have junk and some don't. This case only arises when we have an
455 * EXCEPT or INTERSECT as child, else there won't be resjunk anyway.
457 return list_make1(recurse_set_operations(setOp, root,
459 top_union->colTypes, false,
461 &child_sortclauses));
465 * Generate targetlist for a set-operation plan node
467 * colTypes: column datatypes for non-junk columns
468 * flag: -1 if no flag column needed, 0 or 1 to create a const flag column
469 * varno: varno to use in generated Vars
470 * hack_constants: true to copy up constants (see comments in code)
471 * input_tlist: targetlist of this node's input node
472 * refnames_tlist: targetlist to take column names from
475 generate_setop_tlist(List *colTypes, int flag,
479 List *refnames_tlist)
489 j = list_head(input_tlist);
490 k = list_head(refnames_tlist);
493 Oid colType = lfirst_oid(i);
494 TargetEntry *inputtle = (TargetEntry *) lfirst(j);
495 TargetEntry *reftle = (TargetEntry *) lfirst(k);
497 Assert(inputtle->resno == resno);
498 Assert(reftle->resno == resno);
499 Assert(!inputtle->resjunk);
500 Assert(!reftle->resjunk);
503 * Generate columns referencing input columns and having appropriate
504 * data types and column names. Insert datatype coercions where
507 * HACK: constants in the input's targetlist are copied up as-is
508 * rather than being referenced as subquery outputs. This is mainly
509 * to ensure that when we try to coerce them to the output column's
510 * datatype, the right things happen for UNKNOWN constants. But do
511 * this only at the first level of subquery-scan plans; we don't want
512 * phony constants appearing in the output tlists of upper-level
515 if (hack_constants && inputtle->expr && IsA(inputtle->expr, Const))
516 expr = (Node *) inputtle->expr;
518 expr = (Node *) makeVar(varno,
520 exprType((Node *) inputtle->expr),
521 exprTypmod((Node *) inputtle->expr),
523 if (exprType(expr) != colType)
525 expr = coerce_to_common_type(NULL, /* no UNKNOWNs here */
528 "UNION/INTERSECT/EXCEPT");
530 tle = makeTargetEntry((Expr *) expr,
531 (AttrNumber) resno++,
532 pstrdup(reftle->resname),
534 tlist = lappend(tlist, tle);
542 /* Add a resjunk flag column */
543 /* flag value is the given constant */
544 expr = (Node *) makeConst(INT4OID,
549 tle = makeTargetEntry((Expr *) expr,
550 (AttrNumber) resno++,
553 tlist = lappend(tlist, tle);
560 * Generate targetlist for a set-operation Append node
562 * colTypes: column datatypes for non-junk columns
563 * flag: true to create a flag column copied up from subplans
564 * input_plans: list of sub-plans of the Append
565 * refnames_tlist: targetlist to take column names from
567 * The entries in the Append's targetlist should always be simple Vars;
568 * we just have to make sure they have the right datatypes and typmods.
569 * The Vars are always generated with varno 0.
572 generate_append_tlist(List *colTypes, bool flag,
574 List *refnames_tlist)
578 ListCell *curColType;
579 ListCell *ref_tl_item;
587 * First extract typmods to use.
589 * If the inputs all agree on type and typmod of a particular column, use
590 * that typmod; else use -1.
592 colTypmods = (int32 *) palloc(list_length(colTypes) * sizeof(int32));
594 foreach(planl, input_plans)
596 Plan *subplan = (Plan *) lfirst(planl);
599 curColType = list_head(colTypes);
601 foreach(subtlist, subplan->targetlist)
603 TargetEntry *subtle = (TargetEntry *) lfirst(subtlist);
607 Assert(curColType != NULL);
608 if (exprType((Node *) subtle->expr) == lfirst_oid(curColType))
610 /* If first subplan, copy the typmod; else compare */
611 int32 subtypmod = exprTypmod((Node *) subtle->expr);
613 if (planl == list_head(input_plans))
614 colTypmods[colindex] = subtypmod;
615 else if (subtypmod != colTypmods[colindex])
616 colTypmods[colindex] = -1;
620 /* types disagree, so force typmod to -1 */
621 colTypmods[colindex] = -1;
623 curColType = lnext(curColType);
626 Assert(curColType == NULL);
630 * Now we can build the tlist for the Append.
633 forboth(curColType, colTypes, ref_tl_item, refnames_tlist)
635 Oid colType = lfirst_oid(curColType);
636 int32 colTypmod = colTypmods[colindex++];
637 TargetEntry *reftle = (TargetEntry *) lfirst(ref_tl_item);
639 Assert(reftle->resno == resno);
640 Assert(!reftle->resjunk);
641 expr = (Node *) makeVar(0,
646 tle = makeTargetEntry((Expr *) expr,
647 (AttrNumber) resno++,
648 pstrdup(reftle->resname),
650 tlist = lappend(tlist, tle);
655 /* Add a resjunk flag column */
656 /* flag value is shown as copied up from subplan */
657 expr = (Node *) makeVar(0,
662 tle = makeTargetEntry((Expr *) expr,
663 (AttrNumber) resno++,
666 tlist = lappend(tlist, tle);
676 * find_all_inheritors -
677 * Returns a list of relation OIDs including the given rel plus
678 * all relations that inherit from it, directly or indirectly.
681 find_all_inheritors(Oid parentrel)
687 * We build a list starting with the given rel and adding all direct and
688 * indirect children. We can use a single list as both the record of
689 * already-found rels and the agenda of rels yet to be scanned for more
690 * children. This is a bit tricky but works because the foreach() macro
691 * doesn't fetch the next list element until the bottom of the loop.
693 rels_list = list_make1_oid(parentrel);
695 foreach(l, rels_list)
697 Oid currentrel = lfirst_oid(l);
698 List *currentchildren;
700 /* Get the direct children of this rel */
701 currentchildren = find_inheritance_children(currentrel);
704 * Add to the queue only those children not already seen. This avoids
705 * making duplicate entries in case of multiple inheritance paths from
706 * the same parent. (It'll also keep us from getting into an infinite
707 * loop, though theoretically there can't be any cycles in the
708 * inheritance graph anyway.)
710 rels_list = list_concat_unique_oid(rels_list, currentchildren);
717 * expand_inherited_tables
718 * Expand each rangetable entry that represents an inheritance set
719 * into an "append relation". At the conclusion of this process,
720 * the "inh" flag is set in all and only those RTEs that are append
724 expand_inherited_tables(PlannerInfo *root)
731 * expand_inherited_rtentry may add RTEs to parse->rtable; there is no
732 * need to scan them since they can't have inh=true. So just scan as far
733 * as the original end of the rtable list.
735 nrtes = list_length(root->parse->rtable);
736 rl = list_head(root->parse->rtable);
737 for (rti = 1; rti <= nrtes; rti++)
739 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rl);
741 expand_inherited_rtentry(root, rte, rti);
747 * expand_inherited_rtentry
748 * Check whether a rangetable entry represents an inheritance set.
749 * If so, add entries for all the child tables to the query's
750 * rangetable, and build AppendRelInfo nodes for all the child tables
751 * and add them to root->append_rel_list. If not, clear the entry's
752 * "inh" flag to prevent later code from looking for AppendRelInfos.
754 * Note that the original RTE is considered to represent the whole
755 * inheritance set. The first of the generated RTEs is an RTE for the same
756 * table, but with inh = false, to represent the parent table in its role
757 * as a simple member of the inheritance set.
759 * A childless table is never considered to be an inheritance set; therefore
760 * a parent RTE must always have at least two associated AppendRelInfos.
763 expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
765 Query *parse = root->parse;
767 Relation oldrelation;
773 /* Does RT entry allow inheritance? */
776 /* Ignore any already-expanded UNION ALL nodes */
777 if (rte->rtekind != RTE_RELATION)
779 Assert(rte->rtekind == RTE_SUBQUERY);
782 /* Fast path for common case of childless table */
783 parentOID = rte->relid;
784 if (!has_subclass(parentOID))
786 /* Clear flag before returning */
791 /* Scan for all members of inheritance set */
792 inhOIDs = find_all_inheritors(parentOID);
795 * Check that there's at least one descendant, else treat as no-child
796 * case. This could happen despite above has_subclass() check, if table
797 * once had a child but no longer does.
799 if (list_length(inhOIDs) < 2)
801 /* Clear flag before returning */
807 * Must open the parent relation to examine its tupdesc. We need not lock
808 * it since the rewriter already obtained at least AccessShareLock on each
809 * relation used in the query.
811 oldrelation = heap_open(parentOID, NoLock);
814 * However, for each child relation we add to the query, we must obtain an
815 * appropriate lock, because this will be the first use of those relations
816 * in the parse/rewrite/plan pipeline.
818 * If the parent relation is the query's result relation, then we need
819 * RowExclusiveLock. Otherwise, check to see if the relation is accessed
820 * FOR UPDATE/SHARE or not. We can't just grab AccessShareLock because
821 * then the executor would be trying to upgrade the lock, leading to
822 * possible deadlocks. (This code should match the parser and rewriter.)
824 if (rti == parse->resultRelation)
825 lockmode = RowExclusiveLock;
826 else if (get_rowmark(parse, rti))
827 lockmode = RowShareLock;
829 lockmode = AccessShareLock;
831 /* Scan the inheritance set and expand it */
835 Oid childOID = lfirst_oid(l);
836 Relation newrelation;
837 RangeTblEntry *childrte;
839 AppendRelInfo *appinfo;
842 * It is possible that the parent table has children that are temp
843 * tables of other backends. We cannot safely access such tables
844 * (because of buffering issues), and the best thing to do seems to be
845 * to silently ignore them.
847 if (childOID != parentOID &&
848 isOtherTempNamespace(get_rel_namespace(childOID)))
851 /* Open rel, acquire the appropriate lock type */
852 if (childOID != parentOID)
853 newrelation = heap_open(childOID, lockmode);
855 newrelation = oldrelation;
858 * Build an RTE for the child, and attach to query's rangetable list.
859 * We copy most fields of the parent's RTE, but replace relation OID,
860 * and set inh = false.
862 childrte = copyObject(rte);
863 childrte->relid = childOID;
864 childrte->inh = false;
865 parse->rtable = lappend(parse->rtable, childrte);
866 childRTindex = list_length(parse->rtable);
869 * Build an AppendRelInfo for this parent and child.
871 appinfo = makeNode(AppendRelInfo);
872 appinfo->parent_relid = rti;
873 appinfo->child_relid = childRTindex;
874 appinfo->parent_reltype = oldrelation->rd_rel->reltype;
875 appinfo->child_reltype = newrelation->rd_rel->reltype;
876 make_inh_translation_lists(oldrelation, newrelation, childRTindex,
877 &appinfo->col_mappings,
878 &appinfo->translated_vars);
879 appinfo->parent_reloid = parentOID;
880 appinfos = lappend(appinfos, appinfo);
882 /* Close child relations, but keep locks */
883 if (childOID != parentOID)
884 heap_close(newrelation, NoLock);
887 heap_close(oldrelation, NoLock);
890 * If all the children were temp tables, pretend it's a non-inheritance
891 * situation. The duplicate RTE we added for the parent table is
892 * harmless, so we don't bother to get rid of it.
894 if (list_length(appinfos) < 2)
896 /* Clear flag before returning */
901 /* Otherwise, OK to add to root->append_rel_list */
902 root->append_rel_list = list_concat(root->append_rel_list, appinfos);
905 * The executor will check the parent table's access permissions when it
906 * examines the parent's added RTE entry. There's no need to check twice,
907 * so turn off access check bits in the original RTE.
909 rte->requiredPerms = 0;
913 * make_inh_translation_lists
914 * Build the lists of translations from parent Vars to child Vars for
915 * an inheritance child. We need both a column number mapping list
916 * and a list of Vars representing the child columns.
918 * For paranoia's sake, we match type as well as attribute name.
921 make_inh_translation_lists(Relation oldrelation, Relation newrelation,
923 List **col_mappings, List **translated_vars)
927 TupleDesc old_tupdesc = RelationGetDescr(oldrelation);
928 TupleDesc new_tupdesc = RelationGetDescr(newrelation);
929 int oldnatts = old_tupdesc->natts;
930 int newnatts = new_tupdesc->natts;
933 for (old_attno = 0; old_attno < oldnatts; old_attno++)
935 Form_pg_attribute att;
941 att = old_tupdesc->attrs[old_attno];
942 if (att->attisdropped)
944 /* Just put 0/NULL into this list entry */
945 numbers = lappend_int(numbers, 0);
946 vars = lappend(vars, NULL);
949 attname = NameStr(att->attname);
950 atttypid = att->atttypid;
951 atttypmod = att->atttypmod;
954 * When we are generating the "translation list" for the parent table
955 * of an inheritance set, no need to search for matches.
957 if (oldrelation == newrelation)
959 numbers = lappend_int(numbers, old_attno + 1);
960 vars = lappend(vars, makeVar(newvarno,
961 (AttrNumber) (old_attno + 1),
969 * Otherwise we have to search for the matching column by name.
970 * There's no guarantee it'll have the same column position, because
971 * of cases like ALTER TABLE ADD COLUMN and multiple inheritance.
973 for (new_attno = 0; new_attno < newnatts; new_attno++)
975 att = new_tupdesc->attrs[new_attno];
976 if (att->attisdropped || att->attinhcount == 0)
978 if (strcmp(attname, NameStr(att->attname)) != 0)
980 /* Found it, check type */
981 if (atttypid != att->atttypid || atttypmod != att->atttypmod)
982 elog(ERROR, "attribute \"%s\" of relation \"%s\" does not match parent's type",
983 attname, RelationGetRelationName(newrelation));
985 numbers = lappend_int(numbers, new_attno + 1);
986 vars = lappend(vars, makeVar(newvarno,
987 (AttrNumber) (new_attno + 1),
994 if (new_attno >= newnatts)
995 elog(ERROR, "could not find inherited attribute \"%s\" of relation \"%s\"",
996 attname, RelationGetRelationName(newrelation));
999 *col_mappings = numbers;
1000 *translated_vars = vars;
1004 * adjust_appendrel_attrs
1005 * Copy the specified query or expression and translate Vars referring
1006 * to the parent rel of the specified AppendRelInfo to refer to the
1007 * child rel instead. We also update rtindexes appearing outside Vars,
1008 * such as resultRelation and jointree relids.
1010 * Note: this is only applied after conversion of sublinks to subplans,
1011 * so we don't need to cope with recursion into sub-queries.
1013 * Note: this is not hugely different from what ResolveNew() does; maybe
1014 * we should try to fold the two routines together.
1017 adjust_appendrel_attrs(Node *node, AppendRelInfo *appinfo)
1022 * Must be prepared to start with a Query or a bare expression tree.
1024 if (node && IsA(node, Query))
1028 newnode = query_tree_mutator((Query *) node,
1029 adjust_appendrel_attrs_mutator,
1031 QTW_IGNORE_RT_SUBQUERIES);
1032 if (newnode->resultRelation == appinfo->parent_relid)
1034 newnode->resultRelation = appinfo->child_relid;
1035 /* Fix tlist resnos too, if it's inherited UPDATE */
1036 if (newnode->commandType == CMD_UPDATE)
1037 newnode->targetList =
1038 adjust_inherited_tlist(newnode->targetList,
1041 result = (Node *) newnode;
1044 result = adjust_appendrel_attrs_mutator(node, appinfo);
1050 adjust_appendrel_attrs_mutator(Node *node, AppendRelInfo *context)
1056 Var *var = (Var *) copyObject(node);
1058 if (var->varlevelsup == 0 &&
1059 var->varno == context->parent_relid)
1061 var->varno = context->child_relid;
1062 var->varnoold = context->child_relid;
1063 if (var->varattno > 0)
1067 if (var->varattno > list_length(context->translated_vars))
1068 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
1069 var->varattno, get_rel_name(context->parent_reloid));
1070 newnode = copyObject(list_nth(context->translated_vars,
1071 var->varattno - 1));
1072 if (newnode == NULL)
1073 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
1074 var->varattno, get_rel_name(context->parent_reloid));
1077 else if (var->varattno == 0)
1080 * Whole-row Var: if we are dealing with named rowtypes, we
1081 * can use a whole-row Var for the child table plus a coercion
1082 * step to convert the tuple layout to the parent's rowtype.
1083 * Otherwise we have to generate a RowExpr.
1085 if (OidIsValid(context->child_reltype))
1087 Assert(var->vartype == context->parent_reltype);
1088 if (context->parent_reltype != context->child_reltype)
1090 ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
1092 r->arg = (Expr *) var;
1093 r->resulttype = context->parent_reltype;
1094 r->convertformat = COERCE_IMPLICIT_CAST;
1095 /* Make sure the Var node has the right type ID, too */
1096 var->vartype = context->child_reltype;
1103 * Build a RowExpr containing the translated variables.
1108 fields = (List *) copyObject(context->translated_vars);
1109 rowexpr = makeNode(RowExpr);
1110 rowexpr->args = fields;
1111 rowexpr->row_typeid = var->vartype;
1112 rowexpr->row_format = COERCE_IMPLICIT_CAST;
1113 return (Node *) rowexpr;
1116 /* system attributes don't need any other translation */
1118 return (Node *) var;
1120 if (IsA(node, RangeTblRef))
1122 RangeTblRef *rtr = (RangeTblRef *) copyObject(node);
1124 if (rtr->rtindex == context->parent_relid)
1125 rtr->rtindex = context->child_relid;
1126 return (Node *) rtr;
1128 if (IsA(node, JoinExpr))
1130 /* Copy the JoinExpr node with correct mutation of subnodes */
1133 j = (JoinExpr *) expression_tree_mutator(node,
1134 adjust_appendrel_attrs_mutator,
1136 /* now fix JoinExpr's rtindex (probably never happens) */
1137 if (j->rtindex == context->parent_relid)
1138 j->rtindex = context->child_relid;
1141 if (IsA(node, InClauseInfo))
1143 /* Copy the InClauseInfo node with correct mutation of subnodes */
1144 InClauseInfo *ininfo;
1146 ininfo = (InClauseInfo *) expression_tree_mutator(node,
1147 adjust_appendrel_attrs_mutator,
1149 /* now fix InClauseInfo's relid sets */
1150 ininfo->lefthand = adjust_relid_set(ininfo->lefthand,
1151 context->parent_relid,
1152 context->child_relid);
1153 ininfo->righthand = adjust_relid_set(ininfo->righthand,
1154 context->parent_relid,
1155 context->child_relid);
1156 return (Node *) ininfo;
1158 /* Shouldn't need to handle OuterJoinInfo or AppendRelInfo here */
1159 Assert(!IsA(node, OuterJoinInfo));
1160 Assert(!IsA(node, AppendRelInfo));
1163 * We have to process RestrictInfo nodes specially.
1165 if (IsA(node, RestrictInfo))
1167 RestrictInfo *oldinfo = (RestrictInfo *) node;
1168 RestrictInfo *newinfo = makeNode(RestrictInfo);
1170 /* Copy all flat-copiable fields */
1171 memcpy(newinfo, oldinfo, sizeof(RestrictInfo));
1173 /* Recursively fix the clause itself */
1174 newinfo->clause = (Expr *)
1175 adjust_appendrel_attrs_mutator((Node *) oldinfo->clause, context);
1177 /* and the modified version, if an OR clause */
1178 newinfo->orclause = (Expr *)
1179 adjust_appendrel_attrs_mutator((Node *) oldinfo->orclause, context);
1181 /* adjust relid sets too */
1182 newinfo->clause_relids = adjust_relid_set(oldinfo->clause_relids,
1183 context->parent_relid,
1184 context->child_relid);
1185 newinfo->required_relids = adjust_relid_set(oldinfo->required_relids,
1186 context->parent_relid,
1187 context->child_relid);
1188 newinfo->left_relids = adjust_relid_set(oldinfo->left_relids,
1189 context->parent_relid,
1190 context->child_relid);
1191 newinfo->right_relids = adjust_relid_set(oldinfo->right_relids,
1192 context->parent_relid,
1193 context->child_relid);
1196 * Reset cached derivative fields, since these might need to have
1197 * different values when considering the child relation.
1199 newinfo->eval_cost.startup = -1;
1200 newinfo->this_selec = -1;
1201 newinfo->left_ec = NULL;
1202 newinfo->right_ec = NULL;
1203 newinfo->left_em = NULL;
1204 newinfo->right_em = NULL;
1205 newinfo->scansel_cache = NIL;
1206 newinfo->left_bucketsize = -1;
1207 newinfo->right_bucketsize = -1;
1209 return (Node *) newinfo;
1213 * NOTE: we do not need to recurse into sublinks, because they should
1214 * already have been converted to subplans before we see them.
1216 Assert(!IsA(node, SubLink));
1217 Assert(!IsA(node, Query));
1220 * BUT: although we don't need to recurse into subplans, we do need to
1221 * make sure that they are copied, not just referenced as
1222 * expression_tree_mutator will do by default. Otherwise we'll have the
1223 * same subplan node referenced from each arm of the finished APPEND plan,
1224 * which will cause trouble in the executor. This is a kluge that should
1225 * go away when we redesign querytrees.
1227 if (is_subplan(node))
1231 /* Copy the node and process subplan args */
1232 node = expression_tree_mutator(node, adjust_appendrel_attrs_mutator,
1234 /* Make sure we have separate copies of subplan and its rtable */
1235 subplan = (SubPlan *) node;
1236 subplan->plan = copyObject(subplan->plan);
1237 subplan->rtable = copyObject(subplan->rtable);
1241 return expression_tree_mutator(node, adjust_appendrel_attrs_mutator,
1246 * Substitute newrelid for oldrelid in a Relid set
1249 adjust_relid_set(Relids relids, Index oldrelid, Index newrelid)
1251 if (bms_is_member(oldrelid, relids))
1253 /* Ensure we have a modifiable copy */
1254 relids = bms_copy(relids);
1255 /* Remove old, add new */
1256 relids = bms_del_member(relids, oldrelid);
1257 relids = bms_add_member(relids, newrelid);
1263 * adjust_appendrel_attr_needed
1264 * Adjust an attr_needed[] array to reference a member rel instead of
1265 * the original appendrel
1267 * oldrel: source of data (we use the attr_needed, min_attr, max_attr fields)
1268 * appinfo: supplies parent_relid, child_relid, col_mappings
1269 * new_min_attr, new_max_attr: desired bounds of new attr_needed array
1271 * The relid sets are adjusted by substituting child_relid for parent_relid.
1272 * (NOTE: oldrel is not necessarily the parent_relid relation!) We are also
1273 * careful to map attribute numbers within the array properly. User
1274 * attributes have to be mapped through col_mappings, but system attributes
1275 * and whole-row references always have the same attno.
1277 * Returns a palloc'd array with the specified bounds
1280 adjust_appendrel_attr_needed(RelOptInfo *oldrel, AppendRelInfo *appinfo,
1281 AttrNumber new_min_attr, AttrNumber new_max_attr)
1283 Relids *new_attr_needed;
1284 Index parent_relid = appinfo->parent_relid;
1285 Index child_relid = appinfo->child_relid;
1289 /* Create empty result array */
1290 Assert(new_min_attr <= oldrel->min_attr);
1291 Assert(new_max_attr >= oldrel->max_attr);
1292 new_attr_needed = (Relids *)
1293 palloc0((new_max_attr - new_min_attr + 1) * sizeof(Relids));
1294 /* Process user attributes, with appropriate attno mapping */
1296 foreach(lm, appinfo->col_mappings)
1298 int child_attr = lfirst_int(lm);
1304 Assert(parent_attr <= oldrel->max_attr);
1305 Assert(child_attr <= new_max_attr);
1306 attrneeded = oldrel->attr_needed[parent_attr - oldrel->min_attr];
1307 attrneeded = adjust_relid_set(attrneeded,
1308 parent_relid, child_relid);
1309 new_attr_needed[child_attr - new_min_attr] = attrneeded;
1313 /* Process system attributes, including whole-row references */
1314 for (parent_attr = oldrel->min_attr; parent_attr <= 0; parent_attr++)
1318 attrneeded = oldrel->attr_needed[parent_attr - oldrel->min_attr];
1319 attrneeded = adjust_relid_set(attrneeded,
1320 parent_relid, child_relid);
1321 new_attr_needed[parent_attr - new_min_attr] = attrneeded;
1324 return new_attr_needed;
1328 * Adjust the targetlist entries of an inherited UPDATE operation
1330 * The expressions have already been fixed, but we have to make sure that
1331 * the target resnos match the child table (they may not, in the case of
1332 * a column that was added after-the-fact by ALTER TABLE). In some cases
1333 * this can force us to re-order the tlist to preserve resno ordering.
1334 * (We do all this work in special cases so that preptlist.c is fast for
1335 * the typical case.)
1337 * The given tlist has already been through expression_tree_mutator;
1338 * therefore the TargetEntry nodes are fresh copies that it's okay to
1341 * Note that this is not needed for INSERT because INSERT isn't inheritable.
1344 adjust_inherited_tlist(List *tlist, AppendRelInfo *context)
1346 bool changed_it = false;
1352 /* This should only happen for an inheritance case, not UNION ALL */
1353 Assert(OidIsValid(context->parent_reloid));
1355 /* Scan tlist and update resnos to match attnums of child rel */
1358 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1362 continue; /* ignore junk items */
1364 /* Look up the translation of this column */
1365 if (tle->resno <= 0 ||
1366 tle->resno > list_length(context->col_mappings))
1367 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
1368 tle->resno, get_rel_name(context->parent_reloid));
1369 newattno = list_nth_int(context->col_mappings, tle->resno - 1);
1371 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
1372 tle->resno, get_rel_name(context->parent_reloid));
1374 if (tle->resno != newattno)
1376 tle->resno = newattno;
1382 * If we changed anything, re-sort the tlist by resno, and make sure
1383 * resjunk entries have resnos above the last real resno. The sort
1384 * algorithm is a bit stupid, but for such a seldom-taken path, small is
1385 * probably better than fast.
1392 for (attrno = 1; more; attrno++)
1397 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1400 continue; /* ignore junk items */
1402 if (tle->resno == attrno)
1403 new_tlist = lappend(new_tlist, tle);
1404 else if (tle->resno > attrno)
1411 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1414 continue; /* here, ignore non-junk items */
1416 tle->resno = attrno;
1417 new_tlist = lappend(new_tlist, tle);