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 * generate_opclass_name (Oid opclass)
 
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 3283 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().

3284 {
3285  deparse_namespace *dpns;
3286  RangeTblEntry *rte;
3287 
3288  dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
3289 
3290  /* Build a minimal RTE for the rel */
3291  rte = makeNode(RangeTblEntry);
3292  rte->rtekind = RTE_RELATION;
3293  rte->relid = relid;
3294  rte->relkind = RELKIND_RELATION; /* no need for exactness here */
3296  rte->alias = makeAlias(aliasname, NIL);
3297  rte->eref = rte->alias;
3298  rte->lateral = false;
3299  rte->inh = false;
3300  rte->inFromCl = true;
3301 
3302  /* Build one-element rtable */
3303  dpns->rtable = list_make1(rte);
3304  dpns->subplans = NIL;
3305  dpns->ctes = NIL;
3306  dpns->appendrels = NULL;
3307  set_rtable_names(dpns, NIL, NULL);
3309 
3310  /* Return a one-deep namespace stack */
3311  return list_make1(dpns);
3312 }
#define NIL
Definition: pg_list.h:65
Alias * alias
Definition: parsenodes.h:1114
#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:3449
void * palloc0(Size size)
Definition: mcxt.c:980
#define makeNode(_type_)
Definition: nodes.h:577
static void set_simple_column_names(deparse_namespace *dpns)
Definition: ruleutils.c:3664
RTEKind rtekind
Definition: parsenodes.h:976
Alias * eref
Definition: parsenodes.h:1115

◆ deparse_context_for_plan_tree()

List* deparse_context_for_plan_tree ( struct PlannedStmt pstmt,
List rtable_names 
)

Definition at line 3328 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().

3329 {
3330  deparse_namespace *dpns;
3331 
3332  dpns = (deparse_namespace *) palloc0(sizeof(deparse_namespace));
3333 
3334  /* Initialize fields that stay the same across the whole plan tree */
3335  dpns->rtable = pstmt->rtable;
3336  dpns->rtable_names = rtable_names;
3337  dpns->subplans = pstmt->subplans;
3338  dpns->ctes = NIL;
3339  if (pstmt->appendRelations)
3340  {
3341  /* Set up the array, indexed by child relid */
3342  int ntables = list_length(dpns->rtable);
3343  ListCell *lc;
3344 
3345  dpns->appendrels = (AppendRelInfo **)
3346  palloc0((ntables + 1) * sizeof(AppendRelInfo *));
3347  foreach(lc, pstmt->appendRelations)
3348  {
3349  AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
3350  Index crelid = appinfo->child_relid;
3351 
3352  Assert(crelid > 0 && crelid <= ntables);
3353  Assert(dpns->appendrels[crelid] == NULL);
3354  dpns->appendrels[crelid] = appinfo;
3355  }
3356  }
3357  else
3358  dpns->appendrels = NULL; /* don't need it */
3359 
3360  /*
3361  * Set up column name aliases. We will get rather bogus results for join
3362  * RTEs, but that doesn't matter because plan trees don't contain any join
3363  * alias Vars.
3364  */
3366 
3367  /* Return a one-deep namespace stack */
3368  return list_make1(dpns);
3369 }
#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:3664
Index child_relid
Definition: pathnodes.h:2230
List * appendRelations
Definition: plannodes.h:77

◆ deparse_expression()

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

Definition at line 3223 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().

3225 {
3226  return deparse_expression_pretty(expr, dpcontext, forceprefix,
3227  showimplicit, 0, 0);
3228 }
static char * deparse_expression_pretty(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit, int prettyFlags, int startIndent)
Definition: ruleutils.c:3250

◆ generate_collation_name()

char* generate_collation_name ( Oid  collid)

Definition at line 11253 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().

11254 {
11255  HeapTuple tp;
11256  Form_pg_collation colltup;
11257  char *collname;
11258  char *nspname;
11259  char *result;
11260 
11261  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
11262  if (!HeapTupleIsValid(tp))
11263  elog(ERROR, "cache lookup failed for collation %u", collid);
11264  colltup = (Form_pg_collation) GETSTRUCT(tp);
11265  collname = NameStr(colltup->collname);
11266 
11267  if (!CollationIsVisible(collid))
11268  nspname = get_namespace_name(colltup->collnamespace);
11269  else
11270  nspname = NULL;
11271 
11272  result = quote_qualified_identifier(nspname, collname);
11273 
11274  ReleaseSysCache(tp);
11275 
11276  return result;
11277 }
#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:3191
bool CollationIsVisible(Oid collid)
Definition: namespace.c:2048
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10821
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:214
#define NameStr(name)
Definition: c.h:615

◆ generate_opclass_name()

char* generate_opclass_name ( Oid  opclass)

Definition at line 10607 of file ruleutils.c.

References buf, StringInfoData::data, get_opclass_name(), initStringInfo(), and InvalidOid.

Referenced by index_opclass_options().

10608 {
10610 
10611  initStringInfo(&buf);
10612  get_opclass_name(opclass, InvalidOid, &buf);
10613 
10614  return &buf.data[1]; /* get_opclass_name() prepends space */
10615 }
static char * buf
Definition: pg_test_fsync.c:67
static void get_opclass_name(Oid opclass, Oid actual_datatype, StringInfo buf)
Definition: ruleutils.c:10569
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define InvalidOid
Definition: postgres_ext.h:36

◆ get_range_partbound_string()

char* get_range_partbound_string ( List bound_datums)

Definition at line 11387 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().

11388 {
11389  deparse_context context;
11391  ListCell *cell;
11392  char *sep;
11393 
11394  memset(&context, 0, sizeof(deparse_context));
11395  context.buf = buf;
11396 
11397  appendStringInfoString(buf, "(");
11398  sep = "";
11399  foreach(cell, bound_datums)
11400  {
11401  PartitionRangeDatum *datum =
11403 
11404  appendStringInfoString(buf, sep);
11405  if (datum->kind == PARTITION_RANGE_DATUM_MINVALUE)
11406  appendStringInfoString(buf, "MINVALUE");
11407  else if (datum->kind == PARTITION_RANGE_DATUM_MAXVALUE)
11408  appendStringInfoString(buf, "MAXVALUE");
11409  else
11410  {
11411  Const *val = castNode(Const, datum->value);
11412 
11413  get_const_expr(val, &context, -1);
11414  }
11415  sep = ", ";
11416  }
11417  appendStringInfoChar(buf, ')');
11418 
11419  return buf->data;
11420 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
PartitionRangeDatumKind kind
Definition: parsenodes.h:847
static void get_const_expr(Const *constval, deparse_context *context, int showtype)
Definition: ruleutils.c:9598
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 1926 of file ruleutils.c.

References pg_get_constraintdef_worker().

Referenced by RememberConstraintForRebuilding().

1927 {
1928  return pg_get_constraintdef_worker(constraintId, true, 0, false);
1929 }
static char * pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, int prettyFlags, bool missing_ok)
Definition: ruleutils.c:1935

◆ pg_get_indexdef_columns()

char* pg_get_indexdef_columns ( Oid  indexrelid,
bool  pretty 
)

Definition at line 1169 of file ruleutils.c.

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

Referenced by BuildIndexValueDescription().

1170 {
1171  int prettyFlags;
1172 
1174 
1175  return pg_get_indexdef_worker(indexrelid, 0, NULL,
1176  true, true,
1177  false, false,
1178  prettyFlags, false);
1179 }
#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:1188

◆ pg_get_indexdef_string()

char* pg_get_indexdef_string ( Oid  indexrelid)

Definition at line 1159 of file ruleutils.c.

References pg_get_indexdef_worker().

Referenced by RememberIndexForRebuilding().

1160 {
1161  return pg_get_indexdef_worker(indexrelid, 0, NULL,
1162  false, false,
1163  true, true,
1164  0, false);
1165 }
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:1188

◆ pg_get_partconstrdef_string()

char* pg_get_partconstrdef_string ( Oid  partitionId,
char *  aliasname 
)

Definition at line 1870 of file ruleutils.c.

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

Referenced by RI_PartitionRemove_Check().

1871 {
1872  Expr *constr_expr;
1873  List *context;
1874 
1875  constr_expr = get_partition_qual_relid(partitionId);
1876  context = deparse_context_for(aliasname, partitionId);
1877 
1878  return deparse_expression((Node *) constr_expr, context, true, false);
1879 }
Definition: nodes.h:529
List * deparse_context_for(const char *aliasname, Oid relid)
Definition: ruleutils.c:3283
char * deparse_expression(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
Definition: ruleutils.c:3223
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 1661 of file ruleutils.c.

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

Referenced by ExecBuildSlotPartitionKeyDescription().

1662 {
1663  int prettyFlags;
1664 
1666 
1667  return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
1668 }
#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:1674
#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 3420 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().

3421 {
3422  deparse_namespace dpns;
3423 
3424  memset(&dpns, 0, sizeof(dpns));
3425  dpns.rtable = rtable;
3426  dpns.subplans = NIL;
3427  dpns.ctes = NIL;
3428  dpns.appendrels = NULL;
3429  set_rtable_names(&dpns, NIL, rels_used);
3430  /* We needn't bother computing column aliases yet */
3431 
3432  return dpns.rtable_names;
3433 }
#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:3449

◆ set_deparse_context_plan()

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

Definition at line 3397 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().

3398 {
3399  deparse_namespace *dpns;
3400 
3401  /* Should always have one-entry namespace list for Plan deparsing */
3402  Assert(list_length(dpcontext) == 1);
3403  dpns = (deparse_namespace *) linitial(dpcontext);
3404 
3405  /* Set our attention on the specific plan node passed in */
3406  set_deparse_plan(dpns, plan);
3407  dpns->ancestors = ancestors;
3408 
3409  return dpcontext;
3410 }
static void set_deparse_plan(deparse_namespace *dpns, Plan *plan)
Definition: ruleutils.c:4577
#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