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_inherits.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 "rewrite/rewriteHandler.h"
#include "utils/builtins.h"
#include "utils/datum.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 342 of file partcache.c.

343 {
344  HeapTuple tuple;
345  MemoryContext oldcxt;
346  Datum boundDatum;
347  bool isnull;
348  List *my_qual = NIL,
349  *result = NIL;
350  Oid parentrelid;
351  Relation parent;
352 
353  /* Guard against stack overflow due to overly deep partition tree */
355 
356  /* If we already cached the result, just return a copy */
357  if (rel->rd_partcheckvalid)
358  return copyObject(rel->rd_partcheck);
359 
360  /*
361  * Grab at least an AccessShareLock on the parent table. Must do this
362  * even if the partition has been partially detached, because transactions
363  * concurrent with the detach might still be trying to use a partition
364  * descriptor that includes it.
365  */
366  parentrelid = get_partition_parent(RelationGetRelid(rel), true);
367  parent = relation_open(parentrelid, AccessShareLock);
368 
369  /* Get pg_class.relpartbound */
370  tuple = SearchSysCache1(RELOID, RelationGetRelid(rel));
371  if (!HeapTupleIsValid(tuple))
372  elog(ERROR, "cache lookup failed for relation %u",
373  RelationGetRelid(rel));
374 
375  boundDatum = SysCacheGetAttr(RELOID, tuple,
376  Anum_pg_class_relpartbound,
377  &isnull);
378  if (!isnull)
379  {
380  PartitionBoundSpec *bound;
381 
383  stringToNode(TextDatumGetCString(boundDatum)));
384 
385  my_qual = get_qual_from_partbound(parent, bound);
386  }
387 
388  ReleaseSysCache(tuple);
389 
390  /* Add the parent's quals to the list (if any) */
391  if (parent->rd_rel->relispartition)
392  result = list_concat(generate_partition_qual(parent), my_qual);
393  else
394  result = my_qual;
395 
396  /*
397  * Change Vars to have partition's attnos instead of the parent's. We do
398  * this after we concatenate the parent's quals, because we want every Var
399  * in it to bear this relation's attnos. It's safe to assume varno = 1
400  * here.
401  */
402  result = map_partition_varattnos(result, 1, rel, parent);
403 
404  /* Assert that we're not leaking any old data during assignments below */
405  Assert(rel->rd_partcheckcxt == NULL);
406  Assert(rel->rd_partcheck == NIL);
407 
408  /*
409  * Save a copy in the relcache. The order of these operations is fairly
410  * critical to avoid memory leaks and ensure that we don't leave a corrupt
411  * relcache entry if we fail partway through copyObject.
412  *
413  * If, as is definitely possible, the partcheck list is NIL, then we do
414  * not need to make a context to hold it.
415  */
416  if (result != NIL)
417  {
419  "partition constraint",
423  oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt);
424  rel->rd_partcheck = copyObject(result);
425  MemoryContextSwitchTo(oldcxt);
426  }
427  else
428  rel->rd_partcheck = NIL;
429  rel->rd_partcheckvalid = true;
430 
431  /* Keep the parent locked until commit */
432  relation_close(parent, NoLock);
433 
434  /* Return the working copy to the caller */
435  return result;
436 }
#define TextDatumGetCString(d)
Definition: builtins.h:86
#define ERROR
Definition: elog.h:35
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
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
MemoryContext CacheMemoryContext
Definition: mcxt.c:133
#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:233
#define castNode(_type_, nodeptr)
Definition: nodes.h:186
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
List * get_qual_from_partbound(Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:250
static List * generate_partition_qual(Relation rel)
Definition: partcache.c:342
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
#define NIL
Definition: pg_list.h:66
void check_stack_depth(void)
Definition: postgres.c:3440
uintptr_t Datum
Definition: postgres.h:412
unsigned int Oid
Definition: postgres_ext.h:31
void * stringToNode(const char *str)
Definition: read.c:90
#define RelationGetRelid(relation)
Definition: rel.h:501
#define RelationGetRelationName(relation)
Definition: rel.h:535
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
Definition: pg_list.h:52
List * rd_partcheck
Definition: rel.h:146
bool rd_partcheckvalid
Definition: rel.h:147
MemoryContext rd_partcheckcxt
Definition: rel.h:148
Form_pg_class rd_rel
Definition: rel.h:110
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
@ RELOID
Definition: syscache.h:89

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, RelationData::rd_partcheck, RelationData::rd_partcheckcxt, RelationData::rd_partcheckvalid, RelationData::rd_rel, relation_close(), relation_open(), RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), RELOID, 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 304 of file partcache.c.

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

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 81 of file partcache.c.

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 = SysCacheGetAttr(PARTRELID, tuple,
134  Anum_pg_partitioned_table_partclass, &isnull);
135  Assert(!isnull);
136  opclass = (oidvector *) DatumGetPointer(datum);
137 
138  /* Collation */
139  datum = SysCacheGetAttr(PARTRELID, tuple,
140  Anum_pg_partitioned_table_partcollation, &isnull);
141  Assert(!isnull);
142  collation = (oidvector *) DatumGetPointer(datum);
143 
144  /* Expressions */
145  datum = SysCacheGetAttr(PARTRELID, tuple,
146  Anum_pg_partitioned_table_partexprs, &isnull);
147  if (!isnull)
148  {
149  char *exprString;
150  Node *expr;
151 
152  exprString = TextDatumGetCString(datum);
153  expr = stringToNode(exprString);
154  pfree(exprString);
155 
156  /*
157  * Run the expressions through const-simplification since the planner
158  * will be comparing them to similarly-processed qual clause operands,
159  * and may fail to detect valid matches without this step; fix
160  * opfuncids while at it. We don't need to bother with
161  * canonicalize_qual() though, because partition expressions should be
162  * in canonical form already (ie, no need for OR-merging or constant
163  * elimination).
164  */
165  expr = eval_const_expressions(NULL, expr);
166  fix_opfuncids(expr);
167 
168  oldcxt = MemoryContextSwitchTo(partkeycxt);
169  key->partexprs = (List *) copyObject(expr);
170  MemoryContextSwitchTo(oldcxt);
171  }
172 
173  /* Allocate assorted arrays in the partkeycxt, which we'll fill below */
174  oldcxt = MemoryContextSwitchTo(partkeycxt);
175  key->partattrs = (AttrNumber *) palloc0(key->partnatts * sizeof(AttrNumber));
176  key->partopfamily = (Oid *) palloc0(key->partnatts * sizeof(Oid));
177  key->partopcintype = (Oid *) palloc0(key->partnatts * sizeof(Oid));
178  key->partsupfunc = (FmgrInfo *) palloc0(key->partnatts * sizeof(FmgrInfo));
179 
180  key->partcollation = (Oid *) palloc0(key->partnatts * sizeof(Oid));
181  key->parttypid = (Oid *) palloc0(key->partnatts * sizeof(Oid));
182  key->parttypmod = (int32 *) palloc0(key->partnatts * sizeof(int32));
183  key->parttyplen = (int16 *) palloc0(key->partnatts * sizeof(int16));
184  key->parttypbyval = (bool *) palloc0(key->partnatts * sizeof(bool));
185  key->parttypalign = (char *) palloc0(key->partnatts * sizeof(char));
186  key->parttypcoll = (Oid *) palloc0(key->partnatts * sizeof(Oid));
187  MemoryContextSwitchTo(oldcxt);
188 
189  /* determine support function number to search for */
190  procnum = (key->strategy == PARTITION_STRATEGY_HASH) ?
192 
193  /* Copy partattrs and fill other per-attribute info */
194  memcpy(key->partattrs, attrs, key->partnatts * sizeof(int16));
195  partexprs_item = list_head(key->partexprs);
196  for (i = 0; i < key->partnatts; i++)
197  {
198  AttrNumber attno = key->partattrs[i];
199  HeapTuple opclasstup;
200  Form_pg_opclass opclassform;
201  Oid funcid;
202 
203  /* Collect opfamily information */
204  opclasstup = SearchSysCache1(CLAOID,
205  ObjectIdGetDatum(opclass->values[i]));
206  if (!HeapTupleIsValid(opclasstup))
207  elog(ERROR, "cache lookup failed for opclass %u", opclass->values[i]);
208 
209  opclassform = (Form_pg_opclass) GETSTRUCT(opclasstup);
210  key->partopfamily[i] = opclassform->opcfamily;
211  key->partopcintype[i] = opclassform->opcintype;
212 
213  /* Get a support function for the specified opfamily and datatypes */
214  funcid = get_opfamily_proc(opclassform->opcfamily,
215  opclassform->opcintype,
216  opclassform->opcintype,
217  procnum);
218  if (!OidIsValid(funcid))
219  ereport(ERROR,
220  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
221  errmsg("operator class \"%s\" of access method %s is missing support function %d for type %s",
222  NameStr(opclassform->opcname),
223  (key->strategy == PARTITION_STRATEGY_HASH) ?
224  "hash" : "btree",
225  procnum,
226  format_type_be(opclassform->opcintype))));
227 
228  fmgr_info_cxt(funcid, &key->partsupfunc[i], partkeycxt);
229 
230  /* Collation */
231  key->partcollation[i] = collation->values[i];
232 
233  /* Collect type information */
234  if (attno != 0)
235  {
236  Form_pg_attribute att = TupleDescAttr(relation->rd_att, attno - 1);
237 
238  key->parttypid[i] = att->atttypid;
239  key->parttypmod[i] = att->atttypmod;
240  key->parttypcoll[i] = att->attcollation;
241  }
242  else
243  {
244  if (partexprs_item == NULL)
245  elog(ERROR, "wrong number of partition key expressions");
246 
247  key->parttypid[i] = exprType(lfirst(partexprs_item));
248  key->parttypmod[i] = exprTypmod(lfirst(partexprs_item));
249  key->parttypcoll[i] = exprCollation(lfirst(partexprs_item));
250 
251  partexprs_item = lnext(key->partexprs, partexprs_item);
252  }
253  get_typlenbyvalalign(key->parttypid[i],
254  &key->parttyplen[i],
255  &key->parttypbyval[i],
256  &key->parttypalign[i]);
257 
258  ReleaseSysCache(opclasstup);
259  }
260 
261  ReleaseSysCache(tuple);
262 
263  /* Assert that we're not leaking any old data during assignments below */
264  Assert(relation->rd_partkeycxt == NULL);
265  Assert(relation->rd_partkey == NULL);
266 
267  /*
268  * Success --- reparent our context and make the relcache point to the
269  * newly constructed key
270  */
272  relation->rd_partkeycxt = partkeycxt;
273  relation->rd_partkey = key;
274 }
int16 AttrNumber
Definition: attnum.h:21
#define NameStr(name)
Definition: c.h:682
signed short int16
Definition: c.h:429
signed int int32
Definition: c.h:430
#define OidIsValid(objectId)
Definition: c.h:711
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2132
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ereport(elevel,...)
Definition: elog.h:145
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
#define HASHEXTENDED_PROC
Definition: hash.h:356
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
int i
Definition: isn.c:73
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2229
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:795
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:519
void pfree(void *pointer)
Definition: mcxt.c:1306
void * palloc0(Size size)
Definition: mcxt.c:1230
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1037
MemoryContext CurTransactionContext
Definition: mcxt.c:136
#define BTORDER_PROC
Definition: nbtree.h:701
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:266
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:764
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1641
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:835
@ PARTITION_STRATEGY_LIST
Definition: parsenodes.h:833
@ PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:834
struct PartitionKeyData * PartitionKey
Definition: partdefs.h:18
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
#define lfirst(lc)
Definition: pg_list.h:170
static ListCell * list_head(const List *l)
Definition: pg_list.h:126
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:341
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
FormData_pg_partitioned_table * Form_pg_partitioned_table
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:660
Definition: fmgr.h:57
Definition: nodes.h:118
MemoryContext rd_partkeycxt
Definition: rel.h:126
TupleDesc rd_att
Definition: rel.h:111
PartitionKey rd_partkey
Definition: rel.h:125
Definition: c.h:662
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:669
@ CLAOID
Definition: syscache.h:48
@ PARTRELID
Definition: syscache.h:77
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, Assert(), BTORDER_PROC, CacheMemoryContext, CLAOID, 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, PARTRELID, pfree(), RelationData::rd_att, RelationData::rd_partkey, RelationData::rd_partkeycxt, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), SearchSysCache1(), stringToNode(), SysCacheGetAttr(), TextDatumGetCString, TupleDescAttr, and oidvector::values.

Referenced by RelationGetPartitionKey().

◆ RelationGetPartitionKey()

◆ RelationGetPartitionQual()

List* RelationGetPartitionQual ( Relation  rel)

Definition at line 282 of file partcache.c.

283 {
284  /* Quick exit */
285  if (!rel->rd_rel->relispartition)
286  return NIL;
287 
288  return generate_partition_qual(rel);
289 }

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

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