Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * partition.c
4 : : * Partitioning related data structures and functions.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/catalog/partition.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include "access/attmap.h"
18 : : #include "access/genam.h"
19 : : #include "access/htup_details.h"
20 : : #include "access/sysattr.h"
21 : : #include "access/table.h"
22 : : #include "catalog/indexing.h"
23 : : #include "catalog/partition.h"
24 : : #include "catalog/pg_inherits.h"
25 : : #include "catalog/pg_partitioned_table.h"
26 : : #include "nodes/makefuncs.h"
27 : : #include "optimizer/optimizer.h"
28 : : #include "rewrite/rewriteManip.h"
29 : : #include "utils/fmgroids.h"
30 : : #include "utils/partcache.h"
31 : : #include "utils/rel.h"
32 : : #include "utils/syscache.h"
33 : :
34 : : static Oid get_partition_parent_worker(Relation inhRel, Oid relid,
35 : : bool *detach_pending);
36 : : static void get_partition_ancestors_worker(Relation inhRel, Oid relid,
37 : : List **ancestors);
38 : :
39 : : /*
40 : : * get_partition_parent
41 : : * Obtain direct parent of given relation
42 : : *
43 : : * Returns inheritance parent of a partition by scanning pg_inherits
44 : : *
45 : : * If the partition is in the process of being detached, an error is thrown,
46 : : * unless even_if_detached is passed as true.
47 : : *
48 : : * Note: Because this function assumes that the relation whose OID is passed
49 : : * as an argument will have precisely one parent, it should only be called
50 : : * when it is known that the relation is a partition.
51 : : */
52 : : Oid
53 : 9389 : get_partition_parent(Oid relid, bool even_if_detached)
54 : : {
55 : : Relation catalogRelation;
56 : : Oid result;
57 : : bool detach_pending;
58 : :
59 : 9389 : catalogRelation = table_open(InheritsRelationId, AccessShareLock);
60 : :
61 : 9389 : result = get_partition_parent_worker(catalogRelation, relid,
62 : : &detach_pending);
63 : :
64 [ - + ]: 9389 : if (!OidIsValid(result))
65 [ # # ]: 0 : elog(ERROR, "could not find tuple for parent of relation %u", relid);
66 : :
67 [ + + - + ]: 9389 : if (detach_pending && !even_if_detached)
68 [ # # ]: 0 : elog(ERROR, "relation %u has no parent because it's being detached",
69 : : relid);
70 : :
71 : 9389 : table_close(catalogRelation, AccessShareLock);
72 : :
73 : 9389 : return result;
74 : : }
75 : :
76 : : /*
77 : : * get_partition_parent_worker
78 : : * Scan the pg_inherits relation to return the OID of the parent of the
79 : : * given relation
80 : : *
81 : : * If the partition is being detached, *detach_pending is set true (but the
82 : : * original parent is still returned.)
83 : : */
84 : : static Oid
85 : 20204 : get_partition_parent_worker(Relation inhRel, Oid relid, bool *detach_pending)
86 : : {
87 : : SysScanDesc scan;
88 : : ScanKeyData key[2];
89 : 20204 : Oid result = InvalidOid;
90 : : HeapTuple tuple;
91 : :
92 : 20204 : *detach_pending = false;
93 : :
94 : 20204 : ScanKeyInit(&key[0],
95 : : Anum_pg_inherits_inhrelid,
96 : : BTEqualStrategyNumber, F_OIDEQ,
97 : : ObjectIdGetDatum(relid));
98 : 20204 : ScanKeyInit(&key[1],
99 : : Anum_pg_inherits_inhseqno,
100 : : BTEqualStrategyNumber, F_INT4EQ,
101 : : Int32GetDatum(1));
102 : :
103 : 20204 : scan = systable_beginscan(inhRel, InheritsRelidSeqnoIndexId, true,
104 : : NULL, 2, key);
105 : 20204 : tuple = systable_getnext(scan);
106 [ + + ]: 20204 : if (HeapTupleIsValid(tuple))
107 : : {
108 : 14915 : Form_pg_inherits form = (Form_pg_inherits) GETSTRUCT(tuple);
109 : :
110 : : /* Let caller know of partition being detached */
111 [ + + ]: 14915 : if (form->inhdetachpending)
112 : 45 : *detach_pending = true;
113 : 14915 : result = form->inhparent;
114 : : }
115 : :
116 : 20204 : systable_endscan(scan);
117 : :
118 : 20204 : return result;
119 : : }
120 : :
121 : : /*
122 : : * get_partition_ancestors
123 : : * Obtain ancestors of given relation
124 : : *
125 : : * Returns a list of ancestors of the given relation. The list is ordered:
126 : : * The first element is the immediate parent and the last one is the topmost
127 : : * parent in the partition hierarchy.
128 : : *
129 : : * A partition pending concurrent detach is treated as having no parent. The
130 : : * list can therefore be empty even if the relation is still marked as a
131 : : * partition in pg_class. If an ancestor is pending detach, the list ends at
132 : : * that ancestor.
133 : : *
134 : : * Note: Because this function assumes that the relation whose OID is passed
135 : : * as an argument and each ancestor will have precisely one parent, it should
136 : : * only be called when it is known that the relation is a partition.
137 : : */
138 : : List *
139 : 5292 : get_partition_ancestors(Oid relid)
140 : : {
141 : 5292 : List *result = NIL;
142 : : Relation inhRel;
143 : :
144 : 5292 : inhRel = table_open(InheritsRelationId, AccessShareLock);
145 : :
146 : 5292 : get_partition_ancestors_worker(inhRel, relid, &result);
147 : :
148 : 5292 : table_close(inhRel, AccessShareLock);
149 : :
150 : 5292 : return result;
151 : : }
152 : :
153 : : /*
154 : : * get_partition_ancestors_worker
155 : : * recursive worker for get_partition_ancestors
156 : : */
157 : : static void
158 : 10815 : get_partition_ancestors_worker(Relation inhRel, Oid relid, List **ancestors)
159 : : {
160 : : Oid parentOid;
161 : : bool detach_pending;
162 : :
163 : : /*
164 : : * Recursion ends at the topmost level, ie., when there's no parent; also
165 : : * when the partition is being detached.
166 : : */
167 : 10815 : parentOid = get_partition_parent_worker(inhRel, relid, &detach_pending);
168 [ + + + + ]: 10815 : if (parentOid == InvalidOid || detach_pending)
169 : 5292 : return;
170 : :
171 : 5523 : *ancestors = lappend_oid(*ancestors, parentOid);
172 : 5523 : get_partition_ancestors_worker(inhRel, parentOid, ancestors);
173 : : }
174 : :
175 : : /*
176 : : * index_get_partition
177 : : * Return the OID of index of the given partition that is a child
178 : : * of the given index, or InvalidOid if there isn't one.
179 : : */
180 : : Oid
181 : 1005 : index_get_partition(Relation partition, Oid indexId)
182 : : {
183 : 1005 : List *idxlist = RelationGetIndexList(partition);
184 : : ListCell *l;
185 : :
186 [ + - + + : 1393 : foreach(l, idxlist)
+ + ]
187 : : {
188 : 1143 : Oid partIdx = lfirst_oid(l);
189 : : HeapTuple tup;
190 : : Form_pg_class classForm;
191 : : bool ispartition;
192 : :
193 : 1143 : tup = SearchSysCache1(RELOID, ObjectIdGetDatum(partIdx));
194 [ - + ]: 1143 : if (!HeapTupleIsValid(tup))
195 [ # # ]: 0 : elog(ERROR, "cache lookup failed for relation %u", partIdx);
196 : 1143 : classForm = (Form_pg_class) GETSTRUCT(tup);
197 : 1143 : ispartition = classForm->relispartition;
198 : 1143 : ReleaseSysCache(tup);
199 [ + + ]: 1143 : if (!ispartition)
200 : 368 : continue;
201 [ + + ]: 775 : if (get_partition_parent(partIdx, false) == indexId)
202 : : {
203 : 755 : list_free(idxlist);
204 : 755 : return partIdx;
205 : : }
206 : : }
207 : :
208 : 250 : list_free(idxlist);
209 : 250 : return InvalidOid;
210 : : }
211 : :
212 : : /*
213 : : * map_partition_varattnos - maps varattnos of all Vars in 'expr' (that have
214 : : * varno 'fromrel_varno') from the attnums of 'from_rel' to the attnums of
215 : : * 'to_rel', each of which may be either a leaf partition or a partitioned
216 : : * table, but both of which must be from the same partitioning hierarchy.
217 : : *
218 : : * We need this because even though all of the same column names must be
219 : : * present in all relations in the hierarchy, and they must also have the
220 : : * same types, the attnums may be different.
221 : : *
222 : : * Note: this will work on any node tree, so really the argument and result
223 : : * should be declared "Node *". But a substantial majority of the callers
224 : : * are working on Lists, so it's less messy to do the casts internally.
225 : : */
226 : : List *
227 : 5355 : map_partition_varattnos(List *expr, int fromrel_varno,
228 : : Relation to_rel, Relation from_rel)
229 : : {
230 [ + + ]: 5355 : if (expr != NIL)
231 : : {
232 : : AttrMap *part_attmap;
233 : : bool found_whole_row;
234 : :
235 : 4487 : part_attmap = build_attrmap_by_name(RelationGetDescr(to_rel),
236 : : RelationGetDescr(from_rel),
237 : : false);
238 : 4487 : expr = (List *) map_variable_attnos((Node *) expr,
239 : : fromrel_varno, 0,
240 : : part_attmap,
241 : 4487 : RelationGetForm(to_rel)->reltype,
242 : : &found_whole_row);
243 : : /* Since we provided a to_rowtype, we may ignore found_whole_row. */
244 : : }
245 : :
246 : 5355 : return expr;
247 : : }
248 : :
249 : : /*
250 : : * Checks if any of the 'attnums' is a partition key attribute for rel
251 : : *
252 : : * Sets *used_in_expr if any of the 'attnums' is found to be referenced in some
253 : : * partition key expression. It's possible for a column to be both used
254 : : * directly and as part of an expression; if that happens, *used_in_expr may
255 : : * end up as either true or false. That's OK for current uses of this
256 : : * function, because *used_in_expr is only used to tailor the error message
257 : : * text.
258 : : */
259 : : bool
260 : 2922 : has_partition_attrs(Relation rel, Bitmapset *attnums, bool *used_in_expr)
261 : : {
262 : : PartitionKey key;
263 : : int partnatts;
264 : : List *partexprs;
265 : : ListCell *partexprs_item;
266 : : int i;
267 : :
268 [ + - + + ]: 2922 : if (attnums == NULL || rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
269 : 2566 : return false;
270 : :
271 : 356 : key = RelationGetPartitionKey(rel);
272 : 356 : partnatts = get_partition_natts(key);
273 : 356 : partexprs = get_partition_exprs(key);
274 : :
275 : 356 : partexprs_item = list_head(partexprs);
276 [ + + ]: 712 : for (i = 0; i < partnatts; i++)
277 : : {
278 : 388 : AttrNumber partattno = get_partition_col_attnum(key, i);
279 : :
280 [ + + ]: 388 : if (partattno != 0)
281 : : {
282 [ + + ]: 336 : if (bms_is_member(partattno - FirstLowInvalidHeapAttributeNumber,
283 : : attnums))
284 : : {
285 [ + - ]: 20 : if (used_in_expr)
286 : 20 : *used_in_expr = false;
287 : 20 : return true;
288 : : }
289 : : }
290 : : else
291 : : {
292 : : /* Arbitrary expression */
293 : 52 : Node *expr = (Node *) lfirst(partexprs_item);
294 : 52 : Bitmapset *expr_attrs = NULL;
295 : :
296 : : /* Find all attributes referenced */
297 : 52 : pull_varattnos(expr, 1, &expr_attrs);
298 : 52 : partexprs_item = lnext(partexprs, partexprs_item);
299 : :
300 [ + + ]: 52 : if (bms_overlap(attnums, expr_attrs))
301 : : {
302 [ + - ]: 12 : if (used_in_expr)
303 : 12 : *used_in_expr = true;
304 : 12 : return true;
305 : : }
306 : : }
307 : : }
308 : :
309 : 324 : return false;
310 : : }
311 : :
312 : : /*
313 : : * get_default_partition_oid
314 : : *
315 : : * Given a relation OID, return the OID of the default partition, if one
316 : : * exists. Use get_default_oid_from_partdesc where possible, for
317 : : * efficiency.
318 : : */
319 : : Oid
320 : 6658 : get_default_partition_oid(Oid parentId)
321 : : {
322 : : HeapTuple tuple;
323 : 6658 : Oid defaultPartId = InvalidOid;
324 : :
325 : 6658 : tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(parentId));
326 : :
327 [ + - ]: 6658 : if (HeapTupleIsValid(tuple))
328 : : {
329 : : Form_pg_partitioned_table part_table_form;
330 : :
331 : 6658 : part_table_form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
332 : 6658 : defaultPartId = part_table_form->partdefid;
333 : 6658 : ReleaseSysCache(tuple);
334 : : }
335 : :
336 : 6658 : return defaultPartId;
337 : : }
338 : :
339 : : /*
340 : : * update_default_partition_oid
341 : : *
342 : : * Update pg_partitioned_table.partdefid with a new default partition OID.
343 : : */
344 : : void
345 : 693 : update_default_partition_oid(Oid parentId, Oid defaultPartId)
346 : : {
347 : : HeapTuple tuple;
348 : : Relation pg_partitioned_table;
349 : : Form_pg_partitioned_table part_table_form;
350 : :
351 : 693 : pg_partitioned_table = table_open(PartitionedRelationId, RowExclusiveLock);
352 : :
353 : 693 : tuple = SearchSysCacheCopy1(PARTRELID, ObjectIdGetDatum(parentId));
354 : :
355 [ - + ]: 693 : if (!HeapTupleIsValid(tuple))
356 [ # # ]: 0 : elog(ERROR, "cache lookup failed for partition key of relation %u",
357 : : parentId);
358 : :
359 : 693 : part_table_form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
360 : 693 : part_table_form->partdefid = defaultPartId;
361 : 693 : CatalogTupleUpdate(pg_partitioned_table, &tuple->t_self, tuple);
362 : :
363 : 693 : heap_freetuple(tuple);
364 : 693 : table_close(pg_partitioned_table, RowExclusiveLock);
365 : 693 : }
366 : :
367 : : /*
368 : : * get_proposed_default_constraint
369 : : *
370 : : * This function returns the negation of new_part_constraints, which
371 : : * would be an integral part of the default partition constraints after
372 : : * addition of the partition to which the new_part_constraints belongs.
373 : : */
374 : : List *
375 : 344 : get_proposed_default_constraint(List *new_part_constraints)
376 : : {
377 : : Expr *defPartConstraint;
378 : :
379 : 344 : defPartConstraint = make_ands_explicit(new_part_constraints);
380 : :
381 : : /*
382 : : * Derive the partition constraints of default partition by negating the
383 : : * given partition constraints. The partition constraint never evaluates
384 : : * to NULL, so negating it like this is safe.
385 : : */
386 : 344 : defPartConstraint = makeBoolExpr(NOT_EXPR,
387 : : list_make1(defPartConstraint),
388 : : -1);
389 : :
390 : : /* Simplify, to put the negated expression into canonical form */
391 : : defPartConstraint =
392 : 344 : (Expr *) eval_const_expressions(NULL,
393 : : (Node *) defPartConstraint);
394 : 344 : defPartConstraint = canonicalize_qual(defPartConstraint, true);
395 : :
396 : 344 : return make_ands_implicit(defPartConstraint);
397 : : }
|