@@ -22,8 +22,8 @@ DECLARE
2222v_child_relnameTEXT ;
2323v_plain_schemaTEXT ;
2424v_plain_relnameTEXT ;
25- v_atttypeREGTYPE;
26- v_hashfuncREGPROC;
25+ -- v_atttypeREGTYPE;
26+ -- v_hashfuncREGPROC;
2727v_init_callbackREGPROCEDURE;
2828
2929BEGIN
4141PERFORM @extschema@.common_relation_checks(parent_relid, attribute);
4242
4343/* 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);
4646
4747SELECT * INTO v_plain_schema, v_plain_relname
4848FROM @extschema@.get_plain_schema_and_relname(parent_relid);
@@ -64,15 +64,23 @@ BEGIN
6464parent_relid::TEXT ,
6565@extschema@.get_rel_tablespace_name(parent_relid));
6666
67- EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s
68- CHECK (@extschema@.get_hash_part_idx(%s(%s), %s) = %s)' ,
67+ -- EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s
68+ -- CHECK (@extschema@.get_hash_part_idx(%s(%s), %s) = %s)',
69+ -- v_child_relname,
70+ -- @extschema@.build_check_constraint_name(v_child_relname::REGCLASS,
71+ -- attribute),
72+ -- v_hashfunc::TEXT,
73+ -- attribute,
74+ -- partitions_count,
75+ -- partnum);
76+ EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)' ,
6977 v_child_relname,
70- @extschema@.build_check_constraint_name(v_child_relname::REGCLASS ,
78+ @extschema@.build_check_constraint_name(v_child_relname,
7179 attribute),
72- v_hashfunc:: TEXT ,
73- attribute,
74- partitions_count,
75- partnum);
80+ @extschema@.build_hash_condition(v_child_relname ,
81+ attribute,
82+ partitions_count,
83+ partnum) );
7684
7785PERFORM @extschema@.copy_foreign_keys(parent_relid, v_child_relname::REGCLASS);
7886
105113$$ LANGUAGE plpgsql
106114SET client_min_messages= WARNING;
107115
116+ /*
117+ * Replace hash partition with another one. It could be useful in case when
118+ * someone wants to attach foreign table as a partition
119+ */
120+ CREATEOR REPLACE FUNCTION @extschema@.replace_hash_partition(
121+ old_partitionREGCLASS,
122+ new_partitionREGCLASS)
123+ RETURNS REGCLASSAS
124+ $$
125+ DECLARE
126+ v_attnameTEXT ;
127+ rel_persistenceCHAR ;
128+ v_init_callbackREGPROCEDURE;
129+ v_parent_relidREGCLASS;
130+ v_part_countINT ;
131+ v_part_numINT ;
132+ BEGIN
133+ PERFORM @extschema@.validate_relname(old_partition);
134+ PERFORM @extschema@.validate_relname(new_partition);
135+
136+ /* Parent relation*/
137+ v_parent_relid := @extschema@.get_parent_of_partition(old_partition);
138+
139+ /* Acquire lock on parent*/
140+ PERFORM @extschema@.lock_partitioned_relation(v_parent_relid);
141+
142+ /* Ignore temporary tables*/
143+ SELECT relpersistenceFROM pg_catalog .pg_class
144+ WHERE oid = new_partition INTO rel_persistence;
145+
146+ IF rel_persistence= ' t' ::CHAR THEN
147+ RAISE EXCEPTION' temporary table "%" cannot be used as a partition' ,
148+ new_partition::TEXT ;
149+ END IF;
150+
151+ /* Check that new partition has an equal structure as parent does*/
152+ IF NOT @extschema@.validate_relations_equality(v_parent_relid, new_partition) THEN
153+ RAISE EXCEPTION' partition must have the exact same structure as parent' ;
154+ END IF;
155+
156+ /* Get partitioning key*/
157+ v_attname := attnameFROM @extschema@.pathman_configWHERE partrel= v_parent_relid;
158+ IF v_attname ISNULL THEN
159+ RAISE EXCEPTION' table "%" is not partitioned' , v_parent_relid::TEXT ;
160+ END IF;
161+
162+ /* Calculate partitions count and old partition's number*/
163+ v_part_count := count (* )FROM @extschema@.pathman_partition_listWHERE parent= v_parent_relid;
164+ v_part_num := @extschema@.get_partition_hash(v_parent_relid, old_partition);
165+
166+ /* Detach old partition*/
167+ EXECUTE format(' ALTER TABLE %s NO INHERIT %s' , old_partition, v_parent_relid);
168+ EXECUTE format(' ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s' ,
169+ old_partition,
170+ @extschema@.build_check_constraint_name(old_partition::REGCLASS,
171+ v_attname));
172+
173+ /* Attach new one*/
174+ EXECUTE format(' ALTER TABLE %s INHERIT %s' , new_partition, v_parent_relid);
175+ EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)' ,
176+ new_partition,
177+ @extschema@.build_check_constraint_name(new_partition::regclass,
178+ v_attname),
179+ @extschema@.build_hash_condition(new_partition::regclass,
180+ v_attname,
181+ v_part_count,
182+ v_part_num));
183+
184+ /* Fetch init_callback from 'params' table*/
185+ WITH stub_callback(stub)as (values (0 ))
186+ SELECT coalesce(init_callback,0 ::REGPROCEDURE)
187+ FROM stub_callback
188+ LEFT JOIN @extschema@.pathman_config_paramsAS params
189+ ON params .partrel = v_parent_relid
190+ INTO v_init_callback;
191+
192+ PERFORM @extschema@.invoke_on_partition_created_callback(v_parent_relid,
193+ new_partition,
194+ v_init_callback);
195+
196+ /* Invalidate cache*/
197+ PERFORM @extschema@.on_update_partitions(v_parent_relid);
198+
199+ RETURN new_partition;
200+ END
201+ $$
202+ LANGUAGE plpgsql;
203+
108204/*
109205 * Creates an update trigger
110206*/