PostgreSQL Source Code  git master
partitionfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * partitionfuncs.c
4  * Functions for accessing partition-related metadata
5  *
6  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/partitionfuncs.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "access/htup_details.h"
19 #include "catalog/partition.h"
20 #include "catalog/pg_class.h"
21 #include "catalog/pg_inherits.h"
22 #include "catalog/pg_type.h"
23 #include "funcapi.h"
24 #include "utils/fmgrprotos.h"
25 #include "utils/lsyscache.h"
26 #include "utils/syscache.h"
27 
28 /*
29  * Checks if a given relation can be part of a partition tree. Returns
30  * false if the relation cannot be processed, in which case it is up to
31  * the caller to decide what to do, by either raising an error or doing
32  * something else.
33  */
34 static bool
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 }
53 
54 /*
55  * pg_partition_tree
56  *
57  * Produce a view with one row per member of a partition tree, beginning
58  * from the top-most parent given by the caller. This gives information
59  * about each partition, its immediate partitioned parent, if it is
60  * a leaf partition and its level in the hierarchy.
61  */
62 Datum
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 }
156 
157 /*
158  * pg_partition_root
159  *
160  * Returns the top-most parent of the partition tree to which a given
161  * relation belongs, or NULL if it's not (or cannot be) part of any
162  * partition tree.
163  */
164 Datum
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 }
194 
195 /*
196  * pg_partition_ancestors
197  *
198  * Produces a view with one row per ancestor of the given partition,
199  * including the input relation itself.
200  */
201 Datum
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 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define OidIsValid(objectId)
Definition: c.h:764
#define ERROR
Definition: elog.h:39
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1108
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
void list_free(List *list)
Definition: list.c:1545
List * lcons_oid(Oid datum, List *list)
Definition: list.c:530
#define AccessShareLock
Definition: lockdefs.h:36
bool get_rel_relispartition(Oid relid)
Definition: lsyscache.c:2031
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2007
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
List * get_partition_ancestors(Oid relid)
Definition: partition.c:133
Datum pg_partition_tree(PG_FUNCTION_ARGS)
#define PG_PARTITION_TREE_COLS
Datum pg_partition_root(PG_FUNCTION_ARGS)
static bool check_rel_can_be_partition(Oid relid)
Datum pg_partition_ancestors(PG_FUNCTION_ARGS)
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:256
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:321
#define llast_oid(l)
Definition: pg_list.h:200
#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 ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
#define InvalidOid
Definition: postgres_ext.h:36
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
TupleDesc tuple_desc
Definition: funcapi.h:112
Definition: pg_list.h:54
@ RELOID
Definition: syscache.h:89
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:191