PostgreSQL Source Code  git master
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_enr.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)
 
bool scanNameSpaceForENR (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, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
 
NodecolNameToVar (ParseState *pstate, const char *colname, bool localonly, int location)
 
static FuzzyAttrMatchStatesearchRangeTableForCol (ParseState *pstate, const char *alias, const 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)
 
RangeTblEntryaddRangeTableEntryForTableFunc (ParseState *pstate, TableFunc *tf, Alias *alias, 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)
 
RangeTblEntryaddRangeTableEntryForENR (ParseState *pstate, RangeVar *rv, bool inFromCl)
 
bool isLockedRefname (ParseState *pstate, const char *refname)
 
void addRTEtoQuery (ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
 
void 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, const char *relname, const char *colname, int location)
 
bool isQueryUsingTempRelation (Query *query)
 

Macro Definition Documentation

◆ MAX_FUZZY_DISTANCE

#define MAX_FUZZY_DISTANCE   3

Definition at line 38 of file parse_relation.c.

Referenced by searchRangeTableForCol(), and updateFuzzyAttrMatchState().

Function Documentation

◆ addRangeTableEntry()

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

Definition at line 1194 of file parse_relation.c.

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

Referenced by transformTableEntry().

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

◆ addRangeTableEntryForCTE()

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

Definition at line 1869 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes().

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

◆ addRangeTableEntryForENR()

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

Definition at line 1978 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes().

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

◆ addRangeTableEntryForFunction()

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

Definition at line 1402 of file parse_relation.c.

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

Referenced by transformRangeFunction().

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

◆ addRangeTableEntryForJoin()

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

Definition at line 1799 of file parse_relation.c.

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

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

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

◆ addRangeTableEntryForRelation()

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

Definition at line 1267 of file parse_relation.c.

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

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

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

◆ addRangeTableEntryForSubquery()

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

Definition at line 1322 of file parse_relation.c.

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

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

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

◆ addRangeTableEntryForTableFunc()

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

Definition at line 1658 of file parse_relation.c.

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

Referenced by transformRangeTableFunc().

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

◆ addRangeTableEntryForValues()

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

Definition at line 1721 of file parse_relation.c.

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

Referenced by transformInsertStmt(), and transformValuesClause().

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

◆ addRTEtoQuery()

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

Definition at line 2129 of file parse_relation.c.

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

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

2132 {
2133  if (addToJoinList)
2134  {
2135  int rtindex = RTERangeTablePosn(pstate, rte, NULL);
2137 
2138  rtr->rtindex = rtindex;
2139  pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2140  }
2141  if (addToRelNameSpace || addToVarNameSpace)
2142  {
2143  ParseNamespaceItem *nsitem;
2144 
2145  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2146  nsitem->p_rte = rte;
2147  nsitem->p_rel_visible = addToRelNameSpace;
2148  nsitem->p_cols_visible = addToVarNameSpace;
2149  nsitem->p_lateral_only = false;
2150  nsitem->p_lateral_ok = true;
2151  pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2152  }
2153 }
RangeTblEntry * p_rte
Definition: parse_node.h:247
List * p_namespace
Definition: parse_node.h:177
List * lappend(List *list, void *datum)
Definition: list.c:128
#define makeNode(_type_)
Definition: nodes.h:560
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
void * palloc(Size size)
Definition: mcxt.c:848
List * p_joinlist
Definition: parse_node.h:175

◆ attnameAttNum()

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

Definition at line 3078 of file parse_relation.c.

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

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

3079 {
3080  int i;
3081 
3082  for (i = 0; i < rd->rd_rel->relnatts; i++)
3083  {
3084  Form_pg_attribute att = TupleDescAttr(rd->rd_att, i);
3085 
3086  if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3087  return i + 1;
3088  }
3089 
3090  if (sysColOK)
3091  {
3092  if ((i = specialAttNum(attname)) != InvalidAttrNumber)
3093  {
3094  if (i != ObjectIdAttributeNumber || rd->rd_rel->relhasoids)
3095  return i;
3096  }
3097  }
3098 
3099  /* on failure */
3100  return InvalidAttrNumber;
3101 }
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
int namestrcmp(Name name, const char *str)
Definition: name.c:247
static int specialAttNum(const char *attname)
Form_pg_class rd_rel
Definition: rel.h:114
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
TupleDesc rd_att
Definition: rel.h:115
#define InvalidAttrNumber
Definition: attnum.h:23
int i

◆ attnumAttName()

Name attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3133 of file parse_relation.c.

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

Referenced by transformFkeyGetPrimaryKey().

3134 {
3135  if (attid <= 0)
3136  {
3137  Form_pg_attribute sysatt;
3138 
3139  sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
3140  return &sysatt->attname;
3141  }
3142  if (attid > rd->rd_att->natts)
3143  elog(ERROR, "invalid attribute number %d", attid);
3144  return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3145 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
Form_pg_class rd_rel
Definition: rel.h:114
int natts
Definition: tupdesc.h:79
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
TupleDesc rd_att
Definition: rel.h:115
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:200
#define elog
Definition: elog.h:219

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3175 of file parse_relation.c.

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

3176 {
3177  if (attid <= 0)
3178  {
3179  /* All system attributes are of noncollatable types. */
3180  return InvalidOid;
3181  }
3182  if (attid > rd->rd_att->natts)
3183  elog(ERROR, "invalid attribute number %d", attid);
3184  return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3185 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
int natts
Definition: tupdesc.h:79
#define ERROR
Definition: elog.h:43
TupleDesc rd_att
Definition: rel.h:115
#define InvalidOid
Definition: postgres_ext.h:36
#define elog
Definition: elog.h:219

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3155 of file parse_relation.c.

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

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

3156 {
3157  if (attid <= 0)
3158  {
3159  Form_pg_attribute sysatt;
3160 
3161  sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
3162  return sysatt->atttypid;
3163  }
3164  if (attid > rd->rd_att->natts)
3165  elog(ERROR, "invalid attribute number %d", attid);
3166  return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3167 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
Form_pg_class rd_rel
Definition: rel.h:114
int natts
Definition: tupdesc.h:79
#define ERROR
Definition: elog.h:43
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
TupleDesc rd_att
Definition: rel.h:115
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:200
#define elog
Definition: elog.h:219

◆ buildRelationAliases()

static void buildRelationAliases ( TupleDesc  tupdesc,
Alias alias,
Alias eref 
)
static

Definition at line 1030 of file parse_relation.c.

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

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

1031 {
1032  int maxattrs = tupdesc->natts;
1033  ListCell *aliaslc;
1034  int numaliases;
1035  int varattno;
1036  int numdropped = 0;
1037 
1038  Assert(eref->colnames == NIL);
1039 
1040  if (alias)
1041  {
1042  aliaslc = list_head(alias->colnames);
1043  numaliases = list_length(alias->colnames);
1044  /* We'll rebuild the alias colname list */
1045  alias->colnames = NIL;
1046  }
1047  else
1048  {
1049  aliaslc = NULL;
1050  numaliases = 0;
1051  }
1052 
1053  for (varattno = 0; varattno < maxattrs; varattno++)
1054  {
1055  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1056  Value *attrname;
1057 
1058  if (attr->attisdropped)
1059  {
1060  /* Always insert an empty string for a dropped column */
1061  attrname = makeString(pstrdup(""));
1062  if (aliaslc)
1063  alias->colnames = lappend(alias->colnames, attrname);
1064  numdropped++;
1065  }
1066  else if (aliaslc)
1067  {
1068  /* Use the next user-supplied alias */
1069  attrname = (Value *) lfirst(aliaslc);
1070  aliaslc = lnext(aliaslc);
1071  alias->colnames = lappend(alias->colnames, attrname);
1072  }
1073  else
1074  {
1075  attrname = makeString(pstrdup(NameStr(attr->attname)));
1076  /* we're done with the alias if any */
1077  }
1078 
1079  eref->colnames = lappend(eref->colnames, attrname);
1080  }
1081 
1082  /* Too many user-supplied aliases? */
1083  if (aliaslc)
1084  ereport(ERROR,
1085  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1086  errmsg("table \"%s\" has %d columns available but %d columns specified",
1087  eref->aliasname, maxattrs - numdropped, numaliases)));
1088 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
List * colnames
Definition: primnodes.h:43
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
char * pstrdup(const char *in)
Definition: mcxt.c:1076
int errcode(int sqlerrcode)
Definition: elog.c:575
int natts
Definition: tupdesc.h:79
#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:187
#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 Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
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:547

◆ check_lateral_ref_ok()

static void check_lateral_ref_ok ( ParseState pstate,
ParseNamespaceItem nsitem,
int  location 
)
static

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

437 {
438  if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
439  {
440  /* SQL:2008 demands this be an error, not an invisible item */
441  RangeTblEntry *rte = nsitem->p_rte;
442  char *refname = rte->eref->aliasname;
443 
444  ereport(ERROR,
445  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
446  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
447  refname),
448  (rte == pstate->p_target_rangetblentry) ?
449  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
450  refname) :
451  errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
452  parser_errposition(pstate, location)));
453  }
454 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
RangeTblEntry * p_rte
Definition: parse_node.h:247
#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:184
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
Alias * eref
Definition: parsenodes.h:1055

◆ checkNameSpaceConflicts()

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

Definition at line 389 of file parse_relation.c.

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

Referenced by transformFromClause(), and transformFromClauseItem().

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

◆ chooseScalarFunctionAlias()

static char* chooseScalarFunctionAlias ( Node funcexpr,
char *  funcname,
Alias alias,
int  nfuncs 
)
static

Definition at line 1104 of file parse_relation.c.

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

Referenced by addRangeTableEntryForFunction().

1106 {
1107  char *pname;
1108 
1109  /*
1110  * If the expression is a simple function call, and the function has a
1111  * single OUT parameter that is named, use the parameter's name.
1112  */
1113  if (funcexpr && IsA(funcexpr, FuncExpr))
1114  {
1115  pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1116  if (pname)
1117  return pname;
1118  }
1119 
1120  /*
1121  * If there's just one function in the RTE, and the user gave an RTE alias
1122  * name, use that name. (This makes FROM func() AS foo use "foo" as the
1123  * column name as well as the table alias.)
1124  */
1125  if (nfuncs == 1 && alias)
1126  return alias->aliasname;
1127 
1128  /*
1129  * Otherwise use the function name.
1130  */
1131  return funcname;
1132 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
char * aliasname
Definition: primnodes.h:42
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1073

◆ colNameToVar()

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

Definition at line 757 of file parse_relation.c.

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

Referenced by findTargetlistEntrySQL92(), and transformColumnRef().

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

◆ errorMissingColumn()

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

Definition at line 3250 of file parse_relation.c.

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

Referenced by transformColumnRef().

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

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3194 of file parse_relation.c.

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

Referenced by ExpandColumnRefStar(), and transformColumnRef().

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

◆ expandRelation()

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 2506 of file parse_relation.c.

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

Referenced by expandRTE().

2509 {
2510  Relation rel;
2511 
2512  /* Get the tupledesc and turn it over to expandTupleDesc */
2513  rel = relation_open(relid, AccessShareLock);
2514  expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
2515  rtindex, sublevels_up,
2516  location, include_dropped,
2517  colnames, colvars);
2519 }
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
int natts
Definition: tupdesc.h:79
TupleDesc rd_att
Definition: rel.h:115
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:1120

◆ expandRelAttrs()

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

Definition at line 2619 of file parse_relation.c.

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

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

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

◆ expandRTE()

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

Definition at line 2173 of file parse_relation.c.

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

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

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

◆ expandTupleDesc()

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 2531 of file parse_relation.c.

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

Referenced by expandRelation(), and expandRTE().

2535 {
2536  ListCell *aliascell = list_head(eref->colnames);
2537  int varattno;
2538 
2539  if (colnames)
2540  {
2541  int i;
2542 
2543  for (i = 0; i < offset; i++)
2544  {
2545  if (aliascell)
2546  aliascell = lnext(aliascell);
2547  }
2548  }
2549 
2550  Assert(count <= tupdesc->natts);
2551  for (varattno = 0; varattno < count; varattno++)
2552  {
2553  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
2554 
2555  if (attr->attisdropped)
2556  {
2557  if (include_dropped)
2558  {
2559  if (colnames)
2560  *colnames = lappend(*colnames, makeString(pstrdup("")));
2561  if (colvars)
2562  {
2563  /*
2564  * can't use atttypid here, but it doesn't really matter
2565  * what type the Const claims to be.
2566  */
2567  *colvars = lappend(*colvars,
2569  }
2570  }
2571  if (aliascell)
2572  aliascell = lnext(aliascell);
2573  continue;
2574  }
2575 
2576  if (colnames)
2577  {
2578  char *label;
2579 
2580  if (aliascell)
2581  {
2582  label = strVal(lfirst(aliascell));
2583  aliascell = lnext(aliascell);
2584  }
2585  else
2586  {
2587  /* If we run out of aliases, use the underlying name */
2588  label = NameStr(attr->attname);
2589  }
2590  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2591  }
2592 
2593  if (colvars)
2594  {
2595  Var *varnode;
2596 
2597  varnode = makeVar(rtindex, varattno + offset + 1,
2598  attr->atttypid, attr->atttypmod,
2599  attr->attcollation,
2600  sublevels_up);
2601  varnode->location = location;
2602 
2603  *colvars = lappend(*colvars, varnode);
2604  }
2605  }
2606 }
Value * makeString(char *str)
Definition: value.c:53
List * colnames
Definition: primnodes.h:43
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
char * pstrdup(const char *in)
Definition: mcxt.c:1076
#define INT4OID
Definition: pg_type.h:316
#define strVal(v)
Definition: value.h:54
Definition: primnodes.h:163
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:336
int location
Definition: primnodes.h:178
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#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:82
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
int i
#define NameStr(name)
Definition: c.h:547

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3054 of file parse_relation.c.

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

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

3055 {
3056  ListCell *l;
3057 
3058  foreach(l, qry->rowMarks)
3059  {
3060  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3061 
3062  if (rc->rti == rtindex)
3063  return rc;
3064  }
3065  return NULL;
3066 }
List * rowMarks
Definition: parsenodes.h:161
#define lfirst(lc)
Definition: pg_list.h:106

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2899 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, RangeTblEntry::coltypes, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funccolcount, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_tupdesc(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum, RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), list_nth_oid(), ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), and TupleDescAttr.

Referenced by AcquireRewriteLocks().

2900 {
2901  bool result;
2902 
2903  switch (rte->rtekind)
2904  {
2905  case RTE_RELATION:
2906  {
2907  /*
2908  * Plain relation RTE --- get the attribute's catalog entry
2909  */
2910  HeapTuple tp;
2911  Form_pg_attribute att_tup;
2912 
2913  tp = SearchSysCache2(ATTNUM,
2914  ObjectIdGetDatum(rte->relid),
2915  Int16GetDatum(attnum));
2916  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2917  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2918  attnum, rte->relid);
2919  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2920  result = att_tup->attisdropped;
2921  ReleaseSysCache(tp);
2922  }
2923  break;
2924  case RTE_SUBQUERY:
2925  case RTE_TABLEFUNC:
2926  case RTE_VALUES:
2927  case RTE_CTE:
2928 
2929  /*
2930  * Subselect, Table Functions, Values, CTE RTEs never have dropped
2931  * columns
2932  */
2933  result = false;
2934  break;
2935  case RTE_NAMEDTUPLESTORE:
2936  {
2937  /* Check dropped-ness by testing for valid coltype */
2938  if (attnum <= 0 ||
2939  attnum > list_length(rte->coltypes))
2940  elog(ERROR, "invalid varattno %d", attnum);
2941  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
2942  }
2943  break;
2944  case RTE_JOIN:
2945  {
2946  /*
2947  * A join RTE would not have dropped columns when constructed,
2948  * but one in a stored rule might contain columns that were
2949  * dropped from the underlying tables, if said columns are
2950  * nowhere explicitly referenced in the rule. This will be
2951  * signaled to us by a null pointer in the joinaliasvars list.
2952  */
2953  Var *aliasvar;
2954 
2955  if (attnum <= 0 ||
2956  attnum > list_length(rte->joinaliasvars))
2957  elog(ERROR, "invalid varattno %d", attnum);
2958  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
2959 
2960  result = (aliasvar == NULL);
2961  }
2962  break;
2963  case RTE_FUNCTION:
2964  {
2965  /* Function RTE */
2966  ListCell *lc;
2967  int atts_done = 0;
2968 
2969  /*
2970  * Dropped attributes are only possible with functions that
2971  * return named composite types. In such a case we have to
2972  * look up the result type to see if it currently has this
2973  * column dropped. So first, loop over the funcs until we
2974  * find the one that covers the requested column.
2975  */
2976  foreach(lc, rte->functions)
2977  {
2978  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2979 
2980  if (attnum > atts_done &&
2981  attnum <= atts_done + rtfunc->funccolcount)
2982  {
2983  TupleDesc tupdesc;
2984 
2985  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
2986  true);
2987  if (tupdesc)
2988  {
2989  /* Composite data type, e.g. a table's row type */
2990  Form_pg_attribute att_tup;
2991 
2992  Assert(tupdesc);
2993  Assert(attnum - atts_done <= tupdesc->natts);
2994  att_tup = TupleDescAttr(tupdesc,
2995  attnum - atts_done - 1);
2996  return att_tup->attisdropped;
2997  }
2998  /* Otherwise, it can't have any dropped columns */
2999  return false;
3000  }
3001  atts_done += rtfunc->funccolcount;
3002  }
3003 
3004  /* If we get here, must be looking for the ordinality column */
3005  if (rte->funcordinality && attnum == atts_done + 1)
3006  return false;
3007 
3008  /* this probably can't happen ... */
3009  ereport(ERROR,
3010  (errcode(ERRCODE_UNDEFINED_COLUMN),
3011  errmsg("column %d of relation \"%s\" does not exist",
3012  attnum,
3013  rte->eref->aliasname)));
3014  result = false; /* keep compiler quiet */
3015  }
3016  break;
3017  default:
3018  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3019  result = false; /* keep compiler quiet */
3020  }
3021 
3022  return result;
3023 }
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
List * joinaliasvars
Definition: parsenodes.h:995
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
#define Int16GetDatum(X)
Definition: postgres.h:457
int errcode(int sqlerrcode)
Definition: elog.c:575
bool funcordinality
Definition: parsenodes.h:1006
Definition: primnodes.h:163
#define OidIsValid(objectId)
Definition: c.h:576
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:412
void * list_nth(const List *list, int n)
Definition: list.c:410
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define ereport(elevel, rest)
Definition: elog.h:122
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1005
static int list_length(const List *l)
Definition: pg_list.h:89
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1123
RTEKind rtekind
Definition: parsenodes.h:951
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1055
List * coltypes
Definition: parsenodes.h:1041

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2671 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(), RangeTblEntry::relid, RTE_RELATION, RangeTblEntry::rtekind, and strVal.

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

2672 {
2673  if (attnum == InvalidAttrNumber)
2674  return "*";
2675 
2676  /*
2677  * If there is a user-written column alias, use it.
2678  */
2679  if (rte->alias &&
2680  attnum > 0 && attnum <= list_length(rte->alias->colnames))
2681  return strVal(list_nth(rte->alias->colnames, attnum - 1));
2682 
2683  /*
2684  * If the RTE is a relation, go to the system catalogs not the
2685  * eref->colnames list. This is a little slower but it will give the
2686  * right answer if the column has been renamed since the eref list was
2687  * built (which can easily happen for rules).
2688  */
2689  if (rte->rtekind == RTE_RELATION)
2690  return get_relid_attribute_name(rte->relid, attnum);
2691 
2692  /*
2693  * Otherwise use the column name from eref. There should always be one.
2694  */
2695  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
2696  return strVal(list_nth(rte->eref->colnames, attnum - 1));
2697 
2698  /* else caller gave us a bogus attnum */
2699  elog(ERROR, "invalid attnum %d for rangetable entry %s",
2700  attnum, rte->eref->aliasname);
2701  return NULL; /* keep compiler quiet */
2702 }
Alias * alias
Definition: parsenodes.h:1054
List * colnames
Definition: primnodes.h:43
#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
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:951
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1055

◆ get_rte_attribute_type()

void get_rte_attribute_type ( RangeTblEntry rte,
AttrNumber  attnum,
Oid vartype,
int32 vartypmod,
Oid varcollid 
)

Definition at line 2709 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, 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, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), RangeTblEntry::relid, TargetEntry::resjunk, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), RangeTblEntry::subquery, Query::targetList, TupleDescAttr, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by make_var().

2711 {
2712  switch (rte->rtekind)
2713  {
2714  case RTE_RELATION:
2715  {
2716  /* Plain relation RTE --- get the attribute's type info */
2717  HeapTuple tp;
2718  Form_pg_attribute att_tup;
2719 
2720  tp = SearchSysCache2(ATTNUM,
2721  ObjectIdGetDatum(rte->relid),
2722  Int16GetDatum(attnum));
2723  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2724  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2725  attnum, rte->relid);
2726  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2727 
2728  /*
2729  * If dropped column, pretend it ain't there. See notes in
2730  * scanRTEForColumn.
2731  */
2732  if (att_tup->attisdropped)
2733  ereport(ERROR,
2734  (errcode(ERRCODE_UNDEFINED_COLUMN),
2735  errmsg("column \"%s\" of relation \"%s\" does not exist",
2736  NameStr(att_tup->attname),
2737  get_rel_name(rte->relid))));
2738  *vartype = att_tup->atttypid;
2739  *vartypmod = att_tup->atttypmod;
2740  *varcollid = att_tup->attcollation;
2741  ReleaseSysCache(tp);
2742  }
2743  break;
2744  case RTE_SUBQUERY:
2745  {
2746  /* Subselect RTE --- get type info from subselect's tlist */
2748  attnum);
2749 
2750  if (te == NULL || te->resjunk)
2751  elog(ERROR, "subquery %s does not have attribute %d",
2752  rte->eref->aliasname, attnum);
2753  *vartype = exprType((Node *) te->expr);
2754  *vartypmod = exprTypmod((Node *) te->expr);
2755  *varcollid = exprCollation((Node *) te->expr);
2756  }
2757  break;
2758  case RTE_FUNCTION:
2759  {
2760  /* Function RTE */
2761  ListCell *lc;
2762  int atts_done = 0;
2763 
2764  /* Identify which function covers the requested column */
2765  foreach(lc, rte->functions)
2766  {
2767  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2768 
2769  if (attnum > atts_done &&
2770  attnum <= atts_done + rtfunc->funccolcount)
2771  {
2772  TypeFuncClass functypclass;
2773  Oid funcrettype;
2774  TupleDesc tupdesc;
2775 
2776  attnum -= atts_done; /* now relative to this func */
2777  functypclass = get_expr_result_type(rtfunc->funcexpr,
2778  &funcrettype,
2779  &tupdesc);
2780 
2781  if (functypclass == TYPEFUNC_COMPOSITE ||
2782  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
2783  {
2784  /* Composite data type, e.g. a table's row type */
2785  Form_pg_attribute att_tup;
2786 
2787  Assert(tupdesc);
2788  Assert(attnum <= tupdesc->natts);
2789  att_tup = TupleDescAttr(tupdesc, attnum - 1);
2790 
2791  /*
2792  * If dropped column, pretend it ain't there. See
2793  * notes in scanRTEForColumn.
2794  */
2795  if (att_tup->attisdropped)
2796  ereport(ERROR,
2797  (errcode(ERRCODE_UNDEFINED_COLUMN),
2798  errmsg("column \"%s\" of relation \"%s\" does not exist",
2799  NameStr(att_tup->attname),
2800  rte->eref->aliasname)));
2801  *vartype = att_tup->atttypid;
2802  *vartypmod = att_tup->atttypmod;
2803  *varcollid = att_tup->attcollation;
2804  }
2805  else if (functypclass == TYPEFUNC_SCALAR)
2806  {
2807  /* Base data type, i.e. scalar */
2808  *vartype = funcrettype;
2809  *vartypmod = -1;
2810  *varcollid = exprCollation(rtfunc->funcexpr);
2811  }
2812  else if (functypclass == TYPEFUNC_RECORD)
2813  {
2814  *vartype = list_nth_oid(rtfunc->funccoltypes,
2815  attnum - 1);
2816  *vartypmod = list_nth_int(rtfunc->funccoltypmods,
2817  attnum - 1);
2818  *varcollid = list_nth_oid(rtfunc->funccolcollations,
2819  attnum - 1);
2820  }
2821  else
2822  {
2823  /*
2824  * addRangeTableEntryForFunction should've caught
2825  * this
2826  */
2827  elog(ERROR, "function in FROM has unsupported return type");
2828  }
2829  return;
2830  }
2831  atts_done += rtfunc->funccolcount;
2832  }
2833 
2834  /* If we get here, must be looking for the ordinality column */
2835  if (rte->funcordinality && attnum == atts_done + 1)
2836  {
2837  *vartype = INT8OID;
2838  *vartypmod = -1;
2839  *varcollid = InvalidOid;
2840  return;
2841  }
2842 
2843  /* this probably can't happen ... */
2844  ereport(ERROR,
2845  (errcode(ERRCODE_UNDEFINED_COLUMN),
2846  errmsg("column %d of relation \"%s\" does not exist",
2847  attnum,
2848  rte->eref->aliasname)));
2849  }
2850  break;
2851  case RTE_JOIN:
2852  {
2853  /*
2854  * Join RTE --- get type info from join RTE's alias variable
2855  */
2856  Node *aliasvar;
2857 
2858  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
2859  aliasvar = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
2860  Assert(aliasvar != NULL);
2861  *vartype = exprType(aliasvar);
2862  *vartypmod = exprTypmod(aliasvar);
2863  *varcollid = exprCollation(aliasvar);
2864  }
2865  break;
2866  case RTE_TABLEFUNC:
2867  case RTE_VALUES:
2868  case RTE_CTE:
2869  case RTE_NAMEDTUPLESTORE:
2870  {
2871  /*
2872  * tablefunc, VALUES, CTE, or ENR RTE --- get type info from
2873  * lists in the RTE
2874  */
2875  Assert(attnum > 0 && attnum <= list_length(rte->coltypes));
2876  *vartype = list_nth_oid(rte->coltypes, attnum - 1);
2877  *vartypmod = list_nth_int(rte->coltypmods, attnum - 1);
2878  *varcollid = list_nth_oid(rte->colcollations, attnum - 1);
2879 
2880  /* For ENR, better check for dropped column */
2881  if (!OidIsValid(*vartype))
2882  ereport(ERROR,
2883  (errcode(ERRCODE_UNDEFINED_COLUMN),
2884  errmsg("column %d of relation \"%s\" does not exist",
2885  attnum,
2886  rte->eref->aliasname)));
2887  }
2888  break;
2889  default:
2890  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2891  }
2892 }
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
List * joinaliasvars
Definition: parsenodes.h:995
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
List * coltypmods
Definition: parsenodes.h:1042
#define Int16GetDatum(X)
Definition: postgres.h:457
Definition: nodes.h:512
int errcode(int sqlerrcode)
Definition: elog.c:575
bool funcordinality
Definition: parsenodes.h:1006
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
#define OidIsValid(objectId)
Definition: c.h:576
List * colcollations
Definition: parsenodes.h:1043
List * targetList
Definition: parsenodes.h:138
bool resjunk
Definition: primnodes.h:1382
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#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:187
TypeFuncClass
Definition: funcapi.h:155
#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:1160
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1092
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:1093
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1005
Expr * expr
Definition: primnodes.h:1375
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1123
RTEKind rtekind
Definition: parsenodes.h:951
Query * subquery
Definition: parsenodes.h:974
int errmsg(const char *fmt,...)
Definition: elog.c:797
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define NameStr(name)
Definition: c.h:547
#define elog
Definition: elog.h:219
Alias * eref
Definition: parsenodes.h:1055
List * coltypes
Definition: parsenodes.h:1041
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1745

◆ get_tle_by_resno()

◆ GetCTEForRTE()

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

Definition at line 517 of file parse_relation.c.

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

Referenced by expandRecordVariable(), and markTargetListOrigin().

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

◆ GetRTEByRangeTablePosn()

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

Definition at line 496 of file parse_relation.c.

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

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

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

◆ isFutureCTE()

static bool isFutureCTE ( ParseState pstate,
const char *  refname 
)
static

Definition at line 268 of file parse_relation.c.

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

Referenced by parserOpenTable().

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

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

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

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

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3322 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

3323 {
3324  return isQueryUsingTempRelation_walker((Node *) query, NULL);
3325 }
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
Definition: nodes.h:512

◆ isQueryUsingTempRelation_walker()

static bool isQueryUsingTempRelation_walker ( Node node,
void *  context 
)
static

Definition at line 3328 of file parse_relation.c.

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

Referenced by isQueryUsingTempRelation().

3329 {
3330  if (node == NULL)
3331  return false;
3332 
3333  if (IsA(node, Query))
3334  {
3335  Query *query = (Query *) node;
3336  ListCell *rtable;
3337 
3338  foreach(rtable, query->rtable)
3339  {
3340  RangeTblEntry *rte = lfirst(rtable);
3341 
3342  if (rte->rtekind == RTE_RELATION)
3343  {
3344  Relation rel = heap_open(rte->relid, AccessShareLock);
3345  char relpersistence = rel->rd_rel->relpersistence;
3346 
3348  if (relpersistence == RELPERSISTENCE_TEMP)
3349  return true;
3350  }
3351  }
3352 
3353  return query_tree_walker(query,
3355  context,
3357  }
3358 
3359  return expression_tree_walker(node,
3361  context);
3362 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2245
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:114
List * rtable
Definition: parsenodes.h:135
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
#define lfirst(lc)
Definition: pg_list.h:106
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1834
RTEKind rtekind
Definition: parsenodes.h:951
#define RELPERSISTENCE_TEMP
Definition: pg_class.h:172
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23

◆ markRTEForSelectPriv()

static void markRTEForSelectPriv ( ParseState pstate,
RangeTblEntry rte,
int  rtindex,
AttrNumber  col 
)
static

Definition at line 909 of file parse_relation.c.

References ACL_SELECT, Assert, bms_add_member(), elog, ERROR, FirstLowInvalidHeapAttributeNumber, InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, JoinExpr::larg, list_length(), list_nth(), list_nth_node, markVarForSelectPriv(), nodeTag, 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().

911 {
912  if (rte == NULL)
913  rte = rt_fetch(rtindex, pstate->p_rtable);
914 
915  if (rte->rtekind == RTE_RELATION)
916  {
917  /* Make sure the rel as a whole is marked for SELECT access */
918  rte->requiredPerms |= ACL_SELECT;
919  /* Must offset the attnum to fit in a bitmapset */
922  }
923  else if (rte->rtekind == RTE_JOIN)
924  {
925  if (col == InvalidAttrNumber)
926  {
927  /*
928  * A whole-row reference to a join has to be treated as whole-row
929  * references to the two inputs.
930  */
931  JoinExpr *j;
932 
933  if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
934  j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
935  else
936  j = NULL;
937  if (j == NULL)
938  elog(ERROR, "could not find JoinExpr for whole-row reference");
939 
940  /* Note: we can't see FromExpr here */
941  if (IsA(j->larg, RangeTblRef))
942  {
943  int varno = ((RangeTblRef *) j->larg)->rtindex;
944 
945  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
946  }
947  else if (IsA(j->larg, JoinExpr))
948  {
949  int varno = ((JoinExpr *) j->larg)->rtindex;
950 
951  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
952  }
953  else
954  elog(ERROR, "unrecognized node type: %d",
955  (int) nodeTag(j->larg));
956  if (IsA(j->rarg, RangeTblRef))
957  {
958  int varno = ((RangeTblRef *) j->rarg)->rtindex;
959 
960  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
961  }
962  else if (IsA(j->rarg, JoinExpr))
963  {
964  int varno = ((JoinExpr *) j->rarg)->rtindex;
965 
966  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
967  }
968  else
969  elog(ERROR, "unrecognized node type: %d",
970  (int) nodeTag(j->rarg));
971  }
972  else
973  {
974  /*
975  * Regular join attribute, look at the alias-variable list.
976  *
977  * The aliasvar could be either a Var or a COALESCE expression,
978  * but in the latter case we should already have marked the two
979  * referent variables as being selected, due to their use in the
980  * JOIN clause. So we need only be concerned with the Var case.
981  * But we do need to drill down through implicit coercions.
982  */
983  Var *aliasvar;
984 
985  Assert(col > 0 && col <= list_length(rte->joinaliasvars));
986  aliasvar = (Var *) list_nth(rte->joinaliasvars, col - 1);
987  aliasvar = (Var *) strip_implicit_coercions((Node *) aliasvar);
988  if (aliasvar && IsA(aliasvar, Var))
989  markVarForSelectPriv(pstate, aliasvar, NULL);
990  }
991  }
992  /* other RTE types don't require privilege marking */
993 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
List * joinaliasvars
Definition: parsenodes.h:995
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
Definition: nodes.h:512
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
AclMode requiredPerms
Definition: parsenodes.h:1059
Definition: primnodes.h:163
Node * larg
Definition: primnodes.h:1458
Bitmapset * selectedCols
Definition: parsenodes.h:1061
#define ERROR
Definition: elog.h:43
#define list_nth_node(type, list, n)
Definition: pg_list.h:227
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:73
Node * rarg
Definition: primnodes.h:1459
#define Assert(condition)
Definition: c.h:670
static int list_length(const List *l)
Definition: pg_list.h:89
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
#define InvalidAttrNumber
Definition: attnum.h:23
#define nodeTag(nodeptr)
Definition: nodes.h:517
RTEKind rtekind
Definition: parsenodes.h:951
#define elog
Definition: elog.h:219
List * p_joinexprs
Definition: parse_node.h:174
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:612
List * p_rtable
Definition: parse_node.h:173

◆ markVarForSelectPriv()

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

Definition at line 1003 of file parse_relation.c.

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

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

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

◆ parserOpenTable()

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

Definition at line 1146 of file parse_relation.c.

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

Referenced by addRangeTableEntry(), and setTargetTable().

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

◆ refnameRangeTblEntry()

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

Definition at line 83 of file parse_relation.c.

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

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

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

◆ RTERangeTablePosn()

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

Definition at line 463 of file parse_relation.c.

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

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

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

◆ scanNameSpaceForCTE()

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

Definition at line 237 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes(), and searchRangeTableForRel().

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

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 290 of file parse_relation.c.

References name_matches_visible_ENR().

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

291 {
292  return name_matches_visible_ENR(pstate, refname);
293 }
bool name_matches_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:20

◆ scanNameSpaceForRefname()

static RangeTblEntry * scanNameSpaceForRefname ( ParseState pstate,
const char *  refname,
int  location 
)
static

Definition at line 154 of file parse_relation.c.

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

Referenced by refnameRangeTblEntry().

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

◆ scanNameSpaceForRelid()

static RangeTblEntry * scanNameSpaceForRelid ( ParseState pstate,
Oid  relid,
int  location 
)
static

Definition at line 195 of file parse_relation.c.

References RangeTblEntry::alias, check_lateral_ref_ok(), ereport, errcode(), errmsg(), ERROR, lfirst, 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().

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

◆ scanRTEForColumn()

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

Definition at line 655 of file parse_relation.c.

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

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

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

◆ searchRangeTableForCol()

static FuzzyAttrMatchState* searchRangeTableForCol ( ParseState pstate,
const char *  alias,
const char *  colname,
int  location 
)
static

Definition at line 831 of file parse_relation.c.

References Alias::aliasname, FuzzyAttrMatchState::distance, RangeTblEntry::eref, FuzzyAttrMatchState::first, InvalidAttrNumber, lfirst, MAX_FUZZY_DISTANCE, 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().

833 {
834  ParseState *orig_pstate = pstate;
835  FuzzyAttrMatchState *fuzzystate = palloc(sizeof(FuzzyAttrMatchState));
836 
837  fuzzystate->distance = MAX_FUZZY_DISTANCE + 1;
838  fuzzystate->rfirst = NULL;
839  fuzzystate->rsecond = NULL;
840  fuzzystate->first = InvalidAttrNumber;
841  fuzzystate->second = InvalidAttrNumber;
842 
843  while (pstate != NULL)
844  {
845  ListCell *l;
846 
847  foreach(l, pstate->p_rtable)
848  {
849  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
850  int fuzzy_rte_penalty = 0;
851 
852  /*
853  * Typically, it is not useful to look for matches within join
854  * RTEs; they effectively duplicate other RTEs for our purposes,
855  * and if a match is chosen from a join RTE, an unhelpful alias is
856  * displayed in the final diagnostic message.
857  */
858  if (rte->rtekind == RTE_JOIN)
859  continue;
860 
861  /*
862  * If the user didn't specify an alias, then matches against one
863  * RTE are as good as another. But if the user did specify an
864  * alias, then we want at least a fuzzy - and preferably an exact
865  * - match for the range table entry.
866  */
867  if (alias != NULL)
868  fuzzy_rte_penalty =
869  varstr_levenshtein_less_equal(alias, strlen(alias),
870  rte->eref->aliasname,
871  strlen(rte->eref->aliasname),
872  1, 1, 1,
873  MAX_FUZZY_DISTANCE + 1,
874  true);
875 
876  /*
877  * Scan for a matching column; if we find an exact match, we're
878  * done. Otherwise, update fuzzystate.
879  */
880  if (scanRTEForColumn(orig_pstate, rte, colname, location,
881  fuzzy_rte_penalty, fuzzystate)
882  && fuzzy_rte_penalty == 0)
883  {
884  fuzzystate->rfirst = rte;
885  fuzzystate->first = InvalidAttrNumber;
886  fuzzystate->rsecond = NULL;
887  fuzzystate->second = InvalidAttrNumber;
888  return fuzzystate;
889  }
890  }
891 
892  pstate = pstate->parentParseState;
893  }
894 
895  return fuzzystate;
896 }
Node * scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
RangeTblEntry * rfirst
#define MAX_FUZZY_DISTANCE
struct ParseState * parentParseState
Definition: parse_node.h:171
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
RangeTblEntry * rsecond
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:951
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:848
Alias * eref
Definition: parsenodes.h:1055
List * p_rtable
Definition: parse_node.h:173

◆ searchRangeTableForRel()

static RangeTblEntry* searchRangeTableForRel ( ParseState pstate,
RangeVar relation 
)
static

Definition at line 311 of file parse_relation.c.

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

Referenced by errorMissingRTE().

312 {
313  const char *refname = relation->relname;
314  Oid relId = InvalidOid;
315  CommonTableExpr *cte = NULL;
316  bool isenr = false;
317  Index ctelevelsup = 0;
318  Index levelsup;
319 
320  /*
321  * If it's an unqualified name, check for possible CTE matches. A CTE
322  * hides any real relation matches. If no CTE, look for a matching
323  * relation.
324  *
325  * NB: It's not critical that RangeVarGetRelid return the correct answer
326  * here in the face of concurrent DDL. If it doesn't, the worst case
327  * scenario is a less-clear error message. Also, the tables involved in
328  * the query are already locked, which reduces the number of cases in
329  * which surprising behavior can occur. So we do the name lookup
330  * unlocked.
331  */
332  if (!relation->schemaname)
333  {
334  cte = scanNameSpaceForCTE(pstate, refname, &ctelevelsup);
335  if (!cte)
336  isenr = scanNameSpaceForENR(pstate, refname);
337  }
338 
339  if (!cte && !isenr)
340  relId = RangeVarGetRelid(relation, NoLock, true);
341 
342  /* Now look for RTEs matching either the relation/CTE/ENR or the alias */
343  for (levelsup = 0;
344  pstate != NULL;
345  pstate = pstate->parentParseState, levelsup++)
346  {
347  ListCell *l;
348 
349  foreach(l, pstate->p_rtable)
350  {
351  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
352 
353  if (rte->rtekind == RTE_RELATION &&
354  OidIsValid(relId) &&
355  rte->relid == relId)
356  return rte;
357  if (rte->rtekind == RTE_CTE &&
358  cte != NULL &&
359  rte->ctelevelsup + levelsup == ctelevelsup &&
360  strcmp(rte->ctename, refname) == 0)
361  return rte;
362  if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
363  isenr &&
364  strcmp(rte->enrname, refname) == 0)
365  return rte;
366  if (strcmp(rte->eref->aliasname, refname) == 0)
367  return rte;
368  }
369  }
370  return NULL;
371 }
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
char * schemaname
Definition: primnodes.h:67
char * relname
Definition: primnodes.h:68
#define NoLock
Definition: lockdefs.h:34
bool scanNameSpaceForENR(ParseState *pstate, const char *refname)
char * enrname
Definition: parsenodes.h:1048
struct ParseState * parentParseState
Definition: parse_node.h:171
unsigned int Index
Definition: c.h:413
#define InvalidOid
Definition: postgres_ext.h:36
CommonTableExpr * scanNameSpaceForCTE(ParseState *pstate, const char *refname, Index *ctelevelsup)
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
Index ctelevelsup
Definition: parsenodes.h:1022
RTEKind rtekind
Definition: parsenodes.h:951
char * ctename
Definition: parsenodes.h:1021
Alias * eref
Definition: parsenodes.h:1055
List * p_rtable
Definition: parse_node.h:173

◆ specialAttNum()

static int specialAttNum ( const char *  attname)
static

Definition at line 3113 of file parse_relation.c.

References InvalidAttrNumber, and SystemAttributeByName().

Referenced by attnameAttNum(), and scanRTEForColumn().

3114 {
3115  Form_pg_attribute sysatt;
3116 
3117  sysatt = SystemAttributeByName(attname,
3118  true /* "oid" will be accepted */ );
3119  if (sysatt != NULL)
3120  return sysatt->attnum;
3121  return InvalidAttrNumber;
3122 }
Form_pg_attribute SystemAttributeByName(const char *attname, bool relhasoids)
Definition: heap.c:214
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define InvalidAttrNumber
Definition: attnum.h:23

◆ updateFuzzyAttrMatchState()

static void updateFuzzyAttrMatchState ( int  fuzzy_rte_penalty,
FuzzyAttrMatchState fuzzystate,
RangeTblEntry rte,
const char *  actual,
const char *  match,
int  attnum 
)
static

Definition at line 551 of file parse_relation.c.

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

Referenced by scanRTEForColumn().

554 {
555  int columndistance;
556  int matchlen;
557 
558  /* Bail before computing the Levenshtein distance if there's no hope. */
559  if (fuzzy_rte_penalty > fuzzystate->distance)
560  return;
561 
562  /*
563  * Outright reject dropped columns, which can appear here with apparent
564  * empty actual names, per remarks within scanRTEForColumn().
565  */
566  if (actual[0] == '\0')
567  return;
568 
569  /* Use Levenshtein to compute match distance. */
570  matchlen = strlen(match);
571  columndistance =
572  varstr_levenshtein_less_equal(actual, strlen(actual), match, matchlen,
573  1, 1, 1,
574  fuzzystate->distance + 1
575  - fuzzy_rte_penalty,
576  true);
577 
578  /*
579  * If more than half the characters are different, don't treat it as a
580  * match, to avoid making ridiculous suggestions.
581  */
582  if (columndistance > matchlen / 2)
583  return;
584 
585  /*
586  * From this point on, we can ignore the distinction between the RTE-name
587  * distance and the column-name distance.
588  */
589  columndistance += fuzzy_rte_penalty;
590 
591  /*
592  * If the new distance is less than or equal to that of the best match
593  * found so far, update fuzzystate.
594  */
595  if (columndistance < fuzzystate->distance)
596  {
597  /* Store new lowest observed distance for RTE */
598  fuzzystate->distance = columndistance;
599  fuzzystate->rfirst = rte;
600  fuzzystate->first = attnum;
601  fuzzystate->rsecond = NULL;
602  fuzzystate->second = InvalidAttrNumber;
603  }
604  else if (columndistance == fuzzystate->distance)