File tree 2 files changed +51
-1
lines changed
2 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -683,14 +683,50 @@ def reload(self, params=[]):
683
683
_params = [
684
684
get_bin_path ("pg_ctl" ),
685
685
"-D" , self .data_dir ,
686
- "-w" , # wait
687
686
"reload"
688
687
] + params # yapf: disable
689
688
690
689
execute_utility (_params , self .utils_log_file )
691
690
692
691
return self
693
692
693
+ def promote (self , dbname = None , username = None ):
694
+ """
695
+ Promote standby instance to master using pg_ctl. For PostgreSQL versions
696
+ below 10 some additional actions required to ensure that instance
697
+ became writable and hence `dbname` and `username` parameters may be
698
+ needed.
699
+
700
+ Returns:
701
+ This instance of :class:`.PostgresNode`.
702
+ """
703
+
704
+ _params = [
705
+ get_bin_path ("pg_ctl" ),
706
+ "-D" , self .data_dir ,
707
+ "-w" , # wait
708
+ "promote"
709
+ ] # yapf: disable
710
+
711
+ execute_utility (_params , self .utils_log_file )
712
+
713
+ # for versions below 10 `promote` is asynchronous so we need to wait
714
+ # until it actually becomes writable
715
+ if self ._pg_version < '10' :
716
+ check_query = "SELECT pg_is_in_recovery()"
717
+
718
+ self .poll_query_until (
719
+ query = check_query ,
720
+ expected = False ,
721
+ dbname = dbname ,
722
+ username = username ,
723
+ max_attempts = 0 ) # infinite
724
+
725
+ # node becomes master itself
726
+ self ._master = None
727
+
728
+ return self
729
+
694
730
def pg_ctl (self , params ):
695
731
"""
696
732
Invoke pg_ctl with params.
Original file line number Diff line number Diff line change @@ -529,6 +529,20 @@ def test_incorrect_catchup(self):
529
529
with self .assertRaises (TestgresException ):
530
530
node .catchup ()
531
531
532
+ def test_promotion (self ):
533
+ with get_new_node () as master :
534
+ master .init ().start ()
535
+ master .safe_psql ('create table abc(id serial)' )
536
+
537
+ with master .replicate ().start () as replica :
538
+ master .stop ()
539
+ replica .promote ()
540
+
541
+ # make standby becomes writable master
542
+ replica .safe_psql ('insert into abc values (1)' )
543
+ res = replica .safe_psql ('select * from abc' )
544
+ self .assertEqual (res , b'1\n ' )
545
+
532
546
def test_dump (self ):
533
547
query_create = 'create table test as select generate_series(1, 2) as val'
534
548
query_select = 'select * from test order by val asc'
You can’t perform that action at this time.
0 commit comments