Skip to content

Commit a400a91

Browse files
committed
pathman: option allowing to drop data with partitions
1 parent 5381eb3 commit a400a91

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

contrib/pg_pathman/expected/pg_pathman.out

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -688,17 +688,7 @@ NOTICE: Copying data to partitions...
688688
10
689689
(1 row)
690690

691-
SELECT drop_range_partitions('range_rel');
692-
NOTICE: 0 rows copied from range_rel_10
693-
NOTICE: 0 rows copied from range_rel_9
694-
NOTICE: 0 rows copied from range_rel_8
695-
NOTICE: 0 rows copied from range_rel_7
696-
NOTICE: 0 rows copied from range_rel_6
697-
NOTICE: 0 rows copied from range_rel_5
698-
NOTICE: 65 rows copied from range_rel_4
699-
NOTICE: 100 rows copied from range_rel_3
700-
NOTICE: 100 rows copied from range_rel_2
701-
NOTICE: 100 rows copied from range_rel_1
691+
SELECT drop_range_partitions('range_rel', TRUE);
702692
drop_range_partitions
703693
-----------------------
704694
10

contrib/pg_pathman/range.sql

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,11 @@ $$ LANGUAGE plpgsql;
886886

887887
/*
888888
* Drop partitions
889+
* If delete_data set to TRUE then partitions will be dropped with all the data
889890
*/
890-
CREATE OR REPLACE FUNCTION @extschema@.drop_range_partitions(IN relation TEXT)
891+
CREATE OR REPLACE FUNCTION @extschema@.drop_range_partitions(
892+
relation TEXT,
893+
delete_data BOOLEAN DEFAULT FALSE)
891894
RETURNS INTEGER AS
892895
$$
893896
DECLARE
@@ -904,13 +907,15 @@ BEGIN
904907
FOR v_rec IN (SELECT inhrelid::regclass::text AS tbl
905908
FROM pg_inherits WHERE inhparent = relation::regclass::oid)
906909
LOOP
907-
EXECUTE format('WITH part_data AS (DELETE FROM %s RETURNING *)
908-
INSERT INTO %s SELECT * FROM part_data'
909-
, v_rec.tbl
910-
, relation);
911-
GET DIAGNOSTICS v_rows = ROW_COUNT;
910+
IF NOT delete_data THEN
911+
EXECUTE format('WITH part_data AS (DELETE FROM %s RETURNING *)
912+
INSERT INTO %s SELECT * FROM part_data'
913+
, v_rec.tbl
914+
, relation);
915+
GET DIAGNOSTICS v_rows = ROW_COUNT;
916+
RAISE NOTICE '% rows copied from %', v_rows, v_rec.tbl;
917+
END IF;
912918
EXECUTE format('DROP TABLE %s', v_rec.tbl);
913-
RAISE NOTICE '% rows copied from %', v_rows, v_rec.tbl;
914919
v_part_count := v_part_count + 1;
915920
END LOOP;
916921

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)