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 1200 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, NULL, 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().

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

Definition at line 1874 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, NULL, ParseState::p_rtable, parser_errposition(), RangeTblEntry::requiredPerms, Query::returningList, RTE_CTE, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::self_reference, and RangeTblEntry::updatedCols.

Referenced by transformCTEReference().

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

Definition at line 1983 of file parse_relation.c.

References RangeVar::alias, Alias::aliasname, Assert, tupleDesc::attrs, 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, NULL, ParseState::p_rtable, RangeTblEntry::relid, EphemeralNamedRelationMetadataData::reliddesc, RangeVar::relname, RangeTblEntry::requiredPerms, RTE_NAMEDTUPLESTORE, RangeTblEntry::rtekind, and RangeTblEntry::selectedCols.

Referenced by transformENRReference().

1986 {
1988  Alias *alias = rv->alias;
1989  char *refname = alias ? alias->aliasname : rv->relname;
1991  TupleDesc tupdesc;
1992  int attno;
1993 
1994  Assert(pstate != NULL);
1995  enrmd = get_visible_ENR(pstate, rv->relname);
1996  Assert(enrmd != NULL);
1997 
1998  switch (enrmd->enrtype)
1999  {
2000  case ENR_NAMED_TUPLESTORE:
2002  break;
2003 
2004  default:
2005  elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2006  return NULL; /* for fussy compilers */
2007  }
2008 
2009  /*
2010  * Record dependency on a relation. This allows plans to be invalidated
2011  * if they access transition tables linked to a table that is altered.
2012  */
2013  rte->relid = enrmd->reliddesc;
2014 
2015  /*
2016  * Build the list of effective column names using user-supplied aliases
2017  * and/or actual column names. Also build the cannibalized fields.
2018  */
2019  tupdesc = ENRMetadataGetTupDesc(enrmd);
2020  rte->eref = makeAlias(refname, NIL);
2021  buildRelationAliases(tupdesc, alias, rte->eref);
2022  rte->enrname = enrmd->name;
2023  rte->enrtuples = enrmd->enrtuples;
2024  rte->coltypes = NIL;
2025  rte->coltypmods = NIL;
2026  rte->colcollations = NIL;
2027  for (attno = 1; attno <= tupdesc->natts; ++attno)
2028  {
2029  if (tupdesc->attrs[attno - 1]->atttypid == InvalidOid &&
2030  !(tupdesc->attrs[attno - 1]->attisdropped))
2031  elog(ERROR, "atttypid was invalid for column which has not been dropped from \"%s\"",
2032  rv->relname);
2033  rte->coltypes =
2034  lappend_oid(rte->coltypes,
2035  tupdesc->attrs[attno - 1]->atttypid);
2036  rte->coltypmods =
2037  lappend_int(rte->coltypmods,
2038  tupdesc->attrs[attno - 1]->atttypmod);
2039  rte->colcollations =
2041  tupdesc->attrs[attno - 1]->attcollation);
2042  }
2043 
2044  /*
2045  * Set flags and access permissions.
2046  *
2047  * ENRs are never checked for access rights.
2048  */
2049  rte->lateral = false;
2050  rte->inh = false; /* never true for ENRs */
2051  rte->inFromCl = inFromCl;
2052 
2053  rte->requiredPerms = 0;
2054  rte->checkAsUser = InvalidOid;
2055  rte->selectedCols = NULL;
2056 
2057  /*
2058  * Add completed RTE to pstate's range table list, but not to join list
2059  * nor namespace --- caller must do that if appropriate.
2060  */
2061  pstate->p_rtable = lappend(pstate->p_rtable, rte);
2062 
2063  return rte;
2064 }
#define NIL
Definition: pg_list.h:69
List * coltypmods
Definition: parsenodes.h:1006
Form_pg_attribute * attrs
Definition: tupdesc.h:74
AclMode requiredPerms
Definition: parsenodes.h:1020
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
List * colcollations
Definition: parsenodes.h:1007
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:1022
#define ERROR
Definition: elog.h:43
char * enrname
Definition: parsenodes.h:1009
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:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
char * aliasname
Definition: primnodes.h:42
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
RTEKind rtekind
Definition: parsenodes.h:929
double enrtuples
Definition: parsenodes.h:1010
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:1016
List * coltypes
Definition: parsenodes.h:1005
List * p_rtable
Definition: parse_node.h:169
RangeTblEntry* addRangeTableEntryForFunction ( ParseState pstate,
List funcnames,
List funcexprs,
List coldeflists,
RangeFunction rangefunc,
bool  lateral,
bool  inFromCl 
)

Definition at line 1408 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, NULL, 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().

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

Definition at line 1804 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, NULL, ParseState::p_rtable, RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_JOIN, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::subquery, and RangeTblEntry::updatedCols.

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

1810 {
1812  Alias *eref;
1813  int numaliases;
1814 
1815  Assert(pstate != NULL);
1816 
1817  /*
1818  * Fail if join has too many columns --- we must be able to reference any
1819  * of the columns with an AttrNumber.
1820  */
1821  if (list_length(aliasvars) > MaxAttrNumber)
1822  ereport(ERROR,
1823  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1824  errmsg("joins can have at most %d columns",
1825  MaxAttrNumber)));
1826 
1827  rte->rtekind = RTE_JOIN;
1828  rte->relid = InvalidOid;
1829  rte->subquery = NULL;
1830  rte->jointype = jointype;
1831  rte->joinaliasvars = aliasvars;
1832  rte->alias = alias;
1833 
1834  eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
1835  numaliases = list_length(eref->colnames);
1836 
1837  /* fill in any unspecified alias columns */
1838  if (numaliases < list_length(colnames))
1839  eref->colnames = list_concat(eref->colnames,
1840  list_copy_tail(colnames, numaliases));
1841 
1842  rte->eref = eref;
1843 
1844  /*
1845  * Set flags and access permissions.
1846  *
1847  * Joins are never checked for access rights.
1848  */
1849  rte->lateral = false;
1850  rte->inh = false; /* never true for joins */
1851  rte->inFromCl = inFromCl;
1852 
1853  rte->requiredPerms = 0;
1854  rte->checkAsUser = InvalidOid;
1855  rte->selectedCols = NULL;
1856  rte->insertedCols = NULL;
1857  rte->updatedCols = NULL;
1858 
1859  /*
1860  * Add completed RTE to pstate's range table list, but not to join list
1861  * nor namespace --- caller must do that if appropriate.
1862  */
1863  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1864 
1865  return rte;
1866 }
#define NIL
Definition: pg_list.h:69
List * joinaliasvars
Definition: parsenodes.h:968
Alias * alias
Definition: parsenodes.h:1015
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:1020
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:1022
#define ERROR
Definition: elog.h:43
JoinType jointype
Definition: parsenodes.h:967
#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:1024
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:929
Query * subquery
Definition: parsenodes.h:947
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1023
Alias * eref
Definition: parsenodes.h:1016
#define copyObject(obj)
Definition: nodes.h:621
List * p_rtable
Definition: parse_node.h:169
RangeTblEntry* addRangeTableEntryForRelation ( ParseState pstate,
Relation  rel,
Alias alias,
bool  inh,
bool  inFromCl 
)

Definition at line 1273 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, NULL, 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().

1278 {
1280  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1281 
1282  Assert(pstate != NULL);
1283 
1284  rte->rtekind = RTE_RELATION;
1285  rte->alias = alias;
1286  rte->relid = RelationGetRelid(rel);
1287  rte->relkind = rel->rd_rel->relkind;
1288 
1289  /*
1290  * Build the list of effective column names using user-supplied aliases
1291  * and/or actual column names.
1292  */
1293  rte->eref = makeAlias(refname, NIL);
1294  buildRelationAliases(rel->rd_att, alias, rte->eref);
1295 
1296  /*
1297  * Set flags and access permissions.
1298  *
1299  * The initial default on access checks is always check-for-READ-access,
1300  * which is the right thing for all except target tables.
1301  */
1302  rte->lateral = false;
1303  rte->inh = inh;
1304  rte->inFromCl = inFromCl;
1305 
1306  rte->requiredPerms = ACL_SELECT;
1307  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1308  rte->selectedCols = NULL;
1309  rte->insertedCols = NULL;
1310  rte->updatedCols = NULL;
1311 
1312  /*
1313  * Add completed RTE to pstate's range table list, but not to join list
1314  * nor namespace --- caller must do that if appropriate.
1315  */
1316  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1317 
1318  return rte;
1319 }
#define NIL
Definition: pg_list.h:69
Alias * alias
Definition: parsenodes.h:1015
AclMode requiredPerms
Definition: parsenodes.h:1020
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:1022
#define RelationGetRelationName(relation)
Definition: rel.h:437
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:1024
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:929
Bitmapset * insertedCols
Definition: parsenodes.h:1023
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * eref
Definition: parsenodes.h:1016
#define RelationGetRelid(relation)
Definition: rel.h:417
List * p_rtable
Definition: parse_node.h:169
RangeTblEntry* addRangeTableEntryForSubquery ( ParseState pstate,
Query subquery,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1328 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(), NULL, 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().

1333 {
1335  char *refname = alias->aliasname;
1336  Alias *eref;
1337  int numaliases;
1338  int varattno;
1339  ListCell *tlistitem;
1340 
1341  Assert(pstate != NULL);
1342 
1343  rte->rtekind = RTE_SUBQUERY;
1344  rte->relid = InvalidOid;
1345  rte->subquery = subquery;
1346  rte->alias = alias;
1347 
1348  eref = copyObject(alias);
1349  numaliases = list_length(eref->colnames);
1350 
1351  /* fill in any unspecified alias columns */
1352  varattno = 0;
1353  foreach(tlistitem, subquery->targetList)
1354  {
1355  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1356 
1357  if (te->resjunk)
1358  continue;
1359  varattno++;
1360  Assert(varattno == te->resno);
1361  if (varattno > numaliases)
1362  {
1363  char *attrname;
1364 
1365  attrname = pstrdup(te->resname);
1366  eref->colnames = lappend(eref->colnames, makeString(attrname));
1367  }
1368  }
1369  if (varattno < numaliases)
1370  ereport(ERROR,
1371  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1372  errmsg("table \"%s\" has %d columns available but %d columns specified",
1373  refname, varattno, numaliases)));
1374 
1375  rte->eref = eref;
1376 
1377  /*
1378  * Set flags and access permissions.
1379  *
1380  * Subqueries are never checked for access rights.
1381  */
1382  rte->lateral = lateral;
1383  rte->inh = false; /* never true for subqueries */
1384  rte->inFromCl = inFromCl;
1385 
1386  rte->requiredPerms = 0;
1387  rte->checkAsUser = InvalidOid;
1388  rte->selectedCols = NULL;
1389  rte->insertedCols = NULL;
1390  rte->updatedCols = NULL;
1391 
1392  /*
1393  * Add completed RTE to pstate's range table list, but not to join list
1394  * nor namespace --- caller must do that if appropriate.
1395  */
1396  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1397 
1398  return rte;
1399 }
Value * makeString(char *str)
Definition: value.c:53
Alias * alias
Definition: parsenodes.h:1015
List * colnames
Definition: primnodes.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1077
int errcode(int sqlerrcode)
Definition: elog.c:575
AclMode requiredPerms
Definition: parsenodes.h:1020
char * resname
Definition: primnodes.h:1369
List * targetList
Definition: parsenodes.h:138
Bitmapset * selectedCols
Definition: parsenodes.h:1022
bool resjunk
Definition: primnodes.h:1374
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1368
#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:1024
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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:929
Query * subquery
Definition: parsenodes.h:947
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1023
Alias * eref
Definition: parsenodes.h:1016
#define copyObject(obj)
Definition: nodes.h:621
List * p_rtable
Definition: parse_node.h:169
RangeTblEntry* addRangeTableEntryForTableFunc ( ParseState pstate,
TableFunc tf,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1663 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, NULL, ParseState::p_rtable, pstrdup(), RangeTblEntry::relid, RangeTblEntry::requiredPerms, RTE_TABLEFUNC, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, RangeTblEntry::subquery, RangeTblEntry::tablefunc, and RangeTblEntry::updatedCols.

Referenced by transformRangeTableFunc().

1668 {
1670  char *refname = alias ? alias->aliasname : pstrdup("xmltable");
1671  Alias *eref;
1672  int numaliases;
1673 
1674  Assert(pstate != NULL);
1675 
1676  rte->rtekind = RTE_TABLEFUNC;
1677  rte->relid = InvalidOid;
1678  rte->subquery = NULL;
1679  rte->tablefunc = tf;
1680  rte->coltypes = tf->coltypes;
1681  rte->coltypmods = tf->coltypmods;
1682  rte->colcollations = tf->colcollations;
1683  rte->alias = alias;
1684 
1685  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1686  numaliases = list_length(eref->colnames);
1687 
1688  /* fill in any unspecified alias columns */
1689  if (numaliases < list_length(tf->colnames))
1690  eref->colnames = list_concat(eref->colnames,
1691  list_copy_tail(tf->colnames, numaliases));
1692 
1693  rte->eref = eref;
1694 
1695  /*
1696  * Set flags and access permissions.
1697  *
1698  * Tablefuncs are never checked for access rights (at least, not by the
1699  * RTE permissions mechanism).
1700  */
1701  rte->lateral = lateral;
1702  rte->inh = false; /* never true for tablefunc RTEs */
1703  rte->inFromCl = inFromCl;
1704 
1705  rte->requiredPerms = 0;
1706  rte->checkAsUser = InvalidOid;
1707  rte->selectedCols = NULL;
1708  rte->insertedCols = NULL;
1709  rte->updatedCols = NULL;
1710 
1711  /*
1712  * Add completed RTE to pstate's range table list, but not to join list
1713  * nor namespace --- caller must do that if appropriate.
1714  */
1715  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1716 
1717  return rte;
1718 }
#define NIL
Definition: pg_list.h:69
List * colnames
Definition: primnodes.h:86
Alias * alias
Definition: parsenodes.h:1015
List * colnames
Definition: primnodes.h:43
List * coltypmods
Definition: primnodes.h:88
char * pstrdup(const char *in)
Definition: mcxt.c:1077
List * coltypmods
Definition: parsenodes.h:1006
List * list_concat(List *list1, List *list2)
Definition: list.c:321
AclMode requiredPerms
Definition: parsenodes.h:1020
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
List * colcollations
Definition: parsenodes.h:1007
List * colcollations
Definition: primnodes.h:89
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:1022
TableFunc * tablefunc
Definition: parsenodes.h:984
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:1024
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:929
Query * subquery
Definition: parsenodes.h:947
Bitmapset * insertedCols
Definition: parsenodes.h:1023
Alias * eref
Definition: parsenodes.h:1016
#define copyObject(obj)
Definition: nodes.h:621
List * coltypes
Definition: parsenodes.h:1005
List * p_rtable
Definition: parse_node.h:169
RangeTblEntry* addRangeTableEntryForValues ( ParseState pstate,
List exprs,
List coltypes,
List coltypmods,
List colcollations,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1726 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, NULL, 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().

1734 {
1736  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
1737  Alias *eref;
1738  int numaliases;
1739  int numcolumns;
1740 
1741  Assert(pstate != NULL);
1742 
1743  rte->rtekind = RTE_VALUES;
1744  rte->relid = InvalidOid;
1745  rte->subquery = NULL;
1746  rte->values_lists = exprs;
1747  rte->coltypes = coltypes;
1748  rte->coltypmods = coltypmods;
1749  rte->colcollations = colcollations;
1750  rte->alias = alias;
1751 
1752  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1753 
1754  /* fill in any unspecified alias columns */
1755  numcolumns = list_length((List *) linitial(exprs));
1756  numaliases = list_length(eref->colnames);
1757  while (numaliases < numcolumns)
1758  {
1759  char attrname[64];
1760 
1761  numaliases++;
1762  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
1763  eref->colnames = lappend(eref->colnames,
1764  makeString(pstrdup(attrname)));
1765  }
1766  if (numcolumns < numaliases)
1767  ereport(ERROR,
1768  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1769  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
1770  refname, numcolumns, numaliases)));
1771 
1772  rte->eref = eref;
1773 
1774  /*
1775  * Set flags and access permissions.
1776  *
1777  * Subqueries are never checked for access rights.
1778  */
1779  rte->lateral = lateral;
1780  rte->inh = false; /* never true for values RTEs */
1781  rte->inFromCl = inFromCl;
1782 
1783  rte->requiredPerms = 0;
1784  rte->checkAsUser = InvalidOid;
1785  rte->selectedCols = NULL;
1786  rte->insertedCols = NULL;
1787  rte->updatedCols = NULL;
1788 
1789  /*
1790  * Add completed RTE to pstate's range table list, but not to join list
1791  * nor namespace --- caller must do that if appropriate.
1792  */
1793  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1794 
1795  return rte;
1796 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
Alias * alias
Definition: parsenodes.h:1015
List * colnames
Definition: primnodes.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1077
List * coltypmods
Definition: parsenodes.h:1006
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:1020
List * colcollations
Definition: parsenodes.h:1007
List * values_lists
Definition: parsenodes.h:989
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:1022
#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:1024
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:929
Query * subquery
Definition: parsenodes.h:947
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:1023
Alias * eref
Definition: parsenodes.h:1016
#define copyObject(obj)
Definition: nodes.h:621
List * coltypes
Definition: parsenodes.h:1005
Definition: pg_list.h:45
List * p_rtable
Definition: parse_node.h:169
void addRTEtoQuery ( ParseState pstate,
RangeTblEntry rte,
bool  addToJoinList,
bool  addToRelNameSpace,
bool  addToVarNameSpace 
)

Definition at line 2125 of file parse_relation.c.

References lappend(), makeNode, NULL, 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().

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

Definition at line 3039 of file parse_relation.c.

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

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

3040 {
3041  int i;
3042 
3043  for (i = 0; i < rd->rd_rel->relnatts; i++)
3044  {
3045  Form_pg_attribute att = rd->rd_att->attrs[i];
3046 
3047  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3048  return i + 1;
3049  }
3050 
3051  if (sysColOK)
3052  {
3053  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
3054  {
3055  if (i != ObjectIdAttributeNumber || rd->rd_rel->relhasoids)
3056  return i;
3057  }
3058  }
3059 
3060  /* on failure */
3061  return InvalidAttrNumber;
3062 }
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int namestrcmp(Name name, const char *str)
Definition: name.c:248
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 3094 of file parse_relation.c.

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

Referenced by transformFkeyGetPrimaryKey().

3095 {
3096  if (attid <= 0)
3097  {
3098  Form_pg_attribute sysatt;
3099 
3100  sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
3101  return &sysatt->attname;
3102  }
3103  if (attid > rd->rd_att->natts)
3104  elog(ERROR, "invalid attribute number %d", attid);
3105  return &rd->rd_att->attrs[attid - 1]->attname;
3106 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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 3136 of file parse_relation.c.

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

3137 {
3138  if (attid <= 0)
3139  {
3140  /* All system attributes are of noncollatable types. */
3141  return InvalidOid;
3142  }
3143  if (attid > rd->rd_att->natts)
3144  elog(ERROR, "invalid attribute number %d", attid);
3145  return rd->rd_att->attrs[attid - 1]->attcollation;
3146 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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 3116 of file parse_relation.c.

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

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

3117 {
3118  if (attid <= 0)
3119  {
3120  Form_pg_attribute sysatt;
3121 
3122  sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
3123  return sysatt->atttypid;
3124  }
3125  if (attid > rd->rd_att->natts)
3126  elog(ERROR, "invalid attribute number %d", attid);
3127  return rd->rd_att->attrs[attid - 1]->atttypid;
3128 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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, NULL, 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:1015
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:242
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
RTEKind rtekind
Definition: parsenodes.h:929
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:1016
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, NULL, ParseNamespaceItem::p_cols_visible, ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rte, ParseState::parentParseState, parser_errposition(), result, 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:509
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
RangeTblEntry * p_rte
Definition: parse_node.h:242
#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:173
#define ereport(elevel, rest)
Definition: elog.h:122
struct ParseState * parentParseState
Definition: parse_node.h:167
bool p_lateral_active
Definition: parse_node.h:175
#define NULL
Definition: c.h:229
#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 3211 of file parse_relation.c.

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

Referenced by transformColumnRef().

3213 {
3215  char *closestfirst = NULL;
3216 
3217  /*
3218  * Search the entire rtable looking for possible matches. If we find one,
3219  * emit a hint about it.
3220  *
3221  * TODO: improve this code (and also errorMissingRTE) to mention using
3222  * LATERAL if appropriate.
3223  */
3224  state = searchRangeTableForCol(pstate, relname, colname, location);
3225 
3226  /*
3227  * Extract closest col string for best match, if any.
3228  *
3229  * Infer an exact match referenced despite not being visible from the fact
3230  * that an attribute number was not present in state passed back -- this
3231  * is what is reported when !closestfirst. There might also be an exact
3232  * match that was qualified with an incorrect alias, in which case
3233  * closestfirst will be set (so hint is the same as generic fuzzy case).
3234  */
3235  if (state->rfirst && AttributeNumberIsValid(state->first))
3236  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3237  state->first - 1));
3238 
3239  if (!state->rsecond)
3240  {
3241  /*
3242  * Handle case where there is zero or one column suggestions to hint,
3243  * including exact matches referenced but not visible.
3244  */
3245  ereport(ERROR,
3246  (errcode(ERRCODE_UNDEFINED_COLUMN),
3247  relname ?
3248  errmsg("column %s.%s does not exist", relname, colname) :
3249  errmsg("column \"%s\" does not exist", colname),
3250  state->rfirst ? closestfirst ?
3251  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3252  state->rfirst->eref->aliasname, closestfirst) :
3253  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3254  colname, state->rfirst->eref->aliasname) : 0,
3255  parser_errposition(pstate, location)));
3256  }
3257  else
3258  {
3259  /* Handle case where there are two equally useful column hints */
3260  char *closestsecond;
3261 
3262  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3263  state->second - 1));
3264 
3265  ereport(ERROR,
3266  (errcode(ERRCODE_UNDEFINED_COLUMN),
3267  relname ?
3268  errmsg("column %s.%s does not exist", relname, colname) :
3269  errmsg("column \"%s\" does not exist", colname),
3270  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3271  state->rfirst->eref->aliasname, closestfirst,
3272  state->rsecond->eref->aliasname, closestsecond),
3273  parser_errposition(pstate, location)));
3274  }
3275 }
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
#define NULL
Definition: c.h:229
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:1016
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, char *colname, int location)
void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3155 of file parse_relation.c.

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

Referenced by ExpandColumnRefStar(), and transformColumnRef().

3156 {
3157  RangeTblEntry *rte;
3158  int sublevels_up;
3159  const char *badAlias = NULL;
3160 
3161  /*
3162  * Check to see if there are any potential matches in the query's
3163  * rangetable. (Note: cases involving a bad schema name in the RangeVar
3164  * will throw error immediately here. That seems OK.)
3165  */
3166  rte = searchRangeTableForRel(pstate, relation);
3167 
3168  /*
3169  * If we found a match that has an alias and the alias is visible in the
3170  * namespace, then the problem is probably use of the relation's real name
3171  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3172  * common enough to justify a specific hint.
3173  *
3174  * If we found a match that doesn't meet those criteria, assume the
3175  * problem is illegal use of a relation outside its scope, as in the
3176  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3177  */
3178  if (rte && rte->alias &&
3179  strcmp(rte->eref->aliasname, relation->relname) != 0 &&
3180  refnameRangeTblEntry(pstate, NULL, rte->eref->aliasname,
3181  relation->location,
3182  &sublevels_up) == rte)
3183  badAlias = rte->eref->aliasname;
3184 
3185  if (rte)
3186  ereport(ERROR,
3188  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3189  relation->relname),
3190  (badAlias ?
3191  errhint("Perhaps you meant to reference the table alias \"%s\".",
3192  badAlias) :
3193  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3194  rte->eref->aliasname)),
3195  parser_errposition(pstate, relation->location)));
3196  else
3197  ereport(ERROR,
3199  errmsg("missing FROM-clause entry for table \"%s\"",
3200  relation->relname),
3201  parser_errposition(pstate, relation->location)));
3202 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
Alias * alias
Definition: parsenodes.h:1015
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
#define NULL
Definition: c.h:229
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:1016
List* expandRelAttrs ( ParseState pstate,
RangeTblEntry rte,
int  rtindex,
int  sublevels_up,
int  location 
)

Definition at line 2583 of file parse_relation.c.

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

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

2585 {
2586  List *names,
2587  *vars;
2588  ListCell *name,
2589  *var;
2590  List *te_list = NIL;
2591 
2592  expandRTE(rte, rtindex, sublevels_up, location, false,
2593  &names, &vars);
2594 
2595  /*
2596  * Require read access to the table. This is normally redundant with the
2597  * markVarForSelectPriv calls below, but not if the table has zero
2598  * columns.
2599  */
2600  rte->requiredPerms |= ACL_SELECT;
2601 
2602  forboth(name, names, var, vars)
2603  {
2604  char *label = strVal(lfirst(name));
2605  Var *varnode = (Var *) lfirst(var);
2606  TargetEntry *te;
2607 
2608  te = makeTargetEntry((Expr *) varnode,
2609  (AttrNumber) pstate->p_next_resno++,
2610  label,
2611  false);
2612  te_list = lappend(te_list, te);
2613 
2614  /* Require read access to each column */
2615  markVarForSelectPriv(pstate, varnode, rte);
2616  }
2617 
2618  Assert(name == NULL && var == NULL); /* lists not the same length? */
2619 
2620  return te_list;
2621 }
#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:1020
Definition: primnodes.h:163
int p_next_resno
Definition: parse_node.h:184
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:81
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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 2169 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, NULL, 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().

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

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

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

Definition at line 3283 of file parse_relation.c.

References isQueryUsingTempRelation_walker(), and NULL.

Referenced by DefineView(), and transformCreateTableAsStmt().

3284 {
3285  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3286 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
Definition: nodes.h:509
#define NULL
Definition: c.h:229
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:560
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:167
unsigned int Index
Definition: c.h:365
#define Assert(condition)
Definition: c.h:675
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, get_visible_ENR_metadata(), heap_openrv_extended(), isFutureCTE(), RangeVar::location, NULL, ParseState::p_queryEnv, 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 be a named ephemeral relation.
1164  */
1165  if (get_visible_ENR_metadata(pstate->p_queryEnv, relation->relname))
1166  rel = NULL;
1167 
1168  /*
1169  * An unqualified name might have been meant as a reference to
1170  * some not-yet-in-scope CTE. The bare "does not exist" message
1171  * has proven remarkably unhelpful for figuring out such problems,
1172  * so we take pains to offer a specific hint.
1173  */
1174  else if (isFutureCTE(pstate, relation->relname))
1175  ereport(ERROR,
1177  errmsg("relation \"%s\" does not exist",
1178  relation->relname),
1179  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1180  relation->relname),
1181  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1182  else
1183  ereport(ERROR,
1185  errmsg("relation \"%s\" does not exist",
1186  relation->relname)));
1187  }
1188  }
1190  return rel;
1191 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
QueryEnvironment * p_queryEnv
Definition: parse_node.h:192
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:1341
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
#define NULL
Definition: c.h:229
EphemeralNamedRelationMetadata get_visible_ENR_metadata(QueryEnvironment *queryEnv, const char *refname)
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(), NULL, OidIsValid, ParseState::parentParseState, result, 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  {
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)
return result
Definition: formatting.c:1632
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1683
struct ParseState * parentParseState
Definition: parse_node.h:167
static RangeTblEntry * scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
Oid LookupNamespaceNoError(const char *nspname)
Definition: namespace.c:2780
int RTERangeTablePosn ( ParseState pstate,
RangeTblEntry rte,
int *  sublevels_up 
)

Definition at line 463 of file parse_relation.c.

References elog, ERROR, lfirst, NULL, 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:90
#define ERROR
Definition: elog.h:43
struct ParseState * parentParseState
Definition: parse_node.h:167
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
#define elog
Definition: elog.h:219
List * p_rtable
Definition: parse_node.h:169
CommonTableExpr* scanNameSpaceForCTE ( ParseState pstate,
const char *  refname,
Index ctelevelsup 
)

Definition at line 237 of file parse_relation.c.

References CommonTableExpr::ctename, lfirst, NULL, 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:167
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * p_ctenamespace
Definition: parse_node.h:176
bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 290 of file parse_relation.c.

References name_matches_visible_ENR().

Referenced by getRTEForSpecialRelationTypes(), and searchRangeTableForRel().

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(), NULL, ObjectIdGetDatum, ParseState::p_expr_kind, parser_errposition(), RangeTblEntry::relid, RangeTblEntry::relkind, RELKIND_COMPOSITE_TYPE, result, 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:509
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
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:183
Var * make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
Definition: parse_node.c:189
#define NULL
Definition: c.h:229
#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:176
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:929
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:1016