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