@@ -18,23 +18,35 @@ CREATE OR REPLACE FUNCTION @extschema@.create_hash_partitions(
1818) RETURNS INTEGER AS
1919$$
2020DECLARE
21- v_relname TEXT ;
21+ v_relname TEXT ;
2222 v_child_relname TEXT ;
23- v_type TEXT ;
23+ v_type TEXT ;
24+ v_plain_schema TEXT ;
25+ v_plain_relname TEXT ;
26+ v_hashfunc TEXT ;
2427BEGIN
2528 v_relname := @extschema@.validate_relname(relation);
2629 attribute := lower (attribute);
2730 PERFORM @extschema@.common_relation_checks(relation, attribute);
2831
2932 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;
3341
3442 /* Create partitions and update pg_pathman configuration */
3543 FOR partnum IN 0 ..partitions_count- 1
3644 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+
3850 EXECUTE format(' CREATE TABLE %s (LIKE %s INCLUDING ALL)'
3951 , v_child_relname
4052 , v_relname);
4355 , v_child_relname
4456 , v_relname);
4557
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)'
4759 , v_child_relname
60+ , v_hashfunc
4861 , attribute
4962 , partitions_count
5063 , partnum);
@@ -83,7 +96,7 @@ DECLARE
8396 DECLARE
8497 hash INTEGER;
8598 BEGIN
86- hash := NEW.%s %% %s ;
99+ hash := @extschema@.get_hash(%s( NEW.%s), %s) ;
87100 %s
88101 RETURN NULL;
89102 END $body$ LANGUAGE plpgsql;' ;
@@ -93,11 +106,11 @@ DECLARE
93106 BEFORE INSERT ON %s
94107 FOR EACH ROW EXECUTE PROCEDURE %s();' ;
95108 triggername TEXT ;
96- -- fields TEXT;
97- -- fields_format TEXT;
98109 insert_stmt TEXT ;
99- relname TEXT ;
100- schema TEXT ;
110+ relname TEXT ;
111+ schema TEXT ;
112+ atttype TEXT ;
113+ hashfunc TEXT ;
101114BEGIN
102115 /* drop trigger and corresponding function */
103116 PERFORM @extschema@.drop_hash_triggers(relation);
@@ -113,7 +126,11 @@ BEGIN
113126 funcname := schema || ' .' || quote_ident(format(' %s_insert_trigger_func' , relname));
114127 triggername := quote_ident(format(' %s_%s_insert_trigger' , schema, relname));
115128
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);
117134 trigger := format(trigger, triggername, relation, funcname);
118135 EXECUTE func;
119136 EXECUTE trigger;
@@ -197,8 +214,8 @@ DECLARE
197214 $body$
198215 DECLARE old_hash INTEGER; new_hash INTEGER; q TEXT;
199216 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) ;
202219 IF old_hash = new_hash THEN RETURN NEW; END IF;
203220 q := format(' ' DELETE FROM %8$s WHERE %4$s' ' , old_hash);
204221 EXECUTE q USING %5$s;
@@ -223,6 +240,8 @@ DECLARE
223240 funcname TEXT ;
224241 triggername TEXT ;
225242 child_relname_format TEXT ;
243+ atttype TEXT ;
244+ hashfunc TEXT ;
226245BEGIN
227246 relation := @extschema@.validate_relname(relation);
228247
@@ -252,9 +271,13 @@ BEGIN
252271 child_relname_format := plain_schema || ' .' || quote_ident(plain_relname || ' _%s' );
253272 triggername := quote_ident(format(' %s_%s_update_trigger' , plain_schema, plain_relname));
254273
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+
255278 /* Format function definition and execute it */
256279 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 );
258281 EXECUTE func;
259282
260283 /* Create triggers on child relations */
0 commit comments