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 Listgenerate_partition_qual (Relation rel)
 
void RelationBuildPartitionKey (Relation relation)
 
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 316 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().

317 {
318  HeapTuple tuple;
319  MemoryContext oldcxt;
320  Datum boundDatum;
321  bool isnull;
322  List *my_qual = NIL,
323  *result = NIL;
324  Relation parent;
325  bool found_whole_row;
326 
327  /* Guard against stack overflow due to overly deep partition tree */
329 
330  /* If we already cached the result, just return a copy */
331  if (rel->rd_partcheckvalid)
332  return copyObject(rel->rd_partcheck);
333 
334  /* Grab at least an AccessShareLock on the parent table */
337 
338  /* Get pg_class.relpartbound */
339  tuple = SearchSysCache1(RELOID, RelationGetRelid(rel));
340  if (!HeapTupleIsValid(tuple))
341  elog(ERROR, "cache lookup failed for relation %u",
342  RelationGetRelid(rel));
343 
344  boundDatum = SysCacheGetAttr(RELOID, tuple,
345  Anum_pg_class_relpartbound,
346  &isnull);
347  if (!isnull)
348  {
349  PartitionBoundSpec *bound;
350 
352  stringToNode(TextDatumGetCString(boundDatum)));
353 
354  my_qual = get_qual_from_partbound(rel, parent, bound);
355  }
356 
357  ReleaseSysCache(tuple);
358 
359  /* Add the parent's quals to the list (if any) */
360  if (parent->rd_rel->relispartition)
361  result = list_concat(generate_partition_qual(parent), my_qual);
362  else
363  result = my_qual;
364 
365  /*
366  * Change Vars to have partition's attnos instead of the parent's. We do
367  * this after we concatenate the parent's quals, because we want every Var
368  * in it to bear this relation's attnos. It's safe to assume varno = 1
369  * here.
370  */
371  result = map_partition_varattnos(result, 1, rel, parent,
372  &found_whole_row);
373  /* There can never be a whole-row reference here */
374  if (found_whole_row)
375  elog(ERROR, "unexpected whole-row reference found in partition key");
376 
377  /* Assert that we're not leaking any old data during assignments below */
378  Assert(rel->rd_partcheckcxt == NULL);
379  Assert(rel->rd_partcheck == NIL);
380 
381  /*
382  * Save a copy in the relcache. The order of these operations is fairly
383  * critical to avoid memory leaks and ensure that we don't leave a corrupt
384  * relcache entry if we fail partway through copyObject.
385  *
386  * If, as is definitely possible, the partcheck list is NIL, then we do
387  * not need to make a context to hold it.
388  */
389  if (result != NIL)
390  {
392  "partition constraint",
396  oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt);
397  rel->rd_partcheck = copyObject(result);
398  MemoryContextSwitchTo(oldcxt);
399  }
400  else
401  rel->rd_partcheck = NIL;
402  rel->rd_partcheckvalid = true;
403 
404  /* Keep the parent locked until commit */
405  relation_close(parent, NoLock);
406 
407  /* Return the working copy to the caller */
408  return result;
409 }
#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:118
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
bool rd_partcheckvalid
Definition: rel.h:102
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:83
static List * generate_partition_qual(Relation rel)
Definition: partcache.c:316
List * map_partition_varattnos(List *expr, int fromrel_varno, Relation to_rel, Relation from_rel, bool *found_whole_row)
Definition: partition.c:201
#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:3302
#define RelationGetRelationName(relation)
Definition: rel.h:456
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
#define TextDatumGetCString(d)
Definition: builtins.h:84
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:739
#define elog(elevel,...)
Definition: elog.h:228
MemoryContext rd_partcheckcxt
Definition: rel.h:103
#define copyObject(obj)
Definition: nodes.h:641
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:422
List * rd_partcheck
Definition: rel.h:101
MemoryContext CacheMemoryContext
Definition: mcxt.c:47

◆ get_partition_qual_relid()

Expr* get_partition_qual_relid ( Oid  relid)

Definition at line 278 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().

279 {
280  Expr *result = NULL;
281 
282  /* Do the work only if this relation exists and is a partition. */
283  if (get_rel_relispartition(relid))
284  {
285  Relation rel = relation_open(relid, AccessShareLock);
286  List *and_args;
287 
288  and_args = generate_partition_qual(rel);
289 
290  /* Convert implicit-AND list format to boolean expression */
291  if (and_args == NIL)
292  result = NULL;
293  else if (list_length(and_args) > 1)
294  result = makeBoolExpr(AND_EXPR, and_args, -1);
295  else
296  result = linitial(and_args);
297 
298  /* Keep the lock, to allow safe deparsing against the rel by caller. */
299  relation_close(rel, NoLock);
300  }
301 
302  return result;
303 }
#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:316
#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()

void RelationBuildPartitionKey ( Relation  relation)

Definition at line 58 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 RelationBuildDesc(), and RelationCacheInitializePhase3().

59 {
61  HeapTuple tuple;
62  bool isnull;
63  int i;
65  AttrNumber *attrs;
66  oidvector *opclass;
67  oidvector *collation;
68  ListCell *partexprs_item;
69  Datum datum;
70  MemoryContext partkeycxt,
71  oldcxt;
72  int16 procnum;
73 
74  tuple = SearchSysCache1(PARTRELID,
76 
77  /*
78  * The following happens when we have created our pg_class entry but not
79  * the pg_partitioned_table entry yet.
80  */
81  if (!HeapTupleIsValid(tuple))
82  return;
83 
85  "partition key",
88  RelationGetRelationName(relation));
89 
90  key = (PartitionKey) MemoryContextAllocZero(partkeycxt,
91  sizeof(PartitionKeyData));
92 
93  /* Fixed-length attributes */
94  form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
95  key->strategy = form->partstrat;
96  key->partnatts = form->partnatts;
97 
98  /*
99  * We can rely on the first variable-length attribute being mapped to the
100  * relevant field of the catalog's C struct, because all previous
101  * attributes are non-nullable and fixed-length.
102  */
103  attrs = form->partattrs.values;
104 
105  /* But use the hard way to retrieve further variable-length attributes */
106  /* Operator class */
107  datum = SysCacheGetAttr(PARTRELID, tuple,
108  Anum_pg_partitioned_table_partclass, &isnull);
109  Assert(!isnull);
110  opclass = (oidvector *) DatumGetPointer(datum);
111 
112  /* Collation */
113  datum = SysCacheGetAttr(PARTRELID, tuple,
114  Anum_pg_partitioned_table_partcollation, &isnull);
115  Assert(!isnull);
116  collation = (oidvector *) DatumGetPointer(datum);
117 
118  /* Expressions */
119  datum = SysCacheGetAttr(PARTRELID, tuple,
120  Anum_pg_partitioned_table_partexprs, &isnull);
121  if (!isnull)
122  {
123  char *exprString;
124  Node *expr;
125 
126  exprString = TextDatumGetCString(datum);
127  expr = stringToNode(exprString);
128  pfree(exprString);
129 
130  /*
131  * Run the expressions through const-simplification since the planner
132  * will be comparing them to similarly-processed qual clause operands,
133  * and may fail to detect valid matches without this step; fix
134  * opfuncids while at it. We don't need to bother with
135  * canonicalize_qual() though, because partition expressions should be
136  * in canonical form already (ie, no need for OR-merging or constant
137  * elimination).
138  */
139  expr = eval_const_expressions(NULL, expr);
140  fix_opfuncids(expr);
141 
142  oldcxt = MemoryContextSwitchTo(partkeycxt);
143  key->partexprs = (List *) copyObject(expr);
144  MemoryContextSwitchTo(oldcxt);
145  }
146 
147  /* Allocate assorted arrays in the partkeycxt, which we'll fill below */
148  oldcxt = MemoryContextSwitchTo(partkeycxt);
149  key->partattrs = (AttrNumber *) palloc0(key->partnatts * sizeof(AttrNumber));
150  key->partopfamily = (Oid *) palloc0(key->partnatts * sizeof(Oid));
151  key->partopcintype = (Oid *) palloc0(key->partnatts * sizeof(Oid));
152  key->partsupfunc = (FmgrInfo *) palloc0(key->partnatts * sizeof(FmgrInfo));
153 
154  key->partcollation = (Oid *) palloc0(key->partnatts * sizeof(Oid));
155  key->parttypid = (Oid *) palloc0(key->partnatts * sizeof(Oid));
156  key->parttypmod = (int32 *) palloc0(key->partnatts * sizeof(int32));
157  key->parttyplen = (int16 *) palloc0(key->partnatts * sizeof(int16));
158  key->parttypbyval = (bool *) palloc0(key->partnatts * sizeof(bool));
159  key->parttypalign = (char *) palloc0(key->partnatts * sizeof(char));
160  key->parttypcoll = (Oid *) palloc0(key->partnatts * sizeof(Oid));
161  MemoryContextSwitchTo(oldcxt);
162 
163  /* determine support function number to search for */
164  procnum = (key->strategy == PARTITION_STRATEGY_HASH) ?
166 
167  /* Copy partattrs and fill other per-attribute info */
168  memcpy(key->partattrs, attrs, key->partnatts * sizeof(int16));
169  partexprs_item = list_head(key->partexprs);
170  for (i = 0; i < key->partnatts; i++)
171  {
172  AttrNumber attno = key->partattrs[i];
173  HeapTuple opclasstup;
174  Form_pg_opclass opclassform;
175  Oid funcid;
176 
177  /* Collect opfamily information */
178  opclasstup = SearchSysCache1(CLAOID,
179  ObjectIdGetDatum(opclass->values[i]));
180  if (!HeapTupleIsValid(opclasstup))
181  elog(ERROR, "cache lookup failed for opclass %u", opclass->values[i]);
182 
183  opclassform = (Form_pg_opclass) GETSTRUCT(opclasstup);
184  key->partopfamily[i] = opclassform->opcfamily;
185  key->partopcintype[i] = opclassform->opcintype;
186 
187  /* Get a support function for the specified opfamily and datatypes */
188  funcid = get_opfamily_proc(opclassform->opcfamily,
189  opclassform->opcintype,
190  opclassform->opcintype,
191  procnum);
192  if (!OidIsValid(funcid))
193  ereport(ERROR,
194  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
195  errmsg("operator class \"%s\" of access method %s is missing support function %d for type %s",
196  NameStr(opclassform->opcname),
198  "hash" : "btree",
199  procnum,
200  format_type_be(opclassform->opcintype))));
201 
202  fmgr_info_cxt(funcid, &key->partsupfunc[i], partkeycxt);
203 
204  /* Collation */
205  key->partcollation[i] = collation->values[i];
206 
207  /* Collect type information */
208  if (attno != 0)
209  {
210  Form_pg_attribute att = TupleDescAttr(relation->rd_att, attno - 1);
211 
212  key->parttypid[i] = att->atttypid;
213  key->parttypmod[i] = att->atttypmod;
214  key->parttypcoll[i] = att->attcollation;
215  }
216  else
217  {
218  if (partexprs_item == NULL)
219  elog(ERROR, "wrong number of partition key expressions");
220 
221  key->parttypid[i] = exprType(lfirst(partexprs_item));
222  key->parttypmod[i] = exprTypmod(lfirst(partexprs_item));
223  key->parttypcoll[i] = exprCollation(lfirst(partexprs_item));
224 
225  partexprs_item = lnext(key->partexprs, partexprs_item);
226  }
228  &key->parttyplen[i],
229  &key->parttypbyval[i],
230  &key->parttypalign[i]);
231 
232  ReleaseSysCache(opclasstup);
233  }
234 
235  ReleaseSysCache(tuple);
236 
237  /* Assert that we're not leaking any old data during assignments below */
238  Assert(relation->rd_partkeycxt == NULL);
239  Assert(relation->rd_partkey == NULL);
240 
241  /*
242  * Success --- reparent our context and make the relcache point to the
243  * newly constructed key
244  */
246  relation->rd_partkeycxt = partkeycxt;
247  relation->rd_partkey = key;
248 }
signed short int16
Definition: c.h:346
Definition: c.h:595
Definition: fmgr.h:56
#define AllocSetContextCreate
Definition: memutils.h:170
#define BTORDER_PROC
Definition: nbtree.h:393
#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:2253
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:645
signed int int32
Definition: c.h:347
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
struct PartitionKeyData * rd_partkey
Definition: rel.h:97
#define MemoryContextCopyAndSetIdentifier(cxt, id)
Definition: memutils.h:97
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:603
#define RelationGetRelationName(relation)
Definition: rel.h:456
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:84
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:84
#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:739
#define lfirst(lc)
Definition: pg_list.h:190
int16 * parttyplen
Definition: partcache.h:43
MemoryContext rd_partkeycxt
Definition: rel.h:98
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:616
#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:422
struct PartitionKeyData * PartitionKey
Definition: partdefs.h:18
MemoryContext CacheMemoryContext
Definition: mcxt.c:47

◆ RelationGetPartitionQual()

List* RelationGetPartitionQual ( Relation  rel)

Definition at line 256 of file partcache.c.

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

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

257 {
258  /* Quick exit */
259  if (!rel->rd_rel->relispartition)
260  return NIL;
261 
262  return generate_partition_qual(rel);
263 }
#define NIL
Definition: pg_list.h:65
Form_pg_class rd_rel
Definition: rel.h:83
static List * generate_partition_qual(Relation rel)
Definition: partcache.c:316