@@ -18,23 +18,35 @@ CREATE OR REPLACE FUNCTION @extschema@.create_hash_partitions(
18
18
) RETURNSINTEGER AS
19
19
$$
20
20
DECLARE
21
- v_relnameTEXT ;
21
+ v_relnameTEXT ;
22
22
v_child_relnameTEXT ;
23
- v_typeTEXT ;
23
+ v_typeTEXT ;
24
+ v_plain_schemaTEXT ;
25
+ v_plain_relnameTEXT ;
26
+ v_hashfuncTEXT ;
24
27
BEGIN
25
28
v_relname := @extschema@.validate_relname(relation);
26
29
attribute := lower (attribute);
27
30
PERFORM @extschema@.common_relation_checks(relation, attribute);
28
31
29
32
v_type := @extschema@.get_attribute_type_name(v_relname, attribute);
30
- IF v_type::regtype!= ' integer' ::regtype THEN
31
- RAISE EXCEPTION' Attribute type must be INTEGER' ;
32
- END IF;
33
+ -- IF v_type::regtype != 'integer'::regtype THEN
34
+ -- RAISE EXCEPTION 'Attribute type must be INTEGER';
35
+ -- END IF;
36
+
37
+ SELECT * INTO v_plain_schema, v_plain_relname
38
+ FROM @extschema@.get_plain_schema_and_relname(relation);
39
+
40
+ v_hashfunc := @extschema@.get_type_hash_func(v_type::regtype::oid )::regproc;
33
41
34
42
/* Create partitions and update pg_pathman configuration*/
35
43
FOR partnumIN 0 ..partitions_count- 1
36
44
LOOP
37
- v_child_relname := @extschema@.get_schema_qualified_name(relation,' .' , suffix := ' _' || partnum);
45
+ -- v_child_relname := @extschema@.get_schema_qualified_name(relation, '.', suffix := '_' || partnum);
46
+ v_child_relname := format(' %s.%s' ,
47
+ v_plain_schema,
48
+ quote_ident(v_plain_relname|| ' _' || partnum));
49
+
38
50
EXECUTE format(' CREATE TABLE %s (LIKE %s INCLUDING ALL)'
39
51
, v_child_relname
40
52
, v_relname);
43
55
, v_child_relname
44
56
, v_relname);
45
57
46
- EXECUTE format(' ALTER TABLE %s ADD CHECK (%s %% %s = %s)'
58
+ EXECUTE format(' ALTER TABLE %s ADD CHECK (@extschema@.get_hash(%s(%s), %s) = %s)'
47
59
, v_child_relname
60
+ , v_hashfunc
48
61
, attribute
49
62
, partitions_count
50
63
, partnum);
@@ -83,7 +96,7 @@ DECLARE
83
96
DECLARE
84
97
hash INTEGER;
85
98
BEGIN
86
- hash := NEW.%s %% %s ;
99
+ hash :=@extschema@.get_hash(%s( NEW.%s), %s) ;
87
100
%s
88
101
RETURN NULL;
89
102
END $body$ LANGUAGE plpgsql;' ;
@@ -93,11 +106,11 @@ DECLARE
93
106
BEFORE INSERT ON %s
94
107
FOR EACH ROW EXECUTE PROCEDURE %s();' ;
95
108
triggernameTEXT ;
96
- -- fields TEXT;
97
- -- fields_format TEXT;
98
109
insert_stmtTEXT ;
99
- relnameTEXT ;
100
- schemaTEXT ;
110
+ relnameTEXT ;
111
+ schemaTEXT ;
112
+ atttypeTEXT ;
113
+ hashfuncTEXT ;
101
114
BEGIN
102
115
/* drop trigger and corresponding function*/
103
116
PERFORM @extschema@.drop_hash_triggers(relation);
@@ -113,7 +126,11 @@ BEGIN
113
126
funcname := schema|| ' .' || quote_ident(format(' %s_insert_trigger_func' , relname));
114
127
triggername := quote_ident(format(' %s_%s_insert_trigger' , schema, relname));
115
128
116
- func := format(func, funcname, attr, partitions_count, insert_stmt);
129
+ /* base hash function for type*/
130
+ atttype := @extschema@.get_attribute_type_name(relation, attr);
131
+ hashfunc := @extschema@.get_type_hash_func(atttype::regtype::oid )::regproc;
132
+
133
+ func := format(func, funcname, hashfunc, attr, partitions_count, insert_stmt);
117
134
trigger := format(trigger, triggername, relation, funcname);
118
135
EXECUTE func;
119
136
EXECUTE trigger;
@@ -197,8 +214,8 @@ DECLARE
197
214
$body$
198
215
DECLARE old_hash INTEGER; new_hash INTEGER; q TEXT;
199
216
BEGIN
200
- old_hash := OLD.%2$s %% % 3$s;
201
- new_hash := NEW.%2$s %% % 3$s;
217
+ old_hash :=@extschema@.get_hash(%9$s( OLD.%2$s), % 3$s) ;
218
+ new_hash :=@extschema@.get_hash(%9$s( NEW.%2$s), % 3$s) ;
202
219
IF old_hash = new_hash THEN RETURN NEW; END IF;
203
220
q := format(' ' DELETE FROM %8$s WHERE %4$s' ' , old_hash);
204
221
EXECUTE q USING %5$s;
@@ -223,6 +240,8 @@ DECLARE
223
240
funcnameTEXT ;
224
241
triggernameTEXT ;
225
242
child_relname_formatTEXT ;
243
+ atttypeTEXT ;
244
+ hashfuncTEXT ;
226
245
BEGIN
227
246
relation := @extschema@.validate_relname(relation);
228
247
@@ -252,9 +271,13 @@ BEGIN
252
271
child_relname_format := plain_schema|| ' .' || quote_ident(plain_relname|| ' _%s' );
253
272
triggername := quote_ident(format(' %s_%s_update_trigger' , plain_schema, plain_relname));
254
273
274
+ /* base hash function for type*/
275
+ atttype := @extschema@.get_attribute_type_name(relation, attr);
276
+ hashfunc := @extschema@.get_type_hash_func(atttype::regtype::oid )::regproc;
277
+
255
278
/* Format function definition and execute it*/
256
279
func := format(func, funcname, attr, partitions_count, att_val_fmt,
257
- old_fields, att_fmt, new_fields, child_relname_format);
280
+ old_fields, att_fmt, new_fields, child_relname_format, hashfunc );
258
281
EXECUTE func;
259
282
260
283
/* Create triggers on child relations*/