forked from postgrespro/pg_pathman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.sql
174 lines (150 loc) · 5.35 KB
/
hash.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
/* ------------------------------------------------------------------------
*
* hash.sql
* HASH partitioning functions
*
* Copyright (c) 2015-2016, Postgres Professional
*
* ------------------------------------------------------------------------
*/
/*
* Creates hash partitions for specified relation
*/
CREATE OR REPLACE FUNCTION @[email protected]_hash_partitions(
parent_relid REGCLASS,
expression TEXT,
partitions_count INT4,
partition_data BOOLEAN DEFAULT TRUE,
partition_names TEXT[] DEFAULT NULL,
tablespaces TEXT[] DEFAULT NULL)
RETURNS INTEGER AS $$
BEGIN
PERFORM @[email protected]_for_partitioning(parent_relid,
expression,
partition_data);
/* Insert new entry to pathman config */
PERFORM @[email protected]_to_pathman_config(parent_relid, expression);
/* Create partitions */
PERFORM @[email protected]_hash_partitions_internal(parent_relid,
expression,
partitions_count,
partition_names,
tablespaces);
/* Copy data */
IF partition_data = true THEN
PERFORM @[email protected]_enable_parent(parent_relid, false);
PERFORM @[email protected]_data(parent_relid);
ELSE
PERFORM @[email protected]_enable_parent(parent_relid, true);
END IF;
RETURN partitions_count;
END
$$ LANGUAGE plpgsql
SET client_min_messages = WARNING;
/*
* Replace hash partition with another one. It could be useful in case when
* someone wants to attach foreign table as a partition.
*
* lock_parent - should we take an exclusive lock?
*/
CREATE OR REPLACE FUNCTION @[email protected]_hash_partition(
old_partition REGCLASS,
new_partition REGCLASS,
lock_parent BOOL DEFAULT TRUE)
RETURNS REGCLASS AS $$
DECLARE
parent_relid REGCLASS;
old_constr_name TEXT; /* name of old_partition's constraint */
old_constr_def TEXT; /* definition of old_partition's constraint */
rel_persistence CHAR;
p_init_callback REGPROCEDURE;
BEGIN
PERFORM @[email protected]_relname(old_partition);
PERFORM @[email protected]_relname(new_partition);
/* Parent relation */
parent_relid := @[email protected]_parent_of_partition(old_partition);
IF lock_parent THEN
/* Acquire data modification lock (prevent further modifications) */
PERFORM @[email protected]_data_modification(parent_relid);
ELSE
/* Acquire lock on parent */
PERFORM @[email protected]_part_modification(parent_relid);
END IF;
/* Acquire data modification lock (prevent further modifications) */
PERFORM @[email protected]_data_modification(old_partition);
PERFORM @[email protected]_data_modification(new_partition);
/* Ignore temporary tables */
SELECT relpersistence FROM pg_catalog.pg_class
WHERE oid = new_partition INTO rel_persistence;
IF rel_persistence = 't'::CHAR THEN
RAISE EXCEPTION 'temporary table "%" cannot be used as a partition',
new_partition::TEXT;
END IF;
/* Check that new partition has an equal structure as parent does */
BEGIN
PERFORM @[email protected]_tuple_convertible(parent_relid, new_partition);
EXCEPTION WHEN OTHERS THEN
RAISE EXCEPTION 'partition must have a compatible tuple format';
END;
/* Check that table is partitioned */
IF @[email protected]_partition_key(parent_relid) IS NULL THEN
RAISE EXCEPTION 'table "%" is not partitioned', parent_relid::TEXT;
END IF;
/* Fetch name of old_partition's HASH constraint */
old_constr_name = @[email protected]_check_constraint_name(old_partition::REGCLASS);
/* Fetch definition of old_partition's HASH constraint */
SELECT pg_catalog.pg_get_constraintdef(oid) FROM pg_catalog.pg_constraint
WHERE conrelid = old_partition AND quote_ident(conname) = old_constr_name
INTO old_constr_def;
/* Detach old partition */
EXECUTE format('ALTER TABLE %s NO INHERIT %s', old_partition, parent_relid);
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %s',
old_partition,
old_constr_name);
/* Attach the new one */
EXECUTE format('ALTER TABLE %s INHERIT %s', new_partition, parent_relid);
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s %s',
new_partition,
@[email protected]_check_constraint_name(new_partition::REGCLASS),
old_constr_def);
/* Fetch init_callback from 'params' table */
WITH stub_callback(stub) as (values (0))
SELECT init_callback
FROM stub_callback
LEFT JOIN @[email protected]_config_params AS params
ON params.partrel = parent_relid
INTO p_init_callback;
/* Finally invoke init_callback */
PERFORM @[email protected]_on_partition_created_callback(parent_relid,
new_partition,
p_init_callback);
RETURN new_partition;
END
$$ LANGUAGE plpgsql;
/*
* Just create HASH partitions, called by create_hash_partitions().
*/
CREATE OR REPLACE FUNCTION @[email protected]_hash_partitions_internal(
parent_relid REGCLASS,
attribute TEXT,
partitions_count INT4,
partition_names TEXT[] DEFAULT NULL,
tablespaces TEXT[] DEFAULT NULL)
RETURNS VOID AS 'pg_pathman', 'create_hash_partitions_internal'
LANGUAGE C;
/*
* Calculates hash for integer value
*/
CREATE OR REPLACE FUNCTION @[email protected]_hash_part_idx(INT4, INT4)
RETURNS INTEGER AS 'pg_pathman', 'get_hash_part_idx'
LANGUAGE C STRICT;
/*
* Build hash condition for a CHECK CONSTRAINT
*/
CREATE OR REPLACE FUNCTION @[email protected]_hash_condition(
attribute_type REGTYPE,
attribute TEXT,
partitions_count INT4,
partition_index INT4)
RETURNS TEXT AS 'pg_pathman', 'build_hash_condition'
LANGUAGE C STRICT;