@@ -106,6 +106,48 @@ CREATE TRIGGER new_primary AFTER INSERT ON shardman.partitions
106
106
-- fire trigger only on worker nodes
107
107
ALTER TABLE shardman .partitions ENABLE REPLICA TRIGGER new_primary;
108
108
109
+ -- Executed on node with new primary, see mp_rebuild_lr
110
+ CREATE FUNCTION primary_moved_create_data_pub (p_name name, src int , dst int )
111
+ RETURNS void AS $$
112
+ DECLARE
113
+ -- Metadata is not yet updated, so taking nxt from src node
114
+ replica int := nxt FROM shardman .partitions
115
+ WHERE part_name = p_name AND owner = src;
116
+ new_pubname text := shardman .get_data_pubname (p_name, dst);
117
+ new_subname text := shardman .get_data_subname (p_name, dst, replica);
118
+ BEGIN
119
+ PERFORM shardman .create_repslot (new_pubname);
120
+ -- Create publication for new data channel
121
+ EXECUTE format(' DROP PUBLICATION IF EXISTS %I' , new_pubname);
122
+ EXECUTE format(' CREATE PUBLICATION %I FOR TABLE %I' ,
123
+ new_pubname, p_name);
124
+ -- Make this channel sync
125
+ PERFORM shardman .ensure_sync_standby (new_subname);
126
+ END $$ LANGUAGE plpgsql STRICT;
127
+
128
+ -- Executed on nearest replica after primary moved, see mp_rebuild_lr
129
+ CREATE FUNCTION primary_moved_create_data_sub (p_name name, src int , dst int )
130
+ RETURNS void AS $$
131
+ DECLARE
132
+ -- Metadata is not yet updated, so taking nxt from src node
133
+ replica int := nxt FROM shardman .partitions
134
+ WHERE part_name = p_name AND owner = src;
135
+ new_pubname text := shardman .get_data_pubname (p_name, dst);
136
+ new_subname text := shardman .get_data_subname (p_name, dst, replica);
137
+ cp_logname text := shardman .get_cp_logname (p_name, src, dst);
138
+ new_connstr text := shardman .get_worker_node_connstr (dst);
139
+ BEGIN
140
+ -- Drop subscription used for copy
141
+ PERFORM shardman .eliminate_sub (cp_logname);
142
+ -- Create subscription for new data channel
143
+ -- It should never exist at this moment, but just in case...
144
+ PERFORM shardman .eliminate_sub (new_subname);
145
+ EXECUTE format(
146
+ ' CREATE SUBSCRIPTION %I connection %L
147
+ PUBLICATION %I with (create_slot = false, slot_name = %L, copy_data = false);' ,
148
+ new_subname, new_connstr, new_pubname, new_pubname);
149
+ END $$ LANGUAGE plpgsql STRICT;
150
+
109
151
-- Update metadata according to primary move
110
152
CREATE FUNCTION primary_moved () RETURNS TRIGGER AS $$
111
153
DECLARE
@@ -115,6 +157,8 @@ BEGIN
115
157
RAISE DEBUG ' [SHARDMAN] primary_moved trigger called for part %, owner %->%' ,
116
158
NEW .part_name , OLD .owner , NEW .owner ;
117
159
ASSERT NEW .owner != OLD .owner , ' primary_moved handles only moved parts' ;
160
+ ASSERT NEW .nxt = OLD .nxt OR (NEW .nxt IS NULL AND OLD .nxt IS NULL ),
161
+ ' both primary and replica must not be moved in one update' ;
118
162
IF my_id = OLD .owner THEN -- src node
119
163
-- Drop publication & repslot used for copy
120
164
PERFORM shardman .drop_repslot_and_pub (cp_logname);
@@ -141,7 +185,7 @@ CREATE TRIGGER primary_moved AFTER UPDATE ON shardman.partitions
141
185
ALTER TABLE shardman .partitions ENABLE REPLICA TRIGGER primary_moved;
142
186
143
187
-- Executed on newtail node, see cr_rebuild_lr
144
- CREATE FUNCTION replica_created_rebuild_drop_cp_sub (
188
+ CREATE FUNCTION replica_created_drop_cp_sub (
145
189
part_name name, oldtail int , newtail int ) RETURNS void AS $$
146
190
DECLARE
147
191
cp_logname text := shardman .get_cp_logname (part_name, oldtail, newtail);
@@ -152,7 +196,7 @@ BEGIN
152
196
END $$ LANGUAGE plpgsql;
153
197
154
198
-- Executed on oldtail node, see cr_rebuild_lr
155
- CREATE FUNCTION replica_created_rebuild_lr_create_data_pub (
199
+ CREATE FUNCTION replica_created_create_data_pub (
156
200
part_name name, oldtail int , newtail int ) RETURNS void AS $$
157
201
DECLARE
158
202
cp_logname text := shardman .get_cp_logname (part_name, oldtail, newtail);
@@ -175,7 +219,7 @@ BEGIN
175
219
END $$ LANGUAGE plpgsql;
176
220
177
221
-- Executed on oldtail node, see cr_rebuild_lr
178
- CREATE FUNCTION replica_created_rebuild_lr_create_data_sub (
222
+ CREATE FUNCTION replica_created_create_data_sub (
179
223
part_name name, oldtail int , newtail int ) RETURNS void AS $$
180
224
DECLARE
181
225
oldtail_pubname name := shardman .get_data_pubname (part_name, oldtail);
0 commit comments