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.135 2007/01/05 22:19:32 momjian 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(subquery, tuple_fraction, NULL);
183 * Add a SubqueryScan with the caller-requested targetlist
186 make_subqueryscan(generate_setop_tlist(colTypes, flag,
196 * We don't bother to determine the subquery's output ordering since
197 * it won't be reflected in the set-op result anyhow.
203 else if (IsA(setOp, SetOperationStmt))
205 SetOperationStmt *op = (SetOperationStmt *) setOp;
208 /* UNIONs are much different from INTERSECT/EXCEPT */
209 if (op->op == SETOP_UNION)
210 plan = generate_union_plan(op, root, tuple_fraction,
214 plan = generate_nonunion_plan(op, root,
219 * If necessary, add a Result node to project the caller-requested
222 * XXX you don't really want to know about this: setrefs.c will apply
223 * replace_vars_with_subplan_refs() to the Result node's tlist. This
224 * would fail if the Vars generated by generate_setop_tlist() were not
225 * exactly equal() to the corresponding tlist entries of the subplan.
226 * However, since the subplan was generated by generate_union_plan()
227 * or generate_nonunion_plan(), and hence its tlist was generated by
228 * generate_append_tlist(), this will work. We just tell
229 * generate_setop_tlist() to use varno 0.
232 !tlist_same_datatypes(plan->targetlist, colTypes, junkOK))
235 make_result(generate_setop_tlist(colTypes, flag,
247 elog(ERROR, "unrecognized node type: %d",
248 (int) nodeTag(setOp));
249 return NULL; /* keep compiler quiet */
254 * Generate plan for a UNION or UNION ALL node
257 generate_union_plan(SetOperationStmt *op, PlannerInfo *root,
258 double tuple_fraction,
259 List *refnames_tlist,
267 * If plain UNION, tell children to fetch all tuples.
269 * Note: in UNION ALL, we pass the top-level tuple_fraction unmodified to
270 * each arm of the UNION ALL. One could make a case for reducing the
271 * tuple fraction for later arms (discounting by the expected size of the
272 * earlier arms' results) but it seems not worth the trouble. The normal
273 * case where tuple_fraction isn't already zero is a LIMIT at top level,
274 * and passing it down as-is is usually enough to get the desired result
275 * of preferring fast-start plans.
278 tuple_fraction = 0.0;
281 * If any of my children are identical UNION nodes (same op, all-flag, and
282 * colTypes) then they can be merged into this node so that we generate
283 * only one Append and Sort for the lot. Recurse to find such nodes and
284 * compute their children's plans.
286 planlist = list_concat(recurse_union_children(op->larg, root,
289 recurse_union_children(op->rarg, root,
291 op, refnames_tlist));
294 * Generate tlist for Append plan node.
296 * The tlist for an Append plan isn't important as far as the Append is
297 * concerned, but we must make it look real anyway for the benefit of the
298 * next plan level up.
300 tlist = generate_append_tlist(op->colTypes, false,
301 planlist, refnames_tlist);
304 * Append the child results together.
306 plan = (Plan *) make_append(planlist, false, tlist);
309 * For UNION ALL, we just need the Append plan. For UNION, need to add
310 * Sort and Unique nodes to produce unique output.
316 sortList = addAllTargetsToSortList(NULL, NIL, tlist, false);
319 plan = (Plan *) make_sort_from_sortclauses(root, sortList, plan);
320 plan = (Plan *) make_unique(plan, sortList);
322 *sortClauses = sortList;
331 * Generate plan for an INTERSECT, INTERSECT ALL, EXCEPT, or EXCEPT ALL node
334 generate_nonunion_plan(SetOperationStmt *op, PlannerInfo *root,
335 List *refnames_tlist,
347 /* Recurse on children, ensuring their outputs are marked */
348 lplan = recurse_set_operations(op->larg, root,
349 0.0 /* all tuples needed */ ,
350 op->colTypes, false, 0,
353 rplan = recurse_set_operations(op->rarg, root,
354 0.0 /* all tuples needed */ ,
355 op->colTypes, false, 1,
358 planlist = list_make2(lplan, rplan);
361 * Generate tlist for Append plan node.
363 * The tlist for an Append plan isn't important as far as the Append is
364 * concerned, but we must make it look real anyway for the benefit of the
365 * next plan level up. In fact, it has to be real enough that the flag
366 * column is shown as a variable not a constant, else setrefs.c will get
369 tlist = generate_append_tlist(op->colTypes, true,
370 planlist, refnames_tlist);
373 * Append the child results together.
375 plan = (Plan *) make_append(planlist, false, tlist);
378 * Sort the child results, then add a SetOp plan node to generate the
381 sortList = addAllTargetsToSortList(NULL, NIL, tlist, false);
383 if (sortList == NIL) /* nothing to sort on? */
389 plan = (Plan *) make_sort_from_sortclauses(root, sortList, plan);
392 case SETOP_INTERSECT:
393 cmd = op->all ? SETOPCMD_INTERSECT_ALL : SETOPCMD_INTERSECT;
396 cmd = op->all ? SETOPCMD_EXCEPT_ALL : SETOPCMD_EXCEPT;
399 elog(ERROR, "unrecognized set op: %d",
401 cmd = SETOPCMD_INTERSECT; /* keep compiler quiet */
404 plan = (Plan *) make_setop(cmd, plan, sortList, list_length(op->colTypes) + 1);
406 *sortClauses = sortList;
412 * Pull up children of a UNION node that are identically-propertied UNIONs.
414 * NOTE: we can also pull a UNION ALL up into a UNION, since the distinct
415 * output rows will be lost anyway.
418 recurse_union_children(Node *setOp, PlannerInfo *root,
419 double tuple_fraction,
420 SetOperationStmt *top_union,
421 List *refnames_tlist)
423 List *child_sortclauses;
425 if (IsA(setOp, SetOperationStmt))
427 SetOperationStmt *op = (SetOperationStmt *) setOp;
429 if (op->op == top_union->op &&
430 (op->all == top_union->all || op->all) &&
431 equal(op->colTypes, top_union->colTypes))
433 /* Same UNION, so fold children into parent's subplan list */
434 return list_concat(recurse_union_children(op->larg, root,
438 recurse_union_children(op->rarg, root,
446 * Not same, so plan this child separately.
448 * Note we disallow any resjunk columns in child results. This is
449 * necessary since the Append node that implements the union won't do any
450 * projection, and upper levels will get confused if some of our output
451 * tuples have junk and some don't. This case only arises when we have an
452 * EXCEPT or INTERSECT as child, else there won't be resjunk anyway.
454 return list_make1(recurse_set_operations(setOp, root,
456 top_union->colTypes, false,
458 &child_sortclauses));
462 * Generate targetlist for a set-operation plan node
464 * colTypes: column datatypes for non-junk columns
465 * flag: -1 if no flag column needed, 0 or 1 to create a const flag column
466 * varno: varno to use in generated Vars
467 * hack_constants: true to copy up constants (see comments in code)
468 * input_tlist: targetlist of this node's input node
469 * refnames_tlist: targetlist to take column names from
472 generate_setop_tlist(List *colTypes, int flag,
476 List *refnames_tlist)
486 j = list_head(input_tlist);
487 k = list_head(refnames_tlist);
490 Oid colType = lfirst_oid(i);
491 TargetEntry *inputtle = (TargetEntry *) lfirst(j);
492 TargetEntry *reftle = (TargetEntry *) lfirst(k);
494 Assert(inputtle->resno == resno);
495 Assert(reftle->resno == resno);
496 Assert(!inputtle->resjunk);
497 Assert(!reftle->resjunk);
500 * Generate columns referencing input columns and having appropriate
501 * data types and column names. Insert datatype coercions where
504 * HACK: constants in the input's targetlist are copied up as-is
505 * rather than being referenced as subquery outputs. This is mainly
506 * to ensure that when we try to coerce them to the output column's
507 * datatype, the right things happen for UNKNOWN constants. But do
508 * this only at the first level of subquery-scan plans; we don't want
509 * phony constants appearing in the output tlists of upper-level
512 if (hack_constants && inputtle->expr && IsA(inputtle->expr, Const))
513 expr = (Node *) inputtle->expr;
515 expr = (Node *) makeVar(varno,
517 exprType((Node *) inputtle->expr),
518 exprTypmod((Node *) inputtle->expr),
520 if (exprType(expr) != colType)
522 expr = coerce_to_common_type(NULL, /* no UNKNOWNs here */
525 "UNION/INTERSECT/EXCEPT");
527 tle = makeTargetEntry((Expr *) expr,
528 (AttrNumber) resno++,
529 pstrdup(reftle->resname),
531 tlist = lappend(tlist, tle);
539 /* Add a resjunk flag column */
540 /* flag value is the given constant */
541 expr = (Node *) makeConst(INT4OID,
546 tle = makeTargetEntry((Expr *) expr,
547 (AttrNumber) resno++,
550 tlist = lappend(tlist, tle);
557 * Generate targetlist for a set-operation Append node
559 * colTypes: column datatypes for non-junk columns
560 * flag: true to create a flag column copied up from subplans
561 * input_plans: list of sub-plans of the Append
562 * refnames_tlist: targetlist to take column names from
564 * The entries in the Append's targetlist should always be simple Vars;
565 * we just have to make sure they have the right datatypes and typmods.
566 * The Vars are always generated with varno 0.
569 generate_append_tlist(List *colTypes, bool flag,
571 List *refnames_tlist)
575 ListCell *curColType;
576 ListCell *ref_tl_item;
584 * First extract typmods to use.
586 * If the inputs all agree on type and typmod of a particular column, use
587 * that typmod; else use -1.
589 colTypmods = (int32 *) palloc(list_length(colTypes) * sizeof(int32));
591 foreach(planl, input_plans)
593 Plan *subplan = (Plan *) lfirst(planl);
596 curColType = list_head(colTypes);
598 foreach(subtlist, subplan->targetlist)
600 TargetEntry *subtle = (TargetEntry *) lfirst(subtlist);
604 Assert(curColType != NULL);
605 if (exprType((Node *) subtle->expr) == lfirst_oid(curColType))
607 /* If first subplan, copy the typmod; else compare */
608 int32 subtypmod = exprTypmod((Node *) subtle->expr);
610 if (planl == list_head(input_plans))
611 colTypmods[colindex] = subtypmod;
612 else if (subtypmod != colTypmods[colindex])
613 colTypmods[colindex] = -1;
617 /* types disagree, so force typmod to -1 */
618 colTypmods[colindex] = -1;
620 curColType = lnext(curColType);
623 Assert(curColType == NULL);
627 * Now we can build the tlist for the Append.
630 forboth(curColType, colTypes, ref_tl_item, refnames_tlist)
632 Oid colType = lfirst_oid(curColType);
633 int32 colTypmod = colTypmods[colindex++];
634 TargetEntry *reftle = (TargetEntry *) lfirst(ref_tl_item);
636 Assert(reftle->resno == resno);
637 Assert(!reftle->resjunk);
638 expr = (Node *) makeVar(0,
643 tle = makeTargetEntry((Expr *) expr,
644 (AttrNumber) resno++,
645 pstrdup(reftle->resname),
647 tlist = lappend(tlist, tle);
652 /* Add a resjunk flag column */
653 /* flag value is shown as copied up from subplan */
654 expr = (Node *) makeVar(0,
659 tle = makeTargetEntry((Expr *) expr,
660 (AttrNumber) resno++,
663 tlist = lappend(tlist, tle);
673 * find_all_inheritors -
674 * Returns a list of relation OIDs including the given rel plus
675 * all relations that inherit from it, directly or indirectly.
678 find_all_inheritors(Oid parentrel)
684 * We build a list starting with the given rel and adding all direct and
685 * indirect children. We can use a single list as both the record of
686 * already-found rels and the agenda of rels yet to be scanned for more
687 * children. This is a bit tricky but works because the foreach() macro
688 * doesn't fetch the next list element until the bottom of the loop.
690 rels_list = list_make1_oid(parentrel);
692 foreach(l, rels_list)
694 Oid currentrel = lfirst_oid(l);
695 List *currentchildren;
697 /* Get the direct children of this rel */
698 currentchildren = find_inheritance_children(currentrel);
701 * Add to the queue only those children not already seen. This avoids
702 * making duplicate entries in case of multiple inheritance paths from
703 * the same parent. (It'll also keep us from getting into an infinite
704 * loop, though theoretically there can't be any cycles in the
705 * inheritance graph anyway.)
707 rels_list = list_concat_unique_oid(rels_list, currentchildren);
714 * expand_inherited_tables
715 * Expand each rangetable entry that represents an inheritance set
716 * into an "append relation". At the conclusion of this process,
717 * the "inh" flag is set in all and only those RTEs that are append
721 expand_inherited_tables(PlannerInfo *root)
728 * expand_inherited_rtentry may add RTEs to parse->rtable; there is no
729 * need to scan them since they can't have inh=true. So just scan as far
730 * as the original end of the rtable list.
732 nrtes = list_length(root->parse->rtable);
733 rl = list_head(root->parse->rtable);
734 for (rti = 1; rti <= nrtes; rti++)
736 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rl);
738 expand_inherited_rtentry(root, rte, rti);
744 * expand_inherited_rtentry
745 * Check whether a rangetable entry represents an inheritance set.
746 * If so, add entries for all the child tables to the query's
747 * rangetable, and build AppendRelInfo nodes for all the child tables
748 * and add them to root->append_rel_list. If not, clear the entry's
749 * "inh" flag to prevent later code from looking for AppendRelInfos.
751 * Note that the original RTE is considered to represent the whole
752 * inheritance set. The first of the generated RTEs is an RTE for the same
753 * table, but with inh = false, to represent the parent table in its role
754 * as a simple member of the inheritance set.
756 * A childless table is never considered to be an inheritance set; therefore
757 * a parent RTE must always have at least two associated AppendRelInfos.
760 expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
762 Query *parse = root->parse;
764 Relation oldrelation;
770 /* Does RT entry allow inheritance? */
773 /* Ignore any already-expanded UNION ALL nodes */
774 if (rte->rtekind != RTE_RELATION)
776 Assert(rte->rtekind == RTE_SUBQUERY);
779 /* Fast path for common case of childless table */
780 parentOID = rte->relid;
781 if (!has_subclass(parentOID))
783 /* Clear flag before returning */
788 /* Scan for all members of inheritance set */
789 inhOIDs = find_all_inheritors(parentOID);
792 * Check that there's at least one descendant, else treat as no-child
793 * case. This could happen despite above has_subclass() check, if table
794 * once had a child but no longer does.
796 if (list_length(inhOIDs) < 2)
798 /* Clear flag before returning */
804 * Must open the parent relation to examine its tupdesc. We need not lock
805 * it since the rewriter already obtained at least AccessShareLock on each
806 * relation used in the query.
808 oldrelation = heap_open(parentOID, NoLock);
811 * However, for each child relation we add to the query, we must obtain an
812 * appropriate lock, because this will be the first use of those relations
813 * in the parse/rewrite/plan pipeline.
815 * If the parent relation is the query's result relation, then we need
816 * RowExclusiveLock. Otherwise, check to see if the relation is accessed
817 * FOR UPDATE/SHARE or not. We can't just grab AccessShareLock because
818 * then the executor would be trying to upgrade the lock, leading to
819 * possible deadlocks. (This code should match the parser and rewriter.)
821 if (rti == parse->resultRelation)
822 lockmode = RowExclusiveLock;
823 else if (get_rowmark(parse, rti))
824 lockmode = RowShareLock;
826 lockmode = AccessShareLock;
828 /* Scan the inheritance set and expand it */
832 Oid childOID = lfirst_oid(l);
833 Relation newrelation;
834 RangeTblEntry *childrte;
836 AppendRelInfo *appinfo;
839 * It is possible that the parent table has children that are temp
840 * tables of other backends. We cannot safely access such tables
841 * (because of buffering issues), and the best thing to do seems to be
842 * to silently ignore them.
844 if (childOID != parentOID &&
845 isOtherTempNamespace(get_rel_namespace(childOID)))
848 /* Open rel, acquire the appropriate lock type */
849 if (childOID != parentOID)
850 newrelation = heap_open(childOID, lockmode);
852 newrelation = oldrelation;
855 * Build an RTE for the child, and attach to query's rangetable list.
856 * We copy most fields of the parent's RTE, but replace relation OID,
857 * and set inh = false.
859 childrte = copyObject(rte);
860 childrte->relid = childOID;
861 childrte->inh = false;
862 parse->rtable = lappend(parse->rtable, childrte);
863 childRTindex = list_length(parse->rtable);
866 * Build an AppendRelInfo for this parent and child.
868 appinfo = makeNode(AppendRelInfo);
869 appinfo->parent_relid = rti;
870 appinfo->child_relid = childRTindex;
871 appinfo->parent_reltype = oldrelation->rd_rel->reltype;
872 appinfo->child_reltype = newrelation->rd_rel->reltype;
873 make_inh_translation_lists(oldrelation, newrelation, childRTindex,
874 &appinfo->col_mappings,
875 &appinfo->translated_vars);
876 appinfo->parent_reloid = parentOID;
877 appinfos = lappend(appinfos, appinfo);
879 /* Close child relations, but keep locks */
880 if (childOID != parentOID)
881 heap_close(newrelation, NoLock);
884 heap_close(oldrelation, NoLock);
887 * If all the children were temp tables, pretend it's a non-inheritance
888 * situation. The duplicate RTE we added for the parent table is
889 * harmless, so we don't bother to get rid of it.
891 if (list_length(appinfos) < 2)
893 /* Clear flag before returning */
898 /* Otherwise, OK to add to root->append_rel_list */
899 root->append_rel_list = list_concat(root->append_rel_list, appinfos);
902 * The executor will check the parent table's access permissions when it
903 * examines the parent's added RTE entry. There's no need to check twice,
904 * so turn off access check bits in the original RTE.
906 rte->requiredPerms = 0;
910 * make_inh_translation_lists
911 * Build the lists of translations from parent Vars to child Vars for
912 * an inheritance child. We need both a column number mapping list
913 * and a list of Vars representing the child columns.
915 * For paranoia's sake, we match type as well as attribute name.
918 make_inh_translation_lists(Relation oldrelation, Relation newrelation,
920 List **col_mappings, List **translated_vars)
924 TupleDesc old_tupdesc = RelationGetDescr(oldrelation);
925 TupleDesc new_tupdesc = RelationGetDescr(newrelation);
926 int oldnatts = old_tupdesc->natts;
927 int newnatts = new_tupdesc->natts;
930 for (old_attno = 0; old_attno < oldnatts; old_attno++)
932 Form_pg_attribute att;
938 att = old_tupdesc->attrs[old_attno];
939 if (att->attisdropped)
941 /* Just put 0/NULL into this list entry */
942 numbers = lappend_int(numbers, 0);
943 vars = lappend(vars, NULL);
946 attname = NameStr(att->attname);
947 atttypid = att->atttypid;
948 atttypmod = att->atttypmod;
951 * When we are generating the "translation list" for the parent table
952 * of an inheritance set, no need to search for matches.
954 if (oldrelation == newrelation)
956 numbers = lappend_int(numbers, old_attno + 1);
957 vars = lappend(vars, makeVar(newvarno,
958 (AttrNumber) (old_attno + 1),
966 * Otherwise we have to search for the matching column by name.
967 * There's no guarantee it'll have the same column position, because
968 * of cases like ALTER TABLE ADD COLUMN and multiple inheritance.
970 for (new_attno = 0; new_attno < newnatts; new_attno++)
972 att = new_tupdesc->attrs[new_attno];
973 if (att->attisdropped || att->attinhcount == 0)
975 if (strcmp(attname, NameStr(att->attname)) != 0)
977 /* Found it, check type */
978 if (atttypid != att->atttypid || atttypmod != att->atttypmod)
979 elog(ERROR, "attribute \"%s\" of relation \"%s\" does not match parent's type",
980 attname, RelationGetRelationName(newrelation));
982 numbers = lappend_int(numbers, new_attno + 1);
983 vars = lappend(vars, makeVar(newvarno,
984 (AttrNumber) (new_attno + 1),
991 if (new_attno >= newnatts)
992 elog(ERROR, "could not find inherited attribute \"%s\" of relation \"%s\"",
993 attname, RelationGetRelationName(newrelation));
996 *col_mappings = numbers;
997 *translated_vars = vars;
1001 * adjust_appendrel_attrs
1002 * Copy the specified query or expression and translate Vars referring
1003 * to the parent rel of the specified AppendRelInfo to refer to the
1004 * child rel instead. We also update rtindexes appearing outside Vars,
1005 * such as resultRelation and jointree relids.
1007 * Note: this is only applied after conversion of sublinks to subplans,
1008 * so we don't need to cope with recursion into sub-queries.
1010 * Note: this is not hugely different from what ResolveNew() does; maybe
1011 * we should try to fold the two routines together.
1014 adjust_appendrel_attrs(Node *node, AppendRelInfo *appinfo)
1019 * Must be prepared to start with a Query or a bare expression tree.
1021 if (node && IsA(node, Query))
1025 newnode = query_tree_mutator((Query *) node,
1026 adjust_appendrel_attrs_mutator,
1028 QTW_IGNORE_RT_SUBQUERIES);
1029 if (newnode->resultRelation == appinfo->parent_relid)
1031 newnode->resultRelation = appinfo->child_relid;
1032 /* Fix tlist resnos too, if it's inherited UPDATE */
1033 if (newnode->commandType == CMD_UPDATE)
1034 newnode->targetList =
1035 adjust_inherited_tlist(newnode->targetList,
1038 result = (Node *) newnode;
1041 result = adjust_appendrel_attrs_mutator(node, appinfo);
1047 adjust_appendrel_attrs_mutator(Node *node, AppendRelInfo *context)
1053 Var *var = (Var *) copyObject(node);
1055 if (var->varlevelsup == 0 &&
1056 var->varno == context->parent_relid)
1058 var->varno = context->child_relid;
1059 var->varnoold = context->child_relid;
1060 if (var->varattno > 0)
1064 if (var->varattno > list_length(context->translated_vars))
1065 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
1066 var->varattno, get_rel_name(context->parent_reloid));
1067 newnode = copyObject(list_nth(context->translated_vars,
1068 var->varattno - 1));
1069 if (newnode == NULL)
1070 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
1071 var->varattno, get_rel_name(context->parent_reloid));
1074 else if (var->varattno == 0)
1077 * Whole-row Var: if we are dealing with named rowtypes, we
1078 * can use a whole-row Var for the child table plus a coercion
1079 * step to convert the tuple layout to the parent's rowtype.
1080 * Otherwise we have to generate a RowExpr.
1082 if (OidIsValid(context->child_reltype))
1084 Assert(var->vartype == context->parent_reltype);
1085 if (context->parent_reltype != context->child_reltype)
1087 ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
1089 r->arg = (Expr *) var;
1090 r->resulttype = context->parent_reltype;
1091 r->convertformat = COERCE_IMPLICIT_CAST;
1092 /* Make sure the Var node has the right type ID, too */
1093 var->vartype = context->child_reltype;
1100 * Build a RowExpr containing the translated variables.
1105 fields = (List *) copyObject(context->translated_vars);
1106 rowexpr = makeNode(RowExpr);
1107 rowexpr->args = fields;
1108 rowexpr->row_typeid = var->vartype;
1109 rowexpr->row_format = COERCE_IMPLICIT_CAST;
1110 return (Node *) rowexpr;
1113 /* system attributes don't need any other translation */
1115 return (Node *) var;
1117 if (IsA(node, RangeTblRef))
1119 RangeTblRef *rtr = (RangeTblRef *) copyObject(node);
1121 if (rtr->rtindex == context->parent_relid)
1122 rtr->rtindex = context->child_relid;
1123 return (Node *) rtr;
1125 if (IsA(node, JoinExpr))
1127 /* Copy the JoinExpr node with correct mutation of subnodes */
1130 j = (JoinExpr *) expression_tree_mutator(node,
1131 adjust_appendrel_attrs_mutator,
1133 /* now fix JoinExpr's rtindex (probably never happens) */
1134 if (j->rtindex == context->parent_relid)
1135 j->rtindex = context->child_relid;
1138 if (IsA(node, InClauseInfo))
1140 /* Copy the InClauseInfo node with correct mutation of subnodes */
1141 InClauseInfo *ininfo;
1143 ininfo = (InClauseInfo *) expression_tree_mutator(node,
1144 adjust_appendrel_attrs_mutator,
1146 /* now fix InClauseInfo's relid sets */
1147 ininfo->lefthand = adjust_relid_set(ininfo->lefthand,
1148 context->parent_relid,
1149 context->child_relid);
1150 ininfo->righthand = adjust_relid_set(ininfo->righthand,
1151 context->parent_relid,
1152 context->child_relid);
1153 return (Node *) ininfo;
1155 /* Shouldn't need to handle OuterJoinInfo or AppendRelInfo here */
1156 Assert(!IsA(node, OuterJoinInfo));
1157 Assert(!IsA(node, AppendRelInfo));
1160 * We have to process RestrictInfo nodes specially.
1162 if (IsA(node, RestrictInfo))
1164 RestrictInfo *oldinfo = (RestrictInfo *) node;
1165 RestrictInfo *newinfo = makeNode(RestrictInfo);
1167 /* Copy all flat-copiable fields */
1168 memcpy(newinfo, oldinfo, sizeof(RestrictInfo));
1170 /* Recursively fix the clause itself */
1171 newinfo->clause = (Expr *)
1172 adjust_appendrel_attrs_mutator((Node *) oldinfo->clause, context);
1174 /* and the modified version, if an OR clause */
1175 newinfo->orclause = (Expr *)
1176 adjust_appendrel_attrs_mutator((Node *) oldinfo->orclause, context);
1178 /* adjust relid sets too */
1179 newinfo->clause_relids = adjust_relid_set(oldinfo->clause_relids,
1180 context->parent_relid,
1181 context->child_relid);
1182 newinfo->required_relids = adjust_relid_set(oldinfo->required_relids,
1183 context->parent_relid,
1184 context->child_relid);
1185 newinfo->left_relids = adjust_relid_set(oldinfo->left_relids,
1186 context->parent_relid,
1187 context->child_relid);
1188 newinfo->right_relids = adjust_relid_set(oldinfo->right_relids,
1189 context->parent_relid,
1190 context->child_relid);
1193 * Reset cached derivative fields, since these might need to have
1194 * different values when considering the child relation.
1196 newinfo->eval_cost.startup = -1;
1197 newinfo->this_selec = -1;
1198 newinfo->left_pathkey = NIL;
1199 newinfo->right_pathkey = NIL;
1200 newinfo->left_mergescansel = -1;
1201 newinfo->right_mergescansel = -1;
1202 newinfo->left_bucketsize = -1;
1203 newinfo->right_bucketsize = -1;
1205 return (Node *) newinfo;
1209 * NOTE: we do not need to recurse into sublinks, because they should
1210 * already have been converted to subplans before we see them.
1212 Assert(!IsA(node, SubLink));
1213 Assert(!IsA(node, Query));
1216 * BUT: although we don't need to recurse into subplans, we do need to
1217 * make sure that they are copied, not just referenced as
1218 * expression_tree_mutator will do by default. Otherwise we'll have the
1219 * same subplan node referenced from each arm of the finished APPEND plan,
1220 * which will cause trouble in the executor. This is a kluge that should
1221 * go away when we redesign querytrees.
1223 if (is_subplan(node))
1227 /* Copy the node and process subplan args */
1228 node = expression_tree_mutator(node, adjust_appendrel_attrs_mutator,
1230 /* Make sure we have separate copies of subplan and its rtable */
1231 subplan = (SubPlan *) node;
1232 subplan->plan = copyObject(subplan->plan);
1233 subplan->rtable = copyObject(subplan->rtable);
1237 return expression_tree_mutator(node, adjust_appendrel_attrs_mutator,
1242 * Substitute newrelid for oldrelid in a Relid set
1245 adjust_relid_set(Relids relids, Index oldrelid, Index newrelid)
1247 if (bms_is_member(oldrelid, relids))
1249 /* Ensure we have a modifiable copy */
1250 relids = bms_copy(relids);
1251 /* Remove old, add new */
1252 relids = bms_del_member(relids, oldrelid);
1253 relids = bms_add_member(relids, newrelid);
1259 * adjust_appendrel_attr_needed
1260 * Adjust an attr_needed[] array to reference a member rel instead of
1261 * the original appendrel
1263 * oldrel: source of data (we use the attr_needed, min_attr, max_attr fields)
1264 * appinfo: supplies parent_relid, child_relid, col_mappings
1265 * new_min_attr, new_max_attr: desired bounds of new attr_needed array
1267 * The relid sets are adjusted by substituting child_relid for parent_relid.
1268 * (NOTE: oldrel is not necessarily the parent_relid relation!) We are also
1269 * careful to map attribute numbers within the array properly. User
1270 * attributes have to be mapped through col_mappings, but system attributes
1271 * and whole-row references always have the same attno.
1273 * Returns a palloc'd array with the specified bounds
1276 adjust_appendrel_attr_needed(RelOptInfo *oldrel, AppendRelInfo *appinfo,
1277 AttrNumber new_min_attr, AttrNumber new_max_attr)
1279 Relids *new_attr_needed;
1280 Index parent_relid = appinfo->parent_relid;
1281 Index child_relid = appinfo->child_relid;
1285 /* Create empty result array */
1286 Assert(new_min_attr <= oldrel->min_attr);
1287 Assert(new_max_attr >= oldrel->max_attr);
1288 new_attr_needed = (Relids *)
1289 palloc0((new_max_attr - new_min_attr + 1) * sizeof(Relids));
1290 /* Process user attributes, with appropriate attno mapping */
1292 foreach(lm, appinfo->col_mappings)
1294 int child_attr = lfirst_int(lm);
1300 Assert(parent_attr <= oldrel->max_attr);
1301 Assert(child_attr <= new_max_attr);
1302 attrneeded = oldrel->attr_needed[parent_attr - oldrel->min_attr];
1303 attrneeded = adjust_relid_set(attrneeded,
1304 parent_relid, child_relid);
1305 new_attr_needed[child_attr - new_min_attr] = attrneeded;
1309 /* Process system attributes, including whole-row references */
1310 for (parent_attr = oldrel->min_attr; parent_attr <= 0; parent_attr++)
1314 attrneeded = oldrel->attr_needed[parent_attr - oldrel->min_attr];
1315 attrneeded = adjust_relid_set(attrneeded,
1316 parent_relid, child_relid);
1317 new_attr_needed[parent_attr - new_min_attr] = attrneeded;
1320 return new_attr_needed;
1324 * Adjust the targetlist entries of an inherited UPDATE operation
1326 * The expressions have already been fixed, but we have to make sure that
1327 * the target resnos match the child table (they may not, in the case of
1328 * a column that was added after-the-fact by ALTER TABLE). In some cases
1329 * this can force us to re-order the tlist to preserve resno ordering.
1330 * (We do all this work in special cases so that preptlist.c is fast for
1331 * the typical case.)
1333 * The given tlist has already been through expression_tree_mutator;
1334 * therefore the TargetEntry nodes are fresh copies that it's okay to
1337 * Note that this is not needed for INSERT because INSERT isn't inheritable.
1340 adjust_inherited_tlist(List *tlist, AppendRelInfo *context)
1342 bool changed_it = false;
1348 /* This should only happen for an inheritance case, not UNION ALL */
1349 Assert(OidIsValid(context->parent_reloid));
1351 /* Scan tlist and update resnos to match attnums of child rel */
1354 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1358 continue; /* ignore junk items */
1360 /* Look up the translation of this column */
1361 if (tle->resno <= 0 ||
1362 tle->resno > list_length(context->col_mappings))
1363 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
1364 tle->resno, get_rel_name(context->parent_reloid));
1365 newattno = list_nth_int(context->col_mappings, tle->resno - 1);
1367 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
1368 tle->resno, get_rel_name(context->parent_reloid));
1370 if (tle->resno != newattno)
1372 tle->resno = newattno;
1378 * If we changed anything, re-sort the tlist by resno, and make sure
1379 * resjunk entries have resnos above the last real resno. The sort
1380 * algorithm is a bit stupid, but for such a seldom-taken path, small is
1381 * probably better than fast.
1388 for (attrno = 1; more; attrno++)
1393 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1396 continue; /* ignore junk items */
1398 if (tle->resno == attrno)
1399 new_tlist = lappend(new_tlist, tle);
1400 else if (tle->resno > attrno)
1407 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1410 continue; /* here, ignore non-junk items */
1412 tle->resno = attrno;
1413 new_tlist = lappend(new_tlist, tle);