PostgreSQL Source Code  git master
partcache.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * partcache.c
4  * Support routines for manipulating partition information cached in
5  * relcache
6  *
7  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  * src/backend/utils/cache/partcache.c
12  *
13  *-------------------------------------------------------------------------
14 */
15 #include "postgres.h"
16 
17 #include "access/hash.h"
18 #include "access/htup_details.h"
19 #include "access/nbtree.h"
20 #include "access/relation.h"
21 #include "catalog/partition.h"
22 #include "catalog/pg_inherits.h"
23 #include "catalog/pg_opclass.h"
25 #include "miscadmin.h"
26 #include "nodes/makefuncs.h"
27 #include "nodes/nodeFuncs.h"
28 #include "optimizer/optimizer.h"
30 #include "rewrite/rewriteHandler.h"
31 #include "utils/builtins.h"
32 #include "utils/datum.h"
33 #include "utils/lsyscache.h"
34 #include "utils/memutils.h"
35 #include "utils/partcache.h"
36 #include "utils/rel.h"
37 #include "utils/syscache.h"
38 
39 
40 static void RelationBuildPartitionKey(Relation relation);
42 
43 /*
44  * RelationGetPartitionKey -- get partition key, if relation is partitioned
45  *
46  * Note: partition keys are not allowed to change after the partitioned rel
47  * is created. RelationClearRelation knows this and preserves rd_partkey
48  * across relcache rebuilds, as long as the relation is open. Therefore,
49  * even though we hand back a direct pointer into the relcache entry, it's
50  * safe for callers to continue to use that pointer as long as they hold
51  * the relation open.
52  */
55 {
56  if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
57  return NULL;
58 
59  if (unlikely(rel->rd_partkey == NULL))
61 
62  return rel->rd_partkey;
63 }
64 
65 /*
66  * RelationBuildPartitionKey
67  * Build partition key data of relation, and attach to relcache
68  *
69  * Partitioning key data is a complex structure; to avoid complicated logic to
70  * free individual elements whenever the relcache entry is flushed, we give it
71  * its own memory context, a child of CacheMemoryContext, which can easily be
72  * deleted on its own. To avoid leaking memory in that context in case of an
73  * error partway through this function, the context is initially created as a
74  * child of CurTransactionContext and only re-parented to CacheMemoryContext
75  * at the end, when no further errors are possible. Also, we don't make this
76  * context the current context except in very brief code sections, out of fear
77  * that some of our callees allocate memory on their own which would be leaked
78  * permanently.
79  */
80 static void
82 {
84  HeapTuple tuple;
85  bool isnull;
86  int i;
88  AttrNumber *attrs;
89  oidvector *opclass;
90  oidvector *collation;
91  ListCell *partexprs_item;
92  Datum datum;
93  MemoryContext partkeycxt,
94  oldcxt;
95  int16 procnum;
96 
97  tuple = SearchSysCache1(PARTRELID,
99 
100  if (!HeapTupleIsValid(tuple))
101  elog(ERROR, "cache lookup failed for partition key of relation %u",
102  RelationGetRelid(relation));
103 
105  "partition key",
108  RelationGetRelationName(relation));
109 
110  key = (PartitionKey) MemoryContextAllocZero(partkeycxt,
111  sizeof(PartitionKeyData));
112 
113  /* Fixed-length attributes */
114  form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
115  key->strategy = form->partstrat;
116  key->partnatts = form->partnatts;
117 
118  /* Validate partition strategy code */
119  if (key->strategy != PARTITION_STRATEGY_LIST &&
120  key->strategy != PARTITION_STRATEGY_RANGE &&
121  key->strategy != PARTITION_STRATEGY_HASH)
122  elog(ERROR, "invalid partition strategy \"%c\"", key->strategy);
123 
124  /*
125  * We can rely on the first variable-length attribute being mapped to the
126  * relevant field of the catalog's C struct, because all previous
127  * attributes are non-nullable and fixed-length.
128  */
129  attrs = form->partattrs.values;
130 
131  /* But use the hard way to retrieve further variable-length attributes */
132  /* Operator class */
133  datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
134  Anum_pg_partitioned_table_partclass);
135  opclass = (oidvector *) DatumGetPointer(datum);
136 
137  /* Collation */
138  datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
139  Anum_pg_partitioned_table_partcollation);
140  collation = (oidvector *) DatumGetPointer(datum);
141 
142  /* Expressions */
143  datum = SysCacheGetAttr(PARTRELID, tuple,
144  Anum_pg_partitioned_table_partexprs, &isnull);
145  if (!isnull)
146  {
147  char *exprString;
148  Node *expr;
149 
150  exprString = TextDatumGetCString(datum);
151  expr = stringToNode(exprString);
152  pfree(exprString);
153 
154  /*
155  * Run the expressions through const-simplification since the planner
156  * will be comparing them to similarly-processed qual clause operands,
157  * and may fail to detect valid matches without this step; fix
158  * opfuncids while at it. We don't need to bother with
159  * canonicalize_qual() though, because partition expressions should be
160  * in canonical form already (ie, no need for OR-merging or constant
161  * elimination).
162  */
163  expr = eval_const_expressions(NULL, expr);
164  fix_opfuncids(expr);
165 
166  oldcxt = MemoryContextSwitchTo(partkeycxt);
167  key->partexprs = (List *) copyObject(expr);
168  MemoryContextSwitchTo(oldcxt);
169  }
170 
171  /* Allocate assorted arrays in the partkeycxt, which we'll fill below */
172  oldcxt = MemoryContextSwitchTo(partkeycxt);
173  key->partattrs = (AttrNumber *) palloc0(key->partnatts * sizeof(AttrNumber));
174  key->partopfamily = (Oid *) palloc0(key->partnatts * sizeof(Oid));
175  key->partopcintype = (Oid *) palloc0(key->partnatts * sizeof(Oid));
176  key->partsupfunc = (FmgrInfo *) palloc0(key->partnatts * sizeof(FmgrInfo));
177 
178  key->partcollation = (Oid *) palloc0(key->partnatts * sizeof(Oid));
179  key->parttypid = (Oid *) palloc0(key->partnatts * sizeof(Oid));
180  key->parttypmod = (int32 *) palloc0(key->partnatts * sizeof(int32));
181  key->parttyplen = (int16 *) palloc0(key->partnatts * sizeof(int16));
182  key->parttypbyval = (bool *) palloc0(key->partnatts * sizeof(bool));
183  key->parttypalign = (char *) palloc0(key->partnatts * sizeof(char));
184  key->parttypcoll = (Oid *) palloc0(key->partnatts * sizeof(Oid));
185  MemoryContextSwitchTo(oldcxt);
186 
187  /* determine support function number to search for */
188  procnum = (key->strategy == PARTITION_STRATEGY_HASH) ?
190 
191  /* Copy partattrs and fill other per-attribute info */
192  memcpy(key->partattrs, attrs, key->partnatts * sizeof(int16));
193  partexprs_item = list_head(key->partexprs);
194  for (i = 0; i < key->partnatts; i++)
195  {
196  AttrNumber attno = key->partattrs[i];
197  HeapTuple opclasstup;
198  Form_pg_opclass opclassform;
199  Oid funcid;
200 
201  /* Collect opfamily information */
202  opclasstup = SearchSysCache1(CLAOID,
203  ObjectIdGetDatum(opclass->values[i]));
204  if (!HeapTupleIsValid(opclasstup))
205  elog(ERROR, "cache lookup failed for opclass %u", opclass->values[i]);
206 
207  opclassform = (Form_pg_opclass) GETSTRUCT(opclasstup);
208  key->partopfamily[i] = opclassform->opcfamily;
209  key->partopcintype[i] = opclassform->opcintype;
210 
211  /* Get a support function for the specified opfamily and datatypes */
212  funcid = get_opfamily_proc(opclassform->opcfamily,
213  opclassform->opcintype,
214  opclassform->opcintype,
215  procnum);
216  if (!OidIsValid(funcid))
217  ereport(ERROR,
218  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
219  errmsg("operator class \"%s\" of access method %s is missing support function %d for type %s",
220  NameStr(opclassform->opcname),
221  (key->strategy == PARTITION_STRATEGY_HASH) ?
222  "hash" : "btree",
223  procnum,
224  format_type_be(opclassform->opcintype))));
225 
226  fmgr_info_cxt(funcid, &key->partsupfunc[i], partkeycxt);
227 
228  /* Collation */
229  key->partcollation[i] = collation->values[i];
230 
231  /* Collect type information */
232  if (attno != 0)
233  {
234  Form_pg_attribute att = TupleDescAttr(relation->rd_att, attno - 1);
235 
236  key->parttypid[i] = att->atttypid;
237  key->parttypmod[i] = att->atttypmod;
238  key->parttypcoll[i] = att->attcollation;
239  }
240  else
241  {
242  if (partexprs_item == NULL)
243  elog(ERROR, "wrong number of partition key expressions");
244 
245  key->parttypid[i] = exprType(lfirst(partexprs_item));
246  key->parttypmod[i] = exprTypmod(lfirst(partexprs_item));
247  key->parttypcoll[i] = exprCollation(lfirst(partexprs_item));
248 
249  partexprs_item = lnext(key->partexprs, partexprs_item);
250  }
251  get_typlenbyvalalign(key->parttypid[i],
252  &key->parttyplen[i],
253  &key->parttypbyval[i],
254  &key->parttypalign[i]);
255 
256  ReleaseSysCache(opclasstup);
257  }
258 
259  ReleaseSysCache(tuple);
260 
261  /* Assert that we're not leaking any old data during assignments below */
262  Assert(relation->rd_partkeycxt == NULL);
263  Assert(relation->rd_partkey == NULL);
264 
265  /*
266  * Success --- reparent our context and make the relcache point to the
267  * newly constructed key
268  */
270  relation->rd_partkeycxt = partkeycxt;
271  relation->rd_partkey = key;
272 }
273 
274 /*
275  * RelationGetPartitionQual
276  *
277  * Returns a list of partition quals
278  */
279 List *
281 {
282  /* Quick exit */
283  if (!rel->rd_rel->relispartition)
284  return NIL;
285 
286  return generate_partition_qual(rel);
287 }
288 
289 /*
290  * get_partition_qual_relid
291  *
292  * Returns an expression tree describing the passed-in relation's partition
293  * constraint.
294  *
295  * If the relation is not found, or is not a partition, or there is no
296  * partition constraint, return NULL. We must guard against the first two
297  * cases because this supports a SQL function that could be passed any OID.
298  * The last case can happen even if relispartition is true, when a default
299  * partition is the only partition.
300  */
301 Expr *
303 {
304  Expr *result = NULL;
305 
306  /* Do the work only if this relation exists and is a partition. */
307  if (get_rel_relispartition(relid))
308  {
309  Relation rel = relation_open(relid, AccessShareLock);
310  List *and_args;
311 
312  and_args = generate_partition_qual(rel);
313 
314  /* Convert implicit-AND list format to boolean expression */
315  if (and_args == NIL)
316  result = NULL;
317  else if (list_length(and_args) > 1)
318  result = makeBoolExpr(AND_EXPR, and_args, -1);
319  else
320  result = linitial(and_args);
321 
322  /* Keep the lock, to allow safe deparsing against the rel by caller. */
323  relation_close(rel, NoLock);
324  }
325 
326  return result;
327 }
328 
329 /*
330  * generate_partition_qual
331  *
332  * Generate partition predicate from rel's partition bound expression. The
333  * function returns a NIL list if there is no predicate.
334  *
335  * We cache a copy of the result in the relcache entry, after constructing
336  * it using the caller's context. This approach avoids leaking any data
337  * into long-lived cache contexts, especially if we fail partway through.
338  */
339 static List *
341 {
342  HeapTuple tuple;
343  MemoryContext oldcxt;
344  Datum boundDatum;
345  bool isnull;
346  List *my_qual = NIL,
347  *result = NIL;
348  Oid parentrelid;
349  Relation parent;
350 
351  /* Guard against stack overflow due to overly deep partition tree */
353 
354  /* If we already cached the result, just return a copy */
355  if (rel->rd_partcheckvalid)
356  return copyObject(rel->rd_partcheck);
357 
358  /*
359  * Grab at least an AccessShareLock on the parent table. Must do this
360  * even if the partition has been partially detached, because transactions
361  * concurrent with the detach might still be trying to use a partition
362  * descriptor that includes it.
363  */
364  parentrelid = get_partition_parent(RelationGetRelid(rel), true);
365  parent = relation_open(parentrelid, AccessShareLock);
366 
367  /* Get pg_class.relpartbound */
368  tuple = SearchSysCache1(RELOID,
370  if (!HeapTupleIsValid(tuple))
371  elog(ERROR, "cache lookup failed for relation %u",
372  RelationGetRelid(rel));
373 
374  boundDatum = SysCacheGetAttr(RELOID, tuple,
375  Anum_pg_class_relpartbound,
376  &isnull);
377  if (!isnull)
378  {
379  PartitionBoundSpec *bound;
380 
382  stringToNode(TextDatumGetCString(boundDatum)));
383 
384  my_qual = get_qual_from_partbound(parent, bound);
385  }
386 
387  ReleaseSysCache(tuple);
388 
389  /* Add the parent's quals to the list (if any) */
390  if (parent->rd_rel->relispartition)
391  result = list_concat(generate_partition_qual(parent), my_qual);
392  else
393  result = my_qual;
394 
395  /*
396  * Change Vars to have partition's attnos instead of the parent's. We do
397  * this after we concatenate the parent's quals, because we want every Var
398  * in it to bear this relation's attnos. It's safe to assume varno = 1
399  * here.
400  */
401  result = map_partition_varattnos(result, 1, rel, parent);
402 
403  /* Assert that we're not leaking any old data during assignments below */
404  Assert(rel->rd_partcheckcxt == NULL);
405  Assert(rel->rd_partcheck == NIL);
406 
407  /*
408  * Save a copy in the relcache. The order of these operations is fairly
409  * critical to avoid memory leaks and ensure that we don't leave a corrupt
410  * relcache entry if we fail partway through copyObject.
411  *
412  * If, as is definitely possible, the partcheck list is NIL, then we do
413  * not need to make a context to hold it.
414  */
415  if (result != NIL)
416  {
418  "partition constraint",
422  oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt);
423  rel->rd_partcheck = copyObject(result);
424  MemoryContextSwitchTo(oldcxt);
425  }
426  else
427  rel->rd_partcheck = NIL;
428  rel->rd_partcheckvalid = true;
429 
430  /* Keep the parent locked until commit */
431  relation_close(parent, NoLock);
432 
433  /* Return the working copy to the caller */
434  return result;
435 }
int16 AttrNumber
Definition: attnum.h:21
#define TextDatumGetCString(d)
Definition: builtins.h:95
#define NameStr(name)
Definition: c.h:735
signed short int16
Definition: c.h:482
signed int int32
Definition: c.h:483
#define unlikely(x)
Definition: c.h:300
#define OidIsValid(objectId)
Definition: c.h:764
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2171
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define HASHEXTENDED_PROC
Definition: hash.h:356
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
bool get_rel_relispartition(Oid relid)
Definition: lsyscache.c:2031
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2253
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:795
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:372
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:546
void pfree(void *pointer)
Definition: mcxt.c:1456
void * palloc0(Size size)
Definition: mcxt.c:1257
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1064
MemoryContext CurTransactionContext
Definition: mcxt.c:147
MemoryContext CacheMemoryContext
Definition: mcxt.c:144
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
#define MemoryContextCopyAndSetIdentifier(cxt, id)
Definition: memutils.h:101
#define BTORDER_PROC
Definition: nbtree.h:707
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:282
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:786
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1748
#define copyObject(obj)
Definition: nodes.h:244
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:868
@ PARTITION_STRATEGY_LIST
Definition: parsenodes.h:866
@ PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:867
List * get_qual_from_partbound(Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:250
Expr * get_partition_qual_relid(Oid relid)
Definition: partcache.c:302
static List * generate_partition_qual(Relation rel)
Definition: partcache.c:340
static void RelationBuildPartitionKey(Relation relation)
Definition: partcache.c:81
List * RelationGetPartitionQual(Relation rel)
Definition: partcache.c:280
PartitionKey RelationGetPartitionKey(Relation rel)
Definition: partcache.c:54
struct PartitionKeyData * PartitionKey
Definition: partdefs.h:18
List * map_partition_varattnos(List *expr, int fromrel_varno, Relation to_rel, Relation from_rel)
Definition: partition.c:221
Oid get_partition_parent(Oid relid, bool even_if_detached)
Definition: partition.c:54
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
#define linitial(l)
Definition: pg_list.h:178
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
FormData_pg_partitioned_table * Form_pg_partitioned_table
void check_stack_depth(void)
Definition: postgres.c:3523
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
unsigned int Oid
Definition: postgres_ext.h:31
@ AND_EXPR
Definition: primnodes.h:858
void * stringToNode(const char *str)
Definition: read.c:90
#define RelationGetRelid(relation)
Definition: rel.h:504
#define RelationGetRelationName(relation)
Definition: rel.h:538
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
Definition: fmgr.h:57
Definition: pg_list.h:54
Definition: nodes.h:129
List * rd_partcheck
Definition: rel.h:147
bool rd_partcheckvalid
Definition: rel.h:148
MemoryContext rd_partkeycxt
Definition: rel.h:127
MemoryContext rd_partcheckcxt
Definition: rel.h:149
TupleDesc rd_att
Definition: rel.h:112
PartitionKey rd_partkey
Definition: rel.h:126
Form_pg_class rd_rel
Definition: rel.h:111
Definition: c.h:715
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:722
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:868
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:820
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1081
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:1112
@ RELOID
Definition: syscache.h:89
@ CLAOID
Definition: syscache.h:48
@ PARTRELID
Definition: syscache.h:77
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92