Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aee0875

Browse files
committed
Deny split and merge for subpartitions
1 parent 4bdc792 commit aee0875

File tree

5 files changed

+80
-14
lines changed

5 files changed

+80
-14
lines changed

expected/pathman_subpartitions.out

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,46 @@ SELECT tableoid::regclass, * FROM subpartitions.abc; /* Should create subpartiti
300300
subpartitions.abc_2_3 | 125 | 125
301301
(10 rows)
302302

303+
/* split_range_partition */
304+
SELECT split_range_partition('subpartitions.abc_2', 150);
305+
ERROR: could not split partition if it has children
306+
SELECT split_range_partition('subpartitions.abc_2_2', 75);
307+
split_range_partition
308+
-----------------------
309+
{50,100}
310+
(1 row)
311+
312+
SELECT subpartitions.partitions_tree('subpartitions.abc');
313+
partitions_tree
314+
--------------------------
315+
subpartitions.abc
316+
subpartitions.abc_1
317+
subpartitions.abc_1_1
318+
subpartitions.abc_1_2
319+
subpartitions.abc_2
320+
subpartitions.abc_2_1
321+
subpartitions.abc_2_2
322+
subpartitions.abc_2_4
323+
subpartitions.abc_2_3
324+
(9 rows)
325+
326+
/* merge_range_partitions */
327+
SELECT append_range_partition('subpartitions.abc', 'subpartitions.abc_3');
328+
append_range_partition
329+
------------------------
330+
subpartitions.abc_3
331+
(1 row)
332+
333+
select merge_range_partitions('subpartitions.abc_2', 'subpartitions.abc_3');
334+
ERROR: cannot merge partitions
335+
select merge_range_partitions('subpartitions.abc_2_1', 'subpartitions.abc_2_2');
336+
merge_range_partitions
337+
------------------------
338+
339+
(1 row)
340+
303341
DROP TABLE subpartitions.abc CASCADE;
304-
NOTICE: drop cascades to 10 other objects
342+
NOTICE: drop cascades to 11 other objects
305343
DROP SCHEMA subpartitions CASCADE;
306344
NOTICE: drop cascades to function subpartitions.partitions_tree(regclass,text)
307345
DROP EXTENSION pg_pathman;

range.sql

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,29 @@ CREATE OR REPLACE FUNCTION @extschema@.split_range_partition(
313313
OUT p_range ANYARRAY)
314314
RETURNS ANYARRAY AS $$
315315
DECLARE
316-
parent_relid REGCLASS;
317-
part_type INTEGER;
318-
part_expr TEXT;
319-
part_expr_type REGTYPE;
320-
check_name TEXT;
321-
check_cond TEXT;
322-
new_partition TEXT;
316+
parent_relid REGCLASS;
317+
inhparent REGCLASS;
318+
part_type INTEGER;
319+
part_expr TEXT;
320+
part_expr_type REGTYPE;
321+
check_name TEXT;
322+
check_cond TEXT;
323+
new_partition TEXT;
323324

324325
BEGIN
325326
parent_relid = @extschema@.get_parent_of_partition(partition_relid);
326327

327328
PERFORM @extschema@.validate_relname(parent_relid);
328329
PERFORM @extschema@.validate_relname(partition_relid);
329330

331+
EXECUTE format('SELECT inhparent::REGCLASS FROM pg_inherits WHERE inhparent = $1 LIMIT 1')
332+
USING partition_relid
333+
INTO inhparent;
334+
335+
if inhparent IS NOT NULL THEN
336+
RAISE EXCEPTION 'could not split partition if it has children';
337+
END IF;
338+
330339
/* Acquire lock on parent */
331340
PERFORM @extschema@.prevent_part_modification(parent_relid);
332341

sql/pathman_subpartitions.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,15 @@ SELECT tableoid::regclass, * FROM subpartitions.abc; /* Should be in subpartitio
9797
UPDATE subpartitions.abc SET b = 125 WHERE a = 125 and b = 75;
9898
SELECT tableoid::regclass, * FROM subpartitions.abc; /* Should create subpartitions.abc_2_3 */
9999

100+
/* split_range_partition */
101+
SELECT split_range_partition('subpartitions.abc_2', 150);
102+
SELECT split_range_partition('subpartitions.abc_2_2', 75);
103+
SELECT subpartitions.partitions_tree('subpartitions.abc');
100104

105+
/* merge_range_partitions */
106+
SELECT append_range_partition('subpartitions.abc', 'subpartitions.abc_3');
107+
select merge_range_partitions('subpartitions.abc_2', 'subpartitions.abc_3');
108+
select merge_range_partitions('subpartitions.abc_2_1', 'subpartitions.abc_2_2');
101109

102110
DROP TABLE subpartitions.abc CASCADE;
103111
DROP SCHEMA subpartitions CASCADE;

src/init.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ fini_local_cache(void)
406406
* find_inheritance_children
407407
*
408408
* Returns an array containing the OIDs of all relations which
409-
* inherit *directly* from the relation with OID 'parentrelId'.
409+
* inherit *directly* from the relation with OID 'parent_relid'.
410410
*
411411
* The specified lock type is acquired on each child relation (but not on the
412412
* given rel; caller should already have locked it). If lockmode is NoLock
@@ -416,7 +416,7 @@ fini_local_cache(void)
416416
* borrowed from pg_inherits.c
417417
*/
418418
find_children_status
419-
find_inheritance_children_array(Oid parentrelId,
419+
find_inheritance_children_array(Oid parent_relid,
420420
LOCKMODE lockmode,
421421
bool nowait,
422422
uint32 *children_size, /* ret value #1 */
@@ -444,7 +444,7 @@ find_inheritance_children_array(Oid parentrelId,
444444
* Can skip the scan if pg_class shows the
445445
* relation has never had a subclass.
446446
*/
447-
if (!has_subclass(parentrelId))
447+
if (!has_subclass(parent_relid))
448448
return FCS_NO_CHILDREN;
449449

450450
/*
@@ -459,7 +459,7 @@ find_inheritance_children_array(Oid parentrelId,
459459
ScanKeyInit(&key[0],
460460
Anum_pg_inherits_inhparent,
461461
BTEqualStrategyNumber, F_OIDEQ,
462-
ObjectIdGetDatum(parentrelId));
462+
ObjectIdGetDatum(parent_relid));
463463

464464
scan = systable_beginscan(relation, InheritsParentIndexId, true,
465465
NULL, 1, key);

src/pl_range_funcs.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
#include "xact_handling.h"
1717

1818
#include "access/xact.h"
19+
#include "catalog/heap.h"
1920
#include "catalog/namespace.h"
21+
#include "catalog/pg_inherits_fn.h"
2022
#include "catalog/pg_type.h"
21-
#include "catalog/heap.h"
2223
#include "commands/tablecmds.h"
2324
#include "executor/spi.h"
2425
#include "nodes/nodeFuncs.h"
@@ -636,7 +637,17 @@ merge_range_partitions(PG_FUNCTION_ARGS)
636637
/* Extract partition Oids from array */
637638
partitions = palloc(sizeof(Oid) * nparts);
638639
for (i = 0; i < nparts; i++)
639-
partitions[i] = DatumGetObjectId(datums[i]);
640+
{
641+
Oid partition_relid;
642+
partition_relid = DatumGetObjectId(datums[i]);
643+
644+
/* check that is not has subpartitions */
645+
if (has_subclass(partition_relid))
646+
ereport(ERROR, (errmsg("cannot merge partitions"),
647+
errdetail("at least one of specified partitions has children")));
648+
649+
partitions[i] = partition_relid;
650+
}
640651

641652
if (nparts < 2)
642653
ereport(ERROR, (errmsg("cannot merge partitions"),

0 commit comments

Comments
 (0)