PostgreSQL Source Code  git master
parse_relation.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * parse_relation.c
4  * parser support routines dealing with relations
5  *
6  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/parser/parse_relation.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <ctype.h>
18 
19 #include "access/htup_details.h"
20 #include "access/relation.h"
21 #include "access/sysattr.h"
22 #include "access/table.h"
23 #include "catalog/heap.h"
24 #include "catalog/namespace.h"
25 #include "catalog/pg_type.h"
26 #include "funcapi.h"
27 #include "nodes/makefuncs.h"
28 #include "nodes/nodeFuncs.h"
29 #include "parser/parse_enr.h"
30 #include "parser/parse_relation.h"
31 #include "parser/parse_type.h"
32 #include "parser/parsetree.h"
33 #include "storage/lmgr.h"
34 #include "utils/builtins.h"
35 #include "utils/lsyscache.h"
36 #include "utils/rel.h"
37 #include "utils/syscache.h"
38 #include "utils/varlena.h"
39 
40 
41 /*
42  * Support for fuzzily matching columns.
43  *
44  * This is for building diagnostic messages, where non-exact matching
45  * attributes are suggested to the user. The struct's fields may be facets of
46  * a particular RTE, or of an entire range table, depending on context.
47  */
48 typedef struct
49 {
50  int distance; /* Weighted distance (lowest so far) */
51  RangeTblEntry *rfirst; /* RTE of first */
52  AttrNumber first; /* Closest attribute so far */
53  RangeTblEntry *rsecond; /* RTE of second */
54  AttrNumber second; /* Second closest attribute so far */
56 
57 #define MAX_FUZZY_DISTANCE 3
58 
59 
61  const char *refname,
62  int location);
64  int location);
65 static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem,
66  int location);
67 static int scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte,
68  Alias *eref,
69  const char *colname, int location,
70  int fuzzy_rte_penalty,
71  FuzzyAttrMatchState *fuzzystate);
72 static void markRTEForSelectPriv(ParseState *pstate,
73  int rtindex, AttrNumber col);
74 static void expandRelation(Oid relid, Alias *eref,
75  int rtindex, int sublevels_up,
76  int location, bool include_dropped,
77  List **colnames, List **colvars);
78 static void expandTupleDesc(TupleDesc tupdesc, Alias *eref,
79  int count, int offset,
80  int rtindex, int sublevels_up,
81  int location, bool include_dropped,
82  List **colnames, List **colvars);
83 static int specialAttNum(const char *attname);
84 static bool isQueryUsingTempRelation_walker(Node *node, void *context);
85 
86 
87 /*
88  * refnameNamespaceItem
89  * Given a possibly-qualified refname, look to see if it matches any visible
90  * namespace item. If so, return a pointer to the nsitem; else return NULL.
91  *
92  * Optionally get nsitem's nesting depth (0 = current) into *sublevels_up.
93  * If sublevels_up is NULL, only consider items at the current nesting
94  * level.
95  *
96  * An unqualified refname (schemaname == NULL) can match any item with matching
97  * alias, or matching unqualified relname in the case of alias-less relation
98  * items. It is possible that such a refname matches multiple items in the
99  * nearest nesting level that has a match; if so, we report an error via
100  * ereport().
101  *
102  * A qualified refname (schemaname != NULL) can only match a relation item
103  * that (a) has no alias and (b) is for the same relation identified by
104  * schemaname.refname. In this case we convert schemaname.refname to a
105  * relation OID and search by relid, rather than by alias name. This is
106  * peculiar, but it's what SQL says to do.
107  */
110  const char *schemaname,
111  const char *refname,
112  int location,
113  int *sublevels_up)
114 {
115  Oid relId = InvalidOid;
116 
117  if (sublevels_up)
118  *sublevels_up = 0;
119 
120  if (schemaname != NULL)
121  {
122  Oid namespaceId;
123 
124  /*
125  * We can use LookupNamespaceNoError() here because we are only
126  * interested in finding existing RTEs. Checking USAGE permission on
127  * the schema is unnecessary since it would have already been checked
128  * when the RTE was made. Furthermore, we want to report "RTE not
129  * found", not "no permissions for schema", if the name happens to
130  * match a schema name the user hasn't got access to.
131  */
132  namespaceId = LookupNamespaceNoError(schemaname);
133  if (!OidIsValid(namespaceId))
134  return NULL;
135  relId = get_relname_relid(refname, namespaceId);
136  if (!OidIsValid(relId))
137  return NULL;
138  }
139 
140  while (pstate != NULL)
141  {
142  ParseNamespaceItem *result;
143 
144  if (OidIsValid(relId))
145  result = scanNameSpaceForRelid(pstate, relId, location);
146  else
147  result = scanNameSpaceForRefname(pstate, refname, location);
148 
149  if (result)
150  return result;
151 
152  if (sublevels_up)
153  (*sublevels_up)++;
154  else
155  break;
156 
157  pstate = pstate->parentParseState;
158  }
159  return NULL;
160 }
161 
162 /*
163  * Search the query's table namespace for an item matching the
164  * given unqualified refname. Return the nsitem if a unique match, or NULL
165  * if no match. Raise error if multiple matches.
166  *
167  * Note: it might seem that we shouldn't have to worry about the possibility
168  * of multiple matches; after all, the SQL standard disallows duplicate table
169  * aliases within a given SELECT level. Historically, however, Postgres has
170  * been laxer than that. For example, we allow
171  * SELECT ... FROM tab1 x CROSS JOIN (tab2 x CROSS JOIN tab3 y) z
172  * on the grounds that the aliased join (z) hides the aliases within it,
173  * therefore there is no conflict between the two RTEs named "x". However,
174  * if tab3 is a LATERAL subquery, then from within the subquery both "x"es
175  * are visible. Rather than rejecting queries that used to work, we allow
176  * this situation, and complain only if there's actually an ambiguous
177  * reference to "x".
178  */
179 static ParseNamespaceItem *
180 scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
181 {
182  ParseNamespaceItem *result = NULL;
183  ListCell *l;
184 
185  foreach(l, pstate->p_namespace)
186  {
188 
189  /* Ignore columns-only items */
190  if (!nsitem->p_rel_visible)
191  continue;
192  /* If not inside LATERAL, ignore lateral-only items */
193  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
194  continue;
195 
196  if (strcmp(nsitem->p_names->aliasname, refname) == 0)
197  {
198  if (result)
199  ereport(ERROR,
200  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
201  errmsg("table reference \"%s\" is ambiguous",
202  refname),
203  parser_errposition(pstate, location)));
204  check_lateral_ref_ok(pstate, nsitem, location);
205  result = nsitem;
206  }
207  }
208  return result;
209 }
210 
211 /*
212  * Search the query's table namespace for a relation item matching the
213  * given relation OID. Return the nsitem if a unique match, or NULL
214  * if no match. Raise error if multiple matches.
215  *
216  * See the comments for refnameNamespaceItem to understand why this
217  * acts the way it does.
218  */
219 static ParseNamespaceItem *
220 scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
221 {
222  ParseNamespaceItem *result = NULL;
223  ListCell *l;
224 
225  foreach(l, pstate->p_namespace)
226  {
228  RangeTblEntry *rte = nsitem->p_rte;
229 
230  /* Ignore columns-only items */
231  if (!nsitem->p_rel_visible)
232  continue;
233  /* If not inside LATERAL, ignore lateral-only items */
234  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
235  continue;
236 
237  /* yes, the test for alias == NULL should be there... */
238  if (rte->rtekind == RTE_RELATION &&
239  rte->relid == relid &&
240  rte->alias == NULL)
241  {
242  if (result)
243  ereport(ERROR,
244  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
245  errmsg("table reference %u is ambiguous",
246  relid),
247  parser_errposition(pstate, location)));
248  check_lateral_ref_ok(pstate, nsitem, location);
249  result = nsitem;
250  }
251  }
252  return result;
253 }
254 
255 /*
256  * Search the query's CTE namespace for a CTE matching the given unqualified
257  * refname. Return the CTE (and its levelsup count) if a match, or NULL
258  * if no match. We need not worry about multiple matches, since parse_cte.c
259  * rejects WITH lists containing duplicate CTE names.
260  */
262 scanNameSpaceForCTE(ParseState *pstate, const char *refname,
263  Index *ctelevelsup)
264 {
265  Index levelsup;
266 
267  for (levelsup = 0;
268  pstate != NULL;
269  pstate = pstate->parentParseState, levelsup++)
270  {
271  ListCell *lc;
272 
273  foreach(lc, pstate->p_ctenamespace)
274  {
275  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
276 
277  if (strcmp(cte->ctename, refname) == 0)
278  {
279  *ctelevelsup = levelsup;
280  return cte;
281  }
282  }
283  }
284  return NULL;
285 }
286 
287 /*
288  * Search for a possible "future CTE", that is one that is not yet in scope
289  * according to the WITH scoping rules. This has nothing to do with valid
290  * SQL semantics, but it's important for error reporting purposes.
291  */
292 static bool
293 isFutureCTE(ParseState *pstate, const char *refname)
294 {
295  for (; pstate != NULL; pstate = pstate->parentParseState)
296  {
297  ListCell *lc;
298 
299  foreach(lc, pstate->p_future_ctes)
300  {
301  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
302 
303  if (strcmp(cte->ctename, refname) == 0)
304  return true;
305  }
306  }
307  return false;
308 }
309 
310 /*
311  * Search the query's ephemeral named relation namespace for a relation
312  * matching the given unqualified refname.
313  */
314 bool
315 scanNameSpaceForENR(ParseState *pstate, const char *refname)
316 {
317  return name_matches_visible_ENR(pstate, refname);
318 }
319 
320 /*
321  * searchRangeTableForRel
322  * See if any RangeTblEntry could possibly match the RangeVar.
323  * If so, return a pointer to the RangeTblEntry; else return NULL.
324  *
325  * This is different from refnameNamespaceItem in that it considers every
326  * entry in the ParseState's rangetable(s), not only those that are currently
327  * visible in the p_namespace list(s). This behavior is invalid per the SQL
328  * spec, and it may give ambiguous results (there might be multiple equally
329  * valid matches, but only one will be returned). This must be used ONLY
330  * as a heuristic in giving suitable error messages. See errorMissingRTE.
331  *
332  * Notice that we consider both matches on actual relation (or CTE) name
333  * and matches on alias.
334  */
335 static RangeTblEntry *
337 {
338  const char *refname = relation->relname;
339  Oid relId = InvalidOid;
340  CommonTableExpr *cte = NULL;
341  bool isenr = false;
342  Index ctelevelsup = 0;
343  Index levelsup;
344 
345  /*
346  * If it's an unqualified name, check for possible CTE matches. A CTE
347  * hides any real relation matches. If no CTE, look for a matching
348  * relation.
349  *
350  * NB: It's not critical that RangeVarGetRelid return the correct answer
351  * here in the face of concurrent DDL. If it doesn't, the worst case
352  * scenario is a less-clear error message. Also, the tables involved in
353  * the query are already locked, which reduces the number of cases in
354  * which surprising behavior can occur. So we do the name lookup
355  * unlocked.
356  */
357  if (!relation->schemaname)
358  {
359  cte = scanNameSpaceForCTE(pstate, refname, &ctelevelsup);
360  if (!cte)
361  isenr = scanNameSpaceForENR(pstate, refname);
362  }
363 
364  if (!cte && !isenr)
365  relId = RangeVarGetRelid(relation, NoLock, true);
366 
367  /* Now look for RTEs matching either the relation/CTE/ENR or the alias */
368  for (levelsup = 0;
369  pstate != NULL;
370  pstate = pstate->parentParseState, levelsup++)
371  {
372  ListCell *l;
373 
374  foreach(l, pstate->p_rtable)
375  {
376  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
377 
378  if (rte->rtekind == RTE_RELATION &&
379  OidIsValid(relId) &&
380  rte->relid == relId)
381  return rte;
382  if (rte->rtekind == RTE_CTE &&
383  cte != NULL &&
384  rte->ctelevelsup + levelsup == ctelevelsup &&
385  strcmp(rte->ctename, refname) == 0)
386  return rte;
387  if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
388  isenr &&
389  strcmp(rte->enrname, refname) == 0)
390  return rte;
391  if (strcmp(rte->eref->aliasname, refname) == 0)
392  return rte;
393  }
394  }
395  return NULL;
396 }
397 
398 /*
399  * Check for relation-name conflicts between two namespace lists.
400  * Raise an error if any is found.
401  *
402  * Note: we assume that each given argument does not contain conflicts
403  * itself; we just want to know if the two can be merged together.
404  *
405  * Per SQL, two alias-less plain relation RTEs do not conflict even if
406  * they have the same eref->aliasname (ie, same relation name), if they
407  * are for different relation OIDs (implying they are in different schemas).
408  *
409  * We ignore the lateral-only flags in the namespace items: the lists must
410  * not conflict, even when all items are considered visible. However,
411  * columns-only items should be ignored.
412  */
413 void
415  List *namespace2)
416 {
417  ListCell *l1;
418 
419  foreach(l1, namespace1)
420  {
421  ParseNamespaceItem *nsitem1 = (ParseNamespaceItem *) lfirst(l1);
422  RangeTblEntry *rte1 = nsitem1->p_rte;
423  const char *aliasname1 = nsitem1->p_names->aliasname;
424  ListCell *l2;
425 
426  if (!nsitem1->p_rel_visible)
427  continue;
428 
429  foreach(l2, namespace2)
430  {
431  ParseNamespaceItem *nsitem2 = (ParseNamespaceItem *) lfirst(l2);
432  RangeTblEntry *rte2 = nsitem2->p_rte;
433  const char *aliasname2 = nsitem2->p_names->aliasname;
434 
435  if (!nsitem2->p_rel_visible)
436  continue;
437  if (strcmp(aliasname2, aliasname1) != 0)
438  continue; /* definitely no conflict */
439  if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
440  rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
441  rte1->relid != rte2->relid)
442  continue; /* no conflict per SQL rule */
443  ereport(ERROR,
444  (errcode(ERRCODE_DUPLICATE_ALIAS),
445  errmsg("table name \"%s\" specified more than once",
446  aliasname1)));
447  }
448  }
449 }
450 
451 /*
452  * Complain if a namespace item is currently disallowed as a LATERAL reference.
453  * This enforces both SQL:2008's rather odd idea of what to do with a LATERAL
454  * reference to the wrong side of an outer join, and our own prohibition on
455  * referencing the target table of an UPDATE or DELETE as a lateral reference
456  * in a FROM/USING clause.
457  *
458  * Note: the pstate should be the same query level the nsitem was found in.
459  *
460  * Convenience subroutine to avoid multiple copies of a rather ugly ereport.
461  */
462 static void
464  int location)
465 {
466  if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
467  {
468  /* SQL:2008 demands this be an error, not an invisible item */
469  RangeTblEntry *rte = nsitem->p_rte;
470  char *refname = nsitem->p_names->aliasname;
471 
472  ereport(ERROR,
473  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
474  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
475  refname),
476  (pstate->p_target_nsitem != NULL &&
477  rte == pstate->p_target_nsitem->p_rte) ?
478  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
479  refname) :
480  errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
481  parser_errposition(pstate, location)));
482  }
483 }
484 
485 /*
486  * Given an RT index and nesting depth, find the corresponding
487  * ParseNamespaceItem (there must be one).
488  */
491  int varno,
492  int sublevels_up)
493 {
494  ListCell *lc;
495 
496  while (sublevels_up-- > 0)
497  {
498  pstate = pstate->parentParseState;
499  Assert(pstate != NULL);
500  }
501  foreach(lc, pstate->p_namespace)
502  {
503  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
504 
505  if (nsitem->p_rtindex == varno)
506  return nsitem;
507  }
508  elog(ERROR, "nsitem not found (internal error)");
509  return NULL; /* keep compiler quiet */
510 }
511 
512 /*
513  * Given an RT index and nesting depth, find the corresponding RTE.
514  * (Note that the RTE need not be in the query's namespace.)
515  */
518  int varno,
519  int sublevels_up)
520 {
521  while (sublevels_up-- > 0)
522  {
523  pstate = pstate->parentParseState;
524  Assert(pstate != NULL);
525  }
526  Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
527  return rt_fetch(varno, pstate->p_rtable);
528 }
529 
530 /*
531  * Fetch the CTE for a CTE-reference RTE.
532  *
533  * rtelevelsup is the number of query levels above the given pstate that the
534  * RTE came from.
535  */
537 GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
538 {
539  Index levelsup;
540  ListCell *lc;
541 
542  Assert(rte->rtekind == RTE_CTE);
543  levelsup = rte->ctelevelsup + rtelevelsup;
544  while (levelsup-- > 0)
545  {
546  pstate = pstate->parentParseState;
547  if (!pstate) /* shouldn't happen */
548  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
549  }
550  foreach(lc, pstate->p_ctenamespace)
551  {
552  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
553 
554  if (strcmp(cte->ctename, rte->ctename) == 0)
555  return cte;
556  }
557  /* shouldn't happen */
558  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
559  return NULL; /* keep compiler quiet */
560 }
561 
562 /*
563  * updateFuzzyAttrMatchState
564  * Using Levenshtein distance, consider if column is best fuzzy match.
565  */
566 static void
567 updateFuzzyAttrMatchState(int fuzzy_rte_penalty,
568  FuzzyAttrMatchState *fuzzystate, RangeTblEntry *rte,
569  const char *actual, const char *match, int attnum)
570 {
571  int columndistance;
572  int matchlen;
573 
574  /* Bail before computing the Levenshtein distance if there's no hope. */
575  if (fuzzy_rte_penalty > fuzzystate->distance)
576  return;
577 
578  /*
579  * Outright reject dropped columns, which can appear here with apparent
580  * empty actual names, per remarks within scanRTEForColumn().
581  */
582  if (actual[0] == '\0')
583  return;
584 
585  /* Use Levenshtein to compute match distance. */
586  matchlen = strlen(match);
587  columndistance =
588  varstr_levenshtein_less_equal(actual, strlen(actual), match, matchlen,
589  1, 1, 1,
590  fuzzystate->distance + 1
591  - fuzzy_rte_penalty,
592  true);
593 
594  /*
595  * If more than half the characters are different, don't treat it as a
596  * match, to avoid making ridiculous suggestions.
597  */
598  if (columndistance > matchlen / 2)
599  return;
600 
601  /*
602  * From this point on, we can ignore the distinction between the RTE-name
603  * distance and the column-name distance.
604  */
605  columndistance += fuzzy_rte_penalty;
606 
607  /*
608  * If the new distance is less than or equal to that of the best match
609  * found so far, update fuzzystate.
610  */
611  if (columndistance < fuzzystate->distance)
612  {
613  /* Store new lowest observed distance for RTE */
614  fuzzystate->distance = columndistance;
615  fuzzystate->rfirst = rte;
616  fuzzystate->first = attnum;
617  fuzzystate->rsecond = NULL;
618  fuzzystate->second = InvalidAttrNumber;
619  }
620  else if (columndistance == fuzzystate->distance)
621  {
622  /*
623  * This match distance may equal a prior match within this same range
624  * table. When that happens, the prior match may also be given, but
625  * only if there is no more than two equally distant matches from the
626  * RTE (in turn, our caller will only accept two equally distant
627  * matches overall).
628  */
629  if (AttributeNumberIsValid(fuzzystate->second))
630  {
631  /* Too many RTE-level matches */
632  fuzzystate->rfirst = NULL;
633  fuzzystate->first = InvalidAttrNumber;
634  fuzzystate->rsecond = NULL;
635  fuzzystate->second = InvalidAttrNumber;
636  /* Clearly, distance is too low a bar (for *any* RTE) */
637  fuzzystate->distance = columndistance - 1;
638  }
639  else if (AttributeNumberIsValid(fuzzystate->first))
640  {
641  /* Record as provisional second match for RTE */
642  fuzzystate->rsecond = rte;
643  fuzzystate->second = attnum;
644  }
645  else if (fuzzystate->distance <= MAX_FUZZY_DISTANCE)
646  {
647  /*
648  * Record as provisional first match (this can occasionally occur
649  * because previous lowest distance was "too low a bar", rather
650  * than being associated with a real match)
651  */
652  fuzzystate->rfirst = rte;
653  fuzzystate->first = attnum;
654  }
655  }
656 }
657 
658 /*
659  * scanNSItemForColumn
660  * Search the column names of a single namespace item for the given name.
661  * If found, return an appropriate Var node, else return NULL.
662  * If the name proves ambiguous within this nsitem, raise error.
663  *
664  * Side effect: if we find a match, mark the corresponding RTE as requiring
665  * read access for the column.
666  */
667 Node *
669  int sublevels_up, const char *colname, int location)
670 {
671  RangeTblEntry *rte = nsitem->p_rte;
672  int attnum;
673  Var *var;
674 
675  /*
676  * Scan the nsitem's column names (or aliases) for a match. Complain if
677  * multiple matches.
678  */
679  attnum = scanRTEForColumn(pstate, rte, nsitem->p_names,
680  colname, location,
681  0, NULL);
682 
683  if (attnum == InvalidAttrNumber)
684  return NULL; /* Return NULL if no match */
685 
686  /* In constraint check, no system column is allowed except tableOid */
687  if (pstate->p_expr_kind == EXPR_KIND_CHECK_CONSTRAINT &&
689  ereport(ERROR,
690  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
691  errmsg("system column \"%s\" reference in check constraint is invalid",
692  colname),
693  parser_errposition(pstate, location)));
694 
695  /* In generated column, no system column is allowed except tableOid */
696  if (pstate->p_expr_kind == EXPR_KIND_GENERATED_COLUMN &&
698  ereport(ERROR,
699  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
700  errmsg("cannot use system column \"%s\" in column generation expression",
701  colname),
702  parser_errposition(pstate, location)));
703 
704  /*
705  * In a MERGE WHEN condition, no system column is allowed except tableOid
706  */
707  if (pstate->p_expr_kind == EXPR_KIND_MERGE_WHEN &&
709  ereport(ERROR,
710  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
711  errmsg("cannot use system column \"%s\" in MERGE WHEN condition",
712  colname),
713  parser_errposition(pstate, location)));
714 
715  /* Found a valid match, so build a Var */
717  {
718  /* Get attribute data from the ParseNamespaceColumn array */
719  ParseNamespaceColumn *nscol = &nsitem->p_nscolumns[attnum - 1];
720 
721  /* Complain if dropped column. See notes in scanRTEForColumn. */
722  if (nscol->p_varno == 0)
723  ereport(ERROR,
724  (errcode(ERRCODE_UNDEFINED_COLUMN),
725  errmsg("column \"%s\" of relation \"%s\" does not exist",
726  colname,
727  nsitem->p_names->aliasname)));
728 
729  var = makeVar(nscol->p_varno,
730  nscol->p_varattno,
731  nscol->p_vartype,
732  nscol->p_vartypmod,
733  nscol->p_varcollid,
734  sublevels_up);
735  /* makeVar doesn't offer parameters for these, so set them by hand: */
736  var->varnosyn = nscol->p_varnosyn;
737  var->varattnosyn = nscol->p_varattnosyn;
738  }
739  else
740  {
741  /* System column, so use predetermined type data */
742  const FormData_pg_attribute *sysatt;
743 
745  var = makeVar(nsitem->p_rtindex,
746  attnum,
747  sysatt->atttypid,
748  sysatt->atttypmod,
749  sysatt->attcollation,
750  sublevels_up);
751  }
752  var->location = location;
753 
754  /* Require read access to the column */
755  markVarForSelectPriv(pstate, var);
756 
757  return (Node *) var;
758 }
759 
760 /*
761  * scanRTEForColumn
762  * Search the column names of a single RTE for the given name.
763  * If found, return the attnum (possibly negative, for a system column);
764  * else return InvalidAttrNumber.
765  * If the name proves ambiguous within this RTE, raise error.
766  *
767  * Actually, we only search the names listed in "eref". This can be either
768  * rte->eref, in which case we are indeed searching all the column names,
769  * or for a join it can be rte->join_using_alias, in which case we are only
770  * considering the common column names (which are the first N columns of the
771  * join, so everything works).
772  *
773  * pstate and location are passed only for error-reporting purposes.
774  *
775  * Side effect: if fuzzystate is non-NULL, check non-system columns
776  * for an approximate match and update fuzzystate accordingly.
777  *
778  * Note: this is factored out of scanNSItemForColumn because error message
779  * creation may want to check RTEs that are not in the namespace. To support
780  * that usage, minimize the number of validity checks performed here. It's
781  * okay to complain about ambiguous-name cases, though, since if we are
782  * working to complain about an invalid name, we've already eliminated that.
783  */
784 static int
786  Alias *eref,
787  const char *colname, int location,
788  int fuzzy_rte_penalty,
789  FuzzyAttrMatchState *fuzzystate)
790 {
791  int result = InvalidAttrNumber;
792  int attnum = 0;
793  ListCell *c;
794 
795  /*
796  * Scan the user column names (or aliases) for a match. Complain if
797  * multiple matches.
798  *
799  * Note: eref->colnames may include entries for dropped columns, but those
800  * will be empty strings that cannot match any legal SQL identifier, so we
801  * don't bother to test for that case here.
802  *
803  * Should this somehow go wrong and we try to access a dropped column,
804  * we'll still catch it by virtue of the check in scanNSItemForColumn().
805  * Callers interested in finding match with shortest distance need to
806  * defend against this directly, though.
807  */
808  foreach(c, eref->colnames)
809  {
810  const char *attcolname = strVal(lfirst(c));
811 
812  attnum++;
813  if (strcmp(attcolname, colname) == 0)
814  {
815  if (result)
816  ereport(ERROR,
817  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
818  errmsg("column reference \"%s\" is ambiguous",
819  colname),
820  parser_errposition(pstate, location)));
821  result = attnum;
822  }
823 
824  /* Update fuzzy match state, if provided. */
825  if (fuzzystate != NULL)
826  updateFuzzyAttrMatchState(fuzzy_rte_penalty, fuzzystate,
827  rte, attcolname, colname, attnum);
828  }
829 
830  /*
831  * If we have a unique match, return it. Note that this allows a user
832  * alias to override a system column name (such as OID) without error.
833  */
834  if (result)
835  return result;
836 
837  /*
838  * If the RTE represents a real relation, consider system column names.
839  * Composites are only used for pseudo-relations like ON CONFLICT's
840  * excluded.
841  */
842  if (rte->rtekind == RTE_RELATION &&
843  rte->relkind != RELKIND_COMPOSITE_TYPE)
844  {
845  /* quick check to see if name could be a system column */
846  attnum = specialAttNum(colname);
847  if (attnum != InvalidAttrNumber)
848  {
849  /* now check to see if column actually is defined */
851  ObjectIdGetDatum(rte->relid),
853  result = attnum;
854  }
855  }
856 
857  return result;
858 }
859 
860 /*
861  * colNameToVar
862  * Search for an unqualified column name.
863  * If found, return the appropriate Var node (or expression).
864  * If not found, return NULL. If the name proves ambiguous, raise error.
865  * If localonly is true, only names in the innermost query are considered.
866  */
867 Node *
868 colNameToVar(ParseState *pstate, const char *colname, bool localonly,
869  int location)
870 {
871  Node *result = NULL;
872  int sublevels_up = 0;
873  ParseState *orig_pstate = pstate;
874 
875  while (pstate != NULL)
876  {
877  ListCell *l;
878 
879  foreach(l, pstate->p_namespace)
880  {
882  Node *newresult;
883 
884  /* Ignore table-only items */
885  if (!nsitem->p_cols_visible)
886  continue;
887  /* If not inside LATERAL, ignore lateral-only items */
888  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
889  continue;
890 
891  /* use orig_pstate here for consistency with other callers */
892  newresult = scanNSItemForColumn(orig_pstate, nsitem, sublevels_up,
893  colname, location);
894 
895  if (newresult)
896  {
897  if (result)
898  ereport(ERROR,
899  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
900  errmsg("column reference \"%s\" is ambiguous",
901  colname),
902  parser_errposition(pstate, location)));
903  check_lateral_ref_ok(pstate, nsitem, location);
904  result = newresult;
905  }
906  }
907 
908  if (result != NULL || localonly)
909  break; /* found, or don't want to look at parent */
910 
911  pstate = pstate->parentParseState;
912  sublevels_up++;
913  }
914 
915  return result;
916 }
917 
918 /*
919  * searchRangeTableForCol
920  * See if any RangeTblEntry could possibly provide the given column name (or
921  * find the best match available). Returns state with relevant details.
922  *
923  * This is different from colNameToVar in that it considers every entry in
924  * the ParseState's rangetable(s), not only those that are currently visible
925  * in the p_namespace list(s). This behavior is invalid per the SQL spec,
926  * and it may give ambiguous results (there might be multiple equally valid
927  * matches, but only one will be returned). This must be used ONLY as a
928  * heuristic in giving suitable error messages. See errorMissingColumn.
929  *
930  * This function is also different in that it will consider approximate
931  * matches -- if the user entered an alias/column pair that is only slightly
932  * different from a valid pair, we may be able to infer what they meant to
933  * type and provide a reasonable hint.
934  *
935  * The FuzzyAttrMatchState will have 'rfirst' pointing to the best RTE
936  * containing the most promising match for the alias and column name. If
937  * the alias and column names match exactly, 'first' will be InvalidAttrNumber;
938  * otherwise, it will be the attribute number for the match. In the latter
939  * case, 'rsecond' may point to a second, equally close approximate match,
940  * and 'second' will contain the attribute number for the second match.
941  */
942 static FuzzyAttrMatchState *
943 searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colname,
944  int location)
945 {
946  ParseState *orig_pstate = pstate;
947  FuzzyAttrMatchState *fuzzystate = palloc(sizeof(FuzzyAttrMatchState));
948 
949  fuzzystate->distance = MAX_FUZZY_DISTANCE + 1;
950  fuzzystate->rfirst = NULL;
951  fuzzystate->rsecond = NULL;
952  fuzzystate->first = InvalidAttrNumber;
953  fuzzystate->second = InvalidAttrNumber;
954 
955  while (pstate != NULL)
956  {
957  ListCell *l;
958 
959  foreach(l, pstate->p_rtable)
960  {
961  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
962  int fuzzy_rte_penalty = 0;
963 
964  /*
965  * Typically, it is not useful to look for matches within join
966  * RTEs; they effectively duplicate other RTEs for our purposes,
967  * and if a match is chosen from a join RTE, an unhelpful alias is
968  * displayed in the final diagnostic message.
969  */
970  if (rte->rtekind == RTE_JOIN)
971  continue;
972 
973  /*
974  * If the user didn't specify an alias, then matches against one
975  * RTE are as good as another. But if the user did specify an
976  * alias, then we want at least a fuzzy - and preferably an exact
977  * - match for the range table entry.
978  */
979  if (alias != NULL)
980  fuzzy_rte_penalty =
981  varstr_levenshtein_less_equal(alias, strlen(alias),
982  rte->eref->aliasname,
983  strlen(rte->eref->aliasname),
984  1, 1, 1,
985  MAX_FUZZY_DISTANCE + 1,
986  true);
987 
988  /*
989  * Scan for a matching column; if we find an exact match, we're
990  * done. Otherwise, update fuzzystate.
991  */
992  if (scanRTEForColumn(orig_pstate, rte, rte->eref, colname, location,
993  fuzzy_rte_penalty, fuzzystate)
994  && fuzzy_rte_penalty == 0)
995  {
996  fuzzystate->rfirst = rte;
997  fuzzystate->first = InvalidAttrNumber;
998  fuzzystate->rsecond = NULL;
999  fuzzystate->second = InvalidAttrNumber;
1000  return fuzzystate;
1001  }
1002  }
1003 
1004  pstate = pstate->parentParseState;
1005  }
1006 
1007  return fuzzystate;
1008 }
1009 
1010 /*
1011  * markRTEForSelectPriv
1012  * Mark the specified column of the RTE with index rtindex
1013  * as requiring SELECT privilege
1014  *
1015  * col == InvalidAttrNumber means a "whole row" reference
1016  */
1017 static void
1018 markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
1019 {
1020  RangeTblEntry *rte = rt_fetch(rtindex, pstate->p_rtable);
1021 
1022  if (rte->rtekind == RTE_RELATION)
1023  {
1024  /* Make sure the rel as a whole is marked for SELECT access */
1025  rte->requiredPerms |= ACL_SELECT;
1026  /* Must offset the attnum to fit in a bitmapset */
1029  }
1030  else if (rte->rtekind == RTE_JOIN)
1031  {
1032  if (col == InvalidAttrNumber)
1033  {
1034  /*
1035  * A whole-row reference to a join has to be treated as whole-row
1036  * references to the two inputs.
1037  */
1038  JoinExpr *j;
1039 
1040  if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
1041  j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
1042  else
1043  j = NULL;
1044  if (j == NULL)
1045  elog(ERROR, "could not find JoinExpr for whole-row reference");
1046 
1047  /* Note: we can't see FromExpr here */
1048  if (IsA(j->larg, RangeTblRef))
1049  {
1050  int varno = ((RangeTblRef *) j->larg)->rtindex;
1051 
1052  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1053  }
1054  else if (IsA(j->larg, JoinExpr))
1055  {
1056  int varno = ((JoinExpr *) j->larg)->rtindex;
1057 
1058  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1059  }
1060  else
1061  elog(ERROR, "unrecognized node type: %d",
1062  (int) nodeTag(j->larg));
1063  if (IsA(j->rarg, RangeTblRef))
1064  {
1065  int varno = ((RangeTblRef *) j->rarg)->rtindex;
1066 
1067  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1068  }
1069  else if (IsA(j->rarg, JoinExpr))
1070  {
1071  int varno = ((JoinExpr *) j->rarg)->rtindex;
1072 
1073  markRTEForSelectPriv(pstate, varno, InvalidAttrNumber);
1074  }
1075  else
1076  elog(ERROR, "unrecognized node type: %d",
1077  (int) nodeTag(j->rarg));
1078  }
1079  else
1080  {
1081  /*
1082  * Join alias Vars for ordinary columns must refer to merged JOIN
1083  * USING columns. We don't need to do anything here, because the
1084  * join input columns will also be referenced in the join's qual
1085  * clause, and will get marked for select privilege there.
1086  */
1087  }
1088  }
1089  /* other RTE types don't require privilege marking */
1090 }
1091 
1092 /*
1093  * markVarForSelectPriv
1094  * Mark the RTE referenced by the Var as requiring SELECT privilege
1095  * for the Var's column (the Var could be a whole-row Var, too)
1096  */
1097 void
1099 {
1100  Index lv;
1101 
1102  Assert(IsA(var, Var));
1103  /* Find the appropriate pstate if it's an uplevel Var */
1104  for (lv = 0; lv < var->varlevelsup; lv++)
1105  pstate = pstate->parentParseState;
1106  markRTEForSelectPriv(pstate, var->varno, var->varattno);
1107 }
1108 
1109 /*
1110  * buildRelationAliases
1111  * Construct the eref column name list for a relation RTE.
1112  * This code is also used for function RTEs.
1113  *
1114  * tupdesc: the physical column information
1115  * alias: the user-supplied alias, or NULL if none
1116  * eref: the eref Alias to store column names in
1117  *
1118  * eref->colnames is filled in. Also, alias->colnames is rebuilt to insert
1119  * empty strings for any dropped columns, so that it will be one-to-one with
1120  * physical column numbers.
1121  *
1122  * It is an error for there to be more aliases present than required.
1123  */
1124 static void
1126 {
1127  int maxattrs = tupdesc->natts;
1128  List *aliaslist;
1129  ListCell *aliaslc;
1130  int numaliases;
1131  int varattno;
1132  int numdropped = 0;
1133 
1134  Assert(eref->colnames == NIL);
1135 
1136  if (alias)
1137  {
1138  aliaslist = alias->colnames;
1139  aliaslc = list_head(aliaslist);
1140  numaliases = list_length(aliaslist);
1141  /* We'll rebuild the alias colname list */
1142  alias->colnames = NIL;
1143  }
1144  else
1145  {
1146  aliaslist = NIL;
1147  aliaslc = NULL;
1148  numaliases = 0;
1149  }
1150 
1151  for (varattno = 0; varattno < maxattrs; varattno++)
1152  {
1153  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1154  String *attrname;
1155 
1156  if (attr->attisdropped)
1157  {
1158  /* Always insert an empty string for a dropped column */
1159  attrname = makeString(pstrdup(""));
1160  if (aliaslc)
1161  alias->colnames = lappend(alias->colnames, attrname);
1162  numdropped++;
1163  }
1164  else if (aliaslc)
1165  {
1166  /* Use the next user-supplied alias */
1167  attrname = lfirst_node(String, aliaslc);
1168  aliaslc = lnext(aliaslist, aliaslc);
1169  alias->colnames = lappend(alias->colnames, attrname);
1170  }
1171  else
1172  {
1173  attrname = makeString(pstrdup(NameStr(attr->attname)));
1174  /* we're done with the alias if any */
1175  }
1176 
1177  eref->colnames = lappend(eref->colnames, attrname);
1178  }
1179 
1180  /* Too many user-supplied aliases? */
1181  if (aliaslc)
1182  ereport(ERROR,
1183  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1184  errmsg("table \"%s\" has %d columns available but %d columns specified",
1185  eref->aliasname, maxattrs - numdropped, numaliases)));
1186 }
1187 
1188 /*
1189  * chooseScalarFunctionAlias
1190  * Select the column alias for a function in a function RTE,
1191  * when the function returns a scalar type (not composite or RECORD).
1192  *
1193  * funcexpr: transformed expression tree for the function call
1194  * funcname: function name (as determined by FigureColname)
1195  * alias: the user-supplied alias for the RTE, or NULL if none
1196  * nfuncs: the number of functions appearing in the function RTE
1197  *
1198  * Note that the name we choose might be overridden later, if the user-given
1199  * alias includes column alias names. That's of no concern here.
1200  */
1201 static char *
1202 chooseScalarFunctionAlias(Node *funcexpr, char *funcname,
1203  Alias *alias, int nfuncs)
1204 {
1205  char *pname;
1206 
1207  /*
1208  * If the expression is a simple function call, and the function has a
1209  * single OUT parameter that is named, use the parameter's name.
1210  */
1211  if (funcexpr && IsA(funcexpr, FuncExpr))
1212  {
1213  pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1214  if (pname)
1215  return pname;
1216  }
1217 
1218  /*
1219  * If there's just one function in the RTE, and the user gave an RTE alias
1220  * name, use that name. (This makes FROM func() AS foo use "foo" as the
1221  * column name as well as the table alias.)
1222  */
1223  if (nfuncs == 1 && alias)
1224  return alias->aliasname;
1225 
1226  /*
1227  * Otherwise use the function name.
1228  */
1229  return funcname;
1230 }
1231 
1232 /*
1233  * buildNSItemFromTupleDesc
1234  * Build a ParseNamespaceItem, given a tupdesc describing the columns.
1235  *
1236  * rte: the new RangeTblEntry for the rel
1237  * rtindex: its index in the rangetable list
1238  * tupdesc: the physical column information
1239  */
1240 static ParseNamespaceItem *
1242 {
1243  ParseNamespaceItem *nsitem;
1244  ParseNamespaceColumn *nscolumns;
1245  int maxattrs = tupdesc->natts;
1246  int varattno;
1247 
1248  /* colnames must have the same number of entries as the nsitem */
1249  Assert(maxattrs == list_length(rte->eref->colnames));
1250 
1251  /* extract per-column data from the tupdesc */
1252  nscolumns = (ParseNamespaceColumn *)
1253  palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1254 
1255  for (varattno = 0; varattno < maxattrs; varattno++)
1256  {
1257  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1258 
1259  /* For a dropped column, just leave the entry as zeroes */
1260  if (attr->attisdropped)
1261  continue;
1262 
1263  nscolumns[varattno].p_varno = rtindex;
1264  nscolumns[varattno].p_varattno = varattno + 1;
1265  nscolumns[varattno].p_vartype = attr->atttypid;
1266  nscolumns[varattno].p_vartypmod = attr->atttypmod;
1267  nscolumns[varattno].p_varcollid = attr->attcollation;
1268  nscolumns[varattno].p_varnosyn = rtindex;
1269  nscolumns[varattno].p_varattnosyn = varattno + 1;
1270  }
1271 
1272  /* ... and build the nsitem */
1273  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1274  nsitem->p_names = rte->eref;
1275  nsitem->p_rte = rte;
1276  nsitem->p_rtindex = rtindex;
1277  nsitem->p_nscolumns = nscolumns;
1278  /* set default visibility flags; might get changed later */
1279  nsitem->p_rel_visible = true;
1280  nsitem->p_cols_visible = true;
1281  nsitem->p_lateral_only = false;
1282  nsitem->p_lateral_ok = true;
1283 
1284  return nsitem;
1285 }
1286 
1287 /*
1288  * buildNSItemFromLists
1289  * Build a ParseNamespaceItem, given column type information in lists.
1290  *
1291  * rte: the new RangeTblEntry for the rel
1292  * rtindex: its index in the rangetable list
1293  * coltypes: per-column datatype OIDs
1294  * coltypmods: per-column type modifiers
1295  * colcollation: per-column collation OIDs
1296  */
1297 static ParseNamespaceItem *
1299  List *coltypes, List *coltypmods, List *colcollations)
1300 {
1301  ParseNamespaceItem *nsitem;
1302  ParseNamespaceColumn *nscolumns;
1303  int maxattrs = list_length(coltypes);
1304  int varattno;
1305  ListCell *lct;
1306  ListCell *lcm;
1307  ListCell *lcc;
1308 
1309  /* colnames must have the same number of entries as the nsitem */
1310  Assert(maxattrs == list_length(rte->eref->colnames));
1311 
1312  Assert(maxattrs == list_length(coltypmods));
1313  Assert(maxattrs == list_length(colcollations));
1314 
1315  /* extract per-column data from the lists */
1316  nscolumns = (ParseNamespaceColumn *)
1317  palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1318 
1319  varattno = 0;
1320  forthree(lct, coltypes,
1321  lcm, coltypmods,
1322  lcc, colcollations)
1323  {
1324  nscolumns[varattno].p_varno = rtindex;
1325  nscolumns[varattno].p_varattno = varattno + 1;
1326  nscolumns[varattno].p_vartype = lfirst_oid(lct);
1327  nscolumns[varattno].p_vartypmod = lfirst_int(lcm);
1328  nscolumns[varattno].p_varcollid = lfirst_oid(lcc);
1329  nscolumns[varattno].p_varnosyn = rtindex;
1330  nscolumns[varattno].p_varattnosyn = varattno + 1;
1331  varattno++;
1332  }
1333 
1334  /* ... and build the nsitem */
1335  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1336  nsitem->p_names = rte->eref;
1337  nsitem->p_rte = rte;
1338  nsitem->p_rtindex = rtindex;
1339  nsitem->p_nscolumns = nscolumns;
1340  /* set default visibility flags; might get changed later */
1341  nsitem->p_rel_visible = true;
1342  nsitem->p_cols_visible = true;
1343  nsitem->p_lateral_only = false;
1344  nsitem->p_lateral_ok = true;
1345 
1346  return nsitem;
1347 }
1348 
1349 /*
1350  * Open a table during parse analysis
1351  *
1352  * This is essentially just the same as table_openrv(), except that it caters
1353  * to some parser-specific error reporting needs, notably that it arranges
1354  * to include the RangeVar's parse location in any resulting error.
1355  *
1356  * Note: properly, lockmode should be declared LOCKMODE not int, but that
1357  * would require importing storage/lock.h into parse_relation.h. Since
1358  * LOCKMODE is typedef'd as int anyway, that seems like overkill.
1359  */
1360 Relation
1361 parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
1362 {
1363  Relation rel;
1364  ParseCallbackState pcbstate;
1365 
1366  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1367  rel = table_openrv_extended(relation, lockmode, true);
1368  if (rel == NULL)
1369  {
1370  if (relation->schemaname)
1371  ereport(ERROR,
1373  errmsg("relation \"%s.%s\" does not exist",
1374  relation->schemaname, relation->relname)));
1375  else
1376  {
1377  /*
1378  * An unqualified name might have been meant as a reference to
1379  * some not-yet-in-scope CTE. The bare "does not exist" message
1380  * has proven remarkably unhelpful for figuring out such problems,
1381  * so we take pains to offer a specific hint.
1382  */
1383  if (isFutureCTE(pstate, relation->relname))
1384  ereport(ERROR,
1386  errmsg("relation \"%s\" does not exist",
1387  relation->relname),
1388  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1389  relation->relname),
1390  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1391  else
1392  ereport(ERROR,
1394  errmsg("relation \"%s\" does not exist",
1395  relation->relname)));
1396  }
1397  }
1399  return rel;
1400 }
1401 
1402 /*
1403  * Add an entry for a relation to the pstate's range table (p_rtable).
1404  * Then, construct and return a ParseNamespaceItem for the new RTE.
1405  *
1406  * We do not link the ParseNamespaceItem into the pstate here; it's the
1407  * caller's job to do that in the appropriate way.
1408  *
1409  * Note: formerly this checked for refname conflicts, but that's wrong.
1410  * Caller is responsible for checking for conflicts in the appropriate scope.
1411  */
1414  RangeVar *relation,
1415  Alias *alias,
1416  bool inh,
1417  bool inFromCl)
1418 {
1420  char *refname = alias ? alias->aliasname : relation->relname;
1421  LOCKMODE lockmode;
1422  Relation rel;
1423  ParseNamespaceItem *nsitem;
1424 
1425  Assert(pstate != NULL);
1426 
1427  rte->rtekind = RTE_RELATION;
1428  rte->alias = alias;
1429 
1430  /*
1431  * Identify the type of lock we'll need on this relation. It's not the
1432  * query's target table (that case is handled elsewhere), so we need
1433  * either RowShareLock if it's locked by FOR UPDATE/SHARE, or plain
1434  * AccessShareLock otherwise.
1435  */
1436  lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock;
1437 
1438  /*
1439  * Get the rel's OID. This access also ensures that we have an up-to-date
1440  * relcache entry for the rel. Since this is typically the first access
1441  * to a rel in a statement, we must open the rel with the proper lockmode.
1442  */
1443  rel = parserOpenTable(pstate, relation, lockmode);
1444  rte->relid = RelationGetRelid(rel);
1445  rte->relkind = rel->rd_rel->relkind;
1446  rte->rellockmode = lockmode;
1447 
1448  /*
1449  * Build the list of effective column names using user-supplied aliases
1450  * and/or actual column names.
1451  */
1452  rte->eref = makeAlias(refname, NIL);
1453  buildRelationAliases(rel->rd_att, alias, rte->eref);
1454 
1455  /*
1456  * Set flags and access permissions.
1457  *
1458  * The initial default on access checks is always check-for-READ-access,
1459  * which is the right thing for all except target tables.
1460  */
1461  rte->lateral = false;
1462  rte->inh = inh;
1463  rte->inFromCl = inFromCl;
1464 
1465  rte->requiredPerms = ACL_SELECT;
1466  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1467  rte->selectedCols = NULL;
1468  rte->insertedCols = NULL;
1469  rte->updatedCols = NULL;
1470  rte->extraUpdatedCols = NULL;
1471 
1472  /*
1473  * Add completed RTE to pstate's range table list, so that we know its
1474  * index. But we don't add it to the join list --- caller must do that if
1475  * appropriate.
1476  */
1477  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1478 
1479  /*
1480  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1481  * list --- caller must do that if appropriate.
1482  */
1483  nsitem = buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1484  rel->rd_att);
1485 
1486  /*
1487  * Drop the rel refcount, but keep the access lock till end of transaction
1488  * so that the table can't be deleted or have its schema modified
1489  * underneath us.
1490  */
1491  table_close(rel, NoLock);
1492 
1493  return nsitem;
1494 }
1495 
1496 /*
1497  * Add an entry for a relation to the pstate's range table (p_rtable).
1498  * Then, construct and return a ParseNamespaceItem for the new RTE.
1499  *
1500  * This is just like addRangeTableEntry() except that it makes an RTE
1501  * given an already-open relation instead of a RangeVar reference.
1502  *
1503  * lockmode is the lock type required for query execution; it must be one
1504  * of AccessShareLock, RowShareLock, or RowExclusiveLock depending on the
1505  * RTE's role within the query. The caller must hold that lock mode
1506  * or a stronger one.
1507  *
1508  * Note: properly, lockmode should be declared LOCKMODE not int, but that
1509  * would require importing storage/lock.h into parse_relation.h. Since
1510  * LOCKMODE is typedef'd as int anyway, that seems like overkill.
1511  */
1514  Relation rel,
1515  int lockmode,
1516  Alias *alias,
1517  bool inh,
1518  bool inFromCl)
1519 {
1521  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1522 
1523  Assert(pstate != NULL);
1524 
1525  Assert(lockmode == AccessShareLock ||
1526  lockmode == RowShareLock ||
1527  lockmode == RowExclusiveLock);
1528  Assert(CheckRelationLockedByMe(rel, lockmode, true));
1529 
1530  rte->rtekind = RTE_RELATION;
1531  rte->alias = alias;
1532  rte->relid = RelationGetRelid(rel);
1533  rte->relkind = rel->rd_rel->relkind;
1534  rte->rellockmode = lockmode;
1535 
1536  /*
1537  * Build the list of effective column names using user-supplied aliases
1538  * and/or actual column names.
1539  */
1540  rte->eref = makeAlias(refname, NIL);
1541  buildRelationAliases(rel->rd_att, alias, rte->eref);
1542 
1543  /*
1544  * Set flags and access permissions.
1545  *
1546  * The initial default on access checks is always check-for-READ-access,
1547  * which is the right thing for all except target tables.
1548  */
1549  rte->lateral = false;
1550  rte->inh = inh;
1551  rte->inFromCl = inFromCl;
1552 
1553  rte->requiredPerms = ACL_SELECT;
1554  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1555  rte->selectedCols = NULL;
1556  rte->insertedCols = NULL;
1557  rte->updatedCols = NULL;
1558  rte->extraUpdatedCols = NULL;
1559 
1560  /*
1561  * Add completed RTE to pstate's range table list, so that we know its
1562  * index. But we don't add it to the join list --- caller must do that if
1563  * appropriate.
1564  */
1565  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1566 
1567  /*
1568  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1569  * list --- caller must do that if appropriate.
1570  */
1571  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1572  rel->rd_att);
1573 }
1574 
1575 /*
1576  * Add an entry for a subquery to the pstate's range table (p_rtable).
1577  * Then, construct and return a ParseNamespaceItem for the new RTE.
1578  *
1579  * This is much like addRangeTableEntry() except that it makes a subquery RTE.
1580  * Note that an alias clause *must* be supplied.
1581  */
1584  Query *subquery,
1585  Alias *alias,
1586  bool lateral,
1587  bool inFromCl)
1588 {
1590  char *refname = alias->aliasname;
1591  Alias *eref;
1592  int numaliases;
1593  List *coltypes,
1594  *coltypmods,
1595  *colcollations;
1596  int varattno;
1597  ListCell *tlistitem;
1598 
1599  Assert(pstate != NULL);
1600 
1601  rte->rtekind = RTE_SUBQUERY;
1602  rte->subquery = subquery;
1603  rte->alias = alias;
1604 
1605  eref = copyObject(alias);
1606  numaliases = list_length(eref->colnames);
1607 
1608  /* fill in any unspecified alias columns, and extract column type info */
1609  coltypes = coltypmods = colcollations = NIL;
1610  varattno = 0;
1611  foreach(tlistitem, subquery->targetList)
1612  {
1613  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1614 
1615  if (te->resjunk)
1616  continue;
1617  varattno++;
1618  Assert(varattno == te->resno);
1619  if (varattno > numaliases)
1620  {
1621  char *attrname;
1622 
1623  attrname = pstrdup(te->resname);
1624  eref->colnames = lappend(eref->colnames, makeString(attrname));
1625  }
1626  coltypes = lappend_oid(coltypes,
1627  exprType((Node *) te->expr));
1628  coltypmods = lappend_int(coltypmods,
1629  exprTypmod((Node *) te->expr));
1630  colcollations = lappend_oid(colcollations,
1631  exprCollation((Node *) te->expr));
1632  }
1633  if (varattno < numaliases)
1634  ereport(ERROR,
1635  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1636  errmsg("table \"%s\" has %d columns available but %d columns specified",
1637  refname, varattno, numaliases)));
1638 
1639  rte->eref = eref;
1640 
1641  /*
1642  * Set flags and access permissions.
1643  *
1644  * Subqueries are never checked for access rights.
1645  */
1646  rte->lateral = lateral;
1647  rte->inh = false; /* never true for subqueries */
1648  rte->inFromCl = inFromCl;
1649 
1650  rte->requiredPerms = 0;
1651  rte->checkAsUser = InvalidOid;
1652  rte->selectedCols = NULL;
1653  rte->insertedCols = NULL;
1654  rte->updatedCols = NULL;
1655  rte->extraUpdatedCols = NULL;
1656 
1657  /*
1658  * Add completed RTE to pstate's range table list, so that we know its
1659  * index. But we don't add it to the join list --- caller must do that if
1660  * appropriate.
1661  */
1662  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1663 
1664  /*
1665  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1666  * list --- caller must do that if appropriate.
1667  */
1668  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
1669  coltypes, coltypmods, colcollations);
1670 }
1671 
1672 /*
1673  * Add an entry for a function (or functions) to the pstate's range table
1674  * (p_rtable). Then, construct and return a ParseNamespaceItem for the new RTE.
1675  *
1676  * This is much like addRangeTableEntry() except that it makes a function RTE.
1677  */
1680  List *funcnames,
1681  List *funcexprs,
1682  List *coldeflists,
1683  RangeFunction *rangefunc,
1684  bool lateral,
1685  bool inFromCl)
1686 {
1688  Alias *alias = rangefunc->alias;
1689  Alias *eref;
1690  char *aliasname;
1691  int nfuncs = list_length(funcexprs);
1692  TupleDesc *functupdescs;
1693  TupleDesc tupdesc;
1694  ListCell *lc1,
1695  *lc2,
1696  *lc3;
1697  int i;
1698  int j;
1699  int funcno;
1700  int natts,
1701  totalatts;
1702 
1703  Assert(pstate != NULL);
1704 
1705  rte->rtekind = RTE_FUNCTION;
1706  rte->relid = InvalidOid;
1707  rte->subquery = NULL;
1708  rte->functions = NIL; /* we'll fill this list below */
1709  rte->funcordinality = rangefunc->ordinality;
1710  rte->alias = alias;
1711 
1712  /*
1713  * Choose the RTE alias name. We default to using the first function's
1714  * name even when there's more than one; which is maybe arguable but beats
1715  * using something constant like "table".
1716  */
1717  if (alias)
1718  aliasname = alias->aliasname;
1719  else
1720  aliasname = linitial(funcnames);
1721 
1722  eref = makeAlias(aliasname, NIL);
1723  rte->eref = eref;
1724 
1725  /* Process each function ... */
1726  functupdescs = (TupleDesc *) palloc(nfuncs * sizeof(TupleDesc));
1727 
1728  totalatts = 0;
1729  funcno = 0;
1730  forthree(lc1, funcexprs, lc2, funcnames, lc3, coldeflists)
1731  {
1732  Node *funcexpr = (Node *) lfirst(lc1);
1733  char *funcname = (char *) lfirst(lc2);
1734  List *coldeflist = (List *) lfirst(lc3);
1736  TypeFuncClass functypclass;
1737  Oid funcrettype;
1738 
1739  /* Initialize RangeTblFunction node */
1740  rtfunc->funcexpr = funcexpr;
1741  rtfunc->funccolnames = NIL;
1742  rtfunc->funccoltypes = NIL;
1743  rtfunc->funccoltypmods = NIL;
1744  rtfunc->funccolcollations = NIL;
1745  rtfunc->funcparams = NULL; /* not set until planning */
1746 
1747  /*
1748  * Now determine if the function returns a simple or composite type.
1749  */
1750  functypclass = get_expr_result_type(funcexpr,
1751  &funcrettype,
1752  &tupdesc);
1753 
1754  /*
1755  * A coldeflist is required if the function returns RECORD and hasn't
1756  * got a predetermined record type, and is prohibited otherwise. This
1757  * can be a bit confusing, so we expend some effort on delivering a
1758  * relevant error message.
1759  */
1760  if (coldeflist != NIL)
1761  {
1762  switch (functypclass)
1763  {
1764  case TYPEFUNC_RECORD:
1765  /* ok */
1766  break;
1767  case TYPEFUNC_COMPOSITE:
1769 
1770  /*
1771  * If the function's raw result type is RECORD, we must
1772  * have resolved it using its OUT parameters. Otherwise,
1773  * it must have a named composite type.
1774  */
1775  if (exprType(funcexpr) == RECORDOID)
1776  ereport(ERROR,
1777  (errcode(ERRCODE_SYNTAX_ERROR),
1778  errmsg("a column definition list is redundant for a function with OUT parameters"),
1779  parser_errposition(pstate,
1780  exprLocation((Node *) coldeflist))));
1781  else
1782  ereport(ERROR,
1783  (errcode(ERRCODE_SYNTAX_ERROR),
1784  errmsg("a column definition list is redundant for a function returning a named composite type"),
1785  parser_errposition(pstate,
1786  exprLocation((Node *) coldeflist))));
1787  break;
1788  default:
1789  ereport(ERROR,
1790  (errcode(ERRCODE_SYNTAX_ERROR),
1791  errmsg("a column definition list is only allowed for functions returning \"record\""),
1792  parser_errposition(pstate,
1793  exprLocation((Node *) coldeflist))));
1794  break;
1795  }
1796  }
1797  else
1798  {
1799  if (functypclass == TYPEFUNC_RECORD)
1800  ereport(ERROR,
1801  (errcode(ERRCODE_SYNTAX_ERROR),
1802  errmsg("a column definition list is required for functions returning \"record\""),
1803  parser_errposition(pstate, exprLocation(funcexpr))));
1804  }
1805 
1806  if (functypclass == TYPEFUNC_COMPOSITE ||
1807  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
1808  {
1809  /* Composite data type, e.g. a table's row type */
1810  Assert(tupdesc);
1811  }
1812  else if (functypclass == TYPEFUNC_SCALAR)
1813  {
1814  /* Base data type, i.e. scalar */
1815  tupdesc = CreateTemplateTupleDesc(1);
1816  TupleDescInitEntry(tupdesc,
1817  (AttrNumber) 1,
1818  chooseScalarFunctionAlias(funcexpr, funcname,
1819  alias, nfuncs),
1820  funcrettype,
1821  exprTypmod(funcexpr),
1822  0);
1824  (AttrNumber) 1,
1825  exprCollation(funcexpr));
1826  }
1827  else if (functypclass == TYPEFUNC_RECORD)
1828  {
1829  ListCell *col;
1830 
1831  /*
1832  * Use the column definition list to construct a tupdesc and fill
1833  * in the RangeTblFunction's lists.
1834  */
1835  tupdesc = CreateTemplateTupleDesc(list_length(coldeflist));
1836  i = 1;
1837  foreach(col, coldeflist)
1838  {
1839  ColumnDef *n = (ColumnDef *) lfirst(col);
1840  char *attrname;
1841  Oid attrtype;
1842  int32 attrtypmod;
1843  Oid attrcollation;
1844 
1845  attrname = n->colname;
1846  if (n->typeName->setof)
1847  ereport(ERROR,
1848  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1849  errmsg("column \"%s\" cannot be declared SETOF",
1850  attrname),
1851  parser_errposition(pstate, n->location)));
1852  typenameTypeIdAndMod(pstate, n->typeName,
1853  &attrtype, &attrtypmod);
1854  attrcollation = GetColumnDefCollation(pstate, n, attrtype);
1855  TupleDescInitEntry(tupdesc,
1856  (AttrNumber) i,
1857  attrname,
1858  attrtype,
1859  attrtypmod,
1860  0);
1862  (AttrNumber) i,
1863  attrcollation);
1864  rtfunc->funccolnames = lappend(rtfunc->funccolnames,
1865  makeString(pstrdup(attrname)));
1866  rtfunc->funccoltypes = lappend_oid(rtfunc->funccoltypes,
1867  attrtype);
1868  rtfunc->funccoltypmods = lappend_int(rtfunc->funccoltypmods,
1869  attrtypmod);
1871  attrcollation);
1872 
1873  i++;
1874  }
1875 
1876  /*
1877  * Ensure that the coldeflist defines a legal set of names (no
1878  * duplicates, but we needn't worry about system column names) and
1879  * datatypes. Although we mostly can't allow pseudo-types, it
1880  * seems safe to allow RECORD and RECORD[], since values within
1881  * those type classes are self-identifying at runtime, and the
1882  * coldeflist doesn't represent anything that will be visible to
1883  * other sessions.
1884  */
1885  CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE,
1887  }
1888  else
1889  ereport(ERROR,
1890  (errcode(ERRCODE_DATATYPE_MISMATCH),
1891  errmsg("function \"%s\" in FROM has unsupported return type %s",
1892  funcname, format_type_be(funcrettype)),
1893  parser_errposition(pstate, exprLocation(funcexpr))));
1894 
1895  /* Finish off the RangeTblFunction and add it to the RTE's list */
1896  rtfunc->funccolcount = tupdesc->natts;
1897  rte->functions = lappend(rte->functions, rtfunc);
1898 
1899  /* Save the tupdesc for use below */
1900  functupdescs[funcno] = tupdesc;
1901  totalatts += tupdesc->natts;
1902  funcno++;
1903  }
1904 
1905  /*
1906  * If there's more than one function, or we want an ordinality column, we
1907  * have to produce a merged tupdesc.
1908  */
1909  if (nfuncs > 1 || rangefunc->ordinality)
1910  {
1911  if (rangefunc->ordinality)
1912  totalatts++;
1913 
1914  /* Merge the tuple descs of each function into a composite one */
1915  tupdesc = CreateTemplateTupleDesc(totalatts);
1916  natts = 0;
1917  for (i = 0; i < nfuncs; i++)
1918  {
1919  for (j = 1; j <= functupdescs[i]->natts; j++)
1920  TupleDescCopyEntry(tupdesc, ++natts, functupdescs[i], j);
1921  }
1922 
1923  /* Add the ordinality column if needed */
1924  if (rangefunc->ordinality)
1925  {
1926  TupleDescInitEntry(tupdesc,
1927  (AttrNumber) ++natts,
1928  "ordinality",
1929  INT8OID,
1930  -1,
1931  0);
1932  /* no need to set collation */
1933  }
1934 
1935  Assert(natts == totalatts);
1936  }
1937  else
1938  {
1939  /* We can just use the single function's tupdesc as-is */
1940  tupdesc = functupdescs[0];
1941  }
1942 
1943  /* Use the tupdesc while assigning column aliases for the RTE */
1944  buildRelationAliases(tupdesc, alias, eref);
1945 
1946  /*
1947  * Set flags and access permissions.
1948  *
1949  * Functions are never checked for access rights (at least, not by the RTE
1950  * permissions mechanism).
1951  */
1952  rte->lateral = lateral;
1953  rte->inh = false; /* never true for functions */
1954  rte->inFromCl = inFromCl;
1955 
1956  rte->requiredPerms = 0;
1957  rte->checkAsUser = InvalidOid;
1958  rte->selectedCols = NULL;
1959  rte->insertedCols = NULL;
1960  rte->updatedCols = NULL;
1961  rte->extraUpdatedCols = NULL;
1962 
1963  /*
1964  * Add completed RTE to pstate's range table list, so that we know its
1965  * index. But we don't add it to the join list --- caller must do that if
1966  * appropriate.
1967  */
1968  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1969 
1970  /*
1971  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1972  * list --- caller must do that if appropriate.
1973  */
1974  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1975  tupdesc);
1976 }
1977 
1978 /*
1979  * Add an entry for a table function to the pstate's range table (p_rtable).
1980  * Then, construct and return a ParseNamespaceItem for the new RTE.
1981  *
1982  * This is much like addRangeTableEntry() except that it makes a tablefunc RTE.
1983  */
1986  TableFunc *tf,
1987  Alias *alias,
1988  bool lateral,
1989  bool inFromCl)
1990 {
1992  char *refname;
1993  Alias *eref;
1994  int numaliases;
1995 
1996  refname = alias ? alias->aliasname :
1997  pstrdup(tf->functype == TFT_XMLTABLE ? "xmltable" : "json_table");
1998 
1999  Assert(pstate != NULL);
2000 
2001  rte->rtekind = RTE_TABLEFUNC;
2002  rte->relid = InvalidOid;
2003  rte->subquery = NULL;
2004  rte->tablefunc = tf;
2005  rte->coltypes = tf->coltypes;
2006  rte->coltypmods = tf->coltypmods;
2007  rte->colcollations = tf->colcollations;
2008  rte->alias = alias;
2009 
2010  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2011  numaliases = list_length(eref->colnames);
2012 
2013  /* fill in any unspecified alias columns */
2014  if (numaliases < list_length(tf->colnames))
2015  eref->colnames = list_concat(eref->colnames,
2016  list_copy_tail(tf->colnames, numaliases));
2017 
2018  if (numaliases > list_length(tf->colnames))
2019  ereport(ERROR,
2020  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2021  errmsg("%s function has %d columns available but %d columns specified",
2022  tf->functype == TFT_XMLTABLE ? "XMLTABLE" : "JSON_TABLE",
2023  list_length(tf->colnames), numaliases)));
2024 
2025  rte->eref = eref;
2026 
2027  /*
2028  * Set flags and access permissions.
2029  *
2030  * Tablefuncs are never checked for access rights (at least, not by the
2031  * RTE permissions mechanism).
2032  */
2033  rte->lateral = lateral;
2034  rte->inh = false; /* never true for tablefunc RTEs */
2035  rte->inFromCl = inFromCl;
2036 
2037  rte->requiredPerms = 0;
2038  rte->checkAsUser = InvalidOid;
2039  rte->selectedCols = NULL;
2040  rte->insertedCols = NULL;
2041  rte->updatedCols = NULL;
2042  rte->extraUpdatedCols = NULL;
2043 
2044  /*
2045  * Add completed RTE to pstate's range table list, so that we know its
2046  * index. But we don't add it to the join list --- caller must do that if
2047  * appropriate.
2048  */
2049  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2050 
2051  /*
2052  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2053  * list --- caller must do that if appropriate.
2054  */
2055  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2056  rte->coltypes, rte->coltypmods,
2057  rte->colcollations);
2058 }
2059 
2060 /*
2061  * Add an entry for a VALUES list to the pstate's range table (p_rtable).
2062  * Then, construct and return a ParseNamespaceItem for the new RTE.
2063  *
2064  * This is much like addRangeTableEntry() except that it makes a values RTE.
2065  */
2068  List *exprs,
2069  List *coltypes,
2070  List *coltypmods,
2071  List *colcollations,
2072  Alias *alias,
2073  bool lateral,
2074  bool inFromCl)
2075 {
2077  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
2078  Alias *eref;
2079  int numaliases;
2080  int numcolumns;
2081 
2082  Assert(pstate != NULL);
2083 
2084  rte->rtekind = RTE_VALUES;
2085  rte->relid = InvalidOid;
2086  rte->subquery = NULL;
2087  rte->values_lists = exprs;
2088  rte->coltypes = coltypes;
2089  rte->coltypmods = coltypmods;
2090  rte->colcollations = colcollations;
2091  rte->alias = alias;
2092 
2093  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2094 
2095  /* fill in any unspecified alias columns */
2096  numcolumns = list_length((List *) linitial(exprs));
2097  numaliases = list_length(eref->colnames);
2098  while (numaliases < numcolumns)
2099  {
2100  char attrname[64];
2101 
2102  numaliases++;
2103  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
2104  eref->colnames = lappend(eref->colnames,
2105  makeString(pstrdup(attrname)));
2106  }
2107  if (numcolumns < numaliases)
2108  ereport(ERROR,
2109  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2110  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
2111  refname, numcolumns, numaliases)));
2112 
2113  rte->eref = eref;
2114 
2115  /*
2116  * Set flags and access permissions.
2117  *
2118  * Subqueries are never checked for access rights.
2119  */
2120  rte->lateral = lateral;
2121  rte->inh = false; /* never true for values RTEs */
2122  rte->inFromCl = inFromCl;
2123 
2124  rte->requiredPerms = 0;
2125  rte->checkAsUser = InvalidOid;
2126  rte->selectedCols = NULL;
2127  rte->insertedCols = NULL;
2128  rte->updatedCols = NULL;
2129  rte->extraUpdatedCols = NULL;
2130 
2131  /*
2132  * Add completed RTE to pstate's range table list, so that we know its
2133  * index. But we don't add it to the join list --- caller must do that if
2134  * appropriate.
2135  */
2136  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2137 
2138  /*
2139  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2140  * list --- caller must do that if appropriate.
2141  */
2142  return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2143  rte->coltypes, rte->coltypmods,
2144  rte->colcollations);
2145 }
2146 
2147 /*
2148  * Add an entry for a join to the pstate's range table (p_rtable).
2149  * Then, construct and return a ParseNamespaceItem for the new RTE.
2150  *
2151  * This is much like addRangeTableEntry() except that it makes a join RTE.
2152  * Also, it's more convenient for the caller to construct the
2153  * ParseNamespaceColumn array, so we pass that in.
2154  */
2157  List *colnames,
2158  ParseNamespaceColumn *nscolumns,
2159  JoinType jointype,
2160  int nummergedcols,
2161  List *aliasvars,
2162  List *leftcols,
2163  List *rightcols,
2164  Alias *join_using_alias,
2165  Alias *alias,
2166  bool inFromCl)
2167 {
2169  Alias *eref;
2170  int numaliases;
2171  ParseNamespaceItem *nsitem;
2172 
2173  Assert(pstate != NULL);
2174 
2175  /*
2176  * Fail if join has too many columns --- we must be able to reference any
2177  * of the columns with an AttrNumber.
2178  */
2179  if (list_length(aliasvars) > MaxAttrNumber)
2180  ereport(ERROR,
2181  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2182  errmsg("joins can have at most %d columns",
2183  MaxAttrNumber)));
2184 
2185  rte->rtekind = RTE_JOIN;
2186  rte->relid = InvalidOid;
2187  rte->subquery = NULL;
2188  rte->jointype = jointype;
2189  rte->joinmergedcols = nummergedcols;
2190  rte->joinaliasvars = aliasvars;
2191  rte->joinleftcols = leftcols;
2192  rte->joinrightcols = rightcols;
2193  rte->join_using_alias = join_using_alias;
2194  rte->alias = alias;
2195 
2196  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2197  numaliases = list_length(eref->colnames);
2198 
2199  /* fill in any unspecified alias columns */
2200  if (numaliases < list_length(colnames))
2201  eref->colnames = list_concat(eref->colnames,
2202  list_copy_tail(colnames, numaliases));
2203 
2204  if (numaliases > list_length(colnames))
2205  ereport(ERROR,
2206  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2207  errmsg("join expression \"%s\" has %d columns available but %d columns specified",
2208  eref->aliasname, list_length(colnames), numaliases)));
2209 
2210  rte->eref = eref;
2211 
2212  /*
2213  * Set flags and access permissions.
2214  *
2215  * Joins are never checked for access rights.
2216  */
2217  rte->lateral = false;
2218  rte->inh = false; /* never true for joins */
2219  rte->inFromCl = inFromCl;
2220 
2221  rte->requiredPerms = 0;
2222  rte->checkAsUser = InvalidOid;
2223  rte->selectedCols = NULL;
2224  rte->insertedCols = NULL;
2225  rte->updatedCols = NULL;
2226  rte->extraUpdatedCols = NULL;
2227 
2228  /*
2229  * Add completed RTE to pstate's range table list, so that we know its
2230  * index. But we don't add it to the join list --- caller must do that if
2231  * appropriate.
2232  */
2233  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2234 
2235  /*
2236  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2237  * list --- caller must do that if appropriate.
2238  */
2239  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2240  nsitem->p_names = rte->eref;
2241  nsitem->p_rte = rte;
2242  nsitem->p_rtindex = list_length(pstate->p_rtable);
2243  nsitem->p_nscolumns = nscolumns;
2244  /* set default visibility flags; might get changed later */
2245  nsitem->p_rel_visible = true;
2246  nsitem->p_cols_visible = true;
2247  nsitem->p_lateral_only = false;
2248  nsitem->p_lateral_ok = true;
2249 
2250  return nsitem;
2251 }
2252 
2253 /*
2254  * Add an entry for a CTE reference to the pstate's range table (p_rtable).
2255  * Then, construct and return a ParseNamespaceItem for the new RTE.
2256  *
2257  * This is much like addRangeTableEntry() except that it makes a CTE RTE.
2258  */
2261  CommonTableExpr *cte,
2262  Index levelsup,
2263  RangeVar *rv,
2264  bool inFromCl)
2265 {
2267  Alias *alias = rv->alias;
2268  char *refname = alias ? alias->aliasname : cte->ctename;
2269  Alias *eref;
2270  int numaliases;
2271  int varattno;
2272  ListCell *lc;
2273  int n_dontexpand_columns = 0;
2274  ParseNamespaceItem *psi;
2275 
2276  Assert(pstate != NULL);
2277 
2278  rte->rtekind = RTE_CTE;
2279  rte->ctename = cte->ctename;
2280  rte->ctelevelsup = levelsup;
2281 
2282  /* Self-reference if and only if CTE's parse analysis isn't completed */
2283  rte->self_reference = !IsA(cte->ctequery, Query);
2284  Assert(cte->cterecursive || !rte->self_reference);
2285  /* Bump the CTE's refcount if this isn't a self-reference */
2286  if (!rte->self_reference)
2287  cte->cterefcount++;
2288 
2289  /*
2290  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
2291  * This won't get checked in case of a self-reference, but that's OK
2292  * because data-modifying CTEs aren't allowed to be recursive anyhow.
2293  */
2294  if (IsA(cte->ctequery, Query))
2295  {
2296  Query *ctequery = (Query *) cte->ctequery;
2297 
2298  if (ctequery->commandType != CMD_SELECT &&
2299  ctequery->returningList == NIL)
2300  ereport(ERROR,
2301  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2302  errmsg("WITH query \"%s\" does not have a RETURNING clause",
2303  cte->ctename),
2304  parser_errposition(pstate, rv->location)));
2305  }
2306 
2307  rte->coltypes = list_copy(cte->ctecoltypes);
2308  rte->coltypmods = list_copy(cte->ctecoltypmods);
2310 
2311  rte->alias = alias;
2312  if (alias)
2313  eref = copyObject(alias);
2314  else
2315  eref = makeAlias(refname, NIL);
2316  numaliases = list_length(eref->colnames);
2317 
2318  /* fill in any unspecified alias columns */
2319  varattno = 0;
2320  foreach(lc, cte->ctecolnames)
2321  {
2322  varattno++;
2323  if (varattno > numaliases)
2324  eref->colnames = lappend(eref->colnames, lfirst(lc));
2325  }
2326  if (varattno < numaliases)
2327  ereport(ERROR,
2328  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2329  errmsg("table \"%s\" has %d columns available but %d columns specified",
2330  refname, varattno, numaliases)));
2331 
2332  rte->eref = eref;
2333 
2334  if (cte->search_clause)
2335  {
2338  rte->coltypes = lappend_oid(rte->coltypes, RECORDOID);
2339  else
2340  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2341  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2343 
2344  n_dontexpand_columns += 1;
2345  }
2346 
2347  if (cte->cycle_clause)
2348  {
2353 
2355  rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2356  rte->coltypmods = lappend_int(rte->coltypmods, -1);
2358 
2359  n_dontexpand_columns += 2;
2360  }
2361 
2362  /*
2363  * Set flags and access permissions.
2364  *
2365  * Subqueries are never checked for access rights.
2366  */
2367  rte->lateral = false;
2368  rte->inh = false; /* never true for subqueries */
2369  rte->inFromCl = inFromCl;
2370 
2371  rte->requiredPerms = 0;
2372  rte->checkAsUser = InvalidOid;
2373  rte->selectedCols = NULL;
2374  rte->insertedCols = NULL;
2375  rte->updatedCols = NULL;
2376  rte->extraUpdatedCols = NULL;
2377 
2378  /*
2379  * Add completed RTE to pstate's range table list, so that we know its
2380  * index. But we don't add it to the join list --- caller must do that if
2381  * appropriate.
2382  */
2383  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2384 
2385  /*
2386  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2387  * list --- caller must do that if appropriate.
2388  */
2389  psi = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2390  rte->coltypes, rte->coltypmods,
2391  rte->colcollations);
2392 
2393  /*
2394  * The columns added by search and cycle clauses are not included in star
2395  * expansion in queries contained in the CTE.
2396  */
2397  if (rte->ctelevelsup > 0)
2398  for (int i = 0; i < n_dontexpand_columns; i++)
2399  psi->p_nscolumns[list_length(psi->p_names->colnames) - 1 - i].p_dontexpand = true;
2400 
2401  return psi;
2402 }
2403 
2404 /*
2405  * Add an entry for an ephemeral named relation reference to the pstate's
2406  * range table (p_rtable).
2407  * Then, construct and return a ParseNamespaceItem for the new RTE.
2408  *
2409  * It is expected that the RangeVar, which up until now is only known to be an
2410  * ephemeral named relation, will (in conjunction with the QueryEnvironment in
2411  * the ParseState), create a RangeTblEntry for a specific *kind* of ephemeral
2412  * named relation, based on enrtype.
2413  *
2414  * This is much like addRangeTableEntry() except that it makes an RTE for an
2415  * ephemeral named relation.
2416  */
2419  RangeVar *rv,
2420  bool inFromCl)
2421 {
2423  Alias *alias = rv->alias;
2424  char *refname = alias ? alias->aliasname : rv->relname;
2426  TupleDesc tupdesc;
2427  int attno;
2428 
2429  Assert(pstate != NULL);
2430  enrmd = get_visible_ENR(pstate, rv->relname);
2431  Assert(enrmd != NULL);
2432 
2433  switch (enrmd->enrtype)
2434  {
2435  case ENR_NAMED_TUPLESTORE:
2437  break;
2438 
2439  default:
2440  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2441  return NULL; /* for fussy compilers */
2442  }
2443 
2444  /*
2445  * Record dependency on a relation. This allows plans to be invalidated
2446  * if they access transition tables linked to a table that is altered.
2447  */
2448  rte->relid = enrmd->reliddesc;
2449 
2450  /*
2451  * Build the list of effective column names using user-supplied aliases
2452  * and/or actual column names.
2453  */
2454  tupdesc = ENRMetadataGetTupDesc(enrmd);
2455  rte->eref = makeAlias(refname, NIL);
2456  buildRelationAliases(tupdesc, alias, rte->eref);
2457 
2458  /* Record additional data for ENR, including column type info */
2459  rte->enrname = enrmd->name;
2460  rte->enrtuples = enrmd->enrtuples;
2461  rte->coltypes = NIL;
2462  rte->coltypmods = NIL;
2463  rte->colcollations = NIL;
2464  for (attno = 1; attno <= tupdesc->natts; ++attno)
2465  {
2466  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2467 
2468  if (att->attisdropped)
2469  {
2470  /* Record zeroes for a dropped column */
2471  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2472  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2474  }
2475  else
2476  {
2477  /* Let's just make sure we can tell this isn't dropped */
2478  if (att->atttypid == InvalidOid)
2479  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2480  rv->relname);
2481  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2482  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2484  att->attcollation);
2485  }
2486  }
2487 
2488  /*
2489  * Set flags and access permissions.
2490  *
2491  * ENRs are never checked for access rights.
2492  */
2493  rte->lateral = false;
2494  rte->inh = false; /* never true for ENRs */
2495  rte->inFromCl = inFromCl;
2496 
2497  rte->requiredPerms = 0;
2498  rte->checkAsUser = InvalidOid;
2499  rte->selectedCols = NULL;
2500 
2501  /*
2502  * Add completed RTE to pstate's range table list, so that we know its
2503  * index. But we don't add it to the join list --- caller must do that if
2504  * appropriate.
2505  */
2506  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2507 
2508  /*
2509  * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2510  * list --- caller must do that if appropriate.
2511  */
2512  return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
2513  tupdesc);
2514 }
2515 
2516 
2517 /*
2518  * Has the specified refname been selected FOR UPDATE/FOR SHARE?
2519  *
2520  * This is used when we have not yet done transformLockingClause, but need
2521  * to know the correct lock to take during initial opening of relations.
2522  *
2523  * Note: we pay no attention to whether it's FOR UPDATE vs FOR SHARE,
2524  * since the table-level lock is the same either way.
2525  */
2526 bool
2527 isLockedRefname(ParseState *pstate, const char *refname)
2528 {
2529  ListCell *l;
2530 
2531  /*
2532  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2533  * parent level, then act as though there's a generic FOR UPDATE here.
2534  */
2535  if (pstate->p_locked_from_parent)
2536  return true;
2537 
2538  foreach(l, pstate->p_locking_clause)
2539  {
2540  LockingClause *lc = (LockingClause *) lfirst(l);
2541 
2542  if (lc->lockedRels == NIL)
2543  {
2544  /* all tables used in query */
2545  return true;
2546  }
2547  else
2548  {
2549  /* just the named tables */
2550  ListCell *l2;
2551 
2552  foreach(l2, lc->lockedRels)
2553  {
2554  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2555 
2556  if (strcmp(refname, thisrel->relname) == 0)
2557  return true;
2558  }
2559  }
2560  }
2561  return false;
2562 }
2563 
2564 /*
2565  * Add the given nsitem/RTE as a top-level entry in the pstate's join list
2566  * and/or namespace list. (We assume caller has checked for any
2567  * namespace conflicts.) The nsitem is always marked as unconditionally
2568  * visible, that is, not LATERAL-only.
2569  */
2570 void
2572  bool addToJoinList,
2573  bool addToRelNameSpace, bool addToVarNameSpace)
2574 {
2575  if (addToJoinList)
2576  {
2578 
2579  rtr->rtindex = nsitem->p_rtindex;
2580  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2581  }
2582  if (addToRelNameSpace || addToVarNameSpace)
2583  {
2584  /* Set the new nsitem's visibility flags correctly */
2585  nsitem->p_rel_visible = addToRelNameSpace;
2586  nsitem->p_cols_visible = addToVarNameSpace;
2587  nsitem->p_lateral_only = false;
2588  nsitem->p_lateral_ok = true;
2589  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2590  }
2591 }
2592 
2593 /*
2594  * expandRTE -- expand the columns of a rangetable entry
2595  *
2596  * This creates lists of an RTE's column names (aliases if provided, else
2597  * real names) and Vars for each column. Only user columns are considered.
2598  * If include_dropped is false then dropped columns are omitted from the
2599  * results. If include_dropped is true then empty strings and NULL constants
2600  * (not Vars!) are returned for dropped columns.
2601  *
2602  * rtindex, sublevels_up, and location are the varno, varlevelsup, and location
2603  * values to use in the created Vars. Ordinarily rtindex should match the
2604  * actual position of the RTE in its rangetable.
2605  *
2606  * The output lists go into *colnames and *colvars.
2607  * If only one of the two kinds of output list is needed, pass NULL for the
2608  * output pointer for the unwanted one.
2609  */
2610 void
2611 expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
2612  int location, bool include_dropped,
2613  List **colnames, List **colvars)
2614 {
2615  int varattno;
2616 
2617  if (colnames)
2618  *colnames = NIL;
2619  if (colvars)
2620  *colvars = NIL;
2621 
2622  switch (rte->rtekind)
2623  {
2624  case RTE_RELATION:
2625  /* Ordinary relation RTE */
2626  expandRelation(rte->relid, rte->eref,
2627  rtindex, sublevels_up, location,
2628  include_dropped, colnames, colvars);
2629  break;
2630  case RTE_SUBQUERY:
2631  {
2632  /* Subquery RTE */
2633  ListCell *aliasp_item = list_head(rte->eref->colnames);
2634  ListCell *tlistitem;
2635 
2636  varattno = 0;
2637  foreach(tlistitem, rte->subquery->targetList)
2638  {
2639  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
2640 
2641  if (te->resjunk)
2642  continue;
2643  varattno++;
2644  Assert(varattno == te->resno);
2645 
2646  /*
2647  * In scenarios where columns have been added to a view
2648  * since the outer query was originally parsed, there can
2649  * be more items in the subquery tlist than the outer
2650  * query expects. We should ignore such extra column(s)
2651  * --- compare the behavior for composite-returning
2652  * functions, in the RTE_FUNCTION case below.
2653  */
2654  if (!aliasp_item)
2655  break;
2656 
2657  if (colnames)
2658  {
2659  char *label = strVal(lfirst(aliasp_item));
2660 
2661  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2662  }
2663 
2664  if (colvars)
2665  {
2666  Var *varnode;
2667 
2668  varnode = makeVar(rtindex, varattno,
2669  exprType((Node *) te->expr),
2670  exprTypmod((Node *) te->expr),
2671  exprCollation((Node *) te->expr),
2672  sublevels_up);
2673  varnode->location = location;
2674 
2675  *colvars = lappend(*colvars, varnode);
2676  }
2677 
2678  aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2679  }
2680  }
2681  break;
2682  case RTE_FUNCTION:
2683  {
2684  /* Function RTE */
2685  int atts_done = 0;
2686  ListCell *lc;
2687 
2688  foreach(lc, rte->functions)
2689  {
2690  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2691  TypeFuncClass functypclass;
2692  Oid funcrettype;
2693  TupleDesc tupdesc;
2694 
2695  functypclass = get_expr_result_type(rtfunc->funcexpr,
2696  &funcrettype,
2697  &tupdesc);
2698  if (functypclass == TYPEFUNC_COMPOSITE ||
2699  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
2700  {
2701  /* Composite data type, e.g. a table's row type */
2702  Assert(tupdesc);
2703  expandTupleDesc(tupdesc, rte->eref,
2704  rtfunc->funccolcount, atts_done,
2705  rtindex, sublevels_up, location,
2706  include_dropped, colnames, colvars);
2707  }
2708  else if (functypclass == TYPEFUNC_SCALAR)
2709  {
2710  /* Base data type, i.e. scalar */
2711  if (colnames)
2712  *colnames = lappend(*colnames,
2713  list_nth(rte->eref->colnames,
2714  atts_done));
2715 
2716  if (colvars)
2717  {
2718  Var *varnode;
2719 
2720  varnode = makeVar(rtindex, atts_done + 1,
2721  funcrettype,
2722  exprTypmod(rtfunc->funcexpr),
2723  exprCollation(rtfunc->funcexpr),
2724  sublevels_up);
2725  varnode->location = location;
2726 
2727  *colvars = lappend(*colvars, varnode);
2728  }
2729  }
2730  else if (functypclass == TYPEFUNC_RECORD)
2731  {
2732  if (colnames)
2733  {
2734  List *namelist;
2735 
2736  /* extract appropriate subset of column list */
2737  namelist = list_copy_tail(rte->eref->colnames,
2738  atts_done);
2739  namelist = list_truncate(namelist,
2740  rtfunc->funccolcount);
2741  *colnames = list_concat(*colnames, namelist);
2742  }
2743 
2744  if (colvars)
2745  {
2746  ListCell *l1;
2747  ListCell *l2;
2748  ListCell *l3;
2749  int attnum = atts_done;
2750 
2751  forthree(l1, rtfunc->funccoltypes,
2752  l2, rtfunc->funccoltypmods,
2753  l3, rtfunc->funccolcollations)
2754  {
2755  Oid attrtype = lfirst_oid(l1);
2756  int32 attrtypmod = lfirst_int(l2);
2757  Oid attrcollation = lfirst_oid(l3);
2758  Var *varnode;
2759 
2760  attnum++;
2761  varnode = makeVar(rtindex,
2762  attnum,
2763  attrtype,
2764  attrtypmod,
2765  attrcollation,
2766  sublevels_up);
2767  varnode->location = location;
2768  *colvars = lappend(*colvars, varnode);
2769  }
2770  }
2771  }
2772  else
2773  {
2774  /* addRangeTableEntryForFunction should've caught this */
2775  elog(ERROR, "function in FROM has unsupported return type");
2776  }
2777  atts_done += rtfunc->funccolcount;
2778  }
2779 
2780  /* Append the ordinality column if any */
2781  if (rte->funcordinality)
2782  {
2783  if (colnames)
2784  *colnames = lappend(*colnames,
2785  llast(rte->eref->colnames));
2786 
2787  if (colvars)
2788  {
2789  Var *varnode = makeVar(rtindex,
2790  atts_done + 1,
2791  INT8OID,
2792  -1,
2793  InvalidOid,
2794  sublevels_up);
2795 
2796  *colvars = lappend(*colvars, varnode);
2797  }
2798  }
2799  }
2800  break;
2801  case RTE_JOIN:
2802  {
2803  /* Join RTE */
2804  ListCell *colname;
2805  ListCell *aliasvar;
2806 
2808 
2809  varattno = 0;
2810  forboth(colname, rte->eref->colnames, aliasvar, rte->joinaliasvars)
2811  {
2812  Node *avar = (Node *) lfirst(aliasvar);
2813 
2814  varattno++;
2815 
2816  /*
2817  * During ordinary parsing, there will never be any
2818  * deleted columns in the join. While this function is
2819  * also used by the rewriter and planner, they do not
2820  * currently call it on any JOIN RTEs. Therefore, this
2821  * next bit is dead code, but it seems prudent to handle
2822  * the case correctly anyway.
2823  */
2824  if (avar == NULL)
2825  {
2826  if (include_dropped)
2827  {
2828  if (colnames)
2829  *colnames = lappend(*colnames,
2830  makeString(pstrdup("")));
2831  if (colvars)
2832  {
2833  /*
2834  * Can't use join's column type here (it might
2835  * be dropped!); but it doesn't really matter
2836  * what type the Const claims to be.
2837  */
2838  *colvars = lappend(*colvars,
2839  makeNullConst(INT4OID, -1,
2840  InvalidOid));
2841  }
2842  }
2843  continue;
2844  }
2845 
2846  if (colnames)
2847  {
2848  char *label = strVal(lfirst(colname));
2849 
2850  *colnames = lappend(*colnames,
2852  }
2853 
2854  if (colvars)
2855  {
2856  Var *varnode;
2857 
2858  /*
2859  * If the joinaliasvars entry is a simple Var, just
2860  * copy it (with adjustment of varlevelsup and
2861  * location); otherwise it is a JOIN USING column and
2862  * we must generate a join alias Var. This matches
2863  * the results that expansion of "join.*" by
2864  * expandNSItemVars would have produced, if we had
2865  * access to the ParseNamespaceItem for the join.
2866  */
2867  if (IsA(avar, Var))
2868  {
2869  varnode = copyObject((Var *) avar);
2870  varnode->varlevelsup = sublevels_up;
2871  }
2872  else
2873  varnode = makeVar(rtindex, varattno,
2874  exprType(avar),
2875  exprTypmod(avar),
2876  exprCollation(avar),
2877  sublevels_up);
2878  varnode->location = location;
2879 
2880  *colvars = lappend(*colvars, varnode);
2881  }
2882  }
2883  }
2884  break;
2885  case RTE_TABLEFUNC:
2886  case RTE_VALUES:
2887  case RTE_CTE:
2888  case RTE_NAMEDTUPLESTORE:
2889  {
2890  /* Tablefunc, Values, CTE, or ENR RTE */
2891  ListCell *aliasp_item = list_head(rte->eref->colnames);
2892  ListCell *lct;
2893  ListCell *lcm;
2894  ListCell *lcc;
2895 
2896  varattno = 0;
2897  forthree(lct, rte->coltypes,
2898  lcm, rte->coltypmods,
2899  lcc, rte->colcollations)
2900  {
2901  Oid coltype = lfirst_oid(lct);
2902  int32 coltypmod = lfirst_int(lcm);
2903  Oid colcoll = lfirst_oid(lcc);
2904 
2905  varattno++;
2906 
2907  if (colnames)
2908  {
2909  /* Assume there is one alias per output column */
2910  if (OidIsValid(coltype))
2911  {
2912  char *label = strVal(lfirst(aliasp_item));
2913 
2914  *colnames = lappend(*colnames,
2916  }
2917  else if (include_dropped)
2918  *colnames = lappend(*colnames,
2919  makeString(pstrdup("")));
2920 
2921  aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2922  }
2923 
2924  if (colvars)
2925  {
2926  if (OidIsValid(coltype))
2927  {
2928  Var *varnode;
2929 
2930  varnode = makeVar(rtindex, varattno,
2931  coltype, coltypmod, colcoll,
2932  sublevels_up);
2933  varnode->location = location;
2934 
2935  *colvars = lappend(*colvars, varnode);
2936  }
2937  else if (include_dropped)
2938  {
2939  /*
2940  * It doesn't really matter what type the Const
2941  * claims to be.
2942  */
2943  *colvars = lappend(*colvars,
2944  makeNullConst(INT4OID, -1,
2945  InvalidOid));
2946  }
2947  }
2948  }
2949  }
2950  break;
2951  case RTE_RESULT:
2952  /* These expose no columns, so nothing to do */
2953  break;
2954  default:
2955  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2956  }
2957 }
2958 
2959 /*
2960  * expandRelation -- expandRTE subroutine
2961  */
2962 static void
2963 expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up,
2964  int location, bool include_dropped,
2965  List **colnames, List **colvars)
2966 {
2967  Relation rel;
2968 
2969  /* Get the tupledesc and turn it over to expandTupleDesc */
2970  rel = relation_open(relid, AccessShareLock);
2971  expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
2972  rtindex, sublevels_up,
2973  location, include_dropped,
2974  colnames, colvars);
2976 }
2977 
2978 /*
2979  * expandTupleDesc -- expandRTE subroutine
2980  *
2981  * Generate names and/or Vars for the first "count" attributes of the tupdesc,
2982  * and append them to colnames/colvars. "offset" is added to the varattno
2983  * that each Var would otherwise have, and we also skip the first "offset"
2984  * entries in eref->colnames. (These provisions allow use of this code for
2985  * an individual composite-returning function in an RTE_FUNCTION RTE.)
2986  */
2987 static void
2988 expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset,
2989  int rtindex, int sublevels_up,
2990  int location, bool include_dropped,
2991  List **colnames, List **colvars)
2992 {
2993  ListCell *aliascell;
2994  int varattno;
2995 
2996  aliascell = (offset < list_length(eref->colnames)) ?
2997  list_nth_cell(eref->colnames, offset) : NULL;
2998 
2999  Assert(count <= tupdesc->natts);
3000  for (varattno = 0; varattno < count; varattno++)
3001  {
3002  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
3003 
3004  if (attr->attisdropped)
3005  {
3006  if (include_dropped)
3007  {
3008  if (colnames)
3009  *colnames = lappend(*colnames, makeString(pstrdup("")));
3010  if (colvars)
3011  {
3012  /*
3013  * can't use atttypid here, but it doesn't really matter
3014  * what type the Const claims to be.
3015  */
3016  *colvars = lappend(*colvars,
3017  makeNullConst(INT4OID, -1, InvalidOid));
3018  }
3019  }
3020  if (aliascell)
3021  aliascell = lnext(eref->colnames, aliascell);
3022  continue;
3023  }
3024 
3025  if (colnames)
3026  {
3027  char *label;
3028 
3029  if (aliascell)
3030  {
3031  label = strVal(lfirst(aliascell));
3032  aliascell = lnext(eref->colnames, aliascell);
3033  }
3034  else
3035  {
3036  /* If we run out of aliases, use the underlying name */
3037  label = NameStr(attr->attname);
3038  }
3039  *colnames = lappend(*colnames, makeString(pstrdup(label)));
3040  }
3041 
3042  if (colvars)
3043  {
3044  Var *varnode;
3045 
3046  varnode = makeVar(rtindex, varattno + offset + 1,
3047  attr->atttypid, attr->atttypmod,
3048  attr->attcollation,
3049  sublevels_up);
3050  varnode->location = location;
3051 
3052  *colvars = lappend(*colvars, varnode);
3053  }
3054  }
3055 }
3056 
3057 /*
3058  * expandNSItemVars
3059  * Produce a list of Vars, and optionally a list of column names,
3060  * for the non-dropped columns of the nsitem.
3061  *
3062  * The emitted Vars are marked with the given sublevels_up and location.
3063  *
3064  * If colnames isn't NULL, a list of String items for the columns is stored
3065  * there; note that it's just a subset of the RTE's eref list, and hence
3066  * the list elements mustn't be modified.
3067  */
3068 List *
3070  int sublevels_up, int location,
3071  List **colnames)
3072 {
3073  List *result = NIL;
3074  int colindex;
3075  ListCell *lc;
3076 
3077  if (colnames)
3078  *colnames = NIL;
3079  colindex = 0;
3080  foreach(lc, nsitem->p_names->colnames)
3081  {
3082  String *colnameval = lfirst(lc);
3083  const char *colname = strVal(colnameval);
3084  ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3085 
3086  if (nscol->p_dontexpand)
3087  {
3088  /* skip */
3089  }
3090  else if (colname[0])
3091  {
3092  Var *var;
3093 
3094  Assert(nscol->p_varno > 0);
3095  var = makeVar(nscol->p_varno,
3096  nscol->p_varattno,
3097  nscol->p_vartype,
3098  nscol->p_vartypmod,
3099  nscol->p_varcollid,
3100  sublevels_up);
3101  /* makeVar doesn't offer parameters for these, so set by hand: */
3102  var->varnosyn = nscol->p_varnosyn;
3103  var->varattnosyn = nscol->p_varattnosyn;
3104  var->location = location;
3105  result = lappend(result, var);
3106  if (colnames)
3107  *colnames = lappend(*colnames, colnameval);
3108  }
3109  else
3110  {
3111  /* dropped column, ignore */
3112  Assert(nscol->p_varno == 0);
3113  }
3114  colindex++;
3115  }
3116  return result;
3117 }
3118 
3119 /*
3120  * expandNSItemAttrs -
3121  * Workhorse for "*" expansion: produce a list of targetentries
3122  * for the attributes of the nsitem
3123  *
3124  * pstate->p_next_resno determines the resnos assigned to the TLEs.
3125  * The referenced columns are marked as requiring SELECT access, if
3126  * caller requests that.
3127  */
3128 List *
3130  int sublevels_up, bool require_col_privs, int location)
3131 {
3132  RangeTblEntry *rte = nsitem->p_rte;
3133  List *names,
3134  *vars;
3135  ListCell *name,
3136  *var;
3137  List *te_list = NIL;
3138 
3139  vars = expandNSItemVars(nsitem, sublevels_up, location, &names);
3140 
3141  /*
3142  * Require read access to the table. This is normally redundant with the
3143  * markVarForSelectPriv calls below, but not if the table has zero
3144  * columns. We need not do anything if the nsitem is for a join: its
3145  * component tables will have been marked ACL_SELECT when they were added
3146  * to the rangetable. (This step changes things only for the target
3147  * relation of UPDATE/DELETE, which cannot be under a join.)
3148  */
3149  if (rte->rtekind == RTE_RELATION)
3150  rte->requiredPerms |= ACL_SELECT;
3151 
3152  forboth(name, names, var, vars)
3153  {
3154  char *label = strVal(lfirst(name));
3155  Var *varnode = (Var *) lfirst(var);
3156  TargetEntry *te;
3157 
3158  te = makeTargetEntry((Expr *) varnode,
3159  (AttrNumber) pstate->p_next_resno++,
3160  label,
3161  false);
3162  te_list = lappend(te_list, te);
3163 
3164  if (require_col_privs)
3165  {
3166  /* Require read access to each column */
3167  markVarForSelectPriv(pstate, varnode);
3168  }
3169  }
3170 
3171  Assert(name == NULL && var == NULL); /* lists not the same length? */
3172 
3173  return te_list;
3174 }
3175 
3176 /*
3177  * get_rte_attribute_name
3178  * Get an attribute name from a RangeTblEntry
3179  *
3180  * This is unlike get_attname() because we use aliases if available.
3181  * In particular, it will work on an RTE for a subselect or join, whereas
3182  * get_attname() only works on real relations.
3183  *
3184  * "*" is returned if the given attnum is InvalidAttrNumber --- this case
3185  * occurs when a Var represents a whole tuple of a relation.
3186  */
3187 char *
3189 {
3190  if (attnum == InvalidAttrNumber)
3191  return "*";
3192 
3193  /*
3194  * If there is a user-written column alias, use it.
3195  */
3196  if (rte->alias &&
3197  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3198  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3199 
3200  /*
3201  * If the RTE is a relation, go to the system catalogs not the
3202  * eref->colnames list. This is a little slower but it will give the
3203  * right answer if the column has been renamed since the eref list was
3204  * built (which can easily happen for rules).
3205  */
3206  if (rte->rtekind == RTE_RELATION)
3207  return get_attname(rte->relid, attnum, false);
3208 
3209  /*
3210  * Otherwise use the column name from eref. There should always be one.
3211  */
3212  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3213  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3214 
3215  /* else caller gave us a bogus attnum */
3216  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3217  attnum, rte->eref->aliasname);
3218  return NULL; /* keep compiler quiet */
3219 }
3220 
3221 /*
3222  * get_rte_attribute_is_dropped
3223  * Check whether attempted attribute ref is to a dropped column
3224  */
3225 bool
3227 {
3228  bool result;
3229 
3230  switch (rte->rtekind)
3231  {
3232  case RTE_RELATION:
3233  {
3234  /*
3235  * Plain relation RTE --- get the attribute's catalog entry
3236  */
3237  HeapTuple tp;
3238  Form_pg_attribute att_tup;
3239 
3240  tp = SearchSysCache2(ATTNUM,
3241  ObjectIdGetDatum(rte->relid),
3243  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3244  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3245  attnum, rte->relid);
3246  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3247  result = att_tup->attisdropped;
3248  ReleaseSysCache(tp);
3249  }
3250  break;
3251  case RTE_SUBQUERY:
3252  case RTE_TABLEFUNC:
3253  case RTE_VALUES:
3254  case RTE_CTE:
3255 
3256  /*
3257  * Subselect, Table Functions, Values, CTE RTEs never have dropped
3258  * columns
3259  */
3260  result = false;
3261  break;
3262  case RTE_NAMEDTUPLESTORE:
3263  {
3264  /* Check dropped-ness by testing for valid coltype */
3265  if (attnum <= 0 ||
3266  attnum > list_length(rte->coltypes))
3267  elog(ERROR, "invalid varattno %d", attnum);
3268  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3269  }
3270  break;
3271  case RTE_JOIN:
3272  {
3273  /*
3274  * A join RTE would not have dropped columns when constructed,
3275  * but one in a stored rule might contain columns that were
3276  * dropped from the underlying tables, if said columns are
3277  * nowhere explicitly referenced in the rule. This will be
3278  * signaled to us by a null pointer in the joinaliasvars list.
3279  */
3280  Var *aliasvar;
3281 
3282  if (attnum <= 0 ||
3284  elog(ERROR, "invalid varattno %d", attnum);
3285  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3286 
3287  result = (aliasvar == NULL);
3288  }
3289  break;
3290  case RTE_FUNCTION:
3291  {
3292  /* Function RTE */
3293  ListCell *lc;
3294  int atts_done = 0;
3295 
3296  /*
3297  * Dropped attributes are only possible with functions that
3298  * return named composite types. In such a case we have to
3299  * look up the result type to see if it currently has this
3300  * column dropped. So first, loop over the funcs until we
3301  * find the one that covers the requested column.
3302  */
3303  foreach(lc, rte->functions)
3304  {
3305  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3306 
3307  if (attnum > atts_done &&
3308  attnum <= atts_done + rtfunc->funccolcount)
3309  {
3310  TupleDesc tupdesc;
3311 
3312  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3313  true);
3314  if (tupdesc)
3315  {
3316  /* Composite data type, e.g. a table's row type */
3317  Form_pg_attribute att_tup;
3318 
3319  Assert(tupdesc);
3320  Assert(attnum - atts_done <= tupdesc->natts);
3321  att_tup = TupleDescAttr(tupdesc,
3322  attnum - atts_done - 1);
3323  return att_tup->attisdropped;
3324  }
3325  /* Otherwise, it can't have any dropped columns */
3326  return false;
3327  }
3328  atts_done += rtfunc->funccolcount;
3329  }
3330 
3331  /* If we get here, must be looking for the ordinality column */
3332  if (rte->funcordinality && attnum == atts_done + 1)
3333  return false;
3334 
3335  /* this probably can't happen ... */
3336  ereport(ERROR,
3337  (errcode(ERRCODE_UNDEFINED_COLUMN),
3338  errmsg("column %d of relation \"%s\" does not exist",
3339  attnum,
3340  rte->eref->aliasname)));
3341  result = false; /* keep compiler quiet */
3342  }
3343  break;
3344  case RTE_RESULT:
3345  /* this probably can't happen ... */
3346  ereport(ERROR,
3347  (errcode(ERRCODE_UNDEFINED_COLUMN),
3348  errmsg("column %d of relation \"%s\" does not exist",
3349  attnum,
3350  rte->eref->aliasname)));
3351  result = false; /* keep compiler quiet */
3352  break;
3353  default:
3354  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3355  result = false; /* keep compiler quiet */
3356  }
3357 
3358  return result;
3359 }
3360 
3361 /*
3362  * Given a targetlist and a resno, return the matching TargetEntry
3363  *
3364  * Returns NULL if resno is not present in list.
3365  *
3366  * Note: we need to search, rather than just indexing with list_nth(),
3367  * because not all tlists are sorted by resno.
3368  */
3369 TargetEntry *
3371 {
3372  ListCell *l;
3373 
3374  foreach(l, tlist)
3375  {
3376  TargetEntry *tle = (TargetEntry *) lfirst(l);
3377 
3378  if (tle->resno == resno)
3379  return tle;
3380  }
3381  return NULL;
3382 }
3383 
3384 /*
3385  * Given a Query and rangetable index, return relation's RowMarkClause if any
3386  *
3387  * Returns NULL if relation is not selected FOR UPDATE/SHARE
3388  */
3389 RowMarkClause *
3391 {
3392  ListCell *l;
3393 
3394  foreach(l, qry->rowMarks)
3395  {
3396  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3397 
3398  if (rc->rti == rtindex)
3399  return rc;
3400  }
3401  return NULL;
3402 }
3403 
3404 /*
3405  * given relation and att name, return attnum of variable
3406  *
3407  * Returns InvalidAttrNumber if the attr doesn't exist (or is dropped).
3408  *
3409  * This should only be used if the relation is already
3410  * table_open()'ed. Use the cache version get_attnum()
3411  * for access to non-opened relations.
3412  */
3413 int
3414 attnameAttNum(Relation rd, const char *attname, bool sysColOK)
3415 {
3416  int i;
3417 
3418  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3419  {
3421 
3422  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3423  return i + 1;
3424  }
3425 
3426  if (sysColOK)
3427  {
3429  return i;
3430  }
3431 
3432  /* on failure */
3433  return InvalidAttrNumber;
3434 }
3435 
3436 /* specialAttNum()
3437  *
3438  * Check attribute name to see if it is "special", e.g. "xmin".
3439  * - thomas 2000-02-07
3440  *
3441  * Note: this only discovers whether the name could be a system attribute.
3442  * Caller needs to ensure that it really is an attribute of the rel.
3443  */
3444 static int
3446 {
3447  const FormData_pg_attribute *sysatt;
3448 
3449  sysatt = SystemAttributeByName(attname);
3450  if (sysatt != NULL)
3451  return sysatt->attnum;
3452  return InvalidAttrNumber;
3453 }
3454 
3455 
3456 /*
3457  * given attribute id, return name of that attribute
3458  *
3459  * This should only be used if the relation is already
3460  * table_open()'ed. Use the cache version get_atttype()
3461  * for access to non-opened relations.
3462  */
3463 const NameData *
3464 attnumAttName(Relation rd, int attid)
3465 {
3466  if (attid <= 0)
3467  {
3468  const FormData_pg_attribute *sysatt;
3469 
3470  sysatt = SystemAttributeDefinition(attid);
3471  return &sysatt->attname;
3472  }
3473  if (attid > rd->rd_att->natts)
3474  elog(ERROR, "invalid attribute number %d", attid);
3475  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3476 }
3477 
3478 /*
3479  * given attribute id, return type of that attribute
3480  *
3481  * This should only be used if the relation is already
3482  * table_open()'ed. Use the cache version get_atttype()
3483  * for access to non-opened relations.
3484  */
3485 Oid
3486 attnumTypeId(Relation rd, int attid)
3487 {
3488  if (attid <= 0)
3489  {
3490  const FormData_pg_attribute *sysatt;
3491 
3492  sysatt = SystemAttributeDefinition(attid);
3493  return sysatt->atttypid;
3494  }
3495  if (attid > rd->rd_att->natts)
3496  elog(ERROR, "invalid attribute number %d", attid);
3497  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3498 }
3499 
3500 /*
3501  * given attribute id, return collation of that attribute
3502  *
3503  * This should only be used if the relation is already table_open()'ed.
3504  */
3505 Oid
3507 {
3508  if (attid <= 0)
3509  {
3510  /* All system attributes are of noncollatable types. */
3511  return InvalidOid;
3512  }
3513  if (attid > rd->rd_att->natts)
3514  elog(ERROR, "invalid attribute number %d", attid);
3515  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3516 }
3517 
3518 /*
3519  * Generate a suitable error about a missing RTE.
3520  *
3521  * Since this is a very common type of error, we work rather hard to
3522  * produce a helpful message.
3523  */
3524 void
3526 {
3527  RangeTblEntry *rte;
3528  const char *badAlias = NULL;
3529 
3530  /*
3531  * Check to see if there are any potential matches in the query's
3532  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3533  * will throw error immediately here. That seems OK.)
3534  */
3535  rte = searchRangeTableForRel(pstate, relation);
3536 
3537  /*
3538  * If we found a match that has an alias and the alias is visible in the
3539  * namespace, then the problem is probably use of the relation's real name
3540  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3541  * common enough to justify a specific hint.
3542  *
3543  * If we found a match that doesn't meet those criteria, assume the
3544  * problem is illegal use of a relation outside its scope, as in the
3545  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3546  */
3547  if (rte && rte->alias &&
3548  strcmp(rte->eref->aliasname, relation->relname) != 0)
3549  {
3550  ParseNamespaceItem *nsitem;
3551  int sublevels_up;
3552 
3553  nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3554  relation->location,
3555  &sublevels_up);
3556  if (nsitem && nsitem->p_rte == rte)
3557  badAlias = rte->eref->aliasname;
3558  }
3559 
3560  if (rte)
3561  ereport(ERROR,
3563  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3564  relation->relname),
3565  (badAlias ?
3566  errhint("Perhaps you meant to reference the table alias \"%s\".",
3567  badAlias) :
3568  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3569  rte->eref->aliasname)),
3570  parser_errposition(pstate, relation->location)));
3571  else
3572  ereport(ERROR,
3574  errmsg("missing FROM-clause entry for table \"%s\"",
3575  relation->relname),
3576  parser_errposition(pstate, relation->location)));
3577 }
3578 
3579 /*
3580  * Generate a suitable error about a missing column.
3581  *
3582  * Since this is a very common type of error, we work rather hard to
3583  * produce a helpful message.
3584  */
3585 void
3587  const char *relname, const char *colname, int location)
3588 {
3590  char *closestfirst = NULL;
3591 
3592  /*
3593  * Search the entire rtable looking for possible matches. If we find one,
3594  * emit a hint about it.
3595  *
3596  * TODO: improve this code (and also errorMissingRTE) to mention using
3597  * LATERAL if appropriate.
3598  */
3599  state = searchRangeTableForCol(pstate, relname, colname, location);
3600 
3601  /*
3602  * Extract closest col string for best match, if any.
3603  *
3604  * Infer an exact match referenced despite not being visible from the fact
3605  * that an attribute number was not present in state passed back -- this
3606  * is what is reported when !closestfirst. There might also be an exact
3607  * match that was qualified with an incorrect alias, in which case
3608  * closestfirst will be set (so hint is the same as generic fuzzy case).
3609  */
3610  if (state->rfirst && AttributeNumberIsValid(state->first))
3611  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3612  state->first - 1));
3613 
3614  if (!state->rsecond)
3615  {
3616  /*
3617  * Handle case where there is zero or one column suggestions to hint,
3618  * including exact matches referenced but not visible.
3619  */
3620  ereport(ERROR,
3621  (errcode(ERRCODE_UNDEFINED_COLUMN),
3622  relname ?
3623  errmsg("column %s.%s does not exist", relname, colname) :
3624  errmsg("column \"%s\" does not exist", colname),
3625  state->rfirst ? closestfirst ?
3626  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3627  state->rfirst->eref->aliasname, closestfirst) :
3628  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3629  colname, state->rfirst->eref->aliasname) : 0,
3630  parser_errposition(pstate, location)));
3631  }
3632  else
3633  {
3634  /* Handle case where there are two equally useful column hints */
3635  char *closestsecond;
3636 
3637  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3638  state->second - 1));
3639 
3640  ereport(ERROR,
3641  (errcode(ERRCODE_UNDEFINED_COLUMN),
3642  relname ?
3643  errmsg("column %s.%s does not exist", relname, colname) :
3644  errmsg("column \"%s\" does not exist", colname),
3645  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3646  state->rfirst->eref->aliasname, closestfirst,
3647  state->rsecond->eref->aliasname, closestsecond),
3648  parser_errposition(pstate, location)));
3649  }
3650 }
3651 
3652 
3653 /*
3654  * Examine a fully-parsed query, and return true iff any relation underlying
3655  * the query is a temporary relation (table, view, or materialized view).
3656  */
3657 bool
3659 {
3660  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3661 }
3662 
3663 static bool
3665 {
3666  if (node == NULL)
3667  return false;
3668 
3669  if (IsA(node, Query))
3670  {
3671  Query *query = (Query *) node;
3672  ListCell *rtable;
3673 
3674  foreach(rtable, query->rtable)
3675  {
3676  RangeTblEntry *rte = lfirst(rtable);
3677 
3678  if (rte->rtekind == RTE_RELATION)
3679  {
3681  char relpersistence = rel->rd_rel->relpersistence;
3682 
3684  if (relpersistence == RELPERSISTENCE_TEMP)
3685  return true;
3686  }
3687  }
3688 
3689  return query_tree_walker(query,
3691  context,
3693  }
3694 
3695  return expression_tree_walker(node,
3697  context);
3698 }
int16 AttrNumber
Definition: attnum.h:21
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define MaxAttrNumber
Definition: attnum.h:24
#define InvalidAttrNumber
Definition: attnum.h:23
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:738
#define NameStr(name)
Definition: c.h:692
signed int int32
Definition: c.h:440
unsigned int Index
Definition: c.h:560
#define OidIsValid(objectId)
Definition: c.h:721
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int errhint(const char *fmt,...)
Definition: elog.c:1151
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
const char * name
Definition: encode.c:561
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1565
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:509
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:292
TypeFuncClass
Definition: funcapi.h:147
@ TYPEFUNC_SCALAR
Definition: funcapi.h:148
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
@ TYPEFUNC_RECORD
Definition: funcapi.h:151
@ TYPEFUNC_COMPOSITE_DOMAIN
Definition: funcapi.h:150
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:239
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, int flags)
Definition: heap.c:452
const FormData_pg_attribute * SystemAttributeByName(const char *attname)
Definition: heap.c:251
#define CHKATYPE_ANYRECORD
Definition: heap.h:24
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
int j
Definition: isn.c:74
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_truncate(List *list, int new_size)
Definition: list.c:630
List * lappend(List *list, void *datum)
Definition: list.c:338
List * lappend_int(List *list, int datum)
Definition: list.c:356
List * lappend_oid(List *list, Oid datum)
Definition: list.c:374
List * list_copy(const List *oldlist)
Definition: list.c:1572
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1591
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:331
int LOCKMODE
Definition: lockdefs.h:26
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
#define RowShareLock
Definition: lockdefs.h:37
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1866
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:825
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:388
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:239
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:338
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void * palloc0(Size size)
Definition: mcxt.c:1099
void * palloc(Size size)
Definition: mcxt.c:1068
int namestrcmp(Name name, const char *str)
Definition: name.c:247
Oid LookupNamespaceNoError(const char *nspname)
Definition: namespace.c:2909
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:79
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2570
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:286
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:788
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:2015
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1343
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
#define IsA(nodeptr, _type_)
Definition: nodes.h:625
#define copyObject(obj)
Definition: nodes.h:690
#define nodeTag(nodeptr)
Definition: nodes.h:579
@ CMD_SELECT
Definition: nodes.h:722
#define makeNode(_type_)
Definition: nodes.h:622
JoinType
Definition: nodes.h:745
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:26
bool name_matches_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:20
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:160
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:144
@ EXPR_KIND_MERGE_WHEN
Definition: parse_node.h:58
@ EXPR_KIND_GENERATED_COLUMN
Definition: parse_node.h:82
@ EXPR_KIND_CHECK_CONSTRAINT
Definition: parse_node.h:67
static void expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
ParseNamespaceItem * addRangeTableEntryForCTE(ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
ParseNamespaceItem * addRangeTableEntry(ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
void errorMissingColumn(ParseState *pstate, const char *relname, const char *colname, int location)
static int specialAttNum(const char *attname)
static ParseNamespaceItem * scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colname, int location)
ParseNamespaceItem * addRangeTableEntryForJoin(ParseState *pstate, List *colnames, ParseNamespaceColumn *nscolumns, JoinType jointype, int nummergedcols, List *aliasvars, List *leftcols, List *rightcols, Alias *join_using_alias, Alias *alias, bool inFromCl)
Oid attnumCollationId(Relation rd, int attid)
void markVarForSelectPriv(ParseState *pstate, Var *var)
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
CommonTableExpr * scanNameSpaceForCTE(ParseState *pstate, const char *refname, Index *ctelevelsup)
static RangeTblEntry * searchRangeTableForRel(ParseState *pstate, RangeVar *relation)
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
static ParseNamespaceItem * scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
static bool isFutureCTE(ParseState *pstate, const char *refname)
bool get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
static void updateFuzzyAttrMatchState(int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate, RangeTblEntry *rte, const char *actual, const char *match, int attnum)
const NameData * attnumAttName(Relation rd, int attid)
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
static int scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, Alias *eref, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
#define MAX_FUZZY_DISTANCE
ParseNamespaceItem * GetNSItemByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
ParseNamespaceItem * addRangeTableEntryForSubquery(ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
char * get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
bool scanNameSpaceForENR(ParseState *pstate, const char *refname)
bool isLockedRefname(ParseState *pstate, const char *refname)
Oid attnumTypeId(Relation rd, int attid)
ParseNamespaceItem * addRangeTableEntryForFunction(ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
Node * colNameToVar(ParseState *pstate, const char *colname, bool localonly, int location)
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
ParseNamespaceItem * addRangeTableEntryForTableFunc(ParseState *pstate, TableFunc *tf, Alias *alias, bool lateral, bool inFromCl)
ParseNamespaceItem * addRangeTableEntryForValues(ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
static char * chooseScalarFunctionAlias(Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
List * expandNSItemAttrs(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, bool require_col_privs, int location)
List * expandNSItemVars(ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
bool isQueryUsingTempRelation(Query *query)
ParseNamespaceItem * addRangeTableEntryForENR(ParseState *pstate, RangeVar *rv, bool inFromCl)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
void checkNameSpaceConflicts(ParseState *pstate, List *namespace1, List *namespace2)
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
Oid GetColumnDefCollation(ParseState *pstate, ColumnDef *coldef, Oid typeOid)
Definition: parse_type.c:540
@ RTE_JOIN
Definition: parsenodes.h:1001
@ RTE_CTE
Definition: parsenodes.h:1005
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1006
@ RTE_VALUES
Definition: parsenodes.h:1004
@ RTE_SUBQUERY
Definition: parsenodes.h:1000
@ RTE_RESULT
Definition: parsenodes.h:1007
@ RTE_FUNCTION
Definition: parsenodes.h:1002
@ RTE_TABLEFUNC
Definition: parsenodes.h:1003
@ RTE_RELATION
Definition: parsenodes.h:999
#define ACL_SELECT
Definition: parsenodes.h:83
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
FormData_pg_attribute
Definition: pg_attribute.h:191
NameData attname
Definition: pg_attribute.h:41
int16 attnum
Definition: pg_attribute.h:83
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
static char * label
NameData relname
Definition: pg_class.h:38
#define lfirst(lc)
Definition: pg_list.h:170
#define llast(l)
Definition: pg_list.h:196
#define lfirst_node(type, lc)
Definition: pg_list.h:174
static int list_length(const List *l)
Definition: pg_list.h:150
#define NIL
Definition: pg_list.h:66
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:465
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:319
#define lfirst_int(lc)
Definition: pg_list.h:171
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:510
static ListCell * list_head(const List *l)
Definition: pg_list.h:126
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:275
#define linitial(l)
Definition: pg_list.h:176
static void * list_nth(const List *list, int n)
Definition: pg_list.h:297
#define list_nth_node(type, list, n)
Definition: pg_list.h:325
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:341
#define lfirst_oid(lc)
Definition: pg_list.h:172
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:81
#define snprintf
Definition: port.h:225
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define Int16GetDatum(X)
Definition: postgres.h:495
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char * c
@ TFT_XMLTABLE
Definition: primnodes.h:91
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
@ ENR_NAMED_TUPLESTORE
#define RelationGetRelid(relation)
Definition: rel.h:488
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:494
#define RelationGetRelationName(relation)
Definition: rel.h:522
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
char * aliasname
Definition: primnodes.h:42
List * colnames
Definition: primnodes.h:43
char * cycle_path_column
Definition: parsenodes.h:1512
Oid cycle_mark_collation
Definition: parsenodes.h:1517
char * cycle_mark_column
Definition: parsenodes.h:1509
char * search_seq_column
Definition: parsenodes.h:1501
bool search_breadth_first
Definition: parsenodes.h:1500
int location
Definition: parsenodes.h:696
char * colname
Definition: parsenodes.h:678
TypeName * typeName
Definition: parsenodes.h:679
List * ctecoltypes
Definition: parsenodes.h:1537
CTECycleClause * cycle_clause
Definition: parsenodes.h:1530
CTESearchClause * search_clause
Definition: parsenodes.h:1529
List * ctecolcollations
Definition: parsenodes.h:1539
List * ctecolnames
Definition: parsenodes.h:1536
List * ctecoltypmods
Definition: parsenodes.h:1538
EphemeralNameRelationType enrtype
RangeTblEntry * rfirst
RangeTblEntry * rsecond
Definition: pg_list.h:52
List * lockedRels
Definition: parsenodes.h:785
Definition: nodes.h:575
AttrNumber p_varattno
Definition: parse_node.h:303
AttrNumber p_varattnosyn
Definition: parse_node.h:308
RangeTblEntry * p_rte
Definition: parse_node.h:269
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:272
ParseState * parentParseState
Definition: parse_node.h:181
List * p_ctenamespace
Definition: parse_node.h:190
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:194
ParseExprKind p_expr_kind
Definition: parse_node.h:197
bool p_locked_from_parent
Definition: parse_node.h:201
List * p_namespace
Definition: parse_node.h:187
int p_next_resno
Definition: parse_node.h:198
List * p_joinexprs
Definition: parse_node.h:184
List * p_future_ctes
Definition: parse_node.h:191
List * p_joinlist
Definition: parse_node.h:185
List * p_locking_clause
Definition: parse_node.h:200
bool p_lateral_active
Definition: parse_node.h:189
List * p_rtable
Definition: parse_node.h:183
List * rowMarks
Definition: parsenodes.h:181
List * returningList
Definition: parsenodes.h:162
List * rtable
Definition: parsenodes.h:149
CmdType commandType
Definition: parsenodes.h:121
List * targetList
Definition: parsenodes.h:156
Alias * alias
Definition: parsenodes.h:599
List * colcollations
Definition: parsenodes.h:1150
char * ctename
Definition: parsenodes.h:1126
TableFunc * tablefunc
Definition: parsenodes.h:1116
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1171
bool self_reference
Definition: parsenodes.h:1128
AclMode requiredPerms
Definition: parsenodes.h:1166
Index ctelevelsup
Definition: parsenodes.h:1127
bool funcordinality
Definition: parsenodes.h:1111
Alias * join_using_alias
Definition: parsenodes.h:1100
Bitmapset * updatedCols
Definition: parsenodes.h:1170
Query * subquery
Definition: parsenodes.h:1051
Bitmapset * selectedCols
Definition: parsenodes.h:1168
Alias * eref
Definition: parsenodes.h:1162
List * coltypes
Definition: parsenodes.h:1148
List * joinrightcols
Definition: parsenodes.h:1093
List * values_lists
Definition: parsenodes.h:1121
char * enrname
Definition: parsenodes.h:1155
List * joinaliasvars
Definition: parsenodes.h:1091
JoinType jointype
Definition: parsenodes.h:1089
List * coltypmods
Definition: parsenodes.h:1149
Alias * alias
Definition: parsenodes.h:1161
List * functions
Definition: parsenodes.h:1110
Bitmapset * insertedCols
Definition: parsenodes.h:1169
List * joinleftcols
Definition: parsenodes.h:1092
Cardinality enrtuples
Definition: parsenodes.h:1156
RTEKind rtekind
Definition: parsenodes.h:1016
List * funccolcollations
Definition: parsenodes.h:1201
List * funccoltypmods
Definition: parsenodes.h:1200
Bitmapset * funcparams
Definition: parsenodes.h:1203
int location
Definition: primnodes.h:86
char * relname
Definition: primnodes.h:74
Alias * alias
Definition: primnodes.h:83
char * schemaname
Definition: primnodes.h:71
TupleDesc rd_att
Definition: rel.h:110
Form_pg_class rd_rel
Definition: rel.h:109
Definition: value.h:58
List * coltypmods
Definition: primnodes.h:111
List * coltypes
Definition: primnodes.h:110
List * colnames
Definition: primnodes.h:109
List * colcollations
Definition: primnodes.h:112
TableFuncType functype
Definition: primnodes.h:104
Expr * expr
Definition: primnodes.h:1828
char * resname
Definition: primnodes.h:1830
AttrNumber resno
Definition: primnodes.h:1829
bool resjunk
Definition: primnodes.h:1835
bool setof
Definition: parsenodes.h:229
Definition: primnodes.h:209
AttrNumber varattno
Definition: primnodes.h:221
AttrNumber varattnosyn
Definition: primnodes.h:239
int varno
Definition: primnodes.h:216
Index varlevelsup
Definition: primnodes.h:234
int location
Definition: primnodes.h:242
Index varnosyn
Definition: primnodes.h:237
Definition: c.h:687
Definition: regguts.h:318
Definition: regcomp.c:238
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
#define TableOidAttributeNumber
Definition: sysattr.h:26
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1184
@ ATTNUM
Definition: syscache.h:41
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:190
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Relation table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
Definition: table.c:132
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:763
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:267
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
String * makeString(char *str)
Definition: value.c:63
#define strVal(v)
Definition: value.h:72
int varstr_levenshtein_less_equal(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, int max_d, bool trusted)