Skip to content

Commit c17fe9b

Browse files
committed
Merge branch 'pathman_pgpro9_5' of gitlab.postgrespro.ru:pgpro-dev/postgrespro into pathman_pgpro9_5
2 parents 76af751 + a400a91 commit c17fe9b

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

contrib/pg_pathman/expected/pg_pathman.out

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,15 @@ NOTICE: function test.hash_rel_hash_update_trigger_func() does not exist, skipp
483483

484484
DROP TABLE test.hash_rel CASCADE;
485485
SELECT pathman.drop_range_partitions('test.num_range_rel');
486+
NOTICE: 0 rows copied from test.num_range_rel_7
487+
NOTICE: 0 rows copied from test.num_range_rel_6
488+
NOTICE: 2 rows copied from test.num_range_rel_4
489+
NOTICE: 1000 rows copied from test.num_range_rel_3
490+
NOTICE: 1000 rows copied from test.num_range_rel_2
491+
NOTICE: 998 rows copied from test.num_range_rel_1
486492
drop_range_partitions
487493
-----------------------
488-
494+
6
489495
(1 row)
490496

491497
DROP TABLE test.num_range_rel CASCADE;
@@ -655,9 +661,24 @@ EXPLAIN (COSTS OFF) SELECT * FROM range_rel WHERE dt < '2010-03-01';
655661

656662
/* Create range partitions from whole range */
657663
SELECT drop_range_partitions('range_rel');
664+
NOTICE: 0 rows copied from range_rel_archive
665+
NOTICE: 0 rows copied from range_rel_15
666+
NOTICE: 0 rows copied from range_rel_14
667+
NOTICE: 14 rows copied from range_rel_13
668+
NOTICE: 31 rows copied from range_rel_12
669+
NOTICE: 30 rows copied from range_rel_11
670+
NOTICE: 31 rows copied from range_rel_10
671+
NOTICE: 30 rows copied from range_rel_9
672+
NOTICE: 31 rows copied from range_rel_8
673+
NOTICE: 31 rows copied from range_rel_7
674+
NOTICE: 30 rows copied from range_rel_6
675+
NOTICE: 31 rows copied from range_rel_5
676+
NOTICE: 30 rows copied from range_rel_4
677+
NOTICE: 31 rows copied from range_rel_3
678+
NOTICE: 45 rows copied from range_rel_1
658679
drop_range_partitions
659680
-----------------------
660-
681+
15
661682
(1 row)
662683

663684
SELECT create_partitions_from_range('range_rel', 'id', 1, 1000, 100);
@@ -667,10 +688,10 @@ NOTICE: Copying data to partitions...
667688
10
668689
(1 row)
669690

670-
SELECT drop_range_partitions('range_rel');
691+
SELECT drop_range_partitions('range_rel', TRUE);
671692
drop_range_partitions
672693
-----------------------
673-
694+
10
674695
(1 row)
675696

676697
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: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -905,29 +905,46 @@ $$ LANGUAGE plpgsql;
905905

906906
/*
907907
* Drop partitions
908+
* If delete_data set to TRUE then partitions will be dropped with all the data
908909
*/
909-
CREATE OR REPLACE FUNCTION @extschema@.drop_range_partitions(IN relation TEXT)
910-
RETURNS VOID AS
910+
CREATE OR REPLACE FUNCTION @extschema@.drop_range_partitions(
911+
relation TEXT,
912+
delete_data BOOLEAN DEFAULT FALSE)
913+
RETURNS INTEGER AS
911914
$$
912915
DECLARE
913916
v_rec RECORD;
917+
-- v_total_rows INTEGER;
918+
v_rows INTEGER;
919+
v_part_count INTEGER := 0;
914920
BEGIN
915921
relation := @extschema@.validate_relname(relation);
916922

917923
/* Drop trigger first */
918924
PERFORM @extschema@.drop_range_triggers(relation);
919925

920-
FOR v_rec IN (SELECT inhrelid::regclass AS tbl
926+
FOR v_rec IN (SELECT inhrelid::regclass::text AS tbl
921927
FROM pg_inherits WHERE inhparent = relation::regclass::oid)
922928
LOOP
929+
IF NOT delete_data THEN
930+
EXECUTE format('WITH part_data AS (DELETE FROM %s RETURNING *)
931+
INSERT INTO %s SELECT * FROM part_data'
932+
, v_rec.tbl
933+
, relation);
934+
GET DIAGNOSTICS v_rows = ROW_COUNT;
935+
RAISE NOTICE '% rows copied from %', v_rows, v_rec.tbl;
936+
END IF;
923937
EXECUTE format('DROP TABLE %s', v_rec.tbl);
938+
v_part_count := v_part_count + 1;
924939
END LOOP;
925940

926941
DELETE FROM @extschema@.pathman_config WHERE relname = relation;
927942
-- DELETE FROM pg_pathman_range_rels WHERE parent = relation;
928943

929944
/* Notify backend about changes */
930945
PERFORM @extschema@.on_remove_partitions(relation::regclass::oid);
946+
947+
RETURN v_part_count;
931948
END
932949
$$ LANGUAGE plpgsql;
933950

contrib/pg_pathman/sql/pg_pathman.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ EXPLAIN (COSTS OFF) SELECT * FROM range_rel WHERE dt < '2010-03-01';
175175
/* Create range partitions from whole range */
176176
SELECT drop_range_partitions('range_rel');
177177
SELECT create_partitions_from_range('range_rel', 'id', 1, 1000, 100);
178-
SELECT drop_range_partitions('range_rel');
178+
SELECT drop_range_partitions('range_rel', TRUE);
179179
SELECT create_partitions_from_range('range_rel', 'dt', '2015-01-01'::date, '2015-12-01'::date, '1 month'::interval);
180180
EXPLAIN (COSTS OFF) SELECT * FROM range_rel WHERE dt = '2015-12-15';
181181

0 commit comments

Comments
 (0)