PostgreSQL Source Code  git master
partcache.h File Reference
#include "access/attnum.h"
#include "fmgr.h"
#include "nodes/pg_list.h"
#include "nodes/primnodes.h"
#include "partitioning/partdefs.h"
#include "utils/relcache.h"
Include dependency graph for partcache.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PartitionKeyData
 

Typedefs

typedef struct PartitionKeyData PartitionKeyData
 

Functions

void RelationBuildPartitionKey (Relation relation)
 
ListRelationGetPartitionQual (Relation rel)
 
Exprget_partition_qual_relid (Oid relid)
 
static int get_partition_strategy (PartitionKey key)
 
static int get_partition_natts (PartitionKey key)
 
static Listget_partition_exprs (PartitionKey key)
 
static int16 get_partition_col_attnum (PartitionKey key, int col)
 
static Oid get_partition_col_typid (PartitionKey key, int col)
 
static int32 get_partition_col_typmod (PartitionKey key, int col)
 
static Oid get_partition_col_collation (PartitionKey key, int col)
 

Typedef Documentation

◆ PartitionKeyData

Function Documentation

◆ get_partition_col_attnum()

static int16 get_partition_col_attnum ( PartitionKey  key,
int  col 
)
inlinestatic

Definition at line 78 of file partcache.h.

References PartitionKeyData::partattrs.

Referenced by ExecBuildSlotPartitionKeyDescription(), and has_partition_attrs().

79 {
80  return key->partattrs[col];
81 }
AttrNumber * partattrs
Definition: partcache.h:28

◆ get_partition_col_collation()

static Oid get_partition_col_collation ( PartitionKey  key,
int  col 
)
inlinestatic

Definition at line 96 of file partcache.h.

References PartitionKeyData::partcollation.

Referenced by transformPartitionBound(), and transformPartitionRangeBounds().

97 {
98  return key->partcollation[col];
99 }
Oid * partcollation
Definition: partcache.h:38

◆ get_partition_col_typid()

static Oid get_partition_col_typid ( PartitionKey  key,
int  col 
)
inlinestatic

Definition at line 84 of file partcache.h.

References PartitionKeyData::parttypid.

Referenced by ExecBuildSlotPartitionKeyDescription(), transformPartitionBound(), and transformPartitionRangeBounds().

85 {
86  return key->parttypid[col];
87 }

◆ get_partition_col_typmod()

static int32 get_partition_col_typmod ( PartitionKey  key,
int  col 
)
inlinestatic

Definition at line 90 of file partcache.h.

References PartitionKeyData::parttypmod.

Referenced by transformPartitionBound(), and transformPartitionRangeBounds().

91 {
92  return key->parttypmod[col];
93 }
int32 * parttypmod
Definition: partcache.h:42

◆ get_partition_exprs()

static List* get_partition_exprs ( PartitionKey  key)
inlinestatic

Definition at line 69 of file partcache.h.

References PartitionKeyData::partexprs.

Referenced by has_partition_attrs(), transformPartitionBound(), and transformPartitionRangeBounds().

70 {
71  return key->partexprs;
72 }
List * partexprs
Definition: partcache.h:30

◆ get_partition_natts()

static int get_partition_natts ( PartitionKey  key)
inlinestatic

Definition at line 63 of file partcache.h.

References PartitionKeyData::partnatts.

Referenced by ExecBuildSlotPartitionKeyDescription(), has_partition_attrs(), and transformPartitionBound().

64 {
65  return key->partnatts;
66 }

◆ 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

◆ get_partition_strategy()

static int get_partition_strategy ( PartitionKey  key)
inlinestatic

Definition at line 57 of file partcache.h.

References PartitionKeyData::strategy.

Referenced by transformPartitionBound().

58 {
59  return key->strategy;
60 }

◆ 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