PostgreSQL Source Code  git master
slotfuncs.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/xlog_internal.h"
#include "access/xlogutils.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "replication/decode.h"
#include "replication/logical.h"
#include "replication/slot.h"
#include "utils/builtins.h"
#include "utils/inval.h"
#include "utils/pg_lsn.h"
#include "utils/resowner.h"
Include dependency graph for slotfuncs.c:

Go to the source code of this file.

Macros

#define PG_GET_REPLICATION_SLOTS_COLS   14
 

Functions

static void check_permissions (void)
 
static void create_physical_replication_slot (char *name, bool immediately_reserve, bool temporary, XLogRecPtr restart_lsn)
 
Datum pg_create_physical_replication_slot (PG_FUNCTION_ARGS)
 
static void create_logical_replication_slot (char *name, char *plugin, bool temporary, bool two_phase, XLogRecPtr restart_lsn, bool find_startpoint)
 
Datum pg_create_logical_replication_slot (PG_FUNCTION_ARGS)
 
Datum pg_drop_replication_slot (PG_FUNCTION_ARGS)
 
Datum pg_get_replication_slots (PG_FUNCTION_ARGS)
 
static XLogRecPtr pg_physical_replication_slot_advance (XLogRecPtr moveto)
 
static XLogRecPtr pg_logical_replication_slot_advance (XLogRecPtr moveto)
 
Datum pg_replication_slot_advance (PG_FUNCTION_ARGS)
 
static Datum copy_replication_slot (FunctionCallInfo fcinfo, bool logical_slot)
 
Datum pg_copy_logical_replication_slot_a (PG_FUNCTION_ARGS)
 
Datum pg_copy_logical_replication_slot_b (PG_FUNCTION_ARGS)
 
Datum pg_copy_logical_replication_slot_c (PG_FUNCTION_ARGS)
 
Datum pg_copy_physical_replication_slot_a (PG_FUNCTION_ARGS)
 
Datum pg_copy_physical_replication_slot_b (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ PG_GET_REPLICATION_SLOTS_COLS

#define PG_GET_REPLICATION_SLOTS_COLS   14

Function Documentation

◆ check_permissions()

static void check_permissions ( void  )
static

Definition at line 29 of file slotfuncs.c.

References ereport, errcode(), errmsg(), ERROR, GetUserId(), has_rolreplication(), and superuser().

Referenced by copy_replication_slot(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_drop_replication_slot(), and pg_replication_slot_advance().

30 {
32  ereport(ERROR,
33  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
34  errmsg("must be superuser or replication role to use replication slots")));
35 }
Oid GetUserId(void)
Definition: miscinit.c:478
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
bool has_rolreplication(Oid roleid)
Definition: miscinit.c:659
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ copy_replication_slot()

static Datum copy_replication_slot ( FunctionCallInfo  fcinfo,
bool  logical_slot 
)
static

Definition at line 701 of file slotfuncs.c.

References Assert, ReplicationSlotPersistentData::catalog_xmin, check_permissions(), CheckLogicalDecodingRequirements(), CheckSlotRequirements(), ReplicationSlotPersistentData::confirmed_flush, create_logical_replication_slot(), create_physical_replication_slot(), ReplicationSlot::data, ReplicationSlot::effective_catalog_xmin, ReplicationSlot::effective_xmin, elog, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum, i, ReplicationSlot::in_use, LSNGetDatum, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlot::mutex, MyReplicationSlot, ReplicationSlotPersistentData::name, NameGetDatum, NameStr, ReplicationSlotPersistentData::persistency, PG_GETARG_BOOL, PG_GETARG_NAME, PG_NARGS, PG_RETURN_DATUM, plugin, ReplicationSlotPersistentData::plugin, ReplicationSlotCtlData::replication_slots, ReplicationSlotCtl, ReplicationSlotMarkDirty(), ReplicationSlotPersist(), ReplicationSlotRelease(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotPersistentData::restart_lsn, RS_TEMPORARY, SlotIsLogical, SpinLockAcquire, SpinLockRelease, TYPEFUNC_COMPOSITE, values, wal_segment_size, XLByteToSeg, XLogGetLastRemovedSegno(), XLogRecPtrIsInvalid, and ReplicationSlotPersistentData::xmin.

Referenced by pg_copy_logical_replication_slot_a(), pg_copy_logical_replication_slot_b(), pg_copy_logical_replication_slot_c(), pg_copy_physical_replication_slot_a(), and pg_copy_physical_replication_slot_b().

702 {
703  Name src_name = PG_GETARG_NAME(0);
704  Name dst_name = PG_GETARG_NAME(1);
705  ReplicationSlot *src = NULL;
706  ReplicationSlot first_slot_contents;
707  ReplicationSlot second_slot_contents;
708  XLogRecPtr src_restart_lsn;
709  bool src_islogical;
710  bool temporary;
711  char *plugin;
712  Datum values[2];
713  bool nulls[2];
714  Datum result;
715  TupleDesc tupdesc;
716  HeapTuple tuple;
717 
718  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
719  elog(ERROR, "return type must be a row type");
720 
722 
723  if (logical_slot)
725  else
727 
728  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
729 
730  /*
731  * We need to prevent the source slot's reserved WAL from being removed,
732  * but we don't want to lock that slot for very long, and it can advance
733  * in the meantime. So obtain the source slot's data, and create a new
734  * slot using its restart_lsn. Afterwards we lock the source slot again
735  * and verify that the data we copied (name, type) has not changed
736  * incompatibly. No inconvenient WAL removal can occur once the new slot
737  * is created -- but since WAL removal could have occurred before we
738  * managed to create the new slot, we advance the new slot's restart_lsn
739  * to the source slot's updated restart_lsn the second time we lock it.
740  */
741  for (int i = 0; i < max_replication_slots; i++)
742  {
744 
745  if (s->in_use && strcmp(NameStr(s->data.name), NameStr(*src_name)) == 0)
746  {
747  /* Copy the slot contents while holding spinlock */
748  SpinLockAcquire(&s->mutex);
749  first_slot_contents = *s;
750  SpinLockRelease(&s->mutex);
751  src = s;
752  break;
753  }
754  }
755 
756  LWLockRelease(ReplicationSlotControlLock);
757 
758  if (src == NULL)
759  ereport(ERROR,
760  (errcode(ERRCODE_UNDEFINED_OBJECT),
761  errmsg("replication slot \"%s\" does not exist", NameStr(*src_name))));
762 
763  src_islogical = SlotIsLogical(&first_slot_contents);
764  src_restart_lsn = first_slot_contents.data.restart_lsn;
765  temporary = (first_slot_contents.data.persistency == RS_TEMPORARY);
766  plugin = logical_slot ? NameStr(first_slot_contents.data.plugin) : NULL;
767 
768  /* Check type of replication slot */
769  if (src_islogical != logical_slot)
770  ereport(ERROR,
771  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
772  src_islogical ?
773  errmsg("cannot copy physical replication slot \"%s\" as a logical replication slot",
774  NameStr(*src_name)) :
775  errmsg("cannot copy logical replication slot \"%s\" as a physical replication slot",
776  NameStr(*src_name))));
777 
778  /* Copying non-reserved slot doesn't make sense */
779  if (XLogRecPtrIsInvalid(src_restart_lsn))
780  ereport(ERROR,
781  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
782  errmsg("cannot copy a replication slot that doesn't reserve WAL")));
783 
784  /* Overwrite params from optional arguments */
785  if (PG_NARGS() >= 3)
786  temporary = PG_GETARG_BOOL(2);
787  if (PG_NARGS() >= 4)
788  {
789  Assert(logical_slot);
790  plugin = NameStr(*(PG_GETARG_NAME(3)));
791  }
792 
793  /* Create new slot and acquire it */
794  if (logical_slot)
795  {
796  /*
797  * We must not try to read WAL, since we haven't reserved it yet --
798  * hence pass find_startpoint false. confirmed_flush will be set
799  * below, by copying from the source slot.
800  */
802  plugin,
803  temporary,
804  false,
805  src_restart_lsn,
806  false);
807  }
808  else
810  true,
811  temporary,
812  src_restart_lsn);
813 
814  /*
815  * Update the destination slot to current values of the source slot;
816  * recheck that the source slot is still the one we saw previously.
817  */
818  {
819  TransactionId copy_effective_xmin;
820  TransactionId copy_effective_catalog_xmin;
821  TransactionId copy_xmin;
822  TransactionId copy_catalog_xmin;
823  XLogRecPtr copy_restart_lsn;
824  XLogRecPtr copy_confirmed_flush;
825  bool copy_islogical;
826  char *copy_name;
827 
828  /* Copy data of source slot again */
829  SpinLockAcquire(&src->mutex);
830  second_slot_contents = *src;
831  SpinLockRelease(&src->mutex);
832 
833  copy_effective_xmin = second_slot_contents.effective_xmin;
834  copy_effective_catalog_xmin = second_slot_contents.effective_catalog_xmin;
835 
836  copy_xmin = second_slot_contents.data.xmin;
837  copy_catalog_xmin = second_slot_contents.data.catalog_xmin;
838  copy_restart_lsn = second_slot_contents.data.restart_lsn;
839  copy_confirmed_flush = second_slot_contents.data.confirmed_flush;
840 
841  /* for existence check */
842  copy_name = NameStr(second_slot_contents.data.name);
843  copy_islogical = SlotIsLogical(&second_slot_contents);
844 
845  /*
846  * Check if the source slot still exists and is valid. We regard it as
847  * invalid if the type of replication slot or name has been changed,
848  * or the restart_lsn either is invalid or has gone backward. (The
849  * restart_lsn could go backwards if the source slot is dropped and
850  * copied from an older slot during installation.)
851  *
852  * Since erroring out will release and drop the destination slot we
853  * don't need to release it here.
854  */
855  if (copy_restart_lsn < src_restart_lsn ||
856  src_islogical != copy_islogical ||
857  strcmp(copy_name, NameStr(*src_name)) != 0)
858  ereport(ERROR,
859  (errmsg("could not copy replication slot \"%s\"",
860  NameStr(*src_name)),
861  errdetail("The source replication slot was modified incompatibly during the copy operation.")));
862 
863  /* The source slot must have a consistent snapshot */
864  if (src_islogical && XLogRecPtrIsInvalid(copy_confirmed_flush))
865  ereport(ERROR,
866  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
867  errmsg("cannot copy unfinished logical replication slot \"%s\"",
868  NameStr(*src_name)),
869  errhint("Retry when the source replication slot's confirmed_flush_lsn is valid.")));
870 
871  /* Install copied values again */
873  MyReplicationSlot->effective_xmin = copy_effective_xmin;
874  MyReplicationSlot->effective_catalog_xmin = copy_effective_catalog_xmin;
875 
876  MyReplicationSlot->data.xmin = copy_xmin;
877  MyReplicationSlot->data.catalog_xmin = copy_catalog_xmin;
878  MyReplicationSlot->data.restart_lsn = copy_restart_lsn;
879  MyReplicationSlot->data.confirmed_flush = copy_confirmed_flush;
881 
886 
887 #ifdef USE_ASSERT_CHECKING
888  /* Check that the restart_lsn is available */
889  {
890  XLogSegNo segno;
891 
892  XLByteToSeg(copy_restart_lsn, segno, wal_segment_size);
893  Assert(XLogGetLastRemovedSegno() < segno);
894  }
895 #endif
896  }
897 
898  /* target slot fully created, mark as persistent if needed */
899  if (logical_slot && !temporary)
901 
902  /* All done. Set up the return values */
903  values[0] = NameGetDatum(dst_name);
904  nulls[0] = false;
906  {
908  nulls[1] = false;
909  }
910  else
911  nulls[1] = true;
912 
913  tuple = heap_form_tuple(tupdesc, values, nulls);
914  result = HeapTupleGetDatum(tuple);
915 
917 
918  PG_RETURN_DATUM(result);
919 }
static const char * plugin
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
void CheckSlotRequirements(void)
Definition: slot.c:1044
#define NameGetDatum(X)
Definition: postgres.h:639
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
int errhint(const char *fmt,...)
Definition: elog.c:1156
static void create_physical_replication_slot(char *name, bool immediately_reserve, bool temporary, XLogRecPtr restart_lsn)
Definition: slotfuncs.c:46
int wal_segment_size
Definition: xlog.c:119
uint32 TransactionId
Definition: c.h:587
ReplicationSlotPersistency persistency
Definition: slot.h:54
int errcode(int sqlerrcode)
Definition: elog.c:698
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
void ReplicationSlotSave(void)
Definition: slot.c:710
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
ReplicationSlotPersistentData data
Definition: slot.h:147
XLogRecPtr confirmed_flush
Definition: slot.h:84
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:4006
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:817
#define ERROR
Definition: elog.h:46
void ReplicationSlotPersist(void)
Definition: slot.c:745
TransactionId effective_xmin
Definition: slot.h:143
Definition: c.h:675
static void create_logical_replication_slot(char *name, char *plugin, bool temporary, bool two_phase, XLogRecPtr restart_lsn, bool find_startpoint)
Definition: slotfuncs.c:126
uint64 XLogSegNo
Definition: xlogdefs.h:48
int errdetail(const char *fmt,...)
Definition: elog.c:1042
TransactionId catalog_xmin
Definition: slot.h:70
void ReplicationSlotRelease(void)
Definition: slot.c:469
TransactionId xmin
Definition: slot.h:62
#define SlotIsLogical(slot)
Definition: slot.h:169
static void check_permissions(void)
Definition: slotfuncs.c:29
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
TransactionId effective_catalog_xmin
Definition: slot.h:144
#define ereport(elevel,...)
Definition: elog.h:157
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
#define PG_NARGS()
Definition: fmgr.h:203
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int i
#define NameStr(name)
Definition: c.h:681
ReplicationSlot replication_slots[1]
Definition: slot.h:180
slock_t mutex
Definition: slot.h:120
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:103
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:767
void ReplicationSlotMarkDirty(void)
Definition: slot.c:728
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ create_logical_replication_slot()

static void create_logical_replication_slot ( char *  name,
char *  plugin,
bool  temporary,
bool  two_phase,
XLogRecPtr  restart_lsn,
bool  find_startpoint 
)
static

Definition at line 126 of file slotfuncs.c.

References Assert, CreateInitDecodingContext(), DecodingContextFindStartpoint(), FreeDecodingContext(), MyReplicationSlot, NIL, read_local_xlog_page(), ReplicationSlotCreate(), RS_EPHEMERAL, RS_TEMPORARY, wal_segment_close(), wal_segment_open(), and XL_ROUTINE.

Referenced by copy_replication_slot(), and pg_create_logical_replication_slot().

130 {
131  LogicalDecodingContext *ctx = NULL;
132 
134 
135  /*
136  * Acquire a logical decoding slot, this will check for conflicting names.
137  * Initially create persistent slot as ephemeral - that allows us to
138  * nicely handle errors during initialization because it'll get dropped if
139  * this transaction fails. We'll make it persistent at the end. Temporary
140  * slots can be created as temporary from beginning as they get dropped on
141  * error as well.
142  */
144  temporary ? RS_TEMPORARY : RS_EPHEMERAL, two_phase);
145 
146  /*
147  * Create logical decoding context to find start point or, if we don't
148  * need it, to 1) bump slot's restart_lsn and xmin 2) check plugin sanity.
149  *
150  * Note: when !find_startpoint this is still important, because it's at
151  * this point that the output plugin is validated.
152  */
154  false, /* just catalogs is OK */
155  restart_lsn,
156  XL_ROUTINE(.page_read = read_local_xlog_page,
157  .segment_open = wal_segment_open,
158  .segment_close = wal_segment_close),
159  NULL, NULL, NULL);
160 
161  /*
162  * If caller needs us to determine the decoding start point, do so now.
163  * This might take a while.
164  */
165  if (find_startpoint)
167 
168  /* don't need the decoding context anymore */
169  FreeDecodingContext(ctx);
170 }
#define NIL
Definition: pg_list.h:65
static const char * plugin
void wal_segment_close(XLogReaderState *state)
Definition: xlogutils.c:829
void wal_segment_open(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition: xlogutils.c:804
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency persistency, bool two_phase)
Definition: slot.c:226
void DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
Definition: logical.c:589
#define XL_ROUTINE(...)
Definition: xlogreader.h:116
static bool two_phase
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:633
const char * name
Definition: encode.c:515
int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
Definition: xlogutils.c:848
LogicalDecodingContext * CreateInitDecodingContext(const char *plugin, List *output_plugin_options, bool need_full_snapshot, XLogRecPtr restart_lsn, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:318

◆ create_physical_replication_slot()

static void create_physical_replication_slot ( char *  name,
bool  immediately_reserve,
bool  temporary,
XLogRecPtr  restart_lsn 
)
static

Definition at line 46 of file slotfuncs.c.

References Assert, ReplicationSlot::data, MyReplicationSlot, ReplicationSlotCreate(), ReplicationSlotMarkDirty(), ReplicationSlotReserveWal(), ReplicationSlotSave(), ReplicationSlotPersistentData::restart_lsn, RS_PERSISTENT, RS_TEMPORARY, and XLogRecPtrIsInvalid.

Referenced by copy_replication_slot(), and pg_create_physical_replication_slot().

48 {
50 
51  /* acquire replication slot, this will check for conflicting names */
53  temporary ? RS_TEMPORARY : RS_PERSISTENT, false);
54 
55  if (immediately_reserve)
56  {
57  /* Reserve WAL as the user asked for it */
58  if (XLogRecPtrIsInvalid(restart_lsn))
60  else
61  MyReplicationSlot->data.restart_lsn = restart_lsn;
62 
63  /* Write this slot to disk */
66  }
67 }
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency persistency, bool two_phase)
Definition: slot.c:226
void ReplicationSlotSave(void)
Definition: slot.c:710
ReplicationSlotPersistentData data
Definition: slot.h:147
void ReplicationSlotReserveWal(void)
Definition: slot.c:1069
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:73
const char * name
Definition: encode.c:515
void ReplicationSlotMarkDirty(void)
Definition: slot.c:728

◆ pg_copy_logical_replication_slot_a()

Datum pg_copy_logical_replication_slot_a ( PG_FUNCTION_ARGS  )

Definition at line 923 of file slotfuncs.c.

References copy_replication_slot().

924 {
925  return copy_replication_slot(fcinfo, true);
926 }
static Datum copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
Definition: slotfuncs.c:701

◆ pg_copy_logical_replication_slot_b()

Datum pg_copy_logical_replication_slot_b ( PG_FUNCTION_ARGS  )

Definition at line 929 of file slotfuncs.c.

References copy_replication_slot().

930 {
931  return copy_replication_slot(fcinfo, true);
932 }
static Datum copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
Definition: slotfuncs.c:701

◆ pg_copy_logical_replication_slot_c()

Datum pg_copy_logical_replication_slot_c ( PG_FUNCTION_ARGS  )

Definition at line 935 of file slotfuncs.c.

References copy_replication_slot().

936 {
937  return copy_replication_slot(fcinfo, true);
938 }
static Datum copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
Definition: slotfuncs.c:701

◆ pg_copy_physical_replication_slot_a()

Datum pg_copy_physical_replication_slot_a ( PG_FUNCTION_ARGS  )

Definition at line 941 of file slotfuncs.c.

References copy_replication_slot().

942 {
943  return copy_replication_slot(fcinfo, false);
944 }
static Datum copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
Definition: slotfuncs.c:701

◆ pg_copy_physical_replication_slot_b()

Datum pg_copy_physical_replication_slot_b ( PG_FUNCTION_ARGS  )

Definition at line 947 of file slotfuncs.c.

References copy_replication_slot().

948 {
949  return copy_replication_slot(fcinfo, false);
950 }
static Datum copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
Definition: slotfuncs.c:701

◆ pg_create_logical_replication_slot()

Datum pg_create_logical_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 176 of file slotfuncs.c.

References check_permissions(), CheckLogicalDecodingRequirements(), ReplicationSlotPersistentData::confirmed_flush, create_logical_replication_slot(), ReplicationSlot::data, elog, ERROR, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum, InvalidXLogRecPtr, LSNGetDatum, MyReplicationSlot, ReplicationSlotPersistentData::name, name, NameGetDatum, NameStr, PG_GETARG_BOOL, PG_GETARG_NAME, PG_RETURN_DATUM, plugin, ReplicationSlotPersist(), ReplicationSlotRelease(), two_phase, TYPEFUNC_COMPOSITE, and values.

177 {
178  Name name = PG_GETARG_NAME(0);
180  bool temporary = PG_GETARG_BOOL(2);
181  bool two_phase = PG_GETARG_BOOL(3);
182  Datum result;
183  TupleDesc tupdesc;
184  HeapTuple tuple;
185  Datum values[2];
186  bool nulls[2];
187 
188  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
189  elog(ERROR, "return type must be a row type");
190 
192 
194 
196  NameStr(*plugin),
197  temporary,
198  two_phase,
200  true);
201 
202  values[0] = NameGetDatum(&MyReplicationSlot->data.name);
204 
205  memset(nulls, 0, sizeof(nulls));
206 
207  tuple = heap_form_tuple(tupdesc, values, nulls);
208  result = HeapTupleGetDatum(tuple);
209 
210  /* ok, slot is now fully created, mark it as persistent if needed */
211  if (!temporary)
214 
215  PG_RETURN_DATUM(result);
216 }
static const char * plugin
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define NameGetDatum(X)
Definition: postgres.h:639
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
ReplicationSlotPersistentData data
Definition: slot.h:147
XLogRecPtr confirmed_flush
Definition: slot.h:84
#define ERROR
Definition: elog.h:46
void ReplicationSlotPersist(void)
Definition: slot.c:745
Definition: c.h:675
static void create_logical_replication_slot(char *name, char *plugin, bool temporary, bool two_phase, XLogRecPtr restart_lsn, bool find_startpoint)
Definition: slotfuncs.c:126
void ReplicationSlotRelease(void)
Definition: slot.c:469
static void check_permissions(void)
Definition: slotfuncs.c:29
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static bool two_phase
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
const char * name
Definition: encode.c:515
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:103
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_create_physical_replication_slot()

Datum pg_create_physical_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 74 of file slotfuncs.c.

References check_permissions(), CheckSlotRequirements(), create_physical_replication_slot(), ReplicationSlot::data, elog, ERROR, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum, InvalidXLogRecPtr, LSNGetDatum, MyReplicationSlot, ReplicationSlotPersistentData::name, name, NameGetDatum, NameStr, PG_GETARG_BOOL, PG_GETARG_NAME, PG_RETURN_DATUM, ReplicationSlotRelease(), ReplicationSlotPersistentData::restart_lsn, TYPEFUNC_COMPOSITE, and values.

75 {
77  bool immediately_reserve = PG_GETARG_BOOL(1);
78  bool temporary = PG_GETARG_BOOL(2);
79  Datum values[2];
80  bool nulls[2];
81  TupleDesc tupdesc;
82  HeapTuple tuple;
83  Datum result;
84 
85  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
86  elog(ERROR, "return type must be a row type");
87 
89 
91 
93  immediately_reserve,
94  temporary,
96 
97  values[0] = NameGetDatum(&MyReplicationSlot->data.name);
98  nulls[0] = false;
99 
100  if (immediately_reserve)
101  {
103  nulls[1] = false;
104  }
105  else
106  nulls[1] = true;
107 
108  tuple = heap_form_tuple(tupdesc, values, nulls);
109  result = HeapTupleGetDatum(tuple);
110 
112 
113  PG_RETURN_DATUM(result);
114 }
void CheckSlotRequirements(void)
Definition: slot.c:1044
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define NameGetDatum(X)
Definition: postgres.h:639
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
static void create_physical_replication_slot(char *name, bool immediately_reserve, bool temporary, XLogRecPtr restart_lsn)
Definition: slotfuncs.c:46
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
ReplicationSlotPersistentData data
Definition: slot.h:147
#define ERROR
Definition: elog.h:46
Definition: c.h:675
void ReplicationSlotRelease(void)
Definition: slot.c:469
static void check_permissions(void)
Definition: slotfuncs.c:29
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
XLogRecPtr restart_lsn
Definition: slot.h:73
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
const char * name
Definition: encode.c:515
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_drop_replication_slot()

Datum pg_drop_replication_slot ( PG_FUNCTION_ARGS  )

Definition at line 223 of file slotfuncs.c.

References check_permissions(), CheckSlotRequirements(), name, NameStr, PG_GETARG_NAME, PG_RETURN_VOID, and ReplicationSlotDrop().

224 {
225  Name name = PG_GETARG_NAME(0);
226 
228 
230 
231  ReplicationSlotDrop(NameStr(*name), true);
232 
233  PG_RETURN_VOID();
234 }
void CheckSlotRequirements(void)
Definition: slot.c:1044
Definition: c.h:675
static void check_permissions(void)
Definition: slotfuncs.c:29
#define PG_RETURN_VOID()
Definition: fmgr.h:349
const char * name
Definition: encode.c:515
void ReplicationSlotDrop(const char *name, bool nowait)
Definition: slot.c:563
#define NameStr(name)
Definition: c.h:681
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_get_replication_slots()

Datum pg_get_replication_slots ( PG_FUNCTION_ARGS  )

Definition at line 240 of file slotfuncs.c.

References ReplicationSlot::active_pid, ReturnSetInfo::allowedModes, Assert, BoolGetDatum, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, CStringGetTextDatum, ReplicationSlot::data, ReplicationSlotPersistentData::database, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), GetWALAvailability(), GetXLogWriteRecPtr(), i, ReplicationSlot::in_use, Int32GetDatum, Int64GetDatum(), ReplicationSlotPersistentData::invalidated_at, InvalidOid, InvalidTransactionId, InvalidXLogRecPtr, IsA, LSNGetDatum, LW_SHARED, LWLockAcquire(), LWLockRelease(), Max, max_replication_slots, max_slot_wal_keep_size_mb, MemoryContextSwitchTo(), ReplicationSlot::mutex, ReplicationSlotPersistentData::name, NameGetDatum, ObjectIdGetDatum, ReplicationSlotPersistentData::persistency, PG_GET_REPLICATION_SLOTS_COLS, ReplicationSlotPersistentData::plugin, ReplicationSlotCtlData::replication_slots, ReplicationSlotCtl, ReplicationSlotPersistentData::restart_lsn, ReturnSetInfo::returnMode, RS_TEMPORARY, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SpinLockAcquire, SpinLockRelease, TransactionIdGetDatum, tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), ReplicationSlotPersistentData::two_phase, TYPEFUNC_COMPOSITE, values, wal_keep_size_mb, wal_segment_size, WALAVAIL_EXTENDED, WALAVAIL_INVALID_LSN, WALAVAIL_REMOVED, WALAVAIL_RESERVED, WALAVAIL_UNRESERVED, work_mem, XLByteToSeg, XLogMBVarToSegs, XLogRecPtrIsInvalid, XLogSegNoOffsetToRecPtr, and ReplicationSlotPersistentData::xmin.

241 {
242 #define PG_GET_REPLICATION_SLOTS_COLS 14
243  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
244  TupleDesc tupdesc;
245  Tuplestorestate *tupstore;
246  MemoryContext per_query_ctx;
247  MemoryContext oldcontext;
248  XLogRecPtr currlsn;
249  int slotno;
250 
251  /* check to see if caller supports us returning a tuplestore */
252  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
253  ereport(ERROR,
254  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
255  errmsg("set-valued function called in context that cannot accept a set")));
256  if (!(rsinfo->allowedModes & SFRM_Materialize))
257  ereport(ERROR,
258  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
259  errmsg("materialize mode required, but it is not allowed in this context")));
260 
261  /* Build a tuple descriptor for our result type */
262  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
263  elog(ERROR, "return type must be a row type");
264 
265  /*
266  * We don't require any special permission to see this function's data
267  * because nothing should be sensitive. The most critical being the slot
268  * name, which shouldn't contain anything particularly sensitive.
269  */
270 
271  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
272  oldcontext = MemoryContextSwitchTo(per_query_ctx);
273 
274  tupstore = tuplestore_begin_heap(true, false, work_mem);
275  rsinfo->returnMode = SFRM_Materialize;
276  rsinfo->setResult = tupstore;
277  rsinfo->setDesc = tupdesc;
278 
279  MemoryContextSwitchTo(oldcontext);
280 
281  currlsn = GetXLogWriteRecPtr();
282 
283  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
284  for (slotno = 0; slotno < max_replication_slots; slotno++)
285  {
287  ReplicationSlot slot_contents;
289  bool nulls[PG_GET_REPLICATION_SLOTS_COLS];
290  WALAvailability walstate;
291  int i;
292 
293  if (!slot->in_use)
294  continue;
295 
296  /* Copy slot contents while holding spinlock, then examine at leisure */
297  SpinLockAcquire(&slot->mutex);
298  slot_contents = *slot;
299  SpinLockRelease(&slot->mutex);
300 
301  memset(values, 0, sizeof(values));
302  memset(nulls, 0, sizeof(nulls));
303 
304  i = 0;
305  values[i++] = NameGetDatum(&slot_contents.data.name);
306 
307  if (slot_contents.data.database == InvalidOid)
308  nulls[i++] = true;
309  else
310  values[i++] = NameGetDatum(&slot_contents.data.plugin);
311 
312  if (slot_contents.data.database == InvalidOid)
313  values[i++] = CStringGetTextDatum("physical");
314  else
315  values[i++] = CStringGetTextDatum("logical");
316 
317  if (slot_contents.data.database == InvalidOid)
318  nulls[i++] = true;
319  else
320  values[i++] = ObjectIdGetDatum(slot_contents.data.database);
321 
322  values[i++] = BoolGetDatum(slot_contents.data.persistency == RS_TEMPORARY);
323  values[i++] = BoolGetDatum(slot_contents.active_pid != 0);
324 
325  if (slot_contents.active_pid != 0)
326  values[i++] = Int32GetDatum(slot_contents.active_pid);
327  else
328  nulls[i++] = true;
329 
330  if (slot_contents.data.xmin != InvalidTransactionId)
331  values[i++] = TransactionIdGetDatum(slot_contents.data.xmin);
332  else
333  nulls[i++] = true;
334 
335  if (slot_contents.data.catalog_xmin != InvalidTransactionId)
336  values[i++] = TransactionIdGetDatum(slot_contents.data.catalog_xmin);
337  else
338  nulls[i++] = true;
339 
340  if (slot_contents.data.restart_lsn != InvalidXLogRecPtr)
341  values[i++] = LSNGetDatum(slot_contents.data.restart_lsn);
342  else
343  nulls[i++] = true;
344 
345  if (slot_contents.data.confirmed_flush != InvalidXLogRecPtr)
346  values[i++] = LSNGetDatum(slot_contents.data.confirmed_flush);
347  else
348  nulls[i++] = true;
349 
350  /*
351  * If invalidated_at is valid and restart_lsn is invalid, we know for
352  * certain that the slot has been invalidated. Otherwise, test
353  * availability from restart_lsn.
354  */
355  if (XLogRecPtrIsInvalid(slot_contents.data.restart_lsn) &&
356  !XLogRecPtrIsInvalid(slot_contents.data.invalidated_at))
357  walstate = WALAVAIL_REMOVED;
358  else
359  walstate = GetWALAvailability(slot_contents.data.restart_lsn);
360 
361  switch (walstate)
362  {
364  nulls[i++] = true;
365  break;
366 
367  case WALAVAIL_RESERVED:
368  values[i++] = CStringGetTextDatum("reserved");
369  break;
370 
371  case WALAVAIL_EXTENDED:
372  values[i++] = CStringGetTextDatum("extended");
373  break;
374 
375  case WALAVAIL_UNRESERVED:
376  values[i++] = CStringGetTextDatum("unreserved");
377  break;
378 
379  case WALAVAIL_REMOVED:
380 
381  /*
382  * If we read the restart_lsn long enough ago, maybe that file
383  * has been removed by now. However, the walsender could have
384  * moved forward enough that it jumped to another file after
385  * we looked. If checkpointer signalled the process to
386  * termination, then it's definitely lost; but if a process is
387  * still alive, then "unreserved" seems more appropriate.
388  *
389  * If we do change it, save the state for safe_wal_size below.
390  */
391  if (!XLogRecPtrIsInvalid(slot_contents.data.restart_lsn))
392  {
393  int pid;
394 
395  SpinLockAcquire(&slot->mutex);
396  pid = slot->active_pid;
397  slot_contents.data.restart_lsn = slot->data.restart_lsn;
398  SpinLockRelease(&slot->mutex);
399  if (pid != 0)
400  {
401  values[i++] = CStringGetTextDatum("unreserved");
402  walstate = WALAVAIL_UNRESERVED;
403  break;
404  }
405  }
406  values[i++] = CStringGetTextDatum("lost");
407  break;
408  }
409 
410  /*
411  * safe_wal_size is only computed for slots that have not been lost,
412  * and only if there's a configured maximum size.
413  */
414  if (walstate == WALAVAIL_REMOVED || max_slot_wal_keep_size_mb < 0)
415  nulls[i++] = true;
416  else
417  {
418  XLogSegNo targetSeg;
419  uint64 slotKeepSegs;
420  uint64 keepSegs;
421  XLogSegNo failSeg;
422  XLogRecPtr failLSN;
423 
424  XLByteToSeg(slot_contents.data.restart_lsn, targetSeg, wal_segment_size);
425 
426  /* determine how many segments slots can be kept by slots */
428  /* ditto for wal_keep_size */
430 
431  /* if currpos reaches failLSN, we lose our segment */
432  failSeg = targetSeg + Max(slotKeepSegs, keepSegs) + 1;
433  XLogSegNoOffsetToRecPtr(failSeg, 0, wal_segment_size, failLSN);
434 
435  values[i++] = Int64GetDatum(failLSN - currlsn);
436  }
437 
438  values[i++] = BoolGetDatum(slot_contents.data.two_phase);
439 
441 
442  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
443  }
444 
445  LWLockRelease(ReplicationSlotControlLock);
446 
447  tuplestore_donestoring(tupstore);
448 
449  return (Datum) 0;
450 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define NameGetDatum(X)
Definition: postgres.h:639
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
int wal_segment_size
Definition: xlog.c:119
#define PG_GET_REPLICATION_SLOTS_COLS
WALAvailability
Definition: xlog.h:246
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ReplicationSlotPersistency persistency
Definition: slot.h:54
int errcode(int sqlerrcode)
Definition: elog.c:698
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
int wal_keep_size_mb
Definition: xlog.c:93
ReplicationSlotPersistentData data
Definition: slot.h:147
XLogRecPtr confirmed_flush
Definition: slot.h:84
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
uint64 XLogSegNo
Definition: xlogdefs.h:48
TransactionId catalog_xmin
Definition: slot.h:70
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xmin
Definition: slot.h:62
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
#define TransactionIdGetDatum(X)
Definition: postgres.h:565
uintptr_t Datum
Definition: postgres.h:411
int work_mem
Definition: globals.c:124
#define BoolGetDatum(X)
Definition: postgres.h:446
#define InvalidOid
Definition: postgres_ext.h:36
#define XLogMBVarToSegs(mbvar, wal_segsz_bytes)
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:305
int max_slot_wal_keep_size_mb
Definition: xlog.c:112
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:11786
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
#define Max(x, y)
Definition: c.h:980
int max_replication_slots
Definition: slot.c:99
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
Tuplestorestate * setResult
Definition: execnodes.h:310
static Datum values[MAXATTR]
Definition: bootstrap.c:166
ExprContext * econtext
Definition: execnodes.h:303
#define Int32GetDatum(X)
Definition: postgres.h:523
TupleDesc setDesc
Definition: execnodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:909
pid_t active_pid
Definition: slot.h:126
WALAvailability GetWALAvailability(XLogRecPtr targetLSN)
Definition: xlog.c:9730
#define elog(elevel,...)
Definition: elog.h:232
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:82
ReplicationSlot replication_slots[1]
Definition: slot.h:180
XLogRecPtr invalidated_at
Definition: slot.h:76
slock_t mutex
Definition: slot.h:120
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ pg_logical_replication_slot_advance()

static XLogRecPtr pg_logical_replication_slot_advance ( XLogRecPtr  moveto)
static

Definition at line 497 of file slotfuncs.c.

References Assert, CHECK_FOR_INTERRUPTS, ReplicationSlotPersistentData::confirmed_flush, CreateDecodingContext(), CurrentResourceOwner, ReplicationSlot::data, elog, XLogReaderState::EndRecPtr, ERROR, FreeDecodingContext(), InvalidateSystemCaches(), InvalidXLogRecPtr, LogicalConfirmReceivedLocation(), LogicalDecodingProcessRecord(), MyReplicationSlot, NIL, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, read_local_xlog_page(), LogicalDecodingContext::reader, ReplicationSlotMarkDirty(), ReplicationSlotPersistentData::restart_lsn, wal_segment_close(), wal_segment_open(), XL_ROUTINE, XLogBeginRead(), and XLogReadRecord().

Referenced by pg_replication_slot_advance().

498 {
500  ResourceOwner old_resowner = CurrentResourceOwner;
501  XLogRecPtr retlsn;
502 
503  Assert(moveto != InvalidXLogRecPtr);
504 
505  PG_TRY();
506  {
507  /*
508  * Create our decoding context in fast_forward mode, passing start_lsn
509  * as InvalidXLogRecPtr, so that we start processing from my slot's
510  * confirmed_flush.
511  */
513  NIL,
514  true, /* fast_forward */
515  XL_ROUTINE(.page_read = read_local_xlog_page,
516  .segment_open = wal_segment_open,
517  .segment_close = wal_segment_close),
518  NULL, NULL, NULL);
519 
520  /*
521  * Start reading at the slot's restart_lsn, which we know to point to
522  * a valid record.
523  */
525 
526  /* invalidate non-timetravel entries */
528 
529  /* Decode at least one record, until we run out of records */
530  while (ctx->reader->EndRecPtr < moveto)
531  {
532  char *errm = NULL;
533  XLogRecord *record;
534 
535  /*
536  * Read records. No changes are generated in fast_forward mode,
537  * but snapbuilder/slot statuses are updated properly.
538  */
539  record = XLogReadRecord(ctx->reader, &errm);
540  if (errm)
541  elog(ERROR, "%s", errm);
542 
543  /*
544  * Process the record. Storage-level changes are ignored in
545  * fast_forward mode, but other modules (such as snapbuilder)
546  * might still have critical updates to do.
547  */
548  if (record)
550 
551  /* Stop once the requested target has been reached */
552  if (moveto <= ctx->reader->EndRecPtr)
553  break;
554 
556  }
557 
558  /*
559  * Logical decoding could have clobbered CurrentResourceOwner during
560  * transaction management, so restore the executor's value. (This is
561  * a kluge, but it's not worth cleaning up right now.)
562  */
563  CurrentResourceOwner = old_resowner;
564 
565  if (ctx->reader->EndRecPtr != InvalidXLogRecPtr)
566  {
568 
569  /*
570  * If only the confirmed_flush LSN has changed the slot won't get
571  * marked as dirty by the above. Callers on the walsender
572  * interface are expected to keep track of their own progress and
573  * don't need it written out. But SQL-interface users cannot
574  * specify their own start positions and it's harder for them to
575  * keep track of their progress, so we should make more of an
576  * effort to save it for them.
577  *
578  * Dirty the slot so it is written out at the next checkpoint. The
579  * LSN position advanced to may still be lost on a crash but this
580  * makes the data consistent after a clean shutdown.
581  */
583  }
584 
586 
587  /* free context, call shutdown callback */
588  FreeDecodingContext(ctx);
589 
591  }
592  PG_CATCH();
593  {
594  /* clear all timetravel entries */
596 
597  PG_RE_THROW();
598  }
599  PG_END_TRY();
600 
601  return retlsn;
602 }
#define NIL
Definition: pg_list.h:65
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void wal_segment_close(XLogReaderState *state)
Definition: xlogutils.c:829
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void wal_segment_open(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition: xlogutils.c:804
ReplicationSlotPersistentData data
Definition: slot.h:147
void InvalidateSystemCaches(void)
Definition: inval.c:650
XLogRecPtr confirmed_flush
Definition: slot.h:84
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:271
#define ERROR
Definition: elog.h:46
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:106
LogicalDecodingContext * CreateDecodingContext(XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:478
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:243
#define XL_ROUTINE(...)
Definition: xlogreader.h:116
#define PG_CATCH()
Definition: elog.h:323
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:633
XLogRecPtr restart_lsn
Definition: slot.h:73
#define PG_RE_THROW()
Definition: elog.h:354
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1695
XLogReaderState * reader
Definition: logical.h:41
#define elog(elevel,...)
Definition: elog.h:232
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
#define PG_TRY()
Definition: elog.h:313
int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
Definition: xlogutils.c:848
#define PG_END_TRY()
Definition: elog.h:338
void ReplicationSlotMarkDirty(void)
Definition: slot.c:728

◆ pg_physical_replication_slot_advance()

static XLogRecPtr pg_physical_replication_slot_advance ( XLogRecPtr  moveto)
static

Definition at line 460 of file slotfuncs.c.

References Assert, ReplicationSlot::data, InvalidXLogRecPtr, ReplicationSlot::mutex, MyReplicationSlot, ReplicationSlotMarkDirty(), ReplicationSlotPersistentData::restart_lsn, SpinLockAcquire, and SpinLockRelease.

Referenced by pg_replication_slot_advance().

461 {
463  XLogRecPtr retlsn = startlsn;
464 
465  Assert(moveto != InvalidXLogRecPtr);
466 
467  if (startlsn < moveto)
468  {
472  retlsn = moveto;
473 
474  /*
475  * Dirty the slot so as it is written out at the next checkpoint. Note
476  * that the LSN position advanced may still be lost in the event of a
477  * crash, but this makes the data consistent after a clean shutdown.
478  */
480  }
481 
482  return retlsn;
483 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:147
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:73
slock_t mutex
Definition: slot.h:120
void ReplicationSlotMarkDirty(void)
Definition: slot.c:728

◆ pg_replication_slot_advance()

Datum pg_replication_slot_advance ( PG_FUNCTION_ARGS  )

Definition at line 608 of file slotfuncs.c.

References Assert, check_permissions(), ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, ReplicationSlotPersistentData::database, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, get_call_result_type(), GetFlushRecPtr(), GetXLogReplayRecPtr(), heap_form_tuple(), HeapTupleGetDatum, LSN_FORMAT_ARGS, LSNGetDatum, Min, MyReplicationSlot, ReplicationSlotPersistentData::name, NameGetDatum, NameStr, OidIsValid, PG_GETARG_LSN, PG_GETARG_NAME, pg_logical_replication_slot_advance(), pg_physical_replication_slot_advance(), PG_RETURN_DATUM, RecoveryInProgress(), ReplicationSlotAcquire(), ReplicationSlotRelease(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotPersistentData::restart_lsn, ThisTimeLineID, TYPEFUNC_COMPOSITE, values, and XLogRecPtrIsInvalid.

609 {
610  Name slotname = PG_GETARG_NAME(0);
611  XLogRecPtr moveto = PG_GETARG_LSN(1);
612  XLogRecPtr endlsn;
613  XLogRecPtr minlsn;
614  TupleDesc tupdesc;
615  Datum values[2];
616  bool nulls[2];
617  HeapTuple tuple;
618  Datum result;
619 
621 
623 
624  if (XLogRecPtrIsInvalid(moveto))
625  ereport(ERROR,
626  (errmsg("invalid target WAL LSN")));
627 
628  /* Build a tuple descriptor for our result type */
629  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
630  elog(ERROR, "return type must be a row type");
631 
632  /*
633  * We can't move slot past what's been flushed/replayed so clamp the
634  * target position accordingly.
635  */
636  if (!RecoveryInProgress())
637  moveto = Min(moveto, GetFlushRecPtr());
638  else
639  moveto = Min(moveto, GetXLogReplayRecPtr(&ThisTimeLineID));
640 
641  /* Acquire the slot so we "own" it */
642  ReplicationSlotAcquire(NameStr(*slotname), true);
643 
644  /* A slot whose restart_lsn has never been reserved cannot be advanced */
646  ereport(ERROR,
647  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
648  errmsg("replication slot \"%s\" cannot be advanced",
649  NameStr(*slotname)),
650  errdetail("This slot has never previously reserved WAL, or it has been invalidated.")));
651 
652  /*
653  * Check if the slot is not moving backwards. Physical slots rely simply
654  * on restart_lsn as a minimum point, while logical slots have confirmed
655  * consumption up to confirmed_flush, meaning that in both cases data
656  * older than that is not available anymore.
657  */
660  else
662 
663  if (moveto < minlsn)
664  ereport(ERROR,
665  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
666  errmsg("cannot advance replication slot to %X/%X, minimum is %X/%X",
667  LSN_FORMAT_ARGS(moveto), LSN_FORMAT_ARGS(minlsn))));
668 
669  /* Do the actual slot update, depending on the slot type */
671  endlsn = pg_logical_replication_slot_advance(moveto);
672  else
673  endlsn = pg_physical_replication_slot_advance(moveto);
674 
675  values[0] = NameGetDatum(&MyReplicationSlot->data.name);
676  nulls[0] = false;
677 
678  /*
679  * Recompute the minimum LSN and xmin across all slots to adjust with the
680  * advancing potentially done.
681  */
684 
686 
687  /* Return the reached position. */
688  values[1] = LSNGetDatum(endlsn);
689  nulls[1] = false;
690 
691  tuple = heap_form_tuple(tupdesc, values, nulls);
692  result = HeapTupleGetDatum(tuple);
693 
694  PG_RETURN_DATUM(result);
695 }
static XLogRecPtr pg_logical_replication_slot_advance(XLogRecPtr moveto)
Definition: slotfuncs.c:497
#define NameGetDatum(X)
Definition: postgres.h:639
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
void ReplicationSlotAcquire(const char *name, bool nowait)
Definition: slot.c:380
#define Min(x, y)
Definition: c.h:986
int errcode(int sqlerrcode)
Definition: elog.c:698
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:8578
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
ReplicationSlotPersistentData data
Definition: slot.h:147
bool RecoveryInProgress(void)
Definition: xlog.c:8226
#define OidIsValid(objectId)
Definition: c.h:710
XLogRecPtr confirmed_flush
Definition: slot.h:84
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:817
#define ERROR
Definition: elog.h:46
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11751
Definition: c.h:675
int errdetail(const char *fmt,...)
Definition: elog.c:1042
void ReplicationSlotRelease(void)
Definition: slot.c:469
static void check_permissions(void)
Definition: slotfuncs.c:29
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
TimeLineID ThisTimeLineID
Definition: xlog.c:194
#define ereport(elevel,...)
Definition: elog.h:157
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:73
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
static XLogRecPtr pg_physical_replication_slot_advance(XLogRecPtr moveto)
Definition: slotfuncs.c:460
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:767
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278