PostgreSQL Source Code git master
Loading...
Searching...
No Matches
partition.c File Reference
#include "postgres.h"
#include "access/attmap.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.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/optimizer.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, bool *detach_pending)
 
static void get_partition_ancestors_worker (Relation inhRel, Oid relid, List **ancestors)
 
Oid get_partition_parent (Oid relid, bool even_if_detached)
 
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 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)
 

Function Documentation

◆ get_default_partition_oid()

Oid get_default_partition_oid ( Oid  parentId)

Definition at line 315 of file partition.c.

316{
317 HeapTuple tuple;
319
321
322 if (HeapTupleIsValid(tuple))
323 {
325
327 defaultPartId = part_table_form->partdefid;
328 ReleaseSysCache(tuple);
329 }
330
331 return defaultPartId;
332}
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
FormData_pg_partitioned_table * Form_pg_partitioned_table
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
#define InvalidOid
unsigned int Oid
static int fb(int x)
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition syscache.c:220

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

Referenced by heap_drop_with_catalog(), and RelationBuildPartitionDesc().

◆ get_partition_ancestors()

List * get_partition_ancestors ( Oid  relid)

Definition at line 134 of file partition.c.

135{
136 List *result = NIL;
138
140
141 get_partition_ancestors_worker(inhRel, relid, &result);
142
144
145 return result;
146}
#define AccessShareLock
Definition lockdefs.h:36
static void get_partition_ancestors_worker(Relation inhRel, Oid relid, List **ancestors)
Definition partition.c:153
#define NIL
Definition pg_list.h:68
Definition pg_list.h:54
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40

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

Referenced by ExecGetAncestorResultRels(), ExecInitPartitionInfo(), filter_partitions(), get_rel_sync_entry(), getIdentitySequence(), index_concurrently_swap(), pg_partition_ancestors(), pg_partition_root(), pg_partition_tree(), and RelationBuildPublicationDesc().

◆ get_partition_ancestors_worker()

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

Definition at line 153 of file partition.c.

154{
156 bool detach_pending;
157
158 /*
159 * Recursion ends at the topmost level, ie., when there's no parent; also
160 * when the partition is being detached.
161 */
164 return;
165
166 *ancestors = lappend_oid(*ancestors, parentOid);
168}
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
static Oid get_partition_parent_worker(Relation inhRel, Oid relid, bool *detach_pending)
Definition partition.c:85

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

Referenced by get_partition_ancestors(), and get_partition_ancestors_worker().

◆ get_partition_parent()

Oid get_partition_parent ( Oid  relid,
bool  even_if_detached 
)

Definition at line 53 of file partition.c.

54{
56 Oid result;
57 bool detach_pending;
58
60
63
64 if (!OidIsValid(result))
65 elog(ERROR, "could not find tuple for parent of relation %u", relid);
66
68 elog(ERROR, "relation %u has no parent because it's being detached",
69 relid);
70
72
73 return result;
74}
#define OidIsValid(objectId)
Definition c.h:788
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226

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

Referenced by ATExecAttachPartitionIdx(), ATExecDropNotNull(), checkPartition(), DetachPartitionFinalize(), generate_partition_qual(), heap_drop_with_catalog(), index_get_partition(), RangeVarCallbackForDropRelation(), renametrig(), and validatePartitionedIndex().

◆ get_partition_parent_worker()

static Oid get_partition_parent_worker ( Relation  inhRel,
Oid  relid,
bool detach_pending 
)
static

Definition at line 85 of file partition.c.

86{
87 SysScanDesc scan;
89 Oid result = InvalidOid;
90 HeapTuple tuple;
91
92 *detach_pending = false;
93
94 ScanKeyInit(&key[0],
97 ObjectIdGetDatum(relid));
98 ScanKeyInit(&key[1],
101 Int32GetDatum(1));
102
104 NULL, 2, key);
105 tuple = systable_getnext(scan);
106 if (HeapTupleIsValid(tuple))
107 {
109
110 /* Let caller know of partition being detached */
111 if (form->inhdetachpending)
112 *detach_pending = true;
113 result = form->inhparent;
114 }
115
116 systable_endscan(scan);
117
118 return result;
119}
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
FormData_pg_inherits * Form_pg_inherits
Definition pg_inherits.h:45
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
#define BTEqualStrategyNumber
Definition stratnum.h:31

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

Referenced by get_partition_ancestors_worker(), and get_partition_parent().

◆ get_proposed_default_constraint()

List * get_proposed_default_constraint ( List new_part_constraints)

Definition at line 370 of file partition.c.

371{
373
375
376 /*
377 * Derive the partition constraints of default partition by negating the
378 * given partition constraints. The partition constraint never evaluates
379 * to NULL, so negating it like this is safe.
380 */
383 -1);
384
385 /* Simplify, to put the negated expression into canonical form */
390
392}
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition clauses.c:2267
Expr * make_ands_explicit(List *andclauses)
Definition makefuncs.c:799
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition makefuncs.c:420
List * make_ands_implicit(Expr *clause)
Definition makefuncs.c:810
#define list_make1(x1)
Definition pg_list.h:212
Expr * canonicalize_qual(Expr *qual, bool is_check)
Definition prepqual.c:293
@ NOT_EXPR
Definition primnodes.h:963
Definition nodes.h:135

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

Referenced by ATExecAttachPartition(), and check_default_partition_contents().

◆ has_partition_attrs()

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

Definition at line 255 of file partition.c.

256{
258 int partnatts;
259 List *partexprs;
261 int i;
262
263 if (attnums == NULL || rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
264 return false;
265
267 partnatts = get_partition_natts(key);
268 partexprs = get_partition_exprs(key);
269
270 partexprs_item = list_head(partexprs);
271 for (i = 0; i < partnatts; i++)
272 {
274
275 if (partattno != 0)
276 {
278 attnums))
279 {
280 if (used_in_expr)
281 *used_in_expr = false;
282 return true;
283 }
284 }
285 else
286 {
287 /* Arbitrary expression */
288 Node *expr = (Node *) lfirst(partexprs_item);
290
291 /* Find all attributes referenced */
292 pull_varattnos(expr, 1, &expr_attrs);
293 partexprs_item = lnext(partexprs, partexprs_item);
294
295 if (bms_overlap(attnums, expr_attrs))
296 {
297 if (used_in_expr)
298 *used_in_expr = true;
299 return true;
300 }
301 }
302 }
303
304 return false;
305}
int16 AttrNumber
Definition attnum.h:21
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:581
int i
Definition isn.c:77
PartitionKey RelationGetPartitionKey(Relation rel)
Definition partcache.c:51
static int16 get_partition_col_attnum(PartitionKey key, int col)
Definition partcache.h:80
static int get_partition_natts(PartitionKey key)
Definition partcache.h:65
static List * get_partition_exprs(PartitionKey key)
Definition partcache.h:71
#define lfirst(lc)
Definition pg_list.h:172
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:343
Form_pg_class rd_rel
Definition rel.h:111
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition var.c:296

References bms_is_member(), bms_overlap(), fb(), 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(), and ATPrepAlterColumnType().

◆ index_get_partition()

Oid index_get_partition ( Relation  partition,
Oid  indexId 
)

Definition at line 176 of file partition.c.

177{
179 ListCell *l;
180
181 foreach(l, idxlist)
182 {
186 bool ispartition;
187
189 if (!HeapTupleIsValid(tup))
190 elog(ERROR, "cache lookup failed for relation %u", partIdx);
192 ispartition = classForm->relispartition;
194 if (!ispartition)
195 continue;
196 if (get_partition_parent(partIdx, false) == indexId)
197 {
199 return partIdx;
200 }
201 }
202
204 return InvalidOid;
205}
void list_free(List *list)
Definition list.c:1546
Oid get_partition_parent(Oid relid, bool even_if_detached)
Definition partition.c:53
FormData_pg_class * Form_pg_class
Definition pg_class.h:156
#define lfirst_oid(lc)
Definition pg_list.h:174
List * RelationGetIndexList(Relation relation)
Definition relcache.c:4831

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

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

◆ map_partition_varattnos()

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

Definition at line 222 of file partition.c.

224{
225 if (expr != NIL)
226 {
228 bool found_whole_row;
229
232 false);
233 expr = (List *) map_variable_attnos((Node *) expr,
234 fromrel_varno, 0,
236 RelationGetForm(to_rel)->reltype,
237 &found_whole_row);
238 /* Since we provided a to_rowtype, we may ignore found_whole_row. */
239 }
240
241 return expr;
242}
AttrMap * build_attrmap_by_name(TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
Definition attmap.c:175
#define RelationGetForm(relation)
Definition rel.h:508
#define RelationGetDescr(relation)
Definition rel.h:540
Node * map_variable_attnos(Node *node, int target_varno, int sublevels_up, const AttrMap *attno_map, Oid to_rowtype, bool *found_whole_row)

References build_attrmap_by_name(), fb(), map_variable_attnos(), NIL, RelationGetDescr, and RelationGetForm.

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

◆ update_default_partition_oid()

void update_default_partition_oid ( Oid  parentId,
Oid  defaultPartId 
)

Definition at line 340 of file partition.c.

341{
342 HeapTuple tuple;
345
347
349
350 if (!HeapTupleIsValid(tuple))
351 elog(ERROR, "cache lookup failed for partition key of relation %u",
352 parentId);
353
355 part_table_form->partdefid = defaultPartId;
357
358 heap_freetuple(tuple);
360}
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1435
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
#define RowExclusiveLock
Definition lockdefs.h:38
ItemPointerData t_self
Definition htup.h:65
#define SearchSysCacheCopy1(cacheId, key1)
Definition syscache.h:91

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

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