1 /*-------------------------------------------------------------------------
4 * Routines to plan set-operation queries. The filename is a leftover
5 * from a time when only UNIONs were implemented.
7 * There is also some code here to support planning of queries that use
8 * inheritance (SELECT FROM foo*). This no longer has much connection
9 * to the processing of UNION queries, but it's still here.
12 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
17 * $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.84 2003/01/05 00:56:40 tgl Exp $
19 *-------------------------------------------------------------------------
24 #include "catalog/pg_type.h"
25 #include "nodes/makefuncs.h"
26 #include "optimizer/clauses.h"
27 #include "optimizer/plancat.h"
28 #include "optimizer/planmain.h"
29 #include "optimizer/planner.h"
30 #include "optimizer/prep.h"
31 #include "optimizer/tlist.h"
32 #include "parser/parse_clause.h"
33 #include "parser/parse_coerce.h"
34 #include "parser/parsetree.h"
35 #include "utils/lsyscache.h"
37 /* macros borrowed from expression_tree_mutator */
39 #define FLATCOPY(newnode, node, nodetype) \
40 ( (newnode) = makeNode(nodetype), \
41 memcpy((newnode), (node), sizeof(nodetype)) )
49 } adjust_inherited_attrs_context;
51 static Plan *recurse_set_operations(Node *setOp, Query *parse,
52 List *colTypes, bool junkOK,
53 int flag, List *refnames_tlist);
54 static Plan *generate_union_plan(SetOperationStmt *op, Query *parse,
55 List *refnames_tlist);
56 static Plan *generate_nonunion_plan(SetOperationStmt *op, Query *parse,
57 List *refnames_tlist);
58 static List *recurse_union_children(Node *setOp, Query *parse,
59 SetOperationStmt *top_union,
60 List *refnames_tlist);
61 static List *generate_setop_tlist(List *colTypes, int flag,
64 List *refnames_tlist);
65 static List *generate_append_tlist(List *colTypes, bool flag,
67 List *refnames_tlist);
68 static Node *adjust_inherited_attrs_mutator(Node *node,
69 adjust_inherited_attrs_context *context);
70 static List *adjust_inherited_tlist(List *tlist, Oid new_relid);
76 * Plans the queries for a tree of set operations (UNION/INTERSECT/EXCEPT)
78 * This routine only deals with the setOperations tree of the given query.
79 * Any top-level ORDER BY requested in parse->sortClause will be added
80 * when we return to grouping_planner.
83 plan_set_operations(Query *parse)
85 SetOperationStmt *topop = (SetOperationStmt *) parse->setOperations;
89 Assert(topop && IsA(topop, SetOperationStmt));
92 * Find the leftmost component Query. We need to use its column names
93 * for all generated tlists (else SELECT INTO won't work right).
96 while (node && IsA(node, SetOperationStmt))
97 node = ((SetOperationStmt *) node)->larg;
98 Assert(node && IsA(node, RangeTblRef));
99 leftmostQuery = rt_fetch(((RangeTblRef *) node)->rtindex,
100 parse->rtable)->subquery;
101 Assert(leftmostQuery != NULL);
104 * Recurse on setOperations tree to generate plans for set ops. The
105 * final output plan should have just the column types shown as the
106 * output from the top-level node, plus possibly a resjunk working
107 * column (we can rely on upper-level nodes to deal with that).
109 return recurse_set_operations((Node *) topop, parse,
110 topop->colTypes, true, -1,
111 leftmostQuery->targetList);
115 * recurse_set_operations
116 * Recursively handle one step in a tree of set operations
118 * colTypes: integer list of type OIDs of expected output columns
119 * junkOK: if true, child resjunk columns may be left in the result
120 * flag: if >= 0, add a resjunk output column indicating value of flag
121 * refnames_tlist: targetlist to take column names from
124 recurse_set_operations(Node *setOp, Query *parse,
125 List *colTypes, bool junkOK,
126 int flag, List *refnames_tlist)
128 if (IsA(setOp, RangeTblRef))
130 RangeTblRef *rtr = (RangeTblRef *) setOp;
131 RangeTblEntry *rte = rt_fetch(rtr->rtindex, parse->rtable);
132 Query *subquery = rte->subquery;
136 Assert(subquery != NULL);
139 * Generate plan for primitive subquery
141 subplan = subquery_planner(subquery,
142 -1.0 /* default case */ );
145 * Add a SubqueryScan with the caller-requested targetlist
148 make_subqueryscan(generate_setop_tlist(colTypes, flag, true,
156 else if (IsA(setOp, SetOperationStmt))
158 SetOperationStmt *op = (SetOperationStmt *) setOp;
161 /* UNIONs are much different from INTERSECT/EXCEPT */
162 if (op->op == SETOP_UNION)
163 plan = generate_union_plan(op, parse, refnames_tlist);
165 plan = generate_nonunion_plan(op, parse, refnames_tlist);
168 * If necessary, add a Result node to project the caller-requested
171 * XXX you don't really want to know about this: setrefs.c will apply
172 * replace_vars_with_subplan_refs() to the Result node's tlist.
173 * This would fail if the Vars generated by generate_setop_tlist()
174 * were not exactly equal() to the corresponding tlist entries of
175 * the subplan. However, since the subplan was generated by
176 * generate_union_plan() or generate_nonunion_plan(), and hence
177 * its tlist was generated by generate_append_tlist(), this will
181 !tlist_same_datatypes(plan->targetlist, colTypes, junkOK))
184 make_result(generate_setop_tlist(colTypes, flag, false,
194 elog(ERROR, "recurse_set_operations: unexpected node %d",
195 (int) nodeTag(setOp));
196 return NULL; /* keep compiler quiet */
201 * Generate plan for a UNION or UNION ALL node
204 generate_union_plan(SetOperationStmt *op, Query *parse,
205 List *refnames_tlist)
212 * If any of my children are identical UNION nodes (same op, all-flag,
213 * and colTypes) then they can be merged into this node so that we
214 * generate only one Append and Sort for the lot. Recurse to find
215 * such nodes and compute their children's plans.
217 planlist = nconc(recurse_union_children(op->larg, parse,
219 recurse_union_children(op->rarg, parse,
220 op, refnames_tlist));
223 * Generate tlist for Append plan node.
225 * The tlist for an Append plan isn't important as far as the Append is
226 * concerned, but we must make it look real anyway for the benefit of
227 * the next plan level up.
229 tlist = generate_append_tlist(op->colTypes, false,
230 planlist, refnames_tlist);
233 * Append the child results together.
235 plan = (Plan *) make_append(planlist, false, tlist);
238 * For UNION ALL, we just need the Append plan. For UNION, need to
239 * add Sort and Unique nodes to produce unique output.
245 tlist = new_unsorted_tlist(tlist);
246 sortList = addAllTargetsToSortList(NIL, tlist);
247 plan = make_sortplan(parse, tlist, plan, sortList);
248 plan = (Plan *) make_unique(tlist, plan, copyObject(sortList));
254 * Generate plan for an INTERSECT, INTERSECT ALL, EXCEPT, or EXCEPT ALL node
257 generate_nonunion_plan(SetOperationStmt *op, Query *parse,
258 List *refnames_tlist)
268 /* Recurse on children, ensuring their outputs are marked */
269 lplan = recurse_set_operations(op->larg, parse,
270 op->colTypes, false, 0,
272 rplan = recurse_set_operations(op->rarg, parse,
273 op->colTypes, false, 1,
275 planlist = makeList2(lplan, rplan);
278 * Generate tlist for Append plan node.
280 * The tlist for an Append plan isn't important as far as the Append is
281 * concerned, but we must make it look real anyway for the benefit of
282 * the next plan level up. In fact, it has to be real enough that the
283 * flag column is shown as a variable not a constant, else setrefs.c
286 tlist = generate_append_tlist(op->colTypes, true,
287 planlist, refnames_tlist);
290 * Append the child results together.
292 plan = (Plan *) make_append(planlist, false, tlist);
295 * Sort the child results, then add a SetOp plan node to generate the
298 tlist = new_unsorted_tlist(tlist);
299 sortList = addAllTargetsToSortList(NIL, tlist);
300 plan = make_sortplan(parse, tlist, plan, sortList);
303 case SETOP_INTERSECT:
304 cmd = op->all ? SETOPCMD_INTERSECT_ALL : SETOPCMD_INTERSECT;
307 cmd = op->all ? SETOPCMD_EXCEPT_ALL : SETOPCMD_EXCEPT;
310 elog(ERROR, "generate_nonunion_plan: bogus operation code");
311 cmd = SETOPCMD_INTERSECT; /* keep compiler quiet */
314 plan = (Plan *) make_setop(cmd, tlist, plan, sortList,
315 length(op->colTypes) + 1);
320 * Pull up children of a UNION node that are identically-propertied UNIONs.
322 * NOTE: we can also pull a UNION ALL up into a UNION, since the distinct
323 * output rows will be lost anyway.
326 recurse_union_children(Node *setOp, Query *parse,
327 SetOperationStmt *top_union,
328 List *refnames_tlist)
330 if (IsA(setOp, SetOperationStmt))
332 SetOperationStmt *op = (SetOperationStmt *) setOp;
334 if (op->op == top_union->op &&
335 (op->all == top_union->all || op->all) &&
336 equali(op->colTypes, top_union->colTypes))
338 /* Same UNION, so fold children into parent's subplan list */
339 return nconc(recurse_union_children(op->larg, parse,
342 recurse_union_children(op->rarg, parse,
349 * Not same, so plan this child separately.
351 * Note we disallow any resjunk columns in child results. This is
352 * necessary since the Append node that implements the union won't do
353 * any projection, and upper levels will get confused if some of our
354 * output tuples have junk and some don't. This case only arises when
355 * we have an EXCEPT or INTERSECT as child, else there won't be
358 return makeList1(recurse_set_operations(setOp, parse,
359 top_union->colTypes, false,
360 -1, refnames_tlist));
364 * Generate targetlist for a set-operation plan node
366 * colTypes: column datatypes for non-junk columns
367 * flag: -1 if no flag column needed, 0 or 1 to create a const flag column
368 * hack_constants: true to copy up constants (see comments in code)
369 * input_tlist: targetlist of this node's input node
370 * refnames_tlist: targetlist to take column names from
373 generate_setop_tlist(List *colTypes, int flag,
376 List *refnames_tlist)
386 Oid colType = (Oid) lfirsti(i);
387 TargetEntry *inputtle = (TargetEntry *) lfirst(input_tlist);
388 TargetEntry *reftle = (TargetEntry *) lfirst(refnames_tlist);
391 Assert(inputtle->resdom->resno == resno);
392 Assert(reftle->resdom->resno == resno);
393 Assert(!inputtle->resdom->resjunk);
394 Assert(!reftle->resdom->resjunk);
397 * Generate columns referencing input columns and having
398 * appropriate data types and column names. Insert datatype
399 * coercions where necessary.
401 * HACK: constants in the input's targetlist are copied up as-is
402 * rather than being referenced as subquery outputs. This is
403 * mainly to ensure that when we try to coerce them to the output
404 * column's datatype, the right things happen for UNKNOWN
405 * constants. But do this only at the first level of
406 * subquery-scan plans; we don't want phony constants appearing in
407 * the output tlists of upper-level nodes!
409 if (hack_constants && inputtle->expr && IsA(inputtle->expr, Const))
410 expr = (Node *) inputtle->expr;
412 expr = (Node *) makeVar(0,
413 inputtle->resdom->resno,
414 inputtle->resdom->restype,
415 inputtle->resdom->restypmod,
417 if (inputtle->resdom->restype == colType)
419 /* no coercion needed, and believe the input typmod */
420 colTypmod = inputtle->resdom->restypmod;
424 expr = coerce_to_common_type(expr,
426 "UNION/INTERSECT/EXCEPT");
429 resdom = makeResdom((AttrNumber) resno++,
432 pstrdup(reftle->resdom->resname),
434 tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
435 input_tlist = lnext(input_tlist);
436 refnames_tlist = lnext(refnames_tlist);
441 /* Add a resjunk flag column */
442 resdom = makeResdom((AttrNumber) resno++,
447 /* flag value is the given constant */
448 expr = (Node *) makeConst(INT4OID,
453 tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
460 * Generate targetlist for a set-operation Append node
462 * colTypes: column datatypes for non-junk columns
463 * flag: true to create a flag column copied up from subplans
464 * input_plans: list of sub-plans of the Append
465 * refnames_tlist: targetlist to take column names from
467 * The entries in the Append's targetlist should always be simple Vars;
468 * we just have to make sure they have the right datatypes and typmods.
471 generate_append_tlist(List *colTypes, bool flag,
473 List *refnames_tlist)
485 * First extract typmods to use.
487 * If the inputs all agree on type and typmod of a particular column, use
488 * that typmod; else use -1.
490 colTypmods = (int32 *) palloc(length(colTypes) * sizeof(int32));
492 foreach(planl, input_plans)
494 Plan *subplan = (Plan *) lfirst(planl);
497 curColType = colTypes;
499 foreach(subtlist, subplan->targetlist)
501 TargetEntry *subtle = (TargetEntry *) lfirst(subtlist);
503 if (subtle->resdom->resjunk)
505 Assert(curColType != NIL);
506 if (subtle->resdom->restype == (Oid) lfirsti(curColType))
508 /* If first subplan, copy the typmod; else compare */
509 if (planl == input_plans)
510 colTypmods[colindex] = subtle->resdom->restypmod;
511 else if (subtle->resdom->restypmod != colTypmods[colindex])
512 colTypmods[colindex] = -1;
516 /* types disagree, so force typmod to -1 */
517 colTypmods[colindex] = -1;
519 curColType = lnext(curColType);
522 Assert(curColType == NIL);
526 * Now we can build the tlist for the Append.
529 foreach(curColType, colTypes)
531 Oid colType = (Oid) lfirsti(curColType);
532 int32 colTypmod = colTypmods[colindex++];
533 TargetEntry *reftle = (TargetEntry *) lfirst(refnames_tlist);
535 Assert(reftle->resdom->resno == resno);
536 Assert(!reftle->resdom->resjunk);
537 expr = (Node *) makeVar(0,
542 resdom = makeResdom((AttrNumber) resno++,
545 pstrdup(reftle->resdom->resname),
547 tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
548 refnames_tlist = lnext(refnames_tlist);
553 /* Add a resjunk flag column */
554 resdom = makeResdom((AttrNumber) resno++,
559 /* flag value is shown as copied up from subplan */
560 expr = (Node *) makeVar(0,
565 tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr));
574 * Does tlist have same datatypes as requested colTypes?
576 * Resjunk columns are ignored if junkOK is true; otherwise presence of
577 * a resjunk column will always cause a 'false' result.
580 tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
586 TargetEntry *tle = (TargetEntry *) lfirst(i);
588 if (tle->resdom->resjunk)
597 if (tle->resdom->restype != (Oid) lfirsti(colTypes))
599 colTypes = lnext(colTypes);
609 * find_all_inheritors -
610 * Returns an integer list of relids including the given rel plus
611 * all relations that inherit from it, directly or indirectly.
614 find_all_inheritors(Oid parentrel)
616 List *examined_relids = NIL;
617 List *unexamined_relids = makeListi1(parentrel);
620 * While the queue of unexamined relids is nonempty, remove the first
621 * element, mark it examined, and find its direct descendants. NB:
622 * cannot use foreach(), since we modify the queue inside loop.
624 while (unexamined_relids != NIL)
626 Oid currentrel = lfirsti(unexamined_relids);
627 List *currentchildren;
629 unexamined_relids = lnext(unexamined_relids);
630 examined_relids = lappendi(examined_relids, currentrel);
631 currentchildren = find_inheritance_children(currentrel);
634 * Add to the queue only those children not already seen. This
635 * avoids making duplicate entries in case of multiple inheritance
636 * paths from the same parent. (It'll also keep us from getting
637 * into an infinite loop, though theoretically there can't be any
638 * cycles in the inheritance graph anyway.)
640 currentchildren = set_differencei(currentchildren, examined_relids);
641 unexamined_relids = set_unioni(unexamined_relids, currentchildren);
644 return examined_relids;
648 * expand_inherted_rtentry
649 * Check whether a rangetable entry represents an inheritance set.
650 * If so, add entries for all the child tables to the query's
651 * rangetable, and return an integer list of RT indexes for the
652 * whole inheritance set (parent and children).
653 * If not, return NIL.
655 * When dup_parent is false, the initially given RT index is part of the
656 * returned list (if any). When dup_parent is true, the given RT index
657 * is *not* in the returned list; a duplicate RTE will be made for the
660 * A childless table is never considered to be an inheritance set; therefore
661 * the result will never be a one-element list. It'll be either empty
662 * or have two or more elements.
664 * NOTE: after this routine executes, the specified RTE will always have
665 * its inh flag cleared, whether or not there were any children. This
666 * ensures we won't expand the same RTE twice, which would otherwise occur
667 * for the case of an inherited UPDATE/DELETE target relation.
670 expand_inherted_rtentry(Query *parse, Index rti, bool dup_parent)
672 RangeTblEntry *rte = rt_fetch(rti, parse->rtable);
678 /* Does RT entry allow inheritance? */
681 Assert(rte->rtekind == RTE_RELATION);
682 /* Always clear the parent's inh flag, see above comments */
684 /* Fast path for common case of childless table */
685 parentOID = rte->relid;
686 if (!has_subclass(parentOID))
688 /* Scan for all members of inheritance set */
689 inhOIDs = find_all_inheritors(parentOID);
692 * Check that there's at least one descendant, else treat as no-child
693 * case. This could happen despite above has_subclass() check, if
694 * table once had a child but no longer does.
696 if (lnext(inhOIDs) == NIL)
698 /* OK, it's an inheritance set; expand it */
702 inhRTIs = makeListi1(rti); /* include original RTE in result */
706 Oid childOID = (Oid) lfirsti(l);
707 RangeTblEntry *childrte;
710 /* parent will be in the list too; skip it if not dup requested */
711 if (childOID == parentOID && !dup_parent)
715 * Build an RTE for the child, and attach to query's rangetable
716 * list. We copy most fields of the parent's RTE, but replace
717 * relation real name and OID. Note that inh will be false at
720 childrte = copyObject(rte);
721 childrte->relid = childOID;
722 parse->rtable = lappend(parse->rtable, childrte);
723 childRTindex = length(parse->rtable);
725 inhRTIs = lappendi(inhRTIs, childRTindex);
732 * adjust_inherited_attrs
733 * Copy the specified query or expression and translate Vars referring
734 * to old_rt_index to refer to new_rt_index.
736 * We also adjust varattno to match the new table by column name, rather
737 * than column number. This hack makes it possible for child tables to have
738 * different column positions for the "same" attribute as a parent, which
739 * helps ALTER TABLE ADD COLUMN. Unfortunately this isn't nearly enough to
740 * make it work transparently; there are other places where things fall down
741 * if children and parents don't have the same column numbers for inherited
742 * attributes. It'd be better to rip this code out and fix ALTER TABLE...
745 adjust_inherited_attrs(Node *node,
746 Index old_rt_index, Oid old_relid,
747 Index new_rt_index, Oid new_relid)
749 adjust_inherited_attrs_context context;
751 /* Handle simple case simply... */
752 if (old_rt_index == new_rt_index)
754 Assert(old_relid == new_relid);
755 return copyObject(node);
758 context.old_rt_index = old_rt_index;
759 context.new_rt_index = new_rt_index;
760 context.old_relid = old_relid;
761 context.new_relid = new_relid;
764 * Must be prepared to start with a Query or a bare expression tree.
766 if (node && IsA(node, Query))
768 Query *query = (Query *) node;
771 FLATCOPY(newnode, query, Query);
772 query_tree_mutator(newnode, adjust_inherited_attrs_mutator,
773 (void *) &context, QTW_IGNORE_SUBQUERIES);
774 if (newnode->resultRelation == old_rt_index)
776 newnode->resultRelation = new_rt_index;
777 /* Fix tlist resnos too, if it's inherited UPDATE */
778 if (newnode->commandType == CMD_UPDATE)
779 newnode->targetList =
780 adjust_inherited_tlist(newnode->targetList,
783 return (Node *) newnode;
786 return adjust_inherited_attrs_mutator(node, &context);
790 adjust_inherited_attrs_mutator(Node *node,
791 adjust_inherited_attrs_context *context)
797 Var *var = (Var *) copyObject(node);
799 if (var->varlevelsup == 0 &&
800 var->varno == context->old_rt_index)
802 var->varno = context->new_rt_index;
803 if (var->varattno > 0)
805 char *attname = get_attname(context->old_relid,
808 var->varattno = get_attnum(context->new_relid, attname);
809 if (var->varattno == InvalidAttrNumber)
810 elog(ERROR, "Relation \"%s\" has no column \"%s\"",
811 get_rel_name(context->new_relid), attname);
817 if (IsA(node, RangeTblRef))
819 RangeTblRef *rtr = (RangeTblRef *) copyObject(node);
821 if (rtr->rtindex == context->old_rt_index)
822 rtr->rtindex = context->new_rt_index;
825 if (IsA(node, JoinExpr))
827 /* Copy the JoinExpr node with correct mutation of subnodes */
830 j = (JoinExpr *) expression_tree_mutator(node,
831 adjust_inherited_attrs_mutator,
833 /* now fix JoinExpr's rtindex */
834 if (j->rtindex == context->old_rt_index)
835 j->rtindex = context->new_rt_index;
840 * We have to process RestrictInfo nodes specially: we do NOT want to
841 * copy the original subclauseindices list, since the new rel may have
842 * different indices. The list will be rebuilt during later planning.
844 if (IsA(node, RestrictInfo))
846 RestrictInfo *oldinfo = (RestrictInfo *) node;
847 RestrictInfo *newinfo = makeNode(RestrictInfo);
849 /* Copy all flat-copiable fields */
850 memcpy(newinfo, oldinfo, sizeof(RestrictInfo));
852 newinfo->clause = (Expr *)
853 adjust_inherited_attrs_mutator((Node *) oldinfo->clause, context);
855 newinfo->subclauseindices = NIL;
856 newinfo->eval_cost = -1; /* reset these too */
857 newinfo->this_selec = -1;
858 newinfo->left_pathkey = NIL; /* and these */
859 newinfo->right_pathkey = NIL;
860 newinfo->left_mergescansel = -1;
861 newinfo->right_mergescansel = -1;
862 newinfo->left_bucketsize = -1;
863 newinfo->right_bucketsize = -1;
865 return (Node *) newinfo;
869 * NOTE: we do not need to recurse into sublinks, because they should
870 * already have been converted to subplans before we see them.
874 * BUT: although we don't need to recurse into subplans, we do need to
875 * make sure that they are copied, not just referenced as
876 * expression_tree_mutator will do by default. Otherwise we'll have
877 * the same subplan node referenced from each arm of the inheritance
878 * APPEND plan, which will cause trouble in the executor. This is a
879 * kluge that should go away when we redesign querytrees.
881 if (is_subplan(node))
885 /* Copy the node and process subplan args */
886 node = expression_tree_mutator(node, adjust_inherited_attrs_mutator,
888 /* Make sure we have separate copies of subplan and its rtable */
889 subplan = (SubPlan *) node;
890 subplan->plan = copyObject(subplan->plan);
891 subplan->rtable = copyObject(subplan->rtable);
895 return expression_tree_mutator(node, adjust_inherited_attrs_mutator,
900 * Adjust the targetlist entries of an inherited UPDATE operation
902 * The expressions have already been fixed, but we have to make sure that
903 * the target resnos match the child table (they may not, in the case of
904 * a column that was added after-the-fact by ALTER TABLE). In some cases
905 * this can force us to re-order the tlist to preserve resno ordering.
906 * (We do all this work in special cases so that preptlist.c is fast for
909 * The given tlist has already been through expression_tree_mutator;
910 * therefore the TargetEntry nodes are fresh copies that it's okay to
911 * scribble on. But the Resdom nodes have not been copied; make new ones
912 * if we need to change them!
914 * Note that this is not needed for INSERT because INSERT isn't inheritable.
917 adjust_inherited_tlist(List *tlist, Oid new_relid)
919 bool changed_it = false;
925 /* Scan tlist and update resnos to match attnums of new_relid */
928 TargetEntry *tle = (TargetEntry *) lfirst(tl);
929 Resdom *resdom = tle->resdom;
932 continue; /* ignore junk items */
934 attrno = get_attnum(new_relid, resdom->resname);
935 if (attrno == InvalidAttrNumber)
936 elog(ERROR, "Relation \"%s\" has no column \"%s\"",
937 get_rel_name(new_relid), resdom->resname);
938 if (resdom->resno != attrno)
940 resdom = (Resdom *) copyObject((Node *) resdom);
941 resdom->resno = attrno;
942 tle->resdom = resdom;
948 * If we changed anything, re-sort the tlist by resno, and make sure
949 * resjunk entries have resnos above the last real resno. The sort
950 * algorithm is a bit stupid, but for such a seldom-taken path, small
951 * is probably better than fast.
958 for (attrno = 1; more; attrno++)
963 TargetEntry *tle = (TargetEntry *) lfirst(tl);
964 Resdom *resdom = tle->resdom;
967 continue; /* ignore junk items */
969 if (resdom->resno == attrno)
970 new_tlist = lappend(new_tlist, tle);
971 else if (resdom->resno > attrno)
978 TargetEntry *tle = (TargetEntry *) lfirst(tl);
979 Resdom *resdom = tle->resdom;
981 if (!resdom->resjunk)
982 continue; /* here, ignore non-junk items */
984 if (resdom->resno != attrno)
986 resdom = (Resdom *) copyObject((Node *) resdom);
987 resdom->resno = attrno;
988 tle->resdom = resdom;
990 new_tlist = lappend(new_tlist, tle);