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