1 /*-------------------------------------------------------------------------
4 * transform the raw parse tree into a query tree
6 * For optimizable statements, we are careful to obtain a suitable lock on
7 * each referenced table, and other modules of the backend preserve or
8 * re-obtain these locks before depending on the results. It is therefore
9 * okay to do significant semantic analysis of these statements. For
10 * utility commands, no locks are obtained here (and if they were, we could
11 * not be sure we'd still have them at execution). Hence the general rule
12 * for utility commands is to just dump them into a Query node untransformed.
13 * DECLARE CURSOR and EXPLAIN are exceptions because they contain
14 * optimizable statements.
17 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
18 * Portions Copyright (c) 1994, Regents of the University of California
20 * $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.391 2009/09/09 03:32:52 tgl Exp $
22 *-------------------------------------------------------------------------
27 #include "access/sysattr.h"
28 #include "catalog/pg_type.h"
29 #include "nodes/makefuncs.h"
30 #include "nodes/nodeFuncs.h"
31 #include "optimizer/var.h"
32 #include "parser/analyze.h"
33 #include "parser/parse_agg.h"
34 #include "parser/parse_clause.h"
35 #include "parser/parse_coerce.h"
36 #include "parser/parse_cte.h"
37 #include "parser/parse_oper.h"
38 #include "parser/parse_relation.h"
39 #include "parser/parse_target.h"
40 #include "parser/parsetree.h"
41 #include "rewrite/rewriteManip.h"
42 #include "utils/rel.h"
45 static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
46 static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
47 static List *transformInsertRow(ParseState *pstate, List *exprlist,
48 List *stmtcols, List *icolumns, List *attrnos);
49 static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
50 static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
51 static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
52 static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
53 bool isTopLevel, List **colInfo);
54 static void determineRecursiveColTypes(ParseState *pstate,
55 Node *larg, List *lcolinfo);
56 static void applyColumnNames(List *dst, List *src);
57 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
58 static List *transformReturningList(ParseState *pstate, List *returningList);
59 static Query *transformDeclareCursorStmt(ParseState *pstate,
60 DeclareCursorStmt *stmt);
61 static Query *transformExplainStmt(ParseState *pstate,
63 static void transformLockingClause(ParseState *pstate,
64 Query *qry, LockingClause *lc);
65 static bool check_parameter_resolution_walker(Node *node, ParseState *pstate);
70 * Analyze a raw parse tree and transform it to Query form.
72 * Optionally, information about $n parameter types can be supplied.
73 * References to $n indexes not defined by paramTypes[] are disallowed.
75 * The result is a Query node. Optimizable statements require considerable
76 * transformation, while utility-type statements are simply hung off
77 * a dummy CMD_UTILITY Query node.
80 parse_analyze(Node *parseTree, const char *sourceText,
81 Oid *paramTypes, int numParams)
83 ParseState *pstate = make_parsestate(NULL);
86 Assert(sourceText != NULL); /* required as of 8.4 */
88 pstate->p_sourcetext = sourceText;
89 pstate->p_paramtypes = paramTypes;
90 pstate->p_numparams = numParams;
91 pstate->p_variableparams = false;
93 query = transformStmt(pstate, parseTree);
95 free_parsestate(pstate);
101 * parse_analyze_varparams
103 * This variant is used when it's okay to deduce information about $n
104 * symbol datatypes from context. The passed-in paramTypes[] array can
105 * be modified or enlarged (via repalloc).
108 parse_analyze_varparams(Node *parseTree, const char *sourceText,
109 Oid **paramTypes, int *numParams)
111 ParseState *pstate = make_parsestate(NULL);
114 Assert(sourceText != NULL); /* required as of 8.4 */
116 pstate->p_sourcetext = sourceText;
117 pstate->p_paramtypes = *paramTypes;
118 pstate->p_numparams = *numParams;
119 pstate->p_variableparams = true;
121 query = transformStmt(pstate, parseTree);
123 /* make sure all is well with parameter types */
124 if (pstate->p_numparams > 0)
125 check_parameter_resolution_walker((Node *) query, pstate);
127 *paramTypes = pstate->p_paramtypes;
128 *numParams = pstate->p_numparams;
130 free_parsestate(pstate);
137 * Entry point for recursively analyzing a sub-statement.
140 parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
141 CommonTableExpr *parentCTE)
143 ParseState *pstate = make_parsestate(parentParseState);
146 pstate->p_parent_cte = parentCTE;
148 query = transformStmt(pstate, parseTree);
150 free_parsestate(pstate);
157 * transform a Parse tree into a Query tree.
160 transformStmt(ParseState *pstate, Node *parseTree)
164 switch (nodeTag(parseTree))
167 * Optimizable statements
170 result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
174 result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
178 result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
183 SelectStmt *n = (SelectStmt *) parseTree;
186 result = transformValuesClause(pstate, n);
187 else if (n->op == SETOP_NONE)
188 result = transformSelectStmt(pstate, n);
190 result = transformSetOperationStmt(pstate, n);
197 case T_DeclareCursorStmt:
198 result = transformDeclareCursorStmt(pstate,
199 (DeclareCursorStmt *) parseTree);
203 result = transformExplainStmt(pstate,
204 (ExplainStmt *) parseTree);
210 * other statements don't require any transformation; just return
211 * the original parsetree with a Query node plastered on top.
213 result = makeNode(Query);
214 result->commandType = CMD_UTILITY;
215 result->utilityStmt = (Node *) parseTree;
219 /* Mark as original query until we learn differently */
220 result->querySource = QSRC_ORIGINAL;
221 result->canSetTag = true;
227 * analyze_requires_snapshot
228 * Returns true if a snapshot must be set before doing parse analysis
229 * on the given raw parse tree.
231 * Classification here should match transformStmt(); but we also have to
232 * allow a NULL input (for Parse/Bind of an empty query string).
235 analyze_requires_snapshot(Node *parseTree)
239 if (parseTree == NULL)
242 switch (nodeTag(parseTree))
245 * Optimizable statements
257 case T_DeclareCursorStmt:
258 /* yes, because it's analyzed just like SELECT */
265 * We only need a snapshot in varparams case, but it doesn't seem
266 * worth complicating this function's API to distinguish that.
272 /* utility statements don't have any active parse analysis */
281 * transformDeleteStmt -
282 * transforms a Delete Statement
285 transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
287 Query *qry = makeNode(Query);
290 qry->commandType = CMD_DELETE;
292 /* set up range table with just the result rel */
293 qry->resultRelation = setTargetTable(pstate, stmt->relation,
294 interpretInhOption(stmt->relation->inhOpt),
298 qry->distinctClause = NIL;
301 * The USING clause is non-standard SQL syntax, and is equivalent in
302 * functionality to the FROM list that can be specified for UPDATE. The
303 * USING keyword is used rather than FROM because FROM is already a
304 * keyword in the DELETE syntax.
306 transformFromClause(pstate, stmt->usingClause);
308 qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
310 qry->returningList = transformReturningList(pstate, stmt->returningList);
312 /* done building the range table and jointree */
313 qry->rtable = pstate->p_rtable;
314 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
316 qry->hasSubLinks = pstate->p_hasSubLinks;
317 qry->hasAggs = pstate->p_hasAggs;
318 if (pstate->p_hasAggs)
319 parseCheckAggregates(pstate, qry);
320 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
321 if (pstate->p_hasWindowFuncs)
322 parseCheckWindowFuncs(pstate, qry);
328 * transformInsertStmt -
329 * transform an Insert Statement
332 transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
334 Query *qry = makeNode(Query);
335 SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
336 List *exprList = NIL;
337 bool isGeneralSelect;
339 List *sub_relnamespace;
340 List *sub_varnamespace;
349 qry->commandType = CMD_INSERT;
350 pstate->p_is_insert = true;
353 * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
354 * VALUES list, or general SELECT input. We special-case VALUES, both for
355 * efficiency and so we can handle DEFAULT specifications.
357 isGeneralSelect = (selectStmt && selectStmt->valuesLists == NIL);
360 * If a non-nil rangetable/namespace was passed in, and we are doing
361 * INSERT/SELECT, arrange to pass the rangetable/namespace down to the
362 * SELECT. This can only happen if we are inside a CREATE RULE, and in
363 * that case we want the rule's OLD and NEW rtable entries to appear as
364 * part of the SELECT's rtable, not as outer references for it. (Kluge!)
365 * The SELECT's joinlist is not affected however. We must do this before
366 * adding the target table to the INSERT's rtable.
370 sub_rtable = pstate->p_rtable;
371 pstate->p_rtable = NIL;
372 sub_relnamespace = pstate->p_relnamespace;
373 pstate->p_relnamespace = NIL;
374 sub_varnamespace = pstate->p_varnamespace;
375 pstate->p_varnamespace = NIL;
376 /* There can't be any outer WITH to worry about */
377 Assert(pstate->p_ctenamespace == NIL);
381 sub_rtable = NIL; /* not used, but keep compiler quiet */
382 sub_relnamespace = NIL;
383 sub_varnamespace = NIL;
387 * Must get write lock on INSERT target table before scanning SELECT, else
388 * we will grab the wrong kind of initial lock if the target table is also
389 * mentioned in the SELECT part. Note that the target table is not added
390 * to the joinlist or namespace.
392 qry->resultRelation = setTargetTable(pstate, stmt->relation,
393 false, false, ACL_INSERT);
395 /* Validate stmt->cols list, or build default list if no list given */
396 icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
397 Assert(list_length(icolumns) == list_length(attrnos));
400 * Determine which variant of INSERT we have.
402 if (selectStmt == NULL)
405 * We have INSERT ... DEFAULT VALUES. We can handle this case by
406 * emitting an empty targetlist --- all columns will be defaulted when
407 * the planner expands the targetlist.
411 else if (isGeneralSelect)
414 * We make the sub-pstate a child of the outer pstate so that it can
415 * see any Param definitions supplied from above. Since the outer
416 * pstate's rtable and namespace are presently empty, there are no
417 * side-effects of exposing names the sub-SELECT shouldn't be able to
420 ParseState *sub_pstate = make_parsestate(pstate);
424 * Process the source SELECT.
426 * It is important that this be handled just like a standalone SELECT;
427 * otherwise the behavior of SELECT within INSERT might be different
428 * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
429 * bugs of just that nature...)
431 sub_pstate->p_rtable = sub_rtable;
432 sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
433 sub_pstate->p_relnamespace = sub_relnamespace;
434 sub_pstate->p_varnamespace = sub_varnamespace;
436 selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
438 free_parsestate(sub_pstate);
440 /* The grammar should have produced a SELECT, but it might have INTO */
441 if (!IsA(selectQuery, Query) ||
442 selectQuery->commandType != CMD_SELECT ||
443 selectQuery->utilityStmt != NULL)
444 elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
445 if (selectQuery->intoClause)
447 (errcode(ERRCODE_SYNTAX_ERROR),
448 errmsg("INSERT ... SELECT cannot specify INTO"),
449 parser_errposition(pstate,
450 exprLocation((Node *) selectQuery->intoClause))));
453 * Make the source be a subquery in the INSERT's rangetable, and add
454 * it to the INSERT's joinlist.
456 rte = addRangeTableEntryForSubquery(pstate,
458 makeAlias("*SELECT*", NIL),
460 rtr = makeNode(RangeTblRef);
461 /* assume new rte is at end */
462 rtr->rtindex = list_length(pstate->p_rtable);
463 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
464 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
467 * Generate an expression list for the INSERT that selects all the
468 * non-resjunk columns from the subquery. (INSERT's tlist must be
469 * separate from the subquery's tlist because we may add columns,
470 * insert datatype coercions, etc.)
472 * HACK: unknown-type constants and params in the SELECT's targetlist
473 * are copied up as-is rather than being referenced as subquery
474 * outputs. This is to ensure that when we try to coerce them to
475 * the target column's datatype, the right things happen (see
476 * special cases in coerce_type). Otherwise, this fails:
477 * INSERT INTO foo SELECT 'bar', ... FROM baz
481 foreach(lc, selectQuery->targetList)
483 TargetEntry *tle = (TargetEntry *) lfirst(lc);
489 (IsA(tle->expr, Const) ||IsA(tle->expr, Param)) &&
490 exprType((Node *) tle->expr) == UNKNOWNOID)
494 Var *var = makeVar(rtr->rtindex,
496 exprType((Node *) tle->expr),
497 exprTypmod((Node *) tle->expr),
500 var->location = exprLocation((Node *) tle->expr);
503 exprList = lappend(exprList, expr);
506 /* Prepare row for assignment to target table */
507 exprList = transformInsertRow(pstate, exprList,
511 else if (list_length(selectStmt->valuesLists) > 1)
514 * Process INSERT ... VALUES with multiple VALUES sublists. We
515 * generate a VALUES RTE holding the transformed expression lists, and
516 * build up a targetlist containing Vars that reference the VALUES
519 List *exprsLists = NIL;
520 int sublist_length = -1;
522 /* process the WITH clause */
523 if (selectStmt->withClause)
525 qry->hasRecursive = selectStmt->withClause->recursive;
526 qry->cteList = transformWithClause(pstate, selectStmt->withClause);
529 foreach(lc, selectStmt->valuesLists)
531 List *sublist = (List *) lfirst(lc);
533 /* Do basic expression transformation (same as a ROW() expr) */
534 sublist = transformExpressionList(pstate, sublist);
537 * All the sublists must be the same length, *after*
538 * transformation (which might expand '*' into multiple items).
539 * The VALUES RTE can't handle anything different.
541 if (sublist_length < 0)
543 /* Remember post-transformation length of first sublist */
544 sublist_length = list_length(sublist);
546 else if (sublist_length != list_length(sublist))
549 (errcode(ERRCODE_SYNTAX_ERROR),
550 errmsg("VALUES lists must all be the same length"),
551 parser_errposition(pstate,
552 exprLocation((Node *) sublist))));
555 /* Prepare row for assignment to target table */
556 sublist = transformInsertRow(pstate, sublist,
560 exprsLists = lappend(exprsLists, sublist);
564 * There mustn't have been any table references in the expressions,
565 * else strange things would happen, like Cartesian products of those
566 * tables with the VALUES list ...
568 if (pstate->p_joinlist != NIL)
570 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
571 errmsg("VALUES must not contain table references"),
572 parser_errposition(pstate,
573 locate_var_of_level((Node *) exprsLists, 0))));
576 * Another thing we can't currently support is NEW/OLD references in
577 * rules --- seems we'd need something like SQL99's LATERAL construct
578 * to ensure that the values would be available while evaluating the
579 * VALUES RTE. This is a shame. FIXME
581 if (list_length(pstate->p_rtable) != 1 &&
582 contain_vars_of_level((Node *) exprsLists, 0))
584 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
585 errmsg("VALUES must not contain OLD or NEW references"),
586 errhint("Use SELECT ... UNION ALL ... instead."),
587 parser_errposition(pstate,
588 locate_var_of_level((Node *) exprsLists, 0))));
591 * Generate the VALUES RTE
593 rte = addRangeTableEntryForValues(pstate, exprsLists, NULL, true);
594 rtr = makeNode(RangeTblRef);
595 /* assume new rte is at end */
596 rtr->rtindex = list_length(pstate->p_rtable);
597 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
598 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
601 * Generate list of Vars referencing the RTE
603 expandRTE(rte, rtr->rtindex, 0, -1, false, NULL, &exprList);
608 * Process INSERT ... VALUES with a single VALUES sublist.
609 * We treat this separately for efficiency and for historical
610 * compatibility --- specifically, allowing table references,
612 * INSERT INTO foo VALUES(bar.*)
614 * The sublist is just computed directly as the Query's targetlist,
615 * with no VALUES RTE. So it works just like SELECT without FROM.
618 List *valuesLists = selectStmt->valuesLists;
620 Assert(list_length(valuesLists) == 1);
622 /* process the WITH clause */
623 if (selectStmt->withClause)
625 qry->hasRecursive = selectStmt->withClause->recursive;
626 qry->cteList = transformWithClause(pstate, selectStmt->withClause);
629 /* Do basic expression transformation (same as a ROW() expr) */
630 exprList = transformExpressionList(pstate,
631 (List *) linitial(valuesLists));
633 /* Prepare row for assignment to target table */
634 exprList = transformInsertRow(pstate, exprList,
640 * Generate query's target list using the computed list of expressions.
641 * Also, mark all the target columns as needing insert permissions.
643 rte = pstate->p_target_rangetblentry;
644 qry->targetList = NIL;
645 icols = list_head(icolumns);
646 attnos = list_head(attrnos);
647 foreach(lc, exprList)
649 Expr *expr = (Expr *) lfirst(lc);
654 col = (ResTarget *) lfirst(icols);
655 Assert(IsA(col, ResTarget));
656 attr_num = (AttrNumber) lfirst_int(attnos);
658 tle = makeTargetEntry(expr,
662 qry->targetList = lappend(qry->targetList, tle);
664 rte->modifiedCols = bms_add_member(rte->modifiedCols,
665 attr_num - FirstLowInvalidHeapAttributeNumber);
667 icols = lnext(icols);
668 attnos = lnext(attnos);
672 * If we have a RETURNING clause, we need to add the target relation to
673 * the query namespace before processing it, so that Var references in
674 * RETURNING will work. Also, remove any namespace entries added in a
675 * sub-SELECT or VALUES list.
677 if (stmt->returningList)
679 pstate->p_relnamespace = NIL;
680 pstate->p_varnamespace = NIL;
681 addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
683 qry->returningList = transformReturningList(pstate,
684 stmt->returningList);
687 /* done building the range table and jointree */
688 qry->rtable = pstate->p_rtable;
689 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
691 qry->hasSubLinks = pstate->p_hasSubLinks;
692 /* aggregates not allowed (but subselects are okay) */
693 if (pstate->p_hasAggs)
695 (errcode(ERRCODE_GROUPING_ERROR),
696 errmsg("cannot use aggregate function in VALUES"),
697 parser_errposition(pstate,
698 locate_agg_of_level((Node *) qry, 0))));
699 if (pstate->p_hasWindowFuncs)
701 (errcode(ERRCODE_WINDOWING_ERROR),
702 errmsg("cannot use window function in VALUES"),
703 parser_errposition(pstate,
704 locate_windowfunc((Node *) qry))));
710 * Prepare an INSERT row for assignment to the target table.
712 * The row might be either a VALUES row, or variables referencing a
716 transformInsertRow(ParseState *pstate, List *exprlist,
717 List *stmtcols, List *icolumns, List *attrnos)
725 * Check length of expr list. It must not have more expressions than
726 * there are target columns. We allow fewer, but only if no explicit
727 * columns list was given (the remaining columns are implicitly
728 * defaulted). Note we must check this *after* transformation because
729 * that could expand '*' into multiple items.
731 if (list_length(exprlist) > list_length(icolumns))
733 (errcode(ERRCODE_SYNTAX_ERROR),
734 errmsg("INSERT has more expressions than target columns"),
735 parser_errposition(pstate,
736 exprLocation(list_nth(exprlist,
737 list_length(icolumns))))));
738 if (stmtcols != NIL &&
739 list_length(exprlist) < list_length(icolumns))
741 (errcode(ERRCODE_SYNTAX_ERROR),
742 errmsg("INSERT has more target columns than expressions"),
743 parser_errposition(pstate,
744 exprLocation(list_nth(icolumns,
745 list_length(exprlist))))));
748 * Prepare columns for assignment to target table.
751 icols = list_head(icolumns);
752 attnos = list_head(attrnos);
753 foreach(lc, exprlist)
755 Expr *expr = (Expr *) lfirst(lc);
758 col = (ResTarget *) lfirst(icols);
759 Assert(IsA(col, ResTarget));
761 expr = transformAssignedExpr(pstate, expr,
767 result = lappend(result, expr);
769 icols = lnext(icols);
770 attnos = lnext(attnos);
778 * transformSelectStmt -
779 * transforms a Select Statement
781 * Note: this covers only cases with no set operations and no VALUES lists;
782 * see below for the other cases.
785 transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
787 Query *qry = makeNode(Query);
791 qry->commandType = CMD_SELECT;
793 /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
794 pstate->p_locking_clause = stmt->lockingClause;
796 /* make WINDOW info available for window functions, too */
797 pstate->p_windowdefs = stmt->windowClause;
799 /* process the WITH clause */
800 if (stmt->withClause)
802 qry->hasRecursive = stmt->withClause->recursive;
803 qry->cteList = transformWithClause(pstate, stmt->withClause);
806 /* process the FROM clause */
807 transformFromClause(pstate, stmt->fromClause);
809 /* transform targetlist */
810 qry->targetList = transformTargetList(pstate, stmt->targetList);
812 /* mark column origins */
813 markTargetListOrigins(pstate, qry->targetList);
815 /* transform WHERE */
816 qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
819 * Initial processing of HAVING clause is just like WHERE clause.
821 qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
825 * Transform sorting/grouping stuff. Do ORDER BY first because both
826 * transformGroupClause and transformDistinctClause need the results. Note
827 * that these functions can also change the targetList, so it's passed to
830 qry->sortClause = transformSortClause(pstate,
833 true /* fix unknowns */,
834 false /* not window function */);
836 qry->groupClause = transformGroupClause(pstate,
840 false /* not window function */);
842 if (stmt->distinctClause == NIL)
844 qry->distinctClause = NIL;
845 qry->hasDistinctOn = false;
847 else if (linitial(stmt->distinctClause) == NULL)
849 /* We had SELECT DISTINCT */
850 qry->distinctClause = transformDistinctClause(pstate,
853 qry->hasDistinctOn = false;
857 /* We had SELECT DISTINCT ON */
858 qry->distinctClause = transformDistinctOnClause(pstate,
859 stmt->distinctClause,
862 qry->hasDistinctOn = true;
865 /* transform LIMIT */
866 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
868 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
871 /* transform window clauses after we have seen all window functions */
872 qry->windowClause = transformWindowDefinitions(pstate,
873 pstate->p_windowdefs,
876 /* handle any SELECT INTO/CREATE TABLE AS spec */
877 if (stmt->intoClause)
879 qry->intoClause = stmt->intoClause;
880 if (stmt->intoClause->colNames)
881 applyColumnNames(qry->targetList, stmt->intoClause->colNames);
884 qry->rtable = pstate->p_rtable;
885 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
887 qry->hasSubLinks = pstate->p_hasSubLinks;
888 qry->hasAggs = pstate->p_hasAggs;
889 if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
890 parseCheckAggregates(pstate, qry);
891 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
892 if (pstate->p_hasWindowFuncs)
893 parseCheckWindowFuncs(pstate, qry);
895 foreach(l, stmt->lockingClause)
897 transformLockingClause(pstate, qry, (LockingClause *) lfirst(l));
904 * transformValuesClause -
905 * transforms a VALUES clause that's being used as a standalone SELECT
907 * We build a Query containing a VALUES RTE, rather as if one had written
908 * SELECT * FROM (VALUES ...)
911 transformValuesClause(ParseState *pstate, SelectStmt *stmt)
913 Query *qry = makeNode(Query);
914 List *exprsLists = NIL;
915 List **colexprs = NULL;
916 Oid *coltypes = NULL;
917 int sublist_length = -1;
925 qry->commandType = CMD_SELECT;
927 /* Most SELECT stuff doesn't apply in a VALUES clause */
928 Assert(stmt->distinctClause == NIL);
929 Assert(stmt->targetList == NIL);
930 Assert(stmt->fromClause == NIL);
931 Assert(stmt->whereClause == NULL);
932 Assert(stmt->groupClause == NIL);
933 Assert(stmt->havingClause == NULL);
934 Assert(stmt->windowClause == NIL);
935 Assert(stmt->op == SETOP_NONE);
937 /* process the WITH clause */
938 if (stmt->withClause)
940 qry->hasRecursive = stmt->withClause->recursive;
941 qry->cteList = transformWithClause(pstate, stmt->withClause);
945 * For each row of VALUES, transform the raw expressions and gather type
946 * information. This is also a handy place to reject DEFAULT nodes, which
947 * the grammar allows for simplicity.
949 foreach(lc, stmt->valuesLists)
951 List *sublist = (List *) lfirst(lc);
953 /* Do basic expression transformation (same as a ROW() expr) */
954 sublist = transformExpressionList(pstate, sublist);
957 * All the sublists must be the same length, *after* transformation
958 * (which might expand '*' into multiple items). The VALUES RTE can't
959 * handle anything different.
961 if (sublist_length < 0)
963 /* Remember post-transformation length of first sublist */
964 sublist_length = list_length(sublist);
965 /* and allocate arrays for per-column info */
966 colexprs = (List **) palloc0(sublist_length * sizeof(List *));
967 coltypes = (Oid *) palloc0(sublist_length * sizeof(Oid));
969 else if (sublist_length != list_length(sublist))
972 (errcode(ERRCODE_SYNTAX_ERROR),
973 errmsg("VALUES lists must all be the same length"),
974 parser_errposition(pstate,
975 exprLocation((Node *) sublist))));
978 exprsLists = lappend(exprsLists, sublist);
980 /* Check for DEFAULT and build per-column expression lists */
982 foreach(lc2, sublist)
984 Node *col = (Node *) lfirst(lc2);
986 if (IsA(col, SetToDefault))
988 (errcode(ERRCODE_SYNTAX_ERROR),
989 errmsg("DEFAULT can only appear in a VALUES list within INSERT"),
990 parser_errposition(pstate, exprLocation(col))));
991 colexprs[i] = lappend(colexprs[i], col);
997 * Now resolve the common types of the columns, and coerce everything to
1000 for (i = 0; i < sublist_length; i++)
1002 coltypes[i] = select_common_type(pstate, colexprs[i], "VALUES", NULL);
1005 newExprsLists = NIL;
1006 foreach(lc, exprsLists)
1008 List *sublist = (List *) lfirst(lc);
1009 List *newsublist = NIL;
1012 foreach(lc2, sublist)
1014 Node *col = (Node *) lfirst(lc2);
1016 col = coerce_to_common_type(pstate, col, coltypes[i], "VALUES");
1017 newsublist = lappend(newsublist, col);
1021 newExprsLists = lappend(newExprsLists, newsublist);
1025 * Generate the VALUES RTE
1027 rte = addRangeTableEntryForValues(pstate, newExprsLists, NULL, true);
1028 rtr = makeNode(RangeTblRef);
1029 /* assume new rte is at end */
1030 rtr->rtindex = list_length(pstate->p_rtable);
1031 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1032 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
1033 pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
1036 * Generate a targetlist as though expanding "*"
1038 Assert(pstate->p_next_resno == 1);
1039 qry->targetList = expandRelAttrs(pstate, rte, rtr->rtindex, 0, -1);
1042 * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1045 qry->sortClause = transformSortClause(pstate,
1048 true /* fix unknowns */,
1049 false /* not window function */);
1051 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1053 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1056 if (stmt->lockingClause)
1058 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1059 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES")));
1061 /* handle any CREATE TABLE AS spec */
1062 if (stmt->intoClause)
1064 qry->intoClause = stmt->intoClause;
1065 if (stmt->intoClause->colNames)
1066 applyColumnNames(qry->targetList, stmt->intoClause->colNames);
1070 * There mustn't have been any table references in the expressions, else
1071 * strange things would happen, like Cartesian products of those tables
1072 * with the VALUES list. We have to check this after parsing ORDER BY et
1073 * al since those could insert more junk.
1075 if (list_length(pstate->p_joinlist) != 1)
1077 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1078 errmsg("VALUES must not contain table references"),
1079 parser_errposition(pstate,
1080 locate_var_of_level((Node *) newExprsLists, 0))));
1083 * Another thing we can't currently support is NEW/OLD references in rules
1084 * --- seems we'd need something like SQL99's LATERAL construct to ensure
1085 * that the values would be available while evaluating the VALUES RTE.
1086 * This is a shame. FIXME
1088 if (list_length(pstate->p_rtable) != 1 &&
1089 contain_vars_of_level((Node *) newExprsLists, 0))
1091 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1092 errmsg("VALUES must not contain OLD or NEW references"),
1093 errhint("Use SELECT ... UNION ALL ... instead."),
1094 parser_errposition(pstate,
1095 locate_var_of_level((Node *) newExprsLists, 0))));
1097 qry->rtable = pstate->p_rtable;
1098 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1100 qry->hasSubLinks = pstate->p_hasSubLinks;
1101 /* aggregates not allowed (but subselects are okay) */
1102 if (pstate->p_hasAggs)
1104 (errcode(ERRCODE_GROUPING_ERROR),
1105 errmsg("cannot use aggregate function in VALUES"),
1106 parser_errposition(pstate,
1107 locate_agg_of_level((Node *) newExprsLists, 0))));
1108 if (pstate->p_hasWindowFuncs)
1110 (errcode(ERRCODE_WINDOWING_ERROR),
1111 errmsg("cannot use window function in VALUES"),
1112 parser_errposition(pstate,
1113 locate_windowfunc((Node *) newExprsLists))));
1119 * transformSetOperationStmt -
1120 * transforms a set-operations tree
1122 * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
1123 * structure to it. We must transform each leaf SELECT and build up a top-
1124 * level Query that contains the leaf SELECTs as subqueries in its rangetable.
1125 * The tree of set operations is converted into the setOperations field of
1126 * the top-level Query.
1129 transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
1131 Query *qry = makeNode(Query);
1132 SelectStmt *leftmostSelect;
1134 Query *leftmostQuery;
1135 SetOperationStmt *sostmt;
1137 List *intoColNames = NIL;
1141 List *lockingClause;
1143 ListCell *left_tlist,
1151 int sv_rtable_length;
1152 RangeTblEntry *jrte;
1155 qry->commandType = CMD_SELECT;
1158 * Find leftmost leaf SelectStmt; extract the one-time-only items from it
1159 * and from the top-level node.
1161 leftmostSelect = stmt->larg;
1162 while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1163 leftmostSelect = leftmostSelect->larg;
1164 Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1165 leftmostSelect->larg == NULL);
1166 if (leftmostSelect->intoClause)
1168 qry->intoClause = leftmostSelect->intoClause;
1169 intoColNames = leftmostSelect->intoClause->colNames;
1172 /* clear this to prevent complaints in transformSetOperationTree() */
1173 leftmostSelect->intoClause = NULL;
1176 * These are not one-time, exactly, but we want to process them here and
1177 * not let transformSetOperationTree() see them --- else it'll just
1178 * recurse right back here!
1180 sortClause = stmt->sortClause;
1181 limitOffset = stmt->limitOffset;
1182 limitCount = stmt->limitCount;
1183 lockingClause = stmt->lockingClause;
1185 stmt->sortClause = NIL;
1186 stmt->limitOffset = NULL;
1187 stmt->limitCount = NULL;
1188 stmt->lockingClause = NIL;
1190 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1193 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1194 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1196 /* process the WITH clause */
1197 if (stmt->withClause)
1199 qry->hasRecursive = stmt->withClause->recursive;
1200 qry->cteList = transformWithClause(pstate, stmt->withClause);
1204 * Recursively transform the components of the tree.
1206 sostmt = (SetOperationStmt *) transformSetOperationTree(pstate, stmt,
1209 Assert(sostmt && IsA(sostmt, SetOperationStmt));
1210 qry->setOperations = (Node *) sostmt;
1213 * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1215 node = sostmt->larg;
1216 while (node && IsA(node, SetOperationStmt))
1217 node = ((SetOperationStmt *) node)->larg;
1218 Assert(node && IsA(node, RangeTblRef));
1219 leftmostRTI = ((RangeTblRef *) node)->rtindex;
1220 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1221 Assert(leftmostQuery != NULL);
1224 * Generate dummy targetlist for outer query using column names of
1225 * leftmost select and common datatypes of topmost set operation. Also
1226 * make lists of the dummy vars and their names for use in parsing ORDER
1229 * Note: we use leftmostRTI as the varno of the dummy variables. It
1230 * shouldn't matter too much which RT index they have, as long as they
1231 * have one that corresponds to a real RT entry; else funny things may
1232 * happen when the tree is mashed by rule rewriting.
1234 qry->targetList = NIL;
1237 left_tlist = list_head(leftmostQuery->targetList);
1239 forboth(lct, sostmt->colTypes, lcm, sostmt->colTypmods)
1241 Oid colType = lfirst_oid(lct);
1242 int32 colTypmod = lfirst_int(lcm);
1243 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1248 Assert(!lefttle->resjunk);
1249 colName = pstrdup(lefttle->resname);
1250 var = makeVar(leftmostRTI,
1255 var->location = exprLocation((Node *) lefttle->expr);
1256 tle = makeTargetEntry((Expr *) var,
1257 (AttrNumber) pstate->p_next_resno++,
1260 qry->targetList = lappend(qry->targetList, tle);
1261 targetvars = lappend(targetvars, var);
1262 targetnames = lappend(targetnames, makeString(colName));
1263 left_tlist = lnext(left_tlist);
1267 * As a first step towards supporting sort clauses that are expressions
1268 * using the output columns, generate a varnamespace entry that makes the
1269 * output columns visible. A Join RTE node is handy for this, since we
1270 * can easily control the Vars generated upon matches.
1272 * Note: we don't yet do anything useful with such cases, but at least
1273 * "ORDER BY upper(foo)" will draw the right error message rather than
1276 sv_rtable_length = list_length(pstate->p_rtable);
1278 jrte = addRangeTableEntryForJoin(pstate,
1285 sv_relnamespace = pstate->p_relnamespace;
1286 pstate->p_relnamespace = NIL; /* no qualified names allowed */
1288 sv_varnamespace = pstate->p_varnamespace;
1289 pstate->p_varnamespace = list_make1(jrte);
1292 * For now, we don't support resjunk sort clauses on the output of a
1293 * setOperation tree --- you can only use the SQL92-spec options of
1294 * selecting an output column by name or number. Enforce by checking that
1295 * transformSortClause doesn't add any items to tlist.
1297 tllen = list_length(qry->targetList);
1299 qry->sortClause = transformSortClause(pstate,
1302 false /* no unknowns expected */,
1303 false /* not window function */);
1305 pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1306 pstate->p_relnamespace = sv_relnamespace;
1307 pstate->p_varnamespace = sv_varnamespace;
1309 if (tllen != list_length(qry->targetList))
1311 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1312 errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
1313 errdetail("Only result column names can be used, not expressions or functions."),
1314 errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
1315 parser_errposition(pstate,
1316 exprLocation(list_nth(qry->targetList, tllen)))));
1318 qry->limitOffset = transformLimitClause(pstate, limitOffset,
1320 qry->limitCount = transformLimitClause(pstate, limitCount,
1324 * Handle SELECT INTO/CREATE TABLE AS.
1326 * Any column names from CREATE TABLE AS need to be attached to both the
1327 * top level and the leftmost subquery. We do not do this earlier because
1328 * we do *not* want sortClause processing to be affected.
1332 applyColumnNames(qry->targetList, intoColNames);
1333 applyColumnNames(leftmostQuery->targetList, intoColNames);
1336 qry->rtable = pstate->p_rtable;
1337 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1339 qry->hasSubLinks = pstate->p_hasSubLinks;
1340 qry->hasAggs = pstate->p_hasAggs;
1341 if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
1342 parseCheckAggregates(pstate, qry);
1343 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1344 if (pstate->p_hasWindowFuncs)
1345 parseCheckWindowFuncs(pstate, qry);
1347 foreach(l, lockingClause)
1349 transformLockingClause(pstate, qry, (LockingClause *) lfirst(l));
1356 * transformSetOperationTree
1357 * Recursively transform leaves and internal nodes of a set-op tree
1359 * In addition to returning the transformed node, we return a list of
1360 * expression nodes showing the type, typmod, and location (for error messages)
1361 * of each output column of the set-op node. This is used only during the
1362 * internal recursion of this function. At the upper levels we use
1363 * SetToDefault nodes for this purpose, since they carry exactly the fields
1364 * needed, but any other expression node type would do as well.
1367 transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
1368 bool isTopLevel, List **colInfo)
1372 Assert(stmt && IsA(stmt, SelectStmt));
1375 * Validity-check both leaf and internal SELECTs for disallowed ops.
1377 if (stmt->intoClause)
1379 (errcode(ERRCODE_SYNTAX_ERROR),
1380 errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
1381 parser_errposition(pstate,
1382 exprLocation((Node *) stmt->intoClause))));
1384 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1385 if (stmt->lockingClause)
1387 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1388 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1391 * If an internal node of a set-op tree has ORDER BY, UPDATE, or LIMIT
1392 * clauses attached, we need to treat it like a leaf node to generate an
1393 * independent sub-Query tree. Otherwise, it can be represented by a
1394 * SetOperationStmt node underneath the parent Query.
1396 if (stmt->op == SETOP_NONE)
1398 Assert(stmt->larg == NULL && stmt->rarg == NULL);
1403 Assert(stmt->larg != NULL && stmt->rarg != NULL);
1404 if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
1405 stmt->lockingClause)
1413 /* Process leaf SELECT */
1415 char selectName[32];
1421 * Transform SelectStmt into a Query.
1423 * Note: previously transformed sub-queries don't affect the parsing
1424 * of this sub-query, because they are not in the toplevel pstate's
1427 selectQuery = parse_sub_analyze((Node *) stmt, pstate, NULL);
1430 * Check for bogus references to Vars on the current query level (but
1431 * upper-level references are okay). Normally this can't happen
1432 * because the namespace will be empty, but it could happen if we are
1435 if (pstate->p_relnamespace || pstate->p_varnamespace)
1437 if (contain_vars_of_level((Node *) selectQuery, 1))
1439 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1440 errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
1441 parser_errposition(pstate,
1442 locate_var_of_level((Node *) selectQuery, 1))));
1446 * Extract a list of the result expressions for upper-level checking.
1449 foreach(tl, selectQuery->targetList)
1451 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1454 *colInfo = lappend(*colInfo, tle->expr);
1458 * Make the leaf query be a subquery in the top-level rangetable.
1460 snprintf(selectName, sizeof(selectName), "*SELECT* %d",
1461 list_length(pstate->p_rtable) + 1);
1462 rte = addRangeTableEntryForSubquery(pstate,
1464 makeAlias(selectName, NIL),
1468 * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
1470 rtr = makeNode(RangeTblRef);
1471 /* assume new rte is at end */
1472 rtr->rtindex = list_length(pstate->p_rtable);
1473 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1474 return (Node *) rtr;
1478 /* Process an internal node (set operation node) */
1479 SetOperationStmt *op = makeNode(SetOperationStmt);
1484 const char *context;
1486 context = (stmt->op == SETOP_UNION ? "UNION" :
1487 (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
1491 op->all = stmt->all;
1494 * Recursively transform the left child node.
1496 op->larg = transformSetOperationTree(pstate, stmt->larg,
1501 * If we are processing a recursive union query, now is the time
1502 * to examine the non-recursive term's output columns and mark the
1503 * containing CTE as having those result columns. We should do this
1504 * only at the topmost setop of the CTE, of course.
1507 pstate->p_parent_cte &&
1508 pstate->p_parent_cte->cterecursive)
1509 determineRecursiveColTypes(pstate, op->larg, lcolinfo);
1512 * Recursively transform the right child node.
1514 op->rarg = transformSetOperationTree(pstate, stmt->rarg,
1519 * Verify that the two children have the same number of non-junk
1520 * columns, and determine the types of the merged output columns.
1522 if (list_length(lcolinfo) != list_length(rcolinfo))
1524 (errcode(ERRCODE_SYNTAX_ERROR),
1525 errmsg("each %s query must have the same number of columns",
1527 parser_errposition(pstate,
1528 exprLocation((Node *) rcolinfo))));
1532 op->colTypmods = NIL;
1533 op->groupClauses = NIL;
1534 forboth(lci, lcolinfo, rci, rcolinfo)
1536 Node *lcolinfo = (Node *) lfirst(lci);
1537 Node *rcolinfo = (Node *) lfirst(rci);
1538 Oid lcoltype = exprType(lcolinfo);
1539 Oid rcoltype = exprType(rcolinfo);
1540 int32 lcoltypmod = exprTypmod(lcolinfo);
1541 int32 rcoltypmod = exprTypmod(rcolinfo);
1543 SetToDefault *rescolinfo;
1547 /* select common type, same as CASE et al */
1548 rescoltype = select_common_type(pstate,
1549 list_make2(lcolinfo, rcolinfo),
1552 /* if same type and same typmod, use typmod; else default */
1553 if (lcoltype == rcoltype && lcoltypmod == rcoltypmod)
1554 rescoltypmod = lcoltypmod;
1558 /* verify the coercions are actually possible */
1559 (void) coerce_to_common_type(pstate, lcolinfo,
1560 rescoltype, context);
1561 (void) coerce_to_common_type(pstate, rcolinfo,
1562 rescoltype, context);
1565 rescolinfo = makeNode(SetToDefault);
1566 rescolinfo->typeId = rescoltype;
1567 rescolinfo->typeMod = rescoltypmod;
1568 rescolinfo->location = exprLocation(bestexpr);
1569 *colInfo = lappend(*colInfo, rescolinfo);
1571 op->colTypes = lappend_oid(op->colTypes, rescoltype);
1572 op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
1575 * For all cases except UNION ALL, identify the grouping operators
1576 * (and, if available, sorting operators) that will be used to
1577 * eliminate duplicates.
1579 if (op->op != SETOP_UNION || !op->all)
1581 SortGroupClause *grpcl = makeNode(SortGroupClause);
1584 ParseCallbackState pcbstate;
1586 setup_parser_errposition_callback(&pcbstate, pstate,
1587 rescolinfo->location);
1589 /* determine the eqop and optional sortop */
1590 get_sort_group_operators(rescoltype,
1592 &sortop, &eqop, NULL);
1594 cancel_parser_errposition_callback(&pcbstate);
1596 /* we don't have a tlist yet, so can't assign sortgrouprefs */
1597 grpcl->tleSortGroupRef = 0;
1599 grpcl->sortop = sortop;
1600 grpcl->nulls_first = false; /* OK with or without sortop */
1602 op->groupClauses = lappend(op->groupClauses, grpcl);
1611 * Process the outputs of the non-recursive term of a recursive union
1612 * to set up the parent CTE's columns
1615 determineRecursiveColTypes(ParseState *pstate, Node *larg, List *lcolinfo)
1619 Query *leftmostQuery;
1621 ListCell *left_tlist;
1626 * Find leftmost leaf SELECT
1629 while (node && IsA(node, SetOperationStmt))
1630 node = ((SetOperationStmt *) node)->larg;
1631 Assert(node && IsA(node, RangeTblRef));
1632 leftmostRTI = ((RangeTblRef *) node)->rtindex;
1633 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1634 Assert(leftmostQuery != NULL);
1637 * Generate dummy targetlist using column names of leftmost select
1638 * and dummy result expressions of the non-recursive term.
1641 left_tlist = list_head(leftmostQuery->targetList);
1644 foreach(lci, lcolinfo)
1646 Expr *lcolexpr = (Expr *) lfirst(lci);
1647 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1651 Assert(!lefttle->resjunk);
1652 colName = pstrdup(lefttle->resname);
1653 tle = makeTargetEntry(lcolexpr,
1657 targetList = lappend(targetList, tle);
1658 left_tlist = lnext(left_tlist);
1661 /* Now build CTE's output column info using dummy targetlist */
1662 analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
1666 * Attach column names from a ColumnDef list to a TargetEntry list
1667 * (for CREATE TABLE AS)
1670 applyColumnNames(List *dst, List *src)
1675 src_item = list_head(src);
1677 foreach(dst_item, dst)
1679 TargetEntry *d = (TargetEntry *) lfirst(dst_item);
1682 /* junk targets don't count */
1686 /* fewer ColumnDefs than target entries is OK */
1687 if (src_item == NULL)
1690 s = (ColumnDef *) lfirst(src_item);
1691 src_item = lnext(src_item);
1693 d->resname = pstrdup(s->colname);
1696 /* more ColumnDefs than target entries is not OK */
1697 if (src_item != NULL)
1699 (errcode(ERRCODE_SYNTAX_ERROR),
1700 errmsg("CREATE TABLE AS specifies too many column names")));
1705 * transformUpdateStmt -
1706 * transforms an update statement
1709 transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
1711 Query *qry = makeNode(Query);
1712 RangeTblEntry *target_rte;
1714 ListCell *origTargetList;
1717 qry->commandType = CMD_UPDATE;
1718 pstate->p_is_update = true;
1720 qry->resultRelation = setTargetTable(pstate, stmt->relation,
1721 interpretInhOption(stmt->relation->inhOpt),
1726 * the FROM clause is non-standard SQL syntax. We used to be able to do
1727 * this with REPLACE in POSTQUEL so we keep the feature.
1729 transformFromClause(pstate, stmt->fromClause);
1731 qry->targetList = transformTargetList(pstate, stmt->targetList);
1733 qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
1735 qry->returningList = transformReturningList(pstate, stmt->returningList);
1737 qry->rtable = pstate->p_rtable;
1738 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1740 qry->hasSubLinks = pstate->p_hasSubLinks;
1743 * Top-level aggregates are simply disallowed in UPDATE, per spec. (From
1744 * an implementation point of view, this is forced because the implicit
1745 * ctid reference would otherwise be an ungrouped variable.)
1747 if (pstate->p_hasAggs)
1749 (errcode(ERRCODE_GROUPING_ERROR),
1750 errmsg("cannot use aggregate function in UPDATE"),
1751 parser_errposition(pstate,
1752 locate_agg_of_level((Node *) qry, 0))));
1753 if (pstate->p_hasWindowFuncs)
1755 (errcode(ERRCODE_WINDOWING_ERROR),
1756 errmsg("cannot use window function in UPDATE"),
1757 parser_errposition(pstate,
1758 locate_windowfunc((Node *) qry))));
1761 * Now we are done with SELECT-like processing, and can get on with
1762 * transforming the target list to match the UPDATE target columns.
1765 /* Prepare to assign non-conflicting resnos to resjunk attributes */
1766 if (pstate->p_next_resno <= pstate->p_target_relation->rd_rel->relnatts)
1767 pstate->p_next_resno = pstate->p_target_relation->rd_rel->relnatts + 1;
1769 /* Prepare non-junk columns for assignment to target table */
1770 target_rte = pstate->p_target_rangetblentry;
1771 origTargetList = list_head(stmt->targetList);
1773 foreach(tl, qry->targetList)
1775 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1776 ResTarget *origTarget;
1782 * Resjunk nodes need no additional processing, but be sure they
1783 * have resnos that do not match any target columns; else rewriter
1784 * or planner might get confused. They don't need a resname
1787 tle->resno = (AttrNumber) pstate->p_next_resno++;
1788 tle->resname = NULL;
1791 if (origTargetList == NULL)
1792 elog(ERROR, "UPDATE target count mismatch --- internal error");
1793 origTarget = (ResTarget *) lfirst(origTargetList);
1794 Assert(IsA(origTarget, ResTarget));
1796 attrno = attnameAttNum(pstate->p_target_relation,
1797 origTarget->name, true);
1798 if (attrno == InvalidAttrNumber)
1800 (errcode(ERRCODE_UNDEFINED_COLUMN),
1801 errmsg("column \"%s\" of relation \"%s\" does not exist",
1803 RelationGetRelationName(pstate->p_target_relation)),
1804 parser_errposition(pstate, origTarget->location)));
1806 updateTargetListEntry(pstate, tle, origTarget->name,
1808 origTarget->indirection,
1809 origTarget->location);
1811 /* Mark the target column as requiring update permissions */
1812 target_rte->modifiedCols = bms_add_member(target_rte->modifiedCols,
1813 attrno - FirstLowInvalidHeapAttributeNumber);
1815 origTargetList = lnext(origTargetList);
1817 if (origTargetList != NULL)
1818 elog(ERROR, "UPDATE target count mismatch --- internal error");
1824 * transformReturningList -
1825 * handle a RETURNING clause in INSERT/UPDATE/DELETE
1828 transformReturningList(ParseState *pstate, List *returningList)
1831 int save_next_resno;
1833 bool save_hasWindowFuncs;
1836 if (returningList == NIL)
1837 return NIL; /* nothing to do */
1840 * We need to assign resnos starting at one in the RETURNING list. Save
1841 * and restore the main tlist's value of p_next_resno, just in case
1842 * someone looks at it later (probably won't happen).
1844 save_next_resno = pstate->p_next_resno;
1845 pstate->p_next_resno = 1;
1847 /* save other state so that we can detect disallowed stuff */
1848 save_hasAggs = pstate->p_hasAggs;
1849 pstate->p_hasAggs = false;
1850 save_hasWindowFuncs = pstate->p_hasWindowFuncs;
1851 pstate->p_hasWindowFuncs = false;
1852 length_rtable = list_length(pstate->p_rtable);
1854 /* transform RETURNING identically to a SELECT targetlist */
1855 rlist = transformTargetList(pstate, returningList);
1857 /* check for disallowed stuff */
1859 /* aggregates not allowed (but subselects are okay) */
1860 if (pstate->p_hasAggs)
1862 (errcode(ERRCODE_GROUPING_ERROR),
1863 errmsg("cannot use aggregate function in RETURNING"),
1864 parser_errposition(pstate,
1865 locate_agg_of_level((Node *) rlist, 0))));
1866 if (pstate->p_hasWindowFuncs)
1868 (errcode(ERRCODE_WINDOWING_ERROR),
1869 errmsg("cannot use window function in RETURNING"),
1870 parser_errposition(pstate,
1871 locate_windowfunc((Node *) rlist))));
1873 /* no new relation references please */
1874 if (list_length(pstate->p_rtable) != length_rtable)
1879 /* try to locate such a reference to point to */
1880 for (relid = length_rtable + 1; relid <= list_length(pstate->p_rtable); relid++)
1882 vlocation = locate_var_of_relation((Node *) rlist, relid, 0);
1887 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1888 errmsg("RETURNING cannot contain references to other relations"),
1889 parser_errposition(pstate, vlocation)));
1892 /* mark column origins */
1893 markTargetListOrigins(pstate, rlist);
1896 pstate->p_next_resno = save_next_resno;
1897 pstate->p_hasAggs = save_hasAggs;
1898 pstate->p_hasWindowFuncs = save_hasWindowFuncs;
1905 * transformDeclareCursorStmt -
1906 * transform a DECLARE CURSOR Statement
1908 * DECLARE CURSOR is a hybrid case: it's an optimizable statement (in fact not
1909 * significantly different from a SELECT) as far as parsing/rewriting/planning
1910 * are concerned, but it's not passed to the executor and so in that sense is
1911 * a utility statement. We transform it into a Query exactly as if it were
1912 * a SELECT, then stick the original DeclareCursorStmt into the utilityStmt
1913 * field to carry the cursor name and options.
1916 transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
1921 * Don't allow both SCROLL and NO SCROLL to be specified
1923 if ((stmt->options & CURSOR_OPT_SCROLL) &&
1924 (stmt->options & CURSOR_OPT_NO_SCROLL))
1926 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
1927 errmsg("cannot specify both SCROLL and NO SCROLL")));
1929 result = transformStmt(pstate, stmt->query);
1931 /* Grammar should not have allowed anything but SELECT */
1932 if (!IsA(result, Query) ||
1933 result->commandType != CMD_SELECT ||
1934 result->utilityStmt != NULL)
1935 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
1937 /* But we must explicitly disallow DECLARE CURSOR ... SELECT INTO */
1938 if (result->intoClause)
1940 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
1941 errmsg("DECLARE CURSOR cannot specify INTO"),
1942 parser_errposition(pstate,
1943 exprLocation((Node *) result->intoClause))));
1945 /* FOR UPDATE and WITH HOLD are not compatible */
1946 if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
1948 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1949 errmsg("DECLARE CURSOR WITH HOLD ... FOR UPDATE/SHARE is not supported"),
1950 errdetail("Holdable cursors must be READ ONLY.")));
1952 /* FOR UPDATE and SCROLL are not compatible */
1953 if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
1955 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1956 errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
1957 errdetail("Scrollable cursors must be READ ONLY.")));
1959 /* FOR UPDATE and INSENSITIVE are not compatible */
1960 if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
1962 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1963 errmsg("DECLARE INSENSITIVE CURSOR ... FOR UPDATE/SHARE is not supported"),
1964 errdetail("Insensitive cursors must be READ ONLY.")));
1966 /* We won't need the raw querytree any more */
1969 result->utilityStmt = (Node *) stmt;
1976 * transformExplainStmt -
1977 * transform an EXPLAIN Statement
1979 * EXPLAIN is just like other utility statements in that we emit it as a
1980 * CMD_UTILITY Query node with no transformation of the raw parse tree.
1981 * However, if p_variableparams is set, it could be that the client is
1982 * expecting us to resolve parameter types in something like
1983 * EXPLAIN SELECT * FROM tab WHERE col = $1
1984 * To deal with such cases, we run parse analysis and throw away the result;
1985 * this is a bit grotty but not worth contorting the rest of the system for.
1986 * (The approach we use for DECLARE CURSOR won't work because the statement
1987 * being explained isn't necessarily a SELECT, and in particular might rewrite
1988 * to multiple parsetrees.)
1991 transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
1995 if (pstate->p_variableparams)
1997 /* Since parse analysis scribbles on its input, copy the tree first! */
1998 (void) transformStmt(pstate, copyObject(stmt->query));
2001 /* Now return the untransformed command as a utility Query */
2002 result = makeNode(Query);
2003 result->commandType = CMD_UTILITY;
2004 result->utilityStmt = (Node *) stmt;
2010 /* exported so planner can check again after rewriting, query pullup, etc */
2012 CheckSelectLocking(Query *qry)
2014 if (qry->setOperations)
2016 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2017 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
2018 if (qry->distinctClause != NIL)
2020 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2021 errmsg("SELECT FOR UPDATE/SHARE is not allowed with DISTINCT clause")));
2022 if (qry->groupClause != NIL)
2024 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2025 errmsg("SELECT FOR UPDATE/SHARE is not allowed with GROUP BY clause")));
2026 if (qry->havingQual != NULL)
2028 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2029 errmsg("SELECT FOR UPDATE/SHARE is not allowed with HAVING clause")));
2032 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2033 errmsg("SELECT FOR UPDATE/SHARE is not allowed with aggregate functions")));
2034 if (qry->hasWindowFuncs)
2036 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2037 errmsg("SELECT FOR UPDATE/SHARE is not allowed with window functions")));
2041 * Transform a FOR UPDATE/SHARE clause
2043 * This basically involves replacing names by integer relids.
2045 * NB: if you need to change this, see also markQueryForLocking()
2046 * in rewriteHandler.c, and isLockedRel() in parse_relation.c.
2049 transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc)
2051 List *lockedRels = lc->lockedRels;
2055 LockingClause *allrels;
2057 CheckSelectLocking(qry);
2059 /* make a clause we can pass down to subqueries to select all rels */
2060 allrels = makeNode(LockingClause);
2061 allrels->lockedRels = NIL; /* indicates all rels */
2062 allrels->forUpdate = lc->forUpdate;
2063 allrels->noWait = lc->noWait;
2065 if (lockedRels == NIL)
2067 /* all regular tables used in query */
2069 foreach(rt, qry->rtable)
2071 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2074 switch (rte->rtekind)
2077 applyLockingClause(qry, i, lc->forUpdate, lc->noWait);
2078 rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2083 * FOR UPDATE/SHARE of subquery is propagated to all of
2086 transformLockingClause(pstate, rte->subquery, allrels);
2091 * We allow FOR UPDATE/SHARE of a WITH query to be
2092 * propagated into the WITH, but it doesn't seem very
2093 * sane to allow this for a reference to an
2094 * outer-level WITH. And it definitely wouldn't work
2095 * for a self-reference, since we're not done
2096 * analyzing the CTE anyway.
2098 CommonTableExpr *cte;
2100 if (rte->ctelevelsup > 0 || rte->self_reference)
2102 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2103 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to an outer-level WITH query")));
2104 cte = GetCTEForRTE(pstate, rte, -1);
2105 /* should be analyzed by now */
2106 Assert(IsA(cte->ctequery, Query));
2107 transformLockingClause(pstate,
2108 (Query *) cte->ctequery,
2113 /* ignore JOIN, SPECIAL, FUNCTION RTEs */
2120 /* just the named tables */
2121 foreach(l, lockedRels)
2123 RangeVar *thisrel = (RangeVar *) lfirst(l);
2125 /* For simplicity we insist on unqualified alias names here */
2126 if (thisrel->catalogname || thisrel->schemaname)
2128 (errcode(ERRCODE_SYNTAX_ERROR),
2129 errmsg("SELECT FOR UPDATE/SHARE must specify unqualified relation names"),
2130 parser_errposition(pstate, thisrel->location)));
2133 foreach(rt, qry->rtable)
2135 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2138 if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
2140 switch (rte->rtekind)
2143 applyLockingClause(qry, i,
2144 lc->forUpdate, lc->noWait);
2145 rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2150 * FOR UPDATE/SHARE of subquery is propagated to
2151 * all of subquery's rels
2153 transformLockingClause(pstate, rte->subquery, allrels);
2157 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2158 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a join"),
2159 parser_errposition(pstate, thisrel->location)));
2163 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2164 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to NEW or OLD"),
2165 parser_errposition(pstate, thisrel->location)));
2169 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2170 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a function"),
2171 parser_errposition(pstate, thisrel->location)));
2175 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2176 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES"),
2177 parser_errposition(pstate, thisrel->location)));
2182 * We allow FOR UPDATE/SHARE of a WITH query
2183 * to be propagated into the WITH, but it
2184 * doesn't seem very sane to allow this for a
2185 * reference to an outer-level WITH. And it
2186 * definitely wouldn't work for a
2187 * self-reference, since we're not done
2188 * analyzing the CTE anyway.
2190 CommonTableExpr *cte;
2192 if (rte->ctelevelsup > 0 || rte->self_reference)
2194 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2195 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to an outer-level WITH query"),
2196 parser_errposition(pstate, thisrel->location)));
2197 cte = GetCTEForRTE(pstate, rte, -1);
2198 /* should be analyzed by now */
2199 Assert(IsA(cte->ctequery, Query));
2200 transformLockingClause(pstate,
2201 (Query *) cte->ctequery,
2206 elog(ERROR, "unrecognized RTE type: %d",
2207 (int) rte->rtekind);
2210 break; /* out of foreach loop */
2215 (errcode(ERRCODE_UNDEFINED_TABLE),
2216 errmsg("relation \"%s\" in FOR UPDATE/SHARE clause not found in FROM clause",
2218 parser_errposition(pstate, thisrel->location)));
2224 * Record locking info for a single rangetable item
2227 applyLockingClause(Query *qry, Index rtindex, bool forUpdate, bool noWait)
2231 /* Check for pre-existing entry for same rtindex */
2232 if ((rc = get_rowmark(qry, rtindex)) != NULL)
2235 * If the same RTE is specified both FOR UPDATE and FOR SHARE, treat
2236 * it as FOR UPDATE. (Reasonable, since you can't take both a shared
2237 * and exclusive lock at the same time; it'll end up being exclusive
2240 * We also consider that NOWAIT wins if it's specified both ways. This
2241 * is a bit more debatable but raising an error doesn't seem helpful.
2242 * (Consider for instance SELECT FOR UPDATE NOWAIT from a view that
2243 * internally contains a plain FOR UPDATE spec.)
2245 rc->forUpdate |= forUpdate;
2246 rc->noWait |= noWait;
2250 /* Make a new RowMarkClause */
2251 rc = makeNode(RowMarkClause);
2254 rc->forUpdate = forUpdate;
2255 rc->noWait = noWait;
2256 rc->isParent = false;
2257 qry->rowMarks = lappend(qry->rowMarks, rc);
2262 * Traverse a fully-analyzed tree to verify that parameter symbols
2263 * match their types. We need this because some Params might still
2264 * be UNKNOWN, if there wasn't anything to force their coercion,
2265 * and yet other instances seen later might have gotten coerced.
2268 check_parameter_resolution_walker(Node *node, ParseState *pstate)
2272 if (IsA(node, Param))
2274 Param *param = (Param *) node;
2276 if (param->paramkind == PARAM_EXTERN)
2278 int paramno = param->paramid;
2280 if (paramno <= 0 || /* shouldn't happen, but... */
2281 paramno > pstate->p_numparams)
2283 (errcode(ERRCODE_UNDEFINED_PARAMETER),
2284 errmsg("there is no parameter $%d", paramno),
2285 parser_errposition(pstate, param->location)));
2287 if (param->paramtype != pstate->p_paramtypes[paramno - 1])
2289 (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
2290 errmsg("could not determine data type of parameter $%d",
2292 parser_errposition(pstate, param->location)));
2296 if (IsA(node, Query))
2298 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
2299 return query_tree_walker((Query *) node,
2300 check_parameter_resolution_walker,
2301 (void *) pstate, 0);
2303 return expression_tree_walker(node, check_parameter_resolution_walker,