Skip to content

Commit 2f6501f

Browse files
committed
Move replication slot release to before_shmem_exit().
Previously, replication slots were released in ProcKill() on error, resulting in reporting replication slot drop of ephemeral slots after the stats subsystem was already shut down. To fix this problem, move replication slot release to a before_shmem_exit() hook that is called before the stats collector shuts down. There wasn't really a good reason for the slot handling to be in ProcKill() anyway. Patch by Masahiko Sawada, with very minor polishing by me. I, Andres, wrote a test for dropping slots during process exit, but there may be some OS dependent issues around the number of times FATAL error messages are displayed due to a still debated libpq issue. So that test will be committed separately / later. Reviewed-By: Kyotaro Horiguchi <horikyota.ntt@gmail.com> Reviewed-By: Andres Freund <andres@anarazel.de> Author: Masahiko Sawada <sawada.mshk@gmail.com> Discussion: https://postgr.es/m/CAD21AoDAeEpAbZEyYJsPZJUmSPaRicVSBObaL7sPaofnKz+9zg@mail.gmail.com
1 parent b45fa79 commit 2f6501f

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

src/backend/replication/slot.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "pgstat.h"
4747
#include "replication/slot.h"
4848
#include "storage/fd.h"
49+
#include "storage/ipc.h"
4950
#include "storage/proc.h"
5051
#include "storage/procarray.h"
5152
#include "utils/builtins.h"
@@ -99,6 +100,7 @@ ReplicationSlot *MyReplicationSlot = NULL;
99100
int max_replication_slots = 0; /* the maximum number of replication
100101
* slots */
101102

103+
static void ReplicationSlotShmemExit(int code, Datum arg);
102104
static void ReplicationSlotDropAcquired(void);
103105
static void ReplicationSlotDropPtr(ReplicationSlot *slot);
104106

@@ -160,6 +162,29 @@ ReplicationSlotsShmemInit(void)
160162
}
161163
}
162164

165+
/*
166+
* Register the callback for replication slot cleanup and releasing.
167+
*/
168+
void
169+
ReplicationSlotInitialize(void)
170+
{
171+
before_shmem_exit(ReplicationSlotShmemExit, 0);
172+
}
173+
174+
/*
175+
* Release and cleanup replication slots.
176+
*/
177+
static void
178+
ReplicationSlotShmemExit(int code, Datum arg)
179+
{
180+
/* Make sure active replication slots are released */
181+
if (MyReplicationSlot != NULL)
182+
ReplicationSlotRelease();
183+
184+
/* Also cleanup all the temporary slots. */
185+
ReplicationSlotCleanup();
186+
}
187+
163188
/*
164189
* Check whether the passed slot name is valid and report errors at elevel.
165190
*

src/backend/storage/lmgr/proc.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,13 +831,6 @@ ProcKill(int code, Datum arg)
831831
/* Cancel any pending condition variable sleep, too */
832832
ConditionVariableCancelSleep();
833833

834-
/* Make sure active replication slots are released */
835-
if (MyReplicationSlot != NULL)
836-
ReplicationSlotRelease();
837-
838-
/* Also cleanup all the temporary slots. */
839-
ReplicationSlotCleanup();
840-
841834
/*
842835
* Detach from any lock group of which we are a member. If the leader
843836
* exist before all other group members, its PGPROC will remain allocated

src/backend/tcop/postgres.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4261,8 +4261,8 @@ PostgresMain(const char *dbname, const char *username)
42614261
* We can't release replication slots inside AbortTransaction() as we
42624262
* need to be able to start and abort transactions while having a slot
42634263
* acquired. But we never need to hold them across top level errors,
4264-
* so releasing here is fine. There's another cleanup in ProcKill()
4265-
* ensuring we'll correctly cleanup on FATAL errors as well.
4264+
* so releasing here is fine. There also is a before_shmem_exit()
4265+
* callback ensuring correct cleanup on FATAL errors.
42664266
*/
42674267
if (MyReplicationSlot != NULL)
42684268
ReplicationSlotRelease();

src/backend/utils/init/postinit.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "pgstat.h"
4444
#include "postmaster/autovacuum.h"
4545
#include "postmaster/postmaster.h"
46+
#include "replication/slot.h"
4647
#include "replication/walsender.h"
4748
#include "storage/bufmgr.h"
4849
#include "storage/fd.h"
@@ -626,6 +627,12 @@ BaseInit(void)
626627
* ever try to insert XLOG.
627628
*/
628629
InitXLogInsert();
630+
631+
/*
632+
* Initialize replication slots after pgstat. The exit hook might need to
633+
* drop ephemeral slots, which in turn triggers stats reporting.
634+
*/
635+
ReplicationSlotInitialize();
629636
}
630637

631638

src/include/replication/slot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ extern void ReplicationSlotSave(void);
206206
extern void ReplicationSlotMarkDirty(void);
207207

208208
/* misc stuff */
209+
extern void ReplicationSlotInitialize(void);
209210
extern bool ReplicationSlotValidateName(const char *name, int elevel);
210211
extern void ReplicationSlotReserveWal(void);
211212
extern void ReplicationSlotsComputeRequiredXmin(bool already_locked);

0 commit comments

Comments
 (0)