Skip to content

Commit c3e8ed8

Browse files
committed
reimplement HASH partition creation machinery in C language
1 parent 7d81364 commit c3e8ed8

File tree

9 files changed

+404
-109
lines changed

9 files changed

+404
-109
lines changed

expected/pathman_calamity.out

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ SHOW pg_pathman.enable;
293293
on
294294
(1 row)
295295

296+
/* check function create_hash_partitions_internal() (called for the 2nd time) */
297+
CREATE TABLE calamity.hash_two_times(val serial);
298+
SELECT create_hash_partitions('calamity.hash_two_times', 'val', 2);
299+
create_hash_partitions
300+
------------------------
301+
2
302+
(1 row)
303+
304+
SELECT create_hash_partitions_internal('calamity.hash_two_times', 'val', 2);
305+
ERROR: cannot add new HASH partitions
296306
DROP SCHEMA calamity CASCADE;
297-
NOTICE: drop cascades to 8 other objects
307+
NOTICE: drop cascades to 11 other objects
298308
DROP EXTENSION pg_pathman;

hash.sql

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,43 +51,10 @@ BEGIN
5151
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype)
5252
VALUES (parent_relid, attribute, 1);
5353

54-
/* Create partitions and update pg_pathman configuration */
55-
FOR partnum IN 0..partitions_count-1
56-
LOOP
57-
v_child_relname := format('%s.%s',
58-
quote_ident(v_plain_schema),
59-
quote_ident(v_plain_relname || '_' || partnum));
60-
61-
EXECUTE format(
62-
'CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s) TABLESPACE %s',
63-
v_child_relname,
64-
parent_relid::TEXT,
65-
@extschema@.get_rel_tablespace_name(parent_relid));
66-
67-
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s
68-
CHECK (@extschema@.get_hash_part_idx(%s(%s), %s) = %s)',
69-
v_child_relname,
70-
@extschema@.build_check_constraint_name(v_child_relname::REGCLASS,
71-
attribute),
72-
v_hashfunc::TEXT,
73-
attribute,
74-
partitions_count,
75-
partnum);
76-
77-
PERFORM @extschema@.copy_foreign_keys(parent_relid, v_child_relname::REGCLASS);
78-
79-
/* Fetch init_callback from 'params' table */
80-
WITH stub_callback(stub) as (values (0))
81-
SELECT coalesce(init_callback, 0::REGPROCEDURE)
82-
FROM stub_callback
83-
LEFT JOIN @extschema@.pathman_config_params AS params
84-
ON params.partrel = parent_relid
85-
INTO v_init_callback;
86-
87-
PERFORM @extschema@.invoke_on_partition_created_callback(parent_relid,
88-
v_child_relname::REGCLASS,
89-
v_init_callback);
90-
END LOOP;
54+
/* Create partitions */
55+
PERFORM @extschema@.create_hash_partitions_internal(parent_relid,
56+
attribute,
57+
partitions_count);
9158

9259
/* Notify backend about changes */
9360
PERFORM @extschema@.on_create_partitions(parent_relid);
@@ -202,7 +169,7 @@ BEGIN
202169
old_fields, att_fmt, new_fields, child_relname_format,
203170
@extschema@.get_type_hash_func(atttype)::TEXT);
204171

205-
/* Create trigger on every partition */
172+
/* Create trigger on each partition */
206173
FOR num IN 0..partitions_count-1
207174
LOOP
208175
EXECUTE format(trigger,
@@ -215,6 +182,16 @@ BEGIN
215182
END
216183
$$ LANGUAGE plpgsql;
217184

185+
/*
186+
* Just create HASH partitions, called by create_hash_partitions().
187+
*/
188+
CREATE OR REPLACE FUNCTION @extschema@.create_hash_partitions_internal(
189+
parent_relid REGCLASS,
190+
attribute TEXT,
191+
partitions_count INTEGER)
192+
RETURNS VOID AS 'pg_pathman', 'create_hash_partitions_internal'
193+
LANGUAGE C STRICT;
194+
218195
/*
219196
* Returns hash function OID for specified type
220197
*/

sql/pathman_calamity.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ ALTER TABLE calamity.wrong_partition DROP CONSTRAINT pathman_wrong_partition_1_c
109109
/* check GUC variable */
110110
SHOW pg_pathman.enable;
111111

112+
/* check function create_hash_partitions_internal() (called for the 2nd time) */
113+
CREATE TABLE calamity.hash_two_times(val serial);
114+
SELECT create_hash_partitions('calamity.hash_two_times', 'val', 2);
115+
SELECT create_hash_partitions_internal('calamity.hash_two_times', 'val', 2);
116+
112117

113118
DROP SCHEMA calamity CASCADE;
114119
DROP EXTENSION pg_pathman;

0 commit comments

Comments
 (0)