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