1212 * Creates hash partitions for specified relation
1313 */
1414CREATE OR REPLACE FUNCTION @extschema@.create_hash_partitions(
15- relation REGCLASS
16- , attribute TEXT
17- , partitions_count INTEGER
15+ parent_relid REGCLASS,
16+ attribute TEXT ,
17+ partitions_count INTEGER
1818) RETURNS INTEGER 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 );
2929 attribute := 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
3434 SELECT * 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 */
4040 INSERT 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 */
4444 FOR partnum IN 0 ..partitions_count- 1
4545 LOOP
4646 v_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);
6162 END 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
6970 RETURN partitions_count;
7071END
@@ -75,7 +76,7 @@ SET client_min_messages = WARNING;
7576 * Creates an update trigger
7677 */
7778CREATE OR REPLACE FUNCTION @extschema@.create_hash_update_trigger(
78- IN relation REGCLASS)
79+ parent_relid REGCLASS)
7980RETURNS VOID AS
8081$$
8182DECLARE
@@ -105,61 +106,62 @@ DECLARE
105106 END $body$
106107 LANGUAGE plpgsql' ;
107108
108- trigger TEXT := ' CREATE TRIGGER %s
109- BEFORE UPDATE ON %s
110- FOR EACH ROW EXECUTE PROCEDURE %s()' ;
111-
112- att_names TEXT ;
113- old_fields TEXT ;
114- new_fields TEXT ;
115- att_val_fmt TEXT ;
116- att_fmt TEXT ;
117- relid INTEGER ;
118- partitions_count INTEGER ;
119- attr TEXT ;
120- plain_schema TEXT ;
121- plain_relname TEXT ;
122- funcname TEXT ;
123- triggername TEXT ;
124- child_relname_format TEXT ;
125- atttype TEXT ;
126- hashfunc TEXT ;
109+ trigger TEXT := ' CREATE TRIGGER %s
110+ BEFORE UPDATE ON %s
111+ FOR EACH ROW EXECUTE PROCEDURE %s()' ;
112+
113+ att_names TEXT ;
114+ old_fields TEXT ;
115+ new_fields TEXT ;
116+ att_val_fmt TEXT ;
117+ att_fmt TEXT ;
118+ attr TEXT ;
119+ plain_schema TEXT ;
120+ plain_relname TEXT ;
121+ funcname TEXT ;
122+ triggername TEXT ;
123+ child_relname_format TEXT ;
124+ atttype TEXT ;
125+ hashfunc TEXT ;
126+ partitions_count INTEGER ;
127127
128128BEGIN
129129 SELECT * 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 ;
133132 SELECT 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, ' , ' )
139141 FROM pg_attribute
140- WHERE attrelid= relid AND attnum> 0
142+ WHERE attrelid = parent_relid AND attnum > 0
141143 INTO att_names,
142144 old_fields,
143145 new_fields,
144146 att_val_fmt,
145147 att_fmt;
146148
147- attr := attname FROM @extschema@.pathman_config WHERE partrel = relation ;
149+ attr := attname FROM @extschema@.pathman_config WHERE partrel = parent_relid ;
148150
149151 IF attr IS NULL THEN
150- RAISE EXCEPTION ' Table % is not partitioned' , quote_ident(relation ::TEXT );
152+ RAISE EXCEPTION ' Table % is not partitioned' , quote_ident(parent_relid ::TEXT );
151153 END IF;
152154
153- partitions_count := COUNT (* ) FROM pg_inherits WHERE inhparent = relation ::oid ;
155+ partitions_count := COUNT (* ) FROM pg_inherits WHERE inhparent = parent_relid ::oid ;
154156
155157 /* Function name, trigger name and child relname template */
156158 funcname := plain_schema || ' .' || quote_ident(format(' %s_update_trigger_func' , plain_relname));
157159 child_relname_format := plain_schema || ' .' || quote_ident(plain_relname || ' _%s' );
158160 triggername := 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 */
165167 func := format(func, funcname, attr, partitions_count, att_val_fmt,
@@ -169,18 +171,18 @@ BEGIN
169171 /* Create triggers on child relations */
170172 FOR num IN 0 ..partitions_count- 1
171173 LOOP
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);
176178 END LOOP;
177179END
178180$$ LANGUAGE plpgsql;
179181
180182/*
181183 * Returns hash function OID for specified type
182184 */
183- CREATE OR REPLACE FUNCTION @extschema@.get_type_hash_func(OID )
185+ CREATE OR REPLACE FUNCTION @extschema@.get_type_hash_func(REGTYPE )
184186RETURNS OID AS ' pg_pathman' , ' get_type_hash_func'
185187LANGUAGE C STRICT;
186188
0 commit comments