LCOV - code coverage report
Current view: top level - src/backend/parser - parse_relation.c (source / functions) Hit Total Coverage
Test: PostgreSQL 11devel Lines: 964 1065 90.5 %
Date: 2018-05-21 18:20:01 Functions: 50 50 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13