Skip to content

Commit 93a028f

Browse files
committed
Properly remove ephemeral replication slots after a crash restart.
Ephemeral slots - slots that shouldn't survive database restarts - weren't properly cleaned up after a immediate/crash restart. They were ignored in the sense that they weren't restored into memory and thus didn't cause unwanted resource retention; but they prevented a new slot with the same name from being created. Now ephemeral slots are fully removed during startup. Backpatch to 9.4 where replication slots where added.
1 parent 32d7889 commit 93a028f

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/backend/replication/slot.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,24 @@ RestoreSlotFromDisk(const char *name)
11921192
(errmsg("replication slot file %s: checksum mismatch, is %u, should be %u",
11931193
path, checksum, cp.checksum)));
11941194

1195+
/*
1196+
* If we crashed with an ephemeral slot active, don't restore but delete
1197+
* it.
1198+
*/
1199+
if (cp.slotdata.persistency != RS_PERSISTENT)
1200+
{
1201+
sprintf(path, "pg_replslot/%s", name);
1202+
1203+
if (!rmtree(path, true))
1204+
{
1205+
ereport(WARNING,
1206+
(errcode_for_file_access(),
1207+
errmsg("could not remove directory \"%s\"", path)));
1208+
}
1209+
fsync_fname("pg_replslot", true);
1210+
return;
1211+
}
1212+
11951213
/* nothing can be active yet, don't lock anything */
11961214
for (i = 0; i < max_replication_slots; i++)
11971215
{
@@ -1206,10 +1224,6 @@ RestoreSlotFromDisk(const char *name)
12061224
memcpy(&slot->data, &cp.slotdata,
12071225
sizeof(ReplicationSlotPersistentData));
12081226

1209-
/* Don't restore the slot if it's not parked as persistent. */
1210-
if (slot->data.persistency != RS_PERSISTENT)
1211-
return;
1212-
12131227
/* initialize in memory state */
12141228
slot->effective_xmin = cp.slotdata.xmin;
12151229
slot->effective_catalog_xmin = cp.slotdata.catalog_xmin;

0 commit comments

Comments
 (0)