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

Go to the source code of this file.

Functions

ParseNamespaceItemrefnameNamespaceItem (ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
 
CommonTableExprscanNameSpaceForCTE (ParseState *pstate, const char *refname, Index *ctelevelsup)
 
bool scanNameSpaceForENR (ParseState *pstate, const char *refname)
 
void checkNameSpaceConflicts (ParseState *pstate, List *namespace1, List *namespace2)
 
ParseNamespaceItemGetNSItemByRangeTablePosn (ParseState *pstate, int varno, int sublevels_up)
 
RangeTblEntryGetRTEByRangeTablePosn (ParseState *pstate, int varno, int sublevels_up)
 
CommonTableExprGetCTEForRTE (ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
 
NodescanNSItemForColumn (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
 
NodecolNameToVar (ParseState *pstate, const char *colname, bool localonly, int location)
 
void markNullableIfNeeded (ParseState *pstate, Var *var)
 
void markVarForSelectPriv (ParseState *pstate, Var *var)
 
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)
 
ParseNamespaceItemaddRangeTableEntryForValues (ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
 
ParseNamespaceItemaddRangeTableEntryForTableFunc (ParseState *pstate, TableFunc *tf, 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)
 
RTEPermissionInfoaddRTEPermissionInfo (List **rteperminfos, RangeTblEntry *rte)
 
RTEPermissionInfogetRTEPermissionInfo (List *rteperminfos, RangeTblEntry *rte)
 
bool isLockedRefname (ParseState *pstate, const char *refname)
 
void addNSItemToQuery (ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
 
pg_noreturn void errorMissingRTE (ParseState *pstate, RangeVar *relation)
 
pg_noreturn void errorMissingColumn (ParseState *pstate, const char *relname, const char *colname, int location)
 
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)
 
int attnameAttNum (Relation rd, const char *attname, bool sysColOK)
 
const NameDataattnumAttName (Relation rd, int attid)
 
Oid attnumTypeId (Relation rd, int attid)
 
Oid attnumCollationId (Relation rd, int attid)
 
bool isQueryUsingTempRelation (Query *query)
 

Function Documentation

◆ addNSItemToQuery()

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

Definition at line 2710 of file parse_relation.c.

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

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

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

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

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

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, RangeTblEntry::relid, 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 1751 of file parse_relation.c.

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

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(), RangeTblEntry::relid, 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 2583 of file parse_relation.c.

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

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

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

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(), RangeTblEntry::relid, 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 1584 of file parse_relation.c.

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

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, RangeTblEntry::relid, 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 1655 of file parse_relation.c.

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

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

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

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

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(), RangeTblEntry::relid, RTE_VALUES, RangeTblEntry::rtekind, snprintf, RangeTblEntry::subquery, and RangeTblEntry::values_lists.

Referenced by transformValuesClause().

◆ addRTEPermissionInfo()

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

Definition at line 3980 of file parse_relation.c.

3981{
3982 RTEPermissionInfo *perminfo;
3983
3984 Assert(OidIsValid(rte->relid));
3985 Assert(rte->perminfoindex == 0);
3986
3987 /* Nope, so make one and add to the list. */
3988 perminfo = makeNode(RTEPermissionInfo);
3989 perminfo->relid = rte->relid;
3990 perminfo->inh = rte->inh;
3991 /* Other information is set by fetching the node as and where needed. */
3992
3993 *rteperminfos = lappend(*rteperminfos, perminfo);
3994
3995 /* Note its index (1-based!) */
3996 rte->perminfoindex = list_length(*rteperminfos);
3997
3998 return perminfo;
3999}
#define OidIsValid(objectId)
Definition: c.h:746

References Assert(), RangeTblEntry::inh, RTEPermissionInfo::inh, lappend(), list_length(), makeNode, OidIsValid, RangeTblEntry::relid, 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 3589 of file parse_relation.c.

3590{
3591 int i;
3592
3593 for (i = 0; i < RelationGetNumberOfAttributes(rd); i++)
3594 {
3596
3597 if (namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
3598 return i + 1;
3599 }
3600
3601 if (sysColOK)
3602 {
3604 return i;
3605 }
3606
3607 /* on failure */
3608 return InvalidAttrNumber;
3609}
#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:519

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

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

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

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

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

Referenced by transformFkeyGetPrimaryKey().

◆ attnumTypeId()

Oid attnumTypeId ( Relation  rd,
int  attid 
)

Definition at line 3661 of file parse_relation.c.

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

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

Referenced by transformAssignedExpr(), and transformFkeyGetPrimaryKey().

◆ checkNameSpaceConflicts()

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

Definition at line 442 of file parse_relation.c.

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

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

Referenced by transformFromClause(), and transformFromClauseItem().

◆ colNameToVar()

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

Definition at line 898 of file parse_relation.c.

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

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

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

Definition at line 3771 of file parse_relation.c.

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

pg_noreturn void errorMissingRTE ( ParseState pstate,
RangeVar relation 
)

Definition at line 3700 of file parse_relation.c.

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

3294{
3295 RangeTblEntry *rte = nsitem->p_rte;
3296 RTEPermissionInfo *perminfo = nsitem->p_perminfo;
3297 List *names,
3298 *vars;
3299 ListCell *name,
3300 *var;
3301 List *te_list = NIL;
3302
3303 vars = expandNSItemVars(pstate, nsitem, sublevels_up, location, &names);
3304
3305 /*
3306 * Require read access to the table. This is normally redundant with the
3307 * markVarForSelectPriv calls below, but not if the table has zero
3308 * columns. We need not do anything if the nsitem is for a join: its
3309 * component tables will have been marked ACL_SELECT when they were added
3310 * to the rangetable. (This step changes things only for the target
3311 * relation of UPDATE/DELETE, which cannot be under a join.)
3312 */
3313 if (rte->rtekind == RTE_RELATION)
3314 {
3315 Assert(perminfo != NULL);
3316 perminfo->requiredPerms |= ACL_SELECT;
3317 }
3318
3319 forboth(name, names, var, vars)
3320 {
3321 char *label = strVal(lfirst(name));
3322 Var *varnode = (Var *) lfirst(var);
3323 TargetEntry *te;
3324
3325 te = makeTargetEntry((Expr *) varnode,
3326 (AttrNumber) pstate->p_next_resno++,
3327 label,
3328 false);
3329 te_list = lappend(te_list, te);
3330
3331 if (require_col_privs)
3332 {
3333 /* Require read access to each column */
3334 markVarForSelectPriv(pstate, varnode);
3335 }
3336 }
3337
3338 Assert(name == NULL && var == NULL); /* lists not the same length? */
3339
3340 return te_list;
3341}
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:231
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 3227 of file parse_relation.c.

3230{
3231 List *result = NIL;
3232 int colindex;
3233 ListCell *lc;
3234
3235 if (colnames)
3236 *colnames = NIL;
3237 colindex = 0;
3238 foreach(lc, nsitem->p_names->colnames)
3239 {
3240 String *colnameval = lfirst(lc);
3241 const char *colname = strVal(colnameval);
3242 ParseNamespaceColumn *nscol = nsitem->p_nscolumns + colindex;
3243
3244 if (nscol->p_dontexpand)
3245 {
3246 /* skip */
3247 }
3248 else if (colname[0])
3249 {
3250 Var *var;
3251
3252 Assert(nscol->p_varno > 0);
3253 var = makeVar(nscol->p_varno,
3254 nscol->p_varattno,
3255 nscol->p_vartype,
3256 nscol->p_vartypmod,
3257 nscol->p_varcollid,
3258 sublevels_up);
3259 /* makeVar doesn't offer parameters for these, so set by hand: */
3261 var->varnosyn = nscol->p_varnosyn;
3262 var->varattnosyn = nscol->p_varattnosyn;
3263 var->location = location;
3264
3265 /* ... and update varnullingrels */
3266 markNullableIfNeeded(pstate, var);
3267
3268 result = lappend(result, var);
3269 if (colnames)
3270 *colnames = lappend(*colnames, colnameval);
3271 }
3272 else
3273 {
3274 /* dropped column, ignore */
3275 Assert(nscol->p_varno == 0);
3276 }
3277 colindex++;
3278 }
3279 return result;
3280}
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:351
AttrNumber p_varattno
Definition: parse_node.h:347
AttrNumber p_varattnosyn
Definition: parse_node.h:353
Definition: value.h:64
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().

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

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

◆ GetCTEForRTE()

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

Definition at line 565 of file parse_relation.c.

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

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

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

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

548{
549 while (sublevels_up-- > 0)
550 {
551 pstate = pstate->parentParseState;
552 Assert(pstate != NULL);
553 }
554 Assert(varno > 0 && varno <= list_length(pstate->p_rtable));
555 return rt_fetch(varno, pstate->p_rtable);
556}
#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 4009 of file parse_relation.c.

4010{
4011 RTEPermissionInfo *perminfo;
4012
4013 if (rte->perminfoindex == 0 ||
4014 rte->perminfoindex > list_length(rteperminfos))
4015 elog(ERROR, "invalid perminfoindex %u in RTE with relid %u",
4016 rte->perminfoindex, rte->relid);
4017 perminfo = list_nth_node(RTEPermissionInfo, rteperminfos,
4018 rte->perminfoindex - 1);
4019 if (perminfo->relid != rte->relid)
4020 elog(ERROR, "permission info at index %u (with relid=%u) does not match provided RTE (with relid=%u)",
4021 rte->perminfoindex, perminfo->relid, rte->relid);
4022
4023 return perminfo;
4024}
#define list_nth_node(type, list, n)
Definition: pg_list.h:327

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

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

◆ isLockedRefname()

bool isLockedRefname ( ParseState pstate,
const char *  refname 
)

Definition at line 2666 of file parse_relation.c.

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

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

Referenced by addRangeTableEntry(), and transformRangeSubselect().

◆ isQueryUsingTempRelation()

bool isQueryUsingTempRelation ( Query query)

Definition at line 3930 of file parse_relation.c.

3931{
3932 return isQueryUsingTempRelation_walker((Node *) query, NULL);
3933}
static bool isQueryUsingTempRelation_walker(Node *node, void *context)

References isQueryUsingTempRelation_walker().

Referenced by DefineView(), and transformCreateTableAsStmt().

◆ markNullableIfNeeded()

void markNullableIfNeeded ( ParseState pstate,
Var var 
)

Definition at line 1050 of file parse_relation.c.

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

◆ markVarForSelectPriv()

void markVarForSelectPriv ( ParseState pstate,
Var var 
)

Definition at line 1165 of file parse_relation.c.

1166{
1167 Index lv;
1168
1169 Assert(IsA(var, Var));
1170 /* Find the appropriate pstate if it's an uplevel Var */
1171 for (lv = 0; lv < var->varlevelsup; lv++)
1172 pstate = pstate->parentParseState;
1173 markRTEForSelectPriv(pstate, var->varno, var->varattno);
1174}
static void markRTEForSelectPriv(ParseState *pstate, int rtindex, AttrNumber col)
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 1435 of file parse_relation.c.

1436{
1437 Relation rel;
1438 ParseCallbackState pcbstate;
1439
1440 setup_parser_errposition_callback(&pcbstate, pstate, relation->location);
1441 rel = table_openrv_extended(relation, lockmode, true);
1442 if (rel == NULL)
1443 {
1444 if (relation->schemaname)
1445 ereport(ERROR,
1447 errmsg("relation \"%s.%s\" does not exist",
1448 relation->schemaname, relation->relname)));
1449 else
1450 {
1451 /*
1452 * An unqualified name might have been meant as a reference to
1453 * some not-yet-in-scope CTE. The bare "does not exist" message
1454 * has proven remarkably unhelpful for figuring out such problems,
1455 * so we take pains to offer a specific hint.
1456 */
1457 if (isFutureCTE(pstate, relation->relname))
1458 ereport(ERROR,
1460 errmsg("relation \"%s\" does not exist",
1461 relation->relname),
1462 errdetail("There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1463 relation->relname),
1464 errhint("Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1465 else
1466 ereport(ERROR,
1468 errmsg("relation \"%s\" does not exist",
1469 relation->relname)));
1470 }
1471 }
1473 return rel;
1474}
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 134 of file parse_relation.c.

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

◆ scanNameSpaceForCTE()

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

Definition at line 290 of file parse_relation.c.

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

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

344{
345 return name_matches_visible_ENR(pstate, refname);
346}
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().

◆ scanNSItemForColumn()

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

Definition at line 688 of file parse_relation.c.

690{
691 RangeTblEntry *rte = nsitem->p_rte;
692 int attnum;
693 Var *var;
694
695 /*
696 * Scan the nsitem's column names (or aliases) for a match. Complain if
697 * multiple matches.
698 */
699 attnum = scanRTEForColumn(pstate, rte, nsitem->p_names,
700 colname, location,
701 0, NULL);
702
704 return NULL; /* Return NULL if no match */
705
706 /* In constraint check, no system column is allowed except tableOid */
710 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
711 errmsg("system column \"%s\" reference in check constraint is invalid",
712 colname),
713 parser_errposition(pstate, location)));
714
715 /*
716 * In generated column, no system column is allowed except tableOid.
717 * (Required for stored generated, but we also do it for virtual generated
718 * for now for consistency.)
719 */
723 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
724 errmsg("cannot use system column \"%s\" in column generation expression",
725 colname),
726 parser_errposition(pstate, location)));
727
728 /*
729 * In a MERGE WHEN condition, no system column is allowed except tableOid
730 */
731 if (pstate->p_expr_kind == EXPR_KIND_MERGE_WHEN &&
734 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
735 errmsg("cannot use system column \"%s\" in MERGE WHEN condition",
736 colname),
737 parser_errposition(pstate, location)));
738
739 /* Found a valid match, so build a Var */
741 {
742 /* Get attribute data from the ParseNamespaceColumn array */
743 ParseNamespaceColumn *nscol = &nsitem->p_nscolumns[attnum - 1];
744
745 /* Complain if dropped column. See notes in scanRTEForColumn. */
746 if (nscol->p_varno == 0)
748 (errcode(ERRCODE_UNDEFINED_COLUMN),
749 errmsg("column \"%s\" of relation \"%s\" does not exist",
750 colname,
751 nsitem->p_names->aliasname)));
752
753 var = makeVar(nscol->p_varno,
754 nscol->p_varattno,
755 nscol->p_vartype,
756 nscol->p_vartypmod,
757 nscol->p_varcollid,
758 sublevels_up);
759 /* makeVar doesn't offer parameters for these, so set them by hand: */
760 var->varnosyn = nscol->p_varnosyn;
761 var->varattnosyn = nscol->p_varattnosyn;
762 }
763 else
764 {
765 /* System column, so use predetermined type data */
766 const FormData_pg_attribute *sysatt;
767
769 var = makeVar(nsitem->p_rtindex,
770 attnum,
771 sysatt->atttypid,
772 sysatt->atttypmod,
773 sysatt->attcollation,
774 sublevels_up);
775 }
776 var->location = location;
777
778 /* Mark Var for RETURNING OLD/NEW, as necessary */
779 var->varreturningtype = nsitem->p_returning_type;
780
781 /* Mark Var if it's nulled by any outer joins */
782 markNullableIfNeeded(pstate, var);
783
784 /* Require read access to the column */
785 markVarForSelectPriv(pstate, var);
786
787 return (Node *) var;
788}
@ 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:230
#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().