| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* slotfuncs.c |
| 4 |
* Support functions for replication slots |
| 5 |
* |
| 6 |
* Copyright (c) 2012-2025, PostgreSQL Global Development Group |
| 7 |
* |
| 8 |
* IDENTIFICATION |
| 9 |
* src/backend/replication/slotfuncs.c |
| 10 |
* |
| 11 |
*------------------------------------------------------------------------- |
| 12 |
*/ |
| 13 |
#include "postgres.h" |
| 14 |
|
| 15 |
#include "access/htup_details.h" |
| 16 |
#include "access/xlog_internal.h" |
| 17 |
#include "access/xlogrecovery.h" |
| 18 |
#include "access/xlogutils.h" |
| 19 |
#include "funcapi.h" |
| 20 |
#include "replication/logical.h" |
| 21 |
#include "replication/slot.h" |
| 22 |
#include "replication/slotsync.h" |
| 23 |
#include "utils/builtins.h" |
| 24 |
#include "utils/guc.h" |
| 25 |
#include "utils/pg_lsn.h" |
| 26 |
|
| 27 |
/* |
| 28 |
* Helper function for creating a new physical replication slot with |
| 29 |
* given arguments. Note that this function doesn't release the created |
| 30 |
* slot. |
| 31 |
* |
| 32 |
* If restart_lsn is a valid value, we use it without WAL reservation |
| 33 |
* routine. So the caller must guarantee that WAL is available. |
| 34 |
*/ |
| 35 |
static void |
| 36 |
create_physical_replication_slot(char *name, bool immediately_reserve, |
| 37 |
bool temporary, XLogRecPtr restart_lsn) |
| 38 |
{ |
| 39 |
Assert(!MyReplicationSlot); |
| 40 |
|
| 41 |
/* acquire replication slot, this will check for conflicting names */ |
| 42 |
ReplicationSlotCreate(name, false, |
| 43 |
temporary ? RS_TEMPORARY : RS_PERSISTENT, false, |
| 44 |
false, false); |
| 45 |
|
| 46 |
if (immediately_reserve) |
| 47 |
{ |
| 48 |
/* Reserve WAL as the user asked for it */ |
| 49 |
if (XLogRecPtrIsInvalid(restart_lsn)) |
| 50 |
ReplicationSlotReserveWal(); |
| 51 |
else |
| 52 |
MyReplicationSlot->data.restart_lsn = restart_lsn; |
| 53 |
|
| 54 |
/* Write this slot to disk */ |
| 55 |
ReplicationSlotMarkDirty(); |
| 56 |
ReplicationSlotSave(); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* SQL function for creating a new physical (streaming replication) |
| 62 |
* replication slot. |
| 63 |
*/ |
| 64 |
Datum |
| 65 |
pg_create_physical_replication_slot(PG_FUNCTION_ARGS) |
| 66 |
{ |
| 67 |
Name name = PG_GETARG_NAME(0); |
| 68 |
bool immediately_reserve = PG_GETARG_BOOL(1); |
| 69 |
bool temporary = PG_GETARG_BOOL(2); |
| 70 |
Datum values[2]; |
| 71 |
bool nulls[2]; |
| 72 |
TupleDesc tupdesc; |
| 73 |
HeapTuple tuple; |
| 74 |
Datum result; |
| 75 |
|
| 76 |
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) |
| 77 |
elog(ERROR, "return type must be a row type"); |
| 78 |
|
| 79 |
CheckSlotPermissions(); |
| 80 |
|
| 81 |
CheckSlotRequirements(); |
| 82 |
|
| 83 |
create_physical_replication_slot(NameStr(*name), |
| 84 |
immediately_reserve, |
| 85 |
temporary, |
| 86 |
InvalidXLogRecPtr); |
| 87 |
|
| 88 |
values[0] = NameGetDatum(&MyReplicationSlot->data.name); |
| 89 |
nulls[0] = false; |
| 90 |
|
| 91 |
if (immediately_reserve) |
| 92 |
{ |
| 93 |
values[1] = LSNGetDatum(MyReplicationSlot->data.restart_lsn); |
| 94 |
nulls[1] = false; |
| 95 |
} |
| 96 |
else |
| 97 |
nulls[1] = true; |
| 98 |
|
| 99 |
tuple = heap_form_tuple(tupdesc, values, nulls); |
| 100 |
result = HeapTupleGetDatum(tuple); |
| 101 |
|
| 102 |
ReplicationSlotRelease(); |
| 103 |
|
| 104 |
PG_RETURN_DATUM(result); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
/* |
| 109 |
* Helper function for creating a new logical replication slot with |
| 110 |
* given arguments. Note that this function doesn't release the created |
| 111 |
* slot. |
| 112 |
* |
| 113 |
* When find_startpoint is false, the slot's confirmed_flush is not set; it's |
| 114 |
* caller's responsibility to ensure it's set to something sensible. |
| 115 |
*/ |
| 116 |
static void |
| 117 |
create_logical_replication_slot(char *name, char *plugin, |
| 118 |
bool temporary, bool two_phase, |
| 119 |
bool failover, |
| 120 |
XLogRecPtr restart_lsn, |
| 121 |
bool find_startpoint) |
| 122 |
{ |
| 123 |
LogicalDecodingContext *ctx = NULL; |
| 124 |
|
| 125 |
Assert(!MyReplicationSlot); |
| 126 |
|
| 127 |
/* |
| 128 |
* Acquire a logical decoding slot, this will check for conflicting names. |
| 129 |
* Initially create persistent slot as ephemeral - that allows us to |
| 130 |
* nicely handle errors during initialization because it'll get dropped if |
| 131 |
* this transaction fails. We'll make it persistent at the end. Temporary |
| 132 |
* slots can be created as temporary from beginning as they get dropped on |
| 133 |
* error as well. |
| 134 |
*/ |
| 135 |
ReplicationSlotCreate(name, true, |
| 136 |
temporary ? RS_TEMPORARY : RS_EPHEMERAL, two_phase, |
| 137 |
failover, false); |
| 138 |
|
| 139 |
/* |
| 140 |
* Create logical decoding context to find start point or, if we don't |
| 141 |
* need it, to 1) bump slot's restart_lsn and xmin 2) check plugin sanity. |
| 142 |
* |
| 143 |
* Note: when !find_startpoint this is still important, because it's at |
| 144 |
* this point that the output plugin is validated. |
| 145 |
*/ |
| 146 |
ctx = CreateInitDecodingContext(plugin, NIL, |
| 147 |
false, /* just catalogs is OK */ |
| 148 |
restart_lsn, |
| 149 |
XL_ROUTINE(.page_read = read_local_xlog_page, |
| 150 |
.segment_open = wal_segment_open, |
| 151 |
.segment_close = wal_segment_close), |
| 152 |
NULL, NULL, NULL); |
| 153 |
|
| 154 |
/* |
| 155 |
* If caller needs us to determine the decoding start point, do so now. |
| 156 |
* This might take a while. |
| 157 |
*/ |
| 158 |
if (find_startpoint) |
| 159 |
DecodingContextFindStartpoint(ctx); |
| 160 |
|
| 161 |
/* don't need the decoding context anymore */ |
| 162 |
FreeDecodingContext(ctx); |
| 163 |
} |
| 164 |
|
| 165 |
/* |
| 166 |
* SQL function for creating a new logical replication slot. |
| 167 |
*/ |
| 168 |
Datum |
| 169 |
pg_create_logical_replication_slot(PG_FUNCTION_ARGS) |
| 170 |
{ |
| 171 |
Name name = PG_GETARG_NAME(0); |
| 172 |
Name plugin = PG_GETARG_NAME(1); |
| 173 |
bool temporary = PG_GETARG_BOOL(2); |
| 174 |
bool two_phase = PG_GETARG_BOOL(3); |
| 175 |
bool failover = PG_GETARG_BOOL(4); |
| 176 |
Datum result; |
| 177 |
TupleDesc tupdesc; |
| 178 |
HeapTuple tuple; |
| 179 |
Datum values[2]; |
| 180 |
bool nulls[2]; |
| 181 |
|
| 182 |
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) |
| 183 |
elog(ERROR, "return type must be a row type"); |
| 184 |
|
| 185 |
CheckSlotPermissions(); |
| 186 |
|
| 187 |
CheckLogicalDecodingRequirements(); |
| 188 |
|
| 189 |
create_logical_replication_slot(NameStr(*name), |
| 190 |
NameStr(*plugin), |
| 191 |
temporary, |
| 192 |
two_phase, |
| 193 |
failover, |
| 194 |
InvalidXLogRecPtr, |
| 195 |
true); |
| 196 |
|
| 197 |
values[0] = NameGetDatum(&MyReplicationSlot->data.name); |
| 198 |
values[1] = LSNGetDatum(MyReplicationSlot->data.confirmed_flush); |
| 199 |
|
| 200 |
memset(nulls, 0, sizeof(nulls)); |
| 201 |
|
| 202 |
tuple = heap_form_tuple(tupdesc, values, nulls); |
| 203 |
result = HeapTupleGetDatum(tuple); |
| 204 |
|
| 205 |
/* ok, slot is now fully created, mark it as persistent if needed */ |
| 206 |
if (!temporary) |
| 207 |
ReplicationSlotPersist(); |
| 208 |
ReplicationSlotRelease(); |
| 209 |
|
| 210 |
PG_RETURN_DATUM(result); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
/* |
| 215 |
* SQL function for dropping a replication slot. |
| 216 |
*/ |
| 217 |
Datum |
| 218 |
pg_drop_replication_slot(PG_FUNCTION_ARGS) |
| 219 |
{ |
| 220 |
Name name = PG_GETARG_NAME(0); |
| 221 |
|
| 222 |
CheckSlotPermissions(); |
| 223 |
|
| 224 |
CheckSlotRequirements(); |
| 225 |
|
| 226 |
ReplicationSlotDrop(NameStr(*name), true); |
| 227 |
|
| 228 |
PG_RETURN_VOID(); |
| 229 |
} |
| 230 |
|
| 231 |
/* |
| 232 |
* pg_get_replication_slots - SQL SRF showing all replication slots |
| 233 |
* that currently exist on the database cluster. |
| 234 |
*/ |
| 235 |
Datum |
| 236 |
pg_get_replication_slots(PG_FUNCTION_ARGS) |
| 237 |
{ |
| 238 |
#define PG_GET_REPLICATION_SLOTS_COLS 20 |
| 239 |
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
| 240 |
XLogRecPtr currlsn; |
| 241 |
int slotno; |
| 242 |
|
| 243 |
/* |
| 244 |
* We don't require any special permission to see this function's data |
| 245 |
* because nothing should be sensitive. The most critical being the slot |
| 246 |
* name, which shouldn't contain anything particularly sensitive. |
| 247 |
*/ |
| 248 |
|
| 249 |
InitMaterializedSRF(fcinfo, 0); |
| 250 |
|
| 251 |
currlsn = GetXLogWriteRecPtr(); |
| 252 |
|
| 253 |
LWLockAcquire(ReplicationSlotControlLock, LW_SHARED); |
| 254 |
for (slotno = 0; slotno < max_replication_slots; slotno++) |
| 255 |
{ |
| 256 |
ReplicationSlot *slot = &ReplicationSlotCtl->replication_slots[slotno]; |
| 257 |
ReplicationSlot slot_contents; |
| 258 |
Datum values[PG_GET_REPLICATION_SLOTS_COLS]; |
| 259 |
bool nulls[PG_GET_REPLICATION_SLOTS_COLS]; |
| 260 |
WALAvailability walstate; |
| 261 |
int i; |
| 262 |
ReplicationSlotInvalidationCause cause; |
| 263 |
|
| 264 |
if (!slot->in_use) |
| 265 |
continue; |
| 266 |
|
| 267 |
/* Copy slot contents while holding spinlock, then examine at leisure */ |
| 268 |
SpinLockAcquire(&slot->mutex); |
| 269 |
slot_contents = *slot; |
| 270 |
SpinLockRelease(&slot->mutex); |
| 271 |
|
| 272 |
memset(values, 0, sizeof(values)); |
| 273 |
memset(nulls, 0, sizeof(nulls)); |
| 274 |
|
| 275 |
i = 0; |
| 276 |
values[i++] = NameGetDatum(&slot_contents.data.name); |
| 277 |
|
| 278 |
if (slot_contents.data.database == InvalidOid) |
| 279 |
nulls[i++] = true; |
| 280 |
else |
| 281 |
values[i++] = NameGetDatum(&slot_contents.data.plugin); |
| 282 |
|
| 283 |
if (slot_contents.data.database == InvalidOid) |
| 284 |
values[i++] = CStringGetTextDatum("physical"); |
| 285 |
else |
| 286 |
values[i++] = CStringGetTextDatum("logical"); |
| 287 |
|
| 288 |
if (slot_contents.data.database == InvalidOid) |
| 289 |
nulls[i++] = true; |
| 290 |
else |
| 291 |
values[i++] = ObjectIdGetDatum(slot_contents.data.database); |
| 292 |
|
| 293 |
values[i++] = BoolGetDatum(slot_contents.data.persistency == RS_TEMPORARY); |
| 294 |
values[i++] = BoolGetDatum(slot_contents.active_pid != 0); |
| 295 |
|
| 296 |
if (slot_contents.active_pid != 0) |
| 297 |
values[i++] = Int32GetDatum(slot_contents.active_pid); |
| 298 |
else |
| 299 |
nulls[i++] = true; |
| 300 |
|
| 301 |
if (slot_contents.data.xmin != InvalidTransactionId) |
| 302 |
values[i++] = TransactionIdGetDatum(slot_contents.data.xmin); |
| 303 |
else |
| 304 |
nulls[i++] = true; |
| 305 |
|
| 306 |
if (slot_contents.data.catalog_xmin != InvalidTransactionId) |
| 307 |
values[i++] = TransactionIdGetDatum(slot_contents.data.catalog_xmin); |
| 308 |
else |
| 309 |
nulls[i++] = true; |
| 310 |
|
| 311 |
if (slot_contents.data.restart_lsn != InvalidXLogRecPtr) |
| 312 |
values[i++] = LSNGetDatum(slot_contents.data.restart_lsn); |
| 313 |
else |
| 314 |
nulls[i++] = true; |
| 315 |
|
| 316 |
if (slot_contents.data.confirmed_flush != InvalidXLogRecPtr) |
| 317 |
values[i++] = LSNGetDatum(slot_contents.data.confirmed_flush); |
| 318 |
else |
| 319 |
nulls[i++] = true; |
| 320 |
|
| 321 |
/* |
| 322 |
* If the slot has not been invalidated, test availability from |
| 323 |
* restart_lsn. |
| 324 |
*/ |
| 325 |
if (slot_contents.data.invalidated != RS_INVAL_NONE) |
| 326 |
walstate = WALAVAIL_REMOVED; |
| 327 |
else |
| 328 |
walstate = GetWALAvailability(slot_contents.data.restart_lsn); |
| 329 |
|
| 330 |
switch (walstate) |
| 331 |
{ |
| 332 |
case WALAVAIL_INVALID_LSN: |
| 333 |
nulls[i++] = true; |
| 334 |
break; |
| 335 |
|
| 336 |
case WALAVAIL_RESERVED: |
| 337 |
values[i++] = CStringGetTextDatum("reserved"); |
| 338 |
break; |
| 339 |
|
| 340 |
case WALAVAIL_EXTENDED: |
| 341 |
values[i++] = CStringGetTextDatum("extended"); |
| 342 |
break; |
| 343 |
|
| 344 |
case WALAVAIL_UNRESERVED: |
| 345 |
values[i++] = CStringGetTextDatum("unreserved"); |
| 346 |
break; |
| 347 |
|
| 348 |
case WALAVAIL_REMOVED: |
| 349 |
|
| 350 |
/* |
| 351 |
* If we read the restart_lsn long enough ago, maybe that file |
| 352 |
* has been removed by now. However, the walsender could have |
| 353 |
* moved forward enough that it jumped to another file after |
| 354 |
* we looked. If checkpointer signalled the process to |
| 355 |
* termination, then it's definitely lost; but if a process is |
| 356 |
* still alive, then "unreserved" seems more appropriate. |
| 357 |
* |
| 358 |
* If we do change it, save the state for safe_wal_size below. |
| 359 |
*/ |
| 360 |
if (!XLogRecPtrIsInvalid(slot_contents.data.restart_lsn)) |
| 361 |
{ |
| 362 |
int pid; |
| 363 |
|
| 364 |
SpinLockAcquire(&slot->mutex); |
| 365 |
pid = slot->active_pid; |
| 366 |
slot_contents.data.restart_lsn = slot->data.restart_lsn; |
| 367 |
SpinLockRelease(&slot->mutex); |
| 368 |
if (pid != 0) |
| 369 |
{ |
| 370 |
values[i++] = CStringGetTextDatum("unreserved"); |
| 371 |
walstate = WALAVAIL_UNRESERVED; |
| 372 |
break; |
| 373 |
} |
| 374 |
} |
| 375 |
values[i++] = CStringGetTextDatum("lost"); |
| 376 |
break; |
| 377 |
} |
| 378 |
|
| 379 |
/* |
| 380 |
* safe_wal_size is only computed for slots that have not been lost, |
| 381 |
* and only if there's a configured maximum size. |
| 382 |
*/ |
| 383 |
if (walstate == WALAVAIL_REMOVED || max_slot_wal_keep_size_mb < 0) |
| 384 |
nulls[i++] = true; |
| 385 |
else |
| 386 |
{ |
| 387 |
XLogSegNo targetSeg; |
| 388 |
uint64 slotKeepSegs; |
| 389 |
uint64 keepSegs; |
| 390 |
XLogSegNo failSeg; |
| 391 |
XLogRecPtr failLSN; |
| 392 |
|
| 393 |
XLByteToSeg(slot_contents.data.restart_lsn, targetSeg, wal_segment_size); |
| 394 |
|
| 395 |
/* determine how many segments can be kept by slots */ |
| 396 |
slotKeepSegs = XLogMBVarToSegs(max_slot_wal_keep_size_mb, wal_segment_size); |
| 397 |
/* ditto for wal_keep_size */ |
| 398 |
keepSegs = XLogMBVarToSegs(wal_keep_size_mb, wal_segment_size); |
| 399 |
|
| 400 |
/* if currpos reaches failLSN, we lose our segment */ |
| 401 |
failSeg = targetSeg + Max(slotKeepSegs, keepSegs) + 1; |
| 402 |
XLogSegNoOffsetToRecPtr(failSeg, 0, wal_segment_size, failLSN); |
| 403 |
|
| 404 |
values[i++] = Int64GetDatum(failLSN - currlsn); |
| 405 |
} |
| 406 |
|
| 407 |
values[i++] = BoolGetDatum(slot_contents.data.two_phase); |
| 408 |
|
| 409 |
if (slot_contents.data.two_phase && |
| 410 |
!XLogRecPtrIsInvalid(slot_contents.data.two_phase_at)) |
| 411 |
values[i++] = LSNGetDatum(slot_contents.data.two_phase_at); |
| 412 |
else |
| 413 |
nulls[i++] = true; |
| 414 |
|
| 415 |
if (slot_contents.inactive_since > 0) |
| 416 |
values[i++] = TimestampTzGetDatum(slot_contents.inactive_since); |
| 417 |
else |
| 418 |
nulls[i++] = true; |
| 419 |
|
| 420 |
cause = slot_contents.data.invalidated; |
| 421 |
|
| 422 |
if (SlotIsPhysical(&slot_contents)) |
| 423 |
nulls[i++] = true; |
| 424 |
else |
| 425 |
{ |
| 426 |
/* |
| 427 |
* rows_removed and wal_level_insufficient are the only two |
| 428 |
* reasons for the logical slot's conflict with recovery. |
| 429 |
*/ |
| 430 |
if (cause == RS_INVAL_HORIZON || |
| 431 |
cause == RS_INVAL_WAL_LEVEL) |
| 432 |
values[i++] = BoolGetDatum(true); |
| 433 |
else |
| 434 |
values[i++] = BoolGetDatum(false); |
| 435 |
} |
| 436 |
|
| 437 |
if (cause == RS_INVAL_NONE) |
| 438 |
nulls[i++] = true; |
| 439 |
else |
| 440 |
values[i++] = CStringGetTextDatum(GetSlotInvalidationCauseName(cause)); |
| 441 |
|
| 442 |
values[i++] = BoolGetDatum(slot_contents.data.failover); |
| 443 |
|
| 444 |
values[i++] = BoolGetDatum(slot_contents.data.synced); |
| 445 |
|
| 446 |
Assert(i == PG_GET_REPLICATION_SLOTS_COLS); |
| 447 |
|
| 448 |
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, |
| 449 |
values, nulls); |
| 450 |
} |
| 451 |
|
| 452 |
LWLockRelease(ReplicationSlotControlLock); |
| 453 |
|
| 454 |
return (Datum) 0; |
| 455 |
} |
| 456 |
|
| 457 |
/* |
| 458 |
* Helper function for advancing our physical replication slot forward. |
| 459 |
* |
| 460 |
* The LSN position to move to is compared simply to the slot's restart_lsn, |
| 461 |
* knowing that any position older than that would be removed by successive |
| 462 |
* checkpoints. |
| 463 |
*/ |
| 464 |
static XLogRecPtr |
| 465 |
pg_physical_replication_slot_advance(XLogRecPtr moveto) |
| 466 |
{ |
| 467 |
XLogRecPtr startlsn = MyReplicationSlot->data.restart_lsn; |
| 468 |
XLogRecPtr retlsn = startlsn; |
| 469 |
|
| 470 |
Assert(moveto != InvalidXLogRecPtr); |
| 471 |
|
| 472 |
if (startlsn < moveto) |
| 473 |
{ |
| 474 |
SpinLockAcquire(&MyReplicationSlot->mutex); |
| 475 |
MyReplicationSlot->data.restart_lsn = moveto; |
| 476 |
SpinLockRelease(&MyReplicationSlot->mutex); |
| 477 |
retlsn = moveto; |
| 478 |
|
| 479 |
/* |
| 480 |
* Dirty the slot so as it is written out at the next checkpoint. Note |
| 481 |
* that the LSN position advanced may still be lost in the event of a |
| 482 |
* crash, but this makes the data consistent after a clean shutdown. |
| 483 |
*/ |
| 484 |
ReplicationSlotMarkDirty(); |
| 485 |
|
| 486 |
/* |
| 487 |
* Wake up logical walsenders holding logical failover slots after |
| 488 |
* updating the restart_lsn of the physical slot. |
| 489 |
*/ |
| 490 |
PhysicalWakeupLogicalWalSnd(); |
| 491 |
} |
| 492 |
|
| 493 |
return retlsn; |
| 494 |
} |
| 495 |
|
| 496 |
/* |
| 497 |
* Advance our logical replication slot forward. See |
| 498 |
* LogicalSlotAdvanceAndCheckSnapState for details. |
| 499 |
*/ |
| 500 |
static XLogRecPtr |
| 501 |
pg_logical_replication_slot_advance(XLogRecPtr moveto) |
| 502 |
{ |
| 503 |
return LogicalSlotAdvanceAndCheckSnapState(moveto, NULL); |
| 504 |
} |
| 505 |
|
| 506 |
/* |
| 507 |
* SQL function for moving the position in a replication slot. |
| 508 |
*/ |
| 509 |
Datum |
| 510 |
pg_replication_slot_advance(PG_FUNCTION_ARGS) |
| 511 |
{ |
| 512 |
Name slotname = PG_GETARG_NAME(0); |
| 513 |
XLogRecPtr moveto = PG_GETARG_LSN(1); |
| 514 |
XLogRecPtr endlsn; |
| 515 |
XLogRecPtr minlsn; |
| 516 |
TupleDesc tupdesc; |
| 517 |
Datum values[2]; |
| 518 |
bool nulls[2]; |
| 519 |
HeapTuple tuple; |
| 520 |
Datum result; |
| 521 |
|
| 522 |
Assert(!MyReplicationSlot); |
| 523 |
|
| 524 |
CheckSlotPermissions(); |
| 525 |
|
| 526 |
if (XLogRecPtrIsInvalid(moveto)) |
| 527 |
ereport(ERROR, |
| 528 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 529 |
errmsg("invalid target WAL LSN"))); |
| 530 |
|
| 531 |
/* Build a tuple descriptor for our result type */ |
| 532 |
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) |
| 533 |
elog(ERROR, "return type must be a row type"); |
| 534 |
|
| 535 |
/* |
| 536 |
* We can't move slot past what's been flushed/replayed so clamp the |
| 537 |
* target position accordingly. |
| 538 |
*/ |
| 539 |
if (!RecoveryInProgress()) |
| 540 |
moveto = Min(moveto, GetFlushRecPtr(NULL)); |
| 541 |
else |
| 542 |
moveto = Min(moveto, GetXLogReplayRecPtr(NULL)); |
| 543 |
|
| 544 |
/* Acquire the slot so we "own" it */ |
| 545 |
ReplicationSlotAcquire(NameStr(*slotname), true, true); |
| 546 |
|
| 547 |
/* A slot whose restart_lsn has never been reserved cannot be advanced */ |
| 548 |
if (XLogRecPtrIsInvalid(MyReplicationSlot->data.restart_lsn)) |
| 549 |
ereport(ERROR, |
| 550 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 551 |
errmsg("replication slot \"%s\" cannot be advanced", |
| 552 |
NameStr(*slotname)), |
| 553 |
errdetail("This slot has never previously reserved WAL, or it has been invalidated."))); |
| 554 |
|
| 555 |
/* |
| 556 |
* Check if the slot is not moving backwards. Physical slots rely simply |
| 557 |
* on restart_lsn as a minimum point, while logical slots have confirmed |
| 558 |
* consumption up to confirmed_flush, meaning that in both cases data |
| 559 |
* older than that is not available anymore. |
| 560 |
*/ |
| 561 |
if (OidIsValid(MyReplicationSlot->data.database)) |
| 562 |
minlsn = MyReplicationSlot->data.confirmed_flush; |
| 563 |
else |
| 564 |
minlsn = MyReplicationSlot->data.restart_lsn; |
| 565 |
|
| 566 |
if (moveto < minlsn) |
| 567 |
ereport(ERROR, |
| 568 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 569 |
errmsg("cannot advance replication slot to %X/%X, minimum is %X/%X", |
| 570 |
LSN_FORMAT_ARGS(moveto), LSN_FORMAT_ARGS(minlsn)))); |
| 571 |
|
| 572 |
/* Do the actual slot update, depending on the slot type */ |
| 573 |
if (OidIsValid(MyReplicationSlot->data.database)) |
| 574 |
endlsn = pg_logical_replication_slot_advance(moveto); |
| 575 |
else |
| 576 |
endlsn = pg_physical_replication_slot_advance(moveto); |
| 577 |
|
| 578 |
values[0] = NameGetDatum(&MyReplicationSlot->data.name); |
| 579 |
nulls[0] = false; |
| 580 |
|
| 581 |
/* |
| 582 |
* Recompute the minimum LSN and xmin across all slots to adjust with the |
| 583 |
* advancing potentially done. |
| 584 |
*/ |
| 585 |
ReplicationSlotsComputeRequiredXmin(false); |
| 586 |
ReplicationSlotsComputeRequiredLSN(); |
| 587 |
|
| 588 |
ReplicationSlotRelease(); |
| 589 |
|
| 590 |
/* Return the reached position. */ |
| 591 |
values[1] = LSNGetDatum(endlsn); |
| 592 |
nulls[1] = false; |
| 593 |
|
| 594 |
tuple = heap_form_tuple(tupdesc, values, nulls); |
| 595 |
result = HeapTupleGetDatum(tuple); |
| 596 |
|
| 597 |
PG_RETURN_DATUM(result); |
| 598 |
} |
| 599 |
|
| 600 |
/* |
| 601 |
* Helper function of copying a replication slot. |
| 602 |
*/ |
| 603 |
static Datum |
| 604 |
copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) |
| 605 |
{ |
| 606 |
Name src_name = PG_GETARG_NAME(0); |
| 607 |
Name dst_name = PG_GETARG_NAME(1); |
| 608 |
ReplicationSlot *src = NULL; |
| 609 |
ReplicationSlot first_slot_contents; |
| 610 |
ReplicationSlot second_slot_contents; |
| 611 |
XLogRecPtr src_restart_lsn; |
| 612 |
bool src_islogical; |
| 613 |
bool temporary; |
| 614 |
char *plugin; |
| 615 |
Datum values[2]; |
| 616 |
bool nulls[2]; |
| 617 |
Datum result; |
| 618 |
TupleDesc tupdesc; |
| 619 |
HeapTuple tuple; |
| 620 |
|
| 621 |
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) |
| 622 |
elog(ERROR, "return type must be a row type"); |
| 623 |
|
| 624 |
CheckSlotPermissions(); |
| 625 |
|
| 626 |
if (logical_slot) |
| 627 |
CheckLogicalDecodingRequirements(); |
| 628 |
else |
| 629 |
CheckSlotRequirements(); |
| 630 |
|
| 631 |
LWLockAcquire(ReplicationSlotControlLock, LW_SHARED); |
| 632 |
|
| 633 |
/* |
| 634 |
* We need to prevent the source slot's reserved WAL from being removed, |
| 635 |
* but we don't want to lock that slot for very long, and it can advance |
| 636 |
* in the meantime. So obtain the source slot's data, and create a new |
| 637 |
* slot using its restart_lsn. Afterwards we lock the source slot again |
| 638 |
* and verify that the data we copied (name, type) has not changed |
| 639 |
* incompatibly. No inconvenient WAL removal can occur once the new slot |
| 640 |
* is created -- but since WAL removal could have occurred before we |
| 641 |
* managed to create the new slot, we advance the new slot's restart_lsn |
| 642 |
* to the source slot's updated restart_lsn the second time we lock it. |
| 643 |
*/ |
| 644 |
for (int i = 0; i < max_replication_slots; i++) |
| 645 |
{ |
| 646 |
ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i]; |
| 647 |
|
| 648 |
if (s->in_use && strcmp(NameStr(s->data.name), NameStr(*src_name)) == 0) |
| 649 |
{ |
| 650 |
/* Copy the slot contents while holding spinlock */ |
| 651 |
SpinLockAcquire(&s->mutex); |
| 652 |
first_slot_contents = *s; |
| 653 |
SpinLockRelease(&s->mutex); |
| 654 |
src = s; |
| 655 |
break; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
LWLockRelease(ReplicationSlotControlLock); |
| 660 |
|
| 661 |
if (src == NULL) |
| 662 |
ereport(ERROR, |
| 663 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 664 |
errmsg("replication slot \"%s\" does not exist", NameStr(*src_name)))); |
| 665 |
|
| 666 |
src_islogical = SlotIsLogical(&first_slot_contents); |
| 667 |
src_restart_lsn = first_slot_contents.data.restart_lsn; |
| 668 |
temporary = (first_slot_contents.data.persistency == RS_TEMPORARY); |
| 669 |
plugin = logical_slot ? NameStr(first_slot_contents.data.plugin) : NULL; |
| 670 |
|
| 671 |
/* Check type of replication slot */ |
| 672 |
if (src_islogical != logical_slot) |
| 673 |
ereport(ERROR, |
| 674 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 675 |
src_islogical ? |
| 676 |
errmsg("cannot copy physical replication slot \"%s\" as a logical replication slot", |
| 677 |
NameStr(*src_name)) : |
| 678 |
errmsg("cannot copy logical replication slot \"%s\" as a physical replication slot", |
| 679 |
NameStr(*src_name)))); |
| 680 |
|
| 681 |
/* Copying non-reserved slot doesn't make sense */ |
| 682 |
if (XLogRecPtrIsInvalid(src_restart_lsn)) |
| 683 |
ereport(ERROR, |
| 684 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 685 |
errmsg("cannot copy a replication slot that doesn't reserve WAL"))); |
| 686 |
|
| 687 |
/* Cannot copy an invalidated replication slot */ |
| 688 |
if (first_slot_contents.data.invalidated != RS_INVAL_NONE) |
| 689 |
ereport(ERROR, |
| 690 |
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 691 |
errmsg("cannot copy invalidated replication slot \"%s\"", |
| 692 |
NameStr(*src_name))); |
| 693 |
|
| 694 |
/* Overwrite params from optional arguments */ |
| 695 |
if (PG_NARGS() >= 3) |
| 696 |
temporary = PG_GETARG_BOOL(2); |
| 697 |
if (PG_NARGS() >= 4) |
| 698 |
{ |
| 699 |
Assert(logical_slot); |
| 700 |
plugin = NameStr(*(PG_GETARG_NAME(3))); |
| 701 |
} |
| 702 |
|
| 703 |
/* Create new slot and acquire it */ |
| 704 |
if (logical_slot) |
| 705 |
{ |
| 706 |
/* |
| 707 |
* We must not try to read WAL, since we haven't reserved it yet -- |
| 708 |
* hence pass find_startpoint false. confirmed_flush will be set |
| 709 |
* below, by copying from the source slot. |
| 710 |
* |
| 711 |
* We don't copy the failover option to prevent potential issues with |
| 712 |
* slot synchronization. For instance, if a slot was synchronized to |
| 713 |
* the standby, then dropped on the primary, and immediately recreated |
| 714 |
* by copying from another existing slot with much earlier restart_lsn |
| 715 |
* and confirmed_flush_lsn, the slot synchronization would only |
| 716 |
* observe the LSN of the same slot moving backward. As slot |
| 717 |
* synchronization does not copy the restart_lsn and |
| 718 |
* confirmed_flush_lsn backward (see update_local_synced_slot() for |
| 719 |
* details), if a failover happens before the primary's slot catches |
| 720 |
* up, logical replication cannot continue using the synchronized slot |
| 721 |
* on the promoted standby because the slot retains the restart_lsn |
| 722 |
* and confirmed_flush_lsn that are much later than expected. |
| 723 |
*/ |
| 724 |
create_logical_replication_slot(NameStr(*dst_name), |
| 725 |
plugin, |
| 726 |
temporary, |
| 727 |
false, |
| 728 |
false, |
| 729 |
src_restart_lsn, |
| 730 |
false); |
| 731 |
} |
| 732 |
else |
| 733 |
create_physical_replication_slot(NameStr(*dst_name), |
| 734 |
true, |
| 735 |
temporary, |
| 736 |
src_restart_lsn); |
| 737 |
|
| 738 |
/* |
| 739 |
* Update the destination slot to current values of the source slot; |
| 740 |
* recheck that the source slot is still the one we saw previously. |
| 741 |
*/ |
| 742 |
{ |
| 743 |
TransactionId copy_effective_xmin; |
| 744 |
TransactionId copy_effective_catalog_xmin; |
| 745 |
TransactionId copy_xmin; |
| 746 |
TransactionId copy_catalog_xmin; |
| 747 |
XLogRecPtr copy_restart_lsn; |
| 748 |
XLogRecPtr copy_confirmed_flush; |
| 749 |
bool copy_islogical; |
| 750 |
char *copy_name; |
| 751 |
|
| 752 |
/* Copy data of source slot again */ |
| 753 |
SpinLockAcquire(&src->mutex); |
| 754 |
second_slot_contents = *src; |
| 755 |
SpinLockRelease(&src->mutex); |
| 756 |
|
| 757 |
copy_effective_xmin = second_slot_contents.effective_xmin; |
| 758 |
copy_effective_catalog_xmin = second_slot_contents.effective_catalog_xmin; |
| 759 |
|
| 760 |
copy_xmin = second_slot_contents.data.xmin; |
| 761 |
copy_catalog_xmin = second_slot_contents.data.catalog_xmin; |
| 762 |
copy_restart_lsn = second_slot_contents.data.restart_lsn; |
| 763 |
copy_confirmed_flush = second_slot_contents.data.confirmed_flush; |
| 764 |
|
| 765 |
/* for existence check */ |
| 766 |
copy_name = NameStr(second_slot_contents.data.name); |
| 767 |
copy_islogical = SlotIsLogical(&second_slot_contents); |
| 768 |
|
| 769 |
/* |
| 770 |
* Check if the source slot still exists and is valid. We regard it as |
| 771 |
* invalid if the type of replication slot or name has been changed, |
| 772 |
* or the restart_lsn either is invalid or has gone backward. (The |
| 773 |
* restart_lsn could go backwards if the source slot is dropped and |
| 774 |
* copied from an older slot during installation.) |
| 775 |
* |
| 776 |
* Since erroring out will release and drop the destination slot we |
| 777 |
* don't need to release it here. |
| 778 |
*/ |
| 779 |
if (copy_restart_lsn < src_restart_lsn || |
| 780 |
src_islogical != copy_islogical || |
| 781 |
strcmp(copy_name, NameStr(*src_name)) != 0) |
| 782 |
ereport(ERROR, |
| 783 |
(errmsg("could not copy replication slot \"%s\"", |
| 784 |
NameStr(*src_name)), |
| 785 |
errdetail("The source replication slot was modified incompatibly during the copy operation."))); |
| 786 |
|
| 787 |
/* The source slot must have a consistent snapshot */ |
| 788 |
if (src_islogical && XLogRecPtrIsInvalid(copy_confirmed_flush)) |
| 789 |
ereport(ERROR, |
| 790 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 791 |
errmsg("cannot copy unfinished logical replication slot \"%s\"", |
| 792 |
NameStr(*src_name)), |
| 793 |
errhint("Retry when the source replication slot's confirmed_flush_lsn is valid."))); |
| 794 |
|
| 795 |
/* |
| 796 |
* Copying an invalid slot doesn't make sense. Note that the source |
| 797 |
* slot can become invalid after we create the new slot and copy the |
| 798 |
* data of source slot. This is possible because the operations in |
| 799 |
* InvalidateObsoleteReplicationSlots() are not serialized with this |
| 800 |
* function. Even though we can't detect such a case here, the copied |
| 801 |
* slot will become invalid in the next checkpoint cycle. |
| 802 |
*/ |
| 803 |
if (second_slot_contents.data.invalidated != RS_INVAL_NONE) |
| 804 |
ereport(ERROR, |
| 805 |
errmsg("cannot copy replication slot \"%s\"", |
| 806 |
NameStr(*src_name)), |
| 807 |
errdetail("The source replication slot was invalidated during the copy operation.")); |
| 808 |
|
| 809 |
/* Install copied values again */ |
| 810 |
SpinLockAcquire(&MyReplicationSlot->mutex); |
| 811 |
MyReplicationSlot->effective_xmin = copy_effective_xmin; |
| 812 |
MyReplicationSlot->effective_catalog_xmin = copy_effective_catalog_xmin; |
| 813 |
|
| 814 |
MyReplicationSlot->data.xmin = copy_xmin; |
| 815 |
MyReplicationSlot->data.catalog_xmin = copy_catalog_xmin; |
| 816 |
MyReplicationSlot->data.restart_lsn = copy_restart_lsn; |
| 817 |
MyReplicationSlot->data.confirmed_flush = copy_confirmed_flush; |
| 818 |
SpinLockRelease(&MyReplicationSlot->mutex); |
| 819 |
|
| 820 |
ReplicationSlotMarkDirty(); |
| 821 |
ReplicationSlotsComputeRequiredXmin(false); |
| 822 |
ReplicationSlotsComputeRequiredLSN(); |
| 823 |
ReplicationSlotSave(); |
| 824 |
|
| 825 |
#ifdef USE_ASSERT_CHECKING |
| 826 |
/* Check that the restart_lsn is available */ |
| 827 |
{ |
| 828 |
XLogSegNo segno; |
| 829 |
|
| 830 |
XLByteToSeg(copy_restart_lsn, segno, wal_segment_size); |
| 831 |
Assert(XLogGetLastRemovedSegno() < segno); |
| 832 |
} |
| 833 |
#endif |
| 834 |
} |
| 835 |
|
| 836 |
/* target slot fully created, mark as persistent if needed */ |
| 837 |
if (logical_slot && !temporary) |
| 838 |
ReplicationSlotPersist(); |
| 839 |
|
| 840 |
/* All done. Set up the return values */ |
| 841 |
values[0] = NameGetDatum(dst_name); |
| 842 |
nulls[0] = false; |
| 843 |
if (!XLogRecPtrIsInvalid(MyReplicationSlot->data.confirmed_flush)) |
| 844 |
{ |
| 845 |
values[1] = LSNGetDatum(MyReplicationSlot->data.confirmed_flush); |
| 846 |
nulls[1] = false; |
| 847 |
} |
| 848 |
else |
| 849 |
nulls[1] = true; |
| 850 |
|
| 851 |
tuple = heap_form_tuple(tupdesc, values, nulls); |
| 852 |
result = HeapTupleGetDatum(tuple); |
| 853 |
|
| 854 |
ReplicationSlotRelease(); |
| 855 |
|
| 856 |
PG_RETURN_DATUM(result); |
| 857 |
} |
| 858 |
|
| 859 |
/* The wrappers below are all to appease opr_sanity */ |
| 860 |
Datum |
| 861 |
pg_copy_logical_replication_slot_a(PG_FUNCTION_ARGS) |
| 862 |
{ |
| 863 |
return copy_replication_slot(fcinfo, true); |
| 864 |
} |
| 865 |
|
| 866 |
Datum |
| 867 |
pg_copy_logical_replication_slot_b(PG_FUNCTION_ARGS) |
| 868 |
{ |
| 869 |
return copy_replication_slot(fcinfo, true); |
| 870 |
} |
| 871 |
|
| 872 |
Datum |
| 873 |
pg_copy_logical_replication_slot_c(PG_FUNCTION_ARGS) |
| 874 |
{ |
| 875 |
return copy_replication_slot(fcinfo, true); |
| 876 |
} |
| 877 |
|
| 878 |
Datum |
| 879 |
pg_copy_physical_replication_slot_a(PG_FUNCTION_ARGS) |
| 880 |
{ |
| 881 |
return copy_replication_slot(fcinfo, false); |
| 882 |
} |
| 883 |
|
| 884 |
Datum |
| 885 |
pg_copy_physical_replication_slot_b(PG_FUNCTION_ARGS) |
| 886 |
{ |
| 887 |
return copy_replication_slot(fcinfo, false); |
| 888 |
} |
| 889 |
|
| 890 |
/* |
| 891 |
* Synchronize failover enabled replication slots to a standby server |
| 892 |
* from the primary server. |
| 893 |
*/ |
| 894 |
Datum |
| 895 |
pg_sync_replication_slots(PG_FUNCTION_ARGS) |
| 896 |
{ |
| 897 |
WalReceiverConn *wrconn; |
| 898 |
char *err; |
| 899 |
StringInfoData app_name; |
| 900 |
|
| 901 |
CheckSlotPermissions(); |
| 902 |
|
| 903 |
if (!RecoveryInProgress()) |
| 904 |
ereport(ERROR, |
| 905 |
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 906 |
errmsg("replication slots can only be synchronized to a standby server")); |
| 907 |
|
| 908 |
ValidateSlotSyncParams(ERROR); |
| 909 |
|
| 910 |
/* Load the libpq-specific functions */ |
| 911 |
load_file("libpqwalreceiver", false); |
| 912 |
|
| 913 |
(void) CheckAndGetDbnameFromConninfo(); |
| 914 |
|
| 915 |
initStringInfo(&app_name); |
| 916 |
if (cluster_name[0]) |
| 917 |
appendStringInfo(&app_name, "%s_slotsync", cluster_name); |
| 918 |
else |
| 919 |
appendStringInfoString(&app_name, "slotsync"); |
| 920 |
|
| 921 |
/* Connect to the primary server. */ |
| 922 |
wrconn = walrcv_connect(PrimaryConnInfo, false, false, false, |
| 923 |
app_name.data, &err); |
| 924 |
|
| 925 |
if (!wrconn) |
| 926 |
ereport(ERROR, |
| 927 |
errcode(ERRCODE_CONNECTION_FAILURE), |
| 928 |
errmsg("synchronization worker \"%s\" could not connect to the primary server: %s", |
| 929 |
app_name.data, err)); |
| 930 |
|
| 931 |
pfree(app_name.data); |
| 932 |
|
| 933 |
SyncReplicationSlots(wrconn); |
| 934 |
|
| 935 |
walrcv_disconnect(wrconn); |
| 936 |
|
| 937 |
PG_RETURN_VOID(); |
| 938 |
} |