Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * analyze.c
4 : * transform the raw parse tree into a query tree
5 : *
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.
15 : *
16 : *
17 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
18 : * Portions Copyright (c) 1994, Regents of the University of California
19 : *
20 : * src/backend/parser/analyze.c
21 : *
22 : *-------------------------------------------------------------------------
23 : */
24 :
25 : #include "postgres.h"
26 :
27 : #include "access/sysattr.h"
28 : #include "catalog/dependency.h"
29 : #include "catalog/pg_proc.h"
30 : #include "catalog/pg_type.h"
31 : #include "commands/defrem.h"
32 : #include "miscadmin.h"
33 : #include "nodes/makefuncs.h"
34 : #include "nodes/nodeFuncs.h"
35 : #include "nodes/queryjumble.h"
36 : #include "optimizer/optimizer.h"
37 : #include "parser/analyze.h"
38 : #include "parser/parse_agg.h"
39 : #include "parser/parse_clause.h"
40 : #include "parser/parse_coerce.h"
41 : #include "parser/parse_collate.h"
42 : #include "parser/parse_cte.h"
43 : #include "parser/parse_expr.h"
44 : #include "parser/parse_func.h"
45 : #include "parser/parse_merge.h"
46 : #include "parser/parse_oper.h"
47 : #include "parser/parse_param.h"
48 : #include "parser/parse_relation.h"
49 : #include "parser/parse_target.h"
50 : #include "parser/parse_type.h"
51 : #include "parser/parsetree.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"
57 :
58 :
59 : /* Passthrough data for transformPLAssignStmtTarget */
60 : typedef struct SelectStmtPassthrough
61 : {
62 : PLAssignStmt *stmt; /* the assignment statement */
63 : Node *target; /* node representing the target variable */
64 : List *indirection; /* indirection yet to be applied to target */
65 : } SelectStmtPassthrough;
66 :
67 : /* Hook for plugins to get control at end of parse analysis */
68 : post_parse_analyze_hook_type post_parse_analyze_hook = NULL;
69 :
70 : static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree);
71 : static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
72 : static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
73 : static OnConflictExpr *transformOnConflictClause(ParseState *pstate,
74 : OnConflictClause *onConflictClause);
75 : static int count_rowexpr_columns(ParseState *pstate, Node *expr);
76 : static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt,
77 : SelectStmtPassthrough *passthru);
78 : static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
79 : static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
80 : static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
81 : bool isTopLevel, List **targetlist);
82 : static void determineRecursiveColTypes(ParseState *pstate,
83 : Node *larg, List *nrtargetlist);
84 : static Query *transformReturnStmt(ParseState *pstate, ReturnStmt *stmt);
85 : static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
86 : static Query *transformPLAssignStmt(ParseState *pstate,
87 : PLAssignStmt *stmt);
88 : static List *transformPLAssignStmtTarget(ParseState *pstate, List *tlist,
89 : SelectStmtPassthrough *passthru);
90 : static Query *transformDeclareCursorStmt(ParseState *pstate,
91 : DeclareCursorStmt *stmt);
92 : static Query *transformExplainStmt(ParseState *pstate,
93 : ExplainStmt *stmt);
94 : static Query *transformCreateTableAsStmt(ParseState *pstate,
95 : CreateTableAsStmt *stmt);
96 : static Query *transformCallStmt(ParseState *pstate,
97 : CallStmt *stmt);
98 : static void transformLockingClause(ParseState *pstate, Query *qry,
99 : LockingClause *lc, bool pushedDown);
100 : #ifdef DEBUG_NODE_TESTS_ENABLED
101 : static bool test_raw_expression_coverage(Node *node, void *context);
102 : #endif
103 :
104 :
105 : /*
106 : * parse_analyze_fixedparams
107 : * Analyze a raw parse tree and transform it to Query form.
108 : *
109 : * Optionally, information about $n parameter types can be supplied.
110 : * References to $n indexes not defined by paramTypes[] are disallowed.
111 : *
112 : * The result is a Query node. Optimizable statements require considerable
113 : * transformation, while utility-type statements are simply hung off
114 : * a dummy CMD_UTILITY Query node.
115 : */
116 : Query *
117 414933 : parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
118 : const Oid *paramTypes, int numParams,
119 : QueryEnvironment *queryEnv)
120 : {
121 414933 : ParseState *pstate = make_parsestate(NULL);
122 : Query *query;
123 414933 : JumbleState *jstate = NULL;
124 :
125 : Assert(sourceText != NULL); /* required as of 8.4 */
126 :
127 414933 : pstate->p_sourcetext = sourceText;
128 :
129 414933 : if (numParams > 0)
130 2160 : setup_parse_fixed_parameters(pstate, paramTypes, numParams);
131 :
132 414933 : pstate->p_queryEnv = queryEnv;
133 :
134 414933 : query = transformTopLevelStmt(pstate, parseTree);
135 :
136 410724 : if (IsQueryIdEnabled())
137 73979 : jstate = JumbleQuery(query);
138 :
139 410724 : if (post_parse_analyze_hook)
140 73922 : (*post_parse_analyze_hook) (pstate, query, jstate);
141 :
142 410724 : free_parsestate(pstate);
143 :
144 410724 : pgstat_report_query_id(query->queryId, false);
145 :
146 410724 : return query;
147 : }
148 :
149 : /*
150 : * parse_analyze_varparams
151 : *
152 : * This variant is used when it's okay to deduce information about $n
153 : * symbol datatypes from context. The passed-in paramTypes[] array can
154 : * be modified or enlarged (via repalloc).
155 : */
156 : Query *
157 6682 : parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
158 : Oid **paramTypes, int *numParams,
159 : QueryEnvironment *queryEnv)
160 : {
161 6682 : ParseState *pstate = make_parsestate(NULL);
162 : Query *query;
163 6682 : JumbleState *jstate = NULL;
164 :
165 : Assert(sourceText != NULL); /* required as of 8.4 */
166 :
167 6682 : pstate->p_sourcetext = sourceText;
168 :
169 6682 : setup_parse_variable_parameters(pstate, paramTypes, numParams);
170 :
171 6682 : pstate->p_queryEnv = queryEnv;
172 :
173 6682 : query = transformTopLevelStmt(pstate, parseTree);
174 :
175 : /* make sure all is well with parameter types */
176 6674 : check_variable_parameters(pstate, query);
177 :
178 6674 : if (IsQueryIdEnabled())
179 266 : jstate = JumbleQuery(query);
180 :
181 6674 : if (post_parse_analyze_hook)
182 266 : (*post_parse_analyze_hook) (pstate, query, jstate);
183 :
184 6674 : free_parsestate(pstate);
185 :
186 6674 : pgstat_report_query_id(query->queryId, false);
187 :
188 6674 : return query;
189 : }
190 :
191 : /*
192 : * parse_analyze_withcb
193 : *
194 : * This variant is used when the caller supplies their own parser callback to
195 : * resolve parameters and possibly other things.
196 : */
197 : Query *
198 19786 : parse_analyze_withcb(RawStmt *parseTree, const char *sourceText,
199 : ParserSetupHook parserSetup,
200 : void *parserSetupArg,
201 : QueryEnvironment *queryEnv)
202 : {
203 19786 : ParseState *pstate = make_parsestate(NULL);
204 : Query *query;
205 19786 : JumbleState *jstate = NULL;
206 :
207 : Assert(sourceText != NULL); /* required as of 8.4 */
208 :
209 19786 : pstate->p_sourcetext = sourceText;
210 19786 : pstate->p_queryEnv = queryEnv;
211 19786 : (*parserSetup) (pstate, parserSetupArg);
212 :
213 19786 : query = transformTopLevelStmt(pstate, parseTree);
214 :
215 19724 : if (IsQueryIdEnabled())
216 3882 : jstate = JumbleQuery(query);
217 :
218 19724 : if (post_parse_analyze_hook)
219 3882 : (*post_parse_analyze_hook) (pstate, query, jstate);
220 :
221 19724 : free_parsestate(pstate);
222 :
223 19724 : pgstat_report_query_id(query->queryId, false);
224 :
225 19724 : return query;
226 : }
227 :
228 :
229 : /*
230 : * parse_sub_analyze
231 : * Entry point for recursively analyzing a sub-statement.
232 : */
233 : Query *
234 59461 : parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
235 : CommonTableExpr *parentCTE,
236 : bool locked_from_parent,
237 : bool resolve_unknowns)
238 : {
239 59461 : ParseState *pstate = make_parsestate(parentParseState);
240 : Query *query;
241 :
242 59461 : pstate->p_parent_cte = parentCTE;
243 59461 : pstate->p_locked_from_parent = locked_from_parent;
244 59461 : pstate->p_resolve_unknowns = resolve_unknowns;
245 :
246 59461 : query = transformStmt(pstate, parseTree);
247 :
248 59355 : free_parsestate(pstate);
249 :
250 59355 : return query;
251 : }
252 :
253 : /*
254 : * transformTopLevelStmt -
255 : * transform a Parse tree into a Query tree.
256 : *
257 : * This function is just responsible for transferring statement location data
258 : * from the RawStmt into the finished Query.
259 : */
260 : Query *
261 442929 : transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
262 : {
263 : Query *result;
264 :
265 : /* We're at top level, so allow SELECT INTO */
266 442929 : result = transformOptionalSelectInto(pstate, parseTree->stmt);
267 :
268 438647 : result->stmt_location = parseTree->stmt_location;
269 438647 : result->stmt_len = parseTree->stmt_len;
270 :
271 438647 : return result;
272 : }
273 :
274 : /*
275 : * transformOptionalSelectInto -
276 : * If SELECT has INTO, convert it to CREATE TABLE AS.
277 : *
278 : * The only thing we do here that we don't do in transformStmt() is to
279 : * convert SELECT ... INTO into CREATE TABLE AS. Since utility statements
280 : * aren't allowed within larger statements, this is only allowed at the top
281 : * of the parse tree, and so we only try it before entering the recursive
282 : * transformStmt() processing.
283 : */
284 : static Query *
285 455648 : transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
286 : {
287 455648 : if (IsA(parseTree, SelectStmt))
288 : {
289 198567 : SelectStmt *stmt = (SelectStmt *) parseTree;
290 :
291 : /* If it's a set-operation tree, drill down to leftmost SelectStmt */
292 204393 : while (stmt && stmt->op != SETOP_NONE)
293 5826 : stmt = stmt->larg;
294 : Assert(stmt && IsA(stmt, SelectStmt) && stmt->larg == NULL);
295 :
296 198567 : if (stmt->intoClause)
297 : {
298 54 : CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
299 :
300 54 : ctas->query = parseTree;
301 54 : ctas->into = stmt->intoClause;
302 54 : ctas->objtype = OBJECT_TABLE;
303 54 : ctas->is_select_into = true;
304 :
305 : /*
306 : * Remove the intoClause from the SelectStmt. This makes it safe
307 : * for transformSelectStmt to complain if it finds intoClause set
308 : * (implying that the INTO appeared in a disallowed place).
309 : */
310 54 : stmt->intoClause = NULL;
311 :
312 54 : parseTree = (Node *) ctas;
313 : }
314 : }
315 :
316 455648 : return transformStmt(pstate, parseTree);
317 : }
318 :
319 : /*
320 : * transformStmt -
321 : * recursively transform a Parse tree into a Query tree.
322 : */
323 : Query *
324 525602 : transformStmt(ParseState *pstate, Node *parseTree)
325 : {
326 : Query *result;
327 :
328 : #ifdef DEBUG_NODE_TESTS_ENABLED
329 :
330 : /*
331 : * We apply debug_raw_expression_coverage_test testing to basic DML
332 : * statements; we can't just run it on everything because
333 : * raw_expression_tree_walker() doesn't claim to handle utility
334 : * statements.
335 : */
336 525602 : if (Debug_raw_expression_coverage_test)
337 : {
338 525602 : switch (nodeTag(parseTree))
339 : {
340 314553 : case T_SelectStmt:
341 : case T_InsertStmt:
342 : case T_UpdateStmt:
343 : case T_DeleteStmt:
344 : case T_MergeStmt:
345 314553 : (void) test_raw_expression_coverage(parseTree, NULL);
346 314553 : break;
347 211049 : default:
348 211049 : break;
349 : }
350 : }
351 : #endif /* DEBUG_NODE_TESTS_ENABLED */
352 :
353 : /*
354 : * Caution: when changing the set of statement types that have non-default
355 : * processing here, see also stmt_requires_parse_analysis() and
356 : * analyze_requires_snapshot().
357 : */
358 525602 : switch (nodeTag(parseTree))
359 : {
360 : /*
361 : * Optimizable statements
362 : */
363 38416 : case T_InsertStmt:
364 38416 : result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
365 37660 : break;
366 :
367 2308 : case T_DeleteStmt:
368 2308 : result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
369 2281 : break;
370 :
371 7479 : case T_UpdateStmt:
372 7479 : result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
373 7427 : break;
374 :
375 1061 : case T_MergeStmt:
376 1061 : result = transformMergeStmt(pstate, (MergeStmt *) parseTree);
377 1028 : break;
378 :
379 265289 : case T_SelectStmt:
380 : {
381 265289 : SelectStmt *n = (SelectStmt *) parseTree;
382 :
383 265289 : if (n->valuesLists)
384 4335 : result = transformValuesClause(pstate, n);
385 260954 : else if (n->op == SETOP_NONE)
386 254045 : result = transformSelectStmt(pstate, n, NULL);
387 : else
388 6909 : result = transformSetOperationStmt(pstate, n);
389 : }
390 261795 : break;
391 :
392 2485 : case T_ReturnStmt:
393 2485 : result = transformReturnStmt(pstate, (ReturnStmt *) parseTree);
394 2482 : break;
395 :
396 2785 : case T_PLAssignStmt:
397 2785 : result = transformPLAssignStmt(pstate,
398 : (PLAssignStmt *) parseTree);
399 2772 : break;
400 :
401 : /*
402 : * Special cases
403 : */
404 2308 : case T_DeclareCursorStmt:
405 2308 : result = transformDeclareCursorStmt(pstate,
406 : (DeclareCursorStmt *) parseTree);
407 2297 : break;
408 :
409 12719 : case T_ExplainStmt:
410 12719 : result = transformExplainStmt(pstate,
411 : (ExplainStmt *) parseTree);
412 12712 : break;
413 :
414 1038 : case T_CreateTableAsStmt:
415 1038 : result = transformCreateTableAsStmt(pstate,
416 : (CreateTableAsStmt *) parseTree);
417 1030 : break;
418 :
419 265 : case T_CallStmt:
420 265 : result = transformCallStmt(pstate,
421 : (CallStmt *) parseTree);
422 249 : break;
423 :
424 189449 : default:
425 :
426 : /*
427 : * other statements don't require any transformation; just return
428 : * the original parsetree with a Query node plastered on top.
429 : */
430 189449 : result = makeNode(Query);
431 189449 : result->commandType = CMD_UTILITY;
432 189449 : result->utilityStmt = parseTree;
433 189449 : break;
434 : }
435 :
436 : /* Mark as original query until we learn differently */
437 521182 : result->querySource = QSRC_ORIGINAL;
438 521182 : result->canSetTag = true;
439 :
440 521182 : return result;
441 : }
442 :
443 : /*
444 : * stmt_requires_parse_analysis
445 : * Returns true if parse analysis will do anything non-trivial
446 : * with the given raw parse tree.
447 : *
448 : * Generally, this should return true for any statement type for which
449 : * transformStmt() does more than wrap a CMD_UTILITY Query around it.
450 : * When it returns false, the caller can assume that there is no situation
451 : * in which parse analysis of the raw statement could need to be re-done.
452 : *
453 : * Currently, since the rewriter and planner do nothing for CMD_UTILITY
454 : * Queries, a false result means that the entire parse analysis/rewrite/plan
455 : * pipeline will never need to be re-done. If that ever changes, callers
456 : * will likely need adjustment.
457 : */
458 : bool
459 17145813 : stmt_requires_parse_analysis(RawStmt *parseTree)
460 : {
461 : bool result;
462 :
463 17145813 : switch (nodeTag(parseTree->stmt))
464 : {
465 : /*
466 : * Optimizable statements
467 : */
468 16705798 : case T_InsertStmt:
469 : case T_DeleteStmt:
470 : case T_UpdateStmt:
471 : case T_MergeStmt:
472 : case T_SelectStmt:
473 : case T_ReturnStmt:
474 : case T_PLAssignStmt:
475 16705798 : result = true;
476 16705798 : break;
477 :
478 : /*
479 : * Special cases
480 : */
481 25254 : case T_DeclareCursorStmt:
482 : case T_ExplainStmt:
483 : case T_CreateTableAsStmt:
484 : case T_CallStmt:
485 25254 : result = true;
486 25254 : break;
487 :
488 414761 : default:
489 : /* all other statements just get wrapped in a CMD_UTILITY Query */
490 414761 : result = false;
491 414761 : break;
492 : }
493 :
494 17145813 : return result;
495 : }
496 :
497 : /*
498 : * analyze_requires_snapshot
499 : * Returns true if a snapshot must be set before doing parse analysis
500 : * on the given raw parse tree.
501 : */
502 : bool
503 389689 : analyze_requires_snapshot(RawStmt *parseTree)
504 : {
505 : /*
506 : * Currently, this should return true in exactly the same cases that
507 : * stmt_requires_parse_analysis() does, so we just invoke that function
508 : * rather than duplicating it. We keep the two entry points separate for
509 : * clarity of callers, since from the callers' standpoint these are
510 : * different conditions.
511 : *
512 : * While there may someday be a statement type for which transformStmt()
513 : * does something nontrivial and yet no snapshot is needed for that
514 : * processing, it seems likely that making such a choice would be fragile.
515 : * If you want to install an exception, document the reasoning for it in a
516 : * comment.
517 : */
518 389689 : return stmt_requires_parse_analysis(parseTree);
519 : }
520 :
521 : /*
522 : * query_requires_rewrite_plan()
523 : * Returns true if rewriting or planning is non-trivial for this Query.
524 : *
525 : * This is much like stmt_requires_parse_analysis(), but applies one step
526 : * further down the pipeline.
527 : *
528 : * We do not provide an equivalent of analyze_requires_snapshot(): callers
529 : * can assume that any rewriting or planning activity needs a snapshot.
530 : */
531 : bool
532 308045 : query_requires_rewrite_plan(Query *query)
533 : {
534 : bool result;
535 :
536 308045 : if (query->commandType != CMD_UTILITY)
537 : {
538 : /* All optimizable statements require rewriting/planning */
539 308045 : result = true;
540 : }
541 : else
542 : {
543 : /* This list should match stmt_requires_parse_analysis() */
544 0 : switch (nodeTag(query->utilityStmt))
545 : {
546 0 : case T_DeclareCursorStmt:
547 : case T_ExplainStmt:
548 : case T_CreateTableAsStmt:
549 : case T_CallStmt:
550 0 : result = true;
551 0 : break;
552 0 : default:
553 0 : result = false;
554 0 : break;
555 : }
556 : }
557 308045 : return result;
558 : }
559 :
560 : /*
561 : * transformDeleteStmt -
562 : * transforms a Delete Statement
563 : */
564 : static Query *
565 2308 : transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
566 : {
567 2308 : Query *qry = makeNode(Query);
568 : ParseNamespaceItem *nsitem;
569 : Node *qual;
570 :
571 2308 : qry->commandType = CMD_DELETE;
572 :
573 : /* process the WITH clause independently of all else */
574 2308 : if (stmt->withClause)
575 : {
576 16 : qry->hasRecursive = stmt->withClause->recursive;
577 16 : qry->cteList = transformWithClause(pstate, stmt->withClause);
578 16 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
579 : }
580 :
581 : /* set up range table with just the result rel */
582 4613 : qry->resultRelation = setTargetTable(pstate, stmt->relation,
583 2308 : stmt->relation->inh,
584 : true,
585 : ACL_DELETE);
586 2305 : nsitem = pstate->p_target_nsitem;
587 :
588 : /* there's no DISTINCT in DELETE */
589 2305 : qry->distinctClause = NIL;
590 :
591 : /* subqueries in USING cannot access the result relation */
592 2305 : nsitem->p_lateral_only = true;
593 2305 : nsitem->p_lateral_ok = false;
594 :
595 : /*
596 : * The USING clause is non-standard SQL syntax, and is equivalent in
597 : * functionality to the FROM list that can be specified for UPDATE. The
598 : * USING keyword is used rather than FROM because FROM is already a
599 : * keyword in the DELETE syntax.
600 : */
601 2305 : transformFromClause(pstate, stmt->usingClause);
602 :
603 : /* remaining clauses can reference the result relation normally */
604 2296 : nsitem->p_lateral_only = false;
605 2296 : nsitem->p_lateral_ok = true;
606 :
607 2296 : qual = transformWhereClause(pstate, stmt->whereClause,
608 : EXPR_KIND_WHERE, "WHERE");
609 :
610 2284 : transformReturningClause(pstate, qry, stmt->returningClause,
611 : EXPR_KIND_RETURNING);
612 :
613 : /* done building the range table and jointree */
614 2281 : qry->rtable = pstate->p_rtable;
615 2281 : qry->rteperminfos = pstate->p_rteperminfos;
616 2281 : qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
617 :
618 2281 : qry->hasSubLinks = pstate->p_hasSubLinks;
619 2281 : qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
620 2281 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
621 2281 : qry->hasAggs = pstate->p_hasAggs;
622 :
623 2281 : assign_query_collations(pstate, qry);
624 :
625 : /* this must be done after collations, for reliable comparison of exprs */
626 2281 : if (pstate->p_hasAggs)
627 0 : parseCheckAggregates(pstate, qry);
628 :
629 2281 : return qry;
630 : }
631 :
632 : /*
633 : * transformInsertStmt -
634 : * transform an Insert Statement
635 : */
636 : static Query *
637 38416 : transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
638 : {
639 38416 : Query *qry = makeNode(Query);
640 38416 : SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
641 38416 : List *exprList = NIL;
642 : bool isGeneralSelect;
643 : List *sub_rtable;
644 : List *sub_rteperminfos;
645 : List *sub_namespace;
646 : List *icolumns;
647 : List *attrnos;
648 : ParseNamespaceItem *nsitem;
649 : RTEPermissionInfo *perminfo;
650 : ListCell *icols;
651 : ListCell *attnos;
652 : ListCell *lc;
653 : bool requiresUpdatePerm;
654 : AclMode targetPerms;
655 :
656 : /* There can't be any outer WITH to worry about */
657 : Assert(pstate->p_ctenamespace == NIL);
658 :
659 38416 : qry->commandType = CMD_INSERT;
660 :
661 : /* process the WITH clause independently of all else */
662 38416 : if (stmt->withClause)
663 : {
664 152 : qry->hasRecursive = stmt->withClause->recursive;
665 152 : qry->cteList = transformWithClause(pstate, stmt->withClause);
666 152 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
667 : }
668 :
669 38416 : qry->override = stmt->override;
670 :
671 : /*
672 : * ON CONFLICT DO UPDATE and ON CONFLICT DO SELECT FOR UPDATE/SHARE
673 : * require UPDATE permission on the target relation.
674 : */
675 39601 : requiresUpdatePerm = (stmt->onConflictClause &&
676 1185 : (stmt->onConflictClause->action == ONCONFLICT_UPDATE ||
677 485 : (stmt->onConflictClause->action == ONCONFLICT_SELECT &&
678 183 : stmt->onConflictClause->lockStrength != LCS_NONE)));
679 :
680 : /*
681 : * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
682 : * VALUES list, or general SELECT input. We special-case VALUES, both for
683 : * efficiency and so we can handle DEFAULT specifications.
684 : *
685 : * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
686 : * VALUES clause. If we have any of those, treat it as a general SELECT;
687 : * so it will work, but you can't use DEFAULT items together with those.
688 : */
689 67716 : isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
690 29300 : selectStmt->sortClause != NIL ||
691 29300 : selectStmt->limitOffset != NULL ||
692 29300 : selectStmt->limitCount != NULL ||
693 29300 : selectStmt->lockingClause != NIL ||
694 29300 : selectStmt->withClause != NULL));
695 :
696 : /*
697 : * If a non-nil rangetable/namespace was passed in, and we are doing
698 : * INSERT/SELECT, arrange to pass the rangetable/rteperminfos/namespace
699 : * down to the SELECT. This can only happen if we are inside a CREATE
700 : * RULE, and in that case we want the rule's OLD and NEW rtable entries to
701 : * appear as part of the SELECT's rtable, not as outer references for it.
702 : * (Kluge!) The SELECT's joinlist is not affected however. We must do
703 : * this before adding the target table to the INSERT's rtable.
704 : */
705 38416 : if (isGeneralSelect)
706 : {
707 3712 : sub_rtable = pstate->p_rtable;
708 3712 : pstate->p_rtable = NIL;
709 3712 : sub_rteperminfos = pstate->p_rteperminfos;
710 3712 : pstate->p_rteperminfos = NIL;
711 3712 : sub_namespace = pstate->p_namespace;
712 3712 : pstate->p_namespace = NIL;
713 : }
714 : else
715 : {
716 34704 : sub_rtable = NIL; /* not used, but keep compiler quiet */
717 34704 : sub_rteperminfos = NIL;
718 34704 : sub_namespace = NIL;
719 : }
720 :
721 : /*
722 : * Must get write lock on INSERT target table before scanning SELECT, else
723 : * we will grab the wrong kind of initial lock if the target table is also
724 : * mentioned in the SELECT part. Note that the target table is not added
725 : * to the joinlist or namespace.
726 : */
727 38416 : targetPerms = ACL_INSERT;
728 38416 : if (requiresUpdatePerm)
729 761 : targetPerms |= ACL_UPDATE;
730 38416 : qry->resultRelation = setTargetTable(pstate, stmt->relation,
731 : false, false, targetPerms);
732 :
733 : /* Validate stmt->cols list, or build default list if no list given */
734 38404 : icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
735 : Assert(list_length(icolumns) == list_length(attrnos));
736 :
737 : /*
738 : * Determine which variant of INSERT we have.
739 : */
740 38380 : if (selectStmt == NULL)
741 : {
742 : /*
743 : * We have INSERT ... DEFAULT VALUES. We can handle this case by
744 : * emitting an empty targetlist --- all columns will be defaulted when
745 : * the planner expands the targetlist.
746 : */
747 5404 : exprList = NIL;
748 : }
749 32976 : else if (isGeneralSelect)
750 : {
751 : /*
752 : * We make the sub-pstate a child of the outer pstate so that it can
753 : * see any Param definitions supplied from above. Since the outer
754 : * pstate's rtable and namespace are presently empty, there are no
755 : * side-effects of exposing names the sub-SELECT shouldn't be able to
756 : * see.
757 : */
758 3712 : ParseState *sub_pstate = make_parsestate(pstate);
759 : Query *selectQuery;
760 :
761 : /*
762 : * Process the source SELECT.
763 : *
764 : * It is important that this be handled just like a standalone SELECT;
765 : * otherwise the behavior of SELECT within INSERT might be different
766 : * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
767 : * bugs of just that nature...)
768 : *
769 : * The sole exception is that we prevent resolving unknown-type
770 : * outputs as TEXT. This does not change the semantics since if the
771 : * column type matters semantically, it would have been resolved to
772 : * something else anyway. Doing this lets us resolve such outputs as
773 : * the target column's type, which we handle below.
774 : */
775 3712 : sub_pstate->p_rtable = sub_rtable;
776 3712 : sub_pstate->p_rteperminfos = sub_rteperminfos;
777 3712 : sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
778 3712 : sub_pstate->p_nullingrels = NIL;
779 3712 : sub_pstate->p_namespace = sub_namespace;
780 3712 : sub_pstate->p_resolve_unknowns = false;
781 :
782 3712 : selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
783 :
784 3709 : free_parsestate(sub_pstate);
785 :
786 : /* The grammar should have produced a SELECT */
787 3709 : if (!IsA(selectQuery, Query) ||
788 3709 : selectQuery->commandType != CMD_SELECT)
789 0 : elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
790 :
791 : /*
792 : * Make the source be a subquery in the INSERT's rangetable, and add
793 : * it to the INSERT's joinlist (but not the namespace).
794 : */
795 3709 : nsitem = addRangeTableEntryForSubquery(pstate,
796 : selectQuery,
797 : NULL,
798 : false,
799 : false);
800 3709 : addNSItemToQuery(pstate, nsitem, true, false, false);
801 :
802 : /*----------
803 : * Generate an expression list for the INSERT that selects all the
804 : * non-resjunk columns from the subquery. (INSERT's tlist must be
805 : * separate from the subquery's tlist because we may add columns,
806 : * insert datatype coercions, etc.)
807 : *
808 : * HACK: unknown-type constants and params in the SELECT's targetlist
809 : * are copied up as-is rather than being referenced as subquery
810 : * outputs. This is to ensure that when we try to coerce them to
811 : * the target column's datatype, the right things happen (see
812 : * special cases in coerce_type). Otherwise, this fails:
813 : * INSERT INTO foo SELECT 'bar', ... FROM baz
814 : *----------
815 : */
816 3709 : exprList = NIL;
817 13101 : foreach(lc, selectQuery->targetList)
818 : {
819 9392 : TargetEntry *tle = (TargetEntry *) lfirst(lc);
820 : Expr *expr;
821 :
822 9392 : if (tle->resjunk)
823 50 : continue;
824 9342 : if (tle->expr &&
825 11648 : (IsA(tle->expr, Const) || IsA(tle->expr, Param)) &&
826 2306 : exprType((Node *) tle->expr) == UNKNOWNOID)
827 704 : expr = tle->expr;
828 : else
829 : {
830 8638 : Var *var = makeVarFromTargetEntry(nsitem->p_rtindex, tle);
831 :
832 8638 : var->location = exprLocation((Node *) tle->expr);
833 8638 : expr = (Expr *) var;
834 : }
835 9342 : exprList = lappend(exprList, expr);
836 : }
837 :
838 : /* Prepare row for assignment to target table */
839 3709 : exprList = transformInsertRow(pstate, exprList,
840 : stmt->cols,
841 : icolumns, attrnos,
842 : false);
843 : }
844 29264 : else if (list_length(selectStmt->valuesLists) > 1)
845 : {
846 : /*
847 : * Process INSERT ... VALUES with multiple VALUES sublists. We
848 : * generate a VALUES RTE holding the transformed expression lists, and
849 : * build up a targetlist containing Vars that reference the VALUES
850 : * RTE.
851 : */
852 2611 : List *exprsLists = NIL;
853 2611 : List *coltypes = NIL;
854 2611 : List *coltypmods = NIL;
855 2611 : List *colcollations = NIL;
856 2611 : int sublist_length = -1;
857 2611 : bool lateral = false;
858 :
859 : Assert(selectStmt->intoClause == NULL);
860 :
861 11516 : foreach(lc, selectStmt->valuesLists)
862 : {
863 8905 : List *sublist = (List *) lfirst(lc);
864 :
865 : /*
866 : * Do basic expression transformation (same as a ROW() expr, but
867 : * allow SetToDefault at top level)
868 : */
869 8905 : sublist = transformExpressionList(pstate, sublist,
870 : EXPR_KIND_VALUES, true);
871 :
872 : /*
873 : * All the sublists must be the same length, *after*
874 : * transformation (which might expand '*' into multiple items).
875 : * The VALUES RTE can't handle anything different.
876 : */
877 8905 : if (sublist_length < 0)
878 : {
879 : /* Remember post-transformation length of first sublist */
880 2611 : sublist_length = list_length(sublist);
881 : }
882 6294 : else if (sublist_length != list_length(sublist))
883 : {
884 0 : ereport(ERROR,
885 : (errcode(ERRCODE_SYNTAX_ERROR),
886 : errmsg("VALUES lists must all be the same length"),
887 : parser_errposition(pstate,
888 : exprLocation((Node *) sublist))));
889 : }
890 :
891 : /*
892 : * Prepare row for assignment to target table. We process any
893 : * indirection on the target column specs normally but then strip
894 : * off the resulting field/array assignment nodes, since we don't
895 : * want the parsed statement to contain copies of those in each
896 : * VALUES row. (It's annoying to have to transform the
897 : * indirection specs over and over like this, but avoiding it
898 : * would take some really messy refactoring of
899 : * transformAssignmentIndirection.)
900 : */
901 8905 : sublist = transformInsertRow(pstate, sublist,
902 : stmt->cols,
903 : icolumns, attrnos,
904 : true);
905 :
906 : /*
907 : * We must assign collations now because assign_query_collations
908 : * doesn't process rangetable entries. We just assign all the
909 : * collations independently in each row, and don't worry about
910 : * whether they are consistent vertically. The outer INSERT query
911 : * isn't going to care about the collations of the VALUES columns,
912 : * so it's not worth the effort to identify a common collation for
913 : * each one here. (But note this does have one user-visible
914 : * consequence: INSERT ... VALUES won't complain about conflicting
915 : * explicit COLLATEs in a column, whereas the same VALUES
916 : * construct in another context would complain.)
917 : */
918 8905 : assign_list_collations(pstate, sublist);
919 :
920 8905 : exprsLists = lappend(exprsLists, sublist);
921 : }
922 :
923 : /*
924 : * Construct column type/typmod/collation lists for the VALUES RTE.
925 : * Every expression in each column has been coerced to the type/typmod
926 : * of the corresponding target column or subfield, so it's sufficient
927 : * to look at the exprType/exprTypmod of the first row. We don't care
928 : * about the collation labeling, so just fill in InvalidOid for that.
929 : */
930 7526 : foreach(lc, (List *) linitial(exprsLists))
931 : {
932 4915 : Node *val = (Node *) lfirst(lc);
933 :
934 4915 : coltypes = lappend_oid(coltypes, exprType(val));
935 4915 : coltypmods = lappend_int(coltypmods, exprTypmod(val));
936 4915 : colcollations = lappend_oid(colcollations, InvalidOid);
937 : }
938 :
939 : /*
940 : * Ordinarily there can't be any current-level Vars in the expression
941 : * lists, because the namespace was empty ... but if we're inside
942 : * CREATE RULE, then NEW/OLD references might appear. In that case we
943 : * have to mark the VALUES RTE as LATERAL.
944 : */
945 2625 : if (list_length(pstate->p_rtable) != 1 &&
946 14 : contain_vars_of_level((Node *) exprsLists, 0))
947 14 : lateral = true;
948 :
949 : /*
950 : * Generate the VALUES RTE
951 : */
952 2611 : nsitem = addRangeTableEntryForValues(pstate, exprsLists,
953 : coltypes, coltypmods, colcollations,
954 : NULL, lateral, true);
955 2611 : addNSItemToQuery(pstate, nsitem, true, false, false);
956 :
957 : /*
958 : * Generate list of Vars referencing the RTE
959 : */
960 2611 : exprList = expandNSItemVars(pstate, nsitem, 0, -1, NULL);
961 :
962 : /*
963 : * Re-apply any indirection on the target column specs to the Vars
964 : */
965 2611 : exprList = transformInsertRow(pstate, exprList,
966 : stmt->cols,
967 : icolumns, attrnos,
968 : false);
969 : }
970 : else
971 : {
972 : /*
973 : * Process INSERT ... VALUES with a single VALUES sublist. We treat
974 : * this case separately for efficiency. The sublist is just computed
975 : * directly as the Query's targetlist, with no VALUES RTE. So it
976 : * works just like a SELECT without any FROM.
977 : */
978 26653 : List *valuesLists = selectStmt->valuesLists;
979 :
980 : Assert(list_length(valuesLists) == 1);
981 : Assert(selectStmt->intoClause == NULL);
982 :
983 : /*
984 : * Do basic expression transformation (same as a ROW() expr, but allow
985 : * SetToDefault at top level)
986 : */
987 26653 : exprList = transformExpressionList(pstate,
988 26653 : (List *) linitial(valuesLists),
989 : EXPR_KIND_VALUES_SINGLE,
990 : true);
991 :
992 : /* Prepare row for assignment to target table */
993 26641 : exprList = transformInsertRow(pstate, exprList,
994 : stmt->cols,
995 : icolumns, attrnos,
996 : false);
997 : }
998 :
999 : /*
1000 : * Generate query's target list using the computed list of expressions.
1001 : * Also, mark all the target columns as needing insert permissions.
1002 : */
1003 37717 : perminfo = pstate->p_target_nsitem->p_perminfo;
1004 37717 : qry->targetList = NIL;
1005 : Assert(list_length(exprList) <= list_length(icolumns));
1006 109542 : forthree(lc, exprList, icols, icolumns, attnos, attrnos)
1007 : {
1008 71825 : Expr *expr = (Expr *) lfirst(lc);
1009 71825 : ResTarget *col = lfirst_node(ResTarget, icols);
1010 71825 : AttrNumber attr_num = (AttrNumber) lfirst_int(attnos);
1011 : TargetEntry *tle;
1012 :
1013 71825 : tle = makeTargetEntry(expr,
1014 : attr_num,
1015 : col->name,
1016 : false);
1017 71825 : qry->targetList = lappend(qry->targetList, tle);
1018 :
1019 71825 : perminfo->insertedCols = bms_add_member(perminfo->insertedCols,
1020 : attr_num - FirstLowInvalidHeapAttributeNumber);
1021 : }
1022 :
1023 : /*
1024 : * If we have any clauses yet to process, set the query namespace to
1025 : * contain only the target relation, removing any entries added in a
1026 : * sub-SELECT or VALUES list.
1027 : */
1028 37717 : if (stmt->onConflictClause || stmt->returningClause)
1029 : {
1030 1694 : pstate->p_namespace = NIL;
1031 1694 : addNSItemToQuery(pstate, pstate->p_target_nsitem,
1032 : false, true, true);
1033 : }
1034 :
1035 : /* ON CONFLICT DO SELECT requires a RETURNING clause */
1036 37717 : if (stmt->onConflictClause &&
1037 1185 : stmt->onConflictClause->action == ONCONFLICT_SELECT &&
1038 183 : !stmt->returningClause)
1039 3 : ereport(ERROR,
1040 : errcode(ERRCODE_SYNTAX_ERROR),
1041 : errmsg("ON CONFLICT DO SELECT requires a RETURNING clause"),
1042 : parser_errposition(pstate, stmt->onConflictClause->location));
1043 :
1044 : /* Process ON CONFLICT, if any. */
1045 37714 : if (stmt->onConflictClause)
1046 1182 : qry->onConflict = transformOnConflictClause(pstate,
1047 : stmt->onConflictClause);
1048 :
1049 : /* Process RETURNING, if any. */
1050 37684 : if (stmt->returningClause)
1051 839 : transformReturningClause(pstate, qry, stmt->returningClause,
1052 : EXPR_KIND_RETURNING);
1053 :
1054 : /* done building the range table and jointree */
1055 37660 : qry->rtable = pstate->p_rtable;
1056 37660 : qry->rteperminfos = pstate->p_rteperminfos;
1057 37660 : qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1058 :
1059 37660 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1060 37660 : qry->hasSubLinks = pstate->p_hasSubLinks;
1061 :
1062 37660 : assign_query_collations(pstate, qry);
1063 :
1064 37660 : return qry;
1065 : }
1066 :
1067 : /*
1068 : * Prepare an INSERT row for assignment to the target table.
1069 : *
1070 : * exprlist: transformed expressions for source values; these might come from
1071 : * a VALUES row, or be Vars referencing a sub-SELECT or VALUES RTE output.
1072 : * stmtcols: original target-columns spec for INSERT (we just test for NIL)
1073 : * icolumns: effective target-columns spec (list of ResTarget)
1074 : * attrnos: integer column numbers (must be same length as icolumns)
1075 : * strip_indirection: if true, remove any field/array assignment nodes
1076 : */
1077 : List *
1078 42374 : transformInsertRow(ParseState *pstate, List *exprlist,
1079 : List *stmtcols, List *icolumns, List *attrnos,
1080 : bool strip_indirection)
1081 : {
1082 : List *result;
1083 : ListCell *lc;
1084 : ListCell *icols;
1085 : ListCell *attnos;
1086 :
1087 : /*
1088 : * Check length of expr list. It must not have more expressions than
1089 : * there are target columns. We allow fewer, but only if no explicit
1090 : * columns list was given (the remaining columns are implicitly
1091 : * defaulted). Note we must check this *after* transformation because
1092 : * that could expand '*' into multiple items.
1093 : */
1094 42374 : if (list_length(exprlist) > list_length(icolumns))
1095 13 : ereport(ERROR,
1096 : (errcode(ERRCODE_SYNTAX_ERROR),
1097 : errmsg("INSERT has more expressions than target columns"),
1098 : parser_errposition(pstate,
1099 : exprLocation(list_nth(exprlist,
1100 : list_length(icolumns))))));
1101 51164 : if (stmtcols != NIL &&
1102 8803 : list_length(exprlist) < list_length(icolumns))
1103 : {
1104 : /*
1105 : * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
1106 : * where the user accidentally created a RowExpr instead of separate
1107 : * columns. Add a suitable hint if that seems to be the problem,
1108 : * because the main error message is quite misleading for this case.
1109 : * (If there's no stmtcols, you'll get something about data type
1110 : * mismatch, which is less misleading so we don't worry about giving a
1111 : * hint in that case.)
1112 : */
1113 6 : ereport(ERROR,
1114 : (errcode(ERRCODE_SYNTAX_ERROR),
1115 : errmsg("INSERT has more target columns than expressions"),
1116 : ((list_length(exprlist) == 1 &&
1117 : count_rowexpr_columns(pstate, linitial(exprlist)) ==
1118 : list_length(icolumns)) ?
1119 : 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),
1120 : parser_errposition(pstate,
1121 : exprLocation(list_nth(icolumns,
1122 : list_length(exprlist))))));
1123 : }
1124 :
1125 : /*
1126 : * Prepare columns for assignment to target table.
1127 : */
1128 42355 : result = NIL;
1129 134394 : forthree(lc, exprlist, icols, icolumns, attnos, attrnos)
1130 : {
1131 92668 : Expr *expr = (Expr *) lfirst(lc);
1132 92668 : ResTarget *col = lfirst_node(ResTarget, icols);
1133 92668 : int attno = lfirst_int(attnos);
1134 :
1135 92668 : expr = transformAssignedExpr(pstate, expr,
1136 : EXPR_KIND_INSERT_TARGET,
1137 92668 : col->name,
1138 : attno,
1139 : col->indirection,
1140 : col->location);
1141 :
1142 92039 : if (strip_indirection)
1143 : {
1144 : /*
1145 : * We need to remove top-level FieldStores and SubscriptingRefs,
1146 : * as well as any CoerceToDomain appearing above one of those ---
1147 : * but not a CoerceToDomain that isn't above one of those.
1148 : */
1149 19329 : while (expr)
1150 : {
1151 19329 : Expr *subexpr = expr;
1152 :
1153 19437 : while (IsA(subexpr, CoerceToDomain))
1154 : {
1155 108 : subexpr = ((CoerceToDomain *) subexpr)->arg;
1156 : }
1157 19329 : if (IsA(subexpr, FieldStore))
1158 : {
1159 108 : FieldStore *fstore = (FieldStore *) subexpr;
1160 :
1161 108 : expr = (Expr *) linitial(fstore->newvals);
1162 : }
1163 19221 : else if (IsA(subexpr, SubscriptingRef))
1164 : {
1165 174 : SubscriptingRef *sbsref = (SubscriptingRef *) subexpr;
1166 :
1167 174 : if (sbsref->refassgnexpr == NULL)
1168 0 : break;
1169 :
1170 174 : expr = sbsref->refassgnexpr;
1171 : }
1172 : else
1173 19047 : break;
1174 : }
1175 : }
1176 :
1177 92039 : result = lappend(result, expr);
1178 : }
1179 :
1180 41726 : return result;
1181 : }
1182 :
1183 : /*
1184 : * transformOnConflictClause -
1185 : * transforms an OnConflictClause in an INSERT
1186 : */
1187 : static OnConflictExpr *
1188 1182 : transformOnConflictClause(ParseState *pstate,
1189 : OnConflictClause *onConflictClause)
1190 : {
1191 1182 : ParseNamespaceItem *exclNSItem = NULL;
1192 : List *arbiterElems;
1193 : Node *arbiterWhere;
1194 : Oid arbiterConstraint;
1195 1182 : List *onConflictSet = NIL;
1196 1182 : Node *onConflictWhere = NULL;
1197 1182 : int exclRelIndex = 0;
1198 1182 : List *exclRelTlist = NIL;
1199 : OnConflictExpr *result;
1200 :
1201 : /*
1202 : * If this is ON CONFLICT DO SELECT/UPDATE, first create the range table
1203 : * entry for the EXCLUDED pseudo relation, so that that will be present
1204 : * while processing arbiter expressions. (You can't actually reference it
1205 : * from there, but this provides a useful error message if you try.)
1206 : */
1207 1182 : if (onConflictClause->action == ONCONFLICT_UPDATE ||
1208 482 : onConflictClause->action == ONCONFLICT_SELECT)
1209 : {
1210 880 : Relation targetrel = pstate->p_target_relation;
1211 : RangeTblEntry *exclRte;
1212 :
1213 880 : exclNSItem = addRangeTableEntryForRelation(pstate,
1214 : targetrel,
1215 : RowExclusiveLock,
1216 : makeAlias("excluded", NIL),
1217 : false, false);
1218 880 : exclRte = exclNSItem->p_rte;
1219 880 : exclRelIndex = exclNSItem->p_rtindex;
1220 :
1221 : /*
1222 : * relkind is set to composite to signal that we're not dealing with
1223 : * an actual relation, and no permission checks are required on it.
1224 : * (We'll check the actual target relation, instead.)
1225 : */
1226 880 : exclRte->relkind = RELKIND_COMPOSITE_TYPE;
1227 :
1228 : /* Create EXCLUDED rel's targetlist for use by EXPLAIN */
1229 880 : exclRelTlist = BuildOnConflictExcludedTargetlist(targetrel,
1230 : exclRelIndex);
1231 : }
1232 :
1233 : /* Process the arbiter clause, ON CONFLICT ON (...) */
1234 1182 : transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
1235 : &arbiterWhere, &arbiterConstraint);
1236 :
1237 : /* Process DO SELECT/UPDATE */
1238 1167 : if (onConflictClause->action == ONCONFLICT_UPDATE ||
1239 473 : onConflictClause->action == ONCONFLICT_SELECT)
1240 : {
1241 : /*
1242 : * Add the EXCLUDED pseudo relation to the query namespace, making it
1243 : * available in SET and WHERE subexpressions.
1244 : */
1245 874 : addNSItemToQuery(pstate, exclNSItem, false, true, true);
1246 :
1247 : /* Process the UPDATE SET clause */
1248 874 : if (onConflictClause->action == ONCONFLICT_UPDATE)
1249 : onConflictSet =
1250 694 : transformUpdateTargetList(pstate, onConflictClause->targetList);
1251 :
1252 : /* Process the SELECT/UPDATE WHERE clause */
1253 859 : onConflictWhere = transformWhereClause(pstate,
1254 : onConflictClause->whereClause,
1255 : EXPR_KIND_WHERE, "WHERE");
1256 :
1257 : /*
1258 : * Remove the EXCLUDED pseudo relation from the query namespace, since
1259 : * it's not supposed to be available in RETURNING. (Maybe someday we
1260 : * could allow that, and drop this step.)
1261 : */
1262 : Assert((ParseNamespaceItem *) llast(pstate->p_namespace) == exclNSItem);
1263 859 : pstate->p_namespace = list_delete_last(pstate->p_namespace);
1264 : }
1265 :
1266 : /* Finally, build ON CONFLICT DO [NOTHING | SELECT | UPDATE] expression */
1267 1152 : result = makeNode(OnConflictExpr);
1268 :
1269 1152 : result->action = onConflictClause->action;
1270 1152 : result->arbiterElems = arbiterElems;
1271 1152 : result->arbiterWhere = arbiterWhere;
1272 1152 : result->constraint = arbiterConstraint;
1273 1152 : result->lockStrength = onConflictClause->lockStrength;
1274 1152 : result->onConflictSet = onConflictSet;
1275 1152 : result->onConflictWhere = onConflictWhere;
1276 1152 : result->exclRelIndex = exclRelIndex;
1277 1152 : result->exclRelTlist = exclRelTlist;
1278 :
1279 1152 : return result;
1280 : }
1281 :
1282 :
1283 : /*
1284 : * BuildOnConflictExcludedTargetlist
1285 : * Create target list for the EXCLUDED pseudo-relation of ON CONFLICT,
1286 : * representing the columns of targetrel with varno exclRelIndex.
1287 : *
1288 : * Note: Exported for use in the rewriter.
1289 : */
1290 : List *
1291 991 : BuildOnConflictExcludedTargetlist(Relation targetrel,
1292 : Index exclRelIndex)
1293 : {
1294 991 : List *result = NIL;
1295 : int attno;
1296 : Var *var;
1297 : TargetEntry *te;
1298 :
1299 : /*
1300 : * Note that resnos of the tlist must correspond to attnos of the
1301 : * underlying relation, hence we need entries for dropped columns too.
1302 : */
1303 3540 : for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1304 : {
1305 2549 : Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1306 : char *name;
1307 :
1308 2549 : if (attr->attisdropped)
1309 : {
1310 : /*
1311 : * can't use atttypid here, but it doesn't really matter what type
1312 : * the Const claims to be.
1313 : */
1314 56 : var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1315 56 : name = NULL;
1316 : }
1317 : else
1318 : {
1319 2493 : var = makeVar(exclRelIndex, attno + 1,
1320 : attr->atttypid, attr->atttypmod,
1321 : attr->attcollation,
1322 : 0);
1323 2493 : name = pstrdup(NameStr(attr->attname));
1324 : }
1325 :
1326 2549 : te = makeTargetEntry((Expr *) var,
1327 2549 : attno + 1,
1328 : name,
1329 : false);
1330 :
1331 2549 : result = lappend(result, te);
1332 : }
1333 :
1334 : /*
1335 : * Add a whole-row-Var entry to support references to "EXCLUDED.*". Like
1336 : * the other entries in the EXCLUDED tlist, its resno must match the Var's
1337 : * varattno, else the wrong things happen while resolving references in
1338 : * setrefs.c. This is against normal conventions for targetlists, but
1339 : * it's okay since we don't use this as a real tlist.
1340 : */
1341 991 : var = makeVar(exclRelIndex, InvalidAttrNumber,
1342 991 : targetrel->rd_rel->reltype,
1343 : -1, InvalidOid, 0);
1344 991 : te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1345 991 : result = lappend(result, te);
1346 :
1347 991 : return result;
1348 : }
1349 :
1350 :
1351 : /*
1352 : * count_rowexpr_columns -
1353 : * get number of columns contained in a ROW() expression;
1354 : * return -1 if expression isn't a RowExpr or a Var referencing one.
1355 : *
1356 : * This is currently used only for hint purposes, so we aren't terribly
1357 : * tense about recognizing all possible cases. The Var case is interesting
1358 : * because that's what we'll get in the INSERT ... SELECT (...) case.
1359 : */
1360 : static int
1361 0 : count_rowexpr_columns(ParseState *pstate, Node *expr)
1362 : {
1363 0 : if (expr == NULL)
1364 0 : return -1;
1365 0 : if (IsA(expr, RowExpr))
1366 0 : return list_length(((RowExpr *) expr)->args);
1367 0 : if (IsA(expr, Var))
1368 : {
1369 0 : Var *var = (Var *) expr;
1370 0 : AttrNumber attnum = var->varattno;
1371 :
1372 0 : if (attnum > 0 && var->vartype == RECORDOID)
1373 : {
1374 : RangeTblEntry *rte;
1375 :
1376 0 : rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1377 0 : if (rte->rtekind == RTE_SUBQUERY)
1378 : {
1379 : /* Subselect-in-FROM: examine sub-select's output expr */
1380 0 : TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1381 : attnum);
1382 :
1383 0 : if (ste == NULL || ste->resjunk)
1384 0 : return -1;
1385 0 : expr = (Node *) ste->expr;
1386 0 : if (IsA(expr, RowExpr))
1387 0 : return list_length(((RowExpr *) expr)->args);
1388 : }
1389 : }
1390 : }
1391 0 : return -1;
1392 : }
1393 :
1394 :
1395 : /*
1396 : * transformSelectStmt -
1397 : * transforms a Select Statement
1398 : *
1399 : * This function is also used to transform the source expression of a
1400 : * PLAssignStmt. In that usage, passthru is non-NULL and we need to
1401 : * call transformPLAssignStmtTarget after the initial transformation of the
1402 : * SELECT's targetlist. (We could generalize this into an arbitrary callback
1403 : * function, but for now that would just be more notation with no benefit.)
1404 : * All the rest is the same as a regular SelectStmt.
1405 : *
1406 : * Note: this covers only cases with no set operations and no VALUES lists;
1407 : * see below for the other cases.
1408 : */
1409 : static Query *
1410 256824 : transformSelectStmt(ParseState *pstate, SelectStmt *stmt,
1411 : SelectStmtPassthrough *passthru)
1412 : {
1413 256824 : Query *qry = makeNode(Query);
1414 : Node *qual;
1415 : ListCell *l;
1416 :
1417 256824 : qry->commandType = CMD_SELECT;
1418 :
1419 : /* process the WITH clause independently of all else */
1420 256824 : if (stmt->withClause)
1421 : {
1422 1429 : qry->hasRecursive = stmt->withClause->recursive;
1423 1429 : qry->cteList = transformWithClause(pstate, stmt->withClause);
1424 1281 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1425 : }
1426 :
1427 : /* Complain if we get called from someplace where INTO is not allowed */
1428 256676 : if (stmt->intoClause)
1429 9 : ereport(ERROR,
1430 : (errcode(ERRCODE_SYNTAX_ERROR),
1431 : errmsg("SELECT ... INTO is not allowed here"),
1432 : parser_errposition(pstate,
1433 : exprLocation((Node *) stmt->intoClause))));
1434 :
1435 : /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
1436 256667 : pstate->p_locking_clause = stmt->lockingClause;
1437 :
1438 : /* make WINDOW info available for window functions, too */
1439 256667 : pstate->p_windowdefs = stmt->windowClause;
1440 :
1441 : /* process the FROM clause */
1442 256667 : transformFromClause(pstate, stmt->fromClause);
1443 :
1444 : /* transform targetlist */
1445 256316 : qry->targetList = transformTargetList(pstate, stmt->targetList,
1446 : EXPR_KIND_SELECT_TARGET);
1447 :
1448 : /*
1449 : * If we're within a PLAssignStmt, do further transformation of the
1450 : * targetlist; that has to happen before we consider sorting or grouping.
1451 : * Otherwise, mark column origins (which are useless in a PLAssignStmt).
1452 : */
1453 253607 : if (passthru)
1454 2779 : qry->targetList = transformPLAssignStmtTarget(pstate, qry->targetList,
1455 : passthru);
1456 : else
1457 250828 : markTargetListOrigins(pstate, qry->targetList);
1458 :
1459 : /* transform WHERE */
1460 253600 : qual = transformWhereClause(pstate, stmt->whereClause,
1461 : EXPR_KIND_WHERE, "WHERE");
1462 :
1463 : /* initial processing of HAVING clause is much like WHERE clause */
1464 253546 : qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
1465 : EXPR_KIND_HAVING, "HAVING");
1466 :
1467 : /*
1468 : * Transform sorting/grouping stuff. Do ORDER BY first because both
1469 : * transformGroupClause and transformDistinctClause need the results. Note
1470 : * that these functions can also change the targetList, so it's passed to
1471 : * them by reference.
1472 : */
1473 253543 : qry->sortClause = transformSortClause(pstate,
1474 : stmt->sortClause,
1475 : &qry->targetList,
1476 : EXPR_KIND_ORDER_BY,
1477 : false /* allow SQL92 rules */ );
1478 :
1479 507044 : qry->groupClause = transformGroupClause(pstate,
1480 : stmt->groupClause,
1481 253528 : stmt->groupByAll,
1482 : &qry->groupingSets,
1483 : &qry->targetList,
1484 : qry->sortClause,
1485 : EXPR_KIND_GROUP_BY,
1486 : false /* allow SQL92 rules */ );
1487 253516 : qry->groupDistinct = stmt->groupDistinct;
1488 253516 : qry->groupByAll = stmt->groupByAll;
1489 :
1490 253516 : if (stmt->distinctClause == NIL)
1491 : {
1492 251271 : qry->distinctClause = NIL;
1493 251271 : qry->hasDistinctOn = false;
1494 : }
1495 2245 : else if (linitial(stmt->distinctClause) == NULL)
1496 : {
1497 : /* We had SELECT DISTINCT */
1498 2113 : qry->distinctClause = transformDistinctClause(pstate,
1499 : &qry->targetList,
1500 : qry->sortClause,
1501 : false);
1502 2113 : qry->hasDistinctOn = false;
1503 : }
1504 : else
1505 : {
1506 : /* We had SELECT DISTINCT ON */
1507 132 : qry->distinctClause = transformDistinctOnClause(pstate,
1508 : stmt->distinctClause,
1509 : &qry->targetList,
1510 : qry->sortClause);
1511 126 : qry->hasDistinctOn = true;
1512 : }
1513 :
1514 : /* transform LIMIT */
1515 253510 : qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1516 : EXPR_KIND_OFFSET, "OFFSET",
1517 : stmt->limitOption);
1518 253510 : qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1519 : EXPR_KIND_LIMIT, "LIMIT",
1520 : stmt->limitOption);
1521 253504 : qry->limitOption = stmt->limitOption;
1522 :
1523 : /* transform window clauses after we have seen all window functions */
1524 253504 : qry->windowClause = transformWindowDefinitions(pstate,
1525 : pstate->p_windowdefs,
1526 : &qry->targetList);
1527 :
1528 : /* resolve any still-unresolved output columns as being type text */
1529 253471 : if (pstate->p_resolve_unknowns)
1530 231118 : resolveTargetListUnknowns(pstate, qry->targetList);
1531 :
1532 253471 : qry->rtable = pstate->p_rtable;
1533 253471 : qry->rteperminfos = pstate->p_rteperminfos;
1534 253471 : qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1535 :
1536 253471 : qry->hasSubLinks = pstate->p_hasSubLinks;
1537 253471 : qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1538 253471 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1539 253471 : qry->hasAggs = pstate->p_hasAggs;
1540 :
1541 259020 : foreach(l, stmt->lockingClause)
1542 : {
1543 5570 : transformLockingClause(pstate, qry,
1544 5570 : (LockingClause *) lfirst(l), false);
1545 : }
1546 :
1547 253450 : assign_query_collations(pstate, qry);
1548 :
1549 : /* this must be done after collations, for reliable comparison of exprs */
1550 253429 : if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1551 22280 : parseCheckAggregates(pstate, qry);
1552 :
1553 253372 : return qry;
1554 : }
1555 :
1556 : /*
1557 : * transformValuesClause -
1558 : * transforms a VALUES clause that's being used as a standalone SELECT
1559 : *
1560 : * We build a Query containing a VALUES RTE, rather as if one had written
1561 : * SELECT * FROM (VALUES ...) AS "*VALUES*"
1562 : */
1563 : static Query *
1564 4335 : transformValuesClause(ParseState *pstate, SelectStmt *stmt)
1565 : {
1566 4335 : Query *qry = makeNode(Query);
1567 4335 : List *exprsLists = NIL;
1568 4335 : List *coltypes = NIL;
1569 4335 : List *coltypmods = NIL;
1570 4335 : List *colcollations = NIL;
1571 4335 : List **colexprs = NULL;
1572 4335 : int sublist_length = -1;
1573 4335 : bool lateral = false;
1574 : ParseNamespaceItem *nsitem;
1575 : ListCell *lc;
1576 : ListCell *lc2;
1577 : int i;
1578 :
1579 4335 : qry->commandType = CMD_SELECT;
1580 :
1581 : /* Most SELECT stuff doesn't apply in a VALUES clause */
1582 : Assert(stmt->distinctClause == NIL);
1583 : Assert(stmt->intoClause == NULL);
1584 : Assert(stmt->targetList == NIL);
1585 : Assert(stmt->fromClause == NIL);
1586 : Assert(stmt->whereClause == NULL);
1587 : Assert(stmt->groupClause == NIL);
1588 : Assert(stmt->havingClause == NULL);
1589 : Assert(stmt->windowClause == NIL);
1590 : Assert(stmt->op == SETOP_NONE);
1591 :
1592 : /* process the WITH clause independently of all else */
1593 4335 : if (stmt->withClause)
1594 : {
1595 30 : qry->hasRecursive = stmt->withClause->recursive;
1596 30 : qry->cteList = transformWithClause(pstate, stmt->withClause);
1597 27 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1598 : }
1599 :
1600 : /*
1601 : * For each row of VALUES, transform the raw expressions.
1602 : *
1603 : * Note that the intermediate representation we build is column-organized
1604 : * not row-organized. That simplifies the type and collation processing
1605 : * below.
1606 : */
1607 16021 : foreach(lc, stmt->valuesLists)
1608 : {
1609 11693 : List *sublist = (List *) lfirst(lc);
1610 :
1611 : /*
1612 : * Do basic expression transformation (same as a ROW() expr, but here
1613 : * we disallow SetToDefault)
1614 : */
1615 11693 : sublist = transformExpressionList(pstate, sublist,
1616 : EXPR_KIND_VALUES, false);
1617 :
1618 : /*
1619 : * All the sublists must be the same length, *after* transformation
1620 : * (which might expand '*' into multiple items). The VALUES RTE can't
1621 : * handle anything different.
1622 : */
1623 11689 : if (sublist_length < 0)
1624 : {
1625 : /* Remember post-transformation length of first sublist */
1626 4328 : sublist_length = list_length(sublist);
1627 : /* and allocate array for per-column lists */
1628 4328 : colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1629 : }
1630 7361 : else if (sublist_length != list_length(sublist))
1631 : {
1632 0 : ereport(ERROR,
1633 : (errcode(ERRCODE_SYNTAX_ERROR),
1634 : errmsg("VALUES lists must all be the same length"),
1635 : parser_errposition(pstate,
1636 : exprLocation((Node *) sublist))));
1637 : }
1638 :
1639 : /* Build per-column expression lists */
1640 11689 : i = 0;
1641 28096 : foreach(lc2, sublist)
1642 : {
1643 16407 : Node *col = (Node *) lfirst(lc2);
1644 :
1645 16407 : colexprs[i] = lappend(colexprs[i], col);
1646 16407 : i++;
1647 : }
1648 :
1649 : /* Release sub-list's cells to save memory */
1650 11689 : list_free(sublist);
1651 :
1652 : /* Prepare an exprsLists element for this row */
1653 11689 : exprsLists = lappend(exprsLists, NIL);
1654 : }
1655 :
1656 : /*
1657 : * Now resolve the common types of the columns, and coerce everything to
1658 : * those types. Then identify the common typmod and common collation, if
1659 : * any, of each column.
1660 : *
1661 : * We must do collation processing now because (1) assign_query_collations
1662 : * doesn't process rangetable entries, and (2) we need to label the VALUES
1663 : * RTE with column collations for use in the outer query. We don't
1664 : * consider conflict of implicit collations to be an error here; instead
1665 : * the column will just show InvalidOid as its collation, and you'll get a
1666 : * failure later if that results in failure to resolve a collation.
1667 : *
1668 : * Note we modify the per-column expression lists in-place.
1669 : */
1670 10057 : for (i = 0; i < sublist_length; i++)
1671 : {
1672 : Oid coltype;
1673 : int32 coltypmod;
1674 : Oid colcoll;
1675 :
1676 5729 : coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
1677 :
1678 22136 : foreach(lc, colexprs[i])
1679 : {
1680 16407 : Node *col = (Node *) lfirst(lc);
1681 :
1682 16407 : col = coerce_to_common_type(pstate, col, coltype, "VALUES");
1683 16407 : lfirst(lc) = col;
1684 : }
1685 :
1686 5729 : coltypmod = select_common_typmod(pstate, colexprs[i], coltype);
1687 5729 : colcoll = select_common_collation(pstate, colexprs[i], true);
1688 :
1689 5729 : coltypes = lappend_oid(coltypes, coltype);
1690 5729 : coltypmods = lappend_int(coltypmods, coltypmod);
1691 5729 : colcollations = lappend_oid(colcollations, colcoll);
1692 : }
1693 :
1694 : /*
1695 : * Finally, rearrange the coerced expressions into row-organized lists.
1696 : */
1697 10057 : for (i = 0; i < sublist_length; i++)
1698 : {
1699 22136 : forboth(lc, colexprs[i], lc2, exprsLists)
1700 : {
1701 16407 : Node *col = (Node *) lfirst(lc);
1702 16407 : List *sublist = lfirst(lc2);
1703 :
1704 16407 : sublist = lappend(sublist, col);
1705 16407 : lfirst(lc2) = sublist;
1706 : }
1707 5729 : list_free(colexprs[i]);
1708 : }
1709 :
1710 : /*
1711 : * Ordinarily there can't be any current-level Vars in the expression
1712 : * lists, because the namespace was empty ... but if we're inside CREATE
1713 : * RULE, then NEW/OLD references might appear. In that case we have to
1714 : * mark the VALUES RTE as LATERAL.
1715 : */
1716 4332 : if (pstate->p_rtable != NIL &&
1717 4 : contain_vars_of_level((Node *) exprsLists, 0))
1718 4 : lateral = true;
1719 :
1720 : /*
1721 : * Generate the VALUES RTE
1722 : */
1723 4328 : nsitem = addRangeTableEntryForValues(pstate, exprsLists,
1724 : coltypes, coltypmods, colcollations,
1725 : NULL, lateral, true);
1726 4328 : addNSItemToQuery(pstate, nsitem, true, true, true);
1727 :
1728 : /*
1729 : * Generate a targetlist as though expanding "*"
1730 : */
1731 : Assert(pstate->p_next_resno == 1);
1732 4328 : qry->targetList = expandNSItemAttrs(pstate, nsitem, 0, true, -1);
1733 :
1734 : /*
1735 : * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1736 : * VALUES, so cope.
1737 : */
1738 4328 : qry->sortClause = transformSortClause(pstate,
1739 : stmt->sortClause,
1740 : &qry->targetList,
1741 : EXPR_KIND_ORDER_BY,
1742 : false /* allow SQL92 rules */ );
1743 :
1744 4328 : qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1745 : EXPR_KIND_OFFSET, "OFFSET",
1746 : stmt->limitOption);
1747 4328 : qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1748 : EXPR_KIND_LIMIT, "LIMIT",
1749 : stmt->limitOption);
1750 4328 : qry->limitOption = stmt->limitOption;
1751 :
1752 4328 : if (stmt->lockingClause)
1753 0 : ereport(ERROR,
1754 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1755 : /*------
1756 : translator: %s is a SQL row locking clause such as FOR UPDATE */
1757 : errmsg("%s cannot be applied to VALUES",
1758 : LCS_asString(((LockingClause *)
1759 : linitial(stmt->lockingClause))->strength))));
1760 :
1761 4328 : qry->rtable = pstate->p_rtable;
1762 4328 : qry->rteperminfos = pstate->p_rteperminfos;
1763 4328 : qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1764 :
1765 4328 : qry->hasSubLinks = pstate->p_hasSubLinks;
1766 :
1767 4328 : assign_query_collations(pstate, qry);
1768 :
1769 4328 : return qry;
1770 : }
1771 :
1772 : /*
1773 : * transformSetOperationStmt -
1774 : * transforms a set-operations tree
1775 : *
1776 : * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
1777 : * structure to it. We must transform each leaf SELECT and build up a top-
1778 : * level Query that contains the leaf SELECTs as subqueries in its rangetable.
1779 : * The tree of set operations is converted into the setOperations field of
1780 : * the top-level Query.
1781 : */
1782 : static Query *
1783 6909 : transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
1784 : {
1785 6909 : Query *qry = makeNode(Query);
1786 : SelectStmt *leftmostSelect;
1787 : int leftmostRTI;
1788 : Query *leftmostQuery;
1789 : SetOperationStmt *sostmt;
1790 : List *sortClause;
1791 : Node *limitOffset;
1792 : Node *limitCount;
1793 : List *lockingClause;
1794 : WithClause *withClause;
1795 : Node *node;
1796 : ListCell *left_tlist,
1797 : *lct,
1798 : *lcm,
1799 : *lcc,
1800 : *l;
1801 : List *targetvars,
1802 : *targetnames,
1803 : *sv_namespace;
1804 : int sv_rtable_length;
1805 : ParseNamespaceItem *jnsitem;
1806 : ParseNamespaceColumn *sortnscolumns;
1807 : int sortcolindex;
1808 : int tllen;
1809 :
1810 6909 : qry->commandType = CMD_SELECT;
1811 :
1812 : /*
1813 : * Find leftmost leaf SelectStmt. We currently only need to do this in
1814 : * order to deliver a suitable error message if there's an INTO clause
1815 : * there, implying the set-op tree is in a context that doesn't allow
1816 : * INTO. (transformSetOperationTree would throw error anyway, but it
1817 : * seems worth the trouble to throw a different error for non-leftmost
1818 : * INTO, so we produce that error in transformSetOperationTree.)
1819 : */
1820 6909 : leftmostSelect = stmt->larg;
1821 10624 : while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1822 3715 : leftmostSelect = leftmostSelect->larg;
1823 : Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1824 : leftmostSelect->larg == NULL);
1825 6909 : if (leftmostSelect->intoClause)
1826 0 : ereport(ERROR,
1827 : (errcode(ERRCODE_SYNTAX_ERROR),
1828 : errmsg("SELECT ... INTO is not allowed here"),
1829 : parser_errposition(pstate,
1830 : exprLocation((Node *) leftmostSelect->intoClause))));
1831 :
1832 : /*
1833 : * We need to extract ORDER BY and other top-level clauses here and not
1834 : * let transformSetOperationTree() see them --- else it'll just recurse
1835 : * right back here!
1836 : */
1837 6909 : sortClause = stmt->sortClause;
1838 6909 : limitOffset = stmt->limitOffset;
1839 6909 : limitCount = stmt->limitCount;
1840 6909 : lockingClause = stmt->lockingClause;
1841 6909 : withClause = stmt->withClause;
1842 :
1843 6909 : stmt->sortClause = NIL;
1844 6909 : stmt->limitOffset = NULL;
1845 6909 : stmt->limitCount = NULL;
1846 6909 : stmt->lockingClause = NIL;
1847 6909 : stmt->withClause = NULL;
1848 :
1849 : /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1850 6909 : if (lockingClause)
1851 3 : ereport(ERROR,
1852 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1853 : /*------
1854 : translator: %s is a SQL row locking clause such as FOR UPDATE */
1855 : errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1856 : LCS_asString(((LockingClause *)
1857 : linitial(lockingClause))->strength))));
1858 :
1859 : /* Process the WITH clause independently of all else */
1860 6906 : if (withClause)
1861 : {
1862 152 : qry->hasRecursive = withClause->recursive;
1863 152 : qry->cteList = transformWithClause(pstate, withClause);
1864 152 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1865 : }
1866 :
1867 : /*
1868 : * Recursively transform the components of the tree.
1869 : */
1870 6906 : sostmt = castNode(SetOperationStmt,
1871 : transformSetOperationTree(pstate, stmt, true, NULL));
1872 : Assert(sostmt);
1873 6870 : qry->setOperations = (Node *) sostmt;
1874 :
1875 : /*
1876 : * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1877 : */
1878 6870 : node = sostmt->larg;
1879 10576 : while (node && IsA(node, SetOperationStmt))
1880 3706 : node = ((SetOperationStmt *) node)->larg;
1881 : Assert(node && IsA(node, RangeTblRef));
1882 6870 : leftmostRTI = ((RangeTblRef *) node)->rtindex;
1883 6870 : leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1884 : Assert(leftmostQuery != NULL);
1885 :
1886 : /*
1887 : * Generate dummy targetlist for outer query using column names of
1888 : * leftmost select and common datatypes/collations of topmost set
1889 : * operation. Also make lists of the dummy vars and their names for use
1890 : * in parsing ORDER BY.
1891 : *
1892 : * Note: we use leftmostRTI as the varno of the dummy variables. It
1893 : * shouldn't matter too much which RT index they have, as long as they
1894 : * have one that corresponds to a real RT entry; else funny things may
1895 : * happen when the tree is mashed by rule rewriting.
1896 : */
1897 6870 : qry->targetList = NIL;
1898 6870 : targetvars = NIL;
1899 6870 : targetnames = NIL;
1900 : sortnscolumns = (ParseNamespaceColumn *)
1901 6870 : palloc0(list_length(sostmt->colTypes) * sizeof(ParseNamespaceColumn));
1902 6870 : sortcolindex = 0;
1903 :
1904 24169 : forfour(lct, sostmt->colTypes,
1905 : lcm, sostmt->colTypmods,
1906 : lcc, sostmt->colCollations,
1907 : left_tlist, leftmostQuery->targetList)
1908 : {
1909 17299 : Oid colType = lfirst_oid(lct);
1910 17299 : int32 colTypmod = lfirst_int(lcm);
1911 17299 : Oid colCollation = lfirst_oid(lcc);
1912 17299 : TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1913 : char *colName;
1914 : TargetEntry *tle;
1915 : Var *var;
1916 :
1917 : Assert(!lefttle->resjunk);
1918 17299 : colName = pstrdup(lefttle->resname);
1919 17299 : var = makeVar(leftmostRTI,
1920 17299 : lefttle->resno,
1921 : colType,
1922 : colTypmod,
1923 : colCollation,
1924 : 0);
1925 17299 : var->location = exprLocation((Node *) lefttle->expr);
1926 17299 : tle = makeTargetEntry((Expr *) var,
1927 17299 : (AttrNumber) pstate->p_next_resno++,
1928 : colName,
1929 : false);
1930 17299 : qry->targetList = lappend(qry->targetList, tle);
1931 17299 : targetvars = lappend(targetvars, var);
1932 17299 : targetnames = lappend(targetnames, makeString(colName));
1933 17299 : sortnscolumns[sortcolindex].p_varno = leftmostRTI;
1934 17299 : sortnscolumns[sortcolindex].p_varattno = lefttle->resno;
1935 17299 : sortnscolumns[sortcolindex].p_vartype = colType;
1936 17299 : sortnscolumns[sortcolindex].p_vartypmod = colTypmod;
1937 17299 : sortnscolumns[sortcolindex].p_varcollid = colCollation;
1938 17299 : sortnscolumns[sortcolindex].p_varnosyn = leftmostRTI;
1939 17299 : sortnscolumns[sortcolindex].p_varattnosyn = lefttle->resno;
1940 17299 : sortcolindex++;
1941 : }
1942 :
1943 : /*
1944 : * As a first step towards supporting sort clauses that are expressions
1945 : * using the output columns, generate a namespace entry that makes the
1946 : * output columns visible. A Join RTE node is handy for this, since we
1947 : * can easily control the Vars generated upon matches.
1948 : *
1949 : * Note: we don't yet do anything useful with such cases, but at least
1950 : * "ORDER BY upper(foo)" will draw the right error message rather than
1951 : * "foo not found".
1952 : */
1953 6870 : sv_rtable_length = list_length(pstate->p_rtable);
1954 :
1955 6870 : jnsitem = addRangeTableEntryForJoin(pstate,
1956 : targetnames,
1957 : sortnscolumns,
1958 : JOIN_INNER,
1959 : 0,
1960 : targetvars,
1961 : NIL,
1962 : NIL,
1963 : NULL,
1964 : NULL,
1965 : false);
1966 :
1967 6870 : sv_namespace = pstate->p_namespace;
1968 6870 : pstate->p_namespace = NIL;
1969 :
1970 : /* add jnsitem to column namespace only */
1971 6870 : addNSItemToQuery(pstate, jnsitem, false, false, true);
1972 :
1973 : /*
1974 : * For now, we don't support resjunk sort clauses on the output of a
1975 : * setOperation tree --- you can only use the SQL92-spec options of
1976 : * selecting an output column by name or number. Enforce by checking that
1977 : * transformSortClause doesn't add any items to tlist. Note, if changing
1978 : * this, add_setop_child_rel_equivalences() will need to be updated.
1979 : */
1980 6870 : tllen = list_length(qry->targetList);
1981 :
1982 6870 : qry->sortClause = transformSortClause(pstate,
1983 : sortClause,
1984 : &qry->targetList,
1985 : EXPR_KIND_ORDER_BY,
1986 : false /* allow SQL92 rules */ );
1987 :
1988 : /* restore namespace, remove join RTE from rtable */
1989 6867 : pstate->p_namespace = sv_namespace;
1990 6867 : pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1991 :
1992 6867 : if (tllen != list_length(qry->targetList))
1993 0 : ereport(ERROR,
1994 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1995 : errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
1996 : errdetail("Only result column names can be used, not expressions or functions."),
1997 : errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
1998 : parser_errposition(pstate,
1999 : exprLocation(list_nth(qry->targetList, tllen)))));
2000 :
2001 6867 : qry->limitOffset = transformLimitClause(pstate, limitOffset,
2002 : EXPR_KIND_OFFSET, "OFFSET",
2003 : stmt->limitOption);
2004 6867 : qry->limitCount = transformLimitClause(pstate, limitCount,
2005 : EXPR_KIND_LIMIT, "LIMIT",
2006 : stmt->limitOption);
2007 6867 : qry->limitOption = stmt->limitOption;
2008 :
2009 6867 : qry->rtable = pstate->p_rtable;
2010 6867 : qry->rteperminfos = pstate->p_rteperminfos;
2011 6867 : qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2012 :
2013 6867 : qry->hasSubLinks = pstate->p_hasSubLinks;
2014 6867 : qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2015 6867 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2016 6867 : qry->hasAggs = pstate->p_hasAggs;
2017 :
2018 6867 : foreach(l, lockingClause)
2019 : {
2020 0 : transformLockingClause(pstate, qry,
2021 0 : (LockingClause *) lfirst(l), false);
2022 : }
2023 :
2024 6867 : assign_query_collations(pstate, qry);
2025 :
2026 : /* this must be done after collations, for reliable comparison of exprs */
2027 6867 : if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
2028 0 : parseCheckAggregates(pstate, qry);
2029 :
2030 6867 : return qry;
2031 : }
2032 :
2033 : /*
2034 : * Make a SortGroupClause node for a SetOperationStmt's groupClauses
2035 : *
2036 : * If require_hash is true, the caller is indicating that they need hash
2037 : * support or they will fail. So look extra hard for hash support.
2038 : */
2039 : SortGroupClause *
2040 13871 : makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
2041 : {
2042 13871 : SortGroupClause *grpcl = makeNode(SortGroupClause);
2043 : Oid sortop;
2044 : Oid eqop;
2045 : bool hashable;
2046 :
2047 : /* determine the eqop and optional sortop */
2048 13871 : get_sort_group_operators(rescoltype,
2049 : false, true, false,
2050 : &sortop, &eqop, NULL,
2051 : &hashable);
2052 :
2053 : /*
2054 : * The type cache doesn't believe that record is hashable (see
2055 : * cache_record_field_properties()), but if the caller really needs hash
2056 : * support, we can assume it does. Worst case, if any components of the
2057 : * record don't support hashing, we will fail at execution.
2058 : */
2059 13871 : if (require_hash && (rescoltype == RECORDOID || rescoltype == RECORDARRAYOID))
2060 12 : hashable = true;
2061 :
2062 : /* we don't have a tlist yet, so can't assign sortgrouprefs */
2063 13871 : grpcl->tleSortGroupRef = 0;
2064 13871 : grpcl->eqop = eqop;
2065 13871 : grpcl->sortop = sortop;
2066 13871 : grpcl->reverse_sort = false; /* Sort-op is "less than", or InvalidOid */
2067 13871 : grpcl->nulls_first = false; /* OK with or without sortop */
2068 13871 : grpcl->hashable = hashable;
2069 :
2070 13871 : return grpcl;
2071 : }
2072 :
2073 : /*
2074 : * transformSetOperationTree
2075 : * Recursively transform leaves and internal nodes of a set-op tree
2076 : *
2077 : * In addition to returning the transformed node, if targetlist isn't NULL
2078 : * then we return a list of its non-resjunk TargetEntry nodes. For a leaf
2079 : * set-op node these are the actual targetlist entries; otherwise they are
2080 : * dummy entries created to carry the type, typmod, collation, and location
2081 : * (for error messages) of each output column of the set-op node. This info
2082 : * is needed only during the internal recursion of this function, so outside
2083 : * callers pass NULL for targetlist. Note: the reason for passing the
2084 : * actual targetlist entries of a leaf node is so that upper levels can
2085 : * replace UNKNOWN Consts with properly-coerced constants.
2086 : */
2087 : static Node *
2088 28205 : transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
2089 : bool isTopLevel, List **targetlist)
2090 : {
2091 : bool isLeaf;
2092 :
2093 : Assert(stmt && IsA(stmt, SelectStmt));
2094 :
2095 : /* Guard against stack overflow due to overly complex set-expressions */
2096 28205 : check_stack_depth();
2097 :
2098 : /*
2099 : * Validity-check both leaf and internal SELECTs for disallowed ops.
2100 : */
2101 28205 : if (stmt->intoClause)
2102 0 : ereport(ERROR,
2103 : (errcode(ERRCODE_SYNTAX_ERROR),
2104 : errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
2105 : parser_errposition(pstate,
2106 : exprLocation((Node *) stmt->intoClause))));
2107 :
2108 : /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
2109 28205 : if (stmt->lockingClause)
2110 0 : ereport(ERROR,
2111 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2112 : /*------
2113 : translator: %s is a SQL row locking clause such as FOR UPDATE */
2114 : errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2115 : LCS_asString(((LockingClause *)
2116 : linitial(stmt->lockingClause))->strength))));
2117 :
2118 : /*
2119 : * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
2120 : * or WITH clauses attached, we need to treat it like a leaf node to
2121 : * generate an independent sub-Query tree. Otherwise, it can be
2122 : * represented by a SetOperationStmt node underneath the parent Query.
2123 : */
2124 28205 : if (stmt->op == SETOP_NONE)
2125 : {
2126 : Assert(stmt->larg == NULL && stmt->rarg == NULL);
2127 17524 : isLeaf = true;
2128 : }
2129 : else
2130 : {
2131 : Assert(stmt->larg != NULL && stmt->rarg != NULL);
2132 10681 : if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
2133 10669 : stmt->lockingClause || stmt->withClause)
2134 30 : isLeaf = true;
2135 : else
2136 10651 : isLeaf = false;
2137 : }
2138 :
2139 28205 : if (isLeaf)
2140 : {
2141 : /* Process leaf SELECT */
2142 : Query *selectQuery;
2143 : ParseNamespaceItem *nsitem;
2144 : RangeTblRef *rtr;
2145 :
2146 : /*
2147 : * Transform SelectStmt into a Query.
2148 : *
2149 : * This works the same as SELECT transformation normally would, except
2150 : * that we prevent resolving unknown-type outputs as TEXT. This does
2151 : * not change the subquery's semantics since if the column type
2152 : * matters semantically, it would have been resolved to something else
2153 : * anyway. Doing this lets us resolve such outputs using
2154 : * select_common_type(), below.
2155 : *
2156 : * Note: previously transformed sub-queries don't affect the parsing
2157 : * of this sub-query, because they are not in the toplevel pstate's
2158 : * namespace list.
2159 : */
2160 17554 : selectQuery = parse_sub_analyze((Node *) stmt, pstate,
2161 : NULL, false, false);
2162 :
2163 : /*
2164 : * Check for bogus references to Vars on the current query level (but
2165 : * upper-level references are okay). Normally this can't happen
2166 : * because the namespace will be empty, but it could happen if we are
2167 : * inside a rule.
2168 : */
2169 17539 : if (pstate->p_namespace)
2170 : {
2171 0 : if (contain_vars_of_level((Node *) selectQuery, 1))
2172 0 : ereport(ERROR,
2173 : (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2174 : errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
2175 : parser_errposition(pstate,
2176 : locate_var_of_level((Node *) selectQuery, 1))));
2177 : }
2178 :
2179 : /*
2180 : * Extract a list of the non-junk TLEs for upper-level processing.
2181 : */
2182 17539 : if (targetlist)
2183 : {
2184 : ListCell *tl;
2185 :
2186 17539 : *targetlist = NIL;
2187 68823 : foreach(tl, selectQuery->targetList)
2188 : {
2189 51284 : TargetEntry *tle = (TargetEntry *) lfirst(tl);
2190 :
2191 51284 : if (!tle->resjunk)
2192 51278 : *targetlist = lappend(*targetlist, tle);
2193 : }
2194 : }
2195 :
2196 : /*
2197 : * Make the leaf query be a subquery in the top-level rangetable.
2198 : */
2199 17539 : nsitem = addRangeTableEntryForSubquery(pstate,
2200 : selectQuery,
2201 : NULL,
2202 : false,
2203 : false);
2204 :
2205 : /*
2206 : * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
2207 : */
2208 17539 : rtr = makeNode(RangeTblRef);
2209 17539 : rtr->rtindex = nsitem->p_rtindex;
2210 17539 : return (Node *) rtr;
2211 : }
2212 : else
2213 : {
2214 : /* Process an internal node (set operation node) */
2215 10651 : SetOperationStmt *op = makeNode(SetOperationStmt);
2216 : List *ltargetlist;
2217 : List *rtargetlist;
2218 : const char *context;
2219 11333 : bool recursive = (pstate->p_parent_cte &&
2220 682 : pstate->p_parent_cte->cterecursive);
2221 :
2222 11024 : context = (stmt->op == SETOP_UNION ? "UNION" :
2223 373 : (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
2224 : "EXCEPT"));
2225 :
2226 10651 : op->op = stmt->op;
2227 10651 : op->all = stmt->all;
2228 :
2229 : /*
2230 : * Recursively transform the left child node.
2231 : */
2232 10651 : op->larg = transformSetOperationTree(pstate, stmt->larg,
2233 : false,
2234 : <argetlist);
2235 :
2236 : /*
2237 : * If we are processing a recursive union query, now is the time to
2238 : * examine the non-recursive term's output columns and mark the
2239 : * containing CTE as having those result columns. We should do this
2240 : * only at the topmost setop of the CTE, of course.
2241 : */
2242 10648 : if (isTopLevel && recursive)
2243 610 : determineRecursiveColTypes(pstate, op->larg, ltargetlist);
2244 :
2245 : /*
2246 : * Recursively transform the right child node.
2247 : */
2248 10648 : op->rarg = transformSetOperationTree(pstate, stmt->rarg,
2249 : false,
2250 : &rtargetlist);
2251 :
2252 10636 : constructSetOpTargetlist(pstate, op, ltargetlist, rtargetlist, targetlist,
2253 : context, recursive);
2254 :
2255 10615 : return (Node *) op;
2256 : }
2257 : }
2258 :
2259 : /*
2260 : * constructSetOpTargetlist
2261 : * Compute the types, typmods and collations of the columns in the target
2262 : * list of the given set operation.
2263 : *
2264 : * For every pair of columns in the targetlists of the children, compute the
2265 : * common type, typmod, and collation representing the output (UNION) column.
2266 : * If targetlist is not NULL, also build the dummy output targetlist
2267 : * containing non-resjunk output columns. The values are stored into the
2268 : * given SetOperationStmt node. context is a string for error messages
2269 : * ("UNION" etc.). recursive is true if it is a recursive union.
2270 : */
2271 : void
2272 10926 : constructSetOpTargetlist(ParseState *pstate, SetOperationStmt *op,
2273 : const List *ltargetlist, const List *rtargetlist,
2274 : List **targetlist, const char *context, bool recursive)
2275 : {
2276 : ListCell *ltl;
2277 : ListCell *rtl;
2278 :
2279 : /*
2280 : * Verify that the two children have the same number of non-junk columns,
2281 : * and determine the types of the merged output columns.
2282 : */
2283 10926 : if (list_length(ltargetlist) != list_length(rtargetlist))
2284 0 : ereport(ERROR,
2285 : (errcode(ERRCODE_SYNTAX_ERROR),
2286 : errmsg("each %s query must have the same number of columns",
2287 : context),
2288 : parser_errposition(pstate,
2289 : exprLocation((const Node *) rtargetlist))));
2290 :
2291 10926 : if (targetlist)
2292 3942 : *targetlist = NIL;
2293 10926 : op->colTypes = NIL;
2294 10926 : op->colTypmods = NIL;
2295 10926 : op->colCollations = NIL;
2296 10926 : op->groupClauses = NIL;
2297 :
2298 45577 : forboth(ltl, ltargetlist, rtl, rtargetlist)
2299 : {
2300 34672 : TargetEntry *ltle = (TargetEntry *) lfirst(ltl);
2301 34672 : TargetEntry *rtle = (TargetEntry *) lfirst(rtl);
2302 34672 : Node *lcolnode = (Node *) ltle->expr;
2303 34672 : Node *rcolnode = (Node *) rtle->expr;
2304 34672 : Oid lcoltype = exprType(lcolnode);
2305 34672 : Oid rcoltype = exprType(rcolnode);
2306 : Node *bestexpr;
2307 : int bestlocation;
2308 : Oid rescoltype;
2309 : int32 rescoltypmod;
2310 : Oid rescolcoll;
2311 :
2312 : /* select common type, same as CASE et al */
2313 34672 : rescoltype = select_common_type(pstate,
2314 34672 : list_make2(lcolnode, rcolnode),
2315 : context,
2316 : &bestexpr);
2317 34672 : bestlocation = exprLocation(bestexpr);
2318 :
2319 : /*
2320 : * Verify the coercions are actually possible. If not, we'd fail
2321 : * later anyway, but we want to fail now while we have sufficient
2322 : * context to produce an error cursor position.
2323 : *
2324 : * For all non-UNKNOWN-type cases, we verify coercibility but we don't
2325 : * modify the child's expression, for fear of changing the child
2326 : * query's semantics.
2327 : *
2328 : * If a child expression is an UNKNOWN-type Const or Param, we want to
2329 : * replace it with the coerced expression. This can only happen when
2330 : * the child is a leaf set-op node. It's safe to replace the
2331 : * expression because if the child query's semantics depended on the
2332 : * type of this output column, it'd have already coerced the UNKNOWN
2333 : * to something else. We want to do this because (a) we want to
2334 : * verify that a Const is valid for the target type, or resolve the
2335 : * actual type of an UNKNOWN Param, and (b) we want to avoid
2336 : * unnecessary discrepancies between the output type of the child
2337 : * query and the resolved target type. Such a discrepancy would
2338 : * disable optimization in the planner.
2339 : *
2340 : * If it's some other UNKNOWN-type node, eg a Var, we do nothing
2341 : * (knowing that coerce_to_common_type would fail). The planner is
2342 : * sometimes able to fold an UNKNOWN Var to a constant before it has
2343 : * to coerce the type, so failing now would just break cases that
2344 : * might work.
2345 : */
2346 34672 : if (lcoltype != UNKNOWNOID)
2347 31419 : lcolnode = coerce_to_common_type(pstate, lcolnode,
2348 : rescoltype, context);
2349 3253 : else if (IsA(lcolnode, Const) ||
2350 0 : IsA(lcolnode, Param))
2351 : {
2352 3253 : lcolnode = coerce_to_common_type(pstate, lcolnode,
2353 : rescoltype, context);
2354 3253 : ltle->expr = (Expr *) lcolnode;
2355 : }
2356 :
2357 34672 : if (rcoltype != UNKNOWNOID)
2358 30945 : rcolnode = coerce_to_common_type(pstate, rcolnode,
2359 : rescoltype, context);
2360 3727 : else if (IsA(rcolnode, Const) ||
2361 0 : IsA(rcolnode, Param))
2362 : {
2363 3727 : rcolnode = coerce_to_common_type(pstate, rcolnode,
2364 : rescoltype, context);
2365 3724 : rtle->expr = (Expr *) rcolnode;
2366 : }
2367 :
2368 34669 : rescoltypmod = select_common_typmod(pstate,
2369 34669 : list_make2(lcolnode, rcolnode),
2370 : rescoltype);
2371 :
2372 : /*
2373 : * Select common collation. A common collation is required for all
2374 : * set operators except UNION ALL; see SQL:2008 7.13 <query
2375 : * expression> Syntax Rule 15c. (If we fail to identify a common
2376 : * collation for a UNION ALL column, the colCollations element will be
2377 : * set to InvalidOid, which may result in a runtime error if something
2378 : * at a higher query level wants to use the column's collation.)
2379 : */
2380 34669 : rescolcoll = select_common_collation(pstate,
2381 34669 : list_make2(lcolnode, rcolnode),
2382 34669 : (op->op == SETOP_UNION && op->all));
2383 :
2384 : /* emit results */
2385 34651 : op->colTypes = lappend_oid(op->colTypes, rescoltype);
2386 34651 : op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
2387 34651 : op->colCollations = lappend_oid(op->colCollations, rescolcoll);
2388 :
2389 : /*
2390 : * For all cases except UNION ALL, identify the grouping operators
2391 : * (and, if available, sorting operators) that will be used to
2392 : * eliminate duplicates.
2393 : */
2394 34651 : if (op->op != SETOP_UNION || !op->all)
2395 : {
2396 : ParseCallbackState pcbstate;
2397 :
2398 13859 : setup_parser_errposition_callback(&pcbstate, pstate,
2399 : bestlocation);
2400 :
2401 : /* If it's a recursive union, we need to require hashing support. */
2402 13859 : op->groupClauses = lappend(op->groupClauses,
2403 13859 : makeSortGroupClauseForSetOp(rescoltype, recursive));
2404 :
2405 13859 : cancel_parser_errposition_callback(&pcbstate);
2406 : }
2407 :
2408 : /*
2409 : * Construct a dummy tlist entry to return. We use a SetToDefault
2410 : * node for the expression, since it carries exactly the fields
2411 : * needed, but any other expression node type would do as well.
2412 : */
2413 34651 : if (targetlist)
2414 : {
2415 17086 : SetToDefault *rescolnode = makeNode(SetToDefault);
2416 : TargetEntry *restle;
2417 :
2418 17086 : rescolnode->typeId = rescoltype;
2419 17086 : rescolnode->typeMod = rescoltypmod;
2420 17086 : rescolnode->collation = rescolcoll;
2421 17086 : rescolnode->location = bestlocation;
2422 17086 : restle = makeTargetEntry((Expr *) rescolnode,
2423 : 0, /* no need to set resno */
2424 : NULL,
2425 : false);
2426 17086 : *targetlist = lappend(*targetlist, restle);
2427 : }
2428 : }
2429 10905 : }
2430 :
2431 : /*
2432 : * Process the outputs of the non-recursive term of a recursive union
2433 : * to set up the parent CTE's columns
2434 : */
2435 : static void
2436 610 : determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
2437 : {
2438 : Node *node;
2439 : int leftmostRTI;
2440 : Query *leftmostQuery;
2441 : List *targetList;
2442 : ListCell *left_tlist;
2443 : ListCell *nrtl;
2444 : int next_resno;
2445 :
2446 : /*
2447 : * Find leftmost leaf SELECT
2448 : */
2449 610 : node = larg;
2450 613 : while (node && IsA(node, SetOperationStmt))
2451 3 : node = ((SetOperationStmt *) node)->larg;
2452 : Assert(node && IsA(node, RangeTblRef));
2453 610 : leftmostRTI = ((RangeTblRef *) node)->rtindex;
2454 610 : leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2455 : Assert(leftmostQuery != NULL);
2456 :
2457 : /*
2458 : * Generate dummy targetlist using column names of leftmost select and
2459 : * dummy result expressions of the non-recursive term.
2460 : */
2461 610 : targetList = NIL;
2462 610 : next_resno = 1;
2463 :
2464 1945 : forboth(nrtl, nrtargetlist, left_tlist, leftmostQuery->targetList)
2465 : {
2466 1335 : TargetEntry *nrtle = (TargetEntry *) lfirst(nrtl);
2467 1335 : TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
2468 : char *colName;
2469 : TargetEntry *tle;
2470 :
2471 : Assert(!lefttle->resjunk);
2472 1335 : colName = pstrdup(lefttle->resname);
2473 1335 : tle = makeTargetEntry(nrtle->expr,
2474 1335 : next_resno++,
2475 : colName,
2476 : false);
2477 1335 : targetList = lappend(targetList, tle);
2478 : }
2479 :
2480 : /* Now build CTE's output column info using dummy targetlist */
2481 610 : analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
2482 610 : }
2483 :
2484 :
2485 : /*
2486 : * transformReturnStmt -
2487 : * transforms a return statement
2488 : */
2489 : static Query *
2490 2485 : transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
2491 : {
2492 2485 : Query *qry = makeNode(Query);
2493 :
2494 2485 : qry->commandType = CMD_SELECT;
2495 2485 : qry->isReturn = true;
2496 :
2497 2485 : qry->targetList = list_make1(makeTargetEntry((Expr *) transformExpr(pstate, stmt->returnval, EXPR_KIND_SELECT_TARGET),
2498 : 1, NULL, false));
2499 :
2500 2482 : if (pstate->p_resolve_unknowns)
2501 2482 : resolveTargetListUnknowns(pstate, qry->targetList);
2502 2482 : qry->rtable = pstate->p_rtable;
2503 2482 : qry->rteperminfos = pstate->p_rteperminfos;
2504 2482 : qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2505 2482 : qry->hasSubLinks = pstate->p_hasSubLinks;
2506 2482 : qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2507 2482 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2508 2482 : qry->hasAggs = pstate->p_hasAggs;
2509 :
2510 2482 : assign_query_collations(pstate, qry);
2511 :
2512 2482 : return qry;
2513 : }
2514 :
2515 :
2516 : /*
2517 : * transformUpdateStmt -
2518 : * transforms an update statement
2519 : */
2520 : static Query *
2521 7479 : transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
2522 : {
2523 7479 : Query *qry = makeNode(Query);
2524 : ParseNamespaceItem *nsitem;
2525 : Node *qual;
2526 :
2527 7479 : qry->commandType = CMD_UPDATE;
2528 :
2529 : /* process the WITH clause independently of all else */
2530 7479 : if (stmt->withClause)
2531 : {
2532 43 : qry->hasRecursive = stmt->withClause->recursive;
2533 43 : qry->cteList = transformWithClause(pstate, stmt->withClause);
2534 43 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2535 : }
2536 :
2537 14957 : qry->resultRelation = setTargetTable(pstate, stmt->relation,
2538 7479 : stmt->relation->inh,
2539 : true,
2540 : ACL_UPDATE);
2541 7478 : nsitem = pstate->p_target_nsitem;
2542 :
2543 : /* subqueries in FROM cannot access the result relation */
2544 7478 : nsitem->p_lateral_only = true;
2545 7478 : nsitem->p_lateral_ok = false;
2546 :
2547 : /*
2548 : * the FROM clause is non-standard SQL syntax. We used to be able to do
2549 : * this with REPLACE in POSTQUEL so we keep the feature.
2550 : */
2551 7478 : transformFromClause(pstate, stmt->fromClause);
2552 :
2553 : /* remaining clauses can reference the result relation normally */
2554 7466 : nsitem->p_lateral_only = false;
2555 7466 : nsitem->p_lateral_ok = true;
2556 :
2557 7466 : qual = transformWhereClause(pstate, stmt->whereClause,
2558 : EXPR_KIND_WHERE, "WHERE");
2559 :
2560 7460 : transformReturningClause(pstate, qry, stmt->returningClause,
2561 : EXPR_KIND_RETURNING);
2562 :
2563 : /*
2564 : * Now we are done with SELECT-like processing, and can get on with
2565 : * transforming the target list to match the UPDATE target columns.
2566 : */
2567 7451 : qry->targetList = transformUpdateTargetList(pstate, stmt->targetList);
2568 :
2569 7427 : qry->rtable = pstate->p_rtable;
2570 7427 : qry->rteperminfos = pstate->p_rteperminfos;
2571 7427 : qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
2572 :
2573 7427 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2574 7427 : qry->hasSubLinks = pstate->p_hasSubLinks;
2575 :
2576 7427 : assign_query_collations(pstate, qry);
2577 :
2578 7427 : return qry;
2579 : }
2580 :
2581 : /*
2582 : * transformUpdateTargetList -
2583 : * handle SET clause in UPDATE/MERGE/INSERT ... ON CONFLICT UPDATE
2584 : */
2585 : List *
2586 8914 : transformUpdateTargetList(ParseState *pstate, List *origTlist)
2587 : {
2588 8914 : List *tlist = NIL;
2589 : RTEPermissionInfo *target_perminfo;
2590 : ListCell *orig_tl;
2591 : ListCell *tl;
2592 :
2593 8914 : tlist = transformTargetList(pstate, origTlist,
2594 : EXPR_KIND_UPDATE_SOURCE);
2595 :
2596 : /* Prepare to assign non-conflicting resnos to resjunk attributes */
2597 8890 : if (pstate->p_next_resno <= RelationGetNumberOfAttributes(pstate->p_target_relation))
2598 7537 : pstate->p_next_resno = RelationGetNumberOfAttributes(pstate->p_target_relation) + 1;
2599 :
2600 : /* Prepare non-junk columns for assignment to target table */
2601 8890 : target_perminfo = pstate->p_target_nsitem->p_perminfo;
2602 8890 : orig_tl = list_head(origTlist);
2603 :
2604 20135 : foreach(tl, tlist)
2605 : {
2606 11263 : TargetEntry *tle = (TargetEntry *) lfirst(tl);
2607 : ResTarget *origTarget;
2608 : int attrno;
2609 :
2610 11263 : if (tle->resjunk)
2611 : {
2612 : /*
2613 : * Resjunk nodes need no additional processing, but be sure they
2614 : * have resnos that do not match any target columns; else rewriter
2615 : * or planner might get confused. They don't need a resname
2616 : * either.
2617 : */
2618 69 : tle->resno = (AttrNumber) pstate->p_next_resno++;
2619 69 : tle->resname = NULL;
2620 69 : continue;
2621 : }
2622 11194 : if (orig_tl == NULL)
2623 0 : elog(ERROR, "UPDATE target count mismatch --- internal error");
2624 11194 : origTarget = lfirst_node(ResTarget, orig_tl);
2625 :
2626 11194 : attrno = attnameAttNum(pstate->p_target_relation,
2627 11194 : origTarget->name, true);
2628 11194 : if (attrno == InvalidAttrNumber)
2629 12 : ereport(ERROR,
2630 : (errcode(ERRCODE_UNDEFINED_COLUMN),
2631 : errmsg("column \"%s\" of relation \"%s\" does not exist",
2632 : origTarget->name,
2633 : RelationGetRelationName(pstate->p_target_relation)),
2634 : (origTarget->indirection != NIL &&
2635 : strcmp(origTarget->name, pstate->p_target_nsitem->p_names->aliasname) == 0) ?
2636 : errhint("SET target columns cannot be qualified with the relation name.") : 0,
2637 : parser_errposition(pstate, origTarget->location)));
2638 :
2639 11182 : updateTargetListEntry(pstate, tle, origTarget->name,
2640 : attrno,
2641 : origTarget->indirection,
2642 : origTarget->location);
2643 :
2644 : /* Mark the target column as requiring update permissions */
2645 11176 : target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
2646 : attrno - FirstLowInvalidHeapAttributeNumber);
2647 :
2648 11176 : orig_tl = lnext(origTlist, orig_tl);
2649 : }
2650 8872 : if (orig_tl != NULL)
2651 0 : elog(ERROR, "UPDATE target count mismatch --- internal error");
2652 :
2653 8872 : return tlist;
2654 : }
2655 :
2656 : /*
2657 : * addNSItemForReturning -
2658 : * add a ParseNamespaceItem for the OLD or NEW alias in RETURNING.
2659 : */
2660 : static void
2661 3555 : addNSItemForReturning(ParseState *pstate, const char *aliasname,
2662 : VarReturningType returning_type)
2663 : {
2664 : List *colnames;
2665 : int numattrs;
2666 : ParseNamespaceColumn *nscolumns;
2667 : ParseNamespaceItem *nsitem;
2668 :
2669 : /* copy per-column data from the target relation */
2670 3555 : colnames = pstate->p_target_nsitem->p_rte->eref->colnames;
2671 3555 : numattrs = list_length(colnames);
2672 :
2673 3555 : nscolumns = palloc_array(ParseNamespaceColumn, numattrs);
2674 :
2675 3555 : memcpy(nscolumns, pstate->p_target_nsitem->p_nscolumns,
2676 : numattrs * sizeof(ParseNamespaceColumn));
2677 :
2678 : /* mark all columns as returning OLD/NEW */
2679 13927 : for (int i = 0; i < numattrs; i++)
2680 10372 : nscolumns[i].p_varreturningtype = returning_type;
2681 :
2682 : /* build the nsitem, copying most fields from the target relation */
2683 3555 : nsitem = palloc_object(ParseNamespaceItem);
2684 3555 : nsitem->p_names = makeAlias(aliasname, colnames);
2685 3555 : nsitem->p_rte = pstate->p_target_nsitem->p_rte;
2686 3555 : nsitem->p_rtindex = pstate->p_target_nsitem->p_rtindex;
2687 3555 : nsitem->p_perminfo = pstate->p_target_nsitem->p_perminfo;
2688 3555 : nsitem->p_nscolumns = nscolumns;
2689 3555 : nsitem->p_returning_type = returning_type;
2690 :
2691 : /* add it to the query namespace as a table-only item */
2692 3555 : addNSItemToQuery(pstate, nsitem, false, true, false);
2693 3555 : }
2694 :
2695 : /*
2696 : * transformReturningClause -
2697 : * handle a RETURNING clause in INSERT/UPDATE/DELETE/MERGE
2698 : */
2699 : void
2700 11629 : transformReturningClause(ParseState *pstate, Query *qry,
2701 : ReturningClause *returningClause,
2702 : ParseExprKind exprKind)
2703 : {
2704 11629 : int save_nslen = list_length(pstate->p_namespace);
2705 : int save_next_resno;
2706 :
2707 11629 : if (returningClause == NULL)
2708 9817 : return; /* nothing to do */
2709 :
2710 : /*
2711 : * Scan RETURNING WITH(...) options for OLD/NEW alias names. Complain if
2712 : * there is any conflict with existing relations.
2713 : */
2714 3660 : foreach_node(ReturningOption, option, returningClause->options)
2715 : {
2716 60 : switch (option->option)
2717 : {
2718 27 : case RETURNING_OPTION_OLD:
2719 27 : if (qry->returningOldAlias != NULL)
2720 3 : ereport(ERROR,
2721 : errcode(ERRCODE_SYNTAX_ERROR),
2722 : /* translator: %s is OLD or NEW */
2723 : errmsg("%s cannot be specified multiple times", "OLD"),
2724 : parser_errposition(pstate, option->location));
2725 24 : qry->returningOldAlias = option->value;
2726 24 : break;
2727 :
2728 33 : case RETURNING_OPTION_NEW:
2729 33 : if (qry->returningNewAlias != NULL)
2730 3 : ereport(ERROR,
2731 : errcode(ERRCODE_SYNTAX_ERROR),
2732 : /* translator: %s is OLD or NEW */
2733 : errmsg("%s cannot be specified multiple times", "NEW"),
2734 : parser_errposition(pstate, option->location));
2735 30 : qry->returningNewAlias = option->value;
2736 30 : break;
2737 :
2738 0 : default:
2739 0 : elog(ERROR, "unrecognized returning option: %d", option->option);
2740 : }
2741 :
2742 54 : if (refnameNamespaceItem(pstate, NULL, option->value, -1, NULL) != NULL)
2743 6 : ereport(ERROR,
2744 : errcode(ERRCODE_DUPLICATE_ALIAS),
2745 : errmsg("table name \"%s\" specified more than once",
2746 : option->value),
2747 : parser_errposition(pstate, option->location));
2748 :
2749 48 : addNSItemForReturning(pstate, option->value,
2750 48 : option->option == RETURNING_OPTION_OLD ?
2751 : VAR_RETURNING_OLD : VAR_RETURNING_NEW);
2752 : }
2753 :
2754 : /*
2755 : * If OLD/NEW alias names weren't explicitly specified, use "old"/"new"
2756 : * unless masked by existing relations.
2757 : */
2758 3585 : if (qry->returningOldAlias == NULL &&
2759 1785 : refnameNamespaceItem(pstate, NULL, "old", -1, NULL) == NULL)
2760 : {
2761 1755 : qry->returningOldAlias = "old";
2762 1755 : addNSItemForReturning(pstate, "old", VAR_RETURNING_OLD);
2763 : }
2764 3582 : if (qry->returningNewAlias == NULL &&
2765 1782 : refnameNamespaceItem(pstate, NULL, "new", -1, NULL) == NULL)
2766 : {
2767 1752 : qry->returningNewAlias = "new";
2768 1752 : addNSItemForReturning(pstate, "new", VAR_RETURNING_NEW);
2769 : }
2770 :
2771 : /*
2772 : * We need to assign resnos starting at one in the RETURNING list. Save
2773 : * and restore the main tlist's value of p_next_resno, just in case
2774 : * someone looks at it later (probably won't happen).
2775 : */
2776 1800 : save_next_resno = pstate->p_next_resno;
2777 1800 : pstate->p_next_resno = 1;
2778 :
2779 : /* transform RETURNING expressions identically to a SELECT targetlist */
2780 1800 : qry->returningList = transformTargetList(pstate,
2781 : returningClause->exprs,
2782 : exprKind);
2783 :
2784 : /*
2785 : * Complain if the nonempty tlist expanded to nothing (which is possible
2786 : * if it contains only a star-expansion of a zero-column table). If we
2787 : * allow this, the parsed Query will look like it didn't have RETURNING,
2788 : * with results that would probably surprise the user.
2789 : */
2790 1779 : if (qry->returningList == NIL)
2791 3 : ereport(ERROR,
2792 : (errcode(ERRCODE_SYNTAX_ERROR),
2793 : errmsg("RETURNING must have at least one column"),
2794 : parser_errposition(pstate,
2795 : exprLocation(linitial(returningClause->exprs)))));
2796 :
2797 : /* mark column origins */
2798 1776 : markTargetListOrigins(pstate, qry->returningList);
2799 :
2800 : /* resolve any still-unresolved output columns as being type text */
2801 1776 : if (pstate->p_resolve_unknowns)
2802 1776 : resolveTargetListUnknowns(pstate, qry->returningList);
2803 :
2804 : /* restore state */
2805 1776 : pstate->p_namespace = list_truncate(pstate->p_namespace, save_nslen);
2806 1776 : pstate->p_next_resno = save_next_resno;
2807 : }
2808 :
2809 :
2810 : /*
2811 : * transformPLAssignStmt -
2812 : * transform a PL/pgSQL assignment statement
2813 : *
2814 : * If there is no opt_indirection, the transformed statement looks like
2815 : * "SELECT a_expr ...", except the expression has been cast to the type of
2816 : * the target. With indirection, it's still a SELECT, but the expression will
2817 : * incorporate FieldStore and/or assignment SubscriptingRef nodes to compute a
2818 : * new value for a container-type variable represented by the target. The
2819 : * expression references the target as the container source.
2820 : */
2821 : static Query *
2822 2785 : transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
2823 : {
2824 : Query *qry;
2825 2785 : ColumnRef *cref = makeNode(ColumnRef);
2826 2785 : List *indirection = stmt->indirection;
2827 2785 : int nnames = stmt->nnames;
2828 : Node *target;
2829 : SelectStmtPassthrough passthru;
2830 : bool save_resolve_unknowns;
2831 :
2832 : /*
2833 : * First, construct a ColumnRef for the target variable. If the target
2834 : * has more than one dotted name, we have to pull the extra names out of
2835 : * the indirection list.
2836 : */
2837 2785 : cref->fields = list_make1(makeString(stmt->name));
2838 2785 : cref->location = stmt->location;
2839 2785 : if (nnames > 1)
2840 : {
2841 : /* avoid munging the raw parsetree */
2842 203 : indirection = list_copy(indirection);
2843 413 : while (--nnames > 0 && indirection != NIL)
2844 : {
2845 210 : Node *ind = (Node *) linitial(indirection);
2846 :
2847 210 : if (!IsA(ind, String))
2848 0 : elog(ERROR, "invalid name count in PLAssignStmt");
2849 210 : cref->fields = lappend(cref->fields, ind);
2850 210 : indirection = list_delete_first(indirection);
2851 : }
2852 : }
2853 :
2854 : /*
2855 : * Transform the target reference. Typically we will get back a Param
2856 : * node, but there's no reason to be too picky about its type. (Note that
2857 : * we must do this before calling transformSelectStmt. It's tempting to
2858 : * do it inside transformPLAssignStmtTarget, but we need to do it before
2859 : * adding any FROM tables to the pstate's namespace, else we might wrongly
2860 : * resolve the target as a table column.)
2861 : */
2862 2785 : target = transformExpr(pstate, (Node *) cref,
2863 : EXPR_KIND_UPDATE_TARGET);
2864 :
2865 : /* Set up passthrough data for transformPLAssignStmtTarget */
2866 2779 : passthru.stmt = stmt;
2867 2779 : passthru.target = target;
2868 2779 : passthru.indirection = indirection;
2869 :
2870 : /*
2871 : * To avoid duplicating a lot of code, we use transformSelectStmt to do
2872 : * almost all of the work. However, we need to do additional processing
2873 : * on the SELECT's targetlist after it's been transformed, but before
2874 : * possible addition of targetlist items for ORDER BY or GROUP BY.
2875 : * transformSelectStmt knows it should call transformPLAssignStmtTarget if
2876 : * it's passed a passthru argument.
2877 : *
2878 : * Also, disable resolution of unknown-type tlist items; PL/pgSQL wants to
2879 : * deal with that itself.
2880 : */
2881 2779 : save_resolve_unknowns = pstate->p_resolve_unknowns;
2882 2779 : pstate->p_resolve_unknowns = false;
2883 2779 : qry = transformSelectStmt(pstate, stmt->val, &passthru);
2884 2772 : pstate->p_resolve_unknowns = save_resolve_unknowns;
2885 :
2886 2772 : return qry;
2887 : }
2888 :
2889 : /*
2890 : * Callback function to adjust a SELECT's tlist to make the output suitable
2891 : * for assignment to a PLAssignStmt's target variable.
2892 : *
2893 : * Note: we actually modify the tle->expr in-place, but the function's API
2894 : * is set up to not presume that.
2895 : */
2896 : static List *
2897 2779 : transformPLAssignStmtTarget(ParseState *pstate, List *tlist,
2898 : SelectStmtPassthrough *passthru)
2899 : {
2900 2779 : PLAssignStmt *stmt = passthru->stmt;
2901 2779 : Node *target = passthru->target;
2902 2779 : List *indirection = passthru->indirection;
2903 : Oid targettype;
2904 : int32 targettypmod;
2905 : Oid targetcollation;
2906 : TargetEntry *tle;
2907 : Oid type_id;
2908 :
2909 2779 : targettype = exprType(target);
2910 2779 : targettypmod = exprTypmod(target);
2911 2779 : targetcollation = exprCollation(target);
2912 :
2913 : /* we should have exactly one targetlist item */
2914 2779 : if (list_length(tlist) != 1)
2915 2 : ereport(ERROR,
2916 : (errcode(ERRCODE_SYNTAX_ERROR),
2917 : errmsg_plural("assignment source returned %d column",
2918 : "assignment source returned %d columns",
2919 : list_length(tlist),
2920 : list_length(tlist))));
2921 :
2922 2777 : tle = linitial_node(TargetEntry, tlist);
2923 :
2924 : /*
2925 : * This next bit is similar to transformAssignedExpr; the key difference
2926 : * is we use COERCION_PLPGSQL not COERCION_ASSIGNMENT.
2927 : */
2928 2777 : type_id = exprType((Node *) tle->expr);
2929 :
2930 2777 : pstate->p_expr_kind = EXPR_KIND_UPDATE_TARGET;
2931 :
2932 2777 : if (indirection)
2933 : {
2934 49 : tle->expr = (Expr *)
2935 54 : transformAssignmentIndirection(pstate,
2936 : target,
2937 54 : stmt->name,
2938 : false,
2939 : targettype,
2940 : targettypmod,
2941 : targetcollation,
2942 : indirection,
2943 : list_head(indirection),
2944 54 : (Node *) tle->expr,
2945 : COERCION_PLPGSQL,
2946 : exprLocation(target));
2947 : }
2948 2723 : else if (targettype != type_id &&
2949 828 : (targettype == RECORDOID || ISCOMPLEX(targettype)) &&
2950 225 : (type_id == RECORDOID || ISCOMPLEX(type_id)))
2951 : {
2952 : /*
2953 : * Hack: do not let coerce_to_target_type() deal with inconsistent
2954 : * composite types. Just pass the expression result through as-is,
2955 : * and let the PL/pgSQL executor do the conversion its way. This is
2956 : * rather bogus, but it's needed for backwards compatibility.
2957 : */
2958 : }
2959 : else
2960 : {
2961 : /*
2962 : * For normal non-qualified target column, do type checking and
2963 : * coercion.
2964 : */
2965 2537 : Node *orig_expr = (Node *) tle->expr;
2966 :
2967 2537 : tle->expr = (Expr *)
2968 2537 : coerce_to_target_type(pstate,
2969 : orig_expr, type_id,
2970 : targettype, targettypmod,
2971 : COERCION_PLPGSQL,
2972 : COERCE_IMPLICIT_CAST,
2973 : -1);
2974 : /* With COERCION_PLPGSQL, this error is probably unreachable */
2975 2537 : if (tle->expr == NULL)
2976 0 : ereport(ERROR,
2977 : (errcode(ERRCODE_DATATYPE_MISMATCH),
2978 : errmsg("variable \"%s\" is of type %s"
2979 : " but expression is of type %s",
2980 : stmt->name,
2981 : format_type_be(targettype),
2982 : format_type_be(type_id)),
2983 : errhint("You will need to rewrite or cast the expression."),
2984 : parser_errposition(pstate, exprLocation(orig_expr))));
2985 : }
2986 :
2987 2772 : pstate->p_expr_kind = EXPR_KIND_NONE;
2988 :
2989 2772 : return list_make1(tle);
2990 : }
2991 :
2992 :
2993 : /*
2994 : * transformDeclareCursorStmt -
2995 : * transform a DECLARE CURSOR Statement
2996 : *
2997 : * DECLARE CURSOR is like other utility statements in that we emit it as a
2998 : * CMD_UTILITY Query node; however, we must first transform the contained
2999 : * query. We used to postpone that until execution, but it's really necessary
3000 : * to do it during the normal parse analysis phase to ensure that side effects
3001 : * of parser hooks happen at the expected time.
3002 : */
3003 : static Query *
3004 2308 : transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
3005 : {
3006 : Query *result;
3007 : Query *query;
3008 :
3009 2308 : if ((stmt->options & CURSOR_OPT_SCROLL) &&
3010 120 : (stmt->options & CURSOR_OPT_NO_SCROLL))
3011 0 : ereport(ERROR,
3012 : (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
3013 : /* translator: %s is a SQL keyword */
3014 : errmsg("cannot specify both %s and %s",
3015 : "SCROLL", "NO SCROLL")));
3016 :
3017 2308 : if ((stmt->options & CURSOR_OPT_ASENSITIVE) &&
3018 0 : (stmt->options & CURSOR_OPT_INSENSITIVE))
3019 0 : ereport(ERROR,
3020 : (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
3021 : /* translator: %s is a SQL keyword */
3022 : errmsg("cannot specify both %s and %s",
3023 : "ASENSITIVE", "INSENSITIVE")));
3024 :
3025 : /* Transform contained query, not allowing SELECT INTO */
3026 2308 : query = transformStmt(pstate, stmt->query);
3027 2297 : stmt->query = (Node *) query;
3028 :
3029 : /* Grammar should not have allowed anything but SELECT */
3030 2297 : if (!IsA(query, Query) ||
3031 2297 : query->commandType != CMD_SELECT)
3032 0 : elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
3033 :
3034 : /*
3035 : * We also disallow data-modifying WITH in a cursor. (This could be
3036 : * allowed, but the semantics of when the updates occur might be
3037 : * surprising.)
3038 : */
3039 2297 : if (query->hasModifyingCTE)
3040 0 : ereport(ERROR,
3041 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3042 : errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
3043 :
3044 : /* FOR UPDATE and WITH HOLD are not compatible */
3045 2297 : if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
3046 0 : ereport(ERROR,
3047 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3048 : /*------
3049 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3050 : errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
3051 : LCS_asString(((RowMarkClause *)
3052 : linitial(query->rowMarks))->strength)),
3053 : errdetail("Holdable cursors must be READ ONLY.")));
3054 :
3055 : /* FOR UPDATE and SCROLL are not compatible */
3056 2297 : if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
3057 0 : ereport(ERROR,
3058 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3059 : /*------
3060 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3061 : errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
3062 : LCS_asString(((RowMarkClause *)
3063 : linitial(query->rowMarks))->strength)),
3064 : errdetail("Scrollable cursors must be READ ONLY.")));
3065 :
3066 : /* FOR UPDATE and INSENSITIVE are not compatible */
3067 2297 : if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
3068 0 : ereport(ERROR,
3069 : (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
3070 : /*------
3071 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3072 : errmsg("DECLARE INSENSITIVE CURSOR ... %s is not valid",
3073 : LCS_asString(((RowMarkClause *)
3074 : linitial(query->rowMarks))->strength)),
3075 : errdetail("Insensitive cursors must be READ ONLY.")));
3076 :
3077 : /* represent the command as a utility Query */
3078 2297 : result = makeNode(Query);
3079 2297 : result->commandType = CMD_UTILITY;
3080 2297 : result->utilityStmt = (Node *) stmt;
3081 :
3082 2297 : return result;
3083 : }
3084 :
3085 :
3086 : /*
3087 : * transformExplainStmt -
3088 : * transform an EXPLAIN Statement
3089 : *
3090 : * EXPLAIN is like other utility statements in that we emit it as a
3091 : * CMD_UTILITY Query node; however, we must first transform the contained
3092 : * query. We used to postpone that until execution, but it's really necessary
3093 : * to do it during the normal parse analysis phase to ensure that side effects
3094 : * of parser hooks happen at the expected time.
3095 : */
3096 : static Query *
3097 12719 : transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
3098 : {
3099 : Query *result;
3100 12719 : bool generic_plan = false;
3101 12719 : Oid *paramTypes = NULL;
3102 12719 : int numParams = 0;
3103 :
3104 : /*
3105 : * If we have no external source of parameter definitions, and the
3106 : * GENERIC_PLAN option is specified, then accept variable parameter
3107 : * definitions (similarly to PREPARE, for example).
3108 : */
3109 12719 : if (pstate->p_paramref_hook == NULL)
3110 : {
3111 : ListCell *lc;
3112 :
3113 25016 : foreach(lc, stmt->options)
3114 : {
3115 12306 : DefElem *opt = (DefElem *) lfirst(lc);
3116 :
3117 12306 : if (strcmp(opt->defname, "generic_plan") == 0)
3118 9 : generic_plan = defGetBoolean(opt);
3119 : /* don't "break", as we want the last value */
3120 : }
3121 12710 : if (generic_plan)
3122 9 : setup_parse_variable_parameters(pstate, ¶mTypes, &numParams);
3123 : }
3124 :
3125 : /* transform contained query, allowing SELECT INTO */
3126 12719 : stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
3127 :
3128 : /* make sure all is well with parameter types */
3129 12712 : if (generic_plan)
3130 9 : check_variable_parameters(pstate, (Query *) stmt->query);
3131 :
3132 : /* represent the command as a utility Query */
3133 12712 : result = makeNode(Query);
3134 12712 : result->commandType = CMD_UTILITY;
3135 12712 : result->utilityStmt = (Node *) stmt;
3136 :
3137 12712 : return result;
3138 : }
3139 :
3140 :
3141 : /*
3142 : * transformCreateTableAsStmt -
3143 : * transform a CREATE TABLE AS, SELECT ... INTO, or CREATE MATERIALIZED VIEW
3144 : * Statement
3145 : *
3146 : * As with DECLARE CURSOR and EXPLAIN, transform the contained statement now.
3147 : */
3148 : static Query *
3149 1038 : transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
3150 : {
3151 : Query *result;
3152 : Query *query;
3153 :
3154 : /* transform contained query, not allowing SELECT INTO */
3155 1038 : query = transformStmt(pstate, stmt->query);
3156 1036 : stmt->query = (Node *) query;
3157 :
3158 : /* additional work needed for CREATE MATERIALIZED VIEW */
3159 1036 : if (stmt->objtype == OBJECT_MATVIEW)
3160 : {
3161 : ObjectAddress temp_object;
3162 :
3163 : /*
3164 : * Prohibit a data-modifying CTE in the query used to create a
3165 : * materialized view. It's not sufficiently clear what the user would
3166 : * want to happen if the MV is refreshed or incrementally maintained.
3167 : */
3168 284 : if (query->hasModifyingCTE)
3169 0 : ereport(ERROR,
3170 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3171 : errmsg("materialized views must not use data-modifying statements in WITH")));
3172 :
3173 : /*
3174 : * Check whether any temporary database objects are used in the
3175 : * creation query. It would be hard to refresh data or incrementally
3176 : * maintain it if a source disappeared.
3177 : */
3178 284 : if (query_uses_temp_object(query, &temp_object))
3179 3 : ereport(ERROR,
3180 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3181 : errmsg("materialized views must not use temporary objects"),
3182 : errdetail("This view depends on temporary %s.",
3183 : getObjectDescription(&temp_object, false))));
3184 :
3185 : /*
3186 : * A materialized view would either need to save parameters for use in
3187 : * maintaining/loading the data or prohibit them entirely. The latter
3188 : * seems safer and more sane.
3189 : */
3190 278 : if (query_contains_extern_params(query))
3191 0 : ereport(ERROR,
3192 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3193 : errmsg("materialized views may not be defined using bound parameters")));
3194 :
3195 : /*
3196 : * For now, we disallow unlogged materialized views, because it seems
3197 : * like a bad idea for them to just go to empty after a crash. (If we
3198 : * could mark them as unpopulated, that would be better, but that
3199 : * requires catalog changes which crash recovery can't presently
3200 : * handle.)
3201 : */
3202 278 : if (stmt->into->rel->relpersistence == RELPERSISTENCE_UNLOGGED)
3203 0 : ereport(ERROR,
3204 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3205 : errmsg("materialized views cannot be unlogged")));
3206 :
3207 : /*
3208 : * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
3209 : * for purposes of creating the view's ON SELECT rule. We stash that
3210 : * in the IntoClause because that's where intorel_startup() can
3211 : * conveniently get it from.
3212 : */
3213 278 : stmt->into->viewQuery = copyObject(query);
3214 : }
3215 :
3216 : /* represent the command as a utility Query */
3217 1030 : result = makeNode(Query);
3218 1030 : result->commandType = CMD_UTILITY;
3219 1030 : result->utilityStmt = (Node *) stmt;
3220 :
3221 1030 : return result;
3222 : }
3223 :
3224 : /*
3225 : * transform a CallStmt
3226 : */
3227 : static Query *
3228 265 : transformCallStmt(ParseState *pstate, CallStmt *stmt)
3229 : {
3230 : List *targs;
3231 : ListCell *lc;
3232 : Node *node;
3233 : FuncExpr *fexpr;
3234 : HeapTuple proctup;
3235 : Datum proargmodes;
3236 : bool isNull;
3237 265 : List *outargs = NIL;
3238 : Query *result;
3239 :
3240 : /*
3241 : * First, do standard parse analysis on the procedure call and its
3242 : * arguments, allowing us to identify the called procedure.
3243 : */
3244 265 : targs = NIL;
3245 645 : foreach(lc, stmt->funccall->args)
3246 : {
3247 380 : targs = lappend(targs, transformExpr(pstate,
3248 380 : (Node *) lfirst(lc),
3249 : EXPR_KIND_CALL_ARGUMENT));
3250 : }
3251 :
3252 265 : node = ParseFuncOrColumn(pstate,
3253 265 : stmt->funccall->funcname,
3254 : targs,
3255 : pstate->p_last_srf,
3256 : stmt->funccall,
3257 : true,
3258 265 : stmt->funccall->location);
3259 :
3260 249 : assign_expr_collations(pstate, node);
3261 :
3262 249 : fexpr = castNode(FuncExpr, node);
3263 :
3264 249 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fexpr->funcid));
3265 249 : if (!HeapTupleIsValid(proctup))
3266 0 : elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
3267 :
3268 : /*
3269 : * Expand the argument list to deal with named-argument notation and
3270 : * default arguments. For ordinary FuncExprs this'd be done during
3271 : * planning, but a CallStmt doesn't go through planning, and there seems
3272 : * no good reason not to do it here.
3273 : */
3274 249 : fexpr->args = expand_function_arguments(fexpr->args,
3275 : true,
3276 : fexpr->funcresulttype,
3277 : proctup);
3278 :
3279 : /* Fetch proargmodes; if it's null, there are no output args */
3280 249 : proargmodes = SysCacheGetAttr(PROCOID, proctup,
3281 : Anum_pg_proc_proargmodes,
3282 : &isNull);
3283 249 : if (!isNull)
3284 : {
3285 : /*
3286 : * Split the list into input arguments in fexpr->args and output
3287 : * arguments in stmt->outargs. INOUT arguments appear in both lists.
3288 : */
3289 : ArrayType *arr;
3290 : int numargs;
3291 : char *argmodes;
3292 : List *inargs;
3293 : int i;
3294 :
3295 101 : arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
3296 101 : numargs = list_length(fexpr->args);
3297 101 : if (ARR_NDIM(arr) != 1 ||
3298 101 : ARR_DIMS(arr)[0] != numargs ||
3299 101 : ARR_HASNULL(arr) ||
3300 101 : ARR_ELEMTYPE(arr) != CHAROID)
3301 0 : elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
3302 : numargs);
3303 101 : argmodes = (char *) ARR_DATA_PTR(arr);
3304 :
3305 101 : inargs = NIL;
3306 101 : i = 0;
3307 338 : foreach(lc, fexpr->args)
3308 : {
3309 237 : Node *n = lfirst(lc);
3310 :
3311 237 : switch (argmodes[i])
3312 : {
3313 78 : case PROARGMODE_IN:
3314 : case PROARGMODE_VARIADIC:
3315 78 : inargs = lappend(inargs, n);
3316 78 : break;
3317 58 : case PROARGMODE_OUT:
3318 58 : outargs = lappend(outargs, n);
3319 58 : break;
3320 101 : case PROARGMODE_INOUT:
3321 101 : inargs = lappend(inargs, n);
3322 101 : outargs = lappend(outargs, copyObject(n));
3323 101 : break;
3324 0 : default:
3325 : /* note we don't support PROARGMODE_TABLE */
3326 0 : elog(ERROR, "invalid argmode %c for procedure",
3327 : argmodes[i]);
3328 : break;
3329 : }
3330 237 : i++;
3331 : }
3332 101 : fexpr->args = inargs;
3333 : }
3334 :
3335 249 : stmt->funcexpr = fexpr;
3336 249 : stmt->outargs = outargs;
3337 :
3338 249 : ReleaseSysCache(proctup);
3339 :
3340 : /* represent the command as a utility Query */
3341 249 : result = makeNode(Query);
3342 249 : result->commandType = CMD_UTILITY;
3343 249 : result->utilityStmt = (Node *) stmt;
3344 :
3345 249 : return result;
3346 : }
3347 :
3348 : /*
3349 : * Produce a string representation of a LockClauseStrength value.
3350 : * This should only be applied to valid values (not LCS_NONE).
3351 : */
3352 : const char *
3353 24 : LCS_asString(LockClauseStrength strength)
3354 : {
3355 24 : switch (strength)
3356 : {
3357 0 : case LCS_NONE:
3358 : Assert(false);
3359 0 : break;
3360 0 : case LCS_FORKEYSHARE:
3361 0 : return "FOR KEY SHARE";
3362 0 : case LCS_FORSHARE:
3363 0 : return "FOR SHARE";
3364 3 : case LCS_FORNOKEYUPDATE:
3365 3 : return "FOR NO KEY UPDATE";
3366 21 : case LCS_FORUPDATE:
3367 21 : return "FOR UPDATE";
3368 : }
3369 0 : return "FOR some"; /* shouldn't happen */
3370 : }
3371 :
3372 : /*
3373 : * Check for features that are not supported with FOR [KEY] UPDATE/SHARE.
3374 : *
3375 : * exported so planner can check again after rewriting, query pullup, etc
3376 : */
3377 : void
3378 12534 : CheckSelectLocking(Query *qry, LockClauseStrength strength)
3379 : {
3380 : Assert(strength != LCS_NONE); /* else caller error */
3381 :
3382 12534 : if (qry->setOperations)
3383 0 : ereport(ERROR,
3384 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3385 : /*------
3386 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3387 : errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3388 : LCS_asString(strength))));
3389 12534 : if (qry->distinctClause != NIL)
3390 0 : ereport(ERROR,
3391 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3392 : /*------
3393 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3394 : errmsg("%s is not allowed with DISTINCT clause",
3395 : LCS_asString(strength))));
3396 12534 : if (qry->groupClause != NIL || qry->groupingSets != NIL)
3397 6 : ereport(ERROR,
3398 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3399 : /*------
3400 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3401 : errmsg("%s is not allowed with GROUP BY clause",
3402 : LCS_asString(strength))));
3403 12528 : if (qry->havingQual != NULL)
3404 0 : ereport(ERROR,
3405 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3406 : /*------
3407 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3408 : errmsg("%s is not allowed with HAVING clause",
3409 : LCS_asString(strength))));
3410 12528 : if (qry->hasAggs)
3411 3 : ereport(ERROR,
3412 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3413 : /*------
3414 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3415 : errmsg("%s is not allowed with aggregate functions",
3416 : LCS_asString(strength))));
3417 12525 : if (qry->hasWindowFuncs)
3418 0 : ereport(ERROR,
3419 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3420 : /*------
3421 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3422 : errmsg("%s is not allowed with window functions",
3423 : LCS_asString(strength))));
3424 12525 : if (qry->hasTargetSRFs)
3425 0 : ereport(ERROR,
3426 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3427 : /*------
3428 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3429 : errmsg("%s is not allowed with set-returning functions in the target list",
3430 : LCS_asString(strength))));
3431 12525 : }
3432 :
3433 : /*
3434 : * Transform a FOR [KEY] UPDATE/SHARE clause
3435 : *
3436 : * This basically involves replacing names by integer relids.
3437 : *
3438 : * NB: if you need to change this, see also markQueryForLocking()
3439 : * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
3440 : */
3441 : static void
3442 5576 : transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
3443 : bool pushedDown)
3444 : {
3445 5576 : List *lockedRels = lc->lockedRels;
3446 : ListCell *l;
3447 : ListCell *rt;
3448 : Index i;
3449 : LockingClause *allrels;
3450 :
3451 5576 : CheckSelectLocking(qry, lc->strength);
3452 :
3453 : /* make a clause we can pass down to subqueries to select all rels */
3454 5567 : allrels = makeNode(LockingClause);
3455 5567 : allrels->lockedRels = NIL; /* indicates all rels */
3456 5567 : allrels->strength = lc->strength;
3457 5567 : allrels->waitPolicy = lc->waitPolicy;
3458 :
3459 5567 : if (lockedRels == NIL)
3460 : {
3461 : /*
3462 : * Lock all regular tables used in query and its subqueries. We
3463 : * examine inFromCl to exclude auto-added RTEs, particularly NEW/OLD
3464 : * in rules. This is a bit of an abuse of a mostly-obsolete flag, but
3465 : * it's convenient. We can't rely on the namespace mechanism that has
3466 : * largely replaced inFromCl, since for example we need to lock
3467 : * base-relation RTEs even if they are masked by upper joins.
3468 : */
3469 3683 : i = 0;
3470 7412 : foreach(rt, qry->rtable)
3471 : {
3472 3729 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3473 :
3474 3729 : ++i;
3475 3729 : if (!rte->inFromCl)
3476 6 : continue;
3477 3723 : switch (rte->rtekind)
3478 : {
3479 3708 : case RTE_RELATION:
3480 : {
3481 : RTEPermissionInfo *perminfo;
3482 :
3483 3708 : applyLockingClause(qry, i,
3484 : lc->strength,
3485 : lc->waitPolicy,
3486 : pushedDown);
3487 3708 : perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3488 3708 : perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3489 : }
3490 3708 : break;
3491 0 : case RTE_SUBQUERY:
3492 0 : applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
3493 : pushedDown);
3494 :
3495 : /*
3496 : * FOR UPDATE/SHARE of subquery is propagated to all of
3497 : * subquery's rels, too. We could do this later (based on
3498 : * the marking of the subquery RTE) but it is convenient
3499 : * to have local knowledge in each query level about which
3500 : * rels need to be opened with RowShareLock.
3501 : */
3502 0 : transformLockingClause(pstate, rte->subquery,
3503 : allrels, true);
3504 0 : break;
3505 15 : default:
3506 : /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
3507 15 : break;
3508 : }
3509 : }
3510 : }
3511 : else
3512 : {
3513 : /*
3514 : * Lock just the named tables. As above, we allow locking any base
3515 : * relation regardless of alias-visibility rules, so we need to
3516 : * examine inFromCl to exclude OLD/NEW.
3517 : */
3518 3762 : foreach(l, lockedRels)
3519 : {
3520 1890 : RangeVar *thisrel = (RangeVar *) lfirst(l);
3521 :
3522 : /* For simplicity we insist on unqualified alias names here */
3523 1890 : if (thisrel->catalogname || thisrel->schemaname)
3524 0 : ereport(ERROR,
3525 : (errcode(ERRCODE_SYNTAX_ERROR),
3526 : /*------
3527 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3528 : errmsg("%s must specify unqualified relation names",
3529 : LCS_asString(lc->strength)),
3530 : parser_errposition(pstate, thisrel->location)));
3531 :
3532 1890 : i = 0;
3533 2064 : foreach(rt, qry->rtable)
3534 : {
3535 2058 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3536 2058 : char *rtename = rte->eref->aliasname;
3537 :
3538 2058 : ++i;
3539 2058 : if (!rte->inFromCl)
3540 12 : continue;
3541 :
3542 : /*
3543 : * A join RTE without an alias is not visible as a relation
3544 : * name and needs to be skipped (otherwise it might hide a
3545 : * base relation with the same name), except if it has a USING
3546 : * alias, which *is* visible.
3547 : *
3548 : * Subquery and values RTEs without aliases are never visible
3549 : * as relation names and must always be skipped.
3550 : */
3551 2046 : if (rte->alias == NULL)
3552 : {
3553 93 : if (rte->rtekind == RTE_JOIN)
3554 : {
3555 37 : if (rte->join_using_alias == NULL)
3556 31 : continue;
3557 6 : rtename = rte->join_using_alias->aliasname;
3558 : }
3559 56 : else if (rte->rtekind == RTE_SUBQUERY ||
3560 53 : rte->rtekind == RTE_VALUES)
3561 3 : continue;
3562 : }
3563 :
3564 2012 : if (strcmp(rtename, thisrel->relname) == 0)
3565 : {
3566 1884 : switch (rte->rtekind)
3567 : {
3568 1872 : case RTE_RELATION:
3569 : {
3570 : RTEPermissionInfo *perminfo;
3571 :
3572 1872 : applyLockingClause(qry, i,
3573 : lc->strength,
3574 : lc->waitPolicy,
3575 : pushedDown);
3576 1872 : perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3577 1872 : perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3578 : }
3579 1872 : break;
3580 6 : case RTE_SUBQUERY:
3581 6 : applyLockingClause(qry, i, lc->strength,
3582 : lc->waitPolicy, pushedDown);
3583 : /* see comment above */
3584 6 : transformLockingClause(pstate, rte->subquery,
3585 : allrels, true);
3586 6 : break;
3587 6 : case RTE_JOIN:
3588 6 : ereport(ERROR,
3589 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3590 : /*------
3591 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3592 : errmsg("%s cannot be applied to a join",
3593 : LCS_asString(lc->strength)),
3594 : parser_errposition(pstate, thisrel->location)));
3595 : break;
3596 0 : case RTE_FUNCTION:
3597 0 : ereport(ERROR,
3598 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3599 : /*------
3600 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3601 : errmsg("%s cannot be applied to a function",
3602 : LCS_asString(lc->strength)),
3603 : parser_errposition(pstate, thisrel->location)));
3604 : break;
3605 0 : case RTE_TABLEFUNC:
3606 0 : ereport(ERROR,
3607 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3608 : /*------
3609 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3610 : errmsg("%s cannot be applied to a table function",
3611 : LCS_asString(lc->strength)),
3612 : parser_errposition(pstate, thisrel->location)));
3613 : break;
3614 0 : case RTE_VALUES:
3615 0 : ereport(ERROR,
3616 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3617 : /*------
3618 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3619 : errmsg("%s cannot be applied to VALUES",
3620 : LCS_asString(lc->strength)),
3621 : parser_errposition(pstate, thisrel->location)));
3622 : break;
3623 0 : case RTE_CTE:
3624 0 : ereport(ERROR,
3625 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3626 : /*------
3627 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3628 : errmsg("%s cannot be applied to a WITH query",
3629 : LCS_asString(lc->strength)),
3630 : parser_errposition(pstate, thisrel->location)));
3631 : break;
3632 0 : case RTE_NAMEDTUPLESTORE:
3633 0 : ereport(ERROR,
3634 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3635 : /*------
3636 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3637 : errmsg("%s cannot be applied to a named tuplestore",
3638 : LCS_asString(lc->strength)),
3639 : parser_errposition(pstate, thisrel->location)));
3640 : break;
3641 :
3642 : /* Shouldn't be possible to see RTE_RESULT here */
3643 :
3644 0 : default:
3645 0 : elog(ERROR, "unrecognized RTE type: %d",
3646 : (int) rte->rtekind);
3647 : break;
3648 : }
3649 1878 : break; /* out of foreach loop */
3650 : }
3651 : }
3652 1884 : if (rt == NULL)
3653 6 : ereport(ERROR,
3654 : (errcode(ERRCODE_UNDEFINED_TABLE),
3655 : /*------
3656 : translator: %s is a SQL row locking clause such as FOR UPDATE */
3657 : errmsg("relation \"%s\" in %s clause not found in FROM clause",
3658 : thisrel->relname,
3659 : LCS_asString(lc->strength)),
3660 : parser_errposition(pstate, thisrel->location)));
3661 : }
3662 : }
3663 5555 : }
3664 :
3665 : /*
3666 : * Record locking info for a single rangetable item
3667 : */
3668 : void
3669 5634 : applyLockingClause(Query *qry, Index rtindex,
3670 : LockClauseStrength strength, LockWaitPolicy waitPolicy,
3671 : bool pushedDown)
3672 : {
3673 : RowMarkClause *rc;
3674 :
3675 : Assert(strength != LCS_NONE); /* else caller error */
3676 :
3677 : /* If it's an explicit clause, make sure hasForUpdate gets set */
3678 5634 : if (!pushedDown)
3679 5584 : qry->hasForUpdate = true;
3680 :
3681 : /* Check for pre-existing entry for same rtindex */
3682 5634 : if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
3683 : {
3684 : /*
3685 : * If the same RTE is specified with more than one locking strength,
3686 : * use the strongest. (Reasonable, since you can't take both a shared
3687 : * and exclusive lock at the same time; it'll end up being exclusive
3688 : * anyway.)
3689 : *
3690 : * Similarly, if the same RTE is specified with more than one lock
3691 : * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
3692 : * turn wins over waiting for the lock (the default). This is a bit
3693 : * more debatable but raising an error doesn't seem helpful. (Consider
3694 : * for instance SELECT FOR UPDATE NOWAIT from a view that internally
3695 : * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
3696 : * LOCKED is reasonable since the former throws an error in case of
3697 : * coming across a locked tuple, which may be undesirable in some
3698 : * cases but it seems better than silently returning inconsistent
3699 : * results.
3700 : *
3701 : * And of course pushedDown becomes false if any clause is explicit.
3702 : */
3703 0 : rc->strength = Max(rc->strength, strength);
3704 0 : rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
3705 0 : rc->pushedDown &= pushedDown;
3706 0 : return;
3707 : }
3708 :
3709 : /* Make a new RowMarkClause */
3710 5634 : rc = makeNode(RowMarkClause);
3711 5634 : rc->rti = rtindex;
3712 5634 : rc->strength = strength;
3713 5634 : rc->waitPolicy = waitPolicy;
3714 5634 : rc->pushedDown = pushedDown;
3715 5634 : qry->rowMarks = lappend(qry->rowMarks, rc);
3716 : }
3717 :
3718 : #ifdef DEBUG_NODE_TESTS_ENABLED
3719 : /*
3720 : * Coverage testing for raw_expression_tree_walker().
3721 : *
3722 : * When enabled, we run raw_expression_tree_walker() over every DML statement
3723 : * submitted to parse analysis. Without this provision, that function is only
3724 : * applied in limited cases involving CTEs, and we don't really want to have
3725 : * to test everything inside as well as outside a CTE.
3726 : */
3727 : static bool
3728 14537810 : test_raw_expression_coverage(Node *node, void *context)
3729 : {
3730 14537810 : if (node == NULL)
3731 7791975 : return false;
3732 6745835 : return raw_expression_tree_walker(node,
3733 : test_raw_expression_coverage,
3734 : context);
3735 : }
3736 : #endif /* DEBUG_NODE_TESTS_ENABLED */
|