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 1199 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().

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

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

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

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

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

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

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

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

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

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

2127 {
2128  if (addToJoinList)
2129  {
2130  int rtindex = RTERangeTablePosn(pstate, rte, NULL);
2132 
2133  rtr->rtindex = rtindex;
2134  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2135  }
2136  if (addToRelNameSpace || addToVarNameSpace)
2137  {
2138  ParseNamespaceItem *nsitem;
2139 
2140  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2141  nsitem->p_rte = rte;
2142  nsitem->p_rel_visible = addToRelNameSpace;
2143  nsitem->p_cols_visible = addToVarNameSpace;
2144  nsitem->p_lateral_only = false;
2145  nsitem->p_lateral_ok = true;
2146  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2147  }
2148 }
RangeTblEntry * p_rte
Definition: parse_node.h:241
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 3038 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().

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

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

Referenced by transformFkeyGetPrimaryKey().

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

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

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

3116 {
3117  if (attid <= 0)
3118  {
3119  Form_pg_attribute sysatt;
3120 
3121  sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
3122  return sysatt->atttypid;
3123  }
3124  if (attid > rd->rd_att->natts)
3125  elog(ERROR, "invalid attribute number %d", attid);
3126  return rd->rd_att->attrs[attid - 1]->atttypid;
3127 }
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:1014
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:241
#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:928
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:1015
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:1618
RangeTblEntry * p_rte
Definition: parse_node.h:241
#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 3210 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().

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

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

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

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

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

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

2077 {
2078  ListCell *l;
2079 
2080  /*
2081  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2082  * parent level, then act as though there's a generic FOR UPDATE here.
2083  */
2084  if (pstate->p_locked_from_parent)
2085  return true;
2086 
2087  foreach(l, pstate->p_locking_clause)
2088  {
2089  LockingClause *lc = (LockingClause *) lfirst(l);
2090 
2091  if (lc->lockedRels == NIL)
2092  {
2093  /* all tables used in query */
2094  return true;
2095  }
2096  else
2097  {
2098  /* just the named tables */
2099  ListCell *l2;
2100 
2101  foreach(l2, lc->lockedRels)
2102  {
2103  RangeVar *thisrel = (RangeVar *) lfirst(l2);
2104 
2105  if (strcmp(refname, thisrel->relname) == 0)
2106  return true;
2107  }
2108  }
2109  }
2110  return false;
2111 }
List * lockedRels
Definition: parsenodes.h:737
#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 3282 of file parse_relation.c.

References isQueryUsingTempRelation_walker(), and NULL.

Referenced by DefineView(), and transformCreateTableAsStmt().

3283 {
3284  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3285 }
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  * An unqualified name might have been meant as a reference to
1169  * some not-yet-in-scope CTE. The bare "does not exist" message
1170  * has proven remarkably unhelpful for figuring out such problems,
1171  * so we take pains to offer a specific hint.
1172  */
1173  else if (isFutureCTE(pstate, relation->relname))
1174  ereport(ERROR,
1176  errmsg("relation \"%s\" does not exist",
1177  relation->relname),
1178  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1179  relation->relname),
1180  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1181  else
1182  ereport(ERROR,
1184  errmsg("relation \"%s\" does not exist",
1185  relation->relname)));
1186  }
1187  }
1189  return rel;
1190 }
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:1618
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:2713
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:1618
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:172
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:928
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:1015