Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6bb42f5

Browse files
committed
Add ensure_redundancy function
1 parent 699ff30 commit 6bb42f5

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

pg_shardman--1.0.sql

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,43 @@ BEGIN
577577
-- Broadcast alter subscription commands
578578
PERFORM shardman.broadcast(subs, synchronous => copy_data, super_connstr => true);
579579

580-
-- This function doesn't wait completion of replication sync
580+
-- This function doesn't wait completion of replication sync.
581+
-- Use wait ensure_redundancy function to wait until sync is completed
581582
END
582583
$$ LANGUAGE plpgsql;
583584

585+
-- Wait completion of initial table sync for all replication subscriptions.
586+
-- This function can be used after set_redundancy to ensure that partitions are copied to replicas.
587+
CREATE FUNCTION ensure_redundancy() RETURNS void AS $$
588+
DECLARE
589+
src_node_id int;
590+
dst_node_id int;
591+
timeout_sec int = 1;
592+
sub_name text;
593+
poll text;
594+
response text;
595+
BEGIN
596+
LOOP
597+
poll := ''
598+
FOR src_node_id IN SELECT id FROM shardman.nodes
599+
LOOP
600+
FOR dst_node_id IN SELECT id FROM shardman.nodes WHERE id<>src_node_id
601+
LOOP
602+
sub_name := format('sub_%s_%s', dst_node_id, src_node_id);
603+
poll := format('%s%s:SELECT shardman.is_subscription_ready(%L);',
604+
poll, dst_node_id, sub_name);
605+
END LOOP;
606+
END LOOP;
607+
608+
response := shardman.broadcast(poll);
609+
EXIT WHEN POSITION('f' IN response)=0;
610+
611+
PERFORM pg_sleep(timeout_sec);
612+
END LOOP;
613+
END
614+
$$ LANGUAGE plpgsql;
615+
616+
584617
-- Remove table from all nodes.
585618
CREATE FUNCTION rm_table(rel regclass)
586619
RETURNS void AS $$
@@ -1537,10 +1570,15 @@ CREATE FUNCTION is_shardlord()
15371570
RETURNS bool AS 'pg_shardman' LANGUAGE C STRICT;
15381571

15391572
-- Get subscription status
1540-
CREATE FUNCTION get_subscription_status(sname text) RETURNS "char" AS $$
1541-
SELECT srsubstate FROM pg_subscription_rel srel
1542-
JOIN pg_subscription s on srel.srsubid = s.oid where subname=sname;
1543-
$$ LANGUAGE sql;
1573+
CREATE FUNCTION is_subscription_ready(sname text) RETURNS bool AS $$
1574+
DECLARE
1575+
n_not_ready bigint;
1576+
BEGIN
1577+
SELECT count(*) INTO n_not_ready FROM pg_subscription_rel srel
1578+
JOIN pg_subscription s ON srel.srsubid = s.oid WHERE subname=sname AND srsubstate<>'r';
1579+
RETURN n_not_ready=0;
1580+
END
1581+
$$ LANGUAGE plpgsql;
15441582

15451583
-- Wait initial sync completion
15461584
CREATE FUNCTION wait_sync_completion(src_node_id int, dst_node_id int) RETURNS void AS $$
@@ -1549,9 +1587,9 @@ DECLARE
15491587
response text;
15501588
BEGIN
15511589
LOOP
1552-
response := shardman.broadcast(format('%s:SELECT shardman.get_subscription_status(''sub_%s_%s'');',
1590+
response := shardman.broadcast(format('%s:SELECT shardman.is_subscription_ready(''sub_%s_%s'');',
15531591
dst_node_id, dst_node_id, src_node_id));
1554-
EXIT WHEN response='r';
1592+
EXIT WHEN response::bool;
15551593
PERFORM pg_sleep(timeout_sec);
15561594
END LOOP;
15571595
END
@@ -1573,9 +1611,9 @@ BEGIN
15731611
LOOP
15741612
IF NOT synced
15751613
THEN
1576-
response := shardman.broadcast(format('%s:SELECT shardman.get_subscription_status(%L);',
1614+
response := shardman.broadcast(format('%s:SELECT shardman.is_subscription_ready(%L);',
15771615
dst_node_id, slot));
1578-
IF response='r' THEN
1616+
IF response::bool THEN
15791617
synced := true;
15801618
RAISE DEBUG 'Table % sync completed', part_name;
15811619
CONTINUE;

0 commit comments

Comments
 (0)