PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
parse_cte.c File Reference
#include "postgres.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "nodes/nodeFuncs.h"
#include "parser/analyze.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_cte.h"
#include "parser/parse_expr.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
Include dependency graph for parse_cte.c:

Go to the source code of this file.

Data Structures

struct  CteItem
 
struct  CteState
 

Typedefs

typedef struct CteItem CteItem
 
typedef struct CteState CteState
 

Enumerations

enum  RecursionContext {
  RECURSION_OK , RECURSION_NONRECURSIVETERM , RECURSION_SUBLINK , RECURSION_OUTERJOIN ,
  RECURSION_INTERSECT , RECURSION_EXCEPT
}
 

Functions

static void analyzeCTE (ParseState *pstate, CommonTableExpr *cte)
 
static void makeDependencyGraph (CteState *cstate)
 
static bool makeDependencyGraphWalker (Node *node, CteState *cstate)
 
static void TopologicalSort (ParseState *pstate, CteItem *items, int numitems)
 
static void checkWellFormedRecursion (CteState *cstate)
 
static bool checkWellFormedRecursionWalker (Node *node, CteState *cstate)
 
static void checkWellFormedSelectStmt (SelectStmt *stmt, CteState *cstate)
 
ListtransformWithClause (ParseState *pstate, WithClause *withClause)
 
void analyzeCTETargetList (ParseState *pstate, CommonTableExpr *cte, List *tlist)
 

Variables

static const char *const recursion_errormsgs []
 

Typedef Documentation

◆ CteItem

typedef struct CteItem CteItem

◆ CteState

typedef struct CteState CteState

Enumeration Type Documentation

◆ RecursionContext

Enumerator
RECURSION_OK 
RECURSION_NONRECURSIVETERM 
RECURSION_SUBLINK 
RECURSION_OUTERJOIN 
RECURSION_INTERSECT 
RECURSION_EXCEPT 

Definition at line 31 of file parse_cte.c.

32 {
34  RECURSION_NONRECURSIVETERM, /* inside the left-hand term */
35  RECURSION_SUBLINK, /* inside a sublink */
36  RECURSION_OUTERJOIN, /* inside nullable side of an outer join */
37  RECURSION_INTERSECT, /* underneath INTERSECT (ALL) */
38  RECURSION_EXCEPT, /* underneath EXCEPT (ALL) */
RecursionContext
Definition: parse_cte.c:32
@ RECURSION_INTERSECT
Definition: parse_cte.c:37
@ RECURSION_SUBLINK
Definition: parse_cte.c:35
@ RECURSION_EXCEPT
Definition: parse_cte.c:38
@ RECURSION_OUTERJOIN
Definition: parse_cte.c:36
@ RECURSION_NONRECURSIVETERM
Definition: parse_cte.c:34
@ RECURSION_OK
Definition: parse_cte.c:33

Function Documentation

◆ analyzeCTE()

static void analyzeCTE ( ParseState pstate,
CommonTableExpr cte 
)
static

Definition at line 242 of file parse_cte.c.

243 {
244  Query *query;
245  CTESearchClause *search_clause = cte->search_clause;
246  CTECycleClause *cycle_clause = cte->cycle_clause;
247 
248  /* Analysis not done already */
249  Assert(!IsA(cte->ctequery, Query));
250 
251  /*
252  * Before analyzing the CTE's query, we'd better identify the data type of
253  * the cycle mark column if any, since the query could refer to that.
254  * Other validity checks on the cycle clause will be done afterwards.
255  */
256  if (cycle_clause)
257  {
258  TypeCacheEntry *typentry;
259  Oid op;
260 
261  cycle_clause->cycle_mark_value =
262  transformExpr(pstate, cycle_clause->cycle_mark_value,
264  cycle_clause->cycle_mark_default =
265  transformExpr(pstate, cycle_clause->cycle_mark_default,
267 
268  cycle_clause->cycle_mark_type =
269  select_common_type(pstate,
270  list_make2(cycle_clause->cycle_mark_value,
271  cycle_clause->cycle_mark_default),
272  "CYCLE", NULL);
273  cycle_clause->cycle_mark_value =
274  coerce_to_common_type(pstate,
275  cycle_clause->cycle_mark_value,
276  cycle_clause->cycle_mark_type,
277  "CYCLE/SET/TO");
278  cycle_clause->cycle_mark_default =
279  coerce_to_common_type(pstate,
280  cycle_clause->cycle_mark_default,
281  cycle_clause->cycle_mark_type,
282  "CYCLE/SET/DEFAULT");
283 
284  cycle_clause->cycle_mark_typmod =
285  select_common_typmod(pstate,
286  list_make2(cycle_clause->cycle_mark_value,
287  cycle_clause->cycle_mark_default),
288  cycle_clause->cycle_mark_type);
289 
290  cycle_clause->cycle_mark_collation =
292  list_make2(cycle_clause->cycle_mark_value,
293  cycle_clause->cycle_mark_default),
294  true);
295 
296  /* Might as well look up the relevant <> operator while we are at it */
297  typentry = lookup_type_cache(cycle_clause->cycle_mark_type,
299  if (!OidIsValid(typentry->eq_opr))
300  ereport(ERROR,
301  errcode(ERRCODE_UNDEFINED_FUNCTION),
302  errmsg("could not identify an equality operator for type %s",
303  format_type_be(cycle_clause->cycle_mark_type)));
304  op = get_negator(typentry->eq_opr);
305  if (!OidIsValid(op))
306  ereport(ERROR,
307  errcode(ERRCODE_UNDEFINED_FUNCTION),
308  errmsg("could not identify an inequality operator for type %s",
309  format_type_be(cycle_clause->cycle_mark_type)));
310 
311  cycle_clause->cycle_mark_neop = op;
312  }
313 
314  /* Now we can get on with analyzing the CTE's query */
315  query = parse_sub_analyze(cte->ctequery, pstate, cte, false, true);
316  cte->ctequery = (Node *) query;
317 
318  /*
319  * Check that we got something reasonable. These first two cases should
320  * be prevented by the grammar.
321  */
322  if (!IsA(query, Query))
323  elog(ERROR, "unexpected non-Query statement in WITH");
324  if (query->utilityStmt != NULL)
325  elog(ERROR, "unexpected utility statement in WITH");
326 
327  /*
328  * We disallow data-modifying WITH except at the top level of a query,
329  * because it's not clear when such a modification should be executed.
330  */
331  if (query->commandType != CMD_SELECT &&
332  pstate->parentParseState != NULL)
333  ereport(ERROR,
334  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
335  errmsg("WITH clause containing a data-modifying statement must be at the top level"),
336  parser_errposition(pstate, cte->location)));
337 
338  /*
339  * CTE queries are always marked not canSetTag. (Currently this only
340  * matters for data-modifying statements, for which the flag will be
341  * propagated to the ModifyTable plan node.)
342  */
343  query->canSetTag = false;
344 
345  if (!cte->cterecursive)
346  {
347  /* Compute the output column names/types if not done yet */
348  analyzeCTETargetList(pstate, cte, GetCTETargetList(cte));
349  }
350  else
351  {
352  /*
353  * Verify that the previously determined output column types and
354  * collations match what the query really produced. We have to check
355  * this because the recursive term could have overridden the
356  * non-recursive term, and we don't have any easy way to fix that.
357  */
358  ListCell *lctlist,
359  *lctyp,
360  *lctypmod,
361  *lccoll;
362  int varattno;
363 
364  lctyp = list_head(cte->ctecoltypes);
365  lctypmod = list_head(cte->ctecoltypmods);
366  lccoll = list_head(cte->ctecolcollations);
367  varattno = 0;
368  foreach(lctlist, GetCTETargetList(cte))
369  {
370  TargetEntry *te = (TargetEntry *) lfirst(lctlist);
371  Node *texpr;
372 
373  if (te->resjunk)
374  continue;
375  varattno++;
376  Assert(varattno == te->resno);
377  if (lctyp == NULL || lctypmod == NULL || lccoll == NULL) /* shouldn't happen */
378  elog(ERROR, "wrong number of output columns in WITH");
379  texpr = (Node *) te->expr;
380  if (exprType(texpr) != lfirst_oid(lctyp) ||
381  exprTypmod(texpr) != lfirst_int(lctypmod))
382  ereport(ERROR,
383  (errcode(ERRCODE_DATATYPE_MISMATCH),
384  errmsg("recursive query \"%s\" column %d has type %s in non-recursive term but type %s overall",
385  cte->ctename, varattno,
387  lfirst_int(lctypmod)),
389  exprTypmod(texpr))),
390  errhint("Cast the output of the non-recursive term to the correct type."),
391  parser_errposition(pstate, exprLocation(texpr))));
392  if (exprCollation(texpr) != lfirst_oid(lccoll))
393  ereport(ERROR,
394  (errcode(ERRCODE_COLLATION_MISMATCH),
395  errmsg("recursive query \"%s\" column %d has collation \"%s\" in non-recursive term but collation \"%s\" overall",
396  cte->ctename, varattno,
399  errhint("Use the COLLATE clause to set the collation of the non-recursive term."),
400  parser_errposition(pstate, exprLocation(texpr))));
401  lctyp = lnext(cte->ctecoltypes, lctyp);
402  lctypmod = lnext(cte->ctecoltypmods, lctypmod);
403  lccoll = lnext(cte->ctecolcollations, lccoll);
404  }
405  if (lctyp != NULL || lctypmod != NULL || lccoll != NULL) /* shouldn't happen */
406  elog(ERROR, "wrong number of output columns in WITH");
407  }
408 
409  /*
410  * Now make validity checks on the SEARCH and CYCLE clauses, if present.
411  */
412  if (search_clause || cycle_clause)
413  {
414  Query *ctequery;
415  SetOperationStmt *sos;
416 
417  if (!cte->cterecursive)
418  ereport(ERROR,
419  (errcode(ERRCODE_SYNTAX_ERROR),
420  errmsg("WITH query is not recursive"),
421  parser_errposition(pstate, cte->location)));
422 
423  /*
424  * SQL requires a WITH list element (CTE) to be "expandable" in order
425  * to allow a search or cycle clause. That is a stronger requirement
426  * than just being recursive. It basically means the query expression
427  * looks like
428  *
429  * non-recursive query UNION [ALL] recursive query
430  *
431  * and that the recursive query is not itself a set operation.
432  *
433  * As of this writing, most of these criteria are already satisfied by
434  * all recursive CTEs allowed by PostgreSQL. In the future, if
435  * further variants recursive CTEs are accepted, there might be
436  * further checks required here to determine what is "expandable".
437  */
438 
439  ctequery = castNode(Query, cte->ctequery);
440  Assert(ctequery->setOperations);
441  sos = castNode(SetOperationStmt, ctequery->setOperations);
442 
443  /*
444  * This left side check is not required for expandability, but
445  * rewriteSearchAndCycle() doesn't currently have support for it, so
446  * we catch it here.
447  */
448  if (!IsA(sos->larg, RangeTblRef))
449  ereport(ERROR,
450  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
451  errmsg("with a SEARCH or CYCLE clause, the left side of the UNION must be a SELECT")));
452 
453  if (!IsA(sos->rarg, RangeTblRef))
454  ereport(ERROR,
455  (errcode(ERRCODE_SYNTAX_ERROR),
456  errmsg("with a SEARCH or CYCLE clause, the right side of the UNION must be a SELECT")));
457  }
458 
459  if (search_clause)
460  {
461  ListCell *lc;
462  List *seen = NIL;
463 
464  foreach(lc, search_clause->search_col_list)
465  {
466  String *colname = lfirst_node(String, lc);
467 
468  if (!list_member(cte->ctecolnames, colname))
469  ereport(ERROR,
470  (errcode(ERRCODE_SYNTAX_ERROR),
471  errmsg("search column \"%s\" not in WITH query column list",
472  strVal(colname)),
473  parser_errposition(pstate, search_clause->location)));
474 
475  if (list_member(seen, colname))
476  ereport(ERROR,
477  (errcode(ERRCODE_DUPLICATE_COLUMN),
478  errmsg("search column \"%s\" specified more than once",
479  strVal(colname)),
480  parser_errposition(pstate, search_clause->location)));
481  seen = lappend(seen, colname);
482  }
483 
484  if (list_member(cte->ctecolnames, makeString(search_clause->search_seq_column)))
485  ereport(ERROR,
486  errcode(ERRCODE_SYNTAX_ERROR),
487  errmsg("search sequence column name \"%s\" already used in WITH query column list",
488  search_clause->search_seq_column),
489  parser_errposition(pstate, search_clause->location));
490  }
491 
492  if (cycle_clause)
493  {
494  ListCell *lc;
495  List *seen = NIL;
496 
497  foreach(lc, cycle_clause->cycle_col_list)
498  {
499  String *colname = lfirst_node(String, lc);
500 
501  if (!list_member(cte->ctecolnames, colname))
502  ereport(ERROR,
503  (errcode(ERRCODE_SYNTAX_ERROR),
504  errmsg("cycle column \"%s\" not in WITH query column list",
505  strVal(colname)),
506  parser_errposition(pstate, cycle_clause->location)));
507 
508  if (list_member(seen, colname))
509  ereport(ERROR,
510  (errcode(ERRCODE_DUPLICATE_COLUMN),
511  errmsg("cycle column \"%s\" specified more than once",
512  strVal(colname)),
513  parser_errposition(pstate, cycle_clause->location)));
514  seen = lappend(seen, colname);
515  }
516 
517  if (list_member(cte->ctecolnames, makeString(cycle_clause->cycle_mark_column)))
518  ereport(ERROR,
519  errcode(ERRCODE_SYNTAX_ERROR),
520  errmsg("cycle mark column name \"%s\" already used in WITH query column list",
521  cycle_clause->cycle_mark_column),
522  parser_errposition(pstate, cycle_clause->location));
523 
524  if (list_member(cte->ctecolnames, makeString(cycle_clause->cycle_path_column)))
525  ereport(ERROR,
526  errcode(ERRCODE_SYNTAX_ERROR),
527  errmsg("cycle path column name \"%s\" already used in WITH query column list",
528  cycle_clause->cycle_path_column),
529  parser_errposition(pstate, cycle_clause->location));
530 
531  if (strcmp(cycle_clause->cycle_mark_column,
532  cycle_clause->cycle_path_column) == 0)
533  ereport(ERROR,
534  errcode(ERRCODE_SYNTAX_ERROR),
535  errmsg("cycle mark column name and cycle path column name are the same"),
536  parser_errposition(pstate, cycle_clause->location));
537  }
538 
539  if (search_clause && cycle_clause)
540  {
541  if (strcmp(search_clause->search_seq_column,
542  cycle_clause->cycle_mark_column) == 0)
543  ereport(ERROR,
544  errcode(ERRCODE_SYNTAX_ERROR),
545  errmsg("search sequence column name and cycle mark column name are the same"),
546  parser_errposition(pstate, search_clause->location));
547 
548  if (strcmp(search_clause->search_seq_column,
549  cycle_clause->cycle_path_column) == 0)
550  ereport(ERROR,
551  errcode(ERRCODE_SYNTAX_ERROR),
552  errmsg("search sequence column name and cycle path column name are the same"),
553  parser_errposition(pstate, search_clause->location));
554  }
555 }
#define Assert(condition)
Definition: c.h:863
#define OidIsValid(objectId)
Definition: c.h:780
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:362
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
List * lappend(List *list, void *datum)
Definition: list.c:339
bool list_member(const List *list, const void *datum)
Definition: list.c:661
char * get_collation_name(Oid colloid)
Definition: lsyscache.c:1035
Oid get_negator(Oid opno)
Definition: lsyscache.c:1533
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:816
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1380
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
@ CMD_SELECT
Definition: nodes.h:265
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
int32 select_common_typmod(ParseState *pstate, List *exprs, Oid common_type)
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Oid select_common_collation(ParseState *pstate, List *exprs, bool none_ok)
void analyzeCTETargetList(ParseState *pstate, CommonTableExpr *cte, List *tlist)
Definition: parse_cte.c:570
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:118
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
@ EXPR_KIND_CYCLE_MARK
Definition: parse_node.h:84
#define GetCTETargetList(cte)
Definition: parsenodes.h:1705
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition: analyze.c:222
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define NIL
Definition: pg_list.h:68
#define lfirst_int(lc)
Definition: pg_list.h:173
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define list_make2(x1, x2)
Definition: pg_list.h:214
unsigned int Oid
Definition: postgres_ext.h:31
char * cycle_path_column
Definition: parsenodes.h:1659
ParseLoc location
Definition: parsenodes.h:1660
Node * cycle_mark_default
Definition: parsenodes.h:1658
Oid cycle_mark_collation
Definition: parsenodes.h:1664
List * cycle_col_list
Definition: parsenodes.h:1655
char * cycle_mark_column
Definition: parsenodes.h:1656
Node * cycle_mark_value
Definition: parsenodes.h:1657
ParseLoc location
Definition: parsenodes.h:1649
char * search_seq_column
Definition: parsenodes.h:1648
List * search_col_list
Definition: parsenodes.h:1646
ParseLoc location
Definition: parsenodes.h:1684
Definition: pg_list.h:54
Definition: nodes.h:129
ParseState * parentParseState
Definition: parse_node.h:208
Node * setOperations
Definition: parsenodes.h:221
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:136
Definition: value.h:64
Expr * expr
Definition: primnodes.h:2190
AttrNumber resno
Definition: primnodes.h:2192
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:386
#define TYPECACHE_EQ_OPR
Definition: typcache.h:137
String * makeString(char *str)
Definition: value.c:63
#define strVal(v)
Definition: value.h:82

References analyzeCTETargetList(), Assert, castNode, CMD_SELECT, coerce_to_common_type(), Query::commandType, CommonTableExpr::ctename, CommonTableExpr::ctequery, CTECycleClause::cycle_col_list, CTECycleClause::cycle_mark_collation, CTECycleClause::cycle_mark_column, CTECycleClause::cycle_mark_default, CTECycleClause::cycle_mark_neop, CTECycleClause::cycle_mark_type, CTECycleClause::cycle_mark_typmod, CTECycleClause::cycle_mark_value, CTECycleClause::cycle_path_column, elog, TypeCacheEntry::eq_opr, ereport, errcode(), errhint(), errmsg(), ERROR, TargetEntry::expr, EXPR_KIND_CYCLE_MARK, exprCollation(), exprLocation(), exprType(), exprTypmod(), format_type_be(), format_type_with_typemod(), get_collation_name(), get_negator(), GetCTETargetList, if(), IsA, lappend(), SetOperationStmt::larg, lfirst, lfirst_int, lfirst_node, lfirst_oid, list_head(), list_make2, list_member(), lnext(), CTESearchClause::location, CTECycleClause::location, CommonTableExpr::location, lookup_type_cache(), makeString(), NIL, OidIsValid, ParseState::parentParseState, parse_sub_analyze(), parser_errposition(), SetOperationStmt::rarg, TargetEntry::resno, CTESearchClause::search_col_list, CTESearchClause::search_seq_column, select_common_collation(), select_common_type(), select_common_typmod(), Query::setOperations, strVal, transformExpr(), TYPECACHE_EQ_OPR, and Query::utilityStmt.

Referenced by transformWithClause().

◆ analyzeCTETargetList()

void analyzeCTETargetList ( ParseState pstate,
CommonTableExpr cte,
List tlist 
)

Definition at line 570 of file parse_cte.c.

571 {
572  int numaliases;
573  int varattno;
574  ListCell *tlistitem;
575 
576  /* Not done already ... */
577  Assert(cte->ctecolnames == NIL);
578 
579  /*
580  * We need to determine column names, types, and collations. The alias
581  * column names override anything coming from the query itself. (Note:
582  * the SQL spec says that the alias list must be empty or exactly as long
583  * as the output column set; but we allow it to be shorter for consistency
584  * with Alias handling.)
585  */
586  cte->ctecolnames = copyObject(cte->aliascolnames);
587  cte->ctecoltypes = cte->ctecoltypmods = cte->ctecolcollations = NIL;
588  numaliases = list_length(cte->aliascolnames);
589  varattno = 0;
590  foreach(tlistitem, tlist)
591  {
592  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
593  Oid coltype;
594  int32 coltypmod;
595  Oid colcoll;
596 
597  if (te->resjunk)
598  continue;
599  varattno++;
600  Assert(varattno == te->resno);
601  if (varattno > numaliases)
602  {
603  char *attrname;
604 
605  attrname = pstrdup(te->resname);
606  cte->ctecolnames = lappend(cte->ctecolnames, makeString(attrname));
607  }
608  coltype = exprType((Node *) te->expr);
609  coltypmod = exprTypmod((Node *) te->expr);
610  colcoll = exprCollation((Node *) te->expr);
611 
612  /*
613  * If the CTE is recursive, force the exposed column type of any
614  * "unknown" column to "text". We must deal with this here because
615  * we're called on the non-recursive term before there's been any
616  * attempt to force unknown output columns to some other type. We
617  * have to resolve unknowns before looking at the recursive term.
618  *
619  * The column might contain 'foo' COLLATE "bar", so don't override
620  * collation if it's already set.
621  */
622  if (cte->cterecursive && coltype == UNKNOWNOID)
623  {
624  coltype = TEXTOID;
625  coltypmod = -1; /* should be -1 already, but be sure */
626  if (!OidIsValid(colcoll))
627  colcoll = DEFAULT_COLLATION_OID;
628  }
629  cte->ctecoltypes = lappend_oid(cte->ctecoltypes, coltype);
630  cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, coltypmod);
631  cte->ctecolcollations = lappend_oid(cte->ctecolcollations, colcoll);
632  }
633  if (varattno < numaliases)
634  ereport(ERROR,
635  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
636  errmsg("WITH query \"%s\" has %d columns available but %d columns specified",
637  cte->ctename, varattno, numaliases),
638  parser_errposition(pstate, cte->location)));
639 }
signed int int32
Definition: c.h:508
List * lappend_int(List *list, int datum)
Definition: list.c:357
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
char * pstrdup(const char *in)
Definition: mcxt.c:1696
#define copyObject(obj)
Definition: nodes.h:224
static int list_length(const List *l)
Definition: pg_list.h:152

References Assert, copyObject, CommonTableExpr::ctename, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), lappend(), lappend_int(), lappend_oid(), lfirst, list_length(), CommonTableExpr::location, makeString(), NIL, OidIsValid, parser_errposition(), pstrdup(), and TargetEntry::resno.

Referenced by analyzeCTE(), and determineRecursiveColTypes().

◆ checkWellFormedRecursion()

static void checkWellFormedRecursion ( CteState cstate)
static

Definition at line 849 of file parse_cte.c.

850 {
851  int i;
852 
853  for (i = 0; i < cstate->numitems; i++)
854  {
855  CommonTableExpr *cte = cstate->items[i].cte;
856  SelectStmt *stmt = (SelectStmt *) cte->ctequery;
857 
858  Assert(!IsA(stmt, Query)); /* not analyzed yet */
859 
860  /* Ignore items that weren't found to be recursive */
861  if (!cte->cterecursive)
862  continue;
863 
864  /* Must be a SELECT statement */
865  if (!IsA(stmt, SelectStmt))
866  ereport(ERROR,
867  (errcode(ERRCODE_INVALID_RECURSION),
868  errmsg("recursive query \"%s\" must not contain data-modifying statements",
869  cte->ctename),
870  parser_errposition(cstate->pstate, cte->location)));
871 
872  /* Must have top-level UNION */
873  if (stmt->op != SETOP_UNION)
874  ereport(ERROR,
875  (errcode(ERRCODE_INVALID_RECURSION),
876  errmsg("recursive query \"%s\" does not have the form non-recursive-term UNION [ALL] recursive-term",
877  cte->ctename),
878  parser_errposition(cstate->pstate, cte->location)));
879 
880  /*
881  * Really, we should insist that there not be a top-level WITH, since
882  * syntactically that would enclose the UNION. However, we've not
883  * done so in the past and it's probably too late to change. Settle
884  * for insisting that WITH not contain a self-reference. Test this
885  * before examining the UNION arms, to avoid issuing confusing errors
886  * in such cases.
887  */
888  if (stmt->withClause)
889  {
890  cstate->curitem = i;
891  cstate->innerwiths = NIL;
892  cstate->selfrefcount = 0;
893  cstate->context = RECURSION_SUBLINK;
894  checkWellFormedRecursionWalker((Node *) stmt->withClause->ctes,
895  cstate);
896  Assert(cstate->innerwiths == NIL);
897  }
898 
899  /*
900  * Disallow ORDER BY and similar decoration atop the UNION. These
901  * don't make sense because it's impossible to figure out what they
902  * mean when we have only part of the recursive query's results. (If
903  * we did allow them, we'd have to check for recursive references
904  * inside these subtrees. As for WITH, we have to do this before
905  * examining the UNION arms, to avoid issuing confusing errors if
906  * there is a recursive reference here.)
907  */
908  if (stmt->sortClause)
909  ereport(ERROR,
910  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
911  errmsg("ORDER BY in a recursive query is not implemented"),
912  parser_errposition(cstate->pstate,
913  exprLocation((Node *) stmt->sortClause))));
914  if (stmt->limitOffset)
915  ereport(ERROR,
916  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
917  errmsg("OFFSET in a recursive query is not implemented"),
918  parser_errposition(cstate->pstate,
919  exprLocation(stmt->limitOffset))));
920  if (stmt->limitCount)
921  ereport(ERROR,
922  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
923  errmsg("LIMIT in a recursive query is not implemented"),
924  parser_errposition(cstate->pstate,
925  exprLocation(stmt->limitCount))));
926  if (stmt->lockingClause)
927  ereport(ERROR,
928  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
929  errmsg("FOR UPDATE/SHARE in a recursive query is not implemented"),
930  parser_errposition(cstate->pstate,
931  exprLocation((Node *) stmt->lockingClause))));
932 
933  /*
934  * Now we can get on with checking the UNION operands themselves.
935  *
936  * The left-hand operand mustn't contain a self-reference at all.
937  */
938  cstate->curitem = i;
939  cstate->innerwiths = NIL;
940  cstate->selfrefcount = 0;
942  checkWellFormedRecursionWalker((Node *) stmt->larg, cstate);
943  Assert(cstate->innerwiths == NIL);
944 
945  /* Right-hand operand should contain one reference in a valid place */
946  cstate->curitem = i;
947  cstate->innerwiths = NIL;
948  cstate->selfrefcount = 0;
949  cstate->context = RECURSION_OK;
950  checkWellFormedRecursionWalker((Node *) stmt->rarg, cstate);
951  Assert(cstate->innerwiths == NIL);
952  if (cstate->selfrefcount != 1) /* shouldn't happen */
953  elog(ERROR, "missing recursive reference");
954  }
955 }
#define stmt
Definition: indent_codes.h:59
int i
Definition: isn.c:72
static bool checkWellFormedRecursionWalker(Node *node, CteState *cstate)
Definition: parse_cte.c:961
@ SETOP_UNION
Definition: parsenodes.h:2119
CommonTableExpr * cte
Definition: parse_cte.c:65
int selfrefcount
Definition: parse_cte.c:81
RecursionContext context
Definition: parse_cte.c:82
ParseState * pstate
Definition: parse_cte.c:74
int curitem
Definition: parse_cte.c:78
int numitems
Definition: parse_cte.c:76
List * innerwiths
Definition: parse_cte.c:79
CteItem * items
Definition: parse_cte.c:75

References Assert, checkWellFormedRecursionWalker(), CteState::context, CteItem::cte, CommonTableExpr::ctename, CommonTableExpr::ctequery, CteState::curitem, elog, ereport, errcode(), errmsg(), ERROR, exprLocation(), i, CteState::innerwiths, IsA, CteState::items, CommonTableExpr::location, NIL, CteState::numitems, parser_errposition(), CteState::pstate, RECURSION_NONRECURSIVETERM, RECURSION_OK, RECURSION_SUBLINK, CteState::selfrefcount, SETOP_UNION, and stmt.

Referenced by transformWithClause().

◆ checkWellFormedRecursionWalker()

static bool checkWellFormedRecursionWalker ( Node node,
CteState cstate 
)
static

Definition at line 961 of file parse_cte.c.

962 {
963  RecursionContext save_context = cstate->context;
964 
965  if (node == NULL)
966  return false;
967  if (IsA(node, RangeVar))
968  {
969  RangeVar *rv = (RangeVar *) node;
970 
971  /* If unqualified name, might be a CTE reference */
972  if (!rv->schemaname)
973  {
974  ListCell *lc;
975  CommonTableExpr *mycte;
976 
977  /* ... but first see if it's captured by an inner WITH */
978  foreach(lc, cstate->innerwiths)
979  {
980  List *withlist = (List *) lfirst(lc);
981  ListCell *lc2;
982 
983  foreach(lc2, withlist)
984  {
985  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc2);
986 
987  if (strcmp(rv->relname, cte->ctename) == 0)
988  return false; /* yes, so bail out */
989  }
990  }
991 
992  /* No, could be a reference to the query level we are working on */
993  mycte = cstate->items[cstate->curitem].cte;
994  if (strcmp(rv->relname, mycte->ctename) == 0)
995  {
996  /* Found a recursive reference to the active query */
997  if (cstate->context != RECURSION_OK)
998  ereport(ERROR,
999  (errcode(ERRCODE_INVALID_RECURSION),
1001  mycte->ctename),
1002  parser_errposition(cstate->pstate,
1003  rv->location)));
1004  /* Count references */
1005  if (++(cstate->selfrefcount) > 1)
1006  ereport(ERROR,
1007  (errcode(ERRCODE_INVALID_RECURSION),
1008  errmsg("recursive reference to query \"%s\" must not appear more than once",
1009  mycte->ctename),
1010  parser_errposition(cstate->pstate,
1011  rv->location)));
1012  }
1013  }
1014  return false;
1015  }
1016  if (IsA(node, SelectStmt))
1017  {
1018  SelectStmt *stmt = (SelectStmt *) node;
1019  ListCell *lc;
1020 
1021  if (stmt->withClause)
1022  {
1023  if (stmt->withClause->recursive)
1024  {
1025  /*
1026  * In the RECURSIVE case, all query names of the WITH are
1027  * visible to all WITH items as well as the main query. So
1028  * push them all on, process, pop them all off.
1029  */
1030  cstate->innerwiths = lcons(stmt->withClause->ctes,
1031  cstate->innerwiths);
1032  foreach(lc, stmt->withClause->ctes)
1033  {
1034  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1035 
1036  (void) checkWellFormedRecursionWalker(cte->ctequery, cstate);
1037  }
1039  cstate->innerwiths = list_delete_first(cstate->innerwiths);
1040  }
1041  else
1042  {
1043  /*
1044  * In the non-RECURSIVE case, query names are visible to the
1045  * WITH items after them and to the main query.
1046  */
1047  cstate->innerwiths = lcons(NIL, cstate->innerwiths);
1048  foreach(lc, stmt->withClause->ctes)
1049  {
1050  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1051  ListCell *cell1;
1052 
1053  (void) checkWellFormedRecursionWalker(cte->ctequery, cstate);
1054  /* note that recursion could mutate innerwiths list */
1055  cell1 = list_head(cstate->innerwiths);
1056  lfirst(cell1) = lappend((List *) lfirst(cell1), cte);
1057  }
1059  cstate->innerwiths = list_delete_first(cstate->innerwiths);
1060  }
1061  }
1062  else
1064  /* We're done examining the SelectStmt */
1065  return false;
1066  }
1067  if (IsA(node, WithClause))
1068  {
1069  /*
1070  * Prevent raw_expression_tree_walker from recursing directly into a
1071  * WITH clause. We need that to happen only under the control of the
1072  * code above.
1073  */
1074  return false;
1075  }
1076  if (IsA(node, JoinExpr))
1077  {
1078  JoinExpr *j = (JoinExpr *) node;
1079 
1080  switch (j->jointype)
1081  {
1082  case JOIN_INNER:
1083  checkWellFormedRecursionWalker(j->larg, cstate);
1084  checkWellFormedRecursionWalker(j->rarg, cstate);
1085  checkWellFormedRecursionWalker(j->quals, cstate);
1086  break;
1087  case JOIN_LEFT:
1088  checkWellFormedRecursionWalker(j->larg, cstate);
1089  if (save_context == RECURSION_OK)
1090  cstate->context = RECURSION_OUTERJOIN;
1091  checkWellFormedRecursionWalker(j->rarg, cstate);
1092  cstate->context = save_context;
1093  checkWellFormedRecursionWalker(j->quals, cstate);
1094  break;
1095  case JOIN_FULL:
1096  if (save_context == RECURSION_OK)
1097  cstate->context = RECURSION_OUTERJOIN;
1098  checkWellFormedRecursionWalker(j->larg, cstate);
1099  checkWellFormedRecursionWalker(j->rarg, cstate);
1100  cstate->context = save_context;
1101  checkWellFormedRecursionWalker(j->quals, cstate);
1102  break;
1103  case JOIN_RIGHT:
1104  if (save_context == RECURSION_OK)
1105  cstate->context = RECURSION_OUTERJOIN;
1106  checkWellFormedRecursionWalker(j->larg, cstate);
1107  cstate->context = save_context;
1108  checkWellFormedRecursionWalker(j->rarg, cstate);
1109  checkWellFormedRecursionWalker(j->quals, cstate);
1110  break;
1111  default:
1112  elog(ERROR, "unrecognized join type: %d",
1113  (int) j->jointype);
1114  }
1115  return false;
1116  }
1117  if (IsA(node, SubLink))
1118  {
1119  SubLink *sl = (SubLink *) node;
1120 
1121  /*
1122  * we intentionally override outer context, since subquery is
1123  * independent
1124  */
1125  cstate->context = RECURSION_SUBLINK;
1127  cstate->context = save_context;
1129  return false;
1130  }
1131  return raw_expression_tree_walker(node,
1133  (void *) cstate);
1134 }
int j
Definition: isn.c:73
List * list_delete_first(List *list)
Definition: list.c:943
List * lcons(void *datum, List *list)
Definition: list.c:495
#define raw_expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:176
@ JOIN_FULL
Definition: nodes.h:295
@ JOIN_INNER
Definition: nodes.h:293
@ JOIN_RIGHT
Definition: nodes.h:296
@ JOIN_LEFT
Definition: nodes.h:294
static void checkWellFormedSelectStmt(SelectStmt *stmt, CteState *cstate)
Definition: parse_cte.c:1141
static const char *const recursion_errormsgs[]
Definition: parse_cte.c:42
char * relname
Definition: primnodes.h:82
ParseLoc location
Definition: primnodes.h:94
char * schemaname
Definition: primnodes.h:79

References checkWellFormedSelectStmt(), CteState::context, CteItem::cte, CommonTableExpr::ctename, CommonTableExpr::ctequery, CteState::curitem, elog, ereport, errcode(), errmsg(), ERROR, CteState::innerwiths, IsA, CteState::items, j, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, lappend(), lcons(), lfirst, list_delete_first(), list_head(), RangeVar::location, NIL, parser_errposition(), CteState::pstate, raw_expression_tree_walker, recursion_errormsgs, RECURSION_OK, RECURSION_OUTERJOIN, RECURSION_SUBLINK, RangeVar::relname, RangeVar::schemaname, CteState::selfrefcount, stmt, SubLink::subselect, and SubLink::testexpr.

Referenced by checkWellFormedRecursion(), and checkWellFormedSelectStmt().

◆ checkWellFormedSelectStmt()

static void checkWellFormedSelectStmt ( SelectStmt stmt,
CteState cstate 
)
static

Definition at line 1141 of file parse_cte.c.

1142 {
1143  RecursionContext save_context = cstate->context;
1144 
1145  if (save_context != RECURSION_OK)
1146  {
1147  /* just recurse without changing state */
1150  (void *) cstate);
1151  }
1152  else
1153  {
1154  switch (stmt->op)
1155  {
1156  case SETOP_NONE:
1157  case SETOP_UNION:
1160  (void *) cstate);
1161  break;
1162  case SETOP_INTERSECT:
1163  if (stmt->all)
1164  cstate->context = RECURSION_INTERSECT;
1166  cstate);
1168  cstate);
1169  cstate->context = save_context;
1170  checkWellFormedRecursionWalker((Node *) stmt->sortClause,
1171  cstate);
1172  checkWellFormedRecursionWalker((Node *) stmt->limitOffset,
1173  cstate);
1174  checkWellFormedRecursionWalker((Node *) stmt->limitCount,
1175  cstate);
1176  checkWellFormedRecursionWalker((Node *) stmt->lockingClause,
1177  cstate);
1178  /* stmt->withClause is intentionally ignored here */
1179  break;
1180  case SETOP_EXCEPT:
1181  if (stmt->all)
1182  cstate->context = RECURSION_EXCEPT;
1184  cstate);
1185  cstate->context = RECURSION_EXCEPT;
1187  cstate);
1188  cstate->context = save_context;
1189  checkWellFormedRecursionWalker((Node *) stmt->sortClause,
1190  cstate);
1191  checkWellFormedRecursionWalker((Node *) stmt->limitOffset,
1192  cstate);
1193  checkWellFormedRecursionWalker((Node *) stmt->limitCount,
1194  cstate);
1195  checkWellFormedRecursionWalker((Node *) stmt->lockingClause,
1196  cstate);
1197  /* stmt->withClause is intentionally ignored here */
1198  break;
1199  default:
1200  elog(ERROR, "unrecognized set op: %d",
1201  (int) stmt->op);
1202  }
1203  }
1204 }
@ SETOP_INTERSECT
Definition: parsenodes.h:2120
@ SETOP_EXCEPT
Definition: parsenodes.h:2121
@ SETOP_NONE
Definition: parsenodes.h:2118

References checkWellFormedRecursionWalker(), CteState::context, elog, ERROR, raw_expression_tree_walker, RECURSION_EXCEPT, RECURSION_INTERSECT, RECURSION_OK, SETOP_EXCEPT, SETOP_INTERSECT, SETOP_NONE, SETOP_UNION, and stmt.

Referenced by checkWellFormedRecursionWalker().

◆ makeDependencyGraph()

static void makeDependencyGraph ( CteState cstate)
static

Definition at line 647 of file parse_cte.c.

648 {
649  int i;
650 
651  for (i = 0; i < cstate->numitems; i++)
652  {
653  CommonTableExpr *cte = cstate->items[i].cte;
654 
655  cstate->curitem = i;
656  cstate->innerwiths = NIL;
657  makeDependencyGraphWalker((Node *) cte->ctequery, cstate);
658  Assert(cstate->innerwiths == NIL);
659  }
660 
661  TopologicalSort(cstate->pstate, cstate->items, cstate->numitems);
662 }
static void TopologicalSort(ParseState *pstate, CteItem *items, int numitems)
Definition: parse_cte.c:797
static bool makeDependencyGraphWalker(Node *node, CteState *cstate)
Definition: parse_cte.c:669

References Assert, CteItem::cte, CommonTableExpr::ctequery, CteState::curitem, i, CteState::innerwiths, CteState::items, makeDependencyGraphWalker(), NIL, CteState::numitems, CteState::pstate, and TopologicalSort().

Referenced by transformWithClause().

◆ makeDependencyGraphWalker()

static bool makeDependencyGraphWalker ( Node node,
CteState cstate 
)
static

Definition at line 669 of file parse_cte.c.

670 {
671  if (node == NULL)
672  return false;
673  if (IsA(node, RangeVar))
674  {
675  RangeVar *rv = (RangeVar *) node;
676 
677  /* If unqualified name, might be a CTE reference */
678  if (!rv->schemaname)
679  {
680  ListCell *lc;
681  int i;
682 
683  /* ... but first see if it's captured by an inner WITH */
684  foreach(lc, cstate->innerwiths)
685  {
686  List *withlist = (List *) lfirst(lc);
687  ListCell *lc2;
688 
689  foreach(lc2, withlist)
690  {
691  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc2);
692 
693  if (strcmp(rv->relname, cte->ctename) == 0)
694  return false; /* yes, so bail out */
695  }
696  }
697 
698  /* No, could be a reference to the query level we are working on */
699  for (i = 0; i < cstate->numitems; i++)
700  {
701  CommonTableExpr *cte = cstate->items[i].cte;
702 
703  if (strcmp(rv->relname, cte->ctename) == 0)
704  {
705  int myindex = cstate->curitem;
706 
707  if (i != myindex)
708  {
709  /* Add cross-item dependency */
710  cstate->items[myindex].depends_on =
711  bms_add_member(cstate->items[myindex].depends_on,
712  cstate->items[i].id);
713  }
714  else
715  {
716  /* Found out this one is self-referential */
717  cte->cterecursive = true;
718  }
719  break;
720  }
721  }
722  }
723  return false;
724  }
725  if (IsA(node, SelectStmt))
726  {
727  SelectStmt *stmt = (SelectStmt *) node;
728  ListCell *lc;
729 
730  if (stmt->withClause)
731  {
732  if (stmt->withClause->recursive)
733  {
734  /*
735  * In the RECURSIVE case, all query names of the WITH are
736  * visible to all WITH items as well as the main query. So
737  * push them all on, process, pop them all off.
738  */
739  cstate->innerwiths = lcons(stmt->withClause->ctes,
740  cstate->innerwiths);
741  foreach(lc, stmt->withClause->ctes)
742  {
743  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
744 
745  (void) makeDependencyGraphWalker(cte->ctequery, cstate);
746  }
747  (void) raw_expression_tree_walker(node,
749  (void *) cstate);
750  cstate->innerwiths = list_delete_first(cstate->innerwiths);
751  }
752  else
753  {
754  /*
755  * In the non-RECURSIVE case, query names are visible to the
756  * WITH items after them and to the main query.
757  */
758  cstate->innerwiths = lcons(NIL, cstate->innerwiths);
759  foreach(lc, stmt->withClause->ctes)
760  {
761  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
762  ListCell *cell1;
763 
764  (void) makeDependencyGraphWalker(cte->ctequery, cstate);
765  /* note that recursion could mutate innerwiths list */
766  cell1 = list_head(cstate->innerwiths);
767  lfirst(cell1) = lappend((List *) lfirst(cell1), cte);
768  }
769  (void) raw_expression_tree_walker(node,
771  (void *) cstate);
772  cstate->innerwiths = list_delete_first(cstate->innerwiths);
773  }
774  /* We're done examining the SelectStmt */
775  return false;
776  }
777  /* if no WITH clause, just fall through for normal processing */
778  }
779  if (IsA(node, WithClause))
780  {
781  /*
782  * Prevent raw_expression_tree_walker from recursing directly into a
783  * WITH clause. We need that to happen only under the control of the
784  * code above.
785  */
786  return false;
787  }
788  return raw_expression_tree_walker(node,
790  (void *) cstate);
791 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
int id
Definition: parse_cte.c:66
Bitmapset * depends_on
Definition: parse_cte.c:67

References bms_add_member(), CteItem::cte, CommonTableExpr::ctename, CommonTableExpr::ctequery, CteState::curitem, CteItem::depends_on, i, CteItem::id, CteState::innerwiths, IsA, CteState::items, lappend(), lcons(), lfirst, list_delete_first(), list_head(), NIL, CteState::numitems, raw_expression_tree_walker, RangeVar::relname, RangeVar::schemaname, and stmt.

Referenced by makeDependencyGraph().

◆ TopologicalSort()

static void TopologicalSort ( ParseState pstate,
CteItem items,
int  numitems 
)
static

Definition at line 797 of file parse_cte.c.

798 {
799  int i,
800  j;
801 
802  /* for each position in sequence ... */
803  for (i = 0; i < numitems; i++)
804  {
805  /* ... scan the remaining items to find one that has no dependencies */
806  for (j = i; j < numitems; j++)
807  {
808  if (bms_is_empty(items[j].depends_on))
809  break;
810  }
811 
812  /* if we didn't find one, the dependency graph has a cycle */
813  if (j >= numitems)
814  ereport(ERROR,
815  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
816  errmsg("mutual recursion between WITH items is not implemented"),
817  parser_errposition(pstate, items[i].cte->location)));
818 
819  /*
820  * Found one. Move it to front and remove it from every other item's
821  * dependencies.
822  */
823  if (i != j)
824  {
825  CteItem tmp;
826 
827  tmp = items[i];
828  items[i] = items[j];
829  items[j] = tmp;
830  }
831 
832  /*
833  * Items up through i are known to have no dependencies left, so we
834  * can skip them in this loop.
835  */
836  for (j = i + 1; j < numitems; j++)
837  {
838  items[j].depends_on = bms_del_member(items[j].depends_on,
839  items[i].id);
840  }
841  }
842 }
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
#define bms_is_empty(a)
Definition: bitmapset.h:118
static ItemArray items
Definition: test_tidstore.c:48

References bms_del_member(), bms_is_empty, ereport, errcode(), errmsg(), ERROR, i, items, j, and parser_errposition().

Referenced by makeDependencyGraph().

◆ transformWithClause()

List* transformWithClause ( ParseState pstate,
WithClause withClause 
)

Definition at line 109 of file parse_cte.c.

110 {
111  ListCell *lc;
112 
113  /* Only one WITH clause per query level */
114  Assert(pstate->p_ctenamespace == NIL);
115  Assert(pstate->p_future_ctes == NIL);
116 
117  /*
118  * For either type of WITH, there must not be duplicate CTE names in the
119  * list. Check this right away so we needn't worry later.
120  *
121  * Also, tentatively mark each CTE as non-recursive, and initialize its
122  * reference count to zero, and set pstate->p_hasModifyingCTE if needed.
123  */
124  foreach(lc, withClause->ctes)
125  {
126  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
127  ListCell *rest;
128 
129  for_each_cell(rest, withClause->ctes, lnext(withClause->ctes, lc))
130  {
131  CommonTableExpr *cte2 = (CommonTableExpr *) lfirst(rest);
132 
133  if (strcmp(cte->ctename, cte2->ctename) == 0)
134  ereport(ERROR,
135  (errcode(ERRCODE_DUPLICATE_ALIAS),
136  errmsg("WITH query name \"%s\" specified more than once",
137  cte2->ctename),
138  parser_errposition(pstate, cte2->location)));
139  }
140 
141  cte->cterecursive = false;
142  cte->cterefcount = 0;
143 
144  if (!IsA(cte->ctequery, SelectStmt))
145  {
146  /* must be a data-modifying statement */
147  Assert(IsA(cte->ctequery, InsertStmt) ||
148  IsA(cte->ctequery, UpdateStmt) ||
149  IsA(cte->ctequery, DeleteStmt) ||
150  IsA(cte->ctequery, MergeStmt));
151 
152  pstate->p_hasModifyingCTE = true;
153  }
154  }
155 
156  if (withClause->recursive)
157  {
158  /*
159  * For WITH RECURSIVE, we rearrange the list elements if needed to
160  * eliminate forward references. First, build a work array and set up
161  * the data structure needed by the tree walkers.
162  */
163  CteState cstate;
164  int i;
165 
166  cstate.pstate = pstate;
167  cstate.numitems = list_length(withClause->ctes);
168  cstate.items = (CteItem *) palloc0(cstate.numitems * sizeof(CteItem));
169  i = 0;
170  foreach(lc, withClause->ctes)
171  {
172  cstate.items[i].cte = (CommonTableExpr *) lfirst(lc);
173  cstate.items[i].id = i;
174  i++;
175  }
176 
177  /*
178  * Find all the dependencies and sort the CteItems into a safe
179  * processing order. Also, mark CTEs that contain self-references.
180  */
181  makeDependencyGraph(&cstate);
182 
183  /*
184  * Check that recursive queries are well-formed.
185  */
186  checkWellFormedRecursion(&cstate);
187 
188  /*
189  * Set up the ctenamespace for parse analysis. Per spec, all the WITH
190  * items are visible to all others, so stuff them all in before parse
191  * analysis. We build the list in safe processing order so that the
192  * planner can process the queries in sequence.
193  */
194  for (i = 0; i < cstate.numitems; i++)
195  {
196  CommonTableExpr *cte = cstate.items[i].cte;
197 
198  pstate->p_ctenamespace = lappend(pstate->p_ctenamespace, cte);
199  }
200 
201  /*
202  * Do parse analysis in the order determined by the topological sort.
203  */
204  for (i = 0; i < cstate.numitems; i++)
205  {
206  CommonTableExpr *cte = cstate.items[i].cte;
207 
208  analyzeCTE(pstate, cte);
209  }
210  }
211  else
212  {
213  /*
214  * For non-recursive WITH, just analyze each CTE in sequence and then
215  * add it to the ctenamespace. This corresponds to the spec's
216  * definition of the scope of each WITH name. However, to allow error
217  * reports to be aware of the possibility of an erroneous reference,
218  * we maintain a list in p_future_ctes of the not-yet-visible CTEs.
219  */
220  pstate->p_future_ctes = list_copy(withClause->ctes);
221 
222  foreach(lc, withClause->ctes)
223  {
224  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
225 
226  analyzeCTE(pstate, cte);
227  pstate->p_ctenamespace = lappend(pstate->p_ctenamespace, cte);
228  pstate->p_future_ctes = list_delete_first(pstate->p_future_ctes);
229  }
230  }
231 
232  return pstate->p_ctenamespace;
233 }
List * list_copy(const List *oldlist)
Definition: list.c:1573
void * palloc0(Size size)
Definition: mcxt.c:1347
static void makeDependencyGraph(CteState *cstate)
Definition: parse_cte.c:647
static void checkWellFormedRecursion(CteState *cstate)
Definition: parse_cte.c:849
static void analyzeCTE(ParseState *pstate, CommonTableExpr *cte)
Definition: parse_cte.c:242
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:438
List * p_ctenamespace
Definition: parse_node.h:222
bool p_hasModifyingCTE
Definition: parse_node.h:246
List * p_future_ctes
Definition: parse_node.h:223
List * ctes
Definition: parsenodes.h:1595
bool recursive
Definition: parsenodes.h:1596

References analyzeCTE(), Assert, checkWellFormedRecursion(), CteItem::cte, CommonTableExpr::ctename, CommonTableExpr::ctequery, WithClause::ctes, ereport, errcode(), errmsg(), ERROR, for_each_cell, i, CteItem::id, IsA, CteState::items, lappend(), lfirst, list_copy(), list_delete_first(), list_length(), lnext(), CommonTableExpr::location, makeDependencyGraph(), NIL, CteState::numitems, ParseState::p_ctenamespace, ParseState::p_future_ctes, ParseState::p_hasModifyingCTE, palloc0(), parser_errposition(), CteState::pstate, and WithClause::recursive.

Referenced by transformDeleteStmt(), transformInsertStmt(), transformMergeStmt(), transformSelectStmt(), transformSetOperationStmt(), transformUpdateStmt(), and transformValuesClause().

Variable Documentation

◆ recursion_errormsgs

const char* const recursion_errormsgs[]
static
Initial value:
= {
NULL,
gettext_noop("recursive reference to query \"%s\" must not appear within its non-recursive term"),
gettext_noop("recursive reference to query \"%s\" must not appear within a subquery"),
gettext_noop("recursive reference to query \"%s\" must not appear within an outer join"),
gettext_noop("recursive reference to query \"%s\" must not appear within INTERSECT"),
gettext_noop("recursive reference to query \"%s\" must not appear within EXCEPT")
}
#define gettext_noop(x)
Definition: c.h:1201

Definition at line 42 of file parse_cte.c.

Referenced by checkWellFormedRecursionWalker().