File tree Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Expand file tree Collapse file tree 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=[]):
683683 _params = [
684684 get_bin_path ("pg_ctl" ),
685685 "-D" , self .data_dir ,
686- "-w" , # wait
687686 "reload"
688687 ] + params # yapf: disable
689688
690689 execute_utility (_params , self .utils_log_file )
691690
692691 return self
693692
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+
694730 def pg_ctl (self , params ):
695731 """
696732 Invoke pg_ctl with params.
Original file line number Diff line number Diff line change @@ -529,6 +529,20 @@ def test_incorrect_catchup(self):
529529 with self .assertRaises (TestgresException ):
530530 node .catchup ()
531531
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+
532546 def test_dump (self ):
533547 query_create = 'create table test as select generate_series(1, 2) as val'
534548 query_select = 'select * from test order by val asc'
You can’t perform that action at this time.
0 commit comments