PostgreSQL Source Code  git master
partition.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/tupconvert.h"
#include "access/sysattr.h"
#include "catalog/indexing.h"
#include "catalog/partition.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_partitioned_table.h"
#include "nodes/makefuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/prep.h"
#include "optimizer/var.h"
#include "partitioning/partbounds.h"
#include "rewrite/rewriteManip.h"
#include "utils/fmgroids.h"
#include "utils/partcache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for partition.c:

Go to the source code of this file.

Functions

static Oid get_partition_parent_worker (Relation inhRel, Oid relid)
 
static void get_partition_ancestors_worker (Relation inhRel, Oid relid, List **ancestors)
 
Oid get_partition_parent (Oid relid)
 
Listget_partition_ancestors (Oid relid)
 
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_oid_from_partdesc (PartitionDesc partdesc)
 
Oid get_default_partition_oid (Oid parentId)
 
void update_default_partition_oid (Oid parentId, Oid defaultPartId)
 
Listget_proposed_default_constraint (List *new_part_constraints)
 

Function Documentation

◆ get_default_oid_from_partdesc()

Oid get_default_oid_from_partdesc ( PartitionDesc  partdesc)

Definition at line 265 of file partition.c.

References PartitionDescData::boundinfo, PartitionBoundInfoData::default_index, InvalidOid, PartitionDescData::oids, and partition_bound_has_default.

Referenced by ATExecAttachPartition(), ATExecDetachPartition(), DefineRelation(), and StorePartitionBound().

266 {
267  if (partdesc && partdesc->boundinfo &&
269  return partdesc->oids[partdesc->boundinfo->default_index];
270 
271  return InvalidOid;
272 }
PartitionBoundInfo boundinfo
Definition: partition.h:30
#define partition_bound_has_default(bi)
Definition: partbounds.h:76
#define InvalidOid
Definition: postgres_ext.h:36

◆ get_default_partition_oid()

Oid get_default_partition_oid ( Oid  parentId)

Definition at line 282 of file partition.c.

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

Referenced by heap_drop_with_catalog(), and RelationBuildPartitionDesc().

283 {
284  HeapTuple tuple;
285  Oid defaultPartId = InvalidOid;
286 
287  tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(parentId));
288 
289  if (HeapTupleIsValid(tuple))
290  {
291  Form_pg_partitioned_table part_table_form;
292 
293  part_table_form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
294  defaultPartId = part_table_form->partdefid;
295  ReleaseSysCache(tuple);
296  }
297 
298  return defaultPartId;
299 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:673
unsigned int Oid
Definition: postgres_ext.h:31
FormData_pg_partitioned_table * Form_pg_partitioned_table
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#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 118 of file partition.c.

References AccessShareLock, get_partition_ancestors_worker(), heap_close, heap_open(), and NIL.

Referenced by ExecInitPartitionInfo().

119 {
120  List *result = NIL;
121  Relation inhRel;
122 
123  inhRel = heap_open(InheritsRelationId, AccessShareLock);
124 
125  get_partition_ancestors_worker(inhRel, relid, &result);
126 
127  heap_close(inhRel, AccessShareLock);
128 
129  return result;
130 }
#define NIL
Definition: pg_list.h:69
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
Definition: pg_list.h:45
static void get_partition_ancestors_worker(Relation inhRel, Oid relid, List **ancestors)
Definition: partition.c:137

◆ get_partition_ancestors_worker()

static void get_partition_ancestors_worker ( Relation  inhRel,
Oid  relid,
List **  ancestors 
)
static

Definition at line 137 of file partition.c.

References get_partition_parent_worker(), InvalidOid, and lappend_oid().

Referenced by get_partition_ancestors().

138 {
139  Oid parentOid;
140 
141  /* Recursion ends at the topmost level, ie., when there's no parent */
142  parentOid = get_partition_parent_worker(inhRel, relid);
143  if (parentOid == InvalidOid)
144  return;
145 
146  *ancestors = lappend_oid(*ancestors, parentOid);
147  get_partition_ancestors_worker(inhRel, parentOid, ancestors);
148 }
static Oid get_partition_parent_worker(Relation inhRel, Oid relid)
Definition: partition.c:76
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define InvalidOid
Definition: postgres_ext.h:36
static void get_partition_ancestors_worker(Relation inhRel, Oid relid, List **ancestors)
Definition: partition.c:137

◆ get_partition_parent()

Oid get_partition_parent ( Oid  relid)

Definition at line 53 of file partition.c.

References AccessShareLock, elog, ERROR, get_partition_parent_worker(), heap_close, heap_open(), and OidIsValid.

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

54 {
55  Relation catalogRelation;
56  Oid result;
57 
58  catalogRelation = heap_open(InheritsRelationId, AccessShareLock);
59 
60  result = get_partition_parent_worker(catalogRelation, relid);
61 
62  if (!OidIsValid(result))
63  elog(ERROR, "could not find tuple for parent of relation %u", relid);
64 
65  heap_close(catalogRelation, AccessShareLock);
66 
67  return result;
68 }
static Oid get_partition_parent_worker(Relation inhRel, Oid relid)
Definition: partition.c:76
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
#define ERROR
Definition: elog.h:43
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define elog
Definition: elog.h:219

◆ get_partition_parent_worker()

static Oid get_partition_parent_worker ( Relation  inhRel,
Oid  relid 
)
static

Definition at line 76 of file partition.c.

References BTEqualStrategyNumber, GETSTRUCT, HeapTupleIsValid, InheritsRelidSeqnoIndexId, Int32GetDatum, InvalidOid, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by get_partition_ancestors_worker(), and get_partition_parent().

77 {
78  SysScanDesc scan;
79  ScanKeyData key[2];
80  Oid result = InvalidOid;
81  HeapTuple tuple;
82 
83  ScanKeyInit(&key[0],
84  Anum_pg_inherits_inhrelid,
85  BTEqualStrategyNumber, F_OIDEQ,
86  ObjectIdGetDatum(relid));
87  ScanKeyInit(&key[1],
88  Anum_pg_inherits_inhseqno,
89  BTEqualStrategyNumber, F_INT4EQ,
90  Int32GetDatum(1));
91 
92  scan = systable_beginscan(inhRel, InheritsRelidSeqnoIndexId, true,
93  NULL, 2, key);
94  tuple = systable_getnext(scan);
95  if (HeapTupleIsValid(tuple))
96  {
98 
99  result = form->inhparent;
100  }
101 
102  systable_endscan(scan);
103 
104  return result;
105 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:502
#define GETSTRUCT(TUP)
Definition: htup_details.h:673
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:331
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:419
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define InheritsRelidSeqnoIndexId
Definition: indexing.h:169
FormData_pg_inherits * Form_pg_inherits
Definition: pg_inherits.h:44
#define Int32GetDatum(X)
Definition: postgres.h:462
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ get_proposed_default_constraint()

List* get_proposed_default_constraint ( List new_part_constraints)

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

338 {
339  Expr *defPartConstraint;
340 
341  defPartConstraint = make_ands_explicit(new_part_constraints);
342 
343  /*
344  * Derive the partition constraints of default partition by negating the
345  * given partition constraints. The partition constraint never evaluates
346  * to NULL, so negating it like this is safe.
347  */
348  defPartConstraint = makeBoolExpr(NOT_EXPR,
349  list_make1(defPartConstraint),
350  -1);
351 
352  /* Simplify, to put the negated expression into canonical form */
353  defPartConstraint =
354  (Expr *) eval_const_expressions(NULL,
355  (Node *) defPartConstraint);
356  defPartConstraint = canonicalize_qual(defPartConstraint, true);
357 
358  return make_ands_implicit(defPartConstraint);
359 }
Definition: nodes.h:517
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2460
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:368
Expr * make_ands_explicit(List *andclauses)
Definition: clauses.c:368
#define list_make1(x1)
Definition: pg_list.h:139
List * make_ands_implicit(Expr *clause)
Definition: clauses.c:379
Expr * canonicalize_qual(Expr *qual, bool is_check)
Definition: prepqual.c:291

◆ has_partition_attrs()

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

Definition at line 206 of file partition.c.

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

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

207 {
208  PartitionKey key;
209  int partnatts;
210  List *partexprs;
211  ListCell *partexprs_item;
212  int i;
213 
214  if (attnums == NULL || rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
215  return false;
216 
217  key = RelationGetPartitionKey(rel);
218  partnatts = get_partition_natts(key);
219  partexprs = get_partition_exprs(key);
220 
221  partexprs_item = list_head(partexprs);
222  for (i = 0; i < partnatts; i++)
223  {
224  AttrNumber partattno = get_partition_col_attnum(key, i);
225 
226  if (partattno != 0)
227  {
229  attnums))
230  {
231  if (used_in_expr)
232  *used_in_expr = false;
233  return true;
234  }
235  }
236  else
237  {
238  /* Arbitrary expression */
239  Node *expr = (Node *) lfirst(partexprs_item);
240  Bitmapset *expr_attrs = NULL;
241 
242  /* Find all attributes referenced */
243  pull_varattnos(expr, 1, &expr_attrs);
244  partexprs_item = lnext(partexprs_item);
245 
246  if (bms_overlap(attnums, expr_attrs))
247  {
248  if (used_in_expr)
249  *used_in_expr = true;
250  return true;
251  }
252  }
253  }
254 
255  return false;
256 }
Definition: nodes.h:517
static int get_partition_natts(PartitionKey key)
Definition: partcache.h:64
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
Form_pg_class rd_rel
Definition: rel.h:84
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
static List * get_partition_exprs(PartitionKey key)
Definition: partcache.h:70
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
static int16 get_partition_col_attnum(PartitionKey key, int col)
Definition: partcache.h:79
#define lfirst(lc)
Definition: pg_list.h:106
#define RelationGetPartitionKey(relation)
Definition: rel.h:589
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
int i
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486
int16 AttrNumber
Definition: attnum.h:21

◆ 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 168 of file partition.c.

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

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

171 {
172  bool my_found_whole_row = false;
173 
174  if (expr != NIL)
175  {
176  AttrNumber *part_attnos;
177 
178  part_attnos = convert_tuples_by_name_map(RelationGetDescr(to_rel),
179  RelationGetDescr(from_rel),
180  gettext_noop("could not convert row type"));
181  expr = (List *) map_variable_attnos((Node *) expr,
182  fromrel_varno, 0,
183  part_attnos,
184  RelationGetDescr(from_rel)->natts,
185  RelationGetForm(to_rel)->reltype,
186  &my_found_whole_row);
187  }
188 
189  if (found_whole_row)
190  *found_whole_row = my_found_whole_row;
191 
192  return expr;
193 }
#define NIL
Definition: pg_list.h:69
#define RelationGetDescr(relation)
Definition: rel.h:433
#define RelationGetForm(relation)
Definition: rel.h:401
#define gettext_noop(x)
Definition: c.h:1036
Definition: nodes.h:517
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, const char *msg)
Definition: tupconvert.c:293
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21

◆ update_default_partition_oid()

void update_default_partition_oid ( Oid  parentId,
Oid  defaultPartId 
)

Definition at line 307 of file partition.c.

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

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

308 {
309  HeapTuple tuple;
310  Relation pg_partitioned_table;
311  Form_pg_partitioned_table part_table_form;
312 
313  pg_partitioned_table = heap_open(PartitionedRelationId, RowExclusiveLock);
314 
315  tuple = SearchSysCacheCopy1(PARTRELID, ObjectIdGetDatum(parentId));
316 
317  if (!HeapTupleIsValid(tuple))
318  elog(ERROR, "cache lookup failed for partition key of relation %u",
319  parentId);
320 
321  part_table_form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
322  part_table_form->partdefid = defaultPartId;
323  CatalogTupleUpdate(pg_partitioned_table, &tuple->t_self, tuple);
324 
325  heap_freetuple(tuple);
326  heap_close(pg_partitioned_table, RowExclusiveLock);
327 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:673
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
FormData_pg_partitioned_table * Form_pg_partitioned_table
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:211
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
#define elog
Definition: elog.h:219