Debian Codegraph @

src/backend/replication/slot.c

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