PostgreSQL Source Code git master
parsetree.h File Reference
#include "nodes/parsenodes.h"
Include dependency graph for parsetree.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define rt_fetch(rangetable_index, rangetable)    ((RangeTblEntry *) list_nth(rangetable, (rangetable_index)-1))
 

Functions

char * get_rte_attribute_name (RangeTblEntry *rte, AttrNumber attnum)
 
bool get_rte_attribute_is_dropped (RangeTblEntry *rte, AttrNumber attnum)
 
TargetEntryget_tle_by_resno (List *tlist, AttrNumber resno)
 
RowMarkClauseget_parse_rowmark (Query *qry, Index rtindex)
 

Macro Definition Documentation

◆ rt_fetch

#define rt_fetch (   rangetable_index,
  rangetable 
)     ((RangeTblEntry *) list_nth(rangetable, (rangetable_index)-1))

Definition at line 31 of file parsetree.h.

Function Documentation

◆ get_parse_rowmark()

RowMarkClause * get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3561 of file parse_relation.c.

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

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

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

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3392 of file parse_relation.c.

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

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

Referenced by AcquireRewriteLocks().

◆ get_rte_attribute_name()

char * get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3354 of file parse_relation.c.

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

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

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

◆ get_tle_by_resno()