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.390 2009/08/27 20:08:02 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,
54 static void applyColumnNames(List *dst, List *src);
55 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
56 static List *transformReturningList(ParseState *pstate, List *returningList);
57 static Query *transformDeclareCursorStmt(ParseState *pstate,
58 DeclareCursorStmt *stmt);
59 static Query *transformExplainStmt(ParseState *pstate,
61 static void transformLockingClause(ParseState *pstate,
62 Query *qry, LockingClause *lc);
63 static bool check_parameter_resolution_walker(Node *node, ParseState *pstate);
68 * Analyze a raw parse tree and transform it to Query form.
70 * Optionally, information about $n parameter types can be supplied.
71 * References to $n indexes not defined by paramTypes[] are disallowed.
73 * The result is a Query node. Optimizable statements require considerable
74 * transformation, while utility-type statements are simply hung off
75 * a dummy CMD_UTILITY Query node.
78 parse_analyze(Node *parseTree, const char *sourceText,
79 Oid *paramTypes, int numParams)
81 ParseState *pstate = make_parsestate(NULL);
84 Assert(sourceText != NULL); /* required as of 8.4 */
86 pstate->p_sourcetext = sourceText;
87 pstate->p_paramtypes = paramTypes;
88 pstate->p_numparams = numParams;
89 pstate->p_variableparams = false;
91 query = transformStmt(pstate, parseTree);
93 free_parsestate(pstate);
99 * parse_analyze_varparams
101 * This variant is used when it's okay to deduce information about $n
102 * symbol datatypes from context. The passed-in paramTypes[] array can
103 * be modified or enlarged (via repalloc).
106 parse_analyze_varparams(Node *parseTree, const char *sourceText,
107 Oid **paramTypes, int *numParams)
109 ParseState *pstate = make_parsestate(NULL);
112 Assert(sourceText != NULL); /* required as of 8.4 */
114 pstate->p_sourcetext = sourceText;
115 pstate->p_paramtypes = *paramTypes;
116 pstate->p_numparams = *numParams;
117 pstate->p_variableparams = true;
119 query = transformStmt(pstate, parseTree);
121 /* make sure all is well with parameter types */
122 if (pstate->p_numparams > 0)
123 check_parameter_resolution_walker((Node *) query, pstate);
125 *paramTypes = pstate->p_paramtypes;
126 *numParams = pstate->p_numparams;
128 free_parsestate(pstate);
135 * Entry point for recursively analyzing a sub-statement.
138 parse_sub_analyze(Node *parseTree, ParseState *parentParseState)
140 ParseState *pstate = make_parsestate(parentParseState);
143 query = transformStmt(pstate, parseTree);
145 free_parsestate(pstate);
152 * transform a Parse tree into a Query tree.
155 transformStmt(ParseState *pstate, Node *parseTree)
159 switch (nodeTag(parseTree))
162 * Optimizable statements
165 result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
169 result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
173 result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
178 SelectStmt *n = (SelectStmt *) parseTree;
181 result = transformValuesClause(pstate, n);
182 else if (n->op == SETOP_NONE)
183 result = transformSelectStmt(pstate, n);
185 result = transformSetOperationStmt(pstate, n);
192 case T_DeclareCursorStmt:
193 result = transformDeclareCursorStmt(pstate,
194 (DeclareCursorStmt *) parseTree);
198 result = transformExplainStmt(pstate,
199 (ExplainStmt *) parseTree);
205 * other statements don't require any transformation; just return
206 * the original parsetree with a Query node plastered on top.
208 result = makeNode(Query);
209 result->commandType = CMD_UTILITY;
210 result->utilityStmt = (Node *) parseTree;
214 /* Mark as original query until we learn differently */
215 result->querySource = QSRC_ORIGINAL;
216 result->canSetTag = true;
222 * analyze_requires_snapshot
223 * Returns true if a snapshot must be set before doing parse analysis
224 * on the given raw parse tree.
226 * Classification here should match transformStmt(); but we also have to
227 * allow a NULL input (for Parse/Bind of an empty query string).
230 analyze_requires_snapshot(Node *parseTree)
234 if (parseTree == NULL)
237 switch (nodeTag(parseTree))
240 * Optimizable statements
252 case T_DeclareCursorStmt:
253 /* yes, because it's analyzed just like SELECT */
260 * We only need a snapshot in varparams case, but it doesn't seem
261 * worth complicating this function's API to distinguish that.
267 /* utility statements don't have any active parse analysis */
276 * transformDeleteStmt -
277 * transforms a Delete Statement
280 transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
282 Query *qry = makeNode(Query);
285 qry->commandType = CMD_DELETE;
287 /* set up range table with just the result rel */
288 qry->resultRelation = setTargetTable(pstate, stmt->relation,
289 interpretInhOption(stmt->relation->inhOpt),
293 qry->distinctClause = NIL;
296 * The USING clause is non-standard SQL syntax, and is equivalent in
297 * functionality to the FROM list that can be specified for UPDATE. The
298 * USING keyword is used rather than FROM because FROM is already a
299 * keyword in the DELETE syntax.
301 transformFromClause(pstate, stmt->usingClause);
303 qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
305 qry->returningList = transformReturningList(pstate, stmt->returningList);
307 /* done building the range table and jointree */
308 qry->rtable = pstate->p_rtable;
309 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
311 qry->hasSubLinks = pstate->p_hasSubLinks;
312 qry->hasAggs = pstate->p_hasAggs;
313 if (pstate->p_hasAggs)
314 parseCheckAggregates(pstate, qry);
315 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
316 if (pstate->p_hasWindowFuncs)
317 parseCheckWindowFuncs(pstate, qry);
323 * transformInsertStmt -
324 * transform an Insert Statement
327 transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
329 Query *qry = makeNode(Query);
330 SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
331 List *exprList = NIL;
332 bool isGeneralSelect;
334 List *sub_relnamespace;
335 List *sub_varnamespace;
344 qry->commandType = CMD_INSERT;
345 pstate->p_is_insert = true;
348 * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
349 * VALUES list, or general SELECT input. We special-case VALUES, both for
350 * efficiency and so we can handle DEFAULT specifications.
352 isGeneralSelect = (selectStmt && selectStmt->valuesLists == NIL);
355 * If a non-nil rangetable/namespace was passed in, and we are doing
356 * INSERT/SELECT, arrange to pass the rangetable/namespace down to the
357 * SELECT. This can only happen if we are inside a CREATE RULE, and in
358 * that case we want the rule's OLD and NEW rtable entries to appear as
359 * part of the SELECT's rtable, not as outer references for it. (Kluge!)
360 * The SELECT's joinlist is not affected however. We must do this before
361 * adding the target table to the INSERT's rtable.
365 sub_rtable = pstate->p_rtable;
366 pstate->p_rtable = NIL;
367 sub_relnamespace = pstate->p_relnamespace;
368 pstate->p_relnamespace = NIL;
369 sub_varnamespace = pstate->p_varnamespace;
370 pstate->p_varnamespace = NIL;
371 /* There can't be any outer WITH to worry about */
372 Assert(pstate->p_ctenamespace == NIL);
376 sub_rtable = NIL; /* not used, but keep compiler quiet */
377 sub_relnamespace = NIL;
378 sub_varnamespace = NIL;
382 * Must get write lock on INSERT target table before scanning SELECT, else
383 * we will grab the wrong kind of initial lock if the target table is also
384 * mentioned in the SELECT part. Note that the target table is not added
385 * to the joinlist or namespace.
387 qry->resultRelation = setTargetTable(pstate, stmt->relation,
388 false, false, ACL_INSERT);
390 /* Validate stmt->cols list, or build default list if no list given */
391 icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
392 Assert(list_length(icolumns) == list_length(attrnos));
395 * Determine which variant of INSERT we have.
397 if (selectStmt == NULL)
400 * We have INSERT ... DEFAULT VALUES. We can handle this case by
401 * emitting an empty targetlist --- all columns will be defaulted when
402 * the planner expands the targetlist.
406 else if (isGeneralSelect)
409 * We make the sub-pstate a child of the outer pstate so that it can
410 * see any Param definitions supplied from above. Since the outer
411 * pstate's rtable and namespace are presently empty, there are no
412 * side-effects of exposing names the sub-SELECT shouldn't be able to
415 ParseState *sub_pstate = make_parsestate(pstate);
419 * Process the source SELECT.
421 * It is important that this be handled just like a standalone SELECT;
422 * otherwise the behavior of SELECT within INSERT might be different
423 * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
424 * bugs of just that nature...)
426 sub_pstate->p_rtable = sub_rtable;
427 sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
428 sub_pstate->p_relnamespace = sub_relnamespace;
429 sub_pstate->p_varnamespace = sub_varnamespace;
431 selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
433 free_parsestate(sub_pstate);
435 /* The grammar should have produced a SELECT, but it might have INTO */
436 if (!IsA(selectQuery, Query) ||
437 selectQuery->commandType != CMD_SELECT ||
438 selectQuery->utilityStmt != NULL)
439 elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
440 if (selectQuery->intoClause)
442 (errcode(ERRCODE_SYNTAX_ERROR),
443 errmsg("INSERT ... SELECT cannot specify INTO"),
444 parser_errposition(pstate,
445 exprLocation((Node *) selectQuery->intoClause))));
448 * Make the source be a subquery in the INSERT's rangetable, and add
449 * it to the INSERT's joinlist.
451 rte = addRangeTableEntryForSubquery(pstate,
453 makeAlias("*SELECT*", NIL),
455 rtr = makeNode(RangeTblRef);
456 /* assume new rte is at end */
457 rtr->rtindex = list_length(pstate->p_rtable);
458 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
459 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
462 * Generate an expression list for the INSERT that selects all the
463 * non-resjunk columns from the subquery. (INSERT's tlist must be
464 * separate from the subquery's tlist because we may add columns,
465 * insert datatype coercions, etc.)
467 * HACK: unknown-type constants and params in the SELECT's targetlist
468 * are copied up as-is rather than being referenced as subquery
469 * outputs. This is to ensure that when we try to coerce them to
470 * the target column's datatype, the right things happen (see
471 * special cases in coerce_type). Otherwise, this fails:
472 * INSERT INTO foo SELECT 'bar', ... FROM baz
476 foreach(lc, selectQuery->targetList)
478 TargetEntry *tle = (TargetEntry *) lfirst(lc);
484 (IsA(tle->expr, Const) ||IsA(tle->expr, Param)) &&
485 exprType((Node *) tle->expr) == UNKNOWNOID)
489 Var *var = makeVar(rtr->rtindex,
491 exprType((Node *) tle->expr),
492 exprTypmod((Node *) tle->expr),
495 var->location = exprLocation((Node *) tle->expr);
498 exprList = lappend(exprList, expr);
501 /* Prepare row for assignment to target table */
502 exprList = transformInsertRow(pstate, exprList,
506 else if (list_length(selectStmt->valuesLists) > 1)
509 * Process INSERT ... VALUES with multiple VALUES sublists. We
510 * generate a VALUES RTE holding the transformed expression lists, and
511 * build up a targetlist containing Vars that reference the VALUES
514 List *exprsLists = NIL;
515 int sublist_length = -1;
517 /* process the WITH clause */
518 if (selectStmt->withClause)
520 qry->hasRecursive = selectStmt->withClause->recursive;
521 qry->cteList = transformWithClause(pstate, selectStmt->withClause);
524 foreach(lc, selectStmt->valuesLists)
526 List *sublist = (List *) lfirst(lc);
528 /* Do basic expression transformation (same as a ROW() expr) */
529 sublist = transformExpressionList(pstate, sublist);
532 * All the sublists must be the same length, *after*
533 * transformation (which might expand '*' into multiple items).
534 * The VALUES RTE can't handle anything different.
536 if (sublist_length < 0)
538 /* Remember post-transformation length of first sublist */
539 sublist_length = list_length(sublist);
541 else if (sublist_length != list_length(sublist))
544 (errcode(ERRCODE_SYNTAX_ERROR),
545 errmsg("VALUES lists must all be the same length"),
546 parser_errposition(pstate,
547 exprLocation((Node *) sublist))));
550 /* Prepare row for assignment to target table */
551 sublist = transformInsertRow(pstate, sublist,
555 exprsLists = lappend(exprsLists, sublist);
559 * There mustn't have been any table references in the expressions,
560 * else strange things would happen, like Cartesian products of those
561 * tables with the VALUES list ...
563 if (pstate->p_joinlist != NIL)
565 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
566 errmsg("VALUES must not contain table references"),
567 parser_errposition(pstate,
568 locate_var_of_level((Node *) exprsLists, 0))));
571 * Another thing we can't currently support is NEW/OLD references in
572 * rules --- seems we'd need something like SQL99's LATERAL construct
573 * to ensure that the values would be available while evaluating the
574 * VALUES RTE. This is a shame. FIXME
576 if (list_length(pstate->p_rtable) != 1 &&
577 contain_vars_of_level((Node *) exprsLists, 0))
579 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
580 errmsg("VALUES must not contain OLD or NEW references"),
581 errhint("Use SELECT ... UNION ALL ... instead."),
582 parser_errposition(pstate,
583 locate_var_of_level((Node *) exprsLists, 0))));
586 * Generate the VALUES RTE
588 rte = addRangeTableEntryForValues(pstate, exprsLists, NULL, true);
589 rtr = makeNode(RangeTblRef);
590 /* assume new rte is at end */
591 rtr->rtindex = list_length(pstate->p_rtable);
592 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
593 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
596 * Generate list of Vars referencing the RTE
598 expandRTE(rte, rtr->rtindex, 0, -1, false, NULL, &exprList);
603 * Process INSERT ... VALUES with a single VALUES sublist.
604 * We treat this separately for efficiency and for historical
605 * compatibility --- specifically, allowing table references,
607 * INSERT INTO foo VALUES(bar.*)
609 * The sublist is just computed directly as the Query's targetlist,
610 * with no VALUES RTE. So it works just like SELECT without FROM.
613 List *valuesLists = selectStmt->valuesLists;
615 Assert(list_length(valuesLists) == 1);
617 /* process the WITH clause */
618 if (selectStmt->withClause)
620 qry->hasRecursive = selectStmt->withClause->recursive;
621 qry->cteList = transformWithClause(pstate, selectStmt->withClause);
624 /* Do basic expression transformation (same as a ROW() expr) */
625 exprList = transformExpressionList(pstate,
626 (List *) linitial(valuesLists));
628 /* Prepare row for assignment to target table */
629 exprList = transformInsertRow(pstate, exprList,
635 * Generate query's target list using the computed list of expressions.
636 * Also, mark all the target columns as needing insert permissions.
638 rte = pstate->p_target_rangetblentry;
639 qry->targetList = NIL;
640 icols = list_head(icolumns);
641 attnos = list_head(attrnos);
642 foreach(lc, exprList)
644 Expr *expr = (Expr *) lfirst(lc);
649 col = (ResTarget *) lfirst(icols);
650 Assert(IsA(col, ResTarget));
651 attr_num = (AttrNumber) lfirst_int(attnos);
653 tle = makeTargetEntry(expr,
657 qry->targetList = lappend(qry->targetList, tle);
659 rte->modifiedCols = bms_add_member(rte->modifiedCols,
660 attr_num - FirstLowInvalidHeapAttributeNumber);
662 icols = lnext(icols);
663 attnos = lnext(attnos);
667 * If we have a RETURNING clause, we need to add the target relation to
668 * the query namespace before processing it, so that Var references in
669 * RETURNING will work. Also, remove any namespace entries added in a
670 * sub-SELECT or VALUES list.
672 if (stmt->returningList)
674 pstate->p_relnamespace = NIL;
675 pstate->p_varnamespace = NIL;
676 addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
678 qry->returningList = transformReturningList(pstate,
679 stmt->returningList);
682 /* done building the range table and jointree */
683 qry->rtable = pstate->p_rtable;
684 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
686 qry->hasSubLinks = pstate->p_hasSubLinks;
687 /* aggregates not allowed (but subselects are okay) */
688 if (pstate->p_hasAggs)
690 (errcode(ERRCODE_GROUPING_ERROR),
691 errmsg("cannot use aggregate function in VALUES"),
692 parser_errposition(pstate,
693 locate_agg_of_level((Node *) qry, 0))));
694 if (pstate->p_hasWindowFuncs)
696 (errcode(ERRCODE_WINDOWING_ERROR),
697 errmsg("cannot use window function in VALUES"),
698 parser_errposition(pstate,
699 locate_windowfunc((Node *) qry))));
705 * Prepare an INSERT row for assignment to the target table.
707 * The row might be either a VALUES row, or variables referencing a
711 transformInsertRow(ParseState *pstate, List *exprlist,
712 List *stmtcols, List *icolumns, List *attrnos)
720 * Check length of expr list. It must not have more expressions than
721 * there are target columns. We allow fewer, but only if no explicit
722 * columns list was given (the remaining columns are implicitly
723 * defaulted). Note we must check this *after* transformation because
724 * that could expand '*' into multiple items.
726 if (list_length(exprlist) > list_length(icolumns))
728 (errcode(ERRCODE_SYNTAX_ERROR),
729 errmsg("INSERT has more expressions than target columns"),
730 parser_errposition(pstate,
731 exprLocation(list_nth(exprlist,
732 list_length(icolumns))))));
733 if (stmtcols != NIL &&
734 list_length(exprlist) < list_length(icolumns))
736 (errcode(ERRCODE_SYNTAX_ERROR),
737 errmsg("INSERT has more target columns than expressions"),
738 parser_errposition(pstate,
739 exprLocation(list_nth(icolumns,
740 list_length(exprlist))))));
743 * Prepare columns for assignment to target table.
746 icols = list_head(icolumns);
747 attnos = list_head(attrnos);
748 foreach(lc, exprlist)
750 Expr *expr = (Expr *) lfirst(lc);
753 col = (ResTarget *) lfirst(icols);
754 Assert(IsA(col, ResTarget));
756 expr = transformAssignedExpr(pstate, expr,
762 result = lappend(result, expr);
764 icols = lnext(icols);
765 attnos = lnext(attnos);
773 * transformSelectStmt -
774 * transforms a Select Statement
776 * Note: this covers only cases with no set operations and no VALUES lists;
777 * see below for the other cases.
780 transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
782 Query *qry = makeNode(Query);
786 qry->commandType = CMD_SELECT;
788 /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
789 pstate->p_locking_clause = stmt->lockingClause;
791 /* make WINDOW info available for window functions, too */
792 pstate->p_windowdefs = stmt->windowClause;
794 /* process the WITH clause */
795 if (stmt->withClause)
797 qry->hasRecursive = stmt->withClause->recursive;
798 qry->cteList = transformWithClause(pstate, stmt->withClause);
801 /* process the FROM clause */
802 transformFromClause(pstate, stmt->fromClause);
804 /* transform targetlist */
805 qry->targetList = transformTargetList(pstate, stmt->targetList);
807 /* mark column origins */
808 markTargetListOrigins(pstate, qry->targetList);
810 /* transform WHERE */
811 qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
814 * Initial processing of HAVING clause is just like WHERE clause.
816 qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
820 * Transform sorting/grouping stuff. Do ORDER BY first because both
821 * transformGroupClause and transformDistinctClause need the results. Note
822 * that these functions can also change the targetList, so it's passed to
825 qry->sortClause = transformSortClause(pstate,
828 true /* fix unknowns */,
829 false /* not window function */);
831 qry->groupClause = transformGroupClause(pstate,
835 false /* not window function */);
837 if (stmt->distinctClause == NIL)
839 qry->distinctClause = NIL;
840 qry->hasDistinctOn = false;
842 else if (linitial(stmt->distinctClause) == NULL)
844 /* We had SELECT DISTINCT */
845 qry->distinctClause = transformDistinctClause(pstate,
848 qry->hasDistinctOn = false;
852 /* We had SELECT DISTINCT ON */
853 qry->distinctClause = transformDistinctOnClause(pstate,
854 stmt->distinctClause,
857 qry->hasDistinctOn = true;
860 /* transform LIMIT */
861 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
863 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
866 /* transform window clauses after we have seen all window functions */
867 qry->windowClause = transformWindowDefinitions(pstate,
868 pstate->p_windowdefs,
871 /* handle any SELECT INTO/CREATE TABLE AS spec */
872 if (stmt->intoClause)
874 qry->intoClause = stmt->intoClause;
875 if (stmt->intoClause->colNames)
876 applyColumnNames(qry->targetList, stmt->intoClause->colNames);
879 qry->rtable = pstate->p_rtable;
880 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
882 qry->hasSubLinks = pstate->p_hasSubLinks;
883 qry->hasAggs = pstate->p_hasAggs;
884 if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
885 parseCheckAggregates(pstate, qry);
886 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
887 if (pstate->p_hasWindowFuncs)
888 parseCheckWindowFuncs(pstate, qry);
890 foreach(l, stmt->lockingClause)
892 transformLockingClause(pstate, qry, (LockingClause *) lfirst(l));
899 * transformValuesClause -
900 * transforms a VALUES clause that's being used as a standalone SELECT
902 * We build a Query containing a VALUES RTE, rather as if one had written
903 * SELECT * FROM (VALUES ...)
906 transformValuesClause(ParseState *pstate, SelectStmt *stmt)
908 Query *qry = makeNode(Query);
909 List *exprsLists = NIL;
910 List **colexprs = NULL;
911 Oid *coltypes = NULL;
912 int sublist_length = -1;
920 qry->commandType = CMD_SELECT;
922 /* Most SELECT stuff doesn't apply in a VALUES clause */
923 Assert(stmt->distinctClause == NIL);
924 Assert(stmt->targetList == NIL);
925 Assert(stmt->fromClause == NIL);
926 Assert(stmt->whereClause == NULL);
927 Assert(stmt->groupClause == NIL);
928 Assert(stmt->havingClause == NULL);
929 Assert(stmt->windowClause == NIL);
930 Assert(stmt->op == SETOP_NONE);
932 /* process the WITH clause */
933 if (stmt->withClause)
935 qry->hasRecursive = stmt->withClause->recursive;
936 qry->cteList = transformWithClause(pstate, stmt->withClause);
940 * For each row of VALUES, transform the raw expressions and gather type
941 * information. This is also a handy place to reject DEFAULT nodes, which
942 * the grammar allows for simplicity.
944 foreach(lc, stmt->valuesLists)
946 List *sublist = (List *) lfirst(lc);
948 /* Do basic expression transformation (same as a ROW() expr) */
949 sublist = transformExpressionList(pstate, sublist);
952 * All the sublists must be the same length, *after* transformation
953 * (which might expand '*' into multiple items). The VALUES RTE can't
954 * handle anything different.
956 if (sublist_length < 0)
958 /* Remember post-transformation length of first sublist */
959 sublist_length = list_length(sublist);
960 /* and allocate arrays for per-column info */
961 colexprs = (List **) palloc0(sublist_length * sizeof(List *));
962 coltypes = (Oid *) palloc0(sublist_length * sizeof(Oid));
964 else if (sublist_length != list_length(sublist))
967 (errcode(ERRCODE_SYNTAX_ERROR),
968 errmsg("VALUES lists must all be the same length"),
969 parser_errposition(pstate,
970 exprLocation((Node *) sublist))));
973 exprsLists = lappend(exprsLists, sublist);
975 /* Check for DEFAULT and build per-column expression lists */
977 foreach(lc2, sublist)
979 Node *col = (Node *) lfirst(lc2);
981 if (IsA(col, SetToDefault))
983 (errcode(ERRCODE_SYNTAX_ERROR),
984 errmsg("DEFAULT can only appear in a VALUES list within INSERT"),
985 parser_errposition(pstate, exprLocation(col))));
986 colexprs[i] = lappend(colexprs[i], col);
992 * Now resolve the common types of the columns, and coerce everything to
995 for (i = 0; i < sublist_length; i++)
997 coltypes[i] = select_common_type(pstate, colexprs[i], "VALUES", NULL);
1000 newExprsLists = NIL;
1001 foreach(lc, exprsLists)
1003 List *sublist = (List *) lfirst(lc);
1004 List *newsublist = NIL;
1007 foreach(lc2, sublist)
1009 Node *col = (Node *) lfirst(lc2);
1011 col = coerce_to_common_type(pstate, col, coltypes[i], "VALUES");
1012 newsublist = lappend(newsublist, col);
1016 newExprsLists = lappend(newExprsLists, newsublist);
1020 * Generate the VALUES RTE
1022 rte = addRangeTableEntryForValues(pstate, newExprsLists, NULL, true);
1023 rtr = makeNode(RangeTblRef);
1024 /* assume new rte is at end */
1025 rtr->rtindex = list_length(pstate->p_rtable);
1026 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1027 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
1028 pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
1031 * Generate a targetlist as though expanding "*"
1033 Assert(pstate->p_next_resno == 1);
1034 qry->targetList = expandRelAttrs(pstate, rte, rtr->rtindex, 0, -1);
1037 * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1040 qry->sortClause = transformSortClause(pstate,
1043 true /* fix unknowns */,
1044 false /* not window function */);
1046 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1048 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1051 if (stmt->lockingClause)
1053 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1054 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES")));
1056 /* handle any CREATE TABLE AS spec */
1057 if (stmt->intoClause)
1059 qry->intoClause = stmt->intoClause;
1060 if (stmt->intoClause->colNames)
1061 applyColumnNames(qry->targetList, stmt->intoClause->colNames);
1065 * There mustn't have been any table references in the expressions, else
1066 * strange things would happen, like Cartesian products of those tables
1067 * with the VALUES list. We have to check this after parsing ORDER BY et
1068 * al since those could insert more junk.
1070 if (list_length(pstate->p_joinlist) != 1)
1072 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1073 errmsg("VALUES must not contain table references"),
1074 parser_errposition(pstate,
1075 locate_var_of_level((Node *) newExprsLists, 0))));
1078 * Another thing we can't currently support is NEW/OLD references in rules
1079 * --- seems we'd need something like SQL99's LATERAL construct to ensure
1080 * that the values would be available while evaluating the VALUES RTE.
1081 * This is a shame. FIXME
1083 if (list_length(pstate->p_rtable) != 1 &&
1084 contain_vars_of_level((Node *) newExprsLists, 0))
1086 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1087 errmsg("VALUES must not contain OLD or NEW references"),
1088 errhint("Use SELECT ... UNION ALL ... instead."),
1089 parser_errposition(pstate,
1090 locate_var_of_level((Node *) newExprsLists, 0))));
1092 qry->rtable = pstate->p_rtable;
1093 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1095 qry->hasSubLinks = pstate->p_hasSubLinks;
1096 /* aggregates not allowed (but subselects are okay) */
1097 if (pstate->p_hasAggs)
1099 (errcode(ERRCODE_GROUPING_ERROR),
1100 errmsg("cannot use aggregate function in VALUES"),
1101 parser_errposition(pstate,
1102 locate_agg_of_level((Node *) newExprsLists, 0))));
1103 if (pstate->p_hasWindowFuncs)
1105 (errcode(ERRCODE_WINDOWING_ERROR),
1106 errmsg("cannot use window function in VALUES"),
1107 parser_errposition(pstate,
1108 locate_windowfunc((Node *) newExprsLists))));
1114 * transformSetOperationStmt -
1115 * transforms a set-operations tree
1117 * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
1118 * structure to it. We must transform each leaf SELECT and build up a top-
1119 * level Query that contains the leaf SELECTs as subqueries in its rangetable.
1120 * The tree of set operations is converted into the setOperations field of
1121 * the top-level Query.
1124 transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
1126 Query *qry = makeNode(Query);
1127 SelectStmt *leftmostSelect;
1129 Query *leftmostQuery;
1130 SetOperationStmt *sostmt;
1132 List *intoColNames = NIL;
1136 List *lockingClause;
1138 ListCell *left_tlist,
1146 int sv_rtable_length;
1147 RangeTblEntry *jrte;
1150 qry->commandType = CMD_SELECT;
1153 * Find leftmost leaf SelectStmt; extract the one-time-only items from it
1154 * and from the top-level node.
1156 leftmostSelect = stmt->larg;
1157 while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1158 leftmostSelect = leftmostSelect->larg;
1159 Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1160 leftmostSelect->larg == NULL);
1161 if (leftmostSelect->intoClause)
1163 qry->intoClause = leftmostSelect->intoClause;
1164 intoColNames = leftmostSelect->intoClause->colNames;
1167 /* clear this to prevent complaints in transformSetOperationTree() */
1168 leftmostSelect->intoClause = NULL;
1171 * These are not one-time, exactly, but we want to process them here and
1172 * not let transformSetOperationTree() see them --- else it'll just
1173 * recurse right back here!
1175 sortClause = stmt->sortClause;
1176 limitOffset = stmt->limitOffset;
1177 limitCount = stmt->limitCount;
1178 lockingClause = stmt->lockingClause;
1180 stmt->sortClause = NIL;
1181 stmt->limitOffset = NULL;
1182 stmt->limitCount = NULL;
1183 stmt->lockingClause = NIL;
1185 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1188 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1189 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1191 /* process the WITH clause */
1192 if (stmt->withClause)
1194 qry->hasRecursive = stmt->withClause->recursive;
1195 qry->cteList = transformWithClause(pstate, stmt->withClause);
1199 * Recursively transform the components of the tree.
1201 sostmt = (SetOperationStmt *) transformSetOperationTree(pstate, stmt,
1203 Assert(sostmt && IsA(sostmt, SetOperationStmt));
1204 qry->setOperations = (Node *) sostmt;
1207 * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1209 node = sostmt->larg;
1210 while (node && IsA(node, SetOperationStmt))
1211 node = ((SetOperationStmt *) node)->larg;
1212 Assert(node && IsA(node, RangeTblRef));
1213 leftmostRTI = ((RangeTblRef *) node)->rtindex;
1214 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1215 Assert(leftmostQuery != NULL);
1218 * Generate dummy targetlist for outer query using column names of
1219 * leftmost select and common datatypes of topmost set operation. Also
1220 * make lists of the dummy vars and their names for use in parsing ORDER
1223 * Note: we use leftmostRTI as the varno of the dummy variables. It
1224 * shouldn't matter too much which RT index they have, as long as they
1225 * have one that corresponds to a real RT entry; else funny things may
1226 * happen when the tree is mashed by rule rewriting.
1228 qry->targetList = NIL;
1231 left_tlist = list_head(leftmostQuery->targetList);
1233 forboth(lct, sostmt->colTypes, lcm, sostmt->colTypmods)
1235 Oid colType = lfirst_oid(lct);
1236 int32 colTypmod = lfirst_int(lcm);
1237 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1242 Assert(!lefttle->resjunk);
1243 colName = pstrdup(lefttle->resname);
1244 var = makeVar(leftmostRTI,
1249 var->location = exprLocation((Node *) lefttle->expr);
1250 tle = makeTargetEntry((Expr *) var,
1251 (AttrNumber) pstate->p_next_resno++,
1254 qry->targetList = lappend(qry->targetList, tle);
1255 targetvars = lappend(targetvars, var);
1256 targetnames = lappend(targetnames, makeString(colName));
1257 left_tlist = lnext(left_tlist);
1261 * As a first step towards supporting sort clauses that are expressions
1262 * using the output columns, generate a varnamespace entry that makes the
1263 * output columns visible. A Join RTE node is handy for this, since we
1264 * can easily control the Vars generated upon matches.
1266 * Note: we don't yet do anything useful with such cases, but at least
1267 * "ORDER BY upper(foo)" will draw the right error message rather than
1270 sv_rtable_length = list_length(pstate->p_rtable);
1272 jrte = addRangeTableEntryForJoin(pstate,
1279 sv_relnamespace = pstate->p_relnamespace;
1280 pstate->p_relnamespace = NIL; /* no qualified names allowed */
1282 sv_varnamespace = pstate->p_varnamespace;
1283 pstate->p_varnamespace = list_make1(jrte);
1286 * For now, we don't support resjunk sort clauses on the output of a
1287 * setOperation tree --- you can only use the SQL92-spec options of
1288 * selecting an output column by name or number. Enforce by checking that
1289 * transformSortClause doesn't add any items to tlist.
1291 tllen = list_length(qry->targetList);
1293 qry->sortClause = transformSortClause(pstate,
1296 false /* no unknowns expected */,
1297 false /* not window function */);
1299 pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1300 pstate->p_relnamespace = sv_relnamespace;
1301 pstate->p_varnamespace = sv_varnamespace;
1303 if (tllen != list_length(qry->targetList))
1305 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1306 errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
1307 errdetail("Only result column names can be used, not expressions or functions."),
1308 errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
1309 parser_errposition(pstate,
1310 exprLocation(list_nth(qry->targetList, tllen)))));
1312 qry->limitOffset = transformLimitClause(pstate, limitOffset,
1314 qry->limitCount = transformLimitClause(pstate, limitCount,
1318 * Handle SELECT INTO/CREATE TABLE AS.
1320 * Any column names from CREATE TABLE AS need to be attached to both the
1321 * top level and the leftmost subquery. We do not do this earlier because
1322 * we do *not* want sortClause processing to be affected.
1326 applyColumnNames(qry->targetList, intoColNames);
1327 applyColumnNames(leftmostQuery->targetList, intoColNames);
1330 qry->rtable = pstate->p_rtable;
1331 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1333 qry->hasSubLinks = pstate->p_hasSubLinks;
1334 qry->hasAggs = pstate->p_hasAggs;
1335 if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
1336 parseCheckAggregates(pstate, qry);
1337 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1338 if (pstate->p_hasWindowFuncs)
1339 parseCheckWindowFuncs(pstate, qry);
1341 foreach(l, lockingClause)
1343 transformLockingClause(pstate, qry, (LockingClause *) lfirst(l));
1350 * transformSetOperationTree
1351 * Recursively transform leaves and internal nodes of a set-op tree
1353 * In addition to returning the transformed node, we return a list of
1354 * expression nodes showing the type, typmod, and location (for error messages)
1355 * of each output column of the set-op node. This is used only during the
1356 * internal recursion of this function. At the upper levels we use
1357 * SetToDefault nodes for this purpose, since they carry exactly the fields
1358 * needed, but any other expression node type would do as well.
1361 transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
1366 Assert(stmt && IsA(stmt, SelectStmt));
1369 * Validity-check both leaf and internal SELECTs for disallowed ops.
1371 if (stmt->intoClause)
1373 (errcode(ERRCODE_SYNTAX_ERROR),
1374 errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
1375 parser_errposition(pstate,
1376 exprLocation((Node *) stmt->intoClause))));
1378 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1379 if (stmt->lockingClause)
1381 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1382 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1385 * If an internal node of a set-op tree has ORDER BY, UPDATE, or LIMIT
1386 * clauses attached, we need to treat it like a leaf node to generate an
1387 * independent sub-Query tree. Otherwise, it can be represented by a
1388 * SetOperationStmt node underneath the parent Query.
1390 if (stmt->op == SETOP_NONE)
1392 Assert(stmt->larg == NULL && stmt->rarg == NULL);
1397 Assert(stmt->larg != NULL && stmt->rarg != NULL);
1398 if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
1399 stmt->lockingClause)
1407 /* Process leaf SELECT */
1409 char selectName[32];
1415 * Transform SelectStmt into a Query.
1417 * Note: previously transformed sub-queries don't affect the parsing
1418 * of this sub-query, because they are not in the toplevel pstate's
1421 selectQuery = parse_sub_analyze((Node *) stmt, pstate);
1424 * Check for bogus references to Vars on the current query level (but
1425 * upper-level references are okay). Normally this can't happen
1426 * because the namespace will be empty, but it could happen if we are
1429 if (pstate->p_relnamespace || pstate->p_varnamespace)
1431 if (contain_vars_of_level((Node *) selectQuery, 1))
1433 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1434 errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
1435 parser_errposition(pstate,
1436 locate_var_of_level((Node *) selectQuery, 1))));
1440 * Extract a list of the result expressions for upper-level checking.
1443 foreach(tl, selectQuery->targetList)
1445 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1448 *colInfo = lappend(*colInfo, tle->expr);
1452 * Make the leaf query be a subquery in the top-level rangetable.
1454 snprintf(selectName, sizeof(selectName), "*SELECT* %d",
1455 list_length(pstate->p_rtable) + 1);
1456 rte = addRangeTableEntryForSubquery(pstate,
1458 makeAlias(selectName, NIL),
1462 * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
1464 rtr = makeNode(RangeTblRef);
1465 /* assume new rte is at end */
1466 rtr->rtindex = list_length(pstate->p_rtable);
1467 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1468 return (Node *) rtr;
1472 /* Process an internal node (set operation node) */
1473 SetOperationStmt *op = makeNode(SetOperationStmt);
1478 const char *context;
1480 context = (stmt->op == SETOP_UNION ? "UNION" :
1481 (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
1485 op->all = stmt->all;
1488 * Recursively transform the child nodes.
1490 op->larg = transformSetOperationTree(pstate, stmt->larg,
1492 op->rarg = transformSetOperationTree(pstate, stmt->rarg,
1496 * Verify that the two children have the same number of non-junk
1497 * columns, and determine the types of the merged output columns.
1499 if (list_length(lcolinfo) != list_length(rcolinfo))
1501 (errcode(ERRCODE_SYNTAX_ERROR),
1502 errmsg("each %s query must have the same number of columns",
1504 parser_errposition(pstate,
1505 exprLocation((Node *) rcolinfo))));
1509 op->colTypmods = NIL;
1510 op->groupClauses = NIL;
1511 forboth(lci, lcolinfo, rci, rcolinfo)
1513 Node *lcolinfo = (Node *) lfirst(lci);
1514 Node *rcolinfo = (Node *) lfirst(rci);
1515 Oid lcoltype = exprType(lcolinfo);
1516 Oid rcoltype = exprType(rcolinfo);
1517 int32 lcoltypmod = exprTypmod(lcolinfo);
1518 int32 rcoltypmod = exprTypmod(rcolinfo);
1520 SetToDefault *rescolinfo;
1524 /* select common type, same as CASE et al */
1525 rescoltype = select_common_type(pstate,
1526 list_make2(lcolinfo, rcolinfo),
1529 /* if same type and same typmod, use typmod; else default */
1530 if (lcoltype == rcoltype && lcoltypmod == rcoltypmod)
1531 rescoltypmod = lcoltypmod;
1535 /* verify the coercions are actually possible */
1536 (void) coerce_to_common_type(pstate, lcolinfo,
1537 rescoltype, context);
1538 (void) coerce_to_common_type(pstate, rcolinfo,
1539 rescoltype, context);
1542 rescolinfo = makeNode(SetToDefault);
1543 rescolinfo->typeId = rescoltype;
1544 rescolinfo->typeMod = rescoltypmod;
1545 rescolinfo->location = exprLocation(bestexpr);
1546 *colInfo = lappend(*colInfo, rescolinfo);
1548 op->colTypes = lappend_oid(op->colTypes, rescoltype);
1549 op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
1552 * For all cases except UNION ALL, identify the grouping operators
1553 * (and, if available, sorting operators) that will be used to
1554 * eliminate duplicates.
1556 if (op->op != SETOP_UNION || !op->all)
1558 SortGroupClause *grpcl = makeNode(SortGroupClause);
1561 ParseCallbackState pcbstate;
1563 setup_parser_errposition_callback(&pcbstate, pstate,
1564 rescolinfo->location);
1566 /* determine the eqop and optional sortop */
1567 get_sort_group_operators(rescoltype,
1569 &sortop, &eqop, NULL);
1571 cancel_parser_errposition_callback(&pcbstate);
1573 /* we don't have a tlist yet, so can't assign sortgrouprefs */
1574 grpcl->tleSortGroupRef = 0;
1576 grpcl->sortop = sortop;
1577 grpcl->nulls_first = false; /* OK with or without sortop */
1579 op->groupClauses = lappend(op->groupClauses, grpcl);
1588 * Attach column names from a ColumnDef list to a TargetEntry list
1589 * (for CREATE TABLE AS)
1592 applyColumnNames(List *dst, List *src)
1597 src_item = list_head(src);
1599 foreach(dst_item, dst)
1601 TargetEntry *d = (TargetEntry *) lfirst(dst_item);
1604 /* junk targets don't count */
1608 /* fewer ColumnDefs than target entries is OK */
1609 if (src_item == NULL)
1612 s = (ColumnDef *) lfirst(src_item);
1613 src_item = lnext(src_item);
1615 d->resname = pstrdup(s->colname);
1618 /* more ColumnDefs than target entries is not OK */
1619 if (src_item != NULL)
1621 (errcode(ERRCODE_SYNTAX_ERROR),
1622 errmsg("CREATE TABLE AS specifies too many column names")));
1627 * transformUpdateStmt -
1628 * transforms an update statement
1631 transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
1633 Query *qry = makeNode(Query);
1634 RangeTblEntry *target_rte;
1636 ListCell *origTargetList;
1639 qry->commandType = CMD_UPDATE;
1640 pstate->p_is_update = true;
1642 qry->resultRelation = setTargetTable(pstate, stmt->relation,
1643 interpretInhOption(stmt->relation->inhOpt),
1648 * the FROM clause is non-standard SQL syntax. We used to be able to do
1649 * this with REPLACE in POSTQUEL so we keep the feature.
1651 transformFromClause(pstate, stmt->fromClause);
1653 qry->targetList = transformTargetList(pstate, stmt->targetList);
1655 qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
1657 qry->returningList = transformReturningList(pstate, stmt->returningList);
1659 qry->rtable = pstate->p_rtable;
1660 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1662 qry->hasSubLinks = pstate->p_hasSubLinks;
1665 * Top-level aggregates are simply disallowed in UPDATE, per spec. (From
1666 * an implementation point of view, this is forced because the implicit
1667 * ctid reference would otherwise be an ungrouped variable.)
1669 if (pstate->p_hasAggs)
1671 (errcode(ERRCODE_GROUPING_ERROR),
1672 errmsg("cannot use aggregate function in UPDATE"),
1673 parser_errposition(pstate,
1674 locate_agg_of_level((Node *) qry, 0))));
1675 if (pstate->p_hasWindowFuncs)
1677 (errcode(ERRCODE_WINDOWING_ERROR),
1678 errmsg("cannot use window function in UPDATE"),
1679 parser_errposition(pstate,
1680 locate_windowfunc((Node *) qry))));
1683 * Now we are done with SELECT-like processing, and can get on with
1684 * transforming the target list to match the UPDATE target columns.
1687 /* Prepare to assign non-conflicting resnos to resjunk attributes */
1688 if (pstate->p_next_resno <= pstate->p_target_relation->rd_rel->relnatts)
1689 pstate->p_next_resno = pstate->p_target_relation->rd_rel->relnatts + 1;
1691 /* Prepare non-junk columns for assignment to target table */
1692 target_rte = pstate->p_target_rangetblentry;
1693 origTargetList = list_head(stmt->targetList);
1695 foreach(tl, qry->targetList)
1697 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1698 ResTarget *origTarget;
1704 * Resjunk nodes need no additional processing, but be sure they
1705 * have resnos that do not match any target columns; else rewriter
1706 * or planner might get confused. They don't need a resname
1709 tle->resno = (AttrNumber) pstate->p_next_resno++;
1710 tle->resname = NULL;
1713 if (origTargetList == NULL)
1714 elog(ERROR, "UPDATE target count mismatch --- internal error");
1715 origTarget = (ResTarget *) lfirst(origTargetList);
1716 Assert(IsA(origTarget, ResTarget));
1718 attrno = attnameAttNum(pstate->p_target_relation,
1719 origTarget->name, true);
1720 if (attrno == InvalidAttrNumber)
1722 (errcode(ERRCODE_UNDEFINED_COLUMN),
1723 errmsg("column \"%s\" of relation \"%s\" does not exist",
1725 RelationGetRelationName(pstate->p_target_relation)),
1726 parser_errposition(pstate, origTarget->location)));
1728 updateTargetListEntry(pstate, tle, origTarget->name,
1730 origTarget->indirection,
1731 origTarget->location);
1733 /* Mark the target column as requiring update permissions */
1734 target_rte->modifiedCols = bms_add_member(target_rte->modifiedCols,
1735 attrno - FirstLowInvalidHeapAttributeNumber);
1737 origTargetList = lnext(origTargetList);
1739 if (origTargetList != NULL)
1740 elog(ERROR, "UPDATE target count mismatch --- internal error");
1746 * transformReturningList -
1747 * handle a RETURNING clause in INSERT/UPDATE/DELETE
1750 transformReturningList(ParseState *pstate, List *returningList)
1753 int save_next_resno;
1755 bool save_hasWindowFuncs;
1758 if (returningList == NIL)
1759 return NIL; /* nothing to do */
1762 * We need to assign resnos starting at one in the RETURNING list. Save
1763 * and restore the main tlist's value of p_next_resno, just in case
1764 * someone looks at it later (probably won't happen).
1766 save_next_resno = pstate->p_next_resno;
1767 pstate->p_next_resno = 1;
1769 /* save other state so that we can detect disallowed stuff */
1770 save_hasAggs = pstate->p_hasAggs;
1771 pstate->p_hasAggs = false;
1772 save_hasWindowFuncs = pstate->p_hasWindowFuncs;
1773 pstate->p_hasWindowFuncs = false;
1774 length_rtable = list_length(pstate->p_rtable);
1776 /* transform RETURNING identically to a SELECT targetlist */
1777 rlist = transformTargetList(pstate, returningList);
1779 /* check for disallowed stuff */
1781 /* aggregates not allowed (but subselects are okay) */
1782 if (pstate->p_hasAggs)
1784 (errcode(ERRCODE_GROUPING_ERROR),
1785 errmsg("cannot use aggregate function in RETURNING"),
1786 parser_errposition(pstate,
1787 locate_agg_of_level((Node *) rlist, 0))));
1788 if (pstate->p_hasWindowFuncs)
1790 (errcode(ERRCODE_WINDOWING_ERROR),
1791 errmsg("cannot use window function in RETURNING"),
1792 parser_errposition(pstate,
1793 locate_windowfunc((Node *) rlist))));
1795 /* no new relation references please */
1796 if (list_length(pstate->p_rtable) != length_rtable)
1801 /* try to locate such a reference to point to */
1802 for (relid = length_rtable + 1; relid <= list_length(pstate->p_rtable); relid++)
1804 vlocation = locate_var_of_relation((Node *) rlist, relid, 0);
1809 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1810 errmsg("RETURNING cannot contain references to other relations"),
1811 parser_errposition(pstate, vlocation)));
1814 /* mark column origins */
1815 markTargetListOrigins(pstate, rlist);
1818 pstate->p_next_resno = save_next_resno;
1819 pstate->p_hasAggs = save_hasAggs;
1820 pstate->p_hasWindowFuncs = save_hasWindowFuncs;
1827 * transformDeclareCursorStmt -
1828 * transform a DECLARE CURSOR Statement
1830 * DECLARE CURSOR is a hybrid case: it's an optimizable statement (in fact not
1831 * significantly different from a SELECT) as far as parsing/rewriting/planning
1832 * are concerned, but it's not passed to the executor and so in that sense is
1833 * a utility statement. We transform it into a Query exactly as if it were
1834 * a SELECT, then stick the original DeclareCursorStmt into the utilityStmt
1835 * field to carry the cursor name and options.
1838 transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
1843 * Don't allow both SCROLL and NO SCROLL to be specified
1845 if ((stmt->options & CURSOR_OPT_SCROLL) &&
1846 (stmt->options & CURSOR_OPT_NO_SCROLL))
1848 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
1849 errmsg("cannot specify both SCROLL and NO SCROLL")));
1851 result = transformStmt(pstate, stmt->query);
1853 /* Grammar should not have allowed anything but SELECT */
1854 if (!IsA(result, Query) ||
1855 result->commandType != CMD_SELECT ||
1856 result->utilityStmt != NULL)
1857 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
1859 /* But we must explicitly disallow DECLARE CURSOR ... SELECT INTO */
1860 if (result->intoClause)
1862 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
1863 errmsg("DECLARE CURSOR cannot specify INTO"),
1864 parser_errposition(pstate,
1865 exprLocation((Node *) result->intoClause))));
1867 /* FOR UPDATE and WITH HOLD are not compatible */
1868 if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
1870 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1871 errmsg("DECLARE CURSOR WITH HOLD ... FOR UPDATE/SHARE is not supported"),
1872 errdetail("Holdable cursors must be READ ONLY.")));
1874 /* FOR UPDATE and SCROLL are not compatible */
1875 if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
1877 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1878 errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
1879 errdetail("Scrollable cursors must be READ ONLY.")));
1881 /* FOR UPDATE and INSENSITIVE are not compatible */
1882 if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
1884 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1885 errmsg("DECLARE INSENSITIVE CURSOR ... FOR UPDATE/SHARE is not supported"),
1886 errdetail("Insensitive cursors must be READ ONLY.")));
1888 /* We won't need the raw querytree any more */
1891 result->utilityStmt = (Node *) stmt;
1898 * transformExplainStmt -
1899 * transform an EXPLAIN Statement
1901 * EXPLAIN is just like other utility statements in that we emit it as a
1902 * CMD_UTILITY Query node with no transformation of the raw parse tree.
1903 * However, if p_variableparams is set, it could be that the client is
1904 * expecting us to resolve parameter types in something like
1905 * EXPLAIN SELECT * FROM tab WHERE col = $1
1906 * To deal with such cases, we run parse analysis and throw away the result;
1907 * this is a bit grotty but not worth contorting the rest of the system for.
1908 * (The approach we use for DECLARE CURSOR won't work because the statement
1909 * being explained isn't necessarily a SELECT, and in particular might rewrite
1910 * to multiple parsetrees.)
1913 transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
1917 if (pstate->p_variableparams)
1919 /* Since parse analysis scribbles on its input, copy the tree first! */
1920 (void) transformStmt(pstate, copyObject(stmt->query));
1923 /* Now return the untransformed command as a utility Query */
1924 result = makeNode(Query);
1925 result->commandType = CMD_UTILITY;
1926 result->utilityStmt = (Node *) stmt;
1932 /* exported so planner can check again after rewriting, query pullup, etc */
1934 CheckSelectLocking(Query *qry)
1936 if (qry->setOperations)
1938 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1939 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1940 if (qry->distinctClause != NIL)
1942 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1943 errmsg("SELECT FOR UPDATE/SHARE is not allowed with DISTINCT clause")));
1944 if (qry->groupClause != NIL)
1946 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1947 errmsg("SELECT FOR UPDATE/SHARE is not allowed with GROUP BY clause")));
1948 if (qry->havingQual != NULL)
1950 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1951 errmsg("SELECT FOR UPDATE/SHARE is not allowed with HAVING clause")));
1954 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1955 errmsg("SELECT FOR UPDATE/SHARE is not allowed with aggregate functions")));
1956 if (qry->hasWindowFuncs)
1958 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1959 errmsg("SELECT FOR UPDATE/SHARE is not allowed with window functions")));
1963 * Transform a FOR UPDATE/SHARE clause
1965 * This basically involves replacing names by integer relids.
1967 * NB: if you need to change this, see also markQueryForLocking()
1968 * in rewriteHandler.c, and isLockedRel() in parse_relation.c.
1971 transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc)
1973 List *lockedRels = lc->lockedRels;
1977 LockingClause *allrels;
1979 CheckSelectLocking(qry);
1981 /* make a clause we can pass down to subqueries to select all rels */
1982 allrels = makeNode(LockingClause);
1983 allrels->lockedRels = NIL; /* indicates all rels */
1984 allrels->forUpdate = lc->forUpdate;
1985 allrels->noWait = lc->noWait;
1987 if (lockedRels == NIL)
1989 /* all regular tables used in query */
1991 foreach(rt, qry->rtable)
1993 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
1996 switch (rte->rtekind)
1999 applyLockingClause(qry, i, lc->forUpdate, lc->noWait);
2000 rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2005 * FOR UPDATE/SHARE of subquery is propagated to all of
2008 transformLockingClause(pstate, rte->subquery, allrels);
2013 * We allow FOR UPDATE/SHARE of a WITH query to be
2014 * propagated into the WITH, but it doesn't seem very
2015 * sane to allow this for a reference to an
2016 * outer-level WITH. And it definitely wouldn't work
2017 * for a self-reference, since we're not done
2018 * analyzing the CTE anyway.
2020 CommonTableExpr *cte;
2022 if (rte->ctelevelsup > 0 || rte->self_reference)
2024 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2025 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to an outer-level WITH query")));
2026 cte = GetCTEForRTE(pstate, rte, -1);
2027 /* should be analyzed by now */
2028 Assert(IsA(cte->ctequery, Query));
2029 transformLockingClause(pstate,
2030 (Query *) cte->ctequery,
2035 /* ignore JOIN, SPECIAL, FUNCTION RTEs */
2042 /* just the named tables */
2043 foreach(l, lockedRels)
2045 RangeVar *thisrel = (RangeVar *) lfirst(l);
2047 /* For simplicity we insist on unqualified alias names here */
2048 if (thisrel->catalogname || thisrel->schemaname)
2050 (errcode(ERRCODE_SYNTAX_ERROR),
2051 errmsg("SELECT FOR UPDATE/SHARE must specify unqualified relation names"),
2052 parser_errposition(pstate, thisrel->location)));
2055 foreach(rt, qry->rtable)
2057 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2060 if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
2062 switch (rte->rtekind)
2065 applyLockingClause(qry, i,
2066 lc->forUpdate, lc->noWait);
2067 rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2072 * FOR UPDATE/SHARE of subquery is propagated to
2073 * all of subquery's rels
2075 transformLockingClause(pstate, rte->subquery, allrels);
2079 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2080 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a join"),
2081 parser_errposition(pstate, thisrel->location)));
2085 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2086 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to NEW or OLD"),
2087 parser_errposition(pstate, thisrel->location)));
2091 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2092 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a function"),
2093 parser_errposition(pstate, thisrel->location)));
2097 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2098 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES"),
2099 parser_errposition(pstate, thisrel->location)));
2104 * We allow FOR UPDATE/SHARE of a WITH query
2105 * to be propagated into the WITH, but it
2106 * doesn't seem very sane to allow this for a
2107 * reference to an outer-level WITH. And it
2108 * definitely wouldn't work for a
2109 * self-reference, since we're not done
2110 * analyzing the CTE anyway.
2112 CommonTableExpr *cte;
2114 if (rte->ctelevelsup > 0 || rte->self_reference)
2116 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2117 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to an outer-level WITH query"),
2118 parser_errposition(pstate, thisrel->location)));
2119 cte = GetCTEForRTE(pstate, rte, -1);
2120 /* should be analyzed by now */
2121 Assert(IsA(cte->ctequery, Query));
2122 transformLockingClause(pstate,
2123 (Query *) cte->ctequery,
2128 elog(ERROR, "unrecognized RTE type: %d",
2129 (int) rte->rtekind);
2132 break; /* out of foreach loop */
2137 (errcode(ERRCODE_UNDEFINED_TABLE),
2138 errmsg("relation \"%s\" in FOR UPDATE/SHARE clause not found in FROM clause",
2140 parser_errposition(pstate, thisrel->location)));
2146 * Record locking info for a single rangetable item
2149 applyLockingClause(Query *qry, Index rtindex, bool forUpdate, bool noWait)
2153 /* Check for pre-existing entry for same rtindex */
2154 if ((rc = get_rowmark(qry, rtindex)) != NULL)
2157 * If the same RTE is specified both FOR UPDATE and FOR SHARE, treat
2158 * it as FOR UPDATE. (Reasonable, since you can't take both a shared
2159 * and exclusive lock at the same time; it'll end up being exclusive
2162 * We also consider that NOWAIT wins if it's specified both ways. This
2163 * is a bit more debatable but raising an error doesn't seem helpful.
2164 * (Consider for instance SELECT FOR UPDATE NOWAIT from a view that
2165 * internally contains a plain FOR UPDATE spec.)
2167 rc->forUpdate |= forUpdate;
2168 rc->noWait |= noWait;
2172 /* Make a new RowMarkClause */
2173 rc = makeNode(RowMarkClause);
2176 rc->forUpdate = forUpdate;
2177 rc->noWait = noWait;
2178 rc->isParent = false;
2179 qry->rowMarks = lappend(qry->rowMarks, rc);
2184 * Traverse a fully-analyzed tree to verify that parameter symbols
2185 * match their types. We need this because some Params might still
2186 * be UNKNOWN, if there wasn't anything to force their coercion,
2187 * and yet other instances seen later might have gotten coerced.
2190 check_parameter_resolution_walker(Node *node, ParseState *pstate)
2194 if (IsA(node, Param))
2196 Param *param = (Param *) node;
2198 if (param->paramkind == PARAM_EXTERN)
2200 int paramno = param->paramid;
2202 if (paramno <= 0 || /* shouldn't happen, but... */
2203 paramno > pstate->p_numparams)
2205 (errcode(ERRCODE_UNDEFINED_PARAMETER),
2206 errmsg("there is no parameter $%d", paramno),
2207 parser_errposition(pstate, param->location)));
2209 if (param->paramtype != pstate->p_paramtypes[paramno - 1])
2211 (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
2212 errmsg("could not determine data type of parameter $%d",
2214 parser_errposition(pstate, param->location)));
2218 if (IsA(node, Query))
2220 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
2221 return query_tree_walker((Query *) node,
2222 check_parameter_resolution_walker,
2223 (void *) pstate, 0);
2225 return expression_tree_walker(node, check_parameter_resolution_walker,