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