PostgreSQL Source Code  git master
parse_relation.h File Reference
Include dependency graph for parse_relation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  FuzzyAttrMatchState
 

Functions

RangeTblEntryrefnameRangeTblEntry (ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
 
CommonTableExprscanNameSpaceForCTE (ParseState *pstate, const char *refname, Index *ctelevelsup)
 
bool scanNameSpaceForENR (ParseState *pstate, const char *refname)
 
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)
 
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)
 
void markVarForSelectPriv (ParseState *pstate, Var *var, RangeTblEntry *rte)
 
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)
 
RangeTblEntryaddRangeTableEntryForValues (ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForTableFunc (ParseState *pstate, TableFunc *tf, 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 errorMissingRTE (ParseState *pstate, RangeVar *relation) pg_attribute_noreturn()
 
void errorMissingColumn (ParseState *pstate, const char *relname, const char *colname, int location) pg_attribute_noreturn()
 
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)
 
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)
 
bool isQueryUsingTempRelation (Query *query)
 

Function Documentation

◆ addRangeTableEntry()

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

Definition at line 1210 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().

1215 {
1217  char *refname = alias ? alias->aliasname : relation->relname;
1218  LOCKMODE lockmode;
1219  Relation rel;
1220 
1221  Assert(pstate != NULL);
1222 
1223  rte->rtekind = RTE_RELATION;
1224  rte->alias = alias;
1225 
1226  /*
1227  * Identify the type of lock we'll need on this relation. It's not the
1228  * query's target table (that case is handled elsewhere), so we need
1229  * either RowShareLock if it's locked by FOR UPDATE/SHARE, or plain
1230  * AccessShareLock otherwise.
1231  */
1232  lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock;
1233 
1234  /*
1235  * Get the rel's OID. This access also ensures that we have an up-to-date
1236  * relcache entry for the rel. Since this is typically the first access
1237  * to a rel in a statement, we must open the rel with the proper lockmode.
1238  */
1239  rel = parserOpenTable(pstate, relation, lockmode);
1240  rte->relid = RelationGetRelid(rel);
1241  rte->relkind = rel->rd_rel->relkind;
1242  rte->rellockmode = lockmode;
1243 
1244  /*
1245  * Build the list of effective column names using user-supplied aliases
1246  * and/or actual column names.
1247  */
1248  rte->eref = makeAlias(refname, NIL);
1249  buildRelationAliases(rel->rd_att, alias, rte->eref);
1250 
1251  /*
1252  * Drop the rel refcount, but keep the access lock till end of transaction
1253  * so that the table can't be deleted or have its schema modified
1254  * underneath us.
1255  */
1256  table_close(rel, NoLock);
1257 
1258  /*
1259  * Set flags and access permissions.
1260  *
1261  * The initial default on access checks is always check-for-READ-access,
1262  * which is the right thing for all except target tables.
1263  */
1264  rte->lateral = false;
1265  rte->inh = inh;
1266  rte->inFromCl = inFromCl;
1267 
1268  rte->requiredPerms = ACL_SELECT;
1269  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1270  rte->selectedCols = NULL;
1271  rte->insertedCols = NULL;
1272  rte->updatedCols = NULL;
1273  rte->extraUpdatedCols = NULL;
1274 
1275  /*
1276  * Add completed RTE to pstate's range table list, but not to join list
1277  * nor namespace --- caller must do that if appropriate.
1278  */
1279  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1280 
1281  return rte;
1282 }
#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:322
#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:739
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:422
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForCTE()

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

Definition at line 1920 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().

1925 {
1927  Alias *alias = rv->alias;
1928  char *refname = alias ? alias->aliasname : cte->ctename;
1929  Alias *eref;
1930  int numaliases;
1931  int varattno;
1932  ListCell *lc;
1933 
1934  Assert(pstate != NULL);
1935 
1936  rte->rtekind = RTE_CTE;
1937  rte->ctename = cte->ctename;
1938  rte->ctelevelsup = levelsup;
1939 
1940  /* Self-reference if and only if CTE's parse analysis isn't completed */
1941  rte->self_reference = !IsA(cte->ctequery, Query);
1942  Assert(cte->cterecursive || !rte->self_reference);
1943  /* Bump the CTE's refcount if this isn't a self-reference */
1944  if (!rte->self_reference)
1945  cte->cterefcount++;
1946 
1947  /*
1948  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
1949  * This won't get checked in case of a self-reference, but that's OK
1950  * because data-modifying CTEs aren't allowed to be recursive anyhow.
1951  */
1952  if (IsA(cte->ctequery, Query))
1953  {
1954  Query *ctequery = (Query *) cte->ctequery;
1955 
1956  if (ctequery->commandType != CMD_SELECT &&
1957  ctequery->returningList == NIL)
1958  ereport(ERROR,
1959  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1960  errmsg("WITH query \"%s\" does not have a RETURNING clause",
1961  cte->ctename),
1962  parser_errposition(pstate, rv->location)));
1963  }
1964 
1965  rte->coltypes = cte->ctecoltypes;
1966  rte->coltypmods = cte->ctecoltypmods;
1967  rte->colcollations = cte->ctecolcollations;
1968 
1969  rte->alias = alias;
1970  if (alias)
1971  eref = copyObject(alias);
1972  else
1973  eref = makeAlias(refname, NIL);
1974  numaliases = list_length(eref->colnames);
1975 
1976  /* fill in any unspecified alias columns */
1977  varattno = 0;
1978  foreach(lc, cte->ctecolnames)
1979  {
1980  varattno++;
1981  if (varattno > numaliases)
1982  eref->colnames = lappend(eref->colnames, lfirst(lc));
1983  }
1984  if (varattno < numaliases)
1985  ereport(ERROR,
1986  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1987  errmsg("table \"%s\" has %d columns available but %d columns specified",
1988  refname, varattno, numaliases)));
1989 
1990  rte->eref = eref;
1991 
1992  /*
1993  * Set flags and access permissions.
1994  *
1995  * Subqueries are never checked for access rights.
1996  */
1997  rte->lateral = false;
1998  rte->inh = false; /* never true for subqueries */
1999  rte->inFromCl = inFromCl;
2000 
2001  rte->requiredPerms = 0;
2002  rte->checkAsUser = InvalidOid;
2003  rte->selectedCols = NULL;
2004  rte->insertedCols = NULL;
2005  rte->updatedCols = NULL;
2006  rte->extraUpdatedCols = NULL;
2007 
2008  /*
2009  * Add completed RTE to pstate's range table list, but not to join list
2010  * nor namespace --- caller must do that if appropriate.
2011  */
2012  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2013 
2014  return rte;
2015 }
#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:608
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:322
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:739
#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:110
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:822
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 2030 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().

2033 {
2035  Alias *alias = rv->alias;
2036  char *refname = alias ? alias->aliasname : rv->relname;
2038  TupleDesc tupdesc;
2039  int attno;
2040 
2041  Assert(pstate != NULL);
2042  enrmd = get_visible_ENR(pstate, rv->relname);
2043  Assert(enrmd != NULL);
2044 
2045  switch (enrmd->enrtype)
2046  {
2047  case ENR_NAMED_TUPLESTORE:
2049  break;
2050 
2051  default:
2052  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2053  return NULL; /* for fussy compilers */
2054  }
2055 
2056  /*
2057  * Record dependency on a relation. This allows plans to be invalidated
2058  * if they access transition tables linked to a table that is altered.
2059  */
2060  rte->relid = enrmd->reliddesc;
2061 
2062  /*
2063  * Build the list of effective column names using user-supplied aliases
2064  * and/or actual column names.
2065  */
2066  tupdesc = ENRMetadataGetTupDesc(enrmd);
2067  rte->eref = makeAlias(refname, NIL);
2068  buildRelationAliases(tupdesc, alias, rte->eref);
2069 
2070  /* Record additional data for ENR, including column type info */
2071  rte->enrname = enrmd->name;
2072  rte->enrtuples = enrmd->enrtuples;
2073  rte->coltypes = NIL;
2074  rte->coltypmods = NIL;
2075  rte->colcollations = NIL;
2076  for (attno = 1; attno <= tupdesc->natts; ++attno)
2077  {
2078  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2079 
2080  if (att->attisdropped)
2081  {
2082  /* Record zeroes for a dropped column */
2083  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2084  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2086  }
2087  else
2088  {
2089  /* Let's just make sure we can tell this isn't dropped */
2090  if (att->atttypid == InvalidOid)
2091  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2092  rv->relname);
2093  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2094  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2096  att->attcollation);
2097  }
2098  }
2099 
2100  /*
2101  * Set flags and access permissions.
2102  *
2103  * ENRs are never checked for access rights.
2104  */
2105  rte->lateral = false;
2106  rte->inh = false; /* never true for ENRs */
2107  rte->inFromCl = inFromCl;
2108 
2109  rte->requiredPerms = 0;
2110  rte->checkAsUser = InvalidOid;
2111  rte->selectedCols = NULL;
2112 
2113  /*
2114  * Add completed RTE to pstate's range table list, but not to join list
2115  * nor namespace --- caller must do that if appropriate.
2116  */
2117  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2118 
2119  return rte;
2120 }
#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:358
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:340
List * lappend(List *list, void *datum)
Definition: list.c:322
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
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:228
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 1443 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().

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

1855 {
1857  Alias *eref;
1858  int numaliases;
1859 
1860  Assert(pstate != NULL);
1861 
1862  /*
1863  * Fail if join has too many columns --- we must be able to reference any
1864  * of the columns with an AttrNumber.
1865  */
1866  if (list_length(aliasvars) > MaxAttrNumber)
1867  ereport(ERROR,
1868  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1869  errmsg("joins can have at most %d columns",
1870  MaxAttrNumber)));
1871 
1872  rte->rtekind = RTE_JOIN;
1873  rte->relid = InvalidOid;
1874  rte->subquery = NULL;
1875  rte->jointype = jointype;
1876  rte->joinaliasvars = aliasvars;
1877  rte->alias = alias;
1878 
1879  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
1880  numaliases = list_length(eref->colnames);
1881 
1882  /* fill in any unspecified alias columns */
1883  if (numaliases < list_length(colnames))
1884  eref->colnames = list_concat(eref->colnames,
1885  list_copy_tail(colnames, numaliases));
1886 
1887  rte->eref = eref;
1888 
1889  /*
1890  * Set flags and access permissions.
1891  *
1892  * Joins are never checked for access rights.
1893  */
1894  rte->lateral = false;
1895  rte->inh = false; /* never true for joins */
1896  rte->inFromCl = inFromCl;
1897 
1898  rte->requiredPerms = 0;
1899  rte->checkAsUser = InvalidOid;
1900  rte->selectedCols = NULL;
1901  rte->insertedCols = NULL;
1902  rte->updatedCols = NULL;
1903  rte->extraUpdatedCols = NULL;
1904 
1905  /*
1906  * Add completed RTE to pstate's range table list, but not to join list
1907  * nor namespace --- caller must do that if appropriate.
1908  */
1909  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1910 
1911  return rte;
1912 }
#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:516
int errcode(int sqlerrcode)
Definition: elog.c:608
AclMode requiredPerms
Definition: parsenodes.h:1096
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1423
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:322
#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:739
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:822
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 1300 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().

1306 {
1308  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1309 
1310  Assert(pstate != NULL);
1311 
1312  Assert(lockmode == AccessShareLock ||
1313  lockmode == RowShareLock ||
1314  lockmode == RowExclusiveLock);
1315  Assert(CheckRelationLockedByMe(rel, lockmode, true));
1316 
1317  rte->rtekind = RTE_RELATION;
1318  rte->alias = alias;
1319  rte->relid = RelationGetRelid(rel);
1320  rte->relkind = rel->rd_rel->relkind;
1321  rte->rellockmode = lockmode;
1322 
1323  /*
1324  * Build the list of effective column names using user-supplied aliases
1325  * and/or actual column names.
1326  */
1327  rte->eref = makeAlias(refname, NIL);
1328  buildRelationAliases(rel->rd_att, alias, rte->eref);
1329 
1330  /*
1331  * Set flags and access permissions.
1332  *
1333  * The initial default on access checks is always check-for-READ-access,
1334  * which is the right thing for all except target tables.
1335  */
1336  rte->lateral = false;
1337  rte->inh = inh;
1338  rte->inFromCl = inFromCl;
1339 
1340  rte->requiredPerms = ACL_SELECT;
1341  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1342  rte->selectedCols = NULL;
1343  rte->insertedCols = NULL;
1344  rte->updatedCols = NULL;
1345  rte->extraUpdatedCols = NULL;
1346 
1347  /*
1348  * Add completed RTE to pstate's range table list, but not to join list
1349  * nor namespace --- caller must do that if appropriate.
1350  */
1351  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1352 
1353  return rte;
1354 }
#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:456
List * lappend(List *list, void *datum)
Definition: list.c:322
#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:739
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:422
List * p_rtable
Definition: parse_node.h:177

◆ addRangeTableEntryForSubquery()

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

Definition at line 1363 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().

1368 {
1370  char *refname = alias->aliasname;
1371  Alias *eref;
1372  int numaliases;
1373  int varattno;
1374  ListCell *tlistitem;
1375 
1376  Assert(pstate != NULL);
1377 
1378  rte->rtekind = RTE_SUBQUERY;
1379  rte->subquery = subquery;
1380  rte->alias = alias;
1381 
1382  eref = copyObject(alias);
1383  numaliases = list_length(eref->colnames);
1384 
1385  /* fill in any unspecified alias columns */
1386  varattno = 0;
1387  foreach(tlistitem, subquery->targetList)
1388  {
1389  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1390 
1391  if (te->resjunk)
1392  continue;
1393  varattno++;
1394  Assert(varattno == te->resno);
1395  if (varattno > numaliases)
1396  {
1397  char *attrname;
1398 
1399  attrname = pstrdup(te->resname);
1400  eref->colnames = lappend(eref->colnames, makeString(attrname));
1401  }
1402  }
1403  if (varattno < numaliases)
1404  ereport(ERROR,
1405  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1406  errmsg("table \"%s\" has %d columns available but %d columns specified",
1407  refname, varattno, numaliases)));
1408 
1409  rte->eref = eref;
1410 
1411  /*
1412  * Set flags and access permissions.
1413  *
1414  * Subqueries are never checked for access rights.
1415  */
1416  rte->lateral = lateral;
1417  rte->inh = false; /* never true for subqueries */
1418  rte->inFromCl = inFromCl;
1419 
1420  rte->requiredPerms = 0;
1421  rte->checkAsUser = InvalidOid;
1422  rte->selectedCols = NULL;
1423  rte->insertedCols = NULL;
1424  rte->updatedCols = NULL;
1425  rte->extraUpdatedCols = NULL;
1426 
1427  /*
1428  * Add completed RTE to pstate's range table list, but not to join list
1429  * nor namespace --- caller must do that if appropriate.
1430  */
1431  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1432 
1433  return rte;
1434 }
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:1186
int errcode(int sqlerrcode)
Definition: elog.c:608
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:322
#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:739
#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:822
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 1706 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().

1711 {
1713  char *refname = alias ? alias->aliasname : pstrdup("xmltable");
1714  Alias *eref;
1715  int numaliases;
1716 
1717  Assert(pstate != NULL);
1718 
1719  rte->rtekind = RTE_TABLEFUNC;
1720  rte->relid = InvalidOid;
1721  rte->subquery = NULL;
1722  rte->tablefunc = tf;
1723  rte->coltypes = tf->coltypes;
1724  rte->coltypmods = tf->coltypmods;
1725  rte->colcollations = tf->colcollations;
1726  rte->alias = alias;
1727 
1728  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1729  numaliases = list_length(eref->colnames);
1730 
1731  /* fill in any unspecified alias columns */
1732  if (numaliases < list_length(tf->colnames))
1733  eref->colnames = list_concat(eref->colnames,
1734  list_copy_tail(tf->colnames, numaliases));
1735 
1736  rte->eref = eref;
1737 
1738  /*
1739  * Set flags and access permissions.
1740  *
1741  * Tablefuncs are never checked for access rights (at least, not by the
1742  * RTE permissions mechanism).
1743  */
1744  rte->lateral = lateral;
1745  rte->inh = false; /* never true for tablefunc RTEs */
1746  rte->inFromCl = inFromCl;
1747 
1748  rte->requiredPerms = 0;
1749  rte->checkAsUser = InvalidOid;
1750  rte->selectedCols = NULL;
1751  rte->insertedCols = NULL;
1752  rte->updatedCols = NULL;
1753  rte->extraUpdatedCols = NULL;
1754 
1755  /*
1756  * Add completed RTE to pstate's range table list, but not to join list
1757  * nor namespace --- caller must do that if appropriate.
1758  */
1759  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1760 
1761  return rte;
1762 }
#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:1186
List * coltypmods
Definition: parsenodes.h:1079
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
AclMode requiredPerms
Definition: parsenodes.h:1096
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1423
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:322
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:739
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 1770 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().

1778 {
1780  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
1781  Alias *eref;
1782  int numaliases;
1783  int numcolumns;
1784 
1785  Assert(pstate != NULL);
1786 
1787  rte->rtekind = RTE_VALUES;
1788  rte->relid = InvalidOid;
1789  rte->subquery = NULL;
1790  rte->values_lists = exprs;
1791  rte->coltypes = coltypes;
1792  rte->coltypmods = coltypmods;
1793  rte->colcollations = colcollations;
1794  rte->alias = alias;
1795 
1796  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1797 
1798  /* fill in any unspecified alias columns */
1799  numcolumns = list_length((List *) linitial(exprs));
1800  numaliases = list_length(eref->colnames);
1801  while (numaliases < numcolumns)
1802  {
1803  char attrname[64];
1804 
1805  numaliases++;
1806  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
1807  eref->colnames = lappend(eref->colnames,
1808  makeString(pstrdup(attrname)));
1809  }
1810  if (numcolumns < numaliases)
1811  ereport(ERROR,
1812  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1813  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
1814  refname, numcolumns, numaliases)));
1815 
1816  rte->eref = eref;
1817 
1818  /*
1819  * Set flags and access permissions.
1820  *
1821  * Subqueries are never checked for access rights.
1822  */
1823  rte->lateral = lateral;
1824  rte->inh = false; /* never true for values RTEs */
1825  rte->inFromCl = inFromCl;
1826 
1827  rte->requiredPerms = 0;
1828  rte->checkAsUser = InvalidOid;
1829  rte->selectedCols = NULL;
1830  rte->insertedCols = NULL;
1831  rte->updatedCols = NULL;
1832  rte->extraUpdatedCols = NULL;
1833 
1834  /*
1835  * Add completed RTE to pstate's range table list, but not to join list
1836  * nor namespace --- caller must do that if appropriate.
1837  */
1838  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1839 
1840  return rte;
1841 }
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:1186
List * coltypmods
Definition: parsenodes.h:1079
int errcode(int sqlerrcode)
Definition: elog.c:608
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:322
#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:739
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:822
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 2181 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().

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

◆ attnameAttNum()

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

Definition at line 3142 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().

3143 {
3144  int i;
3145 
3146  for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3147  {
3148  Form_pg_attribute att = TupleDescAttr(rd->rd_att, i);
3149 
3150  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3151  return i + 1;
3152  }
3153 
3154  if (sysColOK)
3155  {
3156  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
3157  return i;
3158  }
3159 
3160  /* on failure */
3161  return InvalidAttrNumber;
3162 }
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:428
#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 3192 of file parse_relation.c.

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

Referenced by transformFkeyGetPrimaryKey().

3193 {
3194  if (attid <= 0)
3195  {
3196  const FormData_pg_attribute *sysatt;
3197 
3198  sysatt = SystemAttributeDefinition(attid);
3199  return &sysatt->attname;
3200  }
3201  if (attid > rd->rd_att->natts)
3202  elog(ERROR, "invalid attribute number %d", attid);
3203  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3204 }
#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:228

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3234 of file parse_relation.c.

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

3235 {
3236  if (attid <= 0)
3237  {
3238  /* All system attributes are of noncollatable types. */
3239  return InvalidOid;
3240  }
3241  if (attid > rd->rd_att->natts)
3242  elog(ERROR, "invalid attribute number %d", attid);
3243  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3244 }
#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:228

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3214 of file parse_relation.c.

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

3215 {
3216  if (attid <= 0)
3217  {
3218  const FormData_pg_attribute *sysatt;
3219 
3220  sysatt = SystemAttributeDefinition(attid);
3221  return sysatt->atttypid;
3222  }
3223  if (attid > rd->rd_att->natts)
3224  elog(ERROR, "invalid attribute number %d", attid);
3225  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3226 }
#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:228

◆ checkNameSpaceConflicts()

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

Definition at line 391 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().

393 {
394  ListCell *l1;
395 
396  foreach(l1, namespace1)
397  {
398  ParseNamespaceItem *nsitem1 = (ParseNamespaceItem *) lfirst(l1);
399  RangeTblEntry *rte1 = nsitem1->p_rte;
400  const char *aliasname1 = rte1->eref->aliasname;
401  ListCell *l2;
402 
403  if (!nsitem1->p_rel_visible)
404  continue;
405 
406  foreach(l2, namespace2)
407  {
408  ParseNamespaceItem *nsitem2 = (ParseNamespaceItem *) lfirst(l2);
409  RangeTblEntry *rte2 = nsitem2->p_rte;
410 
411  if (!nsitem2->p_rel_visible)
412  continue;
413  if (strcmp(rte2->eref->aliasname, aliasname1) != 0)
414  continue; /* definitely no conflict */
415  if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
416  rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
417  rte1->relid != rte2->relid)
418  continue; /* no conflict per SQL rule */
419  ereport(ERROR,
420  (errcode(ERRCODE_DUPLICATE_ALIAS),
421  errmsg("table name \"%s\" specified more than once",
422  aliasname1)));
423  }
424  }
425 }
Alias * alias
Definition: parsenodes.h:1091
int errcode(int sqlerrcode)
Definition: elog.c:608
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:822
Alias * eref
Definition: parsenodes.h:1092

◆ colNameToVar()

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

Definition at line 770 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().

772 {
773  Node *result = NULL;
774  ParseState *orig_pstate = pstate;
775 
776  while (pstate != NULL)
777  {
778  ListCell *l;
779 
780  foreach(l, pstate->p_namespace)
781  {
783  RangeTblEntry *rte = nsitem->p_rte;
784  Node *newresult;
785 
786  /* Ignore table-only items */
787  if (!nsitem->p_cols_visible)
788  continue;
789  /* If not inside LATERAL, ignore lateral-only items */
790  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
791  continue;
792 
793  /* use orig_pstate here to get the right sublevels_up */
794  newresult = scanRTEForColumn(orig_pstate, rte, colname, location,
795  0, NULL);
796 
797  if (newresult)
798  {
799  if (result)
800  ereport(ERROR,
801  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
802  errmsg("column reference \"%s\" is ambiguous",
803  colname),
804  parser_errposition(pstate, location)));
805  check_lateral_ref_ok(pstate, nsitem, location);
806  result = newresult;
807  }
808  }
809 
810  if (result != NULL || localonly)
811  break; /* found, or don't want to look at parent */
812 
813  pstate = pstate->parentParseState;
814  }
815 
816  return result;
817 }
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
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:110
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ errorMissingColumn()

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

Definition at line 3309 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().

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

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3253 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().

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

◆ expandRelAttrs()

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

Definition at line 2666 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().

2668 {
2669  List *names,
2670  *vars;
2671  ListCell *name,
2672  *var;
2673  List *te_list = NIL;
2674 
2675  expandRTE(rte, rtindex, sublevels_up, location, false,
2676  &names, &vars);
2677 
2678  /*
2679  * Require read access to the table. This is normally redundant with the
2680  * markVarForSelectPriv calls below, but not if the table has zero
2681  * columns.
2682  */
2683  rte->requiredPerms |= ACL_SELECT;
2684 
2685  forboth(name, names, var, vars)
2686  {
2687  char *label = strVal(lfirst(name));
2688  Var *varnode = (Var *) lfirst(var);
2689  TargetEntry *te;
2690 
2691  te = makeTargetEntry((Expr *) varnode,
2692  (AttrNumber) pstate->p_next_resno++,
2693  label,
2694  false);
2695  te_list = lappend(te_list, te);
2696 
2697  /* Require read access to each column */
2698  markVarForSelectPriv(pstate, varnode, rte);
2699  }
2700 
2701  Assert(name == NULL && var == NULL); /* lists not the same length? */
2702 
2703  return te_list;
2704 }
#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:322
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
#define Assert(condition)
Definition: c.h:739
#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 2225 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().

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

◆ GetCTEForRTE()

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

Definition at line 519 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().

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

◆ GetRTEByRangeTablePosn()

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

Definition at line 498 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().

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

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2133 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().

2134 {
2135  ListCell *l;
2136 
2137  /*
2138  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2139  * parent level, then act as though there's a generic FOR UPDATE here.
2140  */
2141  if (pstate->p_locked_from_parent)
2142  return true;
2143 
2144  foreach(l, pstate->p_locking_clause)
2145  {
2146  LockingClause *lc = (LockingClause *) lfirst(l);
2147 
2148  if (lc->lockedRels == NIL)
2149  {
2150  /* all tables used in query */
2151  return true;
2152  }
2153  else
2154  {
2155  /* just the named tables */
2156  ListCell *l2;
2157 
2158  foreach(l2, lc->lockedRels)
2159  {
2160  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2161 
2162  if (strcmp(refname, thisrel->relname) == 0)
2163  return true;
2164  }
2165  }
2166  }
2167  return false;
2168 }
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 3381 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

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

◆ markVarForSelectPriv()

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

Definition at line 1016 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().

1017 {
1018  Index lv;
1019 
1020  Assert(IsA(var, Var));
1021  /* Find the appropriate pstate if it's an uplevel Var */
1022  for (lv = 0; lv < var->varlevelsup; lv++)
1023  pstate = pstate->parentParseState;
1024  markRTEForSelectPriv(pstate, rte, var->varno, var->varattno);
1025 }
#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:476
#define Assert(condition)
Definition: c.h:739

◆ parserOpenTable()

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

Definition at line 1162 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().

1163 {
1164  Relation rel;
1165  ParseCallbackState pcbstate;
1166 
1167  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1168  rel = table_openrv_extended(relation, lockmode, true);
1169  if (rel == NULL)
1170  {
1171  if (relation->schemaname)
1172  ereport(ERROR,
1174  errmsg("relation \"%s.%s\" does not exist",
1175  relation->schemaname, relation->relname)));
1176  else
1177  {
1178  /*
1179  * An unqualified name might have been meant as a reference to
1180  * some not-yet-in-scope CTE. The bare "does not exist" message
1181  * has proven remarkably unhelpful for figuring out such problems,
1182  * so we take pains to offer a specific hint.
1183  */
1184  if (isFutureCTE(pstate, relation->relname))
1185  ereport(ERROR,
1187  errmsg("relation \"%s\" does not exist",
1188  relation->relname),
1189  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1190  relation->relname),
1191  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1192  else
1193  ereport(ERROR,
1195  errmsg("relation \"%s\" does not exist",
1196  relation->relname)));
1197  }
1198  }
1200  return rel;
1201 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:74
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:608
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:160
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:955
#define ereport(elevel, rest)
Definition: elog.h:141
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:144
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ refnameRangeTblEntry()

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

Definition at line 85 of file parse_relation.c.

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

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

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

466 {
467  int index;
468  ListCell *l;
469 
470  if (sublevels_up)
471  *sublevels_up = 0;
472 
473  while (pstate != NULL)
474  {
475  index = 1;
476  foreach(l, pstate->p_rtable)
477  {
478  if (rte == (RangeTblEntry *) lfirst(l))
479  return index;
480  index++;
481  }
482  pstate = pstate->parentParseState;
483  if (sublevels_up)
484  (*sublevels_up)++;
485  else
486  break;
487  }
488 
489  elog(ERROR, "RTE not found (internal error)");
490  return 0; /* keep compiler quiet */
491 }
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:228
List * p_rtable
Definition: parse_node.h:177

◆ scanNameSpaceForCTE()

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

Definition at line 239 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes(), and searchRangeTableForRel().

241 {
242  Index levelsup;
243 
244  for (levelsup = 0;
245  pstate != NULL;
246  pstate = pstate->parentParseState, levelsup++)
247  {
248  ListCell *lc;
249 
250  foreach(lc, pstate->p_ctenamespace)
251  {
252  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
253 
254  if (strcmp(cte->ctename, refname) == 0)
255  {
256  *ctelevelsup = levelsup;
257  return cte;
258  }
259  }
260  }
261  return NULL;
262 }
struct ParseState * parentParseState
Definition: parse_node.h:175
unsigned int Index
Definition: c.h:476
#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 292 of file parse_relation.c.

References name_matches_visible_ENR().

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

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

◆ scanRTEForColumn()

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

Definition at line 657 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().

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