@@ -115,14 +115,15 @@ BEGIN
115
115
THEN
116
116
-- Take all nodes in replicationg group excluding myself
117
117
FOR node IN SELECT * FROM shardman .nodes WHERE replication_group = repl_group LOOP
118
- sync_standbys :=
119
- coalesce(ARRAY(SELECT format(' sub_%s_%s' , id, node .id ) FROM shardman .nodes
118
+ -- Construct list of synchronous standbyes=subscriptions to this node
119
+ sync_standbys :=
120
+ coalesce(ARRAY(SELECT format(' sub_%s_%s' , id, node .id ) FROM shardman .nodes
120
121
WHERE replication_group = repl_group AND id <> node .id ), ' {}' ::text []);
121
- sync := format(' %s%s:ALTER SYSTEM SET synchronous_standby_names to ' ' FIRST %s (%s)' ' ;' ,
122
- sync, node .id , array_length(sync_standbys, 1 ),
123
- array_to_string(sync_standbys, ' ,' ));
124
- conf := format(' %s%s:SELECT pg_reload_conf();' , conf, node .id );
125
- END LOOP;
122
+ sync := format(' %s%s:ALTER SYSTEM SET synchronous_standby_names to ' ' FIRST %s (%s)' ' ;' ,
123
+ sync, node .id , array_length(sync_standbys, 1 ),
124
+ array_to_string(sync_standbys, ' ,' ));
125
+ conf := format(' %s%s:SELECT pg_reload_conf();' , conf, node .id );
126
+ END LOOP;
126
127
127
128
PERFORM shardman .broadcast (sync, sync_commit => true);
128
129
PERFORM shardman .broadcast (conf);
@@ -299,7 +300,7 @@ BEGIN
299
300
pubs := format(' %s%s:ALTER PUBLICATION node_%s ADD TABLE %I;' ,
300
301
pubs, new_master_id, repl .node_id , part .part_name );
301
302
-- And refresh subscriptions and replicas
302
- subs := format(' %s%s:ALTER SUBSCRIPTION sub_%s_%s REFRESH PUBLICATION;' ,
303
+ subs := format(' %s%s:ALTER SUBSCRIPTION sub_%s_%s REFRESH PUBLICATION WITH (copy_data=false) ;' ,
303
304
subs, repl .node_id , repl .node_id , new_master_id);
304
305
END LOOP;
305
306
@@ -343,7 +344,7 @@ $$ LANGUAGE plpgsql;
343
344
-- It also scatter partitions through all nodes.
344
345
-- This function expects that empty table is created at shardlord.
345
346
-- So it can be executed only at shardlord and there is no need to redirect this function to shardlord.
346
- CREATE FUNCTION create_hash_partitions (rel regclass, expr text , part_count int )
347
+ CREATE FUNCTION create_hash_partitions (rel regclass, expr text , part_count int , redundancy int = 0 )
347
348
RETURNS void AS $$
348
349
DECLARE
349
350
create_table text ;
@@ -362,7 +363,7 @@ DECLARE
362
363
i int ;
363
364
n_nodes int ;
364
365
BEGIN
365
- IF EXISTS(SELECT relation FROM shardman .partitions WHERE relation = rel_name)
366
+ IF EXISTS(SELECT relation FROM shardman .tables WHERE relation = rel_name)
366
367
THEN
367
368
RAISE EXCEPTION ' Table % is already sharded' , rel_name;
368
369
END IF;
@@ -419,13 +420,18 @@ BEGIN
419
420
PERFORM shardman .broadcast (create_fdws);
420
421
-- Broadcast replace hash partition commands
421
422
PERFORM shardman .broadcast (replace_parts);
423
+
424
+ IF redundancy <> 0
425
+ THEN
426
+ PERFORM shardman .set_redundancy (rel, redundancy, copy_data => false);
427
+ END IF;
422
428
END
423
429
$$ LANGUAGE plpgsql;
424
430
425
431
-- Provide requested level of redundancy. 0 means no redundancy.
426
432
-- If existing level of redundancy is greater than specified, then right now this
427
433
-- function does nothing.
428
- CREATE FUNCTION set_redundancy (rel regclass, redundancy int )
434
+ CREATE FUNCTION set_redundancy (rel regclass, redundancy int , copy_data bool = true )
429
435
RETURNS void AS $$
430
436
DECLARE
431
437
part shardman .partitions ;
@@ -435,12 +441,17 @@ DECLARE
435
441
pubs text = ' ' ;
436
442
subs text = ' ' ;
437
443
rel_name text = rel::text ;
444
+ sub_options text = ' ' ;
438
445
BEGIN
439
446
IF shardman .redirect_to_shardlord (format(' set_redundancy(%L, %L)' , rel_name, redundancy))
440
447
THEN
441
448
RETURN;
442
449
END IF;
443
450
451
+ IF NOT copy_data THEN
452
+ sub_options := ' WITH (copy_data=false)' ;
453
+ END IF;
454
+
444
455
-- Loop through all partitions of this table
445
456
FOR part IN SELECT * from shardman .partitions where relation= rel_name
446
457
LOOP
@@ -460,16 +471,16 @@ BEGIN
460
471
-- Establish publications and subscriptions for this partition
461
472
pubs := format(' %s%s:ALTER PUBLICATION node_%s ADD TABLE %I;' ,
462
473
pubs, part .node_id , repl_node, part .part_name );
463
- subs := format(' %s%s:ALTER SUBSCRIPTION sub_%s_%s REFRESH PUBLICATION;' ,
464
- subs, repl_node, repl_node, part .node_id );
474
+ subs := format(' %s%s:ALTER SUBSCRIPTION sub_%s_%s REFRESH PUBLICATION%s ;' ,
475
+ subs, repl_node, repl_node, part .node_id , sub_options );
465
476
END LOOP;
466
477
END IF;
467
478
END LOOP;
468
479
469
480
-- Broadcast alter publication commands
470
481
PERFORM shardman .broadcast (pubs);
471
482
-- Broadcast alter subscription commands
472
- PERFORM shardman .broadcast (subs);
483
+ PERFORM shardman .broadcast (subs, synchronous => copy_data );
473
484
474
485
-- This function doesn't wait completion of replication sync
475
486
END
@@ -585,8 +596,8 @@ BEGIN
585
596
%s:ALTER PUBLICATION node_%s ADD TABLE %I;' ,
586
597
pubs, src_node_id, repl_node_id, mv_part_name,
587
598
dst_node_id, repl_node_id, mv_part_name);
588
- subs := format(' %s%s:ALTER SUBSCRIPTION sub_%s_%s REFRESH PUBLICATION;
589
- %s:ALTER SUBSCRIPTION sub_%s_%s REFRESH PUBLICATION;' ,
599
+ subs := format(' %s%s:ALTER SUBSCRIPTION sub_%s_%s REFRESH PUBLICATION WITH (copy_data=false) ;
600
+ %s:ALTER SUBSCRIPTION sub_%s_%s REFRESH PUBLICATION WITH (copy_data=false) ;' ,
590
601
subs, repl_node_id, repl_node_id, src_node_id,
591
602
repl_node_id, repl_node_id, dst_node_id);
592
603
END LOOP;
@@ -691,8 +702,11 @@ RETURNS text AS 'pg_shardman' LANGUAGE C STRICT;
691
702
-- prepared with subsequent commit or rollback of prepared transaction at second
692
703
-- phase of two phase commit.
693
704
-- If sync_commit is false, we do set session synchronous_commit to local;
694
- CREATE FUNCTION broadcast (cmds text , ignore_errors bool = false,
695
- two_phase bool = false, sync_commit bool = false)
705
+ CREATE FUNCTION broadcast (cmds text ,
706
+ ignore_errors bool = false,
707
+ two_phase bool = false,
708
+ sync_commit bool = false,
709
+ synchronous bool = false)
696
710
RETURNS text AS ' pg_shardman' LANGUAGE C STRICT;
697
711
698
712
-- Options to postgres_fdw are specified in two places: user & password in user
0 commit comments