]> granicus.if.org Git - postgresql/blob - src/backend/parser/analyze.c
21342e8a9d7f5535ae3a5a016d3c122d0229491e
[postgresql] / src / backend / parser / analyze.c
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 and EXPLAIN are exceptions because they contain
14  * optimizable statements.
15  *
16  *
17  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
18  * Portions Copyright (c) 1994, Regents of the University of California
19  *
20  *      src/backend/parser/analyze.c
21  *
22  *-------------------------------------------------------------------------
23  */
24
25 #include "postgres.h"
26
27 #include "access/sysattr.h"
28 #include "catalog/pg_type.h"
29 #include "nodes/makefuncs.h"
30 #include "nodes/nodeFuncs.h"
31 #include "optimizer/var.h"
32 #include "parser/analyze.h"
33 #include "parser/parse_agg.h"
34 #include "parser/parse_clause.h"
35 #include "parser/parse_coerce.h"
36 #include "parser/parse_cte.h"
37 #include "parser/parse_oper.h"
38 #include "parser/parse_param.h"
39 #include "parser/parse_relation.h"
40 #include "parser/parse_target.h"
41 #include "parser/parsetree.h"
42 #include "rewrite/rewriteManip.h"
43 #include "utils/rel.h"
44
45
46 static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
47 static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
48 static List *transformInsertRow(ParseState *pstate, List *exprlist,
49                                    List *stmtcols, List *icolumns, List *attrnos);
50 static int      count_rowexpr_columns(ParseState *pstate, Node *expr);
51 static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
52 static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
53 static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
54 static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
55                                                   bool isTopLevel, List **colInfo);
56 static void determineRecursiveColTypes(ParseState *pstate,
57                                                    Node *larg, List *lcolinfo);
58 static void applyColumnNames(List *dst, List *src);
59 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
60 static List *transformReturningList(ParseState *pstate, List *returningList);
61 static Query *transformDeclareCursorStmt(ParseState *pstate,
62                                                    DeclareCursorStmt *stmt);
63 static Query *transformExplainStmt(ParseState *pstate,
64                                          ExplainStmt *stmt);
65 static void transformLockingClause(ParseState *pstate, Query *qry,
66                                            LockingClause *lc, bool pushedDown);
67
68
69 /*
70  * parse_analyze
71  *              Analyze a raw parse tree and transform it to Query form.
72  *
73  * Optionally, information about $n parameter types can be supplied.
74  * References to $n indexes not defined by paramTypes[] are disallowed.
75  *
76  * The result is a Query node.  Optimizable statements require considerable
77  * transformation, while utility-type statements are simply hung off
78  * a dummy CMD_UTILITY Query node.
79  */
80 Query *
81 parse_analyze(Node *parseTree, const char *sourceText,
82                           Oid *paramTypes, int numParams)
83 {
84         ParseState *pstate = make_parsestate(NULL);
85         Query      *query;
86
87         Assert(sourceText != NULL); /* required as of 8.4 */
88
89         pstate->p_sourcetext = sourceText;
90
91         if (numParams > 0)
92                 parse_fixed_parameters(pstate, paramTypes, numParams);
93
94         query = transformStmt(pstate, parseTree);
95
96         free_parsestate(pstate);
97
98         return query;
99 }
100
101 /*
102  * parse_analyze_varparams
103  *
104  * This variant is used when it's okay to deduce information about $n
105  * symbol datatypes from context.  The passed-in paramTypes[] array can
106  * be modified or enlarged (via repalloc).
107  */
108 Query *
109 parse_analyze_varparams(Node *parseTree, const char *sourceText,
110                                                 Oid **paramTypes, int *numParams)
111 {
112         ParseState *pstate = make_parsestate(NULL);
113         Query      *query;
114
115         Assert(sourceText != NULL); /* required as of 8.4 */
116
117         pstate->p_sourcetext = sourceText;
118
119         parse_variable_parameters(pstate, paramTypes, numParams);
120
121         query = transformStmt(pstate, parseTree);
122
123         /* make sure all is well with parameter types */
124         check_variable_parameters(pstate, query);
125
126         free_parsestate(pstate);
127
128         return query;
129 }
130
131 /*
132  * parse_sub_analyze
133  *              Entry point for recursively analyzing a sub-statement.
134  */
135 Query *
136 parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
137                                   CommonTableExpr *parentCTE,
138                                   bool locked_from_parent)
139 {
140         ParseState *pstate = make_parsestate(parentParseState);
141         Query      *query;
142
143         pstate->p_parent_cte = parentCTE;
144         pstate->p_locked_from_parent = locked_from_parent;
145
146         query = transformStmt(pstate, parseTree);
147
148         free_parsestate(pstate);
149
150         return query;
151 }
152
153 /*
154  * transformStmt -
155  *        transform a Parse tree into a Query tree.
156  */
157 Query *
158 transformStmt(ParseState *pstate, Node *parseTree)
159 {
160         Query      *result;
161
162         switch (nodeTag(parseTree))
163         {
164                         /*
165                          * Optimizable statements
166                          */
167                 case T_InsertStmt:
168                         result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
169                         break;
170
171                 case T_DeleteStmt:
172                         result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
173                         break;
174
175                 case T_UpdateStmt:
176                         result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
177                         break;
178
179                 case T_SelectStmt:
180                         {
181                                 SelectStmt *n = (SelectStmt *) parseTree;
182
183                                 if (n->valuesLists)
184                                         result = transformValuesClause(pstate, n);
185                                 else if (n->op == SETOP_NONE)
186                                         result = transformSelectStmt(pstate, n);
187                                 else
188                                         result = transformSetOperationStmt(pstate, n);
189                         }
190                         break;
191
192                         /*
193                          * Special cases
194                          */
195                 case T_DeclareCursorStmt:
196                         result = transformDeclareCursorStmt(pstate,
197                                                                                         (DeclareCursorStmt *) parseTree);
198                         break;
199
200                 case T_ExplainStmt:
201                         result = transformExplainStmt(pstate,
202                                                                                   (ExplainStmt *) parseTree);
203                         break;
204
205                 default:
206
207                         /*
208                          * other statements don't require any transformation; just return
209                          * the original parsetree with a Query node plastered on top.
210                          */
211                         result = makeNode(Query);
212                         result->commandType = CMD_UTILITY;
213                         result->utilityStmt = (Node *) parseTree;
214                         break;
215         }
216
217         /* Mark as original query until we learn differently */
218         result->querySource = QSRC_ORIGINAL;
219         result->canSetTag = true;
220
221         return result;
222 }
223
224 /*
225  * analyze_requires_snapshot
226  *              Returns true if a snapshot must be set before doing parse analysis
227  *              on the given raw parse tree.
228  *
229  * Classification here should match transformStmt(); but we also have to
230  * allow a NULL input (for Parse/Bind of an empty query string).
231  */
232 bool
233 analyze_requires_snapshot(Node *parseTree)
234 {
235         bool            result;
236
237         if (parseTree == NULL)
238                 return false;
239
240         switch (nodeTag(parseTree))
241         {
242                         /*
243                          * Optimizable statements
244                          */
245                 case T_InsertStmt:
246                 case T_DeleteStmt:
247                 case T_UpdateStmt:
248                 case T_SelectStmt:
249                         result = true;
250                         break;
251
252                         /*
253                          * Special cases
254                          */
255                 case T_DeclareCursorStmt:
256                         /* yes, because it's analyzed just like SELECT */
257                         result = true;
258                         break;
259
260                 case T_ExplainStmt:
261                         /* yes, because we must analyze the contained statement */
262                         result = true;
263                         break;
264
265                 default:
266                         /* other utility statements don't have any real parse analysis */
267                         result = false;
268                         break;
269         }
270
271         return result;
272 }
273
274 /*
275  * transformDeleteStmt -
276  *        transforms a Delete Statement
277  */
278 static Query *
279 transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
280 {
281         Query      *qry = makeNode(Query);
282         Node       *qual;
283
284         qry->commandType = CMD_DELETE;
285
286         /* set up range table with just the result rel */
287         qry->resultRelation = setTargetTable(pstate, stmt->relation,
288                                                                   interpretInhOption(stmt->relation->inhOpt),
289                                                                                  true,
290                                                                                  ACL_DELETE);
291
292         qry->distinctClause = NIL;
293
294         /*
295          * The USING clause is non-standard SQL syntax, and is equivalent in
296          * functionality to the FROM list that can be specified for UPDATE. The
297          * USING keyword is used rather than FROM because FROM is already a
298          * keyword in the DELETE syntax.
299          */
300         transformFromClause(pstate, stmt->usingClause);
301
302         qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
303
304         qry->returningList = transformReturningList(pstate, stmt->returningList);
305
306         /* done building the range table and jointree */
307         qry->rtable = pstate->p_rtable;
308         qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
309
310         qry->hasSubLinks = pstate->p_hasSubLinks;
311         qry->hasAggs = pstate->p_hasAggs;
312         if (pstate->p_hasAggs)
313                 parseCheckAggregates(pstate, qry);
314         qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
315         if (pstate->p_hasWindowFuncs)
316                 parseCheckWindowFuncs(pstate, qry);
317
318         return qry;
319 }
320
321 /*
322  * transformInsertStmt -
323  *        transform an Insert Statement
324  */
325 static Query *
326 transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
327 {
328         Query      *qry = makeNode(Query);
329         SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
330         List       *exprList = NIL;
331         bool            isGeneralSelect;
332         List       *sub_rtable;
333         List       *sub_relnamespace;
334         List       *sub_varnamespace;
335         List       *icolumns;
336         List       *attrnos;
337         RangeTblEntry *rte;
338         RangeTblRef *rtr;
339         ListCell   *icols;
340         ListCell   *attnos;
341         ListCell   *lc;
342
343         qry->commandType = CMD_INSERT;
344         pstate->p_is_insert = true;
345
346         /*
347          * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
348          * VALUES list, or general SELECT input.  We special-case VALUES, both for
349          * efficiency and so we can handle DEFAULT specifications.
350          *
351          * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
352          * VALUES clause.  If we have any of those, treat it as a general SELECT;
353          * so it will work, but you can't use DEFAULT items together with those.
354          */
355         isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
356                                                                           selectStmt->sortClause != NIL ||
357                                                                           selectStmt->limitOffset != NULL ||
358                                                                           selectStmt->limitCount != NULL ||
359                                                                           selectStmt->lockingClause != NIL ||
360                                                                           selectStmt->withClause != NULL));
361
362         /*
363          * If a non-nil rangetable/namespace was passed in, and we are doing
364          * INSERT/SELECT, arrange to pass the rangetable/namespace down to the
365          * SELECT.      This can only happen if we are inside a CREATE RULE, and in
366          * that case we want the rule's OLD and NEW rtable entries to appear as
367          * part of the SELECT's rtable, not as outer references for it.  (Kluge!)
368          * The SELECT's joinlist is not affected however.  We must do this before
369          * adding the target table to the INSERT's rtable.
370          */
371         if (isGeneralSelect)
372         {
373                 sub_rtable = pstate->p_rtable;
374                 pstate->p_rtable = NIL;
375                 sub_relnamespace = pstate->p_relnamespace;
376                 pstate->p_relnamespace = NIL;
377                 sub_varnamespace = pstate->p_varnamespace;
378                 pstate->p_varnamespace = NIL;
379                 /* There can't be any outer WITH to worry about */
380                 Assert(pstate->p_ctenamespace == NIL);
381         }
382         else
383         {
384                 sub_rtable = NIL;               /* not used, but keep compiler quiet */
385                 sub_relnamespace = NIL;
386                 sub_varnamespace = NIL;
387         }
388
389         /*
390          * Must get write lock on INSERT target table before scanning SELECT, else
391          * we will grab the wrong kind of initial lock if the target table is also
392          * mentioned in the SELECT part.  Note that the target table is not added
393          * to the joinlist or namespace.
394          */
395         qry->resultRelation = setTargetTable(pstate, stmt->relation,
396                                                                                  false, false, ACL_INSERT);
397
398         /* Validate stmt->cols list, or build default list if no list given */
399         icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
400         Assert(list_length(icolumns) == list_length(attrnos));
401
402         /*
403          * Determine which variant of INSERT we have.
404          */
405         if (selectStmt == NULL)
406         {
407                 /*
408                  * We have INSERT ... DEFAULT VALUES.  We can handle this case by
409                  * emitting an empty targetlist --- all columns will be defaulted when
410                  * the planner expands the targetlist.
411                  */
412                 exprList = NIL;
413         }
414         else if (isGeneralSelect)
415         {
416                 /*
417                  * We make the sub-pstate a child of the outer pstate so that it can
418                  * see any Param definitions supplied from above.  Since the outer
419                  * pstate's rtable and namespace are presently empty, there are no
420                  * side-effects of exposing names the sub-SELECT shouldn't be able to
421                  * see.
422                  */
423                 ParseState *sub_pstate = make_parsestate(pstate);
424                 Query      *selectQuery;
425
426                 /*
427                  * Process the source SELECT.
428                  *
429                  * It is important that this be handled just like a standalone SELECT;
430                  * otherwise the behavior of SELECT within INSERT might be different
431                  * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
432                  * bugs of just that nature...)
433                  */
434                 sub_pstate->p_rtable = sub_rtable;
435                 sub_pstate->p_joinexprs = NIL;  /* sub_rtable has no joins */
436                 sub_pstate->p_relnamespace = sub_relnamespace;
437                 sub_pstate->p_varnamespace = sub_varnamespace;
438
439                 selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
440
441                 free_parsestate(sub_pstate);
442
443                 /* The grammar should have produced a SELECT, but it might have INTO */
444                 if (!IsA(selectQuery, Query) ||
445                         selectQuery->commandType != CMD_SELECT ||
446                         selectQuery->utilityStmt != NULL)
447                         elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
448                 if (selectQuery->intoClause)
449                         ereport(ERROR,
450                                         (errcode(ERRCODE_SYNTAX_ERROR),
451                                          errmsg("INSERT ... SELECT cannot specify INTO"),
452                                          parser_errposition(pstate,
453                                                    exprLocation((Node *) selectQuery->intoClause))));
454
455                 /*
456                  * Make the source be a subquery in the INSERT's rangetable, and add
457                  * it to the INSERT's joinlist.
458                  */
459                 rte = addRangeTableEntryForSubquery(pstate,
460                                                                                         selectQuery,
461                                                                                         makeAlias("*SELECT*", NIL),
462                                                                                         false);
463                 rtr = makeNode(RangeTblRef);
464                 /* assume new rte is at end */
465                 rtr->rtindex = list_length(pstate->p_rtable);
466                 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
467                 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
468
469                 /*----------
470                  * Generate an expression list for the INSERT that selects all the
471                  * non-resjunk columns from the subquery.  (INSERT's tlist must be
472                  * separate from the subquery's tlist because we may add columns,
473                  * insert datatype coercions, etc.)
474                  *
475                  * HACK: unknown-type constants and params in the SELECT's targetlist
476                  * are copied up as-is rather than being referenced as subquery
477                  * outputs.  This is to ensure that when we try to coerce them to
478                  * the target column's datatype, the right things happen (see
479                  * special cases in coerce_type).  Otherwise, this fails:
480                  *              INSERT INTO foo SELECT 'bar', ... FROM baz
481                  *----------
482                  */
483                 exprList = NIL;
484                 foreach(lc, selectQuery->targetList)
485                 {
486                         TargetEntry *tle = (TargetEntry *) lfirst(lc);
487                         Expr       *expr;
488
489                         if (tle->resjunk)
490                                 continue;
491                         if (tle->expr &&
492                                 (IsA(tle->expr, Const) ||IsA(tle->expr, Param)) &&
493                                 exprType((Node *) tle->expr) == UNKNOWNOID)
494                                 expr = tle->expr;
495                         else
496                         {
497                                 Var                *var = makeVarFromTargetEntry(rtr->rtindex, tle);
498
499                                 var->location = exprLocation((Node *) tle->expr);
500                                 expr = (Expr *) var;
501                         }
502                         exprList = lappend(exprList, expr);
503                 }
504
505                 /* Prepare row for assignment to target table */
506                 exprList = transformInsertRow(pstate, exprList,
507                                                                           stmt->cols,
508                                                                           icolumns, attrnos);
509         }
510         else if (list_length(selectStmt->valuesLists) > 1)
511         {
512                 /*
513                  * Process INSERT ... VALUES with multiple VALUES sublists. We
514                  * generate a VALUES RTE holding the transformed expression lists, and
515                  * build up a targetlist containing Vars that reference the VALUES
516                  * RTE.
517                  */
518                 List       *exprsLists = NIL;
519                 int                     sublist_length = -1;
520
521                 /* process the WITH clause */
522                 if (selectStmt->withClause)
523                 {
524                         qry->hasRecursive = selectStmt->withClause->recursive;
525                         qry->cteList = transformWithClause(pstate, selectStmt->withClause);
526                 }
527
528                 foreach(lc, selectStmt->valuesLists)
529                 {
530                         List       *sublist = (List *) lfirst(lc);
531
532                         /* Do basic expression transformation (same as a ROW() expr) */
533                         sublist = transformExpressionList(pstate, sublist);
534
535                         /*
536                          * All the sublists must be the same length, *after*
537                          * transformation (which might expand '*' into multiple items).
538                          * The VALUES RTE can't handle anything different.
539                          */
540                         if (sublist_length < 0)
541                         {
542                                 /* Remember post-transformation length of first sublist */
543                                 sublist_length = list_length(sublist);
544                         }
545                         else if (sublist_length != list_length(sublist))
546                         {
547                                 ereport(ERROR,
548                                                 (errcode(ERRCODE_SYNTAX_ERROR),
549                                                  errmsg("VALUES lists must all be the same length"),
550                                                  parser_errposition(pstate,
551                                                                                         exprLocation((Node *) sublist))));
552                         }
553
554                         /* Prepare row for assignment to target table */
555                         sublist = transformInsertRow(pstate, sublist,
556                                                                                  stmt->cols,
557                                                                                  icolumns, attrnos);
558
559                         exprsLists = lappend(exprsLists, sublist);
560                 }
561
562                 /*
563                  * There mustn't have been any table references in the expressions,
564                  * else strange things would happen, like Cartesian products of those
565                  * tables with the VALUES list ...
566                  */
567                 if (pstate->p_joinlist != NIL)
568                         ereport(ERROR,
569                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
570                                          errmsg("VALUES must not contain table references"),
571                                          parser_errposition(pstate,
572                                                           locate_var_of_level((Node *) exprsLists, 0))));
573
574                 /*
575                  * Another thing we can't currently support is NEW/OLD references in
576                  * rules --- seems we'd need something like SQL99's LATERAL construct
577                  * to ensure that the values would be available while evaluating the
578                  * VALUES RTE.  This is a shame.  FIXME
579                  */
580                 if (list_length(pstate->p_rtable) != 1 &&
581                         contain_vars_of_level((Node *) exprsLists, 0))
582                         ereport(ERROR,
583                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
584                                          errmsg("VALUES must not contain OLD or NEW references"),
585                                          errhint("Use SELECT ... UNION ALL ... instead."),
586                                          parser_errposition(pstate,
587                                                           locate_var_of_level((Node *) exprsLists, 0))));
588
589                 /*
590                  * Generate the VALUES RTE
591                  */
592                 rte = addRangeTableEntryForValues(pstate, exprsLists, NULL, true);
593                 rtr = makeNode(RangeTblRef);
594                 /* assume new rte is at end */
595                 rtr->rtindex = list_length(pstate->p_rtable);
596                 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
597                 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
598
599                 /*
600                  * Generate list of Vars referencing the RTE
601                  */
602                 expandRTE(rte, rtr->rtindex, 0, -1, false, NULL, &exprList);
603         }
604         else
605         {
606                 /*----------
607                  * Process INSERT ... VALUES with a single VALUES sublist.
608                  * We treat this separately for efficiency and for historical
609                  * compatibility --- specifically, allowing table references,
610                  * such as
611                  *                      INSERT INTO foo VALUES(bar.*)
612                  *
613                  * The sublist is just computed directly as the Query's targetlist,
614                  * with no VALUES RTE.  So it works just like SELECT without FROM.
615                  *----------
616                  */
617                 List       *valuesLists = selectStmt->valuesLists;
618
619                 Assert(list_length(valuesLists) == 1);
620
621                 /* process the WITH clause */
622                 if (selectStmt->withClause)
623                 {
624                         qry->hasRecursive = selectStmt->withClause->recursive;
625                         qry->cteList = transformWithClause(pstate, selectStmt->withClause);
626                 }
627
628                 /* Do basic expression transformation (same as a ROW() expr) */
629                 exprList = transformExpressionList(pstate,
630                                                                                    (List *) linitial(valuesLists));
631
632                 /* Prepare row for assignment to target table */
633                 exprList = transformInsertRow(pstate, exprList,
634                                                                           stmt->cols,
635                                                                           icolumns, attrnos);
636         }
637
638         /*
639          * Generate query's target list using the computed list of expressions.
640          * Also, mark all the target columns as needing insert permissions.
641          */
642         rte = pstate->p_target_rangetblentry;
643         qry->targetList = NIL;
644         icols = list_head(icolumns);
645         attnos = list_head(attrnos);
646         foreach(lc, exprList)
647         {
648                 Expr       *expr = (Expr *) lfirst(lc);
649                 ResTarget  *col;
650                 AttrNumber      attr_num;
651                 TargetEntry *tle;
652
653                 col = (ResTarget *) lfirst(icols);
654                 Assert(IsA(col, ResTarget));
655                 attr_num = (AttrNumber) lfirst_int(attnos);
656
657                 tle = makeTargetEntry(expr,
658                                                           attr_num,
659                                                           col->name,
660                                                           false);
661                 qry->targetList = lappend(qry->targetList, tle);
662
663                 rte->modifiedCols = bms_add_member(rte->modifiedCols,
664                                                           attr_num - FirstLowInvalidHeapAttributeNumber);
665
666                 icols = lnext(icols);
667                 attnos = lnext(attnos);
668         }
669
670         /*
671          * If we have a RETURNING clause, we need to add the target relation to
672          * the query namespace before processing it, so that Var references in
673          * RETURNING will work.  Also, remove any namespace entries added in a
674          * sub-SELECT or VALUES list.
675          */
676         if (stmt->returningList)
677         {
678                 pstate->p_relnamespace = NIL;
679                 pstate->p_varnamespace = NIL;
680                 addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
681                                           false, true, true);
682                 qry->returningList = transformReturningList(pstate,
683                                                                                                         stmt->returningList);
684         }
685
686         /* done building the range table and jointree */
687         qry->rtable = pstate->p_rtable;
688         qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
689
690         qry->hasSubLinks = pstate->p_hasSubLinks;
691         /* aggregates not allowed (but subselects are okay) */
692         if (pstate->p_hasAggs)
693                 ereport(ERROR,
694                                 (errcode(ERRCODE_GROUPING_ERROR),
695                                  errmsg("cannot use aggregate function in VALUES"),
696                                  parser_errposition(pstate,
697                                                                         locate_agg_of_level((Node *) qry, 0))));
698         if (pstate->p_hasWindowFuncs)
699                 ereport(ERROR,
700                                 (errcode(ERRCODE_WINDOWING_ERROR),
701                                  errmsg("cannot use window function in VALUES"),
702                                  parser_errposition(pstate,
703                                                                         locate_windowfunc((Node *) qry))));
704
705         return qry;
706 }
707
708 /*
709  * Prepare an INSERT row for assignment to the target table.
710  *
711  * The row might be either a VALUES row, or variables referencing a
712  * sub-SELECT output.
713  */
714 static List *
715 transformInsertRow(ParseState *pstate, List *exprlist,
716                                    List *stmtcols, List *icolumns, List *attrnos)
717 {
718         List       *result;
719         ListCell   *lc;
720         ListCell   *icols;
721         ListCell   *attnos;
722
723         /*
724          * Check length of expr list.  It must not have more expressions than
725          * there are target columns.  We allow fewer, but only if no explicit
726          * columns list was given (the remaining columns are implicitly
727          * defaulted).  Note we must check this *after* transformation because
728          * that could expand '*' into multiple items.
729          */
730         if (list_length(exprlist) > list_length(icolumns))
731                 ereport(ERROR,
732                                 (errcode(ERRCODE_SYNTAX_ERROR),
733                                  errmsg("INSERT has more expressions than target columns"),
734                                  parser_errposition(pstate,
735                                                                         exprLocation(list_nth(exprlist,
736                                                                                                   list_length(icolumns))))));
737         if (stmtcols != NIL &&
738                 list_length(exprlist) < list_length(icolumns))
739         {
740                 /*
741                  * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
742                  * where the user accidentally created a RowExpr instead of separate
743                  * columns.  Add a suitable hint if that seems to be the problem,
744                  * because the main error message is quite misleading for this case.
745                  * (If there's no stmtcols, you'll get something about data type
746                  * mismatch, which is less misleading so we don't worry about giving
747                  * a hint in that case.)
748                  */
749                 ereport(ERROR,
750                                 (errcode(ERRCODE_SYNTAX_ERROR),
751                                  errmsg("INSERT has more target columns than expressions"),
752                                  ((list_length(exprlist) == 1 &&
753                                    count_rowexpr_columns(pstate, linitial(exprlist)) ==
754                                    list_length(icolumns)) ?
755                                   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),
756                                  parser_errposition(pstate,
757                                                                         exprLocation(list_nth(icolumns,
758                                                                                                   list_length(exprlist))))));
759         }
760
761         /*
762          * Prepare columns for assignment to target table.
763          */
764         result = NIL;
765         icols = list_head(icolumns);
766         attnos = list_head(attrnos);
767         foreach(lc, exprlist)
768         {
769                 Expr       *expr = (Expr *) lfirst(lc);
770                 ResTarget  *col;
771
772                 col = (ResTarget *) lfirst(icols);
773                 Assert(IsA(col, ResTarget));
774
775                 expr = transformAssignedExpr(pstate, expr,
776                                                                          col->name,
777                                                                          lfirst_int(attnos),
778                                                                          col->indirection,
779                                                                          col->location);
780
781                 result = lappend(result, expr);
782
783                 icols = lnext(icols);
784                 attnos = lnext(attnos);
785         }
786
787         return result;
788 }
789
790 /*
791  * count_rowexpr_columns -
792  *        get number of columns contained in a ROW() expression;
793  *        return -1 if expression isn't a RowExpr or a Var referencing one.
794  *
795  * This is currently used only for hint purposes, so we aren't terribly
796  * tense about recognizing all possible cases.  The Var case is interesting
797  * because that's what we'll get in the INSERT ... SELECT (...) case.
798  */
799 static int
800 count_rowexpr_columns(ParseState *pstate, Node *expr)
801 {
802         if (expr == NULL)
803                 return -1;
804         if (IsA(expr, RowExpr))
805                 return list_length(((RowExpr *) expr)->args);
806         if (IsA(expr, Var))
807         {
808                 Var                *var = (Var *) expr;
809                 AttrNumber      attnum = var->varattno;
810
811                 if (attnum > 0 && var->vartype == RECORDOID)
812                 {
813                         RangeTblEntry *rte;
814
815                         rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
816                         if (rte->rtekind == RTE_SUBQUERY)
817                         {
818                                 /* Subselect-in-FROM: examine sub-select's output expr */
819                                 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
820                                                                                                         attnum);
821
822                                 if (ste == NULL || ste->resjunk)
823                                         return -1;
824                                 expr = (Node *) ste->expr;
825                                 if (IsA(expr, RowExpr))
826                                         return list_length(((RowExpr *) expr)->args);
827                         }
828                 }
829         }
830         return -1;
831 }
832
833
834 /*
835  * transformSelectStmt -
836  *        transforms a Select Statement
837  *
838  * Note: this covers only cases with no set operations and no VALUES lists;
839  * see below for the other cases.
840  */
841 static Query *
842 transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
843 {
844         Query      *qry = makeNode(Query);
845         Node       *qual;
846         ListCell   *l;
847
848         qry->commandType = CMD_SELECT;
849
850         /* process the WITH clause independently of all else */
851         if (stmt->withClause)
852         {
853                 qry->hasRecursive = stmt->withClause->recursive;
854                 qry->cteList = transformWithClause(pstate, stmt->withClause);
855         }
856
857         /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
858         pstate->p_locking_clause = stmt->lockingClause;
859
860         /* make WINDOW info available for window functions, too */
861         pstate->p_windowdefs = stmt->windowClause;
862
863         /* process the FROM clause */
864         transformFromClause(pstate, stmt->fromClause);
865
866         /* transform targetlist */
867         qry->targetList = transformTargetList(pstate, stmt->targetList);
868
869         /* mark column origins */
870         markTargetListOrigins(pstate, qry->targetList);
871
872         /* transform WHERE */
873         qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
874
875         /*
876          * Initial processing of HAVING clause is just like WHERE clause.
877          */
878         qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
879                                                                                    "HAVING");
880
881         /*
882          * Transform sorting/grouping stuff.  Do ORDER BY first because both
883          * transformGroupClause and transformDistinctClause need the results. Note
884          * that these functions can also change the targetList, so it's passed to
885          * them by reference.
886          */
887         qry->sortClause = transformSortClause(pstate,
888                                                                                   stmt->sortClause,
889                                                                                   &qry->targetList,
890                                                                                   true /* fix unknowns */ ,
891                                                                                   false /* allow SQL92 rules */ );
892
893         qry->groupClause = transformGroupClause(pstate,
894                                                                                         stmt->groupClause,
895                                                                                         &qry->targetList,
896                                                                                         qry->sortClause,
897                                                                                         false /* allow SQL92 rules */ );
898
899         if (stmt->distinctClause == NIL)
900         {
901                 qry->distinctClause = NIL;
902                 qry->hasDistinctOn = false;
903         }
904         else if (linitial(stmt->distinctClause) == NULL)
905         {
906                 /* We had SELECT DISTINCT */
907                 qry->distinctClause = transformDistinctClause(pstate,
908                                                                                                           &qry->targetList,
909                                                                                                           qry->sortClause,
910                                                                                                           false);
911                 qry->hasDistinctOn = false;
912         }
913         else
914         {
915                 /* We had SELECT DISTINCT ON */
916                 qry->distinctClause = transformDistinctOnClause(pstate,
917                                                                                                                 stmt->distinctClause,
918                                                                                                                 &qry->targetList,
919                                                                                                                 qry->sortClause);
920                 qry->hasDistinctOn = true;
921         }
922
923         /* transform LIMIT */
924         qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
925                                                                                         "OFFSET");
926         qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
927                                                                                    "LIMIT");
928
929         /* transform window clauses after we have seen all window functions */
930         qry->windowClause = transformWindowDefinitions(pstate,
931                                                                                                    pstate->p_windowdefs,
932                                                                                                    &qry->targetList);
933
934         /* handle any SELECT INTO/CREATE TABLE AS spec */
935         if (stmt->intoClause)
936         {
937                 qry->intoClause = stmt->intoClause;
938                 if (stmt->intoClause->colNames)
939                         applyColumnNames(qry->targetList, stmt->intoClause->colNames);
940         }
941
942         qry->rtable = pstate->p_rtable;
943         qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
944
945         qry->hasSubLinks = pstate->p_hasSubLinks;
946         qry->hasAggs = pstate->p_hasAggs;
947         if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
948                 parseCheckAggregates(pstate, qry);
949         qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
950         if (pstate->p_hasWindowFuncs)
951                 parseCheckWindowFuncs(pstate, qry);
952
953         foreach(l, stmt->lockingClause)
954         {
955                 transformLockingClause(pstate, qry,
956                                                            (LockingClause *) lfirst(l), false);
957         }
958
959         return qry;
960 }
961
962 /*
963  * transformValuesClause -
964  *        transforms a VALUES clause that's being used as a standalone SELECT
965  *
966  * We build a Query containing a VALUES RTE, rather as if one had written
967  *                      SELECT * FROM (VALUES ...)
968  */
969 static Query *
970 transformValuesClause(ParseState *pstate, SelectStmt *stmt)
971 {
972         Query      *qry = makeNode(Query);
973         List       *exprsLists = NIL;
974         List      **colexprs = NULL;
975         Oid                *coltypes = NULL;
976         int                     sublist_length = -1;
977         List       *newExprsLists;
978         RangeTblEntry *rte;
979         RangeTblRef *rtr;
980         ListCell   *lc;
981         ListCell   *lc2;
982         int                     i;
983
984         qry->commandType = CMD_SELECT;
985
986         /* Most SELECT stuff doesn't apply in a VALUES clause */
987         Assert(stmt->distinctClause == NIL);
988         Assert(stmt->targetList == NIL);
989         Assert(stmt->fromClause == NIL);
990         Assert(stmt->whereClause == NULL);
991         Assert(stmt->groupClause == NIL);
992         Assert(stmt->havingClause == NULL);
993         Assert(stmt->windowClause == NIL);
994         Assert(stmt->op == SETOP_NONE);
995
996         /* process the WITH clause independently of all else */
997         if (stmt->withClause)
998         {
999                 qry->hasRecursive = stmt->withClause->recursive;
1000                 qry->cteList = transformWithClause(pstate, stmt->withClause);
1001         }
1002
1003         /*
1004          * For each row of VALUES, transform the raw expressions and gather type
1005          * information.  This is also a handy place to reject DEFAULT nodes, which
1006          * the grammar allows for simplicity.
1007          */
1008         foreach(lc, stmt->valuesLists)
1009         {
1010                 List       *sublist = (List *) lfirst(lc);
1011
1012                 /* Do basic expression transformation (same as a ROW() expr) */
1013                 sublist = transformExpressionList(pstate, sublist);
1014
1015                 /*
1016                  * All the sublists must be the same length, *after* transformation
1017                  * (which might expand '*' into multiple items).  The VALUES RTE can't
1018                  * handle anything different.
1019                  */
1020                 if (sublist_length < 0)
1021                 {
1022                         /* Remember post-transformation length of first sublist */
1023                         sublist_length = list_length(sublist);
1024                         /* and allocate arrays for per-column info */
1025                         colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1026                         coltypes = (Oid *) palloc0(sublist_length * sizeof(Oid));
1027                 }
1028                 else if (sublist_length != list_length(sublist))
1029                 {
1030                         ereport(ERROR,
1031                                         (errcode(ERRCODE_SYNTAX_ERROR),
1032                                          errmsg("VALUES lists must all be the same length"),
1033                                          parser_errposition(pstate,
1034                                                                                 exprLocation((Node *) sublist))));
1035                 }
1036
1037                 exprsLists = lappend(exprsLists, sublist);
1038
1039                 /* Check for DEFAULT and build per-column expression lists */
1040                 i = 0;
1041                 foreach(lc2, sublist)
1042                 {
1043                         Node       *col = (Node *) lfirst(lc2);
1044
1045                         if (IsA(col, SetToDefault))
1046                                 ereport(ERROR,
1047                                                 (errcode(ERRCODE_SYNTAX_ERROR),
1048                                                  errmsg("DEFAULT can only appear in a VALUES list within INSERT"),
1049                                                  parser_errposition(pstate, exprLocation(col))));
1050                         colexprs[i] = lappend(colexprs[i], col);
1051                         i++;
1052                 }
1053         }
1054
1055         /*
1056          * Now resolve the common types of the columns, and coerce everything to
1057          * those types.
1058          */
1059         for (i = 0; i < sublist_length; i++)
1060         {
1061                 coltypes[i] = select_common_type(pstate, colexprs[i], "VALUES", NULL);
1062         }
1063
1064         newExprsLists = NIL;
1065         foreach(lc, exprsLists)
1066         {
1067                 List       *sublist = (List *) lfirst(lc);
1068                 List       *newsublist = NIL;
1069
1070                 i = 0;
1071                 foreach(lc2, sublist)
1072                 {
1073                         Node       *col = (Node *) lfirst(lc2);
1074
1075                         col = coerce_to_common_type(pstate, col, coltypes[i], "VALUES");
1076                         newsublist = lappend(newsublist, col);
1077                         i++;
1078                 }
1079
1080                 newExprsLists = lappend(newExprsLists, newsublist);
1081         }
1082
1083         /*
1084          * Generate the VALUES RTE
1085          */
1086         rte = addRangeTableEntryForValues(pstate, newExprsLists, NULL, true);
1087         rtr = makeNode(RangeTblRef);
1088         /* assume new rte is at end */
1089         rtr->rtindex = list_length(pstate->p_rtable);
1090         Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1091         pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
1092         pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
1093
1094         /*
1095          * Generate a targetlist as though expanding "*"
1096          */
1097         Assert(pstate->p_next_resno == 1);
1098         qry->targetList = expandRelAttrs(pstate, rte, rtr->rtindex, 0, -1);
1099
1100         /*
1101          * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1102          * VALUES, so cope.
1103          */
1104         qry->sortClause = transformSortClause(pstate,
1105                                                                                   stmt->sortClause,
1106                                                                                   &qry->targetList,
1107                                                                                   true /* fix unknowns */ ,
1108                                                                                   false /* allow SQL92 rules */ );
1109
1110         qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1111                                                                                         "OFFSET");
1112         qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1113                                                                                    "LIMIT");
1114
1115         if (stmt->lockingClause)
1116                 ereport(ERROR,
1117                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1118                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES")));
1119
1120         /* handle any CREATE TABLE AS spec */
1121         if (stmt->intoClause)
1122         {
1123                 qry->intoClause = stmt->intoClause;
1124                 if (stmt->intoClause->colNames)
1125                         applyColumnNames(qry->targetList, stmt->intoClause->colNames);
1126         }
1127
1128         /*
1129          * There mustn't have been any table references in the expressions, else
1130          * strange things would happen, like Cartesian products of those tables
1131          * with the VALUES list.  We have to check this after parsing ORDER BY et
1132          * al since those could insert more junk.
1133          */
1134         if (list_length(pstate->p_joinlist) != 1)
1135                 ereport(ERROR,
1136                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1137                                  errmsg("VALUES must not contain table references"),
1138                                  parser_errposition(pstate,
1139                                                    locate_var_of_level((Node *) newExprsLists, 0))));
1140
1141         /*
1142          * Another thing we can't currently support is NEW/OLD references in rules
1143          * --- seems we'd need something like SQL99's LATERAL construct to ensure
1144          * that the values would be available while evaluating the VALUES RTE.
1145          * This is a shame.  FIXME
1146          */
1147         if (list_length(pstate->p_rtable) != 1 &&
1148                 contain_vars_of_level((Node *) newExprsLists, 0))
1149                 ereport(ERROR,
1150                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1151                                  errmsg("VALUES must not contain OLD or NEW references"),
1152                                  errhint("Use SELECT ... UNION ALL ... instead."),
1153                                  parser_errposition(pstate,
1154                                                    locate_var_of_level((Node *) newExprsLists, 0))));
1155
1156         qry->rtable = pstate->p_rtable;
1157         qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1158
1159         qry->hasSubLinks = pstate->p_hasSubLinks;
1160         /* aggregates not allowed (but subselects are okay) */
1161         if (pstate->p_hasAggs)
1162                 ereport(ERROR,
1163                                 (errcode(ERRCODE_GROUPING_ERROR),
1164                                  errmsg("cannot use aggregate function in VALUES"),
1165                                  parser_errposition(pstate,
1166                                                    locate_agg_of_level((Node *) newExprsLists, 0))));
1167         if (pstate->p_hasWindowFuncs)
1168                 ereport(ERROR,
1169                                 (errcode(ERRCODE_WINDOWING_ERROR),
1170                                  errmsg("cannot use window function in VALUES"),
1171                                  parser_errposition(pstate,
1172                                                                 locate_windowfunc((Node *) newExprsLists))));
1173
1174         return qry;
1175 }
1176
1177 /*
1178  * transformSetOperationStmt -
1179  *        transforms a set-operations tree
1180  *
1181  * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
1182  * structure to it.  We must transform each leaf SELECT and build up a top-
1183  * level Query that contains the leaf SELECTs as subqueries in its rangetable.
1184  * The tree of set operations is converted into the setOperations field of
1185  * the top-level Query.
1186  */
1187 static Query *
1188 transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
1189 {
1190         Query      *qry = makeNode(Query);
1191         SelectStmt *leftmostSelect;
1192         int                     leftmostRTI;
1193         Query      *leftmostQuery;
1194         SetOperationStmt *sostmt;
1195         List       *socolinfo;
1196         List       *intoColNames = NIL;
1197         List       *sortClause;
1198         Node       *limitOffset;
1199         Node       *limitCount;
1200         List       *lockingClause;
1201         Node       *node;
1202         ListCell   *left_tlist,
1203                            *lct,
1204                            *lcm,
1205                            *l;
1206         List       *targetvars,
1207                            *targetnames,
1208                            *sv_relnamespace,
1209                            *sv_varnamespace;
1210         int                     sv_rtable_length;
1211         RangeTblEntry *jrte;
1212         int                     tllen;
1213
1214         qry->commandType = CMD_SELECT;
1215
1216         /* process the WITH clause independently of all else */
1217         if (stmt->withClause)
1218         {
1219                 qry->hasRecursive = stmt->withClause->recursive;
1220                 qry->cteList = transformWithClause(pstate, stmt->withClause);
1221         }
1222
1223         /*
1224          * Find leftmost leaf SelectStmt; extract the one-time-only items from it
1225          * and from the top-level node.
1226          */
1227         leftmostSelect = stmt->larg;
1228         while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1229                 leftmostSelect = leftmostSelect->larg;
1230         Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1231                    leftmostSelect->larg == NULL);
1232         if (leftmostSelect->intoClause)
1233         {
1234                 qry->intoClause = leftmostSelect->intoClause;
1235                 intoColNames = leftmostSelect->intoClause->colNames;
1236         }
1237
1238         /* clear this to prevent complaints in transformSetOperationTree() */
1239         leftmostSelect->intoClause = NULL;
1240
1241         /*
1242          * These are not one-time, exactly, but we want to process them here and
1243          * not let transformSetOperationTree() see them --- else it'll just
1244          * recurse right back here!
1245          */
1246         sortClause = stmt->sortClause;
1247         limitOffset = stmt->limitOffset;
1248         limitCount = stmt->limitCount;
1249         lockingClause = stmt->lockingClause;
1250
1251         stmt->sortClause = NIL;
1252         stmt->limitOffset = NULL;
1253         stmt->limitCount = NULL;
1254         stmt->lockingClause = NIL;
1255
1256         /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1257         if (lockingClause)
1258                 ereport(ERROR,
1259                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1260                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1261
1262         /*
1263          * Recursively transform the components of the tree.
1264          */
1265         sostmt = (SetOperationStmt *) transformSetOperationTree(pstate, stmt,
1266                                                                                                                         true,
1267                                                                                                                         &socolinfo);
1268         Assert(sostmt && IsA(sostmt, SetOperationStmt));
1269         qry->setOperations = (Node *) sostmt;
1270
1271         /*
1272          * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1273          */
1274         node = sostmt->larg;
1275         while (node && IsA(node, SetOperationStmt))
1276                 node = ((SetOperationStmt *) node)->larg;
1277         Assert(node && IsA(node, RangeTblRef));
1278         leftmostRTI = ((RangeTblRef *) node)->rtindex;
1279         leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1280         Assert(leftmostQuery != NULL);
1281
1282         /*
1283          * Generate dummy targetlist for outer query using column names of
1284          * leftmost select and common datatypes of topmost set operation. Also
1285          * make lists of the dummy vars and their names for use in parsing ORDER
1286          * BY.
1287          *
1288          * Note: we use leftmostRTI as the varno of the dummy variables. It
1289          * shouldn't matter too much which RT index they have, as long as they
1290          * have one that corresponds to a real RT entry; else funny things may
1291          * happen when the tree is mashed by rule rewriting.
1292          */
1293         qry->targetList = NIL;
1294         targetvars = NIL;
1295         targetnames = NIL;
1296         left_tlist = list_head(leftmostQuery->targetList);
1297
1298         forboth(lct, sostmt->colTypes, lcm, sostmt->colTypmods)
1299         {
1300                 Oid                     colType = lfirst_oid(lct);
1301                 int32           colTypmod = lfirst_int(lcm);
1302                 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1303                 char       *colName;
1304                 TargetEntry *tle;
1305                 Var                *var;
1306
1307                 Assert(!lefttle->resjunk);
1308                 colName = pstrdup(lefttle->resname);
1309                 var = makeVar(leftmostRTI,
1310                                           lefttle->resno,
1311                                           colType,
1312                                           colTypmod,
1313                                           0);
1314                 var->location = exprLocation((Node *) lefttle->expr);
1315                 tle = makeTargetEntry((Expr *) var,
1316                                                           (AttrNumber) pstate->p_next_resno++,
1317                                                           colName,
1318                                                           false);
1319                 qry->targetList = lappend(qry->targetList, tle);
1320                 targetvars = lappend(targetvars, var);
1321                 targetnames = lappend(targetnames, makeString(colName));
1322                 left_tlist = lnext(left_tlist);
1323         }
1324
1325         /*
1326          * As a first step towards supporting sort clauses that are expressions
1327          * using the output columns, generate a varnamespace entry that makes the
1328          * output columns visible.      A Join RTE node is handy for this, since we
1329          * can easily control the Vars generated upon matches.
1330          *
1331          * Note: we don't yet do anything useful with such cases, but at least
1332          * "ORDER BY upper(foo)" will draw the right error message rather than
1333          * "foo not found".
1334          */
1335         sv_rtable_length = list_length(pstate->p_rtable);
1336
1337         jrte = addRangeTableEntryForJoin(pstate,
1338                                                                          targetnames,
1339                                                                          JOIN_INNER,
1340                                                                          targetvars,
1341                                                                          NULL,
1342                                                                          false);
1343
1344         sv_relnamespace = pstate->p_relnamespace;
1345         pstate->p_relnamespace = NIL;           /* no qualified names allowed */
1346
1347         sv_varnamespace = pstate->p_varnamespace;
1348         pstate->p_varnamespace = list_make1(jrte);
1349
1350         /*
1351          * For now, we don't support resjunk sort clauses on the output of a
1352          * setOperation tree --- you can only use the SQL92-spec options of
1353          * selecting an output column by name or number.  Enforce by checking that
1354          * transformSortClause doesn't add any items to tlist.
1355          */
1356         tllen = list_length(qry->targetList);
1357
1358         qry->sortClause = transformSortClause(pstate,
1359                                                                                   sortClause,
1360                                                                                   &qry->targetList,
1361                                                                                   false /* no unknowns expected */ ,
1362                                                                                   false /* allow SQL92 rules */ );
1363
1364         pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1365         pstate->p_relnamespace = sv_relnamespace;
1366         pstate->p_varnamespace = sv_varnamespace;
1367
1368         if (tllen != list_length(qry->targetList))
1369                 ereport(ERROR,
1370                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1371                                  errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
1372                                  errdetail("Only result column names can be used, not expressions or functions."),
1373                                  errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
1374                                  parser_errposition(pstate,
1375                                                    exprLocation(list_nth(qry->targetList, tllen)))));
1376
1377         qry->limitOffset = transformLimitClause(pstate, limitOffset,
1378                                                                                         "OFFSET");
1379         qry->limitCount = transformLimitClause(pstate, limitCount,
1380                                                                                    "LIMIT");
1381
1382         /*
1383          * Handle SELECT INTO/CREATE TABLE AS.
1384          *
1385          * Any column names from CREATE TABLE AS need to be attached to both the
1386          * top level and the leftmost subquery.  We do not do this earlier because
1387          * we do *not* want sortClause processing to be affected.
1388          */
1389         if (intoColNames)
1390         {
1391                 applyColumnNames(qry->targetList, intoColNames);
1392                 applyColumnNames(leftmostQuery->targetList, intoColNames);
1393         }
1394
1395         qry->rtable = pstate->p_rtable;
1396         qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1397
1398         qry->hasSubLinks = pstate->p_hasSubLinks;
1399         qry->hasAggs = pstate->p_hasAggs;
1400         if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
1401                 parseCheckAggregates(pstate, qry);
1402         qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1403         if (pstate->p_hasWindowFuncs)
1404                 parseCheckWindowFuncs(pstate, qry);
1405
1406         foreach(l, lockingClause)
1407         {
1408                 transformLockingClause(pstate, qry,
1409                                                            (LockingClause *) lfirst(l), false);
1410         }
1411
1412         return qry;
1413 }
1414
1415 /*
1416  * transformSetOperationTree
1417  *              Recursively transform leaves and internal nodes of a set-op tree
1418  *
1419  * In addition to returning the transformed node, we return a list of
1420  * expression nodes showing the type, typmod, and location (for error messages)
1421  * of each output column of the set-op node.  This is used only during the
1422  * internal recursion of this function.  At the upper levels we use
1423  * SetToDefault nodes for this purpose, since they carry exactly the fields
1424  * needed, but any other expression node type would do as well.
1425  */
1426 static Node *
1427 transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
1428                                                   bool isTopLevel, List **colInfo)
1429 {
1430         bool            isLeaf;
1431
1432         Assert(stmt && IsA(stmt, SelectStmt));
1433
1434         /*
1435          * Validity-check both leaf and internal SELECTs for disallowed ops.
1436          */
1437         if (stmt->intoClause)
1438                 ereport(ERROR,
1439                                 (errcode(ERRCODE_SYNTAX_ERROR),
1440                                  errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
1441                                  parser_errposition(pstate,
1442                                                                   exprLocation((Node *) stmt->intoClause))));
1443
1444         /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1445         if (stmt->lockingClause)
1446                 ereport(ERROR,
1447                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1448                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1449
1450         /*
1451          * If an internal node of a set-op tree has ORDER BY, LIMIT, or FOR UPDATE
1452          * clauses attached, we need to treat it like a leaf node to generate an
1453          * independent sub-Query tree.  Otherwise, it can be represented by a
1454          * SetOperationStmt node underneath the parent Query.
1455          */
1456         if (stmt->op == SETOP_NONE)
1457         {
1458                 Assert(stmt->larg == NULL && stmt->rarg == NULL);
1459                 isLeaf = true;
1460         }
1461         else
1462         {
1463                 Assert(stmt->larg != NULL && stmt->rarg != NULL);
1464                 if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
1465                         stmt->lockingClause)
1466                         isLeaf = true;
1467                 else
1468                         isLeaf = false;
1469         }
1470
1471         if (isLeaf)
1472         {
1473                 /* Process leaf SELECT */
1474                 Query      *selectQuery;
1475                 char            selectName[32];
1476                 RangeTblEntry *rte;
1477                 RangeTblRef *rtr;
1478                 ListCell   *tl;
1479
1480                 /*
1481                  * Transform SelectStmt into a Query.
1482                  *
1483                  * Note: previously transformed sub-queries don't affect the parsing
1484                  * of this sub-query, because they are not in the toplevel pstate's
1485                  * namespace list.
1486                  */
1487                 selectQuery = parse_sub_analyze((Node *) stmt, pstate, NULL, false);
1488
1489                 /*
1490                  * Check for bogus references to Vars on the current query level (but
1491                  * upper-level references are okay). Normally this can't happen
1492                  * because the namespace will be empty, but it could happen if we are
1493                  * inside a rule.
1494                  */
1495                 if (pstate->p_relnamespace || pstate->p_varnamespace)
1496                 {
1497                         if (contain_vars_of_level((Node *) selectQuery, 1))
1498                                 ereport(ERROR,
1499                                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1500                                                  errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
1501                                                  parser_errposition(pstate,
1502                                                          locate_var_of_level((Node *) selectQuery, 1))));
1503                 }
1504
1505                 /*
1506                  * Extract a list of the result expressions for upper-level checking.
1507                  */
1508                 *colInfo = NIL;
1509                 foreach(tl, selectQuery->targetList)
1510                 {
1511                         TargetEntry *tle = (TargetEntry *) lfirst(tl);
1512
1513                         if (!tle->resjunk)
1514                                 *colInfo = lappend(*colInfo, tle->expr);
1515                 }
1516
1517                 /*
1518                  * Make the leaf query be a subquery in the top-level rangetable.
1519                  */
1520                 snprintf(selectName, sizeof(selectName), "*SELECT* %d",
1521                                  list_length(pstate->p_rtable) + 1);
1522                 rte = addRangeTableEntryForSubquery(pstate,
1523                                                                                         selectQuery,
1524                                                                                         makeAlias(selectName, NIL),
1525                                                                                         false);
1526
1527                 /*
1528                  * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
1529                  */
1530                 rtr = makeNode(RangeTblRef);
1531                 /* assume new rte is at end */
1532                 rtr->rtindex = list_length(pstate->p_rtable);
1533                 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1534                 return (Node *) rtr;
1535         }
1536         else
1537         {
1538                 /* Process an internal node (set operation node) */
1539                 SetOperationStmt *op = makeNode(SetOperationStmt);
1540                 List       *lcolinfo;
1541                 List       *rcolinfo;
1542                 ListCell   *lci;
1543                 ListCell   *rci;
1544                 const char *context;
1545
1546                 context = (stmt->op == SETOP_UNION ? "UNION" :
1547                                    (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
1548                                         "EXCEPT"));
1549
1550                 op->op = stmt->op;
1551                 op->all = stmt->all;
1552
1553                 /*
1554                  * Recursively transform the left child node.
1555                  */
1556                 op->larg = transformSetOperationTree(pstate, stmt->larg,
1557                                                                                          false,
1558                                                                                          &lcolinfo);
1559
1560                 /*
1561                  * If we are processing a recursive union query, now is the time to
1562                  * examine the non-recursive term's output columns and mark the
1563                  * containing CTE as having those result columns.  We should do this
1564                  * only at the topmost setop of the CTE, of course.
1565                  */
1566                 if (isTopLevel &&
1567                         pstate->p_parent_cte &&
1568                         pstate->p_parent_cte->cterecursive)
1569                         determineRecursiveColTypes(pstate, op->larg, lcolinfo);
1570
1571                 /*
1572                  * Recursively transform the right child node.
1573                  */
1574                 op->rarg = transformSetOperationTree(pstate, stmt->rarg,
1575                                                                                          false,
1576                                                                                          &rcolinfo);
1577
1578                 /*
1579                  * Verify that the two children have the same number of non-junk
1580                  * columns, and determine the types of the merged output columns.
1581                  */
1582                 if (list_length(lcolinfo) != list_length(rcolinfo))
1583                         ereport(ERROR,
1584                                         (errcode(ERRCODE_SYNTAX_ERROR),
1585                                  errmsg("each %s query must have the same number of columns",
1586                                                 context),
1587                                          parser_errposition(pstate,
1588                                                                                 exprLocation((Node *) rcolinfo))));
1589
1590                 *colInfo = NIL;
1591                 op->colTypes = NIL;
1592                 op->colTypmods = NIL;
1593                 op->groupClauses = NIL;
1594                 forboth(lci, lcolinfo, rci, rcolinfo)
1595                 {
1596                         Node       *lcolnode = (Node *) lfirst(lci);
1597                         Node       *rcolnode = (Node *) lfirst(rci);
1598                         Oid                     lcoltype = exprType(lcolnode);
1599                         Oid                     rcoltype = exprType(rcolnode);
1600                         int32           lcoltypmod = exprTypmod(lcolnode);
1601                         int32           rcoltypmod = exprTypmod(rcolnode);
1602                         Node       *bestexpr;
1603                         SetToDefault *rescolnode;
1604                         Oid                     rescoltype;
1605                         int32           rescoltypmod;
1606
1607                         /* select common type, same as CASE et al */
1608                         rescoltype = select_common_type(pstate,
1609                                                                                         list_make2(lcolnode, rcolnode),
1610                                                                                         context,
1611                                                                                         &bestexpr);
1612                         /* if same type and same typmod, use typmod; else default */
1613                         if (lcoltype == rcoltype && lcoltypmod == rcoltypmod)
1614                                 rescoltypmod = lcoltypmod;
1615                         else
1616                                 rescoltypmod = -1;
1617
1618                         /*
1619                          * Verify the coercions are actually possible.  If not, we'd fail
1620                          * later anyway, but we want to fail now while we have sufficient
1621                          * context to produce an error cursor position.
1622                          *
1623                          * The if-tests might look wrong, but they are correct: we should
1624                          * verify if the input is non-UNKNOWN *or* if it is an UNKNOWN
1625                          * Const (to verify the literal is valid for the target data type)
1626                          * or Param (to possibly resolve the Param's type).  We should do
1627                          * nothing if the input is say an UNKNOWN Var, which can happen in
1628                          * some cases.  The planner is sometimes able to fold the Var to a
1629                          * constant before it has to coerce the type, so failing now would
1630                          * just break cases that might work.
1631                          */
1632                         if (lcoltype != UNKNOWNOID ||
1633                                 IsA(lcolnode, Const) ||IsA(lcolnode, Param))
1634                                 (void) coerce_to_common_type(pstate, lcolnode,
1635                                                                                          rescoltype, context);
1636                         if (rcoltype != UNKNOWNOID ||
1637                                 IsA(rcolnode, Const) ||IsA(rcolnode, Param))
1638                                 (void) coerce_to_common_type(pstate, rcolnode,
1639                                                                                          rescoltype, context);
1640
1641                         /* emit results */
1642                         rescolnode = makeNode(SetToDefault);
1643                         rescolnode->typeId = rescoltype;
1644                         rescolnode->typeMod = rescoltypmod;
1645                         rescolnode->location = exprLocation(bestexpr);
1646                         *colInfo = lappend(*colInfo, rescolnode);
1647
1648                         op->colTypes = lappend_oid(op->colTypes, rescoltype);
1649                         op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
1650
1651                         /*
1652                          * For all cases except UNION ALL, identify the grouping operators
1653                          * (and, if available, sorting operators) that will be used to
1654                          * eliminate duplicates.
1655                          */
1656                         if (op->op != SETOP_UNION || !op->all)
1657                         {
1658                                 SortGroupClause *grpcl = makeNode(SortGroupClause);
1659                                 Oid                     sortop;
1660                                 Oid                     eqop;
1661                                 ParseCallbackState pcbstate;
1662
1663                                 setup_parser_errposition_callback(&pcbstate, pstate,
1664                                                                                                   rescolnode->location);
1665
1666                                 /* determine the eqop and optional sortop */
1667                                 get_sort_group_operators(rescoltype,
1668                                                                                  false, true, false,
1669                                                                                  &sortop, &eqop, NULL);
1670
1671                                 cancel_parser_errposition_callback(&pcbstate);
1672
1673                                 /* we don't have a tlist yet, so can't assign sortgrouprefs */
1674                                 grpcl->tleSortGroupRef = 0;
1675                                 grpcl->eqop = eqop;
1676                                 grpcl->sortop = sortop;
1677                                 grpcl->nulls_first = false;             /* OK with or without sortop */
1678
1679                                 op->groupClauses = lappend(op->groupClauses, grpcl);
1680                         }
1681                 }
1682
1683                 return (Node *) op;
1684         }
1685 }
1686
1687 /*
1688  * Process the outputs of the non-recursive term of a recursive union
1689  * to set up the parent CTE's columns
1690  */
1691 static void
1692 determineRecursiveColTypes(ParseState *pstate, Node *larg, List *lcolinfo)
1693 {
1694         Node       *node;
1695         int                     leftmostRTI;
1696         Query      *leftmostQuery;
1697         List       *targetList;
1698         ListCell   *left_tlist;
1699         ListCell   *lci;
1700         int                     next_resno;
1701
1702         /*
1703          * Find leftmost leaf SELECT
1704          */
1705         node = larg;
1706         while (node && IsA(node, SetOperationStmt))
1707                 node = ((SetOperationStmt *) node)->larg;
1708         Assert(node && IsA(node, RangeTblRef));
1709         leftmostRTI = ((RangeTblRef *) node)->rtindex;
1710         leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1711         Assert(leftmostQuery != NULL);
1712
1713         /*
1714          * Generate dummy targetlist using column names of leftmost select and
1715          * dummy result expressions of the non-recursive term.
1716          */
1717         targetList = NIL;
1718         left_tlist = list_head(leftmostQuery->targetList);
1719         next_resno = 1;
1720
1721         foreach(lci, lcolinfo)
1722         {
1723                 Expr       *lcolexpr = (Expr *) lfirst(lci);
1724                 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1725                 char       *colName;
1726                 TargetEntry *tle;
1727
1728                 Assert(!lefttle->resjunk);
1729                 colName = pstrdup(lefttle->resname);
1730                 tle = makeTargetEntry(lcolexpr,
1731                                                           next_resno++,
1732                                                           colName,
1733                                                           false);
1734                 targetList = lappend(targetList, tle);
1735                 left_tlist = lnext(left_tlist);
1736         }
1737
1738         /* Now build CTE's output column info using dummy targetlist */
1739         analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
1740 }
1741
1742 /*
1743  * Attach column names from a ColumnDef list to a TargetEntry list
1744  * (for CREATE TABLE AS)
1745  */
1746 static void
1747 applyColumnNames(List *dst, List *src)
1748 {
1749         ListCell   *dst_item;
1750         ListCell   *src_item;
1751
1752         src_item = list_head(src);
1753
1754         foreach(dst_item, dst)
1755         {
1756                 TargetEntry *d = (TargetEntry *) lfirst(dst_item);
1757                 ColumnDef  *s;
1758
1759                 /* junk targets don't count */
1760                 if (d->resjunk)
1761                         continue;
1762
1763                 /* fewer ColumnDefs than target entries is OK */
1764                 if (src_item == NULL)
1765                         break;
1766
1767                 s = (ColumnDef *) lfirst(src_item);
1768                 src_item = lnext(src_item);
1769
1770                 d->resname = pstrdup(s->colname);
1771         }
1772
1773         /* more ColumnDefs than target entries is not OK */
1774         if (src_item != NULL)
1775                 ereport(ERROR,
1776                                 (errcode(ERRCODE_SYNTAX_ERROR),
1777                                  errmsg("CREATE TABLE AS specifies too many column names")));
1778 }
1779
1780
1781 /*
1782  * transformUpdateStmt -
1783  *        transforms an update statement
1784  */
1785 static Query *
1786 transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
1787 {
1788         Query      *qry = makeNode(Query);
1789         RangeTblEntry *target_rte;
1790         Node       *qual;
1791         ListCell   *origTargetList;
1792         ListCell   *tl;
1793
1794         qry->commandType = CMD_UPDATE;
1795         pstate->p_is_update = true;
1796
1797         qry->resultRelation = setTargetTable(pstate, stmt->relation,
1798                                                                   interpretInhOption(stmt->relation->inhOpt),
1799                                                                                  true,
1800                                                                                  ACL_UPDATE);
1801
1802         /*
1803          * the FROM clause is non-standard SQL syntax. We used to be able to do
1804          * this with REPLACE in POSTQUEL so we keep the feature.
1805          */
1806         transformFromClause(pstate, stmt->fromClause);
1807
1808         qry->targetList = transformTargetList(pstate, stmt->targetList);
1809
1810         qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
1811
1812         qry->returningList = transformReturningList(pstate, stmt->returningList);
1813
1814         qry->rtable = pstate->p_rtable;
1815         qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1816
1817         qry->hasSubLinks = pstate->p_hasSubLinks;
1818
1819         /*
1820          * Top-level aggregates are simply disallowed in UPDATE, per spec. (From
1821          * an implementation point of view, this is forced because the implicit
1822          * ctid reference would otherwise be an ungrouped variable.)
1823          */
1824         if (pstate->p_hasAggs)
1825                 ereport(ERROR,
1826                                 (errcode(ERRCODE_GROUPING_ERROR),
1827                                  errmsg("cannot use aggregate function in UPDATE"),
1828                                  parser_errposition(pstate,
1829                                                                         locate_agg_of_level((Node *) qry, 0))));
1830         if (pstate->p_hasWindowFuncs)
1831                 ereport(ERROR,
1832                                 (errcode(ERRCODE_WINDOWING_ERROR),
1833                                  errmsg("cannot use window function in UPDATE"),
1834                                  parser_errposition(pstate,
1835                                                                         locate_windowfunc((Node *) qry))));
1836
1837         /*
1838          * Now we are done with SELECT-like processing, and can get on with
1839          * transforming the target list to match the UPDATE target columns.
1840          */
1841
1842         /* Prepare to assign non-conflicting resnos to resjunk attributes */
1843         if (pstate->p_next_resno <= pstate->p_target_relation->rd_rel->relnatts)
1844                 pstate->p_next_resno = pstate->p_target_relation->rd_rel->relnatts + 1;
1845
1846         /* Prepare non-junk columns for assignment to target table */
1847         target_rte = pstate->p_target_rangetblentry;
1848         origTargetList = list_head(stmt->targetList);
1849
1850         foreach(tl, qry->targetList)
1851         {
1852                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1853                 ResTarget  *origTarget;
1854                 int                     attrno;
1855
1856                 if (tle->resjunk)
1857                 {
1858                         /*
1859                          * Resjunk nodes need no additional processing, but be sure they
1860                          * have resnos that do not match any target columns; else rewriter
1861                          * or planner might get confused.  They don't need a resname
1862                          * either.
1863                          */
1864                         tle->resno = (AttrNumber) pstate->p_next_resno++;
1865                         tle->resname = NULL;
1866                         continue;
1867                 }
1868                 if (origTargetList == NULL)
1869                         elog(ERROR, "UPDATE target count mismatch --- internal error");
1870                 origTarget = (ResTarget *) lfirst(origTargetList);
1871                 Assert(IsA(origTarget, ResTarget));
1872
1873                 attrno = attnameAttNum(pstate->p_target_relation,
1874                                                            origTarget->name, true);
1875                 if (attrno == InvalidAttrNumber)
1876                         ereport(ERROR,
1877                                         (errcode(ERRCODE_UNDEFINED_COLUMN),
1878                                          errmsg("column \"%s\" of relation \"%s\" does not exist",
1879                                                         origTarget->name,
1880                                                  RelationGetRelationName(pstate->p_target_relation)),
1881                                          parser_errposition(pstate, origTarget->location)));
1882
1883                 updateTargetListEntry(pstate, tle, origTarget->name,
1884                                                           attrno,
1885                                                           origTarget->indirection,
1886                                                           origTarget->location);
1887
1888                 /* Mark the target column as requiring update permissions */
1889                 target_rte->modifiedCols = bms_add_member(target_rte->modifiedCols,
1890                                                                 attrno - FirstLowInvalidHeapAttributeNumber);
1891
1892                 origTargetList = lnext(origTargetList);
1893         }
1894         if (origTargetList != NULL)
1895                 elog(ERROR, "UPDATE target count mismatch --- internal error");
1896
1897         return qry;
1898 }
1899
1900 /*
1901  * transformReturningList -
1902  *      handle a RETURNING clause in INSERT/UPDATE/DELETE
1903  */
1904 static List *
1905 transformReturningList(ParseState *pstate, List *returningList)
1906 {
1907         List       *rlist;
1908         int                     save_next_resno;
1909         bool            save_hasAggs;
1910         bool            save_hasWindowFuncs;
1911         int                     length_rtable;
1912
1913         if (returningList == NIL)
1914                 return NIL;                             /* nothing to do */
1915
1916         /*
1917          * We need to assign resnos starting at one in the RETURNING list. Save
1918          * and restore the main tlist's value of p_next_resno, just in case
1919          * someone looks at it later (probably won't happen).
1920          */
1921         save_next_resno = pstate->p_next_resno;
1922         pstate->p_next_resno = 1;
1923
1924         /* save other state so that we can detect disallowed stuff */
1925         save_hasAggs = pstate->p_hasAggs;
1926         pstate->p_hasAggs = false;
1927         save_hasWindowFuncs = pstate->p_hasWindowFuncs;
1928         pstate->p_hasWindowFuncs = false;
1929         length_rtable = list_length(pstate->p_rtable);
1930
1931         /* transform RETURNING identically to a SELECT targetlist */
1932         rlist = transformTargetList(pstate, returningList);
1933
1934         /* check for disallowed stuff */
1935
1936         /* aggregates not allowed (but subselects are okay) */
1937         if (pstate->p_hasAggs)
1938                 ereport(ERROR,
1939                                 (errcode(ERRCODE_GROUPING_ERROR),
1940                                  errmsg("cannot use aggregate function in RETURNING"),
1941                                  parser_errposition(pstate,
1942                                                                         locate_agg_of_level((Node *) rlist, 0))));
1943         if (pstate->p_hasWindowFuncs)
1944                 ereport(ERROR,
1945                                 (errcode(ERRCODE_WINDOWING_ERROR),
1946                                  errmsg("cannot use window function in RETURNING"),
1947                                  parser_errposition(pstate,
1948                                                                         locate_windowfunc((Node *) rlist))));
1949
1950         /* no new relation references please */
1951         if (list_length(pstate->p_rtable) != length_rtable)
1952         {
1953                 int                     vlocation = -1;
1954                 int                     relid;
1955
1956                 /* try to locate such a reference to point to */
1957                 for (relid = length_rtable + 1; relid <= list_length(pstate->p_rtable); relid++)
1958                 {
1959                         vlocation = locate_var_of_relation((Node *) rlist, relid, 0);
1960                         if (vlocation >= 0)
1961                                 break;
1962                 }
1963                 ereport(ERROR,
1964                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1965                         errmsg("RETURNING cannot contain references to other relations"),
1966                                  parser_errposition(pstate, vlocation)));
1967         }
1968
1969         /* mark column origins */
1970         markTargetListOrigins(pstate, rlist);
1971
1972         /* restore state */
1973         pstate->p_next_resno = save_next_resno;
1974         pstate->p_hasAggs = save_hasAggs;
1975         pstate->p_hasWindowFuncs = save_hasWindowFuncs;
1976
1977         return rlist;
1978 }
1979
1980
1981 /*
1982  * transformDeclareCursorStmt -
1983  *      transform a DECLARE CURSOR Statement
1984  *
1985  * DECLARE CURSOR is a hybrid case: it's an optimizable statement (in fact not
1986  * significantly different from a SELECT) as far as parsing/rewriting/planning
1987  * are concerned, but it's not passed to the executor and so in that sense is
1988  * a utility statement.  We transform it into a Query exactly as if it were
1989  * a SELECT, then stick the original DeclareCursorStmt into the utilityStmt
1990  * field to carry the cursor name and options.
1991  */
1992 static Query *
1993 transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
1994 {
1995         Query      *result;
1996
1997         /*
1998          * Don't allow both SCROLL and NO SCROLL to be specified
1999          */
2000         if ((stmt->options & CURSOR_OPT_SCROLL) &&
2001                 (stmt->options & CURSOR_OPT_NO_SCROLL))
2002                 ereport(ERROR,
2003                                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
2004                                  errmsg("cannot specify both SCROLL and NO SCROLL")));
2005
2006         result = transformStmt(pstate, stmt->query);
2007
2008         /* Grammar should not have allowed anything but SELECT */
2009         if (!IsA(result, Query) ||
2010                 result->commandType != CMD_SELECT ||
2011                 result->utilityStmt != NULL)
2012                 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
2013
2014         /* But we must explicitly disallow DECLARE CURSOR ... SELECT INTO */
2015         if (result->intoClause)
2016                 ereport(ERROR,
2017                                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
2018                                  errmsg("DECLARE CURSOR cannot specify INTO"),
2019                                  parser_errposition(pstate,
2020                                                                 exprLocation((Node *) result->intoClause))));
2021
2022         /* FOR UPDATE and WITH HOLD are not compatible */
2023         if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
2024                 ereport(ERROR,
2025                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2026                                  errmsg("DECLARE CURSOR WITH HOLD ... FOR UPDATE/SHARE is not supported"),
2027                                  errdetail("Holdable cursors must be READ ONLY.")));
2028
2029         /* FOR UPDATE and SCROLL are not compatible */
2030         if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
2031                 ereport(ERROR,
2032                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2033                 errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
2034                                  errdetail("Scrollable cursors must be READ ONLY.")));
2035
2036         /* FOR UPDATE and INSENSITIVE are not compatible */
2037         if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
2038                 ereport(ERROR,
2039                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2040                                  errmsg("DECLARE INSENSITIVE CURSOR ... FOR UPDATE/SHARE is not supported"),
2041                                  errdetail("Insensitive cursors must be READ ONLY.")));
2042
2043         /* We won't need the raw querytree any more */
2044         stmt->query = NULL;
2045
2046         result->utilityStmt = (Node *) stmt;
2047
2048         return result;
2049 }
2050
2051
2052 /*
2053  * transformExplainStmt -
2054  *      transform an EXPLAIN Statement
2055  *
2056  * EXPLAIN is like other utility statements in that we emit it as a
2057  * CMD_UTILITY Query node; however, we must first transform the contained
2058  * query.  We used to postpone that until execution, but it's really necessary
2059  * to do it during the normal parse analysis phase to ensure that side effects
2060  * of parser hooks happen at the expected time.
2061  */
2062 static Query *
2063 transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
2064 {
2065         Query      *result;
2066
2067         /* transform contained query */
2068         stmt->query = (Node *) transformStmt(pstate, stmt->query);
2069
2070         /* represent the command as a utility Query */
2071         result = makeNode(Query);
2072         result->commandType = CMD_UTILITY;
2073         result->utilityStmt = (Node *) stmt;
2074
2075         return result;
2076 }
2077
2078
2079 /*
2080  * Check for features that are not supported together with FOR UPDATE/SHARE.
2081  *
2082  * exported so planner can check again after rewriting, query pullup, etc
2083  */
2084 void
2085 CheckSelectLocking(Query *qry)
2086 {
2087         if (qry->setOperations)
2088                 ereport(ERROR,
2089                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2090                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
2091         if (qry->distinctClause != NIL)
2092                 ereport(ERROR,
2093                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2094                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with DISTINCT clause")));
2095         if (qry->groupClause != NIL)
2096                 ereport(ERROR,
2097                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2098                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with GROUP BY clause")));
2099         if (qry->havingQual != NULL)
2100                 ereport(ERROR,
2101                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2102                 errmsg("SELECT FOR UPDATE/SHARE is not allowed with HAVING clause")));
2103         if (qry->hasAggs)
2104                 ereport(ERROR,
2105                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2106                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with aggregate functions")));
2107         if (qry->hasWindowFuncs)
2108                 ereport(ERROR,
2109                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2110                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with window functions")));
2111         if (expression_returns_set((Node *) qry->targetList))
2112                 ereport(ERROR,
2113                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2114                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with set-returning functions in the target list")));
2115 }
2116
2117 /*
2118  * Transform a FOR UPDATE/SHARE clause
2119  *
2120  * This basically involves replacing names by integer relids.
2121  *
2122  * NB: if you need to change this, see also markQueryForLocking()
2123  * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
2124  */
2125 static void
2126 transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
2127                                            bool pushedDown)
2128 {
2129         List       *lockedRels = lc->lockedRels;
2130         ListCell   *l;
2131         ListCell   *rt;
2132         Index           i;
2133         LockingClause *allrels;
2134
2135         CheckSelectLocking(qry);
2136
2137         /* make a clause we can pass down to subqueries to select all rels */
2138         allrels = makeNode(LockingClause);
2139         allrels->lockedRels = NIL;      /* indicates all rels */
2140         allrels->forUpdate = lc->forUpdate;
2141         allrels->noWait = lc->noWait;
2142
2143         if (lockedRels == NIL)
2144         {
2145                 /* all regular tables used in query */
2146                 i = 0;
2147                 foreach(rt, qry->rtable)
2148                 {
2149                         RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2150
2151                         ++i;
2152                         switch (rte->rtekind)
2153                         {
2154                                 case RTE_RELATION:
2155                                         applyLockingClause(qry, i,
2156                                                                            lc->forUpdate, lc->noWait, pushedDown);
2157                                         rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2158                                         break;
2159                                 case RTE_SUBQUERY:
2160                                         applyLockingClause(qry, i,
2161                                                                            lc->forUpdate, lc->noWait, pushedDown);
2162
2163                                         /*
2164                                          * FOR UPDATE/SHARE of subquery is propagated to all of
2165                                          * subquery's rels, too.  We could do this later (based on
2166                                          * the marking of the subquery RTE) but it is convenient
2167                                          * to have local knowledge in each query level about which
2168                                          * rels need to be opened with RowShareLock.
2169                                          */
2170                                         transformLockingClause(pstate, rte->subquery,
2171                                                                                    allrels, true);
2172                                         break;
2173                                 default:
2174                                         /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
2175                                         break;
2176                         }
2177                 }
2178         }
2179         else
2180         {
2181                 /* just the named tables */
2182                 foreach(l, lockedRels)
2183                 {
2184                         RangeVar   *thisrel = (RangeVar *) lfirst(l);
2185
2186                         /* For simplicity we insist on unqualified alias names here */
2187                         if (thisrel->catalogname || thisrel->schemaname)
2188                                 ereport(ERROR,
2189                                                 (errcode(ERRCODE_SYNTAX_ERROR),
2190                                                  errmsg("SELECT FOR UPDATE/SHARE must specify unqualified relation names"),
2191                                                  parser_errposition(pstate, thisrel->location)));
2192
2193                         i = 0;
2194                         foreach(rt, qry->rtable)
2195                         {
2196                                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2197
2198                                 ++i;
2199                                 if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
2200                                 {
2201                                         switch (rte->rtekind)
2202                                         {
2203                                                 case RTE_RELATION:
2204                                                         applyLockingClause(qry, i,
2205                                                                                            lc->forUpdate, lc->noWait,
2206                                                                                            pushedDown);
2207                                                         rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2208                                                         break;
2209                                                 case RTE_SUBQUERY:
2210                                                         applyLockingClause(qry, i,
2211                                                                                            lc->forUpdate, lc->noWait,
2212                                                                                            pushedDown);
2213                                                         /* see comment above */
2214                                                         transformLockingClause(pstate, rte->subquery,
2215                                                                                                    allrels, true);
2216                                                         break;
2217                                                 case RTE_JOIN:
2218                                                         ereport(ERROR,
2219                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2220                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a join"),
2221                                                          parser_errposition(pstate, thisrel->location)));
2222                                                         break;
2223                                                 case RTE_SPECIAL:
2224                                                         ereport(ERROR,
2225                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2226                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to NEW or OLD"),
2227                                                          parser_errposition(pstate, thisrel->location)));
2228                                                         break;
2229                                                 case RTE_FUNCTION:
2230                                                         ereport(ERROR,
2231                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2232                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a function"),
2233                                                          parser_errposition(pstate, thisrel->location)));
2234                                                         break;
2235                                                 case RTE_VALUES:
2236                                                         ereport(ERROR,
2237                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2238                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES"),
2239                                                          parser_errposition(pstate, thisrel->location)));
2240                                                         break;
2241                                                 case RTE_CTE:
2242                                                         ereport(ERROR,
2243                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2244                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a WITH query"),
2245                                                          parser_errposition(pstate, thisrel->location)));
2246                                                         break;
2247                                                 default:
2248                                                         elog(ERROR, "unrecognized RTE type: %d",
2249                                                                  (int) rte->rtekind);
2250                                                         break;
2251                                         }
2252                                         break;          /* out of foreach loop */
2253                                 }
2254                         }
2255                         if (rt == NULL)
2256                                 ereport(ERROR,
2257                                                 (errcode(ERRCODE_UNDEFINED_TABLE),
2258                                                  errmsg("relation \"%s\" in FOR UPDATE/SHARE clause not found in FROM clause",
2259                                                                 thisrel->relname),
2260                                                  parser_errposition(pstate, thisrel->location)));
2261                 }
2262         }
2263 }
2264
2265 /*
2266  * Record locking info for a single rangetable item
2267  */
2268 void
2269 applyLockingClause(Query *qry, Index rtindex,
2270                                    bool forUpdate, bool noWait, bool pushedDown)
2271 {
2272         RowMarkClause *rc;
2273
2274         /* If it's an explicit clause, make sure hasForUpdate gets set */
2275         if (!pushedDown)
2276                 qry->hasForUpdate = true;
2277
2278         /* Check for pre-existing entry for same rtindex */
2279         if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
2280         {
2281                 /*
2282                  * If the same RTE is specified both FOR UPDATE and FOR SHARE, treat
2283                  * it as FOR UPDATE.  (Reasonable, since you can't take both a shared
2284                  * and exclusive lock at the same time; it'll end up being exclusive
2285                  * anyway.)
2286                  *
2287                  * We also consider that NOWAIT wins if it's specified both ways. This
2288                  * is a bit more debatable but raising an error doesn't seem helpful.
2289                  * (Consider for instance SELECT FOR UPDATE NOWAIT from a view that
2290                  * internally contains a plain FOR UPDATE spec.)
2291                  *
2292                  * And of course pushedDown becomes false if any clause is explicit.
2293                  */
2294                 rc->forUpdate |= forUpdate;
2295                 rc->noWait |= noWait;
2296                 rc->pushedDown &= pushedDown;
2297                 return;
2298         }
2299
2300         /* Make a new RowMarkClause */
2301         rc = makeNode(RowMarkClause);
2302         rc->rti = rtindex;
2303         rc->forUpdate = forUpdate;
2304         rc->noWait = noWait;
2305         rc->pushedDown = pushedDown;
2306         qry->rowMarks = lappend(qry->rowMarks, rc);
2307 }