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

3419 {
3420  ListCell *l;
3421 
3422  foreach(l, qry->rowMarks)
3423  {
3424  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
3425 
3426  if (rc->rti == rtindex)
3427  return rc;
3428  }
3429  return NULL;
3430 }
#define lfirst(lc)
Definition: pg_list.h:172
List * rowMarks
Definition: parsenodes.h:208

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

3255 {
3256  bool result;
3257 
3258  switch (rte->rtekind)
3259  {
3260  case RTE_RELATION:
3261  {
3262  /*
3263  * Plain relation RTE --- get the attribute's catalog entry
3264  */
3265  HeapTuple tp;
3266  Form_pg_attribute att_tup;
3267 
3268  tp = SearchSysCache2(ATTNUM,
3269  ObjectIdGetDatum(rte->relid),
3271  if (!HeapTupleIsValid(tp)) /* shouldn't happen */
3272  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
3273  attnum, rte->relid);
3274  att_tup = (Form_pg_attribute) GETSTRUCT(tp);
3275  result = att_tup->attisdropped;
3276  ReleaseSysCache(tp);
3277  }
3278  break;
3279  case RTE_SUBQUERY:
3280  case RTE_TABLEFUNC:
3281  case RTE_VALUES:
3282  case RTE_CTE:
3283 
3284  /*
3285  * Subselect, Table Functions, Values, CTE RTEs never have dropped
3286  * columns
3287  */
3288  result = false;
3289  break;
3290  case RTE_NAMEDTUPLESTORE:
3291  {
3292  /* Check dropped-ness by testing for valid coltype */
3293  if (attnum <= 0 ||
3294  attnum > list_length(rte->coltypes))
3295  elog(ERROR, "invalid varattno %d", attnum);
3296  result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
3297  }
3298  break;
3299  case RTE_JOIN:
3300  {
3301  /*
3302  * A join RTE would not have dropped columns when constructed,
3303  * but one in a stored rule might contain columns that were
3304  * dropped from the underlying tables, if said columns are
3305  * nowhere explicitly referenced in the rule. This will be
3306  * signaled to us by a null pointer in the joinaliasvars list.
3307  */
3308  Var *aliasvar;
3309 
3310  if (attnum <= 0 ||
3312  elog(ERROR, "invalid varattno %d", attnum);
3313  aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3314 
3315  result = (aliasvar == NULL);
3316  }
3317  break;
3318  case RTE_FUNCTION:
3319  {
3320  /* Function RTE */
3321  ListCell *lc;
3322  int atts_done = 0;
3323 
3324  /*
3325  * Dropped attributes are only possible with functions that
3326  * return named composite types. In such a case we have to
3327  * look up the result type to see if it currently has this
3328  * column dropped. So first, loop over the funcs until we
3329  * find the one that covers the requested column.
3330  */
3331  foreach(lc, rte->functions)
3332  {
3333  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
3334 
3335  if (attnum > atts_done &&
3336  attnum <= atts_done + rtfunc->funccolcount)
3337  {
3338  TupleDesc tupdesc;
3339 
3340  tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
3341  true);
3342  if (tupdesc)
3343  {
3344  /* Composite data type, e.g. a table's row type */
3345  Form_pg_attribute att_tup;
3346 
3347  Assert(tupdesc);
3348  Assert(attnum - atts_done <= tupdesc->natts);
3349  att_tup = TupleDescAttr(tupdesc,
3350  attnum - atts_done - 1);
3351  return att_tup->attisdropped;
3352  }
3353  /* Otherwise, it can't have any dropped columns */
3354  return false;
3355  }
3356  atts_done += rtfunc->funccolcount;
3357  }
3358 
3359  /* If we get here, must be looking for the ordinality column */
3360  if (rte->funcordinality && attnum == atts_done + 1)
3361  return false;
3362 
3363  /* this probably can't happen ... */
3364  ereport(ERROR,
3365  (errcode(ERRCODE_UNDEFINED_COLUMN),
3366  errmsg("column %d of relation \"%s\" does not exist",
3367  attnum,
3368  rte->eref->aliasname)));
3369  result = false; /* keep compiler quiet */
3370  }
3371  break;
3372  case RTE_RESULT:
3373  /* this probably can't happen ... */
3374  ereport(ERROR,
3375  (errcode(ERRCODE_UNDEFINED_COLUMN),
3376  errmsg("column %d of relation \"%s\" does not exist",
3377  attnum,
3378  rte->eref->aliasname)));
3379  result = false; /* keep compiler quiet */
3380  break;
3381  default:
3382  elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind);
3383  result = false; /* keep compiler quiet */
3384  }
3385 
3386  return result;
3387 }
#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:1008
@ RTE_CTE
Definition: parsenodes.h:1012
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1013
@ RTE_VALUES
Definition: parsenodes.h:1011
@ RTE_SUBQUERY
Definition: parsenodes.h:1007
@ RTE_RESULT
Definition: parsenodes.h:1014
@ RTE_FUNCTION
Definition: parsenodes.h:1009
@ RTE_TABLEFUNC
Definition: parsenodes.h:1010
@ RTE_RELATION
Definition: parsenodes.h:1006
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: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:1133
Alias * eref
Definition: parsenodes.h:1184
List * coltypes
Definition: parsenodes.h:1170
List * joinaliasvars
Definition: parsenodes.h:1113
List * functions
Definition: parsenodes.h:1132
RTEKind rtekind
Definition: parsenodes.h:1025
Definition: primnodes.h:215
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:865
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:828
@ 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 3216 of file parse_relation.c.

3217 {
3218  if (attnum == InvalidAttrNumber)
3219  return "*";
3220 
3221  /*
3222  * If there is a user-written column alias, use it.
3223  */
3224  if (rte->alias &&
3225  attnum > 0 && attnum <= list_length(rte->alias->colnames))
3226  return strVal(list_nth(rte->alias->colnames, attnum - 1));
3227 
3228  /*
3229  * If the RTE is a relation, go to the system catalogs not the
3230  * eref->colnames list. This is a little slower but it will give the
3231  * right answer if the column has been renamed since the eref list was
3232  * built (which can easily happen for rules).
3233  */
3234  if (rte->rtekind == RTE_RELATION)
3235  return get_attname(rte->relid, attnum, false);
3236 
3237  /*
3238  * Otherwise use the column name from eref. There should always be one.
3239  */
3240  if (attnum > 0 && attnum <= list_length(rte->eref->colnames))
3241  return strVal(list_nth(rte->eref->colnames, attnum - 1));
3242 
3243  /* else caller gave us a bogus attnum */
3244  elog(ERROR, "invalid attnum %d for rangetable entry %s",
3245  attnum, rte->eref->aliasname);
3246  return NULL; /* keep compiler quiet */
3247 }
#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:1183
#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()