PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 3565 of file parse_relation.c.

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

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

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

References Assert(), 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 TupleDescAttr().

Referenced by AcquireRewriteLocks().

◆ get_rte_attribute_name()

char * get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3358 of file parse_relation.c.

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