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