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:2031
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2007
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
@ RELOID
Definition: syscache.h:89
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:191

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 202 of file partitionfuncs.c.

203 {
204  Oid relid = PG_GETARG_OID(0);
205  FuncCallContext *funcctx;
206  List *ancestors;
207 
208  if (SRF_IS_FIRSTCALL())
209  {
210  MemoryContext oldcxt;
211 
212  funcctx = SRF_FIRSTCALL_INIT();
213 
214  if (!check_rel_can_be_partition(relid))
215  SRF_RETURN_DONE(funcctx);
216 
217  oldcxt = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
218 
219  ancestors = get_partition_ancestors(relid);
220  ancestors = lcons_oid(relid, ancestors);
221 
222  /* The only state we need is the ancestors list */
223  funcctx->user_fctx = (void *) ancestors;
224 
225  MemoryContextSwitchTo(oldcxt);
226  }
227 
228  funcctx = SRF_PERCALL_SETUP();
229  ancestors = (List *) funcctx->user_fctx;
230 
231  if (funcctx->call_cntr < list_length(ancestors))
232  {
233  Oid resultrel = list_nth_oid(ancestors, funcctx->call_cntr);
234 
235  SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(resultrel));
236  }
237 
238  SRF_RETURN_DONE(funcctx);
239 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
List * lcons_oid(Oid datum, List *list)
Definition: list.c:530
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
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:152
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:321
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:54

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 165 of file partitionfuncs.c.

166 {
167  Oid relid = PG_GETARG_OID(0);
168  Oid rootrelid;
169  List *ancestors;
170 
171  if (!check_rel_can_be_partition(relid))
172  PG_RETURN_NULL();
173 
174  /* fetch the list of ancestors */
175  ancestors = get_partition_ancestors(relid);
176 
177  /*
178  * If the input relation is already the top-most parent, just return
179  * itself.
180  */
181  if (ancestors == NIL)
182  PG_RETURN_OID(relid);
183 
184  rootrelid = llast_oid(ancestors);
185  list_free(ancestors);
186 
187  /*
188  * "rootrelid" must contain a valid OID, given that the input relation is
189  * a valid partition tree member as checked above.
190  */
191  Assert(OidIsValid(rootrelid));
192  PG_RETURN_OID(rootrelid);
193 }
#define OidIsValid(objectId)
Definition: c.h:764
#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:1545
#define NIL
Definition: pg_list.h:68
#define llast_oid(l)
Definition: pg_list.h:200

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 
91  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
92  elog(ERROR, "return type must be a row type");
93  funcctx->tuple_desc = tupdesc;
94 
95  /* The only state we need is the partition list */
96  funcctx->user_fctx = (void *) partitions;
97 
98  MemoryContextSwitchTo(oldcxt);
99  }
100 
101  /* stuff done on every call of the function */
102  funcctx = SRF_PERCALL_SETUP();
103  partitions = (List *) funcctx->user_fctx;
104 
105  if (funcctx->call_cntr < list_length(partitions))
106  {
107  Datum result;
109  bool nulls[PG_PARTITION_TREE_COLS] = {0};
110  HeapTuple tuple;
111  Oid parentid = InvalidOid;
112  Oid relid = list_nth_oid(partitions, funcctx->call_cntr);
113  char relkind = get_rel_relkind(relid);
114  int level = 0;
115  List *ancestors = get_partition_ancestors(relid);
116  ListCell *lc;
117 
118  /*
119  * Form tuple with appropriate data.
120  */
121 
122  /* relid */
123  values[0] = ObjectIdGetDatum(relid);
124 
125  /* parentid */
126  if (ancestors != NIL)
127  parentid = linitial_oid(ancestors);
128  if (OidIsValid(parentid))
129  values[1] = ObjectIdGetDatum(parentid);
130  else
131  nulls[1] = true;
132 
133  /* isleaf */
134  values[2] = BoolGetDatum(!RELKIND_HAS_PARTITIONS(relkind));
135 
136  /* level */
137  if (relid != rootrelid)
138  {
139  foreach(lc, ancestors)
140  {
141  level++;
142  if (lfirst_oid(lc) == rootrelid)
143  break;
144  }
145  }
146  values[3] = Int32GetDatum(level);
147 
148  tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
149  result = HeapTupleGetDatum(tuple);
150  SRF_RETURN_NEXT(funcctx, result);
151  }
152 
153  /* done when there are no more elements left */
154  SRF_RETURN_DONE(funcctx);
155 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define ERROR
Definition: elog.h:39
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
#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:180
#define lfirst_oid(lc)
Definition: pg_list.h:174
static int partitions
Definition: pgbench.c:223
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
#define InvalidOid
Definition: postgres_ext.h:36
TupleDesc tuple_desc
Definition: funcapi.h:112

References AccessShareLock, BoolGetDatum(), FuncCallContext::call_cntr, check_rel_can_be_partition(), elog(), ERROR, find_all_inheritors(), get_call_result_type(), get_partition_ancestors(), get_rel_relkind(), heap_form_tuple(), HeapTupleGetDatum(), if(), Int32GetDatum(), InvalidOid, lfirst_oid, linitial_oid, list_length(), list_nth_oid(), MemoryContextSwitchTo(), 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, TYPEFUNC_COMPOSITE, FuncCallContext::user_fctx, and values.