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

Function Documentation

◆ get_parse_rowmark()

RowMarkClause* get_parse_rowmark ( Query qry,
Index  rtindex 
)

Definition at line 3359 of file parse_relation.c.

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

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

3360 {
3361  ListCell *l;
3362 
3363  foreach(l, qry->rowMarks)
3364  {
3365  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3366 
3367  if (rc->rti == rtindex)
3368  return rc;
3369  }
3370  return NULL;
3371 }
List * rowMarks
Definition: parsenodes.h:175
#define lfirst(lc)
Definition: pg_list.h:169

◆ get_rte_attribute_is_dropped()

bool get_rte_attribute_is_dropped ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3195 of file parse_relation.c.

References Alias::aliasname, Assert, ATTNUM, RangeTblEntry::coltypes, elog, RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, RangeTblFunction::funccolcount, 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().

3196 {
3197  bool result;
3198 
3199  switch (rte->rtekind)
3200  {
3201  case RTE_RELATION:
3202  {
3203  /*
3204  * Plain relation RTE --- get the attribute's catalog entry
3205  */
3206  HeapTuple tp;
3207  Form_pg_attribute att_tup;
3208 
3209  tp = SearchSysCache2(ATTNUM,
3210  ObjectIdGetDatum(rte->relid),
3212  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3213  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3214  attnum, rte->relid);
3215  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3216  result = att_tup->attisdropped;
3217  ReleaseSysCache(tp);
3218  }
3219  break;
3220  case RTE_SUBQUERY:
3221  case RTE_TABLEFUNC:
3222  case RTE_VALUES:
3223  case RTE_CTE:
3224 
3225  /*
3226  * Subselect, Table Functions, Values, CTE RTEs never have dropped
3227  * columns
3228  */
3229  result = false;
3230  break;
3231  case RTE_NAMEDTUPLESTORE:
3232  {
3233  /* Check dropped-ness by testing for valid coltype */
3234  if (attnum <= 0 ||
3235  attnum > list_length(rte->coltypes))
3236  elog(ERROR, "invalid varattno %d", attnum);
3237  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3238  }
3239  break;
3240  case RTE_JOIN:
3241  {
3242  /*
3243  * A join RTE would not have dropped columns when constructed,
3244  * but one in a stored rule might contain columns that were
3245  * dropped from the underlying tables, if said columns are
3246  * nowhere explicitly referenced in the rule. This will be
3247  * signaled to us by a null pointer in the joinaliasvars list.
3248  */
3249  Var *aliasvar;
3250 
3251  if (attnum <= 0 ||
3253  elog(ERROR, "invalid varattno %d", attnum);
3254  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3255 
3256  result = (aliasvar == NULL);
3257  }
3258  break;
3259  case RTE_FUNCTION:
3260  {
3261  /* Function RTE */
3262  ListCell *lc;
3263  int atts_done = 0;
3264 
3265  /*
3266  * Dropped attributes are only possible with functions that
3267  * return named composite types. In such a case we have to
3268  * look up the result type to see if it currently has this
3269  * column dropped. So first, loop over the funcs until we
3270  * find the one that covers the requested column.
3271  */
3272  foreach(lc, rte->functions)
3273  {
3274  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3275 
3276  if (attnum > atts_done &&
3277  attnum <= atts_done + rtfunc->funccolcount)
3278  {
3279  TupleDesc tupdesc;
3280 
3281  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3282  true);
3283  if (tupdesc)
3284  {
3285  /* Composite data type, e.g. a table's row type */
3286  Form_pg_attribute att_tup;
3287 
3288  Assert(tupdesc);
3289  Assert(attnum - atts_done <= tupdesc->natts);
3290  att_tup = TupleDescAttr(tupdesc,
3291  attnum - atts_done - 1);
3292  return att_tup->attisdropped;
3293  }
3294  /* Otherwise, it can't have any dropped columns */
3295  return false;
3296  }
3297  atts_done += rtfunc->funccolcount;
3298  }
3299 
3300  /* If we get here, must be looking for the ordinality column */
3301  if (rte->funcordinality && attnum == atts_done + 1)
3302  return false;
3303 
3304  /* this probably can't happen ... */
3305  ereport(ERROR,
3306  (errcode(ERRCODE_UNDEFINED_COLUMN),
3307  errmsg("column %d of relation \"%s\" does not exist",
3308  attnum,
3309  rte->eref->aliasname)));
3310  result = false; /* keep compiler quiet */
3311  }
3312  break;
3313  case RTE_RESULT:
3314  /* this probably can't happen ... */
3315  ereport(ERROR,
3316  (errcode(ERRCODE_UNDEFINED_COLUMN),
3317  errmsg("column %d of relation \"%s\" does not exist",
3318  attnum,
3319  rte->eref->aliasname)));
3320  result = false; /* keep compiler quiet */
3321  break;
3322  default:
3323  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3324  result = false; /* keep compiler quiet */
3325  }
3326 
3327  return result;
3328 }
List * joinaliasvars
Definition: parsenodes.h:1082
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define Int16GetDatum(X)
Definition: postgres.h:495
int errcode(int sqlerrcode)
Definition: elog.c:698
bool funcordinality
Definition: parsenodes.h:1102
Definition: primnodes.h:186
#define OidIsValid(objectId)
Definition: c.h:710
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:300
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:440
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
int16 attnum
Definition: pg_attribute.h:83
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
List * functions
Definition: parsenodes.h:1101
static int list_length(const List *l)
Definition: pg_list.h:149
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1138
RTEKind rtekind
Definition: parsenodes.h:1007
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
Alias * eref
Definition: parsenodes.h:1153
List * coltypes
Definition: parsenodes.h:1139

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3157 of file parse_relation.c.

References RangeTblEntry::alias, Alias::aliasname, 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().

3158 {
3159  if (attnum == InvalidAttrNumber)
3160  return "*";
3161 
3162  /*
3163  * If there is a user-written column alias, use it.
3164  */
3165  if (rte->alias &&
3166  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3167  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3168 
3169  /*
3170  * If the RTE is a relation, go to the system catalogs not the
3171  * eref->colnames list. This is a little slower but it will give the
3172  * right answer if the column has been renamed since the eref list was
3173  * built (which can easily happen for rules).
3174  */
3175  if (rte->rtekind == RTE_RELATION)
3176  return get_attname(rte->relid, attnum, false);
3177 
3178  /*
3179  * Otherwise use the column name from eref. There should always be one.
3180  */
3181  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3182  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3183 
3184  /* else caller gave us a bogus attnum */
3185  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3186  attnum, rte->eref->aliasname);
3187  return NULL; /* keep compiler quiet */
3188 }
Alias * alias
Definition: parsenodes.h:1152
List * colnames
Definition: primnodes.h:43
#define strVal(v)
Definition: value.h:65
#define ERROR
Definition: elog.h:46
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
int16 attnum
Definition: pg_attribute.h:83
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:1007
#define elog(elevel,...)
Definition: elog.h:232
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:825
Alias * eref
Definition: parsenodes.h:1153

◆ get_tle_by_resno()

TargetEntry* get_tle_by_resno ( List tlist,
AttrNumber  resno 
)