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