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

3456 {
3457  ListCell *l;
3458 
3459  foreach(l, qry->rowMarks)
3460  {
3461  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3462 
3463  if (rc->rti == rtindex)
3464  return rc;
3465  }
3466  return NULL;
3467 }
#define lfirst(lc)
Definition: pg_list.h:172
List * rowMarks
Definition: parsenodes.h:215

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

3292 {
3293  bool result;
3294 
3295  switch (rte->rtekind)
3296  {
3297  case RTE_RELATION:
3298  {
3299  /*
3300  * Plain relation RTE --- get the attribute's catalog entry
3301  */
3302  HeapTuple tp;
3303  Form_pg_attribute att_tup;
3304 
3305  tp = SearchSysCache2(ATTNUM,
3306  ObjectIdGetDatum(rte->relid),
3308  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3309  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3310  attnum, rte->relid);
3311  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3312  result = att_tup->attisdropped;
3313  ReleaseSysCache(tp);
3314  }
3315  break;
3316  case RTE_SUBQUERY:
3317  case RTE_TABLEFUNC:
3318  case RTE_VALUES:
3319  case RTE_CTE:
3320 
3321  /*
3322  * Subselect, Table Functions, Values, CTE RTEs never have dropped
3323  * columns
3324  */
3325  result = false;
3326  break;
3327  case RTE_NAMEDTUPLESTORE:
3328  {
3329  /* Check dropped-ness by testing for valid coltype */
3330  if (attnum <= 0 ||
3331  attnum > list_length(rte->coltypes))
3332  elog(ERROR, "invalid varattno %d", attnum);
3333  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3334  }
3335  break;
3336  case RTE_JOIN:
3337  {
3338  /*
3339  * A join RTE would not have dropped columns when constructed,
3340  * but one in a stored rule might contain columns that were
3341  * dropped from the underlying tables, if said columns are
3342  * nowhere explicitly referenced in the rule. This will be
3343  * signaled to us by a null pointer in the joinaliasvars list.
3344  */
3345  Var *aliasvar;
3346 
3347  if (attnum <= 0 ||
3349  elog(ERROR, "invalid varattno %d", attnum);
3350  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3351 
3352  result = (aliasvar == NULL);
3353  }
3354  break;
3355  case RTE_FUNCTION:
3356  {
3357  /* Function RTE */
3358  ListCell *lc;
3359  int atts_done = 0;
3360 
3361  /*
3362  * Dropped attributes are only possible with functions that
3363  * return named composite types. In such a case we have to
3364  * look up the result type to see if it currently has this
3365  * column dropped. So first, loop over the funcs until we
3366  * find the one that covers the requested column.
3367  */
3368  foreach(lc, rte->functions)
3369  {
3370  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3371 
3372  if (attnum > atts_done &&
3373  attnum <= atts_done + rtfunc->funccolcount)
3374  {
3375  TupleDesc tupdesc;
3376 
3377  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3378  true);
3379  if (tupdesc)
3380  {
3381  /* Composite data type, e.g. a table's row type */
3382  Form_pg_attribute att_tup;
3383 
3384  Assert(tupdesc);
3385  Assert(attnum - atts_done <= tupdesc->natts);
3386  att_tup = TupleDescAttr(tupdesc,
3387  attnum - atts_done - 1);
3388  return att_tup->attisdropped;
3389  }
3390  /* Otherwise, it can't have any dropped columns */
3391  return false;
3392  }
3393  atts_done += rtfunc->funccolcount;
3394  }
3395 
3396  /* If we get here, must be looking for the ordinality column */
3397  if (rte->funcordinality && attnum == atts_done + 1)
3398  return false;
3399 
3400  /* this probably can't happen ... */
3401  ereport(ERROR,
3402  (errcode(ERRCODE_UNDEFINED_COLUMN),
3403  errmsg("column %d of relation \"%s\" does not exist",
3404  attnum,
3405  rte->eref->aliasname)));
3406  result = false; /* keep compiler quiet */
3407  }
3408  break;
3409  case RTE_RESULT:
3410  /* this probably can't happen ... */
3411  ereport(ERROR,
3412  (errcode(ERRCODE_UNDEFINED_COLUMN),
3413  errmsg("column %d of relation \"%s\" does not exist",
3414  attnum,
3415  rte->eref->aliasname)));
3416  result = false; /* keep compiler quiet */
3417  break;
3418  default:
3419  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3420  result = false; /* keep compiler quiet */
3421  }
3422 
3423  return result;
3424 }
#define OidIsValid(objectId)
Definition: c.h:759
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:543
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
Assert(fmt[strlen(fmt) - 1] !='\n')
@ RTE_JOIN
Definition: parsenodes.h:1016
@ RTE_CTE
Definition: parsenodes.h:1020
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1021
@ RTE_VALUES
Definition: parsenodes.h:1019
@ RTE_SUBQUERY
Definition: parsenodes.h:1015
@ RTE_RESULT
Definition: parsenodes.h:1022
@ RTE_FUNCTION
Definition: parsenodes.h:1017
@ RTE_TABLEFUNC
Definition: parsenodes.h:1018
@ RTE_RELATION
Definition: parsenodes.h:1014
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
static int list_length(const List *l)
Definition: pg_list.h:152
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:172
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
char * aliasname
Definition: primnodes.h:42
bool funcordinality
Definition: parsenodes.h:1149
Alias * eref
Definition: parsenodes.h:1200
List * coltypes
Definition: parsenodes.h:1186
List * joinaliasvars
Definition: parsenodes.h:1129
List * functions
Definition: parsenodes.h:1148
RTEKind rtekind
Definition: parsenodes.h:1033
Definition: primnodes.h:226
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:866
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:829
@ ATTNUM
Definition: syscache.h:41
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References Alias::aliasname, Assert(), attnum, ATTNUM, RangeTblEntry::coltypes, elog(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_tupdesc(), GETSTRUCT, HeapTupleIsValid, Int16GetDatum(), RangeTblEntry::joinaliasvars, lfirst, list_length(), list_nth(), list_nth_oid(), ObjectIdGetDatum(), OidIsValid, ReleaseSysCache(), RangeTblEntry::relid, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, SearchSysCache2(), and TupleDescAttr.

Referenced by AcquireRewriteLocks().

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3253 of file parse_relation.c.

3254 {
3255  if (attnum == InvalidAttrNumber)
3256  return "*";
3257 
3258  /*
3259  * If there is a user-written column alias, use it.
3260  */
3261  if (rte->alias &&
3262  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3263  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3264 
3265  /*
3266  * If the RTE is a relation, go to the system catalogs not the
3267  * eref->colnames list. This is a little slower but it will give the
3268  * right answer if the column has been renamed since the eref list was
3269  * built (which can easily happen for rules).
3270  */
3271  if (rte->rtekind == RTE_RELATION)
3272  return get_attname(rte->relid, attnum, false);
3273 
3274  /*
3275  * Otherwise use the column name from eref. There should always be one.
3276  */
3277  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3278  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3279 
3280  /* else caller gave us a bogus attnum */
3281  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3282  attnum, rte->eref->aliasname);
3283  return NULL; /* keep compiler quiet */
3284 }
#define InvalidAttrNumber
Definition: attnum.h:23
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:826
List * colnames
Definition: primnodes.h:43
Alias * alias
Definition: parsenodes.h:1199
#define strVal(v)
Definition: value.h:82

References RangeTblEntry::alias, Alias::aliasname, attnum, Alias::colnames, elog(), RangeTblEntry::eref, ERROR, get_attname(), InvalidAttrNumber, list_length(), list_nth(), RangeTblEntry::relid, RTE_RELATION, RangeTblEntry::rtekind, and strVal.

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

◆ get_tle_by_resno()