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/table.h"
#include "catalog/heap.h"
#include "catalog/namespace.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/syscache.h"
#include "utils/varlena.h"
Include dependency graph for parse_relation.c:

Go to the source code of this file.

Data Structures

struct  FuzzyAttrMatchState
 

Macros

#define MAX_FUZZY_DISTANCE   3
 

Functions

static ParseNamespaceItemscanNameSpaceForRefname (ParseState *pstate, const char *refname, int location)
 
static ParseNamespaceItemscanNameSpaceForRelid (ParseState *pstate, Oid relid, int location)
 
static void check_lateral_ref_ok (ParseState *pstate, ParseNamespaceItem *nsitem, int location)
 
static int scanRTEForColumn (ParseState *pstate, RangeTblEntry *rte, Alias *eref, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
 
static void markRTEForSelectPriv (ParseState *pstate, int rtindex, AttrNumber col)
 
static void expandRelation (Oid relid, Alias *eref, int rtindex, int sublevels_up, VarReturningType returning_type, 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, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
 
static int specialAttNum (const char *attname)
 
static bool rte_visible_if_lateral (ParseState *pstate, RangeTblEntry *rte)
 
static bool rte_visible_if_qualified (ParseState *pstate, RangeTblEntry *rte)
 
ParseNamespaceItemrefnameNamespaceItem (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)
 
ParseNamespaceItemGetNSItemByRangeTablePosn (ParseState *pstate, int varno, 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)
 
NodescanNSItemForColumn (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
 
NodecolNameToVar (ParseState *pstate, const char *colname, bool localonly, int location)
 
static FuzzyAttrMatchStatesearchRangeTableForCol (ParseState *pstate, const char *alias, const char *colname, int location)
 
void markNullableIfNeeded (ParseState *pstate, Var *var)
 
void markVarForSelectPriv (ParseState *pstate, Var *var)
 
static void buildRelationAliases (TupleDesc tupdesc, Alias *alias, Alias *eref)
 
static char * chooseScalarFunctionAlias (Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
 
static ParseNamespaceItembuildNSItemFromTupleDesc (RangeTblEntry *rte, Index rtindex, RTEPermissionInfo *perminfo, TupleDesc tupdesc)
 
static ParseNamespaceItembuildNSItemFromLists (RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
 
Relation parserOpenTable (ParseState *pstate, const RangeVar *relation, int lockmode)
 
ParseNamespaceItemaddRangeTableEntry (ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForRelation (ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForSubquery (ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForFunction (ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForTableFunc (ParseState *pstate, TableFunc *tf, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForValues (ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForJoin (ParseState *pstate, List *colnames, ParseNamespaceColumn *nscolumns, JoinType jointype, int nummergedcols, List *aliasvars, List *leftcols, List *rightcols, Alias *join_using_alias, Alias *alias, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForCTE (ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForENR (ParseState *pstate, RangeVar *rv, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForGroup (ParseState *pstate, List *groupClauses)
 
bool isLockedRefname (ParseState *pstate, const char *refname)
 
void addNSItemToQuery (ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
 
void expandRTE (RangeTblEntry *rte, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
 
ListexpandNSItemVars (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
 
ListexpandNSItemAttrs (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, bool require_col_privs, int location)
 
char * get_rte_attribute_name (RangeTblEntry *rte, AttrNumber attnum)
 
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)
 
static ParseNamespaceItemfindNSItemForRTE (ParseState *pstate, RangeTblEntry *rte)
 
RTEPermissionInfoaddRTEPermissionInfo (List **rteperminfos, RangeTblEntry *rte)
 
RTEPermissionInfogetRTEPermissionInfo (List *rteperminfos, RangeTblEntry *rte)
 

Macro Definition Documentation

◆ MAX_FUZZY_DISTANCE

#define MAX_FUZZY_DISTANCE   3

Definition at line 72 of file parse_relation.c.

Function Documentation

◆ addNSItemToQuery()

void addNSItemToQuery ( ParseState pstate,
ParseNamespaceItem nsitem,
bool  addToJoinList,
bool  addToRelNameSpace,
bool  addToVarNameSpace 
)

Definition at line 2706 of file parse_relation.c.

2709{
2710 if (addToJoinList)
2711 {
2713
2714 rtr->rtindex = nsitem->p_rtindex;
2715 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
2716 }
2717 if (addToRelNameSpace || addToVarNameSpace)
2718 {
2719 /* Set the new nsitem's visibility flags correctly */
2720 nsitem->p_rel_visible = addToRelNameSpace;
2721 nsitem->p_cols_visible = addToVarNameSpace;
2722 nsitem->p_lateral_only = false;
2723 nsitem->p_lateral_ok = true;
2724 pstate->p_namespace = lappend(pstate->p_namespace, nsitem);
2725 }
2726}
List * lappend(List *list, void *datum)
Definition: list.c:339
#define makeNode(_type_)
Definition: nodes.h:161
List * p_namespace
Definition: parse_node.h:203
List * p_joinlist
Definition: parse_node.h:201

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_rtindex, and RangeTblRef::rtindex.

Referenced by addNSItemForReturning(), AddRelationNewConstraints(), AlterPolicy(), CreatePolicy(), CreateTriggerFiringOn(), DefineRelation(), DoCopy(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformInsertStmt(), transformMergeStmt(), transformOnConflictClause(), transformPartitionSpec(), TransformPubWhereClauses(), transformRuleStmt(), transformSetOperationStmt(), transformStatsStmt(), and transformValuesClause().

◆ addRangeTableEntry()

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

Definition at line 1483 of file parse_relation.c.

1488{
1490 RTEPermissionInfo *perminfo;
1491 char *refname = alias ? alias->aliasname : relation->relname;
1492 LOCKMODE lockmode;
1493 Relation rel;
1494 ParseNamespaceItem *nsitem;
1495
1496 Assert(pstate != NULL);
1497
1498 rte->rtekind = RTE_RELATION;
1499 rte->alias = alias;
1500
1501 /*
1502 * Identify the type of lock we'll need on this relation. It's not the
1503 * query's target table (that case is handled elsewhere), so we need
1504 * either RowShareLock if it's locked by FOR UPDATE/SHARE, or plain
1505 * AccessShareLock otherwise.
1506 */
1507 lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock;
1508
1509 /*
1510 * Get the rel's OID. This access also ensures that we have an up-to-date
1511 * relcache entry for the rel. Since this is typically the first access
1512 * to a rel in a statement, we must open the rel with the proper lockmode.
1513 */
1514 rel = parserOpenTable(pstate, relation, lockmode);
1515 rte->relid = RelationGetRelid(rel);
1516 rte->inh = inh;
1517 rte->relkind = rel->rd_rel->relkind;
1518 rte->rellockmode = lockmode;
1519
1520 /*
1521 * Build the list of effective column names using user-supplied aliases
1522 * and/or actual column names.
1523 */
1524 rte->eref = makeAlias(refname, NIL);
1525 buildRelationAliases(rel->rd_att, alias, rte->eref);
1526
1527 /*
1528 * Set flags and initialize access permissions.
1529 *
1530 * The initial default on access checks is always check-for-READ-access,
1531 * which is the right thing for all except target tables.
1532 */
1533 rte->lateral = false;
1534 rte->inFromCl = inFromCl;
1535
1536 perminfo = addRTEPermissionInfo(&pstate->p_rteperminfos, rte);
1537 perminfo->requiredPerms = ACL_SELECT;
1538
1539 /*
1540 * Add completed RTE to pstate's range table list, so that we know its
1541 * index. But we don't add it to the join list --- caller must do that if
1542 * appropriate.
1543 */
1544 pstate->p_rtable = lappend(pstate->p_rtable, rte);
1545
1546 /*
1547 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1548 * list --- caller must do that if appropriate.
1549 */
1550 nsitem = buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1551 perminfo, rel->rd_att);
1552
1553 /*
1554 * Drop the rel refcount, but keep the access lock till end of transaction
1555 * so that the table can't be deleted or have its schema modified
1556 * underneath us.
1557 */
1558 table_close(rel, NoLock);
1559
1560 return nsitem;
1561}
Assert(PointerIsAligned(start, uint64))
int LOCKMODE
Definition: lockdefs.h:26
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
#define RowShareLock
Definition: lockdefs.h:37
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:438
static ParseNamespaceItem * buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, RTEPermissionInfo *perminfo, TupleDesc tupdesc)
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
bool isLockedRefname(ParseState *pstate, const char *refname)
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
@ RTE_RELATION
Definition: parsenodes.h:1043
#define ACL_SELECT
Definition: parsenodes.h:77
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define RelationGetRelid(relation)
Definition: rel.h:515
char * aliasname
Definition: primnodes.h:51
List * p_rteperminfos
Definition: parse_node.h:197
List * p_rtable
Definition: parse_node.h:196
AclMode requiredPerms
Definition: parsenodes.h:1322
RTEKind rtekind
Definition: parsenodes.h:1078
char * relname
Definition: primnodes.h:83
TupleDesc rd_att
Definition: rel.h:112
Form_pg_class rd_rel
Definition: rel.h:111
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126

References AccessShareLock, ACL_SELECT, addRTEPermissionInfo(), Alias::aliasname, Assert(), buildNSItemFromTupleDesc(), buildRelationAliases(), RangeTblEntry::inh, isLockedRefname(), lappend(), list_length(), makeAlias(), makeNode, NIL, NoLock, ParseState::p_rtable, ParseState::p_rteperminfos, parserOpenTable(), RelationData::rd_att, RelationData::rd_rel, RelationGetRelid, RangeVar::relname, RTEPermissionInfo::requiredPerms, RowShareLock, RTE_RELATION, RangeTblEntry::rtekind, and table_close().

Referenced by transformTableEntry().

◆ addRangeTableEntryForCTE()

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

Definition at line 2328 of file parse_relation.c.

2333{
2335 Alias *alias = rv->alias;
2336 char *refname = alias ? alias->aliasname : cte->ctename;
2337 Alias *eref;
2338 int numaliases;
2339 int varattno;
2340 ListCell *lc;
2341 int n_dontexpand_columns = 0;
2342 ParseNamespaceItem *psi;
2343
2344 Assert(pstate != NULL);
2345
2346 rte->rtekind = RTE_CTE;
2347 rte->ctename = cte->ctename;
2348 rte->ctelevelsup = levelsup;
2349
2350 /* Self-reference if and only if CTE's parse analysis isn't completed */
2351 rte->self_reference = !IsA(cte->ctequery, Query);
2352 Assert(cte->cterecursive || !rte->self_reference);
2353 /* Bump the CTE's refcount if this isn't a self-reference */
2354 if (!rte->self_reference)
2355 cte->cterefcount++;
2356
2357 /*
2358 * We throw error if the CTE is INSERT/UPDATE/DELETE/MERGE without
2359 * RETURNING. This won't get checked in case of a self-reference, but
2360 * that's OK because data-modifying CTEs aren't allowed to be recursive
2361 * anyhow.
2362 */
2363 if (IsA(cte->ctequery, Query))
2364 {
2365 Query *ctequery = (Query *) cte->ctequery;
2366
2367 if (ctequery->commandType != CMD_SELECT &&
2368 ctequery->returningList == NIL)
2369 ereport(ERROR,
2370 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2371 errmsg("WITH query \"%s\" does not have a RETURNING clause",
2372 cte->ctename),
2373 parser_errposition(pstate, rv->location)));
2374 }
2375
2376 rte->coltypes = list_copy(cte->ctecoltypes);
2377 rte->coltypmods = list_copy(cte->ctecoltypmods);
2378 rte->colcollations = list_copy(cte->ctecolcollations);
2379
2380 rte->alias = alias;
2381 if (alias)
2382 eref = copyObject(alias);
2383 else
2384 eref = makeAlias(refname, NIL);
2385 numaliases = list_length(eref->colnames);
2386
2387 /* fill in any unspecified alias columns */
2388 varattno = 0;
2389 foreach(lc, cte->ctecolnames)
2390 {
2391 varattno++;
2392 if (varattno > numaliases)
2393 eref->colnames = lappend(eref->colnames, lfirst(lc));
2394 }
2395 if (varattno < numaliases)
2396 ereport(ERROR,
2397 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2398 errmsg("table \"%s\" has %d columns available but %d columns specified",
2399 refname, varattno, numaliases)));
2400
2401 rte->eref = eref;
2402
2403 if (cte->search_clause)
2404 {
2405 rte->eref->colnames = lappend(rte->eref->colnames, makeString(cte->search_clause->search_seq_column));
2406 if (cte->search_clause->search_breadth_first)
2407 rte->coltypes = lappend_oid(rte->coltypes, RECORDOID);
2408 else
2409 rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2410 rte->coltypmods = lappend_int(rte->coltypmods, -1);
2411 rte->colcollations = lappend_oid(rte->colcollations, InvalidOid);
2412
2413 n_dontexpand_columns += 1;
2414 }
2415
2416 if (cte->cycle_clause)
2417 {
2418 rte->eref->colnames = lappend(rte->eref->colnames, makeString(cte->cycle_clause->cycle_mark_column));
2419 rte->coltypes = lappend_oid(rte->coltypes, cte->cycle_clause->cycle_mark_type);
2420 rte->coltypmods = lappend_int(rte->coltypmods, cte->cycle_clause->cycle_mark_typmod);
2421 rte->colcollations = lappend_oid(rte->colcollations, cte->cycle_clause->cycle_mark_collation);
2422
2423 rte->eref->colnames = lappend(rte->eref->colnames, makeString(cte->cycle_clause->cycle_path_column));
2424 rte->coltypes = lappend_oid(rte->coltypes, RECORDARRAYOID);
2425 rte->coltypmods = lappend_int(rte->coltypmods, -1);
2426 rte->colcollations = lappend_oid(rte->colcollations, InvalidOid);
2427
2428 n_dontexpand_columns += 2;
2429 }
2430
2431 /*
2432 * Set flags and access permissions.
2433 *
2434 * Subqueries are never checked for access rights, so no need to perform
2435 * addRTEPermissionInfo().
2436 */
2437 rte->lateral = false;
2438 rte->inFromCl = inFromCl;
2439
2440 /*
2441 * Add completed RTE to pstate's range table list, so that we know its
2442 * index. But we don't add it to the join list --- caller must do that if
2443 * appropriate.
2444 */
2445 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2446
2447 /*
2448 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2449 * list --- caller must do that if appropriate.
2450 */
2451 psi = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2452 rte->coltypes, rte->coltypmods,
2453 rte->colcollations);
2454
2455 /*
2456 * The columns added by search and cycle clauses are not included in star
2457 * expansion in queries contained in the CTE.
2458 */
2459 if (rte->ctelevelsup > 0)
2460 for (int i = 0; i < n_dontexpand_columns; i++)
2461 psi->p_nscolumns[list_length(psi->p_names->colnames) - 1 - i].p_dontexpand = true;
2462
2463 return psi;
2464}
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
int i
Definition: isn.c:77
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
List * list_copy(const List *oldlist)
Definition: list.c:1573
List * lappend_int(List *list, int datum)
Definition: list.c:357
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define copyObject(obj)
Definition: nodes.h:232
@ CMD_SELECT
Definition: nodes.h:275
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
static ParseNamespaceItem * buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, List *coltypes, List *coltypmods, List *colcollations)
@ RTE_CTE
Definition: parsenodes.h:1049
#define lfirst(lc)
Definition: pg_list.h:172
#define InvalidOid
Definition: postgres_ext.h:37
List * colnames
Definition: primnodes.h:52
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:299
List * returningList
Definition: parsenodes.h:214
CmdType commandType
Definition: parsenodes.h:121
char * ctename
Definition: parsenodes.h:1227
Index ctelevelsup
Definition: parsenodes.h:1229
Alias * alias
Definition: primnodes.h:92
ParseLoc location
Definition: primnodes.h:95
String * makeString(char *str)
Definition: value.c:63

References RangeVar::alias, Alias::aliasname, Assert(), buildNSItemFromLists(), CMD_SELECT, Alias::colnames, Query::commandType, copyObject, RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, CommonTableExpr::ctename, CommonTableExpr::ctequery, ereport, errcode(), errmsg(), ERROR, i, if(), InvalidOid, IsA, lappend(), lappend_int(), lappend_oid(), lfirst, list_copy(), list_length(), RangeVar::location, makeAlias(), makeNode, makeString(), NIL, ParseNamespaceColumn::p_dontexpand, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseState::p_rtable, parser_errposition(), Query::returningList, RTE_CTE, and RangeTblEntry::rtekind.

Referenced by getNSItemForSpecialRelationTypes().

◆ addRangeTableEntryForENR()

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

Definition at line 2480 of file parse_relation.c.

2483{
2485 Alias *alias = rv->alias;
2486 char *refname = alias ? alias->aliasname : rv->relname;
2488 TupleDesc tupdesc;
2489 int attno;
2490
2491 Assert(pstate != NULL);
2492 enrmd = get_visible_ENR(pstate, rv->relname);
2493 Assert(enrmd != NULL);
2494
2495 switch (enrmd->enrtype)
2496 {
2499 break;
2500
2501 default:
2502 elog(ERROR, "unexpected enrtype: %d", enrmd->enrtype);
2503 return NULL; /* for fussy compilers */
2504 }
2505
2506 /*
2507 * Record dependency on a relation. This allows plans to be invalidated
2508 * if they access transition tables linked to a table that is altered.
2509 */
2510 rte->relid = enrmd->reliddesc;
2511
2512 /*
2513 * Build the list of effective column names using user-supplied aliases
2514 * and/or actual column names.
2515 */
2516 tupdesc = ENRMetadataGetTupDesc(enrmd);
2517 rte->eref = makeAlias(refname, NIL);
2518 buildRelationAliases(tupdesc, alias, rte->eref);
2519
2520 /* Record additional data for ENR, including column type info */
2521 rte->enrname = enrmd->name;
2522 rte->enrtuples = enrmd->enrtuples;
2523 rte->coltypes = NIL;
2524 rte->coltypmods = NIL;
2525 rte->colcollations = NIL;
2526 for (attno = 1; attno <= tupdesc->natts; ++attno)
2527 {
2528 Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
2529
2530 if (att->attisdropped)
2531 {
2532 /* Record zeroes for a dropped column */
2533 rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
2534 rte->coltypmods = lappend_int(rte->coltypmods, 0);
2535 rte->colcollations = lappend_oid(rte->colcollations, InvalidOid);
2536 }
2537 else
2538 {
2539 /* Let's just make sure we can tell this isn't dropped */
2540 if (att->atttypid == InvalidOid)
2541 elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
2542 rv->relname);
2543 rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
2544 rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
2545 rte->colcollations = lappend_oid(rte->colcollations,
2546 att->attcollation);
2547 }
2548 }
2549
2550 /*
2551 * Set flags and access permissions.
2552 *
2553 * ENRs are never checked for access rights, so no need to perform
2554 * addRTEPermissionInfo().
2555 */
2556 rte->lateral = false;
2557 rte->inFromCl = inFromCl;
2558
2559 /*
2560 * Add completed RTE to pstate's range table list, so that we know its
2561 * index. But we don't add it to the join list --- caller must do that if
2562 * appropriate.
2563 */
2564 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2565
2566 /*
2567 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2568 * list --- caller must do that if appropriate.
2569 */
2570 return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable), NULL,
2571 tupdesc);
2572}
#define elog(elevel,...)
Definition: elog.h:226
EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:26
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1050
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
@ ENR_NAMED_TUPLESTORE
EphemeralNameRelationType enrtype
char * enrname
Definition: parsenodes.h:1262
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160

References RangeVar::alias, Alias::aliasname, Assert(), buildNSItemFromTupleDesc(), buildRelationAliases(), elog, ENR_NAMED_TUPLESTORE, ENRMetadataGetTupDesc(), RangeTblEntry::enrname, EphemeralNamedRelationMetadataData::enrtuples, EphemeralNamedRelationMetadataData::enrtype, ERROR, get_visible_ENR(), InvalidOid, lappend(), lappend_int(), lappend_oid(), list_length(), makeAlias(), makeNode, EphemeralNamedRelationMetadataData::name, TupleDescData::natts, NIL, ParseState::p_rtable, EphemeralNamedRelationMetadataData::reliddesc, RangeVar::relname, RTE_NAMEDTUPLESTORE, RangeTblEntry::rtekind, and TupleDescAttr().

Referenced by getNSItemForSpecialRelationTypes().

◆ addRangeTableEntryForFunction()

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

Definition at line 1747 of file parse_relation.c.

1754{
1756 Alias *alias = rangefunc->alias;
1757 Alias *eref;
1758 char *aliasname;
1759 int nfuncs = list_length(funcexprs);
1760 TupleDesc *functupdescs;
1761 TupleDesc tupdesc;
1762 ListCell *lc1,
1763 *lc2,
1764 *lc3;
1765 int i;
1766 int j;
1767 int funcno;
1768 int natts,
1769 totalatts;
1770
1771 Assert(pstate != NULL);
1772
1773 rte->rtekind = RTE_FUNCTION;
1774 rte->relid = InvalidOid;
1775 rte->subquery = NULL;
1776 rte->functions = NIL; /* we'll fill this list below */
1777 rte->funcordinality = rangefunc->ordinality;
1778 rte->alias = alias;
1779
1780 /*
1781 * Choose the RTE alias name. We default to using the first function's
1782 * name even when there's more than one; which is maybe arguable but beats
1783 * using something constant like "table".
1784 */
1785 if (alias)
1786 aliasname = alias->aliasname;
1787 else
1788 aliasname = linitial(funcnames);
1789
1790 eref = makeAlias(aliasname, NIL);
1791 rte->eref = eref;
1792
1793 /* Process each function ... */
1794 functupdescs = (TupleDesc *) palloc(nfuncs * sizeof(TupleDesc));
1795
1796 totalatts = 0;
1797 funcno = 0;
1798 forthree(lc1, funcexprs, lc2, funcnames, lc3, coldeflists)
1799 {
1800 Node *funcexpr = (Node *) lfirst(lc1);
1801 char *funcname = (char *) lfirst(lc2);
1802 List *coldeflist = (List *) lfirst(lc3);
1804 TypeFuncClass functypclass;
1805 Oid funcrettype;
1806
1807 /* Initialize RangeTblFunction node */
1808 rtfunc->funcexpr = funcexpr;
1809 rtfunc->funccolnames = NIL;
1810 rtfunc->funccoltypes = NIL;
1811 rtfunc->funccoltypmods = NIL;
1812 rtfunc->funccolcollations = NIL;
1813 rtfunc->funcparams = NULL; /* not set until planning */
1814
1815 /*
1816 * Now determine if the function returns a simple or composite type.
1817 */
1818 functypclass = get_expr_result_type(funcexpr,
1819 &funcrettype,
1820 &tupdesc);
1821
1822 /*
1823 * A coldeflist is required if the function returns RECORD and hasn't
1824 * got a predetermined record type, and is prohibited otherwise. This
1825 * can be a bit confusing, so we expend some effort on delivering a
1826 * relevant error message.
1827 */
1828 if (coldeflist != NIL)
1829 {
1830 switch (functypclass)
1831 {
1832 case TYPEFUNC_RECORD:
1833 /* ok */
1834 break;
1835 case TYPEFUNC_COMPOSITE:
1837
1838 /*
1839 * If the function's raw result type is RECORD, we must
1840 * have resolved it using its OUT parameters. Otherwise,
1841 * it must have a named composite type.
1842 */
1843 if (exprType(funcexpr) == RECORDOID)
1844 ereport(ERROR,
1845 (errcode(ERRCODE_SYNTAX_ERROR),
1846 errmsg("a column definition list is redundant for a function with OUT parameters"),
1847 parser_errposition(pstate,
1848 exprLocation((Node *) coldeflist))));
1849 else
1850 ereport(ERROR,
1851 (errcode(ERRCODE_SYNTAX_ERROR),
1852 errmsg("a column definition list is redundant for a function returning a named composite type"),
1853 parser_errposition(pstate,
1854 exprLocation((Node *) coldeflist))));
1855 break;
1856 default:
1857 ereport(ERROR,
1858 (errcode(ERRCODE_SYNTAX_ERROR),
1859 errmsg("a column definition list is only allowed for functions returning \"record\""),
1860 parser_errposition(pstate,
1861 exprLocation((Node *) coldeflist))));
1862 break;
1863 }
1864 }
1865 else
1866 {
1867 if (functypclass == TYPEFUNC_RECORD)
1868 ereport(ERROR,
1869 (errcode(ERRCODE_SYNTAX_ERROR),
1870 errmsg("a column definition list is required for functions returning \"record\""),
1871 parser_errposition(pstate, exprLocation(funcexpr))));
1872 }
1873
1874 if (functypclass == TYPEFUNC_COMPOSITE ||
1875 functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
1876 {
1877 /* Composite data type, e.g. a table's row type */
1878 Assert(tupdesc);
1879 }
1880 else if (functypclass == TYPEFUNC_SCALAR)
1881 {
1882 /* Base data type, i.e. scalar */
1883 tupdesc = CreateTemplateTupleDesc(1);
1884 TupleDescInitEntry(tupdesc,
1885 (AttrNumber) 1,
1887 alias, nfuncs),
1888 funcrettype,
1889 exprTypmod(funcexpr),
1890 0);
1892 (AttrNumber) 1,
1893 exprCollation(funcexpr));
1894 }
1895 else if (functypclass == TYPEFUNC_RECORD)
1896 {
1897 ListCell *col;
1898
1899 /*
1900 * Use the column definition list to construct a tupdesc and fill
1901 * in the RangeTblFunction's lists. Limit number of columns to
1902 * MaxHeapAttributeNumber, because CheckAttributeNamesTypes will.
1903 */
1904 if (list_length(coldeflist) > MaxHeapAttributeNumber)
1905 ereport(ERROR,
1906 (errcode(ERRCODE_TOO_MANY_COLUMNS),
1907 errmsg("column definition lists can have at most %d entries",
1909 parser_errposition(pstate,
1910 exprLocation((Node *) coldeflist))));
1911 tupdesc = CreateTemplateTupleDesc(list_length(coldeflist));
1912 i = 1;
1913 foreach(col, coldeflist)
1914 {
1915 ColumnDef *n = (ColumnDef *) lfirst(col);
1916 char *attrname;
1917 Oid attrtype;
1918 int32 attrtypmod;
1919 Oid attrcollation;
1920
1921 attrname = n->colname;
1922 if (n->typeName->setof)
1923 ereport(ERROR,
1924 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1925 errmsg("column \"%s\" cannot be declared SETOF",
1926 attrname),
1927 parser_errposition(pstate, n->location)));
1928 typenameTypeIdAndMod(pstate, n->typeName,
1929 &attrtype, &attrtypmod);
1930 attrcollation = GetColumnDefCollation(pstate, n, attrtype);
1931 TupleDescInitEntry(tupdesc,
1932 (AttrNumber) i,
1933 attrname,
1934 attrtype,
1935 attrtypmod,
1936 0);
1938 (AttrNumber) i,
1939 attrcollation);
1940 rtfunc->funccolnames = lappend(rtfunc->funccolnames,
1941 makeString(pstrdup(attrname)));
1942 rtfunc->funccoltypes = lappend_oid(rtfunc->funccoltypes,
1943 attrtype);
1944 rtfunc->funccoltypmods = lappend_int(rtfunc->funccoltypmods,
1945 attrtypmod);
1946 rtfunc->funccolcollations = lappend_oid(rtfunc->funccolcollations,
1947 attrcollation);
1948
1949 i++;
1950 }
1951
1952 /*
1953 * Ensure that the coldeflist defines a legal set of names (no
1954 * duplicates, but we needn't worry about system column names) and
1955 * datatypes. Although we mostly can't allow pseudo-types, it
1956 * seems safe to allow RECORD and RECORD[], since values within
1957 * those type classes are self-identifying at runtime, and the
1958 * coldeflist doesn't represent anything that will be visible to
1959 * other sessions.
1960 */
1961 CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE,
1963 }
1964 else
1965 ereport(ERROR,
1966 (errcode(ERRCODE_DATATYPE_MISMATCH),
1967 errmsg("function \"%s\" in FROM has unsupported return type %s",
1968 funcname, format_type_be(funcrettype)),
1969 parser_errposition(pstate, exprLocation(funcexpr))));
1970
1971 /* Finish off the RangeTblFunction and add it to the RTE's list */
1972 rtfunc->funccolcount = tupdesc->natts;
1973 rte->functions = lappend(rte->functions, rtfunc);
1974
1975 /* Save the tupdesc for use below */
1976 functupdescs[funcno] = tupdesc;
1977 totalatts += tupdesc->natts;
1978 funcno++;
1979 }
1980
1981 /*
1982 * If there's more than one function, or we want an ordinality column, we
1983 * have to produce a merged tupdesc.
1984 */
1985 if (nfuncs > 1 || rangefunc->ordinality)
1986 {
1987 if (rangefunc->ordinality)
1988 totalatts++;
1989
1990 /* Disallow more columns than will fit in a tuple */
1991 if (totalatts > MaxTupleAttributeNumber)
1992 ereport(ERROR,
1993 (errcode(ERRCODE_TOO_MANY_COLUMNS),
1994 errmsg("functions in FROM can return at most %d columns",
1996 parser_errposition(pstate,
1997 exprLocation((Node *) funcexprs))));
1998
1999 /* Merge the tuple descs of each function into a composite one */
2000 tupdesc = CreateTemplateTupleDesc(totalatts);
2001 natts = 0;
2002 for (i = 0; i < nfuncs; i++)
2003 {
2004 for (j = 1; j <= functupdescs[i]->natts; j++)
2005 TupleDescCopyEntry(tupdesc, ++natts, functupdescs[i], j);
2006 }
2007
2008 /* Add the ordinality column if needed */
2009 if (rangefunc->ordinality)
2010 {
2011 TupleDescInitEntry(tupdesc,
2012 (AttrNumber) ++natts,
2013 "ordinality",
2014 INT8OID,
2015 -1,
2016 0);
2017 /* no need to set collation */
2018 }
2019
2020 Assert(natts == totalatts);
2021 }
2022 else
2023 {
2024 /* We can just use the single function's tupdesc as-is */
2025 tupdesc = functupdescs[0];
2026 }
2027
2028 /* Use the tupdesc while assigning column aliases for the RTE */
2029 buildRelationAliases(tupdesc, alias, eref);
2030
2031 /*
2032 * Set flags and access permissions.
2033 *
2034 * Functions are never checked for access rights (at least, not by
2035 * ExecCheckPermissions()), so no need to perform addRTEPermissionInfo().
2036 */
2037 rte->lateral = lateral;
2038 rte->inFromCl = inFromCl;
2039
2040 /*
2041 * Add completed RTE to pstate's range table list, so that we know its
2042 * index. But we don't add it to the join list --- caller must do that if
2043 * appropriate.
2044 */
2045 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2046
2047 /*
2048 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2049 * list --- caller must do that if appropriate.
2050 */
2051 return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable), NULL,
2052 tupdesc);
2053}
int16 AttrNumber
Definition: attnum.h:21
int32_t int32
Definition: c.h:537
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:299
TypeFuncClass
Definition: funcapi.h:147
@ TYPEFUNC_SCALAR
Definition: funcapi.h:148
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
@ TYPEFUNC_RECORD
Definition: funcapi.h:151
@ TYPEFUNC_COMPOSITE_DOMAIN
Definition: funcapi.h:150
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, int flags)
Definition: heap.c:452
#define CHKATYPE_ANYRECORD
Definition: heap.h:24
#define MaxTupleAttributeNumber
Definition: htup_details.h:34
#define MaxHeapAttributeNumber
Definition: htup_details.h:48
#define funcname
Definition: indent_codes.h:69
int j
Definition: isn.c:78
char * pstrdup(const char *in)
Definition: mcxt.c:1759
void * palloc(Size size)
Definition: mcxt.c:1365
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:301
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1384
static char * chooseScalarFunctionAlias(Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
Oid GetColumnDefCollation(ParseState *pstate, const ColumnDef *coldef, Oid typeOid)
Definition: parse_type.c:540
@ RTE_FUNCTION
Definition: parsenodes.h:1046
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:563
#define linitial(l)
Definition: pg_list.h:178
unsigned int Oid
Definition: postgres_ext.h:32
char * colname
Definition: parsenodes.h:754
TypeName * typeName
Definition: parsenodes.h:755
ParseLoc location
Definition: parsenodes.h:773
Definition: pg_list.h:54
Definition: nodes.h:135
Alias * alias
Definition: parsenodes.h:672
bool funcordinality
Definition: parsenodes.h:1210
Query * subquery
Definition: parsenodes.h:1135
List * functions
Definition: parsenodes.h:1208
bool setof
Definition: parsenodes.h:287
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:182
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:1026
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:842
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:468

References RangeFunction::alias, Alias::aliasname, Assert(), buildNSItemFromTupleDesc(), buildRelationAliases(), CheckAttributeNamesTypes(), CHKATYPE_ANYRECORD, chooseScalarFunctionAlias(), ColumnDef::colname, CreateTemplateTupleDesc(), ereport, errcode(), errmsg(), ERROR, exprCollation(), exprLocation(), exprType(), exprTypmod(), format_type_be(), forthree, RangeTblFunction::funcexpr, funcname, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), GetColumnDefCollation(), i, InvalidOid, j, lappend(), lappend_int(), lappend_oid(), lfirst, linitial, list_length(), ColumnDef::location, makeAlias(), makeNode, makeString(), MaxHeapAttributeNumber, MaxTupleAttributeNumber, TupleDescData::natts, NIL, RangeFunction::ordinality, ParseState::p_rtable, palloc(), parser_errposition(), pstrdup(), RTE_FUNCTION, RangeTblEntry::rtekind, TypeName::setof, RangeTblEntry::subquery, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, TYPEFUNC_SCALAR, ColumnDef::typeName, and typenameTypeIdAndMod().

Referenced by transformRangeFunction().

◆ addRangeTableEntryForGroup()

ParseNamespaceItem * addRangeTableEntryForGroup ( ParseState pstate,
List groupClauses 
)

Definition at line 2579 of file parse_relation.c.

2581{
2583 Alias *eref;
2584 List *groupexprs;
2585 List *coltypes,
2586 *coltypmods,
2587 *colcollations;
2588 ListCell *lc;
2589 ParseNamespaceItem *nsitem;
2590
2591 Assert(pstate != NULL);
2592
2593 rte->rtekind = RTE_GROUP;
2594 rte->alias = NULL;
2595
2596 eref = makeAlias("*GROUP*", NIL);
2597
2598 /* fill in any unspecified alias columns, and extract column type info */
2599 groupexprs = NIL;
2600 coltypes = coltypmods = colcollations = NIL;
2601 foreach(lc, groupClauses)
2602 {
2603 TargetEntry *te = (TargetEntry *) lfirst(lc);
2604 char *colname = te->resname ? pstrdup(te->resname) : "?column?";
2605
2606 eref->colnames = lappend(eref->colnames, makeString(colname));
2607
2608 groupexprs = lappend(groupexprs, copyObject(te->expr));
2609
2610 coltypes = lappend_oid(coltypes,
2611 exprType((Node *) te->expr));
2612 coltypmods = lappend_int(coltypmods,
2613 exprTypmod((Node *) te->expr));
2614 colcollations = lappend_oid(colcollations,
2615 exprCollation((Node *) te->expr));
2616 }
2617
2618 rte->eref = eref;
2619 rte->groupexprs = groupexprs;
2620
2621 /*
2622 * Set flags.
2623 *
2624 * The grouping step is never checked for access rights, so no need to
2625 * perform addRTEPermissionInfo().
2626 */
2627 rte->lateral = false;
2628 rte->inFromCl = false;
2629
2630 /*
2631 * Add completed RTE to pstate's range table list, so that we know its
2632 * index. But we don't add it to the join list --- caller must do that if
2633 * appropriate.
2634 */
2635 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2636
2637 /*
2638 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2639 * list --- caller must do that if appropriate.
2640 */
2641 nsitem = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2642 coltypes, coltypmods, colcollations);
2643
2644 return nsitem;
2645}
@ RTE_GROUP
Definition: parsenodes.h:1054
Expr * expr
Definition: primnodes.h:2239

References Assert(), buildNSItemFromLists(), Alias::colnames, copyObject, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), lappend(), lappend_int(), lappend_oid(), lfirst, list_length(), makeAlias(), makeNode, makeString(), NIL, ParseState::p_rtable, pstrdup(), RTE_GROUP, and RangeTblEntry::rtekind.

Referenced by parseCheckAggregates().

◆ addRangeTableEntryForJoin()

ParseNamespaceItem * addRangeTableEntryForJoin ( ParseState pstate,
List colnames,
ParseNamespaceColumn nscolumns,
JoinType  jointype,
int  nummergedcols,
List aliasvars,
List leftcols,
List rightcols,
Alias join_using_alias,
Alias alias,
bool  inFromCl 
)

Definition at line 2229 of file parse_relation.c.

2240{
2242 Alias *eref;
2243 int numaliases;
2244 ParseNamespaceItem *nsitem;
2245
2246 Assert(pstate != NULL);
2247
2248 /*
2249 * Fail if join has too many columns --- we must be able to reference any
2250 * of the columns with an AttrNumber.
2251 */
2252 if (list_length(aliasvars) > MaxAttrNumber)
2253 ereport(ERROR,
2254 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2255 errmsg("joins can have at most %d columns",
2256 MaxAttrNumber)));
2257
2258 rte->rtekind = RTE_JOIN;
2259 rte->relid = InvalidOid;
2260 rte->subquery = NULL;
2261 rte->jointype = jointype;
2262 rte->joinmergedcols = nummergedcols;
2263 rte->joinaliasvars = aliasvars;
2264 rte->joinleftcols = leftcols;
2265 rte->joinrightcols = rightcols;
2266 rte->join_using_alias = join_using_alias;
2267 rte->alias = alias;
2268
2269 eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
2270 numaliases = list_length(eref->colnames);
2271
2272 /* fill in any unspecified alias columns */
2273 if (numaliases < list_length(colnames))
2274 eref->colnames = list_concat(eref->colnames,
2275 list_copy_tail(colnames, numaliases));
2276
2277 if (numaliases > list_length(colnames))
2278 ereport(ERROR,
2279 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2280 errmsg("join expression \"%s\" has %d columns available but %d columns specified",
2281 eref->aliasname, list_length(colnames), numaliases)));
2282
2283 rte->eref = eref;
2284
2285 /*
2286 * Set flags and access permissions.
2287 *
2288 * Joins are never checked for access rights, so no need to perform
2289 * addRTEPermissionInfo().
2290 */
2291 rte->lateral = false;
2292 rte->inFromCl = inFromCl;
2293
2294 /*
2295 * Add completed RTE to pstate's range table list, so that we know its
2296 * index. But we don't add it to the join list --- caller must do that if
2297 * appropriate.
2298 */
2299 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2300
2301 /*
2302 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2303 * list --- caller must do that if appropriate.
2304 */
2305 nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2306 nsitem->p_names = rte->eref;
2307 nsitem->p_rte = rte;
2308 nsitem->p_perminfo = NULL;
2309 nsitem->p_rtindex = list_length(pstate->p_rtable);
2310 nsitem->p_nscolumns = nscolumns;
2311 /* set default visibility flags; might get changed later */
2312 nsitem->p_rel_visible = true;
2313 nsitem->p_cols_visible = true;
2314 nsitem->p_lateral_only = false;
2315 nsitem->p_lateral_ok = true;
2317
2318 return nsitem;
2319}
#define MaxAttrNumber
Definition: attnum.h:24
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1613
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
@ RTE_JOIN
Definition: parsenodes.h:1045
@ VAR_RETURNING_DEFAULT
Definition: primnodes.h:256
RangeTblEntry * p_rte
Definition: parse_node.h:295
RTEPermissionInfo * p_perminfo
Definition: parse_node.h:297
VarReturningType p_returning_type
Definition: parse_node.h:304
JoinType jointype
Definition: parsenodes.h:1182

References Alias::aliasname, Assert(), Alias::colnames, copyObject, ereport, errcode(), errmsg(), ERROR, InvalidOid, RangeTblEntry::jointype, lappend(), list_concat(), list_copy_tail(), list_length(), makeAlias(), makeNode, MaxAttrNumber, NIL, ParseNamespaceItem::p_cols_visible, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_perminfo, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_returning_type, ParseState::p_rtable, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, palloc(), RTE_JOIN, RangeTblEntry::rtekind, RangeTblEntry::subquery, and VAR_RETURNING_DEFAULT.

Referenced by transformFromClauseItem(), and transformSetOperationStmt().

◆ addRangeTableEntryForRelation()

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

Definition at line 1580 of file parse_relation.c.

1586{
1588 RTEPermissionInfo *perminfo;
1589 char *refname = alias ? alias->aliasname : RelationGetRelationName(rel);
1590
1591 Assert(pstate != NULL);
1592
1593 Assert(lockmode == AccessShareLock ||
1594 lockmode == RowShareLock ||
1595 lockmode == RowExclusiveLock);
1596 Assert(CheckRelationLockedByMe(rel, lockmode, true));
1597
1598 rte->rtekind = RTE_RELATION;
1599 rte->alias = alias;
1600 rte->relid = RelationGetRelid(rel);
1601 rte->inh = inh;
1602 rte->relkind = rel->rd_rel->relkind;
1603 rte->rellockmode = lockmode;
1604
1605 /*
1606 * Build the list of effective column names using user-supplied aliases
1607 * and/or actual column names.
1608 */
1609 rte->eref = makeAlias(refname, NIL);
1610 buildRelationAliases(rel->rd_att, alias, rte->eref);
1611
1612 /*
1613 * Set flags and initialize access permissions.
1614 *
1615 * The initial default on access checks is always check-for-READ-access,
1616 * which is the right thing for all except target tables.
1617 */
1618 rte->lateral = false;
1619 rte->inFromCl = inFromCl;
1620
1621 perminfo = addRTEPermissionInfo(&pstate->p_rteperminfos, rte);
1622 perminfo->requiredPerms = ACL_SELECT;
1623
1624 /*
1625 * Add completed RTE to pstate's range table list, so that we know its
1626 * index. But we don't add it to the join list --- caller must do that if
1627 * appropriate.
1628 */
1629 pstate->p_rtable = lappend(pstate->p_rtable, rte);
1630
1631 /*
1632 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1633 * list --- caller must do that if appropriate.
1634 */
1635 return buildNSItemFromTupleDesc(rte, list_length(pstate->p_rtable),
1636 perminfo, rel->rd_att);
1637}
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:334
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:549

References AccessShareLock, ACL_SELECT, addRTEPermissionInfo(), Alias::aliasname, Assert(), buildNSItemFromTupleDesc(), buildRelationAliases(), CheckRelationLockedByMe(), RangeTblEntry::inh, lappend(), list_length(), makeAlias(), makeNode, NIL, ParseState::p_rtable, ParseState::p_rteperminfos, RelationData::rd_att, RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, RTEPermissionInfo::requiredPerms, RowExclusiveLock, RowShareLock, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by AddRelationNewConstraints(), AlterPolicy(), copy_table(), CreatePolicy(), CreateTriggerFiringOn(), DefineRelation(), DoCopy(), rewriteTargetView(), setTargetTable(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformIndexStmt(), transformOnConflictClause(), transformPartitionSpec(), TransformPubWhereClauses(), transformRuleStmt(), and transformStatsStmt().

◆ addRangeTableEntryForSubquery()

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

Definition at line 1651 of file parse_relation.c.

1656{
1658 Alias *eref;
1659 int numaliases;
1660 List *coltypes,
1661 *coltypmods,
1662 *colcollations;
1663 int varattno;
1664 ListCell *tlistitem;
1665 ParseNamespaceItem *nsitem;
1666
1667 Assert(pstate != NULL);
1668
1669 rte->rtekind = RTE_SUBQUERY;
1670 rte->subquery = subquery;
1671 rte->alias = alias;
1672
1673 eref = alias ? copyObject(alias) : makeAlias("unnamed_subquery", NIL);
1674 numaliases = list_length(eref->colnames);
1675
1676 /* fill in any unspecified alias columns, and extract column type info */
1677 coltypes = coltypmods = colcollations = NIL;
1678 varattno = 0;
1679 foreach(tlistitem, subquery->targetList)
1680 {
1681 TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
1682
1683 if (te->resjunk)
1684 continue;
1685 varattno++;
1686 Assert(varattno == te->resno);
1687 if (varattno > numaliases)
1688 {
1689 char *attrname;
1690
1691 attrname = pstrdup(te->resname);
1692 eref->colnames = lappend(eref->colnames, makeString(attrname));
1693 }
1694 coltypes = lappend_oid(coltypes,
1695 exprType((Node *) te->expr));
1696 coltypmods = lappend_int(coltypmods,
1697 exprTypmod((Node *) te->expr));
1698 colcollations = lappend_oid(colcollations,
1699 exprCollation((Node *) te->expr));
1700 }
1701 if (varattno < numaliases)
1702 ereport(ERROR,
1703 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1704 errmsg("table \"%s\" has %d columns available but %d columns specified",
1705 eref->aliasname, varattno, numaliases)));
1706
1707 rte->eref = eref;
1708
1709 /*
1710 * Set flags.
1711 *
1712 * Subqueries are never checked for access rights, so no need to perform
1713 * addRTEPermissionInfo().
1714 */
1715 rte->lateral = lateral;
1716 rte->inFromCl = inFromCl;
1717
1718 /*
1719 * Add completed RTE to pstate's range table list, so that we know its
1720 * index. But we don't add it to the join list --- caller must do that if
1721 * appropriate.
1722 */
1723 pstate->p_rtable = lappend(pstate->p_rtable, rte);
1724
1725 /*
1726 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
1727 * list --- caller must do that if appropriate.
1728 */
1729 nsitem = buildNSItemFromLists(rte, list_length(pstate->p_rtable),
1730 coltypes, coltypmods, colcollations);
1731
1732 /*
1733 * Mark it visible as a relation name only if it had a user-written alias.
1734 */
1735 nsitem->p_rel_visible = (alias != NULL);
1736
1737 return nsitem;
1738}
@ RTE_SUBQUERY
Definition: parsenodes.h:1044
List * targetList
Definition: parsenodes.h:198
AttrNumber resno
Definition: primnodes.h:2241

References Alias::aliasname, Assert(), buildNSItemFromLists(), Alias::colnames, copyObject, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), lappend(), lappend_int(), lappend_oid(), lfirst, list_length(), makeAlias(), makeNode, makeString(), NIL, ParseNamespaceItem::p_rel_visible, ParseState::p_rtable, pstrdup(), TargetEntry::resno, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblEntry::subquery, and Query::targetList.

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

◆ addRangeTableEntryForTableFunc()

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

Definition at line 2062 of file parse_relation.c.

2067{
2069 char *refname;
2070 Alias *eref;
2071 int numaliases;
2072
2073 Assert(pstate != NULL);
2074
2075 /* Disallow more columns than will fit in a tuple */
2076 if (list_length(tf->colnames) > MaxTupleAttributeNumber)
2077 ereport(ERROR,
2078 (errcode(ERRCODE_TOO_MANY_COLUMNS),
2079 errmsg("functions in FROM can return at most %d columns",
2081 parser_errposition(pstate,
2082 exprLocation((Node *) tf))));
2083 Assert(list_length(tf->coltypes) == list_length(tf->colnames));
2084 Assert(list_length(tf->coltypmods) == list_length(tf->colnames));
2085 Assert(list_length(tf->colcollations) == list_length(tf->colnames));
2086
2087 rte->rtekind = RTE_TABLEFUNC;
2088 rte->relid = InvalidOid;
2089 rte->subquery = NULL;
2090 rte->tablefunc = tf;
2091 rte->coltypes = tf->coltypes;
2092 rte->coltypmods = tf->coltypmods;
2093 rte->colcollations = tf->colcollations;
2094 rte->alias = alias;
2095
2096 refname = alias ? alias->aliasname :
2097 pstrdup(tf->functype == TFT_XMLTABLE ? "xmltable" : "json_table");
2098 eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2099 numaliases = list_length(eref->colnames);
2100
2101 /* fill in any unspecified alias columns */
2102 if (numaliases < list_length(tf->colnames))
2103 eref->colnames = list_concat(eref->colnames,
2104 list_copy_tail(tf->colnames, numaliases));
2105
2106 if (numaliases > list_length(tf->colnames))
2107 ereport(ERROR,
2108 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2109 errmsg("%s function has %d columns available but %d columns specified",
2110 tf->functype == TFT_XMLTABLE ? "XMLTABLE" : "JSON_TABLE",
2111 list_length(tf->colnames), numaliases)));
2112
2113 rte->eref = eref;
2114
2115 /*
2116 * Set flags and access permissions.
2117 *
2118 * Tablefuncs are never checked for access rights (at least, not by
2119 * ExecCheckPermissions()), so no need to perform addRTEPermissionInfo().
2120 */
2121 rte->lateral = lateral;
2122 rte->inFromCl = inFromCl;
2123
2124 /*
2125 * Add completed RTE to pstate's range table list, so that we know its
2126 * index. But we don't add it to the join list --- caller must do that if
2127 * appropriate.
2128 */
2129 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2130
2131 /*
2132 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2133 * list --- caller must do that if appropriate.
2134 */
2135 return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2136 rte->coltypes, rte->coltypmods,
2137 rte->colcollations);
2138}
@ RTE_TABLEFUNC
Definition: parsenodes.h:1047
@ TFT_XMLTABLE
Definition: primnodes.h:100
TableFunc * tablefunc
Definition: parsenodes.h:1215
TableFuncType functype
Definition: primnodes.h:114

References Alias::aliasname, Assert(), buildNSItemFromLists(), Alias::colnames, copyObject, ereport, errcode(), errmsg(), ERROR, exprLocation(), TableFunc::functype, InvalidOid, lappend(), list_concat(), list_copy_tail(), list_length(), makeAlias(), makeNode, MaxTupleAttributeNumber, NIL, ParseState::p_rtable, parser_errposition(), pstrdup(), RTE_TABLEFUNC, RangeTblEntry::rtekind, RangeTblEntry::subquery, RangeTblEntry::tablefunc, and TFT_XMLTABLE.

Referenced by transformJsonTable(), and transformRangeTableFunc().

◆ addRangeTableEntryForValues()

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

Definition at line 2147 of file parse_relation.c.

2155{
2157 char *refname = alias ? alias->aliasname : pstrdup("*VALUES*");
2158 Alias *eref;
2159 int numaliases;
2160 int numcolumns;
2161
2162 Assert(pstate != NULL);
2163
2164 rte->rtekind = RTE_VALUES;
2165 rte->relid = InvalidOid;
2166 rte->subquery = NULL;
2167 rte->values_lists = exprs;
2168 rte->coltypes = coltypes;
2169 rte->coltypmods = coltypmods;
2170 rte->colcollations = colcollations;
2171 rte->alias = alias;
2172
2173 eref = alias ? copyObject(alias) : makeAlias(refname, NIL);
2174
2175 /* fill in any unspecified alias columns */
2176 numcolumns = list_length((List *) linitial(exprs));
2177 numaliases = list_length(eref->colnames);
2178 while (numaliases < numcolumns)
2179 {
2180 char attrname[64];
2181
2182 numaliases++;
2183 snprintf(attrname, sizeof(attrname), "column%d", numaliases);
2184 eref->colnames = lappend(eref->colnames,
2185 makeString(pstrdup(attrname)));
2186 }
2187 if (numcolumns < numaliases)
2188 ereport(ERROR,
2189 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2190 errmsg("VALUES lists \"%s\" have %d columns available but %d columns specified",
2191 refname, numcolumns, numaliases)));
2192
2193 rte->eref = eref;
2194
2195 /*
2196 * Set flags and access permissions.
2197 *
2198 * Subqueries are never checked for access rights, so no need to perform
2199 * addRTEPermissionInfo().
2200 */
2201 rte->lateral = lateral;
2202 rte->inFromCl = inFromCl;
2203
2204 /*
2205 * Add completed RTE to pstate's range table list, so that we know its
2206 * index. But we don't add it to the join list --- caller must do that if
2207 * appropriate.
2208 */
2209 pstate->p_rtable = lappend(pstate->p_rtable, rte);
2210
2211 /*
2212 * Build a ParseNamespaceItem, but don't add it to the pstate's namespace
2213 * list --- caller must do that if appropriate.
2214 */
2215 return buildNSItemFromLists(rte, list_length(pstate->p_rtable),
2216 rte->coltypes, rte->coltypmods,
2217 rte->colcollations);
2218}
@ RTE_VALUES
Definition: parsenodes.h:1048
#define snprintf
Definition: port.h:260
List * values_lists
Definition: parsenodes.h:1221

References Alias::aliasname, Assert(), buildNSItemFromLists(), Alias::colnames, copyObject, ereport, errcode(), errmsg(), ERROR, InvalidOid, lappend(), linitial, list_length(), makeAlias(), makeNode, makeString(), NIL, ParseState::p_rtable, pstrdup(), RTE_VALUES, RangeTblEntry::rtekind, snprintf, RangeTblEntry::subquery, and RangeTblEntry::values_lists.

Referenced by transformValuesClause().

◆ addRTEPermissionInfo()

RTEPermissionInfo * addRTEPermissionInfo ( List **  rteperminfos,
RangeTblEntry rte 
)

Definition at line 3929 of file parse_relation.c.

3930{
3931 RTEPermissionInfo *perminfo;
3932
3933 Assert(OidIsValid(rte->relid));
3934 Assert(rte->perminfoindex == 0);
3935
3936 /* Nope, so make one and add to the list. */
3937 perminfo = makeNode(RTEPermissionInfo);
3938 perminfo->relid = rte->relid;
3939 perminfo->inh = rte->inh;
3940 /* Other information is set by fetching the node as and where needed. */
3941
3942 *rteperminfos = lappend(*rteperminfos, perminfo);
3943
3944 /* Note its index (1-based!) */
3945 rte->perminfoindex = list_length(*rteperminfos);
3946
3947 return perminfo;
3948}
#define OidIsValid(objectId)
Definition: c.h:777

References Assert(), RangeTblEntry::inh, RTEPermissionInfo::inh, lappend(), list_length(), makeNode, OidIsValid, and RTEPermissionInfo::relid.

Referenced by add_rte_to_flat_rtable(), addRangeTableEntry(), addRangeTableEntryForRelation(), create_edata_for_relation(), create_estate_for_relation(), plan_cluster_use_sort(), plan_create_index_workers(), and rewriteTargetView().

◆ attnameAttNum()

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

Definition at line 3585 of file parse_relation.c.

3586{
3587 int i;
3588
3589 for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3590 {
3592
3593 if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3594 return i + 1;
3595 }
3596
3597 if (sysColOK)
3598 {
3600 return i;
3601 }
3602
3603 /* on failure */
3604 return InvalidAttrNumber;
3605}
#define InvalidAttrNumber
Definition: attnum.h:23
int namestrcmp(Name name, const char *str)
Definition: name.c:247
static int specialAttNum(const char *attname)
NameData attname
Definition: pg_attribute.h:41
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:521

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

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

◆ attnumAttName()

const NameData * attnumAttName ( Relation  rd,
int  attid 
)

Definition at line 3635 of file parse_relation.c.

3636{
3637 if (attid <= 0)
3638 {
3639 const FormData_pg_attribute *sysatt;
3640
3641 sysatt = SystemAttributeDefinition(attid);
3642 return &sysatt->attname;
3643 }
3644 if (attid > rd->rd_att->natts)
3645 elog(ERROR, "invalid attribute number %d", attid);
3646 return &TupleDescAttr(rd->rd_att, attid - 1)->attname;
3647}
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:236
FormData_pg_attribute
Definition: pg_attribute.h:186

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

Referenced by transformFkeyGetPrimaryKey().

◆ attnumCollationId()

Oid attnumCollationId ( Relation  rd,
int  attid 
)

Definition at line 3677 of file parse_relation.c.

3678{
3679 if (attid <= 0)
3680 {
3681 /* All system attributes are of noncollatable types. */
3682 return InvalidOid;
3683 }
3684 if (attid > rd->rd_att->natts)
3685 elog(ERROR, "invalid attribute number %d", attid);
3686 return TupleDescAttr(rd->rd_att, attid - 1)->attcollation;
3687}

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

Referenced by transformFkeyGetPrimaryKey().

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3657 of file parse_relation.c.

3658{
3659 if (attid <= 0)
3660 {
3661 const FormData_pg_attribute *sysatt;
3662
3663 sysatt = SystemAttributeDefinition(attid);
3664 return sysatt->atttypid;
3665 }
3666 if (attid > rd->rd_att->natts)
3667 elog(ERROR, "invalid attribute number %d", attid);
3668 return TupleDescAttr(rd->rd_att, attid - 1)->atttypid;
3669}

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

◆ buildNSItemFromLists()

static ParseNamespaceItem * buildNSItemFromLists ( RangeTblEntry rte,
Index  rtindex,
List coltypes,
List coltypmods,
List colcollations 
)
static

Definition at line 1366 of file parse_relation.c.

1368{
1369 ParseNamespaceItem *nsitem;
1370 ParseNamespaceColumn *nscolumns;
1371 int maxattrs = list_length(coltypes);
1372 int varattno;
1373 ListCell *lct;
1374 ListCell *lcm;
1375 ListCell *lcc;
1376
1377 /* colnames must have the same number of entries as the nsitem */
1378 Assert(maxattrs == list_length(rte->eref->colnames));
1379
1380 Assert(maxattrs == list_length(coltypmods));
1381 Assert(maxattrs == list_length(colcollations));
1382
1383 /* extract per-column data from the lists */
1384 nscolumns = (ParseNamespaceColumn *)
1385 palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1386
1387 varattno = 0;
1388 forthree(lct, coltypes,
1389 lcm, coltypmods,
1390 lcc, colcollations)
1391 {
1392 nscolumns[varattno].p_varno = rtindex;
1393 nscolumns[varattno].p_varattno = varattno + 1;
1394 nscolumns[varattno].p_vartype = lfirst_oid(lct);
1395 nscolumns[varattno].p_vartypmod = lfirst_int(lcm);
1396 nscolumns[varattno].p_varcollid = lfirst_oid(lcc);
1397 nscolumns[varattno].p_varnosyn = rtindex;
1398 nscolumns[varattno].p_varattnosyn = varattno + 1;
1399 varattno++;
1400 }
1401
1402 /* ... and build the nsitem */
1403 nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1404 nsitem->p_names = rte->eref;
1405 nsitem->p_rte = rte;
1406 nsitem->p_rtindex = rtindex;
1407 nsitem->p_perminfo = NULL;
1408 nsitem->p_nscolumns = nscolumns;
1409 /* set default visibility flags; might get changed later */
1410 nsitem->p_rel_visible = true;
1411 nsitem->p_cols_visible = true;
1412 nsitem->p_lateral_only = false;
1413 nsitem->p_lateral_ok = true;
1415
1416 return nsitem;
1417}
void * palloc0(Size size)
Definition: mcxt.c:1395
#define lfirst_int(lc)
Definition: pg_list.h:173
#define lfirst_oid(lc)
Definition: pg_list.h:174
AttrNumber p_varattno
Definition: parse_node.h:331
AttrNumber p_varattnosyn
Definition: parse_node.h:337

References Assert(), forthree, lfirst_int, lfirst_oid, list_length(), ParseNamespaceItem::p_cols_visible, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_perminfo, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_returning_type, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, ParseNamespaceColumn::p_varattno, ParseNamespaceColumn::p_varattnosyn, ParseNamespaceColumn::p_varcollid, ParseNamespaceColumn::p_varno, ParseNamespaceColumn::p_varnosyn, ParseNamespaceColumn::p_vartype, ParseNamespaceColumn::p_vartypmod, palloc(), palloc0(), and VAR_RETURNING_DEFAULT.

Referenced by addRangeTableEntryForCTE(), addRangeTableEntryForGroup(), addRangeTableEntryForSubquery(), addRangeTableEntryForTableFunc(), and addRangeTableEntryForValues().

◆ buildNSItemFromTupleDesc()

static ParseNamespaceItem * buildNSItemFromTupleDesc ( RangeTblEntry rte,
Index  rtindex,
RTEPermissionInfo perminfo,
TupleDesc  tupdesc 
)
static

Definition at line 1305 of file parse_relation.c.

1308{
1309 ParseNamespaceItem *nsitem;
1310 ParseNamespaceColumn *nscolumns;
1311 int maxattrs = tupdesc->natts;
1312 int varattno;
1313
1314 /* colnames must have the same number of entries as the nsitem */
1315 Assert(maxattrs == list_length(rte->eref->colnames));
1316
1317 /* extract per-column data from the tupdesc */
1318 nscolumns = (ParseNamespaceColumn *)
1319 palloc0(maxattrs * sizeof(ParseNamespaceColumn));
1320
1321 for (varattno = 0; varattno < maxattrs; varattno++)
1322 {
1323 Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1324
1325 /* For a dropped column, just leave the entry as zeroes */
1326 if (attr->attisdropped)
1327 continue;
1328
1329 nscolumns[varattno].p_varno = rtindex;
1330 nscolumns[varattno].p_varattno = varattno + 1;
1331 nscolumns[varattno].p_vartype = attr->atttypid;
1332 nscolumns[varattno].p_vartypmod = attr->atttypmod;
1333 nscolumns[varattno].p_varcollid = attr->attcollation;
1334 nscolumns[varattno].p_varnosyn = rtindex;
1335 nscolumns[varattno].p_varattnosyn = varattno + 1;
1336 }
1337
1338 /* ... and build the nsitem */
1339 nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1340 nsitem->p_names = rte->eref;
1341 nsitem->p_rte = rte;
1342 nsitem->p_rtindex = rtindex;
1343 nsitem->p_perminfo = perminfo;
1344 nsitem->p_nscolumns = nscolumns;
1345 /* set default visibility flags; might get changed later */
1346 nsitem->p_rel_visible = true;
1347 nsitem->p_cols_visible = true;
1348 nsitem->p_lateral_only = false;
1349 nsitem->p_lateral_ok = true;
1351
1352 return nsitem;
1353}

References Assert(), list_length(), TupleDescData::natts, ParseNamespaceItem::p_cols_visible, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_perminfo, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_returning_type, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, ParseNamespaceColumn::p_varattno, ParseNamespaceColumn::p_varattnosyn, ParseNamespaceColumn::p_varcollid, ParseNamespaceColumn::p_varno, ParseNamespaceColumn::p_varnosyn, ParseNamespaceColumn::p_vartype, ParseNamespaceColumn::p_vartypmod, palloc(), palloc0(), TupleDescAttr(), and VAR_RETURNING_DEFAULT.

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

◆ buildRelationAliases()

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

Definition at line 1188 of file parse_relation.c.

1189{
1190 int maxattrs = tupdesc->natts;
1191 List *aliaslist;
1192 ListCell *aliaslc;
1193 int numaliases;
1194 int varattno;
1195 int numdropped = 0;
1196
1197 Assert(eref->colnames == NIL);
1198
1199 if (alias)
1200 {
1201 aliaslist = alias->colnames;
1202 aliaslc = list_head(aliaslist);
1203 numaliases = list_length(aliaslist);
1204 /* We'll rebuild the alias colname list */
1205 alias->colnames = NIL;
1206 }
1207 else
1208 {
1209 aliaslist = NIL;
1210 aliaslc = NULL;
1211 numaliases = 0;
1212 }
1213
1214 for (varattno = 0; varattno < maxattrs; varattno++)
1215 {
1216 Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1217 String *attrname;
1218
1219 if (attr->attisdropped)
1220 {
1221 /* Always insert an empty string for a dropped column */
1222 attrname = makeString(pstrdup(""));
1223 if (aliaslc)
1224 alias->colnames = lappend(alias->colnames, attrname);
1225 numdropped++;
1226 }
1227 else if (aliaslc)
1228 {
1229 /* Use the next user-supplied alias */
1230 attrname = lfirst_node(String, aliaslc);
1231 aliaslc = lnext(aliaslist, aliaslc);
1232 alias->colnames = lappend(alias->colnames, attrname);
1233 }
1234 else
1235 {
1236 attrname = makeString(pstrdup(NameStr(attr->attname)));
1237 /* we're done with the alias if any */
1238 }
1239
1240 eref->colnames = lappend(eref->colnames, attrname);
1241 }
1242
1243 /* Too many user-supplied aliases? */
1244 if (aliaslc)
1245 ereport(ERROR,
1246 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1247 errmsg("table \"%s\" has %d columns available but %d columns specified",
1248 eref->aliasname, maxattrs - numdropped, numaliases)));
1249}
#define NameStr(name)
Definition: c.h:754
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
Definition: value.h:64

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

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

◆ check_lateral_ref_ok()

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

Definition at line 487 of file parse_relation.c.

489{
490 if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
491 {
492 /* SQL:2008 demands this be an error, not an invisible item */
493 RangeTblEntry *rte = nsitem->p_rte;
494 char *refname = nsitem->p_names->aliasname;
495
497 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
498 errmsg("invalid reference to FROM-clause entry for table \"%s\"",
499 refname),
500 (pstate->p_target_nsitem != NULL &&
501 rte == pstate->p_target_nsitem->p_rte) ?
502 errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
503 refname) :
504 errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
505 parser_errposition(pstate, location)));
506 }
507}
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errhint(const char *fmt,...)
Definition: elog.c:1330
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:210

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

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

◆ checkNameSpaceConflicts()

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

Definition at line 438 of file parse_relation.c.

440{
441 ListCell *l1;
442
443 foreach(l1, namespace1)
444 {
446 RangeTblEntry *rte1 = nsitem1->p_rte;
447 const char *aliasname1 = nsitem1->p_names->aliasname;
448 ListCell *l2;
449
450 if (!nsitem1->p_rel_visible)
451 continue;
452
453 foreach(l2, namespace2)
454 {
456 RangeTblEntry *rte2 = nsitem2->p_rte;
457 const char *aliasname2 = nsitem2->p_names->aliasname;
458
459 if (!nsitem2->p_rel_visible)
460 continue;
461 if (strcmp(aliasname2, aliasname1) != 0)
462 continue; /* definitely no conflict */
463 if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
464 rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
465 rte1->relid != rte2->relid)
466 continue; /* no conflict per SQL rule */
468 (errcode(ERRCODE_DUPLICATE_ALIAS),
469 errmsg("table name \"%s\" specified more than once",
470 aliasname1)));
471 }
472 }
473}

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

Referenced by transformFromClause(), and transformFromClauseItem().

◆ chooseScalarFunctionAlias()

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

Definition at line 1265 of file parse_relation.c.

1267{
1268 char *pname;
1269
1270 /*
1271 * If the expression is a simple function call, and the function has a
1272 * single OUT parameter that is named, use the parameter's name.
1273 */
1274 if (funcexpr && IsA(funcexpr, FuncExpr))
1275 {
1276 pname = get_func_result_name(((FuncExpr *) funcexpr)->funcid);
1277 if (pname)
1278 return pname;
1279 }
1280
1281 /*
1282 * If there's just one function in the RTE, and the user gave an RTE alias
1283 * name, use that name. (This makes FROM func() AS foo use "foo" as the
1284 * column name as well as the table alias.)
1285 */
1286 if (nfuncs == 1 && alias)
1287 return alias->aliasname;
1288
1289 /*
1290 * Otherwise use the function name.
1291 */
1292 return funcname;
1293}
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1607

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

Referenced by addRangeTableEntryForFunction().

◆ colNameToVar()

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

Definition at line 894 of file parse_relation.c.

896{
897 Node *result = NULL;
898 int sublevels_up = 0;
899 ParseState *orig_pstate = pstate;
900
901 while (pstate != NULL)
902 {
903 ListCell *l;
904
905 foreach(l, pstate->p_namespace)
906 {
908 Node *newresult;
909
910 /* Ignore table-only items */
911 if (!nsitem->p_cols_visible)
912 continue;
913 /* If not inside LATERAL, ignore lateral-only items */
914 if (nsitem->p_lateral_only && !pstate->p_lateral_active)
915 continue;
916
917 /* use orig_pstate here for consistency with other callers */
918 newresult = scanNSItemForColumn(orig_pstate, nsitem, sublevels_up,
919 colname, location);
920
921 if (newresult)
922 {
923 if (result)
925 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
926 errmsg("column reference \"%s\" is ambiguous",
927 colname),
928 parser_errposition(pstate, location)));
929 check_lateral_ref_ok(pstate, nsitem, location);
930 result = newresult;
931 }
932 }
933
934 if (result != NULL || localonly)
935 break; /* found, or don't want to look at parent */
936
937 pstate = pstate->parentParseState;
938 sublevels_up++;
939 }
940
941 return result;
942}
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
ParseState * parentParseState
Definition: parse_node.h:194
bool p_lateral_active
Definition: parse_node.h:205

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, ParseState::parentParseState, parser_errposition(), and scanNSItemForColumn().

Referenced by findTargetlistEntrySQL92(), and transformColumnRef().

◆ errorMissingColumn()

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

Definition at line 3767 of file parse_relation.c.

3769{
3771
3772 /*
3773 * Search the entire rtable looking for possible matches. If we find one,
3774 * emit a hint about it.
3775 */
3776 state = searchRangeTableForCol(pstate, relname, colname, location);
3777
3778 /*
3779 * If there are exact match(es), they must be inaccessible for some
3780 * reason.
3781 */
3782 if (state->rexact1)
3783 {
3784 /*
3785 * We don't try too hard when there's multiple inaccessible exact
3786 * matches, but at least be sure that we don't misleadingly suggest
3787 * that there's only one.
3788 */
3789 if (state->rexact2)
3790 ereport(ERROR,
3791 (errcode(ERRCODE_UNDEFINED_COLUMN),
3792 relname ?
3793 errmsg("column %s.%s does not exist", relname, colname) :
3794 errmsg("column \"%s\" does not exist", colname),
3795 errdetail("There are columns named \"%s\", but they are in tables that cannot be referenced from this part of the query.",
3796 colname),
3797 !relname ? errhint("Try using a table-qualified name.") : 0,
3798 parser_errposition(pstate, location)));
3799 /* Single exact match, so try to determine why it's inaccessible. */
3800 ereport(ERROR,
3801 (errcode(ERRCODE_UNDEFINED_COLUMN),
3802 relname ?
3803 errmsg("column %s.%s does not exist", relname, colname) :
3804 errmsg("column \"%s\" does not exist", colname),
3805 errdetail("There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3806 colname, state->rexact1->eref->aliasname),
3807 rte_visible_if_lateral(pstate, state->rexact1) ?
3808 errhint("To reference that column, you must mark this subquery with LATERAL.") :
3809 (!relname && rte_visible_if_qualified(pstate, state->rexact1)) ?
3810 errhint("To reference that column, you must use a table-qualified name.") : 0,
3811 parser_errposition(pstate, location)));
3812 }
3813
3814 if (!state->rsecond)
3815 {
3816 /* If we found no match at all, we have little to report */
3817 if (!state->rfirst)
3818 ereport(ERROR,
3819 (errcode(ERRCODE_UNDEFINED_COLUMN),
3820 relname ?
3821 errmsg("column %s.%s does not exist", relname, colname) :
3822 errmsg("column \"%s\" does not exist", colname),
3823 parser_errposition(pstate, location)));
3824 /* Handle case where we have a single alternative spelling to offer */
3825 ereport(ERROR,
3826 (errcode(ERRCODE_UNDEFINED_COLUMN),
3827 relname ?
3828 errmsg("column %s.%s does not exist", relname, colname) :
3829 errmsg("column \"%s\" does not exist", colname),
3830 errhint("Perhaps you meant to reference the column \"%s.%s\".",
3831 state->rfirst->eref->aliasname,
3832 strVal(list_nth(state->rfirst->eref->colnames,
3833 state->first - 1))),
3834 parser_errposition(pstate, location)));
3835 }
3836 else
3837 {
3838 /* Handle case where there are two equally useful column hints */
3839 ereport(ERROR,
3840 (errcode(ERRCODE_UNDEFINED_COLUMN),
3841 relname ?
3842 errmsg("column %s.%s does not exist", relname, colname) :
3843 errmsg("column \"%s\" does not exist", colname),
3844 errhint("Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3845 state->rfirst->eref->aliasname,
3846 strVal(list_nth(state->rfirst->eref->colnames,
3847 state->first - 1)),
3848 state->rsecond->eref->aliasname,
3849 strVal(list_nth(state->rsecond->eref->colnames,
3850 state->second - 1))),
3851 parser_errposition(pstate, location)));
3852 }
3853}
static bool rte_visible_if_lateral(ParseState *pstate, RangeTblEntry *rte)
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colname, int location)
static bool rte_visible_if_qualified(ParseState *pstate, RangeTblEntry *rte)
NameData relname
Definition: pg_class.h:38
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
Definition: regguts.h:323
#define strVal(v)
Definition: value.h:82

References ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, list_nth(), parser_errposition(), relname, rte_visible_if_lateral(), rte_visible_if_qualified(), searchRangeTableForCol(), and strVal.

Referenced by transformColumnRef().

◆ errorMissingRTE()

void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3696 of file parse_relation.c.

3697{
3698 RangeTblEntry *rte;
3699 const char *badAlias = NULL;
3700
3701 /*
3702 * Check to see if there are any potential matches in the query's
3703 * rangetable. (Note: cases involving a bad schema name in the RangeVar
3704 * will throw error immediately here. That seems OK.)
3705 */
3706 rte = searchRangeTableForRel(pstate, relation);
3707
3708 /*
3709 * If we found a match that has an alias and the alias is visible in the
3710 * namespace, then the problem is probably use of the relation's real name
3711 * instead of its alias, ie "SELECT foo.* FROM foo f". This mistake is
3712 * common enough to justify a specific hint.
3713 *
3714 * If we found a match that doesn't meet those criteria, assume the
3715 * problem is illegal use of a relation outside its scope, as in the
3716 * MySQL-ism "SELECT ... FROM a, b LEFT JOIN c ON (a.x = c.y)".
3717 */
3718 if (rte && rte->alias &&
3719 strcmp(rte->eref->aliasname, relation->relname) != 0)
3720 {
3721 ParseNamespaceItem *nsitem;
3722 int sublevels_up;
3723
3724 nsitem = refnameNamespaceItem(pstate, NULL, rte->eref->aliasname,
3725 relation->location,
3726 &sublevels_up);
3727 if (nsitem && nsitem->p_rte == rte)
3728 badAlias = rte->eref->aliasname;
3729 }
3730
3731 /* If it looks like the user forgot to use an alias, hint about that */
3732 if (badAlias)
3733 ereport(ERROR,
3735 errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3736 relation->relname),
3737 errhint("Perhaps you meant to reference the table alias \"%s\".",
3738 badAlias),
3739 parser_errposition(pstate, relation->location)));
3740 /* Hint about case where we found an (inaccessible) exact match */
3741 else if (rte)
3742 ereport(ERROR,
3744 errmsg("invalid reference to FROM-clause entry for table \"%s\"",
3745 relation->relname),
3746 errdetail("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3747 rte->eref->aliasname),
3748 rte_visible_if_lateral(pstate, rte) ?
3749 errhint("To reference that table, you must mark this subquery with LATERAL.") : 0,
3750 parser_errposition(pstate, relation->location)));
3751 /* Else, we have nothing to offer but the bald statement of error */
3752 else
3753 ereport(ERROR,
3755 errmsg("missing FROM-clause entry for table \"%s\"",
3756 relation->relname),
3757 parser_errposition(pstate, relation->location)));
3758}
static RangeTblEntry * searchRangeTableForRel(ParseState *pstate, RangeVar *relation)
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:79

References ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errdetail(), errhint(), errmsg(), ERROR, RangeVar::location, ParseNamespaceItem::p_rte, parser_errposition(), refnameNamespaceItem(), RangeVar::relname, rte_visible_if_lateral(), and searchRangeTableForRel().

Referenced by ExpandColumnRefStar(), and transformColumnRef().

◆ expandNSItemAttrs()

List * expandNSItemAttrs ( ParseState pstate,
ParseNamespaceItem nsitem,
int  sublevels_up,
bool  require_col_privs,
int  location 
)

Definition at line 3288 of file parse_relation.c.

3290{
3291 RangeTblEntry *rte = nsitem->p_rte;
3292 RTEPermissionInfo *perminfo = nsitem->p_perminfo;
3293 List *names,
3294 *vars;
3295 ListCell *name,
3296 *var;
3297 List *te_list = NIL;
3298
3299 vars = expandNSItemVars(pstate, nsitem, sublevels_up, location, &names);
3300
3301 /*
3302 * Require read access to the table. This is normally redundant with the
3303 * markVarForSelectPriv calls below, but not if the table has zero
3304 * columns. We need not do anything if the nsitem is for a join: its
3305 * component tables will have been marked ACL_SELECT when they were added
3306 * to the rangetable. (This step changes things only for the target
3307 * relation of UPDATE/DELETE, which cannot be under a join.)
3308 */
3309 if (rte->rtekind == RTE_RELATION)
3310 {
3311 Assert(perminfo != NULL);
3312 perminfo->requiredPerms |= ACL_SELECT;
3313 }
3314
3315 forboth(name, names, var, vars)
3316 {
3317 char *label = strVal(lfirst(name));
3318 Var *varnode = (Var *) lfirst(var);
3319 TargetEntry *te;
3320
3321 te = makeTargetEntry((Expr *) varnode,
3322 (AttrNumber) pstate->p_next_resno++,
3323 label,
3324 false);
3325 te_list = lappend(te_list, te);
3326
3327 if (require_col_privs)
3328 {
3329 /* Require read access to each column */
3330 markVarForSelectPriv(pstate, varnode);
3331 }
3332 }
3333
3334 Assert(name == NULL && var == NULL); /* lists not the same length? */
3335
3336 return te_list;
3337}
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:289
void markVarForSelectPriv(ParseState *pstate, Var *var)
List * expandNSItemVars(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
static char * label
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
int p_next_resno
Definition: parse_node.h:215
Definition: primnodes.h:262
Definition: regcomp.c:282
const char * name

References ACL_SELECT, Assert(), expandNSItemVars(), forboth, label, lappend(), lfirst, makeTargetEntry(), markVarForSelectPriv(), name, NIL, ParseState::p_next_resno, ParseNamespaceItem::p_perminfo, ParseNamespaceItem::p_rte, RTEPermissionInfo::requiredPerms, RTE_RELATION, RangeTblEntry::rtekind, and strVal.

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

◆ expandNSItemVars()

List * expandNSItemVars ( ParseState pstate,
ParseNamespaceItem nsitem,
int  sublevels_up,
int  location,
List **  colnames 
)

Definition at line 3223 of file parse_relation.c.

3226{
3227 List *result = NIL;
3228 int colindex;
3229 ListCell *lc;
3230
3231 if (colnames)
3232 *colnames = NIL;
3233 colindex = 0;
3234 foreach(lc, nsitem->p_names->colnames)
3235 {
3236 String *colnameval = lfirst(lc);
3237 const char *colname = strVal(colnameval);
3238 ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3239
3240 if (nscol->p_dontexpand)
3241 {
3242 /* skip */
3243 }
3244 else if (colname[0])
3245 {
3246 Var *var;
3247
3248 Assert(nscol->p_varno > 0);
3249 var = makeVar(nscol->p_varno,
3250 nscol->p_varattno,
3251 nscol->p_vartype,
3252 nscol->p_vartypmod,
3253 nscol->p_varcollid,
3254 sublevels_up);
3255 /* makeVar doesn't offer parameters for these, so set by hand: */
3257 var->varnosyn = nscol->p_varnosyn;
3258 var->varattnosyn = nscol->p_varattnosyn;
3259 var->location = location;
3260
3261 /* ... and update varnullingrels */
3262 markNullableIfNeeded(pstate, var);
3263
3264 result = lappend(result, var);
3265 if (colnames)
3266 *colnames = lappend(*colnames, colnameval);
3267 }
3268 else
3269 {
3270 /* dropped column, ignore */
3271 Assert(nscol->p_varno == 0);
3272 }
3273 colindex++;
3274 }
3275 return result;
3276}
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
void markNullableIfNeeded(ParseState *pstate, Var *var)
VarReturningType p_varreturningtype
Definition: parse_node.h:335
ParseLoc location
Definition: primnodes.h:310
VarReturningType varreturningtype
Definition: primnodes.h:297

References Assert(), Alias::colnames, lappend(), lfirst, Var::location, makeVar(), markNullableIfNeeded(), NIL, ParseNamespaceColumn::p_dontexpand, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseNamespaceColumn::p_varattno, ParseNamespaceColumn::p_varattnosyn, ParseNamespaceColumn::p_varcollid, ParseNamespaceColumn::p_varno, ParseNamespaceColumn::p_varnosyn, ParseNamespaceColumn::p_varreturningtype, ParseNamespaceColumn::p_vartype, ParseNamespaceColumn::p_vartypmod, strVal, and Var::varreturningtype.

Referenced by coerce_record_to_complex(), expandNSItemAttrs(), and ExpandSingleTable().

◆ expandRelation()

static void expandRelation ( Oid  relid,
Alias eref,
int  rtindex,
int  sublevels_up,
VarReturningType  returning_type,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)
static

Definition at line 3114 of file parse_relation.c.

3118{
3119 Relation rel;
3120
3121 /* Get the tupledesc and turn it over to expandTupleDesc */
3122 rel = relation_open(relid, AccessShareLock);
3123 expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
3124 rtindex, sublevels_up, returning_type,
3125 location, include_dropped,
3126 colnames, colvars);
3128}
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47

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

Referenced by expandRTE().

◆ expandRTE()

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

Definition at line 2747 of file parse_relation.c.

2751{
2752 int varattno;
2753
2754 if (colnames)
2755 *colnames = NIL;
2756 if (colvars)
2757 *colvars = NIL;
2758
2759 switch (rte->rtekind)
2760 {
2761 case RTE_RELATION:
2762 /* Ordinary relation RTE */
2763 expandRelation(rte->relid, rte->eref,
2764 rtindex, sublevels_up, returning_type, location,
2765 include_dropped, colnames, colvars);
2766 break;
2767 case RTE_SUBQUERY:
2768 {
2769 /* Subquery RTE */
2770 ListCell *aliasp_item = list_head(rte->eref->colnames);
2771 ListCell *tlistitem;
2772
2773 varattno = 0;
2774 foreach(tlistitem, rte->subquery->targetList)
2775 {
2776 TargetEntry *te = (TargetEntry *) lfirst(tlistitem);
2777
2778 if (te->resjunk)
2779 continue;
2780 varattno++;
2781 Assert(varattno == te->resno);
2782
2783 /*
2784 * Formerly it was possible for the subquery tlist to have
2785 * more non-junk entries than the colnames list does (if
2786 * this RTE has been expanded from a view that has more
2787 * columns than it did when the current query was parsed).
2788 * Now that ApplyRetrieveRule cleans up such cases, we
2789 * shouldn't see that anymore, but let's just check.
2790 */
2791 if (!aliasp_item)
2792 elog(ERROR, "too few column names for subquery %s",
2793 rte->eref->aliasname);
2794
2795 if (colnames)
2796 {
2797 char *label = strVal(lfirst(aliasp_item));
2798
2799 *colnames = lappend(*colnames, makeString(pstrdup(label)));
2800 }
2801
2802 if (colvars)
2803 {
2804 Var *varnode;
2805
2806 varnode = makeVar(rtindex, varattno,
2807 exprType((Node *) te->expr),
2808 exprTypmod((Node *) te->expr),
2809 exprCollation((Node *) te->expr),
2810 sublevels_up);
2811 varnode->varreturningtype = returning_type;
2812 varnode->location = location;
2813
2814 *colvars = lappend(*colvars, varnode);
2815 }
2816
2817 aliasp_item = lnext(rte->eref->colnames, aliasp_item);
2818 }
2819 }
2820 break;
2821 case RTE_FUNCTION:
2822 {
2823 /* Function RTE */
2824 int atts_done = 0;
2825 ListCell *lc;
2826
2827 foreach(lc, rte->functions)
2828 {
2829 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2830 TypeFuncClass functypclass;
2831 Oid funcrettype = InvalidOid;
2832 TupleDesc tupdesc = NULL;
2833
2834 /* If it has a coldeflist, it returns RECORD */
2835 if (rtfunc->funccolnames != NIL)
2836 functypclass = TYPEFUNC_RECORD;
2837 else
2838 functypclass = get_expr_result_type(rtfunc->funcexpr,
2839 &funcrettype,
2840 &tupdesc);
2841
2842 if (functypclass == TYPEFUNC_COMPOSITE ||
2843 functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
2844 {
2845 /* Composite data type, e.g. a table's row type */
2846 Assert(tupdesc);
2847 expandTupleDesc(tupdesc, rte->eref,
2848 rtfunc->funccolcount, atts_done,
2849 rtindex, sublevels_up,
2850 returning_type, location,
2851 include_dropped, colnames, colvars);
2852 }
2853 else if (functypclass == TYPEFUNC_SCALAR)
2854 {
2855 /* Base data type, i.e. scalar */
2856 if (colnames)
2857 *colnames = lappend(*colnames,
2858 list_nth(rte->eref->colnames,
2859 atts_done));
2860
2861 if (colvars)
2862 {
2863 Var *varnode;
2864
2865 varnode = makeVar(rtindex, atts_done + 1,
2866 funcrettype,
2867 exprTypmod(rtfunc->funcexpr),
2868 exprCollation(rtfunc->funcexpr),
2869 sublevels_up);
2870 varnode->varreturningtype = returning_type;
2871 varnode->location = location;
2872
2873 *colvars = lappend(*colvars, varnode);
2874 }
2875 }
2876 else if (functypclass == TYPEFUNC_RECORD)
2877 {
2878 if (colnames)
2879 {
2880 List *namelist;
2881
2882 /* extract appropriate subset of column list */
2883 namelist = list_copy_tail(rte->eref->colnames,
2884 atts_done);
2885 namelist = list_truncate(namelist,
2886 rtfunc->funccolcount);
2887 *colnames = list_concat(*colnames, namelist);
2888 }
2889
2890 if (colvars)
2891 {
2892 ListCell *l1;
2893 ListCell *l2;
2894 ListCell *l3;
2895 int attnum = atts_done;
2896
2897 forthree(l1, rtfunc->funccoltypes,
2898 l2, rtfunc->funccoltypmods,
2899 l3, rtfunc->funccolcollations)
2900 {
2901 Oid attrtype = lfirst_oid(l1);
2902 int32 attrtypmod = lfirst_int(l2);
2903 Oid attrcollation = lfirst_oid(l3);
2904 Var *varnode;
2905
2906 attnum++;
2907 varnode = makeVar(rtindex,
2908 attnum,
2909 attrtype,
2910 attrtypmod,
2911 attrcollation,
2912 sublevels_up);
2913 varnode->varreturningtype = returning_type;
2914 varnode->location = location;
2915 *colvars = lappend(*colvars, varnode);
2916 }
2917 }
2918 }
2919 else
2920 {
2921 /* addRangeTableEntryForFunction should've caught this */
2922 elog(ERROR, "function in FROM has unsupported return type");
2923 }
2924 atts_done += rtfunc->funccolcount;
2925 }
2926
2927 /* Append the ordinality column if any */
2928 if (rte->funcordinality)
2929 {
2930 if (colnames)
2931 *colnames = lappend(*colnames,
2932 llast(rte->eref->colnames));
2933
2934 if (colvars)
2935 {
2936 Var *varnode = makeVar(rtindex,
2937 atts_done + 1,
2938 INT8OID,
2939 -1,
2940 InvalidOid,
2941 sublevels_up);
2942
2943 varnode->varreturningtype = returning_type;
2944 *colvars = lappend(*colvars, varnode);
2945 }
2946 }
2947 }
2948 break;
2949 case RTE_JOIN:
2950 {
2951 /* Join RTE */
2952 ListCell *colname;
2953 ListCell *aliasvar;
2954
2955 Assert(list_length(rte->eref->colnames) == list_length(rte->joinaliasvars));
2956
2957 varattno = 0;
2958 forboth(colname, rte->eref->colnames, aliasvar, rte->joinaliasvars)
2959 {
2960 Node *avar = (Node *) lfirst(aliasvar);
2961
2962 varattno++;
2963
2964 /*
2965 * During ordinary parsing, there will never be any
2966 * deleted columns in the join. While this function is
2967 * also used by the rewriter and planner, they do not
2968 * currently call it on any JOIN RTEs. Therefore, this
2969 * next bit is dead code, but it seems prudent to handle
2970 * the case correctly anyway.
2971 */
2972 if (avar == NULL)
2973 {
2974 if (include_dropped)
2975 {
2976 if (colnames)
2977 *colnames = lappend(*colnames,
2978 makeString(pstrdup("")));
2979 if (colvars)
2980 {
2981 /*
2982 * Can't use join's column type here (it might
2983 * be dropped!); but it doesn't really matter
2984 * what type the Const claims to be.
2985 */
2986 *colvars = lappend(*colvars,
2987 makeNullConst(INT4OID, -1,
2988 InvalidOid));
2989 }
2990 }
2991 continue;
2992 }
2993
2994 if (colnames)
2995 {
2996 char *label = strVal(lfirst(colname));
2997
2998 *colnames = lappend(*colnames,
3000 }
3001
3002 if (colvars)
3003 {
3004 Var *varnode;
3005
3006 /*
3007 * If the joinaliasvars entry is a simple Var, just
3008 * copy it (with adjustment of varlevelsup and
3009 * location); otherwise it is a JOIN USING column and
3010 * we must generate a join alias Var. This matches
3011 * the results that expansion of "join.*" by
3012 * expandNSItemVars would have produced, if we had
3013 * access to the ParseNamespaceItem for the join.
3014 */
3015 if (IsA(avar, Var))
3016 {
3017 varnode = copyObject((Var *) avar);
3018 varnode->varlevelsup = sublevels_up;
3019 }
3020 else
3021 varnode = makeVar(rtindex, varattno,
3022 exprType(avar),
3023 exprTypmod(avar),
3024 exprCollation(avar),
3025 sublevels_up);
3026 varnode->varreturningtype = returning_type;
3027 varnode->location = location;
3028
3029 *colvars = lappend(*colvars, varnode);
3030 }
3031 }
3032 }
3033 break;
3034 case RTE_TABLEFUNC:
3035 case RTE_VALUES:
3036 case RTE_CTE:
3038 {
3039 /* Tablefunc, Values, CTE, or ENR RTE */
3040 ListCell *aliasp_item = list_head(rte->eref->colnames);
3041 ListCell *lct;
3042 ListCell *lcm;
3043 ListCell *lcc;
3044
3045 varattno = 0;
3046 forthree(lct, rte->coltypes,
3047 lcm, rte->coltypmods,
3048 lcc, rte->colcollations)
3049 {
3050 Oid coltype = lfirst_oid(lct);
3051 int32 coltypmod = lfirst_int(lcm);
3052 Oid colcoll = lfirst_oid(lcc);
3053
3054 varattno++;
3055
3056 if (colnames)
3057 {
3058 /* Assume there is one alias per output column */
3059 if (OidIsValid(coltype))
3060 {
3061 char *label = strVal(lfirst(aliasp_item));
3062
3063 *colnames = lappend(*colnames,
3065 }
3066 else if (include_dropped)
3067 *colnames = lappend(*colnames,
3068 makeString(pstrdup("")));
3069
3070 aliasp_item = lnext(rte->eref->colnames, aliasp_item);
3071 }
3072
3073 if (colvars)
3074 {
3075 if (OidIsValid(coltype))
3076 {
3077 Var *varnode;
3078
3079 varnode = makeVar(rtindex, varattno,
3080 coltype, coltypmod, colcoll,
3081 sublevels_up);
3082 varnode->varreturningtype = returning_type;
3083 varnode->location = location;
3084
3085 *colvars = lappend(*colvars, varnode);
3086 }
3087 else if (include_dropped)
3088 {
3089 /*
3090 * It doesn't really matter what type the Const
3091 * claims to be.
3092 */
3093 *colvars = lappend(*colvars,
3094 makeNullConst(INT4OID, -1,
3095 InvalidOid));
3096 }
3097 }
3098 }
3099 }
3100 break;
3101 case RTE_RESULT:
3102 case RTE_GROUP:
3103 /* These expose no columns, so nothing to do */
3104 break;
3105 default:
3106 elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3107 }
3108}
List * list_truncate(List *list, int new_size)
Definition: list.c:631
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:388
static void expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
@ RTE_RESULT
Definition: parsenodes.h:1051
int16 attnum
Definition: pg_attribute.h:74
#define llast(l)
Definition: pg_list.h:198
Index varlevelsup
Definition: primnodes.h:294

References Assert(), attnum, copyObject, elog, ERROR, expandRelation(), expandTupleDesc(), TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), forboth, forthree, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), InvalidOid, IsA, 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(), TargetEntry::resno, RTE_CTE, RTE_FUNCTION, RTE_GROUP, 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, TYPEFUNC_SCALAR, Var::varlevelsup, and Var::varreturningtype.

Referenced by build_physical_tlist(), expandRecordVariable(), ReplaceVarFromTargetList(), set_relation_column_names(), and transformWholeRowRef().

◆ expandTupleDesc()

static void expandTupleDesc ( TupleDesc  tupdesc,
Alias eref,
int  count,
int  offset,
int  rtindex,
int  sublevels_up,
VarReturningType  returning_type,
int  location,
bool  include_dropped,
List **  colnames,
List **  colvars 
)
static

Definition at line 3140 of file parse_relation.c.

3145{
3146 ListCell *aliascell;
3147 int varattno;
3148
3149 aliascell = (offset < list_length(eref->colnames)) ?
3150 list_nth_cell(eref->colnames, offset) : NULL;
3151
3152 Assert(count <= tupdesc->natts);
3153 for (varattno = 0; varattno < count; varattno++)
3154 {
3155 Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
3156
3157 if (attr->attisdropped)
3158 {
3159 if (include_dropped)
3160 {
3161 if (colnames)
3162 *colnames = lappend(*colnames, makeString(pstrdup("")));
3163 if (colvars)
3164 {
3165 /*
3166 * can't use atttypid here, but it doesn't really matter
3167 * what type the Const claims to be.
3168 */
3169 *colvars = lappend(*colvars,
3170 makeNullConst(INT4OID, -1, InvalidOid));
3171 }
3172 }
3173 if (aliascell)
3174 aliascell = lnext(eref->colnames, aliascell);
3175 continue;
3176 }
3177
3178 if (colnames)
3179 {
3180 char *label;
3181
3182 if (aliascell)
3183 {
3184 label = strVal(lfirst(aliascell));
3185 aliascell = lnext(eref->colnames, aliascell);
3186 }
3187 else
3188 {
3189 /* If we run out of aliases, use the underlying name */
3190 label = NameStr(attr->attname);
3191 }
3192 *colnames = lappend(*colnames, makeString(pstrdup(label)));
3193 }
3194
3195 if (colvars)
3196 {
3197 Var *varnode;
3198
3199 varnode = makeVar(rtindex, varattno + offset + 1,
3200 attr->atttypid, attr->atttypmod,
3201 attr->attcollation,
3202 sublevels_up);
3203 varnode->varreturningtype = returning_type;
3204 varnode->location = location;
3205
3206 *colvars = lappend(*colvars, varnode);
3207 }
3208 }
3209}
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:277

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

Referenced by expandRelation(), and expandRTE().

◆ findNSItemForRTE()

static ParseNamespaceItem * findNSItemForRTE ( ParseState pstate,
RangeTblEntry rte 
)
static

Definition at line 3860 of file parse_relation.c.

3861{
3862 while (pstate != NULL)
3863 {
3864 ListCell *l;
3865
3866 foreach(l, pstate->p_namespace)
3867 {
3869
3870 if (nsitem->p_rte == rte)
3871 return nsitem;
3872 }
3873 pstate = pstate->parentParseState;
3874 }
3875 return NULL;
3876}

References lfirst, ParseState::p_namespace, ParseNamespaceItem::p_rte, and ParseState::parentParseState.

Referenced by rte_visible_if_lateral(), and rte_visible_if_qualified().

◆ get_parse_rowmark()

RowMarkClause * get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3561 of file parse_relation.c.

3562{
3563 ListCell *l;
3564
3565 foreach(l, qry->rowMarks)
3566 {
3567 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3568
3569 if (rc->rti == rtindex)
3570 return rc;
3571 }
3572 return NULL;
3573}
List * rowMarks
Definition: parsenodes.h:234

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

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

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3392 of file parse_relation.c.

3393{
3394 bool result;
3395
3396 switch (rte->rtekind)
3397 {
3398 case RTE_RELATION:
3399 {
3400 /*
3401 * Plain relation RTE --- get the attribute's catalog entry
3402 */
3403 HeapTuple tp;
3404 Form_pg_attribute att_tup;
3405
3406 tp = SearchSysCache2(ATTNUM,
3407 ObjectIdGetDatum(rte->relid),
3409 if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3410 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3411 attnum, rte->relid);
3412 att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3413 result = att_tup->attisdropped;
3414 ReleaseSysCache(tp);
3415 }
3416 break;
3417 case RTE_SUBQUERY:
3418 case RTE_TABLEFUNC:
3419 case RTE_VALUES:
3420 case RTE_CTE:
3421 case RTE_GROUP:
3422
3423 /*
3424 * Subselect, Table Functions, Values, CTE, GROUP RTEs never have
3425 * dropped columns
3426 */
3427 result = false;
3428 break;
3430 {
3431 /* Check dropped-ness by testing for valid coltype */
3432 if (attnum <= 0 ||
3433 attnum > list_length(rte->coltypes))
3434 elog(ERROR, "invalid varattno %d", attnum);
3435 result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3436 }
3437 break;
3438 case RTE_JOIN:
3439 {
3440 /*
3441 * A join RTE would not have dropped columns when constructed,
3442 * but one in a stored rule might contain columns that were
3443 * dropped from the underlying tables, if said columns are
3444 * nowhere explicitly referenced in the rule. This will be
3445 * signaled to us by a null pointer in the joinaliasvars list.
3446 */
3447 Var *aliasvar;
3448
3449 if (attnum <= 0 ||
3450 attnum > list_length(rte->joinaliasvars))
3451 elog(ERROR, "invalid varattno %d", attnum);
3452 aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3453
3454 result = (aliasvar == NULL);
3455 }
3456 break;
3457 case RTE_FUNCTION:
3458 {
3459 /* Function RTE */
3460 ListCell *lc;
3461 int atts_done = 0;
3462
3463 /*
3464 * Dropped attributes are only possible with functions that
3465 * return named composite types. In such a case we have to
3466 * look up the result type to see if it currently has this
3467 * column dropped. So first, loop over the funcs until we
3468 * find the one that covers the requested column.
3469 */
3470 foreach(lc, rte->functions)
3471 {
3472 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3473
3474 if (attnum > atts_done &&
3475 attnum <= atts_done + rtfunc->funccolcount)
3476 {
3477 TupleDesc tupdesc;
3478
3479 /* If it has a coldeflist, it returns RECORD */
3480 if (rtfunc->funccolnames != NIL)
3481 return false; /* can't have any dropped columns */
3482
3483 tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3484 true);
3485 if (tupdesc)
3486 {
3487 /* Composite data type, e.g. a table's row type */
3488 CompactAttribute *att;
3489
3490 Assert(tupdesc);
3491 Assert(attnum - atts_done <= tupdesc->natts);
3492 att = TupleDescCompactAttr(tupdesc,
3493 attnum - atts_done - 1);
3494 return att->attisdropped;
3495 }
3496 /* Otherwise, it can't have any dropped columns */
3497 return false;
3498 }
3499 atts_done += rtfunc->funccolcount;
3500 }
3501
3502 /* If we get here, must be looking for the ordinality column */
3503 if (rte->funcordinality && attnum == atts_done + 1)
3504 return false;
3505
3506 /* this probably can't happen ... */
3507 ereport(ERROR,
3508 (errcode(ERRCODE_UNDEFINED_COLUMN),
3509 errmsg("column %d of relation \"%s\" does not exist",
3510 attnum,
3511 rte->eref->aliasname)));
3512 result = false; /* keep compiler quiet */
3513 }
3514 break;
3515 case RTE_RESULT:
3516 /* this probably can't happen ... */
3517 ereport(ERROR,
3518 (errcode(ERRCODE_UNDEFINED_COLUMN),
3519 errmsg("column %d of relation \"%s\" does not exist",
3520 attnum,
3521 rte->eref->aliasname)));
3522 result = false; /* keep compiler quiet */
3523 break;
3524 default:
3525 elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3526 result = false; /* keep compiler quiet */
3527 }
3528
3529 return result;
3530}
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:551
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:321
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:182
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
bool attisdropped
Definition: tupdesc.h:77
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:230
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:175

References Assert(), CompactAttribute::attisdropped, attnum, elog, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_tupdesc(), GETSTRUCT(), HeapTupleIsValid, Int16GetDatum(), lfirst, list_length(), list_nth(), list_nth_oid(), NIL, ObjectIdGetDatum(), OidIsValid, ReleaseSysCache(), RTE_CTE, RTE_FUNCTION, RTE_GROUP, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), and TupleDescCompactAttr().

Referenced by AcquireRewriteLocks().

◆ get_rte_attribute_name()

char * get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3354 of file parse_relation.c.

3355{
3357 return "*";
3358
3359 /*
3360 * If there is a user-written column alias, use it.
3361 */
3362 if (rte->alias &&
3363 attnum > 0 && attnum <= list_length(rte->alias->colnames))
3364 return strVal(list_nth(rte->alias->colnames, attnum - 1));
3365
3366 /*
3367 * If the RTE is a relation, go to the system catalogs not the
3368 * eref->colnames list. This is a little slower but it will give the
3369 * right answer if the column has been renamed since the eref list was
3370 * built (which can easily happen for rules).
3371 */
3372 if (rte->rtekind == RTE_RELATION)
3373 return get_attname(rte->relid, attnum, false);
3374
3375 /*
3376 * Otherwise use the column name from eref. There should always be one.
3377 */
3378 if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3379 return strVal(list_nth(rte->eref->colnames, attnum - 1));
3380
3381 /* else caller gave us a bogus attnum */
3382 elog(ERROR, "invalid attnum %d for rangetable entry %s",
3383 attnum, rte->eref->aliasname);
3384 return NULL; /* keep compiler quiet */
3385}
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:920

References attnum, elog, ERROR, get_attname(), InvalidAttrNumber, list_length(), list_nth(), RTE_RELATION, RangeTblEntry::rtekind, and strVal.

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

◆ get_tle_by_resno()

◆ GetCTEForRTE()

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

Definition at line 561 of file parse_relation.c.

562{
563 Index levelsup;
564 ListCell *lc;
565
566 Assert(rte->rtekind == RTE_CTE);
567 levelsup = rte->ctelevelsup + rtelevelsup;
568 while (levelsup-- > 0)
569 {
570 pstate = pstate->parentParseState;
571 if (!pstate) /* shouldn't happen */
572 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
573 }
574 foreach(lc, pstate->p_ctenamespace)
575 {
577
578 if (strcmp(cte->ctename, rte->ctename) == 0)
579 return cte;
580 }
581 /* shouldn't happen */
582 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
583 return NULL; /* keep compiler quiet */
584}
unsigned int Index
Definition: c.h:622
List * p_ctenamespace
Definition: parse_node.h:206

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

Referenced by expandRecordVariable(), and markTargetListOrigin().

◆ GetNSItemByRangeTablePosn()

ParseNamespaceItem * GetNSItemByRangeTablePosn ( ParseState pstate,
int  varno,
int  sublevels_up 
)

Definition at line 514 of file parse_relation.c.

517{
518 ListCell *lc;
519
520 while (sublevels_up-- > 0)
521 {
522 pstate = pstate->parentParseState;
523 Assert(pstate != NULL);
524 }
525 foreach(lc, pstate->p_namespace)
526 {
528
529 if (nsitem->p_rtindex == varno)
530 return nsitem;
531 }
532 elog(ERROR, "nsitem not found (internal error)");
533 return NULL; /* keep compiler quiet */
534}

References Assert(), elog, ERROR, lfirst, ParseState::p_namespace, ParseNamespaceItem::p_rtindex, and ParseState::parentParseState.

Referenced by coerce_record_to_complex(), ExpandRowReference(), ParseComplexProjection(), and transformMergeStmt().

◆ GetRTEByRangeTablePosn()

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

Definition at line 541 of file parse_relation.c.

544{
545 while (sublevels_up-- > 0)
546 {
547 pstate = pstate->parentParseState;
548 Assert(pstate != NULL);
549 }
550 Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
551 return rt_fetch(varno, pstate->p_rtable);
552}
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31

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

Referenced by count_rowexpr_columns(), expandRecordVariable(), markTargetListOrigin(), and unknown_attribute().

◆ getRTEPermissionInfo()

RTEPermissionInfo * getRTEPermissionInfo ( List rteperminfos,
RangeTblEntry rte 
)

Definition at line 3958 of file parse_relation.c.

3959{
3960 RTEPermissionInfo *perminfo;
3961
3962 if (rte->perminfoindex == 0 ||
3963 rte->perminfoindex > list_length(rteperminfos))
3964 elog(ERROR, "invalid perminfoindex %u in RTE with relid %u",
3965 rte->perminfoindex, rte->relid);
3966 perminfo = list_nth_node(RTEPermissionInfo, rteperminfos,
3967 rte->perminfoindex - 1);
3968 if (perminfo->relid != rte->relid)
3969 elog(ERROR, "permission info at index %u (with relid=%u) does not match provided RTE (with relid=%u)",
3970 rte->perminfoindex, perminfo->relid, rte->relid);
3971
3972 return perminfo;
3973}
#define list_nth_node(type, list, n)
Definition: pg_list.h:327

References elog, ERROR, list_length(), list_nth_node, and RTEPermissionInfo::relid.

Referenced by add_rte_to_flat_rtable(), all_rows_selectable(), build_simple_rel(), ExecCheckPermissions(), expand_inherited_rtentry(), get_rel_all_updated_cols(), get_row_security_policies(), GetResultRTEPermissionInfo(), markQueryForLocking(), markRTEForSelectPriv(), rewriteTargetView(), subquery_planner(), and transformLockingClause().

◆ isFutureCTE()

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

Definition at line 317 of file parse_relation.c.

318{
319 for (; pstate != NULL; pstate = pstate->parentParseState)
320 {
321 ListCell *lc;
322
323 foreach(lc, pstate->p_future_ctes)
324 {
326
327 if (strcmp(cte->ctename, refname) == 0)
328 return true;
329 }
330 }
331 return false;
332}
List * p_future_ctes
Definition: parse_node.h:207

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

Referenced by parserOpenTable().

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2662 of file parse_relation.c.

2663{
2664 ListCell *l;
2665
2666 /*
2667 * If we are in a subquery specified as locked FOR UPDATE/SHARE from
2668 * parent level, then act as though there's a generic FOR UPDATE here.
2669 */
2670 if (pstate->p_locked_from_parent)
2671 return true;
2672
2673 foreach(l, pstate->p_locking_clause)
2674 {
2675 LockingClause *lc = (LockingClause *) lfirst(l);
2676
2677 if (lc->lockedRels == NIL)
2678 {
2679 /* all tables used in query */
2680 return true;
2681 }
2682 else if (refname != NULL)
2683 {
2684 /* just the named tables */
2685 ListCell *l2;
2686
2687 foreach(l2, lc->lockedRels)
2688 {
2689 RangeVar *thisrel = (RangeVar *) lfirst(l2);
2690
2691 if (strcmp(refname, thisrel->relname) == 0)
2692 return true;
2693 }
2694 }
2695 }
2696 return false;
2697}
List * lockedRels
Definition: parsenodes.h:862
bool p_locked_from_parent
Definition: parse_node.h:218
List * p_locking_clause
Definition: parse_node.h:217

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

Referenced by addRangeTableEntry(), and transformRangeSubselect().

◆ markNullableIfNeeded()

void markNullableIfNeeded ( ParseState pstate,
Var var 
)

Definition at line 1046 of file parse_relation.c.

1047{
1048 int rtindex = var->varno;
1049 Bitmapset *relids;
1050
1051 /* Find the appropriate pstate */
1052 for (int lv = 0; lv < var->varlevelsup; lv++)
1053 pstate = pstate->parentParseState;
1054
1055 /* Find currently-relevant join relids for the Var's rel */
1056 if (rtindex > 0 && rtindex <= list_length(pstate->p_nullingrels))
1057 relids = (Bitmapset *) list_nth(pstate->p_nullingrels, rtindex - 1);
1058 else
1059 relids = NULL;
1060
1061 /*
1062 * Merge with any already-declared nulling rels. (Typically there won't
1063 * be any, but let's get it right if there are.)
1064 */
1065 if (relids != NULL)
1066 var->varnullingrels = bms_union(var->varnullingrels, relids);
1067}
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
List * p_nullingrels
Definition: parse_node.h:200
int varno
Definition: primnodes.h:269

References bms_union(), if(), list_length(), list_nth(), ParseState::p_nullingrels, ParseState::parentParseState, Var::varlevelsup, and Var::varno.

Referenced by buildVarFromNSColumn(), expandNSItemVars(), scanNSItemForColumn(), and transformWholeRowRef().

◆ markRTEForSelectPriv()

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

Definition at line 1077 of file parse_relation.c.

1078{
1079 RangeTblEntry *rte = rt_fetch(rtindex, pstate->p_rtable);
1080
1081 if (rte->rtekind == RTE_RELATION)
1082 {
1083 RTEPermissionInfo *perminfo;
1084
1085 /* Make sure the rel as a whole is marked for SELECT access */
1086 perminfo = getRTEPermissionInfo(pstate->p_rteperminfos, rte);
1087 perminfo->requiredPerms |= ACL_SELECT;
1088 /* Must offset the attnum to fit in a bitmapset */
1089 perminfo->selectedCols =
1090 bms_add_member(perminfo->selectedCols,
1092 }
1093 else if (rte->rtekind == RTE_JOIN)
1094 {
1095 if (col == InvalidAttrNumber)
1096 {
1097 /*
1098 * A whole-row reference to a join has to be treated as whole-row
1099 * references to the two inputs.
1100 */
1101 JoinExpr *j;
1102
1103 if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
1104 j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
1105 else
1106 j = NULL;
1107 if (j == NULL)
1108 elog(ERROR, "could not find JoinExpr for whole-row reference");
1109
1110 /* Note: we can't see FromExpr here */
1111 if (IsA(j->larg, RangeTblRef))
1112 {
1113 int varno = ((RangeTblRef *) j->larg)->rtindex;
1114
1116 }
1117 else if (IsA(j->larg, JoinExpr))
1118 {
1119 int varno = ((JoinExpr *) j->larg)->rtindex;
1120
1122 }
1123 else
1124 elog(ERROR, "unrecognized node type: %d",
1125 (int) nodeTag(j->larg));
1126 if (IsA(j->rarg, RangeTblRef))
1127 {
1128 int varno = ((RangeTblRef *) j->rarg)->rtindex;
1129
1131 }
1132 else if (IsA(j->rarg, JoinExpr))
1133 {
1134 int varno = ((JoinExpr *) j->rarg)->rtindex;
1135
1137 }
1138 else
1139 elog(ERROR, "unrecognized node type: %d",
1140 (int) nodeTag(j->rarg));
1141 }
1142 else
1143 {
1144 /*
1145 * Join alias Vars for ordinary columns must refer to merged JOIN
1146 * USING columns. We don't need to do anything here, because the
1147 * join input columns will also be referenced in the join's qual
1148 * clause, and will get marked for select privilege there.
1149 */
1150 }
1151 }
1152 /* other RTE types don't require privilege marking */
1153}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:814
#define nodeTag(nodeptr)
Definition: nodes.h:139
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
List * p_joinexprs
Definition: parse_node.h:199
Bitmapset * selectedCols
Definition: parsenodes.h:1324
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References ACL_SELECT, bms_add_member(), elog, ERROR, FirstLowInvalidHeapAttributeNumber, getRTEPermissionInfo(), InvalidAttrNumber, IsA, j, list_length(), list_nth_node, markRTEForSelectPriv(), nodeTag, ParseState::p_joinexprs, ParseState::p_rtable, ParseState::p_rteperminfos, RTEPermissionInfo::requiredPerms, rt_fetch, RTE_JOIN, RTE_RELATION, RangeTblEntry::rtekind, and RTEPermissionInfo::selectedCols.

Referenced by markRTEForSelectPriv(), and markVarForSelectPriv().

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var 
)

Definition at line 1161 of file parse_relation.c.

1162{
1163 Index lv;
1164
1165 Assert(IsA(var, Var));
1166 /* Find the appropriate pstate if it's an uplevel Var */
1167 for (lv = 0; lv < var->varlevelsup; lv++)
1168 pstate = pstate->parentParseState;
1169 markRTEForSelectPriv(pstate, var->varno, var->varattno);
1170}
AttrNumber varattno
Definition: primnodes.h:274

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

Referenced by expandNSItemAttrs(), ExpandSingleTable(), scanNSItemForColumn(), transformJoinUsingClause(), and transformWholeRowRef().

◆ parserOpenTable()

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

Definition at line 1431 of file parse_relation.c.

1432{
1433 Relation rel;
1434 ParseCallbackState pcbstate;
1435
1436 setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1437 rel = table_openrv_extended(relation, lockmode, true);
1438 if (rel == NULL)
1439 {
1440 if (relation->schemaname)
1441 ereport(ERROR,
1443 errmsg("relation \"%s.%s\" does not exist",
1444 relation->schemaname, relation->relname)));
1445 else
1446 {
1447 /*
1448 * An unqualified name might have been meant as a reference to
1449 * some not-yet-in-scope CTE. The bare "does not exist" message
1450 * has proven remarkably unhelpful for figuring out such problems,
1451 * so we take pains to offer a specific hint.
1452 */
1453 if (isFutureCTE(pstate, relation->relname))
1454 ereport(ERROR,
1456 errmsg("relation \"%s\" does not exist",
1457 relation->relname),
1458 errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1459 relation->relname),
1460 errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1461 else
1462 ereport(ERROR,
1464 errmsg("relation \"%s\" does not exist",
1465 relation->relname)));
1466 }
1467 }
1469 return rel;
1470}
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:156
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:140
static bool isFutureCTE(ParseState *pstate, const char *refname)
char * schemaname
Definition: primnodes.h:80
Relation table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
Definition: table.c:103

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

◆ refnameNamespaceItem()

ParseNamespaceItem * refnameNamespaceItem ( ParseState pstate,
const char *  schemaname,
const char *  refname,
int  location,
int *  sublevels_up 
)

Definition at line 130 of file parse_relation.c.

135{
136 Oid relId = InvalidOid;
137
138 if (sublevels_up)
139 *sublevels_up = 0;
140
141 if (schemaname != NULL)
142 {
143 Oid namespaceId;
144
145 /*
146 * We can use LookupNamespaceNoError() here because we are only
147 * interested in finding existing RTEs. Checking USAGE permission on
148 * the schema is unnecessary since it would have already been checked
149 * when the RTE was made. Furthermore, we want to report "RTE not
150 * found", not "no permissions for schema", if the name happens to
151 * match a schema name the user hasn't got access to.
152 */
153 namespaceId = LookupNamespaceNoError(schemaname);
154 if (!OidIsValid(namespaceId))
155 return NULL;
156 relId = get_relname_relid(refname, namespaceId);
157 if (!OidIsValid(relId))
158 return NULL;
159 }
160
161 while (pstate != NULL)
162 {
163 ParseNamespaceItem *result;
164
165 if (OidIsValid(relId))
166 result = scanNameSpaceForRelid(pstate, relId, location);
167 else
168 result = scanNameSpaceForRefname(pstate, refname, location);
169
170 if (result)
171 return result;
172
173 if (sublevels_up)
174 (*sublevels_up)++;
175 else
176 break;
177
178 pstate = pstate->parentParseState;
179 }
180 return NULL;
181}
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:2052
Oid LookupNamespaceNoError(const char *nspname)
Definition: namespace.c:3425
static ParseNamespaceItem * scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
static ParseNamespaceItem * scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)

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

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

◆ rte_visible_if_lateral()

static bool rte_visible_if_lateral ( ParseState pstate,
RangeTblEntry rte 
)
static

Definition at line 3888 of file parse_relation.c.

3889{
3890 ParseNamespaceItem *nsitem;
3891
3892 /* If LATERAL *is* active, we're clearly barking up the wrong tree */
3893 if (pstate->p_lateral_active)
3894 return false;
3895 nsitem = findNSItemForRTE(pstate, rte);
3896 if (nsitem)
3897 {
3898 /* Found it, report whether it's LATERAL-only */
3899 return nsitem->p_lateral_only && nsitem->p_lateral_ok;
3900 }
3901 return false;
3902}
static ParseNamespaceItem * findNSItemForRTE(ParseState *pstate, RangeTblEntry *rte)

References findNSItemForRTE(), ParseState::p_lateral_active, ParseNamespaceItem::p_lateral_ok, and ParseNamespaceItem::p_lateral_only.

Referenced by errorMissingColumn(), and errorMissingRTE().

◆ rte_visible_if_qualified()

static bool rte_visible_if_qualified ( ParseState pstate,
RangeTblEntry rte 
)
static

Definition at line 3908 of file parse_relation.c.

3909{
3910 ParseNamespaceItem *nsitem = findNSItemForRTE(pstate, rte);
3911
3912 if (nsitem)
3913 {
3914 /* Found it, report whether it's relation-only */
3915 return nsitem->p_rel_visible && !nsitem->p_cols_visible;
3916 }
3917 return false;
3918}

References findNSItemForRTE(), ParseNamespaceItem::p_cols_visible, and ParseNamespaceItem::p_rel_visible.

Referenced by errorMissingColumn().

◆ scanNameSpaceForCTE()

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

Definition at line 286 of file parse_relation.c.

288{
289 Index levelsup;
290
291 for (levelsup = 0;
292 pstate != NULL;
293 pstate = pstate->parentParseState, levelsup++)
294 {
295 ListCell *lc;
296
297 foreach(lc, pstate->p_ctenamespace)
298 {
300
301 if (strcmp(cte->ctename, refname) == 0)
302 {
303 *ctelevelsup = levelsup;
304 return cte;
305 }
306 }
307 }
308 return NULL;
309}

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

Referenced by getNSItemForSpecialRelationTypes(), and searchRangeTableForRel().

◆ scanNameSpaceForENR()

bool scanNameSpaceForENR ( ParseState pstate,
const char *  refname 
)

Definition at line 339 of file parse_relation.c.

340{
341 return name_matches_visible_ENR(pstate, refname);
342}
bool name_matches_visible_ENR(ParseState *pstate, const char *refname)
Definition: parse_enr.c:20

References name_matches_visible_ENR().

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

◆ scanNameSpaceForRefname()

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

Definition at line 201 of file parse_relation.c.

202{
203 ParseNamespaceItem *result = NULL;
204 ListCell *l;
205
206 foreach(l, pstate->p_namespace)
207 {
209
210 /* Ignore columns-only items */
211 if (!nsitem->p_rel_visible)
212 continue;
213 /* If not inside LATERAL, ignore lateral-only items */
214 if (nsitem->p_lateral_only && !pstate->p_lateral_active)
215 continue;
216
217 if (strcmp(nsitem->p_names->aliasname, refname) == 0)
218 {
219 if (result)
221 (errcode(ERRCODE_AMBIGUOUS_ALIAS),
222 errmsg("table reference \"%s\" is ambiguous",
223 refname),
224 parser_errposition(pstate, location)));
225 check_lateral_ref_ok(pstate, nsitem, location);
226 result = nsitem;
227 }
228 }
229 return result;
230}

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

Referenced by refnameNamespaceItem().

◆ scanNameSpaceForRelid()

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

Definition at line 241 of file parse_relation.c.

242{
243 ParseNamespaceItem *result = NULL;
244 ListCell *l;
245
246 foreach(l, pstate->p_namespace)
247 {
249 RangeTblEntry *rte = nsitem->p_rte;
250
251 /* Ignore columns-only items */
252 if (!nsitem->p_rel_visible)
253 continue;
254 /* If not inside LATERAL, ignore lateral-only items */
255 if (nsitem->p_lateral_only && !pstate->p_lateral_active)
256 continue;
257 /* Ignore OLD/NEW namespace items that can appear in RETURNING */
259 continue;
260
261 /* yes, the test for alias == NULL should be there... */
262 if (rte->rtekind == RTE_RELATION &&
263 rte->relid == relid &&
264 rte->alias == NULL)
265 {
266 if (result)
268 (errcode(ERRCODE_AMBIGUOUS_ALIAS),
269 errmsg("table reference %u is ambiguous",
270 relid),
271 parser_errposition(pstate, location)));
272 check_lateral_ref_ok(pstate, nsitem, location);
273 result = nsitem;
274 }
275 }
276 return result;
277}

References 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_returning_type, ParseNamespaceItem::p_rte, parser_errposition(), RTE_RELATION, RangeTblEntry::rtekind, and VAR_RETURNING_DEFAULT.

Referenced by refnameNamespaceItem().

◆ scanNSItemForColumn()

Node * scanNSItemForColumn ( ParseState pstate,
ParseNamespaceItem nsitem,
int  sublevels_up,
const char *  colname,
int  location 
)

Definition at line 684 of file parse_relation.c.

686{
687 RangeTblEntry *rte = nsitem->p_rte;
688 int attnum;
689 Var *var;
690
691 /*
692 * Scan the nsitem's column names (or aliases) for a match. Complain if
693 * multiple matches.
694 */
695 attnum = scanRTEForColumn(pstate, rte, nsitem->p_names,
696 colname, location,
697 0, NULL);
698
700 return NULL; /* Return NULL if no match */
701
702 /* In constraint check, no system column is allowed except tableOid */
706 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
707 errmsg("system column \"%s\" reference in check constraint is invalid",
708 colname),
709 parser_errposition(pstate, location)));
710
711 /*
712 * In generated column, no system column is allowed except tableOid.
713 * (Required for stored generated, but we also do it for virtual generated
714 * for now for consistency.)
715 */
719 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
720 errmsg("cannot use system column \"%s\" in column generation expression",
721 colname),
722 parser_errposition(pstate, location)));
723
724 /*
725 * In a MERGE WHEN condition, no system column is allowed except tableOid
726 */
727 if (pstate->p_expr_kind == EXPR_KIND_MERGE_WHEN &&
730 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
731 errmsg("cannot use system column \"%s\" in MERGE WHEN condition",
732 colname),
733 parser_errposition(pstate, location)));
734
735 /* Found a valid match, so build a Var */
737 {
738 /* Get attribute data from the ParseNamespaceColumn array */
739 ParseNamespaceColumn *nscol = &nsitem->p_nscolumns[attnum - 1];
740
741 /* Complain if dropped column. See notes in scanRTEForColumn. */
742 if (nscol->p_varno == 0)
744 (errcode(ERRCODE_UNDEFINED_COLUMN),
745 errmsg("column \"%s\" of relation \"%s\" does not exist",
746 colname,
747 nsitem->p_names->aliasname)));
748
749 var = makeVar(nscol->p_varno,
750 nscol->p_varattno,
751 nscol->p_vartype,
752 nscol->p_vartypmod,
753 nscol->p_varcollid,
754 sublevels_up);
755 /* makeVar doesn't offer parameters for these, so set them by hand: */
756 var->varnosyn = nscol->p_varnosyn;
757 var->varattnosyn = nscol->p_varattnosyn;
758 }
759 else
760 {
761 /* System column, so use predetermined type data */
762 const FormData_pg_attribute *sysatt;
763
765 var = makeVar(nsitem->p_rtindex,
766 attnum,
767 sysatt->atttypid,
768 sysatt->atttypmod,
769 sysatt->attcollation,
770 sublevels_up);
771 }
772 var->location = location;
773
774 /* Mark Var for RETURNING OLD/NEW, as necessary */
775 var->varreturningtype = nsitem->p_returning_type;
776
777 /* Mark Var if it's nulled by any outer joins */
778 markNullableIfNeeded(pstate, var);
779
780 /* Require read access to the column */
781 markVarForSelectPriv(pstate, var);
782
783 return (Node *) var;
784}
@ EXPR_KIND_MERGE_WHEN
Definition: parse_node.h:58
@ EXPR_KIND_GENERATED_COLUMN
Definition: parse_node.h:83
@ EXPR_KIND_CHECK_CONSTRAINT
Definition: parse_node.h:68
static int scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, Alias *eref, const char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
ParseExprKind p_expr_kind
Definition: parse_node.h:214
#define TableOidAttributeNumber
Definition: sysattr.h:26

References Alias::aliasname, attnum, ereport, errcode(), errmsg(), ERROR, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_GENERATED_COLUMN, EXPR_KIND_MERGE_WHEN, FormData_pg_attribute, InvalidAttrNumber, Var::location, makeVar(), markNullableIfNeeded(), markVarForSelectPriv(), ParseState::p_expr_kind, ParseNamespaceItem::p_names, ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_returning_type, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, ParseNamespaceColumn::p_varattno, ParseNamespaceColumn::p_varattnosyn, ParseNamespaceColumn::p_varcollid, ParseNamespaceColumn::p_varno, ParseNamespaceColumn::p_varnosyn, ParseNamespaceColumn::p_vartype, ParseNamespaceColumn::p_vartypmod, parser_errposition(), scanRTEForColumn(), SystemAttributeDefinition(), TableOidAttributeNumber, and Var::varreturningtype.

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

◆ scanRTEForColumn()

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

Definition at line 811 of file parse_relation.c.

816{
817 int result = InvalidAttrNumber;
818 int attnum = 0;
819 ListCell *c;
820
821 /*
822 * Scan the user column names (or aliases) for a match. Complain if
823 * multiple matches.
824 *
825 * Note: eref->colnames may include entries for dropped columns, but those
826 * will be empty strings that cannot match any legal SQL identifier, so we
827 * don't bother to test for that case here.
828 *
829 * Should this somehow go wrong and we try to access a dropped column,
830 * we'll still catch it by virtue of the check in scanNSItemForColumn().
831 * Callers interested in finding match with shortest distance need to
832 * defend against this directly, though.
833 */
834 foreach(c, eref->colnames)
835 {
836 const char *attcolname = strVal(lfirst(c));
837
838 attnum++;
839 if (strcmp(attcolname, colname) == 0)
840 {
841 if (result)
843 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
844 errmsg("column reference \"%s\" is ambiguous",
845 colname),
846 parser_errposition(pstate, location)));
847 result = attnum;
848 }
849
850 /* Update fuzzy match state, if provided. */
851 if (fuzzystate != NULL)
852 updateFuzzyAttrMatchState(fuzzy_rte_penalty, fuzzystate,
853 rte, attcolname, colname, attnum);
854 }
855
856 /*
857 * If we have a unique match, return it. Note that this allows a user
858 * alias to override a system column name (such as OID) without error.
859 */
860 if (result)
861 return result;
862
863 /*
864 * If the RTE represents a real relation, consider system column names.
865 * Composites are only used for pseudo-relations like ON CONFLICT's
866 * excluded.
867 */
868 if (rte->rtekind == RTE_RELATION &&
869 rte->relkind != RELKIND_COMPOSITE_TYPE)
870 {
871 /* quick check to see if name could be a system column */
872 attnum = specialAttNum(colname);
874 {
875 /* now check to see if column actually is defined */
876 if (SearchSysCacheExists2(ATTNUM,
877 ObjectIdGetDatum(rte->relid),
879 result = attnum;
880 }
881 }
882
883 return result;
884}
static void updateFuzzyAttrMatchState(int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate, RangeTblEntry *rte, const char *actual, const char *match, int attnum)
char * c
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:102

References attnum, Alias::colnames, ereport, errcode(), errmsg(), ERROR, Int16GetDatum(), InvalidAttrNumber, lfirst, ObjectIdGetDatum(), parser_errposition(), RTE_RELATION, RangeTblEntry::rtekind, SearchSysCacheExists2, specialAttNum(), strVal, and updateFuzzyAttrMatchState().

Referenced by scanNSItemForColumn(), and searchRangeTableForCol().

◆ searchRangeTableForCol()

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

Definition at line 963 of file parse_relation.c.

965{
966 ParseState *orig_pstate = pstate;
967 FuzzyAttrMatchState *fuzzystate = palloc(sizeof(FuzzyAttrMatchState));
968
969 fuzzystate->distance = MAX_FUZZY_DISTANCE + 1;
970 fuzzystate->rfirst = NULL;
971 fuzzystate->rsecond = NULL;
972 fuzzystate->rexact1 = NULL;
973 fuzzystate->rexact2 = NULL;
974
975 while (pstate != NULL)
976 {
977 ListCell *l;
978
979 foreach(l, pstate->p_rtable)
980 {
981 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
982 int fuzzy_rte_penalty = 0;
983 int attnum;
984
985 /*
986 * Typically, it is not useful to look for matches within join
987 * RTEs; they effectively duplicate other RTEs for our purposes,
988 * and if a match is chosen from a join RTE, an unhelpful alias is
989 * displayed in the final diagnostic message.
990 */
991 if (rte->rtekind == RTE_JOIN)
992 continue;
993
994 /*
995 * If the user didn't specify an alias, then matches against one
996 * RTE are as good as another. But if the user did specify an
997 * alias, then we want at least a fuzzy - and preferably an exact
998 * - match for the range table entry.
999 */
1000 if (alias != NULL)
1001 fuzzy_rte_penalty =
1002 varstr_levenshtein_less_equal(alias, strlen(alias),
1003 rte->eref->aliasname,
1004 strlen(rte->eref->aliasname),
1005 1, 1, 1,
1007 true);
1008
1009 /*
1010 * Scan for a matching column, and update fuzzystate. Non-exact
1011 * matches are dealt with inside scanRTEForColumn, but exact
1012 * matches are handled here. (There won't be more than one exact
1013 * match in the same RTE, else we'd have thrown error earlier.)
1014 */
1015 attnum = scanRTEForColumn(orig_pstate, rte, rte->eref,
1016 colname, location,
1017 fuzzy_rte_penalty, fuzzystate);
1018 if (attnum != InvalidAttrNumber && fuzzy_rte_penalty == 0)
1019 {
1020 if (fuzzystate->rexact1 == NULL)
1021 {
1022 fuzzystate->rexact1 = rte;
1023 fuzzystate->exact1 = attnum;
1024 }
1025 else
1026 {
1027 /* Needn't worry about overwriting previous rexact2 */
1028 fuzzystate->rexact2 = rte;
1029 fuzzystate->exact2 = attnum;
1030 }
1031 }
1032 }
1033
1034 pstate = pstate->parentParseState;
1035 }
1036
1037 return fuzzystate;
1038}
#define MAX_FUZZY_DISTANCE
RangeTblEntry * rfirst
RangeTblEntry * rexact1
RangeTblEntry * rexact2
RangeTblEntry * rsecond
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)

References attnum, FuzzyAttrMatchState::distance, FuzzyAttrMatchState::exact1, FuzzyAttrMatchState::exact2, InvalidAttrNumber, lfirst, MAX_FUZZY_DISTANCE, ParseState::p_rtable, palloc(), ParseState::parentParseState, FuzzyAttrMatchState::rexact1, FuzzyAttrMatchState::rexact2, FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, RTE_JOIN, RangeTblEntry::rtekind, scanRTEForColumn(), and varstr_levenshtein_less_equal().

Referenced by errorMissingColumn().

◆ searchRangeTableForRel()

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

Definition at line 360 of file parse_relation.c.

361{
362 const char *refname = relation->relname;
363 Oid relId = InvalidOid;
364 CommonTableExpr *cte = NULL;
365 bool isenr = false;
366 Index ctelevelsup = 0;
367 Index levelsup;
368
369 /*
370 * If it's an unqualified name, check for possible CTE matches. A CTE
371 * hides any real relation matches. If no CTE, look for a matching
372 * relation.
373 *
374 * NB: It's not critical that RangeVarGetRelid return the correct answer
375 * here in the face of concurrent DDL. If it doesn't, the worst case
376 * scenario is a less-clear error message. Also, the tables involved in
377 * the query are already locked, which reduces the number of cases in
378 * which surprising behavior can occur. So we do the name lookup
379 * unlocked.
380 */
381 if (!relation->schemaname)
382 {
383 cte = scanNameSpaceForCTE(pstate, refname, &ctelevelsup);
384 if (!cte)
385 isenr = scanNameSpaceForENR(pstate, refname);
386 }
387
388 if (!cte && !isenr)
389 relId = RangeVarGetRelid(relation, NoLock, true);
390
391 /* Now look for RTEs matching either the relation/CTE/ENR or the alias */
392 for (levelsup = 0;
393 pstate != NULL;
394 pstate = pstate->parentParseState, levelsup++)
395 {
396 ListCell *l;
397
398 foreach(l, pstate->p_rtable)
399 {
400 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
401
402 if (rte->rtekind == RTE_RELATION &&
403 OidIsValid(relId) &&
404 rte->relid == relId)
405 return rte;
406 if (rte->rtekind == RTE_CTE &&
407 cte != NULL &&
408 rte->ctelevelsup + levelsup == ctelevelsup &&
409 strcmp(rte->ctename, refname) == 0)
410 return rte;
411 if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
412 isenr &&
413 strcmp(rte->enrname, refname) == 0)
414 return rte;
415 if (strcmp(rte->eref->aliasname, refname) == 0)
416 return rte;
417 }
418 }
419 return NULL;
420}
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:98
CommonTableExpr * scanNameSpaceForCTE(ParseState *pstate, const char *refname, Index *ctelevelsup)
bool scanNameSpaceForENR(ParseState *pstate, const char *refname)

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

Referenced by errorMissingRTE().

◆ specialAttNum()

static int specialAttNum ( const char *  attname)
static

Definition at line 3616 of file parse_relation.c.

3617{
3618 const FormData_pg_attribute *sysatt;
3619
3621 if (sysatt != NULL)
3622 return sysatt->attnum;
3623 return InvalidAttrNumber;
3624}
const FormData_pg_attribute * SystemAttributeByName(const char *attname)
Definition: heap.c:248

References attname, FormData_pg_attribute, InvalidAttrNumber, and SystemAttributeByName().

Referenced by attnameAttNum(), and scanRTEForColumn().

◆ updateFuzzyAttrMatchState()

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

Definition at line 591 of file parse_relation.c.

594{
595 int columndistance;
596 int matchlen;
597
598 /* Bail before computing the Levenshtein distance if there's no hope. */
599 if (fuzzy_rte_penalty > fuzzystate->distance)
600 return;
601
602 /*
603 * Outright reject dropped columns, which can appear here with apparent
604 * empty actual names, per remarks within scanRTEForColumn().
605 */
606 if (actual[0] == '\0')
607 return;
608
609 /* Use Levenshtein to compute match distance. */
610 matchlen = strlen(match);
611 columndistance =
612 varstr_levenshtein_less_equal(actual, strlen(actual), match, matchlen,
613 1, 1, 1,
614 fuzzystate->distance + 1
615 - fuzzy_rte_penalty,
616 true);
617
618 /*
619 * If more than half the characters are different, don't treat it as a
620 * match, to avoid making ridiculous suggestions.
621 */
622 if (columndistance > matchlen / 2)
623 return;
624
625 /*
626 * From this point on, we can ignore the distinction between the RTE-name
627 * distance and the column-name distance.
628 */
629 columndistance += fuzzy_rte_penalty;
630
631 /*
632 * If the new distance is less than or equal to that of the best match
633 * found so far, update fuzzystate.
634 */
635 if (columndistance < fuzzystate->distance)
636 {
637 /* Store new lowest observed distance as first/only match */
638 fuzzystate->distance = columndistance;
639 fuzzystate->rfirst = rte;
640 fuzzystate->first = attnum;
641 fuzzystate->rsecond = NULL;
642 }
643 else if (columndistance == fuzzystate->distance)
644 {
645 /* If we already have a match of this distance, update state */
646 if (fuzzystate->rsecond != NULL)
647 {
648 /*
649 * Too many matches at same distance. Clearly, this value of
650 * distance is too low a bar, so drop these entries while keeping
651 * the current distance value, so that only smaller distances will
652 * be considered interesting. Only if we find something of lower
653 * distance will we re-populate rfirst (via the stanza above).
654 */
655 fuzzystate->rfirst = NULL;
656 fuzzystate->rsecond = NULL;
657 }
658 else if (fuzzystate->rfirst != NULL)
659 {
660 /* Record as provisional second match */
661 fuzzystate->rsecond = rte;
662 fuzzystate->second = attnum;
663 }
664 else
665 {
666 /*
667 * Do nothing. When rfirst is NULL, distance is more than what we
668 * want to consider acceptable, so we should ignore this match.
669 */
670 }
671 }
672}

References attnum, FuzzyAttrMatchState::distance, FuzzyAttrMatchState::first, FuzzyAttrMatchState::rfirst, FuzzyAttrMatchState::rsecond, FuzzyAttrMatchState::second, and varstr_levenshtein_less_equal().

Referenced by scanRTEForColumn().