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