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