12
12
* Creates hash partitions for specified relation
13
13
*/
14
14
CREATEOR REPLACE FUNCTION @extschema@.create_hash_partitions(
15
- relation REGCLASS
16
- , attributeTEXT
17
- , partitions_countINTEGER
15
+ parent_relid REGCLASS,
16
+ attributeTEXT ,
17
+ partitions_countINTEGER
18
18
) RETURNSINTEGER AS
19
19
$$
20
20
DECLARE
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
+
27
27
BEGIN
28
- v_relname : = @extschema@.validate_relname(relation );
28
+ PERFORM @extschema@.validate_relname(parent_relid );
29
29
attribute := lower (attribute);
30
- PERFORM @extschema@.common_relation_checks(relation , attribute);
30
+ PERFORM @extschema@.common_relation_checks(parent_relid , attribute);
31
31
32
- v_type := @extschema@.get_attribute_type_name(v_relname , attribute);
32
+ v_type := @extschema@.get_attribute_type_name(parent_relid , attribute);
33
33
34
34
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 );
36
36
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;
38
38
39
39
/* Insert new entry to pathman config*/
40
40
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype)
41
- VALUES (relation , attribute,1 );
41
+ VALUES (parent_relid , attribute,1 );
42
42
43
43
/* Create partitions and update pg_pathman configuration*/
44
44
FOR partnumIN 0 ..partitions_count- 1
45
45
LOOP
46
46
v_child_relname := format(' %s.%s' ,
47
- v_plain_schema,
47
+ quote_ident( v_plain_schema) ,
48
48
quote_ident(v_plain_relname|| ' _' || partnum));
49
49
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);
61
62
END LOOP;
62
63
63
64
/* Notify backend about changes*/
64
- PERFORM @extschema@.on_create_partitions(relation:: oid );
65
+ PERFORM @extschema@.on_create_partitions(parent_relid );
65
66
66
67
/* Copy data*/
67
- PERFORM @extschema@.partition_data(relation );
68
+ PERFORM @extschema@.partition_data(parent_relid );
68
69
69
70
RETURN partitions_count;
70
71
END
@@ -75,7 +76,7 @@ SET client_min_messages = WARNING;
75
76
* Creates an update trigger
76
77
*/
77
78
CREATEOR REPLACE FUNCTION @extschema@.create_hash_update_trigger(
78
- IN relation REGCLASS)
79
+ parent_relid REGCLASS)
79
80
RETURNS VOIDAS
80
81
$$
81
82
DECLARE
@@ -105,61 +106,62 @@ DECLARE
105
106
END $body$
106
107
LANGUAGE plpgsql' ;
107
108
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 ;
127
127
128
128
BEGIN
129
129
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 );
131
131
132
- relid := relation::oid ;
133
132
SELECT string_agg(attname,' ,' ),
134
133
string_agg(' OLD.' || attname,' ,' ),
135
134
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' ),
138
140
string_agg(' $' || attnum,' ,' )
139
141
FROM pg_attribute
140
- WHERE attrelid= relid AND attnum> 0
142
+ WHERE attrelid= parent_relid AND attnum> 0
141
143
INTO att_names,
142
144
old_fields,
143
145
new_fields,
144
146
att_val_fmt,
145
147
att_fmt;
146
148
147
- attr := attnameFROM @extschema@.pathman_configWHERE partrel= relation ;
149
+ attr := attnameFROM @extschema@.pathman_configWHERE partrel= parent_relid ;
148
150
149
151
IF 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 );
151
153
END IF;
152
154
153
- partitions_count := COUNT (* )FROM pg_inheritsWHERE inhparent= relation ::oid ;
155
+ partitions_count := COUNT (* )FROM pg_inheritsWHERE inhparent= parent_relid ::oid ;
154
156
155
157
/* Function name, trigger name and child relname template*/
156
158
funcname := plain_schema|| ' .' || quote_ident(format(' %s_update_trigger_func' , plain_relname));
157
159
child_relname_format := plain_schema|| ' .' || quote_ident(plain_relname|| ' _%s' );
158
160
triggername := quote_ident(format(' %s_%s_update_trigger' , plain_schema, plain_relname));
159
161
160
162
/* 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;
163
165
164
166
/* Format function definition and execute it*/
165
167
func := format(func, funcname, attr, partitions_count, att_val_fmt,
@@ -169,18 +171,18 @@ BEGIN
169
171
/* Create triggers on child relations*/
170
172
FOR numIN 0 ..partitions_count- 1
171
173
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);
176
178
END LOOP;
177
179
END
178
180
$$ LANGUAGE plpgsql;
179
181
180
182
/*
181
183
* Returns hash function OID for specified type
182
184
*/
183
- CREATEOR REPLACE FUNCTION @extschema@.get_type_hash_func(OID )
185
+ CREATEOR REPLACE FUNCTION @extschema@.get_type_hash_func(REGTYPE )
184
186
RETURNSOID AS ' pg_pathman' ,' get_type_hash_func'
185
187
LANGUAGE C STRICT;
186
188