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

3391 {
3392  ListCell *l;
3393 
3394  foreach(l, qry->rowMarks)
3395  {
3396  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3397 
3398  if (rc->rti == rtindex)
3399  return rc;
3400  }
3401  return NULL;
3402 }
#define lfirst(lc)
Definition: pg_list.h:169
List * rowMarks
Definition: parsenodes.h:180

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

3227 {
3228  bool result;
3229 
3230  switch (rte->rtekind)
3231  {
3232  case RTE_RELATION:
3233  {
3234  /*
3235  * Plain relation RTE --- get the attribute's catalog entry
3236  */
3237  HeapTuple tp;
3238  Form_pg_attribute att_tup;
3239 
3240  tp = SearchSysCache2(ATTNUM,
3241  ObjectIdGetDatum(rte->relid),
3243  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3244  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3245  attnum, rte->relid);
3246  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3247  result = att_tup->attisdropped;
3248  ReleaseSysCache(tp);
3249  }
3250  break;
3251  case RTE_SUBQUERY:
3252  case RTE_TABLEFUNC:
3253  case RTE_VALUES:
3254  case RTE_CTE:
3255 
3256  /*
3257  * Subselect, Table Functions, Values, CTE RTEs never have dropped
3258  * columns
3259  */
3260  result = false;
3261  break;
3262  case RTE_NAMEDTUPLESTORE:
3263  {
3264  /* Check dropped-ness by testing for valid coltype */
3265  if (attnum <= 0 ||
3266  attnum > list_length(rte->coltypes))
3267  elog(ERROR, "invalid varattno %d", attnum);
3268  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3269  }
3270  break;
3271  case RTE_JOIN:
3272  {
3273  /*
3274  * A join RTE would not have dropped columns when constructed,
3275  * but one in a stored rule might contain columns that were
3276  * dropped from the underlying tables, if said columns are
3277  * nowhere explicitly referenced in the rule. This will be
3278  * signaled to us by a null pointer in the joinaliasvars list.
3279  */
3280  Var *aliasvar;
3281 
3282  if (attnum <= 0 ||
3284  elog(ERROR, "invalid varattno %d", attnum);
3285  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3286 
3287  result = (aliasvar == NULL);
3288  }
3289  break;
3290  case RTE_FUNCTION:
3291  {
3292  /* Function RTE */
3293  ListCell *lc;
3294  int atts_done = 0;
3295 
3296  /*
3297  * Dropped attributes are only possible with functions that
3298  * return named composite types. In such a case we have to
3299  * look up the result type to see if it currently has this
3300  * column dropped. So first, loop over the funcs until we
3301  * find the one that covers the requested column.
3302  */
3303  foreach(lc, rte->functions)
3304  {
3305  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3306 
3307  if (attnum > atts_done &&
3308  attnum <= atts_done + rtfunc->funccolcount)
3309  {
3310  TupleDesc tupdesc;
3311 
3312  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3313  true);
3314  if (tupdesc)
3315  {
3316  /* Composite data type, e.g. a table's row type */
3317  Form_pg_attribute att_tup;
3318 
3319  Assert(tupdesc);
3320  Assert(attnum - atts_done <= tupdesc->natts);
3321  att_tup = TupleDescAttr(tupdesc,
3322  attnum - atts_done - 1);
3323  return att_tup->attisdropped;
3324  }
3325  /* Otherwise, it can't have any dropped columns */
3326  return false;
3327  }
3328  atts_done += rtfunc->funccolcount;
3329  }
3330 
3331  /* If we get here, must be looking for the ordinality column */
3332  if (rte->funcordinality && attnum == atts_done + 1)
3333  return false;
3334 
3335  /* this probably can't happen ... */
3336  ereport(ERROR,
3337  (errcode(ERRCODE_UNDEFINED_COLUMN),
3338  errmsg("column %d of relation \"%s\" does not exist",
3339  attnum,
3340  rte->eref->aliasname)));
3341  result = false; /* keep compiler quiet */
3342  }
3343  break;
3344  case RTE_RESULT:
3345  /* this probably can't happen ... */
3346  ereport(ERROR,
3347  (errcode(ERRCODE_UNDEFINED_COLUMN),
3348  errmsg("column %d of relation \"%s\" does not exist",
3349  attnum,
3350  rte->eref->aliasname)));
3351  result = false; /* keep compiler quiet */
3352  break;
3353  default:
3354  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3355  result = false; /* keep compiler quiet */
3356  }
3357 
3358  return result;
3359 }
#define OidIsValid(objectId)
Definition: c.h:710
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:509
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
Assert(fmt[strlen(fmt) - 1] !='\n')
@ RTE_JOIN
Definition: parsenodes.h:1000
@ RTE_CTE
Definition: parsenodes.h:1004
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1005
@ RTE_VALUES
Definition: parsenodes.h:1003
@ RTE_SUBQUERY
Definition: parsenodes.h:999
@ RTE_RESULT
Definition: parsenodes.h:1006
@ RTE_FUNCTION
Definition: parsenodes.h:1001
@ RTE_TABLEFUNC
Definition: parsenodes.h:1002
@ RTE_RELATION
Definition: parsenodes.h:998
int16 attnum
Definition: pg_attribute.h:83
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
static int list_length(const List *l)
Definition: pg_list.h:149
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:300
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define Int16GetDatum(X)
Definition: postgres.h:495
char * aliasname
Definition: primnodes.h:42
bool funcordinality
Definition: parsenodes.h:1110
Alias * eref
Definition: parsenodes.h:1161
List * coltypes
Definition: parsenodes.h:1147
List * joinaliasvars
Definition: parsenodes.h:1090
List * functions
Definition: parsenodes.h:1109
RTEKind rtekind
Definition: parsenodes.h:1015
Definition: primnodes.h:196
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1184
@ 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::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().

◆ get_rte_attribute_name()

char* get_rte_attribute_name ( RangeTblEntry rte,
AttrNumber  attnum 
)

Definition at line 3188 of file parse_relation.c.

3189 {
3190  if (attnum == InvalidAttrNumber)
3191  return "*";
3192 
3193  /*
3194  * If there is a user-written column alias, use it.
3195  */
3196  if (rte->alias &&
3197  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3198  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3199 
3200  /*
3201  * If the RTE is a relation, go to the system catalogs not the
3202  * eref->colnames list. This is a little slower but it will give the
3203  * right answer if the column has been renamed since the eref list was
3204  * built (which can easily happen for rules).
3205  */
3206  if (rte->rtekind == RTE_RELATION)
3207  return get_attname(rte->relid, attnum, false);
3208 
3209  /*
3210  * Otherwise use the column name from eref. There should always be one.
3211  */
3212  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3213  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3214 
3215  /* else caller gave us a bogus attnum */
3216  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3217  attnum, rte->eref->aliasname);
3218  return NULL; /* keep compiler quiet */
3219 }
#define InvalidAttrNumber
Definition: attnum.h:23
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:825
List * colnames
Definition: primnodes.h:43
Alias * alias
Definition: parsenodes.h:1160
#define strVal(v)
Definition: value.h:72

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