-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg_particulous--1.0.sql
207 lines (166 loc) · 4.79 KB
/
pg_particulous--1.0.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Special checks */
DO LANGUAGE plpgsql
$$
DECLARE
pathman_schema text;
BEGIN
SELECT extnamespace::regnamespace
FROM pg_catalog.pg_extension
WHERE extname = 'pg_pathman'
INTO pathman_schema;
IF '@extschema@' != pathman_schema THEN
RAISE EXCEPTION 'pg_particulous should be installed into pg_pathman''s schema';
END IF;
END
$$;
CREATE FUNCTION build_vanilla_part_condition(
relation REGCLASS)
RETURNS TEXT
AS 'MODULE_PATHNAME', 'build_vanilla_part_condition'
LANGUAGE C STRICT;
CREATE FUNCTION build_vanilla_part_key(
relation REGCLASS)
RETURNS TEXT AS
$$
DECLARE
raw_key text;
BEGIN
raw_key = pg_get_partkeydef(relation);
/* Replace some keywords */
raw_key = replace(raw_key, 'PARTITION BY', '');
raw_key = replace(raw_key, 'RANGE', '');
raw_key = replace(raw_key, 'LIST', '');
RETURN raw_key;
END
$$
LANGUAGE plpgsql STRICT;
CREATE FUNCTION is_partitioned_by_pg10(
relation REGCLASS)
RETURNS BOOL AS
$$
DECLARE
rows_count int8;
BEGIN
/* Check if this table is partitioned by PG 10 */
EXECUTE '
SELECT count(*)
FROM pg_catalog.pg_partitioned_table
WHERE partrelid = $1'
USING relation INTO rows_count;
RETURN rows_count > 0;
EXCEPTION
WHEN others THEN
RETURN FALSE;
END
$$
LANGUAGE plpgsql STRICT;
CREATE FUNCTION desugar_vanilla(
relation REGCLASS)
RETURNS BOOL AS
$$
DECLARE
part_rel pg_catalog.pg_class;
temp_rel pg_catalog.pg_class;
temp_rel_name text := build_sequence_name(relation);
part_relid regclass;
temp_relid regclass;
partition regclass;
has_parts bool := FALSE;
BEGIN
/* Lock important catalog tables */
LOCK TABLE pg_catalog.pg_class IN EXCLUSIVE MODE;
LOCK TABLE pg_catalog.pg_inherits IN EXCLUSIVE MODE;
/* Lock partitioned table */
EXECUTE format('LOCK TABLE %s IN ACCESS EXCLUSIVE MODE', relation);
/* Create temporary table for storage */
EXECUTE format('CREATE TABLE %s (LIKE %s)', temp_rel_name, relation);
/* Initialize relids */
part_relid = relation;
temp_relid = temp_rel_name::regclass;
/* Create check constraints on partitions */
FOR partition IN SELECT inhrelid
FROM pg_catalog.pg_inherits
WHERE inhparent = part_relid LOOP
IF NOT has_parts THEN
/* Make sure that this table is partitioned by PG 10 */
IF NOT is_partitioned_by_pg10(relation) THEN
RAISE EXCEPTION 'table % is not managed by PostgreSQL 10', relation;
END IF;
/* Do not perform this check next time */
has_parts = TRUE;
END IF;
/* Add a constraint for this table */
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)',
partition,
build_check_constraint_name(partition),
build_vanilla_part_condition(partition));
RAISE NOTICE 'created constraint on partition %', partition;
IF NOT desugar_vanilla(partition) THEN
RAISE EXCEPTION 'cannot desugar partition %', partition;
END IF;
END LOOP;
IF NOT has_parts THEN
RAISE NOTICE 'table % has no partitions', relation;
RETURN TRUE;
END IF;
/* Read tuples of parent & temp tables */
SELECT * FROM pg_catalog.pg_class WHERE oid = part_relid INTO part_rel;
SELECT * FROM pg_catalog.pg_class WHERE oid = temp_relid INTO temp_rel;
/* Use storage of temp table for parent */
UPDATE pg_catalog.pg_class SET
relkind = temp_rel.relkind,
relfilenode = temp_rel.relfilenode,
relfrozenxid = temp_rel.relfrozenxid,
relminmxid = temp_rel.relminmxid
WHERE oid = part_relid;
/* Use storate of parent table for temp */
UPDATE pg_catalog.pg_class SET
relkind = part_rel.relkind,
relfilenode = part_rel.relfilenode,
relfrozenxid = part_rel.relfrozenxid,
relminmxid = part_rel.relminmxid
WHERE oid = temp_relid;
/* Make temporary table kind of 'partitioned' */
EXECUTE '
UPDATE pg_catalog.pg_partitioned_table SET
partrelid = $1
WHERE partrelid = $2'
USING temp_relid, part_relid;
/* Turn partitions into ordinary tables */
UPDATE pg_catalog.pg_class SET
relpartbound = NULL,
relispartition = FALSE
FROM pg_catalog.pg_inherits
WHERE pg_catalog.pg_class.oid = inhrelid AND inhparent = part_relid;
/* Finally, drop temporary table */
EXECUTE format('DROP TABLE %s', temp_rel_name);
RAISE NOTICE 'created storage for table %', relation;
RETURN TRUE;
END
$$
LANGUAGE plpgsql STRICT;
CREATE FUNCTION migrate_to_pathman(
relation REGCLASS,
expression TEXT DEFAULT NULL,
run_tests BOOL DEFAULT TRUE)
RETURNS BOOL AS
$$
BEGIN
IF relation IS NULL THEN
RAISE EXCEPTION 'relation should not be NULL';
END IF;
/* Desugar if partitioned by PG 10 */
IF is_partitioned_by_pg10(relation) THEN
expression = build_vanilla_part_key(relation);
PERFORM desugar_vanilla(relation);
END IF;
/* Check if expression is valid */
IF expression IS NULL THEN
RAISE EXCEPTION 'expression should not be NULL';
END IF;
/* Tell pg_pathman about this table */
PERFORM add_to_pathman_config(relation, expression, NULL);
RETURN TRUE;
END
$$
LANGUAGE plpgsql;