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

Go to the source code of this file.

Data Structures

struct  FuzzyAttrMatchState
 

Functions

RangeTblEntryrefnameRangeTblEntry (ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
 
CommonTableExprscanNameSpaceForCTE (ParseState *pstate, const char *refname, Index *ctelevelsup)
 
bool scanNameSpaceForENR (ParseState *pstate, const char *refname)
 
void checkNameSpaceConflicts (ParseState *pstate, List *namespace1, List *namespace2)
 
int RTERangeTablePosn (ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
 
RangeTblEntryGetRTEByRangeTablePosn (ParseState *pstate, int varno, int sublevels_up)
 
CommonTableExprGetCTEForRTE (ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
 
NodescanRTEForColumn (ParseState *pstate, RangeTblEntry *rte, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
 
NodecolNameToVar (ParseState *pstate, const char *colname, bool localonly, int location)
 
void markVarForSelectPriv (ParseState *pstate, Var *var, RangeTblEntry *rte)
 
Relation parserOpenTable (ParseState *pstate, const RangeVar *relation, int lockmode)
 
RangeTblEntryaddRangeTableEntry (ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForRelation (ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForSubquery (ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForFunction (ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForValues (ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForTableFunc (ParseState *pstate, TableFunc *tf, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForJoin (ParseState *pstate, List *colnames, JoinType jointype, List *aliasvars, Alias *alias, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForCTE (ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForENR (ParseState *pstate, RangeVar *rv, bool inFromCl)
 
bool isLockedRefname (ParseState *pstate, const char *refname)
 
void addRTEtoQuery (ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
 
void errorMissingRTE (ParseState *pstate, RangeVar *relation) pg_attribute_noreturn()
 
void errorMissingColumn (ParseState *pstate, const char *relname, const char *colname, int location) pg_attribute_noreturn()
 
void expandRTE (RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
 
ListexpandRelAttrs (ParseState *pstate, RangeTblEntry *rte, int rtindex, int sublevels_up, int location)
 
int attnameAttNum (Relation rd, const char *attname, bool sysColOK)
 
const NameDataattnumAttName (Relation rd, int attid)
 
Oid attnumTypeId (Relation rd, int attid)
 
Oid attnumCollationId (Relation rd, int attid)
 
bool isQueryUsingTempRelation (Query *query)
 

Function Documentation

◆ addRangeTableEntry()

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

Definition at line 1211 of file parse_relation.c.

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

Referenced by transformTableEntry().

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

◆ addRangeTableEntryForCTE()

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

Definition at line 1921 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes().

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

◆ addRangeTableEntryForENR()

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

Definition at line 2031 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes().

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

◆ addRangeTableEntryForFunction()

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

Definition at line 1444 of file parse_relation.c.

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

Referenced by transformRangeFunction().

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

◆ addRangeTableEntryForJoin()

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

Definition at line 1850 of file parse_relation.c.

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

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

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

◆ addRangeTableEntryForRelation()

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

Definition at line 1301 of file parse_relation.c.

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

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

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

◆ addRangeTableEntryForSubquery()

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

Definition at line 1364 of file parse_relation.c.

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

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

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

◆ addRangeTableEntryForTableFunc()

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

Definition at line 1707 of file parse_relation.c.

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

Referenced by transformRangeTableFunc().

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

◆ addRangeTableEntryForValues()

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

Definition at line 1771 of file parse_relation.c.

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

Referenced by transformInsertStmt(), and transformValuesClause().

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

◆ addRTEtoQuery()

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

Definition at line 2182 of file parse_relation.c.

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

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

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

◆ attnameAttNum()

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

Definition at line 3143 of file parse_relation.c.

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

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

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

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3193 of file parse_relation.c.

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

Referenced by transformFkeyGetPrimaryKey().

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

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3235 of file parse_relation.c.

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

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

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3215 of file parse_relation.c.

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

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

◆ checkNameSpaceConflicts()

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

Definition at line 392 of file parse_relation.c.

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

Referenced by transformFromClause(), and transformFromClauseItem().

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

◆ colNameToVar()

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

Definition at line 771 of file parse_relation.c.

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

Referenced by findTargetlistEntrySQL92(), and transformColumnRef().

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

◆ errorMissingColumn()

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

Definition at line 3310 of file parse_relation.c.

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

Referenced by transformColumnRef().

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

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3254 of file parse_relation.c.

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

Referenced by ExpandColumnRefStar(), and transformColumnRef().

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

◆ expandRelAttrs()

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

Definition at line 2667 of file parse_relation.c.

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

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

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

◆ expandRTE()

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

Definition at line 2226 of file parse_relation.c.

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

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

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

◆ GetCTEForRTE()

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

Definition at line 520 of file parse_relation.c.

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

Referenced by expandRecordVariable(), and markTargetListOrigin().

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

◆ GetRTEByRangeTablePosn()

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

Definition at line 499 of file parse_relation.c.

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

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

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

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2134 of file parse_relation.c.

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

Referenced by addRangeTableEntry(), and transformRangeSubselect().

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

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3382 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

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

◆ markVarForSelectPriv()

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

Definition at line 1017 of file parse_relation.c.

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

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

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

◆ parserOpenTable()

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

Definition at line 1163 of file parse_relation.c.

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

Referenced by addRangeTableEntry(), and setTargetTable().

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

◆ refnameRangeTblEntry()

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

Definition at line 86 of file parse_relation.c.

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

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

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

◆ RTERangeTablePosn()

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

Definition at line 466 of file parse_relation.c.

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

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

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

◆ scanNameSpaceForCTE()

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

Definition at line 240 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes(), and searchRangeTableForRel().

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

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 293 of file parse_relation.c.

References name_matches_visible_ENR().

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

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

◆ scanRTEForColumn()

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

Definition at line 658 of file parse_relation.c.

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

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

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