PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 "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 34 of file partitionfuncs.c.

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

References get_rel_relispartition(), get_rel_relkind(), ObjectIdGetDatum(), 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 201 of file partitionfuncs.c.

202{
203 Oid relid = PG_GETARG_OID(0);
204 FuncCallContext *funcctx;
205 List *ancestors;
206
207 if (SRF_IS_FIRSTCALL())
208 {
209 MemoryContext oldcxt;
210
211 funcctx = SRF_FIRSTCALL_INIT();
212
213 if (!check_rel_can_be_partition(relid))
214 SRF_RETURN_DONE(funcctx);
215
217
218 ancestors = get_partition_ancestors(relid);
219 ancestors = lcons_oid(relid, ancestors);
220
221 /* The only state we need is the ancestors list */
222 funcctx->user_fctx = ancestors;
223
224 MemoryContextSwitchTo(oldcxt);
225 }
226
227 funcctx = SRF_PERCALL_SETUP();
228 ancestors = (List *) funcctx->user_fctx;
229
230 if (funcctx->call_cntr < list_length(ancestors))
231 {
232 Oid resultrel = list_nth_oid(ancestors, funcctx->call_cntr);
233
234 SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(resultrel));
235 }
236
237 SRF_RETURN_DONE(funcctx);
238}
#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:76
List * lcons_oid(Oid datum, List *list)
Definition: list.c:531
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
List * get_partition_ancestors(Oid relid)
Definition: partition.c:134
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:32
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 164 of file partitionfuncs.c.

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

63{
64#define PG_PARTITION_TREE_COLS 4
65 Oid rootrelid = PG_GETARG_OID(0);
66 FuncCallContext *funcctx;
68
69 /* stuff done only on the first call of the function */
70 if (SRF_IS_FIRSTCALL())
71 {
72 MemoryContext oldcxt;
73 TupleDesc tupdesc;
74
75 /* create a function context for cross-call persistence */
76 funcctx = SRF_FIRSTCALL_INIT();
77
78 if (!check_rel_can_be_partition(rootrelid))
79 SRF_RETURN_DONE(funcctx);
80
81 /* switch to memory context appropriate for multiple function calls */
83
84 /*
85 * Find all members of inheritance set. We only need AccessShareLock
86 * on the children for the partition information lookup.
87 */
89
90 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
91 elog(ERROR, "return type must be a row type");
92 funcctx->tuple_desc = tupdesc;
93
94 /* The only state we need is the partition list */
95 funcctx->user_fctx = partitions;
96
98 }
99
100 /* stuff done on every call of the function */
101 funcctx = SRF_PERCALL_SETUP();
102 partitions = (List *) funcctx->user_fctx;
103
104 if (funcctx->call_cntr < list_length(partitions))
105 {
106 Datum result;
108 bool nulls[PG_PARTITION_TREE_COLS] = {0};
109 HeapTuple tuple;
110 Oid parentid = InvalidOid;
111 Oid relid = list_nth_oid(partitions, funcctx->call_cntr);
112 char relkind = get_rel_relkind(relid);
113 int level = 0;
114 List *ancestors = get_partition_ancestors(relid);
115 ListCell *lc;
116
117 /*
118 * Form tuple with appropriate data.
119 */
120
121 /* relid */
122 values[0] = ObjectIdGetDatum(relid);
123
124 /* parentid */
125 if (ancestors != NIL)
126 parentid = linitial_oid(ancestors);
127 if (OidIsValid(parentid))
128 values[1] = ObjectIdGetDatum(parentid);
129 else
130 nulls[1] = true;
131
132 /* isleaf */
133 values[2] = BoolGetDatum(!RELKIND_HAS_PARTITIONS(relkind));
134
135 /* level */
136 if (relid != rootrelid)
137 {
138 foreach(lc, ancestors)
139 {
140 level++;
141 if (lfirst_oid(lc) == rootrelid)
142 break;
143 }
144 }
145 values[3] = Int32GetDatum(level);
146
147 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
148 result = HeapTupleGetDatum(tuple);
149 SRF_RETURN_NEXT(funcctx, result);
150 }
151
152 /* done when there are no more elements left */
153 SRF_RETURN_DONE(funcctx);
154}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
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:255
#define linitial_oid(l)
Definition: pg_list.h:180
#define lfirst_oid(lc)
Definition: pg_list.h:174
static int partitions
Definition: pgbench.c:224
uintptr_t Datum
Definition: postgres.h:69
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
#define InvalidOid
Definition: postgres_ext.h:37
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.