Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * slot.c
4 : : * Replication slot management.
5 : : *
6 : : *
7 : : * Copyright (c) 2012-2026, PostgreSQL Global Development Group
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/replication/slot.c
12 : : *
13 : : * NOTES
14 : : *
15 : : * Replication slots are used to keep state about replication streams
16 : : * originating from this cluster. Their primary purpose is to prevent the
17 : : * premature removal of WAL or of old tuple versions in a manner that would
18 : : * interfere with replication; they are also useful for monitoring purposes.
19 : : * Slots need to be permanent (to allow restarts), crash-safe, and allocatable
20 : : * on standbys (to support cascading setups). The requirement that slots be
21 : : * usable on standbys precludes storing them in the system catalogs.
22 : : *
23 : : * Each replication slot gets its own directory inside the directory
24 : : * $PGDATA / PG_REPLSLOT_DIR. Inside that directory the state file will
25 : : * contain the slot's own data. Additional data can be stored alongside that
26 : : * file if required. While the server is running, the state data is also
27 : : * cached in memory for efficiency.
28 : : *
29 : : * ReplicationSlotAllocationLock must be taken in exclusive mode to allocate
30 : : * or free a slot. ReplicationSlotControlLock must be taken in shared mode
31 : : * to iterate over the slots, and in exclusive mode to change the in_use flag
32 : : * of a slot. The remaining data in each slot is protected by its mutex.
33 : : *
34 : : *-------------------------------------------------------------------------
35 : : */
36 : :
37 : : #include "postgres.h"
38 : :
39 : : #include <unistd.h>
40 : : #include <sys/stat.h>
41 : :
42 : : #include "access/transam.h"
43 : : #include "access/xlog_internal.h"
44 : : #include "access/xlogrecovery.h"
45 : : #include "common/file_utils.h"
46 : : #include "common/string.h"
47 : : #include "miscadmin.h"
48 : : #include "pgstat.h"
49 : : #include "postmaster/interrupt.h"
50 : : #include "replication/logicallauncher.h"
51 : : #include "replication/slotsync.h"
52 : : #include "replication/slot.h"
53 : : #include "replication/walsender_private.h"
54 : : #include "storage/fd.h"
55 : : #include "storage/ipc.h"
56 : : #include "storage/proc.h"
57 : : #include "storage/procarray.h"
58 : : #include "storage/subsystems.h"
59 : : #include "utils/builtins.h"
60 : : #include "utils/guc_hooks.h"
61 : : #include "utils/injection_point.h"
62 : : #include "utils/varlena.h"
63 : : #include "utils/wait_event.h"
64 : :
65 : : /*
66 : : * Replication slot on-disk data structure.
67 : : */
68 : : typedef struct ReplicationSlotOnDisk
69 : : {
70 : : /* first part of this struct needs to be version independent */
71 : :
72 : : /* data not covered by checksum */
73 : : uint32 magic;
74 : : pg_crc32c checksum;
75 : :
76 : : /* data covered by checksum */
77 : : uint32 version;
78 : : uint32 length;
79 : :
80 : : /*
81 : : * The actual data in the slot that follows can differ based on the above
82 : : * 'version'.
83 : : */
84 : :
85 : : ReplicationSlotPersistentData slotdata;
86 : : } ReplicationSlotOnDisk;
87 : :
88 : : /*
89 : : * Struct for the configuration of synchronized_standby_slots.
90 : : *
91 : : * Note: this must be a flat representation that can be held in a single chunk
92 : : * of guc_malloc'd memory, so that it can be stored as the "extra" data for the
93 : : * synchronized_standby_slots GUC.
94 : : */
95 : : typedef struct
96 : : {
97 : : /* Number of slot names in the slot_names[] */
98 : : int nslotnames;
99 : :
100 : : /*
101 : : * slot_names contains 'nslotnames' consecutive null-terminated C strings.
102 : : */
103 : : char slot_names[FLEXIBLE_ARRAY_MEMBER];
104 : : } SyncStandbySlotsConfigData;
105 : :
106 : : /*
107 : : * Lookup table for slot invalidation causes.
108 : : */
109 : : typedef struct SlotInvalidationCauseMap
110 : : {
111 : : ReplicationSlotInvalidationCause cause;
112 : : const char *cause_name;
113 : : } SlotInvalidationCauseMap;
114 : :
115 : : static const SlotInvalidationCauseMap SlotInvalidationCauses[] = {
116 : : {RS_INVAL_NONE, "none"},
117 : : {RS_INVAL_WAL_REMOVED, "wal_removed"},
118 : : {RS_INVAL_HORIZON, "rows_removed"},
119 : : {RS_INVAL_WAL_LEVEL, "wal_level_insufficient"},
120 : : {RS_INVAL_IDLE_TIMEOUT, "idle_timeout"},
121 : : };
122 : :
123 : : /*
124 : : * Ensure that the lookup table is up-to-date with the enums defined in
125 : : * ReplicationSlotInvalidationCause.
126 : : */
127 : : StaticAssertDecl(lengthof(SlotInvalidationCauses) == (RS_INVAL_MAX_CAUSES + 1),
128 : : "array length mismatch");
129 : :
130 : : /* size of version independent data */
131 : : #define ReplicationSlotOnDiskConstantSize \
132 : : offsetof(ReplicationSlotOnDisk, slotdata)
133 : : /* size of the part of the slot not covered by the checksum */
134 : : #define ReplicationSlotOnDiskNotChecksummedSize \
135 : : offsetof(ReplicationSlotOnDisk, version)
136 : : /* size of the part covered by the checksum */
137 : : #define ReplicationSlotOnDiskChecksummedSize \
138 : : sizeof(ReplicationSlotOnDisk) - ReplicationSlotOnDiskNotChecksummedSize
139 : : /* size of the slot data that is version dependent */
140 : : #define ReplicationSlotOnDiskV2Size \
141 : : sizeof(ReplicationSlotOnDisk) - ReplicationSlotOnDiskConstantSize
142 : :
143 : : #define SLOT_MAGIC 0x1051CA1 /* format identifier */
144 : : #define SLOT_VERSION 5 /* version for new files */
145 : :
146 : : /* Control array for replication slot management */
147 : : ReplicationSlotCtlData *ReplicationSlotCtl = NULL;
148 : :
149 : : static void ReplicationSlotsShmemRequest(void *arg);
150 : : static void ReplicationSlotsShmemInit(void *arg);
151 : :
152 : : const ShmemCallbacks ReplicationSlotsShmemCallbacks = {
153 : : .request_fn = ReplicationSlotsShmemRequest,
154 : : .init_fn = ReplicationSlotsShmemInit,
155 : : };
156 : :
157 : : /* My backend's replication slot in the shared memory array */
158 : : ReplicationSlot *MyReplicationSlot = NULL;
159 : :
160 : : /* GUC variables */
161 : : int max_replication_slots = 10; /* the maximum number of replication
162 : : * slots */
163 : : int max_repack_replication_slots = 5; /* the maximum number of slots
164 : : * for REPACK */
165 : :
166 : : /*
167 : : * Invalidate replication slots that have remained idle longer than this
168 : : * duration; '0' disables it.
169 : : */
170 : : int idle_replication_slot_timeout_secs = 0;
171 : :
172 : : /*
173 : : * This GUC lists streaming replication standby server slot names that
174 : : * logical WAL sender processes will wait for.
175 : : */
176 : : char *synchronized_standby_slots;
177 : :
178 : : /* This is the parsed and cached configuration for synchronized_standby_slots */
179 : : static SyncStandbySlotsConfigData *synchronized_standby_slots_config;
180 : :
181 : : /*
182 : : * Oldest LSN that has been confirmed to be flushed to the standbys
183 : : * corresponding to the physical slots specified in the synchronized_standby_slots GUC.
184 : : */
185 : : static XLogRecPtr ss_oldest_flush_lsn = InvalidXLogRecPtr;
186 : :
187 : : static void ReplicationSlotShmemExit(int code, Datum arg);
188 : : static bool IsSlotForConflictCheck(const char *name);
189 : : static void ReplicationSlotDropPtr(ReplicationSlot *slot);
190 : :
191 : : /* internal persistency functions */
192 : : static void RestoreSlotFromDisk(const char *name);
193 : : static void CreateSlotOnDisk(ReplicationSlot *slot);
194 : : static void SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel);
195 : :
196 : : /*
197 : : * Register shared memory space needed for replication slots.
198 : : */
199 : : static void
200 : 1321 : ReplicationSlotsShmemRequest(void *arg)
201 : : {
202 : : Size size;
203 : :
204 [ - + ]: 1321 : if (max_replication_slots + max_repack_replication_slots == 0)
205 : 0 : return;
206 : :
207 : 1321 : size = offsetof(ReplicationSlotCtlData, replication_slots);
208 : 1321 : size = add_size(size,
209 : 1321 : mul_size(max_replication_slots + max_repack_replication_slots,
210 : : sizeof(ReplicationSlot)));
211 : 1321 : ShmemRequestStruct(.name = "ReplicationSlot Ctl",
212 : : .size = size,
213 : : .ptr = (void **) &ReplicationSlotCtl,
214 : : );
215 : : }
216 : :
217 : : /*
218 : : * Initialize shared memory for replication slots.
219 : : */
220 : : static void
221 : 1317 : ReplicationSlotsShmemInit(void *arg)
222 : : {
223 [ + + ]: 20857 : for (int i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
224 : : {
225 : 19540 : ReplicationSlot *slot = &ReplicationSlotCtl->replication_slots[i];
226 : :
227 : : /* everything else is zeroed by the memset above */
228 : 19540 : slot->active_proc = INVALID_PROC_NUMBER;
229 : 19540 : SpinLockInit(&slot->mutex);
230 : 19540 : LWLockInitialize(&slot->io_in_progress_lock,
231 : : LWTRANCHE_REPLICATION_SLOT_IO);
232 : 19540 : ConditionVariableInit(&slot->active_cv);
233 : : }
234 : 1317 : }
235 : :
236 : : /*
237 : : * Register the callback for replication slot cleanup and releasing.
238 : : */
239 : : void
240 : 25809 : ReplicationSlotInitialize(void)
241 : : {
242 : 25809 : before_shmem_exit(ReplicationSlotShmemExit, 0);
243 : 25809 : }
244 : :
245 : : /*
246 : : * Release and cleanup replication slots.
247 : : */
248 : : static void
249 : 25809 : ReplicationSlotShmemExit(int code, Datum arg)
250 : : {
251 : : /* Make sure active replication slots are released */
252 [ + + ]: 25809 : if (MyReplicationSlot != NULL)
253 : 335 : ReplicationSlotRelease();
254 : :
255 : : /* Also cleanup all the temporary slots. */
256 : 25809 : ReplicationSlotCleanup(false);
257 : 25809 : }
258 : :
259 : : /*
260 : : * Check whether the passed slot name is valid and report errors at elevel.
261 : : *
262 : : * See comments for ReplicationSlotValidateNameInternal().
263 : : */
264 : : bool
265 : 944 : ReplicationSlotValidateName(const char *name, bool allow_reserved_name,
266 : : int elevel)
267 : : {
268 : : int err_code;
269 : 944 : char *err_msg = NULL;
270 : 944 : char *err_hint = NULL;
271 : :
272 [ + + ]: 944 : if (!ReplicationSlotValidateNameInternal(name, allow_reserved_name,
273 : : &err_code, &err_msg, &err_hint))
274 : : {
275 : : /*
276 : : * Use errmsg_internal() and errhint_internal() instead of errmsg()
277 : : * and errhint(), since the messages from
278 : : * ReplicationSlotValidateNameInternal() are already translated. This
279 : : * avoids double translation.
280 : : */
281 [ + - + + ]: 5 : ereport(elevel,
282 : : errcode(err_code),
283 : : errmsg_internal("%s", err_msg),
284 : : (err_hint != NULL) ? errhint_internal("%s", err_hint) : 0);
285 : :
286 : 0 : pfree(err_msg);
287 [ # # ]: 0 : if (err_hint != NULL)
288 : 0 : pfree(err_hint);
289 : 0 : return false;
290 : : }
291 : :
292 : 939 : return true;
293 : : }
294 : :
295 : : /*
296 : : * Check whether the passed slot name is valid.
297 : : *
298 : : * An error will be reported for a reserved replication slot name if
299 : : * allow_reserved_name is set to false.
300 : : *
301 : : * Slot names may consist out of [a-z0-9_]{1,NAMEDATALEN-1} which should allow
302 : : * the name to be used as a directory name on every supported OS.
303 : : *
304 : : * Returns true if the slot name is valid. Otherwise, returns false and stores
305 : : * the error code, error message, and optional hint in err_code, err_msg, and
306 : : * err_hint, respectively. The caller is responsible for freeing err_msg and
307 : : * err_hint, which are palloc'd.
308 : : */
309 : : bool
310 : 1161 : ReplicationSlotValidateNameInternal(const char *name, bool allow_reserved_name,
311 : : int *err_code, char **err_msg, char **err_hint)
312 : : {
313 : : const char *cp;
314 : :
315 [ + + ]: 1161 : if (strlen(name) == 0)
316 : : {
317 : 4 : *err_code = ERRCODE_INVALID_NAME;
318 : 4 : *err_msg = psprintf(_("replication slot name \"%s\" is too short"), name);
319 : 4 : *err_hint = NULL;
320 : 4 : return false;
321 : : }
322 : :
323 [ - + ]: 1157 : if (strlen(name) >= NAMEDATALEN)
324 : : {
325 : 0 : *err_code = ERRCODE_NAME_TOO_LONG;
326 : 0 : *err_msg = psprintf(_("replication slot name \"%s\" is too long"), name);
327 : 0 : *err_hint = NULL;
328 : 0 : return false;
329 : : }
330 : :
331 [ + + ]: 22058 : for (cp = name; *cp; cp++)
332 : : {
333 [ + + - + ]: 20903 : if (!((*cp >= 'a' && *cp <= 'z')
334 [ + + + + ]: 9855 : || (*cp >= '0' && *cp <= '9')
335 [ + + ]: 2052 : || (*cp == '_')))
336 : : {
337 : 2 : *err_code = ERRCODE_INVALID_NAME;
338 : 2 : *err_msg = psprintf(_("replication slot name \"%s\" contains invalid character"), name);
339 : 2 : *err_hint = psprintf(_("Replication slot names may only contain lower case letters, numbers, and the underscore character."));
340 : 2 : return false;
341 : : }
342 : : }
343 : :
344 [ + + + + ]: 1155 : if (!allow_reserved_name && IsSlotForConflictCheck(name))
345 : : {
346 : 1 : *err_code = ERRCODE_RESERVED_NAME;
347 : 1 : *err_msg = psprintf(_("replication slot name \"%s\" is reserved"), name);
348 : 1 : *err_hint = psprintf(_("The name \"%s\" is reserved for the conflict detection slot."),
349 : : CONFLICT_DETECTION_SLOT);
350 : 1 : return false;
351 : : }
352 : :
353 : 1154 : return true;
354 : : }
355 : :
356 : : /*
357 : : * Return true if the replication slot name is "pg_conflict_detection".
358 : : */
359 : : static bool
360 : 2509 : IsSlotForConflictCheck(const char *name)
361 : : {
362 : 2509 : return (strcmp(name, CONFLICT_DETECTION_SLOT) == 0);
363 : : }
364 : :
365 : : /*
366 : : * Create a new replication slot and mark it as used by this backend.
367 : : *
368 : : * name: Name of the slot
369 : : * db_specific: logical decoding is db specific; if the slot is going to
370 : : * be used for that pass true, otherwise false.
371 : : * two_phase: If enabled, allows decoding of prepared transactions.
372 : : * repack: If true, use a slot from the pool for REPACK.
373 : : * failover: If enabled, allows the slot to be synced to standbys so
374 : : * that logical replication can be resumed after failover.
375 : : * synced: True if the slot is synchronized from the primary server.
376 : : */
377 : : void
378 : 783 : ReplicationSlotCreate(const char *name, bool db_specific,
379 : : ReplicationSlotPersistency persistency,
380 : : bool two_phase, bool repack, bool failover, bool synced)
381 : : {
382 : 783 : ReplicationSlot *slot = NULL;
383 : : int startpoint,
384 : : endpoint;
385 : :
386 : : Assert(MyReplicationSlot == NULL);
387 : :
388 : : /*
389 : : * The logical launcher or pg_upgrade may create or migrate an internal
390 : : * slot, so using a reserved name is allowed in these cases.
391 : : */
392 [ + + + + ]: 783 : ReplicationSlotValidateName(name, IsBinaryUpgrade || IsLogicalLauncher(),
393 : 783 : ERROR);
394 : :
395 [ + + ]: 782 : if (failover)
396 : : {
397 : : /*
398 : : * Do not allow users to create the failover enabled slots on the
399 : : * standby as we do not support sync to the cascading standby.
400 : : *
401 : : * However, failover enabled slots can be created during slot
402 : : * synchronization because we need to retain the same values as the
403 : : * remote slot.
404 : : */
405 [ + + - + ]: 37 : if (RecoveryInProgress() && !IsSyncingReplicationSlots())
406 [ # # ]: 0 : ereport(ERROR,
407 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
408 : : errmsg("cannot enable failover for a replication slot created on the standby"));
409 : :
410 : : /*
411 : : * Do not allow users to create failover enabled temporary slots,
412 : : * because temporary slots will not be synced to the standby.
413 : : *
414 : : * However, failover enabled temporary slots can be created during
415 : : * slot synchronization. See the comments atop slotsync.c for details.
416 : : */
417 [ + + + + ]: 37 : if (persistency == RS_TEMPORARY && !IsSyncingReplicationSlots())
418 [ + - ]: 1 : ereport(ERROR,
419 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
420 : : errmsg("cannot enable failover for a temporary replication slot"));
421 : : }
422 : :
423 : 781 : INJECTION_POINT("replication-slot-create-begin", unconstify(char *, name));
424 : :
425 : : /*
426 : : * If some other backend ran this code concurrently with us, we'd likely
427 : : * both allocate the same slot, and that would be bad. We'd also be at
428 : : * risk of missing a name collision. Also, we don't want to try to create
429 : : * a new slot while somebody's busy cleaning up an old one, because we
430 : : * might both be monkeying with the same directory.
431 : : */
432 : 781 : LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
433 : :
434 : : /*
435 : : * Check for name collision (across the whole array), and identify an
436 : : * allocatable slot (in the array slice specific to our current use case:
437 : : * either general, or REPACK only). We need to hold
438 : : * ReplicationSlotControlLock in shared mode for this, so that nobody else
439 : : * can change the in_use flags while we're looking at them.
440 : : */
441 : 781 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
442 [ + + ]: 781 : startpoint = !repack ? 0 : max_replication_slots;
443 [ + + ]: 781 : endpoint = max_replication_slots + (repack ? max_repack_replication_slots : 0);
444 [ + + ]: 11527 : for (int i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
445 : : {
446 : 10749 : ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i];
447 : :
448 [ + + + + ]: 10749 : if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0)
449 [ + - ]: 3 : ereport(ERROR,
450 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
451 : : errmsg("replication slot \"%s\" already exists", name)));
452 : :
453 [ + + + + ]: 10746 : if (i >= startpoint && i < endpoint &&
454 [ + + + + ]: 6803 : !s->in_use && slot == NULL)
455 : 777 : slot = s;
456 : : }
457 : 778 : LWLockRelease(ReplicationSlotControlLock);
458 : :
459 : : /* If all slots are in use, we're out of luck. */
460 [ + + ]: 778 : if (slot == NULL)
461 [ + - - + ]: 1 : ereport(ERROR,
462 : : (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
463 : : errmsg("all replication slots are in use"),
464 : : errhint("Free one or increase \"%s\".",
465 : : repack ? "max_repack_replication_slots" : "max_replication_slots")));
466 : :
467 : : /*
468 : : * Since this slot is not in use, nobody should be looking at any part of
469 : : * it other than the in_use field unless they're trying to allocate it.
470 : : * And since we hold ReplicationSlotAllocationLock, nobody except us can
471 : : * be doing that. So it's safe to initialize the slot.
472 : : */
473 : : Assert(!slot->in_use);
474 : : Assert(slot->active_proc == INVALID_PROC_NUMBER);
475 : :
476 : : /* first initialize persistent data */
477 : 777 : memset(&slot->data, 0, sizeof(ReplicationSlotPersistentData));
478 : 777 : namestrcpy(&slot->data.name, name);
479 [ + + ]: 777 : slot->data.database = db_specific ? MyDatabaseId : InvalidOid;
480 : 777 : slot->data.persistency = persistency;
481 : 777 : slot->data.two_phase = two_phase;
482 : 777 : slot->data.two_phase_at = InvalidXLogRecPtr;
483 : 777 : slot->data.failover = failover;
484 : 777 : slot->data.synced = synced;
485 : :
486 : : /* and then data only present in shared memory */
487 : 777 : slot->just_dirtied = false;
488 : 777 : slot->dirty = false;
489 : 777 : slot->effective_xmin = InvalidTransactionId;
490 : 777 : slot->effective_catalog_xmin = InvalidTransactionId;
491 : 777 : slot->candidate_catalog_xmin = InvalidTransactionId;
492 : 777 : slot->candidate_xmin_lsn = InvalidXLogRecPtr;
493 : 777 : slot->candidate_restart_valid = InvalidXLogRecPtr;
494 : 777 : slot->candidate_restart_lsn = InvalidXLogRecPtr;
495 : 777 : slot->last_saved_confirmed_flush = InvalidXLogRecPtr;
496 : 777 : slot->last_saved_restart_lsn = InvalidXLogRecPtr;
497 : 777 : slot->inactive_since = 0;
498 : 777 : slot->slotsync_skip_reason = SS_SKIP_NONE;
499 : :
500 : : /*
501 : : * Create the slot on disk. We haven't actually marked the slot allocated
502 : : * yet, so no special cleanup is required if this errors out.
503 : : */
504 : 777 : CreateSlotOnDisk(slot);
505 : :
506 : : /*
507 : : * We need to briefly prevent any other backend from iterating over the
508 : : * slots while we flip the in_use flag. We also need to set the active
509 : : * flag while holding the ControlLock as otherwise a concurrent
510 : : * ReplicationSlotAcquire() could acquire the slot as well.
511 : : */
512 : 777 : LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
513 : :
514 : 777 : slot->in_use = true;
515 : :
516 : : /* We can now mark the slot active, and that makes it our slot. */
517 : 777 : SpinLockAcquire(&slot->mutex);
518 : : Assert(slot->active_proc == INVALID_PROC_NUMBER);
519 : 777 : slot->active_proc = MyProcNumber;
520 : 777 : SpinLockRelease(&slot->mutex);
521 : 777 : MyReplicationSlot = slot;
522 : :
523 : 777 : LWLockRelease(ReplicationSlotControlLock);
524 : :
525 : : /*
526 : : * Create statistics entry for the new logical slot. We don't collect any
527 : : * stats for physical slots, so no need to create an entry for the same.
528 : : * See ReplicationSlotDropPtr for why we need to do this before releasing
529 : : * ReplicationSlotAllocationLock.
530 : : */
531 [ + + ]: 777 : if (SlotIsLogical(slot))
532 : 560 : pgstat_create_replslot(slot);
533 : :
534 : : /*
535 : : * Now that the slot has been marked as in_use and active, it's safe to
536 : : * let somebody else try to allocate a slot.
537 : : */
538 : 777 : LWLockRelease(ReplicationSlotAllocationLock);
539 : :
540 : : /* Let everybody know we've modified this slot */
541 : 777 : ConditionVariableBroadcast(&slot->active_cv);
542 : 777 : }
543 : :
544 : : /*
545 : : * Search for the named replication slot.
546 : : *
547 : : * Return the replication slot if found, otherwise NULL.
548 : : */
549 : : ReplicationSlot *
550 : 2283 : SearchNamedReplicationSlot(const char *name, bool need_lock)
551 : : {
552 : : int i;
553 : 2283 : ReplicationSlot *slot = NULL;
554 : :
555 [ + + ]: 2283 : if (need_lock)
556 : 693 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
557 : :
558 [ + + ]: 11862 : for (i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
559 : : {
560 : 11291 : ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i];
561 : :
562 [ + + + + ]: 11291 : if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0)
563 : : {
564 : 1712 : slot = s;
565 : 1712 : break;
566 : : }
567 : : }
568 : :
569 [ + + ]: 2283 : if (need_lock)
570 : 693 : LWLockRelease(ReplicationSlotControlLock);
571 : :
572 : 2283 : return slot;
573 : : }
574 : :
575 : : /*
576 : : * Return the index of the replication slot in
577 : : * ReplicationSlotCtl->replication_slots.
578 : : *
579 : : * This is mainly useful to have an efficient key for storing replication slot
580 : : * stats.
581 : : */
582 : : int
583 : 9200 : ReplicationSlotIndex(ReplicationSlot *slot)
584 : : {
585 : : Assert(slot >= ReplicationSlotCtl->replication_slots &&
586 : : slot < ReplicationSlotCtl->replication_slots +
587 : : (max_replication_slots + max_repack_replication_slots));
588 : :
589 : 9200 : return slot - ReplicationSlotCtl->replication_slots;
590 : : }
591 : :
592 : : /*
593 : : * If the slot at 'index' is unused, return false. Otherwise 'name' is set to
594 : : * the slot's name and true is returned.
595 : : *
596 : : * This likely is only useful for pgstat_replslot.c during shutdown, in other
597 : : * cases there are obvious TOCTOU issues.
598 : : */
599 : : bool
600 : 123 : ReplicationSlotName(int index, Name name)
601 : : {
602 : : ReplicationSlot *slot;
603 : : bool found;
604 : :
605 : 123 : slot = &ReplicationSlotCtl->replication_slots[index];
606 : :
607 : : /*
608 : : * Ensure that the slot cannot be dropped while we copy the name. Don't
609 : : * need the spinlock as the name of an existing slot cannot change.
610 : : */
611 : 123 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
612 : 123 : found = slot->in_use;
613 [ + - ]: 123 : if (slot->in_use)
614 : 123 : namestrcpy(name, NameStr(slot->data.name));
615 : 123 : LWLockRelease(ReplicationSlotControlLock);
616 : :
617 : 123 : return found;
618 : : }
619 : :
620 : : /*
621 : : * Find a previously created slot and mark it as used by this process.
622 : : *
623 : : * An error is raised if nowait is true and the slot is currently in use. If
624 : : * nowait is false, we sleep until the slot is released by the owning process.
625 : : *
626 : : * An error is raised if error_if_invalid is true and the slot is found to
627 : : * be invalid. It should always be set to true, except when we are temporarily
628 : : * acquiring the slot and don't intend to change it.
629 : : */
630 : : void
631 : 1504 : ReplicationSlotAcquire(const char *name, bool nowait, bool error_if_invalid)
632 : : {
633 : : ReplicationSlot *s;
634 : : ProcNumber active_proc;
635 : : int active_pid;
636 : :
637 : : Assert(name != NULL);
638 : :
639 : 1504 : retry:
640 : : Assert(MyReplicationSlot == NULL);
641 : :
642 : 1504 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
643 : :
644 : : /* Check if the slot exists with the given name. */
645 : 1504 : s = SearchNamedReplicationSlot(name, false);
646 [ + + - + ]: 1504 : if (s == NULL || !s->in_use)
647 : : {
648 : 11 : LWLockRelease(ReplicationSlotControlLock);
649 : :
650 [ + - ]: 11 : ereport(ERROR,
651 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
652 : : errmsg("replication slot \"%s\" does not exist",
653 : : name)));
654 : : }
655 : :
656 : : /*
657 : : * Do not allow users to acquire the reserved slot. This scenario may
658 : : * occur if the launcher that owns the slot has terminated unexpectedly
659 : : * due to an error, and a backend process attempts to reuse the slot.
660 : : */
661 [ + + - + ]: 1493 : if (!IsLogicalLauncher() && IsSlotForConflictCheck(name))
662 [ # # ]: 0 : ereport(ERROR,
663 : : errcode(ERRCODE_UNDEFINED_OBJECT),
664 : : errmsg("cannot acquire replication slot \"%s\"", name),
665 : : errdetail("The slot is reserved for conflict detection and can only be acquired by logical replication launcher."));
666 : :
667 : : /*
668 : : * This is the slot we want; check if it's active under some other
669 : : * process. In single user mode, we don't need this check.
670 : : */
671 [ + + ]: 1493 : if (IsUnderPostmaster)
672 : : {
673 : : /*
674 : : * Get ready to sleep on the slot in case it is active. (We may end
675 : : * up not sleeping, but we don't want to do this while holding the
676 : : * spinlock.)
677 : : */
678 [ + + ]: 1488 : if (!nowait)
679 : 307 : ConditionVariablePrepareToSleep(&s->active_cv);
680 : :
681 : : /*
682 : : * It is important to reset the inactive_since under spinlock here to
683 : : * avoid race conditions with slot invalidation. See comments related
684 : : * to inactive_since in InvalidatePossiblyObsoleteSlot.
685 : : */
686 : 1488 : SpinLockAcquire(&s->mutex);
687 [ + + ]: 1488 : if (s->active_proc == INVALID_PROC_NUMBER)
688 : 1316 : s->active_proc = MyProcNumber;
689 : 1488 : active_proc = s->active_proc;
690 : 1488 : ReplicationSlotSetInactiveSince(s, 0, false);
691 : 1488 : SpinLockRelease(&s->mutex);
692 : : }
693 : : else
694 : : {
695 : 5 : s->active_proc = active_proc = MyProcNumber;
696 : 5 : ReplicationSlotSetInactiveSince(s, 0, true);
697 : : }
698 : 1493 : active_pid = GetPGProcByNumber(active_proc)->pid;
699 : 1493 : LWLockRelease(ReplicationSlotControlLock);
700 : :
701 : : /*
702 : : * If we found the slot but it's already active in another process, we
703 : : * wait until the owning process signals us that it's been released, or
704 : : * error out.
705 : : */
706 [ - + ]: 1493 : if (active_proc != MyProcNumber)
707 : : {
708 [ # # ]: 0 : if (!nowait)
709 : : {
710 : : /* Wait here until we get signaled, and then restart */
711 : 0 : ConditionVariableSleep(&s->active_cv,
712 : : WAIT_EVENT_REPLICATION_SLOT_DROP);
713 : 0 : ConditionVariableCancelSleep();
714 : 0 : goto retry;
715 : : }
716 : :
717 [ # # ]: 0 : ereport(ERROR,
718 : : (errcode(ERRCODE_OBJECT_IN_USE),
719 : : errmsg("replication slot \"%s\" is active for PID %d",
720 : : NameStr(s->data.name), active_pid)));
721 : : }
722 [ + + ]: 1493 : else if (!nowait)
723 : 307 : ConditionVariableCancelSleep(); /* no sleep needed after all */
724 : :
725 : : /* We made this slot active, so it's ours now. */
726 : 1493 : MyReplicationSlot = s;
727 : :
728 : : /*
729 : : * We need to check for invalidation after making the slot ours to avoid
730 : : * the possible race condition with the checkpointer that can otherwise
731 : : * invalidate the slot immediately after the check.
732 : : */
733 [ + + + + ]: 1493 : if (error_if_invalid && s->data.invalidated != RS_INVAL_NONE)
734 [ + - ]: 7 : ereport(ERROR,
735 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
736 : : errmsg("can no longer access replication slot \"%s\"",
737 : : NameStr(s->data.name)),
738 : : errdetail("This replication slot has been invalidated due to \"%s\".",
739 : : GetSlotInvalidationCauseName(s->data.invalidated)));
740 : :
741 : : /* Let everybody know we've modified this slot */
742 : 1486 : ConditionVariableBroadcast(&s->active_cv);
743 : :
744 : : /*
745 : : * The call to pgstat_acquire_replslot() protects against stats for a
746 : : * different slot, from before a restart or such, being present during
747 : : * pgstat_report_replslot().
748 : : */
749 [ + + ]: 1486 : if (SlotIsLogical(s))
750 : 1240 : pgstat_acquire_replslot(s);
751 : :
752 : :
753 [ + + ]: 1486 : if (am_walsender)
754 : : {
755 [ + - + - : 1030 : ereport(log_replication_commands ? LOG : DEBUG1,
+ + ]
756 : : SlotIsLogical(s)
757 : : ? errmsg("acquired logical replication slot \"%s\"",
758 : : NameStr(s->data.name))
759 : : : errmsg("acquired physical replication slot \"%s\"",
760 : : NameStr(s->data.name)));
761 : : }
762 : 1486 : }
763 : :
764 : : /*
765 : : * Release the replication slot that this backend considers to own.
766 : : *
767 : : * This or another backend can re-acquire the slot later.
768 : : * Resources this slot requires will be preserved.
769 : : */
770 : : void
771 : 1809 : ReplicationSlotRelease(void)
772 : : {
773 : 1809 : ReplicationSlot *slot = MyReplicationSlot;
774 : 1809 : char *slotname = NULL; /* keep compiler quiet */
775 : : bool is_logical;
776 : 1809 : TimestampTz now = 0;
777 : :
778 : : Assert(slot != NULL && slot->active_proc != INVALID_PROC_NUMBER);
779 : :
780 : 1809 : is_logical = SlotIsLogical(slot);
781 : :
782 [ + + ]: 1809 : if (am_walsender)
783 : 1276 : slotname = pstrdup(NameStr(slot->data.name));
784 : :
785 [ + + ]: 1809 : if (slot->data.persistency == RS_EPHEMERAL)
786 : : {
787 : : /*
788 : : * If slot is ephemeral, we drop it upon release, and request logical
789 : : * decoding be disabled.
790 : : */
791 : 12 : ReplicationSlotDropAcquired(is_logical);
792 : : }
793 : : else
794 : : {
795 : : /*
796 : : * If slot needed to temporarily restrain both data and catalog xmin
797 : : * to create the catalog snapshot, remove that temporary constraint.
798 : : * Snapshots can only be exported while the initial snapshot is still
799 : : * acquired.
800 : : */
801 [ + + ]: 1797 : if (!TransactionIdIsValid(slot->data.xmin) &&
802 [ + + ]: 1761 : TransactionIdIsValid(slot->effective_xmin))
803 : : {
804 : 222 : SpinLockAcquire(&slot->mutex);
805 : 222 : slot->effective_xmin = InvalidTransactionId;
806 : 222 : SpinLockRelease(&slot->mutex);
807 : 222 : ReplicationSlotsComputeRequiredXmin(false);
808 : : }
809 : :
810 : : /*
811 : : * Set the time since the slot has become inactive. We get the current
812 : : * time beforehand to avoid system call while holding the spinlock.
813 : : */
814 : 1797 : now = GetCurrentTimestamp();
815 : :
816 [ + + ]: 1797 : if (slot->data.persistency == RS_PERSISTENT)
817 : : {
818 : : /*
819 : : * Mark persistent slot inactive. We're not freeing it, just
820 : : * disconnecting, but wake up others that may be waiting for it.
821 : : */
822 : 1452 : SpinLockAcquire(&slot->mutex);
823 : 1452 : slot->active_proc = INVALID_PROC_NUMBER;
824 : 1452 : ReplicationSlotSetInactiveSince(slot, now, false);
825 : 1452 : SpinLockRelease(&slot->mutex);
826 : 1452 : ConditionVariableBroadcast(&slot->active_cv);
827 : : }
828 : : else
829 : 345 : ReplicationSlotSetInactiveSince(slot, now, true);
830 : :
831 : 1797 : MyReplicationSlot = NULL;
832 : : }
833 : :
834 : : /*
835 : : * Update statusFlags only if PROC_IN_LOGICAL_DECODING is set. An
836 : : * auxiliary process (checkpointer or startup process) may invalidate a
837 : : * slot. Auxiliary processes are not in the proc array, and pgxactoff
838 : : * would point at something which is not its own.
839 : : */
840 [ + + ]: 1809 : if (MyProc->statusFlags & PROC_IN_LOGICAL_DECODING)
841 : : {
842 : 659 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
843 : 659 : MyProc->statusFlags &= ~PROC_IN_LOGICAL_DECODING;
844 : 659 : ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
845 : 659 : LWLockRelease(ProcArrayLock);
846 : : }
847 : :
848 [ + + ]: 1809 : if (am_walsender)
849 : : {
850 [ + - + - : 1276 : ereport(log_replication_commands ? LOG : DEBUG1,
+ + ]
851 : : is_logical
852 : : ? errmsg("released logical replication slot \"%s\"",
853 : : slotname)
854 : : : errmsg("released physical replication slot \"%s\"",
855 : : slotname));
856 : :
857 : 1276 : pfree(slotname);
858 : : }
859 : 1809 : }
860 : :
861 : : /*
862 : : * Cleanup temporary slots created in current session.
863 : : *
864 : : * Cleanup only synced temporary slots if 'synced_only' is true, else
865 : : * cleanup all temporary slots.
866 : : *
867 : : * If it drops the last logical slot in the cluster, requests to disable
868 : : * logical decoding.
869 : : */
870 : : void
871 : 57009 : ReplicationSlotCleanup(bool synced_only)
872 : : {
873 : : int i;
874 : : bool found_valid_logicalslot;
875 : 57009 : bool dropped_logical = false;
876 : :
877 : : Assert(MyReplicationSlot == NULL);
878 : :
879 : 57182 : restart:
880 : 57182 : found_valid_logicalslot = false;
881 : 57182 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
882 [ + + ]: 906316 : for (i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
883 : : {
884 : 849307 : ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i];
885 : :
886 [ + + ]: 849307 : if (!s->in_use)
887 : 833388 : continue;
888 : :
889 : 15919 : SpinLockAcquire(&s->mutex);
890 : :
891 : 31838 : found_valid_logicalslot |=
892 [ + + + + ]: 15919 : (SlotIsLogical(s) && s->data.invalidated == RS_INVAL_NONE);
893 : :
894 [ + + ]: 15919 : if ((s->active_proc == MyProcNumber &&
895 [ - + - - ]: 173 : (!synced_only || s->data.synced)))
896 : : {
897 : : Assert(s->data.persistency == RS_TEMPORARY);
898 : 173 : SpinLockRelease(&s->mutex);
899 : 173 : LWLockRelease(ReplicationSlotControlLock); /* avoid deadlock */
900 : :
901 [ + + ]: 173 : if (SlotIsLogical(s))
902 : 11 : dropped_logical = true;
903 : :
904 : 173 : ReplicationSlotDropPtr(s);
905 : :
906 : 173 : ConditionVariableBroadcast(&s->active_cv);
907 : 173 : goto restart;
908 : : }
909 : : else
910 : 15746 : SpinLockRelease(&s->mutex);
911 : : }
912 : :
913 : 57009 : LWLockRelease(ReplicationSlotControlLock);
914 : :
915 [ + + + + ]: 57009 : if (dropped_logical && !found_valid_logicalslot)
916 : 4 : RequestDisableLogicalDecoding();
917 : 57009 : }
918 : :
919 : : /*
920 : : * Permanently drop the replication slot identified by the passed-in name.
921 : : *
922 : : * If this is a logical slot, request that logical decoding be disabled.
923 : : */
924 : : void
925 : 467 : ReplicationSlotDrop(const char *name, bool nowait)
926 : : {
927 : 467 : ReplicationSlotAcquire(name, nowait, false);
928 : :
929 : : /*
930 : : * Do not allow users to drop the slots which are currently being synced
931 : : * from the primary to the standby.
932 : : */
933 [ + + + + ]: 459 : if (RecoveryInProgress() && MyReplicationSlot->data.synced)
934 [ + - ]: 1 : ereport(ERROR,
935 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
936 : : errmsg("cannot drop replication slot \"%s\"", name),
937 : : errdetail("This replication slot is being synchronized from the primary server."));
938 : :
939 : 458 : ReplicationSlotDropAcquired(SlotIsLogical(MyReplicationSlot));
940 : 458 : }
941 : :
942 : : /*
943 : : * Change the definition of the slot identified by the specified name.
944 : : *
945 : : * Altering the two_phase property of a slot requires caution on the
946 : : * client-side. Enabling it at any random point during decoding has the
947 : : * risk that transactions prepared before this change may be skipped by
948 : : * the decoder, leading to missing prepare records on the client. So, we
949 : : * enable it for subscription related slots only once the initial tablesync
950 : : * is finished. See comments atop worker.c. Disabling it is safe only when
951 : : * there are no pending prepared transaction, otherwise, the changes of
952 : : * already prepared transactions can be replicated again along with their
953 : : * corresponding commit leading to duplicate data or errors.
954 : : */
955 : : void
956 : 7 : ReplicationSlotAlter(const char *name, const bool *failover,
957 : : const bool *two_phase)
958 : : {
959 : 7 : bool update_slot = false;
960 : :
961 : : Assert(MyReplicationSlot == NULL);
962 : : Assert(failover || two_phase);
963 : :
964 : 7 : ReplicationSlotAcquire(name, false, true);
965 : :
966 [ - + ]: 6 : if (SlotIsPhysical(MyReplicationSlot))
967 [ # # ]: 0 : ereport(ERROR,
968 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
969 : : errmsg("cannot use %s with a physical replication slot",
970 : : "ALTER_REPLICATION_SLOT"));
971 : :
972 [ + + ]: 6 : if (RecoveryInProgress())
973 : : {
974 : : /*
975 : : * Do not allow users to alter the slots which are currently being
976 : : * synced from the primary to the standby.
977 : : */
978 [ + - ]: 1 : if (MyReplicationSlot->data.synced)
979 [ + - ]: 1 : ereport(ERROR,
980 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
981 : : errmsg("cannot alter replication slot \"%s\"", name),
982 : : errdetail("This replication slot is being synchronized from the primary server."));
983 : :
984 : : /*
985 : : * Do not allow users to enable failover on the standby as we do not
986 : : * support sync to the cascading standby.
987 : : */
988 [ # # # # ]: 0 : if (failover && *failover)
989 [ # # ]: 0 : ereport(ERROR,
990 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
991 : : errmsg("cannot enable failover for a replication slot"
992 : : " on the standby"));
993 : : }
994 : :
995 [ + + ]: 5 : if (failover)
996 : : {
997 : : /*
998 : : * Do not allow users to enable failover for temporary slots as we do
999 : : * not support syncing temporary slots to the standby.
1000 : : */
1001 [ + + - + ]: 4 : if (*failover && MyReplicationSlot->data.persistency == RS_TEMPORARY)
1002 [ # # ]: 0 : ereport(ERROR,
1003 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1004 : : errmsg("cannot enable failover for a temporary replication slot"));
1005 : :
1006 [ + - ]: 4 : if (MyReplicationSlot->data.failover != *failover)
1007 : : {
1008 : 4 : SpinLockAcquire(&MyReplicationSlot->mutex);
1009 : 4 : MyReplicationSlot->data.failover = *failover;
1010 : 4 : SpinLockRelease(&MyReplicationSlot->mutex);
1011 : :
1012 : 4 : update_slot = true;
1013 : : }
1014 : : }
1015 : :
1016 [ + + + - ]: 5 : if (two_phase && MyReplicationSlot->data.two_phase != *two_phase)
1017 : : {
1018 : 1 : SpinLockAcquire(&MyReplicationSlot->mutex);
1019 : 1 : MyReplicationSlot->data.two_phase = *two_phase;
1020 : 1 : SpinLockRelease(&MyReplicationSlot->mutex);
1021 : :
1022 : 1 : update_slot = true;
1023 : : }
1024 : :
1025 [ + - ]: 5 : if (update_slot)
1026 : : {
1027 : 5 : ReplicationSlotMarkDirty();
1028 : 5 : ReplicationSlotSave();
1029 : : }
1030 : :
1031 : 5 : ReplicationSlotRelease();
1032 : 5 : }
1033 : :
1034 : : /*
1035 : : * Permanently drop the currently acquired replication slot.
1036 : : *
1037 : : * If caller requests it, have checkpointer attempt to disable logical
1038 : : * decoding. Obviously, this should only be done if the slot is logical.
1039 : : */
1040 : : void
1041 : 494 : ReplicationSlotDropAcquired(bool try_disable)
1042 : : {
1043 : : ReplicationSlot *slot;
1044 : :
1045 : : Assert(MyReplicationSlot != NULL);
1046 : 494 : slot = MyReplicationSlot;
1047 : :
1048 : : /* Can only disable logical decoding if slot is logical */
1049 : : Assert(!try_disable || SlotIsLogical(slot));
1050 : :
1051 : : /* slot isn't acquired anymore */
1052 : 494 : MyReplicationSlot = NULL;
1053 : :
1054 : 494 : ReplicationSlotDropPtr(slot);
1055 : :
1056 [ + + ]: 494 : if (try_disable)
1057 : 463 : RequestDisableLogicalDecoding();
1058 : 494 : }
1059 : :
1060 : : /*
1061 : : * Permanently drop the replication slot which will be released by the point
1062 : : * this function returns.
1063 : : */
1064 : : static void
1065 : 667 : ReplicationSlotDropPtr(ReplicationSlot *slot)
1066 : : {
1067 : : char path[MAXPGPATH];
1068 : : char tmppath[MAXPGPATH];
1069 : :
1070 : : /*
1071 : : * If some other backend ran this code concurrently with us, we might try
1072 : : * to delete a slot with a certain name while someone else was trying to
1073 : : * create a slot with the same name.
1074 : : */
1075 : 667 : LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
1076 : :
1077 : : /* Generate pathnames. */
1078 : 667 : sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(slot->data.name));
1079 : 667 : sprintf(tmppath, "%s/%s.tmp", PG_REPLSLOT_DIR, NameStr(slot->data.name));
1080 : :
1081 : : /*
1082 : : * Rename the slot directory on disk, so that we'll no longer recognize
1083 : : * this as a valid slot. Note that if this fails, we've got to mark the
1084 : : * slot inactive before bailing out. If we're dropping an ephemeral or a
1085 : : * temporary slot, we better never fail hard as the caller won't expect
1086 : : * the slot to survive and this might get called during error handling.
1087 : : */
1088 [ + - ]: 667 : if (rename(path, tmppath) == 0)
1089 : : {
1090 : : /*
1091 : : * We need to fsync() the directory we just renamed and its parent to
1092 : : * make sure that our changes are on disk in a crash-safe fashion. If
1093 : : * fsync() fails, we can't be sure whether the changes are on disk or
1094 : : * not. For now, we handle that by panicking;
1095 : : * StartupReplicationSlots() will try to straighten it out after
1096 : : * restart.
1097 : : */
1098 : 667 : START_CRIT_SECTION();
1099 : 667 : fsync_fname(tmppath, true);
1100 : 667 : fsync_fname(PG_REPLSLOT_DIR, true);
1101 : 667 : END_CRIT_SECTION();
1102 : : }
1103 : : else
1104 : : {
1105 : 0 : bool fail_softly = slot->data.persistency != RS_PERSISTENT;
1106 : :
1107 : 0 : SpinLockAcquire(&slot->mutex);
1108 : 0 : slot->active_proc = INVALID_PROC_NUMBER;
1109 : 0 : SpinLockRelease(&slot->mutex);
1110 : :
1111 : : /* wake up anyone waiting on this slot */
1112 : 0 : ConditionVariableBroadcast(&slot->active_cv);
1113 : :
1114 [ # # # # ]: 0 : ereport(fail_softly ? WARNING : ERROR,
1115 : : (errcode_for_file_access(),
1116 : : errmsg("could not rename file \"%s\" to \"%s\": %m",
1117 : : path, tmppath)));
1118 : : }
1119 : :
1120 : : /*
1121 : : * The slot is definitely gone. Lock out concurrent scans of the array
1122 : : * long enough to kill it. It's OK to clear the active PID here without
1123 : : * grabbing the mutex because nobody else can be scanning the array here,
1124 : : * and nobody can be attached to this slot and thus access it without
1125 : : * scanning the array.
1126 : : *
1127 : : * Also wake up processes waiting for it.
1128 : : */
1129 : 667 : LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
1130 : 667 : slot->active_proc = INVALID_PROC_NUMBER;
1131 : 667 : slot->in_use = false;
1132 : 667 : LWLockRelease(ReplicationSlotControlLock);
1133 : 667 : ConditionVariableBroadcast(&slot->active_cv);
1134 : :
1135 : : /*
1136 : : * Slot is dead and doesn't prevent resource removal anymore, recompute
1137 : : * limits.
1138 : : */
1139 : 667 : ReplicationSlotsComputeRequiredXmin(false);
1140 : 667 : ReplicationSlotsComputeRequiredLSN();
1141 : :
1142 : : /*
1143 : : * If removing the directory fails, the worst thing that will happen is
1144 : : * that the user won't be able to create a new slot with the same name
1145 : : * until the next server restart. We warn about it, but that's all.
1146 : : */
1147 [ - + ]: 667 : if (!rmtree(tmppath, true))
1148 [ # # ]: 0 : ereport(WARNING,
1149 : : (errmsg("could not remove directory \"%s\"", tmppath)));
1150 : :
1151 : : /*
1152 : : * Drop the statistics entry for the replication slot. Do this while
1153 : : * holding ReplicationSlotAllocationLock so that we don't drop a
1154 : : * statistics entry for another slot with the same name just created in
1155 : : * another session.
1156 : : */
1157 [ + + ]: 667 : if (SlotIsLogical(slot))
1158 : 484 : pgstat_drop_replslot(slot);
1159 : :
1160 : : /*
1161 : : * We release this at the very end, so that nobody starts trying to create
1162 : : * a slot while we're still cleaning up the detritus of the old one.
1163 : : */
1164 : 667 : LWLockRelease(ReplicationSlotAllocationLock);
1165 : 667 : }
1166 : :
1167 : : /*
1168 : : * Serialize the currently acquired slot's state from memory to disk, thereby
1169 : : * guaranteeing the current state will survive a crash.
1170 : : */
1171 : : void
1172 : 1591 : ReplicationSlotSave(void)
1173 : : {
1174 : : char path[MAXPGPATH];
1175 : :
1176 : : Assert(MyReplicationSlot != NULL);
1177 : :
1178 : 1591 : sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(MyReplicationSlot->data.name));
1179 : 1591 : SaveSlotToPath(MyReplicationSlot, path, ERROR);
1180 : 1591 : }
1181 : :
1182 : : /*
1183 : : * Signal that it would be useful if the currently acquired slot would be
1184 : : * flushed out to disk.
1185 : : *
1186 : : * Note that the actual flush to disk can be delayed for a long time, if
1187 : : * required for correctness explicitly do a ReplicationSlotSave().
1188 : : */
1189 : : void
1190 : 41376 : ReplicationSlotMarkDirty(void)
1191 : : {
1192 : 41376 : ReplicationSlot *slot = MyReplicationSlot;
1193 : :
1194 : : Assert(MyReplicationSlot != NULL);
1195 : :
1196 : 41376 : SpinLockAcquire(&slot->mutex);
1197 : 41376 : MyReplicationSlot->just_dirtied = true;
1198 : 41376 : MyReplicationSlot->dirty = true;
1199 : 41376 : SpinLockRelease(&slot->mutex);
1200 : 41376 : }
1201 : :
1202 : : /*
1203 : : * Convert a slot that's marked as RS_EPHEMERAL or RS_TEMPORARY to a
1204 : : * RS_PERSISTENT slot, guaranteeing it will be there after an eventual crash.
1205 : : */
1206 : : void
1207 : 521 : ReplicationSlotPersist(void)
1208 : : {
1209 : 521 : ReplicationSlot *slot = MyReplicationSlot;
1210 : :
1211 : : Assert(slot != NULL);
1212 : : Assert(slot->data.persistency != RS_PERSISTENT);
1213 : :
1214 : 521 : SpinLockAcquire(&slot->mutex);
1215 : 521 : slot->data.persistency = RS_PERSISTENT;
1216 : 521 : SpinLockRelease(&slot->mutex);
1217 : :
1218 : 521 : ReplicationSlotMarkDirty();
1219 : 521 : ReplicationSlotSave();
1220 : 521 : }
1221 : :
1222 : : /*
1223 : : * Compute the oldest xmin across all slots and store it in the ProcArray.
1224 : : *
1225 : : * If already_locked is true, both the ReplicationSlotControlLock and the
1226 : : * ProcArrayLock have already been acquired exclusively. It is crucial that the
1227 : : * caller first acquires the ReplicationSlotControlLock, followed by the
1228 : : * ProcArrayLock, to prevent any undetectable deadlocks since this function
1229 : : * acquires them in that order.
1230 : : */
1231 : : void
1232 : 2815 : ReplicationSlotsComputeRequiredXmin(bool already_locked)
1233 : : {
1234 : : int i;
1235 : 2815 : TransactionId agg_xmin = InvalidTransactionId;
1236 : 2815 : TransactionId agg_catalog_xmin = InvalidTransactionId;
1237 : :
1238 : : Assert(ReplicationSlotCtl != NULL);
1239 : : Assert(!already_locked ||
1240 : : (LWLockHeldByMeInMode(ReplicationSlotControlLock, LW_EXCLUSIVE) &&
1241 : : LWLockHeldByMeInMode(ProcArrayLock, LW_EXCLUSIVE)));
1242 : :
1243 : : /*
1244 : : * Hold the ReplicationSlotControlLock until after updating the slot xmin
1245 : : * values, so no backend updates the initial xmin for newly created slot
1246 : : * concurrently. A shared lock is used here to minimize lock contention,
1247 : : * especially when many slots exist and advancements occur frequently.
1248 : : * This is safe since an exclusive lock is taken during initial slot xmin
1249 : : * update in slot creation.
1250 : : *
1251 : : * One might think that we can hold the ProcArrayLock exclusively and
1252 : : * update the slot xmin values, but it could increase lock contention on
1253 : : * the ProcArrayLock, which is not great since this function can be called
1254 : : * at non-negligible frequency.
1255 : : *
1256 : : * Concurrent invocation of this function may cause the computed slot xmin
1257 : : * to regress. However, this is harmless because tuples prior to the most
1258 : : * recent xmin are no longer useful once advancement occurs (see
1259 : : * LogicalConfirmReceivedLocation where the slot's xmin value is flushed
1260 : : * before updating the effective_xmin). Thus, such regression merely
1261 : : * prevents VACUUM from prematurely removing tuples without causing the
1262 : : * early deletion of required data.
1263 : : */
1264 [ + + ]: 2815 : if (!already_locked)
1265 : 2258 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1266 : :
1267 [ + + ]: 42860 : for (i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
1268 : : {
1269 : 40045 : ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i];
1270 : : TransactionId effective_xmin;
1271 : : TransactionId effective_catalog_xmin;
1272 : : bool invalidated;
1273 : :
1274 [ + + ]: 40045 : if (!s->in_use)
1275 : 37330 : continue;
1276 : :
1277 : 2715 : SpinLockAcquire(&s->mutex);
1278 : 2715 : effective_xmin = s->effective_xmin;
1279 : 2715 : effective_catalog_xmin = s->effective_catalog_xmin;
1280 : 2715 : invalidated = s->data.invalidated != RS_INVAL_NONE;
1281 : 2715 : SpinLockRelease(&s->mutex);
1282 : :
1283 : : /* invalidated slots need not apply */
1284 [ + + ]: 2715 : if (invalidated)
1285 : 26 : continue;
1286 : :
1287 : : /* check the data xmin */
1288 [ + + + + ]: 2689 : if (TransactionIdIsValid(effective_xmin) &&
1289 [ - + ]: 14 : (!TransactionIdIsValid(agg_xmin) ||
1290 : 14 : TransactionIdPrecedes(effective_xmin, agg_xmin)))
1291 : 409 : agg_xmin = effective_xmin;
1292 : :
1293 : : /* check the catalog xmin */
1294 [ + + + + ]: 2689 : if (TransactionIdIsValid(effective_catalog_xmin) &&
1295 [ + + ]: 1132 : (!TransactionIdIsValid(agg_catalog_xmin) ||
1296 : 1132 : TransactionIdPrecedes(effective_catalog_xmin, agg_catalog_xmin)))
1297 : 1376 : agg_catalog_xmin = effective_catalog_xmin;
1298 : : }
1299 : :
1300 : 2815 : ProcArraySetReplicationSlotXmin(agg_xmin, agg_catalog_xmin, already_locked);
1301 : :
1302 [ + + ]: 2815 : if (!already_locked)
1303 : 2258 : LWLockRelease(ReplicationSlotControlLock);
1304 : 2815 : }
1305 : :
1306 : : /*
1307 : : * Compute the oldest restart LSN across all slots and inform xlog module.
1308 : : *
1309 : : * Note: while max_slot_wal_keep_size is theoretically relevant for this
1310 : : * purpose, we don't try to account for that, because this module doesn't
1311 : : * know what to compare against.
1312 : : */
1313 : : void
1314 : 42487 : ReplicationSlotsComputeRequiredLSN(void)
1315 : : {
1316 : : int i;
1317 : 42487 : XLogRecPtr min_required = InvalidXLogRecPtr;
1318 : :
1319 : : Assert(ReplicationSlotCtl != NULL);
1320 : :
1321 : 42487 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1322 [ + + ]: 675614 : for (i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
1323 : : {
1324 : 633127 : ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i];
1325 : : XLogRecPtr restart_lsn;
1326 : : XLogRecPtr last_saved_restart_lsn;
1327 : : bool invalidated;
1328 : : ReplicationSlotPersistency persistency;
1329 : :
1330 [ + + ]: 633127 : if (!s->in_use)
1331 : 590121 : continue;
1332 : :
1333 : 43006 : SpinLockAcquire(&s->mutex);
1334 : 43006 : persistency = s->data.persistency;
1335 : 43006 : restart_lsn = s->data.restart_lsn;
1336 : 43006 : invalidated = s->data.invalidated != RS_INVAL_NONE;
1337 : 43006 : last_saved_restart_lsn = s->last_saved_restart_lsn;
1338 : 43006 : SpinLockRelease(&s->mutex);
1339 : :
1340 : : /* invalidated slots need not apply */
1341 [ + + ]: 43006 : if (invalidated)
1342 : 27 : continue;
1343 : :
1344 : : /*
1345 : : * For persistent slot use last_saved_restart_lsn to compute the
1346 : : * oldest LSN for removal of WAL segments. The segments between
1347 : : * last_saved_restart_lsn and restart_lsn might be needed by a
1348 : : * persistent slot in the case of database crash. Non-persistent
1349 : : * slots can't survive the database crash, so we don't care about
1350 : : * last_saved_restart_lsn for them.
1351 : : */
1352 [ + + ]: 42979 : if (persistency == RS_PERSISTENT)
1353 : : {
1354 [ + + + + ]: 41916 : if (XLogRecPtrIsValid(last_saved_restart_lsn) &&
1355 : : restart_lsn > last_saved_restart_lsn)
1356 : : {
1357 : 38327 : restart_lsn = last_saved_restart_lsn;
1358 : : }
1359 : : }
1360 : :
1361 [ + + + + ]: 42979 : if (XLogRecPtrIsValid(restart_lsn) &&
1362 [ + + ]: 1760 : (!XLogRecPtrIsValid(min_required) ||
1363 : : restart_lsn < min_required))
1364 : 41407 : min_required = restart_lsn;
1365 : : }
1366 : 42487 : LWLockRelease(ReplicationSlotControlLock);
1367 : :
1368 : 42487 : XLogSetReplicationSlotMinimumLSN(min_required);
1369 : 42487 : }
1370 : :
1371 : : /*
1372 : : * Compute the oldest WAL LSN required by *logical* decoding slots..
1373 : : *
1374 : : * Returns InvalidXLogRecPtr if logical decoding is disabled or no logical
1375 : : * slots exist.
1376 : : *
1377 : : * NB: this returns a value >= ReplicationSlotsComputeRequiredLSN(), since it
1378 : : * ignores physical replication slots.
1379 : : *
1380 : : * The results aren't required frequently, so we don't maintain a precomputed
1381 : : * value like we do for ComputeRequiredLSN() and ComputeRequiredXmin().
1382 : : */
1383 : : XLogRecPtr
1384 : 4054 : ReplicationSlotsComputeLogicalRestartLSN(void)
1385 : : {
1386 : 4054 : XLogRecPtr result = InvalidXLogRecPtr;
1387 : : int i;
1388 : :
1389 [ - + ]: 4054 : if (max_replication_slots + max_repack_replication_slots <= 0)
1390 : 0 : return InvalidXLogRecPtr;
1391 : :
1392 : 4054 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1393 : :
1394 [ + + ]: 64288 : for (i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
1395 : : {
1396 : : ReplicationSlot *s;
1397 : : XLogRecPtr restart_lsn;
1398 : : XLogRecPtr last_saved_restart_lsn;
1399 : : bool invalidated;
1400 : : ReplicationSlotPersistency persistency;
1401 : :
1402 : 60234 : s = &ReplicationSlotCtl->replication_slots[i];
1403 : :
1404 : : /* cannot change while ReplicationSlotCtlLock is held */
1405 [ + + ]: 60234 : if (!s->in_use)
1406 : 59398 : continue;
1407 : :
1408 : : /* we're only interested in logical slots */
1409 [ + + ]: 836 : if (!SlotIsLogical(s))
1410 : 554 : continue;
1411 : :
1412 : : /* read once, it's ok if it increases while we're checking */
1413 : 282 : SpinLockAcquire(&s->mutex);
1414 : 282 : persistency = s->data.persistency;
1415 : 282 : restart_lsn = s->data.restart_lsn;
1416 : 282 : invalidated = s->data.invalidated != RS_INVAL_NONE;
1417 : 282 : last_saved_restart_lsn = s->last_saved_restart_lsn;
1418 : 282 : SpinLockRelease(&s->mutex);
1419 : :
1420 : : /* invalidated slots need not apply */
1421 [ + + ]: 282 : if (invalidated)
1422 : 10 : continue;
1423 : :
1424 : : /*
1425 : : * For persistent slot use last_saved_restart_lsn to compute the
1426 : : * oldest LSN for removal of WAL segments. The segments between
1427 : : * last_saved_restart_lsn and restart_lsn might be needed by a
1428 : : * persistent slot in the case of database crash. Non-persistent
1429 : : * slots can't survive the database crash, so we don't care about
1430 : : * last_saved_restart_lsn for them.
1431 : : */
1432 [ + + ]: 272 : if (persistency == RS_PERSISTENT)
1433 : : {
1434 [ + - - + ]: 270 : if (XLogRecPtrIsValid(last_saved_restart_lsn) &&
1435 : : restart_lsn > last_saved_restart_lsn)
1436 : : {
1437 : 0 : restart_lsn = last_saved_restart_lsn;
1438 : : }
1439 : : }
1440 : :
1441 [ - + ]: 272 : if (!XLogRecPtrIsValid(restart_lsn))
1442 : 0 : continue;
1443 : :
1444 [ + + + + ]: 272 : if (!XLogRecPtrIsValid(result) ||
1445 : : restart_lsn < result)
1446 : 208 : result = restart_lsn;
1447 : : }
1448 : :
1449 : 4054 : LWLockRelease(ReplicationSlotControlLock);
1450 : :
1451 : 4054 : return result;
1452 : : }
1453 : :
1454 : : /*
1455 : : * ReplicationSlotsCountDBSlots -- count the number of slots that refer to the
1456 : : * passed database oid.
1457 : : *
1458 : : * Returns true if there are any slots referencing the database. *nslots will
1459 : : * be set to the absolute number of slots in the database, *nactive to ones
1460 : : * currently active.
1461 : : */
1462 : : bool
1463 : 61 : ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive)
1464 : : {
1465 : : int i;
1466 : :
1467 : 61 : *nslots = *nactive = 0;
1468 : :
1469 [ - + ]: 61 : if (max_replication_slots + max_repack_replication_slots <= 0)
1470 : 0 : return false;
1471 : :
1472 : 61 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1473 [ + + ]: 949 : for (i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
1474 : : {
1475 : : ReplicationSlot *s;
1476 : :
1477 : 888 : s = &ReplicationSlotCtl->replication_slots[i];
1478 : :
1479 : : /* cannot change while ReplicationSlotCtlLock is held */
1480 [ + + ]: 888 : if (!s->in_use)
1481 : 865 : continue;
1482 : :
1483 : : /* only logical slots are database specific, skip */
1484 [ + + ]: 23 : if (!SlotIsLogical(s))
1485 : 11 : continue;
1486 : :
1487 : : /* not our database, skip */
1488 [ + + ]: 12 : if (s->data.database != dboid)
1489 : 9 : continue;
1490 : :
1491 : : /* NB: intentionally counting invalidated slots */
1492 : :
1493 : : /* count slots with spinlock held */
1494 : 3 : SpinLockAcquire(&s->mutex);
1495 : 3 : (*nslots)++;
1496 [ + + ]: 3 : if (s->active_proc != INVALID_PROC_NUMBER)
1497 : 1 : (*nactive)++;
1498 : 3 : SpinLockRelease(&s->mutex);
1499 : : }
1500 : 61 : LWLockRelease(ReplicationSlotControlLock);
1501 : :
1502 [ + + ]: 61 : if (*nslots > 0)
1503 : 3 : return true;
1504 : 58 : return false;
1505 : : }
1506 : :
1507 : : /*
1508 : : * ReplicationSlotsDropDBSlots -- Drop all db-specific slots relating to the
1509 : : * passed database oid. The caller should hold an exclusive lock on the
1510 : : * pg_database oid for the database to prevent creation of new slots on the db
1511 : : * or replay from existing slots.
1512 : : *
1513 : : * Another session that concurrently acquires an existing slot on the target DB
1514 : : * (most likely to drop it) may cause this function to ERROR. If that happens
1515 : : * it may have dropped some but not all slots.
1516 : : *
1517 : : * This routine isn't as efficient as it could be - but we don't drop
1518 : : * databases often, especially databases with lots of slots.
1519 : : *
1520 : : * If the last logical slot in the cluster is dropped, request to disable
1521 : : * logical decoding.
1522 : : */
1523 : : void
1524 : 74 : ReplicationSlotsDropDBSlots(Oid dboid)
1525 : : {
1526 : : int i;
1527 : : bool found_valid_logicalslot;
1528 : 74 : bool dropped = false;
1529 : :
1530 [ + - ]: 74 : if (max_replication_slots + max_repack_replication_slots <= 0)
1531 : 0 : return;
1532 : :
1533 : 74 : restart:
1534 : 79 : found_valid_logicalslot = false;
1535 : 79 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1536 [ + + ]: 1149 : for (i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
1537 : : {
1538 : : ReplicationSlot *s;
1539 : : char *slotname;
1540 : : ProcNumber active_proc;
1541 : :
1542 : 1075 : s = &ReplicationSlotCtl->replication_slots[i];
1543 : :
1544 : : /* cannot change while ReplicationSlotCtlLock is held */
1545 [ + + ]: 1075 : if (!s->in_use)
1546 : 1043 : continue;
1547 : :
1548 : : /* only logical slots are database specific, skip */
1549 [ + + ]: 32 : if (!SlotIsLogical(s))
1550 : 12 : continue;
1551 : :
1552 : : /*
1553 : : * Check logical slots on other databases too so we can disable
1554 : : * logical decoding only if no slots in the cluster.
1555 : : */
1556 : 20 : SpinLockAcquire(&s->mutex);
1557 : 20 : found_valid_logicalslot |= (s->data.invalidated == RS_INVAL_NONE);
1558 : 20 : SpinLockRelease(&s->mutex);
1559 : :
1560 : : /* not our database, skip */
1561 [ + + ]: 20 : if (s->data.database != dboid)
1562 : 15 : continue;
1563 : :
1564 : : /* NB: intentionally including invalidated slots to drop */
1565 : :
1566 : : /* acquire slot, so ReplicationSlotDropAcquired can be reused */
1567 : 5 : SpinLockAcquire(&s->mutex);
1568 : : /* can't change while ReplicationSlotControlLock is held */
1569 : 5 : slotname = NameStr(s->data.name);
1570 : 5 : active_proc = s->active_proc;
1571 [ + - ]: 5 : if (active_proc == INVALID_PROC_NUMBER)
1572 : : {
1573 : 5 : MyReplicationSlot = s;
1574 : 5 : s->active_proc = MyProcNumber;
1575 : : }
1576 : 5 : SpinLockRelease(&s->mutex);
1577 : :
1578 : : /*
1579 : : * Even though we hold an exclusive lock on the database object a
1580 : : * logical slot for that DB can still be active, e.g. if it's
1581 : : * concurrently being dropped by a backend connected to another DB.
1582 : : *
1583 : : * That's fairly unlikely in practice, so we'll just bail out.
1584 : : *
1585 : : * The slot sync worker holds a shared lock on the database before
1586 : : * operating on synced logical slots to avoid conflict with the drop
1587 : : * happening here. The persistent synced slots are thus safe but there
1588 : : * is a possibility that the slot sync worker has created a temporary
1589 : : * slot (which stays active even on release) and we are trying to drop
1590 : : * that here. In practice, the chances of hitting this scenario are
1591 : : * less as during slot synchronization, the temporary slot is
1592 : : * immediately converted to persistent and thus is safe due to the
1593 : : * shared lock taken on the database. So, we'll just bail out in such
1594 : : * a case.
1595 : : *
1596 : : * XXX: We can consider shutting down the slot sync worker before
1597 : : * trying to drop synced temporary slots here.
1598 : : */
1599 [ - + ]: 5 : if (active_proc != INVALID_PROC_NUMBER)
1600 [ # # ]: 0 : ereport(ERROR,
1601 : : (errcode(ERRCODE_OBJECT_IN_USE),
1602 : : errmsg("replication slot \"%s\" is active for PID %d",
1603 : : slotname, GetPGProcByNumber(active_proc)->pid)));
1604 : :
1605 : : /*
1606 : : * To avoid duplicating ReplicationSlotDropAcquired() and to avoid
1607 : : * holding ReplicationSlotControlLock over filesystem operations,
1608 : : * release ReplicationSlotControlLock and use
1609 : : * ReplicationSlotDropAcquired.
1610 : : *
1611 : : * As that means the set of slots could change, restart scan from the
1612 : : * beginning each time we release the lock.
1613 : : */
1614 : 5 : LWLockRelease(ReplicationSlotControlLock);
1615 : 5 : ReplicationSlotDropAcquired(false);
1616 : 5 : dropped = true;
1617 : 5 : goto restart;
1618 : : }
1619 : 74 : LWLockRelease(ReplicationSlotControlLock);
1620 : :
1621 [ + + - + ]: 74 : if (dropped && !found_valid_logicalslot)
1622 : 0 : RequestDisableLogicalDecoding();
1623 : : }
1624 : :
1625 : : /*
1626 : : * Returns true if there is at least one in-use valid logical replication slot.
1627 : : */
1628 : : bool
1629 : 960 : CheckLogicalSlotExists(void)
1630 : : {
1631 : 960 : bool found = false;
1632 : :
1633 [ - + ]: 960 : if (max_replication_slots + max_repack_replication_slots <= 0)
1634 : 0 : return false;
1635 : :
1636 : 960 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1637 [ + + ]: 15165 : for (int i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
1638 : : {
1639 : : ReplicationSlot *s;
1640 : : bool invalidated;
1641 : :
1642 : 14216 : s = &ReplicationSlotCtl->replication_slots[i];
1643 : :
1644 : : /* cannot change while ReplicationSlotCtlLock is held */
1645 [ + + ]: 14216 : if (!s->in_use)
1646 : 14178 : continue;
1647 : :
1648 [ + + ]: 38 : if (SlotIsPhysical(s))
1649 : 24 : continue;
1650 : :
1651 : 14 : SpinLockAcquire(&s->mutex);
1652 : 14 : invalidated = s->data.invalidated != RS_INVAL_NONE;
1653 : 14 : SpinLockRelease(&s->mutex);
1654 : :
1655 [ + + ]: 14 : if (invalidated)
1656 : 3 : continue;
1657 : :
1658 : 11 : found = true;
1659 : 11 : break;
1660 : : }
1661 : 960 : LWLockRelease(ReplicationSlotControlLock);
1662 : :
1663 : 960 : return found;
1664 : : }
1665 : :
1666 : : /*
1667 : : * Check whether the server's configuration supports using replication
1668 : : * slots.
1669 : : */
1670 : : void
1671 : 2051 : CheckSlotRequirements(bool repack)
1672 : : {
1673 : : /*
1674 : : * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
1675 : : * needs the same check.
1676 : : */
1677 : :
1678 [ + + - + ]: 2051 : if (!repack && max_replication_slots == 0)
1679 [ # # ]: 0 : ereport(ERROR,
1680 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1681 : : errmsg("replication slots can only be used if \"%s\" > 0",
1682 : : "max_replication_slots"));
1683 : :
1684 [ + + - + ]: 2051 : if (repack && max_repack_replication_slots == 0)
1685 [ # # ]: 0 : ereport(ERROR,
1686 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1687 : : errmsg("REPACK can only be used if \"%s\" > 0",
1688 : : "max_repack_replication_slots"));
1689 : :
1690 [ - + ]: 2051 : if (wal_level < WAL_LEVEL_REPLICA)
1691 [ # # ]: 0 : ereport(ERROR,
1692 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1693 : : errmsg("replication slots can only be used if \"wal_level\" >= \"replica\"")));
1694 : 2051 : }
1695 : :
1696 : : /*
1697 : : * Check whether the user has privilege to use replication slots.
1698 : : */
1699 : : void
1700 : 642 : CheckSlotPermissions(void)
1701 : : {
1702 [ + + ]: 642 : if (!has_rolreplication(GetUserId()))
1703 [ + - ]: 5 : ereport(ERROR,
1704 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1705 : : errmsg("permission denied to use replication slots"),
1706 : : errdetail("Only roles with the %s attribute may use replication slots.",
1707 : : "REPLICATION")));
1708 : 637 : }
1709 : :
1710 : : /*
1711 : : * Reserve WAL for the currently active slot.
1712 : : *
1713 : : * Compute and set restart_lsn in a manner that's appropriate for the type of
1714 : : * the slot and concurrency safe.
1715 : : */
1716 : : void
1717 : 718 : ReplicationSlotReserveWal(void)
1718 : : {
1719 : 718 : ReplicationSlot *slot = MyReplicationSlot;
1720 : : XLogSegNo segno;
1721 : : XLogRecPtr restart_lsn;
1722 : :
1723 : : Assert(slot != NULL);
1724 : : Assert(!XLogRecPtrIsValid(slot->data.restart_lsn));
1725 : : Assert(!XLogRecPtrIsValid(slot->last_saved_restart_lsn));
1726 : :
1727 : : /*
1728 : : * The replication slot mechanism is used to prevent the removal of
1729 : : * required WAL.
1730 : : *
1731 : : * Acquire an exclusive lock to prevent the checkpoint process from
1732 : : * concurrently computing the minimum slot LSN (see
1733 : : * CheckPointReplicationSlots). This ensures that the WAL reserved for
1734 : : * replication cannot be removed during a checkpoint.
1735 : : *
1736 : : * The mechanism is reliable because if WAL reservation occurs first, the
1737 : : * checkpoint must wait for the restart_lsn update before determining the
1738 : : * minimum non-removable LSN. On the other hand, if the checkpoint happens
1739 : : * first, subsequent WAL reservations will select positions at or beyond
1740 : : * the redo pointer of that checkpoint.
1741 : : */
1742 : 718 : LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
1743 : :
1744 : : /*
1745 : : * For logical slots log a standby snapshot and start logical decoding at
1746 : : * exactly that position. That allows the slot to start up more quickly.
1747 : : * But on a standby we cannot do WAL writes, so just use the replay
1748 : : * pointer; effectively, an attempt to create a logical slot on standby
1749 : : * will cause it to wait for an xl_running_xact record to be logged
1750 : : * independently on the primary, so that a snapshot can be built using the
1751 : : * record.
1752 : : *
1753 : : * None of this is needed (or indeed helpful) for physical slots as
1754 : : * they'll start replay at the last logged checkpoint anyway. Instead,
1755 : : * return the location of the last redo LSN, where a base backup has to
1756 : : * start replay at.
1757 : : */
1758 [ + + ]: 718 : if (SlotIsPhysical(slot))
1759 : 181 : restart_lsn = GetRedoRecPtr();
1760 [ + + ]: 537 : else if (RecoveryInProgress())
1761 : 28 : restart_lsn = GetXLogReplayRecPtr(NULL);
1762 : : else
1763 : 509 : restart_lsn = GetXLogInsertRecPtr();
1764 : :
1765 : 718 : SpinLockAcquire(&slot->mutex);
1766 : 718 : slot->data.restart_lsn = restart_lsn;
1767 : 718 : SpinLockRelease(&slot->mutex);
1768 : :
1769 : : /* prevent WAL removal as fast as possible */
1770 : 718 : ReplicationSlotsComputeRequiredLSN();
1771 : :
1772 : : /* Checkpoint shouldn't remove the required WAL. */
1773 : 718 : XLByteToSeg(slot->data.restart_lsn, segno, wal_segment_size);
1774 [ - + ]: 718 : if (XLogGetLastRemovedSegno() >= segno)
1775 [ # # ]: 0 : elog(ERROR, "WAL required by replication slot %s has been removed concurrently",
1776 : : NameStr(slot->data.name));
1777 : :
1778 : 718 : LWLockRelease(ReplicationSlotAllocationLock);
1779 : :
1780 [ + + + + ]: 718 : if (!RecoveryInProgress() && SlotIsLogical(slot))
1781 : : {
1782 : : XLogRecPtr flushptr;
1783 : :
1784 : : /* make sure we have enough information to start */
1785 : 509 : flushptr = LogStandbySnapshot();
1786 : :
1787 : : /* and make sure it's fsynced to disk */
1788 : 509 : XLogFlush(flushptr);
1789 : : }
1790 : 718 : }
1791 : :
1792 : : /*
1793 : : * Report that replication slot needs to be invalidated
1794 : : */
1795 : : static void
1796 : 23 : ReportSlotInvalidation(ReplicationSlotInvalidationCause cause,
1797 : : bool terminating,
1798 : : int pid,
1799 : : NameData slotname,
1800 : : XLogRecPtr restart_lsn,
1801 : : XLogRecPtr oldestLSN,
1802 : : TransactionId snapshotConflictHorizon,
1803 : : long slot_idle_seconds)
1804 : : {
1805 : : StringInfoData err_detail;
1806 : : StringInfoData err_hint;
1807 : :
1808 : 23 : initStringInfo(&err_detail);
1809 : 23 : initStringInfo(&err_hint);
1810 : :
1811 [ + + + - : 23 : switch (cause)
- ]
1812 : : {
1813 : 7 : case RS_INVAL_WAL_REMOVED:
1814 : : {
1815 : 7 : uint64 ex = oldestLSN - restart_lsn;
1816 : :
1817 : 7 : appendStringInfo(&err_detail,
1818 : 7 : ngettext("The slot's restart_lsn %X/%08X exceeds the limit by %" PRIu64 " byte.",
1819 : : "The slot's restart_lsn %X/%08X exceeds the limit by %" PRIu64 " bytes.",
1820 : : ex),
1821 : 7 : LSN_FORMAT_ARGS(restart_lsn),
1822 : : ex);
1823 : : /* translator: %s is a GUC variable name */
1824 : 7 : appendStringInfo(&err_hint, _("You might need to increase \"%s\"."),
1825 : : "max_slot_wal_keep_size");
1826 : 7 : break;
1827 : : }
1828 : 12 : case RS_INVAL_HORIZON:
1829 : 12 : appendStringInfo(&err_detail, _("The slot conflicted with xid horizon %u."),
1830 : : snapshotConflictHorizon);
1831 : 12 : break;
1832 : :
1833 : 4 : case RS_INVAL_WAL_LEVEL:
1834 : 4 : appendStringInfoString(&err_detail, _("Logical decoding on standby requires the primary server to either set \"wal_level\" >= \"logical\" or have at least one logical slot when \"wal_level\" = \"replica\"."));
1835 : 4 : break;
1836 : :
1837 : 0 : case RS_INVAL_IDLE_TIMEOUT:
1838 : : {
1839 : : /* translator: %s is a GUC variable name */
1840 : 0 : appendStringInfo(&err_detail, _("The slot's idle time of %lds exceeds the configured \"%s\" duration of %ds."),
1841 : : slot_idle_seconds, "idle_replication_slot_timeout",
1842 : : idle_replication_slot_timeout_secs);
1843 : : /* translator: %s is a GUC variable name */
1844 : 0 : appendStringInfo(&err_hint, _("You might need to increase \"%s\"."),
1845 : : "idle_replication_slot_timeout");
1846 : 0 : break;
1847 : : }
1848 : : case RS_INVAL_NONE:
1849 : : pg_unreachable();
1850 : : }
1851 : :
1852 [ + - + + : 23 : ereport(LOG,
+ + ]
1853 : : terminating ?
1854 : : errmsg("terminating process %d to release replication slot \"%s\"",
1855 : : pid, NameStr(slotname)) :
1856 : : errmsg("invalidating obsolete replication slot \"%s\"",
1857 : : NameStr(slotname)),
1858 : : errdetail_internal("%s", err_detail.data),
1859 : : err_hint.len ? errhint("%s", err_hint.data) : 0);
1860 : :
1861 : 23 : pfree(err_detail.data);
1862 : 23 : pfree(err_hint.data);
1863 : 23 : }
1864 : :
1865 : : /*
1866 : : * Can we invalidate an idle replication slot?
1867 : : *
1868 : : * Idle timeout invalidation is allowed only when:
1869 : : *
1870 : : * 1. Idle timeout is set
1871 : : * 2. Slot has reserved WAL
1872 : : * 3. Slot is inactive
1873 : : * 4. The slot is not being synced from the primary while the server is in
1874 : : * recovery. This is because synced slots are always considered to be
1875 : : * inactive because they don't perform logical decoding to produce changes.
1876 : : */
1877 : : static inline bool
1878 : 392 : CanInvalidateIdleSlot(ReplicationSlot *s)
1879 : : {
1880 : 392 : return (idle_replication_slot_timeout_secs != 0 &&
1881 [ # # ]: 0 : XLogRecPtrIsValid(s->data.restart_lsn) &&
1882 [ - + - - ]: 392 : s->inactive_since > 0 &&
1883 [ # # # # ]: 0 : !(RecoveryInProgress() && s->data.synced));
1884 : : }
1885 : :
1886 : : /*
1887 : : * DetermineSlotInvalidationCause - Determine the cause for which a slot
1888 : : * becomes invalid among the given possible causes.
1889 : : *
1890 : : * This function sequentially checks all possible invalidation causes and
1891 : : * returns the first one for which the slot is eligible for invalidation.
1892 : : */
1893 : : static ReplicationSlotInvalidationCause
1894 : 425 : DetermineSlotInvalidationCause(uint32 possible_causes, ReplicationSlot *s,
1895 : : XLogRecPtr oldestLSN, Oid dboid,
1896 : : TransactionId snapshotConflictHorizon,
1897 : : TimestampTz *inactive_since, TimestampTz now)
1898 : : {
1899 : : Assert(possible_causes != RS_INVAL_NONE);
1900 : :
1901 [ + + ]: 425 : if (possible_causes & RS_INVAL_WAL_REMOVED)
1902 : : {
1903 : 399 : XLogRecPtr restart_lsn = s->data.restart_lsn;
1904 : :
1905 [ + + + + ]: 399 : if (XLogRecPtrIsValid(restart_lsn) &&
1906 : : restart_lsn < oldestLSN)
1907 : 7 : return RS_INVAL_WAL_REMOVED;
1908 : : }
1909 : :
1910 [ + + ]: 418 : if (possible_causes & RS_INVAL_HORIZON)
1911 : : {
1912 : : /* invalid DB oid signals a shared relation */
1913 [ + - + + ]: 22 : if (SlotIsLogical(s) &&
1914 [ + - ]: 17 : (dboid == InvalidOid || dboid == s->data.database))
1915 : : {
1916 : 22 : TransactionId effective_xmin = s->effective_xmin;
1917 : 22 : TransactionId catalog_effective_xmin = s->effective_catalog_xmin;
1918 : :
1919 [ - + - - ]: 22 : if (TransactionIdIsValid(effective_xmin) &&
1920 : 0 : TransactionIdPrecedesOrEquals(effective_xmin,
1921 : : snapshotConflictHorizon))
1922 : 0 : return RS_INVAL_HORIZON;
1923 [ + - + + ]: 44 : else if (TransactionIdIsValid(catalog_effective_xmin) &&
1924 : 22 : TransactionIdPrecedesOrEquals(catalog_effective_xmin,
1925 : : snapshotConflictHorizon))
1926 : 12 : return RS_INVAL_HORIZON;
1927 : : }
1928 : : }
1929 : :
1930 [ + + ]: 406 : if (possible_causes & RS_INVAL_WAL_LEVEL)
1931 : : {
1932 [ + - ]: 4 : if (SlotIsLogical(s))
1933 : 4 : return RS_INVAL_WAL_LEVEL;
1934 : : }
1935 : :
1936 [ + + ]: 402 : if (possible_causes & RS_INVAL_IDLE_TIMEOUT)
1937 : : {
1938 : : Assert(now > 0);
1939 : :
1940 [ - + ]: 392 : if (CanInvalidateIdleSlot(s))
1941 : : {
1942 : : /*
1943 : : * Simulate the invalidation due to idle_timeout to test the
1944 : : * timeout behavior promptly, without waiting for it to trigger
1945 : : * naturally.
1946 : : */
1947 : : #ifdef USE_INJECTION_POINTS
1948 [ # # ]: 0 : if (IS_INJECTION_POINT_ATTACHED("slot-timeout-inval"))
1949 : : {
1950 : 0 : *inactive_since = 0; /* since the beginning of time */
1951 : 0 : return RS_INVAL_IDLE_TIMEOUT;
1952 : : }
1953 : : #endif
1954 : :
1955 : : /*
1956 : : * Check if the slot needs to be invalidated due to
1957 : : * idle_replication_slot_timeout GUC.
1958 : : */
1959 [ # # ]: 0 : if (TimestampDifferenceExceedsSeconds(s->inactive_since, now,
1960 : : idle_replication_slot_timeout_secs))
1961 : : {
1962 : 0 : *inactive_since = s->inactive_since;
1963 : 0 : return RS_INVAL_IDLE_TIMEOUT;
1964 : : }
1965 : : }
1966 : : }
1967 : :
1968 : 402 : return RS_INVAL_NONE;
1969 : : }
1970 : :
1971 : : /*
1972 : : * Helper for InvalidateObsoleteReplicationSlots
1973 : : *
1974 : : * Acquires the given slot and mark it invalid, if necessary and possible.
1975 : : *
1976 : : * Returns true if the slot was invalidated.
1977 : : *
1978 : : * Set *released_lock_out if ReplicationSlotControlLock was released in the
1979 : : * interim (and in that case we're not holding the lock at return, otherwise
1980 : : * we are).
1981 : : *
1982 : : * This is inherently racy, because we release the LWLock
1983 : : * for syscalls, so caller must restart if we return true.
1984 : : */
1985 : : static bool
1986 : 464 : InvalidatePossiblyObsoleteSlot(uint32 possible_causes,
1987 : : ReplicationSlot *s,
1988 : : XLogRecPtr oldestLSN,
1989 : : Oid dboid, TransactionId snapshotConflictHorizon,
1990 : : bool *released_lock_out)
1991 : : {
1992 : 464 : int last_signaled_pid = 0;
1993 : 464 : bool released_lock = false;
1994 : 464 : bool invalidated = false;
1995 : 464 : TimestampTz inactive_since = 0;
1996 : :
1997 : : for (;;)
1998 : 7 : {
1999 : : XLogRecPtr restart_lsn;
2000 : : NameData slotname;
2001 : : ProcNumber active_proc;
2002 : 471 : int active_pid = 0;
2003 : 471 : ReplicationSlotInvalidationCause invalidation_cause = RS_INVAL_NONE;
2004 : 471 : TimestampTz now = 0;
2005 : 471 : long slot_idle_secs = 0;
2006 : :
2007 : : Assert(LWLockHeldByMeInMode(ReplicationSlotControlLock, LW_SHARED));
2008 : :
2009 [ - + ]: 471 : if (!s->in_use)
2010 : : {
2011 [ # # ]: 0 : if (released_lock)
2012 : 0 : LWLockRelease(ReplicationSlotControlLock);
2013 : 0 : break;
2014 : : }
2015 : :
2016 [ + + ]: 471 : if (possible_causes & RS_INVAL_IDLE_TIMEOUT)
2017 : : {
2018 : : /*
2019 : : * Assign the current time here to avoid system call overhead
2020 : : * while holding the spinlock in subsequent code.
2021 : : */
2022 : 413 : now = GetCurrentTimestamp();
2023 : : }
2024 : :
2025 : : /*
2026 : : * Check if the slot needs to be invalidated. If it needs to be
2027 : : * invalidated, and is not currently acquired, acquire it and mark it
2028 : : * as having been invalidated. We do this with the spinlock held to
2029 : : * avoid race conditions -- for example the restart_lsn could move
2030 : : * forward, or the slot could be dropped.
2031 : : */
2032 : 471 : SpinLockAcquire(&s->mutex);
2033 : :
2034 : 471 : restart_lsn = s->data.restart_lsn;
2035 : :
2036 : : /* we do nothing if the slot is already invalid */
2037 [ + + ]: 471 : if (s->data.invalidated == RS_INVAL_NONE)
2038 : 425 : invalidation_cause = DetermineSlotInvalidationCause(possible_causes,
2039 : : s, oldestLSN,
2040 : : dboid,
2041 : : snapshotConflictHorizon,
2042 : : &inactive_since,
2043 : : now);
2044 : :
2045 : : /* if there's no invalidation, we're done */
2046 [ + + ]: 471 : if (invalidation_cause == RS_INVAL_NONE)
2047 : : {
2048 : 448 : SpinLockRelease(&s->mutex);
2049 [ - + ]: 448 : if (released_lock)
2050 : 0 : LWLockRelease(ReplicationSlotControlLock);
2051 : 448 : break;
2052 : : }
2053 : :
2054 : 23 : slotname = s->data.name;
2055 : 23 : active_proc = s->active_proc;
2056 : :
2057 : : /*
2058 : : * If the slot can be acquired, do so and mark it invalidated
2059 : : * immediately. Otherwise we'll signal the owning process, below, and
2060 : : * retry.
2061 : : *
2062 : : * Note: Unlike other slot attributes, slot's inactive_since can't be
2063 : : * changed until the acquired slot is released or the owning process
2064 : : * is terminated. So, the inactive slot can only be invalidated
2065 : : * immediately without being terminated.
2066 : : */
2067 [ + + ]: 23 : if (active_proc == INVALID_PROC_NUMBER)
2068 : : {
2069 : 16 : MyReplicationSlot = s;
2070 : 16 : s->active_proc = MyProcNumber;
2071 : 16 : s->data.invalidated = invalidation_cause;
2072 : :
2073 : : /*
2074 : : * XXX: We should consider not overwriting restart_lsn and instead
2075 : : * just rely on .invalidated.
2076 : : */
2077 [ + + ]: 16 : if (invalidation_cause == RS_INVAL_WAL_REMOVED)
2078 : : {
2079 : 5 : s->data.restart_lsn = InvalidXLogRecPtr;
2080 : 5 : s->last_saved_restart_lsn = InvalidXLogRecPtr;
2081 : : }
2082 : :
2083 : : /* Let caller know */
2084 : 16 : invalidated = true;
2085 : : }
2086 : : else
2087 : : {
2088 : 7 : active_pid = GetPGProcByNumber(active_proc)->pid;
2089 : : Assert(active_pid != 0);
2090 : : }
2091 : :
2092 : 23 : SpinLockRelease(&s->mutex);
2093 : :
2094 : : /*
2095 : : * Calculate the idle time duration of the slot if slot is marked
2096 : : * invalidated with RS_INVAL_IDLE_TIMEOUT.
2097 : : */
2098 [ - + ]: 23 : if (invalidation_cause == RS_INVAL_IDLE_TIMEOUT)
2099 : : {
2100 : : int slot_idle_usecs;
2101 : :
2102 : 0 : TimestampDifference(inactive_since, now, &slot_idle_secs,
2103 : : &slot_idle_usecs);
2104 : : }
2105 : :
2106 [ + + ]: 23 : if (active_proc != INVALID_PROC_NUMBER)
2107 : : {
2108 : : /*
2109 : : * Prepare the sleep on the slot's condition variable before
2110 : : * releasing the lock, to close a possible race condition if the
2111 : : * slot is released before the sleep below.
2112 : : */
2113 : 7 : ConditionVariablePrepareToSleep(&s->active_cv);
2114 : :
2115 : 7 : LWLockRelease(ReplicationSlotControlLock);
2116 : 7 : released_lock = true;
2117 : :
2118 : : /*
2119 : : * Signal to terminate the process that owns the slot, if we
2120 : : * haven't already signalled it. (Avoidance of repeated
2121 : : * signalling is the only reason for there to be a loop in this
2122 : : * routine; otherwise we could rely on caller's restart loop.)
2123 : : *
2124 : : * There is the race condition that other process may own the slot
2125 : : * after its current owner process is terminated and before this
2126 : : * process owns it. To handle that, we signal only if the PID of
2127 : : * the owning process has changed from the previous time. (This
2128 : : * logic assumes that the same PID is not reused very quickly.)
2129 : : */
2130 [ + - ]: 7 : if (last_signaled_pid != active_pid)
2131 : : {
2132 : 7 : ReportSlotInvalidation(invalidation_cause, true, active_pid,
2133 : : slotname, restart_lsn,
2134 : : oldestLSN, snapshotConflictHorizon,
2135 : : slot_idle_secs);
2136 : :
2137 [ + + ]: 7 : if (MyBackendType == B_STARTUP)
2138 : 5 : (void) SignalRecoveryConflict(GetPGProcByNumber(active_proc),
2139 : : active_pid,
2140 : : RECOVERY_CONFLICT_LOGICALSLOT);
2141 : : else
2142 : 2 : (void) kill(active_pid, SIGTERM);
2143 : :
2144 : 7 : last_signaled_pid = active_pid;
2145 : : }
2146 : :
2147 : : /* Wait until the slot is released. */
2148 : 7 : ConditionVariableSleep(&s->active_cv,
2149 : : WAIT_EVENT_REPLICATION_SLOT_DROP);
2150 : :
2151 : : /*
2152 : : * Re-acquire lock and start over; we expect to invalidate the
2153 : : * slot next time (unless another process acquires the slot in the
2154 : : * meantime).
2155 : : *
2156 : : * Note: It is possible for a slot to advance its restart_lsn or
2157 : : * xmin values sufficiently between when we release the mutex and
2158 : : * when we recheck, moving from a conflicting state to a non
2159 : : * conflicting state. This is intentional and safe: if the slot
2160 : : * has caught up while we're busy here, the resources we were
2161 : : * concerned about (WAL segments or tuples) have not yet been
2162 : : * removed, and there's no reason to invalidate the slot.
2163 : : */
2164 : 7 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
2165 : 7 : continue;
2166 : : }
2167 : : else
2168 : : {
2169 : : /*
2170 : : * We hold the slot now and have already invalidated it; flush it
2171 : : * to ensure that state persists.
2172 : : *
2173 : : * Don't want to hold ReplicationSlotControlLock across file
2174 : : * system operations, so release it now but be sure to tell caller
2175 : : * to restart from scratch.
2176 : : */
2177 : 16 : LWLockRelease(ReplicationSlotControlLock);
2178 : 16 : released_lock = true;
2179 : :
2180 : : /* Make sure the invalidated state persists across server restart */
2181 : 16 : ReplicationSlotMarkDirty();
2182 : 16 : ReplicationSlotSave();
2183 : 16 : ReplicationSlotRelease();
2184 : :
2185 : 16 : ReportSlotInvalidation(invalidation_cause, false, active_pid,
2186 : : slotname, restart_lsn,
2187 : : oldestLSN, snapshotConflictHorizon,
2188 : : slot_idle_secs);
2189 : :
2190 : : /* done with this slot for now */
2191 : 16 : break;
2192 : : }
2193 : : }
2194 : :
2195 : : Assert(released_lock == !LWLockHeldByMe(ReplicationSlotControlLock));
2196 : :
2197 : 464 : *released_lock_out = released_lock;
2198 : 464 : return invalidated;
2199 : : }
2200 : :
2201 : : /*
2202 : : * Invalidate slots that require resources about to be removed.
2203 : : *
2204 : : * Returns true when any slot have got invalidated.
2205 : : *
2206 : : * Whether a slot needs to be invalidated depends on the invalidation cause.
2207 : : * A slot is invalidated if it:
2208 : : * - RS_INVAL_WAL_REMOVED: requires a LSN older than the given segment
2209 : : * - RS_INVAL_HORIZON: requires a snapshot <= the given horizon in the given
2210 : : * db; dboid may be InvalidOid for shared relations
2211 : : * - RS_INVAL_WAL_LEVEL: is a logical slot and effective_wal_level is not
2212 : : * logical.
2213 : : * - RS_INVAL_IDLE_TIMEOUT: has been idle longer than the configured
2214 : : * "idle_replication_slot_timeout" duration.
2215 : : *
2216 : : * Note: This function attempts to invalidate the slot for multiple possible
2217 : : * causes in a single pass, minimizing redundant iterations. The "cause"
2218 : : * parameter can be a MASK representing one or more of the defined causes.
2219 : : *
2220 : : * If it invalidates the last logical slot in the cluster, it requests to
2221 : : * disable logical decoding.
2222 : : *
2223 : : * NB - this runs as part of checkpoint, so avoid raising errors if possible.
2224 : : */
2225 : : bool
2226 : 2060 : InvalidateObsoleteReplicationSlots(uint32 possible_causes,
2227 : : XLogSegNo oldestSegno, Oid dboid,
2228 : : TransactionId snapshotConflictHorizon)
2229 : : {
2230 : : XLogRecPtr oldestLSN;
2231 : 2060 : bool invalidated = false;
2232 : 2060 : bool invalidated_logical = false;
2233 : : bool found_valid_logicalslot;
2234 : :
2235 : : Assert(!(possible_causes & RS_INVAL_HORIZON) || TransactionIdIsValid(snapshotConflictHorizon));
2236 : : Assert(!(possible_causes & RS_INVAL_WAL_REMOVED) || oldestSegno > 0);
2237 : : Assert(possible_causes != RS_INVAL_NONE);
2238 : :
2239 [ + + - + ]: 2060 : if (max_replication_slots == 0 && max_repack_replication_slots == 0)
2240 : 0 : return invalidated;
2241 : :
2242 : 2060 : XLogSegNoOffsetToRecPtr(oldestSegno, 0, wal_segment_size, oldestLSN);
2243 : :
2244 : 2076 : restart:
2245 : 2076 : found_valid_logicalslot = false;
2246 : 2076 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
2247 [ + + ]: 32608 : for (int i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
2248 : : {
2249 : 30548 : ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i];
2250 : 30548 : bool released_lock = false;
2251 : :
2252 [ + + ]: 30548 : if (!s->in_use)
2253 : 30084 : continue;
2254 : :
2255 : : /* Prevent invalidation of logical slots during binary upgrade */
2256 [ + + + + ]: 476 : if (SlotIsLogical(s) && IsBinaryUpgrade)
2257 : : {
2258 : 12 : SpinLockAcquire(&s->mutex);
2259 : 12 : found_valid_logicalslot |= (s->data.invalidated == RS_INVAL_NONE);
2260 : 12 : SpinLockRelease(&s->mutex);
2261 : :
2262 : 12 : continue;
2263 : : }
2264 : :
2265 [ + + ]: 464 : if (InvalidatePossiblyObsoleteSlot(possible_causes, s, oldestLSN,
2266 : : dboid, snapshotConflictHorizon,
2267 : : &released_lock))
2268 : : {
2269 : : Assert(released_lock);
2270 : :
2271 : : /* Remember we have invalidated a physical or logical slot */
2272 : 16 : invalidated = true;
2273 : :
2274 : : /*
2275 : : * Additionally, remember we have invalidated a logical slot as we
2276 : : * can request disabling logical decoding later.
2277 : : */
2278 [ + + ]: 16 : if (SlotIsLogical(s))
2279 : 13 : invalidated_logical = true;
2280 : : }
2281 : : else
2282 : : {
2283 : : /*
2284 : : * We need to check if the slot is invalidated here since
2285 : : * InvalidatePossiblyObsoleteSlot() returns false also if the slot
2286 : : * is already invalidated.
2287 : : */
2288 : 448 : SpinLockAcquire(&s->mutex);
2289 : 896 : found_valid_logicalslot |=
2290 [ + + + + ]: 448 : (SlotIsLogical(s) && (s->data.invalidated == RS_INVAL_NONE));
2291 : 448 : SpinLockRelease(&s->mutex);
2292 : : }
2293 : :
2294 : : /* if the lock was released, start from scratch */
2295 [ + + ]: 464 : if (released_lock)
2296 : 16 : goto restart;
2297 : : }
2298 : 2060 : LWLockRelease(ReplicationSlotControlLock);
2299 : :
2300 : : /*
2301 : : * If any slots have been invalidated, recalculate the resource limits.
2302 : : */
2303 [ + + ]: 2060 : if (invalidated)
2304 : : {
2305 : 11 : ReplicationSlotsComputeRequiredXmin(false);
2306 : 11 : ReplicationSlotsComputeRequiredLSN();
2307 : : }
2308 : :
2309 : : /*
2310 : : * Request the checkpointer to disable logical decoding if no valid
2311 : : * logical slots remain. If called by the checkpointer during a
2312 : : * checkpoint, only the request is initiated; actual deactivation is
2313 : : * deferred until after the checkpoint completes.
2314 : : */
2315 [ + + + - ]: 2060 : if (invalidated_logical && !found_valid_logicalslot)
2316 : 8 : RequestDisableLogicalDecoding();
2317 : :
2318 : 2060 : return invalidated;
2319 : : }
2320 : :
2321 : : /*
2322 : : * Flush all replication slots to disk.
2323 : : *
2324 : : * It is convenient to flush dirty replication slots at the time of checkpoint.
2325 : : * Additionally, in case of a shutdown checkpoint, we also identify the slots
2326 : : * for which the confirmed_flush LSN has been updated since the last time it
2327 : : * was saved and flush them.
2328 : : */
2329 : : void
2330 : 2027 : CheckPointReplicationSlots(bool is_shutdown)
2331 : : {
2332 : : int i;
2333 : 2027 : bool last_saved_restart_lsn_updated = false;
2334 : :
2335 [ + + ]: 2027 : elog(DEBUG1, "performing replication slot checkpoint");
2336 : :
2337 : : /*
2338 : : * Prevent any slot from being created/dropped while we're active. As we
2339 : : * explicitly do *not* want to block iterating over replication_slots or
2340 : : * acquiring a slot we cannot take the control lock - but that's OK,
2341 : : * because holding ReplicationSlotAllocationLock is strictly stronger, and
2342 : : * enough to guarantee that nobody can change the in_use bits on us.
2343 : : *
2344 : : * Additionally, acquiring the Allocation lock is necessary to serialize
2345 : : * the slot flush process with concurrent slot WAL reservation. This
2346 : : * ensures that the WAL position being reserved is either flushed to disk
2347 : : * or is beyond or equal to the redo pointer of the current checkpoint
2348 : : * (See ReplicationSlotReserveWal for details).
2349 : : */
2350 : 2027 : LWLockAcquire(ReplicationSlotAllocationLock, LW_SHARED);
2351 : :
2352 [ + + ]: 32144 : for (i = 0; i < max_replication_slots + max_repack_replication_slots; i++)
2353 : : {
2354 : 30117 : ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i];
2355 : : char path[MAXPGPATH];
2356 : :
2357 [ + + ]: 30117 : if (!s->in_use)
2358 : 29699 : continue;
2359 : :
2360 : : /* save the slot to disk, locking is handled in SaveSlotToPath() */
2361 : 418 : sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(s->data.name));
2362 : :
2363 : : /*
2364 : : * Slot's data is not flushed each time the confirmed_flush LSN is
2365 : : * updated as that could lead to frequent writes. However, we decide
2366 : : * to force a flush of all logical slot's data at the time of shutdown
2367 : : * if the confirmed_flush LSN is changed since we last flushed it to
2368 : : * disk. This helps in avoiding an unnecessary retreat of the
2369 : : * confirmed_flush LSN after restart.
2370 : : */
2371 [ + + + + ]: 418 : if (is_shutdown && SlotIsLogical(s))
2372 : : {
2373 : 105 : SpinLockAcquire(&s->mutex);
2374 : :
2375 [ + + ]: 105 : if (s->data.invalidated == RS_INVAL_NONE &&
2376 [ + + ]: 104 : s->data.confirmed_flush > s->last_saved_confirmed_flush)
2377 : : {
2378 : 47 : s->just_dirtied = true;
2379 : 47 : s->dirty = true;
2380 : : }
2381 : 105 : SpinLockRelease(&s->mutex);
2382 : : }
2383 : :
2384 : : /*
2385 : : * Track if we're going to update slot's last_saved_restart_lsn. We
2386 : : * need this to know if we need to recompute the required LSN.
2387 : : */
2388 [ + + ]: 418 : if (s->last_saved_restart_lsn != s->data.restart_lsn)
2389 : 210 : last_saved_restart_lsn_updated = true;
2390 : :
2391 : 418 : SaveSlotToPath(s, path, LOG);
2392 : : }
2393 : 2027 : LWLockRelease(ReplicationSlotAllocationLock);
2394 : :
2395 : : /*
2396 : : * Recompute the required LSN if SaveSlotToPath() updated
2397 : : * last_saved_restart_lsn for any slot.
2398 : : */
2399 [ + + ]: 2027 : if (last_saved_restart_lsn_updated)
2400 : 210 : ReplicationSlotsComputeRequiredLSN();
2401 : 2027 : }
2402 : :
2403 : : /*
2404 : : * Load all replication slots from disk into memory at server startup. This
2405 : : * needs to be run before we start crash recovery.
2406 : : */
2407 : : void
2408 : 1141 : StartupReplicationSlots(void)
2409 : : {
2410 : : DIR *replication_dir;
2411 : : struct dirent *replication_de;
2412 : :
2413 [ + + ]: 1141 : elog(DEBUG1, "starting up replication slots");
2414 : :
2415 : : /* restore all slots by iterating over all on-disk entries */
2416 : 1141 : replication_dir = AllocateDir(PG_REPLSLOT_DIR);
2417 [ + + ]: 3552 : while ((replication_de = ReadDir(replication_dir, PG_REPLSLOT_DIR)) != NULL)
2418 : : {
2419 : : char path[MAXPGPATH + sizeof(PG_REPLSLOT_DIR)];
2420 : : PGFileType de_type;
2421 : :
2422 [ + + ]: 2413 : if (strcmp(replication_de->d_name, ".") == 0 ||
2423 [ + + ]: 1274 : strcmp(replication_de->d_name, "..") == 0)
2424 : 2278 : continue;
2425 : :
2426 : 135 : snprintf(path, sizeof(path), "%s/%s", PG_REPLSLOT_DIR, replication_de->d_name);
2427 : 135 : de_type = get_dirent_type(path, replication_de, false, DEBUG1);
2428 : :
2429 : : /* we're only creating directories here, skip if it's not our's */
2430 [ + - - + ]: 135 : if (de_type != PGFILETYPE_ERROR && de_type != PGFILETYPE_DIR)
2431 : 0 : continue;
2432 : :
2433 : : /* we crashed while a slot was being setup or deleted, clean up */
2434 [ - + ]: 135 : if (pg_str_endswith(replication_de->d_name, ".tmp"))
2435 : : {
2436 [ # # ]: 0 : if (!rmtree(path, true))
2437 : : {
2438 [ # # ]: 0 : ereport(WARNING,
2439 : : (errmsg("could not remove directory \"%s\"",
2440 : : path)));
2441 : 0 : continue;
2442 : : }
2443 : 0 : fsync_fname(PG_REPLSLOT_DIR, true);
2444 : 0 : continue;
2445 : : }
2446 : :
2447 : : /* looks like a slot in a normal state, restore */
2448 : 135 : RestoreSlotFromDisk(replication_de->d_name);
2449 : : }
2450 : 1139 : FreeDir(replication_dir);
2451 : :
2452 : : /* currently no slots exist, we're done. */
2453 [ - + ]: 1139 : if (max_replication_slots + max_repack_replication_slots <= 0)
2454 : 0 : return;
2455 : :
2456 : : /* Now that we have recovered all the data, compute replication xmin */
2457 : 1139 : ReplicationSlotsComputeRequiredXmin(false);
2458 : 1139 : ReplicationSlotsComputeRequiredLSN();
2459 : : }
2460 : :
2461 : : /* ----
2462 : : * Manipulation of on-disk state of replication slots
2463 : : *
2464 : : * NB: none of the routines below should take any notice whether a slot is the
2465 : : * current one or not, that's all handled a layer above.
2466 : : * ----
2467 : : */
2468 : : static void
2469 : 777 : CreateSlotOnDisk(ReplicationSlot *slot)
2470 : : {
2471 : : char tmppath[MAXPGPATH];
2472 : : char path[MAXPGPATH];
2473 : : struct stat st;
2474 : :
2475 : : /*
2476 : : * No need to take out the io_in_progress_lock, nobody else can see this
2477 : : * slot yet, so nobody else will write. We're reusing SaveSlotToPath which
2478 : : * takes out the lock, if we'd take the lock here, we'd deadlock.
2479 : : */
2480 : :
2481 : 777 : sprintf(path, "%s/%s", PG_REPLSLOT_DIR, NameStr(slot->data.name));
2482 : 777 : sprintf(tmppath, "%s/%s.tmp", PG_REPLSLOT_DIR, NameStr(slot->data.name));
2483 : :
2484 : : /*
2485 : : * It's just barely possible that some previous effort to create or drop a
2486 : : * slot with this name left a temp directory lying around. If that seems
2487 : : * to be the case, try to remove it. If the rmtree() fails, we'll error
2488 : : * out at the MakePGDirectory() below, so we don't bother checking
2489 : : * success.
2490 : : */
2491 [ - + - - ]: 777 : if (stat(tmppath, &st) == 0 && S_ISDIR(st.st_mode))
2492 : 0 : rmtree(tmppath, true);
2493 : :
2494 : : /* Create and fsync the temporary slot directory. */
2495 [ - + ]: 777 : if (MakePGDirectory(tmppath) < 0)
2496 [ # # ]: 0 : ereport(ERROR,
2497 : : (errcode_for_file_access(),
2498 : : errmsg("could not create directory \"%s\": %m",
2499 : : tmppath)));
2500 : 777 : fsync_fname(tmppath, true);
2501 : :
2502 : : /* Write the actual state file. */
2503 : 777 : slot->dirty = true; /* signal that we really need to write */
2504 : 777 : SaveSlotToPath(slot, tmppath, ERROR);
2505 : :
2506 : : /* Rename the directory into place. */
2507 [ - + ]: 777 : if (rename(tmppath, path) != 0)
2508 [ # # ]: 0 : ereport(ERROR,
2509 : : (errcode_for_file_access(),
2510 : : errmsg("could not rename file \"%s\" to \"%s\": %m",
2511 : : tmppath, path)));
2512 : :
2513 : : /*
2514 : : * If we'd now fail - really unlikely - we wouldn't know whether this slot
2515 : : * would persist after an OS crash or not - so, force a restart. The
2516 : : * restart would try to fsync this again till it works.
2517 : : */
2518 : 777 : START_CRIT_SECTION();
2519 : :
2520 : 777 : fsync_fname(path, true);
2521 : 777 : fsync_fname(PG_REPLSLOT_DIR, true);
2522 : :
2523 : 777 : END_CRIT_SECTION();
2524 : 777 : }
2525 : :
2526 : : /*
2527 : : * Shared functionality between saving and creating a replication slot.
2528 : : */
2529 : : static void
2530 : 2786 : SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
2531 : : {
2532 : : char tmppath[MAXPGPATH];
2533 : : char path[MAXPGPATH];
2534 : : int fd;
2535 : : ReplicationSlotOnDisk cp;
2536 : : bool was_dirty;
2537 : :
2538 : : /* first check whether there's something to write out */
2539 : 2786 : SpinLockAcquire(&slot->mutex);
2540 : 2786 : was_dirty = slot->dirty;
2541 : 2786 : slot->just_dirtied = false;
2542 : 2786 : SpinLockRelease(&slot->mutex);
2543 : :
2544 : : /* and don't do anything if there's nothing to write */
2545 [ + + ]: 2786 : if (!was_dirty)
2546 : 150 : return;
2547 : :
2548 : 2636 : LWLockAcquire(&slot->io_in_progress_lock, LW_EXCLUSIVE);
2549 : :
2550 : : /* silence valgrind :( */
2551 : 2636 : memset(&cp, 0, sizeof(ReplicationSlotOnDisk));
2552 : :
2553 : 2636 : sprintf(tmppath, "%s/state.tmp", dir);
2554 : 2636 : sprintf(path, "%s/state", dir);
2555 : :
2556 : 2636 : fd = OpenTransientFile(tmppath, O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
2557 [ - + ]: 2636 : if (fd < 0)
2558 : : {
2559 : : /*
2560 : : * If not an ERROR, then release the lock before returning. In case
2561 : : * of an ERROR, the error recovery path automatically releases the
2562 : : * lock, but no harm in explicitly releasing even in that case. Note
2563 : : * that LWLockRelease() could affect errno.
2564 : : */
2565 : 0 : int save_errno = errno;
2566 : :
2567 : 0 : LWLockRelease(&slot->io_in_progress_lock);
2568 : 0 : errno = save_errno;
2569 [ # # ]: 0 : ereport(elevel,
2570 : : (errcode_for_file_access(),
2571 : : errmsg("could not create file \"%s\": %m",
2572 : : tmppath)));
2573 : 0 : return;
2574 : : }
2575 : :
2576 : 2636 : cp.magic = SLOT_MAGIC;
2577 : 2636 : INIT_CRC32C(cp.checksum);
2578 : 2636 : cp.version = SLOT_VERSION;
2579 : 2636 : cp.length = ReplicationSlotOnDiskV2Size;
2580 : :
2581 : 2636 : SpinLockAcquire(&slot->mutex);
2582 : :
2583 : 2636 : memcpy(&cp.slotdata, &slot->data, sizeof(ReplicationSlotPersistentData));
2584 : :
2585 : 2636 : SpinLockRelease(&slot->mutex);
2586 : :
2587 : 2636 : COMP_CRC32C(cp.checksum,
2588 : : (char *) (&cp) + ReplicationSlotOnDiskNotChecksummedSize,
2589 : : ReplicationSlotOnDiskChecksummedSize);
2590 : 2636 : FIN_CRC32C(cp.checksum);
2591 : :
2592 : 2636 : errno = 0;
2593 : 2636 : pgstat_report_wait_start(WAIT_EVENT_REPLICATION_SLOT_WRITE);
2594 [ - + ]: 2636 : if ((write(fd, &cp, sizeof(cp))) != sizeof(cp))
2595 : : {
2596 : 0 : int save_errno = errno;
2597 : :
2598 : 0 : pgstat_report_wait_end();
2599 : 0 : CloseTransientFile(fd);
2600 : 0 : unlink(tmppath);
2601 : 0 : LWLockRelease(&slot->io_in_progress_lock);
2602 : :
2603 : : /* if write didn't set errno, assume problem is no disk space */
2604 [ # # ]: 0 : errno = save_errno ? save_errno : ENOSPC;
2605 [ # # ]: 0 : ereport(elevel,
2606 : : (errcode_for_file_access(),
2607 : : errmsg("could not write to file \"%s\": %m",
2608 : : tmppath)));
2609 : 0 : return;
2610 : : }
2611 : 2636 : pgstat_report_wait_end();
2612 : :
2613 : : /* fsync the temporary file */
2614 : 2636 : pgstat_report_wait_start(WAIT_EVENT_REPLICATION_SLOT_SYNC);
2615 [ - + ]: 2636 : if (pg_fsync(fd) != 0)
2616 : : {
2617 : 0 : int save_errno = errno;
2618 : :
2619 : 0 : pgstat_report_wait_end();
2620 : 0 : CloseTransientFile(fd);
2621 : 0 : unlink(tmppath);
2622 : 0 : LWLockRelease(&slot->io_in_progress_lock);
2623 : :
2624 : 0 : errno = save_errno;
2625 [ # # ]: 0 : ereport(elevel,
2626 : : (errcode_for_file_access(),
2627 : : errmsg("could not fsync file \"%s\": %m",
2628 : : tmppath)));
2629 : 0 : return;
2630 : : }
2631 : 2636 : pgstat_report_wait_end();
2632 : :
2633 [ - + ]: 2636 : if (CloseTransientFile(fd) != 0)
2634 : : {
2635 : 0 : int save_errno = errno;
2636 : :
2637 : 0 : unlink(tmppath);
2638 : 0 : LWLockRelease(&slot->io_in_progress_lock);
2639 : :
2640 : 0 : errno = save_errno;
2641 [ # # ]: 0 : ereport(elevel,
2642 : : (errcode_for_file_access(),
2643 : : errmsg("could not close file \"%s\": %m",
2644 : : tmppath)));
2645 : 0 : return;
2646 : : }
2647 : :
2648 : : /* rename to permanent file, fsync file and directory */
2649 [ - + ]: 2636 : if (rename(tmppath, path) != 0)
2650 : : {
2651 : 0 : int save_errno = errno;
2652 : :
2653 : 0 : unlink(tmppath);
2654 : 0 : LWLockRelease(&slot->io_in_progress_lock);
2655 : :
2656 : 0 : errno = save_errno;
2657 [ # # ]: 0 : ereport(elevel,
2658 : : (errcode_for_file_access(),
2659 : : errmsg("could not rename file \"%s\" to \"%s\": %m",
2660 : : tmppath, path)));
2661 : 0 : return;
2662 : : }
2663 : :
2664 : : /*
2665 : : * Check CreateSlotOnDisk() for the reasoning of using a critical section.
2666 : : */
2667 : 2636 : START_CRIT_SECTION();
2668 : :
2669 : 2636 : fsync_fname(path, false);
2670 : 2636 : fsync_fname(dir, true);
2671 : 2636 : fsync_fname(PG_REPLSLOT_DIR, true);
2672 : :
2673 : 2636 : END_CRIT_SECTION();
2674 : :
2675 : : /*
2676 : : * Successfully wrote, unset dirty bit, unless somebody dirtied again
2677 : : * already and remember the confirmed_flush LSN value.
2678 : : */
2679 : 2636 : SpinLockAcquire(&slot->mutex);
2680 [ + + ]: 2636 : if (!slot->just_dirtied)
2681 : 2606 : slot->dirty = false;
2682 : 2636 : slot->last_saved_confirmed_flush = cp.slotdata.confirmed_flush;
2683 : 2636 : slot->last_saved_restart_lsn = cp.slotdata.restart_lsn;
2684 : 2636 : SpinLockRelease(&slot->mutex);
2685 : :
2686 : 2636 : LWLockRelease(&slot->io_in_progress_lock);
2687 : : }
2688 : :
2689 : : /*
2690 : : * Load a single slot from disk into memory.
2691 : : */
2692 : : static void
2693 : 135 : RestoreSlotFromDisk(const char *name)
2694 : : {
2695 : : ReplicationSlotOnDisk cp;
2696 : : int i;
2697 : : char slotdir[MAXPGPATH + sizeof(PG_REPLSLOT_DIR)];
2698 : : char path[MAXPGPATH + sizeof(PG_REPLSLOT_DIR) + 10];
2699 : : int fd;
2700 : 135 : bool restored = false;
2701 : : ssize_t readBytes;
2702 : : pg_crc32c checksum;
2703 : 135 : TimestampTz now = 0;
2704 : :
2705 : : /* no need to lock here, no concurrent access allowed yet */
2706 : :
2707 : : /* delete temp file if it exists */
2708 : 135 : sprintf(slotdir, "%s/%s", PG_REPLSLOT_DIR, name);
2709 : 135 : sprintf(path, "%s/state.tmp", slotdir);
2710 [ + - - + ]: 135 : if (unlink(path) < 0 && errno != ENOENT)
2711 [ # # ]: 0 : ereport(PANIC,
2712 : : (errcode_for_file_access(),
2713 : : errmsg("could not remove file \"%s\": %m", path)));
2714 : :
2715 : 135 : sprintf(path, "%s/state", slotdir);
2716 : :
2717 [ + + ]: 135 : elog(DEBUG1, "restoring replication slot from \"%s\"", path);
2718 : :
2719 : : /* on some operating systems fsyncing a file requires O_RDWR */
2720 : 135 : fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
2721 : :
2722 : : /*
2723 : : * We do not need to handle this as we are rename()ing the directory into
2724 : : * place only after we fsync()ed the state file.
2725 : : */
2726 [ - + ]: 135 : if (fd < 0)
2727 [ # # ]: 0 : ereport(PANIC,
2728 : : (errcode_for_file_access(),
2729 : : errmsg("could not open file \"%s\": %m", path)));
2730 : :
2731 : : /*
2732 : : * Sync state file before we're reading from it. We might have crashed
2733 : : * while it wasn't synced yet and we shouldn't continue on that basis.
2734 : : */
2735 : 135 : pgstat_report_wait_start(WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC);
2736 [ - + ]: 135 : if (pg_fsync(fd) != 0)
2737 [ # # ]: 0 : ereport(PANIC,
2738 : : (errcode_for_file_access(),
2739 : : errmsg("could not fsync file \"%s\": %m",
2740 : : path)));
2741 : 135 : pgstat_report_wait_end();
2742 : :
2743 : : /* Also sync the parent directory */
2744 : 135 : START_CRIT_SECTION();
2745 : 135 : fsync_fname(slotdir, true);
2746 : 135 : END_CRIT_SECTION();
2747 : :
2748 : : /* read part of statefile that's guaranteed to be version independent */
2749 : 135 : pgstat_report_wait_start(WAIT_EVENT_REPLICATION_SLOT_READ);
2750 : 135 : readBytes = read(fd, &cp, ReplicationSlotOnDiskConstantSize);
2751 : 135 : pgstat_report_wait_end();
2752 [ - + ]: 135 : if (readBytes != ReplicationSlotOnDiskConstantSize)
2753 : : {
2754 [ # # ]: 0 : if (readBytes < 0)
2755 [ # # ]: 0 : ereport(PANIC,
2756 : : (errcode_for_file_access(),
2757 : : errmsg("could not read file \"%s\": %m", path)));
2758 : : else
2759 [ # # ]: 0 : ereport(PANIC,
2760 : : (errcode(ERRCODE_DATA_CORRUPTED),
2761 : : errmsg("could not read file \"%s\": read %zd of %zu",
2762 : : path, readBytes,
2763 : : ReplicationSlotOnDiskConstantSize)));
2764 : : }
2765 : :
2766 : : /* verify magic */
2767 [ - + ]: 135 : if (cp.magic != SLOT_MAGIC)
2768 [ # # ]: 0 : ereport(PANIC,
2769 : : (errcode(ERRCODE_DATA_CORRUPTED),
2770 : : errmsg("replication slot file \"%s\" has wrong magic number: %u instead of %u",
2771 : : path, cp.magic, SLOT_MAGIC)));
2772 : :
2773 : : /* verify version */
2774 [ - + ]: 135 : if (cp.version != SLOT_VERSION)
2775 [ # # ]: 0 : ereport(PANIC,
2776 : : (errcode(ERRCODE_DATA_CORRUPTED),
2777 : : errmsg("replication slot file \"%s\" has unsupported version %u",
2778 : : path, cp.version)));
2779 : :
2780 : : /* boundary check on length */
2781 [ - + ]: 135 : if (cp.length != ReplicationSlotOnDiskV2Size)
2782 [ # # ]: 0 : ereport(PANIC,
2783 : : (errcode(ERRCODE_DATA_CORRUPTED),
2784 : : errmsg("replication slot file \"%s\" has corrupted length %u",
2785 : : path, cp.length)));
2786 : :
2787 : : /* Now that we know the size, read the entire file */
2788 : 135 : pgstat_report_wait_start(WAIT_EVENT_REPLICATION_SLOT_READ);
2789 : 135 : readBytes = read(fd,
2790 : : (char *) &cp + ReplicationSlotOnDiskConstantSize,
2791 : 135 : cp.length);
2792 : 135 : pgstat_report_wait_end();
2793 [ - + ]: 135 : if (readBytes != cp.length)
2794 : : {
2795 [ # # ]: 0 : if (readBytes < 0)
2796 [ # # ]: 0 : ereport(PANIC,
2797 : : (errcode_for_file_access(),
2798 : : errmsg("could not read file \"%s\": %m", path)));
2799 : : else
2800 [ # # ]: 0 : ereport(PANIC,
2801 : : (errcode(ERRCODE_DATA_CORRUPTED),
2802 : : errmsg("could not read file \"%s\": read %zd of %zu",
2803 : : path, readBytes, (Size) cp.length)));
2804 : : }
2805 : :
2806 [ - + ]: 135 : if (CloseTransientFile(fd) != 0)
2807 [ # # ]: 0 : ereport(PANIC,
2808 : : (errcode_for_file_access(),
2809 : : errmsg("could not close file \"%s\": %m", path)));
2810 : :
2811 : : /* now verify the CRC */
2812 : 135 : INIT_CRC32C(checksum);
2813 : 135 : COMP_CRC32C(checksum,
2814 : : (char *) &cp + ReplicationSlotOnDiskNotChecksummedSize,
2815 : : ReplicationSlotOnDiskChecksummedSize);
2816 : 135 : FIN_CRC32C(checksum);
2817 : :
2818 [ - + ]: 135 : if (!EQ_CRC32C(checksum, cp.checksum))
2819 [ # # ]: 0 : ereport(PANIC,
2820 : : (errmsg("checksum mismatch for replication slot file \"%s\": is %u, should be %u",
2821 : : path, checksum, cp.checksum)));
2822 : :
2823 : : /*
2824 : : * If we crashed with an ephemeral slot active, don't restore but delete
2825 : : * it.
2826 : : */
2827 [ - + ]: 135 : if (cp.slotdata.persistency != RS_PERSISTENT)
2828 : : {
2829 [ # # ]: 0 : if (!rmtree(slotdir, true))
2830 : : {
2831 [ # # ]: 0 : ereport(WARNING,
2832 : : (errmsg("could not remove directory \"%s\"",
2833 : : slotdir)));
2834 : : }
2835 : 0 : fsync_fname(PG_REPLSLOT_DIR, true);
2836 : 0 : return;
2837 : : }
2838 : :
2839 : : /*
2840 : : * Verify that requirements for the specific slot type are met. That's
2841 : : * important because if these aren't met we're not guaranteed to retain
2842 : : * all the necessary resources for the slot.
2843 : : *
2844 : : * NB: We have to do so *after* the above checks for ephemeral slots,
2845 : : * because otherwise a slot that shouldn't exist anymore could prevent
2846 : : * restarts.
2847 : : *
2848 : : * NB: Changing the requirements here also requires adapting
2849 : : * CheckSlotRequirements() and CheckLogicalDecodingRequirements().
2850 : : */
2851 [ + + ]: 135 : if (cp.slotdata.database != InvalidOid)
2852 : : {
2853 [ + + ]: 96 : if (wal_level < WAL_LEVEL_REPLICA)
2854 [ + - ]: 1 : ereport(FATAL,
2855 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2856 : : errmsg("logical replication slot \"%s\" exists, but \"wal_level\" < \"replica\"",
2857 : : NameStr(cp.slotdata.name)),
2858 : : errhint("Change \"wal_level\" to be \"replica\" or higher.")));
2859 : :
2860 : : /*
2861 : : * In standby mode, the hot standby must be enabled. This check is
2862 : : * necessary to ensure logical slots are invalidated when they become
2863 : : * incompatible due to insufficient wal_level. Otherwise, if the
2864 : : * primary reduces effective_wal_level < logical while hot standby is
2865 : : * disabled, primary disable logical decoding while hot standby is
2866 : : * disabled, logical slots would remain valid even after promotion.
2867 : : */
2868 [ + + + + ]: 95 : if (StandbyMode && !EnableHotStandby)
2869 [ + - ]: 1 : ereport(FATAL,
2870 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2871 : : errmsg("logical replication slot \"%s\" exists on the standby, but \"hot_standby\" = \"off\"",
2872 : : NameStr(cp.slotdata.name)),
2873 : : errhint("Change \"hot_standby\" to be \"on\".")));
2874 : : }
2875 [ - + ]: 39 : else if (wal_level < WAL_LEVEL_REPLICA)
2876 [ # # ]: 0 : ereport(FATAL,
2877 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2878 : : errmsg("physical replication slot \"%s\" exists, but \"wal_level\" < \"replica\"",
2879 : : NameStr(cp.slotdata.name)),
2880 : : errhint("Change \"wal_level\" to be \"replica\" or higher.")));
2881 : :
2882 : : /*
2883 : : * Nothing can be active yet, don't lock anything. Note we iterate up to
2884 : : * max_replication_slots instead of adding max_repack_replication_slots as
2885 : : * in all other places, because we must enforce the GUC value in case
2886 : : * there were more slots before the shutdown than what it is set up to
2887 : : * now.
2888 : : */
2889 [ + - ]: 200 : for (i = 0; i < max_replication_slots; i++)
2890 : : {
2891 : : ReplicationSlot *slot;
2892 : :
2893 : 200 : slot = &ReplicationSlotCtl->replication_slots[i];
2894 : :
2895 [ + + ]: 200 : if (slot->in_use)
2896 : 67 : continue;
2897 : :
2898 : : /* restore the entire set of persistent data */
2899 : 133 : memcpy(&slot->data, &cp.slotdata,
2900 : : sizeof(ReplicationSlotPersistentData));
2901 : :
2902 : : /* initialize in memory state */
2903 : 133 : slot->effective_xmin = cp.slotdata.xmin;
2904 : 133 : slot->effective_catalog_xmin = cp.slotdata.catalog_xmin;
2905 : 133 : slot->last_saved_confirmed_flush = cp.slotdata.confirmed_flush;
2906 : 133 : slot->last_saved_restart_lsn = cp.slotdata.restart_lsn;
2907 : :
2908 : 133 : slot->candidate_catalog_xmin = InvalidTransactionId;
2909 : 133 : slot->candidate_xmin_lsn = InvalidXLogRecPtr;
2910 : 133 : slot->candidate_restart_lsn = InvalidXLogRecPtr;
2911 : 133 : slot->candidate_restart_valid = InvalidXLogRecPtr;
2912 : :
2913 : 133 : slot->in_use = true;
2914 : 133 : slot->active_proc = INVALID_PROC_NUMBER;
2915 : :
2916 : : /*
2917 : : * Set the time since the slot has become inactive after loading the
2918 : : * slot from the disk into memory. Whoever acquires the slot i.e.
2919 : : * makes the slot active will reset it. Use the same inactive_since
2920 : : * time for all the slots.
2921 : : */
2922 [ + - ]: 133 : if (now == 0)
2923 : 133 : now = GetCurrentTimestamp();
2924 : :
2925 : 133 : ReplicationSlotSetInactiveSince(slot, now, false);
2926 : :
2927 : 133 : restored = true;
2928 : 133 : break;
2929 : : }
2930 : :
2931 [ - + ]: 133 : if (!restored)
2932 [ # # ]: 0 : ereport(FATAL,
2933 : : (errmsg("too many replication slots active before shutdown"),
2934 : : errhint("Increase \"max_replication_slots\" and try again.")));
2935 : : }
2936 : :
2937 : : /*
2938 : : * Maps an invalidation reason for a replication slot to
2939 : : * ReplicationSlotInvalidationCause.
2940 : : */
2941 : : ReplicationSlotInvalidationCause
2942 : 0 : GetSlotInvalidationCause(const char *cause_name)
2943 : : {
2944 : : Assert(cause_name);
2945 : :
2946 : : /* Search lookup table for the cause having this name */
2947 [ # # ]: 0 : for (int i = 0; i <= RS_INVAL_MAX_CAUSES; i++)
2948 : : {
2949 [ # # ]: 0 : if (strcmp(SlotInvalidationCauses[i].cause_name, cause_name) == 0)
2950 : 0 : return SlotInvalidationCauses[i].cause;
2951 : : }
2952 : :
2953 : : Assert(false);
2954 : 0 : return RS_INVAL_NONE; /* to keep compiler quiet */
2955 : : }
2956 : :
2957 : : /*
2958 : : * Maps a ReplicationSlotInvalidationCause to the invalidation
2959 : : * reason for a replication slot.
2960 : : */
2961 : : const char *
2962 : 44 : GetSlotInvalidationCauseName(ReplicationSlotInvalidationCause cause)
2963 : : {
2964 : : /* Search lookup table for the name of this cause */
2965 [ + - ]: 141 : for (int i = 0; i <= RS_INVAL_MAX_CAUSES; i++)
2966 : : {
2967 [ + + ]: 141 : if (SlotInvalidationCauses[i].cause == cause)
2968 : 44 : return SlotInvalidationCauses[i].cause_name;
2969 : : }
2970 : :
2971 : : Assert(false);
2972 : 0 : return "none"; /* to keep compiler quiet */
2973 : : }
2974 : :
2975 : : /*
2976 : : * A helper function to validate slots specified in GUC synchronized_standby_slots.
2977 : : *
2978 : : * The rawname will be parsed, and the result will be saved into *elemlist.
2979 : : */
2980 : : static bool
2981 : 28 : validate_sync_standby_slots(char *rawname, List **elemlist)
2982 : : {
2983 : : /* Verify syntax and parse string into a list of identifiers */
2984 [ - + ]: 28 : if (!SplitIdentifierString(rawname, ',', elemlist))
2985 : : {
2986 : 0 : GUC_check_errdetail("List syntax is invalid.");
2987 : 0 : return false;
2988 : : }
2989 : :
2990 : : /* Iterate the list to validate each slot name */
2991 [ + - + + : 80 : foreach_ptr(char, name, *elemlist)
+ + ]
2992 : : {
2993 : : int err_code;
2994 : 28 : char *err_msg = NULL;
2995 : 28 : char *err_hint = NULL;
2996 : :
2997 [ + + ]: 28 : if (!ReplicationSlotValidateNameInternal(name, false, &err_code,
2998 : : &err_msg, &err_hint))
2999 : : {
3000 : 2 : GUC_check_errcode(err_code);
3001 : 2 : GUC_check_errdetail("%s", err_msg);
3002 [ + - ]: 2 : if (err_hint != NULL)
3003 : 2 : GUC_check_errhint("%s", err_hint);
3004 : 2 : return false;
3005 : : }
3006 : : }
3007 : :
3008 : 26 : return true;
3009 : : }
3010 : :
3011 : : /*
3012 : : * GUC check_hook for synchronized_standby_slots
3013 : : */
3014 : : bool
3015 : 1403 : check_synchronized_standby_slots(char **newval, void **extra, GucSource source)
3016 : : {
3017 : : char *rawname;
3018 : : char *ptr;
3019 : : List *elemlist;
3020 : : int size;
3021 : : bool ok;
3022 : : SyncStandbySlotsConfigData *config;
3023 : :
3024 [ + + ]: 1403 : if ((*newval)[0] == '\0')
3025 : 1375 : return true;
3026 : :
3027 : : /* Need a modifiable copy of the GUC string */
3028 : 28 : rawname = pstrdup(*newval);
3029 : :
3030 : : /* Now verify if the specified slots exist and have correct type */
3031 : 28 : ok = validate_sync_standby_slots(rawname, &elemlist);
3032 : :
3033 [ + + - + ]: 28 : if (!ok || elemlist == NIL)
3034 : : {
3035 : 2 : pfree(rawname);
3036 : 2 : list_free(elemlist);
3037 : 2 : return ok;
3038 : : }
3039 : :
3040 : : /* Compute the size required for the SyncStandbySlotsConfigData struct */
3041 : 26 : size = offsetof(SyncStandbySlotsConfigData, slot_names);
3042 [ + - + + : 78 : foreach_ptr(char, slot_name, elemlist)
+ + ]
3043 : 26 : size += strlen(slot_name) + 1;
3044 : :
3045 : : /* GUC extra value must be guc_malloc'd, not palloc'd */
3046 : 26 : config = (SyncStandbySlotsConfigData *) guc_malloc(LOG, size);
3047 [ - + ]: 26 : if (!config)
3048 : 0 : return false;
3049 : :
3050 : : /* Transform the data into SyncStandbySlotsConfigData */
3051 : 26 : config->nslotnames = list_length(elemlist);
3052 : :
3053 : 26 : ptr = config->slot_names;
3054 [ + - + + : 78 : foreach_ptr(char, slot_name, elemlist)
+ + ]
3055 : : {
3056 : 26 : strcpy(ptr, slot_name);
3057 : 26 : ptr += strlen(slot_name) + 1;
3058 : : }
3059 : :
3060 : 26 : *extra = config;
3061 : :
3062 : 26 : pfree(rawname);
3063 : 26 : list_free(elemlist);
3064 : 26 : return true;
3065 : : }
3066 : :
3067 : : /*
3068 : : * GUC assign_hook for synchronized_standby_slots
3069 : : */
3070 : : void
3071 : 1400 : assign_synchronized_standby_slots(const char *newval, void *extra)
3072 : : {
3073 : : /*
3074 : : * The standby slots may have changed, so we must recompute the oldest
3075 : : * LSN.
3076 : : */
3077 : 1400 : ss_oldest_flush_lsn = InvalidXLogRecPtr;
3078 : :
3079 : 1400 : synchronized_standby_slots_config = (SyncStandbySlotsConfigData *) extra;
3080 : 1400 : }
3081 : :
3082 : : /*
3083 : : * Check if the passed slot_name is specified in the synchronized_standby_slots GUC.
3084 : : */
3085 : : bool
3086 : 39259 : SlotExistsInSyncStandbySlots(const char *slot_name)
3087 : : {
3088 : : const char *standby_slot_name;
3089 : :
3090 : : /* Return false if there is no value in synchronized_standby_slots */
3091 [ + + ]: 39259 : if (synchronized_standby_slots_config == NULL)
3092 : 39243 : return false;
3093 : :
3094 : : /*
3095 : : * XXX: We are not expecting this list to be long so a linear search
3096 : : * shouldn't hurt but if that turns out not to be true then we can cache
3097 : : * this information for each WalSender as well.
3098 : : */
3099 : 16 : standby_slot_name = synchronized_standby_slots_config->slot_names;
3100 [ + + ]: 24 : for (int i = 0; i < synchronized_standby_slots_config->nslotnames; i++)
3101 : : {
3102 [ + + ]: 16 : if (strcmp(standby_slot_name, slot_name) == 0)
3103 : 8 : return true;
3104 : :
3105 : 8 : standby_slot_name += strlen(standby_slot_name) + 1;
3106 : : }
3107 : :
3108 : 8 : return false;
3109 : : }
3110 : :
3111 : : /*
3112 : : * Return true if the slots specified in synchronized_standby_slots have caught up to
3113 : : * the given WAL location, false otherwise.
3114 : : *
3115 : : * The elevel parameter specifies the error level used for logging messages
3116 : : * related to slots that do not exist, are invalidated, or are inactive.
3117 : : */
3118 : : bool
3119 : 3224 : StandbySlotsHaveCaughtup(XLogRecPtr wait_for_lsn, int elevel)
3120 : : {
3121 : : const char *name;
3122 : 3224 : int caught_up_slot_num = 0;
3123 : 3224 : XLogRecPtr min_restart_lsn = InvalidXLogRecPtr;
3124 : :
3125 : : /*
3126 : : * Don't need to wait for the standbys to catch up if there is no value in
3127 : : * synchronized_standby_slots.
3128 : : */
3129 [ + + ]: 3224 : if (synchronized_standby_slots_config == NULL)
3130 : 3191 : return true;
3131 : :
3132 : : /*
3133 : : * Don't need to wait for the standbys to catch up if we are on a standby
3134 : : * server, since we do not support syncing slots to cascading standbys.
3135 : : */
3136 [ - + ]: 33 : if (RecoveryInProgress())
3137 : 0 : return true;
3138 : :
3139 : : /*
3140 : : * Don't need to wait for the standbys to catch up if they are already
3141 : : * beyond the specified WAL location.
3142 : : */
3143 [ + + ]: 33 : if (XLogRecPtrIsValid(ss_oldest_flush_lsn) &&
3144 [ + + ]: 18 : ss_oldest_flush_lsn >= wait_for_lsn)
3145 : 9 : return true;
3146 : :
3147 : : /*
3148 : : * To prevent concurrent slot dropping and creation while filtering the
3149 : : * slots, take the ReplicationSlotControlLock outside of the loop.
3150 : : */
3151 : 24 : LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
3152 : :
3153 : 24 : name = synchronized_standby_slots_config->slot_names;
3154 [ + + ]: 32 : for (int i = 0; i < synchronized_standby_slots_config->nslotnames; i++)
3155 : : {
3156 : : XLogRecPtr restart_lsn;
3157 : : bool invalidated;
3158 : : bool inactive;
3159 : : ReplicationSlot *slot;
3160 : :
3161 : 24 : slot = SearchNamedReplicationSlot(name, false);
3162 : :
3163 : : /*
3164 : : * If a slot name provided in synchronized_standby_slots does not
3165 : : * exist, report a message and exit the loop.
3166 : : */
3167 [ - + ]: 24 : if (!slot)
3168 : : {
3169 [ # # ]: 0 : ereport(elevel,
3170 : : errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3171 : : errmsg("replication slot \"%s\" specified in parameter \"%s\" does not exist",
3172 : : name, "synchronized_standby_slots"),
3173 : : errdetail("Logical replication is waiting on the standby associated with replication slot \"%s\".",
3174 : : name),
3175 : : errhint("Create the replication slot \"%s\" or amend parameter \"%s\".",
3176 : : name, "synchronized_standby_slots"));
3177 : 0 : break;
3178 : : }
3179 : :
3180 : : /* Same as above: if a slot is not physical, exit the loop. */
3181 [ - + ]: 24 : if (SlotIsLogical(slot))
3182 : : {
3183 [ # # ]: 0 : ereport(elevel,
3184 : : errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3185 : : errmsg("cannot specify logical replication slot \"%s\" in parameter \"%s\"",
3186 : : name, "synchronized_standby_slots"),
3187 : : errdetail("Logical replication is waiting for correction on replication slot \"%s\".",
3188 : : name),
3189 : : errhint("Remove the logical replication slot \"%s\" from parameter \"%s\".",
3190 : : name, "synchronized_standby_slots"));
3191 : 0 : break;
3192 : : }
3193 : :
3194 : 24 : SpinLockAcquire(&slot->mutex);
3195 : 24 : restart_lsn = slot->data.restart_lsn;
3196 : 24 : invalidated = slot->data.invalidated != RS_INVAL_NONE;
3197 : 24 : inactive = slot->active_proc == INVALID_PROC_NUMBER;
3198 : 24 : SpinLockRelease(&slot->mutex);
3199 : :
3200 [ - + ]: 24 : if (invalidated)
3201 : : {
3202 : : /* Specified physical slot has been invalidated */
3203 [ # # ]: 0 : ereport(elevel,
3204 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3205 : : errmsg("physical replication slot \"%s\" specified in parameter \"%s\" has been invalidated",
3206 : : name, "synchronized_standby_slots"),
3207 : : errdetail("Logical replication is waiting on the standby associated with replication slot \"%s\".",
3208 : : name),
3209 : : errhint("Drop and recreate the replication slot \"%s\", or amend parameter \"%s\".",
3210 : : name, "synchronized_standby_slots"));
3211 : 0 : break;
3212 : : }
3213 : :
3214 [ + + + + ]: 24 : if (!XLogRecPtrIsValid(restart_lsn) || restart_lsn < wait_for_lsn)
3215 : : {
3216 : : /* Log a message if no active_pid for this physical slot */
3217 [ + + ]: 16 : if (inactive)
3218 [ + - ]: 11 : ereport(elevel,
3219 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3220 : : errmsg("replication slot \"%s\" specified in parameter \"%s\" does not have active_pid",
3221 : : name, "synchronized_standby_slots"),
3222 : : errdetail("Logical replication is waiting on the standby associated with replication slot \"%s\".",
3223 : : name),
3224 : : errhint("Start the standby associated with the replication slot \"%s\", or amend parameter \"%s\".",
3225 : : name, "synchronized_standby_slots"));
3226 : :
3227 : : /* Continue if the current slot hasn't caught up. */
3228 : 16 : break;
3229 : : }
3230 : :
3231 : : Assert(restart_lsn >= wait_for_lsn);
3232 : :
3233 [ - + - - ]: 8 : if (!XLogRecPtrIsValid(min_restart_lsn) ||
3234 : : min_restart_lsn > restart_lsn)
3235 : 8 : min_restart_lsn = restart_lsn;
3236 : :
3237 : 8 : caught_up_slot_num++;
3238 : :
3239 : 8 : name += strlen(name) + 1;
3240 : : }
3241 : :
3242 : 24 : LWLockRelease(ReplicationSlotControlLock);
3243 : :
3244 : : /*
3245 : : * Return false if not all the standbys have caught up to the specified
3246 : : * WAL location.
3247 : : */
3248 [ + + ]: 24 : if (caught_up_slot_num != synchronized_standby_slots_config->nslotnames)
3249 : 16 : return false;
3250 : :
3251 : : /* The ss_oldest_flush_lsn must not retreat. */
3252 : : Assert(!XLogRecPtrIsValid(ss_oldest_flush_lsn) ||
3253 : : min_restart_lsn >= ss_oldest_flush_lsn);
3254 : :
3255 : 8 : ss_oldest_flush_lsn = min_restart_lsn;
3256 : :
3257 : 8 : return true;
3258 : : }
3259 : :
3260 : : /*
3261 : : * Wait for physical standbys to confirm receiving the given lsn.
3262 : : *
3263 : : * Used by logical decoding SQL functions. It waits for physical standbys
3264 : : * corresponding to the physical slots specified in the synchronized_standby_slots GUC.
3265 : : */
3266 : : void
3267 : 241 : WaitForStandbyConfirmation(XLogRecPtr wait_for_lsn)
3268 : : {
3269 : : /*
3270 : : * Don't need to wait for the standby to catch up if the current acquired
3271 : : * slot is not a logical failover slot, or there is no value in
3272 : : * synchronized_standby_slots.
3273 : : */
3274 [ + + + + ]: 241 : if (!MyReplicationSlot->data.failover || !synchronized_standby_slots_config)
3275 : 240 : return;
3276 : :
3277 : 1 : ConditionVariablePrepareToSleep(&WalSndCtl->wal_confirm_rcv_cv);
3278 : :
3279 : : for (;;)
3280 : : {
3281 [ - + ]: 2 : CHECK_FOR_INTERRUPTS();
3282 : :
3283 [ + + ]: 2 : if (ConfigReloadPending)
3284 : : {
3285 : 1 : ConfigReloadPending = false;
3286 : 1 : ProcessConfigFile(PGC_SIGHUP);
3287 : : }
3288 : :
3289 : : /* Exit if done waiting for every slot. */
3290 [ + + ]: 2 : if (StandbySlotsHaveCaughtup(wait_for_lsn, WARNING))
3291 : 1 : break;
3292 : :
3293 : : /*
3294 : : * Wait for the slots in the synchronized_standby_slots to catch up,
3295 : : * but use a timeout (1s) so we can also check if the
3296 : : * synchronized_standby_slots has been changed.
3297 : : */
3298 : 1 : ConditionVariableTimedSleep(&WalSndCtl->wal_confirm_rcv_cv, 1000,
3299 : : WAIT_EVENT_WAIT_FOR_STANDBY_CONFIRMATION);
3300 : : }
3301 : :
3302 : 1 : ConditionVariableCancelSleep();
3303 : : }
|