@@ -22,8 +22,8 @@ DECLARE
2222 v_child_relname TEXT ;
2323 v_plain_schema TEXT ;
2424 v_plain_relname TEXT ;
25- v_atttype REGTYPE;
26- v_hashfunc REGPROC;
25+ -- v_atttype REGTYPE;
26+ -- v_hashfunc REGPROC;
2727 v_init_callback REGPROCEDURE;
2828
2929BEGIN
4141 PERFORM @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
4747 SELECT * INTO v_plain_schema, v_plain_relname
4848 FROM @extschema@.get_plain_schema_and_relname(parent_relid);
7272$$ LANGUAGE plpgsql
7373SET client_min_messages = WARNING;
7474
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+ CREATE OR REPLACE FUNCTION @extschema@.replace_hash_partition(
80+ old_partition REGCLASS,
81+ new_partition REGCLASS)
82+ RETURNS REGCLASS AS
83+ $$
84+ DECLARE
85+ v_attname TEXT ;
86+ rel_persistence CHAR ;
87+ v_init_callback REGPROCEDURE;
88+ v_parent_relid REGCLASS;
89+ v_part_count INT ;
90+ v_part_num INT ;
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 relpersistence FROM 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 := attname FROM @extschema@.pathman_config WHERE partrel = v_parent_relid;
117+ IF v_attname IS NULL 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_list WHERE 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_params AS 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+
75163/*
76164 * Creates an update trigger
77165 */
0 commit comments