1212 * Creates hash partitions for specified relation
1313*/
1414CREATEOR REPLACE FUNCTION @extschema@.create_hash_partitions(
15- relation REGCLASS
16- , attributeTEXT
17- , partitions_countINTEGER
15+ parent_relid REGCLASS,
16+ attributeTEXT ,
17+ partitions_countINTEGER
1818) RETURNSINTEGER AS
1919$$
2020DECLARE
21- v_relname TEXT ;
22- v_child_relname TEXT ;
23- v_type TEXT ;
24- v_plain_schema TEXT ;
25- v_plain_relname TEXT ;
26- v_hashfunc TEXT ;
21+ v_child_relname TEXT ;
22+ v_type TEXT ;
23+ v_plain_schema TEXT ;
24+ v_plain_relname TEXT ;
25+ v_hashfunc TEXT ;
26+
2727BEGIN
28- v_relname : = @extschema@.validate_relname(relation );
28+ PERFORM @extschema@.validate_relname(parent_relid );
2929attribute := lower (attribute);
30- PERFORM @extschema@.common_relation_checks(relation , attribute);
30+ PERFORM @extschema@.common_relation_checks(parent_relid , attribute);
3131
32- v_type := @extschema@.get_attribute_type_name(v_relname , attribute);
32+ v_type := @extschema@.get_attribute_type_name(parent_relid , attribute);
3333
3434SELECT * INTO v_plain_schema, v_plain_relname
35- FROM @extschema@.get_plain_schema_and_relname(relation );
35+ FROM @extschema@.get_plain_schema_and_relname(parent_relid );
3636
37- v_hashfunc := @extschema@.get_type_hash_func(v_type::regtype:: oid )::regproc;
37+ v_hashfunc := @extschema@.get_type_hash_func(v_type::regtype)::regproc;
3838
3939/* Insert new entry to pathman config*/
4040INSERT INTO @extschema@.pathman_config (partrel, attname, parttype)
41- VALUES (relation , attribute,1 );
41+ VALUES (parent_relid , attribute,1 );
4242
4343/* Create partitions and update pg_pathman configuration*/
4444FOR partnumIN 0 ..partitions_count- 1
4545LOOP
4646v_child_relname := format(' %s.%s' ,
47- v_plain_schema,
47+ quote_ident( v_plain_schema) ,
4848 quote_ident(v_plain_relname|| ' _' || partnum));
4949
50- EXECUTE format(' CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s)'
51- , v_child_relname
52- , v_relname);
53-
54- EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s CHECK (@extschema@.get_hash(%s(%s), %s) = %s)'
55- , v_child_relname
56- , @extschema@.build_check_constraint_name(v_child_relname::regclass, attribute)
57- , v_hashfunc
58- , attribute
59- , partitions_count
60- , partnum);
50+ EXECUTE format(' CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s)' ,
51+ v_child_relname,
52+ parent_relid::text );
53+
54+ EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s CHECK (@extschema@.get_hash(%s(%s), %s) = %s)' ,
55+ v_child_relname,
56+ @extschema@.build_check_constraint_name(v_child_relname::regclass,
57+ attribute),
58+ v_hashfunc,
59+ attribute,
60+ partitions_count,
61+ partnum);
6162END LOOP;
6263
6364/* Notify backend about changes*/
64- PERFORM @extschema@.on_create_partitions(relation:: oid );
65+ PERFORM @extschema@.on_create_partitions(parent_relid );
6566
6667/* Copy data*/
67- PERFORM @extschema@.partition_data(relation );
68+ PERFORM @extschema@.partition_data(parent_relid );
6869
6970RETURN partitions_count;
7071END
@@ -75,7 +76,7 @@ SET client_min_messages = WARNING;
7576 * Creates an update trigger
7677*/
7778CREATEOR REPLACE FUNCTION @extschema@.create_hash_update_trigger(
78- IN relation REGCLASS)
79+ parent_relid REGCLASS)
7980RETURNS VOIDAS
8081$$
8182DECLARE
@@ -105,61 +106,62 @@ DECLARE
105106 END $body$
106107 LANGUAGE plpgsql' ;
107108
108- triggerTEXT := ' CREATE TRIGGER %s
109- BEFORE UPDATE ON %s
110- FOR EACH ROW EXECUTE PROCEDURE %s()' ;
111-
112- att_namesTEXT ;
113- old_fieldsTEXT ;
114- new_fieldsTEXT ;
115- att_val_fmtTEXT ;
116- att_fmtTEXT ;
117- relidINTEGER ;
118- partitions_countINTEGER ;
119- attrTEXT ;
120- plain_schemaTEXT ;
121- plain_relnameTEXT ;
122- funcnameTEXT ;
123- triggernameTEXT ;
124- child_relname_formatTEXT ;
125- atttypeTEXT ;
126- hashfuncTEXT ;
109+ triggerTEXT := ' CREATE TRIGGER %s
110+ BEFORE UPDATE ON %s
111+ FOR EACH ROW EXECUTE PROCEDURE %s()' ;
112+
113+ att_namesTEXT ;
114+ old_fieldsTEXT ;
115+ new_fieldsTEXT ;
116+ att_val_fmtTEXT ;
117+ att_fmtTEXT ;
118+ attrTEXT ;
119+ plain_schemaTEXT ;
120+ plain_relnameTEXT ;
121+ funcnameTEXT ;
122+ triggernameTEXT ;
123+ child_relname_formatTEXT ;
124+ atttypeTEXT ;
125+ hashfuncTEXT ;
126+ partitions_countINTEGER ;
127127
128128BEGIN
129129SELECT * INTO plain_schema, plain_relname
130- FROM @extschema@.get_plain_schema_and_relname(relation );
130+ FROM @extschema@.get_plain_schema_and_relname(parent_relid );
131131
132- relid := relation::oid ;
133132SELECT string_agg(attname,' ,' ),
134133 string_agg(' OLD.' || attname,' ,' ),
135134 string_agg(' NEW.' || attname,' ,' ),
136- string_agg(' CASE WHEN NOT $' || attnum|| ' IS NULL THEN' || attname|| ' = $' || attnum||
137- ' ELSE' || attname|| ' IS NULL END' ,' AND' ),
135+ string_agg(' CASE WHEN NOT $' || attnum|| ' IS NULL THEN' ||
136+ attname|| ' = $' || attnum|| ' ' ||
137+ ' ELSE' ||
138+ attname|| ' IS NULL END' ,
139+ ' AND' ),
138140 string_agg(' $' || attnum,' ,' )
139141FROM pg_attribute
140- WHERE attrelid= relid AND attnum> 0
142+ WHERE attrelid= parent_relid AND attnum> 0
141143INTO att_names,
142144 old_fields,
143145 new_fields,
144146 att_val_fmt,
145147 att_fmt;
146148
147- attr := attnameFROM @extschema@.pathman_configWHERE partrel= relation ;
149+ attr := attnameFROM @extschema@.pathman_configWHERE partrel= parent_relid ;
148150
149151IF attr ISNULL THEN
150- RAISE EXCEPTION' Table % is not partitioned' , quote_ident(relation ::TEXT );
152+ RAISE EXCEPTION' Table % is not partitioned' , quote_ident(parent_relid ::TEXT );
151153END IF;
152154
153- partitions_count := COUNT (* )FROM pg_inheritsWHERE inhparent= relation ::oid ;
155+ partitions_count := COUNT (* )FROM pg_inheritsWHERE inhparent= parent_relid ::oid ;
154156
155157/* Function name, trigger name and child relname template*/
156158funcname := plain_schema|| ' .' || quote_ident(format(' %s_update_trigger_func' , plain_relname));
157159child_relname_format := plain_schema|| ' .' || quote_ident(plain_relname|| ' _%s' );
158160triggername := quote_ident(format(' %s_%s_update_trigger' , plain_schema, plain_relname));
159161
160162/* base hash function for type*/
161- atttype := @extschema@.get_attribute_type_name(relation , attr);
162- hashfunc := @extschema@.get_type_hash_func(atttype::regtype:: oid )::regproc;
163+ atttype := @extschema@.get_attribute_type_name(parent_relid , attr);
164+ hashfunc := @extschema@.get_type_hash_func(atttype::regtype)::regproc;
163165
164166/* Format function definition and execute it*/
165167func := format(func, funcname, attr, partitions_count, att_val_fmt,
@@ -169,18 +171,18 @@ BEGIN
169171/* Create triggers on child relations*/
170172FOR numIN 0 ..partitions_count- 1
171173LOOP
172- EXECUTE format(trigger
173- , triggername
174- , format(child_relname_format, num)
175- , funcname);
174+ EXECUTE format(trigger,
175+ triggername,
176+ format(child_relname_format, num),
177+ funcname);
176178END LOOP;
177179END
178180$$ LANGUAGE plpgsql;
179181
180182/*
181183 * Returns hash function OID for specified type
182184*/
183- CREATEOR REPLACE FUNCTION @extschema@.get_type_hash_func(OID )
185+ CREATEOR REPLACE FUNCTION @extschema@.get_type_hash_func(REGTYPE )
184186RETURNSOID AS ' pg_pathman' ,' get_type_hash_func'
185187LANGUAGE C STRICT;
186188