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