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