Skip to content

Commit 368bfca

Browse files
committed
Add --if-not-exists flag to receive-wal
This flag prevents `receive-wal` from exiting with an error when a slot with the same name already exists. This is the same behavior as calling `pg_receivewal` with the same flag. References: #1049.
1 parent ebffcd3 commit 368bfca

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

barman/cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,11 @@ def archive_wal(args):
16831683
help="create the replication slot, if it does not exist",
16841684
action="store_true",
16851685
),
1686+
argument(
1687+
"--if-not-exists",
1688+
help="Do not error out when --create-slot is specified and a slot with the specified name already exists.",
1689+
action="store_true",
1690+
),
16861691
argument(
16871692
"--drop-slot",
16881693
help="drop the replication slot, if it exists",
@@ -1714,7 +1719,7 @@ def receive_wal(args):
17141719
server.kill("receive-wal")
17151720
elif args.create_slot:
17161721
with closing(server):
1717-
server.create_physical_repslot()
1722+
server.create_physical_repslot(ignore_duplicate=args.if_not_exists)
17181723
elif args.drop_slot:
17191724
with closing(server):
17201725
server.drop_repslot()

barman/server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,7 +2809,7 @@ def archive_wal(self, verbose=True):
28092809
"on server %s. Skipping to the next server" % self.config.name
28102810
)
28112811

2812-
def create_physical_repslot(self):
2812+
def create_physical_repslot(self, ignore_duplicate=False):
28132813
"""
28142814
Create a physical replication slot using the streaming connection
28152815
"""
@@ -2852,7 +2852,11 @@ def create_physical_repslot(self):
28522852
self.streaming.create_physical_repslot(self.config.slot_name)
28532853
output.info("Replication slot '%s' created", self.config.slot_name)
28542854
except PostgresDuplicateReplicationSlot:
2855-
output.error("Replication slot '%s' already exists", self.config.slot_name)
2855+
args = "Replication slot '%s' already exists", self.config.slot_name
2856+
if ignore_duplicate:
2857+
output.info(*args)
2858+
else:
2859+
output.error(*args)
28562860
except PostgresReplicationSlotsFull:
28572861
output.error(
28582862
"All replication slots for server '%s' are in use\n"

tests/test_server.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,14 @@ def test_create_physical_repslot(self, capsys):
19531953
out, err = capsys.readouterr()
19541954
assert "Replication slot 'test_repslot' already exists" in err
19551955

1956+
# If the replication slot was already created but duplicates are ignored
1957+
# check that no error is reported
1958+
create_physical_repslot.side_effect = PostgresDuplicateReplicationSlot
1959+
server.create_physical_repslot(ignore_duplicate=True)
1960+
create_physical_repslot.assert_called_with("test_repslot")
1961+
out, err = capsys.readouterr()
1962+
assert not err
1963+
19561964
# Test the method failure if the replication slots
19571965
# on the server are all taken
19581966
create_physical_repslot.side_effect = PostgresReplicationSlotsFull

0 commit comments

Comments
 (0)