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-2010, PostgreSQL Global Development Group
18 * Portions Copyright (c) 1994, Regents of the University of California
20 * src/backend/parser/analyze.c
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_param.h"
39 #include "parser/parse_relation.h"
40 #include "parser/parse_target.h"
41 #include "parser/parsetree.h"
42 #include "rewrite/rewriteManip.h"
43 #include "utils/rel.h"
46 static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
47 static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
48 static List *transformInsertRow(ParseState *pstate, List *exprlist,
49 List *stmtcols, List *icolumns, List *attrnos);
50 static int count_rowexpr_columns(ParseState *pstate, Node *expr);
51 static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
52 static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
53 static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
54 static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
55 bool isTopLevel, List **colInfo);
56 static void determineRecursiveColTypes(ParseState *pstate,
57 Node *larg, List *lcolinfo);
58 static void applyColumnNames(List *dst, List *src);
59 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
60 static List *transformReturningList(ParseState *pstate, List *returningList);
61 static Query *transformDeclareCursorStmt(ParseState *pstate,
62 DeclareCursorStmt *stmt);
63 static Query *transformExplainStmt(ParseState *pstate,
65 static void transformLockingClause(ParseState *pstate, Query *qry,
66 LockingClause *lc, bool pushedDown);
71 * Analyze a raw parse tree and transform it to Query form.
73 * Optionally, information about $n parameter types can be supplied.
74 * References to $n indexes not defined by paramTypes[] are disallowed.
76 * The result is a Query node. Optimizable statements require considerable
77 * transformation, while utility-type statements are simply hung off
78 * a dummy CMD_UTILITY Query node.
81 parse_analyze(Node *parseTree, const char *sourceText,
82 Oid *paramTypes, int numParams)
84 ParseState *pstate = make_parsestate(NULL);
87 Assert(sourceText != NULL); /* required as of 8.4 */
89 pstate->p_sourcetext = sourceText;
92 parse_fixed_parameters(pstate, paramTypes, numParams);
94 query = transformStmt(pstate, parseTree);
96 free_parsestate(pstate);
102 * parse_analyze_varparams
104 * This variant is used when it's okay to deduce information about $n
105 * symbol datatypes from context. The passed-in paramTypes[] array can
106 * be modified or enlarged (via repalloc).
109 parse_analyze_varparams(Node *parseTree, const char *sourceText,
110 Oid **paramTypes, int *numParams)
112 ParseState *pstate = make_parsestate(NULL);
115 Assert(sourceText != NULL); /* required as of 8.4 */
117 pstate->p_sourcetext = sourceText;
119 parse_variable_parameters(pstate, paramTypes, numParams);
121 query = transformStmt(pstate, parseTree);
123 /* make sure all is well with parameter types */
124 check_variable_parameters(pstate, query);
126 free_parsestate(pstate);
133 * Entry point for recursively analyzing a sub-statement.
136 parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
137 CommonTableExpr *parentCTE,
138 bool locked_from_parent)
140 ParseState *pstate = make_parsestate(parentParseState);
143 pstate->p_parent_cte = parentCTE;
144 pstate->p_locked_from_parent = locked_from_parent;
146 query = transformStmt(pstate, parseTree);
148 free_parsestate(pstate);
155 * transform a Parse tree into a Query tree.
158 transformStmt(ParseState *pstate, Node *parseTree)
162 switch (nodeTag(parseTree))
165 * Optimizable statements
168 result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
172 result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
176 result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
181 SelectStmt *n = (SelectStmt *) parseTree;
184 result = transformValuesClause(pstate, n);
185 else if (n->op == SETOP_NONE)
186 result = transformSelectStmt(pstate, n);
188 result = transformSetOperationStmt(pstate, n);
195 case T_DeclareCursorStmt:
196 result = transformDeclareCursorStmt(pstate,
197 (DeclareCursorStmt *) parseTree);
201 result = transformExplainStmt(pstate,
202 (ExplainStmt *) parseTree);
208 * other statements don't require any transformation; just return
209 * the original parsetree with a Query node plastered on top.
211 result = makeNode(Query);
212 result->commandType = CMD_UTILITY;
213 result->utilityStmt = (Node *) parseTree;
217 /* Mark as original query until we learn differently */
218 result->querySource = QSRC_ORIGINAL;
219 result->canSetTag = true;
225 * analyze_requires_snapshot
226 * Returns true if a snapshot must be set before doing parse analysis
227 * on the given raw parse tree.
229 * Classification here should match transformStmt(); but we also have to
230 * allow a NULL input (for Parse/Bind of an empty query string).
233 analyze_requires_snapshot(Node *parseTree)
237 if (parseTree == NULL)
240 switch (nodeTag(parseTree))
243 * Optimizable statements
255 case T_DeclareCursorStmt:
256 /* yes, because it's analyzed just like SELECT */
261 /* yes, because we must analyze the contained statement */
266 /* other utility statements don't have any real parse analysis */
275 * transformDeleteStmt -
276 * transforms a Delete Statement
279 transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
281 Query *qry = makeNode(Query);
284 qry->commandType = CMD_DELETE;
286 /* set up range table with just the result rel */
287 qry->resultRelation = setTargetTable(pstate, stmt->relation,
288 interpretInhOption(stmt->relation->inhOpt),
292 qry->distinctClause = NIL;
295 * The USING clause is non-standard SQL syntax, and is equivalent in
296 * functionality to the FROM list that can be specified for UPDATE. The
297 * USING keyword is used rather than FROM because FROM is already a
298 * keyword in the DELETE syntax.
300 transformFromClause(pstate, stmt->usingClause);
302 qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
304 qry->returningList = transformReturningList(pstate, stmt->returningList);
306 /* done building the range table and jointree */
307 qry->rtable = pstate->p_rtable;
308 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
310 qry->hasSubLinks = pstate->p_hasSubLinks;
311 qry->hasAggs = pstate->p_hasAggs;
312 if (pstate->p_hasAggs)
313 parseCheckAggregates(pstate, qry);
314 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
315 if (pstate->p_hasWindowFuncs)
316 parseCheckWindowFuncs(pstate, qry);
322 * transformInsertStmt -
323 * transform an Insert Statement
326 transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
328 Query *qry = makeNode(Query);
329 SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
330 List *exprList = NIL;
331 bool isGeneralSelect;
333 List *sub_relnamespace;
334 List *sub_varnamespace;
343 qry->commandType = CMD_INSERT;
344 pstate->p_is_insert = true;
347 * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
348 * VALUES list, or general SELECT input. We special-case VALUES, both for
349 * efficiency and so we can handle DEFAULT specifications.
351 * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
352 * VALUES clause. If we have any of those, treat it as a general SELECT;
353 * so it will work, but you can't use DEFAULT items together with those.
355 isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
356 selectStmt->sortClause != NIL ||
357 selectStmt->limitOffset != NULL ||
358 selectStmt->limitCount != NULL ||
359 selectStmt->lockingClause != NIL ||
360 selectStmt->withClause != NULL));
363 * If a non-nil rangetable/namespace was passed in, and we are doing
364 * INSERT/SELECT, arrange to pass the rangetable/namespace down to the
365 * SELECT. This can only happen if we are inside a CREATE RULE, and in
366 * that case we want the rule's OLD and NEW rtable entries to appear as
367 * part of the SELECT's rtable, not as outer references for it. (Kluge!)
368 * The SELECT's joinlist is not affected however. We must do this before
369 * adding the target table to the INSERT's rtable.
373 sub_rtable = pstate->p_rtable;
374 pstate->p_rtable = NIL;
375 sub_relnamespace = pstate->p_relnamespace;
376 pstate->p_relnamespace = NIL;
377 sub_varnamespace = pstate->p_varnamespace;
378 pstate->p_varnamespace = NIL;
379 /* There can't be any outer WITH to worry about */
380 Assert(pstate->p_ctenamespace == NIL);
384 sub_rtable = NIL; /* not used, but keep compiler quiet */
385 sub_relnamespace = NIL;
386 sub_varnamespace = NIL;
390 * Must get write lock on INSERT target table before scanning SELECT, else
391 * we will grab the wrong kind of initial lock if the target table is also
392 * mentioned in the SELECT part. Note that the target table is not added
393 * to the joinlist or namespace.
395 qry->resultRelation = setTargetTable(pstate, stmt->relation,
396 false, false, ACL_INSERT);
398 /* Validate stmt->cols list, or build default list if no list given */
399 icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
400 Assert(list_length(icolumns) == list_length(attrnos));
403 * Determine which variant of INSERT we have.
405 if (selectStmt == NULL)
408 * We have INSERT ... DEFAULT VALUES. We can handle this case by
409 * emitting an empty targetlist --- all columns will be defaulted when
410 * the planner expands the targetlist.
414 else if (isGeneralSelect)
417 * We make the sub-pstate a child of the outer pstate so that it can
418 * see any Param definitions supplied from above. Since the outer
419 * pstate's rtable and namespace are presently empty, there are no
420 * side-effects of exposing names the sub-SELECT shouldn't be able to
423 ParseState *sub_pstate = make_parsestate(pstate);
427 * Process the source SELECT.
429 * It is important that this be handled just like a standalone SELECT;
430 * otherwise the behavior of SELECT within INSERT might be different
431 * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
432 * bugs of just that nature...)
434 sub_pstate->p_rtable = sub_rtable;
435 sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
436 sub_pstate->p_relnamespace = sub_relnamespace;
437 sub_pstate->p_varnamespace = sub_varnamespace;
439 selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
441 free_parsestate(sub_pstate);
443 /* The grammar should have produced a SELECT, but it might have INTO */
444 if (!IsA(selectQuery, Query) ||
445 selectQuery->commandType != CMD_SELECT ||
446 selectQuery->utilityStmt != NULL)
447 elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
448 if (selectQuery->intoClause)
450 (errcode(ERRCODE_SYNTAX_ERROR),
451 errmsg("INSERT ... SELECT cannot specify INTO"),
452 parser_errposition(pstate,
453 exprLocation((Node *) selectQuery->intoClause))));
456 * Make the source be a subquery in the INSERT's rangetable, and add
457 * it to the INSERT's joinlist.
459 rte = addRangeTableEntryForSubquery(pstate,
461 makeAlias("*SELECT*", NIL),
463 rtr = makeNode(RangeTblRef);
464 /* assume new rte is at end */
465 rtr->rtindex = list_length(pstate->p_rtable);
466 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
467 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
470 * Generate an expression list for the INSERT that selects all the
471 * non-resjunk columns from the subquery. (INSERT's tlist must be
472 * separate from the subquery's tlist because we may add columns,
473 * insert datatype coercions, etc.)
475 * HACK: unknown-type constants and params in the SELECT's targetlist
476 * are copied up as-is rather than being referenced as subquery
477 * outputs. This is to ensure that when we try to coerce them to
478 * the target column's datatype, the right things happen (see
479 * special cases in coerce_type). Otherwise, this fails:
480 * INSERT INTO foo SELECT 'bar', ... FROM baz
484 foreach(lc, selectQuery->targetList)
486 TargetEntry *tle = (TargetEntry *) lfirst(lc);
492 (IsA(tle->expr, Const) ||IsA(tle->expr, Param)) &&
493 exprType((Node *) tle->expr) == UNKNOWNOID)
497 Var *var = makeVarFromTargetEntry(rtr->rtindex, tle);
499 var->location = exprLocation((Node *) tle->expr);
502 exprList = lappend(exprList, expr);
505 /* Prepare row for assignment to target table */
506 exprList = transformInsertRow(pstate, exprList,
510 else if (list_length(selectStmt->valuesLists) > 1)
513 * Process INSERT ... VALUES with multiple VALUES sublists. We
514 * generate a VALUES RTE holding the transformed expression lists, and
515 * build up a targetlist containing Vars that reference the VALUES
518 List *exprsLists = NIL;
519 int sublist_length = -1;
521 /* process the WITH clause */
522 if (selectStmt->withClause)
524 qry->hasRecursive = selectStmt->withClause->recursive;
525 qry->cteList = transformWithClause(pstate, selectStmt->withClause);
528 foreach(lc, selectStmt->valuesLists)
530 List *sublist = (List *) lfirst(lc);
532 /* Do basic expression transformation (same as a ROW() expr) */
533 sublist = transformExpressionList(pstate, sublist);
536 * All the sublists must be the same length, *after*
537 * transformation (which might expand '*' into multiple items).
538 * The VALUES RTE can't handle anything different.
540 if (sublist_length < 0)
542 /* Remember post-transformation length of first sublist */
543 sublist_length = list_length(sublist);
545 else if (sublist_length != list_length(sublist))
548 (errcode(ERRCODE_SYNTAX_ERROR),
549 errmsg("VALUES lists must all be the same length"),
550 parser_errposition(pstate,
551 exprLocation((Node *) sublist))));
554 /* Prepare row for assignment to target table */
555 sublist = transformInsertRow(pstate, sublist,
559 exprsLists = lappend(exprsLists, sublist);
563 * There mustn't have been any table references in the expressions,
564 * else strange things would happen, like Cartesian products of those
565 * tables with the VALUES list ...
567 if (pstate->p_joinlist != NIL)
569 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
570 errmsg("VALUES must not contain table references"),
571 parser_errposition(pstate,
572 locate_var_of_level((Node *) exprsLists, 0))));
575 * Another thing we can't currently support is NEW/OLD references in
576 * rules --- seems we'd need something like SQL99's LATERAL construct
577 * to ensure that the values would be available while evaluating the
578 * VALUES RTE. This is a shame. FIXME
580 if (list_length(pstate->p_rtable) != 1 &&
581 contain_vars_of_level((Node *) exprsLists, 0))
583 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
584 errmsg("VALUES must not contain OLD or NEW references"),
585 errhint("Use SELECT ... UNION ALL ... instead."),
586 parser_errposition(pstate,
587 locate_var_of_level((Node *) exprsLists, 0))));
590 * Generate the VALUES RTE
592 rte = addRangeTableEntryForValues(pstate, exprsLists, NULL, true);
593 rtr = makeNode(RangeTblRef);
594 /* assume new rte is at end */
595 rtr->rtindex = list_length(pstate->p_rtable);
596 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
597 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
600 * Generate list of Vars referencing the RTE
602 expandRTE(rte, rtr->rtindex, 0, -1, false, NULL, &exprList);
607 * Process INSERT ... VALUES with a single VALUES sublist.
608 * We treat this separately for efficiency and for historical
609 * compatibility --- specifically, allowing table references,
611 * INSERT INTO foo VALUES(bar.*)
613 * The sublist is just computed directly as the Query's targetlist,
614 * with no VALUES RTE. So it works just like SELECT without FROM.
617 List *valuesLists = selectStmt->valuesLists;
619 Assert(list_length(valuesLists) == 1);
621 /* process the WITH clause */
622 if (selectStmt->withClause)
624 qry->hasRecursive = selectStmt->withClause->recursive;
625 qry->cteList = transformWithClause(pstate, selectStmt->withClause);
628 /* Do basic expression transformation (same as a ROW() expr) */
629 exprList = transformExpressionList(pstate,
630 (List *) linitial(valuesLists));
632 /* Prepare row for assignment to target table */
633 exprList = transformInsertRow(pstate, exprList,
639 * Generate query's target list using the computed list of expressions.
640 * Also, mark all the target columns as needing insert permissions.
642 rte = pstate->p_target_rangetblentry;
643 qry->targetList = NIL;
644 icols = list_head(icolumns);
645 attnos = list_head(attrnos);
646 foreach(lc, exprList)
648 Expr *expr = (Expr *) lfirst(lc);
653 col = (ResTarget *) lfirst(icols);
654 Assert(IsA(col, ResTarget));
655 attr_num = (AttrNumber) lfirst_int(attnos);
657 tle = makeTargetEntry(expr,
661 qry->targetList = lappend(qry->targetList, tle);
663 rte->modifiedCols = bms_add_member(rte->modifiedCols,
664 attr_num - FirstLowInvalidHeapAttributeNumber);
666 icols = lnext(icols);
667 attnos = lnext(attnos);
671 * If we have a RETURNING clause, we need to add the target relation to
672 * the query namespace before processing it, so that Var references in
673 * RETURNING will work. Also, remove any namespace entries added in a
674 * sub-SELECT or VALUES list.
676 if (stmt->returningList)
678 pstate->p_relnamespace = NIL;
679 pstate->p_varnamespace = NIL;
680 addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
682 qry->returningList = transformReturningList(pstate,
683 stmt->returningList);
686 /* done building the range table and jointree */
687 qry->rtable = pstate->p_rtable;
688 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
690 qry->hasSubLinks = pstate->p_hasSubLinks;
691 /* aggregates not allowed (but subselects are okay) */
692 if (pstate->p_hasAggs)
694 (errcode(ERRCODE_GROUPING_ERROR),
695 errmsg("cannot use aggregate function in VALUES"),
696 parser_errposition(pstate,
697 locate_agg_of_level((Node *) qry, 0))));
698 if (pstate->p_hasWindowFuncs)
700 (errcode(ERRCODE_WINDOWING_ERROR),
701 errmsg("cannot use window function in VALUES"),
702 parser_errposition(pstate,
703 locate_windowfunc((Node *) qry))));
709 * Prepare an INSERT row for assignment to the target table.
711 * The row might be either a VALUES row, or variables referencing a
715 transformInsertRow(ParseState *pstate, List *exprlist,
716 List *stmtcols, List *icolumns, List *attrnos)
724 * Check length of expr list. It must not have more expressions than
725 * there are target columns. We allow fewer, but only if no explicit
726 * columns list was given (the remaining columns are implicitly
727 * defaulted). Note we must check this *after* transformation because
728 * that could expand '*' into multiple items.
730 if (list_length(exprlist) > list_length(icolumns))
732 (errcode(ERRCODE_SYNTAX_ERROR),
733 errmsg("INSERT has more expressions than target columns"),
734 parser_errposition(pstate,
735 exprLocation(list_nth(exprlist,
736 list_length(icolumns))))));
737 if (stmtcols != NIL &&
738 list_length(exprlist) < list_length(icolumns))
741 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
742 * where the user accidentally created a RowExpr instead of separate
743 * columns. Add a suitable hint if that seems to be the problem,
744 * because the main error message is quite misleading for this case.
745 * (If there's no stmtcols, you'll get something about data type
746 * mismatch, which is less misleading so we don't worry about giving
747 * a hint in that case.)
750 (errcode(ERRCODE_SYNTAX_ERROR),
751 errmsg("INSERT has more target columns than expressions"),
752 ((list_length(exprlist) == 1 &&
753 count_rowexpr_columns(pstate, linitial(exprlist)) ==
754 list_length(icolumns)) ?
755 errhint("The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?") : 0),
756 parser_errposition(pstate,
757 exprLocation(list_nth(icolumns,
758 list_length(exprlist))))));
762 * Prepare columns for assignment to target table.
765 icols = list_head(icolumns);
766 attnos = list_head(attrnos);
767 foreach(lc, exprlist)
769 Expr *expr = (Expr *) lfirst(lc);
772 col = (ResTarget *) lfirst(icols);
773 Assert(IsA(col, ResTarget));
775 expr = transformAssignedExpr(pstate, expr,
781 result = lappend(result, expr);
783 icols = lnext(icols);
784 attnos = lnext(attnos);
791 * count_rowexpr_columns -
792 * get number of columns contained in a ROW() expression;
793 * return -1 if expression isn't a RowExpr or a Var referencing one.
795 * This is currently used only for hint purposes, so we aren't terribly
796 * tense about recognizing all possible cases. The Var case is interesting
797 * because that's what we'll get in the INSERT ... SELECT (...) case.
800 count_rowexpr_columns(ParseState *pstate, Node *expr)
804 if (IsA(expr, RowExpr))
805 return list_length(((RowExpr *) expr)->args);
808 Var *var = (Var *) expr;
809 AttrNumber attnum = var->varattno;
811 if (attnum > 0 && var->vartype == RECORDOID)
815 rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
816 if (rte->rtekind == RTE_SUBQUERY)
818 /* Subselect-in-FROM: examine sub-select's output expr */
819 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
822 if (ste == NULL || ste->resjunk)
824 expr = (Node *) ste->expr;
825 if (IsA(expr, RowExpr))
826 return list_length(((RowExpr *) expr)->args);
835 * transformSelectStmt -
836 * transforms a Select Statement
838 * Note: this covers only cases with no set operations and no VALUES lists;
839 * see below for the other cases.
842 transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
844 Query *qry = makeNode(Query);
848 qry->commandType = CMD_SELECT;
850 /* process the WITH clause independently of all else */
851 if (stmt->withClause)
853 qry->hasRecursive = stmt->withClause->recursive;
854 qry->cteList = transformWithClause(pstate, stmt->withClause);
857 /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
858 pstate->p_locking_clause = stmt->lockingClause;
860 /* make WINDOW info available for window functions, too */
861 pstate->p_windowdefs = stmt->windowClause;
863 /* process the FROM clause */
864 transformFromClause(pstate, stmt->fromClause);
866 /* transform targetlist */
867 qry->targetList = transformTargetList(pstate, stmt->targetList);
869 /* mark column origins */
870 markTargetListOrigins(pstate, qry->targetList);
872 /* transform WHERE */
873 qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
876 * Initial processing of HAVING clause is just like WHERE clause.
878 qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
882 * Transform sorting/grouping stuff. Do ORDER BY first because both
883 * transformGroupClause and transformDistinctClause need the results. Note
884 * that these functions can also change the targetList, so it's passed to
887 qry->sortClause = transformSortClause(pstate,
890 true /* fix unknowns */ ,
891 false /* allow SQL92 rules */ );
893 qry->groupClause = transformGroupClause(pstate,
897 false /* allow SQL92 rules */ );
899 if (stmt->distinctClause == NIL)
901 qry->distinctClause = NIL;
902 qry->hasDistinctOn = false;
904 else if (linitial(stmt->distinctClause) == NULL)
906 /* We had SELECT DISTINCT */
907 qry->distinctClause = transformDistinctClause(pstate,
911 qry->hasDistinctOn = false;
915 /* We had SELECT DISTINCT ON */
916 qry->distinctClause = transformDistinctOnClause(pstate,
917 stmt->distinctClause,
920 qry->hasDistinctOn = true;
923 /* transform LIMIT */
924 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
926 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
929 /* transform window clauses after we have seen all window functions */
930 qry->windowClause = transformWindowDefinitions(pstate,
931 pstate->p_windowdefs,
934 /* handle any SELECT INTO/CREATE TABLE AS spec */
935 if (stmt->intoClause)
937 qry->intoClause = stmt->intoClause;
938 if (stmt->intoClause->colNames)
939 applyColumnNames(qry->targetList, stmt->intoClause->colNames);
942 qry->rtable = pstate->p_rtable;
943 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
945 qry->hasSubLinks = pstate->p_hasSubLinks;
946 qry->hasAggs = pstate->p_hasAggs;
947 if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
948 parseCheckAggregates(pstate, qry);
949 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
950 if (pstate->p_hasWindowFuncs)
951 parseCheckWindowFuncs(pstate, qry);
953 foreach(l, stmt->lockingClause)
955 transformLockingClause(pstate, qry,
956 (LockingClause *) lfirst(l), false);
963 * transformValuesClause -
964 * transforms a VALUES clause that's being used as a standalone SELECT
966 * We build a Query containing a VALUES RTE, rather as if one had written
967 * SELECT * FROM (VALUES ...)
970 transformValuesClause(ParseState *pstate, SelectStmt *stmt)
972 Query *qry = makeNode(Query);
973 List *exprsLists = NIL;
974 List **colexprs = NULL;
975 Oid *coltypes = NULL;
976 int sublist_length = -1;
984 qry->commandType = CMD_SELECT;
986 /* Most SELECT stuff doesn't apply in a VALUES clause */
987 Assert(stmt->distinctClause == NIL);
988 Assert(stmt->targetList == NIL);
989 Assert(stmt->fromClause == NIL);
990 Assert(stmt->whereClause == NULL);
991 Assert(stmt->groupClause == NIL);
992 Assert(stmt->havingClause == NULL);
993 Assert(stmt->windowClause == NIL);
994 Assert(stmt->op == SETOP_NONE);
996 /* process the WITH clause independently of all else */
997 if (stmt->withClause)
999 qry->hasRecursive = stmt->withClause->recursive;
1000 qry->cteList = transformWithClause(pstate, stmt->withClause);
1004 * For each row of VALUES, transform the raw expressions and gather type
1005 * information. This is also a handy place to reject DEFAULT nodes, which
1006 * the grammar allows for simplicity.
1008 foreach(lc, stmt->valuesLists)
1010 List *sublist = (List *) lfirst(lc);
1012 /* Do basic expression transformation (same as a ROW() expr) */
1013 sublist = transformExpressionList(pstate, sublist);
1016 * All the sublists must be the same length, *after* transformation
1017 * (which might expand '*' into multiple items). The VALUES RTE can't
1018 * handle anything different.
1020 if (sublist_length < 0)
1022 /* Remember post-transformation length of first sublist */
1023 sublist_length = list_length(sublist);
1024 /* and allocate arrays for per-column info */
1025 colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1026 coltypes = (Oid *) palloc0(sublist_length * sizeof(Oid));
1028 else if (sublist_length != list_length(sublist))
1031 (errcode(ERRCODE_SYNTAX_ERROR),
1032 errmsg("VALUES lists must all be the same length"),
1033 parser_errposition(pstate,
1034 exprLocation((Node *) sublist))));
1037 exprsLists = lappend(exprsLists, sublist);
1039 /* Check for DEFAULT and build per-column expression lists */
1041 foreach(lc2, sublist)
1043 Node *col = (Node *) lfirst(lc2);
1045 if (IsA(col, SetToDefault))
1047 (errcode(ERRCODE_SYNTAX_ERROR),
1048 errmsg("DEFAULT can only appear in a VALUES list within INSERT"),
1049 parser_errposition(pstate, exprLocation(col))));
1050 colexprs[i] = lappend(colexprs[i], col);
1056 * Now resolve the common types of the columns, and coerce everything to
1059 for (i = 0; i < sublist_length; i++)
1061 coltypes[i] = select_common_type(pstate, colexprs[i], "VALUES", NULL);
1064 newExprsLists = NIL;
1065 foreach(lc, exprsLists)
1067 List *sublist = (List *) lfirst(lc);
1068 List *newsublist = NIL;
1071 foreach(lc2, sublist)
1073 Node *col = (Node *) lfirst(lc2);
1075 col = coerce_to_common_type(pstate, col, coltypes[i], "VALUES");
1076 newsublist = lappend(newsublist, col);
1080 newExprsLists = lappend(newExprsLists, newsublist);
1084 * Generate the VALUES RTE
1086 rte = addRangeTableEntryForValues(pstate, newExprsLists, NULL, true);
1087 rtr = makeNode(RangeTblRef);
1088 /* assume new rte is at end */
1089 rtr->rtindex = list_length(pstate->p_rtable);
1090 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1091 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
1092 pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
1095 * Generate a targetlist as though expanding "*"
1097 Assert(pstate->p_next_resno == 1);
1098 qry->targetList = expandRelAttrs(pstate, rte, rtr->rtindex, 0, -1);
1101 * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1104 qry->sortClause = transformSortClause(pstate,
1107 true /* fix unknowns */ ,
1108 false /* allow SQL92 rules */ );
1110 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1112 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1115 if (stmt->lockingClause)
1117 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1118 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES")));
1120 /* handle any CREATE TABLE AS spec */
1121 if (stmt->intoClause)
1123 qry->intoClause = stmt->intoClause;
1124 if (stmt->intoClause->colNames)
1125 applyColumnNames(qry->targetList, stmt->intoClause->colNames);
1129 * There mustn't have been any table references in the expressions, else
1130 * strange things would happen, like Cartesian products of those tables
1131 * with the VALUES list. We have to check this after parsing ORDER BY et
1132 * al since those could insert more junk.
1134 if (list_length(pstate->p_joinlist) != 1)
1136 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1137 errmsg("VALUES must not contain table references"),
1138 parser_errposition(pstate,
1139 locate_var_of_level((Node *) newExprsLists, 0))));
1142 * Another thing we can't currently support is NEW/OLD references in rules
1143 * --- seems we'd need something like SQL99's LATERAL construct to ensure
1144 * that the values would be available while evaluating the VALUES RTE.
1145 * This is a shame. FIXME
1147 if (list_length(pstate->p_rtable) != 1 &&
1148 contain_vars_of_level((Node *) newExprsLists, 0))
1150 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1151 errmsg("VALUES must not contain OLD or NEW references"),
1152 errhint("Use SELECT ... UNION ALL ... instead."),
1153 parser_errposition(pstate,
1154 locate_var_of_level((Node *) newExprsLists, 0))));
1156 qry->rtable = pstate->p_rtable;
1157 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1159 qry->hasSubLinks = pstate->p_hasSubLinks;
1160 /* aggregates not allowed (but subselects are okay) */
1161 if (pstate->p_hasAggs)
1163 (errcode(ERRCODE_GROUPING_ERROR),
1164 errmsg("cannot use aggregate function in VALUES"),
1165 parser_errposition(pstate,
1166 locate_agg_of_level((Node *) newExprsLists, 0))));
1167 if (pstate->p_hasWindowFuncs)
1169 (errcode(ERRCODE_WINDOWING_ERROR),
1170 errmsg("cannot use window function in VALUES"),
1171 parser_errposition(pstate,
1172 locate_windowfunc((Node *) newExprsLists))));
1178 * transformSetOperationStmt -
1179 * transforms a set-operations tree
1181 * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
1182 * structure to it. We must transform each leaf SELECT and build up a top-
1183 * level Query that contains the leaf SELECTs as subqueries in its rangetable.
1184 * The tree of set operations is converted into the setOperations field of
1185 * the top-level Query.
1188 transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
1190 Query *qry = makeNode(Query);
1191 SelectStmt *leftmostSelect;
1193 Query *leftmostQuery;
1194 SetOperationStmt *sostmt;
1196 List *intoColNames = NIL;
1200 List *lockingClause;
1202 ListCell *left_tlist,
1210 int sv_rtable_length;
1211 RangeTblEntry *jrte;
1214 qry->commandType = CMD_SELECT;
1216 /* process the WITH clause independently of all else */
1217 if (stmt->withClause)
1219 qry->hasRecursive = stmt->withClause->recursive;
1220 qry->cteList = transformWithClause(pstate, stmt->withClause);
1224 * Find leftmost leaf SelectStmt; extract the one-time-only items from it
1225 * and from the top-level node.
1227 leftmostSelect = stmt->larg;
1228 while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1229 leftmostSelect = leftmostSelect->larg;
1230 Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1231 leftmostSelect->larg == NULL);
1232 if (leftmostSelect->intoClause)
1234 qry->intoClause = leftmostSelect->intoClause;
1235 intoColNames = leftmostSelect->intoClause->colNames;
1238 /* clear this to prevent complaints in transformSetOperationTree() */
1239 leftmostSelect->intoClause = NULL;
1242 * These are not one-time, exactly, but we want to process them here and
1243 * not let transformSetOperationTree() see them --- else it'll just
1244 * recurse right back here!
1246 sortClause = stmt->sortClause;
1247 limitOffset = stmt->limitOffset;
1248 limitCount = stmt->limitCount;
1249 lockingClause = stmt->lockingClause;
1251 stmt->sortClause = NIL;
1252 stmt->limitOffset = NULL;
1253 stmt->limitCount = NULL;
1254 stmt->lockingClause = NIL;
1256 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1259 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1260 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1263 * Recursively transform the components of the tree.
1265 sostmt = (SetOperationStmt *) transformSetOperationTree(pstate, stmt,
1268 Assert(sostmt && IsA(sostmt, SetOperationStmt));
1269 qry->setOperations = (Node *) sostmt;
1272 * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1274 node = sostmt->larg;
1275 while (node && IsA(node, SetOperationStmt))
1276 node = ((SetOperationStmt *) node)->larg;
1277 Assert(node && IsA(node, RangeTblRef));
1278 leftmostRTI = ((RangeTblRef *) node)->rtindex;
1279 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1280 Assert(leftmostQuery != NULL);
1283 * Generate dummy targetlist for outer query using column names of
1284 * leftmost select and common datatypes of topmost set operation. Also
1285 * make lists of the dummy vars and their names for use in parsing ORDER
1288 * Note: we use leftmostRTI as the varno of the dummy variables. It
1289 * shouldn't matter too much which RT index they have, as long as they
1290 * have one that corresponds to a real RT entry; else funny things may
1291 * happen when the tree is mashed by rule rewriting.
1293 qry->targetList = NIL;
1296 left_tlist = list_head(leftmostQuery->targetList);
1298 forboth(lct, sostmt->colTypes, lcm, sostmt->colTypmods)
1300 Oid colType = lfirst_oid(lct);
1301 int32 colTypmod = lfirst_int(lcm);
1302 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1307 Assert(!lefttle->resjunk);
1308 colName = pstrdup(lefttle->resname);
1309 var = makeVar(leftmostRTI,
1314 var->location = exprLocation((Node *) lefttle->expr);
1315 tle = makeTargetEntry((Expr *) var,
1316 (AttrNumber) pstate->p_next_resno++,
1319 qry->targetList = lappend(qry->targetList, tle);
1320 targetvars = lappend(targetvars, var);
1321 targetnames = lappend(targetnames, makeString(colName));
1322 left_tlist = lnext(left_tlist);
1326 * As a first step towards supporting sort clauses that are expressions
1327 * using the output columns, generate a varnamespace entry that makes the
1328 * output columns visible. A Join RTE node is handy for this, since we
1329 * can easily control the Vars generated upon matches.
1331 * Note: we don't yet do anything useful with such cases, but at least
1332 * "ORDER BY upper(foo)" will draw the right error message rather than
1335 sv_rtable_length = list_length(pstate->p_rtable);
1337 jrte = addRangeTableEntryForJoin(pstate,
1344 sv_relnamespace = pstate->p_relnamespace;
1345 pstate->p_relnamespace = NIL; /* no qualified names allowed */
1347 sv_varnamespace = pstate->p_varnamespace;
1348 pstate->p_varnamespace = list_make1(jrte);
1351 * For now, we don't support resjunk sort clauses on the output of a
1352 * setOperation tree --- you can only use the SQL92-spec options of
1353 * selecting an output column by name or number. Enforce by checking that
1354 * transformSortClause doesn't add any items to tlist.
1356 tllen = list_length(qry->targetList);
1358 qry->sortClause = transformSortClause(pstate,
1361 false /* no unknowns expected */ ,
1362 false /* allow SQL92 rules */ );
1364 pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1365 pstate->p_relnamespace = sv_relnamespace;
1366 pstate->p_varnamespace = sv_varnamespace;
1368 if (tllen != list_length(qry->targetList))
1370 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1371 errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
1372 errdetail("Only result column names can be used, not expressions or functions."),
1373 errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
1374 parser_errposition(pstate,
1375 exprLocation(list_nth(qry->targetList, tllen)))));
1377 qry->limitOffset = transformLimitClause(pstate, limitOffset,
1379 qry->limitCount = transformLimitClause(pstate, limitCount,
1383 * Handle SELECT INTO/CREATE TABLE AS.
1385 * Any column names from CREATE TABLE AS need to be attached to both the
1386 * top level and the leftmost subquery. We do not do this earlier because
1387 * we do *not* want sortClause processing to be affected.
1391 applyColumnNames(qry->targetList, intoColNames);
1392 applyColumnNames(leftmostQuery->targetList, intoColNames);
1395 qry->rtable = pstate->p_rtable;
1396 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1398 qry->hasSubLinks = pstate->p_hasSubLinks;
1399 qry->hasAggs = pstate->p_hasAggs;
1400 if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
1401 parseCheckAggregates(pstate, qry);
1402 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1403 if (pstate->p_hasWindowFuncs)
1404 parseCheckWindowFuncs(pstate, qry);
1406 foreach(l, lockingClause)
1408 transformLockingClause(pstate, qry,
1409 (LockingClause *) lfirst(l), false);
1416 * transformSetOperationTree
1417 * Recursively transform leaves and internal nodes of a set-op tree
1419 * In addition to returning the transformed node, we return a list of
1420 * expression nodes showing the type, typmod, and location (for error messages)
1421 * of each output column of the set-op node. This is used only during the
1422 * internal recursion of this function. At the upper levels we use
1423 * SetToDefault nodes for this purpose, since they carry exactly the fields
1424 * needed, but any other expression node type would do as well.
1427 transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
1428 bool isTopLevel, List **colInfo)
1432 Assert(stmt && IsA(stmt, SelectStmt));
1435 * Validity-check both leaf and internal SELECTs for disallowed ops.
1437 if (stmt->intoClause)
1439 (errcode(ERRCODE_SYNTAX_ERROR),
1440 errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
1441 parser_errposition(pstate,
1442 exprLocation((Node *) stmt->intoClause))));
1444 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1445 if (stmt->lockingClause)
1447 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1448 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1451 * If an internal node of a set-op tree has ORDER BY, LIMIT, or FOR UPDATE
1452 * clauses attached, we need to treat it like a leaf node to generate an
1453 * independent sub-Query tree. Otherwise, it can be represented by a
1454 * SetOperationStmt node underneath the parent Query.
1456 if (stmt->op == SETOP_NONE)
1458 Assert(stmt->larg == NULL && stmt->rarg == NULL);
1463 Assert(stmt->larg != NULL && stmt->rarg != NULL);
1464 if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
1465 stmt->lockingClause)
1473 /* Process leaf SELECT */
1475 char selectName[32];
1481 * Transform SelectStmt into a Query.
1483 * Note: previously transformed sub-queries don't affect the parsing
1484 * of this sub-query, because they are not in the toplevel pstate's
1487 selectQuery = parse_sub_analyze((Node *) stmt, pstate, NULL, false);
1490 * Check for bogus references to Vars on the current query level (but
1491 * upper-level references are okay). Normally this can't happen
1492 * because the namespace will be empty, but it could happen if we are
1495 if (pstate->p_relnamespace || pstate->p_varnamespace)
1497 if (contain_vars_of_level((Node *) selectQuery, 1))
1499 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1500 errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
1501 parser_errposition(pstate,
1502 locate_var_of_level((Node *) selectQuery, 1))));
1506 * Extract a list of the result expressions for upper-level checking.
1509 foreach(tl, selectQuery->targetList)
1511 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1514 *colInfo = lappend(*colInfo, tle->expr);
1518 * Make the leaf query be a subquery in the top-level rangetable.
1520 snprintf(selectName, sizeof(selectName), "*SELECT* %d",
1521 list_length(pstate->p_rtable) + 1);
1522 rte = addRangeTableEntryForSubquery(pstate,
1524 makeAlias(selectName, NIL),
1528 * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
1530 rtr = makeNode(RangeTblRef);
1531 /* assume new rte is at end */
1532 rtr->rtindex = list_length(pstate->p_rtable);
1533 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1534 return (Node *) rtr;
1538 /* Process an internal node (set operation node) */
1539 SetOperationStmt *op = makeNode(SetOperationStmt);
1544 const char *context;
1546 context = (stmt->op == SETOP_UNION ? "UNION" :
1547 (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
1551 op->all = stmt->all;
1554 * Recursively transform the left child node.
1556 op->larg = transformSetOperationTree(pstate, stmt->larg,
1561 * If we are processing a recursive union query, now is the time to
1562 * examine the non-recursive term's output columns and mark the
1563 * containing CTE as having those result columns. We should do this
1564 * only at the topmost setop of the CTE, of course.
1567 pstate->p_parent_cte &&
1568 pstate->p_parent_cte->cterecursive)
1569 determineRecursiveColTypes(pstate, op->larg, lcolinfo);
1572 * Recursively transform the right child node.
1574 op->rarg = transformSetOperationTree(pstate, stmt->rarg,
1579 * Verify that the two children have the same number of non-junk
1580 * columns, and determine the types of the merged output columns.
1582 if (list_length(lcolinfo) != list_length(rcolinfo))
1584 (errcode(ERRCODE_SYNTAX_ERROR),
1585 errmsg("each %s query must have the same number of columns",
1587 parser_errposition(pstate,
1588 exprLocation((Node *) rcolinfo))));
1592 op->colTypmods = NIL;
1593 op->groupClauses = NIL;
1594 forboth(lci, lcolinfo, rci, rcolinfo)
1596 Node *lcolnode = (Node *) lfirst(lci);
1597 Node *rcolnode = (Node *) lfirst(rci);
1598 Oid lcoltype = exprType(lcolnode);
1599 Oid rcoltype = exprType(rcolnode);
1600 int32 lcoltypmod = exprTypmod(lcolnode);
1601 int32 rcoltypmod = exprTypmod(rcolnode);
1603 SetToDefault *rescolnode;
1607 /* select common type, same as CASE et al */
1608 rescoltype = select_common_type(pstate,
1609 list_make2(lcolnode, rcolnode),
1612 /* if same type and same typmod, use typmod; else default */
1613 if (lcoltype == rcoltype && lcoltypmod == rcoltypmod)
1614 rescoltypmod = lcoltypmod;
1619 * Verify the coercions are actually possible. If not, we'd fail
1620 * later anyway, but we want to fail now while we have sufficient
1621 * context to produce an error cursor position.
1623 * The if-tests might look wrong, but they are correct: we should
1624 * verify if the input is non-UNKNOWN *or* if it is an UNKNOWN
1625 * Const (to verify the literal is valid for the target data type)
1626 * or Param (to possibly resolve the Param's type). We should do
1627 * nothing if the input is say an UNKNOWN Var, which can happen in
1628 * some cases. The planner is sometimes able to fold the Var to a
1629 * constant before it has to coerce the type, so failing now would
1630 * just break cases that might work.
1632 if (lcoltype != UNKNOWNOID ||
1633 IsA(lcolnode, Const) ||IsA(lcolnode, Param))
1634 (void) coerce_to_common_type(pstate, lcolnode,
1635 rescoltype, context);
1636 if (rcoltype != UNKNOWNOID ||
1637 IsA(rcolnode, Const) ||IsA(rcolnode, Param))
1638 (void) coerce_to_common_type(pstate, rcolnode,
1639 rescoltype, context);
1642 rescolnode = makeNode(SetToDefault);
1643 rescolnode->typeId = rescoltype;
1644 rescolnode->typeMod = rescoltypmod;
1645 rescolnode->location = exprLocation(bestexpr);
1646 *colInfo = lappend(*colInfo, rescolnode);
1648 op->colTypes = lappend_oid(op->colTypes, rescoltype);
1649 op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
1652 * For all cases except UNION ALL, identify the grouping operators
1653 * (and, if available, sorting operators) that will be used to
1654 * eliminate duplicates.
1656 if (op->op != SETOP_UNION || !op->all)
1658 SortGroupClause *grpcl = makeNode(SortGroupClause);
1661 ParseCallbackState pcbstate;
1663 setup_parser_errposition_callback(&pcbstate, pstate,
1664 rescolnode->location);
1666 /* determine the eqop and optional sortop */
1667 get_sort_group_operators(rescoltype,
1669 &sortop, &eqop, NULL);
1671 cancel_parser_errposition_callback(&pcbstate);
1673 /* we don't have a tlist yet, so can't assign sortgrouprefs */
1674 grpcl->tleSortGroupRef = 0;
1676 grpcl->sortop = sortop;
1677 grpcl->nulls_first = false; /* OK with or without sortop */
1679 op->groupClauses = lappend(op->groupClauses, grpcl);
1688 * Process the outputs of the non-recursive term of a recursive union
1689 * to set up the parent CTE's columns
1692 determineRecursiveColTypes(ParseState *pstate, Node *larg, List *lcolinfo)
1696 Query *leftmostQuery;
1698 ListCell *left_tlist;
1703 * Find leftmost leaf SELECT
1706 while (node && IsA(node, SetOperationStmt))
1707 node = ((SetOperationStmt *) node)->larg;
1708 Assert(node && IsA(node, RangeTblRef));
1709 leftmostRTI = ((RangeTblRef *) node)->rtindex;
1710 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1711 Assert(leftmostQuery != NULL);
1714 * Generate dummy targetlist using column names of leftmost select and
1715 * dummy result expressions of the non-recursive term.
1718 left_tlist = list_head(leftmostQuery->targetList);
1721 foreach(lci, lcolinfo)
1723 Expr *lcolexpr = (Expr *) lfirst(lci);
1724 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1728 Assert(!lefttle->resjunk);
1729 colName = pstrdup(lefttle->resname);
1730 tle = makeTargetEntry(lcolexpr,
1734 targetList = lappend(targetList, tle);
1735 left_tlist = lnext(left_tlist);
1738 /* Now build CTE's output column info using dummy targetlist */
1739 analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
1743 * Attach column names from a ColumnDef list to a TargetEntry list
1744 * (for CREATE TABLE AS)
1747 applyColumnNames(List *dst, List *src)
1752 src_item = list_head(src);
1754 foreach(dst_item, dst)
1756 TargetEntry *d = (TargetEntry *) lfirst(dst_item);
1759 /* junk targets don't count */
1763 /* fewer ColumnDefs than target entries is OK */
1764 if (src_item == NULL)
1767 s = (ColumnDef *) lfirst(src_item);
1768 src_item = lnext(src_item);
1770 d->resname = pstrdup(s->colname);
1773 /* more ColumnDefs than target entries is not OK */
1774 if (src_item != NULL)
1776 (errcode(ERRCODE_SYNTAX_ERROR),
1777 errmsg("CREATE TABLE AS specifies too many column names")));
1782 * transformUpdateStmt -
1783 * transforms an update statement
1786 transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
1788 Query *qry = makeNode(Query);
1789 RangeTblEntry *target_rte;
1791 ListCell *origTargetList;
1794 qry->commandType = CMD_UPDATE;
1795 pstate->p_is_update = true;
1797 qry->resultRelation = setTargetTable(pstate, stmt->relation,
1798 interpretInhOption(stmt->relation->inhOpt),
1803 * the FROM clause is non-standard SQL syntax. We used to be able to do
1804 * this with REPLACE in POSTQUEL so we keep the feature.
1806 transformFromClause(pstate, stmt->fromClause);
1808 qry->targetList = transformTargetList(pstate, stmt->targetList);
1810 qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
1812 qry->returningList = transformReturningList(pstate, stmt->returningList);
1814 qry->rtable = pstate->p_rtable;
1815 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1817 qry->hasSubLinks = pstate->p_hasSubLinks;
1820 * Top-level aggregates are simply disallowed in UPDATE, per spec. (From
1821 * an implementation point of view, this is forced because the implicit
1822 * ctid reference would otherwise be an ungrouped variable.)
1824 if (pstate->p_hasAggs)
1826 (errcode(ERRCODE_GROUPING_ERROR),
1827 errmsg("cannot use aggregate function in UPDATE"),
1828 parser_errposition(pstate,
1829 locate_agg_of_level((Node *) qry, 0))));
1830 if (pstate->p_hasWindowFuncs)
1832 (errcode(ERRCODE_WINDOWING_ERROR),
1833 errmsg("cannot use window function in UPDATE"),
1834 parser_errposition(pstate,
1835 locate_windowfunc((Node *) qry))));
1838 * Now we are done with SELECT-like processing, and can get on with
1839 * transforming the target list to match the UPDATE target columns.
1842 /* Prepare to assign non-conflicting resnos to resjunk attributes */
1843 if (pstate->p_next_resno <= pstate->p_target_relation->rd_rel->relnatts)
1844 pstate->p_next_resno = pstate->p_target_relation->rd_rel->relnatts + 1;
1846 /* Prepare non-junk columns for assignment to target table */
1847 target_rte = pstate->p_target_rangetblentry;
1848 origTargetList = list_head(stmt->targetList);
1850 foreach(tl, qry->targetList)
1852 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1853 ResTarget *origTarget;
1859 * Resjunk nodes need no additional processing, but be sure they
1860 * have resnos that do not match any target columns; else rewriter
1861 * or planner might get confused. They don't need a resname
1864 tle->resno = (AttrNumber) pstate->p_next_resno++;
1865 tle->resname = NULL;
1868 if (origTargetList == NULL)
1869 elog(ERROR, "UPDATE target count mismatch --- internal error");
1870 origTarget = (ResTarget *) lfirst(origTargetList);
1871 Assert(IsA(origTarget, ResTarget));
1873 attrno = attnameAttNum(pstate->p_target_relation,
1874 origTarget->name, true);
1875 if (attrno == InvalidAttrNumber)
1877 (errcode(ERRCODE_UNDEFINED_COLUMN),
1878 errmsg("column \"%s\" of relation \"%s\" does not exist",
1880 RelationGetRelationName(pstate->p_target_relation)),
1881 parser_errposition(pstate, origTarget->location)));
1883 updateTargetListEntry(pstate, tle, origTarget->name,
1885 origTarget->indirection,
1886 origTarget->location);
1888 /* Mark the target column as requiring update permissions */
1889 target_rte->modifiedCols = bms_add_member(target_rte->modifiedCols,
1890 attrno - FirstLowInvalidHeapAttributeNumber);
1892 origTargetList = lnext(origTargetList);
1894 if (origTargetList != NULL)
1895 elog(ERROR, "UPDATE target count mismatch --- internal error");
1901 * transformReturningList -
1902 * handle a RETURNING clause in INSERT/UPDATE/DELETE
1905 transformReturningList(ParseState *pstate, List *returningList)
1908 int save_next_resno;
1910 bool save_hasWindowFuncs;
1913 if (returningList == NIL)
1914 return NIL; /* nothing to do */
1917 * We need to assign resnos starting at one in the RETURNING list. Save
1918 * and restore the main tlist's value of p_next_resno, just in case
1919 * someone looks at it later (probably won't happen).
1921 save_next_resno = pstate->p_next_resno;
1922 pstate->p_next_resno = 1;
1924 /* save other state so that we can detect disallowed stuff */
1925 save_hasAggs = pstate->p_hasAggs;
1926 pstate->p_hasAggs = false;
1927 save_hasWindowFuncs = pstate->p_hasWindowFuncs;
1928 pstate->p_hasWindowFuncs = false;
1929 length_rtable = list_length(pstate->p_rtable);
1931 /* transform RETURNING identically to a SELECT targetlist */
1932 rlist = transformTargetList(pstate, returningList);
1934 /* check for disallowed stuff */
1936 /* aggregates not allowed (but subselects are okay) */
1937 if (pstate->p_hasAggs)
1939 (errcode(ERRCODE_GROUPING_ERROR),
1940 errmsg("cannot use aggregate function in RETURNING"),
1941 parser_errposition(pstate,
1942 locate_agg_of_level((Node *) rlist, 0))));
1943 if (pstate->p_hasWindowFuncs)
1945 (errcode(ERRCODE_WINDOWING_ERROR),
1946 errmsg("cannot use window function in RETURNING"),
1947 parser_errposition(pstate,
1948 locate_windowfunc((Node *) rlist))));
1950 /* no new relation references please */
1951 if (list_length(pstate->p_rtable) != length_rtable)
1956 /* try to locate such a reference to point to */
1957 for (relid = length_rtable + 1; relid <= list_length(pstate->p_rtable); relid++)
1959 vlocation = locate_var_of_relation((Node *) rlist, relid, 0);
1964 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1965 errmsg("RETURNING cannot contain references to other relations"),
1966 parser_errposition(pstate, vlocation)));
1969 /* mark column origins */
1970 markTargetListOrigins(pstate, rlist);
1973 pstate->p_next_resno = save_next_resno;
1974 pstate->p_hasAggs = save_hasAggs;
1975 pstate->p_hasWindowFuncs = save_hasWindowFuncs;
1982 * transformDeclareCursorStmt -
1983 * transform a DECLARE CURSOR Statement
1985 * DECLARE CURSOR is a hybrid case: it's an optimizable statement (in fact not
1986 * significantly different from a SELECT) as far as parsing/rewriting/planning
1987 * are concerned, but it's not passed to the executor and so in that sense is
1988 * a utility statement. We transform it into a Query exactly as if it were
1989 * a SELECT, then stick the original DeclareCursorStmt into the utilityStmt
1990 * field to carry the cursor name and options.
1993 transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
1998 * Don't allow both SCROLL and NO SCROLL to be specified
2000 if ((stmt->options & CURSOR_OPT_SCROLL) &&
2001 (stmt->options & CURSOR_OPT_NO_SCROLL))
2003 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
2004 errmsg("cannot specify both SCROLL and NO SCROLL")));
2006 result = transformStmt(pstate, stmt->query);
2008 /* Grammar should not have allowed anything but SELECT */
2009 if (!IsA(result, Query) ||
2010 result->commandType != CMD_SELECT ||
2011 result->utilityStmt != NULL)
2012 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
2014 /* But we must explicitly disallow DECLARE CURSOR ... SELECT INTO */
2015 if (result->intoClause)
2017 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
2018 errmsg("DECLARE CURSOR cannot specify INTO"),
2019 parser_errposition(pstate,
2020 exprLocation((Node *) result->intoClause))));
2022 /* FOR UPDATE and WITH HOLD are not compatible */
2023 if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
2025 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2026 errmsg("DECLARE CURSOR WITH HOLD ... FOR UPDATE/SHARE is not supported"),
2027 errdetail("Holdable cursors must be READ ONLY.")));
2029 /* FOR UPDATE and SCROLL are not compatible */
2030 if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
2032 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2033 errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
2034 errdetail("Scrollable cursors must be READ ONLY.")));
2036 /* FOR UPDATE and INSENSITIVE are not compatible */
2037 if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
2039 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2040 errmsg("DECLARE INSENSITIVE CURSOR ... FOR UPDATE/SHARE is not supported"),
2041 errdetail("Insensitive cursors must be READ ONLY.")));
2043 /* We won't need the raw querytree any more */
2046 result->utilityStmt = (Node *) stmt;
2053 * transformExplainStmt -
2054 * transform an EXPLAIN Statement
2056 * EXPLAIN is like other utility statements in that we emit it as a
2057 * CMD_UTILITY Query node; however, we must first transform the contained
2058 * query. We used to postpone that until execution, but it's really necessary
2059 * to do it during the normal parse analysis phase to ensure that side effects
2060 * of parser hooks happen at the expected time.
2063 transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
2067 /* transform contained query */
2068 stmt->query = (Node *) transformStmt(pstate, stmt->query);
2070 /* represent the command as a utility Query */
2071 result = makeNode(Query);
2072 result->commandType = CMD_UTILITY;
2073 result->utilityStmt = (Node *) stmt;
2080 * Check for features that are not supported together with FOR UPDATE/SHARE.
2082 * exported so planner can check again after rewriting, query pullup, etc
2085 CheckSelectLocking(Query *qry)
2087 if (qry->setOperations)
2089 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2090 errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
2091 if (qry->distinctClause != NIL)
2093 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2094 errmsg("SELECT FOR UPDATE/SHARE is not allowed with DISTINCT clause")));
2095 if (qry->groupClause != NIL)
2097 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2098 errmsg("SELECT FOR UPDATE/SHARE is not allowed with GROUP BY clause")));
2099 if (qry->havingQual != NULL)
2101 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2102 errmsg("SELECT FOR UPDATE/SHARE is not allowed with HAVING clause")));
2105 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2106 errmsg("SELECT FOR UPDATE/SHARE is not allowed with aggregate functions")));
2107 if (qry->hasWindowFuncs)
2109 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2110 errmsg("SELECT FOR UPDATE/SHARE is not allowed with window functions")));
2111 if (expression_returns_set((Node *) qry->targetList))
2113 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2114 errmsg("SELECT FOR UPDATE/SHARE is not allowed with set-returning functions in the target list")));
2118 * Transform a FOR UPDATE/SHARE clause
2120 * This basically involves replacing names by integer relids.
2122 * NB: if you need to change this, see also markQueryForLocking()
2123 * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
2126 transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
2129 List *lockedRels = lc->lockedRels;
2133 LockingClause *allrels;
2135 CheckSelectLocking(qry);
2137 /* make a clause we can pass down to subqueries to select all rels */
2138 allrels = makeNode(LockingClause);
2139 allrels->lockedRels = NIL; /* indicates all rels */
2140 allrels->forUpdate = lc->forUpdate;
2141 allrels->noWait = lc->noWait;
2143 if (lockedRels == NIL)
2145 /* all regular tables used in query */
2147 foreach(rt, qry->rtable)
2149 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2152 switch (rte->rtekind)
2155 applyLockingClause(qry, i,
2156 lc->forUpdate, lc->noWait, pushedDown);
2157 rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2160 applyLockingClause(qry, i,
2161 lc->forUpdate, lc->noWait, pushedDown);
2164 * FOR UPDATE/SHARE of subquery is propagated to all of
2165 * subquery's rels, too. We could do this later (based on
2166 * the marking of the subquery RTE) but it is convenient
2167 * to have local knowledge in each query level about which
2168 * rels need to be opened with RowShareLock.
2170 transformLockingClause(pstate, rte->subquery,
2174 /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
2181 /* just the named tables */
2182 foreach(l, lockedRels)
2184 RangeVar *thisrel = (RangeVar *) lfirst(l);
2186 /* For simplicity we insist on unqualified alias names here */
2187 if (thisrel->catalogname || thisrel->schemaname)
2189 (errcode(ERRCODE_SYNTAX_ERROR),
2190 errmsg("SELECT FOR UPDATE/SHARE must specify unqualified relation names"),
2191 parser_errposition(pstate, thisrel->location)));
2194 foreach(rt, qry->rtable)
2196 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2199 if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
2201 switch (rte->rtekind)
2204 applyLockingClause(qry, i,
2205 lc->forUpdate, lc->noWait,
2207 rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2210 applyLockingClause(qry, i,
2211 lc->forUpdate, lc->noWait,
2213 /* see comment above */
2214 transformLockingClause(pstate, rte->subquery,
2219 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2220 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a join"),
2221 parser_errposition(pstate, thisrel->location)));
2225 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2226 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to NEW or OLD"),
2227 parser_errposition(pstate, thisrel->location)));
2231 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2232 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a function"),
2233 parser_errposition(pstate, thisrel->location)));
2237 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2238 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES"),
2239 parser_errposition(pstate, thisrel->location)));
2243 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2244 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a WITH query"),
2245 parser_errposition(pstate, thisrel->location)));
2248 elog(ERROR, "unrecognized RTE type: %d",
2249 (int) rte->rtekind);
2252 break; /* out of foreach loop */
2257 (errcode(ERRCODE_UNDEFINED_TABLE),
2258 errmsg("relation \"%s\" in FOR UPDATE/SHARE clause not found in FROM clause",
2260 parser_errposition(pstate, thisrel->location)));
2266 * Record locking info for a single rangetable item
2269 applyLockingClause(Query *qry, Index rtindex,
2270 bool forUpdate, bool noWait, bool pushedDown)
2274 /* If it's an explicit clause, make sure hasForUpdate gets set */
2276 qry->hasForUpdate = true;
2278 /* Check for pre-existing entry for same rtindex */
2279 if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
2282 * If the same RTE is specified both FOR UPDATE and FOR SHARE, treat
2283 * it as FOR UPDATE. (Reasonable, since you can't take both a shared
2284 * and exclusive lock at the same time; it'll end up being exclusive
2287 * We also consider that NOWAIT wins if it's specified both ways. This
2288 * is a bit more debatable but raising an error doesn't seem helpful.
2289 * (Consider for instance SELECT FOR UPDATE NOWAIT from a view that
2290 * internally contains a plain FOR UPDATE spec.)
2292 * And of course pushedDown becomes false if any clause is explicit.
2294 rc->forUpdate |= forUpdate;
2295 rc->noWait |= noWait;
2296 rc->pushedDown &= pushedDown;
2300 /* Make a new RowMarkClause */
2301 rc = makeNode(RowMarkClause);
2303 rc->forUpdate = forUpdate;
2304 rc->noWait = noWait;
2305 rc->pushedDown = pushedDown;
2306 qry->rowMarks = lappend(qry->rowMarks, rc);