Skip to content

Commit 5dc7251

Browse files
committed
improve function create_hash_partitions(), add calamity tests
1 parent c0a1bb8 commit 5dc7251

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

expected/pathman_calamity.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ SELECT count(*) FROM calamity.part_test;
100100
(1 row)
101101

102102
DELETE FROM calamity.part_test;
103+
/* test function create_hash_partitions() */
104+
SELECT create_hash_partitions('calamity.part_test', 'val', 2,
105+
relnames := ARRAY['calamity.p1']::TEXT[]);
106+
ERROR: size of array 'relnames' must be equal to 'partitions_count'
107+
SELECT create_hash_partitions('calamity.part_test', 'val', 2,
108+
tablespaces := ARRAY['abcd']::TEXT[]);
109+
ERROR: size of array 'tablespaces' must be equal to 'partitions_count'
103110
/* test stub 'enable_parent' value for PATHMAN_CONFIG_PARAMS */
104111
INSERT INTO calamity.part_test SELECT generate_series(1, 30);
105112
SELECT create_range_partitions('calamity.part_test', 'val', 1, 10);

hash.sql

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ BEGIN
3838
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype)
3939
VALUES (parent_relid, attribute, 1);
4040

41-
IF array_length(relnames, 1) != partitions_count THEN
42-
RAISE EXCEPTION 'Partition names array size must be equal the partitions count';
43-
END IF;
44-
45-
IF array_length(tablespaces, 1) != partitions_count THEN
46-
RAISE EXCEPTION 'Partition tablespaces array size must be equal the partitions count';
47-
END IF;
48-
4941
/* Create partitions */
5042
PERFORM @extschema@.create_hash_partitions_internal(parent_relid,
5143
attribute,

sql/pathman_calamity.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ SELECT count(*) FROM calamity.part_test;
3535
DELETE FROM calamity.part_test;
3636

3737

38+
/* test function create_hash_partitions() */
39+
SELECT create_hash_partitions('calamity.part_test', 'val', 2,
40+
relnames := ARRAY['calamity.p1']::TEXT[]);
41+
SELECT create_hash_partitions('calamity.part_test', 'val', 2,
42+
tablespaces := ARRAY['abcd']::TEXT[]);
43+
44+
3845
/* test stub 'enable_parent' value for PATHMAN_CONFIG_PARAMS */
3946
INSERT INTO calamity.part_test SELECT generate_series(1, 30);
4047
SELECT create_range_partitions('calamity.part_test', 'val', 1, 10);

src/pl_hash_funcs.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ create_hash_partitions_internal(PG_FUNCTION_ARGS)
5454
Oid parent_relid = PG_GETARG_OID(0);
5555
const char *partitioned_col_name = TextDatumGetCString(PG_GETARG_DATUM(1));
5656
Oid partitioned_col_type;
57-
uint32 part_count = PG_GETARG_INT32(2),
57+
uint32 partitions_count = PG_GETARG_INT32(2),
5858
i;
5959

6060
/* Partition names and tablespaces */
@@ -84,6 +84,14 @@ create_hash_partitions_internal(PG_FUNCTION_ARGS)
8484
if (relnames && tablespaces && relnames_size != tablespaces_size)
8585
elog(ERROR, "sizes of arrays 'relnames' and 'tablespaces' are different");
8686

87+
/* Validate size of 'relnames' */
88+
if (relnames && relnames_size != partitions_count)
89+
elog(ERROR, "size of array 'relnames' must be equal to 'partitions_count'");
90+
91+
/* Validate size of 'tablespaces' */
92+
if (tablespaces && tablespaces_size != partitions_count)
93+
elog(ERROR, "size of array 'tablespaces' must be equal to 'partitions_count'");
94+
8795
/* Convert partition names into RangeVars */
8896
if (relnames)
8997
{
@@ -97,13 +105,13 @@ create_hash_partitions_internal(PG_FUNCTION_ARGS)
97105
}
98106

99107
/* Finally create HASH partitions */
100-
for (i = 0; i < part_count; i++)
108+
for (i = 0; i < partitions_count; i++)
101109
{
102110
RangeVar *partition_rv = rangevars ? rangevars[i] : NULL;
103111
char *tablespace = tablespaces ? tablespaces[i] : NULL;
104112

105113
/* Create a partition (copy FKs, invoke callbacks etc) */
106-
create_single_hash_partition_internal(parent_relid, i, part_count,
114+
create_single_hash_partition_internal(parent_relid, i, partitions_count,
107115
partitioned_col_type,
108116
partition_rv, tablespace);
109117
}

0 commit comments

Comments
 (0)