@@ -22,8 +22,8 @@ DECLARE
22
22
v_child_relnameTEXT ;
23
23
v_plain_schemaTEXT ;
24
24
v_plain_relnameTEXT ;
25
- v_atttypeREGTYPE;
26
- v_hashfuncREGPROC;
25
+ -- v_atttypeREGTYPE;
26
+ -- v_hashfuncREGPROC;
27
27
v_init_callbackREGPROCEDURE;
28
28
29
29
BEGIN
41
41
PERFORM @extschema@.common_relation_checks(parent_relid, attribute);
42
42
43
43
/* Fetch atttype and its hash function*/
44
- v_atttype := @extschema@.get_attribute_type(parent_relid, attribute);
45
- v_hashfunc := @extschema@.get_type_hash_func(v_atttype);
44
+ -- v_atttype := @extschema@.get_attribute_type(parent_relid, attribute);
45
+ -- v_hashfunc := @extschema@.get_type_hash_func(v_atttype);
46
46
47
47
SELECT * INTO v_plain_schema, v_plain_relname
48
48
FROM @extschema@.get_plain_schema_and_relname(parent_relid);
72
72
$$ LANGUAGE plpgsql
73
73
SET client_min_messages= WARNING;
74
74
75
+ /*
76
+ * Replace hash partition with another one. It could be useful in case when
77
+ * someone wants to attach foreign table as a partition
78
+ */
79
+ CREATEOR REPLACE FUNCTION @extschema@.replace_hash_partition(
80
+ old_partitionREGCLASS,
81
+ new_partitionREGCLASS)
82
+ RETURNS REGCLASSAS
83
+ $$
84
+ DECLARE
85
+ v_attnameTEXT ;
86
+ rel_persistenceCHAR ;
87
+ v_init_callbackREGPROCEDURE;
88
+ v_parent_relidREGCLASS;
89
+ v_part_countINT ;
90
+ v_part_numINT ;
91
+ BEGIN
92
+ PERFORM @extschema@.validate_relname(old_partition);
93
+ PERFORM @extschema@.validate_relname(new_partition);
94
+
95
+ /* Parent relation*/
96
+ v_parent_relid := @extschema@.get_parent_of_partition(old_partition);
97
+
98
+ /* Acquire lock on parent*/
99
+ PERFORM @extschema@.lock_partitioned_relation(v_parent_relid);
100
+
101
+ /* Ignore temporary tables*/
102
+ SELECT relpersistenceFROM pg_catalog .pg_class
103
+ WHERE oid = new_partition INTO rel_persistence;
104
+
105
+ IF rel_persistence= ' t' ::CHAR THEN
106
+ RAISE EXCEPTION' temporary table "%" cannot be used as a partition' ,
107
+ new_partition::TEXT ;
108
+ END IF;
109
+
110
+ /* Check that new partition has an equal structure as parent does*/
111
+ IF NOT @extschema@.validate_relations_equality(v_parent_relid, new_partition) THEN
112
+ RAISE EXCEPTION' partition must have the exact same structure as parent' ;
113
+ END IF;
114
+
115
+ /* Get partitioning key*/
116
+ v_attname := attnameFROM @extschema@.pathman_configWHERE partrel= v_parent_relid;
117
+ IF v_attname ISNULL THEN
118
+ RAISE EXCEPTION' table "%" is not partitioned' , v_parent_relid::TEXT ;
119
+ END IF;
120
+
121
+ /* Calculate partitions count and old partition's number*/
122
+ v_part_count := count (* )FROM @extschema@.pathman_partition_listWHERE parent= v_parent_relid;
123
+ v_part_num := @extschema@.get_partition_hash(v_parent_relid, old_partition);
124
+
125
+ /* Detach old partition*/
126
+ EXECUTE format(' ALTER TABLE %s NO INHERIT %s' , old_partition, v_parent_relid);
127
+ EXECUTE format(' ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s' ,
128
+ old_partition,
129
+ @extschema@.build_check_constraint_name(old_partition::REGCLASS,
130
+ v_attname));
131
+
132
+ /* Attach new one*/
133
+ EXECUTE format(' ALTER TABLE %s INHERIT %s' , new_partition, v_parent_relid);
134
+ EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)' ,
135
+ new_partition,
136
+ @extschema@.build_check_constraint_name(new_partition::regclass,
137
+ v_attname),
138
+ @extschema@.build_hash_condition(new_partition::regclass,
139
+ v_attname,
140
+ v_part_count,
141
+ v_part_num));
142
+
143
+ /* Fetch init_callback from 'params' table*/
144
+ WITH stub_callback(stub)as (values (0 ))
145
+ SELECT coalesce(init_callback,0 ::REGPROCEDURE)
146
+ FROM stub_callback
147
+ LEFT JOIN @extschema@.pathman_config_paramsAS params
148
+ ON params .partrel = v_parent_relid
149
+ INTO v_init_callback;
150
+
151
+ PERFORM @extschema@.invoke_on_partition_created_callback(v_parent_relid,
152
+ new_partition,
153
+ v_init_callback);
154
+
155
+ /* Invalidate cache*/
156
+ PERFORM @extschema@.on_update_partitions(v_parent_relid);
157
+
158
+ RETURN new_partition;
159
+ END
160
+ $$
161
+ LANGUAGE plpgsql;
162
+
75
163
/*
76
164
* Creates an update trigger
77
165
*/