PostgreSQL Source Code  git master
partitionfuncs.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/partition.h"
#include "catalog/pg_class.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "utils/fmgrprotos.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for partitionfuncs.c:

Go to the source code of this file.

Macros

#define PG_PARTITION_TREE_COLS   4
 

Functions

static bool check_rel_can_be_partition (Oid relid)
 
Datum pg_partition_tree (PG_FUNCTION_ARGS)
 
Datum pg_partition_root (PG_FUNCTION_ARGS)
 
Datum pg_partition_ancestors (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ PG_PARTITION_TREE_COLS

#define PG_PARTITION_TREE_COLS   4

Function Documentation

◆ check_rel_can_be_partition()

static bool check_rel_can_be_partition ( Oid  relid)
static

Definition at line 35 of file partitionfuncs.c.

36 {
37  char relkind;
38  bool relispartition;
39 
40  /* Check if relation exists */
42  return false;
43 
44  relkind = get_rel_relkind(relid);
45  relispartition = get_rel_relispartition(relid);
46 
47  /* Only allow relation types that can appear in partition trees. */
48  if (!relispartition && !RELKIND_HAS_PARTITIONS(relkind))
49  return false;
50 
51  return true;
52 }
bool get_rel_relispartition(Oid relid)
Definition: lsyscache.c:2008
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1984
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
@ RELOID
Definition: syscache.h:89
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:188

References get_rel_relispartition(), get_rel_relkind(), ObjectIdGetDatum, RELOID, and SearchSysCacheExists1.

Referenced by pg_partition_ancestors(), pg_partition_root(), and pg_partition_tree().

◆ pg_partition_ancestors()

Datum pg_partition_ancestors ( PG_FUNCTION_ARGS  )

Definition at line 212 of file partitionfuncs.c.

213 {
214  Oid relid = PG_GETARG_OID(0);
215  FuncCallContext *funcctx;
216  List *ancestors;
217 
218  if (SRF_IS_FIRSTCALL())
219  {
220  MemoryContext oldcxt;
221 
222  funcctx = SRF_FIRSTCALL_INIT();
223 
224  if (!check_rel_can_be_partition(relid))
225  SRF_RETURN_DONE(funcctx);
226 
227  oldcxt = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
228 
229  ancestors = get_partition_ancestors(relid);
230  ancestors = lcons_oid(relid, ancestors);
231 
232  /* The only state we need is the ancestors list */
233  funcctx->user_fctx = (void *) ancestors;
234 
235  MemoryContextSwitchTo(oldcxt);
236  }
237 
238  funcctx = SRF_PERCALL_SETUP();
239  ancestors = (List *) funcctx->user_fctx;
240 
241  if (funcctx->call_cntr < list_length(ancestors))
242  {
243  Oid relid = list_nth_oid(ancestors, funcctx->call_cntr);
244 
245  SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(relid));
246  }
247 
248  SRF_RETURN_DONE(funcctx);
249 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:299
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:305
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:301
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:323
List * lcons_oid(Oid datum, List *list)
Definition: list.c:510
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
List * get_partition_ancestors(Oid relid)
Definition: partition.c:133
static bool check_rel_can_be_partition(Oid relid)
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
unsigned int Oid
Definition: postgres_ext.h:31
void * user_fctx
Definition: funcapi.h:82
uint64 call_cntr
Definition: funcapi.h:65
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
Definition: pg_list.h:51

References FuncCallContext::call_cntr, check_rel_can_be_partition(), get_partition_ancestors(), if(), lcons_oid(), list_length(), list_nth_oid(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, ObjectIdGetDatum, PG_GETARG_OID, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, and FuncCallContext::user_fctx.

◆ pg_partition_root()

Datum pg_partition_root ( PG_FUNCTION_ARGS  )

Definition at line 175 of file partitionfuncs.c.

176 {
177  Oid relid = PG_GETARG_OID(0);
178  Oid rootrelid;
179  List *ancestors;
180 
181  if (!check_rel_can_be_partition(relid))
182  PG_RETURN_NULL();
183 
184  /* fetch the list of ancestors */
185  ancestors = get_partition_ancestors(relid);
186 
187  /*
188  * If the input relation is already the top-most parent, just return
189  * itself.
190  */
191  if (ancestors == NIL)
192  PG_RETURN_OID(relid);
193 
194  rootrelid = llast_oid(ancestors);
195  list_free(ancestors);
196 
197  /*
198  * "rootrelid" must contain a valid OID, given that the input relation is
199  * a valid partition tree member as checked above.
200  */
201  Assert(OidIsValid(rootrelid));
202  PG_RETURN_OID(rootrelid);
203 }
#define OidIsValid(objectId)
Definition: c.h:710
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
Assert(fmt[strlen(fmt) - 1] !='\n')
void list_free(List *list)
Definition: list.c:1505
#define NIL
Definition: pg_list.h:65
#define llast_oid(l)
Definition: pg_list.h:196

References Assert(), check_rel_can_be_partition(), get_partition_ancestors(), list_free(), llast_oid, NIL, OidIsValid, PG_GETARG_OID, PG_RETURN_NULL, and PG_RETURN_OID.

◆ pg_partition_tree()

Datum pg_partition_tree ( PG_FUNCTION_ARGS  )

Definition at line 63 of file partitionfuncs.c.

64 {
65 #define PG_PARTITION_TREE_COLS 4
66  Oid rootrelid = PG_GETARG_OID(0);
67  FuncCallContext *funcctx;
69 
70  /* stuff done only on the first call of the function */
71  if (SRF_IS_FIRSTCALL())
72  {
73  MemoryContext oldcxt;
74  TupleDesc tupdesc;
75 
76  /* create a function context for cross-call persistence */
77  funcctx = SRF_FIRSTCALL_INIT();
78 
79  if (!check_rel_can_be_partition(rootrelid))
80  SRF_RETURN_DONE(funcctx);
81 
82  /* switch to memory context appropriate for multiple function calls */
84 
85  /*
86  * Find all members of inheritance set. We only need AccessShareLock
87  * on the children for the partition information lookup.
88  */
90 
92  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "relid",
93  REGCLASSOID, -1, 0);
94  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "parentid",
95  REGCLASSOID, -1, 0);
96  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "isleaf",
97  BOOLOID, -1, 0);
98  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "level",
99  INT4OID, -1, 0);
100 
101  funcctx->tuple_desc = BlessTupleDesc(tupdesc);
102 
103  /* The only state we need is the partition list */
104  funcctx->user_fctx = (void *) partitions;
105 
106  MemoryContextSwitchTo(oldcxt);
107  }
108 
109  /* stuff done on every call of the function */
110  funcctx = SRF_PERCALL_SETUP();
111  partitions = (List *) funcctx->user_fctx;
112 
113  if (funcctx->call_cntr < list_length(partitions))
114  {
115  Datum result;
117  bool nulls[PG_PARTITION_TREE_COLS];
118  HeapTuple tuple;
119  Oid parentid = InvalidOid;
120  Oid relid = list_nth_oid(partitions, funcctx->call_cntr);
121  char relkind = get_rel_relkind(relid);
122  int level = 0;
123  List *ancestors = get_partition_ancestors(relid);
124  ListCell *lc;
125 
126  /*
127  * Form tuple with appropriate data.
128  */
129  MemSet(nulls, 0, sizeof(nulls));
130  MemSet(values, 0, sizeof(values));
131 
132  /* relid */
133  values[0] = ObjectIdGetDatum(relid);
134 
135  /* parentid */
136  if (ancestors != NIL)
137  parentid = linitial_oid(ancestors);
138  if (OidIsValid(parentid))
139  values[1] = ObjectIdGetDatum(parentid);
140  else
141  nulls[1] = true;
142 
143  /* isleaf */
144  values[2] = BoolGetDatum(!RELKIND_HAS_PARTITIONS(relkind));
145 
146  /* level */
147  if (relid != rootrelid)
148  {
149  foreach(lc, ancestors)
150  {
151  level++;
152  if (lfirst_oid(lc) == rootrelid)
153  break;
154  }
155  }
156  values[3] = Int32GetDatum(level);
157 
158  tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
159  result = HeapTupleGetDatum(tuple);
160  SRF_RETURN_NEXT(funcctx, result);
161  }
162 
163  /* done when there are no more elements left */
164  SRF_RETURN_DONE(funcctx);
165 }
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define MemSet(start, val, len)
Definition: c.h:1008
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2071
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define AccessShareLock
Definition: lockdefs.h:36
#define PG_PARTITION_TREE_COLS
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:256
#define linitial_oid(l)
Definition: pg_list.h:176
#define lfirst_oid(lc)
Definition: pg_list.h:171
static int partitions
Definition: pgbench.c:236
uintptr_t Datum
Definition: postgres.h:411
#define BoolGetDatum(X)
Definition: postgres.h:446
#define Int32GetDatum(X)
Definition: postgres.h:523
#define InvalidOid
Definition: postgres_ext.h:36
TupleDesc tuple_desc
Definition: funcapi.h:112
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583

References AccessShareLock, BlessTupleDesc(), BoolGetDatum, FuncCallContext::call_cntr, check_rel_can_be_partition(), CreateTemplateTupleDesc(), find_all_inheritors(), get_partition_ancestors(), get_rel_relkind(), heap_form_tuple(), HeapTupleGetDatum, if(), Int32GetDatum, InvalidOid, lfirst_oid, linitial_oid, list_length(), list_nth_oid(), MemoryContextSwitchTo(), MemSet, FuncCallContext::multi_call_memory_ctx, NIL, ObjectIdGetDatum, OidIsValid, partitions, PG_GETARG_OID, PG_PARTITION_TREE_COLS, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, FuncCallContext::tuple_desc, TupleDescInitEntry(), FuncCallContext::user_fctx, and values.