PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parse_relation.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "catalog/heap.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parsetree.h"
#include "parser/parse_relation.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/varlena.h"
Include dependency graph for parse_relation.c:

Go to the source code of this file.

Macros

#define MAX_FUZZY_DISTANCE   3
 

Functions

static RangeTblEntryscanNameSpaceForRefname (ParseState *pstate, const char *refname, int location)
 
static RangeTblEntryscanNameSpaceForRelid (ParseState *pstate, Oid relid, int location)
 
static void check_lateral_ref_ok (ParseState *pstate, ParseNamespaceItem *nsitem, int location)
 
static void markRTEForSelectPriv (ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
 
static void expandRelation (Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
 
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)
 
static int specialAttNum (const char *attname)
 
static bool isQueryUsingTempRelation_walker (Node *node, void *context)
 
RangeTblEntryrefnameRangeTblEntry (ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
 
CommonTableExprscanNameSpaceForCTE (ParseState *pstate, const char *refname, Index *ctelevelsup)
 
static bool isFutureCTE (ParseState *pstate, const char *refname)
 
static RangeTblEntrysearchRangeTableForRel (ParseState *pstate, RangeVar *relation)
 
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)
 
static void updateFuzzyAttrMatchState (int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate, RangeTblEntry *rte, const char *actual, const char *match, int attnum)
 
NodescanRTEForColumn (ParseState *pstate, RangeTblEntry *rte, char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
 
NodecolNameToVar (ParseState *pstate, char *colname, bool localonly, int location)
 
static FuzzyAttrMatchStatesearchRangeTableForCol (ParseState *pstate, const char *alias, char *colname, int location)
 
void markVarForSelectPriv (ParseState *pstate, Var *var, RangeTblEntry *rte)
 
static void buildRelationAliases (TupleDesc tupdesc, Alias *alias, Alias *eref)
 
static char * chooseScalarFunctionAlias (Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
 
Relation parserOpenTable (ParseState *pstate, const RangeVar *relation, int lockmode)
 
RangeTblEntryaddRangeTableEntry (ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForRelation (ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForSubquery (ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForFunction (ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForValues (ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForJoin (ParseState *pstate, List *colnames, JoinType jointype, List *aliasvars, Alias *alias, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForCTE (ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
 
bool isLockedRefname (ParseState *pstate, const char *refname)
 
void addRTEtoQuery (ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
 
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)
 
char * get_rte_attribute_name (RangeTblEntry *rte, AttrNumber attnum)
 
void get_rte_attribute_type (RangeTblEntry *rte, AttrNumber attnum, Oid *vartype, int32 *vartypmod, Oid *varcollid)
 
bool get_rte_attribute_is_dropped (RangeTblEntry *rte, AttrNumber attnum)
 
TargetEntryget_tle_by_resno (List *tlist, AttrNumber resno)
 
RowMarkClauseget_parse_rowmark (Query *qry, Index rtindex)
 
int attnameAttNum (Relation rd, const char *attname, bool sysColOK)
 
Name attnumAttName (Relation rd, int attid)
 
Oid attnumTypeId (Relation rd, int attid)
 
Oid attnumCollationId (Relation rd, int attid)
 
void errorMissingRTE (ParseState *pstate, RangeVar *relation)
 
void errorMissingColumn (ParseState *pstate, char *relname, char *colname, int location)
 
bool isQueryUsingTempRelation (Query *query)
 

Macro Definition Documentation

#define MAX_FUZZY_DISTANCE   3

Definition at line 37 of file parse_relation.c.

Referenced by searchRangeTableForCol(), and updateFuzzyAttrMatchState().

Function Documentation

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

Definition at line 1173 of file parse_relation.c.

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

Referenced by transformTableEntry().

1178 {
1180  char *refname = alias ? alias->aliasname : relation->relname;
1181  LOCKMODE lockmode;
1182  Relation rel;
1183 
1184  Assert(pstate != NULL);
1185 
1186  rte->rtekind = RTE_RELATION;
1187  rte->alias = alias;
1188 
1189  /*
1190  * Get the rel's OID. This access also ensures that we have an up-to-date
1191  * relcache entry for the rel. Since this is typically the first access
1192  * to a rel in a statement, be careful to get the right access level
1193  * depending on whether we're doing SELECT FOR UPDATE/SHARE.
1194  */
1195  lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock;
1196  rel = parserOpenTable(pstate, relation, lockmode);
1197  rte->relid = RelationGetRelid(rel);
1198  rte->relkind = rel->rd_rel->relkind;
1199 
1200  /*
1201  * Build the list of effective column names using user-supplied aliases
1202  * and/or actual column names.
1203  */
1204  rte->eref = makeAlias(refname, NIL);
1205  buildRelationAliases(rel->rd_att, alias, rte->eref);
1206 
1207  /*
1208  * Drop the rel refcount, but keep the access lock till end of transaction
1209  * so that the table can't be deleted or have its schema modified
1210  * underneath us.
1211  */
1212  heap_close(rel, NoLock);
1213 
1214  /*
1215  * Set flags and access permissions.
1216  *
1217  * The initial default on access checks is always check-for-READ-access,
1218  * which is the right thing for all except target tables.
1219  */
1220  rte->lateral = false;
1221  rte->inh = inh;
1222  rte->inFromCl = inFromCl;
1223 
1224  rte->requiredPerms = ACL_SELECT;
1225  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1226  rte->selectedCols = NULL;
1227  rte->insertedCols = NULL;
1228  rte->updatedCols = NULL;
1229 
1230  /*
1231  * Add completed RTE to pstate's range table list, but not to join list
1232  * nor namespace --- caller must do that if appropriate.
1233  */
1234  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1235 
1236  return rte;
1237 }
#define NIL
Definition: pg_list.h:69
Alias * alias
Definition: parsenodes.h:960
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:965
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:113
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
char * relname
Definition: primnodes.h:67
Bitmapset * selectedCols
Definition: parsenodes.h:967
#define NoLock
Definition: lockdefs.h:34
List * lappend(List *list, void *datum)
Definition: list.c:128
#define RowShareLock
Definition: lockdefs.h:37
#define ACL_SELECT
Definition: parsenodes.h:66
TupleDesc rd_att
Definition: rel.h:114
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:969
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
char * aliasname
Definition: primnodes.h:41
RTEKind rtekind
Definition: parsenodes.h:882
Bitmapset * insertedCols
Definition: parsenodes.h:968
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
bool isLockedRefname(ParseState *pstate, const char *refname)
Alias * eref
Definition: parsenodes.h:961
#define RelationGetRelid(relation)
Definition: rel.h:413
List * p_rtable
Definition: parse_node.h:168
RangeTblEntry* addRangeTableEntryForCTE ( ParseState pstate,
CommonTableExpr cte,
Index  levelsup,
RangeVar rv,
bool  inFromCl 
)

Definition at line 1784 of file parse_relation.c.

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

Referenced by transformCTEReference().

1789 {
1791  Alias *alias = rv->alias;
1792  char *refname = alias ? alias->aliasname : cte->ctename;
1793  Alias *eref;
1794  int numaliases;
1795  int varattno;
1796  ListCell *lc;
1797 
1798  Assert(pstate != NULL);
1799 
1800  rte->rtekind = RTE_CTE;
1801  rte->ctename = cte->ctename;
1802  rte->ctelevelsup = levelsup;
1803 
1804  /* Self-reference if and only if CTE's parse analysis isn't completed */
1805  rte->self_reference = !IsA(cte->ctequery, Query);
1806  Assert(cte->cterecursive || !rte->self_reference);
1807  /* Bump the CTE's refcount if this isn't a self-reference */
1808  if (!rte->self_reference)
1809  cte->cterefcount++;
1810 
1811  /*
1812  * We throw error if the CTE is INSERT/UPDATE/DELETE without RETURNING.
1813  * This won't get checked in case of a self-reference, but that's OK
1814  * because data-modifying CTEs aren't allowed to be recursive anyhow.
1815  */
1816  if (IsA(cte->ctequery, Query))
1817  {
1818  Query *ctequery = (Query *) cte->ctequery;
1819 
1820  if (ctequery->commandType != CMD_SELECT &&
1821  ctequery->returningList == NIL)
1822  ereport(ERROR,
1823  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1824  errmsg("WITH query \"%s\" does not have a RETURNING clause",
1825  cte->ctename),
1826  parser_errposition(pstate, rv->location)));
1827  }
1828 
1829  rte->coltypes = cte->ctecoltypes;
1830  rte->coltypmods = cte->ctecoltypmods;
1831  rte->colcollations = cte->ctecolcollations;
1832 
1833  rte->alias = alias;
1834  if (alias)
1835  eref = copyObject(alias);
1836  else
1837  eref = makeAlias(refname, NIL);
1838  numaliases = list_length(eref->colnames);
1839 
1840  /* fill in any unspecified alias columns */
1841  varattno = 0;
1842  foreach(lc, cte->ctecolnames)
1843  {
1844  varattno++;
1845  if (varattno > numaliases)
1846  eref->colnames = lappend(eref->colnames, lfirst(lc));
1847  }
1848  if (varattno < numaliases)
1849  ereport(ERROR,
1850  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1851  errmsg("table \"%s\" has %d columns available but %d columns specified",
1852  refname, varattno, numaliases)));
1853 
1854  rte->eref = eref;
1855 
1856  /*
1857  * Set flags and access permissions.
1858  *
1859  * Subqueries are never checked for access rights.
1860  */
1861  rte->lateral = false;
1862  rte->inh = false; /* never true for subqueries */
1863  rte->inFromCl = inFromCl;
1864 
1865  rte->requiredPerms = 0;
1866  rte->checkAsUser = InvalidOid;
1867  rte->selectedCols = NULL;
1868  rte->insertedCols = NULL;
1869  rte->updatedCols = NULL;
1870 
1871  /*
1872  * Add completed RTE to pstate's range table list, but not to join list
1873  * nor namespace --- caller must do that if appropriate.
1874  */
1875  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1876 
1877  return rte;
1878 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
Alias * alias
Definition: parsenodes.h:960
List * colnames
Definition: primnodes.h:42
List * coltypmods
Definition: parsenodes.h:954
int errcode(int sqlerrcode)
Definition: elog.c:575
AclMode requiredPerms
Definition: parsenodes.h:965
List * colcollations
Definition: parsenodes.h:955
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
int location
Definition: primnodes.h:72
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:967
#define ERROR
Definition: elog.h:43
List * ctecoltypmods
Definition: parsenodes.h:1291
List * returningList
Definition: parsenodes.h:135
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
List * ctecolnames
Definition: parsenodes.h:1289
bool self_reference
Definition: parsenodes.h:944
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:969
CmdType commandType
Definition: parsenodes.h:103
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
Index ctelevelsup
Definition: parsenodes.h:943
List * ctecoltypes
Definition: parsenodes.h:1290
RTEKind rtekind
Definition: parsenodes.h:882
char * ctename
Definition: parsenodes.h:942
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:968
Alias * alias
Definition: primnodes.h:71
Alias * eref
Definition: parsenodes.h:961
List * ctecolcollations
Definition: parsenodes.h:1292
List * coltypes
Definition: parsenodes.h:953
List * p_rtable
Definition: parse_node.h:168
RangeTblEntry* addRangeTableEntryForFunction ( ParseState pstate,
List funcnames,
List funcexprs,
List coldeflists,
RangeFunction rangefunc,
bool  lateral,
bool  inFromCl 
)

Definition at line 1381 of file parse_relation.c.

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

Referenced by transformRangeFunction().

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

Definition at line 1714 of file parse_relation.c.

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

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

1720 {
1722  Alias *eref;
1723  int numaliases;
1724 
1725  Assert(pstate != NULL);
1726 
1727  /*
1728  * Fail if join has too many columns --- we must be able to reference any
1729  * of the columns with an AttrNumber.
1730  */
1731  if (list_length(aliasvars) > MaxAttrNumber)
1732  ereport(ERROR,
1733  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1734  errmsg("joins can have at most %d columns",
1735  MaxAttrNumber)));
1736 
1737  rte->rtekind = RTE_JOIN;
1738  rte->relid = InvalidOid;
1739  rte->subquery = NULL;
1740  rte->jointype = jointype;
1741  rte->joinaliasvars = aliasvars;
1742  rte->alias = alias;
1743 
1744  eref = alias ? (Alias *) copyObject(alias) : makeAlias("unnamed_join", NIL);
1745  numaliases = list_length(eref->colnames);
1746 
1747  /* fill in any unspecified alias columns */
1748  if (numaliases < list_length(colnames))
1749  eref->colnames = list_concat(eref->colnames,
1750  list_copy_tail(colnames, numaliases));
1751 
1752  rte->eref = eref;
1753 
1754  /*
1755  * Set flags and access permissions.
1756  *
1757  * Joins are never checked for access rights.
1758  */
1759  rte->lateral = false;
1760  rte->inh = false; /* never true for joins */
1761  rte->inFromCl = inFromCl;
1762 
1763  rte->requiredPerms = 0;
1764  rte->checkAsUser = InvalidOid;
1765  rte->selectedCols = NULL;
1766  rte->insertedCols = NULL;
1767  rte->updatedCols = NULL;
1768 
1769  /*
1770  * Add completed RTE to pstate's range table list, but not to join list
1771  * nor namespace --- caller must do that if appropriate.
1772  */
1773  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1774 
1775  return rte;
1776 }
#define NIL
Definition: pg_list.h:69
List * joinaliasvars
Definition: parsenodes.h:921
Alias * alias
Definition: parsenodes.h:960
List * colnames
Definition: primnodes.h:42
#define MaxAttrNumber
Definition: attnum.h:24
int errcode(int sqlerrcode)
Definition: elog.c:575
List * list_concat(List *list1, List *list2)
Definition: list.c:321
AclMode requiredPerms
Definition: parsenodes.h:965
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:967
#define ERROR
Definition: elog.h:43
JoinType jointype
Definition: parsenodes.h:920
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:969
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:882
Query * subquery
Definition: parsenodes.h:900
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:968
Alias * eref
Definition: parsenodes.h:961
List * p_rtable
Definition: parse_node.h:168
RangeTblEntry* addRangeTableEntryForRelation ( ParseState pstate,
Relation  rel,
Alias alias,
bool  inh,
bool  inFromCl 
)

Definition at line 1246 of file parse_relation.c.

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

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

1251 {
1253  char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1254 
1255  Assert(pstate != NULL);
1256 
1257  rte->rtekind = RTE_RELATION;
1258  rte->alias = alias;
1259  rte->relid = RelationGetRelid(rel);
1260  rte->relkind = rel->rd_rel->relkind;
1261 
1262  /*
1263  * Build the list of effective column names using user-supplied aliases
1264  * and/or actual column names.
1265  */
1266  rte->eref = makeAlias(refname, NIL);
1267  buildRelationAliases(rel->rd_att, alias, rte->eref);
1268 
1269  /*
1270  * Set flags and access permissions.
1271  *
1272  * The initial default on access checks is always check-for-READ-access,
1273  * which is the right thing for all except target tables.
1274  */
1275  rte->lateral = false;
1276  rte->inh = inh;
1277  rte->inFromCl = inFromCl;
1278 
1279  rte->requiredPerms = ACL_SELECT;
1280  rte->checkAsUser = InvalidOid; /* not set-uid by default, either */
1281  rte->selectedCols = NULL;
1282  rte->insertedCols = NULL;
1283  rte->updatedCols = NULL;
1284 
1285  /*
1286  * Add completed RTE to pstate's range table list, but not to join list
1287  * nor namespace --- caller must do that if appropriate.
1288  */
1289  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1290 
1291  return rte;
1292 }
#define NIL
Definition: pg_list.h:69
Alias * alias
Definition: parsenodes.h:960
AclMode requiredPerms
Definition: parsenodes.h:965
Form_pg_class rd_rel
Definition: rel.h:113
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:967
#define RelationGetRelationName(relation)
Definition: rel.h:433
List * lappend(List *list, void *datum)
Definition: list.c:128
#define ACL_SELECT
Definition: parsenodes.h:66
TupleDesc rd_att
Definition: rel.h:114
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:969
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
char * aliasname
Definition: primnodes.h:41
RTEKind rtekind
Definition: parsenodes.h:882
Bitmapset * insertedCols
Definition: parsenodes.h:968
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
Alias * eref
Definition: parsenodes.h:961
#define RelationGetRelid(relation)
Definition: rel.h:413
List * p_rtable
Definition: parse_node.h:168
RangeTblEntry* addRangeTableEntryForSubquery ( ParseState pstate,
Query subquery,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1301 of file parse_relation.c.

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

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

1306 {
1308  char *refname = alias->aliasname;
1309  Alias *eref;
1310  int numaliases;
1311  int varattno;
1312  ListCell *tlistitem;
1313 
1314  Assert(pstate != NULL);
1315 
1316  rte->rtekind = RTE_SUBQUERY;
1317  rte->relid = InvalidOid;
1318  rte->subquery = subquery;
1319  rte->alias = alias;
1320 
1321  eref = copyObject(alias);
1322  numaliases = list_length(eref->colnames);
1323 
1324  /* fill in any unspecified alias columns */
1325  varattno = 0;
1326  foreach(tlistitem, subquery->targetList)
1327  {
1328  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1329 
1330  if (te->resjunk)
1331  continue;
1332  varattno++;
1333  Assert(varattno == te->resno);
1334  if (varattno > numaliases)
1335  {
1336  char *attrname;
1337 
1338  attrname = pstrdup(te->resname);
1339  eref->colnames = lappend(eref->colnames, makeString(attrname));
1340  }
1341  }
1342  if (varattno < numaliases)
1343  ereport(ERROR,
1344  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1345  errmsg("table \"%s\" has %d columns available but %d columns specified",
1346  refname, varattno, numaliases)));
1347 
1348  rte->eref = eref;
1349 
1350  /*
1351  * Set flags and access permissions.
1352  *
1353  * Subqueries are never checked for access rights.
1354  */
1355  rte->lateral = lateral;
1356  rte->inh = false; /* never true for subqueries */
1357  rte->inFromCl = inFromCl;
1358 
1359  rte->requiredPerms = 0;
1360  rte->checkAsUser = InvalidOid;
1361  rte->selectedCols = NULL;
1362  rte->insertedCols = NULL;
1363  rte->updatedCols = NULL;
1364 
1365  /*
1366  * Add completed RTE to pstate's range table list, but not to join list
1367  * nor namespace --- caller must do that if appropriate.
1368  */
1369  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1370 
1371  return rte;
1372 }
Value * makeString(char *str)
Definition: value.c:53
Alias * alias
Definition: parsenodes.h:960
List * colnames
Definition: primnodes.h:42
char * pstrdup(const char *in)
Definition: mcxt.c:1165
int errcode(int sqlerrcode)
Definition: elog.c:575
AclMode requiredPerms
Definition: parsenodes.h:965
char * resname
Definition: primnodes.h:1332
List * targetList
Definition: parsenodes.h:131
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
Bitmapset * selectedCols
Definition: parsenodes.h:967
bool resjunk
Definition: primnodes.h:1337
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1331
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:969
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:882
Query * subquery
Definition: parsenodes.h:900
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:968
Alias * eref
Definition: parsenodes.h:961
List * p_rtable
Definition: parse_node.h:168
RangeTblEntry* addRangeTableEntryForValues ( ParseState pstate,
List exprs,
List coltypes,
List coltypmods,
List colcollations,
Alias alias,
bool  lateral,
bool  inFromCl 
)

Definition at line 1636 of file parse_relation.c.

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

Referenced by transformValuesClause().

1644 {
1646  char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
1647  Alias *eref;
1648  int numaliases;
1649  int numcolumns;
1650 
1651  Assert(pstate != NULL);
1652 
1653  rte->rtekind = RTE_VALUES;
1654  rte->relid = InvalidOid;
1655  rte->subquery = NULL;
1656  rte->values_lists = exprs;
1657  rte->coltypes = coltypes;
1658  rte->coltypmods = coltypmods;
1659  rte->colcollations = colcollations;
1660  rte->alias = alias;
1661 
1662  eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
1663 
1664  /* fill in any unspecified alias columns */
1665  numcolumns = list_length((List *) linitial(exprs));
1666  numaliases = list_length(eref->colnames);
1667  while (numaliases < numcolumns)
1668  {
1669  char attrname[64];
1670 
1671  numaliases++;
1672  snprintf(attrname, sizeof(attrname), "column%d", numaliases);
1673  eref->colnames = lappend(eref->colnames,
1674  makeString(pstrdup(attrname)));
1675  }
1676  if (numcolumns < numaliases)
1677  ereport(ERROR,
1678  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1679  errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
1680  refname, numcolumns, numaliases)));
1681 
1682  rte->eref = eref;
1683 
1684  /*
1685  * Set flags and access permissions.
1686  *
1687  * Subqueries are never checked for access rights.
1688  */
1689  rte->lateral = lateral;
1690  rte->inh = false; /* never true for values RTEs */
1691  rte->inFromCl = inFromCl;
1692 
1693  rte->requiredPerms = 0;
1694  rte->checkAsUser = InvalidOid;
1695  rte->selectedCols = NULL;
1696  rte->insertedCols = NULL;
1697  rte->updatedCols = NULL;
1698 
1699  /*
1700  * Add completed RTE to pstate's range table list, but not to join list
1701  * nor namespace --- caller must do that if appropriate.
1702  */
1703  pstate->p_rtable = lappend(pstate->p_rtable, rte);
1704 
1705  return rte;
1706 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
Alias * alias
Definition: parsenodes.h:960
List * colnames
Definition: primnodes.h:42
char * pstrdup(const char *in)
Definition: mcxt.c:1165
List * coltypmods
Definition: parsenodes.h:954
int errcode(int sqlerrcode)
Definition: elog.c:575
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
AclMode requiredPerms
Definition: parsenodes.h:965
List * colcollations
Definition: parsenodes.h:955
List * values_lists
Definition: parsenodes.h:937
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:384
Bitmapset * selectedCols
Definition: parsenodes.h:967
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
Bitmapset * updatedCols
Definition: parsenodes.h:969
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
char * aliasname
Definition: primnodes.h:41
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:882
Query * subquery
Definition: parsenodes.h:900
int errmsg(const char *fmt,...)
Definition: elog.c:797
Bitmapset * insertedCols
Definition: parsenodes.h:968
Alias * eref
Definition: parsenodes.h:961
List * coltypes
Definition: parsenodes.h:953
Definition: pg_list.h:45
List * p_rtable
Definition: parse_node.h:168
void addRTEtoQuery ( ParseState pstate,
RangeTblEntry rte,
bool  addToJoinList,
bool  addToRelNameSpace,
bool  addToVarNameSpace 
)

Definition at line 1939 of file parse_relation.c.

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

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

1942 {
1943  if (addToJoinList)
1944  {
1945  int rtindex = RTERangeTablePosn(pstate, rte, NULL);
1947 
1948  rtr->rtindex = rtindex;
1949  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
1950  }
1951  if (addToRelNameSpace || addToVarNameSpace)
1952  {
1953  ParseNamespaceItem *nsitem;
1954 
1955  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1956  nsitem->p_rte = rte;
1957  nsitem->p_rel_visible = addToRelNameSpace;
1958  nsitem->p_cols_visible = addToVarNameSpace;
1959  nsitem->p_lateral_only = false;
1960  nsitem->p_lateral_ok = true;
1961  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
1962  }
1963 }
RangeTblEntry * p_rte
Definition: parse_node.h:238
List * p_namespace
Definition: parse_node.h:172
List * lappend(List *list, void *datum)
Definition: list.c:128
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
void * palloc(Size size)
Definition: mcxt.c:891
List * p_joinlist
Definition: parse_node.h:170
int attnameAttNum ( Relation  rd,
const char *  attname,
bool  sysColOK 
)

Definition at line 2828 of file parse_relation.c.

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

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

2829 {
2830  int i;
2831 
2832  for (i = 0; i < rd->rd_rel->relnatts; i++)
2833  {
2834  Form_pg_attribute att = rd->rd_att->attrs[i];
2835 
2836  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
2837  return i + 1;
2838  }
2839 
2840  if (sysColOK)
2841  {
2842  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
2843  {
2844  if (i != ObjectIdAttributeNumber || rd->rd_rel->relhasoids)
2845  return i;
2846  }
2847  }
2848 
2849  /* on failure */
2850  return InvalidAttrNumber;
2851 }
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int namestrcmp(Name name, const char *str)
Definition: name.c:248
static int specialAttNum(const char *attname)
Form_pg_class rd_rel
Definition: rel.h:113
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
TupleDesc rd_att
Definition: rel.h:114
#define InvalidAttrNumber
Definition: attnum.h:23
int i
Name attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 2883 of file parse_relation.c.

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

Referenced by transformFkeyGetPrimaryKey().

2884 {
2885  if (attid <= 0)
2886  {
2887  Form_pg_attribute sysatt;
2888 
2889  sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
2890  return &sysatt->attname;
2891  }
2892  if (attid > rd->rd_att->natts)
2893  elog(ERROR, "invalid attribute number %d", attid);
2894  return &rd->rd_att->attrs[attid - 1]->attname;
2895 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Form_pg_class rd_rel
Definition: rel.h:113
int natts
Definition: tupdesc.h:73
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
TupleDesc rd_att
Definition: rel.h:114
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:198
#define elog
Definition: elog.h:219
Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 2925 of file parse_relation.c.

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

2926 {
2927  if (attid <= 0)
2928  {
2929  /* All system attributes are of noncollatable types. */
2930  return InvalidOid;
2931  }
2932  if (attid > rd->rd_att->natts)
2933  elog(ERROR, "invalid attribute number %d", attid);
2934  return rd->rd_att->attrs[attid - 1]->attcollation;
2935 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int natts
Definition: tupdesc.h:73
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:114
#define InvalidOid
Definition: postgres_ext.h:36
#define elog
Definition: elog.h:219
Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 2905 of file parse_relation.c.

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

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

2906 {
2907  if (attid <= 0)
2908  {
2909  Form_pg_attribute sysatt;
2910 
2911  sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
2912  return sysatt->atttypid;
2913  }
2914  if (attid > rd->rd_att->natts)
2915  elog(ERROR, "invalid attribute number %d", attid);
2916  return rd->rd_att->attrs[attid - 1]->atttypid;
2917 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Form_pg_class rd_rel
Definition: rel.h:113
int natts
Definition: tupdesc.h:73
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
TupleDesc rd_att
Definition: rel.h:114
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:198
#define elog
Definition: elog.h:219
static void buildRelationAliases ( TupleDesc  tupdesc,
Alias alias,
Alias eref 
)
static

Definition at line 1009 of file parse_relation.c.

References Alias::aliasname, Assert, tupleDesc::attrs, Alias::colnames, ereport, errcode(), errmsg(), ERROR, lappend(), lfirst, list_head(), list_length(), lnext, makeString(), NameStr, tupleDesc::natts, NIL, NULL, and pstrdup().

Referenced by addRangeTableEntry(), addRangeTableEntryForFunction(), and addRangeTableEntryForRelation().

1010 {
1011  int maxattrs = tupdesc->natts;
1012  ListCell *aliaslc;
1013  int numaliases;
1014  int varattno;
1015  int numdropped = 0;
1016 
1017  Assert(eref->colnames == NIL);
1018 
1019  if (alias)
1020  {
1021  aliaslc = list_head(alias->colnames);
1022  numaliases = list_length(alias->colnames);
1023  /* We'll rebuild the alias colname list */
1024  alias->colnames = NIL;
1025  }
1026  else
1027  {
1028  aliaslc = NULL;
1029  numaliases = 0;
1030  }
1031 
1032  for (varattno = 0; varattno < maxattrs; varattno++)
1033  {
1034  Form_pg_attribute attr = tupdesc->attrs[varattno];
1035  Value *attrname;
1036 
1037  if (attr->attisdropped)
1038  {
1039  /* Always insert an empty string for a dropped column */
1040  attrname = makeString(pstrdup(""));
1041  if (aliaslc)
1042  alias->colnames = lappend(alias->colnames, attrname);
1043  numdropped++;
1044  }
1045  else if (aliaslc)
1046  {
1047  /* Use the next user-supplied alias */
1048  attrname = (Value *) lfirst(aliaslc);
1049  aliaslc = lnext(aliaslc);
1050  alias->colnames = lappend(alias->colnames, attrname);
1051  }
1052  else
1053  {
1054  attrname = makeString(pstrdup(NameStr(attr->attname)));
1055  /* we're done with the alias if any */
1056  }
1057 
1058  eref->colnames = lappend(eref->colnames, attrname);
1059  }
1060 
1061  /* Too many user-supplied aliases? */
1062  if (aliaslc)
1063  ereport(ERROR,
1064  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1065  errmsg("table \"%s\" has %d columns available but %d columns specified",
1066  eref->aliasname, maxattrs - numdropped, numaliases)));
1067 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
List * colnames
Definition: primnodes.h:42
char * pstrdup(const char *in)
Definition: mcxt.c:1165
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int errcode(int sqlerrcode)
Definition: elog.c:575
int natts
Definition: tupdesc.h:73
#define ERROR
Definition: elog.h:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
Definition: value.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:495
static void check_lateral_ref_ok ( ParseState pstate,
ParseNamespaceItem nsitem,
int  location 
)
static

Definition at line 414 of file parse_relation.c.

References Alias::aliasname, RangeTblEntry::eref, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_rte, ParseState::p_target_rangetblentry, and parser_errposition().

Referenced by colNameToVar(), scanNameSpaceForRefname(), and scanNameSpaceForRelid().

416 {
417  if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
418  {
419  /* SQL:2008 demands this be an error, not an invisible item */
420  RangeTblEntry *rte = nsitem->p_rte;
421  char *refname = rte->eref->aliasname;
422 
423  ereport(ERROR,
424  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
425  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
426  refname),
427  (rte == pstate->p_target_rangetblentry) ?
428  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
429  refname) :
430  errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
431  parser_errposition(pstate, location)));
432  }
433 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:238
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:179
char * aliasname
Definition: primnodes.h:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:961
void checkNameSpaceConflicts ( ParseState pstate,
List namespace1,
List namespace2 
)

Definition at line 368 of file parse_relation.c.

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

Referenced by transformFromClause(), and transformFromClauseItem().

370 {
371  ListCell *l1;
372 
373  foreach(l1, namespace1)
374  {
375  ParseNamespaceItem *nsitem1 = (ParseNamespaceItem *) lfirst(l1);
376  RangeTblEntry *rte1 = nsitem1->p_rte;
377  const char *aliasname1 = rte1->eref->aliasname;
378  ListCell *l2;
379 
380  if (!nsitem1->p_rel_visible)
381  continue;
382 
383  foreach(l2, namespace2)
384  {
385  ParseNamespaceItem *nsitem2 = (ParseNamespaceItem *) lfirst(l2);
386  RangeTblEntry *rte2 = nsitem2->p_rte;
387 
388  if (!nsitem2->p_rel_visible)
389  continue;
390  if (strcmp(rte2->eref->aliasname, aliasname1) != 0)
391  continue; /* definitely no conflict */
392  if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
393  rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
394  rte1->relid != rte2->relid)
395  continue; /* no conflict per SQL rule */
396  ereport(ERROR,
397  (errcode(ERRCODE_DUPLICATE_ALIAS),
398  errmsg("table name \"%s\" specified more than once",
399  aliasname1)));
400  }
401  }
402 }
Alias * alias
Definition: parsenodes.h:960
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:238
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
RTEKind rtekind
Definition: parsenodes.h:882
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:961
static char* chooseScalarFunctionAlias ( Node funcexpr,
char *  funcname,
Alias alias,
int  nfuncs 
)
static

Definition at line 1083 of file parse_relation.c.

References Alias::aliasname, get_func_result_name(), and IsA.

Referenced by addRangeTableEntryForFunction().

1085 {
1086  char *pname;
1087 
1088  /*
1089  * If the expression is a simple function call, and the function has a
1090  * single OUT parameter that is named, use the parameter's name.
1091  */
1092  if (funcexpr && IsA(funcexpr, FuncExpr))
1093  {
1094  pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1095  if (pname)
1096  return pname;
1097  }
1098 
1099  /*
1100  * If there's just one function in the RTE, and the user gave an RTE alias
1101  * name, use that name. (This makes FROM func() AS foo use "foo" as the
1102  * column name as well as the table alias.)
1103  */
1104  if (nfuncs == 1 && alias)
1105  return alias->aliasname;
1106 
1107  /*
1108  * Otherwise use the function name.
1109  */
1110  return funcname;
1111 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
char * aliasname
Definition: primnodes.h:41
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1018
Node* colNameToVar ( ParseState pstate,
char *  colname,
bool  localonly,
int  location 
)

Definition at line 736 of file parse_relation.c.

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

Referenced by findTargetlistEntrySQL92(), and transformColumnRef().

738 {
739  Node *result = NULL;
740  ParseState *orig_pstate = pstate;
741 
742  while (pstate != NULL)
743  {
744  ListCell *l;
745 
746  foreach(l, pstate->p_namespace)
747  {
749  RangeTblEntry *rte = nsitem->p_rte;
750  Node *newresult;
751 
752  /* Ignore table-only items */
753  if (!nsitem->p_cols_visible)
754  continue;
755  /* If not inside LATERAL, ignore lateral-only items */
756  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
757  continue;
758 
759  /* use orig_pstate here to get the right sublevels_up */
760  newresult = scanRTEForColumn(orig_pstate, rte, colname, location,
761  0, NULL);
762 
763  if (newresult)
764  {
765  if (result)
766  ereport(ERROR,
767  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
768  errmsg("column reference \"%s\" is ambiguous",
769  colname),
770  parser_errposition(pstate, location)));
771  check_lateral_ref_ok(pstate, nsitem, location);
772  result = newresult;
773  }
774  }
775 
776  if (result != NULL || localonly)
777  break; /* found, or don't want to look at parent */
778 
779  pstate = pstate->parentParseState;
780  }
781 
782  return result;
783 }
Node * scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
Definition: nodes.h:508
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:238
#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:172
#define ereport(elevel, rest)
Definition: elog.h:122
struct ParseState * parentParseState
Definition: parse_node.h:166
bool p_lateral_active
Definition: parse_node.h:174
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
void errorMissingColumn ( ParseState pstate,
char *  relname,
char *  colname,
int  location 
)

Definition at line 3000 of file parse_relation.c.

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

Referenced by transformColumnRef().

3002 {
3004  char *closestfirst = NULL;
3005 
3006  /*
3007  * Search the entire rtable looking for possible matches. If we find one,
3008  * emit a hint about it.
3009  *
3010  * TODO: improve this code (and also errorMissingRTE) to mention using
3011  * LATERAL if appropriate.
3012  */
3013  state = searchRangeTableForCol(pstate, relname, colname, location);
3014 
3015  /*
3016  * Extract closest col string for best match, if any.
3017  *
3018  * Infer an exact match referenced despite not being visible from the fact
3019  * that an attribute number was not present in state passed back -- this
3020  * is what is reported when !closestfirst. There might also be an exact
3021  * match that was qualified with an incorrect alias, in which case
3022  * closestfirst will be set (so hint is the same as generic fuzzy case).
3023  */
3024  if (state->rfirst && AttributeNumberIsValid(state->first))
3025  closestfirst = strVal(list_nth(state->rfirst->eref->colnames,
3026  state->first - 1));
3027 
3028  if (!state->rsecond)
3029  {
3030  /*
3031  * Handle case where there is zero or one column suggestions to hint,
3032  * including exact matches referenced but not visible.
3033  */
3034  ereport(ERROR,
3035  (errcode(ERRCODE_UNDEFINED_COLUMN),
3036  relname ?
3037  errmsg("column %s.%s does not exist", relname, colname) :
3038  errmsg("column \"%s\" does not exist", colname),
3039  state->rfirst ? closestfirst ?
3040  errhint("Perhaps you meant to reference the column \"%s.%s\".",
3041  state->rfirst->eref->aliasname, closestfirst) :
3042  errhint("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3043  colname, state->rfirst->eref->aliasname) : 0,
3044  parser_errposition(pstate, location)));
3045  }
3046  else
3047  {
3048  /* Handle case where there are two equally useful column hints */
3049  char *closestsecond;
3050 
3051  closestsecond = strVal(list_nth(state->rsecond->eref->colnames,
3052  state->second - 1));
3053 
3054  ereport(ERROR,
3055  (errcode(ERRCODE_UNDEFINED_COLUMN),
3056  relname ?
3057  errmsg("column %s.%s does not exist", relname, colname) :
3058  errmsg("column \"%s\" does not exist", colname),
3059  errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3060  state->rfirst->eref->aliasname, closestfirst,
3061  state->rsecond->eref->aliasname, closestsecond),
3062  parser_errposition(pstate, location)));
3063  }
3064 }
int errhint(const char *fmt,...)
Definition: elog.c:987
List * colnames
Definition: primnodes.h:42
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
RangeTblEntry * rfirst
void * list_nth(const List *list, int n)
Definition: list.c:410
#define ereport(elevel, rest)
Definition: elog.h:122
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define NULL
Definition: c.h:226
char * aliasname
Definition: primnodes.h:41
Definition: regguts.h:298
RangeTblEntry * rsecond
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:961
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, char *colname, int location)
void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 2944 of file parse_relation.c.

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

Referenced by ExpandColumnRefStar(), and transformColumnRef().

2945 {
2946  RangeTblEntry *rte;
2947  int sublevels_up;
2948  const char *badAlias = NULL;
2949 
2950  /*
2951  * Check to see if there are any potential matches in the query's
2952  * rangetable. (Note: cases involving a bad schema name in the RangeVar
2953  * will throw error immediately here. That seems OK.)
2954  */
2955  rte = searchRangeTableForRel(pstate, relation);
2956 
2957  /*
2958  * If we found a match that has an alias and the alias is visible in the
2959  * namespace, then the problem is probably use of the relation's real name
2960  * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
2961  * common enough to justify a specific hint.
2962  *
2963  * If we found a match that doesn't meet those criteria, assume the
2964  * problem is illegal use of a relation outside its scope, as in the
2965  * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
2966  */
2967  if (rte && rte->alias &&
2968  strcmp(rte->eref->aliasname, relation->relname) != 0 &&
2969  refnameRangeTblEntry(pstate, NULL, rte->eref->aliasname,
2970  relation->location,
2971  &sublevels_up) == rte)
2972  badAlias = rte->eref->aliasname;
2973 
2974  if (rte)
2975  ereport(ERROR,
2977  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
2978  relation->relname),
2979  (badAlias ?
2980  errhint("Perhaps you meant to reference the table alias \"%s\".",
2981  badAlias) :
2982  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
2983  rte->eref->aliasname)),
2984  parser_errposition(pstate, relation->location)));
2985  else
2986  ereport(ERROR,
2988  errmsg("missing FROM-clause entry for table \"%s\"",
2989  relation->relname),
2990  parser_errposition(pstate, relation->location)));
2991 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
Alias * alias
Definition: parsenodes.h:960
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * refnameRangeTblEntry(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
int location
Definition: primnodes.h:72
char * relname
Definition: primnodes.h:67
static RangeTblEntry * searchRangeTableForRel(ParseState *pstate, RangeVar *relation)
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
char * aliasname
Definition: primnodes.h:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:961
static void expandRelation ( Oid  relid,
Alias eref,
int  rtindex,
int  sublevels_up,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)
static

Definition at line 2282 of file parse_relation.c.

References AccessShareLock, expandTupleDesc(), tupleDesc::natts, RelationData::rd_att, relation_close(), and relation_open().

Referenced by expandRTE().

2285 {
2286  Relation rel;
2287 
2288  /* Get the tupledesc and turn it over to expandTupleDesc */
2289  rel = relation_open(relid, AccessShareLock);
2290  expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
2291  rtindex, sublevels_up,
2292  location, include_dropped,
2293  colnames, colvars);
2295 }
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
int natts
Definition: tupdesc.h:73
TupleDesc rd_att
Definition: rel.h:114
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)
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1117
List* expandRelAttrs ( ParseState pstate,
RangeTblEntry rte,
int  rtindex,
int  sublevels_up,
int  location 
)

Definition at line 2395 of file parse_relation.c.

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

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

2397 {
2398  List *names,
2399  *vars;
2400  ListCell *name,
2401  *var;
2402  List *te_list = NIL;
2403 
2404  expandRTE(rte, rtindex, sublevels_up, location, false,
2405  &names, &vars);
2406 
2407  /*
2408  * Require read access to the table. This is normally redundant with the
2409  * markVarForSelectPriv calls below, but not if the table has zero
2410  * columns.
2411  */
2412  rte->requiredPerms |= ACL_SELECT;
2413 
2414  forboth(name, names, var, vars)
2415  {
2416  char *label = strVal(lfirst(name));
2417  Var *varnode = (Var *) lfirst(var);
2418  TargetEntry *te;
2419 
2420  te = makeTargetEntry((Expr *) varnode,
2421  (AttrNumber) pstate->p_next_resno++,
2422  label,
2423  false);
2424  te_list = lappend(te_list, te);
2425 
2426  /* Require read access to each column */
2427  markVarForSelectPriv(pstate, varnode, rte);
2428  }
2429 
2430  Assert(name == NULL && var == NULL); /* lists not the same length? */
2431 
2432  return te_list;
2433 }
#define NIL
Definition: pg_list.h:69
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
#define strVal(v)
Definition: value.h:54
AclMode requiredPerms
Definition: parsenodes.h:965
Definition: primnodes.h:141
int p_next_resno
Definition: parse_node.h:183
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
List * lappend(List *list, void *datum)
Definition: list.c:128
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
#define ACL_SELECT
Definition: parsenodes.h:66
static char * label
Definition: pg_basebackup.c:83
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
const char * name
Definition: encode.c:521
Definition: regcomp.c:226
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
void expandRTE ( RangeTblEntry rte,
int  rtindex,
int  sublevels_up,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)

Definition at line 1983 of file parse_relation.c.

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

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

1986 {
1987  int varattno;
1988 
1989  if (colnames)
1990  *colnames = NIL;
1991  if (colvars)
1992  *colvars = NIL;
1993 
1994  switch (rte->rtekind)
1995  {
1996  case RTE_RELATION:
1997  /* Ordinary relation RTE */
1998  expandRelation(rte->relid, rte->eref,
1999  rtindex, sublevels_up, location,
2000  include_dropped, colnames, colvars);
2001  break;
2002  case RTE_SUBQUERY:
2003  {
2004  /* Subquery RTE */
2005  ListCell *aliasp_item = list_head(rte->eref->colnames);
2006  ListCell *tlistitem;
2007 
2008  varattno = 0;
2009  foreach(tlistitem, rte->subquery->targetList)
2010  {
2011  TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
2012 
2013  if (te->resjunk)
2014  continue;
2015  varattno++;
2016  Assert(varattno == te->resno);
2017 
2018  if (colnames)
2019  {
2020  /* Assume there is one alias per target item */
2021  char *label = strVal(lfirst(aliasp_item));
2022 
2023  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2024  aliasp_item = lnext(aliasp_item);
2025  }
2026 
2027  if (colvars)
2028  {
2029  Var *varnode;
2030 
2031  varnode = makeVar(rtindex, varattno,
2032  exprType((Node *) te->expr),
2033  exprTypmod((Node *) te->expr),
2034  exprCollation((Node *) te->expr),
2035  sublevels_up);
2036  varnode->location = location;
2037 
2038  *colvars = lappend(*colvars, varnode);
2039  }
2040  }
2041  }
2042  break;
2043  case RTE_FUNCTION:
2044  {
2045  /* Function RTE */
2046  int atts_done = 0;
2047  ListCell *lc;
2048 
2049  foreach(lc, rte->functions)
2050  {
2051  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2052  TypeFuncClass functypclass;
2053  Oid funcrettype;
2054  TupleDesc tupdesc;
2055 
2056  functypclass = get_expr_result_type(rtfunc->funcexpr,
2057  &funcrettype,
2058  &tupdesc);
2059  if (functypclass == TYPEFUNC_COMPOSITE)
2060  {
2061  /* Composite data type, e.g. a table's row type */
2062  Assert(tupdesc);
2063  expandTupleDesc(tupdesc, rte->eref,
2064  rtfunc->funccolcount, atts_done,
2065  rtindex, sublevels_up, location,
2066  include_dropped, colnames, colvars);
2067  }
2068  else if (functypclass == TYPEFUNC_SCALAR)
2069  {
2070  /* Base data type, i.e. scalar */
2071  if (colnames)
2072  *colnames = lappend(*colnames,
2073  list_nth(rte->eref->colnames,
2074  atts_done));
2075 
2076  if (colvars)
2077  {
2078  Var *varnode;
2079 
2080  varnode = makeVar(rtindex, atts_done + 1,
2081  funcrettype, -1,
2082  exprCollation(rtfunc->funcexpr),
2083  sublevels_up);
2084  varnode->location = location;
2085 
2086  *colvars = lappend(*colvars, varnode);
2087  }
2088  }
2089  else if (functypclass == TYPEFUNC_RECORD)
2090  {
2091  if (colnames)
2092  {
2093  List *namelist;
2094 
2095  /* extract appropriate subset of column list */
2096  namelist = list_copy_tail(rte->eref->colnames,
2097  atts_done);
2098  namelist = list_truncate(namelist,
2099  rtfunc->funccolcount);
2100  *colnames = list_concat(*colnames, namelist);
2101  }
2102 
2103  if (colvars)
2104  {
2105  ListCell *l1;
2106  ListCell *l2;
2107  ListCell *l3;
2108  int attnum = atts_done;
2109 
2110  forthree(l1, rtfunc->funccoltypes,
2111  l2, rtfunc->funccoltypmods,
2112  l3, rtfunc->funccolcollations)
2113  {
2114  Oid attrtype = lfirst_oid(l1);
2115  int32 attrtypmod = lfirst_int(l2);
2116  Oid attrcollation = lfirst_oid(l3);
2117  Var *varnode;
2118 
2119  attnum++;
2120  varnode = makeVar(rtindex,
2121  attnum,
2122  attrtype,
2123  attrtypmod,
2124  attrcollation,
2125  sublevels_up);
2126  varnode->location = location;
2127  *colvars = lappend(*colvars, varnode);
2128  }
2129  }
2130  }
2131  else
2132  {
2133  /* addRangeTableEntryForFunction should've caught this */
2134  elog(ERROR, "function in FROM has unsupported return type");
2135  }
2136  atts_done += rtfunc->funccolcount;
2137  }
2138 
2139  /* Append the ordinality column if any */
2140  if (rte->funcordinality)
2141  {
2142  if (colnames)
2143  *colnames = lappend(*colnames,
2144  llast(rte->eref->colnames));
2145 
2146  if (colvars)
2147  {
2148  Var *varnode = makeVar(rtindex,
2149  atts_done + 1,
2150  INT8OID,
2151  -1,
2152  InvalidOid,
2153  sublevels_up);
2154 
2155  *colvars = lappend(*colvars, varnode);
2156  }
2157  }
2158  }
2159  break;
2160  case RTE_JOIN:
2161  {
2162  /* Join RTE */
2163  ListCell *colname;
2164  ListCell *aliasvar;
2165 
2167 
2168  varattno = 0;
2169  forboth(colname, rte->eref->colnames, aliasvar, rte->joinaliasvars)
2170  {
2171  Node *avar = (Node *) lfirst(aliasvar);
2172 
2173  varattno++;
2174 
2175  /*
2176  * During ordinary parsing, there will never be any
2177  * deleted columns in the join; but we have to check since
2178  * this routine is also used by the rewriter, and joins
2179  * found in stored rules might have join columns for
2180  * since-deleted columns. This will be signaled by a null
2181  * pointer in the alias-vars list.
2182  */
2183  if (avar == NULL)
2184  {
2185  if (include_dropped)
2186  {
2187  if (colnames)
2188  *colnames = lappend(*colnames,
2189  makeString(pstrdup("")));
2190  if (colvars)
2191  {
2192  /*
2193  * Can't use join's column type here (it might
2194  * be dropped!); but it doesn't really matter
2195  * what type the Const claims to be.
2196  */
2197  *colvars = lappend(*colvars,
2198  makeNullConst(INT4OID, -1,
2199  InvalidOid));
2200  }
2201  }
2202  continue;
2203  }
2204 
2205  if (colnames)
2206  {
2207  char *label = strVal(lfirst(colname));
2208 
2209  *colnames = lappend(*colnames,
2210  makeString(pstrdup(label)));
2211  }
2212 
2213  if (colvars)
2214  {
2215  Var *varnode;
2216 
2217  varnode = makeVar(rtindex, varattno,
2218  exprType(avar),
2219  exprTypmod(avar),
2220  exprCollation(avar),
2221  sublevels_up);
2222  varnode->location = location;
2223 
2224  *colvars = lappend(*colvars, varnode);
2225  }
2226  }
2227  }
2228  break;
2229  case RTE_VALUES:
2230  case RTE_CTE:
2231  {
2232  /* Values or CTE RTE */
2233  ListCell *aliasp_item = list_head(rte->eref->colnames);
2234  ListCell *lct;
2235  ListCell *lcm;
2236  ListCell *lcc;
2237 
2238  varattno = 0;
2239  forthree(lct, rte->coltypes,
2240  lcm, rte->coltypmods,
2241  lcc, rte->colcollations)
2242  {
2243  Oid coltype = lfirst_oid(lct);
2244  int32 coltypmod = lfirst_int(lcm);
2245  Oid colcoll = lfirst_oid(lcc);
2246 
2247  varattno++;
2248 
2249  if (colnames)
2250  {
2251  /* Assume there is one alias per output column */
2252  char *label = strVal(lfirst(aliasp_item));
2253 
2254  *colnames = lappend(*colnames,
2255  makeString(pstrdup(label)));
2256  aliasp_item = lnext(aliasp_item);
2257  }
2258 
2259  if (colvars)
2260  {
2261  Var *varnode;
2262 
2263  varnode = makeVar(rtindex, varattno,
2264  coltype, coltypmod, colcoll,
2265  sublevels_up);
2266  varnode->location = location;
2267 
2268  *colvars = lappend(*colvars, varnode);
2269  }
2270  }
2271  }
2272  break;
2273  default:
2274  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2275  }
2276 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
List * joinaliasvars
Definition: parsenodes.h:921
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
List * colnames
Definition: primnodes.h:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:183
char * pstrdup(const char *in)
Definition: mcxt.c:1165
List * coltypmods
Definition: parsenodes.h:954
#define llast(l)
Definition: pg_list.h:126
List * list_truncate(List *list, int new_size)
Definition: list.c:350
#define INT4OID
Definition: pg_type.h:316
Definition: nodes.h:508
#define strVal(v)
Definition: value.h:54
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
bool funcordinality
Definition: parsenodes.h:932
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:141
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
List * colcollations
Definition: parsenodes.h:955
signed int int32
Definition: c.h:253
List * targetList
Definition: parsenodes.h:131
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
bool resjunk
Definition: primnodes.h:1337
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
int location
Definition: primnodes.h:156
void * list_nth(const List *list, int n)
Definition: list.c:410
AttrNumber resno
Definition: primnodes.h:1331
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
TypeFuncClass
Definition: funcapi.h:150
#define lnext(lc)
Definition: pg_list.h:105
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
static void expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
List * lappend(List *list, void *datum)
Definition: list.c:128
static char * label
Definition: pg_basebackup.c:83
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:998
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:999
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
List * functions
Definition: parsenodes.h:931
Expr * expr
Definition: primnodes.h:1330
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
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:882
Query * subquery
Definition: parsenodes.h:900
List * funccoltypes
Definition: parsenodes.h:997
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:961
List * coltypes
Definition: parsenodes.h:953
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
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 
)
static

Definition at line 2307 of file parse_relation.c.

References Assert, tupleDesc::attrs, Alias::colnames, i, INT4OID, InvalidOid, label, lappend(), lfirst, list_head(), lnext, Var::location, makeNullConst(), makeString(), makeVar(), NameStr, pstrdup(), and strVal.

Referenced by expandRelation(), and expandRTE().

2311 {
2312  ListCell *aliascell = list_head(eref->colnames);
2313  int varattno;
2314 
2315  if (colnames)
2316  {
2317  int i;
2318 
2319  for (i = 0; i < offset; i++)
2320  {
2321  if (aliascell)
2322  aliascell = lnext(aliascell);
2323  }
2324  }
2325 
2326  Assert(count <= tupdesc->natts);
2327  for (varattno = 0; varattno < count; varattno++)
2328  {
2329  Form_pg_attribute attr = tupdesc->attrs[varattno];
2330 
2331  if (attr->attisdropped)
2332  {
2333  if (include_dropped)
2334  {
2335  if (colnames)
2336  *colnames = lappend(*colnames, makeString(pstrdup("")));
2337  if (colvars)
2338  {
2339  /*
2340  * can't use atttypid here, but it doesn't really matter
2341  * what type the Const claims to be.
2342  */
2343  *colvars = lappend(*colvars,
2345  }
2346  }
2347  if (aliascell)
2348  aliascell = lnext(aliascell);
2349  continue;
2350  }
2351 
2352  if (colnames)
2353  {
2354  char *label;
2355 
2356  if (aliascell)
2357  {
2358  label = strVal(lfirst(aliascell));
2359  aliascell = lnext(aliascell);
2360  }
2361  else
2362  {
2363  /* If we run out of aliases, use the underlying name */
2364  label = NameStr(attr->attname);
2365  }
2366  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2367  }
2368 
2369  if (colvars)
2370  {
2371  Var *varnode;
2372 
2373  varnode = makeVar(rtindex, varattno + offset + 1,
2374  attr->atttypid, attr->atttypmod,
2375  attr->attcollation,
2376  sublevels_up);
2377  varnode->location = location;
2378 
2379  *colvars = lappend(*colvars, varnode);
2380  }
2381  }
2382 }
Value * makeString(char *str)
Definition: value.c:53
List * colnames
Definition: primnodes.h:42
char * pstrdup(const char *in)
Definition: mcxt.c:1165
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define INT4OID
Definition: pg_type.h:316
#define strVal(v)
Definition: value.h:54
Definition: primnodes.h:141
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
int location
Definition: primnodes.h:156
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
#define lnext(lc)
Definition: pg_list.h:105
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
List * lappend(List *list, void *datum)
Definition: list.c:128
static char * label
Definition: pg_basebackup.c:83
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
int i
#define NameStr(name)
Definition: c.h:495
RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 2804 of file parse_relation.c.

References lfirst, NULL, Query::rowMarks, and RowMarkClause::rti.

Referenced by AcquireRewriteLocks(), applyLockingClause(), ApplyRetrieveRule(), fireRIRrules(), and ScanQueryForLocks().

2805 {
2806  ListCell *l;
2807 
2808  foreach(l, qry->rowMarks)
2809  {
2810  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
2811 
2812  if (rc->rti == rtindex)
2813  return rc;
2814  }
2815  return NULL;
2816 }
List * rowMarks
Definition: parsenodes.h:152
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2661 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, tupleDesc::attrs, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funccolcount, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), NULL, ObjectIdGetDatum, ReleaseSysCache(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2, and TYPEFUNC_COMPOSITE.

Referenced by AcquireRewriteLocks().

2662 {
2663  bool result;
2664 
2665  switch (rte->rtekind)
2666  {
2667  case RTE_RELATION:
2668  {
2669  /*
2670  * Plain relation RTE --- get the attribute's catalog entry
2671  */
2672  HeapTuple tp;
2673  Form_pg_attribute att_tup;
2674 
2675  tp = SearchSysCache2(ATTNUM,
2676  ObjectIdGetDatum(rte->relid),
2677  Int16GetDatum(attnum));
2678  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2679  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2680  attnum, rte->relid);
2681  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2682  result = att_tup->attisdropped;
2683  ReleaseSysCache(tp);
2684  }
2685  break;
2686  case RTE_SUBQUERY:
2687  case RTE_VALUES:
2688  case RTE_CTE:
2689  /* Subselect, Values, CTE RTEs never have dropped columns */
2690  result = false;
2691  break;
2692  case RTE_JOIN:
2693  {
2694  /*
2695  * A join RTE would not have dropped columns when constructed,
2696  * but one in a stored rule might contain columns that were
2697  * dropped from the underlying tables, if said columns are
2698  * nowhere explicitly referenced in the rule. This will be
2699  * signaled to us by a null pointer in the joinaliasvars list.
2700  */
2701  Var *aliasvar;
2702 
2703  if (attnum <= 0 ||
2704  attnum > list_length(rte->joinaliasvars))
2705  elog(ERROR, "invalid varattno %d", attnum);
2706  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
2707 
2708  result = (aliasvar == NULL);
2709  }
2710  break;
2711  case RTE_FUNCTION:
2712  {
2713  /* Function RTE */
2714  ListCell *lc;
2715  int atts_done = 0;
2716 
2717  /*
2718  * Dropped attributes are only possible with functions that
2719  * return named composite types. In such a case we have to
2720  * look up the result type to see if it currently has this
2721  * column dropped. So first, loop over the funcs until we
2722  * find the one that covers the requested column.
2723  */
2724  foreach(lc, rte->functions)
2725  {
2726  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2727 
2728  if (attnum > atts_done &&
2729  attnum <= atts_done + rtfunc->funccolcount)
2730  {
2731  TypeFuncClass functypclass;
2732  Oid funcrettype;
2733  TupleDesc tupdesc;
2734 
2735  functypclass = get_expr_result_type(rtfunc->funcexpr,
2736  &funcrettype,
2737  &tupdesc);
2738  if (functypclass == TYPEFUNC_COMPOSITE)
2739  {
2740  /* Composite data type, e.g. a table's row type */
2741  Form_pg_attribute att_tup;
2742 
2743  Assert(tupdesc);
2744  Assert(attnum - atts_done <= tupdesc->natts);
2745  att_tup = tupdesc->attrs[attnum - atts_done - 1];
2746  return att_tup->attisdropped;
2747  }
2748  /* Otherwise, it can't have any dropped columns */
2749  return false;
2750  }
2751  atts_done += rtfunc->funccolcount;
2752  }
2753 
2754  /* If we get here, must be looking for the ordinality column */
2755  if (rte->funcordinality && attnum == atts_done + 1)
2756  return false;
2757 
2758  /* this probably can't happen ... */
2759  ereport(ERROR,
2760  (errcode(ERRCODE_UNDEFINED_COLUMN),
2761  errmsg("column %d of relation \"%s\" does not exist",
2762  attnum,
2763  rte->eref->aliasname)));
2764  result = false; /* keep compiler quiet */
2765  }
2766  break;
2767  default:
2768  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2769  result = false; /* keep compiler quiet */
2770  }
2771 
2772  return result;
2773 }
List * joinaliasvars
Definition: parsenodes.h:921
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define Int16GetDatum(X)
Definition: postgres.h:459
int errcode(int sqlerrcode)
Definition: elog.c:575
bool funcordinality
Definition: parsenodes.h:932
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:141
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
TypeFuncClass
Definition: funcapi.h:150
#define ereport(elevel, rest)
Definition: elog.h:122
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
List * functions
Definition: parsenodes.h:931
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:882
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:961
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:151
char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2447 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Alias::colnames, elog, RangeTblEntry::eref, ERROR, get_relid_attribute_name(), InvalidAttrNumber, list_length(), list_nth(), NULL, RangeTblEntry::relid, RTE_RELATION, RangeTblEntry::rtekind, and strVal.

Referenced by check_ungrouped_columns_walker(), get_name_for_var_field(), get_variable(), and print_expr().

2448 {
2449  if (attnum == InvalidAttrNumber)
2450  return "*";
2451 
2452  /*
2453  * If there is a user-written column alias, use it.
2454  */
2455  if (rte->alias &&
2456  attnum > 0 && attnum <= list_length(rte->alias->colnames))
2457  return strVal(list_nth(rte->alias->colnames, attnum - 1));
2458 
2459  /*
2460  * If the RTE is a relation, go to the system catalogs not the
2461  * eref->colnames list. This is a little slower but it will give the
2462  * right answer if the column has been renamed since the eref list was
2463  * built (which can easily happen for rules).
2464  */
2465  if (rte->rtekind == RTE_RELATION)
2466  return get_relid_attribute_name(rte->relid, attnum);
2467 
2468  /*
2469  * Otherwise use the column name from eref. There should always be one.
2470  */
2471  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
2472  return strVal(list_nth(rte->eref->colnames, attnum - 1));
2473 
2474  /* else caller gave us a bogus attnum */
2475  elog(ERROR, "invalid attnum %d for rangetable entry %s",
2476  attnum, rte->eref->aliasname);
2477  return NULL; /* keep compiler quiet */
2478 }
Alias * alias
Definition: parsenodes.h:960
List * colnames
Definition: primnodes.h:42
#define strVal(v)
Definition: value.h:54
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
char * get_relid_attribute_name(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:801
#define NULL
Definition: c.h:226
char * aliasname
Definition: primnodes.h:41
static int list_length(const List *l)
Definition: pg_list.h:89
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:882
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:961
void get_rte_attribute_type ( RangeTblEntry rte,
AttrNumber  attnum,
Oid vartype,
int32 vartypmod,
Oid varcollid 
)

Definition at line 2485 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, tupleDesc::attrs, RangeTblEntry::colcollations, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), get_rel_name(), get_tle_by_resno(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum, INT8OID, InvalidOid, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), list_nth_int(), list_nth_oid(), NameStr, NULL, ObjectIdGetDatum, ReleaseSysCache(), RangeTblEntry::relid, TargetEntry::resjunk, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_RELATION, RTE_SUBQUERY, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2, RangeTblEntry::subquery, Query::targetList, TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by make_var().

2487 {
2488  switch (rte->rtekind)
2489  {
2490  case RTE_RELATION:
2491  {
2492  /* Plain relation RTE --- get the attribute's type info */
2493  HeapTuple tp;
2494  Form_pg_attribute att_tup;
2495 
2496  tp = SearchSysCache2(ATTNUM,
2497  ObjectIdGetDatum(rte->relid),
2498  Int16GetDatum(attnum));
2499  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2500  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2501  attnum, rte->relid);
2502  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2503 
2504  /*
2505  * If dropped column, pretend it ain't there. See notes in
2506  * scanRTEForColumn.
2507  */
2508  if (att_tup->attisdropped)
2509  ereport(ERROR,
2510  (errcode(ERRCODE_UNDEFINED_COLUMN),
2511  errmsg("column \"%s\" of relation \"%s\" does not exist",
2512  NameStr(att_tup->attname),
2513  get_rel_name(rte->relid))));
2514  *vartype = att_tup->atttypid;
2515  *vartypmod = att_tup->atttypmod;
2516  *varcollid = att_tup->attcollation;
2517  ReleaseSysCache(tp);
2518  }
2519  break;
2520  case RTE_SUBQUERY:
2521  {
2522  /* Subselect RTE --- get type info from subselect's tlist */
2524  attnum);
2525 
2526  if (te == NULL || te->resjunk)
2527  elog(ERROR, "subquery %s does not have attribute %d",
2528  rte->eref->aliasname, attnum);
2529  *vartype = exprType((Node *) te->expr);
2530  *vartypmod = exprTypmod((Node *) te->expr);
2531  *varcollid = exprCollation((Node *) te->expr);
2532  }
2533  break;
2534  case RTE_FUNCTION:
2535  {
2536  /* Function RTE */
2537  ListCell *lc;
2538  int atts_done = 0;
2539 
2540  /* Identify which function covers the requested column */
2541  foreach(lc, rte->functions)
2542  {
2543  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2544 
2545  if (attnum > atts_done &&
2546  attnum <= atts_done + rtfunc->funccolcount)
2547  {
2548  TypeFuncClass functypclass;
2549  Oid funcrettype;
2550  TupleDesc tupdesc;
2551 
2552  attnum -= atts_done; /* now relative to this func */
2553  functypclass = get_expr_result_type(rtfunc->funcexpr,
2554  &funcrettype,
2555  &tupdesc);
2556 
2557  if (functypclass == TYPEFUNC_COMPOSITE)
2558  {
2559  /* Composite data type, e.g. a table's row type */
2560  Form_pg_attribute att_tup;
2561 
2562  Assert(tupdesc);
2563  Assert(attnum <= tupdesc->natts);
2564  att_tup = tupdesc->attrs[attnum - 1];
2565 
2566  /*
2567  * If dropped column, pretend it ain't there. See
2568  * notes in scanRTEForColumn.
2569  */
2570  if (att_tup->attisdropped)
2571  ereport(ERROR,
2572  (errcode(ERRCODE_UNDEFINED_COLUMN),
2573  errmsg("column \"%s\" of relation \"%s\" does not exist",
2574  NameStr(att_tup->attname),
2575  rte->eref->aliasname)));
2576  *vartype = att_tup->atttypid;
2577  *vartypmod = att_tup->atttypmod;
2578  *varcollid = att_tup->attcollation;
2579  }
2580  else if (functypclass == TYPEFUNC_SCALAR)
2581  {
2582  /* Base data type, i.e. scalar */
2583  *vartype = funcrettype;
2584  *vartypmod = -1;
2585  *varcollid = exprCollation(rtfunc->funcexpr);
2586  }
2587  else if (functypclass == TYPEFUNC_RECORD)
2588  {
2589  *vartype = list_nth_oid(rtfunc->funccoltypes,
2590  attnum - 1);
2591  *vartypmod = list_nth_int(rtfunc->funccoltypmods,
2592  attnum - 1);
2593  *varcollid = list_nth_oid(rtfunc->funccolcollations,
2594  attnum - 1);
2595  }
2596  else
2597  {
2598  /*
2599  * addRangeTableEntryForFunction should've caught
2600  * this
2601  */
2602  elog(ERROR, "function in FROM has unsupported return type");
2603  }
2604  return;
2605  }
2606  atts_done += rtfunc->funccolcount;
2607  }
2608 
2609  /* If we get here, must be looking for the ordinality column */
2610  if (rte->funcordinality && attnum == atts_done + 1)
2611  {
2612  *vartype = INT8OID;
2613  *vartypmod = -1;
2614  *varcollid = InvalidOid;
2615  return;
2616  }
2617 
2618  /* this probably can't happen ... */
2619  ereport(ERROR,
2620  (errcode(ERRCODE_UNDEFINED_COLUMN),
2621  errmsg("column %d of relation \"%s\" does not exist",
2622  attnum,
2623  rte->eref->aliasname)));
2624  }
2625  break;
2626  case RTE_JOIN:
2627  {
2628  /*
2629  * Join RTE --- get type info from join RTE's alias variable
2630  */
2631  Node *aliasvar;
2632 
2633  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
2634  aliasvar = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
2635  Assert(aliasvar != NULL);
2636  *vartype = exprType(aliasvar);
2637  *vartypmod = exprTypmod(aliasvar);
2638  *varcollid = exprCollation(aliasvar);
2639  }
2640  break;
2641  case RTE_VALUES:
2642  case RTE_CTE:
2643  {
2644  /* VALUES or CTE RTE --- get type info from lists in the RTE */
2645  Assert(attnum > 0 && attnum <= list_length(rte->coltypes));
2646  *vartype = list_nth_oid(rte->coltypes, attnum - 1);
2647  *vartypmod = list_nth_int(rte->coltypmods, attnum - 1);
2648  *varcollid = list_nth_oid(rte->colcollations, attnum - 1);
2649  }
2650  break;
2651  default:
2652  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2653  }
2654 }
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
List * joinaliasvars
Definition: parsenodes.h:921
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
List * coltypmods
Definition: parsenodes.h:954
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define Int16GetDatum(X)
Definition: postgres.h:459
Definition: nodes.h:508
int errcode(int sqlerrcode)
Definition: elog.c:575
bool funcordinality
Definition: parsenodes.h:932
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
List * colcollations
Definition: parsenodes.h:955
List * targetList
Definition: parsenodes.h:131
bool resjunk
Definition: primnodes.h:1337
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
TypeFuncClass
Definition: funcapi.h:150
#define ereport(elevel, rest)
Definition: elog.h:122
int list_nth_int(const List *list, int n)
Definition: list.c:421
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:998
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:999
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
List * functions
Definition: parsenodes.h:931
Expr * expr
Definition: primnodes.h:1330
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
RTEKind rtekind
Definition: parsenodes.h:882
Query * subquery
Definition: parsenodes.h:900
int errmsg(const char *fmt,...)
Definition: elog.c:797
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
List * funccoltypes
Definition: parsenodes.h:997
#define NameStr(name)
Definition: c.h:495
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:961
List * coltypes
Definition: parsenodes.h:953
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1694
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:151
CommonTableExpr* GetCTEForRTE ( ParseState pstate,
RangeTblEntry rte,
int  rtelevelsup 
)

Definition at line 496 of file parse_relation.c.

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

Referenced by expandRecordVariable(), and markTargetListOrigin().

497 {
498  Index levelsup;
499  ListCell *lc;
500 
501  /* Determine RTE's levelsup if caller didn't know it */
502  if (rtelevelsup < 0)
503  (void) RTERangeTablePosn(pstate, rte, &rtelevelsup);
504 
505  Assert(rte->rtekind == RTE_CTE);
506  levelsup = rte->ctelevelsup + rtelevelsup;
507  while (levelsup-- > 0)
508  {
509  pstate = pstate->parentParseState;
510  if (!pstate) /* shouldn't happen */
511  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
512  }
513  foreach(lc, pstate->p_ctenamespace)
514  {
515  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
516 
517  if (strcmp(cte->ctename, rte->ctename) == 0)
518  return cte;
519  }
520  /* shouldn't happen */
521  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
522  return NULL; /* keep compiler quiet */
523 }
#define ERROR
Definition: elog.h:43
struct ParseState * parentParseState
Definition: parse_node.h:166
unsigned int Index
Definition: c.h:362
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
Index ctelevelsup
Definition: parsenodes.h:943
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
RTEKind rtekind
Definition: parsenodes.h:882
char * ctename
Definition: parsenodes.h:942
List * p_ctenamespace
Definition: parse_node.h:175
#define elog
Definition: elog.h:219
RangeTblEntry* GetRTEByRangeTablePosn ( ParseState pstate,
int  varno,
int  sublevels_up 
)

Definition at line 475 of file parse_relation.c.

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

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

478 {
479  while (sublevels_up-- > 0)
480  {
481  pstate = pstate->parentParseState;
482  Assert(pstate != NULL);
483  }
484  Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
485  return rt_fetch(varno, pstate->p_rtable);
486 }
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
struct ParseState * parentParseState
Definition: parse_node.h:166
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static int list_length(const List *l)
Definition: pg_list.h:89
List * p_rtable
Definition: parse_node.h:168
static bool isFutureCTE ( ParseState pstate,
const char *  refname 
)
static

Definition at line 267 of file parse_relation.c.

References CommonTableExpr::ctename, lfirst, NULL, ParseState::p_future_ctes, and ParseState::parentParseState.

Referenced by parserOpenTable().

268 {
269  for (; pstate != NULL; pstate = pstate->parentParseState)
270  {
271  ListCell *lc;
272 
273  foreach(lc, pstate->p_future_ctes)
274  {
275  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
276 
277  if (strcmp(cte->ctename, refname) == 0)
278  return true;
279  }
280  }
281  return false;
282 }
struct ParseState * parentParseState
Definition: parse_node.h:166
List * p_future_ctes
Definition: parse_node.h:176
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

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

1892 {
1893  ListCell *l;
1894 
1895  /*
1896  * If we are in a subquery specified as locked FOR UPDATE/SHARE from
1897  * parent level, then act as though there's a generic FOR UPDATE here.
1898  */
1899  if (pstate->p_locked_from_parent)
1900  return true;
1901 
1902  foreach(l, pstate->p_locking_clause)
1903  {
1904  LockingClause *lc = (LockingClause *) lfirst(l);
1905 
1906  if (lc->lockedRels == NIL)
1907  {
1908  /* all tables used in query */
1909  return true;
1910  }
1911  else
1912  {
1913  /* just the named tables */
1914  ListCell *l2;
1915 
1916  foreach(l2, lc->lockedRels)
1917  {
1918  RangeVar *thisrel = (RangeVar *) lfirst(l2);
1919 
1920  if (strcmp(refname, thisrel->relname) == 0)
1921  return true;
1922  }
1923  }
1924  }
1925  return false;
1926 }
List * lockedRels
Definition: parsenodes.h:693
#define NIL
Definition: pg_list.h:69
bool p_locked_from_parent
Definition: parse_node.h:186
char * relname
Definition: primnodes.h:67
List * p_locking_clause
Definition: parse_node.h:185
#define lfirst(lc)
Definition: pg_list.h:106
bool isQueryUsingTempRelation ( Query query)

Definition at line 3072 of file parse_relation.c.

References isQueryUsingTempRelation_walker(), and NULL.

Referenced by DefineView(), and transformCreateTableAsStmt().

3073 {
3074  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3075 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
Definition: nodes.h:508
#define NULL
Definition: c.h:226
static bool isQueryUsingTempRelation_walker ( Node node,
void *  context 
)
static

Definition at line 3078 of file parse_relation.c.

References AccessShareLock, expression_tree_walker(), heap_close, heap_open(), IsA, lfirst, NULL, QTW_IGNORE_JOINALIASES, query_tree_walker(), RelationData::rd_rel, RangeTblEntry::relid, RELPERSISTENCE_TEMP, Query::rtable, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by isQueryUsingTempRelation().

3079 {
3080  if (node == NULL)
3081  return false;
3082 
3083  if (IsA(node, Query))
3084  {
3085  Query *query = (Query *) node;
3086  ListCell *rtable;
3087 
3088  foreach(rtable, query->rtable)
3089  {
3090  RangeTblEntry *rte = lfirst(rtable);
3091 
3092  if (rte->rtekind == RTE_RELATION)
3093  {
3094  Relation rel = heap_open(rte->relid, AccessShareLock);
3095  char relpersistence = rel->rd_rel->relpersistence;
3096 
3098  if (relpersistence == RELPERSISTENCE_TEMP)
3099  return true;
3100  }
3101  }
3102 
3103  return query_tree_walker(query,
3105  context,
3107  }
3108 
3109  return expression_tree_walker(node,
3111  context);
3112 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2238
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:113
List * rtable
Definition: parsenodes.h:128
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1852
RTEKind rtekind
Definition: parsenodes.h:882
#define RELPERSISTENCE_TEMP
Definition: pg_class.h:172
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
static void markRTEForSelectPriv ( ParseState pstate,
RangeTblEntry rte,
int  rtindex,
AttrNumber  col 
)
static

Definition at line 888 of file parse_relation.c.

References ACL_SELECT, Assert, bms_add_member(), castNode, elog, ERROR, FirstLowInvalidHeapAttributeNumber, InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, JoinExpr::larg, list_length(), list_nth(), markVarForSelectPriv(), nodeTag, NULL, ParseState::p_joinexprs, ParseState::p_rtable, JoinExpr::rarg, RangeTblEntry::requiredPerms, rt_fetch, RTE_JOIN, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, and strip_implicit_coercions().

Referenced by markVarForSelectPriv().

890 {
891  if (rte == NULL)
892  rte = rt_fetch(rtindex, pstate->p_rtable);
893 
894  if (rte->rtekind == RTE_RELATION)
895  {
896  /* Make sure the rel as a whole is marked for SELECT access */
897  rte->requiredPerms |= ACL_SELECT;
898  /* Must offset the attnum to fit in a bitmapset */
901  }
902  else if (rte->rtekind == RTE_JOIN)
903  {
904  if (col == InvalidAttrNumber)
905  {
906  /*
907  * A whole-row reference to a join has to be treated as whole-row
908  * references to the two inputs.
909  */
910  JoinExpr *j;
911 
912  if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
913  j = castNode(JoinExpr, list_nth(pstate->p_joinexprs, rtindex - 1));
914  else
915  j = NULL;
916  if (j == NULL)
917  elog(ERROR, "could not find JoinExpr for whole-row reference");
918 
919  /* Note: we can't see FromExpr here */
920  if (IsA(j->larg, RangeTblRef))
921  {
922  int varno = ((RangeTblRef *) j->larg)->rtindex;
923 
925  }
926  else if (IsA(j->larg, JoinExpr))
927  {
928  int varno = ((JoinExpr *) j->larg)->rtindex;
929 
931  }
932  else
933  elog(ERROR, "unrecognized node type: %d",
934  (int) nodeTag(j->larg));
935  if (IsA(j->rarg, RangeTblRef))
936  {
937  int varno = ((RangeTblRef *) j->rarg)->rtindex;
938 
940  }
941  else if (IsA(j->rarg, JoinExpr))
942  {
943  int varno = ((JoinExpr *) j->rarg)->rtindex;
944 
946  }
947  else
948  elog(ERROR, "unrecognized node type: %d",
949  (int) nodeTag(j->rarg));
950  }
951  else
952  {
953  /*
954  * Regular join attribute, look at the alias-variable list.
955  *
956  * The aliasvar could be either a Var or a COALESCE expression,
957  * but in the latter case we should already have marked the two
958  * referent variables as being selected, due to their use in the
959  * JOIN clause. So we need only be concerned with the Var case.
960  * But we do need to drill down through implicit coercions.
961  */
962  Var *aliasvar;
963 
964  Assert(col > 0 && col <= list_length(rte->joinaliasvars));
965  aliasvar = (Var *) list_nth(rte->joinaliasvars, col - 1);
966  aliasvar = (Var *) strip_implicit_coercions((Node *) aliasvar);
967  if (aliasvar && IsA(aliasvar, Var))
968  markVarForSelectPriv(pstate, aliasvar, NULL);
969  }
970  }
971  /* other RTE types don't require privilege marking */
972 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
List * joinaliasvars
Definition: parsenodes.h:921
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
Definition: nodes.h:508
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
AclMode requiredPerms
Definition: parsenodes.h:965
Definition: primnodes.h:141
Node * larg
Definition: primnodes.h:1413
Bitmapset * selectedCols
Definition: parsenodes.h:967
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
#define ACL_SELECT
Definition: parsenodes.h:66
Node * rarg
Definition: primnodes.h:1414
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static int list_length(const List *l)
Definition: pg_list.h:89
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
#define InvalidAttrNumber
Definition: attnum.h:23
#define nodeTag(nodeptr)
Definition: nodes.h:513
RTEKind rtekind
Definition: parsenodes.h:882
#define elog
Definition: elog.h:219
List * p_joinexprs
Definition: parse_node.h:169
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:609
List * p_rtable
Definition: parse_node.h:168
void markVarForSelectPriv ( ParseState pstate,
Var var,
RangeTblEntry rte 
)

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

983 {
984  Index lv;
985 
986  Assert(IsA(var, Var));
987  /* Find the appropriate pstate if it's an uplevel Var */
988  for (lv = 0; lv < var->varlevelsup; lv++)
989  pstate = pstate->parentParseState;
990  markRTEForSelectPriv(pstate, rte, var->varno, var->varattno);
991 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
Index varlevelsup
Definition: primnodes.h:151
AttrNumber varattno
Definition: primnodes.h:146
Definition: primnodes.h:141
static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
Index varno
Definition: primnodes.h:144
struct ParseState * parentParseState
Definition: parse_node.h:166
unsigned int Index
Definition: c.h:362
#define Assert(condition)
Definition: c.h:671
Relation parserOpenTable ( ParseState pstate,
const RangeVar relation,
int  lockmode 
)

Definition at line 1125 of file parse_relation.c.

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

Referenced by addRangeTableEntry(), and setTargetTable().

1126 {
1127  Relation rel;
1128  ParseCallbackState pcbstate;
1129 
1130  setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1131  rel = heap_openrv_extended(relation, lockmode, true);
1132  if (rel == NULL)
1133  {
1134  if (relation->schemaname)
1135  ereport(ERROR,
1137  errmsg("relation \"%s.%s\" does not exist",
1138  relation->schemaname, relation->relname)));
1139  else
1140  {
1141  /*
1142  * An unqualified name might have been meant as a reference to
1143  * some not-yet-in-scope CTE. The bare "does not exist" message
1144  * has proven remarkably unhelpful for figuring out such problems,
1145  * so we take pains to offer a specific hint.
1146  */
1147  if (isFutureCTE(pstate, relation->relname))
1148  ereport(ERROR,
1150  errmsg("relation \"%s\" does not exist",
1151  relation->relname),
1152  errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1153  relation->relname),
1154  errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1155  else
1156  ereport(ERROR,
1158  errmsg("relation \"%s\" does not exist",
1159  relation->relname)));
1160  }
1161  }
1163  return rel;
1164 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
static bool isFutureCTE(ParseState *pstate, const char *refname)
int errcode(int sqlerrcode)
Definition: elog.c:575
char * schemaname
Definition: primnodes.h:66
int location
Definition: primnodes.h:72
char * relname
Definition: primnodes.h:67
Relation heap_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
Definition: heapam.c:1344
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:159
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:143
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
RangeTblEntry* refnameRangeTblEntry ( ParseState pstate,
const char *  schemaname,
const char *  refname,
int  location,
int *  sublevels_up 
)

Definition at line 82 of file parse_relation.c.

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

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

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

Definition at line 442 of file parse_relation.c.

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

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

443 {
444  int index;
445  ListCell *l;
446 
447  if (sublevels_up)
448  *sublevels_up = 0;
449 
450  while (pstate != NULL)
451  {
452  index = 1;
453  foreach(l, pstate->p_rtable)
454  {
455  if (rte == (RangeTblEntry *) lfirst(l))
456  return index;
457  index++;
458  }
459  pstate = pstate->parentParseState;
460  if (sublevels_up)
461  (*sublevels_up)++;
462  else
463  break;
464  }
465 
466  elog(ERROR, "RTE not found (internal error)");
467  return 0; /* keep compiler quiet */
468 }
Definition: type.h:90
#define ERROR
Definition: elog.h:43
struct ParseState * parentParseState
Definition: parse_node.h:166
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
#define elog
Definition: elog.h:219
List * p_rtable
Definition: parse_node.h:168
CommonTableExpr* scanNameSpaceForCTE ( ParseState pstate,
const char *  refname,
Index ctelevelsup 
)

Definition at line 236 of file parse_relation.c.

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

Referenced by searchRangeTableForRel(), and transformFromClauseItem().

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

Definition at line 153 of file parse_relation.c.

References Alias::aliasname, check_lateral_ref_ok(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, lfirst, NULL, ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, and parser_errposition().

Referenced by refnameRangeTblEntry().

154 {
155  RangeTblEntry *result = NULL;
156  ListCell *l;
157 
158  foreach(l, pstate->p_namespace)
159  {
161  RangeTblEntry *rte = nsitem->p_rte;
162 
163  /* Ignore columns-only items */
164  if (!nsitem->p_rel_visible)
165  continue;
166  /* If not inside LATERAL, ignore lateral-only items */
167  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
168  continue;
169 
170  if (strcmp(rte->eref->aliasname, refname) == 0)
171  {
172  if (result)
173  ereport(ERROR,
174  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
175  errmsg("table reference \"%s\" is ambiguous",
176  refname),
177  parser_errposition(pstate, location)));
178  check_lateral_ref_ok(pstate, nsitem, location);
179  result = rte;
180  }
181  }
182  return result;
183 }
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:238
#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:172
#define ereport(elevel, rest)
Definition: elog.h:122
bool p_lateral_active
Definition: parse_node.h:174
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:961
static RangeTblEntry * scanNameSpaceForRelid ( ParseState pstate,
Oid  relid,
int  location 
)
static

Definition at line 194 of file parse_relation.c.

References RangeTblEntry::alias, check_lateral_ref_ok(), ereport, errcode(), errmsg(), ERROR, lfirst, NULL, ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_only, ParseState::p_namespace, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, parser_errposition(), RangeTblEntry::relid, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by refnameRangeTblEntry().

195 {
196  RangeTblEntry *result = NULL;
197  ListCell *l;
198 
199  foreach(l, pstate->p_namespace)
200  {
202  RangeTblEntry *rte = nsitem->p_rte;
203 
204  /* Ignore columns-only items */
205  if (!nsitem->p_rel_visible)
206  continue;
207  /* If not inside LATERAL, ignore lateral-only items */
208  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
209  continue;
210 
211  /* yes, the test for alias == NULL should be there... */
212  if (rte->rtekind == RTE_RELATION &&
213  rte->relid == relid &&
214  rte->alias == NULL)
215  {
216  if (result)
217  ereport(ERROR,
218  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
219  errmsg("table reference %u is ambiguous",
220  relid),
221  parser_errposition(pstate, location)));
222  check_lateral_ref_ok(pstate, nsitem, location);
223  result = rte;
224  }
225  }
226  return result;
227 }
Alias * alias
Definition: parsenodes.h:960
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:238
#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:172
#define ereport(elevel, rest)
Definition: elog.h:122
bool p_lateral_active
Definition: parse_node.h:174
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
RTEKind rtekind
Definition: parsenodes.h:882
int errmsg(const char *fmt,...)
Definition: elog.c:797
Node* scanRTEForColumn ( ParseState pstate,
RangeTblEntry rte,
char *  colname,
int  location,
int  fuzzy_rte_penalty,
FuzzyAttrMatchState fuzzystate 
)

Definition at line 634 of file parse_relation.c.

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

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

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

Definition at line 810 of file parse_relation.c.

References Alias::aliasname, FuzzyAttrMatchState::distance, RangeTblEntry::eref, FuzzyAttrMatchState::first, InvalidAttrNumber, lfirst, MAX_FUZZY_DISTANCE, NULL, ParseState::p_rtable, palloc(), ParseState::parentParseState, FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, RTE_JOIN, RangeTblEntry::rtekind, scanRTEForColumn(), FuzzyAttrMatchState::second, and varstr_levenshtein_less_equal().

Referenced by errorMissingColumn().

812 {
813  ParseState *orig_pstate = pstate;
814  FuzzyAttrMatchState *fuzzystate = palloc(sizeof(FuzzyAttrMatchState));
815 
816  fuzzystate->distance = MAX_FUZZY_DISTANCE + 1;
817  fuzzystate->rfirst = NULL;
818  fuzzystate->rsecond = NULL;
819  fuzzystate->first = InvalidAttrNumber;
820  fuzzystate->second = InvalidAttrNumber;
821 
822  while (pstate != NULL)
823  {
824  ListCell *l;
825 
826  foreach(l, pstate->p_rtable)
827  {
828  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
829  int fuzzy_rte_penalty = 0;
830 
831  /*
832  * Typically, it is not useful to look for matches within join
833  * RTEs; they effectively duplicate other RTEs for our purposes,
834  * and if a match is chosen from a join RTE, an unhelpful alias is
835  * displayed in the final diagnostic message.
836  */
837  if (rte->rtekind == RTE_JOIN)
838  continue;
839 
840  /*
841  * If the user didn't specify an alias, then matches against one
842  * RTE are as good as another. But if the user did specify an
843  * alias, then we want at least a fuzzy - and preferably an exact
844  * - match for the range table entry.
845  */
846  if (alias != NULL)
847  fuzzy_rte_penalty =
848  varstr_levenshtein_less_equal(alias, strlen(alias),
849  rte->eref->aliasname,
850  strlen(rte->eref->aliasname),
851  1, 1, 1,
852  MAX_FUZZY_DISTANCE + 1,
853  true);
854 
855  /*
856  * Scan for a matching column; if we find an exact match, we're
857  * done. Otherwise, update fuzzystate.
858  */
859  if (scanRTEForColumn(orig_pstate, rte, colname, location,
860  fuzzy_rte_penalty, fuzzystate)
861  && fuzzy_rte_penalty == 0)
862  {
863  fuzzystate->rfirst = rte;
864  fuzzystate->first = InvalidAttrNumber;
865  fuzzystate->rsecond = NULL;
866  fuzzystate->second = InvalidAttrNumber;
867  return fuzzystate;
868  }
869  }
870 
871  pstate = pstate->parentParseState;
872  }
873 
874  return fuzzystate;
875 }
Node * scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
RangeTblEntry * rfirst
#define MAX_FUZZY_DISTANCE
struct ParseState * parentParseState
Definition: parse_node.h:166
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
RangeTblEntry * rsecond
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:882
int varstr_levenshtein_less_equal(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, int max_d, bool trusted)
void * palloc(Size size)
Definition: mcxt.c:891
Alias * eref
Definition: parsenodes.h:961
List * p_rtable
Definition: parse_node.h:168
static RangeTblEntry* searchRangeTableForRel ( ParseState pstate,
RangeVar relation 
)
static

Definition at line 300 of file parse_relation.c.

References Alias::aliasname, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, RangeTblEntry::eref, InvalidOid, lfirst, NoLock, NULL, OidIsValid, ParseState::p_rtable, ParseState::parentParseState, RangeVarGetRelid, RangeTblEntry::relid, RangeVar::relname, RTE_CTE, RTE_RELATION, RangeTblEntry::rtekind, scanNameSpaceForCTE(), and RangeVar::schemaname.

Referenced by errorMissingRTE().

301 {
302  const char *refname = relation->relname;
303  Oid relId = InvalidOid;
304  CommonTableExpr *cte = NULL;
305  Index ctelevelsup = 0;
306  Index levelsup;
307 
308  /*
309  * If it's an unqualified name, check for possible CTE matches. A CTE
310  * hides any real relation matches. If no CTE, look for a matching
311  * relation.
312  *
313  * NB: It's not critical that RangeVarGetRelid return the correct answer
314  * here in the face of concurrent DDL. If it doesn't, the worst case
315  * scenario is a less-clear error message. Also, the tables involved in
316  * the query are already locked, which reduces the number of cases in
317  * which surprising behavior can occur. So we do the name lookup
318  * unlocked.
319  */
320  if (!relation->schemaname)
321  cte = scanNameSpaceForCTE(pstate, refname, &ctelevelsup);
322  if (!cte)
323  relId = RangeVarGetRelid(relation, NoLock, true);
324 
325  /* Now look for RTEs matching either the relation/CTE or the alias */
326  for (levelsup = 0;
327  pstate != NULL;
328  pstate = pstate->parentParseState, levelsup++)
329  {
330  ListCell *l;
331 
332  foreach(l, pstate->p_rtable)
333  {
334  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
335 
336  if (rte->rtekind == RTE_RELATION &&
337  OidIsValid(relId) &&
338  rte->relid == relId)
339  return rte;
340  if (rte->rtekind == RTE_CTE &&
341  cte != NULL &&
342  rte->ctelevelsup + levelsup == ctelevelsup &&
343  strcmp(rte->ctename, refname) == 0)
344  return rte;
345  if (strcmp(rte->eref->aliasname, refname) == 0)
346  return rte;
347  }
348  }
349  return NULL;
350 }
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
char * schemaname
Definition: primnodes.h:66
char * relname
Definition: primnodes.h:67
#define NoLock
Definition: lockdefs.h:34
struct ParseState * parentParseState
Definition: parse_node.h:166
unsigned int Index
Definition: c.h:362
#define InvalidOid
Definition: postgres_ext.h:36
CommonTableExpr * scanNameSpaceForCTE(ParseState *pstate, const char *refname, Index *ctelevelsup)
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:41
Index ctelevelsup
Definition: parsenodes.h:943
RTEKind rtekind
Definition: parsenodes.h:882
char * ctename
Definition: parsenodes.h:942
Alias * eref
Definition: parsenodes.h:961
List * p_rtable
Definition: parse_node.h:168
static int specialAttNum ( const char *  attname)
static

Definition at line 2863 of file parse_relation.c.

References InvalidAttrNumber, NULL, and SystemAttributeByName().

Referenced by attnameAttNum(), and scanRTEForColumn().

2864 {
2865  Form_pg_attribute sysatt;
2866 
2867  sysatt = SystemAttributeByName(attname,
2868  true /* "oid" will be accepted */ );
2869  if (sysatt != NULL)
2870  return sysatt->attnum;
2871  return InvalidAttrNumber;
2872 }
Form_pg_attribute SystemAttributeByName(const char *attname, bool relhasoids)
Definition: heap.c:212
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
#define NULL
Definition: c.h:226
#define InvalidAttrNumber
Definition: attnum.h:23
static void updateFuzzyAttrMatchState ( int  fuzzy_rte_penalty,
FuzzyAttrMatchState fuzzystate,
RangeTblEntry rte,
const char *  actual,
const char *  match,
int  attnum 
)
static

Definition at line 530 of file parse_relation.c.

References AttributeNumberIsValid, FuzzyAttrMatchState::distance, FuzzyAttrMatchState::first, InvalidAttrNumber, MAX_FUZZY_DISTANCE, NULL, FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, FuzzyAttrMatchState::second, and varstr_levenshtein_less_equal().

Referenced by scanRTEForColumn().

533 {
534  int columndistance;
535  int matchlen;
536 
537  /* Bail before computing the Levenshtein distance if there's no hope. */
538  if (fuzzy_rte_penalty > fuzzystate->distance)
539  return;
540 
541  /*
542  * Outright reject dropped columns, which can appear here with apparent
543  * empty actual names, per remarks within scanRTEForColumn().
544  */
545  if (actual[0] == '\0')
546  return;
547 
548  /* Use Levenshtein to compute match distance. */
549  matchlen = strlen(match);
550  columndistance =
551  varstr_levenshtein_less_equal(actual, strlen(actual), match, matchlen,
552  1, 1, 1,
553  fuzzystate->distance + 1
554  - fuzzy_rte_penalty,
555  true);
556 
557  /*
558  * If more than half the characters are different, don't treat it as a
559  * match, to avoid making ridiculous suggestions.
560  */
561  if (columndistance > matchlen / 2)
562  return;
563 
564  /*
565  * From this point on, we can ignore the distinction between the RTE-name
566  * distance and the column-name distance.
567  */
568  columndistance += fuzzy_rte_penalty;
569 
570  /*
571  * If the new distance is less than or equal to that of the best match
572  * found so far, update fuzzystate.
573  */
574  if (columndistance < fuzzystate->distance)
575  {
576  /* Store new lowest observed distance for RTE */
577  fuzzystate->distance = columndistance;
578  fuzzystate->rfirst = rte;
579  fuzzystate->first = attnum;
580  fuzzystate->rsecond = NULL;
581  fuzzystate->second = InvalidAttrNumber;
582  }
583  else if (columndistance == fuzzystate->distance)
584  {
585  /*
586  * This match distance may equal a prior match within this same range
587  * table. When that happens, the prior match may also be given, but
588  * only if there is no more than two equally distant matches from the
589  * RTE (in turn, our caller will only accept two equally distant
590  * matches overall).
591  */
592  if (AttributeNumberIsValid(fuzzystate->second))
593  {
594  /* Too many RTE-level matches */
595  fuzzystate->rfirst = NULL;
596  fuzzystate->first = InvalidAttrNumber;
597  fuzzystate->rsecond = NULL;
598  fuzzystate->second = InvalidAttrNumber;
599  /* Clearly, distance is too low a bar (for *any* RTE) */
600  fuzzystate->distance = columndistance - 1;
601  }
602  else if (AttributeNumberIsValid(fuzzystate->first))
603  {
604  /* Record as provisional second match for RTE */
605  fuzzystate->rsecond = rte;
606  fuzzystate->second = attnum;
607  }
608  else if (fuzzystate->distance <= MAX_FUZZY_DISTANCE)
609  {
610  /*
611  * Record as provisional first match (this can occasionally occur
612  * because previous lowest distance was "too low a bar", rather
613  * than being associated with a real match)
614  */
615  fuzzystate->rfirst = rte;
616  fuzzystate->first = attnum;
617  }
618  }
619 }
RangeTblEntry * rfirst
#define MAX_FUZZY_DISTANCE
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define NULL
Definition: c.h:226
RangeTblEntry * rsecond
#define InvalidAttrNumber
Definition: attnum.h:23
int varstr_levenshtein_less_equal(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, int max_d, bool trusted)