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,
83 const ForPortionOfClause *forPortionOf,
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 /* disallow DELETE ... WHERE CURRENT OF on a view */
599 if (stmt->whereClause &&
600 IsA(stmt->whereClause, CurrentOfExpr) &&
601 pstate->p_target_relation->rd_rel->relkind == RELKIND_VIEW)
604 errmsg("WHERE CURRENT OF on a view is not implemented"));
605
606 /* there's no DISTINCT in DELETE */
607 qry->distinctClause = NIL;
608
609 /* subqueries in USING cannot access the result relation */
610 nsitem->p_lateral_only = true;
611 nsitem->p_lateral_ok = false;
612
613 /*
614 * The USING clause is non-standard SQL syntax, and is equivalent in
615 * functionality to the FROM list that can be specified for UPDATE. The
616 * USING keyword is used rather than FROM because FROM is already a
617 * keyword in the DELETE syntax.
618 */
619 transformFromClause(pstate, stmt->usingClause);
620
621 /* remaining clauses can reference the result relation normally */
622 nsitem->p_lateral_only = false;
623 nsitem->p_lateral_ok = true;
624
625 if (stmt->forPortionOf)
627 qry->resultRelation,
629 false);
630
631 qual = transformWhereClause(pstate, stmt->whereClause,
632 EXPR_KIND_WHERE, "WHERE");
633
634 transformReturningClause(pstate, qry, stmt->returningClause,
636
637 /* done building the range table and jointree */
638 qry->rtable = pstate->p_rtable;
639 qry->rteperminfos = pstate->p_rteperminfos;
640 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
641
642 qry->hasSubLinks = pstate->p_hasSubLinks;
643 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
644 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
645 qry->hasAggs = pstate->p_hasAggs;
646
647 assign_query_collations(pstate, qry);
648
649 /* this must be done after collations, for reliable comparison of exprs */
650 if (pstate->p_hasAggs)
651 parseCheckAggregates(pstate, qry);
652
653 return qry;
654}
655
656/*
657 * transformInsertStmt -
658 * transform an Insert Statement
659 */
660static Query *
662{
663 Query *qry = makeNode(Query);
664 SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
665 List *exprList = NIL;
666 bool isGeneralSelect;
670 List *icolumns;
671 List *attrnos;
676 ListCell *lc;
679
680 /* There can't be any outer WITH to worry about */
681 Assert(pstate->p_ctenamespace == NIL);
682
683 qry->commandType = CMD_INSERT;
684
685 /* process the WITH clause independently of all else */
686 if (stmt->withClause)
687 {
688 qry->hasRecursive = stmt->withClause->recursive;
689 qry->cteList = transformWithClause(pstate, stmt->withClause);
690 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
691 }
692
693 qry->override = stmt->override;
694
695 /*
696 * ON CONFLICT DO UPDATE and ON CONFLICT DO SELECT FOR UPDATE/SHARE
697 * require UPDATE permission on the target relation.
698 */
699 requiresUpdatePerm = (stmt->onConflictClause &&
700 (stmt->onConflictClause->action == ONCONFLICT_UPDATE ||
701 (stmt->onConflictClause->action == ONCONFLICT_SELECT &&
702 stmt->onConflictClause->lockStrength != LCS_NONE)));
703
704 /*
705 * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
706 * VALUES list, or general SELECT input. We special-case VALUES, both for
707 * efficiency and so we can handle DEFAULT specifications.
708 *
709 * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
710 * VALUES clause. If we have any of those, treat it as a general SELECT;
711 * so it will work, but you can't use DEFAULT items together with those.
712 */
713 isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
714 selectStmt->sortClause != NIL ||
715 selectStmt->limitOffset != NULL ||
716 selectStmt->limitCount != NULL ||
717 selectStmt->lockingClause != NIL ||
718 selectStmt->withClause != NULL));
719
720 /*
721 * If a non-nil rangetable/namespace was passed in, and we are doing
722 * INSERT/SELECT, arrange to pass the rangetable/rteperminfos/namespace
723 * down to the SELECT. This can only happen if we are inside a CREATE
724 * RULE, and in that case we want the rule's OLD and NEW rtable entries to
725 * appear as part of the SELECT's rtable, not as outer references for it.
726 * (Kluge!) The SELECT's joinlist is not affected however. We must do
727 * this before adding the target table to the INSERT's rtable.
728 */
729 if (isGeneralSelect)
730 {
731 sub_rtable = pstate->p_rtable;
732 pstate->p_rtable = NIL;
734 pstate->p_rteperminfos = NIL;
735 sub_namespace = pstate->p_namespace;
736 pstate->p_namespace = NIL;
737 }
738 else
739 {
740 sub_rtable = NIL; /* not used, but keep compiler quiet */
743 }
744
745 /*
746 * Must get write lock on INSERT target table before scanning SELECT, else
747 * we will grab the wrong kind of initial lock if the target table is also
748 * mentioned in the SELECT part. Note that the target table is not added
749 * to the joinlist or namespace.
750 */
754 qry->resultRelation = setTargetTable(pstate, stmt->relation,
755 false, false, targetPerms);
756
757 /* Validate stmt->cols list, or build default list if no list given */
758 icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
760
761 /*
762 * Determine which variant of INSERT we have.
763 */
764 if (selectStmt == NULL)
765 {
766 /*
767 * We have INSERT ... DEFAULT VALUES. We can handle this case by
768 * emitting an empty targetlist --- all columns will be defaulted when
769 * the planner expands the targetlist.
770 */
771 exprList = NIL;
772 }
773 else if (isGeneralSelect)
774 {
775 /*
776 * We make the sub-pstate a child of the outer pstate so that it can
777 * see any Param definitions supplied from above. Since the outer
778 * pstate's rtable and namespace are presently empty, there are no
779 * side-effects of exposing names the sub-SELECT shouldn't be able to
780 * see.
781 */
784
785 /*
786 * Process the source SELECT.
787 *
788 * It is important that this be handled just like a standalone SELECT;
789 * otherwise the behavior of SELECT within INSERT might be different
790 * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
791 * bugs of just that nature...)
792 *
793 * The sole exception is that we prevent resolving unknown-type
794 * outputs as TEXT. This does not change the semantics since if the
795 * column type matters semantically, it would have been resolved to
796 * something else anyway. Doing this lets us resolve such outputs as
797 * the target column's type, which we handle below.
798 */
799 sub_pstate->p_rtable = sub_rtable;
800 sub_pstate->p_rteperminfos = sub_rteperminfos;
801 sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
802 sub_pstate->p_nullingrels = NIL;
803 sub_pstate->p_namespace = sub_namespace;
804 sub_pstate->p_resolve_unknowns = false;
805
807
809
810 /* The grammar should have produced a SELECT */
811 if (!IsA(selectQuery, Query) ||
812 selectQuery->commandType != CMD_SELECT)
813 elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
814
815 /*
816 * Make the source be a subquery in the INSERT's rangetable, and add
817 * it to the INSERT's joinlist (but not the namespace).
818 */
821 NULL,
822 false,
823 false);
824 addNSItemToQuery(pstate, nsitem, true, false, false);
825
826 /*----------
827 * Generate an expression list for the INSERT that selects all the
828 * non-resjunk columns from the subquery. (INSERT's tlist must be
829 * separate from the subquery's tlist because we may add columns,
830 * insert datatype coercions, etc.)
831 *
832 * HACK: unknown-type constants and params in the SELECT's targetlist
833 * are copied up as-is rather than being referenced as subquery
834 * outputs. This is to ensure that when we try to coerce them to
835 * the target column's datatype, the right things happen (see
836 * special cases in coerce_type). Otherwise, this fails:
837 * INSERT INTO foo SELECT 'bar', ... FROM baz
838 *----------
839 */
840 exprList = NIL;
841 foreach(lc, selectQuery->targetList)
842 {
844 Expr *expr;
845
846 if (tle->resjunk)
847 continue;
848 if (tle->expr &&
849 (IsA(tle->expr, Const) || IsA(tle->expr, Param)) &&
850 exprType((Node *) tle->expr) == UNKNOWNOID)
851 expr = tle->expr;
852 else
853 {
854 Var *var = makeVarFromTargetEntry(nsitem->p_rtindex, tle);
855
856 var->location = exprLocation((Node *) tle->expr);
857 expr = (Expr *) var;
858 }
859 exprList = lappend(exprList, expr);
860 }
861
862 /* Prepare row for assignment to target table */
864 stmt->cols,
866 false);
867 }
868 else if (list_length(selectStmt->valuesLists) > 1)
869 {
870 /*
871 * Process INSERT ... VALUES with multiple VALUES sublists. We
872 * generate a VALUES RTE holding the transformed expression lists, and
873 * build up a targetlist containing Vars that reference the VALUES
874 * RTE.
875 */
877 List *coltypes = NIL;
880 int sublist_length = -1;
881 bool lateral = false;
882
883 Assert(selectStmt->intoClause == NULL);
884
885 foreach(lc, selectStmt->valuesLists)
886 {
887 List *sublist = (List *) lfirst(lc);
888
889 /*
890 * Do basic expression transformation (same as a ROW() expr, but
891 * allow SetToDefault at top level)
892 */
894 EXPR_KIND_VALUES, true);
895
896 /*
897 * All the sublists must be the same length, *after*
898 * transformation (which might expand '*' into multiple items).
899 * The VALUES RTE can't handle anything different.
900 */
901 if (sublist_length < 0)
902 {
903 /* Remember post-transformation length of first sublist */
905 }
907 {
910 errmsg("VALUES lists must all be the same length"),
911 parser_errposition(pstate,
912 exprLocation((Node *) sublist))));
913 }
914
915 /*
916 * Prepare row for assignment to target table. We process any
917 * indirection on the target column specs normally but then strip
918 * off the resulting field/array assignment nodes, since we don't
919 * want the parsed statement to contain copies of those in each
920 * VALUES row. (It's annoying to have to transform the
921 * indirection specs over and over like this, but avoiding it
922 * would take some really messy refactoring of
923 * transformAssignmentIndirection.)
924 */
926 stmt->cols,
928 true);
929
930 /*
931 * We must assign collations now because assign_query_collations
932 * doesn't process rangetable entries. We just assign all the
933 * collations independently in each row, and don't worry about
934 * whether they are consistent vertically. The outer INSERT query
935 * isn't going to care about the collations of the VALUES columns,
936 * so it's not worth the effort to identify a common collation for
937 * each one here. (But note this does have one user-visible
938 * consequence: INSERT ... VALUES won't complain about conflicting
939 * explicit COLLATEs in a column, whereas the same VALUES
940 * construct in another context would complain.)
941 */
943
945 }
946
947 /*
948 * Construct column type/typmod/collation lists for the VALUES RTE.
949 * Every expression in each column has been coerced to the type/typmod
950 * of the corresponding target column or subfield, so it's sufficient
951 * to look at the exprType/exprTypmod of the first row. We don't care
952 * about the collation labeling, so just fill in InvalidOid for that.
953 */
954 foreach(lc, (List *) linitial(exprsLists))
955 {
956 Node *val = (Node *) lfirst(lc);
957
961 }
962
963 /*
964 * Ordinarily there can't be any current-level Vars in the expression
965 * lists, because the namespace was empty ... but if we're inside
966 * CREATE RULE, then NEW/OLD references might appear. In that case we
967 * have to mark the VALUES RTE as LATERAL.
968 */
969 if (list_length(pstate->p_rtable) != 1 &&
971 lateral = true;
972
973 /*
974 * Generate the VALUES RTE
975 */
978 NULL, lateral, true);
979 addNSItemToQuery(pstate, nsitem, true, false, false);
980
981 /*
982 * Generate list of Vars referencing the RTE
983 */
984 exprList = expandNSItemVars(pstate, nsitem, 0, -1, NULL);
985
986 /*
987 * Re-apply any indirection on the target column specs to the Vars
988 */
990 stmt->cols,
992 false);
993 }
994 else
995 {
996 /*
997 * Process INSERT ... VALUES with a single VALUES sublist. We treat
998 * this case separately for efficiency. The sublist is just computed
999 * directly as the Query's targetlist, with no VALUES RTE. So it
1000 * works just like a SELECT without any FROM.
1001 */
1002 List *valuesLists = selectStmt->valuesLists;
1003
1004 Assert(list_length(valuesLists) == 1);
1005 Assert(selectStmt->intoClause == NULL);
1006
1007 /*
1008 * Do basic expression transformation (same as a ROW() expr, but allow
1009 * SetToDefault at top level)
1010 */
1012 (List *) linitial(valuesLists),
1014 true);
1015
1016 /* Prepare row for assignment to target table */
1018 stmt->cols,
1020 false);
1021 }
1022
1023 /*
1024 * Generate query's target list using the computed list of expressions.
1025 * Also, mark all the target columns as needing insert permissions.
1026 */
1028 qry->targetList = NIL;
1031 {
1032 Expr *expr = (Expr *) lfirst(lc);
1036
1037 tle = makeTargetEntry(expr,
1038 attr_num,
1039 col->name,
1040 false);
1041 qry->targetList = lappend(qry->targetList, tle);
1042
1043 perminfo->insertedCols = bms_add_member(perminfo->insertedCols,
1045 }
1046
1047 /*
1048 * If we have any clauses yet to process, set the query namespace to
1049 * contain only the target relation, removing any entries added in a
1050 * sub-SELECT or VALUES list.
1051 */
1052 if (stmt->onConflictClause || stmt->returningClause)
1053 {
1054 pstate->p_namespace = NIL;
1055 addNSItemToQuery(pstate, pstate->p_target_nsitem,
1056 false, true, true);
1057 }
1058
1059 /* ON CONFLICT DO SELECT requires a RETURNING clause */
1060 if (stmt->onConflictClause &&
1061 stmt->onConflictClause->action == ONCONFLICT_SELECT &&
1062 !stmt->returningClause)
1063 ereport(ERROR,
1065 errmsg("ON CONFLICT DO SELECT requires a RETURNING clause"),
1066 parser_errposition(pstate, stmt->onConflictClause->location));
1067
1068 /* Process ON CONFLICT, if any. */
1069 if (stmt->onConflictClause)
1071 stmt->onConflictClause);
1072
1073 /* Process RETURNING, if any. */
1074 if (stmt->returningClause)
1075 transformReturningClause(pstate, qry, stmt->returningClause,
1077
1078 /* done building the range table and jointree */
1079 qry->rtable = pstate->p_rtable;
1080 qry->rteperminfos = pstate->p_rteperminfos;
1081 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1082
1083 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1084 qry->hasSubLinks = pstate->p_hasSubLinks;
1085
1086 assign_query_collations(pstate, qry);
1087
1088 return qry;
1089}
1090
1091/*
1092 * Prepare an INSERT row for assignment to the target table.
1093 *
1094 * exprlist: transformed expressions for source values; these might come from
1095 * a VALUES row, or be Vars referencing a sub-SELECT or VALUES RTE output.
1096 * stmtcols: original target-columns spec for INSERT (we just test for NIL)
1097 * icolumns: effective target-columns spec (list of ResTarget)
1098 * attrnos: integer column numbers (must be same length as icolumns)
1099 * strip_indirection: if true, remove any field/array assignment nodes
1100 */
1101List *
1104 bool strip_indirection)
1105{
1106 List *result;
1107 ListCell *lc;
1108 ListCell *icols;
1110
1111 /*
1112 * Check length of expr list. It must not have more expressions than
1113 * there are target columns. We allow fewer, but only if no explicit
1114 * columns list was given (the remaining columns are implicitly
1115 * defaulted). Note we must check this *after* transformation because
1116 * that could expand '*' into multiple items.
1117 */
1119 ereport(ERROR,
1121 errmsg("INSERT has more expressions than target columns"),
1122 parser_errposition(pstate,
1124 list_length(icolumns))))));
1125 if (stmtcols != NIL &&
1127 {
1128 /*
1129 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
1130 * where the user accidentally created a RowExpr instead of separate
1131 * columns. Add a suitable hint if that seems to be the problem,
1132 * because the main error message is quite misleading for this case.
1133 * (If there's no stmtcols, you'll get something about data type
1134 * mismatch, which is less misleading so we don't worry about giving a
1135 * hint in that case.)
1136 */
1137 ereport(ERROR,
1139 errmsg("INSERT has more target columns than expressions"),
1140 ((list_length(exprlist) == 1 &&
1143 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),
1144 parser_errposition(pstate,
1146 list_length(exprlist))))));
1147 }
1148
1149 /*
1150 * Prepare columns for assignment to target table.
1151 */
1152 result = NIL;
1154 {
1155 Expr *expr = (Expr *) lfirst(lc);
1157 int attno = lfirst_int(attnos);
1158
1159 expr = transformAssignedExpr(pstate, expr,
1161 col->name,
1162 attno,
1163 col->indirection,
1164 col->location);
1165
1167 {
1168 /*
1169 * We need to remove top-level FieldStores and SubscriptingRefs,
1170 * as well as any CoerceToDomain appearing above one of those ---
1171 * but not a CoerceToDomain that isn't above one of those.
1172 */
1173 while (expr)
1174 {
1175 Expr *subexpr = expr;
1176
1177 while (IsA(subexpr, CoerceToDomain))
1178 {
1179 subexpr = ((CoerceToDomain *) subexpr)->arg;
1180 }
1181 if (IsA(subexpr, FieldStore))
1182 {
1183 FieldStore *fstore = (FieldStore *) subexpr;
1184
1185 expr = (Expr *) linitial(fstore->newvals);
1186 }
1187 else if (IsA(subexpr, SubscriptingRef))
1188 {
1189 SubscriptingRef *sbsref = (SubscriptingRef *) subexpr;
1190
1191 if (sbsref->refassgnexpr == NULL)
1192 break;
1193
1194 expr = sbsref->refassgnexpr;
1195 }
1196 else
1197 break;
1198 }
1199 }
1200
1201 result = lappend(result, expr);
1202 }
1203
1204 return result;
1205}
1206
1207/*
1208 * transformOnConflictClause -
1209 * transforms an OnConflictClause in an INSERT
1210 */
1211static OnConflictExpr *
1213 OnConflictClause *onConflictClause)
1214{
1216 List *arbiterElems;
1217 Node *arbiterWhere;
1219 List *onConflictSet = NIL;
1220 Node *onConflictWhere = NULL;
1221 int exclRelIndex = 0;
1222 List *exclRelTlist = NIL;
1224
1225 /*
1226 * If this is ON CONFLICT DO SELECT/UPDATE, first create the range table
1227 * entry for the EXCLUDED pseudo relation, so that that will be present
1228 * while processing arbiter expressions. (You can't actually reference it
1229 * from there, but this provides a useful error message if you try.)
1230 */
1231 if (onConflictClause->action == ONCONFLICT_UPDATE ||
1232 onConflictClause->action == ONCONFLICT_SELECT)
1233 {
1236
1238 targetrel,
1240 makeAlias("excluded", NIL),
1241 false, false);
1242 exclRte = exclNSItem->p_rte;
1243 exclRelIndex = exclNSItem->p_rtindex;
1244
1245 /*
1246 * relkind is set to composite to signal that we're not dealing with
1247 * an actual relation, and no permission checks are required on it.
1248 * (We'll check the actual target relation, instead.)
1249 */
1251
1252 /* Create EXCLUDED rel's targetlist for use by EXPLAIN */
1254 exclRelIndex);
1255 }
1256
1257 /* Process the arbiter clause, ON CONFLICT ON (...) */
1258 transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
1259 &arbiterWhere, &arbiterConstraint);
1260
1261 /* Process DO SELECT/UPDATE */
1262 if (onConflictClause->action == ONCONFLICT_UPDATE ||
1263 onConflictClause->action == ONCONFLICT_SELECT)
1264 {
1265 /*
1266 * Add the EXCLUDED pseudo relation to the query namespace, making it
1267 * available in SET and WHERE subexpressions.
1268 */
1269 addNSItemToQuery(pstate, exclNSItem, false, true, true);
1270
1271 /* Process the UPDATE SET clause */
1272 if (onConflictClause->action == ONCONFLICT_UPDATE)
1273 onConflictSet =
1274 transformUpdateTargetList(pstate, onConflictClause->targetList, NULL);
1275
1276 /* Process the SELECT/UPDATE WHERE clause */
1277 onConflictWhere = transformWhereClause(pstate,
1278 onConflictClause->whereClause,
1279 EXPR_KIND_WHERE, "WHERE");
1280
1281 /*
1282 * Remove the EXCLUDED pseudo relation from the query namespace, since
1283 * it's not supposed to be available in RETURNING. (Maybe someday we
1284 * could allow that, and drop this step.)
1285 */
1287 pstate->p_namespace = list_delete_last(pstate->p_namespace);
1288 }
1289
1290 /* Finally, build ON CONFLICT DO [NOTHING | SELECT | UPDATE] expression */
1292
1293 result->action = onConflictClause->action;
1294 result->arbiterElems = arbiterElems;
1295 result->arbiterWhere = arbiterWhere;
1296 result->constraint = arbiterConstraint;
1297 result->lockStrength = onConflictClause->lockStrength;
1298 result->onConflictSet = onConflictSet;
1299 result->onConflictWhere = onConflictWhere;
1300 result->exclRelIndex = exclRelIndex;
1301 result->exclRelTlist = exclRelTlist;
1302
1303 return result;
1304}
1305
1306/*
1307 * transformForPortionOfClause
1308 *
1309 * Transforms a ForPortionOfClause in an UPDATE/DELETE statement.
1310 *
1311 * - Look up the range/period requested.
1312 * - Build a compatible range value from the FROM and TO expressions.
1313 * - Build an "overlaps" expression for filtering, used later by the
1314 * rewriter.
1315 * - For UPDATEs, build an "intersects" expression the rewriter can add
1316 * to the targetList to change the temporal bounds.
1317 */
1318static ForPortionOfExpr *
1320 int rtindex,
1321 const ForPortionOfClause *forPortionOf,
1322 bool isUpdate)
1323{
1326 Form_pg_attribute attr;
1328 Oid opclass;
1329 Oid opfamily;
1330 Oid opcintype;
1331 Oid funcid = InvalidOid;
1333 Oid opid;
1334 OpExpr *op;
1336 Var *rangeVar;
1337
1338 /* We don't support FOR PORTION OF FDW queries. */
1339 if (targetrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1340 ereport(ERROR,
1342 errmsg("foreign tables don't support FOR PORTION OF")));
1343
1345
1346 /* Look up the FOR PORTION OF name requested. */
1347 range_attno = attnameAttNum(targetrel, forPortionOf->range_name, false);
1349 ereport(ERROR,
1351 errmsg("column \"%s\" of relation \"%s\" does not exist",
1352 forPortionOf->range_name,
1354 parser_errposition(pstate, forPortionOf->location)));
1355 attr = TupleDescAttr(targetrel->rd_att, range_attno - 1);
1356
1357 attbasetype = getBaseType(attr->atttypid);
1358
1359 rangeVar = makeVar(rtindex,
1361 attr->atttypid,
1362 attr->atttypmod,
1363 attr->attcollation,
1364 0);
1365 rangeVar->location = forPortionOf->location;
1366 result->rangeVar = rangeVar;
1367
1368 /* Require SELECT privilege on the application-time column. */
1369 markVarForSelectPriv(pstate, rangeVar);
1370
1371 /*
1372 * Use the basetype for the target, which shouldn't be required to follow
1373 * domain rules. The table's column type is in the Var if we need it.
1374 */
1375 result->rangeType = attbasetype;
1376 result->isDomain = attbasetype != attr->atttypid;
1377
1378 if (forPortionOf->target)
1379 {
1382
1383 /*
1384 * We were already given an expression for the target, so we don't
1385 * have to build anything. We still have to make sure we got the right
1386 * type. NULL will be caught be the executor.
1387 */
1388
1389 result->targetRange = transformExpr(pstate,
1390 forPortionOf->target,
1392
1393 actual_target_type = exprType(result->targetRange);
1394
1396 ereport(ERROR,
1398 errmsg("could not coerce FOR PORTION OF target from %s to %s",
1401 parser_errposition(pstate, exprLocation(forPortionOf->target))));
1402
1403 result->targetRange = coerce_type(pstate,
1404 result->targetRange,
1407 -1,
1410 exprLocation(forPortionOf->target));
1411
1412 /*
1413 * XXX: For now we only support ranges and multiranges, so we fail on
1414 * anything else.
1415 */
1417 ereport(ERROR,
1419 errmsg("column \"%s\" of relation \"%s\" is not a range or multirange type",
1420 forPortionOf->range_name,
1422 parser_errposition(pstate, forPortionOf->location)));
1423
1424 }
1425 else
1426 {
1430 List *args;
1431
1432 /*
1433 * Make sure it's a range column. XXX: We could support this syntax on
1434 * multirange columns too, if we just built a one-range multirange
1435 * from the FROM/TO phrases.
1436 */
1438 ereport(ERROR,
1440 errmsg("column \"%s\" of relation \"%s\" is not a range type",
1441 forPortionOf->range_name,
1443 parser_errposition(pstate, forPortionOf->location)));
1444
1448
1449 /*
1450 * Build a range from the FROM ... TO ... bounds. This should give a
1451 * constant result, so we accept functions like NOW() but not column
1452 * references, subqueries, etc.
1453 */
1454 result->targetFrom = transformExpr(pstate,
1455 forPortionOf->target_start,
1457 result->targetTo = transformExpr(pstate,
1458 forPortionOf->target_end,
1460 actual_arg_types[0] = exprType(result->targetFrom);
1461 actual_arg_types[1] = exprType(result->targetTo);
1462 args = list_make2(copyObject(result->targetFrom),
1463 copyObject(result->targetTo));
1464
1465 /*
1466 * Check the bound types separately, for better error message and
1467 * location
1468 */
1470 ereport(ERROR,
1472 errmsg("could not coerce FOR PORTION OF %s bound from %s to %s",
1473 "FROM",
1476 parser_errposition(pstate, exprLocation(forPortionOf->target_start))));
1478 ereport(ERROR,
1480 errmsg("could not coerce FOR PORTION OF %s bound from %s to %s",
1481 "TO",
1484 parser_errposition(pstate, exprLocation(forPortionOf->target_end))));
1485
1489 args,
1491 }
1493 ereport(ERROR,
1494 (errmsg("FOR PORTION OF bounds cannot contain volatile functions")));
1495
1496 /*
1497 * Build overlapsExpr to use as an extra qual. This means we only hit rows
1498 * matching the FROM & TO bounds. We must look up the overlaps operator
1499 * (usually "&&").
1500 */
1501 opclass = GetDefaultOpClass(attr->atttypid, GIST_AM_OID);
1502 if (!OidIsValid(opclass))
1503 ereport(ERROR,
1505 errmsg("data type %s has no default operator class for access method \"%s\"",
1506 format_type_be(attr->atttypid), "gist"),
1507 errhint("You must define a default operator class for the data type.")));
1508
1509 /* Look up the operators and functions we need. */
1511 op = makeNode(OpExpr);
1512 op->opno = opid;
1513 op->opfuncid = get_opcode(opid);
1514 op->opresulttype = BOOLOID;
1515 op->args = list_make2(copyObject(rangeVar), copyObject(result->targetRange));
1516 result->overlapsExpr = (Node *) op;
1517
1518 /*
1519 * Look up the without_portion func. This computes the bounds of temporal
1520 * leftovers.
1521 *
1522 * XXX: Find a more extensible way to look up the function, permitting
1523 * user-defined types. An opclass support function doesn't make sense,
1524 * since there is no index involved. Perhaps a type support function.
1525 */
1526 if (get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
1527 switch (opcintype)
1528 {
1529 case ANYRANGEOID:
1530 result->withoutPortionProc = F_RANGE_MINUS_MULTI;
1531 break;
1532 case ANYMULTIRANGEOID:
1533 result->withoutPortionProc = F_MULTIRANGE_MINUS_MULTI;
1534 break;
1535 default:
1536 elog(ERROR, "unexpected opcintype: %u", opcintype);
1537 }
1538 else
1539 elog(ERROR, "unexpected opclass: %u", opclass);
1540
1541 if (isUpdate)
1542 {
1543 /*
1544 * Now make sure we update the start/end time of the record. For a
1545 * range col (r) this is `r = r * targetRange` (where * is the
1546 * intersect operator).
1547 */
1549 List *funcArgs;
1552
1553 /*
1554 * Whatever operator is used for intersect by temporal foreign keys,
1555 * we can use its backing procedure for intersects in FOR PORTION OF.
1556 * XXX: Share code with FindFKPeriodOpers?
1557 */
1558 switch (opcintype)
1559 {
1560 case ANYRANGEOID:
1562 break;
1563 case ANYMULTIRANGEOID:
1565 break;
1566 default:
1567 elog(ERROR, "unexpected opcintype: %u", opcintype);
1568 }
1569 funcid = get_opcode(intersectoperoid);
1570 if (!OidIsValid(funcid))
1571 ereport(ERROR,
1573 errmsg("could not identify an intersect function for type %s",
1574 format_type_be(opcintype)));
1575
1576 funcArgs = list_make2(copyObject(rangeVar),
1577 copyObject(result->targetRange));
1581
1582 /*
1583 * Coerce to domain if necessary. If we skip this, we will allow
1584 * updating to forbidden values.
1585 */
1586 rangeTLEExpr = coerce_type(pstate,
1589 attr->atttypid,
1590 -1,
1593 exprLocation(forPortionOf->target));
1594
1595 /* Make a TLE to set the range column */
1596 result->rangeTargetList = NIL;
1598 forPortionOf->range_name, false);
1599 result->rangeTargetList = lappend(result->rangeTargetList, tle);
1600
1601 /*
1602 * The range column will change, but you don't need UPDATE permission
1603 * on it, so we don't add to updatedCols here. XXX: If
1604 * https://www.postgresql.org/message-id/CACJufxEtY1hdLcx%3DFhnqp-ERcV1PhbvELG5COy_CZjoEW76ZPQ%40mail.gmail.com
1605 * is merged (only validate CHECK constraints if they depend on one of
1606 * the columns being UPDATEd), we need to make sure that code knows
1607 * that we are updating the application-time column.
1608 */
1609 }
1610 else
1611 result->rangeTargetList = NIL;
1612
1613 result->range_name = forPortionOf->range_name;
1614 result->location = forPortionOf->location;
1615 result->targetLocation = forPortionOf->target_location;
1616
1617 return result;
1618}
1619
1620/*
1621 * BuildOnConflictExcludedTargetlist
1622 * Create target list for the EXCLUDED pseudo-relation of ON CONFLICT,
1623 * representing the columns of targetrel with varno exclRelIndex.
1624 *
1625 * Note: Exported for use in the rewriter.
1626 */
1627List *
1629 Index exclRelIndex)
1630{
1631 List *result = NIL;
1632 int attno;
1633 Var *var;
1634 TargetEntry *te;
1635
1636 /*
1637 * Note that resnos of the tlist must correspond to attnos of the
1638 * underlying relation, hence we need entries for dropped columns too.
1639 */
1640 for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1641 {
1642 Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1643 char *name;
1644
1645 if (attr->attisdropped)
1646 {
1647 /*
1648 * can't use atttypid here, but it doesn't really matter what type
1649 * the Const claims to be.
1650 */
1651 var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1652 name = NULL;
1653 }
1654 else
1655 {
1656 var = makeVar(exclRelIndex, attno + 1,
1657 attr->atttypid, attr->atttypmod,
1658 attr->attcollation,
1659 0);
1660 name = pstrdup(NameStr(attr->attname));
1661 }
1662
1663 te = makeTargetEntry((Expr *) var,
1664 attno + 1,
1665 name,
1666 false);
1667
1668 result = lappend(result, te);
1669 }
1670
1671 /*
1672 * Add a whole-row-Var entry to support references to "EXCLUDED.*". Like
1673 * the other entries in the EXCLUDED tlist, its resno must match the Var's
1674 * varattno, else the wrong things happen while resolving references in
1675 * setrefs.c. This is against normal conventions for targetlists, but
1676 * it's okay since we don't use this as a real tlist.
1677 */
1678 var = makeVar(exclRelIndex, InvalidAttrNumber,
1679 targetrel->rd_rel->reltype,
1680 -1, InvalidOid, 0);
1681 te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1682 result = lappend(result, te);
1683
1684 return result;
1685}
1686
1687
1688/*
1689 * count_rowexpr_columns -
1690 * get number of columns contained in a ROW() expression;
1691 * return -1 if expression isn't a RowExpr or a Var referencing one.
1692 *
1693 * This is currently used only for hint purposes, so we aren't terribly
1694 * tense about recognizing all possible cases. The Var case is interesting
1695 * because that's what we'll get in the INSERT ... SELECT (...) case.
1696 */
1697static int
1699{
1700 if (expr == NULL)
1701 return -1;
1702 if (IsA(expr, RowExpr))
1703 return list_length(((RowExpr *) expr)->args);
1704 if (IsA(expr, Var))
1705 {
1706 Var *var = (Var *) expr;
1707 AttrNumber attnum = var->varattno;
1708
1709 if (attnum > 0 && var->vartype == RECORDOID)
1710 {
1712
1713 rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1714 if (rte->rtekind == RTE_SUBQUERY)
1715 {
1716 /* Subselect-in-FROM: examine sub-select's output expr */
1717 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1718 attnum);
1719
1720 if (ste == NULL || ste->resjunk)
1721 return -1;
1722 expr = (Node *) ste->expr;
1723 if (IsA(expr, RowExpr))
1724 return list_length(((RowExpr *) expr)->args);
1725 }
1726 }
1727 }
1728 return -1;
1729}
1730
1731
1732/*
1733 * transformSelectStmt -
1734 * transforms a Select Statement
1735 *
1736 * This function is also used to transform the source expression of a
1737 * PLAssignStmt. In that usage, passthru is non-NULL and we need to
1738 * call transformPLAssignStmtTarget after the initial transformation of the
1739 * SELECT's targetlist. (We could generalize this into an arbitrary callback
1740 * function, but for now that would just be more notation with no benefit.)
1741 * All the rest is the same as a regular SelectStmt.
1742 *
1743 * Note: this covers only cases with no set operations and no VALUES lists;
1744 * see below for the other cases.
1745 */
1746static Query *
1749{
1750 Query *qry = makeNode(Query);
1751 Node *qual;
1752 ListCell *l;
1753
1754 qry->commandType = CMD_SELECT;
1755
1756 /* process the WITH clause independently of all else */
1757 if (stmt->withClause)
1758 {
1759 qry->hasRecursive = stmt->withClause->recursive;
1760 qry->cteList = transformWithClause(pstate, stmt->withClause);
1761 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1762 }
1763
1764 /* Complain if we get called from someplace where INTO is not allowed */
1765 if (stmt->intoClause)
1766 ereport(ERROR,
1768 errmsg("SELECT ... INTO is not allowed here"),
1769 parser_errposition(pstate,
1770 exprLocation((Node *) stmt->intoClause))));
1771
1772 /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
1773 pstate->p_locking_clause = stmt->lockingClause;
1774
1775 /* make WINDOW info available for window functions, too */
1776 pstate->p_windowdefs = stmt->windowClause;
1777
1778 /* process the FROM clause */
1779 transformFromClause(pstate, stmt->fromClause);
1780
1781 /* transform targetlist */
1782 qry->targetList = transformTargetList(pstate, stmt->targetList,
1784
1785 /*
1786 * If we're within a PLAssignStmt, do further transformation of the
1787 * targetlist; that has to happen before we consider sorting or grouping.
1788 * Otherwise, mark column origins (which are useless in a PLAssignStmt).
1789 */
1790 if (passthru)
1792 passthru);
1793 else
1794 markTargetListOrigins(pstate, qry->targetList);
1795
1796 /* transform WHERE */
1797 qual = transformWhereClause(pstate, stmt->whereClause,
1798 EXPR_KIND_WHERE, "WHERE");
1799
1800 /* initial processing of HAVING clause is much like WHERE clause */
1801 qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
1802 EXPR_KIND_HAVING, "HAVING");
1803
1804 /*
1805 * Transform sorting/grouping stuff. Do ORDER BY first because both
1806 * transformGroupClause and transformDistinctClause need the results. Note
1807 * that these functions can also change the targetList, so it's passed to
1808 * them by reference.
1809 */
1810 qry->sortClause = transformSortClause(pstate,
1811 stmt->sortClause,
1812 &qry->targetList,
1814 false /* allow SQL92 rules */ );
1815
1816 qry->groupClause = transformGroupClause(pstate,
1817 stmt->groupClause,
1818 stmt->groupByAll,
1819 &qry->groupingSets,
1820 &qry->targetList,
1821 qry->sortClause,
1823 false /* allow SQL92 rules */ );
1824 qry->groupDistinct = stmt->groupDistinct;
1825 qry->groupByAll = stmt->groupByAll;
1826
1827 if (stmt->distinctClause == NIL)
1828 {
1829 qry->distinctClause = NIL;
1830 qry->hasDistinctOn = false;
1831 }
1832 else if (linitial(stmt->distinctClause) == NULL)
1833 {
1834 /* We had SELECT DISTINCT */
1836 &qry->targetList,
1837 qry->sortClause,
1838 false);
1839 qry->hasDistinctOn = false;
1840 }
1841 else
1842 {
1843 /* We had SELECT DISTINCT ON */
1845 stmt->distinctClause,
1846 &qry->targetList,
1847 qry->sortClause);
1848 qry->hasDistinctOn = true;
1849 }
1850
1851 /* transform LIMIT */
1852 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1853 EXPR_KIND_OFFSET, "OFFSET",
1854 stmt->limitOption);
1855 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1856 EXPR_KIND_LIMIT, "LIMIT",
1857 stmt->limitOption);
1858 qry->limitOption = stmt->limitOption;
1859
1860 /* transform window clauses after we have seen all window functions */
1862 pstate->p_windowdefs,
1863 &qry->targetList);
1864
1865 /* resolve any still-unresolved output columns as being type text */
1866 if (pstate->p_resolve_unknowns)
1868
1869 qry->rtable = pstate->p_rtable;
1870 qry->rteperminfos = pstate->p_rteperminfos;
1871 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1872
1873 qry->hasSubLinks = pstate->p_hasSubLinks;
1874 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1875 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1876 qry->hasAggs = pstate->p_hasAggs;
1877
1878 foreach(l, stmt->lockingClause)
1879 {
1880 transformLockingClause(pstate, qry,
1881 (LockingClause *) lfirst(l), false);
1882 }
1883
1884 assign_query_collations(pstate, qry);
1885
1886 /* this must be done after collations, for reliable comparison of exprs */
1887 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1888 parseCheckAggregates(pstate, qry);
1889
1890 return qry;
1891}
1892
1893/*
1894 * transformValuesClause -
1895 * transforms a VALUES clause that's being used as a standalone SELECT
1896 *
1897 * We build a Query containing a VALUES RTE, rather as if one had written
1898 * SELECT * FROM (VALUES ...) AS "*VALUES*"
1899 */
1900static Query *
1902{
1903 Query *qry = makeNode(Query);
1904 List *exprsLists = NIL;
1905 List *coltypes = NIL;
1906 List *coltypmods = NIL;
1908 List **colexprs = NULL;
1909 int sublist_length = -1;
1910 bool lateral = false;
1912 ListCell *lc;
1913 ListCell *lc2;
1914 int i;
1915
1916 qry->commandType = CMD_SELECT;
1917
1918 /* Most SELECT stuff doesn't apply in a VALUES clause */
1919 Assert(stmt->distinctClause == NIL);
1920 Assert(stmt->intoClause == NULL);
1921 Assert(stmt->targetList == NIL);
1922 Assert(stmt->fromClause == NIL);
1923 Assert(stmt->whereClause == NULL);
1924 Assert(stmt->groupClause == NIL);
1925 Assert(stmt->havingClause == NULL);
1926 Assert(stmt->windowClause == NIL);
1927 Assert(stmt->op == SETOP_NONE);
1928
1929 /* process the WITH clause independently of all else */
1930 if (stmt->withClause)
1931 {
1932 qry->hasRecursive = stmt->withClause->recursive;
1933 qry->cteList = transformWithClause(pstate, stmt->withClause);
1934 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1935 }
1936
1937 /*
1938 * For each row of VALUES, transform the raw expressions.
1939 *
1940 * Note that the intermediate representation we build is column-organized
1941 * not row-organized. That simplifies the type and collation processing
1942 * below.
1943 */
1944 foreach(lc, stmt->valuesLists)
1945 {
1946 List *sublist = (List *) lfirst(lc);
1947
1948 /*
1949 * Do basic expression transformation (same as a ROW() expr, but here
1950 * we disallow SetToDefault)
1951 */
1953 EXPR_KIND_VALUES, false);
1954
1955 /*
1956 * All the sublists must be the same length, *after* transformation
1957 * (which might expand '*' into multiple items). The VALUES RTE can't
1958 * handle anything different.
1959 */
1960 if (sublist_length < 0)
1961 {
1962 /* Remember post-transformation length of first sublist */
1964 /* and allocate array for per-column lists */
1965 colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1966 }
1967 else if (sublist_length != list_length(sublist))
1968 {
1969 ereport(ERROR,
1971 errmsg("VALUES lists must all be the same length"),
1972 parser_errposition(pstate,
1973 exprLocation((Node *) sublist))));
1974 }
1975
1976 /* Build per-column expression lists */
1977 i = 0;
1978 foreach(lc2, sublist)
1979 {
1980 Node *col = (Node *) lfirst(lc2);
1981
1982 colexprs[i] = lappend(colexprs[i], col);
1983 i++;
1984 }
1985
1986 /* Release sub-list's cells to save memory */
1988
1989 /* Prepare an exprsLists element for this row */
1991 }
1992
1993 /*
1994 * Now resolve the common types of the columns, and coerce everything to
1995 * those types. Then identify the common typmod and common collation, if
1996 * any, of each column.
1997 *
1998 * We must do collation processing now because (1) assign_query_collations
1999 * doesn't process rangetable entries, and (2) we need to label the VALUES
2000 * RTE with column collations for use in the outer query. We don't
2001 * consider conflict of implicit collations to be an error here; instead
2002 * the column will just show InvalidOid as its collation, and you'll get a
2003 * failure later if that results in failure to resolve a collation.
2004 *
2005 * Note we modify the per-column expression lists in-place.
2006 */
2007 for (i = 0; i < sublist_length; i++)
2008 {
2009 Oid coltype;
2011 Oid colcoll;
2012
2013 coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
2014
2015 foreach(lc, colexprs[i])
2016 {
2017 Node *col = (Node *) lfirst(lc);
2018
2019 col = coerce_to_common_type(pstate, col, coltype, "VALUES");
2020 lfirst(lc) = col;
2021 }
2022
2023 coltypmod = select_common_typmod(pstate, colexprs[i], coltype);
2024 colcoll = select_common_collation(pstate, colexprs[i], true);
2025
2026 coltypes = lappend_oid(coltypes, coltype);
2029 }
2030
2031 /*
2032 * Finally, rearrange the coerced expressions into row-organized lists.
2033 */
2034 for (i = 0; i < sublist_length; i++)
2035 {
2036 forboth(lc, colexprs[i], lc2, exprsLists)
2037 {
2038 Node *col = (Node *) lfirst(lc);
2039 List *sublist = lfirst(lc2);
2040
2042 lfirst(lc2) = sublist;
2043 }
2044 list_free(colexprs[i]);
2045 }
2046
2047 /*
2048 * Ordinarily there can't be any current-level Vars in the expression
2049 * lists, because the namespace was empty ... but if we're inside CREATE
2050 * RULE, then NEW/OLD references might appear. In that case we have to
2051 * mark the VALUES RTE as LATERAL.
2052 */
2053 if (pstate->p_rtable != NIL &&
2055 lateral = true;
2056
2057 /*
2058 * Generate the VALUES RTE
2059 */
2062 NULL, lateral, true);
2063 addNSItemToQuery(pstate, nsitem, true, true, true);
2064
2065 /*
2066 * Generate a targetlist as though expanding "*"
2067 */
2068 Assert(pstate->p_next_resno == 1);
2069 qry->targetList = expandNSItemAttrs(pstate, nsitem, 0, true, -1);
2070
2071 /*
2072 * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
2073 * VALUES, so cope.
2074 */
2075 qry->sortClause = transformSortClause(pstate,
2076 stmt->sortClause,
2077 &qry->targetList,
2079 false /* allow SQL92 rules */ );
2080
2081 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
2082 EXPR_KIND_OFFSET, "OFFSET",
2083 stmt->limitOption);
2084 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
2085 EXPR_KIND_LIMIT, "LIMIT",
2086 stmt->limitOption);
2087 qry->limitOption = stmt->limitOption;
2088
2089 if (stmt->lockingClause)
2090 ereport(ERROR,
2092 /*------
2093 translator: %s is a SQL row locking clause such as FOR UPDATE */
2094 errmsg("%s cannot be applied to VALUES",
2096 linitial(stmt->lockingClause))->strength))));
2097
2098 qry->rtable = pstate->p_rtable;
2099 qry->rteperminfos = pstate->p_rteperminfos;
2100 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2101
2102 qry->hasSubLinks = pstate->p_hasSubLinks;
2103
2104 assign_query_collations(pstate, qry);
2105
2106 return qry;
2107}
2108
2109/*
2110 * transformSetOperationStmt -
2111 * transforms a set-operations tree
2112 *
2113 * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
2114 * structure to it. We must transform each leaf SELECT and build up a top-
2115 * level Query that contains the leaf SELECTs as subqueries in its rangetable.
2116 * The tree of set operations is converted into the setOperations field of
2117 * the top-level Query.
2118 */
2119static Query *
2121{
2122 Query *qry = makeNode(Query);
2124 int leftmostRTI;
2127 List *sortClause;
2128 Node *limitOffset;
2129 Node *limitCount;
2130 List *lockingClause;
2131 WithClause *withClause;
2132 Node *node;
2134 *lct,
2135 *lcm,
2136 *lcc,
2137 *l;
2139 *targetnames,
2140 *sv_namespace;
2141 int sv_rtable_length;
2144 int sortcolindex;
2145 int tllen;
2146
2147 qry->commandType = CMD_SELECT;
2148
2149 /*
2150 * Find leftmost leaf SelectStmt. We currently only need to do this in
2151 * order to deliver a suitable error message if there's an INTO clause
2152 * there, implying the set-op tree is in a context that doesn't allow
2153 * INTO. (transformSetOperationTree would throw error anyway, but it
2154 * seems worth the trouble to throw a different error for non-leftmost
2155 * INTO, so we produce that error in transformSetOperationTree.)
2156 */
2157 leftmostSelect = stmt->larg;
2158 while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
2161 leftmostSelect->larg == NULL);
2162 if (leftmostSelect->intoClause)
2163 ereport(ERROR,
2165 errmsg("SELECT ... INTO is not allowed here"),
2166 parser_errposition(pstate,
2167 exprLocation((Node *) leftmostSelect->intoClause))));
2168
2169 /*
2170 * We need to extract ORDER BY and other top-level clauses here and not
2171 * let transformSetOperationTree() see them --- else it'll just recurse
2172 * right back here!
2173 */
2174 sortClause = stmt->sortClause;
2175 limitOffset = stmt->limitOffset;
2176 limitCount = stmt->limitCount;
2177 lockingClause = stmt->lockingClause;
2178 withClause = stmt->withClause;
2179
2180 stmt->sortClause = NIL;
2181 stmt->limitOffset = NULL;
2182 stmt->limitCount = NULL;
2183 stmt->lockingClause = NIL;
2184 stmt->withClause = NULL;
2185
2186 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
2187 if (lockingClause)
2188 ereport(ERROR,
2190 /*------
2191 translator: %s is a SQL row locking clause such as FOR UPDATE */
2192 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2194 linitial(lockingClause))->strength))));
2195
2196 /* Process the WITH clause independently of all else */
2197 if (withClause)
2198 {
2199 qry->hasRecursive = withClause->recursive;
2200 qry->cteList = transformWithClause(pstate, withClause);
2201 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2202 }
2203
2204 /*
2205 * Recursively transform the components of the tree.
2206 */
2208 transformSetOperationTree(pstate, stmt, true, NULL));
2209 Assert(sostmt);
2210 qry->setOperations = (Node *) sostmt;
2211
2212 /*
2213 * Re-find leftmost SELECT (now it's a sub-query in rangetable)
2214 */
2215 node = sostmt->larg;
2216 while (node && IsA(node, SetOperationStmt))
2217 node = ((SetOperationStmt *) node)->larg;
2218 Assert(node && IsA(node, RangeTblRef));
2219 leftmostRTI = ((RangeTblRef *) node)->rtindex;
2220 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2222
2223 /*
2224 * Generate dummy targetlist for outer query using column names of
2225 * leftmost select and common datatypes/collations of topmost set
2226 * operation. Also make lists of the dummy vars and their names for use
2227 * in parsing ORDER BY.
2228 *
2229 * Note: we use leftmostRTI as the varno of the dummy variables. It
2230 * shouldn't matter too much which RT index they have, as long as they
2231 * have one that corresponds to a real RT entry; else funny things may
2232 * happen when the tree is mashed by rule rewriting.
2233 */
2234 qry->targetList = NIL;
2235 targetvars = NIL;
2236 targetnames = NIL;
2238 palloc0(list_length(sostmt->colTypes) * sizeof(ParseNamespaceColumn));
2239 sortcolindex = 0;
2240
2241 forfour(lct, sostmt->colTypes,
2242 lcm, sostmt->colTypmods,
2243 lcc, sostmt->colCollations,
2244 left_tlist, leftmostQuery->targetList)
2245 {
2250 char *colName;
2252 Var *var;
2253
2254 Assert(!lefttle->resjunk);
2255 colName = pstrdup(lefttle->resname);
2256 var = makeVar(leftmostRTI,
2257 lefttle->resno,
2258 colType,
2259 colTypmod,
2261 0);
2262 var->location = exprLocation((Node *) lefttle->expr);
2263 tle = makeTargetEntry((Expr *) var,
2264 (AttrNumber) pstate->p_next_resno++,
2265 colName,
2266 false);
2267 qry->targetList = lappend(qry->targetList, tle);
2271 sortnscolumns[sortcolindex].p_varattno = lefttle->resno;
2272 sortnscolumns[sortcolindex].p_vartype = colType;
2273 sortnscolumns[sortcolindex].p_vartypmod = colTypmod;
2276 sortnscolumns[sortcolindex].p_varattnosyn = lefttle->resno;
2277 sortcolindex++;
2278 }
2279
2280 /*
2281 * As a first step towards supporting sort clauses that are expressions
2282 * using the output columns, generate a namespace entry that makes the
2283 * output columns visible. A Join RTE node is handy for this, since we
2284 * can easily control the Vars generated upon matches.
2285 *
2286 * Note: we don't yet do anything useful with such cases, but at least
2287 * "ORDER BY upper(foo)" will draw the right error message rather than
2288 * "foo not found".
2289 */
2291
2295 JOIN_INNER,
2296 0,
2297 targetvars,
2298 NIL,
2299 NIL,
2300 NULL,
2301 NULL,
2302 false);
2303
2304 sv_namespace = pstate->p_namespace;
2305 pstate->p_namespace = NIL;
2306
2307 /* add jnsitem to column namespace only */
2308 addNSItemToQuery(pstate, jnsitem, false, false, true);
2309
2310 /*
2311 * For now, we don't support resjunk sort clauses on the output of a
2312 * setOperation tree --- you can only use the SQL92-spec options of
2313 * selecting an output column by name or number. Enforce by checking that
2314 * transformSortClause doesn't add any items to tlist. Note, if changing
2315 * this, add_setop_child_rel_equivalences() will need to be updated.
2316 */
2318
2319 qry->sortClause = transformSortClause(pstate,
2320 sortClause,
2321 &qry->targetList,
2323 false /* allow SQL92 rules */ );
2324
2325 /* restore namespace, remove join RTE from rtable */
2326 pstate->p_namespace = sv_namespace;
2327 pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
2328
2329 if (tllen != list_length(qry->targetList))
2330 ereport(ERROR,
2332 errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
2333 errdetail("Only result column names can be used, not expressions or functions."),
2334 errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
2335 parser_errposition(pstate,
2337
2338 qry->limitOffset = transformLimitClause(pstate, limitOffset,
2339 EXPR_KIND_OFFSET, "OFFSET",
2340 stmt->limitOption);
2341 qry->limitCount = transformLimitClause(pstate, limitCount,
2342 EXPR_KIND_LIMIT, "LIMIT",
2343 stmt->limitOption);
2344 qry->limitOption = stmt->limitOption;
2345
2346 qry->rtable = pstate->p_rtable;
2347 qry->rteperminfos = pstate->p_rteperminfos;
2348 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2349
2350 qry->hasSubLinks = pstate->p_hasSubLinks;
2351 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2352 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2353 qry->hasAggs = pstate->p_hasAggs;
2354
2355 foreach(l, lockingClause)
2356 {
2357 transformLockingClause(pstate, qry,
2358 (LockingClause *) lfirst(l), false);
2359 }
2360
2361 assign_query_collations(pstate, qry);
2362
2363 /* this must be done after collations, for reliable comparison of exprs */
2364 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
2365 parseCheckAggregates(pstate, qry);
2366
2367 return qry;
2368}
2369
2370/*
2371 * Make a SortGroupClause node for a SetOperationStmt's groupClauses
2372 *
2373 * If require_hash is true, the caller is indicating that they need hash
2374 * support or they will fail. So look extra hard for hash support.
2375 */
2378{
2380 Oid sortop;
2381 Oid eqop;
2382 bool hashable;
2383
2384 /* determine the eqop and optional sortop */
2386 false, true, false,
2387 &sortop, &eqop, NULL,
2388 &hashable);
2389
2390 /*
2391 * The type cache doesn't believe that record is hashable (see
2392 * cache_record_field_properties()), but if the caller really needs hash
2393 * support, we can assume it does. Worst case, if any components of the
2394 * record don't support hashing, we will fail at execution.
2395 */
2397 hashable = true;
2398
2399 /* we don't have a tlist yet, so can't assign sortgrouprefs */
2400 grpcl->tleSortGroupRef = 0;
2401 grpcl->eqop = eqop;
2402 grpcl->sortop = sortop;
2403 grpcl->reverse_sort = false; /* Sort-op is "less than", or InvalidOid */
2404 grpcl->nulls_first = false; /* OK with or without sortop */
2405 grpcl->hashable = hashable;
2406
2407 return grpcl;
2408}
2409
2410/*
2411 * transformSetOperationTree
2412 * Recursively transform leaves and internal nodes of a set-op tree
2413 *
2414 * In addition to returning the transformed node, if targetlist isn't NULL
2415 * then we return a list of its non-resjunk TargetEntry nodes. For a leaf
2416 * set-op node these are the actual targetlist entries; otherwise they are
2417 * dummy entries created to carry the type, typmod, collation, and location
2418 * (for error messages) of each output column of the set-op node. This info
2419 * is needed only during the internal recursion of this function, so outside
2420 * callers pass NULL for targetlist. Note: the reason for passing the
2421 * actual targetlist entries of a leaf node is so that upper levels can
2422 * replace UNKNOWN Consts with properly-coerced constants.
2423 */
2424static Node *
2426 bool isTopLevel, List **targetlist)
2427{
2428 bool isLeaf;
2429
2431
2432 /* Guard against stack overflow due to overly complex set-expressions */
2434
2435 /*
2436 * Validity-check both leaf and internal SELECTs for disallowed ops.
2437 */
2438 if (stmt->intoClause)
2439 ereport(ERROR,
2441 errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
2442 parser_errposition(pstate,
2443 exprLocation((Node *) stmt->intoClause))));
2444
2445 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
2446 if (stmt->lockingClause)
2447 ereport(ERROR,
2449 /*------
2450 translator: %s is a SQL row locking clause such as FOR UPDATE */
2451 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2453 linitial(stmt->lockingClause))->strength))));
2454
2455 /*
2456 * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
2457 * or WITH clauses attached, we need to treat it like a leaf node to
2458 * generate an independent sub-Query tree. Otherwise, it can be
2459 * represented by a SetOperationStmt node underneath the parent Query.
2460 */
2461 if (stmt->op == SETOP_NONE)
2462 {
2463 Assert(stmt->larg == NULL && stmt->rarg == NULL);
2464 isLeaf = true;
2465 }
2466 else
2467 {
2468 Assert(stmt->larg != NULL && stmt->rarg != NULL);
2469 if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
2470 stmt->lockingClause || stmt->withClause)
2471 isLeaf = true;
2472 else
2473 isLeaf = false;
2474 }
2475
2476 if (isLeaf)
2477 {
2478 /* Process leaf SELECT */
2482
2483 /*
2484 * Transform SelectStmt into a Query.
2485 *
2486 * This works the same as SELECT transformation normally would, except
2487 * that we prevent resolving unknown-type outputs as TEXT. This does
2488 * not change the subquery's semantics since if the column type
2489 * matters semantically, it would have been resolved to something else
2490 * anyway. Doing this lets us resolve such outputs using
2491 * select_common_type(), below.
2492 *
2493 * Note: previously transformed sub-queries don't affect the parsing
2494 * of this sub-query, because they are not in the toplevel pstate's
2495 * namespace list.
2496 */
2497 selectQuery = parse_sub_analyze((Node *) stmt, pstate,
2498 NULL, false, false);
2499
2500 /*
2501 * Check for bogus references to Vars on the current query level (but
2502 * upper-level references are okay). Normally this can't happen
2503 * because the namespace will be empty, but it could happen if we are
2504 * inside a rule.
2505 */
2506 if (pstate->p_namespace)
2507 {
2509 ereport(ERROR,
2511 errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
2512 parser_errposition(pstate,
2514 }
2515
2516 /*
2517 * Extract a list of the non-junk TLEs for upper-level processing.
2518 */
2519 if (targetlist)
2520 {
2521 ListCell *tl;
2522
2523 *targetlist = NIL;
2524 foreach(tl, selectQuery->targetList)
2525 {
2527
2528 if (!tle->resjunk)
2529 *targetlist = lappend(*targetlist, tle);
2530 }
2531 }
2532
2533 /*
2534 * Make the leaf query be a subquery in the top-level rangetable.
2535 */
2538 NULL,
2539 false,
2540 false);
2541
2542 /*
2543 * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
2544 */
2546 rtr->rtindex = nsitem->p_rtindex;
2547 return (Node *) rtr;
2548 }
2549 else
2550 {
2551 /* Process an internal node (set operation node) */
2555 const char *context;
2556 bool recursive = (pstate->p_parent_cte &&
2557 pstate->p_parent_cte->cterecursive);
2558
2559 context = (stmt->op == SETOP_UNION ? "UNION" :
2560 (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
2561 "EXCEPT"));
2562
2563 op->op = stmt->op;
2564 op->all = stmt->all;
2565
2566 /*
2567 * Recursively transform the left child node.
2568 */
2569 op->larg = transformSetOperationTree(pstate, stmt->larg,
2570 false,
2571 &ltargetlist);
2572
2573 /*
2574 * If we are processing a recursive union query, now is the time to
2575 * examine the non-recursive term's output columns and mark the
2576 * containing CTE as having those result columns. We should do this
2577 * only at the topmost setop of the CTE, of course.
2578 */
2579 if (isTopLevel && recursive)
2581
2582 /*
2583 * Recursively transform the right child node.
2584 */
2585 op->rarg = transformSetOperationTree(pstate, stmt->rarg,
2586 false,
2587 &rtargetlist);
2588
2589 constructSetOpTargetlist(pstate, op, ltargetlist, rtargetlist, targetlist,
2590 context, recursive);
2591
2592 return (Node *) op;
2593 }
2594}
2595
2596/*
2597 * constructSetOpTargetlist
2598 * Compute the types, typmods and collations of the columns in the target
2599 * list of the given set operation.
2600 *
2601 * For every pair of columns in the targetlists of the children, compute the
2602 * common type, typmod, and collation representing the output (UNION) column.
2603 * If targetlist is not NULL, also build the dummy output targetlist
2604 * containing non-resjunk output columns. The values are stored into the
2605 * given SetOperationStmt node. context is a string for error messages
2606 * ("UNION" etc.). recursive is true if it is a recursive union.
2607 */
2608void
2610 const List *ltargetlist, const List *rtargetlist,
2611 List **targetlist, const char *context, bool recursive)
2612{
2613 ListCell *ltl;
2614 ListCell *rtl;
2615
2616 /*
2617 * Verify that the two children have the same number of non-junk columns,
2618 * and determine the types of the merged output columns.
2619 */
2621 ereport(ERROR,
2623 errmsg("each %s query must have the same number of columns",
2624 context),
2625 parser_errposition(pstate,
2626 exprLocation((const Node *) rtargetlist))));
2627
2628 if (targetlist)
2629 *targetlist = NIL;
2630 op->colTypes = NIL;
2631 op->colTypmods = NIL;
2632 op->colCollations = NIL;
2633 op->groupClauses = NIL;
2634
2636 {
2639 Node *lcolnode = (Node *) ltle->expr;
2640 Node *rcolnode = (Node *) rtle->expr;
2643 Node *bestexpr;
2644 int bestlocation;
2648
2649 /* select common type, same as CASE et al */
2652 context,
2653 &bestexpr);
2655
2656 /*
2657 * Verify the coercions are actually possible. If not, we'd fail
2658 * later anyway, but we want to fail now while we have sufficient
2659 * context to produce an error cursor position.
2660 *
2661 * For all non-UNKNOWN-type cases, we verify coercibility but we don't
2662 * modify the child's expression, for fear of changing the child
2663 * query's semantics.
2664 *
2665 * If a child expression is an UNKNOWN-type Const or Param, we want to
2666 * replace it with the coerced expression. This can only happen when
2667 * the child is a leaf set-op node. It's safe to replace the
2668 * expression because if the child query's semantics depended on the
2669 * type of this output column, it'd have already coerced the UNKNOWN
2670 * to something else. We want to do this because (a) we want to
2671 * verify that a Const is valid for the target type, or resolve the
2672 * actual type of an UNKNOWN Param, and (b) we want to avoid
2673 * unnecessary discrepancies between the output type of the child
2674 * query and the resolved target type. Such a discrepancy would
2675 * disable optimization in the planner.
2676 *
2677 * If it's some other UNKNOWN-type node, eg a Var, we do nothing
2678 * (knowing that coerce_to_common_type would fail). The planner is
2679 * sometimes able to fold an UNKNOWN Var to a constant before it has
2680 * to coerce the type, so failing now would just break cases that
2681 * might work.
2682 */
2683 if (lcoltype != UNKNOWNOID)
2685 rescoltype, context);
2686 else if (IsA(lcolnode, Const) ||
2687 IsA(lcolnode, Param))
2688 {
2690 rescoltype, context);
2691 ltle->expr = (Expr *) lcolnode;
2692 }
2693
2694 if (rcoltype != UNKNOWNOID)
2696 rescoltype, context);
2697 else if (IsA(rcolnode, Const) ||
2698 IsA(rcolnode, Param))
2699 {
2701 rescoltype, context);
2702 rtle->expr = (Expr *) rcolnode;
2703 }
2704
2707 rescoltype);
2708
2709 /*
2710 * Select common collation. A common collation is required for all
2711 * set operators except UNION ALL; see SQL:2008 7.13 <query
2712 * expression> Syntax Rule 15c. (If we fail to identify a common
2713 * collation for a UNION ALL column, the colCollations element will be
2714 * set to InvalidOid, which may result in a runtime error if something
2715 * at a higher query level wants to use the column's collation.)
2716 */
2719 (op->op == SETOP_UNION && op->all));
2720
2721 /* emit results */
2722 op->colTypes = lappend_oid(op->colTypes, rescoltype);
2723 op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
2724 op->colCollations = lappend_oid(op->colCollations, rescolcoll);
2725
2726 /*
2727 * For all cases except UNION ALL, identify the grouping operators
2728 * (and, if available, sorting operators) that will be used to
2729 * eliminate duplicates.
2730 */
2731 if (op->op != SETOP_UNION || !op->all)
2732 {
2734
2736 bestlocation);
2737
2738 /* If it's a recursive union, we need to require hashing support. */
2739 op->groupClauses = lappend(op->groupClauses,
2741
2743 }
2744
2745 /*
2746 * Construct a dummy tlist entry to return. We use a SetToDefault
2747 * node for the expression, since it carries exactly the fields
2748 * needed, but any other expression node type would do as well.
2749 */
2750 if (targetlist)
2751 {
2754
2755 rescolnode->typeId = rescoltype;
2756 rescolnode->typeMod = rescoltypmod;
2757 rescolnode->collation = rescolcoll;
2758 rescolnode->location = bestlocation;
2760 0, /* no need to set resno */
2761 NULL,
2762 false);
2763 *targetlist = lappend(*targetlist, restle);
2764 }
2765 }
2766}
2767
2768/*
2769 * Process the outputs of the non-recursive term of a recursive union
2770 * to set up the parent CTE's columns
2771 */
2772static void
2774{
2775 Node *node;
2776 int leftmostRTI;
2778 List *targetList;
2780 ListCell *nrtl;
2781 int next_resno;
2782
2783 /*
2784 * Find leftmost leaf SELECT
2785 */
2786 node = larg;
2787 while (node && IsA(node, SetOperationStmt))
2788 node = ((SetOperationStmt *) node)->larg;
2789 Assert(node && IsA(node, RangeTblRef));
2790 leftmostRTI = ((RangeTblRef *) node)->rtindex;
2791 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2793
2794 /*
2795 * Generate dummy targetlist using column names of leftmost select and
2796 * dummy result expressions of the non-recursive term.
2797 */
2798 targetList = NIL;
2799 next_resno = 1;
2800
2802 {
2805 char *colName;
2807
2808 Assert(!lefttle->resjunk);
2809 colName = pstrdup(lefttle->resname);
2810 tle = makeTargetEntry(nrtle->expr,
2811 next_resno++,
2812 colName,
2813 false);
2814 targetList = lappend(targetList, tle);
2815 }
2816
2817 /* Now build CTE's output column info using dummy targetlist */
2818 analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
2819}
2820
2821
2822/*
2823 * transformReturnStmt -
2824 * transforms a return statement
2825 */
2826static Query *
2828{
2829 Query *qry = makeNode(Query);
2830
2831 qry->commandType = CMD_SELECT;
2832 qry->isReturn = true;
2833
2835 1, NULL, false));
2836
2837 if (pstate->p_resolve_unknowns)
2839 qry->rtable = pstate->p_rtable;
2840 qry->rteperminfos = pstate->p_rteperminfos;
2841 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2842 qry->hasSubLinks = pstate->p_hasSubLinks;
2843 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2844 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2845 qry->hasAggs = pstate->p_hasAggs;
2846
2847 assign_query_collations(pstate, qry);
2848
2849 return qry;
2850}
2851
2852
2853/*
2854 * transformUpdateStmt -
2855 * transforms an update statement
2856 */
2857static Query *
2859{
2860 Query *qry = makeNode(Query);
2862 Node *qual;
2863
2864 qry->commandType = CMD_UPDATE;
2865
2866 /* process the WITH clause independently of all else */
2867 if (stmt->withClause)
2868 {
2869 qry->hasRecursive = stmt->withClause->recursive;
2870 qry->cteList = transformWithClause(pstate, stmt->withClause);
2871 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2872 }
2873
2874 qry->resultRelation = setTargetTable(pstate, stmt->relation,
2875 stmt->relation->inh,
2876 true,
2877 ACL_UPDATE);
2878
2879 /* disallow UPDATE ... WHERE CURRENT OF on a view */
2880 if (stmt->whereClause &&
2881 IsA(stmt->whereClause, CurrentOfExpr) &&
2882 pstate->p_target_relation->rd_rel->relkind == RELKIND_VIEW)
2883 ereport(ERROR,
2885 errmsg("WHERE CURRENT OF on a view is not implemented"));
2886
2887 if (stmt->forPortionOf)
2889 qry->resultRelation,
2891 true);
2892
2893 nsitem = pstate->p_target_nsitem;
2894
2895 /* subqueries in FROM cannot access the result relation */
2896 nsitem->p_lateral_only = true;
2897 nsitem->p_lateral_ok = false;
2898
2899 /*
2900 * the FROM clause is non-standard SQL syntax. We used to be able to do
2901 * this with REPLACE in POSTQUEL so we keep the feature.
2902 */
2903 transformFromClause(pstate, stmt->fromClause);
2904
2905 /* remaining clauses can reference the result relation normally */
2906 nsitem->p_lateral_only = false;
2907 nsitem->p_lateral_ok = true;
2908
2909 qual = transformWhereClause(pstate, stmt->whereClause,
2910 EXPR_KIND_WHERE, "WHERE");
2911
2912 transformReturningClause(pstate, qry, stmt->returningClause,
2914
2915 /*
2916 * Now we are done with SELECT-like processing, and can get on with
2917 * transforming the target list to match the UPDATE target columns.
2918 */
2919 qry->targetList = transformUpdateTargetList(pstate, stmt->targetList,
2920 qry->forPortionOf);
2921
2922 qry->rtable = pstate->p_rtable;
2923 qry->rteperminfos = pstate->p_rteperminfos;
2924 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
2925
2926 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2927 qry->hasSubLinks = pstate->p_hasSubLinks;
2928
2929 assign_query_collations(pstate, qry);
2930
2931 return qry;
2932}
2933
2934/*
2935 * transformUpdateTargetList -
2936 * handle SET clause in UPDATE/MERGE/INSERT ... ON CONFLICT UPDATE
2937 */
2938List *
2940{
2941 List *tlist = NIL;
2944 ListCell *tl;
2945
2946 tlist = transformTargetList(pstate, origTlist,
2948
2949 /* Prepare to assign non-conflicting resnos to resjunk attributes */
2952
2953 /* Prepare non-junk columns for assignment to target table */
2956
2957 foreach(tl, tlist)
2958 {
2961 int attrno;
2962
2963 if (tle->resjunk)
2964 {
2965 /*
2966 * Resjunk nodes need no additional processing, but be sure they
2967 * have resnos that do not match any target columns; else rewriter
2968 * or planner might get confused. They don't need a resname
2969 * either.
2970 */
2971 tle->resno = (AttrNumber) pstate->p_next_resno++;
2972 tle->resname = NULL;
2973 continue;
2974 }
2975 if (orig_tl == NULL)
2976 elog(ERROR, "UPDATE target count mismatch --- internal error");
2978
2980 origTarget->name, true);
2982 ereport(ERROR,
2984 errmsg("column \"%s\" of relation \"%s\" does not exist",
2985 origTarget->name,
2987 (origTarget->indirection != NIL &&
2988 strcmp(origTarget->name, pstate->p_target_nsitem->p_names->aliasname) == 0) ?
2989 errhint("SET target columns cannot be qualified with the relation name.") : 0,
2990 parser_errposition(pstate, origTarget->location)));
2991
2992 /*
2993 * If this is a FOR PORTION OF update, forbid directly setting the
2994 * range column, since that would conflict with the implicit updates.
2995 */
2996 if (forPortionOf != NULL)
2997 {
2998 if (attrno == forPortionOf->rangeVar->varattno)
2999 ereport(ERROR,
3001 errmsg("cannot update column \"%s\" because it is used in FOR PORTION OF",
3002 origTarget->name),
3003 parser_errposition(pstate, origTarget->location)));
3004 }
3005
3006 updateTargetListEntry(pstate, tle, origTarget->name,
3007 attrno,
3008 origTarget->indirection,
3009 origTarget->location);
3010
3011 /* Mark the target column as requiring update permissions */
3012 target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
3014
3016 }
3017 if (orig_tl != NULL)
3018 elog(ERROR, "UPDATE target count mismatch --- internal error");
3019
3020 return tlist;
3021}
3022
3023/*
3024 * addNSItemForReturning -
3025 * add a ParseNamespaceItem for the OLD or NEW alias in RETURNING.
3026 */
3027static void
3028addNSItemForReturning(ParseState *pstate, const char *aliasname,
3029 VarReturningType returning_type)
3030{
3031 List *colnames;
3032 int numattrs;
3035
3036 /* copy per-column data from the target relation */
3037 colnames = pstate->p_target_nsitem->p_rte->eref->colnames;
3038 numattrs = list_length(colnames);
3039
3041
3043 numattrs * sizeof(ParseNamespaceColumn));
3044
3045 /* mark all columns as returning OLD/NEW */
3046 for (int i = 0; i < numattrs; i++)
3047 nscolumns[i].p_varreturningtype = returning_type;
3048
3049 /* build the nsitem, copying most fields from the target relation */
3051 nsitem->p_names = makeAlias(aliasname, colnames);
3052 nsitem->p_rte = pstate->p_target_nsitem->p_rte;
3053 nsitem->p_rtindex = pstate->p_target_nsitem->p_rtindex;
3054 nsitem->p_perminfo = pstate->p_target_nsitem->p_perminfo;
3055 nsitem->p_nscolumns = nscolumns;
3056 nsitem->p_returning_type = returning_type;
3057
3058 /* add it to the query namespace as a table-only item */
3059 addNSItemToQuery(pstate, nsitem, false, true, false);
3060}
3061
3062/*
3063 * transformReturningClause -
3064 * handle a RETURNING clause in INSERT/UPDATE/DELETE/MERGE
3065 */
3066void
3068 ReturningClause *returningClause,
3070{
3071 int save_nslen = list_length(pstate->p_namespace);
3072 int save_next_resno;
3073
3074 if (returningClause == NULL)
3075 return; /* nothing to do */
3076
3077 /*
3078 * Scan RETURNING WITH(...) options for OLD/NEW alias names. Complain if
3079 * there is any conflict with existing relations.
3080 */
3081 foreach_node(ReturningOption, option, returningClause->options)
3082 {
3083 switch (option->option)
3084 {
3086 if (qry->returningOldAlias != NULL)
3087 ereport(ERROR,
3089 /* translator: %s is OLD or NEW */
3090 errmsg("%s cannot be specified multiple times", "OLD"),
3091 parser_errposition(pstate, option->location));
3092 qry->returningOldAlias = option->value;
3093 break;
3094
3096 if (qry->returningNewAlias != NULL)
3097 ereport(ERROR,
3099 /* translator: %s is OLD or NEW */
3100 errmsg("%s cannot be specified multiple times", "NEW"),
3101 parser_errposition(pstate, option->location));
3102 qry->returningNewAlias = option->value;
3103 break;
3104
3105 default:
3106 elog(ERROR, "unrecognized returning option: %d", option->option);
3107 }
3108
3109 if (refnameNamespaceItem(pstate, NULL, option->value, -1, NULL) != NULL)
3110 ereport(ERROR,
3112 errmsg("table name \"%s\" specified more than once",
3113 option->value),
3114 parser_errposition(pstate, option->location));
3115
3116 addNSItemForReturning(pstate, option->value,
3117 option->option == RETURNING_OPTION_OLD ?
3119 }
3120
3121 /*
3122 * If OLD/NEW alias names weren't explicitly specified, use "old"/"new"
3123 * unless masked by existing relations.
3124 */
3125 if (qry->returningOldAlias == NULL &&
3126 refnameNamespaceItem(pstate, NULL, "old", -1, NULL) == NULL)
3127 {
3128 qry->returningOldAlias = "old";
3130 }
3131 if (qry->returningNewAlias == NULL &&
3132 refnameNamespaceItem(pstate, NULL, "new", -1, NULL) == NULL)
3133 {
3134 qry->returningNewAlias = "new";
3136 }
3137
3138 /*
3139 * We need to assign resnos starting at one in the RETURNING list. Save
3140 * and restore the main tlist's value of p_next_resno, just in case
3141 * someone looks at it later (probably won't happen).
3142 */
3143 save_next_resno = pstate->p_next_resno;
3144 pstate->p_next_resno = 1;
3145
3146 /* transform RETURNING expressions identically to a SELECT targetlist */
3147 qry->returningList = transformTargetList(pstate,
3148 returningClause->exprs,
3149 exprKind);
3150
3151 /*
3152 * Complain if the nonempty tlist expanded to nothing (which is possible
3153 * if it contains only a star-expansion of a zero-column table). If we
3154 * allow this, the parsed Query will look like it didn't have RETURNING,
3155 * with results that would probably surprise the user.
3156 */
3157 if (qry->returningList == NIL)
3158 ereport(ERROR,
3160 errmsg("RETURNING must have at least one column"),
3161 parser_errposition(pstate,
3162 exprLocation(linitial(returningClause->exprs)))));
3163
3164 /* mark column origins */
3166
3167 /* resolve any still-unresolved output columns as being type text */
3168 if (pstate->p_resolve_unknowns)
3170
3171 /* restore state */
3172 pstate->p_namespace = list_truncate(pstate->p_namespace, save_nslen);
3173 pstate->p_next_resno = save_next_resno;
3174}
3175
3176
3177/*
3178 * transformPLAssignStmt -
3179 * transform a PL/pgSQL assignment statement
3180 *
3181 * If there is no opt_indirection, the transformed statement looks like
3182 * "SELECT a_expr ...", except the expression has been cast to the type of
3183 * the target. With indirection, it's still a SELECT, but the expression will
3184 * incorporate FieldStore and/or assignment SubscriptingRef nodes to compute a
3185 * new value for a container-type variable represented by the target. The
3186 * expression references the target as the container source.
3187 */
3188static Query *
3190{
3191 Query *qry;
3193 List *indirection = stmt->indirection;
3194 int nnames = stmt->nnames;
3195 Node *target;
3198
3199 /*
3200 * First, construct a ColumnRef for the target variable. If the target
3201 * has more than one dotted name, we have to pull the extra names out of
3202 * the indirection list.
3203 */
3204 cref->fields = list_make1(makeString(stmt->name));
3205 cref->location = stmt->location;
3206 if (nnames > 1)
3207 {
3208 /* avoid munging the raw parsetree */
3209 indirection = list_copy(indirection);
3210 while (--nnames > 0 && indirection != NIL)
3211 {
3212 Node *ind = (Node *) linitial(indirection);
3213
3214 if (!IsA(ind, String))
3215 elog(ERROR, "invalid name count in PLAssignStmt");
3216 cref->fields = lappend(cref->fields, ind);
3217 indirection = list_delete_first(indirection);
3218 }
3219 }
3220
3221 /*
3222 * Transform the target reference. Typically we will get back a Param
3223 * node, but there's no reason to be too picky about its type. (Note that
3224 * we must do this before calling transformSelectStmt. It's tempting to
3225 * do it inside transformPLAssignStmtTarget, but we need to do it before
3226 * adding any FROM tables to the pstate's namespace, else we might wrongly
3227 * resolve the target as a table column.)
3228 */
3229 target = transformExpr(pstate, (Node *) cref,
3231
3232 /* Set up passthrough data for transformPLAssignStmtTarget */
3233 passthru.stmt = stmt;
3234 passthru.target = target;
3235 passthru.indirection = indirection;
3236
3237 /*
3238 * To avoid duplicating a lot of code, we use transformSelectStmt to do
3239 * almost all of the work. However, we need to do additional processing
3240 * on the SELECT's targetlist after it's been transformed, but before
3241 * possible addition of targetlist items for ORDER BY or GROUP BY.
3242 * transformSelectStmt knows it should call transformPLAssignStmtTarget if
3243 * it's passed a passthru argument.
3244 *
3245 * Also, disable resolution of unknown-type tlist items; PL/pgSQL wants to
3246 * deal with that itself.
3247 */
3249 pstate->p_resolve_unknowns = false;
3250 qry = transformSelectStmt(pstate, stmt->val, &passthru);
3252
3253 return qry;
3254}
3255
3256/*
3257 * Callback function to adjust a SELECT's tlist to make the output suitable
3258 * for assignment to a PLAssignStmt's target variable.
3259 *
3260 * Note: we actually modify the tle->expr in-place, but the function's API
3261 * is set up to not presume that.
3262 */
3263static List *
3266{
3267 PLAssignStmt *stmt = passthru->stmt;
3268 Node *target = passthru->target;
3269 List *indirection = passthru->indirection;
3270 Oid targettype;
3271 int32 targettypmod;
3274 Oid type_id;
3275
3276 targettype = exprType(target);
3277 targettypmod = exprTypmod(target);
3279
3280 /* we should have exactly one targetlist item */
3281 if (list_length(tlist) != 1)
3282 ereport(ERROR,
3284 errmsg_plural("assignment source returned %d column",
3285 "assignment source returned %d columns",
3286 list_length(tlist),
3287 list_length(tlist))));
3288
3289 tle = linitial_node(TargetEntry, tlist);
3290
3291 /*
3292 * This next bit is similar to transformAssignedExpr; the key difference
3293 * is we use COERCION_PLPGSQL not COERCION_ASSIGNMENT.
3294 */
3295 type_id = exprType((Node *) tle->expr);
3296
3298
3299 if (indirection)
3300 {
3301 tle->expr = (Expr *)
3303 target,
3304 stmt->name,
3305 false,
3306 targettype,
3307 targettypmod,
3309 indirection,
3310 list_head(indirection),
3311 (Node *) tle->expr,
3313 exprLocation(target));
3314 }
3315 else if (targettype != type_id &&
3316 (targettype == RECORDOID || ISCOMPLEX(targettype)) &&
3317 (type_id == RECORDOID || ISCOMPLEX(type_id)))
3318 {
3319 /*
3320 * Hack: do not let coerce_to_target_type() deal with inconsistent
3321 * composite types. Just pass the expression result through as-is,
3322 * and let the PL/pgSQL executor do the conversion its way. This is
3323 * rather bogus, but it's needed for backwards compatibility.
3324 */
3325 }
3326 else
3327 {
3328 /*
3329 * For normal non-qualified target column, do type checking and
3330 * coercion.
3331 */
3332 Node *orig_expr = (Node *) tle->expr;
3333
3334 tle->expr = (Expr *)
3335 coerce_to_target_type(pstate,
3336 orig_expr, type_id,
3337 targettype, targettypmod,
3340 -1);
3341 /* With COERCION_PLPGSQL, this error is probably unreachable */
3342 if (tle->expr == NULL)
3343 ereport(ERROR,
3345 errmsg("variable \"%s\" is of type %s"
3346 " but expression is of type %s",
3347 stmt->name,
3348 format_type_be(targettype),
3349 format_type_be(type_id)),
3350 errhint("You will need to rewrite or cast the expression."),
3352 }
3353
3354 pstate->p_expr_kind = EXPR_KIND_NONE;
3355
3356 return list_make1(tle);
3357}
3358
3359
3360/*
3361 * transformDeclareCursorStmt -
3362 * transform a DECLARE CURSOR Statement
3363 *
3364 * DECLARE CURSOR is like other utility statements in that we emit it as a
3365 * CMD_UTILITY Query node; however, we must first transform the contained
3366 * query. We used to postpone that until execution, but it's really necessary
3367 * to do it during the normal parse analysis phase to ensure that side effects
3368 * of parser hooks happen at the expected time.
3369 */
3370static Query *
3372{
3373 Query *result;
3374 Query *query;
3375
3376 if ((stmt->options & CURSOR_OPT_SCROLL) &&
3377 (stmt->options & CURSOR_OPT_NO_SCROLL))
3378 ereport(ERROR,
3380 /* translator: %s is a SQL keyword */
3381 errmsg("cannot specify both %s and %s",
3382 "SCROLL", "NO SCROLL")));
3383
3384 if ((stmt->options & CURSOR_OPT_ASENSITIVE) &&
3385 (stmt->options & CURSOR_OPT_INSENSITIVE))
3386 ereport(ERROR,
3388 /* translator: %s is a SQL keyword */
3389 errmsg("cannot specify both %s and %s",
3390 "ASENSITIVE", "INSENSITIVE")));
3391
3392 /* Transform contained query, not allowing SELECT INTO */
3393 query = transformStmt(pstate, stmt->query);
3394 stmt->query = (Node *) query;
3395
3396 /* Grammar should not have allowed anything but SELECT */
3397 if (!IsA(query, Query) ||
3398 query->commandType != CMD_SELECT)
3399 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
3400
3401 /*
3402 * We also disallow data-modifying WITH in a cursor. (This could be
3403 * allowed, but the semantics of when the updates occur might be
3404 * surprising.)
3405 */
3406 if (query->hasModifyingCTE)
3407 ereport(ERROR,
3409 errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
3410
3411 /* FOR UPDATE and WITH HOLD are not compatible */
3412 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
3413 ereport(ERROR,
3415 /*------
3416 translator: %s is a SQL row locking clause such as FOR UPDATE */
3417 errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
3419 linitial(query->rowMarks))->strength)),
3420 errdetail("Holdable cursors must be READ ONLY.")));
3421
3422 /* FOR UPDATE and SCROLL are not compatible */
3423 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
3424 ereport(ERROR,
3426 /*------
3427 translator: %s is a SQL row locking clause such as FOR UPDATE */
3428 errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
3430 linitial(query->rowMarks))->strength)),
3431 errdetail("Scrollable cursors must be READ ONLY.")));
3432
3433 /* FOR UPDATE and INSENSITIVE are not compatible */
3434 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
3435 ereport(ERROR,
3437 /*------
3438 translator: %s is a SQL row locking clause such as FOR UPDATE */
3439 errmsg("DECLARE INSENSITIVE CURSOR ... %s is not valid",
3441 linitial(query->rowMarks))->strength)),
3442 errdetail("Insensitive cursors must be READ ONLY.")));
3443
3444 /* represent the command as a utility Query */
3446 result->commandType = CMD_UTILITY;
3447 result->utilityStmt = (Node *) stmt;
3448
3449 return result;
3450}
3451
3452
3453/*
3454 * transformExplainStmt -
3455 * transform an EXPLAIN Statement
3456 *
3457 * EXPLAIN is like other utility statements in that we emit it as a
3458 * CMD_UTILITY Query node; however, we must first transform the contained
3459 * query. We used to postpone that until execution, but it's really necessary
3460 * to do it during the normal parse analysis phase to ensure that side effects
3461 * of parser hooks happen at the expected time.
3462 */
3463static Query *
3465{
3466 Query *result;
3467 bool generic_plan = false;
3468 Oid *paramTypes = NULL;
3469 int numParams = 0;
3470
3471 /*
3472 * If we have no external source of parameter definitions, and the
3473 * GENERIC_PLAN option is specified, then accept variable parameter
3474 * definitions (similarly to PREPARE, for example).
3475 */
3476 if (pstate->p_paramref_hook == NULL)
3477 {
3478 ListCell *lc;
3479
3480 foreach(lc, stmt->options)
3481 {
3482 DefElem *opt = (DefElem *) lfirst(lc);
3483
3484 if (strcmp(opt->defname, "generic_plan") == 0)
3486 /* don't "break", as we want the last value */
3487 }
3488 if (generic_plan)
3489 setup_parse_variable_parameters(pstate, &paramTypes, &numParams);
3490 }
3491
3492 /* transform contained query, allowing SELECT INTO */
3493 stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
3494
3495 /* make sure all is well with parameter types */
3496 if (generic_plan)
3497 check_variable_parameters(pstate, (Query *) stmt->query);
3498
3499 /* represent the command as a utility Query */
3501 result->commandType = CMD_UTILITY;
3502 result->utilityStmt = (Node *) stmt;
3503
3504 return result;
3505}
3506
3507
3508/*
3509 * transformCreateTableAsStmt -
3510 * transform a CREATE TABLE AS, SELECT ... INTO, or CREATE MATERIALIZED VIEW
3511 * Statement
3512 *
3513 * As with DECLARE CURSOR and EXPLAIN, transform the contained statement now.
3514 */
3515static Query *
3517{
3518 Query *result;
3519 Query *query;
3520
3521 /* transform contained query, not allowing SELECT INTO */
3522 query = transformStmt(pstate, stmt->query);
3523 stmt->query = (Node *) query;
3524
3525 /* additional work needed for CREATE MATERIALIZED VIEW */
3526 if (stmt->objtype == OBJECT_MATVIEW)
3527 {
3529
3530 /*
3531 * Prohibit a data-modifying CTE in the query used to create a
3532 * materialized view. It's not sufficiently clear what the user would
3533 * want to happen if the MV is refreshed or incrementally maintained.
3534 */
3535 if (query->hasModifyingCTE)
3536 ereport(ERROR,
3538 errmsg("materialized views must not use data-modifying statements in WITH")));
3539
3540 /*
3541 * Check whether any temporary database objects are used in the
3542 * creation query. It would be hard to refresh data or incrementally
3543 * maintain it if a source disappeared.
3544 */
3546 ereport(ERROR,
3548 errmsg("materialized views must not use temporary objects"),
3549 errdetail("This view depends on temporary %s.",
3551
3552 /*
3553 * A materialized view would either need to save parameters for use in
3554 * maintaining/loading the data or prohibit them entirely. The latter
3555 * seems safer and more sane.
3556 */
3558 ereport(ERROR,
3560 errmsg("materialized views may not be defined using bound parameters")));
3561
3562 /*
3563 * For now, we disallow unlogged materialized views, because it seems
3564 * like a bad idea for them to just go to empty after a crash. (If we
3565 * could mark them as unpopulated, that would be better, but that
3566 * requires catalog changes which crash recovery can't presently
3567 * handle.)
3568 */
3569 if (stmt->into->rel->relpersistence == RELPERSISTENCE_UNLOGGED)
3570 ereport(ERROR,
3572 errmsg("materialized views cannot be unlogged")));
3573
3574 /*
3575 * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
3576 * for purposes of creating the view's ON SELECT rule. We stash that
3577 * in the IntoClause because that's where intorel_startup() can
3578 * conveniently get it from.
3579 */
3580 stmt->into->viewQuery = copyObject(query);
3581 }
3582
3583 /* represent the command as a utility Query */
3585 result->commandType = CMD_UTILITY;
3586 result->utilityStmt = (Node *) stmt;
3587
3588 return result;
3589}
3590
3591/*
3592 * transform a CallStmt
3593 */
3594static Query *
3596{
3597 List *targs;
3598 ListCell *lc;
3599 Node *node;
3600 FuncExpr *fexpr;
3603 bool isNull;
3604 List *outargs = NIL;
3605 Query *result;
3606
3607 /*
3608 * First, do standard parse analysis on the procedure call and its
3609 * arguments, allowing us to identify the called procedure.
3610 */
3611 targs = NIL;
3612 foreach(lc, stmt->funccall->args)
3613 {
3614 targs = lappend(targs, transformExpr(pstate,
3615 (Node *) lfirst(lc),
3617 }
3618
3619 node = ParseFuncOrColumn(pstate,
3620 stmt->funccall->funcname,
3621 targs,
3622 pstate->p_last_srf,
3623 stmt->funccall,
3624 true,
3625 stmt->funccall->location);
3626
3627 assign_expr_collations(pstate, node);
3628
3629 fexpr = castNode(FuncExpr, node);
3630
3633 elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
3634
3635 /*
3636 * Expand the argument list to deal with named-argument notation and
3637 * default arguments. For ordinary FuncExprs this'd be done during
3638 * planning, but a CallStmt doesn't go through planning, and there seems
3639 * no good reason not to do it here.
3640 */
3642 true,
3643 fexpr->funcresulttype,
3644 proctup);
3645
3646 /* Fetch proargmodes; if it's null, there are no output args */
3649 &isNull);
3650 if (!isNull)
3651 {
3652 /*
3653 * Split the list into input arguments in fexpr->args and output
3654 * arguments in stmt->outargs. INOUT arguments appear in both lists.
3655 */
3656 ArrayType *arr;
3657 int numargs;
3658 char *argmodes;
3659 List *inargs;
3660 int i;
3661
3662 arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
3663 numargs = list_length(fexpr->args);
3664 if (ARR_NDIM(arr) != 1 ||
3665 ARR_DIMS(arr)[0] != numargs ||
3666 ARR_HASNULL(arr) ||
3667 ARR_ELEMTYPE(arr) != CHAROID)
3668 elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
3669 numargs);
3670 argmodes = (char *) ARR_DATA_PTR(arr);
3671
3672 inargs = NIL;
3673 i = 0;
3674 foreach(lc, fexpr->args)
3675 {
3676 Node *n = lfirst(lc);
3677
3678 switch (argmodes[i])
3679 {
3680 case PROARGMODE_IN:
3682 inargs = lappend(inargs, n);
3683 break;
3684 case PROARGMODE_OUT:
3685 outargs = lappend(outargs, n);
3686 break;
3687 case PROARGMODE_INOUT:
3688 inargs = lappend(inargs, n);
3689 outargs = lappend(outargs, copyObject(n));
3690 break;
3691 default:
3692 /* note we don't support PROARGMODE_TABLE */
3693 elog(ERROR, "invalid argmode %c for procedure",
3694 argmodes[i]);
3695 break;
3696 }
3697 i++;
3698 }
3699 fexpr->args = inargs;
3700 }
3701
3702 stmt->funcexpr = fexpr;
3703 stmt->outargs = outargs;
3704
3706
3707 /* represent the command as a utility Query */
3709 result->commandType = CMD_UTILITY;
3710 result->utilityStmt = (Node *) stmt;
3711
3712 return result;
3713}
3714
3715/*
3716 * Produce a string representation of a LockClauseStrength value.
3717 * This should only be applied to valid values (not LCS_NONE).
3718 */
3719const char *
3721{
3722 switch (strength)
3723 {
3724 case LCS_NONE:
3725 Assert(false);
3726 break;
3727 case LCS_FORKEYSHARE:
3728 return "FOR KEY SHARE";
3729 case LCS_FORSHARE:
3730 return "FOR SHARE";
3731 case LCS_FORNOKEYUPDATE:
3732 return "FOR NO KEY UPDATE";
3733 case LCS_FORUPDATE:
3734 return "FOR UPDATE";
3735 }
3736 return "FOR some"; /* shouldn't happen */
3737}
3738
3739/*
3740 * Check for features that are not supported with FOR [KEY] UPDATE/SHARE.
3741 *
3742 * exported so planner can check again after rewriting, query pullup, etc
3743 */
3744void
3746{
3747 Assert(strength != LCS_NONE); /* else caller error */
3748
3749 if (qry->setOperations)
3750 ereport(ERROR,
3752 /*------
3753 translator: %s is a SQL row locking clause such as FOR UPDATE */
3754 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3755 LCS_asString(strength))));
3756 if (qry->distinctClause != NIL)
3757 ereport(ERROR,
3759 /*------
3760 translator: %s is a SQL row locking clause such as FOR UPDATE */
3761 errmsg("%s is not allowed with DISTINCT clause",
3762 LCS_asString(strength))));
3763 if (qry->groupClause != NIL || qry->groupingSets != NIL)
3764 ereport(ERROR,
3766 /*------
3767 translator: %s is a SQL row locking clause such as FOR UPDATE */
3768 errmsg("%s is not allowed with GROUP BY clause",
3769 LCS_asString(strength))));
3770 if (qry->havingQual != NULL)
3771 ereport(ERROR,
3773 /*------
3774 translator: %s is a SQL row locking clause such as FOR UPDATE */
3775 errmsg("%s is not allowed with HAVING clause",
3776 LCS_asString(strength))));
3777 if (qry->hasAggs)
3778 ereport(ERROR,
3780 /*------
3781 translator: %s is a SQL row locking clause such as FOR UPDATE */
3782 errmsg("%s is not allowed with aggregate functions",
3783 LCS_asString(strength))));
3784 if (qry->hasWindowFuncs)
3785 ereport(ERROR,
3787 /*------
3788 translator: %s is a SQL row locking clause such as FOR UPDATE */
3789 errmsg("%s is not allowed with window functions",
3790 LCS_asString(strength))));
3791 if (qry->hasTargetSRFs)
3792 ereport(ERROR,
3794 /*------
3795 translator: %s is a SQL row locking clause such as FOR UPDATE */
3796 errmsg("%s is not allowed with set-returning functions in the target list",
3797 LCS_asString(strength))));
3798}
3799
3800/*
3801 * Transform a FOR [KEY] UPDATE/SHARE clause
3802 *
3803 * This basically involves replacing names by integer relids.
3804 *
3805 * NB: if you need to change this, see also markQueryForLocking()
3806 * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
3807 */
3808static void
3810 bool pushedDown)
3811{
3812 List *lockedRels = lc->lockedRels;
3813 ListCell *l;
3814 ListCell *rt;
3815 Index i;
3817
3818 CheckSelectLocking(qry, lc->strength);
3819
3820 /* make a clause we can pass down to subqueries to select all rels */
3822 allrels->lockedRels = NIL; /* indicates all rels */
3823 allrels->strength = lc->strength;
3824 allrels->waitPolicy = lc->waitPolicy;
3825
3826 if (lockedRels == NIL)
3827 {
3828 /*
3829 * Lock all regular tables used in query and its subqueries. We
3830 * examine inFromCl to exclude auto-added RTEs, particularly NEW/OLD
3831 * in rules. This is a bit of an abuse of a mostly-obsolete flag, but
3832 * it's convenient. We can't rely on the namespace mechanism that has
3833 * largely replaced inFromCl, since for example we need to lock
3834 * base-relation RTEs even if they are masked by upper joins.
3835 */
3836 i = 0;
3837 foreach(rt, qry->rtable)
3838 {
3840
3841 ++i;
3842 if (!rte->inFromCl)
3843 continue;
3844 switch (rte->rtekind)
3845 {
3846 case RTE_RELATION:
3847 {
3849
3850 applyLockingClause(qry, i,
3851 lc->strength,
3852 lc->waitPolicy,
3853 pushedDown);
3854 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3855 perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3856 }
3857 break;
3858 case RTE_SUBQUERY:
3859 applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
3860 pushedDown);
3861
3862 /*
3863 * FOR UPDATE/SHARE of subquery is propagated to all of
3864 * subquery's rels, too. We could do this later (based on
3865 * the marking of the subquery RTE) but it is convenient
3866 * to have local knowledge in each query level about which
3867 * rels need to be opened with RowShareLock.
3868 */
3869 transformLockingClause(pstate, rte->subquery,
3870 allrels, true);
3871 break;
3872 default:
3873 /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
3874 break;
3875 }
3876 }
3877 }
3878 else
3879 {
3880 /*
3881 * Lock just the named tables. As above, we allow locking any base
3882 * relation regardless of alias-visibility rules, so we need to
3883 * examine inFromCl to exclude OLD/NEW.
3884 */
3885 foreach(l, lockedRels)
3886 {
3887 RangeVar *thisrel = (RangeVar *) lfirst(l);
3888
3889 /* For simplicity we insist on unqualified alias names here */
3890 if (thisrel->catalogname || thisrel->schemaname)
3891 ereport(ERROR,
3893 /*------
3894 translator: %s is a SQL row locking clause such as FOR UPDATE */
3895 errmsg("%s must specify unqualified relation names",
3896 LCS_asString(lc->strength)),
3897 parser_errposition(pstate, thisrel->location)));
3898
3899 i = 0;
3900 foreach(rt, qry->rtable)
3901 {
3903 char *rtename = rte->eref->aliasname;
3904
3905 ++i;
3906 if (!rte->inFromCl)
3907 continue;
3908
3909 /*
3910 * A join RTE without an alias is not visible as a relation
3911 * name and needs to be skipped (otherwise it might hide a
3912 * base relation with the same name), except if it has a USING
3913 * alias, which *is* visible.
3914 *
3915 * Subquery and values RTEs without aliases are never visible
3916 * as relation names and must always be skipped.
3917 */
3918 if (rte->alias == NULL)
3919 {
3920 if (rte->rtekind == RTE_JOIN)
3921 {
3922 if (rte->join_using_alias == NULL)
3923 continue;
3924 rtename = rte->join_using_alias->aliasname;
3925 }
3926 else if (rte->rtekind == RTE_SUBQUERY ||
3927 rte->rtekind == RTE_VALUES)
3928 continue;
3929 }
3930
3931 if (strcmp(rtename, thisrel->relname) == 0)
3932 {
3933 switch (rte->rtekind)
3934 {
3935 case RTE_RELATION:
3936 {
3938
3939 applyLockingClause(qry, i,
3940 lc->strength,
3941 lc->waitPolicy,
3942 pushedDown);
3943 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3944 perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3945 }
3946 break;
3947 case RTE_SUBQUERY:
3948 applyLockingClause(qry, i, lc->strength,
3949 lc->waitPolicy, pushedDown);
3950 /* see comment above */
3951 transformLockingClause(pstate, rte->subquery,
3952 allrels, true);
3953 break;
3954 case RTE_JOIN:
3955 ereport(ERROR,
3957 /*------
3958 translator: %s is a SQL row locking clause such as FOR UPDATE */
3959 errmsg("%s cannot be applied to a join",
3960 LCS_asString(lc->strength)),
3961 parser_errposition(pstate, thisrel->location)));
3962 break;
3963 case RTE_FUNCTION:
3964 ereport(ERROR,
3966 /*------
3967 translator: %s is a SQL row locking clause such as FOR UPDATE */
3968 errmsg("%s cannot be applied to a function",
3969 LCS_asString(lc->strength)),
3970 parser_errposition(pstate, thisrel->location)));
3971 break;
3972 case RTE_TABLEFUNC:
3973 ereport(ERROR,
3975 /*------
3976 translator: %s is a SQL row locking clause such as FOR UPDATE */
3977 errmsg("%s cannot be applied to a table function",
3978 LCS_asString(lc->strength)),
3979 parser_errposition(pstate, thisrel->location)));
3980 break;
3981 case RTE_VALUES:
3982 ereport(ERROR,
3984 /*------
3985 translator: %s is a SQL row locking clause such as FOR UPDATE */
3986 errmsg("%s cannot be applied to VALUES",
3987 LCS_asString(lc->strength)),
3988 parser_errposition(pstate, thisrel->location)));
3989 break;
3990 case RTE_CTE:
3991 ereport(ERROR,
3993 /*------
3994 translator: %s is a SQL row locking clause such as FOR UPDATE */
3995 errmsg("%s cannot be applied to a WITH query",
3996 LCS_asString(lc->strength)),
3997 parser_errposition(pstate, thisrel->location)));
3998 break;
4000 ereport(ERROR,
4002 /*------
4003 translator: %s is a SQL row locking clause such as FOR UPDATE */
4004 errmsg("%s cannot be applied to a named tuplestore",
4005 LCS_asString(lc->strength)),
4006 parser_errposition(pstate, thisrel->location)));
4007 break;
4008
4009 /* Shouldn't be possible to see RTE_RESULT here */
4010
4011 default:
4012 elog(ERROR, "unrecognized RTE type: %d",
4013 (int) rte->rtekind);
4014 break;
4015 }
4016 break; /* out of foreach loop */
4017 }
4018 }
4019 if (rt == NULL)
4020 ereport(ERROR,
4022 /*------
4023 translator: %s is a SQL row locking clause such as FOR UPDATE */
4024 errmsg("relation \"%s\" in %s clause not found in FROM clause",
4025 thisrel->relname,
4026 LCS_asString(lc->strength)),
4027 parser_errposition(pstate, thisrel->location)));
4028 }
4029 }
4030}
4031
4032/*
4033 * Record locking info for a single rangetable item
4034 */
4035void
4037 LockClauseStrength strength, LockWaitPolicy waitPolicy,
4038 bool pushedDown)
4039{
4040 RowMarkClause *rc;
4041
4042 Assert(strength != LCS_NONE); /* else caller error */
4043
4044 /* If it's an explicit clause, make sure hasForUpdate gets set */
4045 if (!pushedDown)
4046 qry->hasForUpdate = true;
4047
4048 /* Check for pre-existing entry for same rtindex */
4049 if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
4050 {
4051 /*
4052 * If the same RTE is specified with more than one locking strength,
4053 * use the strongest. (Reasonable, since you can't take both a shared
4054 * and exclusive lock at the same time; it'll end up being exclusive
4055 * anyway.)
4056 *
4057 * Similarly, if the same RTE is specified with more than one lock
4058 * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
4059 * turn wins over waiting for the lock (the default). This is a bit
4060 * more debatable but raising an error doesn't seem helpful. (Consider
4061 * for instance SELECT FOR UPDATE NOWAIT from a view that internally
4062 * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
4063 * LOCKED is reasonable since the former throws an error in case of
4064 * coming across a locked tuple, which may be undesirable in some
4065 * cases but it seems better than silently returning inconsistent
4066 * results.
4067 *
4068 * And of course pushedDown becomes false if any clause is explicit.
4069 */
4070 rc->strength = Max(rc->strength, strength);
4071 rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
4072 rc->pushedDown &= pushedDown;
4073 return;
4074 }
4075
4076 /* Make a new RowMarkClause */
4077 rc = makeNode(RowMarkClause);
4078 rc->rti = rtindex;
4079 rc->strength = strength;
4080 rc->waitPolicy = waitPolicy;
4081 rc->pushedDown = pushedDown;
4082 qry->rowMarks = lappend(qry->rowMarks, rc);
4083}
4084
4085#ifdef DEBUG_NODE_TESTS_ENABLED
4086/*
4087 * Coverage testing for raw_expression_tree_walker().
4088 *
4089 * When enabled, we run raw_expression_tree_walker() over every DML statement
4090 * submitted to parse analysis. Without this provision, that function is only
4091 * applied in limited cases involving CTEs, and we don't really want to have
4092 * to test everything inside as well as outside a CTE.
4093 */
4094static bool
4095test_raw_expression_coverage(Node *node, void *context)
4096{
4097 if (node == NULL)
4098 return false;
4099 return raw_expression_tree_walker(node,
4101 context);
4102}
4103#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:672
List * expand_function_arguments(List *args, bool include_out_arguments, Oid result_type, HeapTuple func_tuple)
Definition clauses.c:4914
@ 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:875
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:3602
RegProcedure get_range_constructor2(Oid rangeOid)
Definition lsyscache.c:3653
bool type_is_range(Oid typid)
Definition lsyscache.c:2883
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
Definition lsyscache.c:1380
RegProcedure get_opcode(Oid opno)
Definition lsyscache.c:1478
Oid getBaseType(Oid typid)
Definition lsyscache.c:2716
bool type_is_multirange(Oid typid)
Definition lsyscache.c:2893
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:1212
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:3809
static Query * transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
Definition analyze.c:575
void CheckSelectLocking(Query *qry, LockClauseStrength strength)
Definition analyze.c:3745
SortGroupClause * makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
Definition analyze.c:2377
static ForPortionOfExpr * transformForPortionOfClause(ParseState *pstate, int rtindex, const ForPortionOfClause *forPortionOf, bool isUpdate)
Definition analyze.c:1319
static Node * transformSetOperationTree(ParseState *pstate, SelectStmt *stmt, bool isTopLevel, List **targetlist)
Definition analyze.c:2425
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:1102
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Definition analyze.c:4036
static void determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
Definition analyze.c:2773
void constructSetOpTargetlist(ParseState *pstate, SetOperationStmt *op, const List *ltargetlist, const List *rtargetlist, List **targetlist, const char *context, bool recursive)
Definition analyze.c:2609
static Query * transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
Definition analyze.c:2827
static void addNSItemForReturning(ParseState *pstate, const char *aliasname, VarReturningType returning_type)
Definition analyze.c:3028
void transformReturningClause(ParseState *pstate, Query *qry, ReturningClause *returningClause, ParseExprKind exprKind)
Definition analyze.c:3067
static Query * transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
Definition analyze.c:3189
static Query * transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
Definition analyze.c:3516
post_parse_analyze_hook_type post_parse_analyze_hook
Definition analyze.c:74
static Query * transformCallStmt(ParseState *pstate, CallStmt *stmt)
Definition analyze.c:3595
static Query * transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
Definition analyze.c:2120
List * transformUpdateTargetList(ParseState *pstate, List *origTlist, ForPortionOfExpr *forPortionOf)
Definition analyze.c:2939
bool query_requires_rewrite_plan(Query *query)
Definition analyze.c:542
static Query * transformSelectStmt(ParseState *pstate, SelectStmt *stmt, SelectStmtPassthrough *passthru)
Definition analyze.c:1747
Query * transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
Definition analyze.c:271
const char * LCS_asString(LockClauseStrength strength)
Definition analyze.c:3720
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:2858
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition analyze.c:244
static Query * transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
Definition analyze.c:3464
List * BuildOnConflictExcludedTargetlist(Relation targetrel, Index exclRelIndex)
Definition analyze.c:1628
static List * transformPLAssignStmtTarget(ParseState *pstate, List *tlist, SelectStmtPassthrough *passthru)
Definition analyze.c:3264
static int count_rowexpr_columns(ParseState *pstate, Node *expr)
Definition analyze.c:1698
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:3371
static Query * transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
Definition analyze.c:661
static Query * transformValuesClause(ParseState *pstate, SelectStmt *stmt)
Definition analyze.c:1901
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
Form_pg_class rd_rel
Definition rel.h:111
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