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