PostgreSQL Source Code  git master
parse_relation.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "access/relation.h"
#include "access/sysattr.h"
#include "access/table.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/parse_enr.h"
#include "parser/parse_relation.h"
#include "parser/parse_type.h"
#include "parser/parsetree.h"
#include "storage/lmgr.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, int lockmode, Alias *alias, bool inh, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForSubquery (ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
 
RangeTblEntryaddRangeTableEntryForFunction (ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
 
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)
 
const NameDataattnumAttName (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 40 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 1210 of file parse_relation.c.

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

Referenced by transformTableEntry().

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

◆ addRangeTableEntryForCTE()

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

Definition at line 1920 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes().

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

◆ addRangeTableEntryForENR()

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

Definition at line 2030 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes().

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

◆ addRangeTableEntryForFunction()

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

Definition at line 1443 of file parse_relation.c.

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

Referenced by transformRangeFunction().

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

◆ addRangeTableEntryForJoin()

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

Definition at line 1849 of file parse_relation.c.

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

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

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

◆ addRangeTableEntryForRelation()

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

Definition at line 1300 of file parse_relation.c.

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

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

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

◆ addRangeTableEntryForSubquery()

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

Definition at line 1363 of file parse_relation.c.

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

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

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

◆ addRangeTableEntryForTableFunc()

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

Definition at line 1706 of file parse_relation.c.

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

Referenced by transformRangeTableFunc().

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

◆ addRangeTableEntryForValues()

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

Definition at line 1770 of file parse_relation.c.

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

Referenced by transformInsertStmt(), and transformValuesClause().

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

◆ addRTEtoQuery()

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

Definition at line 2181 of file parse_relation.c.

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

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

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

◆ attnameAttNum()

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

Definition at line 3142 of file parse_relation.c.

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

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

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

◆ attnumAttName()

const NameData* attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3192 of file parse_relation.c.

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

Referenced by transformFkeyGetPrimaryKey().

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

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3234 of file parse_relation.c.

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

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

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3214 of file parse_relation.c.

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

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

◆ buildRelationAliases()

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

Definition at line 1043 of file parse_relation.c.

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

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

1044 {
1045  int maxattrs = tupdesc->natts;
1046  List *aliaslist;
1047  ListCell *aliaslc;
1048  int numaliases;
1049  int varattno;
1050  int numdropped = 0;
1051 
1052  Assert(eref->colnames == NIL);
1053 
1054  if (alias)
1055  {
1056  aliaslist = alias->colnames;
1057  aliaslc = list_head(aliaslist);
1058  numaliases = list_length(aliaslist);
1059  /* We'll rebuild the alias colname list */
1060  alias->colnames = NIL;
1061  }
1062  else
1063  {
1064  aliaslist = NIL;
1065  aliaslc = NULL;
1066  numaliases = 0;
1067  }
1068 
1069  for (varattno = 0; varattno < maxattrs; varattno++)
1070  {
1071  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1072  Value *attrname;
1073 
1074  if (attr->attisdropped)
1075  {
1076  /* Always insert an empty string for a dropped column */
1077  attrname = makeString(pstrdup(""));
1078  if (aliaslc)
1079  alias->colnames = lappend(alias->colnames, attrname);
1080  numdropped++;
1081  }
1082  else if (aliaslc)
1083  {
1084  /* Use the next user-supplied alias */
1085  attrname = (Value *) lfirst(aliaslc);
1086  aliaslc = lnext(aliaslist, aliaslc);
1087  alias->colnames = lappend(alias->colnames, attrname);
1088  }
1089  else
1090  {
1091  attrname = makeString(pstrdup(NameStr(attr->attname)));
1092  /* we're done with the alias if any */
1093  }
1094 
1095  eref->colnames = lappend(eref->colnames, attrname);
1096  }
1097 
1098  /* Too many user-supplied aliases? */
1099  if (aliaslc)
1100  ereport(ERROR,
1101  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1102  errmsg("table \"%s\" has %d columns available but %d columns specified",
1103  eref->aliasname, maxattrs - numdropped, numaliases)));
1104 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
List * colnames
Definition: primnodes.h:43
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1186
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
List * lappend(List *list, void *datum)
Definition: list.c:322
#define Assert(condition)
Definition: c.h:733
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
Definition: value.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define NameStr(name)
Definition: c.h:610
Definition: pg_list.h:50

◆ check_lateral_ref_ok()

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

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

439 {
440  if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
441  {
442  /* SQL:2008 demands this be an error, not an invisible item */
443  RangeTblEntry *rte = nsitem->p_rte;
444  char *refname = rte->eref->aliasname;
445 
446  ereport(ERROR,
447  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
448  errmsg("invalid reference to FROM-clause entry for table \"%s\"",
449  refname),
450  (rte == pstate->p_target_rangetblentry) ?
451  errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
452  refname) :
453  errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
454  parser_errposition(pstate, location)));
455  }
456 }
int errhint(const char *fmt,...)
Definition: elog.c:1071
int errcode(int sqlerrcode)
Definition: elog.c:608
RangeTblEntry * p_rte
Definition: parse_node.h:251
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:957
#define ereport(elevel, rest)
Definition: elog.h:141
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:188
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:822
Alias * eref
Definition: parsenodes.h:1092

◆ checkNameSpaceConflicts()

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

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

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

◆ chooseScalarFunctionAlias()

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

Definition at line 1120 of file parse_relation.c.

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

Referenced by addRangeTableEntryForFunction().

1122 {
1123  char *pname;
1124 
1125  /*
1126  * If the expression is a simple function call, and the function has a
1127  * single OUT parameter that is named, use the parameter's name.
1128  */
1129  if (funcexpr && IsA(funcexpr, FuncExpr))
1130  {
1131  pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1132  if (pname)
1133  return pname;
1134  }
1135 
1136  /*
1137  * If there's just one function in the RTE, and the user gave an RTE alias
1138  * name, use that name. (This makes FROM func() AS foo use "foo" as the
1139  * column name as well as the table alias.)
1140  */
1141  if (nfuncs == 1 && alias)
1142  return alias->aliasname;
1143 
1144  /*
1145  * Otherwise use the function name.
1146  */
1147  return funcname;
1148 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
char * aliasname
Definition: primnodes.h:42
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1090

◆ colNameToVar()

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

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

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

◆ errorMissingColumn()

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

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

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

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

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

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

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

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

Referenced by expandRTE().

2564 {
2565  Relation rel;
2566 
2567  /* Get the tupledesc and turn it over to expandTupleDesc */
2568  rel = relation_open(relid, AccessShareLock);
2569  expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
2570  rtindex, sublevels_up,
2571  location, include_dropped,
2572  colnames, colvars);
2574 }
#define AccessShareLock
Definition: lockdefs.h:36
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
TupleDesc rd_att
Definition: rel.h:84
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
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)

◆ expandRelAttrs()

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

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

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

◆ expandRTE()

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

Definition at line 2225 of file parse_relation.c.

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

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

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

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

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

Referenced by expandRelation(), and expandRTE().

2590 {
2591  ListCell *aliascell;
2592  int varattno;
2593 
2594  aliascell = (offset < list_length(eref->colnames)) ?
2595  list_nth_cell(eref->colnames, offset) : NULL;
2596 
2597  Assert(count <= tupdesc->natts);
2598  for (varattno = 0; varattno < count; varattno++)
2599  {
2600  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
2601 
2602  if (attr->attisdropped)
2603  {
2604  if (include_dropped)
2605  {
2606  if (colnames)
2607  *colnames = lappend(*colnames, makeString(pstrdup("")));
2608  if (colvars)
2609  {
2610  /*
2611  * can't use atttypid here, but it doesn't really matter
2612  * what type the Const claims to be.
2613  */
2614  *colvars = lappend(*colvars,
2615  makeNullConst(INT4OID, -1, InvalidOid));
2616  }
2617  }
2618  if (aliascell)
2619  aliascell = lnext(eref->colnames, aliascell);
2620  continue;
2621  }
2622 
2623  if (colnames)
2624  {
2625  char *label;
2626 
2627  if (aliascell)
2628  {
2629  label = strVal(lfirst(aliascell));
2630  aliascell = lnext(eref->colnames, aliascell);
2631  }
2632  else
2633  {
2634  /* If we run out of aliases, use the underlying name */
2635  label = NameStr(attr->attname);
2636  }
2637  *colnames = lappend(*colnames, makeString(pstrdup(label)));
2638  }
2639 
2640  if (colvars)
2641  {
2642  Var *varnode;
2643 
2644  varnode = makeVar(rtindex, varattno + offset + 1,
2645  attr->atttypid, attr->atttypmod,
2646  attr->attcollation,
2647  sublevels_up);
2648  varnode->location = location;
2649 
2650  *colvars = lappend(*colvars, varnode);
2651  }
2652  }
2653 }
Value * makeString(char *str)
Definition: value.c:53
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:265
List * colnames
Definition: primnodes.h:43
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define strVal(v)
Definition: value.h:54
Definition: primnodes.h:167
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:335
int location
Definition: primnodes.h:182
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
List * lappend(List *list, void *datum)
Definition: list.c:322
static char * label
Definition: pg_basebackup.c:86
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:733
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
#define NameStr(name)
Definition: c.h:610

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3118 of file parse_relation.c.

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

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

3119 {
3120  ListCell *l;
3121 
3122  foreach(l, qry->rowMarks)
3123  {
3124  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3125 
3126  if (rc->rti == rtindex)
3127  return rc;
3128  }
3129  return NULL;
3130 }
List * rowMarks
Definition: parsenodes.h:163
#define lfirst(lc)
Definition: pg_list.h:190

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2954 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_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), and TupleDescAttr.

Referenced by AcquireRewriteLocks().

2955 {
2956  bool result;
2957 
2958  switch (rte->rtekind)
2959  {
2960  case RTE_RELATION:
2961  {
2962  /*
2963  * Plain relation RTE --- get the attribute's catalog entry
2964  */
2965  HeapTuple tp;
2966  Form_pg_attribute att_tup;
2967 
2968  tp = SearchSysCache2(ATTNUM,
2969  ObjectIdGetDatum(rte->relid),
2971  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2972  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2973  attnum, rte->relid);
2974  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2975  result = att_tup->attisdropped;
2976  ReleaseSysCache(tp);
2977  }
2978  break;
2979  case RTE_SUBQUERY:
2980  case RTE_TABLEFUNC:
2981  case RTE_VALUES:
2982  case RTE_CTE:
2983 
2984  /*
2985  * Subselect, Table Functions, Values, CTE RTEs never have dropped
2986  * columns
2987  */
2988  result = false;
2989  break;
2990  case RTE_NAMEDTUPLESTORE:
2991  {
2992  /* Check dropped-ness by testing for valid coltype */
2993  if (attnum <= 0 ||
2994  attnum > list_length(rte->coltypes))
2995  elog(ERROR, "invalid varattno %d", attnum);
2996  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
2997  }
2998  break;
2999  case RTE_JOIN:
3000  {
3001  /*
3002  * A join RTE would not have dropped columns when constructed,
3003  * but one in a stored rule might contain columns that were
3004  * dropped from the underlying tables, if said columns are
3005  * nowhere explicitly referenced in the rule. This will be
3006  * signaled to us by a null pointer in the joinaliasvars list.
3007  */
3008  Var *aliasvar;
3009 
3010  if (attnum <= 0 ||
3012  elog(ERROR, "invalid varattno %d", attnum);
3013  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3014 
3015  result = (aliasvar == NULL);
3016  }
3017  break;
3018  case RTE_FUNCTION:
3019  {
3020  /* Function RTE */
3021  ListCell *lc;
3022  int atts_done = 0;
3023 
3024  /*
3025  * Dropped attributes are only possible with functions that
3026  * return named composite types. In such a case we have to
3027  * look up the result type to see if it currently has this
3028  * column dropped. So first, loop over the funcs until we
3029  * find the one that covers the requested column.
3030  */
3031  foreach(lc, rte->functions)
3032  {
3033  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3034 
3035  if (attnum > atts_done &&
3036  attnum <= atts_done + rtfunc->funccolcount)
3037  {
3038  TupleDesc tupdesc;
3039 
3040  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3041  true);
3042  if (tupdesc)
3043  {
3044  /* Composite data type, e.g. a table's row type */
3045  Form_pg_attribute att_tup;
3046 
3047  Assert(tupdesc);
3048  Assert(attnum - atts_done <= tupdesc->natts);
3049  att_tup = TupleDescAttr(tupdesc,
3050  attnum - atts_done - 1);
3051  return att_tup->attisdropped;
3052  }
3053  /* Otherwise, it can't have any dropped columns */
3054  return false;
3055  }
3056  atts_done += rtfunc->funccolcount;
3057  }
3058 
3059  /* If we get here, must be looking for the ordinality column */
3060  if (rte->funcordinality && attnum == atts_done + 1)
3061  return false;
3062 
3063  /* this probably can't happen ... */
3064  ereport(ERROR,
3065  (errcode(ERRCODE_UNDEFINED_COLUMN),
3066  errmsg("column %d of relation \"%s\" does not exist",
3067  attnum,
3068  rte->eref->aliasname)));
3069  result = false; /* keep compiler quiet */
3070  }
3071  break;
3072  case RTE_RESULT:
3073  /* this probably can't happen ... */
3074  ereport(ERROR,
3075  (errcode(ERRCODE_UNDEFINED_COLUMN),
3076  errmsg("column %d of relation \"%s\" does not exist",
3077  attnum,
3078  rte->eref->aliasname)));
3079  result = false; /* keep compiler quiet */
3080  break;
3081  default:
3082  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3083  result = false; /* keep compiler quiet */
3084  }
3085 
3086  return result;
3087 }
List * joinaliasvars
Definition: parsenodes.h:1030
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define Int16GetDatum(X)
Definition: postgres.h:451
int errcode(int sqlerrcode)
Definition: elog.c:608
bool funcordinality
Definition: parsenodes.h:1041
Definition: primnodes.h:167
#define OidIsValid(objectId)
Definition: c.h:639
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:429
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
int16 attnum
Definition: pg_attribute.h:79
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:733
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1040
static int list_length(const List *l)
Definition: pg_list.h:169
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
RTEKind rtekind
Definition: parsenodes.h:974
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
Alias * eref
Definition: parsenodes.h:1092
List * coltypes
Definition: parsenodes.h:1078

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 2718 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, Alias::colnames, elog, RangeTblEntry::eref, ERROR, get_attname(), 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().

2719 {
2720  if (attnum == InvalidAttrNumber)
2721  return "*";
2722 
2723  /*
2724  * If there is a user-written column alias, use it.
2725  */
2726  if (rte->alias &&
2727  attnum > 0 && attnum <= list_length(rte->alias->colnames))
2728  return strVal(list_nth(rte->alias->colnames, attnum - 1));
2729 
2730  /*
2731  * If the RTE is a relation, go to the system catalogs not the
2732  * eref->colnames list. This is a little slower but it will give the
2733  * right answer if the column has been renamed since the eref list was
2734  * built (which can easily happen for rules).
2735  */
2736  if (rte->rtekind == RTE_RELATION)
2737  return get_attname(rte->relid, attnum, false);
2738 
2739  /*
2740  * Otherwise use the column name from eref. There should always be one.
2741  */
2742  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
2743  return strVal(list_nth(rte->eref->colnames, attnum - 1));
2744 
2745  /* else caller gave us a bogus attnum */
2746  elog(ERROR, "invalid attnum %d for rangetable entry %s",
2747  attnum, rte->eref->aliasname);
2748  return NULL; /* keep compiler quiet */
2749 }
Alias * alias
Definition: parsenodes.h:1091
List * colnames
Definition: primnodes.h:43
#define strVal(v)
Definition: value.h:54
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
int16 attnum
Definition: pg_attribute.h:79
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:169
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:974
#define elog(elevel,...)
Definition: elog.h:228
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:775
Alias * eref
Definition: parsenodes.h:1092

◆ get_rte_attribute_type()

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

Definition at line 2756 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, 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_RESULT, 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().

2758 {
2759  switch (rte->rtekind)
2760  {
2761  case RTE_RELATION:
2762  {
2763  /* Plain relation RTE --- get the attribute's type info */
2764  HeapTuple tp;
2765  Form_pg_attribute att_tup;
2766 
2767  tp = SearchSysCache2(ATTNUM,
2768  ObjectIdGetDatum(rte->relid),
2770  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
2771  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2772  attnum, rte->relid);
2773  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
2774 
2775  /*
2776  * If dropped column, pretend it ain't there. See notes in
2777  * scanRTEForColumn.
2778  */
2779  if (att_tup->attisdropped)
2780  ereport(ERROR,
2781  (errcode(ERRCODE_UNDEFINED_COLUMN),
2782  errmsg("column \"%s\" of relation \"%s\" does not exist",
2783  NameStr(att_tup->attname),
2784  get_rel_name(rte->relid))));
2785  *vartype = att_tup->atttypid;
2786  *vartypmod = att_tup->atttypmod;
2787  *varcollid = att_tup->attcollation;
2788  ReleaseSysCache(tp);
2789  }
2790  break;
2791  case RTE_SUBQUERY:
2792  {
2793  /* Subselect RTE --- get type info from subselect's tlist */
2795  attnum);
2796 
2797  if (te == NULL || te->resjunk)
2798  elog(ERROR, "subquery %s does not have attribute %d",
2799  rte->eref->aliasname, attnum);
2800  *vartype = exprType((Node *) te->expr);
2801  *vartypmod = exprTypmod((Node *) te->expr);
2802  *varcollid = exprCollation((Node *) te->expr);
2803  }
2804  break;
2805  case RTE_FUNCTION:
2806  {
2807  /* Function RTE */
2808  ListCell *lc;
2809  int atts_done = 0;
2810 
2811  /* Identify which function covers the requested column */
2812  foreach(lc, rte->functions)
2813  {
2814  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2815 
2816  if (attnum > atts_done &&
2817  attnum <= atts_done + rtfunc->funccolcount)
2818  {
2819  TypeFuncClass functypclass;
2820  Oid funcrettype;
2821  TupleDesc tupdesc;
2822 
2823  attnum -= atts_done; /* now relative to this func */
2824  functypclass = get_expr_result_type(rtfunc->funcexpr,
2825  &funcrettype,
2826  &tupdesc);
2827 
2828  if (functypclass == TYPEFUNC_COMPOSITE ||
2829  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
2830  {
2831  /* Composite data type, e.g. a table's row type */
2832  Form_pg_attribute att_tup;
2833 
2834  Assert(tupdesc);
2835  Assert(attnum <= tupdesc->natts);
2836  att_tup = TupleDescAttr(tupdesc, attnum - 1);
2837 
2838  /*
2839  * If dropped column, pretend it ain't there. See
2840  * notes in scanRTEForColumn.
2841  */
2842  if (att_tup->attisdropped)
2843  ereport(ERROR,
2844  (errcode(ERRCODE_UNDEFINED_COLUMN),
2845  errmsg("column \"%s\" of relation \"%s\" does not exist",
2846  NameStr(att_tup->attname),
2847  rte->eref->aliasname)));
2848  *vartype = att_tup->atttypid;
2849  *vartypmod = att_tup->atttypmod;
2850  *varcollid = att_tup->attcollation;
2851  }
2852  else if (functypclass == TYPEFUNC_SCALAR)
2853  {
2854  /* Base data type, i.e. scalar */
2855  *vartype = funcrettype;
2856  *vartypmod = -1;
2857  *varcollid = exprCollation(rtfunc->funcexpr);
2858  }
2859  else if (functypclass == TYPEFUNC_RECORD)
2860  {
2861  *vartype = list_nth_oid(rtfunc->funccoltypes,
2862  attnum - 1);
2863  *vartypmod = list_nth_int(rtfunc->funccoltypmods,
2864  attnum - 1);
2865  *varcollid = list_nth_oid(rtfunc->funccolcollations,
2866  attnum - 1);
2867  }
2868  else
2869  {
2870  /*
2871  * addRangeTableEntryForFunction should've caught
2872  * this
2873  */
2874  elog(ERROR, "function in FROM has unsupported return type");
2875  }
2876  return;
2877  }
2878  atts_done += rtfunc->funccolcount;
2879  }
2880 
2881  /* If we get here, must be looking for the ordinality column */
2882  if (rte->funcordinality && attnum == atts_done + 1)
2883  {
2884  *vartype = INT8OID;
2885  *vartypmod = -1;
2886  *varcollid = InvalidOid;
2887  return;
2888  }
2889 
2890  /* this probably can't happen ... */
2891  ereport(ERROR,
2892  (errcode(ERRCODE_UNDEFINED_COLUMN),
2893  errmsg("column %d of relation \"%s\" does not exist",
2894  attnum,
2895  rte->eref->aliasname)));
2896  }
2897  break;
2898  case RTE_JOIN:
2899  {
2900  /*
2901  * Join RTE --- get type info from join RTE's alias variable
2902  */
2903  Node *aliasvar;
2904 
2905  Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
2906  aliasvar = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
2907  Assert(aliasvar != NULL);
2908  *vartype = exprType(aliasvar);
2909  *vartypmod = exprTypmod(aliasvar);
2910  *varcollid = exprCollation(aliasvar);
2911  }
2912  break;
2913  case RTE_TABLEFUNC:
2914  case RTE_VALUES:
2915  case RTE_CTE:
2916  case RTE_NAMEDTUPLESTORE:
2917  {
2918  /*
2919  * tablefunc, VALUES, CTE, or ENR RTE --- get type info from
2920  * lists in the RTE
2921  */
2922  Assert(attnum > 0 && attnum <= list_length(rte->coltypes));
2923  *vartype = list_nth_oid(rte->coltypes, attnum - 1);
2924  *vartypmod = list_nth_int(rte->coltypmods, attnum - 1);
2925  *varcollid = list_nth_oid(rte->colcollations, attnum - 1);
2926 
2927  /* For ENR, better check for dropped column */
2928  if (!OidIsValid(*vartype))
2929  ereport(ERROR,
2930  (errcode(ERRCODE_UNDEFINED_COLUMN),
2931  errmsg("column %d of relation \"%s\" does not exist",
2932  attnum,
2933  rte->eref->aliasname)));
2934  }
2935  break;
2936  case RTE_RESULT:
2937  /* this probably can't happen ... */
2938  ereport(ERROR,
2939  (errcode(ERRCODE_UNDEFINED_COLUMN),
2940  errmsg("column %d of relation \"%s\" does not exist",
2941  attnum,
2942  rte->eref->aliasname)));
2943  break;
2944  default:
2945  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
2946  }
2947 }
List * joinaliasvars
Definition: parsenodes.h:1030
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
List * coltypmods
Definition: parsenodes.h:1079
#define Int16GetDatum(X)
Definition: postgres.h:451
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
bool funcordinality
Definition: parsenodes.h:1041
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
#define OidIsValid(objectId)
Definition: c.h:639
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:288
List * colcollations
Definition: parsenodes.h:1080
List * targetList
Definition: parsenodes.h:140
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
bool resjunk
Definition: primnodes.h:1400
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
TypeFuncClass
Definition: funcapi.h:147
#define ereport(elevel, rest)
Definition: elog.h:141
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define InvalidOid
Definition: postgres_ext.h:36
List * funccoltypmods
Definition: parsenodes.h:1130
int16 attnum
Definition: pg_attribute.h:79
List * funccolcollations
Definition: parsenodes.h:1131
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:733
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1040
Expr * expr
Definition: primnodes.h:1393
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1127
RTEKind rtekind
Definition: parsenodes.h:974
Query * subquery
Definition: parsenodes.h:1009
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define NameStr(name)
Definition: c.h:610
Alias * eref
Definition: parsenodes.h:1092
List * coltypes
Definition: parsenodes.h:1078
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730

◆ get_tle_by_resno()

◆ GetCTEForRTE()

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

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

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

◆ GetRTEByRangeTablePosn()

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

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

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

◆ isFutureCTE()

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

Definition at line 270 of file parse_relation.c.

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

Referenced by parserOpenTable().

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

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

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

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

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3381 of file parse_relation.c.

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

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

◆ isQueryUsingTempRelation_walker()

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

Definition at line 3387 of file parse_relation.c.

References AccessShareLock, expression_tree_walker(), IsA, lfirst, QTW_IGNORE_JOINALIASES, query_tree_walker(), RelationData::rd_rel, RangeTblEntry::relid, relpersistence, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, table_close(), and table_open().

Referenced by isQueryUsingTempRelation().

3388 {
3389  if (node == NULL)
3390  return false;
3391 
3392  if (IsA(node, Query))
3393  {
3394  Query *query = (Query *) node;
3395  ListCell *rtable;
3396 
3397  foreach(rtable, query->rtable)
3398  {
3399  RangeTblEntry *rte = lfirst(rtable);
3400 
3401  if (rte->rtekind == RTE_RELATION)
3402  {
3404  char relpersistence = rel->rd_rel->relpersistence;
3405 
3407  if (relpersistence == RELPERSISTENCE_TEMP)
3408  return true;
3409  }
3410  }
3411 
3412  return query_tree_walker(query,
3414  context,
3416  }
3417 
3418  return expression_tree_walker(node,
3420  context);
3421 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2273
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define AccessShareLock
Definition: lockdefs.h:36
Form_pg_class rd_rel
Definition: rel.h:83
List * rtable
Definition: parsenodes.h:137
char relpersistence
Definition: pg_class.h:78
#define lfirst(lc)
Definition: pg_list.h:190
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839
RTEKind rtekind
Definition: parsenodes.h:974
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23

◆ markRTEForSelectPriv()

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

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

924 {
925  if (rte == NULL)
926  rte = rt_fetch(rtindex, pstate->p_rtable);
927 
928  if (rte->rtekind == RTE_RELATION)
929  {
930  /* Make sure the rel as a whole is marked for SELECT access */
931  rte->requiredPerms |= ACL_SELECT;
932  /* Must offset the attnum to fit in a bitmapset */
935  }
936  else if (rte->rtekind == RTE_JOIN)
937  {
938  if (col == InvalidAttrNumber)
939  {
940  /*
941  * A whole-row reference to a join has to be treated as whole-row
942  * references to the two inputs.
943  */
944  JoinExpr *j;
945 
946  if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
947  j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
948  else
949  j = NULL;
950  if (j == NULL)
951  elog(ERROR, "could not find JoinExpr for whole-row reference");
952 
953  /* Note: we can't see FromExpr here */
954  if (IsA(j->larg, RangeTblRef))
955  {
956  int varno = ((RangeTblRef *) j->larg)->rtindex;
957 
958  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
959  }
960  else if (IsA(j->larg, JoinExpr))
961  {
962  int varno = ((JoinExpr *) j->larg)->rtindex;
963 
964  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
965  }
966  else
967  elog(ERROR, "unrecognized node type: %d",
968  (int) nodeTag(j->larg));
969  if (IsA(j->rarg, RangeTblRef))
970  {
971  int varno = ((RangeTblRef *) j->rarg)->rtindex;
972 
973  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
974  }
975  else if (IsA(j->rarg, JoinExpr))
976  {
977  int varno = ((JoinExpr *) j->rarg)->rtindex;
978 
979  markRTEForSelectPriv(pstate, NULL, varno, InvalidAttrNumber);
980  }
981  else
982  elog(ERROR, "unrecognized node type: %d",
983  (int) nodeTag(j->rarg));
984  }
985  else
986  {
987  /*
988  * Regular join attribute, look at the alias-variable list.
989  *
990  * The aliasvar could be either a Var or a COALESCE expression,
991  * but in the latter case we should already have marked the two
992  * referent variables as being selected, due to their use in the
993  * JOIN clause. So we need only be concerned with the Var case.
994  * But we do need to drill down through implicit coercions.
995  */
996  Var *aliasvar;
997 
998  Assert(col > 0 && col <= list_length(rte->joinaliasvars));
999  aliasvar = (Var *) list_nth(rte->joinaliasvars, col - 1);
1000  aliasvar = (Var *) strip_implicit_coercions((Node *) aliasvar);
1001  if (aliasvar && IsA(aliasvar, Var))
1002  markVarForSelectPriv(pstate, aliasvar, NULL);
1003  }
1004  }
1005  /* other RTE types don't require privilege marking */
1006 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
List * joinaliasvars
Definition: parsenodes.h:1030
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
Definition: nodes.h:525
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
AclMode requiredPerms
Definition: parsenodes.h:1096
Definition: primnodes.h:167
Node * larg
Definition: primnodes.h:1476
Bitmapset * selectedCols
Definition: parsenodes.h:1098
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
#define list_nth_node(type, list, n)
Definition: pg_list.h:305
#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:75
Node * rarg
Definition: primnodes.h:1477
#define Assert(condition)
Definition: c.h:733
static int list_length(const List *l)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define InvalidAttrNumber
Definition: attnum.h:23
#define nodeTag(nodeptr)
Definition: nodes.h:530
RTEKind rtekind
Definition: parsenodes.h:974
#define elog(elevel,...)
Definition: elog.h:228
List * p_joinexprs
Definition: parse_node.h:178
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:611
List * p_rtable
Definition: parse_node.h:177

◆ markVarForSelectPriv()

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

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

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

◆ parserOpenTable()

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

Definition at line 1162 of file parse_relation.c.

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

Referenced by addRangeTableEntry(), and setTargetTable().

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

◆ refnameRangeTblEntry()

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

Definition at line 85 of file parse_relation.c.

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

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

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

◆ RTERangeTablePosn()

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

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

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

◆ scanNameSpaceForCTE()

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

Definition at line 239 of file parse_relation.c.

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

Referenced by getRTEForSpecialRelationTypes(), and searchRangeTableForRel().

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

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 292 of file parse_relation.c.

References name_matches_visible_ENR().

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

293 {
294  return name_matches_visible_ENR(pstate, refname);
295 }
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 156 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().

157 {
158  RangeTblEntry *result = NULL;
159  ListCell *l;
160 
161  foreach(l, pstate->p_namespace)
162  {
164  RangeTblEntry *rte = nsitem->p_rte;
165 
166  /* Ignore columns-only items */
167  if (!nsitem->p_rel_visible)
168  continue;
169  /* If not inside LATERAL, ignore lateral-only items */
170  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
171  continue;
172 
173  if (strcmp(rte->eref->aliasname, refname) == 0)
174  {
175  if (result)
176  ereport(ERROR,
177  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
178  errmsg("table reference \"%s\" is ambiguous",
179  refname),
180  parser_errposition(pstate, location)));
181  check_lateral_ref_ok(pstate, nsitem, location);
182  result = rte;
183  }
184  }
185  return result;
186 }
int errcode(int sqlerrcode)
Definition: elog.c:608
RangeTblEntry * p_rte
Definition: parse_node.h:251
#define ERROR
Definition: elog.h:43
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
List * p_namespace
Definition: parse_node.h:181
#define ereport(elevel, rest)
Definition: elog.h:141
bool p_lateral_active
Definition: parse_node.h:183
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int errmsg(const char *fmt,...)
Definition: elog.c:822
Alias * eref
Definition: parsenodes.h:1092

◆ scanNameSpaceForRelid()

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

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

198 {
199  RangeTblEntry *result = NULL;
200  ListCell *l;
201 
202  foreach(l, pstate->p_namespace)
203  {
205  RangeTblEntry *rte = nsitem->p_rte;
206 
207  /* Ignore columns-only items */
208  if (!nsitem->p_rel_visible)
209  continue;
210  /* If not inside LATERAL, ignore lateral-only items */
211  if (nsitem->p_lateral_only && !pstate->p_lateral_active)
212  continue;
213 
214  /* yes, the test for alias == NULL should be there... */
215  if (rte->rtekind == RTE_RELATION &&
216  rte->relid == relid &&
217  rte->alias == NULL)
218  {
219  if (result)
220  ereport(ERROR,
221  (errcode(ERRCODE_AMBIGUOUS_ALIAS),
222  errmsg("table reference %u is ambiguous",
223  relid),
224  parser_errposition(pstate, location)));
225  check_lateral_ref_ok(pstate, nsitem, location);
226  result = rte;
227  }
228  }
229  return result;
230 }
Alias * alias
Definition: parsenodes.h:1091
int errcode(int sqlerrcode)
Definition: elog.c:608
RangeTblEntry * p_rte
Definition: parse_node.h:251
#define ERROR
Definition: elog.h:43
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
List * p_namespace
Definition: parse_node.h:181
#define ereport(elevel, rest)
Definition: elog.h:141
bool p_lateral_active
Definition: parse_node.h:183
#define lfirst(lc)
Definition: pg_list.h:190
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
RTEKind rtekind
Definition: parsenodes.h:974
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ scanRTEForColumn()

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

Definition at line 657 of file parse_relation.c.

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

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

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

◆ searchRangeTableForCol()

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

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

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

◆ searchRangeTableForRel()

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

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

314 {
315  const char *refname = relation->relname;
316  Oid relId = InvalidOid;
317  CommonTableExpr *cte = NULL;
318  bool isenr = false;
319  Index ctelevelsup = 0;
320  Index levelsup;
321 
322  /*
323  * If it's an unqualified name, check for possible CTE matches. A CTE
324  * hides any real relation matches. If no CTE, look for a matching
325  * relation.
326  *
327  * NB: It's not critical that RangeVarGetRelid return the correct answer
328  * here in the face of concurrent DDL. If it doesn't, the worst case
329  * scenario is a less-clear error message. Also, the tables involved in
330  * the query are already locked, which reduces the number of cases in
331  * which surprising behavior can occur. So we do the name lookup
332  * unlocked.
333  */
334  if (!relation->schemaname)
335  {
336  cte = scanNameSpaceForCTE(pstate, refname, &ctelevelsup);
337  if (!cte)
338  isenr = scanNameSpaceForENR(pstate, refname);
339  }
340 
341  if (!cte && !isenr)
342  relId = RangeVarGetRelid(relation, NoLock, true);
343 
344  /* Now look for RTEs matching either the relation/CTE/ENR or the alias */
345  for (levelsup = 0;
346  pstate != NULL;
347  pstate = pstate->parentParseState, levelsup++)
348  {
349  ListCell *l;
350 
351  foreach(l, pstate->p_rtable)
352  {
353  RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
354 
355  if (rte->rtekind == RTE_RELATION &&
356  OidIsValid(relId) &&
357  rte->relid == relId)
358  return rte;
359  if (rte->rtekind == RTE_CTE &&
360  cte != NULL &&
361  rte->ctelevelsup + levelsup == ctelevelsup &&
362  strcmp(rte->ctename, refname) == 0)
363  return rte;
364  if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
365  isenr &&