PostgreSQL Source Code  git master
partition.h File Reference
Include dependency graph for partition.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define HASH_PARTITION_SEED   UINT64CONST(0x7A5B22367996DCFD)
 

Functions

Oid get_partition_parent (Oid relid)
 
Listget_partition_ancestors (Oid relid)
 
Oid index_get_partition (Relation partition, Oid indexId)
 
Listmap_partition_varattnos (List *expr, int fromrel_varno, Relation to_rel, Relation from_rel, bool *found_whole_row)
 
bool has_partition_attrs (Relation rel, Bitmapset *attnums, bool *used_in_expr)
 
Oid get_default_partition_oid (Oid parentId)
 
void update_default_partition_oid (Oid parentId, Oid defaultPartId)
 
Listget_proposed_default_constraint (List *new_part_constraints)
 

Macro Definition Documentation

◆ HASH_PARTITION_SEED

#define HASH_PARTITION_SEED   UINT64CONST(0x7A5B22367996DCFD)

Definition at line 20 of file partition.h.

Referenced by compute_partition_hash_value(), and satisfies_hash_partition().

Function Documentation

◆ get_default_partition_oid()

Oid get_default_partition_oid ( Oid  parentId)

Definition at line 299 of file partition.c.

References GETSTRUCT, HeapTupleIsValid, InvalidOid, ObjectIdGetDatum, PARTRELID, ReleaseSysCache(), and SearchSysCache1().

Referenced by heap_drop_with_catalog(), and RelationBuildPartitionDesc().

300 {
301  HeapTuple tuple;
302  Oid defaultPartId = InvalidOid;
303 
304  tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(parentId));
305 
306  if (HeapTupleIsValid(tuple))
307  {
308  Form_pg_partitioned_table part_table_form;
309 
310  part_table_form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
311  defaultPartId = part_table_form->partdefid;
312  ReleaseSysCache(tuple);
313  }
314 
315  return defaultPartId;
316 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
unsigned int Oid
Definition: postgres_ext.h:31
FormData_pg_partitioned_table * Form_pg_partitioned_table
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

◆ get_partition_ancestors()

List* get_partition_ancestors ( Oid  relid)

Definition at line 116 of file partition.c.

References AccessShareLock, get_partition_ancestors_worker(), NIL, table_close(), and table_open().

Referenced by ExecInitPartitionInfo(), index_concurrently_swap(), pg_partition_ancestors(), pg_partition_root(), and pg_partition_tree().

117 {
118  List *result = NIL;
119  Relation inhRel;
120 
121  inhRel = table_open(InheritsRelationId, AccessShareLock);
122 
123  get_partition_ancestors_worker(inhRel, relid, &result);
124 
125  table_close(inhRel, AccessShareLock);
126 
127  return result;
128 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define AccessShareLock
Definition: lockdefs.h:36
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
static void get_partition_ancestors_worker(Relation inhRel, Oid relid, List **ancestors)
Definition: partition.c:135

◆ get_partition_parent()

Oid get_partition_parent ( Oid  relid)

Definition at line 51 of file partition.c.

References AccessShareLock, elog, ERROR, get_partition_parent_worker(), OidIsValid, table_close(), and table_open().

Referenced by ATExecAttachPartitionIdx(), ATExecDetachPartition(), ATExecDropNotNull(), generate_partition_qual(), heap_drop_with_catalog(), index_get_partition(), RangeVarCallbackForDropRelation(), and validatePartitionedIndex().

52 {
53  Relation catalogRelation;
54  Oid result;
55 
56  catalogRelation = table_open(InheritsRelationId, AccessShareLock);
57 
58  result = get_partition_parent_worker(catalogRelation, relid);
59 
60  if (!OidIsValid(result))
61  elog(ERROR, "could not find tuple for parent of relation %u", relid);
62 
63  table_close(catalogRelation, AccessShareLock);
64 
65  return result;
66 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static Oid get_partition_parent_worker(Relation inhRel, Oid relid)
Definition: partition.c:74
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:226
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ get_proposed_default_constraint()

List* get_proposed_default_constraint ( List new_part_constraints)

Definition at line 354 of file partition.c.

References canonicalize_qual(), eval_const_expressions(), list_make1, make_ands_explicit(), make_ands_implicit(), makeBoolExpr(), and NOT_EXPR.

Referenced by ATExecAttachPartition(), and check_default_partition_contents().

355 {
356  Expr *defPartConstraint;
357 
358  defPartConstraint = make_ands_explicit(new_part_constraints);
359 
360  /*
361  * Derive the partition constraints of default partition by negating the
362  * given partition constraints. The partition constraint never evaluates
363  * to NULL, so negating it like this is safe.
364  */
365  defPartConstraint = makeBoolExpr(NOT_EXPR,
366  list_make1(defPartConstraint),
367  -1);
368 
369  /* Simplify, to put the negated expression into canonical form */
370  defPartConstraint =
371  (Expr *) eval_const_expressions(NULL,
372  (Node *) defPartConstraint);
373  defPartConstraint = canonicalize_qual(defPartConstraint, true);
374 
375  return make_ands_implicit(defPartConstraint);
376 }
Definition: nodes.h:525
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2253
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:367
#define list_make1(x1)
Definition: pg_list.h:227
Expr * canonicalize_qual(Expr *qual, bool is_check)
Definition: prepqual.c:292
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:705
List * make_ands_implicit(Expr *clause)
Definition: makefuncs.c:716

◆ has_partition_attrs()

bool has_partition_attrs ( Relation  rel,
Bitmapset attnums,
bool used_in_expr 
)

Definition at line 239 of file partition.c.

References bms_is_member(), bms_overlap(), FirstLowInvalidHeapAttributeNumber, get_partition_col_attnum(), get_partition_exprs(), get_partition_natts(), i, sort-test::key, lfirst, list_head(), lnext(), pull_varattnos(), RelationData::rd_rel, and RelationGetPartitionKey.

Referenced by ATExecDropColumn(), ATPrepAlterColumnType(), and expand_partitioned_rtentry().

240 {
242  int partnatts;
243  List *partexprs;
244  ListCell *partexprs_item;
245  int i;
246 
247  if (attnums == NULL || rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
248  return false;
249 
250  key = RelationGetPartitionKey(rel);
251  partnatts = get_partition_natts(key);
252  partexprs = get_partition_exprs(key);
253 
254  partexprs_item = list_head(partexprs);
255  for (i = 0; i < partnatts; i++)
256  {
257  AttrNumber partattno = get_partition_col_attnum(key, i);
258 
259  if (partattno != 0)
260  {
262  attnums))
263  {
264  if (used_in_expr)
265  *used_in_expr = false;
266  return true;
267  }
268  }
269  else
270  {
271  /* Arbitrary expression */
272  Node *expr = (Node *) lfirst(partexprs_item);
273  Bitmapset *expr_attrs = NULL;
274 
275  /* Find all attributes referenced */
276  pull_varattnos(expr, 1, &expr_attrs);
277  partexprs_item = lnext(partexprs, partexprs_item);
278 
279  if (bms_overlap(attnums, expr_attrs))
280  {
281  if (used_in_expr)
282  *used_in_expr = true;
283  return true;
284  }
285  }
286  }
287 
288  return false;
289 }
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
Definition: nodes.h:525
static int get_partition_natts(PartitionKey key)
Definition: partcache.h:63
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
Form_pg_class rd_rel
Definition: rel.h:83
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
static List * get_partition_exprs(PartitionKey key)
Definition: partcache.h:69
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
static int16 get_partition_col_attnum(PartitionKey key, int col)
Definition: partcache.h:78
#define lfirst(lc)
Definition: pg_list.h:190
#define RelationGetPartitionKey(relation)
Definition: rel.h:600
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
int16 AttrNumber
Definition: attnum.h:21

◆ index_get_partition()

Oid index_get_partition ( Relation  partition,
Oid  indexId 
)

Definition at line 154 of file partition.c.

References elog, ERROR, get_partition_parent(), GETSTRUCT, HeapTupleIsValid, InvalidOid, lfirst_oid, list_free(), ObjectIdGetDatum, RelationGetIndexList(), ReleaseSysCache(), RELOID, and SearchSysCache1().

Referenced by addFkRecurseReferenced(), CloneFkReferenced(), and refuseDupeIndexAttach().

155 {
156  List *idxlist = RelationGetIndexList(partition);
157  ListCell *l;
158 
159  foreach(l, idxlist)
160  {
161  Oid partIdx = lfirst_oid(l);
162  HeapTuple tup;
163  Form_pg_class classForm;
164  bool ispartition;
165 
166  tup = SearchSysCache1(RELOID, ObjectIdGetDatum(partIdx));
167  if (!HeapTupleIsValid(tup))
168  elog(ERROR, "cache lookup failed for relation %u", partIdx);
169  classForm = (Form_pg_class) GETSTRUCT(tup);
170  ispartition = classForm->relispartition;
171  ReleaseSysCache(tup);
172  if (!ispartition)
173  continue;
174  if (get_partition_parent(lfirst_oid(l)) == indexId)
175  {
176  list_free(idxlist);
177  return partIdx;
178  }
179  }
180 
181  return InvalidOid;
182 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
Oid get_partition_parent(Oid relid)
Definition: partition.c:51
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4348
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
void list_free(List *list)
Definition: list.c:1377
#define elog(elevel,...)
Definition: elog.h:226
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ map_partition_varattnos()

List* map_partition_varattnos ( List expr,
int  fromrel_varno,
Relation  to_rel,
Relation  from_rel,
bool found_whole_row 
)

Definition at line 202 of file partition.c.

References convert_tuples_by_name_map(), map_variable_attnos(), NIL, RelationGetDescr, and RelationGetForm.

Referenced by ATExecAttachPartition(), check_default_partition_contents(), CloneRowTriggersToPartition(), CreateTrigger(), generate_partition_qual(), and QueuePartitionConstraintValidation().

205 {
206  bool my_found_whole_row = false;
207 
208  if (expr != NIL)
209  {
210  AttrNumber *part_attnos;
211 
212  part_attnos = convert_tuples_by_name_map(RelationGetDescr(to_rel),
213  RelationGetDescr(from_rel));
214  expr = (List *) map_variable_attnos((Node *) expr,
215  fromrel_varno, 0,
216  part_attnos,
217  RelationGetDescr(from_rel)->natts,
218  RelationGetForm(to_rel)->reltype,
219  &my_found_whole_row);
220  }
221 
222  if (found_whole_row)
223  *found_whole_row = my_found_whole_row;
224 
225  return expr;
226 }
#define NIL
Definition: pg_list.h:65
#define RelationGetDescr(relation)
Definition: rel.h:445
#define RelationGetForm(relation)
Definition: rel.h:413
Definition: nodes.h:525
Node * map_variable_attnos(Node *node, int target_varno, int sublevels_up, const AttrNumber *attno_map, int map_length, Oid to_rowtype, bool *found_whole_row)
AttrNumber * convert_tuples_by_name_map(TupleDesc indesc, TupleDesc outdesc)
Definition: tupconvert.c:245
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21

◆ update_default_partition_oid()

void update_default_partition_oid ( Oid  parentId,
Oid  defaultPartId 
)

Definition at line 324 of file partition.c.

References CatalogTupleUpdate(), elog, ERROR, GETSTRUCT, heap_freetuple(), HeapTupleIsValid, ObjectIdGetDatum, PARTRELID, RowExclusiveLock, SearchSysCacheCopy1, HeapTupleData::t_self, table_close(), and table_open().

Referenced by ATExecDetachPartition(), heap_drop_with_catalog(), and StorePartitionBound().

325 {
326  HeapTuple tuple;
327  Relation pg_partitioned_table;
328  Form_pg_partitioned_table part_table_form;
329 
330  pg_partitioned_table = table_open(PartitionedRelationId, RowExclusiveLock);
331 
332  tuple = SearchSysCacheCopy1(PARTRELID, ObjectIdGetDatum(parentId));
333 
334  if (!HeapTupleIsValid(tuple))
335  elog(ERROR, "cache lookup failed for partition key of relation %u",
336  parentId);
337 
338  part_table_form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
339  part_table_form->partdefid = defaultPartId;
340  CatalogTupleUpdate(pg_partitioned_table, &tuple->t_self, tuple);
341 
342  heap_freetuple(tuple);
343  table_close(pg_partitioned_table, RowExclusiveLock);
344 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
FormData_pg_partitioned_table * Form_pg_partitioned_table
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:224
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
#define elog(elevel,...)
Definition: elog.h:226
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39