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