@@ -18,14 +18,6 @@ CREATE OR REPLACE FUNCTION @extschema@.create_hash_partitions(
18
18
partition_dataBOOLEAN DEFAULT TRUE)
19
19
RETURNSINTEGER AS
20
20
$$
21
- DECLARE
22
- v_child_relnameTEXT ;
23
- v_plain_schemaTEXT ;
24
- v_plain_relnameTEXT ;
25
- -- v_atttypeREGTYPE;
26
- -- v_hashfuncREGPROC;
27
- v_init_callbackREGPROCEDURE;
28
-
29
21
BEGIN
30
22
PERFORM @extschema@.validate_relname(parent_relid);
31
23
40
32
attribute := lower (attribute);
41
33
PERFORM @extschema@.common_relation_checks(parent_relid, attribute);
42
34
43
- /* Fetch atttype and its hash function*/
44
- -- v_atttype := @extschema@.get_attribute_type(parent_relid, attribute);
45
- -- v_hashfunc := @extschema@.get_type_hash_func(v_atttype);
46
-
47
- SELECT * INTO v_plain_schema, v_plain_relname
48
- FROM @extschema@.get_plain_schema_and_relname(parent_relid);
49
-
50
35
/* Insert new entry to pathman config*/
51
36
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype)
52
37
VALUES (parent_relid, attribute,1 );
@@ -82,21 +67,26 @@ CREATE OR REPLACE FUNCTION @extschema@.replace_hash_partition(
82
67
RETURNS REGCLASSAS
83
68
$$
84
69
DECLARE
85
- v_attnameTEXT ;
70
+ parent_relidREGCLASS;
71
+ part_attnameTEXT ;/* partitioned column*/
72
+ old_constr_nameTEXT ;/* name of old_partition's constraint*/
73
+ old_constr_defTEXT ;/* definition of old_partition's constraint*/
86
74
rel_persistenceCHAR ;
87
- v_init_callbackREGPROCEDURE;
88
- v_parent_relidREGCLASS;
89
- v_part_countINT ;
90
- v_part_numINT ;
75
+ p_init_callbackREGPROCEDURE;
76
+
91
77
BEGIN
92
78
PERFORM @extschema@.validate_relname(old_partition);
93
79
PERFORM @extschema@.validate_relname(new_partition);
94
80
95
81
/* Parent relation*/
96
- v_parent_relid := @extschema@.get_parent_of_partition(old_partition);
82
+ parent_relid := @extschema@.get_parent_of_partition(old_partition);
97
83
98
84
/* Acquire lock on parent*/
99
- PERFORM @extschema@.lock_partitioned_relation(v_parent_relid);
85
+ PERFORM @extschema@.lock_partitioned_relation(parent_relid);
86
+
87
+ /* Acquire data modification lock (prevent further modifications)*/
88
+ PERFORM @extschema@.prevent_relation_modification(old_partition);
89
+ PERFORM @extschema@.prevent_relation_modification(new_partition);
100
90
101
91
/* Ignore temporary tables*/
102
92
SELECT relpersistenceFROM pg_catalog .pg_class
@@ -108,52 +98,54 @@ BEGIN
108
98
END IF;
109
99
110
100
/* Check that new partition has an equal structure as parent does*/
111
- IF NOT @extschema@.validate_relations_equality(v_parent_relid , new_partition) THEN
101
+ IF NOT @extschema@.validate_relations_equality(parent_relid , new_partition) THEN
112
102
RAISE EXCEPTION' partition must have the exact same structure as parent' ;
113
103
END IF;
114
104
115
105
/* Get partitioning key*/
116
- v_attname := attnameFROM @extschema@.pathman_configWHERE partrel= v_parent_relid ;
117
- IFv_attname ISNULL THEN
118
- RAISE EXCEPTION' table "%" is not partitioned' ,v_parent_relid ::TEXT ;
106
+ part_attname := attnameFROM @extschema@.pathman_configWHERE partrel= parent_relid ;
107
+ IFpart_attname ISNULL THEN
108
+ RAISE EXCEPTION' table "%" is not partitioned' ,parent_relid ::TEXT ;
119
109
END IF;
120
110
121
- /* Calculate partitions count and old partition's number*/
122
- v_part_count := count (* )FROM @extschema@.pathman_partition_listWHERE parent= v_parent_relid;
123
- v_part_num := @extschema@.get_partition_hash(v_parent_relid, old_partition);
111
+ /* Fetch name of old_partition's HASH constraint*/
112
+ old_constr_name= @extschema@.build_check_constraint_name(old_partition::REGCLASS,
113
+ part_attname);
114
+
115
+ /* Fetch definition of old_partition's HASH constraint*/
116
+ SELECT pg_catalog .pg_get_constraintdef (oid )FROM pg_catalog .pg_constraint
117
+ WHERE conrelid= old_partitionAND conname= old_constr_name
118
+ INTO old_constr_def;
124
119
125
120
/* Detach old partition*/
126
- EXECUTE format(' ALTER TABLE %s NO INHERIT %s' , old_partition, v_parent_relid);
127
- EXECUTE format(' ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s' ,
128
- old_partition,
129
- @extschema@.build_check_constraint_name(old_partition::REGCLASS,
130
- v_attname));
131
-
132
- /* Attach new one*/
133
- EXECUTE format(' ALTER TABLE %s INHERIT %s' , new_partition, v_parent_relid);
134
- EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)' ,
121
+ EXECUTE format(' ALTER TABLE %s NO INHERIT %s' , old_partition, parent_relid);
122
+ EXECUTE format(' ALTER TABLE %s DROP CONSTRAINT %s' ,
123
+ old_partition,
124
+ old_constr_name);
125
+
126
+ /* Attach the new one*/
127
+ EXECUTE format(' ALTER TABLE %s INHERIT %s' , new_partition, parent_relid);
128
+ EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s %s' ,
135
129
new_partition,
136
- @extschema@.build_check_constraint_name(new_partition::regclass,
137
- v_attname),
138
- @extschema@.build_hash_condition(new_partition::regclass,
139
- v_attname,
140
- v_part_count,
141
- v_part_num));
130
+ @extschema@.build_check_constraint_name(new_partition::REGCLASS,
131
+ part_attname),
132
+ old_constr_def);
142
133
143
134
/* Fetch init_callback from 'params' table*/
144
135
WITH stub_callback(stub)as (values (0 ))
145
136
SELECT coalesce(init_callback,0 ::REGPROCEDURE)
146
137
FROM stub_callback
147
138
LEFT JOIN @extschema@.pathman_config_paramsAS params
148
- ON params .partrel = v_parent_relid
149
- INTOv_init_callback ;
139
+ ON params .partrel = parent_relid
140
+ INTOp_init_callback ;
150
141
151
- PERFORM @extschema@.invoke_on_partition_created_callback(v_parent_relid,
142
+ /* Finally invoke init_callback*/
143
+ PERFORM @extschema@.invoke_on_partition_created_callback(parent_relid,
152
144
new_partition,
153
- v_init_callback );
145
+ p_init_callback );
154
146
155
147
/* Invalidate cache*/
156
- PERFORM @extschema@.on_update_partitions(v_parent_relid );
148
+ PERFORM @extschema@.on_update_partitions(parent_relid );
157
149
158
150
RETURN new_partition;
159
151
END
@@ -292,3 +284,14 @@ LANGUAGE C STRICT;
292
284
CREATEOR REPLACE FUNCTION @extschema@.get_hash_part_idx(INTEGER ,INTEGER )
293
285
RETURNSINTEGER AS ' pg_pathman' ,' get_hash_part_idx'
294
286
LANGUAGE C STRICT;
287
+
288
+ /*
289
+ * Build hash condition for a CHECK CONSTRAINT
290
+ */
291
+ CREATEOR REPLACE FUNCTION @extschema@.build_hash_condition(
292
+ parent_relidREGCLASS,
293
+ attributeTEXT ,
294
+ part_countINT4,
295
+ part_idxINT4)
296
+ RETURNSTEXT AS ' pg_pathman' ,' build_hash_condition'
297
+ LANGUAGE C STRICT;