PostgreSQL Source Code  git master
parse_relation.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "access/relation.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/heap.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parsetree.h"
#include "parser/parse_enr.h"
#include "parser/parse_relation.h"
#include "parser/parse_type.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/varlena.h"
Include dependency graph for parse_relation.c:

Go to the source code of this file.

Macros

#define MAX_FUZZY_DISTANCE   3
 

Functions

static RangeTblEntryscanNameSpaceForRefname (ParseState *pstate, const char *refname, int location)
 
static RangeTblEntryscanNameSpaceForRelid (ParseState *pstate, Oid relid, int location)
 
static void check_lateral_ref_ok (ParseState *pstate, ParseNamespaceItem *nsitem, int location)
 
static void markRTEForSelectPriv (ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
 
static void expandRelation (Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
 
static void expandTupleDesc (TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
 
static int specialAttNum (const char *attname)
 
static bool isQueryUsingTempRelation_walker (Node *node, void *context)
 
RangeTblEntryrefnameRangeTblEntry (ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
 
CommonTableExprscanNameSpaceForCTE (ParseState *pstate, const char *refname, Index *ctelevelsup)
 
static bool isFutureCTE (ParseState *pstate, const char *refname)
 
bool scanNameSpaceForENR (ParseState *pstate, const char *refname)
 
static RangeTblEntrysearchRangeTableForRel (ParseState *pstate, RangeVar *relation)
 
void checkNameSpaceConflicts (ParseState *pstate, List *namespace1, List *namespace2)
 
int RTERangeTablePosn (ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
 
RangeTblEntryGetRTEByRangeTablePosn (ParseState *pstate, int varno, int sublevels_up)
 
CommonTableExprGetCTEForRTE (ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
 
static void updateFuzzyAttrMatchState (int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate, RangeTblEntry *rte, const char *actual, const char *match, int attnum)
 
NodescanRTEForColumn (ParseState *pstate, RangeTblEntry *rte, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
 
NodecolNameToVar (ParseState *pstate, const char *colname, bool localonly, int location)
 
static FuzzyAttrMatchStatesearchRangeTableForCol (ParseState *pstate, const char *alias, const char *colname, int location)
 
void markVarForSelectPriv (ParseState *pstate, Var *var, RangeTblEntry *rte)
 
static void buildRelationAliases (TupleDesc tupdesc, Alias *alias, Alias *eref)
 
static char * chooseScalarFunctionAlias (Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
 
Relation parserOpenTable (ParseState *pstate, const RangeVar *relation, int lockmode)
 
RangeTblEntryaddRangeTableEntry (ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForRelation (ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForSubquery (ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForFunction (ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForTableFunc (ParseState *pstate, TableFunc *tf, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForValues (ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForJoin (ParseState *pstate, List *colnames, JoinType jointype, List *aliasvars, Alias *alias, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForCTE (ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForENR (ParseState *pstate, RangeVar *rv, bool inFromCl)
 
bool isLockedRefname (ParseState *pstate, const char *refname)
 
void addRTEtoQuery (ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
 
void expandRTE (RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
 
ListexpandRelAttrs (ParseState *pstate, RangeTblEntry *rte, int rtindex, int sublevels_up, int location)
 
char * get_rte_attribute_name (RangeTblEntry *rte, AttrNumber attnum)
 
void get_rte_attribute_type (RangeTblEntry *rte, AttrNumber attnum, Oid *vartype, int32 *vartypmod, Oid *varcollid)
 
bool get_rte_attribute_is_dropped (RangeTblEntry *rte, AttrNumber attnum)
 
TargetEntryget_tle_by_resno (List *tlist, AttrNumber resno)
 
RowMarkClauseget_parse_rowmark (Query *qry, Index rtindex)
 
int attnameAttNum (Relation rd, const char *attname, bool sysColOK)
 
const NameDataattnumAttName (Relation rd, int attid)
 
Oid attnumTypeId (Relation rd, int attid)
 
Oid attnumCollationId (Relation rd, int attid)
 
void errorMissingRTE (ParseState *pstate, RangeVar *relation)
 
void errorMissingColumn (ParseState *pstate, const char *relname, const char *colname, int location)
 
bool isQueryUsingTempRelation (Query *query)
 

Macro Definition Documentation

◆ MAX_FUZZY_DISTANCE

#define MAX_FUZZY_DISTANCE   3

Definition at line 41 of file parse_relation.c.

Referenced by searchRangeTableForCol(), and updateFuzzyAttrMatchState().

Function Documentation

◆ addRangeTableEntry()

RangeTblEntry* addRangeTableEntry ( ParseState pstate,
RangeVar relation,
Alias alias,
bool  inh,
bool  inFromCl 
)

Definition at line 1211 of file parse_relation.c.

References AccessShareLock, ACL_SELECT, RangeTblEntry::alias, Alias::aliasname, Assert, buildRelationAliases(), RangeTblEntry::checkAsUser, RangeTblEntry::eref, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, isLockedRefname(), lappend(), RangeTblEntry::lateral, makeAlias(), makeNode, NIL, NoLock, ParseState::p_rtable, parserOpenTable(), RelationData::rd_att, RelationData::rd_rel, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, RangeVar::relname, RangeTblEntry::requiredPerms, RowShareLock, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, table_close(), and RangeTblEntry::updatedCols.

Referenced by transformTableEntry().

1216 {
1218  char *refname = alias ? alias->aliasname : relation->relname;
1219  LOCKMODE lockmode;
1220  Relation rel;
1221 
1222  Assert(pstate != NULL);
1223 
1224  rte->rtekind = RTE_RELATION;
1225  rte->alias = alias;
1226 
1227  /*
1228  * Identify the type of lock we'll need on this relation. It's not the
1229  * query's target table (that case is handled elsewhere), so we need
1230  * either RowShareLock if it's locked by FOR UPDATE/SHARE, or plain
1231  * AccessShareLock otherwise.
1232  */
1233  lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock;
1234 
1235  /*
1236  * Get the rel's OID. This access also ensures that we have an up-to-date
1237  * relcache entry for the rel. Since this is typically the first access
1238  * to a rel in a statement, we must open the rel with the proper lockmode.
1239  */
1240  rel = parserOpenTable(pstate, relation, lockmode);
1241  rte->relid = RelationGetRelid(rel);
1242  rte->relkind = rel->rd_rel->relkind;
1243  rte->rellockmode = lockmode;
1244 
1245  /*
1246  * Build the list of effective column names using user-supplied aliases
1247  * and/or actual column names.
1248  */
1249  rte->eref = makeAlias(refname, NIL);
1250  buildRelationAliases(rel->rd_att, alias, rte->eref);
1251 
1252  /*
1253  * Drop the rel refcount, but keep the access lock till end of transaction
1254  * so that the table can't be deleted or have its schema modified
1255  * underneath us.
1256  */
1257  table_close(rel, NoLock);
1258 
1259  /*
1260  * Set flags and access permissions.
1261  *
1262  * The initial default on access checks is always check-for-READ-access,
1263  * which is the right thing for all except target tables.
1264  */
1265  rte->lateral = false;
1266  rte->inh = inh;
1267  rte->inFromCl = inFromCl;
1268 
1269  rte->requiredPerms = ACL_SELECT;
1270  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1271  rte->selectedCols = NULL;
1272  rte->insertedCols = NULL;
1273  rte->updatedCols = NULL;
1274  rte->extraUpdatedCols = NULL;
1275 
1276  /*
1277  * Add completed RTE to pstate's range table list, but not to join list
1278  * nor namespace --- caller must do that if appropriate.
1279  */
1280  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1281 
1282  return rte;
1283 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
Alias * alias
Definition: parsenodes.h:1091
int LOCKMODE
Definition: lockdefs.h:26
#define AccessShareLock
Definition: lockdefs.h:36
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
AclMode requiredPerms
Definition: parsenodes.h:1096
Form_pg_class rd_rel
Definition: rel.h:83
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
char * relname
Definition: primnodes.h:68
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1101
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define NoLock
Definition: lockdefs.h:34
List * lappend(List *list, void *datum)
Definition: list.c:321
#define RowShareLock
Definition: lockdefs.h:37
#define ACL_SELECT
Definition: parsenodes.h:75
TupleDesc rd_att
Definition: rel.h:84
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1100
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:974
Bitmapset * insertedCols
Definition: parsenodes.h:1099
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
bool isLockedRefname(ParseState *pstate, const char *refname)
Alias * eref
Definition: parsenodes.h:1092
#define RelationGetRelid(relation)
Definition: rel.h:416
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForCTE()

RangeTblEntry* addRangeTableEntryForCTE ( ParseState pstate,
CommonTableExpr cte,
Index  levelsup,
RangeVar rv,
bool  inFromCl 
)

Definition at line 1921 of file parse_relation.c.

References RangeVar::alias, RangeTblEntry::alias, Alias::aliasname, Assert, RangeTblEntry::checkAsUser, CMD_SELECT, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, Query::commandType, copyObject, CommonTableExpr::ctecolcollations, CommonTableExpr::ctecolnames, CommonTableExpr::ctecoltypes, CommonTableExpr::ctecoltypmods, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, CommonTableExpr::ctename, CommonTableExpr::ctequery, CommonTableExpr::cterecursive, CommonTableExpr::cterefcount, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, IsA, lappend(), RangeTblEntry::lateral, lfirst, list_length(), RangeVar::location, makeAlias(), makeNode, NIL, ParseState::p_rtable, parser_errposition(), RangeTblEntry::requiredPerms, Query::returningList, RTE_CTE, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::self_reference, and RangeTblEntry::updatedCols.

Referenced by getRTEForSpecialRelationTypes().

1926 {
1928  Alias *alias = rv->alias;
1929  char *refname = alias ? alias->aliasname : cte->ctename;
1930  Alias *eref;
1931  int numaliases;
1932  int varattno;
1933  ListCell *lc;
1934 
1935  Assert(pstate != NULL);
1936 
1937  rte->rtekind = RTE_CTE;
1938  rte->ctename = cte->ctename;
1939  rte->ctelevelsup = levelsup;
1940 
1941  /* Self-reference if and only if CTE's parse analysis isn't completed */
1942  rte->self_reference = !IsA(cte->ctequery, Query);
1943  Assert(cte->cterecursive || !rte->self_reference);
1944  /* Bump the CTE's refcount if this isn't a self-reference */
1945  if (!rte->self_reference)
1946  cte->cterefcount++;
1947 
1948  /*
1949  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
1950  * This won't get checked in case of a self-reference, but that's OK
1951  * because data-modifying CTEs aren't allowed to be recursive anyhow.
1952  */
1953  if (IsA(cte->ctequery, Query))
1954  {
1955  Query *ctequery = (Query *) cte->ctequery;
1956 
1957  if (ctequery->commandType != CMD_SELECT &&
1958  ctequery->returningList == NIL)
1959  ereport(ERROR,
1960  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1961  errmsg("WITH query \"%s\" does not have a RETURNING clause",
1962  cte->ctename),
1963  parser_errposition(pstate, rv->location)));
1964  }
1965 
1966  rte->coltypes = cte->ctecoltypes;
1967  rte->coltypmods = cte->ctecoltypmods;
1968  rte->colcollations = cte->ctecolcollations;
1969 
1970  rte->alias = alias;
1971  if (alias)
1972  eref = copyObject(alias);
1973  else
1974  eref = makeAlias(refname, NIL);
1975  numaliases = list_length(eref->colnames);
1976 
1977  /* fill in any unspecified alias columns */
1978  varattno = 0;
1979  foreach(lc, cte->ctecolnames)
1980  {
1981  varattno++;
1982  if (varattno > numaliases)
1983  eref->colnames = lappend(eref->colnames, lfirst(lc));
1984  }
1985  if (varattno < numaliases)
1986  ereport(ERROR,
1987  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1988  errmsg("table \"%s\" has %d columns available but %d columns specified",
1989  refname, varattno, numaliases)));
1990 
1991  rte->eref = eref;
1992 
1993  /*
1994  * Set flags and access permissions.
1995  *
1996  * Subqueries are never checked for access rights.
1997  */
1998  rte->lateral = false;
1999  rte->inh = false; /* never true for subqueries */
2000  rte->inFromCl = inFromCl;
2001 
2002  rte->requiredPerms = 0;
2003  rte->checkAsUser = InvalidOid;
2004  rte->selectedCols = NULL;
2005  rte->insertedCols = NULL;
2006  rte->updatedCols = NULL;
2007  rte->extraUpdatedCols = NULL;
2008 
2009  /*
2010  * Add completed RTE to pstate's range table list, but not to join list
2011  * nor namespace --- caller must do that if appropriate.
2012  */
2013  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2014 
2015  return rte;
2016 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Alias * alias
Definition: parsenodes.h:1091
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: parsenodes.h:1079
int errcode(int sqlerrcode)
Definition: elog.c:570
AclMode requiredPerms
Definition: parsenodes.h:1096
List * colcollations
Definition: parsenodes.h:1080
int location
Definition: primnodes.h:73
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1101
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define ERROR
Definition: elog.h:43
List * ctecoltypmods
Definition: parsenodes.h:1439
List * returningList
Definition: parsenodes.h:146
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:321
List * ctecolnames
Definition: parsenodes.h:1437
bool self_reference
Definition: parsenodes.h:1058
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1100
CmdType commandType
Definition: parsenodes.h:112
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Index ctelevelsup
Definition: parsenodes.h:1057
List * ctecoltypes
Definition: parsenodes.h:1438
RTEKind rtekind
Definition: parsenodes.h:974
char * ctename
Definition: parsenodes.h:1056
int errmsg(const char *fmt,...)
Definition: elog.c:784
Bitmapset * insertedCols
Definition: parsenodes.h:1099
Alias * alias
Definition: primnodes.h:72
Alias * eref
Definition: parsenodes.h:1092
List * ctecolcollations
Definition: parsenodes.h:1440
#define copyObject(obj)
Definition: nodes.h:641
List * coltypes
Definition: parsenodes.h:1078
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForENR()

RangeTblEntry* addRangeTableEntryForENR ( ParseState pstate,
RangeVar rv,
bool  inFromCl 
)

Definition at line 2031 of file parse_relation.c.

References RangeVar::alias, Alias::aliasname, Assert, buildRelationAliases(), RangeTblEntry::checkAsUser, RangeTblEntry::colcollations, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, elog, ENR_NAMED_TUPLESTORE, ENRMetadataGetTupDesc(), RangeTblEntry::enrname, EphemeralNamedRelationMetadataData::enrtuples, RangeTblEntry::enrtuples, EphemeralNamedRelationMetadataData::enrtype, RangeTblEntry::eref, ERROR, get_visible_ENR(), RangeTblEntry::inFromCl, RangeTblEntry::inh, InvalidOid, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, makeAlias(), makeNode, EphemeralNamedRelationMetadataData::name, TupleDescData::natts, NIL, ParseState::p_rtable, RangeTblEntry::relid, EphemeralNamedRelationMetadataData::reliddesc, RangeVar::relname, RangeTblEntry::requiredPerms, RTE_NAMEDTUPLESTORE, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, and TupleDescAttr.

Referenced by getRTEForSpecialRelationTypes().

2034 {
2036  Alias *alias = rv->alias;
2037  char *refname = alias ? alias->aliasname : rv->relname;
2039  TupleDesc tupdesc;
2040  int attno;
2041 
2042  Assert(pstate != NULL);
2043  enrmd = get_visible_ENR(pstate, rv->relname);
2044  Assert(enrmd != NULL);
2045 
2046  switch (enrmd->enrtype)
2047  {
2048  case ENR_NAMED_TUPLESTORE:
2050  break;
2051 
2052  default:
2053  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2054  return NULL; /* for fussy compilers */
2055  }
2056 
2057  /*
2058  * Record dependency on a relation. This allows plans to be invalidated
2059  * if they access transition tables linked to a table that is altered.
2060  */
2061  rte->relid = enrmd->reliddesc;
2062 
2063  /*
2064  * Build the list of effective column names using user-supplied aliases
2065  * and/or actual column names.
2066  */
2067  tupdesc = ENRMetadataGetTupDesc(enrmd);
2068  rte->eref = makeAlias(refname, NIL);
2069  buildRelationAliases(tupdesc, alias, rte->eref);
2070 
2071  /* Record additional data for ENR, including column type info */
2072  rte->enrname = enrmd->name;
2073  rte->enrtuples = enrmd->enrtuples;
2074  rte->coltypes = NIL;
2075  rte->coltypmods = NIL;
2076  rte->colcollations = NIL;
2077  for (attno = 1; attno <= tupdesc->natts; ++attno)
2078  {
2079  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2080 
2081  if (att->attisdropped)
2082  {
2083  /* Record zeroes for a dropped column */
2084  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2085  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2087  }
2088  else
2089  {
2090  /* Let's just make sure we can tell this isn't dropped */
2091  if (att->atttypid == InvalidOid)
2092  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2093  rv->relname);
2094  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2095  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2097  att->attcollation);
2098  }
2099  }
2100 
2101  /*
2102  * Set flags and access permissions.
2103  *
2104  * ENRs are never checked for access rights.
2105  */
2106  rte->lateral = false;
2107  rte->inh = false; /* never true for ENRs */
2108  rte->inFromCl = inFromCl;
2109 
2110  rte->requiredPerms = 0;
2111  rte->checkAsUser = InvalidOid;
2112  rte->selectedCols = NULL;
2113 
2114  /*
2115  * Add completed RTE to pstate's range table list, but not to join list
2116  * nor namespace --- caller must do that if appropriate.
2117  */
2118  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2119 
2120  return rte;
2121 }
#define NIL
Definition: pg_list.h:65
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
List * coltypmods
Definition: parsenodes.h:1079
AclMode requiredPerms
Definition: parsenodes.h:1096
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
List * colcollations
Definition: parsenodes.h:1080
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:26
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
char * relname
Definition: primnodes.h:68
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
char * enrname
Definition: parsenodes.h:1085
List * lappend_int(List *list, int datum)
Definition: list.c:339
List * lappend(List *list, void *datum)
Definition: list.c:321
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
char * aliasname
Definition: primnodes.h:42
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
RTEKind rtekind
Definition: parsenodes.h:974
double enrtuples
Definition: parsenodes.h:1086
#define elog(elevel,...)
Definition: elog.h:226
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * alias
Definition: primnodes.h:72
EphemeralNameRelationType enrtype
Alias * eref
Definition: parsenodes.h:1092
List * coltypes
Definition: parsenodes.h:1078
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForFunction()

RangeTblEntry* addRangeTableEntryForFunction ( ParseState pstate,
List funcnames,
List funcexprs,
List coldeflists,
RangeFunction rangefunc,
bool  lateral,
bool  inFromCl 
)

Definition at line 1444 of file parse_relation.c.

References RangeFunction::alias, RangeTblEntry::alias, Alias::aliasname, Assert, buildRelationAliases(), RangeTblEntry::checkAsUser, CheckAttributeNamesTypes(), CHKATYPE_ANYRECORD, chooseScalarFunctionAlias(), ColumnDef::colname, CreateTemplateTupleDesc(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, exprLocation(), RangeTblEntry::extraUpdatedCols, format_type_be(), forthree, RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccolnames, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblFunction::funcparams, RangeTblEntry::functions, get_expr_result_type(), GetColumnDefCollation(), i, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, linitial, list_length(), ColumnDef::location, makeAlias(), makeNode, makeString(), TupleDescData::natts, NIL, RangeFunction::ordinality, ParseState::p_rtable, palloc(), parser_errposition(), pstrdup(), RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_FUNCTION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, TypeName::setof, RangeTblEntry::subquery, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, TYPEFUNC_SCALAR, ColumnDef::typeName, typenameTypeIdAndMod(), and RangeTblEntry::updatedCols.

Referenced by transformRangeFunction().

1451 {
1453  Alias *alias = rangefunc->alias;
1454  Alias *eref;
1455  char *aliasname;
1456  int nfuncs = list_length(funcexprs);
1457  TupleDesc *functupdescs;
1458  TupleDesc tupdesc;
1459  ListCell *lc1,
1460  *lc2,
1461  *lc3;
1462  int i;
1463  int j;
1464  int funcno;
1465  int natts,
1466  totalatts;
1467 
1468  Assert(pstate != NULL);
1469 
1470  rte->rtekind = RTE_FUNCTION;
1471  rte->relid = InvalidOid;
1472  rte->subquery = NULL;
1473  rte->functions = NIL; /* we'll fill this list below */
1474  rte->funcordinality = rangefunc->ordinality;
1475  rte->alias = alias;
1476 
1477  /*
1478  * Choose the RTE alias name. We default to using the first function's
1479  * name even when there's more than one; which is maybe arguable but beats
1480  * using something constant like "table".
1481  */
1482  if (alias)
1483  aliasname = alias->aliasname;
1484  else
1485  aliasname = linitial(funcnames);
1486 
1487  eref = makeAlias(aliasname, NIL);
1488  rte->eref = eref;
1489 
1490  /* Process each function ... */
1491  functupdescs = (TupleDesc *) palloc(nfuncs * sizeof(TupleDesc));
1492 
1493  totalatts = 0;
1494  funcno = 0;
1495  forthree(lc1, funcexprs, lc2, funcnames, lc3, coldeflists)
1496  {
1497  Node *funcexpr = (Node *) lfirst(lc1);
1498  char *funcname = (char *) lfirst(lc2);
1499  List *coldeflist = (List *) lfirst(lc3);
1501  TypeFuncClass functypclass;
1502  Oid funcrettype;
1503 
1504  /* Initialize RangeTblFunction node */
1505  rtfunc->funcexpr = funcexpr;
1506  rtfunc->funccolnames = NIL;
1507  rtfunc->funccoltypes = NIL;
1508  rtfunc->funccoltypmods = NIL;
1509  rtfunc->funccolcollations = NIL;
1510  rtfunc->funcparams = NULL; /* not set until planning */
1511 
1512  /*
1513  * Now determine if the function returns a simple or composite type.
1514  */
1515  functypclass = get_expr_result_type(funcexpr,
1516  &funcrettype,
1517  &tupdesc);
1518 
1519  /*
1520  * A coldeflist is required if the function returns RECORD and hasn't
1521  * got a predetermined record type, and is prohibited otherwise.
1522  */
1523  if (coldeflist != NIL)
1524  {
1525  if (functypclass != TYPEFUNC_RECORD)
1526  ereport(ERROR,
1527  (errcode(ERRCODE_SYNTAX_ERROR),
1528  errmsg("a column definition list is only allowed for functions returning \"record\""),
1529  parser_errposition(pstate,
1530  exprLocation((Node *) coldeflist))));
1531  }
1532  else
1533  {
1534  if (functypclass == TYPEFUNC_RECORD)
1535  ereport(ERROR,
1536  (errcode(ERRCODE_SYNTAX_ERROR),
1537  errmsg("a column definition list is required for functions returning \"record\""),
1538  parser_errposition(pstate, exprLocation(funcexpr))));
1539  }
1540 
1541  if (functypclass == TYPEFUNC_COMPOSITE ||
1542  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
1543  {
1544  /* Composite data type, e.g. a table's row type */
1545  Assert(tupdesc);
1546  }
1547  else if (functypclass == TYPEFUNC_SCALAR)
1548  {
1549  /* Base data type, i.e. scalar */
1550  tupdesc = CreateTemplateTupleDesc(1);
1551  TupleDescInitEntry(tupdesc,
1552  (AttrNumber) 1,
1553  chooseScalarFunctionAlias(funcexpr, funcname,
1554  alias, nfuncs),
1555  funcrettype,
1556  -1,
1557  0);
1558  }
1559  else if (functypclass == TYPEFUNC_RECORD)
1560  {
1561  ListCell *col;
1562 
1563  /*
1564  * Use the column definition list to construct a tupdesc and fill
1565  * in the RangeTblFunction's lists.
1566  */
1567  tupdesc = CreateTemplateTupleDesc(list_length(coldeflist));
1568  i = 1;
1569  foreach(col, coldeflist)
1570  {
1571  ColumnDef *n = (ColumnDef *) lfirst(col);
1572  char *attrname;
1573  Oid attrtype;
1574  int32 attrtypmod;
1575  Oid attrcollation;
1576 
1577  attrname = n->colname;
1578  if (n->typeName->setof)
1579  ereport(ERROR,
1580  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1581  errmsg("column \"%s\" cannot be declared SETOF",
1582  attrname),
1583  parser_errposition(pstate, n->location)));
1584  typenameTypeIdAndMod(pstate, n->typeName,
1585  &attrtype, &attrtypmod);
1586  attrcollation = GetColumnDefCollation(pstate, n, attrtype);
1587  TupleDescInitEntry(tupdesc,
1588  (AttrNumber) i,
1589  attrname,
1590  attrtype,
1591  attrtypmod,
1592  0);
1594  (AttrNumber) i,
1595  attrcollation);
1596  rtfunc->funccolnames = lappend(rtfunc->funccolnames,
1597  makeString(pstrdup(attrname)));
1598  rtfunc->funccoltypes = lappend_oid(rtfunc->funccoltypes,
1599  attrtype);
1600  rtfunc->funccoltypmods = lappend_int(rtfunc->funccoltypmods,
1601  attrtypmod);
1603  attrcollation);
1604 
1605  i++;
1606  }
1607 
1608  /*
1609  * Ensure that the coldeflist defines a legal set of names (no
1610  * duplicates, but we needn't worry about system column names) and
1611  * datatypes. Although we mostly can't allow pseudo-types, it
1612  * seems safe to allow RECORD and RECORD[], since values within
1613  * those type classes are self-identifying at runtime, and the
1614  * coldeflist doesn't represent anything that will be visible to
1615  * other sessions.
1616  */
1617  CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE,
1619  }
1620  else
1621  ereport(ERROR,
1622  (errcode(ERRCODE_DATATYPE_MISMATCH),
1623  errmsg("function \"%s\" in FROM has unsupported return type %s",
1624  funcname, format_type_be(funcrettype)),
1625  parser_errposition(pstate, exprLocation(funcexpr))));
1626 
1627  /* Finish off the RangeTblFunction and add it to the RTE's list */
1628  rtfunc->funccolcount = tupdesc->natts;
1629  rte->functions = lappend(rte->functions, rtfunc);
1630 
1631  /* Save the tupdesc for use below */
1632  functupdescs[funcno] = tupdesc;
1633  totalatts += tupdesc->natts;
1634  funcno++;
1635  }
1636 
1637  /*
1638  * If there's more than one function, or we want an ordinality column, we
1639  * have to produce a merged tupdesc.
1640  */
1641  if (nfuncs > 1 || rangefunc->ordinality)
1642  {
1643  if (rangefunc->ordinality)
1644  totalatts++;
1645 
1646  /* Merge the tuple descs of each function into a composite one */
1647  tupdesc = CreateTemplateTupleDesc(totalatts);
1648  natts = 0;
1649  for (i = 0; i < nfuncs; i++)
1650  {
1651  for (j = 1; j <= functupdescs[i]->natts; j++)
1652  TupleDescCopyEntry(tupdesc, ++natts, functupdescs[i], j);
1653  }
1654 
1655  /* Add the ordinality column if needed */
1656  if (rangefunc->ordinality)
1657  TupleDescInitEntry(tupdesc,
1658  (AttrNumber) ++natts,
1659  "ordinality",
1660  INT8OID,
1661  -1,
1662  0);
1663 
1664  Assert(natts == totalatts);
1665  }
1666  else
1667  {
1668  /* We can just use the single function's tupdesc as-is */
1669  tupdesc = functupdescs[0];
1670  }
1671 
1672  /* Use the tupdesc while assigning column aliases for the RTE */
1673  buildRelationAliases(tupdesc, alias, eref);
1674 
1675  /*
1676  * Set flags and access permissions.
1677  *
1678  * Functions are never checked for access rights (at least, not by the RTE
1679  * permissions mechanism).
1680  */
1681  rte->lateral = lateral;
1682  rte->inh = false; /* never true for functions */
1683  rte->inFromCl = inFromCl;
1684 
1685  rte->requiredPerms = 0;
1686  rte->checkAsUser = InvalidOid;
1687  rte->selectedCols = NULL;
1688  rte->insertedCols = NULL;
1689  rte->updatedCols = NULL;
1690  rte->extraUpdatedCols = NULL;
1691 
1692  /*
1693  * Add completed RTE to pstate's range table list, but not to join list
1694  * nor namespace --- caller must do that if appropriate.
1695  */
1696  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1697 
1698  return rte;
1699 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:567
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1192
Alias * alias
Definition: parsenodes.h:1091
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
static char * chooseScalarFunctionAlias(Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:464
char * pstrdup(const char *in)
Definition: mcxt.c:1161
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:570
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
AclMode requiredPerms
Definition: parsenodes.h:1096
bool funcordinality
Definition: parsenodes.h:1041
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
signed int int32
Definition: c.h:346
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1101
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:271
bool setof
Definition: parsenodes.h:211
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
int location
Definition: parsenodes.h:663
TypeFuncClass
Definition: funcapi.h:147
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend_int(List *list, int datum)
Definition: list.c:339
List * lappend(List *list, void *datum)
Definition: list.c:321
Oid GetColumnDefCollation(ParseState *pstate, ColumnDef *coldef, Oid typeOid)
Definition: parse_type.c:539
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:311
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1130
Bitmapset * updatedCols
Definition: parsenodes.h:1100
List * funccolcollations
Definition: parsenodes.h:1131
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1040
static int list_length(const List *l)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
TypeName * typeName
Definition: parsenodes.h:647
Bitmapset * funcparams
Definition: parsenodes.h:1133
RTEKind rtekind
Definition: parsenodes.h:974
#define CHKATYPE_ANYRECORD
Definition: heap.h:24
Query * subquery
Definition: parsenodes.h:1009
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
Bitmapset * insertedCols
Definition: parsenodes.h:1099
int i
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
char * colname
Definition: parsenodes.h:646
Alias * eref
Definition: parsenodes.h:1092
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, int flags)
Definition: heap.c:490
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForJoin()

RangeTblEntry* addRangeTableEntryForJoin ( ParseState pstate,
List colnames,
JoinType  jointype,
List aliasvars,
Alias alias,
bool  inFromCl 
)

Definition at line 1850 of file parse_relation.c.

References RangeTblEntry::alias, Assert, RangeTblEntry::checkAsUser, Alias::colnames, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, RangeTblEntry::joinaliasvars, RangeTblEntry::jointype, lappend(), RangeTblEntry::lateral, list_concat(), list_copy_tail(), list_length(), makeAlias(), makeNode, MaxAttrNumber, NIL, ParseState::p_rtable, RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_JOIN, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::subquery, and RangeTblEntry::updatedCols.

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

1856 {
1858  Alias *eref;
1859  int numaliases;
1860 
1861  Assert(pstate != NULL);
1862 
1863  /*
1864  * Fail if join has too many columns --- we must be able to reference any
1865  * of the columns with an AttrNumber.
1866  */
1867  if (list_length(aliasvars) > MaxAttrNumber)
1868  ereport(ERROR,
1869  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1870  errmsg("joins can have at most %d columns",
1871  MaxAttrNumber)));
1872 
1873  rte->rtekind = RTE_JOIN;
1874  rte->relid = InvalidOid;
1875  rte->subquery = NULL;
1876  rte->jointype = jointype;
1877  rte->joinaliasvars = aliasvars;
1878  rte->alias = alias;
1879 
1880  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
1881  numaliases = list_length(eref->colnames);
1882 
1883  /* fill in any unspecified alias columns */
1884  if (numaliases < list_length(colnames))
1885  eref->colnames = list_concat(eref->colnames,
1886  list_copy_tail(colnames, numaliases));
1887 
1888  rte->eref = eref;
1889 
1890  /*
1891  * Set flags and access permissions.
1892  *
1893  * Joins are never checked for access rights.
1894  */
1895  rte->lateral = false;
1896  rte->inh = false; /* never true for joins */
1897  rte->inFromCl = inFromCl;
1898 
1899  rte->requiredPerms = 0;
1900  rte->checkAsUser = InvalidOid;
1901  rte->selectedCols = NULL;
1902  rte->insertedCols = NULL;
1903  rte->updatedCols = NULL;
1904  rte->extraUpdatedCols = NULL;
1905 
1906  /*
1907  * Add completed RTE to pstate's range table list, but not to join list
1908  * nor namespace --- caller must do that if appropriate.
1909  */
1910  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1911 
1912  return rte;
1913 }
#define NIL
Definition: pg_list.h:65
List * joinaliasvars
Definition: parsenodes.h:1030
Alias * alias
Definition: parsenodes.h:1091
List * colnames
Definition: primnodes.h:43
#define MaxAttrNumber
Definition: attnum.h:24
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
int errcode(int sqlerrcode)
Definition: elog.c:570
AclMode requiredPerms
Definition: parsenodes.h:1096
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1419
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1101
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define ERROR
Definition: elog.h:43
JoinType jointype
Definition: parsenodes.h:1029
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:321
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1100
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
static int list_length(const List *l)
Definition: pg_list.h:169
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
int errmsg(const char *fmt,...)
Definition: elog.c:784
Bitmapset * insertedCols
Definition: parsenodes.h:1099
Alias * eref
Definition: parsenodes.h:1092
#define copyObject(obj)
Definition: nodes.h:641
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForRelation()

RangeTblEntry* addRangeTableEntryForRelation ( ParseState pstate,
Relation  rel,
int  lockmode,
Alias alias,
bool  inh,
bool  inFromCl 
)

Definition at line 1301 of file parse_relation.c.

References AccessShareLock, ACL_SELECT, RangeTblEntry::alias, Alias::aliasname, Assert, buildRelationAliases(), RangeTblEntry::checkAsUser, CheckRelationLockedByMe(), RangeTblEntry::eref, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), RangeTblEntry::lateral, makeAlias(), makeNode, NIL, ParseState::p_rtable, RelationData::rd_att, RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, RangeTblEntry::requiredPerms, RowExclusiveLock, RowShareLock, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, and RangeTblEntry::updatedCols.

Referenced by AddRelationNewConstraints(), AlterPolicy(), copy_table(), CreatePolicy(), CreateTrigger(), DefineRelation(), DoCopy(), RemoveRoleFromObjectPolicy(), rewriteTargetView(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformOnConflictClause(), transformPartitionSpec(), transformRuleStmt(), and UpdateRangeTableOfViewParse().

1307 {
1309  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1310 
1311  Assert(pstate != NULL);
1312 
1313  Assert(lockmode == AccessShareLock ||
1314  lockmode == RowShareLock ||
1315  lockmode == RowExclusiveLock);
1316  Assert(CheckRelationLockedByMe(rel, lockmode, true));
1317 
1318  rte->rtekind = RTE_RELATION;
1319  rte->alias = alias;
1320  rte->relid = RelationGetRelid(rel);
1321  rte->relkind = rel->rd_rel->relkind;
1322  rte->rellockmode = lockmode;
1323 
1324  /*
1325  * Build the list of effective column names using user-supplied aliases
1326  * and/or actual column names.
1327  */
1328  rte->eref = makeAlias(refname, NIL);
1329  buildRelationAliases(rel->rd_att, alias, rte->eref);
1330 
1331  /*
1332  * Set flags and access permissions.
1333  *
1334  * The initial default on access checks is always check-for-READ-access,
1335  * which is the right thing for all except target tables.
1336  */
1337  rte->lateral = false;
1338  rte->inh = inh;
1339  rte->inFromCl = inFromCl;
1340 
1341  rte->requiredPerms = ACL_SELECT;
1342  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1343  rte->selectedCols = NULL;
1344  rte->insertedCols = NULL;
1345  rte->updatedCols = NULL;
1346  rte->extraUpdatedCols = NULL;
1347 
1348  /*
1349  * Add completed RTE to pstate's range table list, but not to join list
1350  * nor namespace --- caller must do that if appropriate.
1351  */
1352  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1353 
1354  return rte;
1355 }
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1091
#define AccessShareLock
Definition: lockdefs.h:36
AclMode requiredPerms
Definition: parsenodes.h:1096
Form_pg_class rd_rel
Definition: rel.h:83
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1101
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:450
List * lappend(List *list, void *datum)
Definition: list.c:321
#define RowShareLock
Definition: lockdefs.h:37
#define ACL_SELECT
Definition: parsenodes.h:75
TupleDesc rd_att
Definition: rel.h:84
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1100
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
char * aliasname
Definition: primnodes.h:42
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:302
RTEKind rtekind
Definition: parsenodes.h:974
Bitmapset * insertedCols
Definition: parsenodes.h:1099
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * eref
Definition: parsenodes.h:1092
#define RelationGetRelid(relation)
Definition: rel.h:416
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForSubquery()

RangeTblEntry* addRangeTableEntryForSubquery ( ParseState pstate,
Query subquery,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1364 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Assert, RangeTblEntry::checkAsUser, Alias::colnames, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), RangeTblEntry::lateral, lfirst, list_length(), makeNode, makeString(), ParseState::p_rtable, pstrdup(), RangeTblEntry::requiredPerms, TargetEntry::resjunk, TargetEntry::resname, TargetEntry::resno, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::subquery, Query::targetList, and RangeTblEntry::updatedCols.

Referenced by convert_ANY_sublink_to_join(), transformInsertStmt(), transformRangeSubselect(), and transformSetOperationTree().

1369 {
1371  char *refname = alias->aliasname;
1372  Alias *eref;
1373  int numaliases;
1374  int varattno;
1375  ListCell *tlistitem;
1376 
1377  Assert(pstate != NULL);
1378 
1379  rte->rtekind = RTE_SUBQUERY;
1380  rte->subquery = subquery;
1381  rte->alias = alias;
1382 
1383  eref = copyObject(alias);
1384  numaliases = list_length(eref->colnames);
1385 
1386  /* fill in any unspecified alias columns */
1387  varattno = 0;
1388  foreach(tlistitem, subquery->targetList)
1389  {
1390  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1391 
1392  if (te->resjunk)
1393  continue;
1394  varattno++;
1395  Assert(varattno == te->resno);
1396  if (varattno > numaliases)
1397  {
1398  char *attrname;
1399 
1400  attrname = pstrdup(te->resname);
1401  eref->colnames = lappend(eref->colnames, makeString(attrname));
1402  }
1403  }
1404  if (varattno < numaliases)
1405  ereport(ERROR,
1406  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1407  errmsg("table \"%s\" has %d columns available but %d columns specified",
1408  refname, varattno, numaliases)));
1409 
1410  rte->eref = eref;
1411 
1412  /*
1413  * Set flags and access permissions.
1414  *
1415  * Subqueries are never checked for access rights.
1416  */
1417  rte->lateral = lateral;
1418  rte->inh = false; /* never true for subqueries */
1419  rte->inFromCl = inFromCl;
1420 
1421  rte->requiredPerms = 0;
1422  rte->checkAsUser = InvalidOid;
1423  rte->selectedCols = NULL;
1424  rte->insertedCols = NULL;
1425  rte->updatedCols = NULL;
1426  rte->extraUpdatedCols = NULL;
1427 
1428  /*
1429  * Add completed RTE to pstate's range table list, but not to join list
1430  * nor namespace --- caller must do that if appropriate.
1431  */
1432  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1433 
1434  return rte;
1435 }
Value * makeString(char *str)
Definition: value.c:53
Alias * alias
Definition: parsenodes.h:1091
List * colnames
Definition: primnodes.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1161
int errcode(int sqlerrcode)
Definition: elog.c:570
AclMode requiredPerms
Definition: parsenodes.h:1096
char * resname
Definition: primnodes.h:1395
List * targetList
Definition: parsenodes.h:140
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1101
Bitmapset * selectedCols
Definition: parsenodes.h:1098
bool resjunk
Definition: primnodes.h:1400
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1394
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:321
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1100
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
int errmsg(const char *fmt,...)
Definition: elog.c:784
Bitmapset * insertedCols
Definition: parsenodes.h:1099
Alias * eref
Definition: parsenodes.h:1092
#define copyObject(obj)
Definition: nodes.h:641
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForTableFunc()

RangeTblEntry* addRangeTableEntryForTableFunc ( ParseState pstate,
TableFunc tf,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1707 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Assert, RangeTblEntry::checkAsUser, TableFunc::colcollations, RangeTblEntry::colcollations, Alias::colnames, TableFunc::colnames, TableFunc::coltypes, RangeTblEntry::coltypes, TableFunc::coltypmods, RangeTblEntry::coltypmods, copyObject, RangeTblEntry::eref, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), RangeTblEntry::lateral, list_concat(), list_copy_tail(), list_length(), makeAlias(), makeNode, NIL, ParseState::p_rtable, pstrdup(), RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_TABLEFUNC, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::subquery, RangeTblEntry::tablefunc, and RangeTblEntry::updatedCols.

Referenced by transformRangeTableFunc().

1712 {
1714  char *refname = alias ? alias->aliasname : pstrdup("xmltable");
1715  Alias *eref;
1716  int numaliases;
1717 
1718  Assert(pstate != NULL);
1719 
1720  rte->rtekind = RTE_TABLEFUNC;
1721  rte->relid = InvalidOid;
1722  rte->subquery = NULL;
1723  rte->tablefunc = tf;
1724  rte->coltypes = tf->coltypes;
1725  rte->coltypmods = tf->coltypmods;
1726  rte->colcollations = tf->colcollations;
1727  rte->alias = alias;
1728 
1729  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1730  numaliases = list_length(eref->colnames);
1731 
1732  /* fill in any unspecified alias columns */
1733  if (numaliases < list_length(tf->colnames))
1734  eref->colnames = list_concat(eref->colnames,
1735  list_copy_tail(tf->colnames, numaliases));
1736 
1737  rte->eref = eref;
1738 
1739  /*
1740  * Set flags and access permissions.
1741  *
1742  * Tablefuncs are never checked for access rights (at least, not by the
1743  * RTE permissions mechanism).
1744  */
1745  rte->lateral = lateral;
1746  rte->inh = false; /* never true for tablefunc RTEs */
1747  rte->inFromCl = inFromCl;
1748 
1749  rte->requiredPerms = 0;
1750  rte->checkAsUser = InvalidOid;
1751  rte->selectedCols = NULL;
1752  rte->insertedCols = NULL;
1753  rte->updatedCols = NULL;
1754  rte->extraUpdatedCols = NULL;
1755 
1756  /*
1757  * Add completed RTE to pstate's range table list, but not to join list
1758  * nor namespace --- caller must do that if appropriate.
1759  */
1760  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1761 
1762  return rte;
1763 }
#define NIL
Definition: pg_list.h:65
List * colnames
Definition: primnodes.h:89
Alias * alias
Definition: parsenodes.h:1091
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: primnodes.h:91
char * pstrdup(const char *in)
Definition: mcxt.c:1161
List * coltypmods
Definition: parsenodes.h:1079
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
AclMode requiredPerms
Definition: parsenodes.h:1096
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1419
List * colcollations
Definition: parsenodes.h:1080
List * colcollations
Definition: primnodes.h:92
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1101
Bitmapset * selectedCols
Definition: parsenodes.h:1098
TableFunc * tablefunc
Definition: parsenodes.h:1046
List * lappend(List *list, void *datum)
Definition: list.c:321
List * coltypes
Definition: primnodes.h:90
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1100
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
Bitmapset * insertedCols
Definition: parsenodes.h:1099
Alias * eref
Definition: parsenodes.h:1092
#define copyObject(obj)
Definition: nodes.h:641
List * coltypes
Definition: parsenodes.h:1078
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForValues()

RangeTblEntry* addRangeTableEntryForValues ( ParseState pstate,
List exprs,
List coltypes,
List coltypmods,
List colcollations,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1771 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Assert, RangeTblEntry::checkAsUser, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::extraUpdatedCols, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), RangeTblEntry::lateral, linitial, list_length(), makeAlias(), makeNode, makeString(), NIL, ParseState::p_rtable, pstrdup(), RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, snprintf, RangeTblEntry::subquery, RangeTblEntry::updatedCols, and RangeTblEntry::values_lists.

Referenced by transformInsertStmt(), and transformValuesClause().

1779 {
1781  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
1782  Alias *eref;
1783  int numaliases;
1784  int numcolumns;
1785 
1786  Assert(pstate != NULL);
1787 
1788  rte->rtekind = RTE_VALUES;
1789  rte->relid = InvalidOid;
1790  rte->subquery = NULL;
1791  rte->values_lists = exprs;
1792  rte->coltypes = coltypes;
1793  rte->coltypmods = coltypmods;
1794  rte->colcollations = colcollations;
1795  rte->alias = alias;
1796 
1797  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1798 
1799  /* fill in any unspecified alias columns */
1800  numcolumns = list_length((List *) linitial(exprs));
1801  numaliases = list_length(eref->colnames);
1802  while (numaliases < numcolumns)
1803  {
1804  char attrname[64];
1805 
1806  numaliases++;
1807  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
1808  eref->colnames = lappend(eref->colnames,
1809  makeString(pstrdup(attrname)));
1810  }
1811  if (numcolumns < numaliases)
1812  ereport(ERROR,
1813  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1814  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
1815  refname, numcolumns, numaliases)));
1816 
1817  rte->eref = eref;
1818 
1819  /*
1820  * Set flags and access permissions.
1821  *
1822  * Subqueries are never checked for access rights.
1823  */
1824  rte->lateral = lateral;
1825  rte->inh = false; /* never true for values RTEs */
1826  rte->inFromCl = inFromCl;
1827 
1828  rte->requiredPerms = 0;
1829  rte->checkAsUser = InvalidOid;
1830  rte->selectedCols = NULL;
1831  rte->insertedCols = NULL;
1832  rte->updatedCols = NULL;
1833  rte->extraUpdatedCols = NULL;
1834 
1835  /*
1836  * Add completed RTE to pstate's range table list, but not to join list
1837  * nor namespace --- caller must do that if appropriate.
1838  */
1839  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1840 
1841  return rte;
1842 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1091
List * colnames
Definition: primnodes.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1161
List * coltypmods
Definition: parsenodes.h:1079
int errcode(int sqlerrcode)
Definition: elog.c:570
AclMode requiredPerms
Definition: parsenodes.h:1096
List * colcollations
Definition: parsenodes.h:1080
List * values_lists
Definition: parsenodes.h:1051
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1101
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:321
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1100
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
int errmsg(const char *fmt,...)
Definition: elog.c:784
Bitmapset * insertedCols
Definition: parsenodes.h:1099
Alias * eref
Definition: parsenodes.h:1092
#define copyObject(obj)
Definition: nodes.h:641
List * coltypes
Definition: parsenodes.h:1078
Definition: pg_list.h:50
#define snprintf
Definition: port.h:192
List * p_rtable
Definition: parse_node.h:177

◆ addRTEtoQuery()

void addRTEtoQuery ( ParseState pstate,
RangeTblEntry rte,
bool  addToJoinList,
bool  addToRelNameSpace,
bool  addToVarNameSpace 
)

Definition at line 2182 of file parse_relation.c.

References lappend(), makeNode, ParseNamespaceItem::p_cols_visible, ParseState::p_joinlist, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, palloc(), RTERangeTablePosn(), and RangeTblRef::rtindex.

Referenced by AddRelationNewConstraints(), AlterPolicy(), CreatePolicy(), CreateTrigger(), DefineRelation(), DoCopy(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformInsertStmt(), transformOnConflictArbiter(), transformOnConflictClause(), transformPartitionSpec(), transformRuleStmt(), transformSetOperationStmt(), and transformValuesClause().

2185 {
2186  if (addToJoinList)
2187  {
2188  int rtindex = RTERangeTablePosn(pstate, rte, NULL);
2190 
2191  rtr->rtindex = rtindex;
2192  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2193  }
2194  if (addToRelNameSpace || addToVarNameSpace)
2195  {
2196  ParseNamespaceItem *nsitem;
2197 
2198  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2199  nsitem->p_rte = rte;
2200  nsitem->p_rel_visible = addToRelNameSpace;
2201  nsitem->p_cols_visible = addToVarNameSpace;
2202  nsitem->p_lateral_only = false;
2203  nsitem->p_lateral_ok = true;
2204  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2205  }
2206 }
RangeTblEntry * p_rte
Definition: parse_node.h:251
List * p_namespace
Definition: parse_node.h:181
List * lappend(List *list, void *datum)
Definition: list.c:321
#define makeNode(_type_)
Definition: nodes.h:573
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
void * palloc(Size size)
Definition: mcxt.c:924
List * p_joinlist
Definition: parse_node.h:179

◆ attnameAttNum()

int attnameAttNum ( Relation  rd,
const char *  attname,
bool  sysColOK 
)

Definition at line 3143 of file parse_relation.c.

References i, InvalidAttrNumber, namestrcmp(), RelationData::rd_att, RelationGetNumberOfAttributes, specialAttNum(), and TupleDescAttr.

Referenced by checkInsertTargets(), CreateTrigger(), do_analyze_rel(), and transformUpdateTargetList().

3144 {
3145  int i;
3146 
3147  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3148  {
3149  Form_pg_attribute att = TupleDescAttr(rd->rd_att, i);
3150 
3151  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3152  return i + 1;
3153  }
3154 
3155  if (sysColOK)
3156  {
3157  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
3158  return i;
3159  }
3160 
3161  /* on failure */
3162  return InvalidAttrNumber;
3163 }
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:422
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int namestrcmp(Name name, const char *str)
Definition: name.c:287
static int specialAttNum(const char *attname)
NameData attname
Definition: pg_attribute.h:40
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
TupleDesc rd_att
Definition: rel.h:84
#define InvalidAttrNumber
Definition: attnum.h:23
int i

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3193 of file parse_relation.c.

References elog, ERROR, FormData_pg_attribute, TupleDescData::natts, RelationData::rd_att, SystemAttributeDefinition(), and TupleDescAttr.

Referenced by transformFkeyGetPrimaryKey().

3194 {
3195  if (attid <= 0)
3196  {
3197  const FormData_pg_attribute *sysatt;
3198 
3199  sysatt = SystemAttributeDefinition(attid);
3200  return &sysatt->attname;
3201  }
3202  if (attid > rd->rd_att->natts)
3203  elog(ERROR, "invalid attribute number %d", attid);
3204  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3205 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:250
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:84
FormData_pg_attribute
Definition: pg_attribute.h:184
#define elog(elevel,...)
Definition: elog.h:226

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3235 of file parse_relation.c.

References elog, ERROR, InvalidOid, TupleDescData::natts, RelationData::rd_att, and TupleDescAttr.

3236 {
3237  if (attid <= 0)
3238  {
3239  /* All system attributes are of noncollatable types. */
3240  return InvalidOid;
3241  }
3242  if (attid > rd->rd_att->natts)
3243  elog(ERROR, "invalid attribute number %d", attid);
3244  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3245 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:84
#define InvalidOid
Definition: postgres_ext.h:36
#define elog(elevel,...)
Definition: elog.h:226

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3215 of file parse_relation.c.

References elog, ERROR, FormData_pg_attribute, TupleDescData::natts, RelationData::rd_att, SystemAttributeDefinition(), and TupleDescAttr.

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

3216 {
3217  if (attid <= 0)
3218  {
3219  const FormData_pg_attribute *sysatt;
3220 
3221  sysatt = SystemAttributeDefinition(attid);
3222  return sysatt->atttypid;
3223  }
3224  if (attid > rd->rd_att->natts)
3225  elog(ERROR, "invalid attribute number %d", attid);
3226  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3227 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:250
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:84
FormData_pg_attribute
Definition: pg_attribute.h:184
#define elog(elevel,...)
Definition: elog.h:226

◆ buildRelationAliases()

static void buildRelationAliases ( TupleDesc  tupdesc,
Alias alias,
Alias eref 
)
static

Definition at line 1044 of file parse_relation.c.

References Alias::aliasname, Assert, Alias::colnames, ereport, errcode(), errmsg(), ERROR, lappend(), lfirst, list_head(), list_length(), lnext(), makeString(), NameStr, TupleDescData::natts, NIL, pstrdup(), and TupleDescAttr.

Referenced by addRangeTableEntry(), addRangeTableEntryForENR(), addRangeTableEntryForFunction(), and addRangeTableEntryForRelation().

1045 {
1046  int maxattrs = tupdesc->natts;
1047  List *aliaslist;
1048  ListCell *aliaslc;
1049  int numaliases;
1050  int varattno;
1051  int numdropped = 0;
1052 
1053  Assert(eref->colnames == NIL);
1054 
1055  if (alias)
1056  {
1057  aliaslist = alias->colnames;
1058  aliaslc = list_head(aliaslist);
1059  numaliases = list_length(aliaslist);
1060  /* We'll rebuild the alias colname list */
1061  alias->colnames = NIL;
1062  }
1063  else
1064  {
1065  aliaslist = NIL;
1066  aliaslc = NULL;
1067  numaliases = 0;
1068  }
1069 
1070  for (varattno = 0; varattno < maxattrs; varattno++)
1071  {
1072  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1073  Value *attrname;
1074 
1075  if (attr->attisdropped)
1076  {
1077  /* Always insert an empty string for a dropped column */
1078  attrname = makeString(pstrdup(""));
1079  if (aliaslc)
1080  alias->colnames = lappend(alias->colnames, attrname);
1081  numdropped++;
1082  }
1083  else if (aliaslc)
1084  {
1085  /* Use the next user-supplied alias */
1086  attrname = (Value *) lfirst(aliaslc);
1087  aliaslc = lnext(aliaslist, aliaslc);
1088  alias->colnames = lappend(alias->colnames, attrname);
1089  }
1090  else
1091  {
1092  attrname = makeString(pstrdup(NameStr(attr->attname)));
1093  /* we're done with the alias if any */
1094  }
1095 
1096  eref->colnames = lappend(eref->colnames, attrname);
1097  }
1098 
1099  /* Too many user-supplied aliases? */
1100  if (aliaslc)
1101  ereport(ERROR,
1102  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1103  errmsg("table \"%s\" has %d columns available but %d columns specified",
1104  eref->aliasname, maxattrs - numdropped, numaliases)));
1105 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
List * colnames
Definition: primnodes.h:43
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1161
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:321
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
Definition: value.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define NameStr(name)
Definition: c.h:609
Definition: pg_list.h:50

◆ check_lateral_ref_ok()

static void check_lateral_ref_ok ( ParseState pstate,
ParseNamespaceItem nsitem,
int  location 
)
static

Definition at line 438 of file parse_relation.c.

References Alias::aliasname, RangeTblEntry::eref, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_rte, ParseState::p_target_rangetblentry, and parser_errposition().

Referenced by colNameToVar(), scanNameSpaceForRefname(), and scanNameSpaceForRelid().

440 {
441  if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
442  {
443  /* SQL:2008 demands this be an error, not an invisible item */
444  RangeTblEntry *rte = nsitem->p_rte;
445  char *refname = rte->eref->aliasname;
446 
447  ereport(ERROR,
448  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
449  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
450  refname),
451  (rte == pstate->p_target_rangetblentry) ?
452  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
453  refname) :
454  errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
455  parser_errposition(pstate, location)));
456  }
457 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
RangeTblEntry * p_rte
Definition: parse_node.h:251
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define ereport(elevel, rest)
Definition: elog.h:141
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:188
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:784
Alias * eref
Definition: parsenodes.h:1092

◆ checkNameSpaceConflicts()

void checkNameSpaceConflicts ( ParseState pstate,
List namespace1,
List namespace2 
)

Definition at line 392 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, lfirst, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, RangeTblEntry::relid, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by transformFromClause(), and transformFromClauseItem().

394 {
395  ListCell *l1;
396 
397  foreach(l1, namespace1)
398  {
399  ParseNamespaceItem *nsitem1 = (ParseNamespaceItem *) lfirst(l1);
400  RangeTblEntry *rte1 = nsitem1->p_rte;
401  const char *aliasname1 = rte1->eref->aliasname;
402  ListCell *l2;
403 
404  if (!nsitem1->p_rel_visible)
405  continue;
406 
407  foreach(l2, namespace2)
408  {
409  ParseNamespaceItem *nsitem2 = (ParseNamespaceItem *) lfirst(l2);
410  RangeTblEntry *rte2 = nsitem2->p_rte;
411 
412  if (!nsitem2->p_rel_visible)
413  continue;
414  if (strcmp(rte2->eref->aliasname, aliasname1) != 0)
415  continue; /* definitely no conflict */
416  if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
417  rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
418  rte1->relid != rte2->relid)
419  continue; /* no conflict per SQL rule */
420  ereport(ERROR,
421  (errcode(ERRCODE_DUPLICATE_ALIAS),
422  errmsg("table name \"%s\" specified more than once",
423  aliasname1)));
424  }
425  }
426 }
Alias * alias
Definition: parsenodes.h:1091
int errcode(int sqlerrcode)
Definition: elog.c:570
RangeTblEntry * p_rte
Definition: parse_node.h:251
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:974
int errmsg(const char *fmt,...)
Definition: elog.c:784
Alias * eref
Definition: parsenodes.h:1092

◆ chooseScalarFunctionAlias()

static char* chooseScalarFunctionAlias ( Node funcexpr,
char *  funcname,
Alias alias,
int  nfuncs 
)
static

Definition at line 1121 of file parse_relation.c.

References Alias::aliasname, get_func_result_name(), and IsA.

Referenced by addRangeTableEntryForFunction().

1123 {
1124  char *pname;
1125 
1126  /*
1127  * If the expression is a simple function call, and the function has a
1128  * single OUT parameter that is named, use the parameter's name.
1129  */
1130  if (funcexpr && IsA(funcexpr, FuncExpr))
1131  {
1132  pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1133  if (pname)
1134  return pname;
1135  }
1136 
1137  /*
1138  * If there's just one function in the RTE, and the user gave an RTE alias
1139  * name, use that name. (This makes FROM func() AS foo use "foo" as the
1140  * column name as well as the table alias.)
1141  */
1142  if (nfuncs == 1 && alias)
1143  return alias->aliasname;
1144 
1145  /*
1146  * Otherwise use the function name.
1147  */
1148  return funcname;
1149 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
char * aliasname
Definition: primnodes.h:42
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1058

◆ colNameToVar()

Node* colNameToVar ( ParseState pstate,
const char *  colname,
bool  localonly,
int  location 
)

Definition at line 771 of file parse_relation.c.

References check_lateral_ref_ok(), ereport, errcode(), errmsg(), ERROR, lfirst, ParseNamespaceItem::p_cols_visible, ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rte, ParseState::parentParseState, parser_errposition(), and scanRTEForColumn().

Referenced by findTargetlistEntrySQL92(), and transformColumnRef().

773 {
774  Node *result = NULL;
775  ParseState *orig_pstate = pstate;
776 
777  while (pstate != NULL)
778  {
779  ListCell *l;
780 
781  foreach(l, pstate->p_namespace)
782  {
784  RangeTblEntry *rte = nsitem->p_rte;
785  Node *newresult;
786 
787  /* Ignore table-only items */
788  if (!nsitem->p_cols_visible)
789  continue;
790  /* If not inside LATERAL, ignore lateral-only items */
791  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
792  continue;
793 
794  /* use orig_pstate here to get the right sublevels_up */
795  newresult = scanRTEForColumn(orig_pstate, rte, colname, location,
796  0, NULL);
797 
798  if (newresult)
799  {
800  if (result)
801  ereport(ERROR,
802  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
803  errmsg("column reference \"%s\" is ambiguous",
804  colname),
805  parser_errposition(pstate, location)));
806  check_lateral_ref_ok(pstate, nsitem, location);
807  result = newresult;
808  }
809  }
810 
811  if (result != NULL || localonly)
812  break; /* found, or don't want to look at parent */
813 
814  pstate = pstate->parentParseState;
815  }
816 
817  return result;
818 }
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:570
RangeTblEntry * p_rte
Definition: parse_node.h:251
Node * scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
#define ERROR
Definition: elog.h:43
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
List * p_namespace
Definition: parse_node.h:181
#define ereport(elevel, rest)
Definition: elog.h:141
struct ParseState * parentParseState
Definition: parse_node.h:175
bool p_lateral_active
Definition: parse_node.h:183
#define lfirst(lc)
Definition: pg_list.h:190
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ errorMissingColumn()

void errorMissingColumn ( ParseState pstate,
const char *  relname,
const char *  colname,
int  location 
)

Definition at line 3310 of file parse_relation.c.

References Alias::aliasname, AttributeNumberIsValid, Alias::colnames, RangeTblEntry::eref, ereport, errcode(), errhint(), errmsg(), ERROR, FuzzyAttrMatchState::first, list_nth(), parser_errposition(), FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, searchRangeTableForCol(), FuzzyAttrMatchState::second, and strVal.

Referenced by transformColumnRef().

3312 {
3314  char *closestfirst = NULL;
3315 
3316  /*
3317  * Search the entire rtable looking for possible matches. If we find one,
3318  * emit a hint about it.
3319  *
3320  * TODO: improve this code (and also errorMissingRTE) to mention using
3321  * LATERAL if appropriate.
3322  */
3323  state = searchRangeTableForCol(pstate, relname, colname, location);
3324 
3325  /*
3326  * Extract closest col string for best match, if any.
3327  *
3328  * Infer an exact match referenced despite not being visible from the fact
3329  * that an attribute number was not present in state passed back -- this
3330  * is what is reported when !closestfirst. There might also be an exact
3331  * match that was qualified with an incorrect alias, in which case
3332  * closestfirst will be set (so hint is the same as generic fuzzy case).
3333  */
3334  if (state->rfirst && AttributeNumberIsValid(state->first))
3335  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3336  state->first - 1));
3337 
3338  if (!state->rsecond)
3339  {
3340  /*
3341  * Handle case where there is zero or one column suggestions to hint,
3342  * including exact matches referenced but not visible.
3343  */
3344  ereport(ERROR,
3345  (errcode(ERRCODE_UNDEFINED_COLUMN),
3346  relname ?
3347  errmsg("column %s.%s does not exist", relname, colname) :
3348  errmsg("column \"%s\" does not exist", colname),
3349  state->rfirst ? closestfirst ?
3350  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3351  state->rfirst->eref->aliasname, closestfirst) :
3352  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3353  colname, state->rfirst->eref->aliasname) : 0,
3354  parser_errposition(pstate, location)));
3355  }
3356  else
3357  {
3358  /* Handle case where there are two equally useful column hints */
3359  char *closestsecond;
3360 
3361  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3362  state->second - 1));
3363 
3364  ereport(ERROR,
3365  (errcode(ERRCODE_UNDEFINED_COLUMN),
3366  relname ?
3367  errmsg("column %s.%s does not exist", relname, colname) :
3368  errmsg("column \"%s\" does not exist", colname),
3369  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3370  state->rfirst->eref->aliasname, closestfirst,
3371  state->rsecond->eref->aliasname, closestsecond),
3372  parser_errposition(pstate, location)));
3373  }
3374 }
int errhint(const char *fmt,...)
Definition: elog.c:974
List * colnames
Definition: primnodes.h:43
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colname, int location)
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:570
NameData relname
Definition: pg_class.h:35
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
RangeTblEntry * rfirst
#define ereport(elevel, rest)
Definition: elog.h:141
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
char * aliasname
Definition: primnodes.h:42
Definition: regguts.h:298
RangeTblEntry * rsecond
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:784
Alias * eref
Definition: parsenodes.h:1092

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3254 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, RangeTblEntry::eref, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errhint(), errmsg(), ERROR, RangeVar::location, parser_errposition(), refnameRangeTblEntry(), RangeVar::relname, and searchRangeTableForRel().

Referenced by ExpandColumnRefStar(), and transformColumnRef().

3255 {
3256  RangeTblEntry *rte;
3257  int sublevels_up;
3258  const char *badAlias = NULL;
3259 
3260  /*
3261  * Check to see if there are any potential matches in the query's
3262  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3263  * will throw error immediately here. That seems OK.)
3264  */
3265  rte = searchRangeTableForRel(pstate, relation);
3266 
3267  /*
3268  * If we found a match that has an alias and the alias is visible in the
3269  * namespace, then the problem is probably use of the relation's real name
3270  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3271  * common enough to justify a specific hint.
3272  *
3273  * If we found a match that doesn't meet those criteria, assume the
3274  * problem is illegal use of a relation outside its scope, as in the
3275  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3276  */
3277  if (rte && rte->alias &&
3278  strcmp(rte->eref->aliasname, relation->relname) != 0 &&
3279  refnameRangeTblEntry(pstate, NULL, rte->eref->aliasname,
3280  relation->location,
3281  &sublevels_up) == rte)
3282  badAlias = rte->eref->aliasname;
3283 
3284  if (rte)
3285  ereport(ERROR,
3287  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3288  relation->relname),
3289  (badAlias ?
3290  errhint("Perhaps you meant to reference the table alias \"%s\".",
3291  badAlias) :
3292  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3293  rte->eref->aliasname)),
3294  parser_errposition(pstate, relation->location)));
3295  else
3296  ereport(ERROR,
3298  errmsg("missing FROM-clause entry for table \"%s\"",
3299  relation->relname),
3300  parser_errposition(pstate, relation->location)));
3301 }
int errhint(const char *fmt,...)
Definition: elog.c:974
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:72
Alias * alias
Definition: parsenodes.h:1091
int errcode(int sqlerrcode)
Definition: elog.c:570
RangeTblEntry * refnameRangeTblEntry(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
int location
Definition: primnodes.h:73
char * relname
Definition: primnodes.h:68
static RangeTblEntry * searchRangeTableForRel(ParseState *pstate, RangeVar *relation)
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:784
Alias * eref
Definition: parsenodes.h:1092

◆ expandRelation()

static void expandRelation ( Oid  relid,
Alias eref,
int  rtindex,
int  sublevels_up,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)
static

Definition at line 2562 of file parse_relation.c.

References AccessShareLock, expandTupleDesc(), TupleDescData::natts, RelationData::rd_att, relation_close(), and relation_open().

Referenced by expandRTE().

2565 {
2566  Relation rel;
2567 
2568  /* Get the tupledesc and turn it over to expandTupleDesc */
2569  rel = relation_open(relid, AccessShareLock);
2570  expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
2571  rtindex, sublevels_up,
2572  location, include_dropped,
2573  colnames, colvars);
2575 }
#define AccessShareLock
Definition: lockdefs.h:36
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
TupleDesc rd_att
Definition: rel.h:84
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)

◆ expandRelAttrs()

List* expandRelAttrs ( ParseState pstate,
RangeTblEntry rte,
int  rtindex,
int  sublevels_up,
int  location 
)

Definition at line 2667 of file parse_relation.c.

References ACL_SELECT, Assert, expandRTE(), forboth, label, lappend(), lfirst, makeTargetEntry(), markVarForSelectPriv(), name, NIL, ParseState::p_next_resno, RangeTblEntry::requiredPerms, and strVal.

Referenced by ExpandAllTables(), ExpandSingleTable(), and transformValuesClause().

2669 {
2670  List *names,
2671  *vars;
2672  ListCell *name,
2673  *var;
2674  List *te_list = NIL;
2675 
2676  expandRTE(rte, rtindex, sublevels_up, location, false,
2677  &names, &vars);
2678 
2679  /*
2680  * Require read access to the table. This is normally redundant with the
2681  * markVarForSelectPriv calls below, but not if the table has zero
2682  * columns.
2683  */
2684  rte->requiredPerms |= ACL_SELECT;
2685 
2686  forboth(name, names, var, vars)
2687  {
2688  char *label = strVal(lfirst(name));
2689  Var *varnode = (Var *) lfirst(var);
2690  TargetEntry *te;
2691 
2692  te = makeTargetEntry((Expr *) varnode,
2693  (AttrNumber) pstate->p_next_resno++,
2694  label,
2695  false);
2696  te_list = lappend(te_list, te);
2697 
2698  /* Require read access to each column */
2699  markVarForSelectPriv(pstate, varnode, rte);
2700  }
2701 
2702  Assert(name == NULL && var == NULL); /* lists not the same length? */
2703 
2704  return te_list;
2705 }
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
#define strVal(v)
Definition: value.h:54
AclMode requiredPerms
Definition: parsenodes.h:1096
Definition: primnodes.h:167
int p_next_resno
Definition: parse_node.h:192
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:236
List * lappend(List *list, void *datum)
Definition: list.c:321
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
#define ACL_SELECT
Definition: parsenodes.h:75
static char * label
Definition: pg_basebackup.c:90
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
const char * name
Definition: encode.c:521
Definition: regcomp.c:224
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21

◆ expandRTE()

void expandRTE ( RangeTblEntry rte,
int  rtindex,
int  sublevels_up,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)

Definition at line 2226 of file parse_relation.c.

References Assert, attnum, RangeTblEntry::colcollations, Alias::colnames, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, elog, RangeTblEntry::eref, ERROR, expandRelation(), expandTupleDesc(), TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), forboth, forthree, RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), InvalidOid, RangeTblEntry::joinaliasvars, label, lappend(), lfirst, lfirst_int, lfirst_oid, list_concat(), list_copy_tail(), list_head(), list_length(), list_nth(), list_truncate(), llast, lnext(), Var::location, makeNullConst(), makeString(), makeVar(), NIL, OidIsValid, pstrdup(), RangeTblEntry::relid, TargetEntry::resjunk, TargetEntry::resno, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, strVal, RangeTblEntry::subquery, Query::targetList, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by build_physical_tlist(), coerce_record_to_complex(), expandRecordVariable(), expandRelAttrs(), ExpandSingleTable(), pullup_replace_vars_callback(), ReplaceVarsFromTargetList_callback(), transformFromClauseItem(), and transformInsertStmt().

2229 {
2230  int varattno;
2231 
2232  if (colnames)
2233  *colnames = NIL;
2234  if (colvars)
2235  *colvars = NIL;
2236 
2237  switch (rte->rtekind)
2238  {
2239  case RTE_RELATION:
2240  /* Ordinary relation RTE */
2241  expandRelation(rte->relid, rte->eref,
2242  rtindex, sublevels_up, location,
2243  include_dropped, colnames, colvars);
2244  break;
2245  case RTE_SUBQUERY:
2246  {
2247  /* Subquery RTE */
2248  ListCell *aliasp_item = list_head(rte->eref->colnames);
2249  ListCell *tlistitem;
2250 
2251  varattno = 0;
2252  foreach(tlistitem, rte->subquery->targetList)
2253  {
2254  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
2255 
2256  if (te->resjunk)
2257  continue;
2258  varattno++;
2259  Assert(varattno == te->resno);
2260 
2261  /*
2262  * In scenarios where columns have been added to a view
2263  * since the outer query was originally parsed, there can
2264  * be more items in the subquery tlist than the outer
2265  * query expects. We should ignore such extra column(s)
2266  * --- compare the behavior for composite-returning
2267  * functions, in the RTE_FUNCTION case below.
2268  */
2269  if (!aliasp_item)
2270  break;
2271 
2272  if (colnames)
2273  {
2274  char *label = strVal(lfirst(aliasp_item));
2275 
2276  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2277  }
2278 
2279  if (colvars)
2280  {
2281  Var *varnode;
2282 
2283  varnode = makeVar(rtindex, varattno,
2284  exprType((Node *) te->expr),
2285  exprTypmod((Node *) te->expr),
2286  exprCollation((Node *) te->expr),
2287  sublevels_up);
2288  varnode->location = location;
2289 
2290  *colvars = lappend(*colvars, varnode);
2291  }
2292 
2293  aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2294  }
2295  }
2296  break;
2297  case RTE_FUNCTION:
2298  {
2299  /* Function RTE */
2300  int atts_done = 0;
2301  ListCell *lc;
2302 
2303  foreach(lc, rte->functions)
2304  {
2305  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2306  TypeFuncClass functypclass;
2307  Oid funcrettype;
2308  TupleDesc tupdesc;
2309 
2310  functypclass = get_expr_result_type(rtfunc->funcexpr,
2311  &funcrettype,
2312  &tupdesc);
2313  if (functypclass == TYPEFUNC_COMPOSITE ||
2314  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
2315  {
2316  /* Composite data type, e.g. a table's row type */
2317  Assert(tupdesc);
2318  expandTupleDesc(tupdesc, rte->eref,
2319  rtfunc->funccolcount, atts_done,
2320  rtindex, sublevels_up, location,
2321  include_dropped, colnames, colvars);
2322  }
2323  else if (functypclass == TYPEFUNC_SCALAR)
2324  {
2325  /* Base data type, i.e. scalar */
2326  if (colnames)
2327  *colnames = lappend(*colnames,
2328  list_nth(rte->eref->colnames,
2329  atts_done));
2330 
2331  if (colvars)
2332  {
2333  Var *varnode;
2334 
2335  varnode = makeVar(rtindex, atts_done + 1,
2336  funcrettype, -1,
2337  exprCollation(rtfunc->funcexpr),
2338  sublevels_up);
2339  varnode->location = location;
2340 
2341  *colvars = lappend(*colvars, varnode);
2342  }
2343  }
2344  else if (functypclass == TYPEFUNC_RECORD)
2345  {
2346  if (colnames)
2347  {
2348  List *namelist;
2349 
2350  /* extract appropriate subset of column list */
2351  namelist = list_copy_tail(rte->eref->colnames,
2352  atts_done);
2353  namelist = list_truncate(namelist,
2354  rtfunc->funccolcount);
2355  *colnames = list_concat(*colnames, namelist);
2356  }
2357 
2358  if (colvars)
2359  {
2360  ListCell *l1;
2361  ListCell *l2;
2362  ListCell *l3;
2363  int attnum = atts_done;
2364 
2365  forthree(l1, rtfunc->funccoltypes,
2366  l2, rtfunc->funccoltypmods,
2367  l3, rtfunc->funccolcollations)
2368  {
2369  Oid attrtype = lfirst_oid(l1);
2370  int32 attrtypmod = lfirst_int(l2);
2371  Oid attrcollation = lfirst_oid(l3);
2372  Var *varnode;
2373 
2374  attnum++;
2375  varnode = makeVar(rtindex,
2376  attnum,
2377  attrtype,
2378  attrtypmod,
2379  attrcollation,
2380  sublevels_up);
2381  varnode->location = location;
2382  *colvars = lappend(*colvars, varnode);
2383  }
2384  }
2385  }
2386  else
2387  {
2388  /* addRangeTableEntryForFunction should've caught this */
2389  elog(ERROR, "function in FROM has unsupported return type");
2390  }
2391  atts_done += rtfunc->funccolcount;
2392  }
2393 
2394  /* Append the ordinality column if any */
2395  if (rte->funcordinality)
2396  {
2397  if (colnames)
2398  *colnames = lappend(*colnames,
2399  llast(rte->eref->colnames));
2400 
2401  if (colvars)
2402  {
2403  Var *varnode = makeVar(rtindex,
2404  atts_done + 1,
2405  INT8OID,
2406  -1,
2407  InvalidOid,
2408  sublevels_up);
2409 
2410  *colvars = lappend(*colvars, varnode);
2411  }
2412  }
2413  }
2414  break;
2415  case RTE_JOIN:
2416  {
2417  /* Join RTE */
2418  ListCell *colname;
2419  ListCell *aliasvar;
2420 
2422 
2423  varattno = 0;
2424  forboth(colname, rte->eref->colnames, aliasvar, rte->joinaliasvars)
2425  {
2426  Node *avar = (Node *) lfirst(aliasvar);
2427 
2428  varattno++;
2429 
2430  /*
2431  * During ordinary parsing, there will never be any
2432  * deleted columns in the join; but we have to check since
2433  * this routine is also used by the rewriter, and joins
2434  * found in stored rules might have join columns for
2435  * since-deleted columns. This will be signaled by a null
2436  * pointer in the alias-vars list.
2437  */
2438  if (avar == NULL)
2439  {
2440  if (include_dropped)
2441  {
2442  if (colnames)
2443  *colnames = lappend(*colnames,
2444  makeString(pstrdup("")));
2445  if (colvars)
2446  {
2447  /*
2448  * Can't use join's column type here (it might
2449  * be dropped!); but it doesn't really matter
2450  * what type the Const claims to be.
2451  */
2452  *colvars = lappend(*colvars,
2453  makeNullConst(INT4OID, -1,
2454  InvalidOid));
2455  }
2456  }
2457  continue;
2458  }
2459 
2460  if (colnames)
2461  {
2462  char *label = strVal(lfirst(colname));
2463 
2464  *colnames = lappend(*colnames,
2465  makeString(pstrdup(label)));
2466  }
2467 
2468  if (colvars)
2469  {
2470  Var *varnode;
2471 
2472  varnode = makeVar(rtindex, varattno,
2473  exprType(avar),
2474  exprTypmod(avar),
2475  exprCollation(avar),
2476  sublevels_up);
2477  varnode->location = location;
2478 
2479  *colvars = lappend(*colvars, varnode);
2480  }
2481  }
2482  }
2483  break;
2484  case RTE_TABLEFUNC:
2485  case RTE_VALUES:
2486  case RTE_CTE:
2487  case RTE_NAMEDTUPLESTORE:
2488  {
2489  /* Tablefunc, Values, CTE, or ENR RTE */
2490  ListCell *aliasp_item = list_head(rte->eref->colnames);
2491  ListCell *lct;
2492  ListCell *lcm;
2493  ListCell *lcc;
2494 
2495  varattno = 0;
2496  forthree(lct, rte->coltypes,
2497  lcm, rte->coltypmods,
2498  lcc, rte->colcollations)
2499  {
2500  Oid coltype = lfirst_oid(lct);
2501  int32 coltypmod = lfirst_int(lcm);
2502  Oid colcoll = lfirst_oid(lcc);
2503 
2504  varattno++;
2505 
2506  if (colnames)
2507  {
2508  /* Assume there is one alias per output column */
2509  if (OidIsValid(coltype))
2510  {
2511  char *label = strVal(lfirst(aliasp_item));
2512 
2513  *colnames = lappend(*colnames,
2514  makeString(pstrdup(label)));
2515  }
2516  else if (include_dropped)
2517  *colnames = lappend(*colnames,
2518  makeString(pstrdup("")));
2519 
2520  aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2521  }
2522 
2523  if (colvars)
2524  {
2525  if (OidIsValid(coltype))
2526  {
2527  Var *varnode;
2528 
2529  varnode = makeVar(rtindex, varattno,
2530  coltype, coltypmod, colcoll,
2531  sublevels_up);
2532  varnode->location = location;
2533 
2534  *colvars = lappend(*colvars, varnode);
2535  }
2536  else if (include_dropped)
2537  {
2538  /*
2539  * It doesn't really matter what type the Const
2540  * claims to be.
2541  */
2542  *colvars = lappend(*colvars,
2543  makeNullConst(INT4OID, -1,
2544  InvalidOid));
2545  }
2546  }
2547  }
2548  }
2549  break;
2550  case RTE_RESULT:
2551  /* These expose no columns, so nothing to do */
2552  break;
2553  default:
2554  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2555  }
2556 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
List * joinaliasvars
Definition: parsenodes.h:1030
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
List * colnames
Definition: primnodes.h:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:464
char * pstrdup(const char *in)
Definition: mcxt.c:1161
List * coltypmods
Definition: parsenodes.h:1079
#define llast(l)
Definition: pg_list.h:215
List * list_truncate(List *list, int new_size)
Definition: list.c:585
Definition: nodes.h:525
#define strVal(v)
Definition: value.h:54
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1419
bool funcordinality
Definition: parsenodes.h:1041
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:167
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
#define OidIsValid(objectId)
Definition: c.h:638
List * colcollations
Definition: parsenodes.h:1080
signed int int32
Definition: c.h:346
List * targetList
Definition: parsenodes.h:140
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:335
bool resjunk
Definition: primnodes.h:1400
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:191
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
int location
Definition: primnodes.h:182
AttrNumber resno
Definition: primnodes.h:1394
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
TypeFuncClass
Definition: funcapi.h:147
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
static void expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
List * lappend(List *list, void *datum)
Definition: list.c:321
static char * label
Definition: pg_basebackup.c:90
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1130
int16 attnum
Definition: pg_attribute.h:79
List * funccolcollations
Definition: parsenodes.h:1131
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
List * functions
Definition: parsenodes.h:1040
Expr * expr
Definition: primnodes.h:1393
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
#define elog(elevel,...)
Definition: elog.h:226
Alias * eref
Definition: parsenodes.h:1092
List * coltypes
Definition: parsenodes.h:1078
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ expandTupleDesc()

static void expandTupleDesc ( TupleDesc  tupdesc,
Alias eref,
int  count,
int  offset,
int  rtindex,
int  sublevels_up,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)
static

Definition at line 2587 of file parse_relation.c.

References Assert, Alias::colnames, InvalidOid, label, lappend(), lfirst, list_length(), list_nth_cell(), lnext(), Var::location, makeNullConst(), makeString(), makeVar(), NameStr, pstrdup(), strVal, and TupleDescAttr.

Referenced by expandRelation(), and expandRTE().

2591 {
2592  ListCell *aliascell;
2593  int varattno;
2594 
2595  aliascell = (offset < list_length(eref->colnames)) ?
2596  list_nth_cell(eref->colnames, offset) : NULL;
2597 
2598  Assert(count <= tupdesc->natts);
2599  for (varattno = 0; varattno < count; varattno++)
2600  {
2601  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
2602 
2603  if (attr->attisdropped)
2604  {
2605  if (include_dropped)
2606  {
2607  if (colnames)
2608  *colnames = lappend(*colnames, makeString(pstrdup("")));
2609  if (colvars)
2610  {
2611  /*
2612  * can't use atttypid here, but it doesn't really matter
2613  * what type the Const claims to be.
2614  */
2615  *colvars = lappend(*colvars,
2616  makeNullConst(INT4OID, -1, InvalidOid));
2617  }
2618  }
2619  if (aliascell)
2620  aliascell = lnext(eref->colnames, aliascell);
2621  continue;
2622  }
2623 
2624  if (colnames)
2625  {
2626  char *label;
2627 
2628  if (aliascell)
2629  {
2630  label = strVal(lfirst(aliascell));
2631  aliascell = lnext(eref->colnames, aliascell);
2632  }
2633  else
2634  {
2635  /* If we run out of aliases, use the underlying name */
2636  label = NameStr(attr->attname);
2637  }
2638  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2639  }
2640 
2641  if (colvars)
2642  {
2643  Var *varnode;
2644 
2645  varnode = makeVar(rtindex, varattno + offset + 1,
2646  attr->atttypid, attr->atttypmod,
2647  attr->attcollation,
2648  sublevels_up);
2649  varnode->location = location;
2650 
2651  *colvars = lappend(*colvars, varnode);
2652  }
2653  }
2654 }
Value * makeString(char *str)
Definition: value.c:53
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:265
List * colnames
Definition: primnodes.h:43
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1161
#define strVal(v)
Definition: value.h:54
Definition: primnodes.h:167
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:335
int location
Definition: primnodes.h:182
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
List * lappend(List *list, void *datum)
Definition: list.c:321
static char * label
Definition: pg_basebackup.c:90
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
#define NameStr(name)
Definition: c.h:609

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3119 of file parse_relation.c.

References lfirst, Query::rowMarks, and RowMarkClause::rti.

Referenced by AcquireRewriteLocks(), applyLockingClause(), and ApplyRetrieveRule().

3120 {
3121  ListCell *l;
3122 
3123  foreach(l, qry->rowMarks)
3124  {
3125  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3126 
3127  if (rc->rti == rtindex)
3128  return rc;
3129  }
3130  return NULL;
3131 }
List * rowMarks
Definition: parsenodes.h:163
#define lfirst(lc)
Definition: pg_list.h:190

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2955 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, RangeTblEntry::coltypes, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funccolcount, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_tupdesc(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), list_nth_oid(), ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), and TupleDescAttr.

Referenced by AcquireRewriteLocks().

2956 {
2957  bool result;
2958 
2959  switch (rte->rtekind)
2960  {
2961  case RTE_RELATION:
2962  {
2963  /*
2964  * Plain relation RTE --- get the attribute's catalog entry
2965  */
2966  HeapTuple tp;
2967  Form_pg_attribute att_tup;
2968 
2969  tp = SearchSysCache2(ATTNUM,
2970  ObjectIdGetDatum(rte->relid),
2972  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2973  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2974  attnum, rte->relid);
2975  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2976  result = att_tup->attisdropped;
2977  ReleaseSysCache(tp);
2978  }
2979  break;
2980  case RTE_SUBQUERY:
2981  case RTE_TABLEFUNC:
2982  case RTE_VALUES:
2983  case RTE_CTE:
2984 
2985  /*
2986  * Subselect, Table Functions, Values, CTE RTEs never have dropped
2987  * columns
2988  */
2989  result = false;
2990  break;
2991  case RTE_NAMEDTUPLESTORE:
2992  {
2993  /* Check dropped-ness by testing for valid coltype */
2994  if (attnum <= 0 ||
2995  attnum > list_length(rte->coltypes))
2996  elog(ERROR, "invalid varattno %d", attnum);
2997  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
2998  }
2999  break;
3000  case RTE_JOIN:
3001  {
3002  /*
3003  * A join RTE would not have dropped columns when constructed,
3004  * but one in a stored rule might contain columns that were
3005  * dropped from the underlying tables, if said columns are
3006  * nowhere explicitly referenced in the rule. This will be
3007  * signaled to us by a null pointer in the joinaliasvars list.
3008  */
3009  Var *aliasvar;
3010 
3011  if (attnum <= 0 ||
3013  elog(ERROR, "invalid varattno %d", attnum);
3014  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3015 
3016  result = (aliasvar == NULL);
3017  }
3018  break;
3019  case RTE_FUNCTION:
3020  {
3021  /* Function RTE */
3022  ListCell *lc;
3023  int atts_done = 0;
3024 
3025  /*
3026  * Dropped attributes are only possible with functions that
3027  * return named composite types. In such a case we have to
3028  * look up the result type to see if it currently has this
3029  * column dropped. So first, loop over the funcs until we
3030  * find the one that covers the requested column.
3031  */
3032  foreach(lc, rte->functions)
3033  {
3034  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3035 
3036  if (attnum > atts_done &&
3037  attnum <= atts_done + rtfunc->funccolcount)
3038  {
3039  TupleDesc tupdesc;
3040 
3041  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3042  true);
3043  if (tupdesc)
3044  {
3045  /* Composite data type, e.g. a table's row type */
3046  Form_pg_attribute att_tup;
3047 
3048  Assert(tupdesc);
3049  Assert(attnum - atts_done <= tupdesc->natts);
3050  att_tup = TupleDescAttr(tupdesc,
3051  attnum - atts_done - 1);
3052  return att_tup->attisdropped;
3053  }
3054  /* Otherwise, it can't have any dropped columns */
3055  return false;
3056  }
3057  atts_done += rtfunc->funccolcount;
3058  }
3059 
3060  /* If we get here, must be looking for the ordinality column */
3061  if (rte->funcordinality && attnum == atts_done + 1)
3062  return false;
3063 
3064  /* this probably can't happen ... */
3065  ereport(ERROR,
3066  (errcode(ERRCODE_UNDEFINED_COLUMN),
3067  errmsg("column %d of relation \"%s\" does not exist",
3068  attnum,
3069  rte->eref->aliasname)));
3070  result = false; /* keep compiler quiet */
3071  }
3072  break;
3073  case RTE_RESULT:
3074  /* this probably can't happen ... */
3075  ereport(ERROR,
3076  (errcode(ERRCODE_UNDEFINED_COLUMN),
3077  errmsg("column %d of relation \"%s\" does not exist",
3078  attnum,
3079  rte->eref->aliasname)));
3080  result = false; /* keep compiler quiet */
3081  break;
3082  default:
3083  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3084  result = false; /* keep compiler quiet */
3085  }
3086 
3087  return result;
3088 }
List * joinaliasvars
Definition: parsenodes.h:1030
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define Int16GetDatum(X)
Definition: postgres.h:451
int errcode(int sqlerrcode)
Definition: elog.c:570
bool funcordinality
Definition: parsenodes.h:1041
Definition: primnodes.h:167
#define OidIsValid(objectId)
Definition: c.h:638
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:397
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
int16 attnum
Definition: pg_attribute.h:79
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1040
static int list_length(const List *l)
Definition: pg_list.h:169
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1135
RTEKind rtekind
Definition: parsenodes.h:974
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
Alias * eref
Definition: parsenodes.h:1092
List * coltypes
Definition: parsenodes.h:1078

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2719 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Alias::colnames, elog, RangeTblEntry::eref, ERROR, get_attname(), InvalidAttrNumber, list_length(), list_nth(), RangeTblEntry::relid, RTE_RELATION, RangeTblEntry::rtekind, and strVal.

Referenced by check_ungrouped_columns_walker(), get_name_for_var_field(), get_variable(), and print_expr().

2720 {
2721  if (attnum == InvalidAttrNumber)
2722  return "*";
2723 
2724  /*
2725  * If there is a user-written column alias, use it.
2726  */
2727  if (rte->alias &&
2728  attnum > 0 && attnum <= list_length(rte->alias->colnames))
2729  return strVal(list_nth(rte->alias->colnames, attnum - 1));
2730 
2731  /*
2732  * If the RTE is a relation, go to the system catalogs not the
2733  * eref->colnames list. This is a little slower but it will give the
2734  * right answer if the column has been renamed since the eref list was
2735  * built (which can easily happen for rules).
2736  */
2737  if (rte->rtekind == RTE_RELATION)
2738  return get_attname(rte->relid, attnum, false);
2739 
2740  /*
2741  * Otherwise use the column name from eref. There should always be one.
2742  */
2743  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
2744  return strVal(list_nth(rte->eref->colnames, attnum - 1));
2745 
2746  /* else caller gave us a bogus attnum */
2747  elog(ERROR, "invalid attnum %d for rangetable entry %s",
2748  attnum, rte->eref->aliasname);
2749  return NULL; /* keep compiler quiet */
2750 }
Alias * alias
Definition: parsenodes.h:1091
List * colnames
Definition: primnodes.h:43
#define strVal(v)
Definition: value.h:54
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
int16 attnum
Definition: pg_attribute.h:79
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:974
#define elog(elevel,...)
Definition: elog.h:226
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:775
Alias * eref
Definition: parsenodes.h:1092

◆ get_rte_attribute_type()

void get_rte_attribute_type ( RangeTblEntry rte,
AttrNumber  attnum,
Oid vartype,
int32 vartypmod,
Oid varcollid 
)

Definition at line 2757 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, RangeTblEntry::colcollations, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), get_rel_name(), get_tle_by_resno(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum, InvalidOid, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), list_nth_int(), list_nth_oid(), NameStr, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), RangeTblEntry::relid, TargetEntry::resjunk, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), RangeTblEntry::subquery, Query::targetList, TupleDescAttr, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by make_var().

2759 {
2760  switch (rte->rtekind)
2761  {
2762  case RTE_RELATION:
2763  {
2764  /* Plain relation RTE --- get the attribute's type info */
2765  HeapTuple tp;
2766  Form_pg_attribute att_tup;
2767 
2768  tp = SearchSysCache2(ATTNUM,
2769  ObjectIdGetDatum(rte->relid),
2771  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2772  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2773  attnum, rte->relid);
2774  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2775 
2776  /*
2777  * If dropped column, pretend it ain't there. See notes in
2778  * scanRTEForColumn.
2779  */
2780  if (att_tup->attisdropped)
2781  ereport(ERROR,
2782  (errcode(ERRCODE_UNDEFINED_COLUMN),
2783  errmsg("column \"%s\" of relation \"%s\" does not exist",
2784  NameStr(att_tup->attname),
2785  get_rel_name(rte->relid))));
2786  *vartype = att_tup->atttypid;
2787  *vartypmod = att_tup->atttypmod;
2788  *varcollid = att_tup->attcollation;
2789  ReleaseSysCache(tp);
2790  }
2791  break;
2792  case RTE_SUBQUERY:
2793  {
2794  /* Subselect RTE --- get type info from subselect's tlist */
2796  attnum);
2797 
2798  if (te == NULL || te->resjunk)
2799  elog(ERROR, "subquery %s does not have attribute %d",
2800  rte->eref->aliasname, attnum);
2801  *vartype = exprType((Node *) te->expr);
2802  *vartypmod = exprTypmod((Node *) te->expr);
2803  *varcollid = exprCollation((Node *) te->expr);
2804  }
2805  break;
2806  case RTE_FUNCTION:
2807  {
2808  /* Function RTE */
2809  ListCell *lc;
2810  int atts_done = 0;
2811 
2812  /* Identify which function covers the requested column */
2813  foreach(lc, rte->functions)
2814  {
2815  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2816 
2817  if (attnum > atts_done &&
2818  attnum <= atts_done + rtfunc->funccolcount)
2819  {
2820  TypeFuncClass functypclass;
2821  Oid funcrettype;
2822  TupleDesc tupdesc;
2823 
2824  attnum -= atts_done; /* now relative to this func */
2825  functypclass = get_expr_result_type(rtfunc->funcexpr,
2826  &funcrettype,
2827  &tupdesc);
2828 
2829  if (functypclass == TYPEFUNC_COMPOSITE ||
2830  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
2831  {
2832  /* Composite data type, e.g. a table's row type */
2833  Form_pg_attribute att_tup;
2834 
2835  Assert(tupdesc);
2836  Assert(attnum <= tupdesc->natts);
2837  att_tup = TupleDescAttr(tupdesc, attnum - 1);
2838 
2839  /*
2840  * If dropped column, pretend it ain't there. See
2841  * notes in scanRTEForColumn.
2842  */
2843  if (att_tup->attisdropped)
2844  ereport(ERROR,
2845  (errcode(ERRCODE_UNDEFINED_COLUMN),
2846  errmsg("column \"%s\" of relation \"%s\" does not exist",
2847  NameStr(att_tup->attname),
2848  rte->eref->aliasname)));
2849  *vartype = att_tup->atttypid;
2850  *vartypmod = att_tup->atttypmod;
2851  *varcollid = att_tup->attcollation;
2852  }
2853  else if (functypclass == TYPEFUNC_SCALAR)
2854  {
2855  /* Base data type, i.e. scalar */
2856  *vartype = funcrettype;
2857  *vartypmod = -1;
2858  *varcollid = exprCollation(rtfunc->funcexpr);
2859  }
2860  else if (functypclass == TYPEFUNC_RECORD)
2861  {
2862  *vartype = list_nth_oid(rtfunc->funccoltypes,
2863  attnum - 1);
2864  *vartypmod = list_nth_int(rtfunc->funccoltypmods,
2865  attnum - 1);
2866  *varcollid = list_nth_oid(rtfunc->funccolcollations,
2867  attnum - 1);
2868  }
2869  else
2870  {
2871  /*
2872  * addRangeTableEntryForFunction should've caught
2873  * this
2874  */
2875  elog(ERROR, "function in FROM has unsupported return type");
2876  }
2877  return;
2878  }
2879  atts_done += rtfunc->funccolcount;
2880  }
2881 
2882  /* If we get here, must be looking for the ordinality column */
2883  if (rte->funcordinality && attnum == atts_done + 1)
2884  {
2885  *vartype = INT8OID;
2886  *vartypmod = -1;
2887  *varcollid = InvalidOid;
2888  return;
2889  }
2890 
2891  /* this probably can't happen ... */
2892  ereport(ERROR,
2893  (errcode(ERRCODE_UNDEFINED_COLUMN),
2894  errmsg("column %d of relation \"%s\" does not exist",
2895  attnum,
2896  rte->eref->aliasname)));
2897  }
2898  break;
2899  case RTE_JOIN:
2900  {
2901  /*
2902  * Join RTE --- get type info from join RTE's alias variable
2903  */
2904  Node *aliasvar;
2905 
2906  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
2907  aliasvar = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
2908  Assert(aliasvar != NULL);
2909  *vartype = exprType(aliasvar);
2910  *vartypmod = exprTypmod(aliasvar);
2911  *varcollid = exprCollation(aliasvar);
2912  }
2913  break;
2914  case RTE_TABLEFUNC:
2915  case RTE_VALUES:
2916  case RTE_CTE:
2917  case RTE_NAMEDTUPLESTORE:
2918  {
2919  /*
2920  * tablefunc, VALUES, CTE, or ENR RTE --- get type info from
2921  * lists in the RTE
2922  */
2923  Assert(attnum > 0 && attnum <= list_length(rte->coltypes));
2924  *vartype = list_nth_oid(rte->coltypes, attnum - 1);
2925  *vartypmod = list_nth_int(rte->coltypmods, attnum - 1);
2926  *varcollid = list_nth_oid(rte->colcollations, attnum - 1);
2927 
2928  /* For ENR, better check for dropped column */
2929  if (!OidIsValid(*vartype))
2930  ereport(ERROR,
2931  (errcode(ERRCODE_UNDEFINED_COLUMN),
2932  errmsg("column %d of relation \"%s\" does not exist",
2933  attnum,
2934  rte->eref->aliasname)));
2935  }
2936  break;
2937  case RTE_RESULT:
2938  /* this probably can't happen ... */
2939  ereport(ERROR,
2940  (errcode(ERRCODE_UNDEFINED_COLUMN),
2941  errmsg("column %d of relation \"%s\" does not exist",
2942  attnum,
2943  rte->eref->aliasname)));
2944  break;
2945  default:
2946  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2947  }
2948 }
List * joinaliasvars
Definition: parsenodes.h:1030
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
List * coltypmods
Definition: parsenodes.h:1079
#define Int16GetDatum(X)
Definition: postgres.h:451
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:570
bool funcordinality
Definition: parsenodes.h:1041
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
#define OidIsValid(objectId)
Definition: c.h:638
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:288
List * colcollations
Definition: parsenodes.h:1080
List * targetList
Definition: parsenodes.h:140
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
bool resjunk
Definition: primnodes.h:1400
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
TypeFuncClass
Definition: funcapi.h:147
#define ereport(elevel, rest)
Definition: elog.h:141
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1130
int16 attnum
Definition: pg_attribute.h:79
List * funccolcollations
Definition: parsenodes.h:1131
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1040
Expr * expr
Definition: primnodes.h:1393
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1135
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define NameStr(name)
Definition: c.h:609
Alias * eref
Definition: parsenodes.h:1092
List * coltypes
Definition: parsenodes.h:1078
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730

◆ get_tle_by_resno()

◆ GetCTEForRTE()

CommonTableExpr* GetCTEForRTE ( ParseState pstate,
RangeTblEntry rte,
int  rtelevelsup 
)

Definition at line 520 of file parse_relation.c.

References Assert, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, CommonTableExpr::ctename, elog, ERROR, lfirst, ParseState::p_ctenamespace, ParseState::parentParseState, RTE_CTE, RangeTblEntry::rtekind, and RTERangeTablePosn().

Referenced by expandRecordVariable(), and markTargetListOrigin().

521 {
522  Index levelsup;
523  ListCell *lc;
524 
525  /* Determine RTE's levelsup if caller didn't know it */
526  if (rtelevelsup < 0)
527  (void) RTERangeTablePosn(pstate, rte, &rtelevelsup);
528 
529  Assert(rte->rtekind == RTE_CTE);
530  levelsup = rte->ctelevelsup + rtelevelsup;
531  while (levelsup-- > 0)
532  {
533  pstate = pstate->parentParseState;
534  if (!pstate) /* shouldn't happen */
535  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
536  }
537  foreach(lc, pstate->p_ctenamespace)
538  {
539  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
540 
541  if (strcmp(cte->ctename, rte->ctename) == 0)
542  return cte;
543  }
544  /* shouldn't happen */
545  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
546  return NULL; /* keep compiler quiet */
547 }
#define ERROR
Definition: elog.h:43
struct ParseState * parentParseState
Definition: parse_node.h:175
unsigned int Index
Definition: c.h:475
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
Index ctelevelsup
Definition: parsenodes.h:1057
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
RTEKind rtekind
Definition: parsenodes.h:974
char * ctename
Definition: parsenodes.h:1056
#define elog(elevel,...)
Definition: elog.h:226
List * p_ctenamespace
Definition: parse_node.h:184

◆ GetRTEByRangeTablePosn()

RangeTblEntry* GetRTEByRangeTablePosn ( ParseState pstate,
int  varno,
int  sublevels_up 
)

Definition at line 499 of file parse_relation.c.

References Assert, list_length(), ParseState::p_rtable, ParseState::parentParseState, and rt_fetch.

Referenced by coerce_record_to_complex(), count_rowexpr_columns(), expandRecordVariable(), ExpandRowReference(), markTargetListOrigin(), ParseComplexProjection(), and unknown_attribute().

502 {
503  while (sublevels_up-- > 0)
504  {
505  pstate = pstate->parentParseState;
506  Assert(pstate != NULL);
507  }
508  Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
509  return rt_fetch(varno, pstate->p_rtable);
510 }
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
struct ParseState * parentParseState
Definition: parse_node.h:175
#define Assert(condition)
Definition: c.h:732
static int list_length(const List *l)
Definition: pg_list.h:169
List * p_rtable
Definition: parse_node.h:177

◆ isFutureCTE()

static bool isFutureCTE ( ParseState pstate,
const char *  refname 
)
static

Definition at line 271 of file parse_relation.c.

References CommonTableExpr::ctename, lfirst, ParseState::p_future_ctes, and ParseState::parentParseState.

Referenced by parserOpenTable().

272 {
273  for (; pstate != NULL; pstate = pstate->parentParseState)
274  {
275  ListCell *lc;
276 
277  foreach(lc, pstate->p_future_ctes)
278  {
279  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
280 
281  if (strcmp(cte->ctename, refname) == 0)
282  return true;
283  }
284  }
285  return false;
286 }
struct ParseState * parentParseState
Definition: parse_node.h:175
List * p_future_ctes
Definition: parse_node.h:185
#define lfirst(lc)
Definition: pg_list.h:190

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2134 of file parse_relation.c.

References lfirst, LockingClause::lockedRels, NIL, ParseState::p_locked_from_parent, ParseState::p_locking_clause, and RangeVar::relname.

Referenced by addRangeTableEntry(), and transformRangeSubselect().

2135 {
2136  ListCell *l;
2137 
2138  /*
2139  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2140  * parent level, then act as though there's a generic FOR UPDATE here.
2141  */
2142  if (pstate->p_locked_from_parent)
2143  return true;
2144 
2145  foreach(l, pstate->p_locking_clause)
2146  {
2147  LockingClause *lc = (LockingClause *) lfirst(l);
2148 
2149  if (lc->lockedRels == NIL)
2150  {
2151  /* all tables used in query */
2152  return true;
2153  }
2154  else
2155  {
2156  /* just the named tables */
2157  ListCell *l2;
2158 
2159  foreach(l2, lc->lockedRels)
2160  {
2161  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2162 
2163  if (strcmp(refname, thisrel->relname) == 0)
2164  return true;
2165  }
2166  }
2167  }
2168  return false;
2169 }
List * lockedRels
Definition: parsenodes.h:748
#define NIL
Definition: pg_list.h:65
bool p_locked_from_parent
Definition: parse_node.h:195
char * relname
Definition: primnodes.h:68
List * p_locking_clause
Definition: parse_node.h:194
#define lfirst(lc)
Definition: pg_list.h:190

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3382 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

3383 {
3384  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3385 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
Definition: nodes.h:525

◆ isQueryUsingTempRelation_walker()

static bool isQueryUsingTempRelation_walker ( Node node,
void *  context 
)
static

Definition at line 3388 of file parse_relation.c.

References AccessShareLock, expression_tree_walker(), IsA, lfirst, QTW_IGNORE_JOINALIASES, query_tree_walker(), RelationData::rd_rel, RangeTblEntry::relid, relpersistence, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, table_close(), and table_open().

Referenced by isQueryUsingTempRelation().

3389 {
3390  if (node == NULL)
3391  return false;
3392 
3393  if (IsA(node, Query))
3394  {
3395  Query *query = (Query *) node;
3396  ListCell *rtable;
3397 
3398  foreach(rtable, query->rtable)
3399  {
3400  RangeTblEntry *rte = lfirst(rtable);
3401 
3402  if (rte->rtekind == RTE_RELATION)
3403  {
3405  char relpersistence = rel->rd_rel->relpersistence;
3406 
3408  if (relpersistence == RELPERSISTENCE_TEMP)
3409  return true;
3410  }
3411  }
3412 
3413  return query_tree_walker(query,
3415  context,
3417  }
3418 
3419  return expression_tree_walker(node,
3421  context);
3422 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2274
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define AccessShareLock
Definition: lockdefs.h:36
Form_pg_class rd_rel
Definition: rel.h:83
List * rtable
Definition: parsenodes.h:137
char relpersistence
Definition: pg_class.h:78
#define lfirst(lc)
Definition: pg_list.h:190
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840
RTEKind rtekind
Definition: parsenodes.h:974
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23

◆ markRTEForSelectPriv()

static void markRTEForSelectPriv ( ParseState pstate,
RangeTblEntry rte,
int  rtindex,
AttrNumber  col 
)
static

Definition at line 923 of file parse_relation.c.

References ACL_SELECT, Assert, bms_add_member(), elog, ERROR, FirstLowInvalidHeapAttributeNumber, InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, JoinExpr::larg, list_length(), list_nth(), list_nth_node, markVarForSelectPriv(), nodeTag, ParseState::p_joinexprs, ParseState::p_rtable, JoinExpr::rarg, RangeTblEntry::requiredPerms, rt_fetch, RTE_JOIN, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, and strip_implicit_coercions().

Referenced by markVarForSelectPriv().

925 {
926  if (rte == NULL)
927  rte = rt_fetch(rtindex, pstate->p_rtable);
928 
929  if (rte->rtekind == RTE_RELATION)
930  {
931  /* Make sure the rel as a whole is marked for SELECT access */
932  rte->requiredPerms |= ACL_SELECT;
933  /* Must offset the attnum to fit in a bitmapset */
936  }
937  else if (rte->rtekind == RTE_JOIN)
938  {
939  if (col == InvalidAttrNumber)
940  {
941  /*
942  * A whole-row reference to a join has to be treated as whole-row
943  * references to the two inputs.
944  */
945  JoinExpr *j;
946 
947  if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
948  j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
949  else
950  j = NULL;
951  if (j == NULL)
952  elog(ERROR, "could not find JoinExpr for whole-row reference");
953 
954  /* Note: we can't see FromExpr here */
955  if (IsA(j->larg, RangeTblRef))
956  {
957  int varno = ((RangeTblRef *) j->larg)->rtindex;
958 
959  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
960  }
961  else if (IsA(j->larg, JoinExpr))
962  {
963  int varno = ((JoinExpr *) j->larg)->rtindex;
964 
965  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
966  }
967  else
968  elog(ERROR, "unrecognized node type: %d",
969  (int) nodeTag(j->larg));
970  if (IsA(j->rarg, RangeTblRef))
971  {
972  int varno = ((RangeTblRef *) j->rarg)->rtindex;
973 
974  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
975  }
976  else if (IsA(j->rarg, JoinExpr))
977  {
978  int varno = ((JoinExpr *) j->rarg)->rtindex;
979 
980  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
981  }
982  else
983  elog(ERROR, "unrecognized node type: %d",
984  (int) nodeTag(j->rarg));
985  }
986  else
987  {
988  /*
989  * Regular join attribute, look at the alias-variable list.
990  *
991  * The aliasvar could be either a Var or a COALESCE expression,
992  * but in the latter case we should already have marked the two
993  * referent variables as being selected, due to their use in the
994  * JOIN clause. So we need only be concerned with the Var case.
995  * But we do need to drill down through implicit coercions.
996  */
997  Var *aliasvar;
998 
999  Assert(col > 0 && col <= list_length(rte->joinaliasvars));
1000  aliasvar = (Var *) list_nth(rte->joinaliasvars, col - 1);
1001  aliasvar = (Var *) strip_implicit_coercions((Node *) aliasvar);
1002  if (aliasvar && IsA(aliasvar, Var))
1003  markVarForSelectPriv(pstate, aliasvar, NULL);
1004  }
1005  }
1006  /* other RTE types don't require privilege marking */
1007 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
List * joinaliasvars
Definition: parsenodes.h:1030
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
Definition: nodes.h:525
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
AclMode requiredPerms
Definition: parsenodes.h:1096
Definition: primnodes.h:167
Node * larg
Definition: primnodes.h:1476
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
#define list_nth_node(type, list, n)
Definition: pg_list.h:305
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
#define ACL_SELECT
Definition: parsenodes.h:75
Node * rarg
Definition: primnodes.h:1477
#define Assert(condition)
Definition: c.h:732
static int list_length(const List *l)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define InvalidAttrNumber
Definition: attnum.h:23
#define nodeTag(nodeptr)
Definition: nodes.h:530
RTEKind rtekind
Definition: parsenodes.h:974
#define elog(elevel,...)
Definition: elog.h:226
List * p_joinexprs
Definition: parse_node.h:178
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:612
List * p_rtable
Definition: parse_node.h:177

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var,
RangeTblEntry rte 
)

Definition at line 1017 of file parse_relation.c.

References Assert, IsA, markRTEForSelectPriv(), ParseState::parentParseState, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by expandRelAttrs(), ExpandSingleTable(), markRTEForSelectPriv(), scanRTEForColumn(), transformJoinUsingClause(), and transformWholeRowRef().

1018 {
1019  Index lv;
1020 
1021  Assert(IsA(var, Var));
1022  /* Find the appropriate pstate if it's an uplevel Var */
1023  for (lv = 0; lv < var->varlevelsup; lv++)
1024  pstate = pstate->parentParseState;
1025  markRTEForSelectPriv(pstate, rte, var->varno, var->varattno);
1026 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Index varlevelsup
Definition: primnodes.h:177
AttrNumber varattno
Definition: primnodes.h:172
Definition: primnodes.h:167
static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
Index varno
Definition: primnodes.h:170
struct ParseState * parentParseState
Definition: parse_node.h:175
unsigned int Index
Definition: c.h:475
#define Assert(condition)
Definition: c.h:732

◆ parserOpenTable()

Relation parserOpenTable ( ParseState pstate,
const RangeVar relation,
int  lockmode 
)

Definition at line 1163 of file parse_relation.c.

References cancel_parser_errposition_callback(), ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errdetail(), errhint(), errmsg(), ERROR, isFutureCTE(), RangeVar::location, RangeVar::relname, RangeVar::schemaname, setup_parser_errposition_callback(), and table_openrv_extended().

Referenced by addRangeTableEntry(), and setTargetTable().

1164 {
1165  Relation rel;
1166  ParseCallbackState pcbstate;
1167 
1168  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1169  rel = table_openrv_extended(relation, lockmode, true);
1170  if (rel == NULL)
1171  {
1172  if (relation->schemaname)
1173  ereport(ERROR,
1175  errmsg("relation \"%s.%s\" does not exist",
1176  relation->schemaname, relation->relname)));
1177  else
1178  {
1179  /*
1180  * An unqualified name might have been meant as a reference to
1181  * some not-yet-in-scope CTE. The bare "does not exist" message
1182  * has proven remarkably unhelpful for figuring out such problems,
1183  * so we take pains to offer a specific hint.
1184  */
1185  if (isFutureCTE(pstate, relation->relname))
1186  ereport(ERROR,
1188  errmsg("relation \"%s\" does not exist",
1189  relation->relname),
1190  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1191  relation->relname),
1192  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1193  else
1194  ereport(ERROR,
1196  errmsg("relation \"%s\" does not exist",
1197  relation->relname)));
1198  }
1199  }
1201  return rel;
1202 }
int errhint(const char *fmt,...)
Definition: elog.c:974
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:72
Relation table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
Definition: table.c:98
static bool isFutureCTE(ParseState *pstate, const char *refname)
int errcode(int sqlerrcode)
Definition: elog.c:570
char * schemaname
Definition: primnodes.h:67
int location
Definition: primnodes.h:73
char * relname
Definition: primnodes.h:68
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define ereport(elevel, rest)
Definition: elog.h:141
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ refnameRangeTblEntry()

RangeTblEntry* refnameRangeTblEntry ( ParseState pstate,
const char *  schemaname,
const char *  refname,
int  location,
int *  sublevels_up 
)

Definition at line 86 of file parse_relation.c.

References get_relname_relid(), InvalidOid, LookupNamespaceNoError(), OidIsValid, ParseState::parentParseState, scanNameSpaceForRefname(), and scanNameSpaceForRelid().

Referenced by errorMissingRTE(), ExpandColumnRefStar(), and transformColumnRef().

91 {
92  Oid relId = InvalidOid;
93 
94  if (sublevels_up)
95  *sublevels_up = 0;
96 
97  if (schemaname != NULL)
98  {
99  Oid namespaceId;
100 
101  /*
102  * We can use LookupNamespaceNoError() here because we are only
103  * interested in finding existing RTEs. Checking USAGE permission on
104  * the schema is unnecessary since it would have already been checked
105  * when the RTE was made. Furthermore, we want to report "RTE not
106  * found", not "no permissions for schema", if the name happens to
107  * match a schema name the user hasn't got access to.
108  */
109  namespaceId = LookupNamespaceNoError(schemaname);
110  if (!OidIsValid(namespaceId))
111  return NULL;
112  relId = get_relname_relid(refname, namespaceId);
113  if (!OidIsValid(relId))
114  return NULL;
115  }
116 
117  while (pstate != NULL)
118  {
119  RangeTblEntry *result;
120 
121  if (OidIsValid(relId))
122  result = scanNameSpaceForRelid(pstate, relId, location);
123  else
124  result = scanNameSpaceForRefname(pstate, refname, location);
125 
126  if (result)
127  return result;
128 
129  if (sublevels_up)
130  (*sublevels_up)++;
131  else
132  break;
133 
134  pstate = pstate->parentParseState;
135  }
136  return NULL;
137 }
static RangeTblEntry * scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1687
struct ParseState * parentParseState
Definition: parse_node.h:175
static RangeTblEntry * scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
#define InvalidOid
Definition: postgres_ext.h:36
Oid LookupNamespaceNoError(const char *nspname)
Definition: namespace.c:2855

◆ RTERangeTablePosn()

int RTERangeTablePosn ( ParseState pstate,
RangeTblEntry rte,
int *  sublevels_up 
)

Definition at line 466 of file parse_relation.c.

References elog, ERROR, lfirst, ParseState::p_rtable, and ParseState::parentParseState.

Referenced by addRTEtoQuery(), ExpandAllTables(), ExpandSingleTable(), GetCTEForRTE(), make_var(), transformCurrentOfExpr(), and transformWholeRowRef().

467 {
468  int index;
469  ListCell *l;
470 
471  if (sublevels_up)
472  *sublevels_up = 0;
473 
474  while (pstate != NULL)
475  {
476  index = 1;
477  foreach(l, pstate->p_rtable)
478  {
479  if (rte == (RangeTblEntry *) lfirst(l))
480  return index;
481  index++;
482  }
483  pstate = pstate->parentParseState;
484  if (sublevels_up)
485  (*sublevels_up)++;
486  else
487  break;
488  }
489 
490  elog(ERROR, "RTE not found (internal error)");
491  return 0; /* keep compiler quiet */
492 }
Definition: type.h:89
#define ERROR
Definition: elog.h:43
struct ParseState * parentParseState
Definition: parse_node.h:175
#define lfirst(lc)
Definition: pg_list.h:190
#define elog(elevel,...)
Definition: elog.h:226
List * p_rtable
Definition: parse_node.h:177

◆ scanNameSpaceForCTE()

CommonTableExpr* scanNameSpaceForCTE ( ParseState pstate,
const char *  refname,
Index ctelevelsup 
)

Definition at line 240 of file parse_relation.c.

References CommonTableExpr::ctename, lfirst, ParseState::p_ctenamespace, and ParseState::parentParseState.

Referenced by getRTEForSpecialRelationTypes(), and searchRangeTableForRel().

242 {
243  Index levelsup;
244 
245  for (levelsup = 0;
246  pstate != NULL;
247  pstate = pstate->parentParseState, levelsup++)
248  {
249  ListCell *lc;
250 
251  foreach(lc, pstate->p_ctenamespace)
252  {
253  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
254 
255  if (strcmp(cte->ctename, refname) == 0)
256  {
257  *ctelevelsup = levelsup;
258  return cte;
259  }
260  }
261  }
262  return NULL;
263 }
struct ParseState * parentParseState
Definition: parse_node.h:175
unsigned int Index
Definition: c.h:475
#define lfirst(lc)
Definition: pg_list.h:190
List * p_ctenamespace
Definition: parse_node.h:184

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 293 of file parse_relation.c.

References name_matches_visible_ENR().

Referenced by getRTEForSpecialRelationTypes(), searchRangeTableForRel(), and setTargetTable().

294 {
295  return name_matches_visible_ENR(pstate, refname);
296 }
bool name_matches_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:20

◆ scanNameSpaceForRefname()

static RangeTblEntry * scanNameSpaceForRefname ( ParseState pstate,
const char *  refname,
int  location 
)
static

Definition at line 157 of file parse_relation.c.

References Alias::aliasname, check_lateral_ref_ok(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, lfirst, ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, and parser_errposition().

Referenced by refnameRangeTblEntry().

158 {
159  RangeTblEntry *result = NULL;
160  ListCell *l;
161 
162  foreach(l, pstate->p_namespace)
163  {
165  RangeTblEntry *rte = nsitem->p_rte;
166 
167  /* Ignore columns-only items */
168  if (!nsitem->p_rel_visible)
169  continue;
170  /* If not inside LATERAL, ignore lateral-only items */
171  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
172  continue;
173 
174  if (strcmp(rte->eref->aliasname, refname) == 0)
175  {
176  if (result)
177  ereport(ERROR,
178  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
179  errmsg("table reference \"%s\" is ambiguous",
180  refname),
181  parser_errposition(pstate, location)));
182  check_lateral_ref_ok(pstate, nsitem, location);
183  result = rte;
184  }
185  }
186  return result;
187 }
int errcode(int sqlerrcode)
Definition: elog.c:570
RangeTblEntry * p_rte
Definition: parse_node.h:251
#define ERROR
Definition: elog.h:43
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
List * p_namespace
Definition: parse_node.h:181
#define ereport(elevel, rest)
Definition: elog.h:141
bool p_lateral_active
Definition: parse_node.h:183
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:784
Alias * eref
Definition: parsenodes.h:1092

◆ scanNameSpaceForRelid()

static RangeTblEntry * scanNameSpaceForRelid ( ParseState pstate,
Oid  relid,
int  location 
)
static

Definition at line 198 of file parse_relation.c.

References RangeTblEntry::alias, check_lateral_ref_ok(), ereport, errcode(), errmsg(), ERROR, lfirst, ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, parser_errposition(), RangeTblEntry::relid, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by refnameRangeTblEntry().

199 {
200  RangeTblEntry *result = NULL;
201  ListCell *l;
202 
203  foreach(l, pstate->p_namespace)
204  {
206  RangeTblEntry *rte = nsitem->p_rte;
207 
208  /* Ignore columns-only items */
209  if (!nsitem->p_rel_visible)
210  continue;
211  /* If not inside LATERAL, ignore lateral-only items */
212  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
213  continue;
214 
215  /* yes, the test for alias == NULL should be there... */
216  if (rte->rtekind == RTE_RELATION &&
217  rte->relid == relid &&
218  rte->alias == NULL)
219  {
220  if (result)
221  ereport(ERROR,
222  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
223  errmsg("table reference %u is ambiguous",
224  relid),
225  parser_errposition(pstate, location)));
226  check_lateral_ref_ok(pstate, nsitem, location);
227  result = rte;
228  }
229  }
230  return result;
231 }
Alias * alias
Definition: parsenodes.h:1091
int errcode(int sqlerrcode)
Definition: elog.c:570
RangeTblEntry * p_rte
Definition: parse_node.h:251
#define ERROR
Definition: elog.h:43
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
List * p_namespace
Definition: parse_node.h:181
#define ereport(elevel, rest)
Definition: elog.h:141
bool p_lateral_active
Definition: parse_node.h:183
#define lfirst(lc)
Definition: pg_list.h:190
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
RTEKind rtekind
Definition: parsenodes.h:974
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ scanRTEForColumn()

Node* scanRTEForColumn ( ParseState pstate,
RangeTblEntry rte,
const char *  colname,
int  location,
int  fuzzy_rte_penalty,
FuzzyAttrMatchState fuzzystate 
)

Definition at line 658 of file parse_relation.c.

References ATTNUM, attnum, Alias::colnames, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_GENERATED_COLUMN, Int16GetDatum, InvalidAttrNumber, lfirst, make_var(), markVarForSelectPriv(), ObjectIdGetDatum, ParseState::p_expr_kind, parser_errposition(), RangeTblEntry::relid, RangeTblEntry::relkind, RTE_RELATION, RangeTblEntry::rtekind, SearchSysCacheExists2, specialAttNum(), strVal, TableOidAttributeNumber, and updateFuzzyAttrMatchState().

Referenced by colNameToVar(), ParseComplexProjection(), searchRangeTableForCol(), and transformColumnRef().

661 {
662  Node *result = NULL;
663  int attnum = 0;
664  Var *var;
665  ListCell *c;
666 
667  /*
668  * Scan the user column names (or aliases) for a match. Complain if
669  * multiple matches.
670  *
671  * Note: eref->colnames may include entries for dropped columns, but those
672  * will be empty strings that cannot match any legal SQL identifier, so we
673  * don't bother to test for that case here.
674  *
675  * Should this somehow go wrong and we try to access a dropped column,
676  * we'll still catch it by virtue of the checks in
677  * get_rte_attribute_type(), which is called by make_var(). That routine
678  * has to do a cache lookup anyway, so the check there is cheap. Callers
679  * interested in finding match with shortest distance need to defend
680  * against this directly, though.
681  */
682  foreach(c, rte->eref->colnames)
683  {
684  const char *attcolname = strVal(lfirst(c));
685 
686  attnum++;
687  if (strcmp(attcolname, colname) == 0)
688  {
689  if (result)
690  ereport(ERROR,
691  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
692  errmsg("column reference \"%s\" is ambiguous",
693  colname),
694  parser_errposition(pstate, location)));
695  var = make_var(pstate, rte, attnum, location);
696  /* Require read access to the column */
697  markVarForSelectPriv(pstate, var, rte);
698  result = (Node *) var;
699  }
700 
701  /* Updating fuzzy match state, if provided. */
702  if (fuzzystate != NULL)
703  updateFuzzyAttrMatchState(fuzzy_rte_penalty, fuzzystate,
704  rte, attcolname, colname, attnum);
705  }
706 
707  /*
708  * If we have a unique match, return it. Note that this allows a user
709  * alias to override a system column name (such as OID) without error.
710  */
711  if (result)
712  return result;
713 
714  /*
715  * If the RTE represents a real relation, consider system column names.
716  * Composites are only used for pseudo-relations like ON CONFLICT's
717  * excluded.
718  */
719  if (rte->rtekind == RTE_RELATION &&
720  rte->relkind != RELKIND_COMPOSITE_TYPE)
721  {
722  /* quick check to see if name could be a system column */
723  attnum = specialAttNum(colname);
724 
725  /* In constraint check, no system column is allowed except tableOid */
726  if (pstate->p_expr_kind == EXPR_KIND_CHECK_CONSTRAINT &&
727  attnum < InvalidAttrNumber && attnum != TableOidAttributeNumber)
728  ereport(ERROR,
729  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
730  errmsg("system column \"%s\" reference in check constraint is invalid",
731  colname),
732  parser_errposition(pstate, location)));
733 
734  /*
735  * In generated column, no system column is allowed except tableOid.
736  */
737  if (pstate->p_expr_kind == EXPR_KIND_GENERATED_COLUMN &&
738  attnum < InvalidAttrNumber && attnum != TableOidAttributeNumber)
739  ereport(ERROR,
740  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
741  errmsg("cannot use system column \"%s\" in column generation expression",
742  colname),
743  parser_errposition(pstate, location)));
744 
745  if (attnum != InvalidAttrNumber)
746  {
747  /* now check to see if column actually is defined */
749  ObjectIdGetDatum(rte->relid),
750  Int16GetDatum(attnum)))
751  {
752  var = make_var(pstate, rte, attnum, location);
753  /* Require read access to the column */
754  markVarForSelectPriv(pstate, var, rte);
755  result = (Node *) var;
756  }
757  }
758  }
759 
760  return result;
761 }
List * colnames
Definition: primnodes.h:43
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
static void updateFuzzyAttrMatchState(int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate, RangeTblEntry *rte, const char *actual, const char *match, int attnum)
#define Int16GetDatum(X)
Definition: postgres.h:451
Definition: nodes.h:525
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:570
static int specialAttNum(const char *attname)
Definition: primnodes.h:167
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
char * c
#define TableOidAttributeNumber
Definition: sysattr.h:26
#define ereport(elevel, rest)
Definition: elog.h:141
ParseExprKind p_expr_kind
Definition: parse_node.h:191
int16 attnum
Definition: pg_attribute.h:79
Var * make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
Definition: parse_node.c:189
#define lfirst(lc)
Definition: pg_list.h:190
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:185
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:974
int errmsg(const char *fmt,...)
Definition: elog.c:784
Alias * eref
Definition: parsenodes.h:1092

◆ searchRangeTableForCol()

static FuzzyAttrMatchState* searchRangeTableForCol ( ParseState pstate,
const char *  alias,
const char *  colname,
int  location 
)
static

Definition at line 845 of file parse_relation.c.

References Alias::aliasname, FuzzyAttrMatchState::distance, RangeTblEntry::eref, FuzzyAttrMatchState::first, InvalidAttrNumber, lfirst, MAX_FUZZY_DISTANCE, ParseState::p_rtable, palloc(), ParseState::parentParseState, FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, RTE_JOIN, RangeTblEntry::rtekind, scanRTEForColumn(), FuzzyAttrMatchState::second, and varstr_levenshtein_less_equal().

Referenced by errorMissingColumn().

847 {
848  ParseState *orig_pstate = pstate;
849  FuzzyAttrMatchState *fuzzystate = palloc(sizeof(FuzzyAttrMatchState));
850 
851  fuzzystate->distance = MAX_FUZZY_DISTANCE + 1;
852  fuzzystate->rfirst = NULL;
853  fuzzystate->rsecond = NULL;
854  fuzzystate->first = InvalidAttrNumber;
855  fuzzystate->second = InvalidAttrNumber;
856 
857  while (pstate != NULL)
858  {
859  ListCell *l;
860 
861  foreach(l, pstate->p_rtable)
862  {
863  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
864  int fuzzy_rte_penalty = 0;
865 
866  /*
867  * Typically, it is not useful to look for matches within join
868  * RTEs; they effectively duplicate other RTEs for our purposes,
869  * and if a match is chosen from a join RTE, an unhelpful alias is
870  * displayed in the final diagnostic message.
871  */
872  if (rte->rtekind == RTE_JOIN)
873  continue;
874 
875  /*
876  * If the user didn't specify an alias, then matches against one
877  * RTE are as good as another. But if the user did specify an
878  * alias, then we want at least a fuzzy - and preferably an exact
879  * - match for the range table entry.
880  */
881  if (alias != NULL)
882  fuzzy_rte_penalty =
883  varstr_levenshtein_less_equal(alias, strlen(alias),
884  rte->eref->aliasname,
885  strlen(rte->eref->aliasname),
886  1, 1, 1,
887  MAX_FUZZY_DISTANCE + 1,
888  true);
889 
890  /*
891  * Scan for a matching column; if we find an exact match, we're
892  * done. Otherwise, update fuzzystate.
893  */
894  if (scanRTEForColumn(orig_pstate, rte, colname, location,
895  fuzzy_rte_penalty, fuzzystate)
896  && fuzzy_rte_penalty == 0)
897  {
898  fuzzystate->rfirst = rte;
899  fuzzystate->first = InvalidAttrNumber;
900  fuzzystate->rsecond = NULL;
901  fuzzystate->second = InvalidAttrNumber;
902  return fuzzystate;
903  }
904  }
905 
906  pstate = pstate->parentParseState;
907  }
908 
909  return fuzzystate;
910 }
Node * scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
RangeTblEntry * rfirst
#define MAX_FUZZY_DISTANCE
struct ParseState * parentParseState
Definition: parse_node.h:175
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
RangeTblEntry * rsecond
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:974
int varstr_levenshtein_less_equal(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, int max_d, bool trusted)
void * palloc(Size size)
Definition: mcxt.c:924
Alias * eref
Definition: parsenodes.h:1092
List * p_rtable
Definition: parse_node.h:177

◆ searchRangeTableForRel()

static RangeTblEntry* searchRangeTableForRel ( ParseState pstate,
RangeVar relation 
)
static

Definition at line 314 of file parse_relation.c.

References Alias::aliasname, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, RangeTblEntry::enrname, RangeTblEntry::eref, InvalidOid, lfirst, NoLock, OidIsValid, ParseState::p_rtable, ParseState::parentParseState, RangeVarGetRelid, RangeTblEntry::relid, RangeVar::relname, RTE_CTE, RTE_NAMEDTUPLESTORE, RTE_RELATION, RangeTblEntry::rtekind, scanNameSpaceForCTE(), scanNameSpaceForENR(), and RangeVar::schemaname.

Referenced by errorMissingRTE().

315 {
316  const char *refname = relation->relname;
317  Oid relId = InvalidOid;
318  CommonTableExpr *cte = NULL;
319  bool isenr = false;
320  Index ctelevelsup = 0;
321  Index levelsup;
322 
323  /*
324  * If it's an unqualified name, check for possible CTE matches. A CTE
325  * hides any real relation matches. If no CTE, look for a matching
326  * relation.
327  *
328  * NB: It's not critical that RangeVarGetRelid return the correct answer
329  * here in the face of concurrent DDL. If it doesn't, the worst case
330  * scenario is a less-clear error message. Also, the tables involved in
331  * the query are already locked, which reduces the number of cases in
332  * which surprising behavior can occur. So we do the name lookup
333  * unlocked.
334  */
335  if (!relation->schemaname)
336  {
337  cte = scanNameSpaceForCTE(pstate, refname, &ctelevelsup);
338  if (!cte)
339  isenr = scanNameSpaceForENR(pstate, refname);
340  }
341 
342  if (!cte && !isenr)
343  relId = RangeVarGetRelid(relation, NoLock, true);
344 
345  /* Now look for RTEs matching either the relation/CTE/ENR or the alias */
346  for (levelsup = 0;
347  pstate != NULL;
348  pstate = pstate->parentParseState, levelsup++)
349  {
350  ListCell *l;
351 
352  foreach(l, pstate->p_rtable)
353  {
354  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
355 
356  if (rte->rtekind == RTE_RELATION &&
357  OidIsValid(relId) &&
358  rte->relid == relId)
359  return rte;
360  if (rte->rtekind == RTE_CTE &&
361  cte != NULL &&
362  rte->ctelevelsup + levelsup == ctelevelsup &&
363  strcmp(rte->ctename, refname) == 0)
364  return rte;
365  if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
366  isenr &&