Skip to content

Commit adaa6e3

Browse files
committed
add tablespace support
1 parent 71b348d commit adaa6e3

File tree

6 files changed

+175
-33
lines changed

6 files changed

+175
-33
lines changed

hash.sql

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ DECLARE
2424
v_plain_schema TEXT;
2525
v_plain_relname TEXT;
2626
v_hashfunc TEXT;
27+
v_tablespace TEXT;
2728

2829
BEGIN
2930
IF partition_data = true THEN
@@ -49,16 +50,21 @@ BEGIN
4950
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype)
5051
VALUES (parent_relid, attribute, 1);
5152

53+
/* Determine tablespace of parent table */
54+
v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
55+
5256
/* Create partitions and update pg_pathman configuration */
5357
FOR partnum IN 0..partitions_count-1
5458
LOOP
5559
v_child_relname := format('%s.%s',
5660
quote_ident(v_plain_schema),
5761
quote_ident(v_plain_relname || '_' || partnum));
5862

59-
EXECUTE format('CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s)',
60-
v_child_relname,
61-
parent_relid::TEXT);
63+
EXECUTE format(
64+
'CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s) TABLESPACE %s',
65+
v_child_relname,
66+
parent_relid::TEXT,
67+
v_tablespace);
6268

6369
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s
6470
CHECK (@extschema@.get_hash_part_idx(%s(%s), %s) = %s)',

init.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ CREATE TABLE IF NOT EXISTS @extschema@.pathman_config_params (
4040
CREATE UNIQUE INDEX i_pathman_config_params
4141
ON @extschema@.pathman_config_params(partrel);
4242

43+
CREATE TYPE partition AS (
44+
parent REGCLASS,
45+
parttype INTEGER,
46+
child REGCLASS,
47+
start_value TEXT,
48+
end_value TEXT
49+
);
50+
4351
/*
4452
* Invalidate relcache every time someone changes parameters config.
4553
*/
@@ -695,3 +703,10 @@ LANGUAGE C STRICT;
695703
CREATE OR REPLACE FUNCTION @extschema@.debug_capture()
696704
RETURNS VOID AS 'pg_pathman', 'debug_capture'
697705
LANGUAGE C STRICT;
706+
707+
/*
708+
* Return tablespace name for specified relation
709+
*/
710+
CREATE OR REPLACE FUNCTION @extschema@.get_rel_tablespace_name(relation REGCLASS)
711+
RETURNS TEXT AS 'pg_pathman', 'get_rel_tablespace_name'
712+
LANGUAGE C STRICT;

range.sql

Lines changed: 95 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ DECLARE
9191
v_rows_count INTEGER;
9292
v_max p_start_value%TYPE;
9393
v_cur_value p_start_value%TYPE := p_start_value;
94+
v_tablespace TEXT;
9495
i INTEGER;
95-
9696
BEGIN
9797
IF partition_data = true THEN
9898
/* Acquire data modification lock */
@@ -149,12 +149,20 @@ BEGIN
149149
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
150150
VALUES (parent_relid, p_attribute, 2, p_interval::TEXT);
151151

152+
/* Determine tablespace of parent table */
153+
v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
154+
152155
/* Create first partition */
153156
FOR i IN 1..p_count
154157
LOOP
155-
EXECUTE format('SELECT @extschema@.create_single_range_partition($1, $2, $3::%s)',
156-
pg_typeof(p_start_value))
157-
USING parent_relid, p_start_value, p_start_value + p_interval;
158+
EXECUTE
159+
format('SELECT @extschema@.create_single_range_partition($1, $2, $3::%s, tablespace:=$4)',
160+
pg_typeof(p_start_value))
161+
USING
162+
parent_relid,
163+
p_start_value,
164+
p_start_value + p_interval,
165+
v_tablespace;
158166

159167
p_start_value := p_start_value + p_interval;
160168
END LOOP;
@@ -190,6 +198,7 @@ DECLARE
190198
v_rows_count INTEGER;
191199
v_max p_start_value%TYPE;
192200
v_cur_value p_start_value%TYPE := p_start_value;
201+
v_tablespace TEXT;
193202
i INTEGER;
194203

195204
BEGIN
@@ -250,12 +259,18 @@ BEGIN
250259
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
251260
VALUES (parent_relid, p_attribute, 2, p_interval::TEXT);
252261

262+
/* Determine tablespace of parent table */
263+
v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
264+
253265
/* create first partition */
254266
FOR i IN 1..p_count
255267
LOOP
256-
PERFORM @extschema@.create_single_range_partition(parent_relid,
257-
p_start_value,
258-
p_start_value + p_interval);
268+
PERFORM @extschema@.create_single_range_partition(
269+
parent_relid,
270+
p_start_value,
271+
p_start_value + p_interval,
272+
tablespace := v_tablespace);
273+
259274
p_start_value := p_start_value + p_interval;
260275
END LOOP;
261276

@@ -288,6 +303,7 @@ RETURNS INTEGER AS
288303
$$
289304
DECLARE
290305
part_count INTEGER := 0;
306+
v_tablespace TEXT;
291307

292308
BEGIN
293309
IF partition_data = true THEN
@@ -320,11 +336,17 @@ BEGIN
320336
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
321337
VALUES (parent_relid, p_attribute, 2, p_interval::TEXT);
322338

339+
/* Determine tablespace of parent table */
340+
v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
341+
323342
WHILE p_start_value <= p_end_value
324343
LOOP
325-
PERFORM @extschema@.create_single_range_partition(parent_relid,
326-
p_start_value,
327-
p_start_value + p_interval);
344+
PERFORM @extschema@.create_single_range_partition(
345+
parent_relid,
346+
p_start_value,
347+
p_start_value + p_interval,
348+
tablespace := v_tablespace);
349+
328350
p_start_value := p_start_value + p_interval;
329351
part_count := part_count + 1;
330352
END LOOP;
@@ -358,6 +380,7 @@ RETURNS INTEGER AS
358380
$$
359381
DECLARE
360382
part_count INTEGER := 0;
383+
v_tablespace TEXT;
361384

362385
BEGIN
363386
IF partition_data = true THEN
@@ -386,11 +409,19 @@ BEGIN
386409
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
387410
VALUES (parent_relid, p_attribute, 2, p_interval::TEXT);
388411

412+
/* Determine tablespace of parent table */
413+
v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
414+
389415
WHILE p_start_value <= p_end_value
390416
LOOP
391-
EXECUTE format('SELECT @extschema@.create_single_range_partition($1, $2, $3::%s);',
392-
pg_typeof(p_start_value))
393-
USING parent_relid, p_start_value, p_start_value + p_interval;
417+
EXECUTE
418+
format('SELECT @extschema@.create_single_range_partition($1, $2, $3::%s, tablespace:=$4);',
419+
pg_typeof(p_start_value))
420+
USING
421+
parent_relid,
422+
p_start_value,
423+
p_start_value + p_interval,
424+
v_tablespace;
394425

395426
p_start_value := p_start_value + p_interval;
396427
part_count := part_count + 1;
@@ -419,7 +450,8 @@ CREATE OR REPLACE FUNCTION @extschema@.create_single_range_partition(
419450
parent_relid REGCLASS,
420451
p_start_value ANYELEMENT,
421452
p_end_value ANYELEMENT,
422-
partition_name TEXT DEFAULT NULL)
453+
partition_name TEXT DEFAULT NULL,
454+
tablespace TEXT DEFAULT NULL)
423455
RETURNS TEXT AS
424456
$$
425457
DECLARE
@@ -431,7 +463,7 @@ DECLARE
431463
v_plain_relname TEXT;
432464
v_child_relname_exists BOOL;
433465
v_seq_name TEXT;
434-
466+
v_create_table_query TEXT;
435467
BEGIN
436468
v_attname := attname FROM @extschema@.pathman_config
437469
WHERE partrel = parent_relid;
@@ -466,7 +498,15 @@ BEGIN
466498
v_child_relname := partition_name;
467499
END IF;
468500

469-
EXECUTE format('CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s)',
501+
v_create_table_query := 'CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s)';
502+
503+
/* If tablespace is specified then add it to a create query */
504+
if NOT tablespace IS NULL THEN
505+
v_create_table_query := v_create_table_query || ' TABLESPACE ' ||tablespace;
506+
END IF;
507+
RAISE NOTICE 'query: %', v_create_table_query;
508+
509+
EXECUTE format(v_create_table_query,
470510
v_child_relname,
471511
parent_relid::TEXT);
472512

@@ -708,7 +748,8 @@ $$ LANGUAGE plpgsql;
708748
*/
709749
CREATE OR REPLACE FUNCTION @extschema@.append_range_partition(
710750
parent_relid REGCLASS,
711-
partition_name TEXT DEFAULT NULL)
751+
partition_name TEXT DEFAULT NULL,
752+
tablespace TEXT DEFAULT NULL)
712753
RETURNS TEXT AS
713754
$$
714755
DECLARE
@@ -734,13 +775,14 @@ BEGIN
734775

735776
EXECUTE
736777
format(
737-
'SELECT @extschema@.append_partition_internal($1, $2, $3, ARRAY[]::%s[], $4)',
778+
'SELECT @extschema@.append_partition_internal($1, $2, $3, ARRAY[]::%s[], $4, $5)',
738779
v_atttype)
739780
USING
740781
parent_relid,
741782
v_atttype,
742783
v_interval,
743-
partition_name
784+
partition_name,
785+
tablespace
744786
INTO
745787
v_part_name;
746788

@@ -762,7 +804,8 @@ CREATE OR REPLACE FUNCTION @extschema@.append_partition_internal(
762804
p_atttype TEXT,
763805
p_interval TEXT,
764806
p_range ANYARRAY DEFAULT NULL,
765-
partition_name TEXT DEFAULT NULL)
807+
partition_name TEXT DEFAULT NULL,
808+
tablespace TEXT DEFAULT NULL)
766809
RETURNS TEXT AS
767810
$$
768811
DECLARE
@@ -773,6 +816,11 @@ BEGIN
773816
RAISE EXCEPTION 'Cannot append to empty partitions set';
774817
END IF;
775818

819+
/* If tablespace isn't specified then choose parent's tablespace */
820+
IF tablespace IS NULL THEN
821+
tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
822+
END IF;
823+
776824
p_range := @extschema@.get_range_by_idx(parent_relid, -1, 0);
777825

778826
IF @extschema@.is_date_type(p_atttype::regtype) THEN
@@ -784,13 +832,14 @@ BEGIN
784832
ELSE
785833
EXECUTE
786834
format(
787-
'SELECT @extschema@.create_single_range_partition($1, $2, $2 + $3::%s, $4)',
835+
'SELECT @extschema@.create_single_range_partition($1, $2, $2 + $3::%s, $4, $5)',
788836
p_atttype)
789837
USING
790838
parent_relid,
791839
p_range[2],
792840
p_interval,
793-
partition_name
841+
partition_name,
842+
tablespace
794843
INTO
795844
v_part_name;
796845
END IF;
@@ -806,7 +855,8 @@ LANGUAGE plpgsql;
806855
*/
807856
CREATE OR REPLACE FUNCTION @extschema@.prepend_range_partition(
808857
parent_relid REGCLASS,
809-
partition_name TEXT DEFAULT NULL)
858+
partition_name TEXT DEFAULT NULL,
859+
tablespace TEXT DEFAULT NULL)
810860
RETURNS TEXT AS
811861
$$
812862
DECLARE
@@ -829,13 +879,14 @@ BEGIN
829879

830880
EXECUTE
831881
format(
832-
'SELECT @extschema@.prepend_partition_internal($1, $2, $3, ARRAY[]::%s[], $4)',
882+
'SELECT @extschema@.prepend_partition_internal($1, $2, $3, ARRAY[]::%s[], $4, $5)',
833883
v_atttype)
834884
USING
835885
parent_relid,
836886
v_atttype,
837887
v_interval,
838-
partition_name
888+
partition_name,
889+
tablespace
839890
INTO
840891
v_part_name;
841892

@@ -857,7 +908,8 @@ CREATE OR REPLACE FUNCTION @extschema@.prepend_partition_internal(
857908
p_atttype TEXT,
858909
p_interval TEXT,
859910
p_range ANYARRAY DEFAULT NULL,
860-
partition_name TEXT DEFAULT NULL)
911+
partition_name TEXT DEFAULT NULL,
912+
tablespace TEXT DEFAULT NULL)
861913
RETURNS TEXT AS
862914
$$
863915
DECLARE
@@ -868,6 +920,11 @@ BEGIN
868920
RAISE EXCEPTION 'Cannot prepend to empty partitions set';
869921
END IF;
870922

923+
/* If tablespace isn't specified then choose parent's tablespace */
924+
IF tablespace IS NULL THEN
925+
tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
926+
END IF;
927+
871928
p_range := @extschema@.get_range_by_idx(parent_relid, 0, 0);
872929

873930
IF @extschema@.is_date_type(p_atttype::regtype) THEN
@@ -879,13 +936,14 @@ BEGIN
879936
ELSE
880937
EXECUTE
881938
format(
882-
'SELECT @extschema@.create_single_range_partition($1, $2 - $3::%s, $2, $4)',
939+
'SELECT @extschema@.create_single_range_partition($1, $2 - $3::%s, $2, $4, $5)',
883940
p_atttype)
884941
USING
885942
parent_relid,
886943
p_range[1],
887944
p_interval,
888-
partition_name
945+
partition_name,
946+
tablespace
889947
INTO
890948
v_part_name;
891949
END IF;
@@ -903,7 +961,8 @@ CREATE OR REPLACE FUNCTION @extschema@.add_range_partition(
903961
parent_relid REGCLASS,
904962
p_start_value ANYELEMENT,
905963
p_end_value ANYELEMENT,
906-
partition_name TEXT DEFAULT NULL)
964+
partition_name TEXT DEFAULT NULL,
965+
tablespace TEXT DEFAULT NULL)
907966
RETURNS TEXT AS
908967
$$
909968
DECLARE
@@ -923,11 +982,17 @@ BEGIN
923982
RAISE EXCEPTION 'Specified range overlaps with existing partitions';
924983
END IF;
925984

985+
/* If tablespace isn't specified then choose parent's tablespace */
986+
IF tablespace IS NULL THEN
987+
tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
988+
END IF;
989+
926990
/* Create new partition */
927991
v_part_name := @extschema@.create_single_range_partition(parent_relid,
928992
p_start_value,
929993
p_end_value,
930-
partition_name);
994+
partition_name,
995+
tablespace);
931996
PERFORM @extschema@.on_update_partitions(parent_relid);
932997

933998
RETURN v_part_name;

0 commit comments

Comments
 (0)