Skip to content

Commit 1e178f0

Browse files
committed
pathman: copy data before dropping partitions
1 parent ad580df commit 1e178f0

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

contrib/pg_pathman/expected/pg_pathman.out

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,15 @@ NOTICE: function test.hash_rel_hash_update_trigger_func() does not exist, skipp
481481

482482
DROP TABLE test.hash_rel CASCADE;
483483
SELECT pathman.drop_range_partitions('test.num_range_rel');
484+
NOTICE: 0 rows copied from test.num_range_rel_7
485+
NOTICE: 0 rows copied from test.num_range_rel_6
486+
NOTICE: 2 rows copied from test.num_range_rel_4
487+
NOTICE: 1000 rows copied from test.num_range_rel_3
488+
NOTICE: 1000 rows copied from test.num_range_rel_2
489+
NOTICE: 998 rows copied from test.num_range_rel_1
484490
drop_range_partitions
485491
-----------------------
486-
492+
6
487493
(1 row)
488494

489495
DROP TABLE test.num_range_rel CASCADE;
@@ -668,9 +674,24 @@ EXPLAIN (COSTS OFF) SELECT * FROM range_rel WHERE dt < '2010-03-01';
668674

669675
/* Create range partitions from whole range */
670676
SELECT drop_range_partitions('range_rel');
677+
NOTICE: 0 rows copied from range_rel_archive
678+
NOTICE: 0 rows copied from range_rel_15
679+
NOTICE: 0 rows copied from range_rel_14
680+
NOTICE: 14 rows copied from range_rel_13
681+
NOTICE: 31 rows copied from range_rel_12
682+
NOTICE: 30 rows copied from range_rel_11
683+
NOTICE: 31 rows copied from range_rel_10
684+
NOTICE: 30 rows copied from range_rel_9
685+
NOTICE: 31 rows copied from range_rel_8
686+
NOTICE: 31 rows copied from range_rel_7
687+
NOTICE: 30 rows copied from range_rel_6
688+
NOTICE: 31 rows copied from range_rel_5
689+
NOTICE: 30 rows copied from range_rel_4
690+
NOTICE: 31 rows copied from range_rel_3
691+
NOTICE: 45 rows copied from range_rel_1
671692
drop_range_partitions
672693
-----------------------
673-
694+
15
674695
(1 row)
675696

676697
SELECT create_partitions_from_range('range_rel', 'id', 1, 1000, 100);
@@ -681,9 +702,19 @@ NOTICE: Copying data to partitions...
681702
(1 row)
682703

683704
SELECT drop_range_partitions('range_rel');
705+
NOTICE: 0 rows copied from range_rel_10
706+
NOTICE: 0 rows copied from range_rel_9
707+
NOTICE: 0 rows copied from range_rel_8
708+
NOTICE: 0 rows copied from range_rel_7
709+
NOTICE: 0 rows copied from range_rel_6
710+
NOTICE: 0 rows copied from range_rel_5
711+
NOTICE: 65 rows copied from range_rel_4
712+
NOTICE: 100 rows copied from range_rel_3
713+
NOTICE: 100 rows copied from range_rel_2
714+
NOTICE: 100 rows copied from range_rel_1
684715
drop_range_partitions
685716
-----------------------
686-
717+
10
687718
(1 row)
688719

689720
SELECT create_partitions_from_range('range_rel', 'dt', '2015-01-01'::date, '2015-12-01'::date, '1 month'::interval);

contrib/pg_pathman/range.sql

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,27 +719,39 @@ $$ LANGUAGE plpgsql;
719719
* Drop partitions
720720
*/
721721
CREATE OR REPLACE FUNCTION @extschema@.drop_range_partitions(IN relation TEXT)
722-
RETURNS VOID AS
722+
RETURNS INTEGER AS
723723
$$
724724
DECLARE
725725
v_rec RECORD;
726+
-- v_total_rows INTEGER;
727+
v_rows INTEGER;
728+
v_part_count INTEGER := 0;
726729
BEGIN
727730
relation := @extschema@.validate_relname(relation);
728731

729732
/* Drop trigger first */
730733
PERFORM @extschema@.drop_range_triggers(relation);
731734

732-
FOR v_rec IN (SELECT inhrelid::regclass AS tbl
735+
FOR v_rec IN (SELECT inhrelid::regclass::text AS tbl
733736
FROM pg_inherits WHERE inhparent = relation::regclass::oid)
734737
LOOP
738+
EXECUTE format('WITH part_data AS (DELETE FROM %s RETURNING *)
739+
INSERT INTO %s SELECT * FROM part_data'
740+
, v_rec.tbl
741+
, relation);
742+
GET DIAGNOSTICS v_rows = ROW_COUNT;
735743
EXECUTE format('DROP TABLE %s', v_rec.tbl);
744+
RAISE NOTICE '% rows copied from %', v_rows, v_rec.tbl;
745+
v_part_count := v_part_count + 1;
736746
END LOOP;
737747

738748
DELETE FROM @extschema@.pathman_config WHERE relname = relation;
739749
-- DELETE FROM pg_pathman_range_rels WHERE parent = relation;
740750

741751
/* Notify backend about changes */
742752
PERFORM @extschema@.on_remove_partitions(relation::regclass::oid);
753+
754+
RETURN v_part_count;
743755
END
744756
$$ LANGUAGE plpgsql;
745757

0 commit comments

Comments
 (0)