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

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().

337 {
338  HeapTuple tuple;
339  MemoryContext oldcxt;
340  Datum boundDatum;
341  bool isnull;
342  List *my_qual = NIL,
343  *result = NIL;
344  Relation parent;
345 
346  /* Guard against stack overflow due to overly deep partition tree */
348 
349  /* If we already cached the result, just return a copy */
350  if (rel->rd_partcheckvalid)
351  return copyObject(rel->rd_partcheck);
352 
353  /* Grab at least an AccessShareLock on the parent table */
356 
357  /* Get pg_class.relpartbound */
358  tuple = SearchSysCache1(RELOID, RelationGetRelid(rel));
359  if (!HeapTupleIsValid(tuple))
360  elog(ERROR, "cache lookup failed for relation %u",
361  RelationGetRelid(rel));
362 
363  boundDatum = SysCacheGetAttr(RELOID, tuple,
364  Anum_pg_class_relpartbound,
365  &isnull);
366  if (!isnull)
367  {
368  PartitionBoundSpec *bound;
369 
371  stringToNode(TextDatumGetCString(boundDatum)));
372 
373  my_qual = get_qual_from_partbound(rel, parent, bound);
374  }
375 
376  ReleaseSysCache(tuple);
377 
378  /* Add the parent's quals to the list (if any) */
379  if (parent->rd_rel->relispartition)
380  result = list_concat(generate_partition_qual(parent), my_qual);
381  else
382  result = my_qual;
383 
384  /*
385  * Change Vars to have partition's attnos instead of the parent's. We do
386  * this after we concatenate the parent's quals, because we want every Var
387  * in it to bear this relation's attnos. It's safe to assume varno = 1
388  * here.
389  */
390  result = map_partition_varattnos(result, 1, rel, parent);
391 
392  /* Assert that we're not leaking any old data during assignments below */
393  Assert(rel->rd_partcheckcxt == NULL);
394  Assert(rel->rd_partcheck == NIL);
395 
396  /*
397  * Save a copy in the relcache. The order of these operations is fairly
398  * critical to avoid memory leaks and ensure that we don't leave a corrupt
399  * relcache entry if we fail partway through copyObject.
400  *
401  * If, as is definitely possible, the partcheck list is NIL, then we do
402  * not need to make a context to hold it.
403  */
404  if (result != NIL)
405  {
407  "partition constraint",
411  oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt);
412  rel->rd_partcheck = copyObject(result);
413  MemoryContextSwitchTo(oldcxt);
414  }
415  else
416  rel->rd_partcheck = NIL;
417  rel->rd_partcheckvalid = true;
418 
419  /* Keep the parent locked until commit */
420  relation_close(parent, NoLock);
421 
422  /* Return the working copy to the caller */
423  return result;
424 }
#define NIL
Definition: pg_list.h:65
#define AllocSetContextCreate
Definition: memutils.h:170
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
List * get_qual_from_partbound(Relation rel, Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:117
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
bool rd_partcheckvalid
Definition: rel.h:108
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
void * stringToNode(const char *str)
Definition: read.c:89
Form_pg_class rd_rel
Definition: rel.h:84
static List * generate_partition_qual(Relation rel)
Definition: partcache.c:336
#define ERROR
Definition: elog.h:43
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
Oid get_partition_parent(Oid relid)
Definition: partition.c:50
#define MemoryContextCopyAndSetIdentifier(cxt, id)
Definition: memutils.h:97
#define NoLock
Definition: lockdefs.h:34
void check_stack_depth(void)
Definition: postgres.c:3288
#define RelationGetRelationName(relation)
Definition: rel.h:462
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
#define TextDatumGetCString(d)
Definition: builtins.h:88
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define Assert(condition)
Definition: c.h:738
#define elog(elevel,...)
Definition: elog.h:228
MemoryContext rd_partcheckcxt
Definition: rel.h:109
#define copyObject(obj)
Definition: nodes.h:641
Definition: pg_list.h:50
List * map_partition_varattnos(List *expr, int fromrel_varno, Relation to_rel, Relation from_rel)
Definition: partition.c:198
#define RelationGetRelid(relation)
Definition: rel.h:428
List * rd_partcheck
Definition: rel.h:107
MemoryContext CacheMemoryContext
Definition: mcxt.c:47

◆ get_partition_qual_relid()

Expr* get_partition_qual_relid ( Oid  relid)

Definition at line 298 of file partcache.c.

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().

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

◆ RelationBuildPartitionKey()

static void RelationBuildPartitionKey ( Relation  relation)
static

Definition at line 81 of file partcache.c.

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(), PartitionKeyData::partattrs, PartitionKeyData::partcollation, PartitionKeyData::partexprs, PARTITION_STRATEGY_HASH, PartitionKeyData::partnatts, PartitionKeyData::partopcintype, PartitionKeyData::partopfamily, PARTRELID, PartitionKeyData::partsupfunc, PartitionKeyData::parttypalign, PartitionKeyData::parttypbyval, PartitionKeyData::parttypcoll, PartitionKeyData::parttypid, PartitionKeyData::parttyplen, PartitionKeyData::parttypmod, pfree(), RelationData::rd_att, RelationData::rd_partkey, RelationData::rd_partkeycxt, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), SearchSysCache1(), PartitionKeyData::strategy, stringToNode(), SysCacheGetAttr(), TextDatumGetCString, TupleDescAttr, and oidvector::values.

Referenced by RelationGetPartitionKey().

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  /*
119  * We can rely on the first variable-length attribute being mapped to the
120  * relevant field of the catalog's C struct, because all previous
121  * attributes are non-nullable and fixed-length.
122  */
123  attrs = form->partattrs.values;
124 
125  /* But use the hard way to retrieve further variable-length attributes */
126  /* Operator class */
127  datum = SysCacheGetAttr(PARTRELID, tuple,
128  Anum_pg_partitioned_table_partclass, &isnull);
129  Assert(!isnull);
130  opclass = (oidvector *) DatumGetPointer(datum);
131 
132  /* Collation */
133  datum = SysCacheGetAttr(PARTRELID, tuple,
134  Anum_pg_partitioned_table_partcollation, &isnull);
135  Assert(!isnull);
136  collation = (oidvector *) DatumGetPointer(datum);
137 
138  /* Expressions */
139  datum = SysCacheGetAttr(PARTRELID, tuple,
140  Anum_pg_partitioned_table_partexprs, &isnull);
141  if (!isnull)
142  {
143  char *exprString;
144  Node *expr;
145 
146  exprString = TextDatumGetCString(datum);
147  expr = stringToNode(exprString);
148  pfree(exprString);
149 
150  /*
151  * Run the expressions through const-simplification since the planner
152  * will be comparing them to similarly-processed qual clause operands,
153  * and may fail to detect valid matches without this step; fix
154  * opfuncids while at it. We don't need to bother with
155  * canonicalize_qual() though, because partition expressions should be
156  * in canonical form already (ie, no need for OR-merging or constant
157  * elimination).
158  */
159  expr = eval_const_expressions(NULL, expr);
160  fix_opfuncids(expr);
161 
162  oldcxt = MemoryContextSwitchTo(partkeycxt);
163  key->partexprs = (List *) copyObject(expr);
164  MemoryContextSwitchTo(oldcxt);
165  }
166 
167  /* Allocate assorted arrays in the partkeycxt, which we'll fill below */
168  oldcxt = MemoryContextSwitchTo(partkeycxt);
169  key->partattrs = (AttrNumber *) palloc0(key->partnatts * sizeof(AttrNumber));
170  key->partopfamily = (Oid *) palloc0(key->partnatts * sizeof(Oid));
171  key->partopcintype = (Oid *) palloc0(key->partnatts * sizeof(Oid));
172  key->partsupfunc = (FmgrInfo *) palloc0(key->partnatts * sizeof(FmgrInfo));
173 
174  key->partcollation = (Oid *) palloc0(key->partnatts * sizeof(Oid));
175  key->parttypid = (Oid *) palloc0(key->partnatts * sizeof(Oid));
176  key->parttypmod = (int32 *) palloc0(key->partnatts * sizeof(int32));
177  key->parttyplen = (int16 *) palloc0(key->partnatts * sizeof(int16));
178  key->parttypbyval = (bool *) palloc0(key->partnatts * sizeof(bool));
179  key->parttypalign = (char *) palloc0(key->partnatts * sizeof(char));
180  key->parttypcoll = (Oid *) palloc0(key->partnatts * sizeof(Oid));
181  MemoryContextSwitchTo(oldcxt);
182 
183  /* determine support function number to search for */
184  procnum = (key->strategy == PARTITION_STRATEGY_HASH) ?
186 
187  /* Copy partattrs and fill other per-attribute info */
188  memcpy(key->partattrs, attrs, key->partnatts * sizeof(int16));
189  partexprs_item = list_head(key->partexprs);
190  for (i = 0; i < key->partnatts; i++)
191  {
192  AttrNumber attno = key->partattrs[i];
193  HeapTuple opclasstup;
194  Form_pg_opclass opclassform;
195  Oid funcid;
196 
197  /* Collect opfamily information */
198  opclasstup = SearchSysCache1(CLAOID,
199  ObjectIdGetDatum(opclass->values[i]));
200  if (!HeapTupleIsValid(opclasstup))
201  elog(ERROR, "cache lookup failed for opclass %u", opclass->values[i]);
202 
203  opclassform = (Form_pg_opclass) GETSTRUCT(opclasstup);
204  key->partopfamily[i] = opclassform->opcfamily;
205  key->partopcintype[i] = opclassform->opcintype;
206 
207  /* Get a support function for the specified opfamily and datatypes */
208  funcid = get_opfamily_proc(opclassform->opcfamily,
209  opclassform->opcintype,
210  opclassform->opcintype,
211  procnum);
212  if (!OidIsValid(funcid))
213  ereport(ERROR,
214  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
215  errmsg("operator class \"%s\" of access method %s is missing support function %d for type %s",
216  NameStr(opclassform->opcname),
218  "hash" : "btree",
219  procnum,
220  format_type_be(opclassform->opcintype))));
221 
222  fmgr_info_cxt(funcid, &key->partsupfunc[i], partkeycxt);
223 
224  /* Collation */
225  key->partcollation[i] = collation->values[i];
226 
227  /* Collect type information */
228  if (attno != 0)
229  {
230  Form_pg_attribute att = TupleDescAttr(relation->rd_att, attno - 1);
231 
232  key->parttypid[i] = att->atttypid;
233  key->parttypmod[i] = att->atttypmod;
234  key->parttypcoll[i] = att->attcollation;
235  }
236  else
237  {
238  if (partexprs_item == NULL)
239  elog(ERROR, "wrong number of partition key expressions");
240 
241  key->parttypid[i] = exprType(lfirst(partexprs_item));
242  key->parttypmod[i] = exprTypmod(lfirst(partexprs_item));
243  key->parttypcoll[i] = exprCollation(lfirst(partexprs_item));
244 
245  partexprs_item = lnext(key->partexprs, partexprs_item);
246  }
248  &key->parttyplen[i],
249  &key->parttypbyval[i],
250  &key->parttypalign[i]);
251 
252  ReleaseSysCache(opclasstup);
253  }
254 
255  ReleaseSysCache(tuple);
256 
257  /* Assert that we're not leaking any old data during assignments below */
258  Assert(relation->rd_partkeycxt == NULL);
259  Assert(relation->rd_partkey == NULL);
260 
261  /*
262  * Success --- reparent our context and make the relcache point to the
263  * newly constructed key
264  */
266  relation->rd_partkeycxt = partkeycxt;
267  relation->rd_partkey = key;
268 }
signed short int16
Definition: c.h:354
Definition: c.h:594
Definition: fmgr.h:56
#define AllocSetContextCreate
Definition: memutils.h:170
#define BTORDER_PROC
Definition: nbtree.h:392
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
Oid * partopfamily
Definition: partcache.h:33
FmgrInfo * partsupfunc
Definition: partcache.h:35
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2049
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:354
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1587
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:608
void * stringToNode(const char *str)
Definition: read.c:89
#define HASHEXTENDED_PROC
Definition: hash.h:354
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2252
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
List * partexprs
Definition: partcache.h:30
unsigned int Oid
Definition: postgres_ext.h:31
FormData_pg_partitioned_table * Form_pg_partitioned_table
#define OidIsValid(objectId)
Definition: c.h:644
signed int int32
Definition: c.h:355
PartitionKey rd_partkey
Definition: rel.h:99
void pfree(void *pointer)
Definition: mcxt.c:1056
Oid * parttypcoll
Definition: partcache.h:46
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define MemoryContextCopyAndSetIdentifier(cxt, id)
Definition: memutils.h:97
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:602
#define RelationGetRelationName(relation)
Definition: rel.h:462
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
Oid * partcollation
Definition: partcache.h:38
#define TextDatumGetCString(d)
Definition: builtins.h:88
void * palloc0(Size size)
Definition: mcxt.c:980
AttrNumber * partattrs
Definition: partcache.h:28
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
char * parttypalign
Definition: partcache.h:45
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
TupleDesc rd_att
Definition: rel.h:85
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:798
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
int32 * parttypmod
Definition: partcache.h:42
bool * parttypbyval
Definition: partcache.h:44
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
int16 * parttyplen
Definition: partcache.h:43
MemoryContext rd_partkeycxt
Definition: rel.h:100
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
#define DatumGetPointer(X)
Definition: postgres.h:549
int errmsg(const char *fmt,...)
Definition: elog.c:822
Oid * partopcintype
Definition: partcache.h:34
#define elog(elevel,...)
Definition: elog.h:228
int i
#define NameStr(name)
Definition: c.h:615
#define copyObject(obj)
Definition: nodes.h:641
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:428
struct PartitionKeyData * PartitionKey
Definition: partdefs.h:18
MemoryContext CacheMemoryContext
Definition: mcxt.c:47

◆ RelationGetPartitionKey()

◆ RelationGetPartitionQual()

List* RelationGetPartitionQual ( Relation  rel)

Definition at line 276 of file partcache.c.

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

Referenced by ATExecAttachPartition(), InitResultRelInfo(), and set_baserel_partition_constraint().

277 {
278  /* Quick exit */
279  if (!rel->rd_rel->relispartition)
280  return NIL;
281 
282  return generate_partition_qual(rel);
283 }
#define NIL
Definition: pg_list.h:65
Form_pg_class rd_rel
Definition: rel.h:84
static List * generate_partition_qual(Relation rel)
Definition: partcache.c:336