PostgreSQL Source Code  git master
partcache.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "access/htup_details.h"
#include "access/nbtree.h"
#include "access/relation.h"
#include "catalog/partition.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_partitioned_table.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "partitioning/partbounds.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/partcache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for partcache.c:

Go to the source code of this file.

Functions

static void RelationBuildPartitionKey (Relation relation)
 
static Listgenerate_partition_qual (Relation rel)
 
PartitionKey RelationGetPartitionKey (Relation rel)
 
ListRelationGetPartitionQual (Relation rel)
 
Exprget_partition_qual_relid (Oid relid)
 

Function Documentation

◆ generate_partition_qual()

static List * generate_partition_qual ( Relation  rel)
static

Definition at line 337 of file partcache.c.

338 {
339  HeapTuple tuple;
340  MemoryContext oldcxt;
341  Datum boundDatum;
342  bool isnull;
343  List *my_qual = NIL,
344  *result = NIL;
345  Oid parentrelid;
346  Relation parent;
347 
348  /* Guard against stack overflow due to overly deep partition tree */
350 
351  /* If we already cached the result, just return a copy */
352  if (rel->rd_partcheckvalid)
353  return copyObject(rel->rd_partcheck);
354 
355  /*
356  * Grab at least an AccessShareLock on the parent table. Must do this
357  * even if the partition has been partially detached, because transactions
358  * concurrent with the detach might still be trying to use a partition
359  * descriptor that includes it.
360  */
361  parentrelid = get_partition_parent(RelationGetRelid(rel), true);
362  parent = relation_open(parentrelid, AccessShareLock);
363 
364  /* Get pg_class.relpartbound */
365  tuple = SearchSysCache1(RELOID,
367  if (!HeapTupleIsValid(tuple))
368  elog(ERROR, "cache lookup failed for relation %u",
369  RelationGetRelid(rel));
370 
371  boundDatum = SysCacheGetAttr(RELOID, tuple,
372  Anum_pg_class_relpartbound,
373  &isnull);
374  if (!isnull)
375  {
376  PartitionBoundSpec *bound;
377 
379  stringToNode(TextDatumGetCString(boundDatum)));
380 
381  my_qual = get_qual_from_partbound(parent, bound);
382  }
383 
384  ReleaseSysCache(tuple);
385 
386  /* Add the parent's quals to the list (if any) */
387  if (parent->rd_rel->relispartition)
388  result = list_concat(generate_partition_qual(parent), my_qual);
389  else
390  result = my_qual;
391 
392  /*
393  * Change Vars to have partition's attnos instead of the parent's. We do
394  * this after we concatenate the parent's quals, because we want every Var
395  * in it to bear this relation's attnos. It's safe to assume varno = 1
396  * here.
397  */
398  result = map_partition_varattnos(result, 1, rel, parent);
399 
400  /* Assert that we're not leaking any old data during assignments below */
401  Assert(rel->rd_partcheckcxt == NULL);
402  Assert(rel->rd_partcheck == NIL);
403 
404  /*
405  * Save a copy in the relcache. The order of these operations is fairly
406  * critical to avoid memory leaks and ensure that we don't leave a corrupt
407  * relcache entry if we fail partway through copyObject.
408  *
409  * If, as is definitely possible, the partcheck list is NIL, then we do
410  * not need to make a context to hold it.
411  */
412  if (result != NIL)
413  {
415  "partition constraint",
419  oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt);
420  rel->rd_partcheck = copyObject(result);
421  MemoryContextSwitchTo(oldcxt);
422  }
423  else
424  rel->rd_partcheck = NIL;
425  rel->rd_partcheckvalid = true;
426 
427  /* Keep the parent locked until commit */
428  relation_close(parent, NoLock);
429 
430  /* Return the working copy to the caller */
431  return result;
432 }
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
MemoryContext CacheMemoryContext
Definition: mcxt.c:140
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
#define MemoryContextCopyAndSetIdentifier(cxt, id)
Definition: memutils.h:101
#define copyObject(obj)
Definition: nodes.h:223
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
List * get_qual_from_partbound(Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:249
static List * generate_partition_qual(Relation rel)
Definition: partcache.c:337
List * map_partition_varattnos(List *expr, int fromrel_varno, Relation to_rel, Relation from_rel)
Definition: partition.c:222
Oid get_partition_parent(Oid relid, bool even_if_detached)
Definition: partition.c:53
#define NIL
Definition: pg_list.h:68
void check_stack_depth(void)
Definition: postgres.c:3531
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:31
void * stringToNode(const char *str)
Definition: read.c:90
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetRelationName(relation)
Definition: rel.h:539
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
Definition: pg_list.h:54
List * rd_partcheck
Definition: rel.h:147
bool rd_partcheckvalid
Definition: rel.h:148
MemoryContext rd_partcheckcxt
Definition: rel.h:149
Form_pg_class rd_rel
Definition: rel.h:111
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:218
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:479

References AccessShareLock, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, Assert(), CacheMemoryContext, castNode, check_stack_depth(), copyObject, elog, ERROR, get_partition_parent(), get_qual_from_partbound(), HeapTupleIsValid, list_concat(), map_partition_varattnos(), MemoryContextCopyAndSetIdentifier, MemoryContextSwitchTo(), NIL, NoLock, ObjectIdGetDatum(), RelationData::rd_partcheck, RelationData::rd_partcheckcxt, RelationData::rd_partcheckvalid, RelationData::rd_rel, relation_close(), relation_open(), RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), SearchSysCache1(), stringToNode(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by get_partition_qual_relid(), and RelationGetPartitionQual().

◆ get_partition_qual_relid()

Expr* get_partition_qual_relid ( Oid  relid)

Definition at line 299 of file partcache.c.

300 {
301  Expr *result = NULL;
302 
303  /* Do the work only if this relation exists and is a partition. */
304  if (get_rel_relispartition(relid))
305  {
306  Relation rel = relation_open(relid, AccessShareLock);
307  List *and_args;
308 
309  and_args = generate_partition_qual(rel);
310 
311  /* Convert implicit-AND list format to boolean expression */
312  if (and_args == NIL)
313  result = NULL;
314  else if (list_length(and_args) > 1)
315  result = makeBoolExpr(AND_EXPR, and_args, -1);
316  else
317  result = linitial(and_args);
318 
319  /* Keep the lock, to allow safe deparsing against the rel by caller. */
320  relation_close(rel, NoLock);
321  }
322 
323  return result;
324 }
bool get_rel_relispartition(Oid relid)
Definition: lsyscache.c:2005
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:371
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
@ AND_EXPR
Definition: primnodes.h:887

References AccessShareLock, AND_EXPR, generate_partition_qual(), get_rel_relispartition(), linitial, list_length(), makeBoolExpr(), NIL, NoLock, relation_close(), and relation_open().

Referenced by pg_get_partconstrdef_string(), and pg_get_partition_constraintdef().

◆ RelationBuildPartitionKey()

static void RelationBuildPartitionKey ( Relation  relation)
static

Definition at line 78 of file partcache.c.

79 {
81  HeapTuple tuple;
82  bool isnull;
83  int i;
85  AttrNumber *attrs;
86  oidvector *opclass;
87  oidvector *collation;
88  ListCell *partexprs_item;
89  Datum datum;
90  MemoryContext partkeycxt,
91  oldcxt;
92  int16 procnum;
93 
94  tuple = SearchSysCache1(PARTRELID,
96 
97  if (!HeapTupleIsValid(tuple))
98  elog(ERROR, "cache lookup failed for partition key of relation %u",
99  RelationGetRelid(relation));
100 
102  "partition key",
105  RelationGetRelationName(relation));
106 
107  key = (PartitionKey) MemoryContextAllocZero(partkeycxt,
108  sizeof(PartitionKeyData));
109 
110  /* Fixed-length attributes */
111  form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
112  key->strategy = form->partstrat;
113  key->partnatts = form->partnatts;
114 
115  /* Validate partition strategy code */
116  if (key->strategy != PARTITION_STRATEGY_LIST &&
117  key->strategy != PARTITION_STRATEGY_RANGE &&
118  key->strategy != PARTITION_STRATEGY_HASH)
119  elog(ERROR, "invalid partition strategy \"%c\"", key->strategy);
120 
121  /*
122  * We can rely on the first variable-length attribute being mapped to the
123  * relevant field of the catalog's C struct, because all previous
124  * attributes are non-nullable and fixed-length.
125  */
126  attrs = form->partattrs.values;
127 
128  /* But use the hard way to retrieve further variable-length attributes */
129  /* Operator class */
130  datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
131  Anum_pg_partitioned_table_partclass);
132  opclass = (oidvector *) DatumGetPointer(datum);
133 
134  /* Collation */
135  datum = SysCacheGetAttrNotNull(PARTRELID, tuple,
136  Anum_pg_partitioned_table_partcollation);
137  collation = (oidvector *) DatumGetPointer(datum);
138 
139  /* Expressions */
140  datum = SysCacheGetAttr(PARTRELID, tuple,
141  Anum_pg_partitioned_table_partexprs, &isnull);
142  if (!isnull)
143  {
144  char *exprString;
145  Node *expr;
146 
147  exprString = TextDatumGetCString(datum);
148  expr = stringToNode(exprString);
149  pfree(exprString);
150 
151  /*
152  * Run the expressions through const-simplification since the planner
153  * will be comparing them to similarly-processed qual clause operands,
154  * and may fail to detect valid matches without this step; fix
155  * opfuncids while at it. We don't need to bother with
156  * canonicalize_qual() though, because partition expressions should be
157  * in canonical form already (ie, no need for OR-merging or constant
158  * elimination).
159  */
160  expr = eval_const_expressions(NULL, expr);
161  fix_opfuncids(expr);
162 
163  oldcxt = MemoryContextSwitchTo(partkeycxt);
164  key->partexprs = (List *) copyObject(expr);
165  MemoryContextSwitchTo(oldcxt);
166  }
167 
168  /* Allocate assorted arrays in the partkeycxt, which we'll fill below */
169  oldcxt = MemoryContextSwitchTo(partkeycxt);
170  key->partattrs = (AttrNumber *) palloc0(key->partnatts * sizeof(AttrNumber));
171  key->partopfamily = (Oid *) palloc0(key->partnatts * sizeof(Oid));
172  key->partopcintype = (Oid *) palloc0(key->partnatts * sizeof(Oid));
173  key->partsupfunc = (FmgrInfo *) palloc0(key->partnatts * sizeof(FmgrInfo));
174 
175  key->partcollation = (Oid *) palloc0(key->partnatts * sizeof(Oid));
176  key->parttypid = (Oid *) palloc0(key->partnatts * sizeof(Oid));
177  key->parttypmod = (int32 *) palloc0(key->partnatts * sizeof(int32));
178  key->parttyplen = (int16 *) palloc0(key->partnatts * sizeof(int16));
179  key->parttypbyval = (bool *) palloc0(key->partnatts * sizeof(bool));
180  key->parttypalign = (char *) palloc0(key->partnatts * sizeof(char));
181  key->parttypcoll = (Oid *) palloc0(key->partnatts * sizeof(Oid));
182  MemoryContextSwitchTo(oldcxt);
183 
184  /* determine support function number to search for */
185  procnum = (key->strategy == PARTITION_STRATEGY_HASH) ?
187 
188  /* Copy partattrs and fill other per-attribute info */
189  memcpy(key->partattrs, attrs, key->partnatts * sizeof(int16));
190  partexprs_item = list_head(key->partexprs);
191  for (i = 0; i < key->partnatts; i++)
192  {
193  AttrNumber attno = key->partattrs[i];
194  HeapTuple opclasstup;
195  Form_pg_opclass opclassform;
196  Oid funcid;
197 
198  /* Collect opfamily information */
199  opclasstup = SearchSysCache1(CLAOID,
200  ObjectIdGetDatum(opclass->values[i]));
201  if (!HeapTupleIsValid(opclasstup))
202  elog(ERROR, "cache lookup failed for opclass %u", opclass->values[i]);
203 
204  opclassform = (Form_pg_opclass) GETSTRUCT(opclasstup);
205  key->partopfamily[i] = opclassform->opcfamily;
206  key->partopcintype[i] = opclassform->opcintype;
207 
208  /* Get a support function for the specified opfamily and datatypes */
209  funcid = get_opfamily_proc(opclassform->opcfamily,
210  opclassform->opcintype,
211  opclassform->opcintype,
212  procnum);
213  if (!OidIsValid(funcid))
214  ereport(ERROR,
215  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
216  errmsg("operator class \"%s\" of access method %s is missing support function %d for type %s",
217  NameStr(opclassform->opcname),
218  (key->strategy == PARTITION_STRATEGY_HASH) ?
219  "hash" : "btree",
220  procnum,
221  format_type_be(opclassform->opcintype))));
222 
223  fmgr_info_cxt(funcid, &key->partsupfunc[i], partkeycxt);
224 
225  /* Collation */
226  key->partcollation[i] = collation->values[i];
227 
228  /* Collect type information */
229  if (attno != 0)
230  {
231  Form_pg_attribute att = TupleDescAttr(relation->rd_att, attno - 1);
232 
233  key->parttypid[i] = att->atttypid;
234  key->parttypmod[i] = att->atttypmod;
235  key->parttypcoll[i] = att->attcollation;
236  }
237  else
238  {
239  if (partexprs_item == NULL)
240  elog(ERROR, "wrong number of partition key expressions");
241 
242  key->parttypid[i] = exprType(lfirst(partexprs_item));
243  key->parttypmod[i] = exprTypmod(lfirst(partexprs_item));
244  key->parttypcoll[i] = exprCollation(lfirst(partexprs_item));
245 
246  partexprs_item = lnext(key->partexprs, partexprs_item);
247  }
248  get_typlenbyvalalign(key->parttypid[i],
249  &key->parttyplen[i],
250  &key->parttypbyval[i],
251  &key->parttypalign[i]);
252 
253  ReleaseSysCache(opclasstup);
254  }
255 
256  ReleaseSysCache(tuple);
257 
258  /* Assert that we're not leaking any old data during assignments below */
259  Assert(relation->rd_partkeycxt == NULL);
260  Assert(relation->rd_partkey == NULL);
261 
262  /*
263  * Success --- reparent our context and make the relcache point to the
264  * newly constructed key
265  */
267  relation->rd_partkeycxt = partkeycxt;
268  relation->rd_partkey = key;
269 }
int16 AttrNumber
Definition: attnum.h:21
#define NameStr(name)
Definition: c.h:733
signed short int16
Definition: c.h:480
signed int int32
Definition: c.h:481
#define OidIsValid(objectId)
Definition: c.h:762
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2234
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#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 GETSTRUCT(TUP)
Definition: htup_details.h:653
int i
Definition: isn.c:73
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2227
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:796
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:625
void pfree(void *pointer)
Definition: mcxt.c:1508
void * palloc0(Size size)
Definition: mcxt.c:1334
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1202
MemoryContext CurTransactionContext
Definition: mcxt.c:143
#define BTORDER_PROC
Definition: nbtree.h:707
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:284
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:788
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1759
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:870
@ PARTITION_STRATEGY_LIST
Definition: parsenodes.h:868
@ PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:869
struct PartitionKeyData * PartitionKey
Definition: partdefs.h:18
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define lfirst(lc)
Definition: pg_list.h:172
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
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
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
Definition: fmgr.h:57
Definition: nodes.h:129
MemoryContext rd_partkeycxt
Definition: rel.h:127
TupleDesc rd_att
Definition: rel.h:112
PartitionKey rd_partkey
Definition: rel.h:126
Definition: c.h:713
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:720
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:510
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, Assert(), BTORDER_PROC, CacheMemoryContext, copyObject, CurTransactionContext, DatumGetPointer(), elog, ereport, errcode(), errmsg(), ERROR, eval_const_expressions(), exprCollation(), exprType(), exprTypmod(), fix_opfuncids(), fmgr_info_cxt(), format_type_be(), get_opfamily_proc(), get_typlenbyvalalign(), GETSTRUCT, HASHEXTENDED_PROC, HeapTupleIsValid, i, sort-test::key, lfirst, list_head(), lnext(), MemoryContextAllocZero(), MemoryContextCopyAndSetIdentifier, MemoryContextSetParent(), MemoryContextSwitchTo(), NameStr, ObjectIdGetDatum(), OidIsValid, palloc0(), PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, pfree(), RelationData::rd_att, RelationData::rd_partkey, RelationData::rd_partkeycxt, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), SearchSysCache1(), stringToNode(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), TextDatumGetCString, TupleDescAttr, and oidvector::values.

Referenced by RelationGetPartitionKey().

◆ RelationGetPartitionKey()

◆ RelationGetPartitionQual()

List* RelationGetPartitionQual ( Relation  rel)

Definition at line 277 of file partcache.c.

278 {
279  /* Quick exit */
280  if (!rel->rd_rel->relispartition)
281  return NIL;
282 
283  return generate_partition_qual(rel);
284 }

References generate_partition_qual(), NIL, and RelationData::rd_rel.

Referenced by ATExecAttachPartition(), DetachAddConstraintIfNeeded(), ExecPartitionCheck(), and set_baserel_partition_constraint().