PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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, char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
 
NodecolNameToVar (ParseState *pstate, 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, 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, char *relname, 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)
 
Name attnumAttName (Relation rd, int attid)
 
Oid attnumTypeId (Relation rd, int attid)
 
Oid attnumCollationId (Relation rd, int attid)
 
bool isQueryUsingTempRelation (Query *query)
 

Function Documentation

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

Definition at line 1194 of file parse_relation.c.

References AccessShareLock, ACL_SELECT, RangeTblEntry::alias, Alias::aliasname, Assert, buildRelationAliases(), RangeTblEntry::checkAsUser, RangeTblEntry::eref, heap_close, 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, RangeVar::relname, RangeTblEntry::requiredPerms, RowShareLock, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, and RangeTblEntry::updatedCols.

Referenced by transformTableEntry().

1199 {
1201  char *refname = alias ? alias->aliasname : relation->relname;
1202  LOCKMODE lockmode;
1203  Relation rel;
1204 
1205  Assert(pstate != NULL);
1206 
1207  rte->rtekind = RTE_RELATION;
1208  rte->alias = alias;
1209 
1210  /*
1211  * Get the rel's OID. This access also ensures that we have an up-to-date
1212  * relcache entry for the rel. Since this is typically the first access
1213  * to a rel in a statement, be careful to get the right access level
1214  * depending on whether we're doing SELECT FOR UPDATE/SHARE.
1215  */
1216  lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock;
1217  rel = parserOpenTable(pstate, relation, lockmode);
1218  rte->relid = RelationGetRelid(rel);
1219  rte->relkind = rel->rd_rel->relkind;
1220 
1221  /*
1222  * Build the list of effective column names using user-supplied aliases
1223  * and/or actual column names.
1224  */
1225  rte->eref = makeAlias(refname, NIL);
1226  buildRelationAliases(rel->rd_att, alias, rte->eref);
1227 
1228  /*
1229  * Drop the rel refcount, but keep the access lock till end of transaction
1230  * so that the table can't be deleted or have its schema modified
1231  * underneath us.
1232  */
1233  heap_close(rel, NoLock);
1234 
1235  /*
1236  * Set flags and access permissions.
1237  *
1238  * The initial default on access checks is always check-for-READ-access,
1239  * which is the right thing for all except target tables.
1240  */
1241  rte->lateral = false;
1242  rte->inh = inh;
1243  rte->inFromCl = inFromCl;
1244 
1245  rte->requiredPerms = ACL_SELECT;
1246  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1247  rte->selectedCols = NULL;
1248  rte->insertedCols = NULL;
1249  rte->updatedCols = NULL;
1250 
1251  /*
1252  * Add completed RTE to pstate's range table list, but not to join list
1253  * nor namespace --- caller must do that if appropriate.
1254  */
1255  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1256 
1257  return rte;
1258 }
#define NIL
Definition: pg_list.h:69
Alias * alias
Definition: parsenodes.h:1048
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:1053
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:114
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
char * relname
Definition: primnodes.h:68
Bitmapset * selectedCols
Definition: parsenodes.h:1055
#define NoLock
Definition: lockdefs.h:34
List * lappend(List *list, void *datum)
Definition: list.c:128
#define RowShareLock
Definition: lockdefs.h:37
#define ACL_SELECT
Definition: parsenodes.h:73
TupleDesc rd_att
Definition: rel.h:115
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1057
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:945
Bitmapset * insertedCols
Definition: parsenodes.h:1056
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
bool isLockedRefname(ParseState *pstate, const char *refname)
Alias * eref
Definition: parsenodes.h:1049
#define RelationGetRelid(relation)
Definition: rel.h:416
List * p_rtable
Definition: parse_node.h:172
RangeTblEntry* addRangeTableEntryForCTE ( ParseState pstate,
CommonTableExpr cte,
Index  levelsup,
RangeVar rv,
bool  inFromCl 
)

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

1873 {
1875  Alias *alias = rv->alias;
1876  char *refname = alias ? alias->aliasname : cte->ctename;
1877  Alias *eref;
1878  int numaliases;
1879  int varattno;
1880  ListCell *lc;
1881 
1882  Assert(pstate != NULL);
1883 
1884  rte->rtekind = RTE_CTE;
1885  rte->ctename = cte->ctename;
1886  rte->ctelevelsup = levelsup;
1887 
1888  /* Self-reference if and only if CTE's parse analysis isn't completed */
1889  rte->self_reference = !IsA(cte->ctequery, Query);
1890  Assert(cte->cterecursive || !rte->self_reference);
1891  /* Bump the CTE's refcount if this isn't a self-reference */
1892  if (!rte->self_reference)
1893  cte->cterefcount++;
1894 
1895  /*
1896  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
1897  * This won't get checked in case of a self-reference, but that's OK
1898  * because data-modifying CTEs aren't allowed to be recursive anyhow.
1899  */
1900  if (IsA(cte->ctequery, Query))
1901  {
1902  Query *ctequery = (Query *) cte->ctequery;
1903 
1904  if (ctequery->commandType != CMD_SELECT &&
1905  ctequery->returningList == NIL)
1906  ereport(ERROR,
1907  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1908  errmsg("WITH query \"%s\" does not have a RETURNING clause",
1909  cte->ctename),
1910  parser_errposition(pstate, rv->location)));
1911  }
1912 
1913  rte->coltypes = cte->ctecoltypes;
1914  rte->coltypmods = cte->ctecoltypmods;
1915  rte->colcollations = cte->ctecolcollations;
1916 
1917  rte->alias = alias;
1918  if (alias)
1919  eref = copyObject(alias);
1920  else
1921  eref = makeAlias(refname, NIL);
1922  numaliases = list_length(eref->colnames);
1923 
1924  /* fill in any unspecified alias columns */
1925  varattno = 0;
1926  foreach(lc, cte->ctecolnames)
1927  {
1928  varattno++;
1929  if (varattno > numaliases)
1930  eref->colnames = lappend(eref->colnames, lfirst(lc));
1931  }
1932  if (varattno < numaliases)
1933  ereport(ERROR,
1934  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1935  errmsg("table \"%s\" has %d columns available but %d columns specified",
1936  refname, varattno, numaliases)));
1937 
1938  rte->eref = eref;
1939 
1940  /*
1941  * Set flags and access permissions.
1942  *
1943  * Subqueries are never checked for access rights.
1944  */
1945  rte->lateral = false;
1946  rte->inh = false; /* never true for subqueries */
1947  rte->inFromCl = inFromCl;
1948 
1949  rte->requiredPerms = 0;
1950  rte->checkAsUser = InvalidOid;
1951  rte->selectedCols = NULL;
1952  rte->insertedCols = NULL;
1953  rte->updatedCols = NULL;
1954 
1955  /*
1956  * Add completed RTE to pstate's range table list, but not to join list
1957  * nor namespace --- caller must do that if appropriate.
1958  */
1959  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1960 
1961  return rte;
1962 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
Alias * alias
Definition: parsenodes.h:1048
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: parsenodes.h:1036
int errcode(int sqlerrcode)
Definition: elog.c:575
AclMode requiredPerms
Definition: parsenodes.h:1053
List * colcollations
Definition: parsenodes.h:1037
int location
Definition: primnodes.h:73
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:1055
#define ERROR
Definition: elog.h:43
List * ctecoltypmods
Definition: parsenodes.h:1379
List * returningList
Definition: parsenodes.h:144
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
List * ctecolnames
Definition: parsenodes.h:1377
bool self_reference
Definition: parsenodes.h:1017
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1057
CmdType commandType
Definition: parsenodes.h:110
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Index ctelevelsup
Definition: parsenodes.h:1016
List * ctecoltypes
Definition: parsenodes.h:1378
RTEKind rtekind
Definition: parsenodes.h:945
char * ctename
Definition: parsenodes.h:1015
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1056
Alias * alias
Definition: primnodes.h:72
Alias * eref
Definition: parsenodes.h:1049
List * ctecolcollations
Definition: parsenodes.h:1380
#define copyObject(obj)
Definition: nodes.h:623
List * coltypes
Definition: parsenodes.h:1035
List * p_rtable
Definition: parse_node.h:172
RangeTblEntry* addRangeTableEntryForENR ( ParseState pstate,
RangeVar rv,
bool  inFromCl 
)

Definition at line 1977 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, tupleDesc::natts, NIL, ParseState::p_rtable, RangeTblEntry::relid, EphemeralNamedRelationMetadataData::reliddesc, RangeVar::relname, RangeTblEntry::requiredPerms, RTE_NAMEDTUPLESTORE, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, and TupleDescAttr.

Referenced by getRTEForSpecialRelationTypes().

1980 {
1982  Alias *alias = rv->alias;
1983  char *refname = alias ? alias->aliasname : rv->relname;
1985  TupleDesc tupdesc;
1986  int attno;
1987 
1988  Assert(pstate != NULL);
1989  enrmd = get_visible_ENR(pstate, rv->relname);
1990  Assert(enrmd != NULL);
1991 
1992  switch (enrmd->enrtype)
1993  {
1994  case ENR_NAMED_TUPLESTORE:
1996  break;
1997 
1998  default:
1999  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2000  return NULL; /* for fussy compilers */
2001  }
2002 
2003  /*
2004  * Record dependency on a relation. This allows plans to be invalidated
2005  * if they access transition tables linked to a table that is altered.
2006  */
2007  rte->relid = enrmd->reliddesc;
2008 
2009  /*
2010  * Build the list of effective column names using user-supplied aliases
2011  * and/or actual column names.
2012  */
2013  tupdesc = ENRMetadataGetTupDesc(enrmd);
2014  rte->eref = makeAlias(refname, NIL);
2015  buildRelationAliases(tupdesc, alias, rte->eref);
2016 
2017  /* Record additional data for ENR, including column type info */
2018  rte->enrname = enrmd->name;
2019  rte->enrtuples = enrmd->enrtuples;
2020  rte->coltypes = NIL;
2021  rte->coltypmods = NIL;
2022  rte->colcollations = NIL;
2023  for (attno = 1; attno <= tupdesc->natts; ++attno)
2024  {
2025  Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2026 
2027  if (att->attisdropped)
2028  {
2029  /* Record zeroes for a dropped column */
2030  rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2031  rte->coltypmods = lappend_int(rte->coltypmods, 0);
2033  }
2034  else
2035  {
2036  /* Let's just make sure we can tell this isn't dropped */
2037  if (att->atttypid == InvalidOid)
2038  elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2039  rv->relname);
2040  rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2041  rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2043  att->attcollation);
2044  }
2045  }
2046 
2047  /*
2048  * Set flags and access permissions.
2049  *
2050  * ENRs are never checked for access rights.
2051  */
2052  rte->lateral = false;
2053  rte->inh = false; /* never true for ENRs */
2054  rte->inFromCl = inFromCl;
2055 
2056  rte->requiredPerms = 0;
2057  rte->checkAsUser = InvalidOid;
2058  rte->selectedCols = NULL;
2059 
2060  /*
2061  * Add completed RTE to pstate's range table list, but not to join list
2062  * nor namespace --- caller must do that if appropriate.
2063  */
2064  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2065 
2066  return rte;
2067 }
#define NIL
Definition: pg_list.h:69
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
List * coltypmods
Definition: parsenodes.h:1036
AclMode requiredPerms
Definition: parsenodes.h:1053
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
List * colcollations
Definition: parsenodes.h:1037
int natts
Definition: tupdesc.h:73
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:26
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
char * relname
Definition: primnodes.h:68
Bitmapset * selectedCols
Definition: parsenodes.h:1055
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
char * enrname
Definition: parsenodes.h:1042
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
char * aliasname
Definition: primnodes.h:42
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
RTEKind rtekind
Definition: parsenodes.h:945
double enrtuples
Definition: parsenodes.h:1043
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * alias
Definition: primnodes.h:72
EphemeralNameRelationType enrtype
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1049
List * coltypes
Definition: parsenodes.h:1035
List * p_rtable
Definition: parse_node.h:172
RangeTblEntry* addRangeTableEntryForFunction ( ParseState pstate,
List funcnames,
List funcexprs,
List coldeflists,
RangeFunction rangefunc,
bool  lateral,
bool  inFromCl 
)

Definition at line 1402 of file parse_relation.c.

References RangeFunction::alias, RangeTblEntry::alias, Alias::aliasname, Assert, buildRelationAliases(), RangeTblEntry::checkAsUser, CheckAttributeNamesTypes(), chooseScalarFunctionAlias(), ColumnDef::colname, CreateTemplateTupleDesc(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, exprLocation(), 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, INT8OID, InvalidOid, lappend(), lappend_int(), lappend_oid(), RangeTblEntry::lateral, lfirst, linitial, list_length(), ColumnDef::location, makeAlias(), makeNode, makeString(), tupleDesc::natts, NIL, RangeFunction::ordinality, ParseState::p_rtable, palloc(), parser_errposition(), pstrdup(), RangeTblEntry::relid, RELKIND_COMPOSITE_TYPE, RangeTblEntry::requiredPerms, RTE_FUNCTION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, TypeName::setof, RangeTblEntry::subquery, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, TYPEFUNC_SCALAR, ColumnDef::typeName, typenameTypeIdAndMod(), and RangeTblEntry::updatedCols.

Referenced by transformRangeFunction().

1409 {
1411  Alias *alias = rangefunc->alias;
1412  Alias *eref;
1413  char *aliasname;
1414  int nfuncs = list_length(funcexprs);
1415  TupleDesc *functupdescs;
1416  TupleDesc tupdesc;
1417  ListCell *lc1,
1418  *lc2,
1419  *lc3;
1420  int i;
1421  int j;
1422  int funcno;
1423  int natts,
1424  totalatts;
1425 
1426  Assert(pstate != NULL);
1427 
1428  rte->rtekind = RTE_FUNCTION;
1429  rte->relid = InvalidOid;
1430  rte->subquery = NULL;
1431  rte->functions = NIL; /* we'll fill this list below */
1432  rte->funcordinality = rangefunc->ordinality;
1433  rte->alias = alias;
1434 
1435  /*
1436  * Choose the RTE alias name. We default to using the first function's
1437  * name even when there's more than one; which is maybe arguable but beats
1438  * using something constant like "table".
1439  */
1440  if (alias)
1441  aliasname = alias->aliasname;
1442  else
1443  aliasname = linitial(funcnames);
1444 
1445  eref = makeAlias(aliasname, NIL);
1446  rte->eref = eref;
1447 
1448  /* Process each function ... */
1449  functupdescs = (TupleDesc *) palloc(nfuncs * sizeof(TupleDesc));
1450 
1451  totalatts = 0;
1452  funcno = 0;
1453  forthree(lc1, funcexprs, lc2, funcnames, lc3, coldeflists)
1454  {
1455  Node *funcexpr = (Node *) lfirst(lc1);
1456  char *funcname = (char *) lfirst(lc2);
1457  List *coldeflist = (List *) lfirst(lc3);
1459  TypeFuncClass functypclass;
1460  Oid funcrettype;
1461 
1462  /* Initialize RangeTblFunction node */
1463  rtfunc->funcexpr = funcexpr;
1464  rtfunc->funccolnames = NIL;
1465  rtfunc->funccoltypes = NIL;
1466  rtfunc->funccoltypmods = NIL;
1467  rtfunc->funccolcollations = NIL;
1468  rtfunc->funcparams = NULL; /* not set until planning */
1469 
1470  /*
1471  * Now determine if the function returns a simple or composite type.
1472  */
1473  functypclass = get_expr_result_type(funcexpr,
1474  &funcrettype,
1475  &tupdesc);
1476 
1477  /*
1478  * A coldeflist is required if the function returns RECORD and hasn't
1479  * got a predetermined record type, and is prohibited otherwise.
1480  */
1481  if (coldeflist != NIL)
1482  {
1483  if (functypclass != TYPEFUNC_RECORD)
1484  ereport(ERROR,
1485  (errcode(ERRCODE_SYNTAX_ERROR),
1486  errmsg("a column definition list is only allowed for functions returning \"record\""),
1487  parser_errposition(pstate,
1488  exprLocation((Node *) coldeflist))));
1489  }
1490  else
1491  {
1492  if (functypclass == TYPEFUNC_RECORD)
1493  ereport(ERROR,
1494  (errcode(ERRCODE_SYNTAX_ERROR),
1495  errmsg("a column definition list is required for functions returning \"record\""),
1496  parser_errposition(pstate, exprLocation(funcexpr))));
1497  }
1498 
1499  if (functypclass == TYPEFUNC_COMPOSITE)
1500  {
1501  /* Composite data type, e.g. a table's row type */
1502  Assert(tupdesc);
1503  }
1504  else if (functypclass == TYPEFUNC_SCALAR)
1505  {
1506  /* Base data type, i.e. scalar */
1507  tupdesc = CreateTemplateTupleDesc(1, false);
1508  TupleDescInitEntry(tupdesc,
1509  (AttrNumber) 1,
1510  chooseScalarFunctionAlias(funcexpr, funcname,
1511  alias, nfuncs),
1512  funcrettype,
1513  -1,
1514  0);
1515  }
1516  else if (functypclass == TYPEFUNC_RECORD)
1517  {
1518  ListCell *col;
1519 
1520  /*
1521  * Use the column definition list to construct a tupdesc and fill
1522  * in the RangeTblFunction's lists.
1523  */
1524  tupdesc = CreateTemplateTupleDesc(list_length(coldeflist), false);
1525  i = 1;
1526  foreach(col, coldeflist)
1527  {
1528  ColumnDef *n = (ColumnDef *) lfirst(col);
1529  char *attrname;
1530  Oid attrtype;
1531  int32 attrtypmod;
1532  Oid attrcollation;
1533 
1534  attrname = n->colname;
1535  if (n->typeName->setof)
1536  ereport(ERROR,
1537  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1538  errmsg("column \"%s\" cannot be declared SETOF",
1539  attrname),
1540  parser_errposition(pstate, n->location)));
1541  typenameTypeIdAndMod(pstate, n->typeName,
1542  &attrtype, &attrtypmod);
1543  attrcollation = GetColumnDefCollation(pstate, n, attrtype);
1544  TupleDescInitEntry(tupdesc,
1545  (AttrNumber) i,
1546  attrname,
1547  attrtype,
1548  attrtypmod,
1549  0);
1551  (AttrNumber) i,
1552  attrcollation);
1553  rtfunc->funccolnames = lappend(rtfunc->funccolnames,
1554  makeString(pstrdup(attrname)));
1555  rtfunc->funccoltypes = lappend_oid(rtfunc->funccoltypes,
1556  attrtype);
1557  rtfunc->funccoltypmods = lappend_int(rtfunc->funccoltypmods,
1558  attrtypmod);
1560  attrcollation);
1561 
1562  i++;
1563  }
1564 
1565  /*
1566  * Ensure that the coldeflist defines a legal set of names (no
1567  * duplicates) and datatypes (no pseudo-types, for instance).
1568  */
1570  }
1571  else
1572  ereport(ERROR,
1573  (errcode(ERRCODE_DATATYPE_MISMATCH),
1574  errmsg("function \"%s\" in FROM has unsupported return type %s",
1575  funcname, format_type_be(funcrettype)),
1576  parser_errposition(pstate, exprLocation(funcexpr))));
1577 
1578  /* Finish off the RangeTblFunction and add it to the RTE's list */
1579  rtfunc->funccolcount = tupdesc->natts;
1580  rte->functions = lappend(rte->functions, rtfunc);
1581 
1582  /* Save the tupdesc for use below */
1583  functupdescs[funcno] = tupdesc;
1584  totalatts += tupdesc->natts;
1585  funcno++;
1586  }
1587 
1588  /*
1589  * If there's more than one function, or we want an ordinality column, we
1590  * have to produce a merged tupdesc.
1591  */
1592  if (nfuncs > 1 || rangefunc->ordinality)
1593  {
1594  if (rangefunc->ordinality)
1595  totalatts++;
1596 
1597  /* Merge the tuple descs of each function into a composite one */
1598  tupdesc = CreateTemplateTupleDesc(totalatts, false);
1599  natts = 0;
1600  for (i = 0; i < nfuncs; i++)
1601  {
1602  for (j = 1; j <= functupdescs[i]->natts; j++)
1603  TupleDescCopyEntry(tupdesc, ++natts, functupdescs[i], j);
1604  }
1605 
1606  /* Add the ordinality column if needed */
1607  if (rangefunc->ordinality)
1608  TupleDescInitEntry(tupdesc,
1609  (AttrNumber) ++natts,
1610  "ordinality",
1611  INT8OID,
1612  -1,
1613  0);
1614 
1615  Assert(natts == totalatts);
1616  }
1617  else
1618  {
1619  /* We can just use the single function's tupdesc as-is */
1620  tupdesc = functupdescs[0];
1621  }
1622 
1623  /* Use the tupdesc while assigning column aliases for the RTE */
1624  buildRelationAliases(tupdesc, alias, eref);
1625 
1626  /*
1627  * Set flags and access permissions.
1628  *
1629  * Functions are never checked for access rights (at least, not by the RTE
1630  * permissions mechanism).
1631  */
1632  rte->lateral = lateral;
1633  rte->inh = false; /* never true for functions */
1634  rte->inFromCl = inFromCl;
1635 
1636  rte->requiredPerms = 0;
1637  rte->checkAsUser = InvalidOid;
1638  rte->selectedCols = NULL;
1639  rte->insertedCols = NULL;
1640  rte->updatedCols = NULL;
1641 
1642  /*
1643  * Add completed RTE to pstate's range table list, but not to join list
1644  * nor namespace --- caller must do that if appropriate.
1645  */
1646  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1647 
1648  return rte;
1649 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
Alias * alias
Definition: parsenodes.h:560
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, bool allow_system_table_mods)
Definition: heap.c:410
Alias * alias
Definition: parsenodes.h:1048
static char * chooseScalarFunctionAlias(Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:203
char * pstrdup(const char *in)
Definition: mcxt.c:1076
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
AclMode requiredPerms
Definition: parsenodes.h:1053
bool funcordinality
Definition: parsenodes.h:1000
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
int natts
Definition: tupdesc.h:73
#define RELKIND_COMPOSITE_TYPE
Definition: pg_class.h:166
signed int int32
Definition: c.h:246
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:1055
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:211
bool setof
Definition: parsenodes.h:210
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:664
int location
Definition: parsenodes.h:654
TypeFuncClass
Definition: funcapi.h:150
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:505
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
Oid GetColumnDefCollation(ParseState *pstate, ColumnDef *coldef, Oid typeOid)
Definition: parse_type.c:521
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:293
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1086
Bitmapset * updatedCols
Definition: parsenodes.h:1057
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:1087
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:999
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
TypeName * typeName
Definition: parsenodes.h:640
Bitmapset * funcparams
Definition: parsenodes.h:1089
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:43
RTEKind rtekind
Definition: parsenodes.h:945
Query * subquery
Definition: parsenodes.h:968
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1056
int i
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
char * colname
Definition: parsenodes.h:639
Alias * eref
Definition: parsenodes.h:1049
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
List * p_rtable
Definition: parse_node.h:172
RangeTblEntry* addRangeTableEntryForJoin ( ParseState pstate,
List colnames,
JoinType  jointype,
List aliasvars,
Alias alias,
bool  inFromCl 
)

Definition at line 1798 of file parse_relation.c.

References RangeTblEntry::alias, Assert, RangeTblEntry::checkAsUser, Alias::colnames, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, 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().

1804 {
1806  Alias *eref;
1807  int numaliases;
1808 
1809  Assert(pstate != NULL);
1810 
1811  /*
1812  * Fail if join has too many columns --- we must be able to reference any
1813  * of the columns with an AttrNumber.
1814  */
1815  if (list_length(aliasvars) > MaxAttrNumber)
1816  ereport(ERROR,
1817  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1818  errmsg("joins can have at most %d columns",
1819  MaxAttrNumber)));
1820 
1821  rte->rtekind = RTE_JOIN;
1822  rte->relid = InvalidOid;
1823  rte->subquery = NULL;
1824  rte->jointype = jointype;
1825  rte->joinaliasvars = aliasvars;
1826  rte->alias = alias;
1827 
1828  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
1829  numaliases = list_length(eref->colnames);
1830 
1831  /* fill in any unspecified alias columns */
1832  if (numaliases < list_length(colnames))
1833  eref->colnames = list_concat(eref->colnames,
1834  list_copy_tail(colnames, numaliases));
1835 
1836  rte->eref = eref;
1837 
1838  /*
1839  * Set flags and access permissions.
1840  *
1841  * Joins are never checked for access rights.
1842  */
1843  rte->lateral = false;
1844  rte->inh = false; /* never true for joins */
1845  rte->inFromCl = inFromCl;
1846 
1847  rte->requiredPerms = 0;
1848  rte->checkAsUser = InvalidOid;
1849  rte->selectedCols = NULL;
1850  rte->insertedCols = NULL;
1851  rte->updatedCols = NULL;
1852 
1853  /*
1854  * Add completed RTE to pstate's range table list, but not to join list
1855  * nor namespace --- caller must do that if appropriate.
1856  */
1857  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1858 
1859  return rte;
1860 }
#define NIL
Definition: pg_list.h:69
List * joinaliasvars
Definition: parsenodes.h:989
Alias * alias
Definition: parsenodes.h:1048
List * colnames
Definition: primnodes.h:43
#define MaxAttrNumber
Definition: attnum.h:24
int errcode(int sqlerrcode)
Definition: elog.c:575
List * list_concat(List *list1, List *list2)
Definition: list.c:321
AclMode requiredPerms
Definition: parsenodes.h:1053
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:1055
#define ERROR
Definition: elog.h:43
JoinType jointype
Definition: parsenodes.h:988
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1057
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:945
Query * subquery
Definition: parsenodes.h:968
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1056
Alias * eref
Definition: parsenodes.h:1049
#define copyObject(obj)
Definition: nodes.h:623
List * p_rtable
Definition: parse_node.h:172
RangeTblEntry* addRangeTableEntryForRelation ( ParseState pstate,
Relation  rel,
Alias alias,
bool  inh,
bool  inFromCl 
)

Definition at line 1267 of file parse_relation.c.

References ACL_SELECT, RangeTblEntry::alias, Alias::aliasname, Assert, buildRelationAliases(), RangeTblEntry::checkAsUser, RangeTblEntry::eref, 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::requiredPerms, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, and RangeTblEntry::updatedCols.

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

1272 {
1274  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1275 
1276  Assert(pstate != NULL);
1277 
1278  rte->rtekind = RTE_RELATION;
1279  rte->alias = alias;
1280  rte->relid = RelationGetRelid(rel);
1281  rte->relkind = rel->rd_rel->relkind;
1282 
1283  /*
1284  * Build the list of effective column names using user-supplied aliases
1285  * and/or actual column names.
1286  */
1287  rte->eref = makeAlias(refname, NIL);
1288  buildRelationAliases(rel->rd_att, alias, rte->eref);
1289 
1290  /*
1291  * Set flags and access permissions.
1292  *
1293  * The initial default on access checks is always check-for-READ-access,
1294  * which is the right thing for all except target tables.
1295  */
1296  rte->lateral = false;
1297  rte->inh = inh;
1298  rte->inFromCl = inFromCl;
1299 
1300  rte->requiredPerms = ACL_SELECT;
1301  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1302  rte->selectedCols = NULL;
1303  rte->insertedCols = NULL;
1304  rte->updatedCols = NULL;
1305 
1306  /*
1307  * Add completed RTE to pstate's range table list, but not to join list
1308  * nor namespace --- caller must do that if appropriate.
1309  */
1310  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1311 
1312  return rte;
1313 }
#define NIL
Definition: pg_list.h:69
Alias * alias
Definition: parsenodes.h:1048
AclMode requiredPerms
Definition: parsenodes.h:1053
Form_pg_class rd_rel
Definition: rel.h:114
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:1055
#define RelationGetRelationName(relation)
Definition: rel.h:436
List * lappend(List *list, void *datum)
Definition: list.c:128
#define ACL_SELECT
Definition: parsenodes.h:73
TupleDesc rd_att
Definition: rel.h:115
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1057
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:945
Bitmapset * insertedCols
Definition: parsenodes.h:1056
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * eref
Definition: parsenodes.h:1049
#define RelationGetRelid(relation)
Definition: rel.h:416
List * p_rtable
Definition: parse_node.h:172
RangeTblEntry* addRangeTableEntryForSubquery ( ParseState pstate,
Query subquery,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1322 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Assert, RangeTblEntry::checkAsUser, Alias::colnames, copyObject, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::insertedCols, InvalidOid, lappend(), RangeTblEntry::lateral, lfirst, list_length(), makeNode, makeString(), ParseState::p_rtable, pstrdup(), RangeTblEntry::relid, 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().

1327 {
1329  char *refname = alias->aliasname;
1330  Alias *eref;
1331  int numaliases;
1332  int varattno;
1333  ListCell *tlistitem;
1334 
1335  Assert(pstate != NULL);
1336 
1337  rte->rtekind = RTE_SUBQUERY;
1338  rte->relid = InvalidOid;
1339  rte->subquery = subquery;
1340  rte->alias = alias;
1341 
1342  eref = copyObject(alias);
1343  numaliases = list_length(eref->colnames);
1344 
1345  /* fill in any unspecified alias columns */
1346  varattno = 0;
1347  foreach(tlistitem, subquery->targetList)
1348  {
1349  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1350 
1351  if (te->resjunk)
1352  continue;
1353  varattno++;
1354  Assert(varattno == te->resno);
1355  if (varattno > numaliases)
1356  {
1357  char *attrname;
1358 
1359  attrname = pstrdup(te->resname);
1360  eref->colnames = lappend(eref->colnames, makeString(attrname));
1361  }
1362  }
1363  if (varattno < numaliases)
1364  ereport(ERROR,
1365  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1366  errmsg("table \"%s\" has %d columns available but %d columns specified",
1367  refname, varattno, numaliases)));
1368 
1369  rte->eref = eref;
1370 
1371  /*
1372  * Set flags and access permissions.
1373  *
1374  * Subqueries are never checked for access rights.
1375  */
1376  rte->lateral = lateral;
1377  rte->inh = false; /* never true for subqueries */
1378  rte->inFromCl = inFromCl;
1379 
1380  rte->requiredPerms = 0;
1381  rte->checkAsUser = InvalidOid;
1382  rte->selectedCols = NULL;
1383  rte->insertedCols = NULL;
1384  rte->updatedCols = NULL;
1385 
1386  /*
1387  * Add completed RTE to pstate's range table list, but not to join list
1388  * nor namespace --- caller must do that if appropriate.
1389  */
1390  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1391 
1392  return rte;
1393 }
Value * makeString(char *str)
Definition: value.c:53
Alias * alias
Definition: parsenodes.h:1048
List * colnames
Definition: primnodes.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1076
int errcode(int sqlerrcode)
Definition: elog.c:575
AclMode requiredPerms
Definition: parsenodes.h:1053
char * resname
Definition: primnodes.h:1370
List * targetList
Definition: parsenodes.h:138
Bitmapset * selectedCols
Definition: parsenodes.h:1055
bool resjunk
Definition: primnodes.h:1375
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1369
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1057
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:945
Query * subquery
Definition: parsenodes.h:968
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1056
Alias * eref
Definition: parsenodes.h:1049
#define copyObject(obj)
Definition: nodes.h:623
List * p_rtable
Definition: parse_node.h:172
RangeTblEntry* addRangeTableEntryForTableFunc ( ParseState pstate,
TableFunc tf,
Alias alias,
bool  lateral,
bool  inFromCl 
)

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

1662 {
1664  char *refname = alias ? alias->aliasname : pstrdup("xmltable");
1665  Alias *eref;
1666  int numaliases;
1667 
1668  Assert(pstate != NULL);
1669 
1670  rte->rtekind = RTE_TABLEFUNC;
1671  rte->relid = InvalidOid;
1672  rte->subquery = NULL;
1673  rte->tablefunc = tf;
1674  rte->coltypes = tf->coltypes;
1675  rte->coltypmods = tf->coltypmods;
1676  rte->colcollations = tf->colcollations;
1677  rte->alias = alias;
1678 
1679  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1680  numaliases = list_length(eref->colnames);
1681 
1682  /* fill in any unspecified alias columns */
1683  if (numaliases < list_length(tf->colnames))
1684  eref->colnames = list_concat(eref->colnames,
1685  list_copy_tail(tf->colnames, numaliases));
1686 
1687  rte->eref = eref;
1688 
1689  /*
1690  * Set flags and access permissions.
1691  *
1692  * Tablefuncs are never checked for access rights (at least, not by the
1693  * RTE permissions mechanism).
1694  */
1695  rte->lateral = lateral;
1696  rte->inh = false; /* never true for tablefunc RTEs */
1697  rte->inFromCl = inFromCl;
1698 
1699  rte->requiredPerms = 0;
1700  rte->checkAsUser = InvalidOid;
1701  rte->selectedCols = NULL;
1702  rte->insertedCols = NULL;
1703  rte->updatedCols = NULL;
1704 
1705  /*
1706  * Add completed RTE to pstate's range table list, but not to join list
1707  * nor namespace --- caller must do that if appropriate.
1708  */
1709  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1710 
1711  return rte;
1712 }
#define NIL
Definition: pg_list.h:69
List * colnames
Definition: primnodes.h:86
Alias * alias
Definition: parsenodes.h:1048
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: primnodes.h:88
char * pstrdup(const char *in)
Definition: mcxt.c:1076
List * coltypmods
Definition: parsenodes.h:1036
List * list_concat(List *list1, List *list2)
Definition: list.c:321
AclMode requiredPerms
Definition: parsenodes.h:1053
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
List * colcollations
Definition: parsenodes.h:1037
List * colcollations
Definition: primnodes.h:89
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:1055
TableFunc * tablefunc
Definition: parsenodes.h:1005
List * lappend(List *list, void *datum)
Definition: list.c:128
List * coltypes
Definition: primnodes.h:87
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1057
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:945
Query * subquery
Definition: parsenodes.h:968
Bitmapset * insertedCols
Definition: parsenodes.h:1056
Alias * eref
Definition: parsenodes.h:1049
#define copyObject(obj)
Definition: nodes.h:623
List * coltypes
Definition: parsenodes.h:1035
List * p_rtable
Definition: parse_node.h:172
RangeTblEntry* addRangeTableEntryForValues ( ParseState pstate,
List exprs,
List coltypes,
List coltypmods,
List colcollations,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1720 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::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 transformValuesClause().

1728 {
1730  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
1731  Alias *eref;
1732  int numaliases;
1733  int numcolumns;
1734 
1735  Assert(pstate != NULL);
1736 
1737  rte->rtekind = RTE_VALUES;
1738  rte->relid = InvalidOid;
1739  rte->subquery = NULL;
1740  rte->values_lists = exprs;
1741  rte->coltypes = coltypes;
1742  rte->coltypmods = coltypmods;
1743  rte->colcollations = colcollations;
1744  rte->alias = alias;
1745 
1746  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1747 
1748  /* fill in any unspecified alias columns */
1749  numcolumns = list_length((List *) linitial(exprs));
1750  numaliases = list_length(eref->colnames);
1751  while (numaliases < numcolumns)
1752  {
1753  char attrname[64];
1754 
1755  numaliases++;
1756  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
1757  eref->colnames = lappend(eref->colnames,
1758  makeString(pstrdup(attrname)));
1759  }
1760  if (numcolumns < numaliases)
1761  ereport(ERROR,
1762  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1763  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
1764  refname, numcolumns, numaliases)));
1765 
1766  rte->eref = eref;
1767 
1768  /*
1769  * Set flags and access permissions.
1770  *
1771  * Subqueries are never checked for access rights.
1772  */
1773  rte->lateral = lateral;
1774  rte->inh = false; /* never true for values RTEs */
1775  rte->inFromCl = inFromCl;
1776 
1777  rte->requiredPerms = 0;
1778  rte->checkAsUser = InvalidOid;
1779  rte->selectedCols = NULL;
1780  rte->insertedCols = NULL;
1781  rte->updatedCols = NULL;
1782 
1783  /*
1784  * Add completed RTE to pstate's range table list, but not to join list
1785  * nor namespace --- caller must do that if appropriate.
1786  */
1787  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1788 
1789  return rte;
1790 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
Alias * alias
Definition: parsenodes.h:1048
List * colnames
Definition: primnodes.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1076
List * coltypmods
Definition: parsenodes.h:1036
int errcode(int sqlerrcode)
Definition: elog.c:575
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
AclMode requiredPerms
Definition: parsenodes.h:1053
List * colcollations
Definition: parsenodes.h:1037
List * values_lists
Definition: parsenodes.h:1010
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:1055
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:1057
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:945
Query * subquery
Definition: parsenodes.h:968
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1056
Alias * eref
Definition: parsenodes.h:1049
#define copyObject(obj)
Definition: nodes.h:623
List * coltypes
Definition: parsenodes.h:1035
Definition: pg_list.h:45
List * p_rtable
Definition: parse_node.h:172
void addRTEtoQuery ( ParseState pstate,
RangeTblEntry rte,
bool  addToJoinList,
bool  addToRelNameSpace,
bool  addToVarNameSpace 
)

Definition at line 2128 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(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformOnConflictArbiter(), transformOnConflictClause(), transformPartitionSpec(), transformRuleStmt(), transformSetOperationStmt(), and transformValuesClause().

2131 {
2132  if (addToJoinList)
2133  {
2134  int rtindex = RTERangeTablePosn(pstate, rte, NULL);
2136 
2137  rtr->rtindex = rtindex;
2138  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2139  }
2140  if (addToRelNameSpace || addToVarNameSpace)
2141  {
2142  ParseNamespaceItem *nsitem;
2143 
2144  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2145  nsitem->p_rte = rte;
2146  nsitem->p_rel_visible = addToRelNameSpace;
2147  nsitem->p_cols_visible = addToVarNameSpace;
2148  nsitem->p_lateral_only = false;
2149  nsitem->p_lateral_ok = true;
2150  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2151  }
2152 }
RangeTblEntry * p_rte
Definition: parse_node.h:246
List * p_namespace
Definition: parse_node.h:176
List * lappend(List *list, void *datum)
Definition: list.c:128
#define makeNode(_type_)
Definition: nodes.h:558
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
void * palloc(Size size)
Definition: mcxt.c:848
List * p_joinlist
Definition: parse_node.h:174
int attnameAttNum ( Relation  rd,
const char *  attname,
bool  sysColOK 
)

Definition at line 3067 of file parse_relation.c.

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

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

3068 {
3069  int i;
3070 
3071  for (i = 0; i < rd->rd_rel->relnatts; i++)
3072  {
3073  Form_pg_attribute att = TupleDescAttr(rd->rd_att, i);
3074 
3075  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3076  return i + 1;
3077  }
3078 
3079  if (sysColOK)
3080  {
3081  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
3082  {
3083  if (i != ObjectIdAttributeNumber || rd->rd_rel->relhasoids)
3084  return i;
3085  }
3086  }
3087 
3088  /* on failure */
3089  return InvalidAttrNumber;
3090 }
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
int namestrcmp(Name name, const char *str)
Definition: name.c:247
static int specialAttNum(const char *attname)
Form_pg_class rd_rel
Definition: rel.h:114
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
TupleDesc rd_att
Definition: rel.h:115
#define InvalidAttrNumber
Definition: attnum.h:23
int i
Name attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3122 of file parse_relation.c.

References elog, ERROR, tupleDesc::natts, RelationData::rd_att, RelationData::rd_rel, SystemAttributeDefinition(), and TupleDescAttr.

Referenced by transformFkeyGetPrimaryKey().

3123 {
3124  if (attid <= 0)
3125  {
3126  Form_pg_attribute sysatt;
3127 
3128  sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
3129  return &sysatt->attname;
3130  }
3131  if (attid > rd->rd_att->natts)
3132  elog(ERROR, "invalid attribute number %d", attid);
3133  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3134 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
Form_pg_class rd_rel
Definition: rel.h:114
int natts
Definition: tupdesc.h:73
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
TupleDesc rd_att
Definition: rel.h:115
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:200
#define elog
Definition: elog.h:219
Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3164 of file parse_relation.c.

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

3165 {
3166  if (attid <= 0)
3167  {
3168  /* All system attributes are of noncollatable types. */
3169  return InvalidOid;
3170  }
3171  if (attid > rd->rd_att->natts)
3172  elog(ERROR, "invalid attribute number %d", attid);
3173  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3174 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
int natts
Definition: tupdesc.h:73
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:115
#define InvalidOid
Definition: postgres_ext.h:36
#define elog
Definition: elog.h:219
Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3144 of file parse_relation.c.

References elog, ERROR, tupleDesc::natts, RelationData::rd_att, RelationData::rd_rel, SystemAttributeDefinition(), and TupleDescAttr.

Referenced by refresh_by_match_merge(), transformAssignedExpr(), and transformFkeyGetPrimaryKey().

3145 {
3146  if (attid <= 0)
3147  {
3148  Form_pg_attribute sysatt;
3149 
3150  sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
3151  return sysatt->atttypid;
3152  }
3153  if (attid > rd->rd_att->natts)
3154  elog(ERROR, "invalid attribute number %d", attid);
3155  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3156 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
Form_pg_class rd_rel
Definition: rel.h:114
int natts
Definition: tupdesc.h:73
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
TupleDesc rd_att
Definition: rel.h:115
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:200
#define elog
Definition: elog.h:219
void checkNameSpaceConflicts ( ParseState pstate,
List namespace1,
List namespace2 
)

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

391 {
392  ListCell *l1;
393 
394  foreach(l1, namespace1)
395  {
396  ParseNamespaceItem *nsitem1 = (ParseNamespaceItem *) lfirst(l1);
397  RangeTblEntry *rte1 = nsitem1->p_rte;
398  const char *aliasname1 = rte1->eref->aliasname;
399  ListCell *l2;
400 
401  if (!nsitem1->p_rel_visible)
402  continue;
403 
404  foreach(l2, namespace2)
405  {
406  ParseNamespaceItem *nsitem2 = (ParseNamespaceItem *) lfirst(l2);
407  RangeTblEntry *rte2 = nsitem2->p_rte;
408 
409  if (!nsitem2->p_rel_visible)
410  continue;
411  if (strcmp(rte2->eref->aliasname, aliasname1) != 0)
412  continue; /* definitely no conflict */
413  if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
414  rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
415  rte1->relid != rte2->relid)
416  continue; /* no conflict per SQL rule */
417  ereport(ERROR,
418  (errcode(ERRCODE_DUPLICATE_ALIAS),
419  errmsg("table name \"%s\" specified more than once",
420  aliasname1)));
421  }
422  }
423 }
Alias * alias
Definition: parsenodes.h:1048
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:246
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:945
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:1049
Node* colNameToVar ( ParseState pstate,
char *  colname,
bool  localonly,
int  location 
)

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

759 {
760  Node *result = NULL;
761  ParseState *orig_pstate = pstate;
762 
763  while (pstate != NULL)
764  {
765  ListCell *l;
766 
767  foreach(l, pstate->p_namespace)
768  {
770  RangeTblEntry *rte = nsitem->p_rte;
771  Node *newresult;
772 
773  /* Ignore table-only items */
774  if (!nsitem->p_cols_visible)
775  continue;
776  /* If not inside LATERAL, ignore lateral-only items */
777  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
778  continue;
779 
780  /* use orig_pstate here to get the right sublevels_up */
781  newresult = scanRTEForColumn(orig_pstate, rte, colname, location,
782  0, NULL);
783 
784  if (newresult)
785  {
786  if (result)
787  ereport(ERROR,
788  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
789  errmsg("column reference \"%s\" is ambiguous",
790  colname),
791  parser_errposition(pstate, location)));
792  check_lateral_ref_ok(pstate, nsitem, location);
793  result = newresult;
794  }
795  }
796 
797  if (result != NULL || localonly)
798  break; /* found, or don't want to look at parent */
799 
800  pstate = pstate->parentParseState;
801  }
802 
803  return result;
804 }
Node * scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:246
#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:176
#define ereport(elevel, rest)
Definition: elog.h:122
struct ParseState * parentParseState
Definition: parse_node.h:170
bool p_lateral_active
Definition: parse_node.h:178
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
void errorMissingColumn ( ParseState pstate,
char *  relname,
char *  colname,
int  location 
)

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

3241 {
3243  char *closestfirst = NULL;
3244 
3245  /*
3246  * Search the entire rtable looking for possible matches. If we find one,
3247  * emit a hint about it.
3248  *
3249  * TODO: improve this code (and also errorMissingRTE) to mention using
3250  * LATERAL if appropriate.
3251  */
3252  state = searchRangeTableForCol(pstate, relname, colname, location);
3253 
3254  /*
3255  * Extract closest col string for best match, if any.
3256  *
3257  * Infer an exact match referenced despite not being visible from the fact
3258  * that an attribute number was not present in state passed back -- this
3259  * is what is reported when !closestfirst. There might also be an exact
3260  * match that was qualified with an incorrect alias, in which case
3261  * closestfirst will be set (so hint is the same as generic fuzzy case).
3262  */
3263  if (state->rfirst && AttributeNumberIsValid(state->first))
3264  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3265  state->first - 1));
3266 
3267  if (!state->rsecond)
3268  {
3269  /*
3270  * Handle case where there is zero or one column suggestions to hint,
3271  * including exact matches referenced but not visible.
3272  */
3273  ereport(ERROR,
3274  (errcode(ERRCODE_UNDEFINED_COLUMN),
3275  relname ?
3276  errmsg("column %s.%s does not exist", relname, colname) :
3277  errmsg("column \"%s\" does not exist", colname),
3278  state->rfirst ? closestfirst ?
3279  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3280  state->rfirst->eref->aliasname, closestfirst) :
3281  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3282  colname, state->rfirst->eref->aliasname) : 0,
3283  parser_errposition(pstate, location)));
3284  }
3285  else
3286  {
3287  /* Handle case where there are two equally useful column hints */
3288  char *closestsecond;
3289 
3290  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3291  state->second - 1));
3292 
3293  ereport(ERROR,
3294  (errcode(ERRCODE_UNDEFINED_COLUMN),
3295  relname ?
3296  errmsg("column %s.%s does not exist", relname, colname) :
3297  errmsg("column \"%s\" does not exist", colname),
3298  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3299  state->rfirst->eref->aliasname, closestfirst,
3300  state->rsecond->eref->aliasname, closestsecond),
3301  parser_errposition(pstate, location)));
3302  }
3303 }
int errhint(const char *fmt,...)
Definition: elog.c:987
List * colnames
Definition: primnodes.h:43
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
RangeTblEntry * rfirst
void * list_nth(const List *list, int n)
Definition: list.c:410
#define ereport(elevel, rest)
Definition: elog.h:122
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
char * aliasname
Definition: primnodes.h:42
Definition: regguts.h:298
RangeTblEntry * rsecond
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:1049
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, char *colname, int location)
void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

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

3184 {
3185  RangeTblEntry *rte;
3186  int sublevels_up;
3187  const char *badAlias = NULL;
3188 
3189  /*
3190  * Check to see if there are any potential matches in the query's
3191  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3192  * will throw error immediately here. That seems OK.)
3193  */
3194  rte = searchRangeTableForRel(pstate, relation);
3195 
3196  /*
3197  * If we found a match that has an alias and the alias is visible in the
3198  * namespace, then the problem is probably use of the relation's real name
3199  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3200  * common enough to justify a specific hint.
3201  *
3202  * If we found a match that doesn't meet those criteria, assume the
3203  * problem is illegal use of a relation outside its scope, as in the
3204  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3205  */
3206  if (rte && rte->alias &&
3207  strcmp(rte->eref->aliasname, relation->relname) != 0 &&
3208  refnameRangeTblEntry(pstate, NULL, rte->eref->aliasname,
3209  relation->location,
3210  &sublevels_up) == rte)
3211  badAlias = rte->eref->aliasname;
3212 
3213  if (rte)
3214  ereport(ERROR,
3216  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3217  relation->relname),
3218  (badAlias ?
3219  errhint("Perhaps you meant to reference the table alias \"%s\".",
3220  badAlias) :
3221  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3222  rte->eref->aliasname)),
3223  parser_errposition(pstate, relation->location)));
3224  else
3225  ereport(ERROR,
3227  errmsg("missing FROM-clause entry for table \"%s\"",
3228  relation->relname),
3229  parser_errposition(pstate, relation->location)));
3230 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
Alias * alias
Definition: parsenodes.h:1048
int errcode(int sqlerrcode)
Definition: elog.c:575
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:122
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:1049
List* expandRelAttrs ( ParseState pstate,
RangeTblEntry rte,
int  rtindex,
int  sublevels_up,
int  location 
)

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

2608 {
2609  List *names,
2610  *vars;
2611  ListCell *name,
2612  *var;
2613  List *te_list = NIL;
2614 
2615  expandRTE(rte, rtindex, sublevels_up, location, false,
2616  &names, &vars);
2617 
2618  /*
2619  * Require read access to the table. This is normally redundant with the
2620  * markVarForSelectPriv calls below, but not if the table has zero
2621  * columns.
2622  */
2623  rte->requiredPerms |= ACL_SELECT;
2624 
2625  forboth(name, names, var, vars)
2626  {
2627  char *label = strVal(lfirst(name));
2628  Var *varnode = (Var *) lfirst(var);
2629  TargetEntry *te;
2630 
2631  te = makeTargetEntry((Expr *) varnode,
2632  (AttrNumber) pstate->p_next_resno++,
2633  label,
2634  false);
2635  te_list = lappend(te_list, te);
2636 
2637  /* Require read access to each column */
2638  markVarForSelectPriv(pstate, varnode, rte);
2639  }
2640 
2641  Assert(name == NULL && var == NULL); /* lists not the same length? */
2642 
2643  return te_list;
2644 }
#define NIL
Definition: pg_list.h:69
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
#define strVal(v)
Definition: value.h:54
AclMode requiredPerms
Definition: parsenodes.h:1053
Definition: primnodes.h:163
int p_next_resno
Definition: parse_node.h:187
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
List * lappend(List *list, void *datum)
Definition: list.c:128
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
#define ACL_SELECT
Definition: parsenodes.h:73
static char * label
Definition: pg_basebackup.c:82
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
const char * name
Definition: encode.c:521
Definition: regcomp.c:224
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
void expandRTE ( RangeTblEntry rte,
int  rtindex,
int  sublevels_up,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)

Definition at line 2172 of file parse_relation.c.

References Assert, 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(), INT4OID, INT8OID, 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_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, strVal, RangeTblEntry::subquery, Query::targetList, TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

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

2175 {
2176  int varattno;
2177 
2178  if (colnames)
2179  *colnames = NIL;
2180  if (colvars)
2181  *colvars = NIL;
2182 
2183  switch (rte->rtekind)
2184  {
2185  case RTE_RELATION:
2186  /* Ordinary relation RTE */
2187  expandRelation(rte->relid, rte->eref,
2188  rtindex, sublevels_up, location,
2189  include_dropped, colnames, colvars);
2190  break;
2191  case RTE_SUBQUERY:
2192  {
2193  /* Subquery RTE */
2194  ListCell *aliasp_item = list_head(rte->eref->colnames);
2195  ListCell *tlistitem;
2196 
2197  varattno = 0;
2198  foreach(tlistitem, rte->subquery->targetList)
2199  {
2200  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
2201 
2202  if (te->resjunk)
2203  continue;
2204  varattno++;
2205  Assert(varattno == te->resno);
2206 
2207  if (colnames)
2208  {
2209  /* Assume there is one alias per target item */
2210  char *label = strVal(lfirst(aliasp_item));
2211 
2212  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2213  aliasp_item = lnext(aliasp_item);
2214  }
2215 
2216  if (colvars)
2217  {
2218  Var *varnode;
2219 
2220  varnode = makeVar(rtindex, varattno,
2221  exprType((Node *) te->expr),
2222  exprTypmod((Node *) te->expr),
2223  exprCollation((Node *) te->expr),
2224  sublevels_up);
2225  varnode->location = location;
2226 
2227  *colvars = lappend(*colvars, varnode);
2228  }
2229  }
2230  }
2231  break;
2232  case RTE_FUNCTION:
2233  {
2234  /* Function RTE */
2235  int atts_done = 0;
2236  ListCell *lc;
2237 
2238  foreach(lc, rte->functions)
2239  {
2240  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2241  TypeFuncClass functypclass;
2242  Oid funcrettype;
2243  TupleDesc tupdesc;
2244 
2245  functypclass = get_expr_result_type(rtfunc->funcexpr,
2246  &funcrettype,
2247  &tupdesc);
2248  if (functypclass == TYPEFUNC_COMPOSITE)
2249  {
2250  /* Composite data type, e.g. a table's row type */
2251  Assert(tupdesc);
2252  expandTupleDesc(tupdesc, rte->eref,
2253  rtfunc->funccolcount, atts_done,
2254  rtindex, sublevels_up, location,
2255  include_dropped, colnames, colvars);
2256  }
2257  else if (functypclass == TYPEFUNC_SCALAR)
2258  {
2259  /* Base data type, i.e. scalar */
2260  if (colnames)
2261  *colnames = lappend(*colnames,
2262  list_nth(rte->eref->colnames,
2263  atts_done));
2264 
2265  if (colvars)
2266  {
2267  Var *varnode;
2268 
2269  varnode = makeVar(rtindex, atts_done + 1,
2270  funcrettype, -1,
2271  exprCollation(rtfunc->funcexpr),
2272  sublevels_up);
2273  varnode->location = location;
2274 
2275  *colvars = lappend(*colvars, varnode);
2276  }
2277  }
2278  else if (functypclass == TYPEFUNC_RECORD)
2279  {
2280  if (colnames)
2281  {
2282  List *namelist;
2283 
2284  /* extract appropriate subset of column list */
2285  namelist = list_copy_tail(rte->eref->colnames,
2286  atts_done);
2287  namelist = list_truncate(namelist,
2288  rtfunc->funccolcount);
2289  *colnames = list_concat(*colnames, namelist);
2290  }
2291 
2292  if (colvars)
2293  {
2294  ListCell *l1;
2295  ListCell *l2;
2296  ListCell *l3;
2297  int attnum = atts_done;
2298 
2299  forthree(l1, rtfunc->funccoltypes,
2300  l2, rtfunc->funccoltypmods,
2301  l3, rtfunc->funccolcollations)
2302  {
2303  Oid attrtype = lfirst_oid(l1);
2304  int32 attrtypmod = lfirst_int(l2);
2305  Oid attrcollation = lfirst_oid(l3);
2306  Var *varnode;
2307 
2308  attnum++;
2309  varnode = makeVar(rtindex,
2310  attnum,
2311  attrtype,
2312  attrtypmod,
2313  attrcollation,
2314  sublevels_up);
2315  varnode->location = location;
2316  *colvars = lappend(*colvars, varnode);
2317  }
2318  }
2319  }
2320  else
2321  {
2322  /* addRangeTableEntryForFunction should've caught this */
2323  elog(ERROR, "function in FROM has unsupported return type");
2324  }
2325  atts_done += rtfunc->funccolcount;
2326  }
2327 
2328  /* Append the ordinality column if any */
2329  if (rte->funcordinality)
2330  {
2331  if (colnames)
2332  *colnames = lappend(*colnames,
2333  llast(rte->eref->colnames));
2334 
2335  if (colvars)
2336  {
2337  Var *varnode = makeVar(rtindex,
2338  atts_done + 1,
2339  INT8OID,
2340  -1,
2341  InvalidOid,
2342  sublevels_up);
2343 
2344  *colvars = lappend(*colvars, varnode);
2345  }
2346  }
2347  }
2348  break;
2349  case RTE_JOIN:
2350  {
2351  /* Join RTE */
2352  ListCell *colname;
2353  ListCell *aliasvar;
2354 
2356 
2357  varattno = 0;
2358  forboth(colname, rte->eref->colnames, aliasvar, rte->joinaliasvars)
2359  {
2360  Node *avar = (Node *) lfirst(aliasvar);
2361 
2362  varattno++;
2363 
2364  /*
2365  * During ordinary parsing, there will never be any
2366  * deleted columns in the join; but we have to check since
2367  * this routine is also used by the rewriter, and joins
2368  * found in stored rules might have join columns for
2369  * since-deleted columns. This will be signaled by a null
2370  * pointer in the alias-vars list.
2371  */
2372  if (avar == NULL)
2373  {
2374  if (include_dropped)
2375  {
2376  if (colnames)
2377  *colnames = lappend(*colnames,
2378  makeString(pstrdup("")));
2379  if (colvars)
2380  {
2381  /*
2382  * Can't use join's column type here (it might
2383  * be dropped!); but it doesn't really matter
2384  * what type the Const claims to be.
2385  */
2386  *colvars = lappend(*colvars,
2387  makeNullConst(INT4OID, -1,
2388  InvalidOid));
2389  }
2390  }
2391  continue;
2392  }
2393 
2394  if (colnames)
2395  {
2396  char *label = strVal(lfirst(colname));
2397 
2398  *colnames = lappend(*colnames,
2399  makeString(pstrdup(label)));
2400  }
2401 
2402  if (colvars)
2403  {
2404  Var *varnode;
2405 
2406  varnode = makeVar(rtindex, varattno,
2407  exprType(avar),
2408  exprTypmod(avar),
2409  exprCollation(avar),
2410  sublevels_up);
2411  varnode->location = location;
2412 
2413  *colvars = lappend(*colvars, varnode);
2414  }
2415  }
2416  }
2417  break;
2418  case RTE_TABLEFUNC:
2419  case RTE_VALUES:
2420  case RTE_CTE:
2421  case RTE_NAMEDTUPLESTORE:
2422  {
2423  /* Tablefunc, Values, CTE, or ENR RTE */
2424  ListCell *aliasp_item = list_head(rte->eref->colnames);
2425  ListCell *lct;
2426  ListCell *lcm;
2427  ListCell *lcc;
2428 
2429  varattno = 0;
2430  forthree(lct, rte->coltypes,
2431  lcm, rte->coltypmods,
2432  lcc, rte->colcollations)
2433  {
2434  Oid coltype = lfirst_oid(lct);
2435  int32 coltypmod = lfirst_int(lcm);
2436  Oid colcoll = lfirst_oid(lcc);
2437 
2438  varattno++;
2439 
2440  if (colnames)
2441  {
2442  /* Assume there is one alias per output column */
2443  if (OidIsValid(coltype))
2444  {
2445  char *label = strVal(lfirst(aliasp_item));
2446 
2447  *colnames = lappend(*colnames,
2448  makeString(pstrdup(label)));
2449  }
2450  else if (include_dropped)
2451  *colnames = lappend(*colnames,
2452  makeString(pstrdup("")));
2453 
2454  aliasp_item = lnext(aliasp_item);
2455  }
2456 
2457  if (colvars)
2458  {
2459  if (OidIsValid(coltype))
2460  {
2461  Var *varnode;
2462 
2463  varnode = makeVar(rtindex, varattno,
2464  coltype, coltypmod, colcoll,
2465  sublevels_up);
2466  varnode->location = location;
2467 
2468  *colvars = lappend(*colvars, varnode);
2469  }
2470  else if (include_dropped)
2471  {
2472  /*
2473  * It doesn't really matter what type the Const
2474  * claims to be.
2475  */
2476  *colvars = lappend(*colvars,
2477  makeNullConst(INT4OID, -1,
2478  InvalidOid));
2479  }
2480  }
2481  }
2482  }
2483  break;
2484  default:
2485  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2486  }
2487 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
List * joinaliasvars
Definition: parsenodes.h:989
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
List * colnames
Definition: primnodes.h:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:203
char * pstrdup(const char *in)
Definition: mcxt.c:1076
List * coltypmods
Definition: parsenodes.h:1036
#define llast(l)
Definition: pg_list.h:131
List * list_truncate(List *list, int new_size)
Definition: list.c:350
#define INT4OID
Definition: pg_type.h:316
Definition: nodes.h:510
#define strVal(v)
Definition: value.h:54
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
bool funcordinality
Definition: parsenodes.h:1000
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
#define OidIsValid(objectId)
Definition: c.h:532
List * colcollations
Definition: parsenodes.h:1037
signed int int32
Definition: c.h:246
List * targetList
Definition: parsenodes.h:138
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
bool resjunk
Definition: primnodes.h:1375
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
int location
Definition: primnodes.h:178
void * list_nth(const List *list, int n)
Definition: list.c:410
AttrNumber resno
Definition: primnodes.h:1369
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
TypeFuncClass
Definition: funcapi.h:150
#define lnext(lc)
Definition: pg_list.h:105
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
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:128
static char * label
Definition: pg_basebackup.c:82
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1086
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:1087
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
List * functions
Definition: parsenodes.h:999
Expr * expr
Definition: primnodes.h:1368
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
RTEKind rtekind
Definition: parsenodes.h:945
Query * subquery
Definition: parsenodes.h:968
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1049
List * coltypes
Definition: parsenodes.h:1035
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
CommonTableExpr* GetCTEForRTE ( ParseState pstate,
RangeTblEntry rte,
int  rtelevelsup 
)

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

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

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

499 {
500  while (sublevels_up-- > 0)
501  {
502  pstate = pstate->parentParseState;
503  Assert(pstate != NULL);
504  }
505  Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
506  return rt_fetch(varno, pstate->p_rtable);
507 }
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
struct ParseState * parentParseState
Definition: parse_node.h:170
#define Assert(condition)
Definition: c.h:681
static int list_length(const List *l)
Definition: pg_list.h:89
List * p_rtable
Definition: parse_node.h:172
bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

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

2081 {
2082  ListCell *l;
2083 
2084  /*
2085  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2086  * parent level, then act as though there's a generic FOR UPDATE here.
2087  */
2088  if (pstate->p_locked_from_parent)
2089  return true;
2090 
2091  foreach(l, pstate->p_locking_clause)
2092  {
2093  LockingClause *lc = (LockingClause *) lfirst(l);
2094 
2095  if (lc->lockedRels == NIL)
2096  {
2097  /* all tables used in query */
2098  return true;
2099  }
2100  else
2101  {
2102  /* just the named tables */
2103  ListCell *l2;
2104 
2105  foreach(l2, lc->lockedRels)
2106  {
2107  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2108 
2109  if (strcmp(refname, thisrel->relname) == 0)
2110  return true;
2111  }
2112  }
2113  }
2114  return false;
2115 }
List * lockedRels
Definition: parsenodes.h:737
#define NIL
Definition: pg_list.h:69
bool p_locked_from_parent
Definition: parse_node.h:190
char * relname
Definition: primnodes.h:68
List * p_locking_clause
Definition: parse_node.h:189
#define lfirst(lc)
Definition: pg_list.h:106
bool isQueryUsingTempRelation ( Query query)

Definition at line 3311 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

3312 {
3313  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3314 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
Definition: nodes.h:510
void markVarForSelectPriv ( ParseState pstate,
Var var,
RangeTblEntry rte 
)

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

1004 {
1005  Index lv;
1006 
1007  Assert(IsA(var, Var));
1008  /* Find the appropriate pstate if it's an uplevel Var */
1009  for (lv = 0; lv < var->varlevelsup; lv++)
1010  pstate = pstate->parentParseState;
1011  markRTEForSelectPriv(pstate, rte, var->varno, var->varattno);
1012 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
Index varlevelsup
Definition: primnodes.h:173
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
Index varno
Definition: primnodes.h:166
struct ParseState * parentParseState
Definition: parse_node.h:170
unsigned int Index
Definition: c.h:359
#define Assert(condition)
Definition: c.h:681
Relation parserOpenTable ( ParseState pstate,
const RangeVar relation,
int  lockmode 
)

Definition at line 1146 of file parse_relation.c.

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

Referenced by addRangeTableEntry(), and setTargetTable().

1147 {
1148  Relation rel;
1149  ParseCallbackState pcbstate;
1150 
1151  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1152  rel = heap_openrv_extended(relation, lockmode, true);
1153  if (rel == NULL)
1154  {
1155  if (relation->schemaname)
1156  ereport(ERROR,
1158  errmsg("relation \"%s.%s\" does not exist",
1159  relation->schemaname, relation->relname)));
1160  else
1161  {
1162  /*
1163  * An unqualified name might have been meant as a reference to
1164  * some not-yet-in-scope CTE. The bare "does not exist" message
1165  * has proven remarkably unhelpful for figuring out such problems,
1166  * so we take pains to offer a specific hint.
1167  */
1168  if (isFutureCTE(pstate, relation->relname))
1169  ereport(ERROR,
1171  errmsg("relation \"%s\" does not exist",
1172  relation->relname),
1173  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1174  relation->relname),
1175  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1176  else
1177  ereport(ERROR,
1179  errmsg("relation \"%s\" does not exist",
1180  relation->relname)));
1181  }
1182  }
1184  return rel;
1185 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
static bool isFutureCTE(ParseState *pstate, const char *refname)
int errcode(int sqlerrcode)
Definition: elog.c:575
char * schemaname
Definition: primnodes.h:67
int location
Definition: primnodes.h:73
char * relname
Definition: primnodes.h:68
Relation heap_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
Definition: heapam.c:1347
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
int errmsg(const char *fmt,...)
Definition: elog.c:797
RangeTblEntry* refnameRangeTblEntry ( ParseState pstate,
const char *  schemaname,
const char *  refname,
int  location,
int *  sublevels_up 
)

Definition at line 83 of file parse_relation.c.

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

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

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

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

464 {
465  int index;
466  ListCell *l;
467 
468  if (sublevels_up)
469  *sublevels_up = 0;
470 
471  while (pstate != NULL)
472  {
473  index = 1;
474  foreach(l, pstate->p_rtable)
475  {
476  if (rte == (RangeTblEntry *) lfirst(l))
477  return index;
478  index++;
479  }
480  pstate = pstate->parentParseState;
481  if (sublevels_up)
482  (*sublevels_up)++;
483  else
484  break;
485  }
486 
487  elog(ERROR, "RTE not found (internal error)");
488  return 0; /* keep compiler quiet */
489 }
Definition: type.h:89
#define ERROR
Definition: elog.h:43
struct ParseState * parentParseState
Definition: parse_node.h:170
#define lfirst(lc)
Definition: pg_list.h:106
#define elog
Definition: elog.h:219
List * p_rtable
Definition: parse_node.h:172
CommonTableExpr* scanNameSpaceForCTE ( ParseState pstate,
const char *  refname,
Index ctelevelsup 
)

Definition at line 237 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes(), and searchRangeTableForRel().

239 {
240  Index levelsup;
241 
242  for (levelsup = 0;
243  pstate != NULL;
244  pstate = pstate->parentParseState, levelsup++)
245  {
246  ListCell *lc;
247 
248  foreach(lc, pstate->p_ctenamespace)
249  {
250  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
251 
252  if (strcmp(cte->ctename, refname) == 0)
253  {
254  *ctelevelsup = levelsup;
255  return cte;
256  }
257  }
258  }
259  return NULL;
260 }
struct ParseState * parentParseState
Definition: parse_node.h:170
unsigned int Index
Definition: c.h:359
#define lfirst(lc)
Definition: pg_list.h:106
List * p_ctenamespace
Definition: parse_node.h:179
bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 290 of file parse_relation.c.

References name_matches_visible_ENR().

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

291 {
292  return name_matches_visible_ENR(pstate, refname);
293 }
bool name_matches_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:20
Node* scanRTEForColumn ( ParseState pstate,
RangeTblEntry rte,
char *  colname,
int  location,
int  fuzzy_rte_penalty,
FuzzyAttrMatchState fuzzystate 
)

Definition at line 655 of file parse_relation.c.

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

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

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