PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
analyze.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * analyze.c
4  * transform the raw parse tree into a query tree
5  *
6  * For optimizable statements, we are careful to obtain a suitable lock on
7  * each referenced table, and other modules of the backend preserve or
8  * re-obtain these locks before depending on the results. It is therefore
9  * okay to do significant semantic analysis of these statements. For
10  * utility commands, no locks are obtained here (and if they were, we could
11  * not be sure we'd still have them at execution). Hence the general rule
12  * for utility commands is to just dump them into a Query node untransformed.
13  * DECLARE CURSOR, EXPLAIN, and CREATE TABLE AS are exceptions because they
14  * contain optimizable statements, which we should transform.
15  *
16  *
17  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
18  * Portions Copyright (c) 1994, Regents of the University of California
19  *
20  * src/backend/parser/analyze.c
21  *
22  *-------------------------------------------------------------------------
23  */
24 
25 #include "postgres.h"
26 
27 #include "access/sysattr.h"
28 #include "catalog/pg_type.h"
29 #include "miscadmin.h"
30 #include "nodes/makefuncs.h"
31 #include "nodes/nodeFuncs.h"
32 #include "optimizer/var.h"
33 #include "parser/analyze.h"
34 #include "parser/parse_agg.h"
35 #include "parser/parse_clause.h"
36 #include "parser/parse_coerce.h"
37 #include "parser/parse_collate.h"
38 #include "parser/parse_cte.h"
39 #include "parser/parse_oper.h"
40 #include "parser/parse_param.h"
41 #include "parser/parse_relation.h"
42 #include "parser/parse_target.h"
43 #include "parser/parsetree.h"
44 #include "rewrite/rewriteManip.h"
45 #include "utils/rel.h"
46 
47 
48 /* Hook for plugins to get control at end of parse analysis */
50 
51 static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree);
52 static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
53 static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
54 static List *transformInsertRow(ParseState *pstate, List *exprlist,
55  List *stmtcols, List *icolumns, List *attrnos,
56  bool strip_indirection);
58  OnConflictClause *onConflictClause);
59 static int count_rowexpr_columns(ParseState *pstate, Node *expr);
60 static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
61 static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
64  bool isTopLevel, List **targetlist);
65 static void determineRecursiveColTypes(ParseState *pstate,
66  Node *larg, List *nrtargetlist);
67 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
68 static List *transformReturningList(ParseState *pstate, List *returningList);
70  List *targetList);
72  DeclareCursorStmt *stmt);
73 static Query *transformExplainStmt(ParseState *pstate,
74  ExplainStmt *stmt);
76  CreateTableAsStmt *stmt);
77 static void transformLockingClause(ParseState *pstate, Query *qry,
78  LockingClause *lc, bool pushedDown);
79 #ifdef RAW_EXPRESSION_COVERAGE_TEST
80 static bool test_raw_expression_coverage(Node *node, void *context);
81 #endif
82 
83 
84 /*
85  * parse_analyze
86  * Analyze a raw parse tree and transform it to Query form.
87  *
88  * Optionally, information about $n parameter types can be supplied.
89  * References to $n indexes not defined by paramTypes[] are disallowed.
90  *
91  * The result is a Query node. Optimizable statements require considerable
92  * transformation, while utility-type statements are simply hung off
93  * a dummy CMD_UTILITY Query node.
94  */
95 Query *
96 parse_analyze(RawStmt *parseTree, const char *sourceText,
97  Oid *paramTypes, int numParams,
98  QueryEnvironment *queryEnv)
99 {
100  ParseState *pstate = make_parsestate(NULL);
101  Query *query;
102 
103  Assert(sourceText != NULL); /* required as of 8.4 */
104 
105  pstate->p_sourcetext = sourceText;
106 
107  if (numParams > 0)
108  parse_fixed_parameters(pstate, paramTypes, numParams);
109 
110  pstate->p_queryEnv = queryEnv;
111 
112  query = transformTopLevelStmt(pstate, parseTree);
113 
115  (*post_parse_analyze_hook) (pstate, query);
116 
117  free_parsestate(pstate);
118 
119  return query;
120 }
121 
122 /*
123  * parse_analyze_varparams
124  *
125  * This variant is used when it's okay to deduce information about $n
126  * symbol datatypes from context. The passed-in paramTypes[] array can
127  * be modified or enlarged (via repalloc).
128  */
129 Query *
130 parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
131  Oid **paramTypes, int *numParams)
132 {
133  ParseState *pstate = make_parsestate(NULL);
134  Query *query;
135 
136  Assert(sourceText != NULL); /* required as of 8.4 */
137 
138  pstate->p_sourcetext = sourceText;
139 
140  parse_variable_parameters(pstate, paramTypes, numParams);
141 
142  query = transformTopLevelStmt(pstate, parseTree);
143 
144  /* make sure all is well with parameter types */
145  check_variable_parameters(pstate, query);
146 
148  (*post_parse_analyze_hook) (pstate, query);
149 
150  free_parsestate(pstate);
151 
152  return query;
153 }
154 
155 /*
156  * parse_sub_analyze
157  * Entry point for recursively analyzing a sub-statement.
158  */
159 Query *
160 parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
161  CommonTableExpr *parentCTE,
162  bool locked_from_parent,
163  bool resolve_unknowns)
164 {
165  ParseState *pstate = make_parsestate(parentParseState);
166  Query *query;
167 
168  pstate->p_parent_cte = parentCTE;
169  pstate->p_locked_from_parent = locked_from_parent;
170  pstate->p_resolve_unknowns = resolve_unknowns;
171 
172  query = transformStmt(pstate, parseTree);
173 
174  free_parsestate(pstate);
175 
176  return query;
177 }
178 
179 /*
180  * transformTopLevelStmt -
181  * transform a Parse tree into a Query tree.
182  *
183  * This function is just responsible for transferring statement location data
184  * from the RawStmt into the finished Query.
185  */
186 Query *
188 {
189  Query *result;
190 
191  /* We're at top level, so allow SELECT INTO */
192  result = transformOptionalSelectInto(pstate, parseTree->stmt);
193 
194  result->stmt_location = parseTree->stmt_location;
195  result->stmt_len = parseTree->stmt_len;
196 
197  return result;
198 }
199 
200 /*
201  * transformOptionalSelectInto -
202  * If SELECT has INTO, convert it to CREATE TABLE AS.
203  *
204  * The only thing we do here that we don't do in transformStmt() is to
205  * convert SELECT ... INTO into CREATE TABLE AS. Since utility statements
206  * aren't allowed within larger statements, this is only allowed at the top
207  * of the parse tree, and so we only try it before entering the recursive
208  * transformStmt() processing.
209  */
210 static Query *
212 {
213  if (IsA(parseTree, SelectStmt))
214  {
215  SelectStmt *stmt = (SelectStmt *) parseTree;
216 
217  /* If it's a set-operation tree, drill down to leftmost SelectStmt */
218  while (stmt && stmt->op != SETOP_NONE)
219  stmt = stmt->larg;
220  Assert(stmt && IsA(stmt, SelectStmt) &&stmt->larg == NULL);
221 
222  if (stmt->intoClause)
223  {
225 
226  ctas->query = parseTree;
227  ctas->into = stmt->intoClause;
228  ctas->relkind = OBJECT_TABLE;
229  ctas->is_select_into = true;
230 
231  /*
232  * Remove the intoClause from the SelectStmt. This makes it safe
233  * for transformSelectStmt to complain if it finds intoClause set
234  * (implying that the INTO appeared in a disallowed place).
235  */
236  stmt->intoClause = NULL;
237 
238  parseTree = (Node *) ctas;
239  }
240  }
241 
242  return transformStmt(pstate, parseTree);
243 }
244 
245 /*
246  * transformStmt -
247  * recursively transform a Parse tree into a Query tree.
248  */
249 Query *
250 transformStmt(ParseState *pstate, Node *parseTree)
251 {
252  Query *result;
253 
254  /*
255  * We apply RAW_EXPRESSION_COVERAGE_TEST testing to basic DML statements;
256  * we can't just run it on everything because raw_expression_tree_walker()
257  * doesn't claim to handle utility statements.
258  */
259 #ifdef RAW_EXPRESSION_COVERAGE_TEST
260  switch (nodeTag(parseTree))
261  {
262  case T_SelectStmt:
263  case T_InsertStmt:
264  case T_UpdateStmt:
265  case T_DeleteStmt:
266  (void) test_raw_expression_coverage(parseTree, NULL);
267  break;
268  default:
269  break;
270  }
271 #endif /* RAW_EXPRESSION_COVERAGE_TEST */
272 
273  switch (nodeTag(parseTree))
274  {
275  /*
276  * Optimizable statements
277  */
278  case T_InsertStmt:
279  result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
280  break;
281 
282  case T_DeleteStmt:
283  result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
284  break;
285 
286  case T_UpdateStmt:
287  result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
288  break;
289 
290  case T_SelectStmt:
291  {
292  SelectStmt *n = (SelectStmt *) parseTree;
293 
294  if (n->valuesLists)
295  result = transformValuesClause(pstate, n);
296  else if (n->op == SETOP_NONE)
297  result = transformSelectStmt(pstate, n);
298  else
299  result = transformSetOperationStmt(pstate, n);
300  }
301  break;
302 
303  /*
304  * Special cases
305  */
306  case T_DeclareCursorStmt:
307  result = transformDeclareCursorStmt(pstate,
308  (DeclareCursorStmt *) parseTree);
309  break;
310 
311  case T_ExplainStmt:
312  result = transformExplainStmt(pstate,
313  (ExplainStmt *) parseTree);
314  break;
315 
316  case T_CreateTableAsStmt:
317  result = transformCreateTableAsStmt(pstate,
318  (CreateTableAsStmt *) parseTree);
319  break;
320 
321  default:
322 
323  /*
324  * other statements don't require any transformation; just return
325  * the original parsetree with a Query node plastered on top.
326  */
327  result = makeNode(Query);
328  result->commandType = CMD_UTILITY;
329  result->utilityStmt = (Node *) parseTree;
330  break;
331  }
332 
333  /* Mark as original query until we learn differently */
334  result->querySource = QSRC_ORIGINAL;
335  result->canSetTag = true;
336 
337  return result;
338 }
339 
340 /*
341  * analyze_requires_snapshot
342  * Returns true if a snapshot must be set before doing parse analysis
343  * on the given raw parse tree.
344  *
345  * Classification here should match transformStmt().
346  */
347 bool
349 {
350  bool result;
351 
352  switch (nodeTag(parseTree->stmt))
353  {
354  /*
355  * Optimizable statements
356  */
357  case T_InsertStmt:
358  case T_DeleteStmt:
359  case T_UpdateStmt:
360  case T_SelectStmt:
361  result = true;
362  break;
363 
364  /*
365  * Special cases
366  */
367  case T_DeclareCursorStmt:
368  case T_ExplainStmt:
369  case T_CreateTableAsStmt:
370  /* yes, because we must analyze the contained statement */
371  result = true;
372  break;
373 
374  default:
375  /* other utility statements don't have any real parse analysis */
376  result = false;
377  break;
378  }
379 
380  return result;
381 }
382 
383 /*
384  * transformDeleteStmt -
385  * transforms a Delete Statement
386  */
387 static Query *
389 {
390  Query *qry = makeNode(Query);
391  ParseNamespaceItem *nsitem;
392  Node *qual;
393 
394  qry->commandType = CMD_DELETE;
395 
396  /* process the WITH clause independently of all else */
397  if (stmt->withClause)
398  {
399  qry->hasRecursive = stmt->withClause->recursive;
400  qry->cteList = transformWithClause(pstate, stmt->withClause);
401  qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
402  }
403 
404  /* set up range table with just the result rel */
405  qry->resultRelation = setTargetTable(pstate, stmt->relation,
406  stmt->relation->inh,
407  true,
408  ACL_DELETE);
409 
410  /* grab the namespace item made by setTargetTable */
411  nsitem = (ParseNamespaceItem *) llast(pstate->p_namespace);
412 
413  /* there's no DISTINCT in DELETE */
414  qry->distinctClause = NIL;
415 
416  /* subqueries in USING cannot access the result relation */
417  nsitem->p_lateral_only = true;
418  nsitem->p_lateral_ok = false;
419 
420  /*
421  * The USING clause is non-standard SQL syntax, and is equivalent in
422  * functionality to the FROM list that can be specified for UPDATE. The
423  * USING keyword is used rather than FROM because FROM is already a
424  * keyword in the DELETE syntax.
425  */
426  transformFromClause(pstate, stmt->usingClause);
427 
428  /* remaining clauses can reference the result relation normally */
429  nsitem->p_lateral_only = false;
430  nsitem->p_lateral_ok = true;
431 
432  qual = transformWhereClause(pstate, stmt->whereClause,
433  EXPR_KIND_WHERE, "WHERE");
434 
435  qry->returningList = transformReturningList(pstate, stmt->returningList);
436 
437  /* done building the range table and jointree */
438  qry->rtable = pstate->p_rtable;
439  qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
440 
441  qry->hasSubLinks = pstate->p_hasSubLinks;
442  qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
443  qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
444  qry->hasAggs = pstate->p_hasAggs;
445  if (pstate->p_hasAggs)
446  parseCheckAggregates(pstate, qry);
447 
448  assign_query_collations(pstate, qry);
449 
450  return qry;
451 }
452 
453 /*
454  * transformInsertStmt -
455  * transform an Insert Statement
456  */
457 static Query *
459 {
460  Query *qry = makeNode(Query);
461  SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
462  List *exprList = NIL;
463  bool isGeneralSelect;
464  List *sub_rtable;
465  List *sub_namespace;
466  List *icolumns;
467  List *attrnos;
468  RangeTblEntry *rte;
469  RangeTblRef *rtr;
470  ListCell *icols;
471  ListCell *attnos;
472  ListCell *lc;
473  bool isOnConflictUpdate;
474  AclMode targetPerms;
475 
476  /* There can't be any outer WITH to worry about */
477  Assert(pstate->p_ctenamespace == NIL);
478 
479  qry->commandType = CMD_INSERT;
480  pstate->p_is_insert = true;
481 
482  /* process the WITH clause independently of all else */
483  if (stmt->withClause)
484  {
485  qry->hasRecursive = stmt->withClause->recursive;
486  qry->cteList = transformWithClause(pstate, stmt->withClause);
487  qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
488  }
489 
490  qry->override = stmt->override;
491 
492  isOnConflictUpdate = (stmt->onConflictClause &&
494 
495  /*
496  * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
497  * VALUES list, or general SELECT input. We special-case VALUES, both for
498  * efficiency and so we can handle DEFAULT specifications.
499  *
500  * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
501  * VALUES clause. If we have any of those, treat it as a general SELECT;
502  * so it will work, but you can't use DEFAULT items together with those.
503  */
504  isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
505  selectStmt->sortClause != NIL ||
506  selectStmt->limitOffset != NULL ||
507  selectStmt->limitCount != NULL ||
508  selectStmt->lockingClause != NIL ||
509  selectStmt->withClause != NULL));
510 
511  /*
512  * If a non-nil rangetable/namespace was passed in, and we are doing
513  * INSERT/SELECT, arrange to pass the rangetable/namespace down to the
514  * SELECT. This can only happen if we are inside a CREATE RULE, and in
515  * that case we want the rule's OLD and NEW rtable entries to appear as
516  * part of the SELECT's rtable, not as outer references for it. (Kluge!)
517  * The SELECT's joinlist is not affected however. We must do this before
518  * adding the target table to the INSERT's rtable.
519  */
520  if (isGeneralSelect)
521  {
522  sub_rtable = pstate->p_rtable;
523  pstate->p_rtable = NIL;
524  sub_namespace = pstate->p_namespace;
525  pstate->p_namespace = NIL;
526  }
527  else
528  {
529  sub_rtable = NIL; /* not used, but keep compiler quiet */
530  sub_namespace = NIL;
531  }
532 
533  /*
534  * Must get write lock on INSERT target table before scanning SELECT, else
535  * we will grab the wrong kind of initial lock if the target table is also
536  * mentioned in the SELECT part. Note that the target table is not added
537  * to the joinlist or namespace.
538  */
539  targetPerms = ACL_INSERT;
540  if (isOnConflictUpdate)
541  targetPerms |= ACL_UPDATE;
542  qry->resultRelation = setTargetTable(pstate, stmt->relation,
543  false, false, targetPerms);
544 
545  /* Validate stmt->cols list, or build default list if no list given */
546  icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
547  Assert(list_length(icolumns) == list_length(attrnos));
548 
549  /*
550  * Determine which variant of INSERT we have.
551  */
552  if (selectStmt == NULL)
553  {
554  /*
555  * We have INSERT ... DEFAULT VALUES. We can handle this case by
556  * emitting an empty targetlist --- all columns will be defaulted when
557  * the planner expands the targetlist.
558  */
559  exprList = NIL;
560  }
561  else if (isGeneralSelect)
562  {
563  /*
564  * We make the sub-pstate a child of the outer pstate so that it can
565  * see any Param definitions supplied from above. Since the outer
566  * pstate's rtable and namespace are presently empty, there are no
567  * side-effects of exposing names the sub-SELECT shouldn't be able to
568  * see.
569  */
570  ParseState *sub_pstate = make_parsestate(pstate);
571  Query *selectQuery;
572 
573  /*
574  * Process the source SELECT.
575  *
576  * It is important that this be handled just like a standalone SELECT;
577  * otherwise the behavior of SELECT within INSERT might be different
578  * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
579  * bugs of just that nature...)
580  *
581  * The sole exception is that we prevent resolving unknown-type
582  * outputs as TEXT. This does not change the semantics since if the
583  * column type matters semantically, it would have been resolved to
584  * something else anyway. Doing this lets us resolve such outputs as
585  * the target column's type, which we handle below.
586  */
587  sub_pstate->p_rtable = sub_rtable;
588  sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
589  sub_pstate->p_namespace = sub_namespace;
590  sub_pstate->p_resolve_unknowns = false;
591 
592  selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
593 
594  free_parsestate(sub_pstate);
595 
596  /* The grammar should have produced a SELECT */
597  if (!IsA(selectQuery, Query) ||
598  selectQuery->commandType != CMD_SELECT)
599  elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
600 
601  /*
602  * Make the source be a subquery in the INSERT's rangetable, and add
603  * it to the INSERT's joinlist.
604  */
605  rte = addRangeTableEntryForSubquery(pstate,
606  selectQuery,
607  makeAlias("*SELECT*", NIL),
608  false,
609  false);
610  rtr = makeNode(RangeTblRef);
611  /* assume new rte is at end */
612  rtr->rtindex = list_length(pstate->p_rtable);
613  Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
614  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
615 
616  /*----------
617  * Generate an expression list for the INSERT that selects all the
618  * non-resjunk columns from the subquery. (INSERT's tlist must be
619  * separate from the subquery's tlist because we may add columns,
620  * insert datatype coercions, etc.)
621  *
622  * HACK: unknown-type constants and params in the SELECT's targetlist
623  * are copied up as-is rather than being referenced as subquery
624  * outputs. This is to ensure that when we try to coerce them to
625  * the target column's datatype, the right things happen (see
626  * special cases in coerce_type). Otherwise, this fails:
627  * INSERT INTO foo SELECT 'bar', ... FROM baz
628  *----------
629  */
630  exprList = NIL;
631  foreach(lc, selectQuery->targetList)
632  {
633  TargetEntry *tle = (TargetEntry *) lfirst(lc);
634  Expr *expr;
635 
636  if (tle->resjunk)
637  continue;
638  if (tle->expr &&
639  (IsA(tle->expr, Const) ||IsA(tle->expr, Param)) &&
640  exprType((Node *) tle->expr) == UNKNOWNOID)
641  expr = tle->expr;
642  else
643  {
644  Var *var = makeVarFromTargetEntry(rtr->rtindex, tle);
645 
646  var->location = exprLocation((Node *) tle->expr);
647  expr = (Expr *) var;
648  }
649  exprList = lappend(exprList, expr);
650  }
651 
652  /* Prepare row for assignment to target table */
653  exprList = transformInsertRow(pstate, exprList,
654  stmt->cols,
655  icolumns, attrnos,
656  false);
657  }
658  else if (list_length(selectStmt->valuesLists) > 1)
659  {
660  /*
661  * Process INSERT ... VALUES with multiple VALUES sublists. We
662  * generate a VALUES RTE holding the transformed expression lists, and
663  * build up a targetlist containing Vars that reference the VALUES
664  * RTE.
665  */
666  List *exprsLists = NIL;
667  List *coltypes = NIL;
668  List *coltypmods = NIL;
669  List *colcollations = NIL;
670  int sublist_length = -1;
671  bool lateral = false;
672 
673  Assert(selectStmt->intoClause == NULL);
674 
675  foreach(lc, selectStmt->valuesLists)
676  {
677  List *sublist = (List *) lfirst(lc);
678 
679  /*
680  * Do basic expression transformation (same as a ROW() expr, but
681  * allow SetToDefault at top level)
682  */
683  sublist = transformExpressionList(pstate, sublist,
684  EXPR_KIND_VALUES, true);
685 
686  /*
687  * All the sublists must be the same length, *after*
688  * transformation (which might expand '*' into multiple items).
689  * The VALUES RTE can't handle anything different.
690  */
691  if (sublist_length < 0)
692  {
693  /* Remember post-transformation length of first sublist */
694  sublist_length = list_length(sublist);
695  }
696  else if (sublist_length != list_length(sublist))
697  {
698  ereport(ERROR,
699  (errcode(ERRCODE_SYNTAX_ERROR),
700  errmsg("VALUES lists must all be the same length"),
701  parser_errposition(pstate,
702  exprLocation((Node *) sublist))));
703  }
704 
705  /*
706  * Prepare row for assignment to target table. We process any
707  * indirection on the target column specs normally but then strip
708  * off the resulting field/array assignment nodes, since we don't
709  * want the parsed statement to contain copies of those in each
710  * VALUES row. (It's annoying to have to transform the
711  * indirection specs over and over like this, but avoiding it
712  * would take some really messy refactoring of
713  * transformAssignmentIndirection.)
714  */
715  sublist = transformInsertRow(pstate, sublist,
716  stmt->cols,
717  icolumns, attrnos,
718  true);
719 
720  /*
721  * We must assign collations now because assign_query_collations
722  * doesn't process rangetable entries. We just assign all the
723  * collations independently in each row, and don't worry about
724  * whether they are consistent vertically. The outer INSERT query
725  * isn't going to care about the collations of the VALUES columns,
726  * so it's not worth the effort to identify a common collation for
727  * each one here. (But note this does have one user-visible
728  * consequence: INSERT ... VALUES won't complain about conflicting
729  * explicit COLLATEs in a column, whereas the same VALUES
730  * construct in another context would complain.)
731  */
732  assign_list_collations(pstate, sublist);
733 
734  exprsLists = lappend(exprsLists, sublist);
735  }
736 
737  /*
738  * Construct column type/typmod/collation lists for the VALUES RTE.
739  * Every expression in each column has been coerced to the type/typmod
740  * of the corresponding target column or subfield, so it's sufficient
741  * to look at the exprType/exprTypmod of the first row. We don't care
742  * about the collation labeling, so just fill in InvalidOid for that.
743  */
744  foreach(lc, (List *) linitial(exprsLists))
745  {
746  Node *val = (Node *) lfirst(lc);
747 
748  coltypes = lappend_oid(coltypes, exprType(val));
749  coltypmods = lappend_int(coltypmods, exprTypmod(val));
750  colcollations = lappend_oid(colcollations, InvalidOid);
751  }
752 
753  /*
754  * Ordinarily there can't be any current-level Vars in the expression
755  * lists, because the namespace was empty ... but if we're inside
756  * CREATE RULE, then NEW/OLD references might appear. In that case we
757  * have to mark the VALUES RTE as LATERAL.
758  */
759  if (list_length(pstate->p_rtable) != 1 &&
760  contain_vars_of_level((Node *) exprsLists, 0))
761  lateral = true;
762 
763  /*
764  * Generate the VALUES RTE
765  */
766  rte = addRangeTableEntryForValues(pstate, exprsLists,
767  coltypes, coltypmods, colcollations,
768  NULL, lateral, true);
769  rtr = makeNode(RangeTblRef);
770  /* assume new rte is at end */
771  rtr->rtindex = list_length(pstate->p_rtable);
772  Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
773  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
774 
775  /*
776  * Generate list of Vars referencing the RTE
777  */
778  expandRTE(rte, rtr->rtindex, 0, -1, false, NULL, &exprList);
779 
780  /*
781  * Re-apply any indirection on the target column specs to the Vars
782  */
783  exprList = transformInsertRow(pstate, exprList,
784  stmt->cols,
785  icolumns, attrnos,
786  false);
787  }
788  else
789  {
790  /*
791  * Process INSERT ... VALUES with a single VALUES sublist. We treat
792  * this case separately for efficiency. The sublist is just computed
793  * directly as the Query's targetlist, with no VALUES RTE. So it
794  * works just like a SELECT without any FROM.
795  */
796  List *valuesLists = selectStmt->valuesLists;
797 
798  Assert(list_length(valuesLists) == 1);
799  Assert(selectStmt->intoClause == NULL);
800 
801  /*
802  * Do basic expression transformation (same as a ROW() expr, but allow
803  * SetToDefault at top level)
804  */
805  exprList = transformExpressionList(pstate,
806  (List *) linitial(valuesLists),
808  true);
809 
810  /* Prepare row for assignment to target table */
811  exprList = transformInsertRow(pstate, exprList,
812  stmt->cols,
813  icolumns, attrnos,
814  false);
815  }
816 
817  /*
818  * Generate query's target list using the computed list of expressions.
819  * Also, mark all the target columns as needing insert permissions.
820  */
821  rte = pstate->p_target_rangetblentry;
822  qry->targetList = NIL;
823  icols = list_head(icolumns);
824  attnos = list_head(attrnos);
825  foreach(lc, exprList)
826  {
827  Expr *expr = (Expr *) lfirst(lc);
828  ResTarget *col;
829  AttrNumber attr_num;
830  TargetEntry *tle;
831 
832  col = lfirst_node(ResTarget, icols);
833  attr_num = (AttrNumber) lfirst_int(attnos);
834 
835  tle = makeTargetEntry(expr,
836  attr_num,
837  col->name,
838  false);
839  qry->targetList = lappend(qry->targetList, tle);
840 
841  rte->insertedCols = bms_add_member(rte->insertedCols,
843 
844  icols = lnext(icols);
845  attnos = lnext(attnos);
846  }
847 
848  /* Process ON CONFLICT, if any. */
849  if (stmt->onConflictClause)
850  {
851  /* Bail out if target relation is partitioned table */
852  if (pstate->p_target_rangetblentry->relkind == RELKIND_PARTITIONED_TABLE)
853  ereport(ERROR,
854  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
855  errmsg("ON CONFLICT clause is not supported with partitioned tables")));
856 
857  qry->onConflict = transformOnConflictClause(pstate,
858  stmt->onConflictClause);
859  }
860 
861  /*
862  * If we have a RETURNING clause, we need to add the target relation to
863  * the query namespace before processing it, so that Var references in
864  * RETURNING will work. Also, remove any namespace entries added in a
865  * sub-SELECT or VALUES list.
866  */
867  if (stmt->returningList)
868  {
869  pstate->p_namespace = NIL;
870  addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
871  false, true, true);
872  qry->returningList = transformReturningList(pstate,
873  stmt->returningList);
874  }
875 
876  /* done building the range table and jointree */
877  qry->rtable = pstate->p_rtable;
878  qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
879 
880  qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
881  qry->hasSubLinks = pstate->p_hasSubLinks;
882 
883  assign_query_collations(pstate, qry);
884 
885  return qry;
886 }
887 
888 /*
889  * Prepare an INSERT row for assignment to the target table.
890  *
891  * exprlist: transformed expressions for source values; these might come from
892  * a VALUES row, or be Vars referencing a sub-SELECT or VALUES RTE output.
893  * stmtcols: original target-columns spec for INSERT (we just test for NIL)
894  * icolumns: effective target-columns spec (list of ResTarget)
895  * attrnos: integer column numbers (must be same length as icolumns)
896  * strip_indirection: if true, remove any field/array assignment nodes
897  */
898 static List *
899 transformInsertRow(ParseState *pstate, List *exprlist,
900  List *stmtcols, List *icolumns, List *attrnos,
901  bool strip_indirection)
902 {
903  List *result;
904  ListCell *lc;
905  ListCell *icols;
906  ListCell *attnos;
907 
908  /*
909  * Check length of expr list. It must not have more expressions than
910  * there are target columns. We allow fewer, but only if no explicit
911  * columns list was given (the remaining columns are implicitly
912  * defaulted). Note we must check this *after* transformation because
913  * that could expand '*' into multiple items.
914  */
915  if (list_length(exprlist) > list_length(icolumns))
916  ereport(ERROR,
917  (errcode(ERRCODE_SYNTAX_ERROR),
918  errmsg("INSERT has more expressions than target columns"),
919  parser_errposition(pstate,
920  exprLocation(list_nth(exprlist,
921  list_length(icolumns))))));
922  if (stmtcols != NIL &&
923  list_length(exprlist) < list_length(icolumns))
924  {
925  /*
926  * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
927  * where the user accidentally created a RowExpr instead of separate
928  * columns. Add a suitable hint if that seems to be the problem,
929  * because the main error message is quite misleading for this case.
930  * (If there's no stmtcols, you'll get something about data type
931  * mismatch, which is less misleading so we don't worry about giving a
932  * hint in that case.)
933  */
934  ereport(ERROR,
935  (errcode(ERRCODE_SYNTAX_ERROR),
936  errmsg("INSERT has more target columns than expressions"),
937  ((list_length(exprlist) == 1 &&
938  count_rowexpr_columns(pstate, linitial(exprlist)) ==
939  list_length(icolumns)) ?
940  errhint("The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?") : 0),
941  parser_errposition(pstate,
942  exprLocation(list_nth(icolumns,
943  list_length(exprlist))))));
944  }
945 
946  /*
947  * Prepare columns for assignment to target table.
948  */
949  result = NIL;
950  icols = list_head(icolumns);
951  attnos = list_head(attrnos);
952  foreach(lc, exprlist)
953  {
954  Expr *expr = (Expr *) lfirst(lc);
955  ResTarget *col;
956 
957  col = lfirst_node(ResTarget, icols);
958 
959  expr = transformAssignedExpr(pstate, expr,
961  col->name,
962  lfirst_int(attnos),
963  col->indirection,
964  col->location);
965 
966  if (strip_indirection)
967  {
968  while (expr)
969  {
970  if (IsA(expr, FieldStore))
971  {
972  FieldStore *fstore = (FieldStore *) expr;
973 
974  expr = (Expr *) linitial(fstore->newvals);
975  }
976  else if (IsA(expr, ArrayRef))
977  {
978  ArrayRef *aref = (ArrayRef *) expr;
979 
980  if (aref->refassgnexpr == NULL)
981  break;
982  expr = aref->refassgnexpr;
983  }
984  else
985  break;
986  }
987  }
988 
989  result = lappend(result, expr);
990 
991  icols = lnext(icols);
992  attnos = lnext(attnos);
993  }
994 
995  return result;
996 }
997 
998 /*
999  * transformOnConflictClause -
1000  * transforms an OnConflictClause in an INSERT
1001  */
1002 static OnConflictExpr *
1004  OnConflictClause *onConflictClause)
1005 {
1006  List *arbiterElems;
1007  Node *arbiterWhere;
1008  Oid arbiterConstraint;
1009  List *onConflictSet = NIL;
1010  Node *onConflictWhere = NULL;
1011  RangeTblEntry *exclRte = NULL;
1012  int exclRelIndex = 0;
1013  List *exclRelTlist = NIL;
1015 
1016  /* Process the arbiter clause, ON CONFLICT ON (...) */
1017  transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
1018  &arbiterWhere, &arbiterConstraint);
1019 
1020  /* Process DO UPDATE */
1021  if (onConflictClause->action == ONCONFLICT_UPDATE)
1022  {
1023  Relation targetrel = pstate->p_target_relation;
1024  Var *var;
1025  TargetEntry *te;
1026  int attno;
1027 
1028  /*
1029  * All INSERT expressions have been parsed, get ready for potentially
1030  * existing SET statements that need to be processed like an UPDATE.
1031  */
1032  pstate->p_is_insert = false;
1033 
1034  /*
1035  * Add range table entry for the EXCLUDED pseudo relation; relkind is
1036  * set to composite to signal that we're not dealing with an actual
1037  * relation.
1038  */
1039  exclRte = addRangeTableEntryForRelation(pstate,
1040  targetrel,
1041  makeAlias("excluded", NIL),
1042  false, false);
1043  exclRte->relkind = RELKIND_COMPOSITE_TYPE;
1044  exclRelIndex = list_length(pstate->p_rtable);
1045 
1046  /*
1047  * Build a targetlist representing the columns of the EXCLUDED pseudo
1048  * relation. Have to be careful to use resnos that correspond to
1049  * attnos of the underlying relation.
1050  */
1051  for (attno = 0; attno < targetrel->rd_rel->relnatts; attno++)
1052  {
1053  Form_pg_attribute attr = targetrel->rd_att->attrs[attno];
1054  char *name;
1055 
1056  if (attr->attisdropped)
1057  {
1058  /*
1059  * can't use atttypid here, but it doesn't really matter what
1060  * type the Const claims to be.
1061  */
1062  var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1063  name = "";
1064  }
1065  else
1066  {
1067  var = makeVar(exclRelIndex, attno + 1,
1068  attr->atttypid, attr->atttypmod,
1069  attr->attcollation,
1070  0);
1071  name = pstrdup(NameStr(attr->attname));
1072  }
1073 
1074  te = makeTargetEntry((Expr *) var,
1075  attno + 1,
1076  name,
1077  false);
1078 
1079  /* don't require select access yet */
1080  exclRelTlist = lappend(exclRelTlist, te);
1081  }
1082 
1083  /*
1084  * Add a whole-row-Var entry to support references to "EXCLUDED.*".
1085  * Like the other entries in exclRelTlist, its resno must match the
1086  * Var's varattno, else the wrong things happen while resolving
1087  * references in setrefs.c. This is against normal conventions for
1088  * targetlists, but it's okay since we don't use this as a real tlist.
1089  */
1090  var = makeVar(exclRelIndex, InvalidAttrNumber,
1091  targetrel->rd_rel->reltype,
1092  -1, InvalidOid, 0);
1093  te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1094  exclRelTlist = lappend(exclRelTlist, te);
1095 
1096  /*
1097  * Add EXCLUDED and the target RTE to the namespace, so that they can
1098  * be used in the UPDATE statement.
1099  */
1100  addRTEtoQuery(pstate, exclRte, false, true, true);
1101  addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
1102  false, true, true);
1103 
1104  onConflictSet =
1105  transformUpdateTargetList(pstate, onConflictClause->targetList);
1106 
1107  onConflictWhere = transformWhereClause(pstate,
1108  onConflictClause->whereClause,
1109  EXPR_KIND_WHERE, "WHERE");
1110  }
1111 
1112  /* Finally, build ON CONFLICT DO [NOTHING | UPDATE] expression */
1113  result = makeNode(OnConflictExpr);
1114 
1115  result->action = onConflictClause->action;
1116  result->arbiterElems = arbiterElems;
1117  result->arbiterWhere = arbiterWhere;
1118  result->constraint = arbiterConstraint;
1119  result->onConflictSet = onConflictSet;
1120  result->onConflictWhere = onConflictWhere;
1121  result->exclRelIndex = exclRelIndex;
1122  result->exclRelTlist = exclRelTlist;
1123 
1124  return result;
1125 }
1126 
1127 
1128 /*
1129  * count_rowexpr_columns -
1130  * get number of columns contained in a ROW() expression;
1131  * return -1 if expression isn't a RowExpr or a Var referencing one.
1132  *
1133  * This is currently used only for hint purposes, so we aren't terribly
1134  * tense about recognizing all possible cases. The Var case is interesting
1135  * because that's what we'll get in the INSERT ... SELECT (...) case.
1136  */
1137 static int
1139 {
1140  if (expr == NULL)
1141  return -1;
1142  if (IsA(expr, RowExpr))
1143  return list_length(((RowExpr *) expr)->args);
1144  if (IsA(expr, Var))
1145  {
1146  Var *var = (Var *) expr;
1147  AttrNumber attnum = var->varattno;
1148 
1149  if (attnum > 0 && var->vartype == RECORDOID)
1150  {
1151  RangeTblEntry *rte;
1152 
1153  rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1154  if (rte->rtekind == RTE_SUBQUERY)
1155  {
1156  /* Subselect-in-FROM: examine sub-select's output expr */
1158  attnum);
1159 
1160  if (ste == NULL || ste->resjunk)
1161  return -1;
1162  expr = (Node *) ste->expr;
1163  if (IsA(expr, RowExpr))
1164  return list_length(((RowExpr *) expr)->args);
1165  }
1166  }
1167  }
1168  return -1;
1169 }
1170 
1171 
1172 /*
1173  * transformSelectStmt -
1174  * transforms a Select Statement
1175  *
1176  * Note: this covers only cases with no set operations and no VALUES lists;
1177  * see below for the other cases.
1178  */
1179 static Query *
1181 {
1182  Query *qry = makeNode(Query);
1183  Node *qual;
1184  ListCell *l;
1185 
1186  qry->commandType = CMD_SELECT;
1187 
1188  /* process the WITH clause independently of all else */
1189  if (stmt->withClause)
1190  {
1191  qry->hasRecursive = stmt->withClause->recursive;
1192  qry->cteList = transformWithClause(pstate, stmt->withClause);
1193  qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1194  }
1195 
1196  /* Complain if we get called from someplace where INTO is not allowed */
1197  if (stmt->intoClause)
1198  ereport(ERROR,
1199  (errcode(ERRCODE_SYNTAX_ERROR),
1200  errmsg("SELECT ... INTO is not allowed here"),
1201  parser_errposition(pstate,
1202  exprLocation((Node *) stmt->intoClause))));
1203 
1204  /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
1205  pstate->p_locking_clause = stmt->lockingClause;
1206 
1207  /* make WINDOW info available for window functions, too */
1208  pstate->p_windowdefs = stmt->windowClause;
1209 
1210  /* process the FROM clause */
1211  transformFromClause(pstate, stmt->fromClause);
1212 
1213  /* transform targetlist */
1214  qry->targetList = transformTargetList(pstate, stmt->targetList,
1216 
1217  /* mark column origins */
1218  markTargetListOrigins(pstate, qry->targetList);
1219 
1220  /* transform WHERE */
1221  qual = transformWhereClause(pstate, stmt->whereClause,
1222  EXPR_KIND_WHERE, "WHERE");
1223 
1224  /* initial processing of HAVING clause is much like WHERE clause */
1225  qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
1226  EXPR_KIND_HAVING, "HAVING");
1227 
1228  /*
1229  * Transform sorting/grouping stuff. Do ORDER BY first because both
1230  * transformGroupClause and transformDistinctClause need the results. Note
1231  * that these functions can also change the targetList, so it's passed to
1232  * them by reference.
1233  */
1234  qry->sortClause = transformSortClause(pstate,
1235  stmt->sortClause,
1236  &qry->targetList,
1238  false /* allow SQL92 rules */ );
1239 
1240  qry->groupClause = transformGroupClause(pstate,
1241  stmt->groupClause,
1242  &qry->groupingSets,
1243  &qry->targetList,
1244  qry->sortClause,
1246  false /* allow SQL92 rules */ );
1247 
1248  if (stmt->distinctClause == NIL)
1249  {
1250  qry->distinctClause = NIL;
1251  qry->hasDistinctOn = false;
1252  }
1253  else if (linitial(stmt->distinctClause) == NULL)
1254  {
1255  /* We had SELECT DISTINCT */
1257  &qry->targetList,
1258  qry->sortClause,
1259  false);
1260  qry->hasDistinctOn = false;
1261  }
1262  else
1263  {
1264  /* We had SELECT DISTINCT ON */
1266  stmt->distinctClause,
1267  &qry->targetList,
1268  qry->sortClause);
1269  qry->hasDistinctOn = true;
1270  }
1271 
1272  /* transform LIMIT */
1273  qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1274  EXPR_KIND_OFFSET, "OFFSET");
1275  qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1276  EXPR_KIND_LIMIT, "LIMIT");
1277 
1278  /* transform window clauses after we have seen all window functions */
1280  pstate->p_windowdefs,
1281  &qry->targetList);
1282 
1283  /* resolve any still-unresolved output columns as being type text */
1284  if (pstate->p_resolve_unknowns)
1285  resolveTargetListUnknowns(pstate, qry->targetList);
1286 
1287  qry->rtable = pstate->p_rtable;
1288  qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1289 
1290  qry->hasSubLinks = pstate->p_hasSubLinks;
1291  qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1292  qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1293  qry->hasAggs = pstate->p_hasAggs;
1294  if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1295  parseCheckAggregates(pstate, qry);
1296 
1297  foreach(l, stmt->lockingClause)
1298  {
1299  transformLockingClause(pstate, qry,
1300  (LockingClause *) lfirst(l), false);
1301  }
1302 
1303  assign_query_collations(pstate, qry);
1304 
1305  return qry;
1306 }
1307 
1308 /*
1309  * transformValuesClause -
1310  * transforms a VALUES clause that's being used as a standalone SELECT
1311  *
1312  * We build a Query containing a VALUES RTE, rather as if one had written
1313  * SELECT * FROM (VALUES ...) AS "*VALUES*"
1314  */
1315 static Query *
1317 {
1318  Query *qry = makeNode(Query);
1319  List *exprsLists;
1320  List *coltypes = NIL;
1321  List *coltypmods = NIL;
1322  List *colcollations = NIL;
1323  List **colexprs = NULL;
1324  int sublist_length = -1;
1325  bool lateral = false;
1326  RangeTblEntry *rte;
1327  int rtindex;
1328  ListCell *lc;
1329  ListCell *lc2;
1330  int i;
1331 
1332  qry->commandType = CMD_SELECT;
1333 
1334  /* Most SELECT stuff doesn't apply in a VALUES clause */
1335  Assert(stmt->distinctClause == NIL);
1336  Assert(stmt->intoClause == NULL);
1337  Assert(stmt->targetList == NIL);
1338  Assert(stmt->fromClause == NIL);
1339  Assert(stmt->whereClause == NULL);
1340  Assert(stmt->groupClause == NIL);
1341  Assert(stmt->havingClause == NULL);
1342  Assert(stmt->windowClause == NIL);
1343  Assert(stmt->op == SETOP_NONE);
1344 
1345  /* process the WITH clause independently of all else */
1346  if (stmt->withClause)
1347  {
1348  qry->hasRecursive = stmt->withClause->recursive;
1349  qry->cteList = transformWithClause(pstate, stmt->withClause);
1350  qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1351  }
1352 
1353  /*
1354  * For each row of VALUES, transform the raw expressions.
1355  *
1356  * Note that the intermediate representation we build is column-organized
1357  * not row-organized. That simplifies the type and collation processing
1358  * below.
1359  */
1360  foreach(lc, stmt->valuesLists)
1361  {
1362  List *sublist = (List *) lfirst(lc);
1363 
1364  /*
1365  * Do basic expression transformation (same as a ROW() expr, but here
1366  * we disallow SetToDefault)
1367  */
1368  sublist = transformExpressionList(pstate, sublist,
1369  EXPR_KIND_VALUES, false);
1370 
1371  /*
1372  * All the sublists must be the same length, *after* transformation
1373  * (which might expand '*' into multiple items). The VALUES RTE can't
1374  * handle anything different.
1375  */
1376  if (sublist_length < 0)
1377  {
1378  /* Remember post-transformation length of first sublist */
1379  sublist_length = list_length(sublist);
1380  /* and allocate array for per-column lists */
1381  colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1382  }
1383  else if (sublist_length != list_length(sublist))
1384  {
1385  ereport(ERROR,
1386  (errcode(ERRCODE_SYNTAX_ERROR),
1387  errmsg("VALUES lists must all be the same length"),
1388  parser_errposition(pstate,
1389  exprLocation((Node *) sublist))));
1390  }
1391 
1392  /* Build per-column expression lists */
1393  i = 0;
1394  foreach(lc2, sublist)
1395  {
1396  Node *col = (Node *) lfirst(lc2);
1397 
1398  colexprs[i] = lappend(colexprs[i], col);
1399  i++;
1400  }
1401 
1402  /* Release sub-list's cells to save memory */
1403  list_free(sublist);
1404  }
1405 
1406  /*
1407  * Now resolve the common types of the columns, and coerce everything to
1408  * those types. Then identify the common typmod and common collation, if
1409  * any, of each column.
1410  *
1411  * We must do collation processing now because (1) assign_query_collations
1412  * doesn't process rangetable entries, and (2) we need to label the VALUES
1413  * RTE with column collations for use in the outer query. We don't
1414  * consider conflict of implicit collations to be an error here; instead
1415  * the column will just show InvalidOid as its collation, and you'll get a
1416  * failure later if that results in failure to resolve a collation.
1417  *
1418  * Note we modify the per-column expression lists in-place.
1419  */
1420  for (i = 0; i < sublist_length; i++)
1421  {
1422  Oid coltype;
1423  int32 coltypmod = -1;
1424  Oid colcoll;
1425  bool first = true;
1426 
1427  coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
1428 
1429  foreach(lc, colexprs[i])
1430  {
1431  Node *col = (Node *) lfirst(lc);
1432 
1433  col = coerce_to_common_type(pstate, col, coltype, "VALUES");
1434  lfirst(lc) = (void *) col;
1435  if (first)
1436  {
1437  coltypmod = exprTypmod(col);
1438  first = false;
1439  }
1440  else
1441  {
1442  /* As soon as we see a non-matching typmod, fall back to -1 */
1443  if (coltypmod >= 0 && coltypmod != exprTypmod(col))
1444  coltypmod = -1;
1445  }
1446  }
1447 
1448  colcoll = select_common_collation(pstate, colexprs[i], true);
1449 
1450  coltypes = lappend_oid(coltypes, coltype);
1451  coltypmods = lappend_int(coltypmods, coltypmod);
1452  colcollations = lappend_oid(colcollations, colcoll);
1453  }
1454 
1455  /*
1456  * Finally, rearrange the coerced expressions into row-organized lists.
1457  */
1458  exprsLists = NIL;
1459  foreach(lc, colexprs[0])
1460  {
1461  Node *col = (Node *) lfirst(lc);
1462  List *sublist;
1463 
1464  sublist = list_make1(col);
1465  exprsLists = lappend(exprsLists, sublist);
1466  }
1467  list_free(colexprs[0]);
1468  for (i = 1; i < sublist_length; i++)
1469  {
1470  forboth(lc, colexprs[i], lc2, exprsLists)
1471  {
1472  Node *col = (Node *) lfirst(lc);
1473  List *sublist = lfirst(lc2);
1474 
1475  /* sublist pointer in exprsLists won't need adjustment */
1476  (void) lappend(sublist, col);
1477  }
1478  list_free(colexprs[i]);
1479  }
1480 
1481  /*
1482  * Ordinarily there can't be any current-level Vars in the expression
1483  * lists, because the namespace was empty ... but if we're inside CREATE
1484  * RULE, then NEW/OLD references might appear. In that case we have to
1485  * mark the VALUES RTE as LATERAL.
1486  */
1487  if (pstate->p_rtable != NIL &&
1488  contain_vars_of_level((Node *) exprsLists, 0))
1489  lateral = true;
1490 
1491  /*
1492  * Generate the VALUES RTE
1493  */
1494  rte = addRangeTableEntryForValues(pstate, exprsLists,
1495  coltypes, coltypmods, colcollations,
1496  NULL, lateral, true);
1497  addRTEtoQuery(pstate, rte, true, true, true);
1498 
1499  /* assume new rte is at end */
1500  rtindex = list_length(pstate->p_rtable);
1501  Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1502 
1503  /*
1504  * Generate a targetlist as though expanding "*"
1505  */
1506  Assert(pstate->p_next_resno == 1);
1507  qry->targetList = expandRelAttrs(pstate, rte, rtindex, 0, -1);
1508 
1509  /*
1510  * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1511  * VALUES, so cope.
1512  */
1513  qry->sortClause = transformSortClause(pstate,
1514  stmt->sortClause,
1515  &qry->targetList,
1517  false /* allow SQL92 rules */ );
1518 
1519  qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1520  EXPR_KIND_OFFSET, "OFFSET");
1521  qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1522  EXPR_KIND_LIMIT, "LIMIT");
1523 
1524  if (stmt->lockingClause)
1525  ereport(ERROR,
1526  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1527  /*------
1528  translator: %s is a SQL row locking clause such as FOR UPDATE */
1529  errmsg("%s cannot be applied to VALUES",
1531  linitial(stmt->lockingClause))->strength))));
1532 
1533  qry->rtable = pstate->p_rtable;
1534  qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1535 
1536  qry->hasSubLinks = pstate->p_hasSubLinks;
1537 
1538  assign_query_collations(pstate, qry);
1539 
1540  return qry;
1541 }
1542 
1543 /*
1544  * transformSetOperationStmt -
1545  * transforms a set-operations tree
1546  *
1547  * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
1548  * structure to it. We must transform each leaf SELECT and build up a top-
1549  * level Query that contains the leaf SELECTs as subqueries in its rangetable.
1550  * The tree of set operations is converted into the setOperations field of
1551  * the top-level Query.
1552  */
1553 static Query *
1555 {
1556  Query *qry = makeNode(Query);
1557  SelectStmt *leftmostSelect;
1558  int leftmostRTI;
1559  Query *leftmostQuery;
1560  SetOperationStmt *sostmt;
1561  List *sortClause;
1562  Node *limitOffset;
1563  Node *limitCount;
1564  List *lockingClause;
1565  WithClause *withClause;
1566  Node *node;
1567  ListCell *left_tlist,
1568  *lct,
1569  *lcm,
1570  *lcc,
1571  *l;
1572  List *targetvars,
1573  *targetnames,
1574  *sv_namespace;
1575  int sv_rtable_length;
1576  RangeTblEntry *jrte;
1577  int tllen;
1578 
1579  qry->commandType = CMD_SELECT;
1580 
1581  /*
1582  * Find leftmost leaf SelectStmt. We currently only need to do this in
1583  * order to deliver a suitable error message if there's an INTO clause
1584  * there, implying the set-op tree is in a context that doesn't allow
1585  * INTO. (transformSetOperationTree would throw error anyway, but it
1586  * seems worth the trouble to throw a different error for non-leftmost
1587  * INTO, so we produce that error in transformSetOperationTree.)
1588  */
1589  leftmostSelect = stmt->larg;
1590  while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1591  leftmostSelect = leftmostSelect->larg;
1592  Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1593  leftmostSelect->larg == NULL);
1594  if (leftmostSelect->intoClause)
1595  ereport(ERROR,
1596  (errcode(ERRCODE_SYNTAX_ERROR),
1597  errmsg("SELECT ... INTO is not allowed here"),
1598  parser_errposition(pstate,
1599  exprLocation((Node *) leftmostSelect->intoClause))));
1600 
1601  /*
1602  * We need to extract ORDER BY and other top-level clauses here and not
1603  * let transformSetOperationTree() see them --- else it'll just recurse
1604  * right back here!
1605  */
1606  sortClause = stmt->sortClause;
1607  limitOffset = stmt->limitOffset;
1608  limitCount = stmt->limitCount;
1609  lockingClause = stmt->lockingClause;
1610  withClause = stmt->withClause;
1611 
1612  stmt->sortClause = NIL;
1613  stmt->limitOffset = NULL;
1614  stmt->limitCount = NULL;
1615  stmt->lockingClause = NIL;
1616  stmt->withClause = NULL;
1617 
1618  /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1619  if (lockingClause)
1620  ereport(ERROR,
1621  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1622  /*------
1623  translator: %s is a SQL row locking clause such as FOR UPDATE */
1624  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1626  linitial(lockingClause))->strength))));
1627 
1628  /* Process the WITH clause independently of all else */
1629  if (withClause)
1630  {
1631  qry->hasRecursive = withClause->recursive;
1632  qry->cteList = transformWithClause(pstate, withClause);
1633  qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1634  }
1635 
1636  /*
1637  * Recursively transform the components of the tree.
1638  */
1639  sostmt = castNode(SetOperationStmt,
1640  transformSetOperationTree(pstate, stmt, true, NULL));
1641  Assert(sostmt);
1642  qry->setOperations = (Node *) sostmt;
1643 
1644  /*
1645  * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1646  */
1647  node = sostmt->larg;
1648  while (node && IsA(node, SetOperationStmt))
1649  node = ((SetOperationStmt *) node)->larg;
1650  Assert(node && IsA(node, RangeTblRef));
1651  leftmostRTI = ((RangeTblRef *) node)->rtindex;
1652  leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1653  Assert(leftmostQuery != NULL);
1654 
1655  /*
1656  * Generate dummy targetlist for outer query using column names of
1657  * leftmost select and common datatypes/collations of topmost set
1658  * operation. Also make lists of the dummy vars and their names for use
1659  * in parsing ORDER BY.
1660  *
1661  * Note: we use leftmostRTI as the varno of the dummy variables. It
1662  * shouldn't matter too much which RT index they have, as long as they
1663  * have one that corresponds to a real RT entry; else funny things may
1664  * happen when the tree is mashed by rule rewriting.
1665  */
1666  qry->targetList = NIL;
1667  targetvars = NIL;
1668  targetnames = NIL;
1669  left_tlist = list_head(leftmostQuery->targetList);
1670 
1671  forthree(lct, sostmt->colTypes,
1672  lcm, sostmt->colTypmods,
1673  lcc, sostmt->colCollations)
1674  {
1675  Oid colType = lfirst_oid(lct);
1676  int32 colTypmod = lfirst_int(lcm);
1677  Oid colCollation = lfirst_oid(lcc);
1678  TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1679  char *colName;
1680  TargetEntry *tle;
1681  Var *var;
1682 
1683  Assert(!lefttle->resjunk);
1684  colName = pstrdup(lefttle->resname);
1685  var = makeVar(leftmostRTI,
1686  lefttle->resno,
1687  colType,
1688  colTypmod,
1689  colCollation,
1690  0);
1691  var->location = exprLocation((Node *) lefttle->expr);
1692  tle = makeTargetEntry((Expr *) var,
1693  (AttrNumber) pstate->p_next_resno++,
1694  colName,
1695  false);
1696  qry->targetList = lappend(qry->targetList, tle);
1697  targetvars = lappend(targetvars, var);
1698  targetnames = lappend(targetnames, makeString(colName));
1699  left_tlist = lnext(left_tlist);
1700  }
1701 
1702  /*
1703  * As a first step towards supporting sort clauses that are expressions
1704  * using the output columns, generate a namespace entry that makes the
1705  * output columns visible. A Join RTE node is handy for this, since we
1706  * can easily control the Vars generated upon matches.
1707  *
1708  * Note: we don't yet do anything useful with such cases, but at least
1709  * "ORDER BY upper(foo)" will draw the right error message rather than
1710  * "foo not found".
1711  */
1712  sv_rtable_length = list_length(pstate->p_rtable);
1713 
1714  jrte = addRangeTableEntryForJoin(pstate,
1715  targetnames,
1716  JOIN_INNER,
1717  targetvars,
1718  NULL,
1719  false);
1720 
1721  sv_namespace = pstate->p_namespace;
1722  pstate->p_namespace = NIL;
1723 
1724  /* add jrte to column namespace only */
1725  addRTEtoQuery(pstate, jrte, false, false, true);
1726 
1727  /*
1728  * For now, we don't support resjunk sort clauses on the output of a
1729  * setOperation tree --- you can only use the SQL92-spec options of
1730  * selecting an output column by name or number. Enforce by checking that
1731  * transformSortClause doesn't add any items to tlist.
1732  */
1733  tllen = list_length(qry->targetList);
1734 
1735  qry->sortClause = transformSortClause(pstate,
1736  sortClause,
1737  &qry->targetList,
1739  false /* allow SQL92 rules */ );
1740 
1741  /* restore namespace, remove jrte from rtable */
1742  pstate->p_namespace = sv_namespace;
1743  pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1744 
1745  if (tllen != list_length(qry->targetList))
1746  ereport(ERROR,
1747  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1748  errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
1749  errdetail("Only result column names can be used, not expressions or functions."),
1750  errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
1751  parser_errposition(pstate,
1752  exprLocation(list_nth(qry->targetList, tllen)))));
1753 
1754  qry->limitOffset = transformLimitClause(pstate, limitOffset,
1755  EXPR_KIND_OFFSET, "OFFSET");
1756  qry->limitCount = transformLimitClause(pstate, limitCount,
1757  EXPR_KIND_LIMIT, "LIMIT");
1758 
1759  qry->rtable = pstate->p_rtable;
1760  qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1761 
1762  qry->hasSubLinks = pstate->p_hasSubLinks;
1763  qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1764  qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1765  qry->hasAggs = pstate->p_hasAggs;
1766  if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1767  parseCheckAggregates(pstate, qry);
1768 
1769  foreach(l, lockingClause)
1770  {
1771  transformLockingClause(pstate, qry,
1772  (LockingClause *) lfirst(l), false);
1773  }
1774 
1775  assign_query_collations(pstate, qry);
1776 
1777  return qry;
1778 }
1779 
1780 /*
1781  * transformSetOperationTree
1782  * Recursively transform leaves and internal nodes of a set-op tree
1783  *
1784  * In addition to returning the transformed node, if targetlist isn't NULL
1785  * then we return a list of its non-resjunk TargetEntry nodes. For a leaf
1786  * set-op node these are the actual targetlist entries; otherwise they are
1787  * dummy entries created to carry the type, typmod, collation, and location
1788  * (for error messages) of each output column of the set-op node. This info
1789  * is needed only during the internal recursion of this function, so outside
1790  * callers pass NULL for targetlist. Note: the reason for passing the
1791  * actual targetlist entries of a leaf node is so that upper levels can
1792  * replace UNKNOWN Consts with properly-coerced constants.
1793  */
1794 static Node *
1796  bool isTopLevel, List **targetlist)
1797 {
1798  bool isLeaf;
1799 
1800  Assert(stmt && IsA(stmt, SelectStmt));
1801 
1802  /* Guard against stack overflow due to overly complex set-expressions */
1804 
1805  /*
1806  * Validity-check both leaf and internal SELECTs for disallowed ops.
1807  */
1808  if (stmt->intoClause)
1809  ereport(ERROR,
1810  (errcode(ERRCODE_SYNTAX_ERROR),
1811  errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
1812  parser_errposition(pstate,
1813  exprLocation((Node *) stmt->intoClause))));
1814 
1815  /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1816  if (stmt->lockingClause)
1817  ereport(ERROR,
1818  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1819  /*------
1820  translator: %s is a SQL row locking clause such as FOR UPDATE */
1821  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1823  linitial(stmt->lockingClause))->strength))));
1824 
1825  /*
1826  * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
1827  * or WITH clauses attached, we need to treat it like a leaf node to
1828  * generate an independent sub-Query tree. Otherwise, it can be
1829  * represented by a SetOperationStmt node underneath the parent Query.
1830  */
1831  if (stmt->op == SETOP_NONE)
1832  {
1833  Assert(stmt->larg == NULL && stmt->rarg == NULL);
1834  isLeaf = true;
1835  }
1836  else
1837  {
1838  Assert(stmt->larg != NULL && stmt->rarg != NULL);
1839  if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
1840  stmt->lockingClause || stmt->withClause)
1841  isLeaf = true;
1842  else
1843  isLeaf = false;
1844  }
1845 
1846  if (isLeaf)
1847  {
1848  /* Process leaf SELECT */
1849  Query *selectQuery;
1850  char selectName[32];
1852  RangeTblRef *rtr;
1853  ListCell *tl;
1854 
1855  /*
1856  * Transform SelectStmt into a Query.
1857  *
1858  * This works the same as SELECT transformation normally would, except
1859  * that we prevent resolving unknown-type outputs as TEXT. This does
1860  * not change the subquery's semantics since if the column type
1861  * matters semantically, it would have been resolved to something else
1862  * anyway. Doing this lets us resolve such outputs using
1863  * select_common_type(), below.
1864  *
1865  * Note: previously transformed sub-queries don't affect the parsing
1866  * of this sub-query, because they are not in the toplevel pstate's
1867  * namespace list.
1868  */
1869  selectQuery = parse_sub_analyze((Node *) stmt, pstate,
1870  NULL, false, false);
1871 
1872  /*
1873  * Check for bogus references to Vars on the current query level (but
1874  * upper-level references are okay). Normally this can't happen
1875  * because the namespace will be empty, but it could happen if we are
1876  * inside a rule.
1877  */
1878  if (pstate->p_namespace)
1879  {
1880  if (contain_vars_of_level((Node *) selectQuery, 1))
1881  ereport(ERROR,
1882  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1883  errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
1884  parser_errposition(pstate,
1885  locate_var_of_level((Node *) selectQuery, 1))));
1886  }
1887 
1888  /*
1889  * Extract a list of the non-junk TLEs for upper-level processing.
1890  */
1891  if (targetlist)
1892  {
1893  *targetlist = NIL;
1894  foreach(tl, selectQuery->targetList)
1895  {
1896  TargetEntry *tle = (TargetEntry *) lfirst(tl);
1897 
1898  if (!tle->resjunk)
1899  *targetlist = lappend(*targetlist, tle);
1900  }
1901  }
1902 
1903  /*
1904  * Make the leaf query be a subquery in the top-level rangetable.
1905  */
1906  snprintf(selectName, sizeof(selectName), "*SELECT* %d",
1907  list_length(pstate->p_rtable) + 1);
1908  rte = addRangeTableEntryForSubquery(pstate,
1909  selectQuery,
1910  makeAlias(selectName, NIL),
1911  false,
1912  false);
1913 
1914  /*
1915  * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
1916  */
1917  rtr = makeNode(RangeTblRef);
1918  /* assume new rte is at end */
1919  rtr->rtindex = list_length(pstate->p_rtable);
1920  Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1921  return (Node *) rtr;
1922  }
1923  else
1924  {
1925  /* Process an internal node (set operation node) */
1927  List *ltargetlist;
1928  List *rtargetlist;
1929  ListCell *ltl;
1930  ListCell *rtl;
1931  const char *context;
1932 
1933  context = (stmt->op == SETOP_UNION ? "UNION" :
1934  (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
1935  "EXCEPT"));
1936 
1937  op->op = stmt->op;
1938  op->all = stmt->all;
1939 
1940  /*
1941  * Recursively transform the left child node.
1942  */
1943  op->larg = transformSetOperationTree(pstate, stmt->larg,
1944  false,
1945  &ltargetlist);
1946 
1947  /*
1948  * If we are processing a recursive union query, now is the time to
1949  * examine the non-recursive term's output columns and mark the
1950  * containing CTE as having those result columns. We should do this
1951  * only at the topmost setop of the CTE, of course.
1952  */
1953  if (isTopLevel &&
1954  pstate->p_parent_cte &&
1955  pstate->p_parent_cte->cterecursive)
1956  determineRecursiveColTypes(pstate, op->larg, ltargetlist);
1957 
1958  /*
1959  * Recursively transform the right child node.
1960  */
1961  op->rarg = transformSetOperationTree(pstate, stmt->rarg,
1962  false,
1963  &rtargetlist);
1964 
1965  /*
1966  * Verify that the two children have the same number of non-junk
1967  * columns, and determine the types of the merged output columns.
1968  */
1969  if (list_length(ltargetlist) != list_length(rtargetlist))
1970  ereport(ERROR,
1971  (errcode(ERRCODE_SYNTAX_ERROR),
1972  errmsg("each %s query must have the same number of columns",
1973  context),
1974  parser_errposition(pstate,
1975  exprLocation((Node *) rtargetlist))));
1976 
1977  if (targetlist)
1978  *targetlist = NIL;
1979  op->colTypes = NIL;
1980  op->colTypmods = NIL;
1981  op->colCollations = NIL;
1982  op->groupClauses = NIL;
1983  forboth(ltl, ltargetlist, rtl, rtargetlist)
1984  {
1985  TargetEntry *ltle = (TargetEntry *) lfirst(ltl);
1986  TargetEntry *rtle = (TargetEntry *) lfirst(rtl);
1987  Node *lcolnode = (Node *) ltle->expr;
1988  Node *rcolnode = (Node *) rtle->expr;
1989  Oid lcoltype = exprType(lcolnode);
1990  Oid rcoltype = exprType(rcolnode);
1991  int32 lcoltypmod = exprTypmod(lcolnode);
1992  int32 rcoltypmod = exprTypmod(rcolnode);
1993  Node *bestexpr;
1994  int bestlocation;
1995  Oid rescoltype;
1996  int32 rescoltypmod;
1997  Oid rescolcoll;
1998 
1999  /* select common type, same as CASE et al */
2000  rescoltype = select_common_type(pstate,
2001  list_make2(lcolnode, rcolnode),
2002  context,
2003  &bestexpr);
2004  bestlocation = exprLocation(bestexpr);
2005  /* if same type and same typmod, use typmod; else default */
2006  if (lcoltype == rcoltype && lcoltypmod == rcoltypmod)
2007  rescoltypmod = lcoltypmod;
2008  else
2009  rescoltypmod = -1;
2010 
2011  /*
2012  * Verify the coercions are actually possible. If not, we'd fail
2013  * later anyway, but we want to fail now while we have sufficient
2014  * context to produce an error cursor position.
2015  *
2016  * For all non-UNKNOWN-type cases, we verify coercibility but we
2017  * don't modify the child's expression, for fear of changing the
2018  * child query's semantics.
2019  *
2020  * If a child expression is an UNKNOWN-type Const or Param, we
2021  * want to replace it with the coerced expression. This can only
2022  * happen when the child is a leaf set-op node. It's safe to
2023  * replace the expression because if the child query's semantics
2024  * depended on the type of this output column, it'd have already
2025  * coerced the UNKNOWN to something else. We want to do this
2026  * because (a) we want to verify that a Const is valid for the
2027  * target type, or resolve the actual type of an UNKNOWN Param,
2028  * and (b) we want to avoid unnecessary discrepancies between the
2029  * output type of the child query and the resolved target type.
2030  * Such a discrepancy would disable optimization in the planner.
2031  *
2032  * If it's some other UNKNOWN-type node, eg a Var, we do nothing
2033  * (knowing that coerce_to_common_type would fail). The planner
2034  * is sometimes able to fold an UNKNOWN Var to a constant before
2035  * it has to coerce the type, so failing now would just break
2036  * cases that might work.
2037  */
2038  if (lcoltype != UNKNOWNOID)
2039  lcolnode = coerce_to_common_type(pstate, lcolnode,
2040  rescoltype, context);
2041  else if (IsA(lcolnode, Const) ||
2042  IsA(lcolnode, Param))
2043  {
2044  lcolnode = coerce_to_common_type(pstate, lcolnode,
2045  rescoltype, context);
2046  ltle->expr = (Expr *) lcolnode;
2047  }
2048 
2049  if (rcoltype != UNKNOWNOID)
2050  rcolnode = coerce_to_common_type(pstate, rcolnode,
2051  rescoltype, context);
2052  else if (IsA(rcolnode, Const) ||
2053  IsA(rcolnode, Param))
2054  {
2055  rcolnode = coerce_to_common_type(pstate, rcolnode,
2056  rescoltype, context);
2057  rtle->expr = (Expr *) rcolnode;
2058  }
2059 
2060  /*
2061  * Select common collation. A common collation is required for
2062  * all set operators except UNION ALL; see SQL:2008 7.13 <query
2063  * expression> Syntax Rule 15c. (If we fail to identify a common
2064  * collation for a UNION ALL column, the curCollations element
2065  * will be set to InvalidOid, which may result in a runtime error
2066  * if something at a higher query level wants to use the column's
2067  * collation.)
2068  */
2069  rescolcoll = select_common_collation(pstate,
2070  list_make2(lcolnode, rcolnode),
2071  (op->op == SETOP_UNION && op->all));
2072 
2073  /* emit results */
2074  op->colTypes = lappend_oid(op->colTypes, rescoltype);
2075  op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
2076  op->colCollations = lappend_oid(op->colCollations, rescolcoll);
2077 
2078  /*
2079  * For all cases except UNION ALL, identify the grouping operators
2080  * (and, if available, sorting operators) that will be used to
2081  * eliminate duplicates.
2082  */
2083  if (op->op != SETOP_UNION || !op->all)
2084  {
2086  Oid sortop;
2087  Oid eqop;
2088  bool hashable;
2089  ParseCallbackState pcbstate;
2090 
2091  setup_parser_errposition_callback(&pcbstate, pstate,
2092  bestlocation);
2093 
2094  /* determine the eqop and optional sortop */
2095  get_sort_group_operators(rescoltype,
2096  false, true, false,
2097  &sortop, &eqop, NULL,
2098  &hashable);
2099 
2101 
2102  /* we don't have a tlist yet, so can't assign sortgrouprefs */
2103  grpcl->tleSortGroupRef = 0;
2104  grpcl->eqop = eqop;
2105  grpcl->sortop = sortop;
2106  grpcl->nulls_first = false; /* OK with or without sortop */
2107  grpcl->hashable = hashable;
2108 
2109  op->groupClauses = lappend(op->groupClauses, grpcl);
2110  }
2111 
2112  /*
2113  * Construct a dummy tlist entry to return. We use a SetToDefault
2114  * node for the expression, since it carries exactly the fields
2115  * needed, but any other expression node type would do as well.
2116  */
2117  if (targetlist)
2118  {
2119  SetToDefault *rescolnode = makeNode(SetToDefault);
2120  TargetEntry *restle;
2121 
2122  rescolnode->typeId = rescoltype;
2123  rescolnode->typeMod = rescoltypmod;
2124  rescolnode->collation = rescolcoll;
2125  rescolnode->location = bestlocation;
2126  restle = makeTargetEntry((Expr *) rescolnode,
2127  0, /* no need to set resno */
2128  NULL,
2129  false);
2130  *targetlist = lappend(*targetlist, restle);
2131  }
2132  }
2133 
2134  return (Node *) op;
2135  }
2136 }
2137 
2138 /*
2139  * Process the outputs of the non-recursive term of a recursive union
2140  * to set up the parent CTE's columns
2141  */
2142 static void
2143 determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
2144 {
2145  Node *node;
2146  int leftmostRTI;
2147  Query *leftmostQuery;
2148  List *targetList;
2149  ListCell *left_tlist;
2150  ListCell *nrtl;
2151  int next_resno;
2152 
2153  /*
2154  * Find leftmost leaf SELECT
2155  */
2156  node = larg;
2157  while (node && IsA(node, SetOperationStmt))
2158  node = ((SetOperationStmt *) node)->larg;
2159  Assert(node && IsA(node, RangeTblRef));
2160  leftmostRTI = ((RangeTblRef *) node)->rtindex;
2161  leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2162  Assert(leftmostQuery != NULL);
2163 
2164  /*
2165  * Generate dummy targetlist using column names of leftmost select and
2166  * dummy result expressions of the non-recursive term.
2167  */
2168  targetList = NIL;
2169  left_tlist = list_head(leftmostQuery->targetList);
2170  next_resno = 1;
2171 
2172  foreach(nrtl, nrtargetlist)
2173  {
2174  TargetEntry *nrtle = (TargetEntry *) lfirst(nrtl);
2175  TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
2176  char *colName;
2177  TargetEntry *tle;
2178 
2179  Assert(!lefttle->resjunk);
2180  colName = pstrdup(lefttle->resname);
2181  tle = makeTargetEntry(nrtle->expr,
2182  next_resno++,
2183  colName,
2184  false);
2185  targetList = lappend(targetList, tle);
2186  left_tlist = lnext(left_tlist);
2187  }
2188 
2189  /* Now build CTE's output column info using dummy targetlist */
2190  analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
2191 }
2192 
2193 
2194 /*
2195  * transformUpdateStmt -
2196  * transforms an update statement
2197  */
2198 static Query *
2200 {
2201  Query *qry = makeNode(Query);
2202  ParseNamespaceItem *nsitem;
2203  Node *qual;
2204 
2205  qry->commandType = CMD_UPDATE;
2206  pstate->p_is_insert = false;
2207 
2208  /* process the WITH clause independently of all else */
2209  if (stmt->withClause)
2210  {
2211  qry->hasRecursive = stmt->withClause->recursive;
2212  qry->cteList = transformWithClause(pstate, stmt->withClause);
2213  qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2214  }
2215 
2216  qry->resultRelation = setTargetTable(pstate, stmt->relation,
2217  stmt->relation->inh,
2218  true,
2219  ACL_UPDATE);
2220 
2221  /* grab the namespace item made by setTargetTable */
2222  nsitem = (ParseNamespaceItem *) llast(pstate->p_namespace);
2223 
2224  /* subqueries in FROM cannot access the result relation */
2225  nsitem->p_lateral_only = true;
2226  nsitem->p_lateral_ok = false;
2227 
2228  /*
2229  * the FROM clause is non-standard SQL syntax. We used to be able to do
2230  * this with REPLACE in POSTQUEL so we keep the feature.
2231  */
2232  transformFromClause(pstate, stmt->fromClause);
2233 
2234  /* remaining clauses can reference the result relation normally */
2235  nsitem->p_lateral_only = false;
2236  nsitem->p_lateral_ok = true;
2237 
2238  qual = transformWhereClause(pstate, stmt->whereClause,
2239  EXPR_KIND_WHERE, "WHERE");
2240 
2241  qry->returningList = transformReturningList(pstate, stmt->returningList);
2242 
2243  /*
2244  * Now we are done with SELECT-like processing, and can get on with
2245  * transforming the target list to match the UPDATE target columns.
2246  */
2247  qry->targetList = transformUpdateTargetList(pstate, stmt->targetList);
2248 
2249  qry->rtable = pstate->p_rtable;
2250  qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
2251 
2252  qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2253  qry->hasSubLinks = pstate->p_hasSubLinks;
2254 
2255  assign_query_collations(pstate, qry);
2256 
2257  return qry;
2258 }
2259 
2260 /*
2261  * transformUpdateTargetList -
2262  * handle SET clause in UPDATE/INSERT ... ON CONFLICT UPDATE
2263  */
2264 static List *
2266 {
2267  List *tlist = NIL;
2268  RangeTblEntry *target_rte;
2269  ListCell *orig_tl;
2270  ListCell *tl;
2271 
2272  tlist = transformTargetList(pstate, origTlist,
2274 
2275  /* Prepare to assign non-conflicting resnos to resjunk attributes */
2276  if (pstate->p_next_resno <= pstate->p_target_relation->rd_rel->relnatts)
2277  pstate->p_next_resno = pstate->p_target_relation->rd_rel->relnatts + 1;
2278 
2279  /* Prepare non-junk columns for assignment to target table */
2280  target_rte = pstate->p_target_rangetblentry;
2281  orig_tl = list_head(origTlist);
2282 
2283  foreach(tl, tlist)
2284  {
2285  TargetEntry *tle = (TargetEntry *) lfirst(tl);
2286  ResTarget *origTarget;
2287  int attrno;
2288 
2289  if (tle->resjunk)
2290  {
2291  /*
2292  * Resjunk nodes need no additional processing, but be sure they
2293  * have resnos that do not match any target columns; else rewriter
2294  * or planner might get confused. They don't need a resname
2295  * either.
2296  */
2297  tle->resno = (AttrNumber) pstate->p_next_resno++;
2298  tle->resname = NULL;
2299  continue;
2300  }
2301  if (orig_tl == NULL)
2302  elog(ERROR, "UPDATE target count mismatch --- internal error");
2303  origTarget = lfirst_node(ResTarget, orig_tl);
2304 
2305  attrno = attnameAttNum(pstate->p_target_relation,
2306  origTarget->name, true);
2307  if (attrno == InvalidAttrNumber)
2308  ereport(ERROR,
2309  (errcode(ERRCODE_UNDEFINED_COLUMN),
2310  errmsg("column \"%s\" of relation \"%s\" does not exist",
2311  origTarget->name,
2313  parser_errposition(pstate, origTarget->location)));
2314 
2315  updateTargetListEntry(pstate, tle, origTarget->name,
2316  attrno,
2317  origTarget->indirection,
2318  origTarget->location);
2319 
2320  /* Mark the target column as requiring update permissions */
2321  target_rte->updatedCols = bms_add_member(target_rte->updatedCols,
2323 
2324  orig_tl = lnext(orig_tl);
2325  }
2326  if (orig_tl != NULL)
2327  elog(ERROR, "UPDATE target count mismatch --- internal error");
2328 
2329  return tlist;
2330 }
2331 
2332 /*
2333  * transformReturningList -
2334  * handle a RETURNING clause in INSERT/UPDATE/DELETE
2335  */
2336 static List *
2337 transformReturningList(ParseState *pstate, List *returningList)
2338 {
2339  List *rlist;
2340  int save_next_resno;
2341 
2342  if (returningList == NIL)
2343  return NIL; /* nothing to do */
2344 
2345  /*
2346  * We need to assign resnos starting at one in the RETURNING list. Save
2347  * and restore the main tlist's value of p_next_resno, just in case
2348  * someone looks at it later (probably won't happen).
2349  */
2350  save_next_resno = pstate->p_next_resno;
2351  pstate->p_next_resno = 1;
2352 
2353  /* transform RETURNING identically to a SELECT targetlist */
2354  rlist = transformTargetList(pstate, returningList, EXPR_KIND_RETURNING);
2355 
2356  /*
2357  * Complain if the nonempty tlist expanded to nothing (which is possible
2358  * if it contains only a star-expansion of a zero-column table). If we
2359  * allow this, the parsed Query will look like it didn't have RETURNING,
2360  * with results that would probably surprise the user.
2361  */
2362  if (rlist == NIL)
2363  ereport(ERROR,
2364  (errcode(ERRCODE_SYNTAX_ERROR),
2365  errmsg("RETURNING must have at least one column"),
2366  parser_errposition(pstate,
2367  exprLocation(linitial(returningList)))));
2368 
2369  /* mark column origins */
2370  markTargetListOrigins(pstate, rlist);
2371 
2372  /* resolve any still-unresolved output columns as being type text */
2373  if (pstate->p_resolve_unknowns)
2374  resolveTargetListUnknowns(pstate, rlist);
2375 
2376  /* restore state */
2377  pstate->p_next_resno = save_next_resno;
2378 
2379  return rlist;
2380 }
2381 
2382 
2383 /*
2384  * transformDeclareCursorStmt -
2385  * transform a DECLARE CURSOR Statement
2386  *
2387  * DECLARE CURSOR is like other utility statements in that we emit it as a
2388  * CMD_UTILITY Query node; however, we must first transform the contained
2389  * query. We used to postpone that until execution, but it's really necessary
2390  * to do it during the normal parse analysis phase to ensure that side effects
2391  * of parser hooks happen at the expected time.
2392  */
2393 static Query *
2395 {
2396  Query *result;
2397  Query *query;
2398 
2399  /*
2400  * Don't allow both SCROLL and NO SCROLL to be specified
2401  */
2402  if ((stmt->options & CURSOR_OPT_SCROLL) &&
2403  (stmt->options & CURSOR_OPT_NO_SCROLL))
2404  ereport(ERROR,
2405  (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
2406  errmsg("cannot specify both SCROLL and NO SCROLL")));
2407 
2408  /* Transform contained query, not allowing SELECT INTO */
2409  query = transformStmt(pstate, stmt->query);
2410  stmt->query = (Node *) query;
2411 
2412  /* Grammar should not have allowed anything but SELECT */
2413  if (!IsA(query, Query) ||
2414  query->commandType != CMD_SELECT)
2415  elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
2416 
2417  /*
2418  * We also disallow data-modifying WITH in a cursor. (This could be
2419  * allowed, but the semantics of when the updates occur might be
2420  * surprising.)
2421  */
2422  if (query->hasModifyingCTE)
2423  ereport(ERROR,
2424  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2425  errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
2426 
2427  /* FOR UPDATE and WITH HOLD are not compatible */
2428  if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
2429  ereport(ERROR,
2430  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2431  /*------
2432  translator: %s is a SQL row locking clause such as FOR UPDATE */
2433  errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
2435  linitial(query->rowMarks))->strength)),
2436  errdetail("Holdable cursors must be READ ONLY.")));
2437 
2438  /* FOR UPDATE and SCROLL are not compatible */
2439  if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
2440  ereport(ERROR,
2441  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2442  /*------
2443  translator: %s is a SQL row locking clause such as FOR UPDATE */
2444  errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
2446  linitial(query->rowMarks))->strength)),
2447  errdetail("Scrollable cursors must be READ ONLY.")));
2448 
2449  /* FOR UPDATE and INSENSITIVE are not compatible */
2450  if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
2451  ereport(ERROR,
2452  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2453  /*------
2454  translator: %s is a SQL row locking clause such as FOR UPDATE */
2455  errmsg("DECLARE INSENSITIVE CURSOR ... %s is not supported",
2457  linitial(query->rowMarks))->strength)),
2458  errdetail("Insensitive cursors must be READ ONLY.")));
2459 
2460  /* represent the command as a utility Query */
2461  result = makeNode(Query);
2462  result->commandType = CMD_UTILITY;
2463  result->utilityStmt = (Node *) stmt;
2464 
2465  return result;
2466 }
2467 
2468 
2469 /*
2470  * transformExplainStmt -
2471  * transform an EXPLAIN Statement
2472  *
2473  * EXPLAIN is like other utility statements in that we emit it as a
2474  * CMD_UTILITY Query node; however, we must first transform the contained
2475  * query. We used to postpone that until execution, but it's really necessary
2476  * to do it during the normal parse analysis phase to ensure that side effects
2477  * of parser hooks happen at the expected time.
2478  */
2479 static Query *
2481 {
2482  Query *result;
2483 
2484  /* transform contained query, allowing SELECT INTO */
2485  stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
2486 
2487  /* represent the command as a utility Query */
2488  result = makeNode(Query);
2489  result->commandType = CMD_UTILITY;
2490  result->utilityStmt = (Node *) stmt;
2491 
2492  return result;
2493 }
2494 
2495 
2496 /*
2497  * transformCreateTableAsStmt -
2498  * transform a CREATE TABLE AS, SELECT ... INTO, or CREATE MATERIALIZED VIEW
2499  * Statement
2500  *
2501  * As with DECLARE CURSOR and EXPLAIN, transform the contained statement now.
2502  */
2503 static Query *
2505 {
2506  Query *result;
2507  Query *query;
2508 
2509  /* transform contained query, not allowing SELECT INTO */
2510  query = transformStmt(pstate, stmt->query);
2511  stmt->query = (Node *) query;
2512 
2513  /* additional work needed for CREATE MATERIALIZED VIEW */
2514  if (stmt->relkind == OBJECT_MATVIEW)
2515  {
2516  /*
2517  * Prohibit a data-modifying CTE in the query used to create a
2518  * materialized view. It's not sufficiently clear what the user would
2519  * want to happen if the MV is refreshed or incrementally maintained.
2520  */
2521  if (query->hasModifyingCTE)
2522  ereport(ERROR,
2523  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2524  errmsg("materialized views must not use data-modifying statements in WITH")));
2525 
2526  /*
2527  * Check whether any temporary database objects are used in the
2528  * creation query. It would be hard to refresh data or incrementally
2529  * maintain it if a source disappeared.
2530  */
2531  if (isQueryUsingTempRelation(query))
2532  ereport(ERROR,
2533  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2534  errmsg("materialized views must not use temporary tables or views")));
2535 
2536  /*
2537  * A materialized view would either need to save parameters for use in
2538  * maintaining/loading the data or prohibit them entirely. The latter
2539  * seems safer and more sane.
2540  */
2541  if (query_contains_extern_params(query))
2542  ereport(ERROR,
2543  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2544  errmsg("materialized views may not be defined using bound parameters")));
2545 
2546  /*
2547  * For now, we disallow unlogged materialized views, because it seems
2548  * like a bad idea for them to just go to empty after a crash. (If we
2549  * could mark them as unpopulated, that would be better, but that
2550  * requires catalog changes which crash recovery can't presently
2551  * handle.)
2552  */
2554  ereport(ERROR,
2555  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2556  errmsg("materialized views cannot be UNLOGGED")));
2557 
2558  /*
2559  * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
2560  * for purposes of creating the view's ON SELECT rule. We stash that
2561  * in the IntoClause because that's where intorel_startup() can
2562  * conveniently get it from.
2563  */
2564  stmt->into->viewQuery = (Node *) copyObject(query);
2565  }
2566 
2567  /* represent the command as a utility Query */
2568  result = makeNode(Query);
2569  result->commandType = CMD_UTILITY;
2570  result->utilityStmt = (Node *) stmt;
2571 
2572  return result;
2573 }
2574 
2575 
2576 /*
2577  * Produce a string representation of a LockClauseStrength value.
2578  * This should only be applied to valid values (not LCS_NONE).
2579  */
2580 const char *
2582 {
2583  switch (strength)
2584  {
2585  case LCS_NONE:
2586  Assert(false);
2587  break;
2588  case LCS_FORKEYSHARE:
2589  return "FOR KEY SHARE";
2590  case LCS_FORSHARE:
2591  return "FOR SHARE";
2592  case LCS_FORNOKEYUPDATE:
2593  return "FOR NO KEY UPDATE";
2594  case LCS_FORUPDATE:
2595  return "FOR UPDATE";
2596  }
2597  return "FOR some"; /* shouldn't happen */
2598 }
2599 
2600 /*
2601  * Check for features that are not supported with FOR [KEY] UPDATE/SHARE.
2602  *
2603  * exported so planner can check again after rewriting, query pullup, etc
2604  */
2605 void
2607 {
2608  Assert(strength != LCS_NONE); /* else caller error */
2609 
2610  if (qry->setOperations)
2611  ereport(ERROR,
2612  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2613  /*------
2614  translator: %s is a SQL row locking clause such as FOR UPDATE */
2615  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2616  LCS_asString(strength))));
2617  if (qry->distinctClause != NIL)
2618  ereport(ERROR,
2619  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2620  /*------
2621  translator: %s is a SQL row locking clause such as FOR UPDATE */
2622  errmsg("%s is not allowed with DISTINCT clause",
2623  LCS_asString(strength))));
2624  if (qry->groupClause != NIL)
2625  ereport(ERROR,
2626  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2627  /*------
2628  translator: %s is a SQL row locking clause such as FOR UPDATE */
2629  errmsg("%s is not allowed with GROUP BY clause",
2630  LCS_asString(strength))));
2631  if (qry->havingQual != NULL)
2632  ereport(ERROR,
2633  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2634  /*------
2635  translator: %s is a SQL row locking clause such as FOR UPDATE */
2636  errmsg("%s is not allowed with HAVING clause",
2637  LCS_asString(strength))));
2638  if (qry->hasAggs)
2639  ereport(ERROR,
2640  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2641  /*------
2642  translator: %s is a SQL row locking clause such as FOR UPDATE */
2643  errmsg("%s is not allowed with aggregate functions",
2644  LCS_asString(strength))));
2645  if (qry->hasWindowFuncs)
2646  ereport(ERROR,
2647  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2648  /*------
2649  translator: %s is a SQL row locking clause such as FOR UPDATE */
2650  errmsg("%s is not allowed with window functions",
2651  LCS_asString(strength))));
2652  if (qry->hasTargetSRFs)
2653  ereport(ERROR,
2654  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2655  /*------
2656  translator: %s is a SQL row locking clause such as FOR UPDATE */
2657  errmsg("%s is not allowed with set-returning functions in the target list",
2658  LCS_asString(strength))));
2659 }
2660 
2661 /*
2662  * Transform a FOR [KEY] UPDATE/SHARE clause
2663  *
2664  * This basically involves replacing names by integer relids.
2665  *
2666  * NB: if you need to change this, see also markQueryForLocking()
2667  * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
2668  */
2669 static void
2671  bool pushedDown)
2672 {
2673  List *lockedRels = lc->lockedRels;
2674  ListCell *l;
2675  ListCell *rt;
2676  Index i;
2677  LockingClause *allrels;
2678 
2679  CheckSelectLocking(qry, lc->strength);
2680 
2681  /* make a clause we can pass down to subqueries to select all rels */
2682  allrels = makeNode(LockingClause);
2683  allrels->lockedRels = NIL; /* indicates all rels */
2684  allrels->strength = lc->strength;
2685  allrels->waitPolicy = lc->waitPolicy;
2686 
2687  if (lockedRels == NIL)
2688  {
2689  /* all regular tables used in query */
2690  i = 0;
2691  foreach(rt, qry->rtable)
2692  {
2693  RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2694 
2695  ++i;
2696  switch (rte->rtekind)
2697  {
2698  case RTE_RELATION:
2699  applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
2700  pushedDown);
2702  break;
2703  case RTE_SUBQUERY:
2704  applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
2705  pushedDown);
2706 
2707  /*
2708  * FOR UPDATE/SHARE of subquery is propagated to all of
2709  * subquery's rels, too. We could do this later (based on
2710  * the marking of the subquery RTE) but it is convenient
2711  * to have local knowledge in each query level about which
2712  * rels need to be opened with RowShareLock.
2713  */
2714  transformLockingClause(pstate, rte->subquery,
2715  allrels, true);
2716  break;
2717  default:
2718  /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
2719  break;
2720  }
2721  }
2722  }
2723  else
2724  {
2725  /* just the named tables */
2726  foreach(l, lockedRels)
2727  {
2728  RangeVar *thisrel = (RangeVar *) lfirst(l);
2729 
2730  /* For simplicity we insist on unqualified alias names here */
2731  if (thisrel->catalogname || thisrel->schemaname)
2732  ereport(ERROR,
2733  (errcode(ERRCODE_SYNTAX_ERROR),
2734  /*------
2735  translator: %s is a SQL row locking clause such as FOR UPDATE */
2736  errmsg("%s must specify unqualified relation names",
2737  LCS_asString(lc->strength)),
2738  parser_errposition(pstate, thisrel->location)));
2739 
2740  i = 0;
2741  foreach(rt, qry->rtable)
2742  {
2743  RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2744 
2745  ++i;
2746  if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
2747  {
2748  switch (rte->rtekind)
2749  {
2750  case RTE_RELATION:
2751  applyLockingClause(qry, i, lc->strength,
2752  lc->waitPolicy, pushedDown);
2754  break;
2755  case RTE_SUBQUERY:
2756  applyLockingClause(qry, i, lc->strength,
2757  lc->waitPolicy, pushedDown);
2758  /* see comment above */
2759  transformLockingClause(pstate, rte->subquery,
2760  allrels, true);
2761  break;
2762  case RTE_JOIN:
2763  ereport(ERROR,
2764  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2765  /*------
2766  translator: %s is a SQL row locking clause such as FOR UPDATE */
2767  errmsg("%s cannot be applied to a join",
2768  LCS_asString(lc->strength)),
2769  parser_errposition(pstate, thisrel->location)));
2770  break;
2771  case RTE_FUNCTION:
2772  ereport(ERROR,
2773  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2774  /*------
2775  translator: %s is a SQL row locking clause such as FOR UPDATE */
2776  errmsg("%s cannot be applied to a function",
2777  LCS_asString(lc->strength)),
2778  parser_errposition(pstate, thisrel->location)));
2779  break;
2780  case RTE_TABLEFUNC:
2781  ereport(ERROR,
2782  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2783  /*------
2784  translator: %s is a SQL row locking clause such as FOR UPDATE */
2785  errmsg("%s cannot be applied to a table function",
2786  LCS_asString(lc->strength)),
2787  parser_errposition(pstate, thisrel->location)));
2788  break;
2789  case RTE_VALUES:
2790  ereport(ERROR,
2791  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2792  /*------
2793  translator: %s is a SQL row locking clause such as FOR UPDATE */
2794  errmsg("%s cannot be applied to VALUES",
2795  LCS_asString(lc->strength)),
2796  parser_errposition(pstate, thisrel->location)));
2797  break;
2798  case RTE_CTE:
2799  ereport(ERROR,
2800  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2801  /*------
2802  translator: %s is a SQL row locking clause such as FOR UPDATE */
2803  errmsg("%s cannot be applied to a WITH query",
2804  LCS_asString(lc->strength)),
2805  parser_errposition(pstate, thisrel->location)));
2806  break;
2807  case RTE_NAMEDTUPLESTORE:
2808  ereport(ERROR,
2809  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2810  /*------
2811  translator: %s is a SQL row locking clause such as FOR UPDATE */
2812  errmsg("%s cannot be applied to a named tuplestore",
2813  LCS_asString(lc->strength)),
2814  parser_errposition(pstate, thisrel->location)));
2815  break;
2816  default:
2817  elog(ERROR, "unrecognized RTE type: %d",
2818  (int) rte->rtekind);
2819  break;
2820  }
2821  break; /* out of foreach loop */
2822  }
2823  }
2824  if (rt == NULL)
2825  ereport(ERROR,
2827  /*------
2828  translator: %s is a SQL row locking clause such as FOR UPDATE */
2829  errmsg("relation \"%s\" in %s clause not found in FROM clause",
2830  thisrel->relname,
2831  LCS_asString(lc->strength)),
2832  parser_errposition(pstate, thisrel->location)));
2833  }
2834  }
2835 }
2836 
2837 /*
2838  * Record locking info for a single rangetable item
2839  */
2840 void
2842  LockClauseStrength strength, LockWaitPolicy waitPolicy,
2843  bool pushedDown)
2844 {
2845  RowMarkClause *rc;
2846 
2847  Assert(strength != LCS_NONE); /* else caller error */
2848 
2849  /* If it's an explicit clause, make sure hasForUpdate gets set */
2850  if (!pushedDown)
2851  qry->hasForUpdate = true;
2852 
2853  /* Check for pre-existing entry for same rtindex */
2854  if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
2855  {
2856  /*
2857  * If the same RTE is specified with more than one locking strength,
2858  * use the strongest. (Reasonable, since you can't take both a shared
2859  * and exclusive lock at the same time; it'll end up being exclusive
2860  * anyway.)
2861  *
2862  * Similarly, if the same RTE is specified with more than one lock
2863  * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
2864  * turn wins over waiting for the lock (the default). This is a bit
2865  * more debatable but raising an error doesn't seem helpful. (Consider
2866  * for instance SELECT FOR UPDATE NOWAIT from a view that internally
2867  * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
2868  * LOCKED is reasonable since the former throws an error in case of
2869  * coming across a locked tuple, which may be undesirable in some
2870  * cases but it seems better than silently returning inconsistent
2871  * results.
2872  *
2873  * And of course pushedDown becomes false if any clause is explicit.
2874  */
2875  rc->strength = Max(rc->strength, strength);
2876  rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
2877  rc->pushedDown &= pushedDown;
2878  return;
2879  }
2880 
2881  /* Make a new RowMarkClause */
2882  rc = makeNode(RowMarkClause);
2883  rc->rti = rtindex;
2884  rc->strength = strength;
2885  rc->waitPolicy = waitPolicy;
2886  rc->pushedDown = pushedDown;
2887  qry->rowMarks = lappend(qry->rowMarks, rc);
2888 }
2889 
2890 /*
2891  * Coverage testing for raw_expression_tree_walker().
2892  *
2893  * When enabled, we run raw_expression_tree_walker() over every DML statement
2894  * submitted to parse analysis. Without this provision, that function is only
2895  * applied in limited cases involving CTEs, and we don't really want to have
2896  * to test everything inside as well as outside a CTE.
2897  */
2898 #ifdef RAW_EXPRESSION_COVERAGE_TEST
2899 
2900 static bool
2901 test_raw_expression_coverage(Node *node, void *context)
2902 {
2903  if (node == NULL)
2904  return false;
2905  return raw_expression_tree_walker(node,
2906  test_raw_expression_coverage,
2907  context);
2908 }
2909 
2910 #endif /* RAW_EXPRESSION_COVERAGE_TEST */
#define list_make2(x1, x2)
Definition: pg_list.h:140
Value * makeString(char *str)
Definition: value.c:53
List * lockedRels
Definition: parsenodes.h:737
List * indirection
Definition: parsenodes.h:440
Node * limitOffset
Definition: parsenodes.h:158
#define NIL
Definition: pg_list.h:69
struct SelectStmt * larg
Definition: parsenodes.h:1532
Expr * refassgnexpr
Definition: primnodes.h:410
List * fromClause
Definition: parsenodes.h:1464
static void transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc, bool pushedDown)
Definition: analyze.c:2670
bool p_hasSubLinks
Definition: parse_node.h:201
static List * transformUpdateTargetList(ParseState *pstate, List *targetList)
Definition: analyze.c:2265
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition: makefuncs.c:282
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
static Query * transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
Definition: analyze.c:2504
const char * LCS_asString(LockClauseStrength strength)
Definition: analyze.c:2581
Index varlevelsup
Definition: primnodes.h:173
int errhint(const char *fmt,...)
Definition: elog.c:987
static Query * transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
Definition: analyze.c:2394
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
int stmt_location
Definition: parsenodes.h:179
bool analyze_requires_snapshot(RawStmt *parseTree)
Definition: analyze.c:348
List * sortClause
Definition: parsenodes.h:156
void(* post_parse_analyze_hook_type)(ParseState *pstate, Query *query)
Definition: analyze.h:20
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
bool isQueryUsingTempRelation(Query *query)
List * transformDistinctOnClause(ParseState *pstate, List *distinctlist, List **targetlist, List *sortClause)
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
IntoClause * intoClause
Definition: parsenodes.h:1499
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
#define RELPERSISTENCE_UNLOGGED
Definition: pg_class.h:171
List * fromClause
Definition: parsenodes.h:1501
RangeTblEntry * addRangeTableEntryForJoin(ParseState *pstate, List *colnames, JoinType jointype, List *aliasvars, Alias *alias, bool inFromCl)
RangeVar * relation
Definition: parsenodes.h:1447
FromExpr * jointree
Definition: parsenodes.h:136
char * name
Definition: parsenodes.h:439
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
QueryEnvironment * p_queryEnv
Definition: parse_node.h:195
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
void analyzeCTETargetList(ParseState *pstate, CommonTableExpr *cte, List *tlist)
Definition: parse_cte.c:352
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
Node * limitOffset
Definition: parsenodes.h:1522
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:203
char * pstrdup(const char *in)
Definition: mcxt.c:1077
bool hasAggs
Definition: parsenodes.h:123
#define CURSOR_OPT_HOLD
Definition: parsenodes.h:2618
int resultRelation
Definition: parsenodes.h:120
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define llast(l)
Definition: pg_list.h:131
Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle)
Definition: makefuncs.c:104
static Query * transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
Definition: analyze.c:2199
List * list_truncate(List *list, int new_size)
Definition: list.c:350
Index tleSortGroupRef
Definition: parsenodes.h:1176
bool p_hasTargetSRFs
Definition: parse_node.h:200
#define INT4OID
Definition: pg_type.h:316
List * groupingSets
Definition: parsenodes.h:148
void parse_variable_parameters(ParseState *pstate, Oid **paramTypes, int *numParams)
Definition: parse_param.c:83
static Query * transformValuesClause(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1316
Definition: nodes.h:509
static int count_rowexpr_columns(ParseState *pstate, Node *expr)
Definition: analyze.c:1138
int errcode(int sqlerrcode)
Definition: elog.c:575
void transformOnConflictArbiter(ParseState *pstate, OnConflictClause *onConflictClause, List **arbiterExpr, Node **arbiterWhere, Oid *constraint)
Query * parse_analyze_varparams(RawStmt *parseTree, const char *sourceText, Oid **paramTypes, int *numParams)
Definition: analyze.c:130
#define ACL_DELETE
Definition: parsenodes.h:75
AttrNumber varattno
Definition: primnodes.h:168
bool p_hasAggs
Definition: parse_node.h:198
#define CURSOR_OPT_INSENSITIVE
Definition: parsenodes.h:2617
return result
Definition: formatting.c:1633
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
List * targetList
Definition: parsenodes.h:1462
static List * transformReturningList(ParseState *pstate, List *returningList)
Definition: analyze.c:2337
AclMode requiredPerms
Definition: parsenodes.h:1039
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
Expr * transformAssignedExpr(ParseState *pstate, Expr *expr, ParseExprKind exprKind, char *colname, int attrno, List *indirection, int location)
Definition: parse_target.c:455
List * rowMarks
Definition: parsenodes.h:161
char * resname
Definition: primnodes.h:1370
Node * utilityStmt
Definition: parsenodes.h:118
Definition: primnodes.h:163
Node * transformLimitClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
CommonTableExpr * p_parent_cte
Definition: parse_node.h:181
void parseCheckAggregates(ParseState *pstate, Query *qry)
Definition: parse_agg.c:985
WithClause * withClause
Definition: parsenodes.h:1436
#define CURSOR_OPT_NO_SCROLL
Definition: parsenodes.h:2616
#define RELKIND_COMPOSITE_TYPE
Definition: pg_class.h:166
LockClauseStrength strength
Definition: parsenodes.h:1294
bool hasDistinctOn
Definition: parsenodes.h:127
Node * whereClause
Definition: parsenodes.h:1463
signed int int32
Definition: c.h:256
OnConflictClause * onConflictClause
Definition: parsenodes.h:1434
List * windowClause
Definition: parsenodes.h:152
List * targetList
Definition: parsenodes.h:138
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
bool p_hasWindowFuncs
Definition: parse_node.h:199
bool p_locked_from_parent
Definition: parse_node.h:190
List * arbiterElems
Definition: primnodes.h:1490
bool hasRecursive
Definition: parsenodes.h:128
char * schemaname
Definition: primnodes.h:67
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
int location
Definition: parsenodes.h:442
#define list_make1(x1)
Definition: pg_list.h:139
int location
Definition: primnodes.h:73
List * transformTargetList(ParseState *pstate, List *targetlist, ParseExprKind exprKind)
Definition: parse_target.c:131
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
WithClause * withClause
Definition: parsenodes.h:1451
char * relname
Definition: primnodes.h:68
Node * query
Definition: parsenodes.h:3104
uint32 AclMode
Definition: parsenodes.h:70
int locate_var_of_level(Node *node, int levelsup)
Definition: var.c:437
List * distinctClause
Definition: parsenodes.h:1497
List * returningList
Definition: parsenodes.h:1450
void CheckSelectLocking(Query *qry, LockClauseStrength strength)
Definition: analyze.c:2606
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
bool resjunk
Definition: primnodes.h:1375
#define linitial(l)
Definition: pg_list.h:111
List * rtable
Definition: parsenodes.h:135
List * distinctClause
Definition: parsenodes.h:154
#define ERROR
Definition: elog.h:43
Node * viewQuery
Definition: primnodes.h:114
bool raw_expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3232
#define lfirst_int(lc)
Definition: pg_list.h:107
Node * selectStmt
Definition: parsenodes.h:1433
Oid vartype
Definition: primnodes.h:170
List * p_windowdefs
Definition: parse_node.h:185
bool p_resolve_unknowns
Definition: parse_node.h:192
List * cols
Definition: parsenodes.h:1432
#define lfirst_node(type, lc)
Definition: pg_list.h:109
int location
Definition: primnodes.h:178
void assign_list_collations(ParseState *pstate, List *exprs)
Node * limitCount
Definition: parsenodes.h:159
List * p_namespace
Definition: parse_node.h:176
void * list_nth(const List *list, int n)
Definition: list.c:410
Node * stmt
Definition: parsenodes.h:1411
static Query * transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
Definition: analyze.c:2480
int32 typeMod
Definition: primnodes.h:1257
List * sortClause
Definition: parsenodes.h:1521
List * exclRelTlist
Definition: primnodes.h:1499
void check_stack_depth(void)
Definition: postgres.c:3117
List * targetList
Definition: parsenodes.h:1500
int errdetail(const char *fmt,...)
Definition: elog.c:873
LockClauseStrength strength
Definition: parsenodes.h:738
AttrNumber resno
Definition: primnodes.h:1369
Oid select_common_collation(ParseState *pstate, List *exprs, bool none_ok)
int p_next_resno
Definition: parse_node.h:187
List * transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
#define RelationGetRelationName(relation)
Definition: rel.h:436
static Query * transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1180
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
List * p_locking_clause
Definition: parse_node.h:189
#define RECORDOID
Definition: pg_type.h:680
bool recursive
Definition: parsenodes.h:1310
List * valuesLists
Definition: parsenodes.h:1515
const char * p_sourcetext
Definition: parse_node.h:171
List * expandRelAttrs(ParseState *pstate, RangeTblEntry *rte, int rtindex, int sublevels_up, int location)
List * returningList
Definition: parsenodes.h:144
ObjectType relkind
Definition: parsenodes.h:3126
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
static Node * transformSetOperationTree(ParseState *pstate, SelectStmt *stmt, bool isTopLevel, List **targetlist)
Definition: analyze.c:1795
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
List * lockingClause
Definition: parsenodes.h:1524
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
bool p_hasModifyingCTE
Definition: parse_node.h:202
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * newvals
Definition: primnodes.h:769
Query * transformStmt(ParseState *pstate, Node *parseTree)
Definition: analyze.c:250
bool inh
Definition: primnodes.h:69
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
List * lappend(List *list, void *datum)
Definition: list.c:128
OnConflictAction action
Definition: primnodes.h:1487
RangeTblEntry * addRangeTableEntryForSubquery(ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
Index varno
Definition: primnodes.h:166
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
OverridingKind override
Definition: parsenodes.h:1437
#define ACL_UPDATE
Definition: parsenodes.h:74
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
List * transformWindowDefinitions(ParseState *pstate, List *windowdefs, List **targetlist)
LockClauseStrength
Definition: lockoptions.h:21
List * colCollations
Definition: parsenodes.h:1569
void * palloc0(Size size)
Definition: mcxt.c:878
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
Definition: parse_target.c:228
OverridingKind override
Definition: parsenodes.h:140
void resolveTargetListUnknowns(ParseState *pstate, List *targetlist)
Definition: parse_target.c:299
RangeVar * relation
Definition: parsenodes.h:1431
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:183
int stmt_len
Definition: parsenodes.h:1413
void transformFromClause(ParseState *pstate, List *frmList)
Definition: parse_clause.c:115
int stmt_location
Definition: parsenodes.h:1412
unsigned int Index
Definition: c.h:365
Node * whereClause
Definition: parsenodes.h:1449
List * usingClause
Definition: parsenodes.h:1448
TupleDesc rd_att
Definition: rel.h:115
static Query * transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
Definition: analyze.c:458
List * windowClause
Definition: parsenodes.h:1505
SetOperation op
Definition: parsenodes.h:1530
#define InvalidOid
Definition: postgres_ext.h:36
int setTargetTable(ParseState *pstate, RangeVar *relation, bool inh, bool alsoSource, AclMode requiredPerms)
Definition: parse_clause.c:181
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition: analyze.c:160
Bitmapset * updatedCols
Definition: parsenodes.h:1043
Query * parse_analyze(RawStmt *parseTree, const char *sourceText, Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
Definition: analyze.c:96
IntoClause * into
Definition: parsenodes.h:3125
CmdType commandType
Definition: parsenodes.h:110
bool hasTargetSRFs
Definition: parsenodes.h:125
#define Max(x, y)
Definition: c.h:800
#define makeNode(_type_)
Definition: nodes.h:557
RangeTblEntry * addRangeTableEntryForValues(ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
#define NULL
Definition: c.h:229
QuerySource querySource
Definition: parsenodes.h:112
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
bool hasWindowFuncs
Definition: parsenodes.h:124
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition: parse_oper.c:187
void markTargetListOrigins(ParseState *pstate, List *targetlist)
Definition: parse_target.c:329
void parse_fixed_parameters(ParseState *pstate, Oid *paramTypes, int numParams)
Definition: parse_param.c:67
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
Expr * expr
Definition: primnodes.h:1368
Query * transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
Definition: analyze.c:187
#define ACL_INSERT
Definition: parsenodes.h:72
#define ACL_SELECT_FOR_UPDATE
Definition: parsenodes.h:88
RangeVar * relation
Definition: parsenodes.h:1461
bool canSetTag
Definition: parsenodes.h:116
struct SelectStmt * rarg
Definition: parsenodes.h:1533
List * checkInsertTargets(ParseState *pstate, List *cols, List **attrnos)
Definition: parse_target.c:953
List * transformWithClause(ParseState *pstate, WithClause *withClause)
Definition: parse_cte.c:105
static Query * transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
Definition: analyze.c:388
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
List * returningList
Definition: parsenodes.h:1465
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
static OnConflictExpr * transformOnConflictClause(ParseState *pstate, OnConflictClause *onConflictClause)
Definition: analyze.c:1003
SetOperation op
Definition: parsenodes.h:1560
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1295
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
static Query * transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
Definition: analyze.c:1554
#define UNKNOWNOID
Definition: pg_type.h:431
const char * name
Definition: encode.c:521
bool query_contains_extern_params(Query *query)
Definition: parse_param.c:325
#define InvalidAttrNumber
Definition: attnum.h:23
#define nodeTag(nodeptr)
Definition: nodes.h:514
List * groupClause
Definition: parsenodes.h:1503
char relpersistence
Definition: primnodes.h:71
RTEKind rtekind
Definition: parsenodes.h:936
Node * arbiterWhere
Definition: primnodes.h:1492
List * cteList
Definition: parsenodes.h:133
Node * setOperations
Definition: parsenodes.h:163
Query * subquery
Definition: parsenodes.h:959
List * groupClause
Definition: parsenodes.h:146
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool hasSubLinks
Definition: parsenodes.h:126
RangeVar * rel
Definition: primnodes.h:109
void assign_query_collations(ParseState *pstate, Query *query)
Relation p_target_relation
Definition: parse_node.h:182
Node * havingClause
Definition: parsenodes.h:1504
static Query * transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
Definition: analyze.c:211
void list_free(List *list)
Definition: list.c:1133
bool p_is_insert
Definition: parse_node.h:184
int i
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:2615
bool hasForUpdate
Definition: parsenodes.h:130
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
List * onConflictSet
Definition: primnodes.h:1496
#define NameStr(name)
Definition: c.h:499
void check_variable_parameters(ParseState *pstate, Query *query)
Definition: parse_param.c:263
List * p_ctenamespace
Definition: parse_node.h:179
List * p_joinlist
Definition: parse_node.h:174
bool hasModifyingCTE
Definition: parsenodes.h:129
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
WithClause * withClause
Definition: parsenodes.h:1525
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1035
static void determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
Definition: analyze.c:2143
#define copyObject(obj)
Definition: nodes.h:622
List * transformDistinctClause(ParseState *pstate, List **targetlist, List *sortClause, bool is_agg)
Node * havingQual
Definition: parsenodes.h:150
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:990
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:77
Node * onConflictWhere
Definition: primnodes.h:1497
OnConflictAction action
Definition: parsenodes.h:1338
Definition: pg_list.h:45
void updateTargetListEntry(ParseState *pstate, TargetEntry *tle, char *colname, int attrno, List *indirection, int location)
Definition: parse_target.c:617
int stmt_len
Definition: parsenodes.h:180
int16 AttrNumber
Definition: attnum.h:21
LockWaitPolicy waitPolicy
Definition: parsenodes.h:739
LockWaitPolicy
Definition: lockoptions.h:36
long val
Definition: informix.c:689
post_parse_analyze_hook_type post_parse_analyze_hook
Definition: analyze.c:49
char * catalogname
Definition: primnodes.h:66
WithClause * withClause
Definition: parsenodes.h:1466
List * p_joinexprs
Definition: parse_node.h:173
Node * limitCount
Definition: parsenodes.h:1523
Node * whereClause
Definition: parsenodes.h:1502
#define lfirst_oid(lc)
Definition: pg_list.h:108
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Definition: analyze.c:2841
static List * transformInsertRow(ParseState *pstate, List *exprlist, List *stmtcols, List *icolumns, List *attrnos, bool strip_indirection)
Definition: analyze.c:899
List * p_rtable
Definition: parse_node.h:172