Remove dependency to query text in JumbleQuery()
[pgsql.git] / src / backend / parser / analyze.c
blob40066320927867d8e96014a5bba3537b87083aa9
1 /*-------------------------------------------------------------------------
3 * analyze.c
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, EXPLAIN, and CREATE TABLE AS are exceptions because they
14 * contain optimizable statements, which we should transform.
17 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
18 * Portions Copyright (c) 1994, Regents of the University of California
20 * src/backend/parser/analyze.c
22 *-------------------------------------------------------------------------
25 #include "postgres.h"
27 #include "access/sysattr.h"
28 #include "catalog/pg_proc.h"
29 #include "catalog/pg_type.h"
30 #include "commands/defrem.h"
31 #include "miscadmin.h"
32 #include "nodes/makefuncs.h"
33 #include "nodes/nodeFuncs.h"
34 #include "nodes/queryjumble.h"
35 #include "optimizer/optimizer.h"
36 #include "parser/analyze.h"
37 #include "parser/parse_agg.h"
38 #include "parser/parse_clause.h"
39 #include "parser/parse_coerce.h"
40 #include "parser/parse_collate.h"
41 #include "parser/parse_cte.h"
42 #include "parser/parse_expr.h"
43 #include "parser/parse_func.h"
44 #include "parser/parse_merge.h"
45 #include "parser/parse_oper.h"
46 #include "parser/parse_param.h"
47 #include "parser/parse_relation.h"
48 #include "parser/parse_target.h"
49 #include "parser/parse_type.h"
50 #include "parser/parsetree.h"
51 #include "rewrite/rewriteManip.h"
52 #include "utils/backend_status.h"
53 #include "utils/builtins.h"
54 #include "utils/guc.h"
55 #include "utils/rel.h"
56 #include "utils/syscache.h"
59 /* Hook for plugins to get control at end of parse analysis */
60 post_parse_analyze_hook_type post_parse_analyze_hook = NULL;
62 static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree);
63 static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
64 static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
65 static OnConflictExpr *transformOnConflictClause(ParseState *pstate,
66 OnConflictClause *onConflictClause);
67 static int count_rowexpr_columns(ParseState *pstate, Node *expr);
68 static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
69 static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
70 static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
71 static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
72 bool isTopLevel, List **targetlist);
73 static void determineRecursiveColTypes(ParseState *pstate,
74 Node *larg, List *nrtargetlist);
75 static Query *transformReturnStmt(ParseState *pstate, ReturnStmt *stmt);
76 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
77 static List *transformReturningList(ParseState *pstate, List *returningList);
78 static Query *transformPLAssignStmt(ParseState *pstate,
79 PLAssignStmt *stmt);
80 static Query *transformDeclareCursorStmt(ParseState *pstate,
81 DeclareCursorStmt *stmt);
82 static Query *transformExplainStmt(ParseState *pstate,
83 ExplainStmt *stmt);
84 static Query *transformCreateTableAsStmt(ParseState *pstate,
85 CreateTableAsStmt *stmt);
86 static Query *transformCallStmt(ParseState *pstate,
87 CallStmt *stmt);
88 static void transformLockingClause(ParseState *pstate, Query *qry,
89 LockingClause *lc, bool pushedDown);
90 #ifdef RAW_EXPRESSION_COVERAGE_TEST
91 static bool test_raw_expression_coverage(Node *node, void *context);
92 #endif
96 * parse_analyze_fixedparams
97 * Analyze a raw parse tree and transform it to Query form.
99 * Optionally, information about $n parameter types can be supplied.
100 * References to $n indexes not defined by paramTypes[] are disallowed.
102 * The result is a Query node. Optimizable statements require considerable
103 * transformation, while utility-type statements are simply hung off
104 * a dummy CMD_UTILITY Query node.
106 Query *
107 parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
108 const Oid *paramTypes, int numParams,
109 QueryEnvironment *queryEnv)
111 ParseState *pstate = make_parsestate(NULL);
112 Query *query;
113 JumbleState *jstate = NULL;
115 Assert(sourceText != NULL); /* required as of 8.4 */
117 pstate->p_sourcetext = sourceText;
119 if (numParams > 0)
120 setup_parse_fixed_parameters(pstate, paramTypes, numParams);
122 pstate->p_queryEnv = queryEnv;
124 query = transformTopLevelStmt(pstate, parseTree);
126 if (IsQueryIdEnabled())
127 jstate = JumbleQuery(query);
129 if (post_parse_analyze_hook)
130 (*post_parse_analyze_hook) (pstate, query, jstate);
132 free_parsestate(pstate);
134 pgstat_report_query_id(query->queryId, false);
136 return query;
140 * parse_analyze_varparams
142 * This variant is used when it's okay to deduce information about $n
143 * symbol datatypes from context. The passed-in paramTypes[] array can
144 * be modified or enlarged (via repalloc).
146 Query *
147 parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
148 Oid **paramTypes, int *numParams,
149 QueryEnvironment *queryEnv)
151 ParseState *pstate = make_parsestate(NULL);
152 Query *query;
153 JumbleState *jstate = NULL;
155 Assert(sourceText != NULL); /* required as of 8.4 */
157 pstate->p_sourcetext = sourceText;
159 setup_parse_variable_parameters(pstate, paramTypes, numParams);
161 pstate->p_queryEnv = queryEnv;
163 query = transformTopLevelStmt(pstate, parseTree);
165 /* make sure all is well with parameter types */
166 check_variable_parameters(pstate, query);
168 if (IsQueryIdEnabled())
169 jstate = JumbleQuery(query);
171 if (post_parse_analyze_hook)
172 (*post_parse_analyze_hook) (pstate, query, jstate);
174 free_parsestate(pstate);
176 pgstat_report_query_id(query->queryId, false);
178 return query;
182 * parse_analyze_withcb
184 * This variant is used when the caller supplies their own parser callback to
185 * resolve parameters and possibly other things.
187 Query *
188 parse_analyze_withcb(RawStmt *parseTree, const char *sourceText,
189 ParserSetupHook parserSetup,
190 void *parserSetupArg,
191 QueryEnvironment *queryEnv)
193 ParseState *pstate = make_parsestate(NULL);
194 Query *query;
195 JumbleState *jstate = NULL;
197 Assert(sourceText != NULL); /* required as of 8.4 */
199 pstate->p_sourcetext = sourceText;
200 pstate->p_queryEnv = queryEnv;
201 (*parserSetup) (pstate, parserSetupArg);
203 query = transformTopLevelStmt(pstate, parseTree);
205 if (IsQueryIdEnabled())
206 jstate = JumbleQuery(query);
208 if (post_parse_analyze_hook)
209 (*post_parse_analyze_hook) (pstate, query, jstate);
211 free_parsestate(pstate);
213 pgstat_report_query_id(query->queryId, false);
215 return query;
220 * parse_sub_analyze
221 * Entry point for recursively analyzing a sub-statement.
223 Query *
224 parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
225 CommonTableExpr *parentCTE,
226 bool locked_from_parent,
227 bool resolve_unknowns)
229 ParseState *pstate = make_parsestate(parentParseState);
230 Query *query;
232 pstate->p_parent_cte = parentCTE;
233 pstate->p_locked_from_parent = locked_from_parent;
234 pstate->p_resolve_unknowns = resolve_unknowns;
236 query = transformStmt(pstate, parseTree);
238 free_parsestate(pstate);
240 return query;
244 * transformTopLevelStmt -
245 * transform a Parse tree into a Query tree.
247 * This function is just responsible for transferring statement location data
248 * from the RawStmt into the finished Query.
250 Query *
251 transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
253 Query *result;
255 /* We're at top level, so allow SELECT INTO */
256 result = transformOptionalSelectInto(pstate, parseTree->stmt);
258 result->stmt_location = parseTree->stmt_location;
259 result->stmt_len = parseTree->stmt_len;
261 return result;
265 * transformOptionalSelectInto -
266 * If SELECT has INTO, convert it to CREATE TABLE AS.
268 * The only thing we do here that we don't do in transformStmt() is to
269 * convert SELECT ... INTO into CREATE TABLE AS. Since utility statements
270 * aren't allowed within larger statements, this is only allowed at the top
271 * of the parse tree, and so we only try it before entering the recursive
272 * transformStmt() processing.
274 static Query *
275 transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
277 if (IsA(parseTree, SelectStmt))
279 SelectStmt *stmt = (SelectStmt *) parseTree;
281 /* If it's a set-operation tree, drill down to leftmost SelectStmt */
282 while (stmt && stmt->op != SETOP_NONE)
283 stmt = stmt->larg;
284 Assert(stmt && IsA(stmt, SelectStmt) && stmt->larg == NULL);
286 if (stmt->intoClause)
288 CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
290 ctas->query = parseTree;
291 ctas->into = stmt->intoClause;
292 ctas->objtype = OBJECT_TABLE;
293 ctas->is_select_into = true;
296 * Remove the intoClause from the SelectStmt. This makes it safe
297 * for transformSelectStmt to complain if it finds intoClause set
298 * (implying that the INTO appeared in a disallowed place).
300 stmt->intoClause = NULL;
302 parseTree = (Node *) ctas;
306 return transformStmt(pstate, parseTree);
310 * transformStmt -
311 * recursively transform a Parse tree into a Query tree.
313 Query *
314 transformStmt(ParseState *pstate, Node *parseTree)
316 Query *result;
319 * We apply RAW_EXPRESSION_COVERAGE_TEST testing to basic DML statements;
320 * we can't just run it on everything because raw_expression_tree_walker()
321 * doesn't claim to handle utility statements.
323 #ifdef RAW_EXPRESSION_COVERAGE_TEST
324 switch (nodeTag(parseTree))
326 case T_SelectStmt:
327 case T_InsertStmt:
328 case T_UpdateStmt:
329 case T_DeleteStmt:
330 case T_MergeStmt:
331 (void) test_raw_expression_coverage(parseTree, NULL);
332 break;
333 default:
334 break;
336 #endif /* RAW_EXPRESSION_COVERAGE_TEST */
338 switch (nodeTag(parseTree))
341 * Optimizable statements
343 case T_InsertStmt:
344 result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
345 break;
347 case T_DeleteStmt:
348 result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
349 break;
351 case T_UpdateStmt:
352 result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
353 break;
355 case T_MergeStmt:
356 result = transformMergeStmt(pstate, (MergeStmt *) parseTree);
357 break;
359 case T_SelectStmt:
361 SelectStmt *n = (SelectStmt *) parseTree;
363 if (n->valuesLists)
364 result = transformValuesClause(pstate, n);
365 else if (n->op == SETOP_NONE)
366 result = transformSelectStmt(pstate, n);
367 else
368 result = transformSetOperationStmt(pstate, n);
370 break;
372 case T_ReturnStmt:
373 result = transformReturnStmt(pstate, (ReturnStmt *) parseTree);
374 break;
376 case T_PLAssignStmt:
377 result = transformPLAssignStmt(pstate,
378 (PLAssignStmt *) parseTree);
379 break;
382 * Special cases
384 case T_DeclareCursorStmt:
385 result = transformDeclareCursorStmt(pstate,
386 (DeclareCursorStmt *) parseTree);
387 break;
389 case T_ExplainStmt:
390 result = transformExplainStmt(pstate,
391 (ExplainStmt *) parseTree);
392 break;
394 case T_CreateTableAsStmt:
395 result = transformCreateTableAsStmt(pstate,
396 (CreateTableAsStmt *) parseTree);
397 break;
399 case T_CallStmt:
400 result = transformCallStmt(pstate,
401 (CallStmt *) parseTree);
402 break;
404 default:
407 * other statements don't require any transformation; just return
408 * the original parsetree with a Query node plastered on top.
410 result = makeNode(Query);
411 result->commandType = CMD_UTILITY;
412 result->utilityStmt = (Node *) parseTree;
413 break;
416 /* Mark as original query until we learn differently */
417 result->querySource = QSRC_ORIGINAL;
418 result->canSetTag = true;
420 return result;
424 * analyze_requires_snapshot
425 * Returns true if a snapshot must be set before doing parse analysis
426 * on the given raw parse tree.
428 * Classification here should match transformStmt().
430 bool
431 analyze_requires_snapshot(RawStmt *parseTree)
433 bool result;
435 switch (nodeTag(parseTree->stmt))
438 * Optimizable statements
440 case T_InsertStmt:
441 case T_DeleteStmt:
442 case T_UpdateStmt:
443 case T_MergeStmt:
444 case T_SelectStmt:
445 case T_PLAssignStmt:
446 result = true;
447 break;
450 * Special cases
452 case T_DeclareCursorStmt:
453 case T_ExplainStmt:
454 case T_CreateTableAsStmt:
455 /* yes, because we must analyze the contained statement */
456 result = true;
457 break;
459 default:
460 /* other utility statements don't have any real parse analysis */
461 result = false;
462 break;
465 return result;
469 * transformDeleteStmt -
470 * transforms a Delete Statement
472 static Query *
473 transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
475 Query *qry = makeNode(Query);
476 ParseNamespaceItem *nsitem;
477 Node *qual;
479 qry->commandType = CMD_DELETE;
481 /* process the WITH clause independently of all else */
482 if (stmt->withClause)
484 qry->hasRecursive = stmt->withClause->recursive;
485 qry->cteList = transformWithClause(pstate, stmt->withClause);
486 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
489 /* set up range table with just the result rel */
490 qry->resultRelation = setTargetTable(pstate, stmt->relation,
491 stmt->relation->inh,
492 true,
493 ACL_DELETE);
494 nsitem = pstate->p_target_nsitem;
496 /* there's no DISTINCT in DELETE */
497 qry->distinctClause = NIL;
499 /* subqueries in USING cannot access the result relation */
500 nsitem->p_lateral_only = true;
501 nsitem->p_lateral_ok = false;
504 * The USING clause is non-standard SQL syntax, and is equivalent in
505 * functionality to the FROM list that can be specified for UPDATE. The
506 * USING keyword is used rather than FROM because FROM is already a
507 * keyword in the DELETE syntax.
509 transformFromClause(pstate, stmt->usingClause);
511 /* remaining clauses can reference the result relation normally */
512 nsitem->p_lateral_only = false;
513 nsitem->p_lateral_ok = true;
515 qual = transformWhereClause(pstate, stmt->whereClause,
516 EXPR_KIND_WHERE, "WHERE");
518 qry->returningList = transformReturningList(pstate, stmt->returningList);
520 /* done building the range table and jointree */
521 qry->rtable = pstate->p_rtable;
522 qry->rteperminfos = pstate->p_rteperminfos;
523 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
525 qry->hasSubLinks = pstate->p_hasSubLinks;
526 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
527 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
528 qry->hasAggs = pstate->p_hasAggs;
530 assign_query_collations(pstate, qry);
532 /* this must be done after collations, for reliable comparison of exprs */
533 if (pstate->p_hasAggs)
534 parseCheckAggregates(pstate, qry);
536 return qry;
540 * transformInsertStmt -
541 * transform an Insert Statement
543 static Query *
544 transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
546 Query *qry = makeNode(Query);
547 SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
548 List *exprList = NIL;
549 bool isGeneralSelect;
550 List *sub_rtable;
551 List *sub_rteperminfos;
552 List *sub_namespace;
553 List *icolumns;
554 List *attrnos;
555 ParseNamespaceItem *nsitem;
556 RTEPermissionInfo *perminfo;
557 ListCell *icols;
558 ListCell *attnos;
559 ListCell *lc;
560 bool isOnConflictUpdate;
561 AclMode targetPerms;
563 /* There can't be any outer WITH to worry about */
564 Assert(pstate->p_ctenamespace == NIL);
566 qry->commandType = CMD_INSERT;
567 pstate->p_is_insert = true;
569 /* process the WITH clause independently of all else */
570 if (stmt->withClause)
572 qry->hasRecursive = stmt->withClause->recursive;
573 qry->cteList = transformWithClause(pstate, stmt->withClause);
574 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
577 qry->override = stmt->override;
579 isOnConflictUpdate = (stmt->onConflictClause &&
580 stmt->onConflictClause->action == ONCONFLICT_UPDATE);
583 * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
584 * VALUES list, or general SELECT input. We special-case VALUES, both for
585 * efficiency and so we can handle DEFAULT specifications.
587 * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
588 * VALUES clause. If we have any of those, treat it as a general SELECT;
589 * so it will work, but you can't use DEFAULT items together with those.
591 isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
592 selectStmt->sortClause != NIL ||
593 selectStmt->limitOffset != NULL ||
594 selectStmt->limitCount != NULL ||
595 selectStmt->lockingClause != NIL ||
596 selectStmt->withClause != NULL));
599 * If a non-nil rangetable/namespace was passed in, and we are doing
600 * INSERT/SELECT, arrange to pass the rangetable/rteperminfos/namespace
601 * down to the SELECT. This can only happen if we are inside a CREATE
602 * RULE, and in that case we want the rule's OLD and NEW rtable entries to
603 * appear as part of the SELECT's rtable, not as outer references for it.
604 * (Kluge!) The SELECT's joinlist is not affected however. We must do
605 * this before adding the target table to the INSERT's rtable.
607 if (isGeneralSelect)
609 sub_rtable = pstate->p_rtable;
610 pstate->p_rtable = NIL;
611 sub_rteperminfos = pstate->p_rteperminfos;
612 pstate->p_rteperminfos = NIL;
613 sub_namespace = pstate->p_namespace;
614 pstate->p_namespace = NIL;
616 else
618 sub_rtable = NIL; /* not used, but keep compiler quiet */
619 sub_rteperminfos = NIL;
620 sub_namespace = NIL;
624 * Must get write lock on INSERT target table before scanning SELECT, else
625 * we will grab the wrong kind of initial lock if the target table is also
626 * mentioned in the SELECT part. Note that the target table is not added
627 * to the joinlist or namespace.
629 targetPerms = ACL_INSERT;
630 if (isOnConflictUpdate)
631 targetPerms |= ACL_UPDATE;
632 qry->resultRelation = setTargetTable(pstate, stmt->relation,
633 false, false, targetPerms);
635 /* Validate stmt->cols list, or build default list if no list given */
636 icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
637 Assert(list_length(icolumns) == list_length(attrnos));
640 * Determine which variant of INSERT we have.
642 if (selectStmt == NULL)
645 * We have INSERT ... DEFAULT VALUES. We can handle this case by
646 * emitting an empty targetlist --- all columns will be defaulted when
647 * the planner expands the targetlist.
649 exprList = NIL;
651 else if (isGeneralSelect)
654 * We make the sub-pstate a child of the outer pstate so that it can
655 * see any Param definitions supplied from above. Since the outer
656 * pstate's rtable and namespace are presently empty, there are no
657 * side-effects of exposing names the sub-SELECT shouldn't be able to
658 * see.
660 ParseState *sub_pstate = make_parsestate(pstate);
661 Query *selectQuery;
664 * Process the source SELECT.
666 * It is important that this be handled just like a standalone SELECT;
667 * otherwise the behavior of SELECT within INSERT might be different
668 * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
669 * bugs of just that nature...)
671 * The sole exception is that we prevent resolving unknown-type
672 * outputs as TEXT. This does not change the semantics since if the
673 * column type matters semantically, it would have been resolved to
674 * something else anyway. Doing this lets us resolve such outputs as
675 * the target column's type, which we handle below.
677 sub_pstate->p_rtable = sub_rtable;
678 sub_pstate->p_rteperminfos = sub_rteperminfos;
679 sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
680 sub_pstate->p_nullingrels = NIL;
681 sub_pstate->p_namespace = sub_namespace;
682 sub_pstate->p_resolve_unknowns = false;
684 selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
686 free_parsestate(sub_pstate);
688 /* The grammar should have produced a SELECT */
689 if (!IsA(selectQuery, Query) ||
690 selectQuery->commandType != CMD_SELECT)
691 elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
694 * Make the source be a subquery in the INSERT's rangetable, and add
695 * it to the INSERT's joinlist (but not the namespace).
697 nsitem = addRangeTableEntryForSubquery(pstate,
698 selectQuery,
699 makeAlias("*SELECT*", NIL),
700 false,
701 false);
702 addNSItemToQuery(pstate, nsitem, true, false, false);
704 /*----------
705 * Generate an expression list for the INSERT that selects all the
706 * non-resjunk columns from the subquery. (INSERT's tlist must be
707 * separate from the subquery's tlist because we may add columns,
708 * insert datatype coercions, etc.)
710 * HACK: unknown-type constants and params in the SELECT's targetlist
711 * are copied up as-is rather than being referenced as subquery
712 * outputs. This is to ensure that when we try to coerce them to
713 * the target column's datatype, the right things happen (see
714 * special cases in coerce_type). Otherwise, this fails:
715 * INSERT INTO foo SELECT 'bar', ... FROM baz
716 *----------
718 exprList = NIL;
719 foreach(lc, selectQuery->targetList)
721 TargetEntry *tle = (TargetEntry *) lfirst(lc);
722 Expr *expr;
724 if (tle->resjunk)
725 continue;
726 if (tle->expr &&
727 (IsA(tle->expr, Const) || IsA(tle->expr, Param)) &&
728 exprType((Node *) tle->expr) == UNKNOWNOID)
729 expr = tle->expr;
730 else
732 Var *var = makeVarFromTargetEntry(nsitem->p_rtindex, tle);
734 var->location = exprLocation((Node *) tle->expr);
735 expr = (Expr *) var;
737 exprList = lappend(exprList, expr);
740 /* Prepare row for assignment to target table */
741 exprList = transformInsertRow(pstate, exprList,
742 stmt->cols,
743 icolumns, attrnos,
744 false);
746 else if (list_length(selectStmt->valuesLists) > 1)
749 * Process INSERT ... VALUES with multiple VALUES sublists. We
750 * generate a VALUES RTE holding the transformed expression lists, and
751 * build up a targetlist containing Vars that reference the VALUES
752 * RTE.
754 List *exprsLists = NIL;
755 List *coltypes = NIL;
756 List *coltypmods = NIL;
757 List *colcollations = NIL;
758 int sublist_length = -1;
759 bool lateral = false;
761 Assert(selectStmt->intoClause == NULL);
763 foreach(lc, selectStmt->valuesLists)
765 List *sublist = (List *) lfirst(lc);
768 * Do basic expression transformation (same as a ROW() expr, but
769 * allow SetToDefault at top level)
771 sublist = transformExpressionList(pstate, sublist,
772 EXPR_KIND_VALUES, true);
775 * All the sublists must be the same length, *after*
776 * transformation (which might expand '*' into multiple items).
777 * The VALUES RTE can't handle anything different.
779 if (sublist_length < 0)
781 /* Remember post-transformation length of first sublist */
782 sublist_length = list_length(sublist);
784 else if (sublist_length != list_length(sublist))
786 ereport(ERROR,
787 (errcode(ERRCODE_SYNTAX_ERROR),
788 errmsg("VALUES lists must all be the same length"),
789 parser_errposition(pstate,
790 exprLocation((Node *) sublist))));
794 * Prepare row for assignment to target table. We process any
795 * indirection on the target column specs normally but then strip
796 * off the resulting field/array assignment nodes, since we don't
797 * want the parsed statement to contain copies of those in each
798 * VALUES row. (It's annoying to have to transform the
799 * indirection specs over and over like this, but avoiding it
800 * would take some really messy refactoring of
801 * transformAssignmentIndirection.)
803 sublist = transformInsertRow(pstate, sublist,
804 stmt->cols,
805 icolumns, attrnos,
806 true);
809 * We must assign collations now because assign_query_collations
810 * doesn't process rangetable entries. We just assign all the
811 * collations independently in each row, and don't worry about
812 * whether they are consistent vertically. The outer INSERT query
813 * isn't going to care about the collations of the VALUES columns,
814 * so it's not worth the effort to identify a common collation for
815 * each one here. (But note this does have one user-visible
816 * consequence: INSERT ... VALUES won't complain about conflicting
817 * explicit COLLATEs in a column, whereas the same VALUES
818 * construct in another context would complain.)
820 assign_list_collations(pstate, sublist);
822 exprsLists = lappend(exprsLists, sublist);
826 * Construct column type/typmod/collation lists for the VALUES RTE.
827 * Every expression in each column has been coerced to the type/typmod
828 * of the corresponding target column or subfield, so it's sufficient
829 * to look at the exprType/exprTypmod of the first row. We don't care
830 * about the collation labeling, so just fill in InvalidOid for that.
832 foreach(lc, (List *) linitial(exprsLists))
834 Node *val = (Node *) lfirst(lc);
836 coltypes = lappend_oid(coltypes, exprType(val));
837 coltypmods = lappend_int(coltypmods, exprTypmod(val));
838 colcollations = lappend_oid(colcollations, InvalidOid);
842 * Ordinarily there can't be any current-level Vars in the expression
843 * lists, because the namespace was empty ... but if we're inside
844 * CREATE RULE, then NEW/OLD references might appear. In that case we
845 * have to mark the VALUES RTE as LATERAL.
847 if (list_length(pstate->p_rtable) != 1 &&
848 contain_vars_of_level((Node *) exprsLists, 0))
849 lateral = true;
852 * Generate the VALUES RTE
854 nsitem = addRangeTableEntryForValues(pstate, exprsLists,
855 coltypes, coltypmods, colcollations,
856 NULL, lateral, true);
857 addNSItemToQuery(pstate, nsitem, true, false, false);
860 * Generate list of Vars referencing the RTE
862 exprList = expandNSItemVars(pstate, nsitem, 0, -1, NULL);
865 * Re-apply any indirection on the target column specs to the Vars
867 exprList = transformInsertRow(pstate, exprList,
868 stmt->cols,
869 icolumns, attrnos,
870 false);
872 else
875 * Process INSERT ... VALUES with a single VALUES sublist. We treat
876 * this case separately for efficiency. The sublist is just computed
877 * directly as the Query's targetlist, with no VALUES RTE. So it
878 * works just like a SELECT without any FROM.
880 List *valuesLists = selectStmt->valuesLists;
882 Assert(list_length(valuesLists) == 1);
883 Assert(selectStmt->intoClause == NULL);
886 * Do basic expression transformation (same as a ROW() expr, but allow
887 * SetToDefault at top level)
889 exprList = transformExpressionList(pstate,
890 (List *) linitial(valuesLists),
891 EXPR_KIND_VALUES_SINGLE,
892 true);
894 /* Prepare row for assignment to target table */
895 exprList = transformInsertRow(pstate, exprList,
896 stmt->cols,
897 icolumns, attrnos,
898 false);
902 * Generate query's target list using the computed list of expressions.
903 * Also, mark all the target columns as needing insert permissions.
905 perminfo = pstate->p_target_nsitem->p_perminfo;
906 qry->targetList = NIL;
907 Assert(list_length(exprList) <= list_length(icolumns));
908 forthree(lc, exprList, icols, icolumns, attnos, attrnos)
910 Expr *expr = (Expr *) lfirst(lc);
911 ResTarget *col = lfirst_node(ResTarget, icols);
912 AttrNumber attr_num = (AttrNumber) lfirst_int(attnos);
913 TargetEntry *tle;
915 tle = makeTargetEntry(expr,
916 attr_num,
917 col->name,
918 false);
919 qry->targetList = lappend(qry->targetList, tle);
921 perminfo->insertedCols = bms_add_member(perminfo->insertedCols,
922 attr_num - FirstLowInvalidHeapAttributeNumber);
926 * If we have any clauses yet to process, set the query namespace to
927 * contain only the target relation, removing any entries added in a
928 * sub-SELECT or VALUES list.
930 if (stmt->onConflictClause || stmt->returningList)
932 pstate->p_namespace = NIL;
933 addNSItemToQuery(pstate, pstate->p_target_nsitem,
934 false, true, true);
937 /* Process ON CONFLICT, if any. */
938 if (stmt->onConflictClause)
939 qry->onConflict = transformOnConflictClause(pstate,
940 stmt->onConflictClause);
942 /* Process RETURNING, if any. */
943 if (stmt->returningList)
944 qry->returningList = transformReturningList(pstate,
945 stmt->returningList);
947 /* done building the range table and jointree */
948 qry->rtable = pstate->p_rtable;
949 qry->rteperminfos = pstate->p_rteperminfos;
950 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
952 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
953 qry->hasSubLinks = pstate->p_hasSubLinks;
955 assign_query_collations(pstate, qry);
957 return qry;
961 * Prepare an INSERT row for assignment to the target table.
963 * exprlist: transformed expressions for source values; these might come from
964 * a VALUES row, or be Vars referencing a sub-SELECT or VALUES RTE output.
965 * stmtcols: original target-columns spec for INSERT (we just test for NIL)
966 * icolumns: effective target-columns spec (list of ResTarget)
967 * attrnos: integer column numbers (must be same length as icolumns)
968 * strip_indirection: if true, remove any field/array assignment nodes
970 List *
971 transformInsertRow(ParseState *pstate, List *exprlist,
972 List *stmtcols, List *icolumns, List *attrnos,
973 bool strip_indirection)
975 List *result;
976 ListCell *lc;
977 ListCell *icols;
978 ListCell *attnos;
981 * Check length of expr list. It must not have more expressions than
982 * there are target columns. We allow fewer, but only if no explicit
983 * columns list was given (the remaining columns are implicitly
984 * defaulted). Note we must check this *after* transformation because
985 * that could expand '*' into multiple items.
987 if (list_length(exprlist) > list_length(icolumns))
988 ereport(ERROR,
989 (errcode(ERRCODE_SYNTAX_ERROR),
990 errmsg("INSERT has more expressions than target columns"),
991 parser_errposition(pstate,
992 exprLocation(list_nth(exprlist,
993 list_length(icolumns))))));
994 if (stmtcols != NIL &&
995 list_length(exprlist) < list_length(icolumns))
998 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
999 * where the user accidentally created a RowExpr instead of separate
1000 * columns. Add a suitable hint if that seems to be the problem,
1001 * because the main error message is quite misleading for this case.
1002 * (If there's no stmtcols, you'll get something about data type
1003 * mismatch, which is less misleading so we don't worry about giving a
1004 * hint in that case.)
1006 ereport(ERROR,
1007 (errcode(ERRCODE_SYNTAX_ERROR),
1008 errmsg("INSERT has more target columns than expressions"),
1009 ((list_length(exprlist) == 1 &&
1010 count_rowexpr_columns(pstate, linitial(exprlist)) ==
1011 list_length(icolumns)) ?
1012 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),
1013 parser_errposition(pstate,
1014 exprLocation(list_nth(icolumns,
1015 list_length(exprlist))))));
1019 * Prepare columns for assignment to target table.
1021 result = NIL;
1022 forthree(lc, exprlist, icols, icolumns, attnos, attrnos)
1024 Expr *expr = (Expr *) lfirst(lc);
1025 ResTarget *col = lfirst_node(ResTarget, icols);
1026 int attno = lfirst_int(attnos);
1028 expr = transformAssignedExpr(pstate, expr,
1029 EXPR_KIND_INSERT_TARGET,
1030 col->name,
1031 attno,
1032 col->indirection,
1033 col->location);
1035 if (strip_indirection)
1037 while (expr)
1039 if (IsA(expr, FieldStore))
1041 FieldStore *fstore = (FieldStore *) expr;
1043 expr = (Expr *) linitial(fstore->newvals);
1045 else if (IsA(expr, SubscriptingRef))
1047 SubscriptingRef *sbsref = (SubscriptingRef *) expr;
1049 if (sbsref->refassgnexpr == NULL)
1050 break;
1052 expr = sbsref->refassgnexpr;
1054 else
1055 break;
1059 result = lappend(result, expr);
1062 return result;
1066 * transformOnConflictClause -
1067 * transforms an OnConflictClause in an INSERT
1069 static OnConflictExpr *
1070 transformOnConflictClause(ParseState *pstate,
1071 OnConflictClause *onConflictClause)
1073 ParseNamespaceItem *exclNSItem = NULL;
1074 List *arbiterElems;
1075 Node *arbiterWhere;
1076 Oid arbiterConstraint;
1077 List *onConflictSet = NIL;
1078 Node *onConflictWhere = NULL;
1079 int exclRelIndex = 0;
1080 List *exclRelTlist = NIL;
1081 OnConflictExpr *result;
1084 * If this is ON CONFLICT ... UPDATE, first create the range table entry
1085 * for the EXCLUDED pseudo relation, so that that will be present while
1086 * processing arbiter expressions. (You can't actually reference it from
1087 * there, but this provides a useful error message if you try.)
1089 if (onConflictClause->action == ONCONFLICT_UPDATE)
1091 Relation targetrel = pstate->p_target_relation;
1092 RangeTblEntry *exclRte;
1094 exclNSItem = addRangeTableEntryForRelation(pstate,
1095 targetrel,
1096 RowExclusiveLock,
1097 makeAlias("excluded", NIL),
1098 false, false);
1099 exclRte = exclNSItem->p_rte;
1100 exclRelIndex = exclNSItem->p_rtindex;
1103 * relkind is set to composite to signal that we're not dealing with
1104 * an actual relation, and no permission checks are required on it.
1105 * (We'll check the actual target relation, instead.)
1107 exclRte->relkind = RELKIND_COMPOSITE_TYPE;
1109 /* Create EXCLUDED rel's targetlist for use by EXPLAIN */
1110 exclRelTlist = BuildOnConflictExcludedTargetlist(targetrel,
1111 exclRelIndex);
1114 /* Process the arbiter clause, ON CONFLICT ON (...) */
1115 transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
1116 &arbiterWhere, &arbiterConstraint);
1118 /* Process DO UPDATE */
1119 if (onConflictClause->action == ONCONFLICT_UPDATE)
1122 * Expressions in the UPDATE targetlist need to be handled like UPDATE
1123 * not INSERT. We don't need to save/restore this because all INSERT
1124 * expressions have been parsed already.
1126 pstate->p_is_insert = false;
1129 * Add the EXCLUDED pseudo relation to the query namespace, making it
1130 * available in the UPDATE subexpressions.
1132 addNSItemToQuery(pstate, exclNSItem, false, true, true);
1135 * Now transform the UPDATE subexpressions.
1137 onConflictSet =
1138 transformUpdateTargetList(pstate, onConflictClause->targetList);
1140 onConflictWhere = transformWhereClause(pstate,
1141 onConflictClause->whereClause,
1142 EXPR_KIND_WHERE, "WHERE");
1145 * Remove the EXCLUDED pseudo relation from the query namespace, since
1146 * it's not supposed to be available in RETURNING. (Maybe someday we
1147 * could allow that, and drop this step.)
1149 Assert((ParseNamespaceItem *) llast(pstate->p_namespace) == exclNSItem);
1150 pstate->p_namespace = list_delete_last(pstate->p_namespace);
1153 /* Finally, build ON CONFLICT DO [NOTHING | UPDATE] expression */
1154 result = makeNode(OnConflictExpr);
1156 result->action = onConflictClause->action;
1157 result->arbiterElems = arbiterElems;
1158 result->arbiterWhere = arbiterWhere;
1159 result->constraint = arbiterConstraint;
1160 result->onConflictSet = onConflictSet;
1161 result->onConflictWhere = onConflictWhere;
1162 result->exclRelIndex = exclRelIndex;
1163 result->exclRelTlist = exclRelTlist;
1165 return result;
1170 * BuildOnConflictExcludedTargetlist
1171 * Create target list for the EXCLUDED pseudo-relation of ON CONFLICT,
1172 * representing the columns of targetrel with varno exclRelIndex.
1174 * Note: Exported for use in the rewriter.
1176 List *
1177 BuildOnConflictExcludedTargetlist(Relation targetrel,
1178 Index exclRelIndex)
1180 List *result = NIL;
1181 int attno;
1182 Var *var;
1183 TargetEntry *te;
1186 * Note that resnos of the tlist must correspond to attnos of the
1187 * underlying relation, hence we need entries for dropped columns too.
1189 for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1191 Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1192 char *name;
1194 if (attr->attisdropped)
1197 * can't use atttypid here, but it doesn't really matter what type
1198 * the Const claims to be.
1200 var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1201 name = NULL;
1203 else
1205 var = makeVar(exclRelIndex, attno + 1,
1206 attr->atttypid, attr->atttypmod,
1207 attr->attcollation,
1209 name = pstrdup(NameStr(attr->attname));
1212 te = makeTargetEntry((Expr *) var,
1213 attno + 1,
1214 name,
1215 false);
1217 result = lappend(result, te);
1221 * Add a whole-row-Var entry to support references to "EXCLUDED.*". Like
1222 * the other entries in the EXCLUDED tlist, its resno must match the Var's
1223 * varattno, else the wrong things happen while resolving references in
1224 * setrefs.c. This is against normal conventions for targetlists, but
1225 * it's okay since we don't use this as a real tlist.
1227 var = makeVar(exclRelIndex, InvalidAttrNumber,
1228 targetrel->rd_rel->reltype,
1229 -1, InvalidOid, 0);
1230 te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1231 result = lappend(result, te);
1233 return result;
1238 * count_rowexpr_columns -
1239 * get number of columns contained in a ROW() expression;
1240 * return -1 if expression isn't a RowExpr or a Var referencing one.
1242 * This is currently used only for hint purposes, so we aren't terribly
1243 * tense about recognizing all possible cases. The Var case is interesting
1244 * because that's what we'll get in the INSERT ... SELECT (...) case.
1246 static int
1247 count_rowexpr_columns(ParseState *pstate, Node *expr)
1249 if (expr == NULL)
1250 return -1;
1251 if (IsA(expr, RowExpr))
1252 return list_length(((RowExpr *) expr)->args);
1253 if (IsA(expr, Var))
1255 Var *var = (Var *) expr;
1256 AttrNumber attnum = var->varattno;
1258 if (attnum > 0 && var->vartype == RECORDOID)
1260 RangeTblEntry *rte;
1262 rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1263 if (rte->rtekind == RTE_SUBQUERY)
1265 /* Subselect-in-FROM: examine sub-select's output expr */
1266 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1267 attnum);
1269 if (ste == NULL || ste->resjunk)
1270 return -1;
1271 expr = (Node *) ste->expr;
1272 if (IsA(expr, RowExpr))
1273 return list_length(((RowExpr *) expr)->args);
1277 return -1;
1282 * transformSelectStmt -
1283 * transforms a Select Statement
1285 * Note: this covers only cases with no set operations and no VALUES lists;
1286 * see below for the other cases.
1288 static Query *
1289 transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
1291 Query *qry = makeNode(Query);
1292 Node *qual;
1293 ListCell *l;
1295 qry->commandType = CMD_SELECT;
1297 /* process the WITH clause independently of all else */
1298 if (stmt->withClause)
1300 qry->hasRecursive = stmt->withClause->recursive;
1301 qry->cteList = transformWithClause(pstate, stmt->withClause);
1302 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1305 /* Complain if we get called from someplace where INTO is not allowed */
1306 if (stmt->intoClause)
1307 ereport(ERROR,
1308 (errcode(ERRCODE_SYNTAX_ERROR),
1309 errmsg("SELECT ... INTO is not allowed here"),
1310 parser_errposition(pstate,
1311 exprLocation((Node *) stmt->intoClause))));
1313 /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
1314 pstate->p_locking_clause = stmt->lockingClause;
1316 /* make WINDOW info available for window functions, too */
1317 pstate->p_windowdefs = stmt->windowClause;
1319 /* process the FROM clause */
1320 transformFromClause(pstate, stmt->fromClause);
1322 /* transform targetlist */
1323 qry->targetList = transformTargetList(pstate, stmt->targetList,
1324 EXPR_KIND_SELECT_TARGET);
1326 /* mark column origins */
1327 markTargetListOrigins(pstate, qry->targetList);
1329 /* transform WHERE */
1330 qual = transformWhereClause(pstate, stmt->whereClause,
1331 EXPR_KIND_WHERE, "WHERE");
1333 /* initial processing of HAVING clause is much like WHERE clause */
1334 qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
1335 EXPR_KIND_HAVING, "HAVING");
1338 * Transform sorting/grouping stuff. Do ORDER BY first because both
1339 * transformGroupClause and transformDistinctClause need the results. Note
1340 * that these functions can also change the targetList, so it's passed to
1341 * them by reference.
1343 qry->sortClause = transformSortClause(pstate,
1344 stmt->sortClause,
1345 &qry->targetList,
1346 EXPR_KIND_ORDER_BY,
1347 false /* allow SQL92 rules */ );
1349 qry->groupClause = transformGroupClause(pstate,
1350 stmt->groupClause,
1351 &qry->groupingSets,
1352 &qry->targetList,
1353 qry->sortClause,
1354 EXPR_KIND_GROUP_BY,
1355 false /* allow SQL92 rules */ );
1356 qry->groupDistinct = stmt->groupDistinct;
1358 if (stmt->distinctClause == NIL)
1360 qry->distinctClause = NIL;
1361 qry->hasDistinctOn = false;
1363 else if (linitial(stmt->distinctClause) == NULL)
1365 /* We had SELECT DISTINCT */
1366 qry->distinctClause = transformDistinctClause(pstate,
1367 &qry->targetList,
1368 qry->sortClause,
1369 false);
1370 qry->hasDistinctOn = false;
1372 else
1374 /* We had SELECT DISTINCT ON */
1375 qry->distinctClause = transformDistinctOnClause(pstate,
1376 stmt->distinctClause,
1377 &qry->targetList,
1378 qry->sortClause);
1379 qry->hasDistinctOn = true;
1382 /* transform LIMIT */
1383 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1384 EXPR_KIND_OFFSET, "OFFSET",
1385 stmt->limitOption);
1386 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1387 EXPR_KIND_LIMIT, "LIMIT",
1388 stmt->limitOption);
1389 qry->limitOption = stmt->limitOption;
1391 /* transform window clauses after we have seen all window functions */
1392 qry->windowClause = transformWindowDefinitions(pstate,
1393 pstate->p_windowdefs,
1394 &qry->targetList);
1396 /* resolve any still-unresolved output columns as being type text */
1397 if (pstate->p_resolve_unknowns)
1398 resolveTargetListUnknowns(pstate, qry->targetList);
1400 qry->rtable = pstate->p_rtable;
1401 qry->rteperminfos = pstate->p_rteperminfos;
1402 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1404 qry->hasSubLinks = pstate->p_hasSubLinks;
1405 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1406 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1407 qry->hasAggs = pstate->p_hasAggs;
1409 foreach(l, stmt->lockingClause)
1411 transformLockingClause(pstate, qry,
1412 (LockingClause *) lfirst(l), false);
1415 assign_query_collations(pstate, qry);
1417 /* this must be done after collations, for reliable comparison of exprs */
1418 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1419 parseCheckAggregates(pstate, qry);
1421 return qry;
1425 * transformValuesClause -
1426 * transforms a VALUES clause that's being used as a standalone SELECT
1428 * We build a Query containing a VALUES RTE, rather as if one had written
1429 * SELECT * FROM (VALUES ...) AS "*VALUES*"
1431 static Query *
1432 transformValuesClause(ParseState *pstate, SelectStmt *stmt)
1434 Query *qry = makeNode(Query);
1435 List *exprsLists = NIL;
1436 List *coltypes = NIL;
1437 List *coltypmods = NIL;
1438 List *colcollations = NIL;
1439 List **colexprs = NULL;
1440 int sublist_length = -1;
1441 bool lateral = false;
1442 ParseNamespaceItem *nsitem;
1443 ListCell *lc;
1444 ListCell *lc2;
1445 int i;
1447 qry->commandType = CMD_SELECT;
1449 /* Most SELECT stuff doesn't apply in a VALUES clause */
1450 Assert(stmt->distinctClause == NIL);
1451 Assert(stmt->intoClause == NULL);
1452 Assert(stmt->targetList == NIL);
1453 Assert(stmt->fromClause == NIL);
1454 Assert(stmt->whereClause == NULL);
1455 Assert(stmt->groupClause == NIL);
1456 Assert(stmt->havingClause == NULL);
1457 Assert(stmt->windowClause == NIL);
1458 Assert(stmt->op == SETOP_NONE);
1460 /* process the WITH clause independently of all else */
1461 if (stmt->withClause)
1463 qry->hasRecursive = stmt->withClause->recursive;
1464 qry->cteList = transformWithClause(pstate, stmt->withClause);
1465 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1469 * For each row of VALUES, transform the raw expressions.
1471 * Note that the intermediate representation we build is column-organized
1472 * not row-organized. That simplifies the type and collation processing
1473 * below.
1475 foreach(lc, stmt->valuesLists)
1477 List *sublist = (List *) lfirst(lc);
1480 * Do basic expression transformation (same as a ROW() expr, but here
1481 * we disallow SetToDefault)
1483 sublist = transformExpressionList(pstate, sublist,
1484 EXPR_KIND_VALUES, false);
1487 * All the sublists must be the same length, *after* transformation
1488 * (which might expand '*' into multiple items). The VALUES RTE can't
1489 * handle anything different.
1491 if (sublist_length < 0)
1493 /* Remember post-transformation length of first sublist */
1494 sublist_length = list_length(sublist);
1495 /* and allocate array for per-column lists */
1496 colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1498 else if (sublist_length != list_length(sublist))
1500 ereport(ERROR,
1501 (errcode(ERRCODE_SYNTAX_ERROR),
1502 errmsg("VALUES lists must all be the same length"),
1503 parser_errposition(pstate,
1504 exprLocation((Node *) sublist))));
1507 /* Build per-column expression lists */
1508 i = 0;
1509 foreach(lc2, sublist)
1511 Node *col = (Node *) lfirst(lc2);
1513 colexprs[i] = lappend(colexprs[i], col);
1514 i++;
1517 /* Release sub-list's cells to save memory */
1518 list_free(sublist);
1520 /* Prepare an exprsLists element for this row */
1521 exprsLists = lappend(exprsLists, NIL);
1525 * Now resolve the common types of the columns, and coerce everything to
1526 * those types. Then identify the common typmod and common collation, if
1527 * any, of each column.
1529 * We must do collation processing now because (1) assign_query_collations
1530 * doesn't process rangetable entries, and (2) we need to label the VALUES
1531 * RTE with column collations for use in the outer query. We don't
1532 * consider conflict of implicit collations to be an error here; instead
1533 * the column will just show InvalidOid as its collation, and you'll get a
1534 * failure later if that results in failure to resolve a collation.
1536 * Note we modify the per-column expression lists in-place.
1538 for (i = 0; i < sublist_length; i++)
1540 Oid coltype;
1541 int32 coltypmod;
1542 Oid colcoll;
1544 coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
1546 foreach(lc, colexprs[i])
1548 Node *col = (Node *) lfirst(lc);
1550 col = coerce_to_common_type(pstate, col, coltype, "VALUES");
1551 lfirst(lc) = (void *) col;
1554 coltypmod = select_common_typmod(pstate, colexprs[i], coltype);
1555 colcoll = select_common_collation(pstate, colexprs[i], true);
1557 coltypes = lappend_oid(coltypes, coltype);
1558 coltypmods = lappend_int(coltypmods, coltypmod);
1559 colcollations = lappend_oid(colcollations, colcoll);
1563 * Finally, rearrange the coerced expressions into row-organized lists.
1565 for (i = 0; i < sublist_length; i++)
1567 forboth(lc, colexprs[i], lc2, exprsLists)
1569 Node *col = (Node *) lfirst(lc);
1570 List *sublist = lfirst(lc2);
1572 sublist = lappend(sublist, col);
1573 lfirst(lc2) = sublist;
1575 list_free(colexprs[i]);
1579 * Ordinarily there can't be any current-level Vars in the expression
1580 * lists, because the namespace was empty ... but if we're inside CREATE
1581 * RULE, then NEW/OLD references might appear. In that case we have to
1582 * mark the VALUES RTE as LATERAL.
1584 if (pstate->p_rtable != NIL &&
1585 contain_vars_of_level((Node *) exprsLists, 0))
1586 lateral = true;
1589 * Generate the VALUES RTE
1591 nsitem = addRangeTableEntryForValues(pstate, exprsLists,
1592 coltypes, coltypmods, colcollations,
1593 NULL, lateral, true);
1594 addNSItemToQuery(pstate, nsitem, true, true, true);
1597 * Generate a targetlist as though expanding "*"
1599 Assert(pstate->p_next_resno == 1);
1600 qry->targetList = expandNSItemAttrs(pstate, nsitem, 0, true, -1);
1603 * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1604 * VALUES, so cope.
1606 qry->sortClause = transformSortClause(pstate,
1607 stmt->sortClause,
1608 &qry->targetList,
1609 EXPR_KIND_ORDER_BY,
1610 false /* allow SQL92 rules */ );
1612 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1613 EXPR_KIND_OFFSET, "OFFSET",
1614 stmt->limitOption);
1615 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1616 EXPR_KIND_LIMIT, "LIMIT",
1617 stmt->limitOption);
1618 qry->limitOption = stmt->limitOption;
1620 if (stmt->lockingClause)
1621 ereport(ERROR,
1622 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1623 /*------
1624 translator: %s is a SQL row locking clause such as FOR UPDATE */
1625 errmsg("%s cannot be applied to VALUES",
1626 LCS_asString(((LockingClause *)
1627 linitial(stmt->lockingClause))->strength))));
1629 qry->rtable = pstate->p_rtable;
1630 qry->rteperminfos = pstate->p_rteperminfos;
1631 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1633 qry->hasSubLinks = pstate->p_hasSubLinks;
1635 assign_query_collations(pstate, qry);
1637 return qry;
1641 * transformSetOperationStmt -
1642 * transforms a set-operations tree
1644 * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
1645 * structure to it. We must transform each leaf SELECT and build up a top-
1646 * level Query that contains the leaf SELECTs as subqueries in its rangetable.
1647 * The tree of set operations is converted into the setOperations field of
1648 * the top-level Query.
1650 static Query *
1651 transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
1653 Query *qry = makeNode(Query);
1654 SelectStmt *leftmostSelect;
1655 int leftmostRTI;
1656 Query *leftmostQuery;
1657 SetOperationStmt *sostmt;
1658 List *sortClause;
1659 Node *limitOffset;
1660 Node *limitCount;
1661 List *lockingClause;
1662 WithClause *withClause;
1663 Node *node;
1664 ListCell *left_tlist,
1665 *lct,
1666 *lcm,
1667 *lcc,
1669 List *targetvars,
1670 *targetnames,
1671 *sv_namespace;
1672 int sv_rtable_length;
1673 ParseNamespaceItem *jnsitem;
1674 ParseNamespaceColumn *sortnscolumns;
1675 int sortcolindex;
1676 int tllen;
1678 qry->commandType = CMD_SELECT;
1681 * Find leftmost leaf SelectStmt. We currently only need to do this in
1682 * order to deliver a suitable error message if there's an INTO clause
1683 * there, implying the set-op tree is in a context that doesn't allow
1684 * INTO. (transformSetOperationTree would throw error anyway, but it
1685 * seems worth the trouble to throw a different error for non-leftmost
1686 * INTO, so we produce that error in transformSetOperationTree.)
1688 leftmostSelect = stmt->larg;
1689 while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1690 leftmostSelect = leftmostSelect->larg;
1691 Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1692 leftmostSelect->larg == NULL);
1693 if (leftmostSelect->intoClause)
1694 ereport(ERROR,
1695 (errcode(ERRCODE_SYNTAX_ERROR),
1696 errmsg("SELECT ... INTO is not allowed here"),
1697 parser_errposition(pstate,
1698 exprLocation((Node *) leftmostSelect->intoClause))));
1701 * We need to extract ORDER BY and other top-level clauses here and not
1702 * let transformSetOperationTree() see them --- else it'll just recurse
1703 * right back here!
1705 sortClause = stmt->sortClause;
1706 limitOffset = stmt->limitOffset;
1707 limitCount = stmt->limitCount;
1708 lockingClause = stmt->lockingClause;
1709 withClause = stmt->withClause;
1711 stmt->sortClause = NIL;
1712 stmt->limitOffset = NULL;
1713 stmt->limitCount = NULL;
1714 stmt->lockingClause = NIL;
1715 stmt->withClause = NULL;
1717 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1718 if (lockingClause)
1719 ereport(ERROR,
1720 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1721 /*------
1722 translator: %s is a SQL row locking clause such as FOR UPDATE */
1723 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1724 LCS_asString(((LockingClause *)
1725 linitial(lockingClause))->strength))));
1727 /* Process the WITH clause independently of all else */
1728 if (withClause)
1730 qry->hasRecursive = withClause->recursive;
1731 qry->cteList = transformWithClause(pstate, withClause);
1732 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1736 * Recursively transform the components of the tree.
1738 sostmt = castNode(SetOperationStmt,
1739 transformSetOperationTree(pstate, stmt, true, NULL));
1740 Assert(sostmt);
1741 qry->setOperations = (Node *) sostmt;
1744 * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1746 node = sostmt->larg;
1747 while (node && IsA(node, SetOperationStmt))
1748 node = ((SetOperationStmt *) node)->larg;
1749 Assert(node && IsA(node, RangeTblRef));
1750 leftmostRTI = ((RangeTblRef *) node)->rtindex;
1751 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1752 Assert(leftmostQuery != NULL);
1755 * Generate dummy targetlist for outer query using column names of
1756 * leftmost select and common datatypes/collations of topmost set
1757 * operation. Also make lists of the dummy vars and their names for use
1758 * in parsing ORDER BY.
1760 * Note: we use leftmostRTI as the varno of the dummy variables. It
1761 * shouldn't matter too much which RT index they have, as long as they
1762 * have one that corresponds to a real RT entry; else funny things may
1763 * happen when the tree is mashed by rule rewriting.
1765 qry->targetList = NIL;
1766 targetvars = NIL;
1767 targetnames = NIL;
1768 sortnscolumns = (ParseNamespaceColumn *)
1769 palloc0(list_length(sostmt->colTypes) * sizeof(ParseNamespaceColumn));
1770 sortcolindex = 0;
1772 forfour(lct, sostmt->colTypes,
1773 lcm, sostmt->colTypmods,
1774 lcc, sostmt->colCollations,
1775 left_tlist, leftmostQuery->targetList)
1777 Oid colType = lfirst_oid(lct);
1778 int32 colTypmod = lfirst_int(lcm);
1779 Oid colCollation = lfirst_oid(lcc);
1780 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1781 char *colName;
1782 TargetEntry *tle;
1783 Var *var;
1785 Assert(!lefttle->resjunk);
1786 colName = pstrdup(lefttle->resname);
1787 var = makeVar(leftmostRTI,
1788 lefttle->resno,
1789 colType,
1790 colTypmod,
1791 colCollation,
1793 var->location = exprLocation((Node *) lefttle->expr);
1794 tle = makeTargetEntry((Expr *) var,
1795 (AttrNumber) pstate->p_next_resno++,
1796 colName,
1797 false);
1798 qry->targetList = lappend(qry->targetList, tle);
1799 targetvars = lappend(targetvars, var);
1800 targetnames = lappend(targetnames, makeString(colName));
1801 sortnscolumns[sortcolindex].p_varno = leftmostRTI;
1802 sortnscolumns[sortcolindex].p_varattno = lefttle->resno;
1803 sortnscolumns[sortcolindex].p_vartype = colType;
1804 sortnscolumns[sortcolindex].p_vartypmod = colTypmod;
1805 sortnscolumns[sortcolindex].p_varcollid = colCollation;
1806 sortnscolumns[sortcolindex].p_varnosyn = leftmostRTI;
1807 sortnscolumns[sortcolindex].p_varattnosyn = lefttle->resno;
1808 sortcolindex++;
1812 * As a first step towards supporting sort clauses that are expressions
1813 * using the output columns, generate a namespace entry that makes the
1814 * output columns visible. A Join RTE node is handy for this, since we
1815 * can easily control the Vars generated upon matches.
1817 * Note: we don't yet do anything useful with such cases, but at least
1818 * "ORDER BY upper(foo)" will draw the right error message rather than
1819 * "foo not found".
1821 sv_rtable_length = list_length(pstate->p_rtable);
1823 jnsitem = addRangeTableEntryForJoin(pstate,
1824 targetnames,
1825 sortnscolumns,
1826 JOIN_INNER,
1828 targetvars,
1829 NIL,
1830 NIL,
1831 NULL,
1832 NULL,
1833 false);
1835 sv_namespace = pstate->p_namespace;
1836 pstate->p_namespace = NIL;
1838 /* add jnsitem to column namespace only */
1839 addNSItemToQuery(pstate, jnsitem, false, false, true);
1842 * For now, we don't support resjunk sort clauses on the output of a
1843 * setOperation tree --- you can only use the SQL92-spec options of
1844 * selecting an output column by name or number. Enforce by checking that
1845 * transformSortClause doesn't add any items to tlist.
1847 tllen = list_length(qry->targetList);
1849 qry->sortClause = transformSortClause(pstate,
1850 sortClause,
1851 &qry->targetList,
1852 EXPR_KIND_ORDER_BY,
1853 false /* allow SQL92 rules */ );
1855 /* restore namespace, remove join RTE from rtable */
1856 pstate->p_namespace = sv_namespace;
1857 pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1859 if (tllen != list_length(qry->targetList))
1860 ereport(ERROR,
1861 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1862 errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
1863 errdetail("Only result column names can be used, not expressions or functions."),
1864 errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
1865 parser_errposition(pstate,
1866 exprLocation(list_nth(qry->targetList, tllen)))));
1868 qry->limitOffset = transformLimitClause(pstate, limitOffset,
1869 EXPR_KIND_OFFSET, "OFFSET",
1870 stmt->limitOption);
1871 qry->limitCount = transformLimitClause(pstate, limitCount,
1872 EXPR_KIND_LIMIT, "LIMIT",
1873 stmt->limitOption);
1874 qry->limitOption = stmt->limitOption;
1876 qry->rtable = pstate->p_rtable;
1877 qry->rteperminfos = pstate->p_rteperminfos;
1878 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1880 qry->hasSubLinks = pstate->p_hasSubLinks;
1881 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1882 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1883 qry->hasAggs = pstate->p_hasAggs;
1885 foreach(l, lockingClause)
1887 transformLockingClause(pstate, qry,
1888 (LockingClause *) lfirst(l), false);
1891 assign_query_collations(pstate, qry);
1893 /* this must be done after collations, for reliable comparison of exprs */
1894 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1895 parseCheckAggregates(pstate, qry);
1897 return qry;
1901 * Make a SortGroupClause node for a SetOperationStmt's groupClauses
1903 * If require_hash is true, the caller is indicating that they need hash
1904 * support or they will fail. So look extra hard for hash support.
1906 SortGroupClause *
1907 makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
1909 SortGroupClause *grpcl = makeNode(SortGroupClause);
1910 Oid sortop;
1911 Oid eqop;
1912 bool hashable;
1914 /* determine the eqop and optional sortop */
1915 get_sort_group_operators(rescoltype,
1916 false, true, false,
1917 &sortop, &eqop, NULL,
1918 &hashable);
1921 * The type cache doesn't believe that record is hashable (see
1922 * cache_record_field_properties()), but if the caller really needs hash
1923 * support, we can assume it does. Worst case, if any components of the
1924 * record don't support hashing, we will fail at execution.
1926 if (require_hash && (rescoltype == RECORDOID || rescoltype == RECORDARRAYOID))
1927 hashable = true;
1929 /* we don't have a tlist yet, so can't assign sortgrouprefs */
1930 grpcl->tleSortGroupRef = 0;
1931 grpcl->eqop = eqop;
1932 grpcl->sortop = sortop;
1933 grpcl->nulls_first = false; /* OK with or without sortop */
1934 grpcl->hashable = hashable;
1936 return grpcl;
1940 * transformSetOperationTree
1941 * Recursively transform leaves and internal nodes of a set-op tree
1943 * In addition to returning the transformed node, if targetlist isn't NULL
1944 * then we return a list of its non-resjunk TargetEntry nodes. For a leaf
1945 * set-op node these are the actual targetlist entries; otherwise they are
1946 * dummy entries created to carry the type, typmod, collation, and location
1947 * (for error messages) of each output column of the set-op node. This info
1948 * is needed only during the internal recursion of this function, so outside
1949 * callers pass NULL for targetlist. Note: the reason for passing the
1950 * actual targetlist entries of a leaf node is so that upper levels can
1951 * replace UNKNOWN Consts with properly-coerced constants.
1953 static Node *
1954 transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
1955 bool isTopLevel, List **targetlist)
1957 bool isLeaf;
1959 Assert(stmt && IsA(stmt, SelectStmt));
1961 /* Guard against stack overflow due to overly complex set-expressions */
1962 check_stack_depth();
1965 * Validity-check both leaf and internal SELECTs for disallowed ops.
1967 if (stmt->intoClause)
1968 ereport(ERROR,
1969 (errcode(ERRCODE_SYNTAX_ERROR),
1970 errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
1971 parser_errposition(pstate,
1972 exprLocation((Node *) stmt->intoClause))));
1974 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1975 if (stmt->lockingClause)
1976 ereport(ERROR,
1977 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1978 /*------
1979 translator: %s is a SQL row locking clause such as FOR UPDATE */
1980 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1981 LCS_asString(((LockingClause *)
1982 linitial(stmt->lockingClause))->strength))));
1985 * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
1986 * or WITH clauses attached, we need to treat it like a leaf node to
1987 * generate an independent sub-Query tree. Otherwise, it can be
1988 * represented by a SetOperationStmt node underneath the parent Query.
1990 if (stmt->op == SETOP_NONE)
1992 Assert(stmt->larg == NULL && stmt->rarg == NULL);
1993 isLeaf = true;
1995 else
1997 Assert(stmt->larg != NULL && stmt->rarg != NULL);
1998 if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
1999 stmt->lockingClause || stmt->withClause)
2000 isLeaf = true;
2001 else
2002 isLeaf = false;
2005 if (isLeaf)
2007 /* Process leaf SELECT */
2008 Query *selectQuery;
2009 char selectName[32];
2010 ParseNamespaceItem *nsitem;
2011 RangeTblRef *rtr;
2012 ListCell *tl;
2015 * Transform SelectStmt into a Query.
2017 * This works the same as SELECT transformation normally would, except
2018 * that we prevent resolving unknown-type outputs as TEXT. This does
2019 * not change the subquery's semantics since if the column type
2020 * matters semantically, it would have been resolved to something else
2021 * anyway. Doing this lets us resolve such outputs using
2022 * select_common_type(), below.
2024 * Note: previously transformed sub-queries don't affect the parsing
2025 * of this sub-query, because they are not in the toplevel pstate's
2026 * namespace list.
2028 selectQuery = parse_sub_analyze((Node *) stmt, pstate,
2029 NULL, false, false);
2032 * Check for bogus references to Vars on the current query level (but
2033 * upper-level references are okay). Normally this can't happen
2034 * because the namespace will be empty, but it could happen if we are
2035 * inside a rule.
2037 if (pstate->p_namespace)
2039 if (contain_vars_of_level((Node *) selectQuery, 1))
2040 ereport(ERROR,
2041 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2042 errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
2043 parser_errposition(pstate,
2044 locate_var_of_level((Node *) selectQuery, 1))));
2048 * Extract a list of the non-junk TLEs for upper-level processing.
2050 if (targetlist)
2052 *targetlist = NIL;
2053 foreach(tl, selectQuery->targetList)
2055 TargetEntry *tle = (TargetEntry *) lfirst(tl);
2057 if (!tle->resjunk)
2058 *targetlist = lappend(*targetlist, tle);
2063 * Make the leaf query be a subquery in the top-level rangetable.
2065 snprintf(selectName, sizeof(selectName), "*SELECT* %d",
2066 list_length(pstate->p_rtable) + 1);
2067 nsitem = addRangeTableEntryForSubquery(pstate,
2068 selectQuery,
2069 makeAlias(selectName, NIL),
2070 false,
2071 false);
2074 * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
2076 rtr = makeNode(RangeTblRef);
2077 rtr->rtindex = nsitem->p_rtindex;
2078 return (Node *) rtr;
2080 else
2082 /* Process an internal node (set operation node) */
2083 SetOperationStmt *op = makeNode(SetOperationStmt);
2084 List *ltargetlist;
2085 List *rtargetlist;
2086 ListCell *ltl;
2087 ListCell *rtl;
2088 const char *context;
2089 bool recursive = (pstate->p_parent_cte &&
2090 pstate->p_parent_cte->cterecursive);
2092 context = (stmt->op == SETOP_UNION ? "UNION" :
2093 (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
2094 "EXCEPT"));
2096 op->op = stmt->op;
2097 op->all = stmt->all;
2100 * Recursively transform the left child node.
2102 op->larg = transformSetOperationTree(pstate, stmt->larg,
2103 false,
2104 &ltargetlist);
2107 * If we are processing a recursive union query, now is the time to
2108 * examine the non-recursive term's output columns and mark the
2109 * containing CTE as having those result columns. We should do this
2110 * only at the topmost setop of the CTE, of course.
2112 if (isTopLevel && recursive)
2113 determineRecursiveColTypes(pstate, op->larg, ltargetlist);
2116 * Recursively transform the right child node.
2118 op->rarg = transformSetOperationTree(pstate, stmt->rarg,
2119 false,
2120 &rtargetlist);
2123 * Verify that the two children have the same number of non-junk
2124 * columns, and determine the types of the merged output columns.
2126 if (list_length(ltargetlist) != list_length(rtargetlist))
2127 ereport(ERROR,
2128 (errcode(ERRCODE_SYNTAX_ERROR),
2129 errmsg("each %s query must have the same number of columns",
2130 context),
2131 parser_errposition(pstate,
2132 exprLocation((Node *) rtargetlist))));
2134 if (targetlist)
2135 *targetlist = NIL;
2136 op->colTypes = NIL;
2137 op->colTypmods = NIL;
2138 op->colCollations = NIL;
2139 op->groupClauses = NIL;
2140 forboth(ltl, ltargetlist, rtl, rtargetlist)
2142 TargetEntry *ltle = (TargetEntry *) lfirst(ltl);
2143 TargetEntry *rtle = (TargetEntry *) lfirst(rtl);
2144 Node *lcolnode = (Node *) ltle->expr;
2145 Node *rcolnode = (Node *) rtle->expr;
2146 Oid lcoltype = exprType(lcolnode);
2147 Oid rcoltype = exprType(rcolnode);
2148 Node *bestexpr;
2149 int bestlocation;
2150 Oid rescoltype;
2151 int32 rescoltypmod;
2152 Oid rescolcoll;
2154 /* select common type, same as CASE et al */
2155 rescoltype = select_common_type(pstate,
2156 list_make2(lcolnode, rcolnode),
2157 context,
2158 &bestexpr);
2159 bestlocation = exprLocation(bestexpr);
2162 * Verify the coercions are actually possible. If not, we'd fail
2163 * later anyway, but we want to fail now while we have sufficient
2164 * context to produce an error cursor position.
2166 * For all non-UNKNOWN-type cases, we verify coercibility but we
2167 * don't modify the child's expression, for fear of changing the
2168 * child query's semantics.
2170 * If a child expression is an UNKNOWN-type Const or Param, we
2171 * want to replace it with the coerced expression. This can only
2172 * happen when the child is a leaf set-op node. It's safe to
2173 * replace the expression because if the child query's semantics
2174 * depended on the type of this output column, it'd have already
2175 * coerced the UNKNOWN to something else. We want to do this
2176 * because (a) we want to verify that a Const is valid for the
2177 * target type, or resolve the actual type of an UNKNOWN Param,
2178 * and (b) we want to avoid unnecessary discrepancies between the
2179 * output type of the child query and the resolved target type.
2180 * Such a discrepancy would disable optimization in the planner.
2182 * If it's some other UNKNOWN-type node, eg a Var, we do nothing
2183 * (knowing that coerce_to_common_type would fail). The planner
2184 * is sometimes able to fold an UNKNOWN Var to a constant before
2185 * it has to coerce the type, so failing now would just break
2186 * cases that might work.
2188 if (lcoltype != UNKNOWNOID)
2189 lcolnode = coerce_to_common_type(pstate, lcolnode,
2190 rescoltype, context);
2191 else if (IsA(lcolnode, Const) ||
2192 IsA(lcolnode, Param))
2194 lcolnode = coerce_to_common_type(pstate, lcolnode,
2195 rescoltype, context);
2196 ltle->expr = (Expr *) lcolnode;
2199 if (rcoltype != UNKNOWNOID)
2200 rcolnode = coerce_to_common_type(pstate, rcolnode,
2201 rescoltype, context);
2202 else if (IsA(rcolnode, Const) ||
2203 IsA(rcolnode, Param))
2205 rcolnode = coerce_to_common_type(pstate, rcolnode,
2206 rescoltype, context);
2207 rtle->expr = (Expr *) rcolnode;
2210 rescoltypmod = select_common_typmod(pstate,
2211 list_make2(lcolnode, rcolnode),
2212 rescoltype);
2215 * Select common collation. A common collation is required for
2216 * all set operators except UNION ALL; see SQL:2008 7.13 <query
2217 * expression> Syntax Rule 15c. (If we fail to identify a common
2218 * collation for a UNION ALL column, the colCollations element
2219 * will be set to InvalidOid, which may result in a runtime error
2220 * if something at a higher query level wants to use the column's
2221 * collation.)
2223 rescolcoll = select_common_collation(pstate,
2224 list_make2(lcolnode, rcolnode),
2225 (op->op == SETOP_UNION && op->all));
2227 /* emit results */
2228 op->colTypes = lappend_oid(op->colTypes, rescoltype);
2229 op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
2230 op->colCollations = lappend_oid(op->colCollations, rescolcoll);
2233 * For all cases except UNION ALL, identify the grouping operators
2234 * (and, if available, sorting operators) that will be used to
2235 * eliminate duplicates.
2237 if (op->op != SETOP_UNION || !op->all)
2239 ParseCallbackState pcbstate;
2241 setup_parser_errposition_callback(&pcbstate, pstate,
2242 bestlocation);
2245 * If it's a recursive union, we need to require hashing
2246 * support.
2248 op->groupClauses = lappend(op->groupClauses,
2249 makeSortGroupClauseForSetOp(rescoltype, recursive));
2251 cancel_parser_errposition_callback(&pcbstate);
2255 * Construct a dummy tlist entry to return. We use a SetToDefault
2256 * node for the expression, since it carries exactly the fields
2257 * needed, but any other expression node type would do as well.
2259 if (targetlist)
2261 SetToDefault *rescolnode = makeNode(SetToDefault);
2262 TargetEntry *restle;
2264 rescolnode->typeId = rescoltype;
2265 rescolnode->typeMod = rescoltypmod;
2266 rescolnode->collation = rescolcoll;
2267 rescolnode->location = bestlocation;
2268 restle = makeTargetEntry((Expr *) rescolnode,
2269 0, /* no need to set resno */
2270 NULL,
2271 false);
2272 *targetlist = lappend(*targetlist, restle);
2276 return (Node *) op;
2281 * Process the outputs of the non-recursive term of a recursive union
2282 * to set up the parent CTE's columns
2284 static void
2285 determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
2287 Node *node;
2288 int leftmostRTI;
2289 Query *leftmostQuery;
2290 List *targetList;
2291 ListCell *left_tlist;
2292 ListCell *nrtl;
2293 int next_resno;
2296 * Find leftmost leaf SELECT
2298 node = larg;
2299 while (node && IsA(node, SetOperationStmt))
2300 node = ((SetOperationStmt *) node)->larg;
2301 Assert(node && IsA(node, RangeTblRef));
2302 leftmostRTI = ((RangeTblRef *) node)->rtindex;
2303 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2304 Assert(leftmostQuery != NULL);
2307 * Generate dummy targetlist using column names of leftmost select and
2308 * dummy result expressions of the non-recursive term.
2310 targetList = NIL;
2311 next_resno = 1;
2313 forboth(nrtl, nrtargetlist, left_tlist, leftmostQuery->targetList)
2315 TargetEntry *nrtle = (TargetEntry *) lfirst(nrtl);
2316 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
2317 char *colName;
2318 TargetEntry *tle;
2320 Assert(!lefttle->resjunk);
2321 colName = pstrdup(lefttle->resname);
2322 tle = makeTargetEntry(nrtle->expr,
2323 next_resno++,
2324 colName,
2325 false);
2326 targetList = lappend(targetList, tle);
2329 /* Now build CTE's output column info using dummy targetlist */
2330 analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
2335 * transformReturnStmt -
2336 * transforms a return statement
2338 static Query *
2339 transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
2341 Query *qry = makeNode(Query);
2343 qry->commandType = CMD_SELECT;
2344 qry->isReturn = true;
2346 qry->targetList = list_make1(makeTargetEntry((Expr *) transformExpr(pstate, stmt->returnval, EXPR_KIND_SELECT_TARGET),
2347 1, NULL, false));
2349 if (pstate->p_resolve_unknowns)
2350 resolveTargetListUnknowns(pstate, qry->targetList);
2351 qry->rtable = pstate->p_rtable;
2352 qry->rteperminfos = pstate->p_rteperminfos;
2353 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2354 qry->hasSubLinks = pstate->p_hasSubLinks;
2355 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2356 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2357 qry->hasAggs = pstate->p_hasAggs;
2359 assign_query_collations(pstate, qry);
2361 return qry;
2366 * transformUpdateStmt -
2367 * transforms an update statement
2369 static Query *
2370 transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
2372 Query *qry = makeNode(Query);
2373 ParseNamespaceItem *nsitem;
2374 Node *qual;
2376 qry->commandType = CMD_UPDATE;
2377 pstate->p_is_insert = false;
2379 /* process the WITH clause independently of all else */
2380 if (stmt->withClause)
2382 qry->hasRecursive = stmt->withClause->recursive;
2383 qry->cteList = transformWithClause(pstate, stmt->withClause);
2384 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2387 qry->resultRelation = setTargetTable(pstate, stmt->relation,
2388 stmt->relation->inh,
2389 true,
2390 ACL_UPDATE);
2391 nsitem = pstate->p_target_nsitem;
2393 /* subqueries in FROM cannot access the result relation */
2394 nsitem->p_lateral_only = true;
2395 nsitem->p_lateral_ok = false;
2398 * the FROM clause is non-standard SQL syntax. We used to be able to do
2399 * this with REPLACE in POSTQUEL so we keep the feature.
2401 transformFromClause(pstate, stmt->fromClause);
2403 /* remaining clauses can reference the result relation normally */
2404 nsitem->p_lateral_only = false;
2405 nsitem->p_lateral_ok = true;
2407 qual = transformWhereClause(pstate, stmt->whereClause,
2408 EXPR_KIND_WHERE, "WHERE");
2410 qry->returningList = transformReturningList(pstate, stmt->returningList);
2413 * Now we are done with SELECT-like processing, and can get on with
2414 * transforming the target list to match the UPDATE target columns.
2416 qry->targetList = transformUpdateTargetList(pstate, stmt->targetList);
2418 qry->rtable = pstate->p_rtable;
2419 qry->rteperminfos = pstate->p_rteperminfos;
2420 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
2422 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2423 qry->hasSubLinks = pstate->p_hasSubLinks;
2425 assign_query_collations(pstate, qry);
2427 return qry;
2431 * transformUpdateTargetList -
2432 * handle SET clause in UPDATE/MERGE/INSERT ... ON CONFLICT UPDATE
2434 List *
2435 transformUpdateTargetList(ParseState *pstate, List *origTlist)
2437 List *tlist = NIL;
2438 RTEPermissionInfo *target_perminfo;
2439 ListCell *orig_tl;
2440 ListCell *tl;
2442 tlist = transformTargetList(pstate, origTlist,
2443 EXPR_KIND_UPDATE_SOURCE);
2445 /* Prepare to assign non-conflicting resnos to resjunk attributes */
2446 if (pstate->p_next_resno <= RelationGetNumberOfAttributes(pstate->p_target_relation))
2447 pstate->p_next_resno = RelationGetNumberOfAttributes(pstate->p_target_relation) + 1;
2449 /* Prepare non-junk columns for assignment to target table */
2450 target_perminfo = pstate->p_target_nsitem->p_perminfo;
2451 orig_tl = list_head(origTlist);
2453 foreach(tl, tlist)
2455 TargetEntry *tle = (TargetEntry *) lfirst(tl);
2456 ResTarget *origTarget;
2457 int attrno;
2459 if (tle->resjunk)
2462 * Resjunk nodes need no additional processing, but be sure they
2463 * have resnos that do not match any target columns; else rewriter
2464 * or planner might get confused. They don't need a resname
2465 * either.
2467 tle->resno = (AttrNumber) pstate->p_next_resno++;
2468 tle->resname = NULL;
2469 continue;
2471 if (orig_tl == NULL)
2472 elog(ERROR, "UPDATE target count mismatch --- internal error");
2473 origTarget = lfirst_node(ResTarget, orig_tl);
2475 attrno = attnameAttNum(pstate->p_target_relation,
2476 origTarget->name, true);
2477 if (attrno == InvalidAttrNumber)
2478 ereport(ERROR,
2479 (errcode(ERRCODE_UNDEFINED_COLUMN),
2480 errmsg("column \"%s\" of relation \"%s\" does not exist",
2481 origTarget->name,
2482 RelationGetRelationName(pstate->p_target_relation)),
2483 parser_errposition(pstate, origTarget->location)));
2485 updateTargetListEntry(pstate, tle, origTarget->name,
2486 attrno,
2487 origTarget->indirection,
2488 origTarget->location);
2490 /* Mark the target column as requiring update permissions */
2491 target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
2492 attrno - FirstLowInvalidHeapAttributeNumber);
2494 orig_tl = lnext(origTlist, orig_tl);
2496 if (orig_tl != NULL)
2497 elog(ERROR, "UPDATE target count mismatch --- internal error");
2499 return tlist;
2503 * transformReturningList -
2504 * handle a RETURNING clause in INSERT/UPDATE/DELETE
2506 static List *
2507 transformReturningList(ParseState *pstate, List *returningList)
2509 List *rlist;
2510 int save_next_resno;
2512 if (returningList == NIL)
2513 return NIL; /* nothing to do */
2516 * We need to assign resnos starting at one in the RETURNING list. Save
2517 * and restore the main tlist's value of p_next_resno, just in case
2518 * someone looks at it later (probably won't happen).
2520 save_next_resno = pstate->p_next_resno;
2521 pstate->p_next_resno = 1;
2523 /* transform RETURNING identically to a SELECT targetlist */
2524 rlist = transformTargetList(pstate, returningList, EXPR_KIND_RETURNING);
2527 * Complain if the nonempty tlist expanded to nothing (which is possible
2528 * if it contains only a star-expansion of a zero-column table). If we
2529 * allow this, the parsed Query will look like it didn't have RETURNING,
2530 * with results that would probably surprise the user.
2532 if (rlist == NIL)
2533 ereport(ERROR,
2534 (errcode(ERRCODE_SYNTAX_ERROR),
2535 errmsg("RETURNING must have at least one column"),
2536 parser_errposition(pstate,
2537 exprLocation(linitial(returningList)))));
2539 /* mark column origins */
2540 markTargetListOrigins(pstate, rlist);
2542 /* resolve any still-unresolved output columns as being type text */
2543 if (pstate->p_resolve_unknowns)
2544 resolveTargetListUnknowns(pstate, rlist);
2546 /* restore state */
2547 pstate->p_next_resno = save_next_resno;
2549 return rlist;
2554 * transformPLAssignStmt -
2555 * transform a PL/pgSQL assignment statement
2557 * If there is no opt_indirection, the transformed statement looks like
2558 * "SELECT a_expr ...", except the expression has been cast to the type of
2559 * the target. With indirection, it's still a SELECT, but the expression will
2560 * incorporate FieldStore and/or assignment SubscriptingRef nodes to compute a
2561 * new value for a container-type variable represented by the target. The
2562 * expression references the target as the container source.
2564 static Query *
2565 transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
2567 Query *qry = makeNode(Query);
2568 ColumnRef *cref = makeNode(ColumnRef);
2569 List *indirection = stmt->indirection;
2570 int nnames = stmt->nnames;
2571 SelectStmt *sstmt = stmt->val;
2572 Node *target;
2573 Oid targettype;
2574 int32 targettypmod;
2575 Oid targetcollation;
2576 List *tlist;
2577 TargetEntry *tle;
2578 Oid type_id;
2579 Node *qual;
2580 ListCell *l;
2583 * First, construct a ColumnRef for the target variable. If the target
2584 * has more than one dotted name, we have to pull the extra names out of
2585 * the indirection list.
2587 cref->fields = list_make1(makeString(stmt->name));
2588 cref->location = stmt->location;
2589 if (nnames > 1)
2591 /* avoid munging the raw parsetree */
2592 indirection = list_copy(indirection);
2593 while (--nnames > 0 && indirection != NIL)
2595 Node *ind = (Node *) linitial(indirection);
2597 if (!IsA(ind, String))
2598 elog(ERROR, "invalid name count in PLAssignStmt");
2599 cref->fields = lappend(cref->fields, ind);
2600 indirection = list_delete_first(indirection);
2605 * Transform the target reference. Typically we will get back a Param
2606 * node, but there's no reason to be too picky about its type.
2608 target = transformExpr(pstate, (Node *) cref,
2609 EXPR_KIND_UPDATE_TARGET);
2610 targettype = exprType(target);
2611 targettypmod = exprTypmod(target);
2612 targetcollation = exprCollation(target);
2615 * The rest mostly matches transformSelectStmt, except that we needn't
2616 * consider WITH or INTO, and we build a targetlist our own way.
2618 qry->commandType = CMD_SELECT;
2619 pstate->p_is_insert = false;
2621 /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
2622 pstate->p_locking_clause = sstmt->lockingClause;
2624 /* make WINDOW info available for window functions, too */
2625 pstate->p_windowdefs = sstmt->windowClause;
2627 /* process the FROM clause */
2628 transformFromClause(pstate, sstmt->fromClause);
2630 /* initially transform the targetlist as if in SELECT */
2631 tlist = transformTargetList(pstate, sstmt->targetList,
2632 EXPR_KIND_SELECT_TARGET);
2634 /* we should have exactly one targetlist item */
2635 if (list_length(tlist) != 1)
2636 ereport(ERROR,
2637 (errcode(ERRCODE_SYNTAX_ERROR),
2638 errmsg_plural("assignment source returned %d column",
2639 "assignment source returned %d columns",
2640 list_length(tlist),
2641 list_length(tlist))));
2643 tle = linitial_node(TargetEntry, tlist);
2646 * This next bit is similar to transformAssignedExpr; the key difference
2647 * is we use COERCION_PLPGSQL not COERCION_ASSIGNMENT.
2649 type_id = exprType((Node *) tle->expr);
2651 pstate->p_expr_kind = EXPR_KIND_UPDATE_TARGET;
2653 if (indirection)
2655 tle->expr = (Expr *)
2656 transformAssignmentIndirection(pstate,
2657 target,
2658 stmt->name,
2659 false,
2660 targettype,
2661 targettypmod,
2662 targetcollation,
2663 indirection,
2664 list_head(indirection),
2665 (Node *) tle->expr,
2666 COERCION_PLPGSQL,
2667 exprLocation(target));
2669 else if (targettype != type_id &&
2670 (targettype == RECORDOID || ISCOMPLEX(targettype)) &&
2671 (type_id == RECORDOID || ISCOMPLEX(type_id)))
2674 * Hack: do not let coerce_to_target_type() deal with inconsistent
2675 * composite types. Just pass the expression result through as-is,
2676 * and let the PL/pgSQL executor do the conversion its way. This is
2677 * rather bogus, but it's needed for backwards compatibility.
2680 else
2683 * For normal non-qualified target column, do type checking and
2684 * coercion.
2686 Node *orig_expr = (Node *) tle->expr;
2688 tle->expr = (Expr *)
2689 coerce_to_target_type(pstate,
2690 orig_expr, type_id,
2691 targettype, targettypmod,
2692 COERCION_PLPGSQL,
2693 COERCE_IMPLICIT_CAST,
2694 -1);
2695 /* With COERCION_PLPGSQL, this error is probably unreachable */
2696 if (tle->expr == NULL)
2697 ereport(ERROR,
2698 (errcode(ERRCODE_DATATYPE_MISMATCH),
2699 errmsg("variable \"%s\" is of type %s"
2700 " but expression is of type %s",
2701 stmt->name,
2702 format_type_be(targettype),
2703 format_type_be(type_id)),
2704 errhint("You will need to rewrite or cast the expression."),
2705 parser_errposition(pstate, exprLocation(orig_expr))));
2708 pstate->p_expr_kind = EXPR_KIND_NONE;
2710 qry->targetList = list_make1(tle);
2712 /* transform WHERE */
2713 qual = transformWhereClause(pstate, sstmt->whereClause,
2714 EXPR_KIND_WHERE, "WHERE");
2716 /* initial processing of HAVING clause is much like WHERE clause */
2717 qry->havingQual = transformWhereClause(pstate, sstmt->havingClause,
2718 EXPR_KIND_HAVING, "HAVING");
2721 * Transform sorting/grouping stuff. Do ORDER BY first because both
2722 * transformGroupClause and transformDistinctClause need the results. Note
2723 * that these functions can also change the targetList, so it's passed to
2724 * them by reference.
2726 qry->sortClause = transformSortClause(pstate,
2727 sstmt->sortClause,
2728 &qry->targetList,
2729 EXPR_KIND_ORDER_BY,
2730 false /* allow SQL92 rules */ );
2732 qry->groupClause = transformGroupClause(pstate,
2733 sstmt->groupClause,
2734 &qry->groupingSets,
2735 &qry->targetList,
2736 qry->sortClause,
2737 EXPR_KIND_GROUP_BY,
2738 false /* allow SQL92 rules */ );
2740 if (sstmt->distinctClause == NIL)
2742 qry->distinctClause = NIL;
2743 qry->hasDistinctOn = false;
2745 else if (linitial(sstmt->distinctClause) == NULL)
2747 /* We had SELECT DISTINCT */
2748 qry->distinctClause = transformDistinctClause(pstate,
2749 &qry->targetList,
2750 qry->sortClause,
2751 false);
2752 qry->hasDistinctOn = false;
2754 else
2756 /* We had SELECT DISTINCT ON */
2757 qry->distinctClause = transformDistinctOnClause(pstate,
2758 sstmt->distinctClause,
2759 &qry->targetList,
2760 qry->sortClause);
2761 qry->hasDistinctOn = true;
2764 /* transform LIMIT */
2765 qry->limitOffset = transformLimitClause(pstate, sstmt->limitOffset,
2766 EXPR_KIND_OFFSET, "OFFSET",
2767 sstmt->limitOption);
2768 qry->limitCount = transformLimitClause(pstate, sstmt->limitCount,
2769 EXPR_KIND_LIMIT, "LIMIT",
2770 sstmt->limitOption);
2771 qry->limitOption = sstmt->limitOption;
2773 /* transform window clauses after we have seen all window functions */
2774 qry->windowClause = transformWindowDefinitions(pstate,
2775 pstate->p_windowdefs,
2776 &qry->targetList);
2778 qry->rtable = pstate->p_rtable;
2779 qry->rteperminfos = pstate->p_rteperminfos;
2780 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
2782 qry->hasSubLinks = pstate->p_hasSubLinks;
2783 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2784 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2785 qry->hasAggs = pstate->p_hasAggs;
2787 foreach(l, sstmt->lockingClause)
2789 transformLockingClause(pstate, qry,
2790 (LockingClause *) lfirst(l), false);
2793 assign_query_collations(pstate, qry);
2795 /* this must be done after collations, for reliable comparison of exprs */
2796 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
2797 parseCheckAggregates(pstate, qry);
2799 return qry;
2804 * transformDeclareCursorStmt -
2805 * transform a DECLARE CURSOR Statement
2807 * DECLARE CURSOR is like other utility statements in that we emit it as a
2808 * CMD_UTILITY Query node; however, we must first transform the contained
2809 * query. We used to postpone that until execution, but it's really necessary
2810 * to do it during the normal parse analysis phase to ensure that side effects
2811 * of parser hooks happen at the expected time.
2813 static Query *
2814 transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
2816 Query *result;
2817 Query *query;
2819 if ((stmt->options & CURSOR_OPT_SCROLL) &&
2820 (stmt->options & CURSOR_OPT_NO_SCROLL))
2821 ereport(ERROR,
2822 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
2823 /* translator: %s is a SQL keyword */
2824 errmsg("cannot specify both %s and %s",
2825 "SCROLL", "NO SCROLL")));
2827 if ((stmt->options & CURSOR_OPT_ASENSITIVE) &&
2828 (stmt->options & CURSOR_OPT_INSENSITIVE))
2829 ereport(ERROR,
2830 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
2831 /* translator: %s is a SQL keyword */
2832 errmsg("cannot specify both %s and %s",
2833 "ASENSITIVE", "INSENSITIVE")));
2835 /* Transform contained query, not allowing SELECT INTO */
2836 query = transformStmt(pstate, stmt->query);
2837 stmt->query = (Node *) query;
2839 /* Grammar should not have allowed anything but SELECT */
2840 if (!IsA(query, Query) ||
2841 query->commandType != CMD_SELECT)
2842 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
2845 * We also disallow data-modifying WITH in a cursor. (This could be
2846 * allowed, but the semantics of when the updates occur might be
2847 * surprising.)
2849 if (query->hasModifyingCTE)
2850 ereport(ERROR,
2851 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2852 errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
2854 /* FOR UPDATE and WITH HOLD are not compatible */
2855 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
2856 ereport(ERROR,
2857 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2858 /*------
2859 translator: %s is a SQL row locking clause such as FOR UPDATE */
2860 errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
2861 LCS_asString(((RowMarkClause *)
2862 linitial(query->rowMarks))->strength)),
2863 errdetail("Holdable cursors must be READ ONLY.")));
2865 /* FOR UPDATE and SCROLL are not compatible */
2866 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
2867 ereport(ERROR,
2868 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2869 /*------
2870 translator: %s is a SQL row locking clause such as FOR UPDATE */
2871 errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
2872 LCS_asString(((RowMarkClause *)
2873 linitial(query->rowMarks))->strength)),
2874 errdetail("Scrollable cursors must be READ ONLY.")));
2876 /* FOR UPDATE and INSENSITIVE are not compatible */
2877 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
2878 ereport(ERROR,
2879 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
2880 /*------
2881 translator: %s is a SQL row locking clause such as FOR UPDATE */
2882 errmsg("DECLARE INSENSITIVE CURSOR ... %s is not valid",
2883 LCS_asString(((RowMarkClause *)
2884 linitial(query->rowMarks))->strength)),
2885 errdetail("Insensitive cursors must be READ ONLY.")));
2887 /* represent the command as a utility Query */
2888 result = makeNode(Query);
2889 result->commandType = CMD_UTILITY;
2890 result->utilityStmt = (Node *) stmt;
2892 return result;
2897 * transformExplainStmt -
2898 * transform an EXPLAIN Statement
2900 * EXPLAIN is like other utility statements in that we emit it as a
2901 * CMD_UTILITY Query node; however, we must first transform the contained
2902 * query. We used to postpone that until execution, but it's really necessary
2903 * to do it during the normal parse analysis phase to ensure that side effects
2904 * of parser hooks happen at the expected time.
2906 static Query *
2907 transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
2909 Query *result;
2910 bool generic_plan = false;
2911 Oid *paramTypes = NULL;
2912 int numParams = 0;
2915 * If we have no external source of parameter definitions, and the
2916 * GENERIC_PLAN option is specified, then accept variable parameter
2917 * definitions (similarly to PREPARE, for example).
2919 if (pstate->p_paramref_hook == NULL)
2921 ListCell *lc;
2923 foreach(lc, stmt->options)
2925 DefElem *opt = (DefElem *) lfirst(lc);
2927 if (strcmp(opt->defname, "generic_plan") == 0)
2928 generic_plan = defGetBoolean(opt);
2929 /* don't "break", as we want the last value */
2931 if (generic_plan)
2932 setup_parse_variable_parameters(pstate, &paramTypes, &numParams);
2935 /* transform contained query, allowing SELECT INTO */
2936 stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
2938 /* make sure all is well with parameter types */
2939 if (generic_plan)
2940 check_variable_parameters(pstate, (Query *) stmt->query);
2942 /* represent the command as a utility Query */
2943 result = makeNode(Query);
2944 result->commandType = CMD_UTILITY;
2945 result->utilityStmt = (Node *) stmt;
2947 return result;
2952 * transformCreateTableAsStmt -
2953 * transform a CREATE TABLE AS, SELECT ... INTO, or CREATE MATERIALIZED VIEW
2954 * Statement
2956 * As with DECLARE CURSOR and EXPLAIN, transform the contained statement now.
2958 static Query *
2959 transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
2961 Query *result;
2962 Query *query;
2964 /* transform contained query, not allowing SELECT INTO */
2965 query = transformStmt(pstate, stmt->query);
2966 stmt->query = (Node *) query;
2968 /* additional work needed for CREATE MATERIALIZED VIEW */
2969 if (stmt->objtype == OBJECT_MATVIEW)
2972 * Prohibit a data-modifying CTE in the query used to create a
2973 * materialized view. It's not sufficiently clear what the user would
2974 * want to happen if the MV is refreshed or incrementally maintained.
2976 if (query->hasModifyingCTE)
2977 ereport(ERROR,
2978 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2979 errmsg("materialized views must not use data-modifying statements in WITH")));
2982 * Check whether any temporary database objects are used in the
2983 * creation query. It would be hard to refresh data or incrementally
2984 * maintain it if a source disappeared.
2986 if (isQueryUsingTempRelation(query))
2987 ereport(ERROR,
2988 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2989 errmsg("materialized views must not use temporary tables or views")));
2992 * A materialized view would either need to save parameters for use in
2993 * maintaining/loading the data or prohibit them entirely. The latter
2994 * seems safer and more sane.
2996 if (query_contains_extern_params(query))
2997 ereport(ERROR,
2998 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2999 errmsg("materialized views may not be defined using bound parameters")));
3002 * For now, we disallow unlogged materialized views, because it seems
3003 * like a bad idea for them to just go to empty after a crash. (If we
3004 * could mark them as unpopulated, that would be better, but that
3005 * requires catalog changes which crash recovery can't presently
3006 * handle.)
3008 if (stmt->into->rel->relpersistence == RELPERSISTENCE_UNLOGGED)
3009 ereport(ERROR,
3010 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3011 errmsg("materialized views cannot be unlogged")));
3014 * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
3015 * for purposes of creating the view's ON SELECT rule. We stash that
3016 * in the IntoClause because that's where intorel_startup() can
3017 * conveniently get it from.
3019 stmt->into->viewQuery = (Node *) copyObject(query);
3022 /* represent the command as a utility Query */
3023 result = makeNode(Query);
3024 result->commandType = CMD_UTILITY;
3025 result->utilityStmt = (Node *) stmt;
3027 return result;
3031 * transform a CallStmt
3033 static Query *
3034 transformCallStmt(ParseState *pstate, CallStmt *stmt)
3036 List *targs;
3037 ListCell *lc;
3038 Node *node;
3039 FuncExpr *fexpr;
3040 HeapTuple proctup;
3041 Datum proargmodes;
3042 bool isNull;
3043 List *outargs = NIL;
3044 Query *result;
3047 * First, do standard parse analysis on the procedure call and its
3048 * arguments, allowing us to identify the called procedure.
3050 targs = NIL;
3051 foreach(lc, stmt->funccall->args)
3053 targs = lappend(targs, transformExpr(pstate,
3054 (Node *) lfirst(lc),
3055 EXPR_KIND_CALL_ARGUMENT));
3058 node = ParseFuncOrColumn(pstate,
3059 stmt->funccall->funcname,
3060 targs,
3061 pstate->p_last_srf,
3062 stmt->funccall,
3063 true,
3064 stmt->funccall->location);
3066 assign_expr_collations(pstate, node);
3068 fexpr = castNode(FuncExpr, node);
3070 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fexpr->funcid));
3071 if (!HeapTupleIsValid(proctup))
3072 elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
3075 * Expand the argument list to deal with named-argument notation and
3076 * default arguments. For ordinary FuncExprs this'd be done during
3077 * planning, but a CallStmt doesn't go through planning, and there seems
3078 * no good reason not to do it here.
3080 fexpr->args = expand_function_arguments(fexpr->args,
3081 true,
3082 fexpr->funcresulttype,
3083 proctup);
3085 /* Fetch proargmodes; if it's null, there are no output args */
3086 proargmodes = SysCacheGetAttr(PROCOID, proctup,
3087 Anum_pg_proc_proargmodes,
3088 &isNull);
3089 if (!isNull)
3092 * Split the list into input arguments in fexpr->args and output
3093 * arguments in stmt->outargs. INOUT arguments appear in both lists.
3095 ArrayType *arr;
3096 int numargs;
3097 char *argmodes;
3098 List *inargs;
3099 int i;
3101 arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
3102 numargs = list_length(fexpr->args);
3103 if (ARR_NDIM(arr) != 1 ||
3104 ARR_DIMS(arr)[0] != numargs ||
3105 ARR_HASNULL(arr) ||
3106 ARR_ELEMTYPE(arr) != CHAROID)
3107 elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
3108 numargs);
3109 argmodes = (char *) ARR_DATA_PTR(arr);
3111 inargs = NIL;
3112 i = 0;
3113 foreach(lc, fexpr->args)
3115 Node *n = lfirst(lc);
3117 switch (argmodes[i])
3119 case PROARGMODE_IN:
3120 case PROARGMODE_VARIADIC:
3121 inargs = lappend(inargs, n);
3122 break;
3123 case PROARGMODE_OUT:
3124 outargs = lappend(outargs, n);
3125 break;
3126 case PROARGMODE_INOUT:
3127 inargs = lappend(inargs, n);
3128 outargs = lappend(outargs, copyObject(n));
3129 break;
3130 default:
3131 /* note we don't support PROARGMODE_TABLE */
3132 elog(ERROR, "invalid argmode %c for procedure",
3133 argmodes[i]);
3134 break;
3136 i++;
3138 fexpr->args = inargs;
3141 stmt->funcexpr = fexpr;
3142 stmt->outargs = outargs;
3144 ReleaseSysCache(proctup);
3146 /* represent the command as a utility Query */
3147 result = makeNode(Query);
3148 result->commandType = CMD_UTILITY;
3149 result->utilityStmt = (Node *) stmt;
3151 return result;
3155 * Produce a string representation of a LockClauseStrength value.
3156 * This should only be applied to valid values (not LCS_NONE).
3158 const char *
3159 LCS_asString(LockClauseStrength strength)
3161 switch (strength)
3163 case LCS_NONE:
3164 Assert(false);
3165 break;
3166 case LCS_FORKEYSHARE:
3167 return "FOR KEY SHARE";
3168 case LCS_FORSHARE:
3169 return "FOR SHARE";
3170 case LCS_FORNOKEYUPDATE:
3171 return "FOR NO KEY UPDATE";
3172 case LCS_FORUPDATE:
3173 return "FOR UPDATE";
3175 return "FOR some"; /* shouldn't happen */
3179 * Check for features that are not supported with FOR [KEY] UPDATE/SHARE.
3181 * exported so planner can check again after rewriting, query pullup, etc
3183 void
3184 CheckSelectLocking(Query *qry, LockClauseStrength strength)
3186 Assert(strength != LCS_NONE); /* else caller error */
3188 if (qry->setOperations)
3189 ereport(ERROR,
3190 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3191 /*------
3192 translator: %s is a SQL row locking clause such as FOR UPDATE */
3193 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3194 LCS_asString(strength))));
3195 if (qry->distinctClause != NIL)
3196 ereport(ERROR,
3197 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3198 /*------
3199 translator: %s is a SQL row locking clause such as FOR UPDATE */
3200 errmsg("%s is not allowed with DISTINCT clause",
3201 LCS_asString(strength))));
3202 if (qry->groupClause != NIL || qry->groupingSets != NIL)
3203 ereport(ERROR,
3204 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3205 /*------
3206 translator: %s is a SQL row locking clause such as FOR UPDATE */
3207 errmsg("%s is not allowed with GROUP BY clause",
3208 LCS_asString(strength))));
3209 if (qry->havingQual != NULL)
3210 ereport(ERROR,
3211 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3212 /*------
3213 translator: %s is a SQL row locking clause such as FOR UPDATE */
3214 errmsg("%s is not allowed with HAVING clause",
3215 LCS_asString(strength))));
3216 if (qry->hasAggs)
3217 ereport(ERROR,
3218 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3219 /*------
3220 translator: %s is a SQL row locking clause such as FOR UPDATE */
3221 errmsg("%s is not allowed with aggregate functions",
3222 LCS_asString(strength))));
3223 if (qry->hasWindowFuncs)
3224 ereport(ERROR,
3225 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3226 /*------
3227 translator: %s is a SQL row locking clause such as FOR UPDATE */
3228 errmsg("%s is not allowed with window functions",
3229 LCS_asString(strength))));
3230 if (qry->hasTargetSRFs)
3231 ereport(ERROR,
3232 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3233 /*------
3234 translator: %s is a SQL row locking clause such as FOR UPDATE */
3235 errmsg("%s is not allowed with set-returning functions in the target list",
3236 LCS_asString(strength))));
3240 * Transform a FOR [KEY] UPDATE/SHARE clause
3242 * This basically involves replacing names by integer relids.
3244 * NB: if you need to change this, see also markQueryForLocking()
3245 * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
3247 static void
3248 transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
3249 bool pushedDown)
3251 List *lockedRels = lc->lockedRels;
3252 ListCell *l;
3253 ListCell *rt;
3254 Index i;
3255 LockingClause *allrels;
3257 CheckSelectLocking(qry, lc->strength);
3259 /* make a clause we can pass down to subqueries to select all rels */
3260 allrels = makeNode(LockingClause);
3261 allrels->lockedRels = NIL; /* indicates all rels */
3262 allrels->strength = lc->strength;
3263 allrels->waitPolicy = lc->waitPolicy;
3265 if (lockedRels == NIL)
3268 * Lock all regular tables used in query and its subqueries. We
3269 * examine inFromCl to exclude auto-added RTEs, particularly NEW/OLD
3270 * in rules. This is a bit of an abuse of a mostly-obsolete flag, but
3271 * it's convenient. We can't rely on the namespace mechanism that has
3272 * largely replaced inFromCl, since for example we need to lock
3273 * base-relation RTEs even if they are masked by upper joins.
3275 i = 0;
3276 foreach(rt, qry->rtable)
3278 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3280 ++i;
3281 if (!rte->inFromCl)
3282 continue;
3283 switch (rte->rtekind)
3285 case RTE_RELATION:
3287 RTEPermissionInfo *perminfo;
3289 applyLockingClause(qry, i,
3290 lc->strength,
3291 lc->waitPolicy,
3292 pushedDown);
3293 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3294 perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3296 break;
3297 case RTE_SUBQUERY:
3298 applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
3299 pushedDown);
3302 * FOR UPDATE/SHARE of subquery is propagated to all of
3303 * subquery's rels, too. We could do this later (based on
3304 * the marking of the subquery RTE) but it is convenient
3305 * to have local knowledge in each query level about which
3306 * rels need to be opened with RowShareLock.
3308 transformLockingClause(pstate, rte->subquery,
3309 allrels, true);
3310 break;
3311 default:
3312 /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
3313 break;
3317 else
3320 * Lock just the named tables. As above, we allow locking any base
3321 * relation regardless of alias-visibility rules, so we need to
3322 * examine inFromCl to exclude OLD/NEW.
3324 foreach(l, lockedRels)
3326 RangeVar *thisrel = (RangeVar *) lfirst(l);
3328 /* For simplicity we insist on unqualified alias names here */
3329 if (thisrel->catalogname || thisrel->schemaname)
3330 ereport(ERROR,
3331 (errcode(ERRCODE_SYNTAX_ERROR),
3332 /*------
3333 translator: %s is a SQL row locking clause such as FOR UPDATE */
3334 errmsg("%s must specify unqualified relation names",
3335 LCS_asString(lc->strength)),
3336 parser_errposition(pstate, thisrel->location)));
3338 i = 0;
3339 foreach(rt, qry->rtable)
3341 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3342 char *rtename = rte->eref->aliasname;
3344 ++i;
3345 if (!rte->inFromCl)
3346 continue;
3349 * A join RTE without an alias is not visible as a relation
3350 * name and needs to be skipped (otherwise it might hide a
3351 * base relation with the same name), except if it has a USING
3352 * alias, which *is* visible.
3354 * Subquery and values RTEs without aliases are never visible
3355 * as relation names and must always be skipped.
3357 if (rte->alias == NULL)
3359 if (rte->rtekind == RTE_JOIN)
3361 if (rte->join_using_alias == NULL)
3362 continue;
3363 rtename = rte->join_using_alias->aliasname;
3365 else if (rte->rtekind == RTE_SUBQUERY ||
3366 rte->rtekind == RTE_VALUES)
3367 continue;
3370 if (strcmp(rtename, thisrel->relname) == 0)
3372 switch (rte->rtekind)
3374 case RTE_RELATION:
3376 RTEPermissionInfo *perminfo;
3378 applyLockingClause(qry, i,
3379 lc->strength,
3380 lc->waitPolicy,
3381 pushedDown);
3382 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3383 perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3385 break;
3386 case RTE_SUBQUERY:
3387 applyLockingClause(qry, i, lc->strength,
3388 lc->waitPolicy, pushedDown);
3389 /* see comment above */
3390 transformLockingClause(pstate, rte->subquery,
3391 allrels, true);
3392 break;
3393 case RTE_JOIN:
3394 ereport(ERROR,
3395 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3396 /*------
3397 translator: %s is a SQL row locking clause such as FOR UPDATE */
3398 errmsg("%s cannot be applied to a join",
3399 LCS_asString(lc->strength)),
3400 parser_errposition(pstate, thisrel->location)));
3401 break;
3402 case RTE_FUNCTION:
3403 ereport(ERROR,
3404 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3405 /*------
3406 translator: %s is a SQL row locking clause such as FOR UPDATE */
3407 errmsg("%s cannot be applied to a function",
3408 LCS_asString(lc->strength)),
3409 parser_errposition(pstate, thisrel->location)));
3410 break;
3411 case RTE_TABLEFUNC:
3412 ereport(ERROR,
3413 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3414 /*------
3415 translator: %s is a SQL row locking clause such as FOR UPDATE */
3416 errmsg("%s cannot be applied to a table function",
3417 LCS_asString(lc->strength)),
3418 parser_errposition(pstate, thisrel->location)));
3419 break;
3420 case RTE_VALUES:
3421 ereport(ERROR,
3422 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3423 /*------
3424 translator: %s is a SQL row locking clause such as FOR UPDATE */
3425 errmsg("%s cannot be applied to VALUES",
3426 LCS_asString(lc->strength)),
3427 parser_errposition(pstate, thisrel->location)));
3428 break;
3429 case RTE_CTE:
3430 ereport(ERROR,
3431 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3432 /*------
3433 translator: %s is a SQL row locking clause such as FOR UPDATE */
3434 errmsg("%s cannot be applied to a WITH query",
3435 LCS_asString(lc->strength)),
3436 parser_errposition(pstate, thisrel->location)));
3437 break;
3438 case RTE_NAMEDTUPLESTORE:
3439 ereport(ERROR,
3440 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3441 /*------
3442 translator: %s is a SQL row locking clause such as FOR UPDATE */
3443 errmsg("%s cannot be applied to a named tuplestore",
3444 LCS_asString(lc->strength)),
3445 parser_errposition(pstate, thisrel->location)));
3446 break;
3448 /* Shouldn't be possible to see RTE_RESULT here */
3450 default:
3451 elog(ERROR, "unrecognized RTE type: %d",
3452 (int) rte->rtekind);
3453 break;
3455 break; /* out of foreach loop */
3458 if (rt == NULL)
3459 ereport(ERROR,
3460 (errcode(ERRCODE_UNDEFINED_TABLE),
3461 /*------
3462 translator: %s is a SQL row locking clause such as FOR UPDATE */
3463 errmsg("relation \"%s\" in %s clause not found in FROM clause",
3464 thisrel->relname,
3465 LCS_asString(lc->strength)),
3466 parser_errposition(pstate, thisrel->location)));
3472 * Record locking info for a single rangetable item
3474 void
3475 applyLockingClause(Query *qry, Index rtindex,
3476 LockClauseStrength strength, LockWaitPolicy waitPolicy,
3477 bool pushedDown)
3479 RowMarkClause *rc;
3481 Assert(strength != LCS_NONE); /* else caller error */
3483 /* If it's an explicit clause, make sure hasForUpdate gets set */
3484 if (!pushedDown)
3485 qry->hasForUpdate = true;
3487 /* Check for pre-existing entry for same rtindex */
3488 if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
3491 * If the same RTE is specified with more than one locking strength,
3492 * use the strongest. (Reasonable, since you can't take both a shared
3493 * and exclusive lock at the same time; it'll end up being exclusive
3494 * anyway.)
3496 * Similarly, if the same RTE is specified with more than one lock
3497 * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
3498 * turn wins over waiting for the lock (the default). This is a bit
3499 * more debatable but raising an error doesn't seem helpful. (Consider
3500 * for instance SELECT FOR UPDATE NOWAIT from a view that internally
3501 * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
3502 * LOCKED is reasonable since the former throws an error in case of
3503 * coming across a locked tuple, which may be undesirable in some
3504 * cases but it seems better than silently returning inconsistent
3505 * results.
3507 * And of course pushedDown becomes false if any clause is explicit.
3509 rc->strength = Max(rc->strength, strength);
3510 rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
3511 rc->pushedDown &= pushedDown;
3512 return;
3515 /* Make a new RowMarkClause */
3516 rc = makeNode(RowMarkClause);
3517 rc->rti = rtindex;
3518 rc->strength = strength;
3519 rc->waitPolicy = waitPolicy;
3520 rc->pushedDown = pushedDown;
3521 qry->rowMarks = lappend(qry->rowMarks, rc);
3525 * Coverage testing for raw_expression_tree_walker().
3527 * When enabled, we run raw_expression_tree_walker() over every DML statement
3528 * submitted to parse analysis. Without this provision, that function is only
3529 * applied in limited cases involving CTEs, and we don't really want to have
3530 * to test everything inside as well as outside a CTE.
3532 #ifdef RAW_EXPRESSION_COVERAGE_TEST
3534 static bool
3535 test_raw_expression_coverage(Node *node, void *context)
3537 if (node == NULL)
3538 return false;
3539 return raw_expression_tree_walker(node,
3540 test_raw_expression_coverage,
3541 context);
3544 #endif /* RAW_EXPRESSION_COVERAGE_TEST */