PostgreSQL Source Code  git master
ruleutils.h File Reference
#include "nodes/nodes.h"
#include "nodes/parsenodes.h"
#include "nodes/pg_list.h"
Include dependency graph for ruleutils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * pg_get_indexdef_string (Oid indexrelid)
 
char * pg_get_indexdef_columns (Oid indexrelid, bool pretty)
 
char * pg_get_partkeydef_columns (Oid relid, bool pretty)
 
char * pg_get_partconstrdef_string (Oid partitionId, char *aliasname)
 
char * pg_get_constraintdef_command (Oid constraintId)
 
char * deparse_expression (Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
 
Listdeparse_context_for (const char *aliasname, Oid relid)
 
Listdeparse_context_for_plan_tree (struct PlannedStmt *pstmt, List *rtable_names)
 
Listset_deparse_context_plan (List *dpcontext, struct Plan *plan, List *ancestors)
 
Listselect_rtable_names_for_explain (List *rtable, Bitmapset *rels_used)
 
char * generate_collation_name (Oid collid)
 
char * get_range_partbound_string (List *bound_datums)
 

Function Documentation

◆ deparse_context_for()

List* deparse_context_for ( const char *  aliasname,
Oid  relid 
)

Definition at line 3272 of file ruleutils.c.

References AccessShareLock, RangeTblEntry::alias, deparse_namespace::appendrels, deparse_namespace::ctes, RangeTblEntry::eref, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::lateral, list_make1, makeAlias(), makeNode, NIL, palloc0(), RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, deparse_namespace::rtable, RTE_RELATION, RangeTblEntry::rtekind, set_rtable_names(), set_simple_column_names(), and deparse_namespace::subplans.

Referenced by pg_get_constraintdef_worker(), pg_get_expr_worker(), pg_get_indexdef_worker(), pg_get_partconstrdef_string(), pg_get_partition_constraintdef(), pg_get_partkeydef_worker(), transformPartitionBound(), and transformPartitionRangeBounds().

3273 {
3274  deparse_namespace *dpns;
3275  RangeTblEntry *rte;
3276 
3277  dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
3278 
3279  /* Build a minimal RTE for the rel */
3280  rte = makeNode(RangeTblEntry);
3281  rte->rtekind = RTE_RELATION;
3282  rte->relid = relid;
3283  rte->relkind = RELKIND_RELATION; /* no need for exactness here */
3285  rte->alias = makeAlias(aliasname, NIL);
3286  rte->eref = rte->alias;
3287  rte->lateral = false;
3288  rte->inh = false;
3289  rte->inFromCl = true;
3290 
3291  /* Build one-element rtable */
3292  dpns->rtable = list_make1(rte);
3293  dpns->subplans = NIL;
3294  dpns->ctes = NIL;
3295  dpns->appendrels = NULL;
3296  set_rtable_names(dpns, NIL, NULL);
3298 
3299  /* Return a one-deep namespace stack */
3300  return list_make1(dpns);
3301 }
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1112
#define AccessShareLock
Definition: lockdefs.h:36
AppendRelInfo ** appendrels
Definition: ruleutils.c:165
#define list_make1(x1)
Definition: pg_list.h:227
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:385
static void set_rtable_names(deparse_namespace *dpns, List *parent_namespaces, Bitmapset *rels_used)
Definition: ruleutils.c:3438
void * palloc0(Size size)
Definition: mcxt.c:980
#define makeNode(_type_)
Definition: nodes.h:573
static void set_simple_column_names(deparse_namespace *dpns)
Definition: ruleutils.c:3653
RTEKind rtekind
Definition: parsenodes.h:974
Alias * eref
Definition: parsenodes.h:1113

◆ deparse_context_for_plan_tree()

List* deparse_context_for_plan_tree ( struct PlannedStmt pstmt,
List rtable_names 
)

Definition at line 3317 of file ruleutils.c.

References PlannedStmt::appendRelations, deparse_namespace::appendrels, Assert, AppendRelInfo::child_relid, deparse_namespace::ctes, lfirst_node, list_length(), list_make1, NIL, palloc0(), PlannedStmt::rtable, deparse_namespace::rtable, deparse_namespace::rtable_names, set_simple_column_names(), PlannedStmt::subplans, and deparse_namespace::subplans.

Referenced by ExplainPrintPlan().

3318 {
3319  deparse_namespace *dpns;
3320 
3321  dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
3322 
3323  /* Initialize fields that stay the same across the whole plan tree */
3324  dpns->rtable = pstmt->rtable;
3325  dpns->rtable_names = rtable_names;
3326  dpns->subplans = pstmt->subplans;
3327  dpns->ctes = NIL;
3328  if (pstmt->appendRelations)
3329  {
3330  /* Set up the array, indexed by child relid */
3331  int ntables = list_length(dpns->rtable);
3332  ListCell *lc;
3333 
3334  dpns->appendrels = (AppendRelInfo **)
3335  palloc0((ntables + 1) * sizeof(AppendRelInfo *));
3336  foreach(lc, pstmt->appendRelations)
3337  {
3338  AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
3339  Index crelid = appinfo->child_relid;
3340 
3341  Assert(crelid > 0 && crelid <= ntables);
3342  Assert(dpns->appendrels[crelid] == NULL);
3343  dpns->appendrels[crelid] = appinfo;
3344  }
3345  }
3346  else
3347  dpns->appendrels = NULL; /* don't need it */
3348 
3349  /*
3350  * Set up column name aliases. We will get rather bogus results for join
3351  * RTEs, but that doesn't matter because plan trees don't contain any join
3352  * alias Vars.
3353  */
3355 
3356  /* Return a one-deep namespace stack */
3357  return list_make1(dpns);
3358 }
#define NIL
Definition: pg_list.h:65
List * rtable_names
Definition: ruleutils.c:161
AppendRelInfo ** appendrels
Definition: ruleutils.c:165
#define list_make1(x1)
Definition: pg_list.h:227
#define lfirst_node(type, lc)
Definition: pg_list.h:193
void * palloc0(Size size)
Definition: mcxt.c:980
unsigned int Index
Definition: c.h:475
List * subplans
Definition: plannodes.h:79
#define Assert(condition)
Definition: c.h:738
static int list_length(const List *l)
Definition: pg_list.h:169
List * rtable
Definition: plannodes.h:66
static void set_simple_column_names(deparse_namespace *dpns)
Definition: ruleutils.c:3653
Index child_relid
Definition: pathnodes.h:2190
List * appendRelations
Definition: plannodes.h:77

◆ deparse_expression()

char* deparse_expression ( Node expr,
List dpcontext,
bool  forceprefix,
bool  showimplicit 
)

Definition at line 3212 of file ruleutils.c.

References deparse_expression_pretty().

Referenced by AlterDomainDefault(), DefineDomain(), pg_get_function_arg_default(), pg_get_partconstrdef_string(), print_function_arguments(), show_expression(), show_grouping_set_keys(), show_plan_tlist(), show_sort_group_keys(), show_tablesample(), transformPartitionBound(), and transformPartitionRangeBounds().

3214 {
3215  return deparse_expression_pretty(expr, dpcontext, forceprefix,
3216  showimplicit, 0, 0);
3217 }
static char * deparse_expression_pretty(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit, int prettyFlags, int startIndent)
Definition: ruleutils.c:3239

◆ generate_collation_name()

char* generate_collation_name ( Oid  collid)

Definition at line 11212 of file ruleutils.c.

References CollationIsVisible(), COLLOID, elog, ERROR, get_namespace_name(), GETSTRUCT, HeapTupleIsValid, NameStr, ObjectIdGetDatum, quote_qualified_identifier(), ReleaseSysCache(), and SearchSysCache1().

Referenced by get_const_collation(), get_from_clause_coldeflist(), get_rule_expr(), pg_collation_for(), pg_get_indexdef_worker(), and pg_get_partkeydef_worker().

11213 {
11214  HeapTuple tp;
11215  Form_pg_collation colltup;
11216  char *collname;
11217  char *nspname;
11218  char *result;
11219 
11220  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
11221  if (!HeapTupleIsValid(tp))
11222  elog(ERROR, "cache lookup failed for collation %u", collid);
11223  colltup = (Form_pg_collation) GETSTRUCT(tp);
11224  collname = NameStr(colltup->collname);
11225 
11226  if (!CollationIsVisible(collid))
11227  nspname = get_namespace_name(colltup->collnamespace);
11228  else
11229  nspname = NULL;
11230 
11231  result = quote_qualified_identifier(nspname, collname);
11232 
11233  ReleaseSysCache(tp);
11234 
11235  return result;
11236 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
bool CollationIsVisible(Oid collid)
Definition: namespace.c:2040
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10780
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:51
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:615

◆ get_range_partbound_string()

char* get_range_partbound_string ( List bound_datums)

Definition at line 11337 of file ruleutils.c.

References appendStringInfoChar(), appendStringInfoString(), buf, deparse_context::buf, castNode, StringInfoData::data, get_const_expr(), PartitionRangeDatum::kind, lfirst, makeStringInfo(), PARTITION_RANGE_DATUM_MAXVALUE, PARTITION_RANGE_DATUM_MINVALUE, val, and PartitionRangeDatum::value.

Referenced by check_new_partition_bound(), and get_rule_expr().

11338 {
11339  deparse_context context;
11341  ListCell *cell;
11342  char *sep;
11343 
11344  memset(&context, 0, sizeof(deparse_context));
11345  context.buf = buf;
11346 
11347  appendStringInfoString(buf, "(");
11348  sep = "";
11349  foreach(cell, bound_datums)
11350  {
11351  PartitionRangeDatum *datum =
11353 
11354  appendStringInfoString(buf, sep);
11355  if (datum->kind == PARTITION_RANGE_DATUM_MINVALUE)
11356  appendStringInfoString(buf, "MINVALUE");
11357  else if (datum->kind == PARTITION_RANGE_DATUM_MAXVALUE)
11358  appendStringInfoString(buf, "MAXVALUE");
11359  else
11360  {
11361  Const *val = castNode(Const, datum->value);
11362 
11363  get_const_expr(val, &context, -1);
11364  }
11365  sep = ", ";
11366  }
11367  appendStringInfoChar(buf, ')');
11368 
11369  return buf->data;
11370 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
PartitionRangeDatumKind kind
Definition: parsenodes.h:845
static void get_const_expr(Const *constval, deparse_context *context, int showtype)
Definition: ruleutils.c:9574
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
#define lfirst(lc)
Definition: pg_list.h:190
StringInfo buf
Definition: ruleutils.c:112
long val
Definition: informix.c:664

◆ pg_get_constraintdef_command()

char* pg_get_constraintdef_command ( Oid  constraintId)

Definition at line 1915 of file ruleutils.c.

References pg_get_constraintdef_worker().

Referenced by RememberConstraintForRebuilding().

1916 {
1917  return pg_get_constraintdef_worker(constraintId, true, 0, false);
1918 }
static char * pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, int prettyFlags, bool missing_ok)
Definition: ruleutils.c:1924

◆ pg_get_indexdef_columns()

char* pg_get_indexdef_columns ( Oid  indexrelid,
bool  pretty 
)

Definition at line 1168 of file ruleutils.c.

References pg_get_indexdef_worker(), PRETTYFLAG_INDENT, PRETTYFLAG_PAREN, and PRETTYFLAG_SCHEMA.

Referenced by BuildIndexValueDescription().

1169 {
1170  int prettyFlags;
1171 
1173 
1174  return pg_get_indexdef_worker(indexrelid, 0, NULL,
1175  true, true,
1176  false, false,
1177  prettyFlags, false);
1178 }
#define PRETTYFLAG_INDENT
Definition: ruleutils.c:92
#define PRETTYFLAG_SCHEMA
Definition: ruleutils.c:93
#define PRETTYFLAG_PAREN
Definition: ruleutils.c:91
static char * pg_get_indexdef_worker(Oid indexrelid, int colno, const Oid *excludeOps, bool attrsOnly, bool keysOnly, bool showTblSpc, bool inherits, int prettyFlags, bool missing_ok)
Definition: ruleutils.c:1187

◆ pg_get_indexdef_string()

char* pg_get_indexdef_string ( Oid  indexrelid)

Definition at line 1158 of file ruleutils.c.

References pg_get_indexdef_worker().

Referenced by RememberIndexForRebuilding().

1159 {
1160  return pg_get_indexdef_worker(indexrelid, 0, NULL,
1161  false, false,
1162  true, true,
1163  0, false);
1164 }
static char * pg_get_indexdef_worker(Oid indexrelid, int colno, const Oid *excludeOps, bool attrsOnly, bool keysOnly, bool showTblSpc, bool inherits, int prettyFlags, bool missing_ok)
Definition: ruleutils.c:1187

◆ pg_get_partconstrdef_string()

char* pg_get_partconstrdef_string ( Oid  partitionId,
char *  aliasname 
)

Definition at line 1859 of file ruleutils.c.

References deparse_context_for(), deparse_expression(), and get_partition_qual_relid().

Referenced by RI_PartitionRemove_Check().

1860 {
1861  Expr *constr_expr;
1862  List *context;
1863 
1864  constr_expr = get_partition_qual_relid(partitionId);
1865  context = deparse_context_for(aliasname, partitionId);
1866 
1867  return deparse_expression((Node *) constr_expr, context, true, false);
1868 }
Definition: nodes.h:525
List * deparse_context_for(const char *aliasname, Oid relid)
Definition: ruleutils.c:3272
char * deparse_expression(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
Definition: ruleutils.c:3212
Expr * get_partition_qual_relid(Oid relid)
Definition: partcache.c:298
Definition: pg_list.h:50

◆ pg_get_partkeydef_columns()

char* pg_get_partkeydef_columns ( Oid  relid,
bool  pretty 
)

Definition at line 1650 of file ruleutils.c.

References pg_get_partkeydef_worker(), PRETTYFLAG_INDENT, PRETTYFLAG_PAREN, and PRETTYFLAG_SCHEMA.

Referenced by ExecBuildSlotPartitionKeyDescription().

1651 {
1652  int prettyFlags;
1653 
1655 
1656  return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
1657 }
#define PRETTYFLAG_INDENT
Definition: ruleutils.c:92
#define PRETTYFLAG_SCHEMA
Definition: ruleutils.c:93
static char * pg_get_partkeydef_worker(Oid relid, int prettyFlags, bool attrsOnly, bool missing_ok)
Definition: ruleutils.c:1663
#define PRETTYFLAG_PAREN
Definition: ruleutils.c:91

◆ select_rtable_names_for_explain()

List* select_rtable_names_for_explain ( List rtable,
Bitmapset rels_used 
)

Definition at line 3409 of file ruleutils.c.

References deparse_namespace::appendrels, deparse_namespace::ctes, NIL, deparse_namespace::rtable, deparse_namespace::rtable_names, set_rtable_names(), and deparse_namespace::subplans.

Referenced by ExplainPrintPlan().

3410 {
3411  deparse_namespace dpns;
3412 
3413  memset(&dpns, 0, sizeof(dpns));
3414  dpns.rtable = rtable;
3415  dpns.subplans = NIL;
3416  dpns.ctes = NIL;
3417  dpns.appendrels = NULL;
3418  set_rtable_names(&dpns, NIL, rels_used);
3419  /* We needn't bother computing column aliases yet */
3420 
3421  return dpns.rtable_names;
3422 }
#define NIL
Definition: pg_list.h:65
List * rtable_names
Definition: ruleutils.c:161
AppendRelInfo ** appendrels
Definition: ruleutils.c:165
static void set_rtable_names(deparse_namespace *dpns, List *parent_namespaces, Bitmapset *rels_used)
Definition: ruleutils.c:3438

◆ set_deparse_context_plan()

List* set_deparse_context_plan ( List dpcontext,
struct Plan plan,
List ancestors 
)

Definition at line 3386 of file ruleutils.c.

References deparse_namespace::ancestors, Assert, linitial, list_length(), and set_deparse_plan().

Referenced by show_expression(), show_grouping_sets(), show_plan_tlist(), show_sort_group_keys(), and show_tablesample().

3387 {
3388  deparse_namespace *dpns;
3389 
3390  /* Should always have one-entry namespace list for Plan deparsing */
3391  Assert(list_length(dpcontext) == 1);
3392  dpns = (deparse_namespace *) linitial(dpcontext);
3393 
3394  /* Set our attention on the specific plan node passed in */
3395  set_deparse_plan(dpns, plan);
3396  dpns->ancestors = ancestors;
3397 
3398  return dpcontext;
3399 }
static void set_deparse_plan(deparse_namespace *dpns, Plan *plan)
Definition: ruleutils.c:4566
#define linitial(l)
Definition: pg_list.h:195
#define Assert(condition)
Definition: c.h:738
static int list_length(const List *l)
Definition: pg_list.h:169