@@ -278,7 +278,7 @@ def _assign_master(self, master):
278278 # now this node has a master
279279 self ._master = master
280280
281- def _create_recovery_conf (self , username , slot_name = None ):
281+ def _create_recovery_conf (self , username , slot = None ):
282282 """NOTE: this is a private method!"""
283283
284284 # fetch master of this node
@@ -306,8 +306,26 @@ def _create_recovery_conf(self, username, slot_name=None):
306306 "standby_mode=on\n "
307307 ).format (conninfo )
308308
309- if slot_name :
310- line += "primary_slot_name={}\n " .format (slot_name )
309+ if slot :
310+ # Connect to master for some additional actions
311+ with master .connect (username = username ) as con :
312+ # check if slot already exists
313+ res = con .execute ("""
314+ select exists (
315+ select from pg_catalog.pg_replication_slots
316+ where slot_name = $1
317+ )
318+ """ , slot )
319+
320+ if res [0 ][0 ]:
321+ raise TestgresException ("Slot '{}' already exists" .format (slot ))
322+
323+ # TODO: we should drop this slot after replica's cleanup()
324+ con .execute ("""
325+ select pg_catalog.pg_create_physical_replication_slot($1)
326+ """ , slot )
327+
328+ line += "primary_slot_name={}\n " .format (slot )
311329
312330 self .append_conf (RECOVERY_CONF_FILE , line )
313331
@@ -352,28 +370,6 @@ def _collect_special_files(self):
352370
353371 return result
354372
355- def _create_replication_slot (self , slot_name , dbname = None , username = None ):
356- """
357- Create a physical replication slot.
358-
359- Args:
360- slot_name: slot name
361- dbname: database name
362- username: database user name
363- """
364- rs = self .execute ("select exists (select * from pg_replication_slots "
365- "where slot_name = '{}')" .format (slot_name ),
366- dbname = dbname , username = username )
367-
368- if rs [0 ][0 ]:
369- raise TestgresException ("Slot '{}' already exists" .format (slot_name ))
370-
371- query = (
372- "select pg_create_physical_replication_slot('{}')"
373- ).format (slot_name )
374-
375- self .execute (query = query , dbname = dbname , username = username )
376-
377373 def init (self , initdb_params = None , ** kwargs ):
378374 """
379375 Perform initdb for this node.
@@ -969,26 +965,24 @@ def backup(self, **kwargs):
969965
970966 return NodeBackup (node = self , ** kwargs )
971967
972- def replicate (self , name = None , slot_name = None , ** kwargs ):
968+ def replicate (self , name = None , slot = None , ** kwargs ):
973969 """
974970 Create a binary replica of this node.
975971
976972 Args:
977973 name: replica's application name.
974+ slot: create a replication slot with the specified name.
978975 username: database user name.
979976 xlog_method: a method for collecting the logs ('fetch' | 'stream').
980977 base_dir: the base directory for data files and logs
981978 """
982979
983- if slot_name :
984- self ._create_replication_slot (slot_name , ** kwargs )
985-
986980 backup = self .backup (** kwargs )
987981
988982 # transform backup into a replica
989983 return backup .spawn_replica (name = name ,
990984 destroy = True ,
991- slot_name = slot_name )
985+ slot = slot )
992986
993987 def catchup (self , dbname = None , username = None ):
994988 """
0 commit comments