PostgreSQL Source Code  git master
slot.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  * slot.h
3  * Replication slot management.
4  *
5  * Copyright (c) 2012-2021, PostgreSQL Global Development Group
6  *
7  *-------------------------------------------------------------------------
8  */
9 #ifndef SLOT_H
10 #define SLOT_H
11 
12 #include "access/xlog.h"
13 #include "access/xlogreader.h"
15 #include "storage/lwlock.h"
16 #include "storage/shmem.h"
17 #include "storage/spin.h"
19 
20 /*
21  * Behaviour of replication slots, upon release or crash.
22  *
23  * Slots marked as PERSISTENT are crash-safe and will not be dropped when
24  * released. Slots marked as EPHEMERAL will be dropped when released or after
25  * restarts. Slots marked TEMPORARY will be dropped at the end of a session
26  * or on error.
27  *
28  * EPHEMERAL is used as a not-quite-ready state when creating persistent
29  * slots. EPHEMERAL slots can be made PERSISTENT by calling
30  * ReplicationSlotPersist(). For a slot that goes away at the end of a
31  * session, TEMPORARY is the appropriate choice.
32  */
34 {
39 
40 /* For ReplicationSlotAcquire, q.v. */
41 typedef enum SlotAcquireBehavior
42 {
47 
48 /*
49  * On-Disk data of a replication slot, preserved across restarts.
50  */
52 {
53  /* The slot's identifier */
55 
56  /* database the slot is active on */
58 
59  /*
60  * The slot's behaviour when being dropped (or restored after a crash).
61  */
63 
64  /*
65  * xmin horizon for data
66  *
67  * NB: This may represent a value that hasn't been written to disk yet;
68  * see notes for effective_xmin, below.
69  */
71 
72  /*
73  * xmin horizon for catalog tuples
74  *
75  * NB: This may represent a value that hasn't been written to disk yet;
76  * see notes for effective_xmin, below.
77  */
79 
80  /* oldest LSN that might be required by this replication slot */
82 
83  /* restart_lsn is copied here when the slot is invalidated */
85 
86  /*
87  * Oldest LSN that the client has acked receipt for. This is used as the
88  * start_lsn point in case the client doesn't specify one, and also as a
89  * safety measure to jump forwards in case the client specifies a
90  * start_lsn that's further in the past than this value.
91  */
93 
94  /*
95  * LSN at which we found a consistent point at the time of slot creation.
96  * This is also the point where we have exported a snapshot for the
97  * initial copy.
98  */
100 
101  /*
102  * Allow decoding of prepared transactions?
103  */
104  bool two_phase;
105 
106  /* plugin name */
109 
110 /*
111  * Shared memory state of a single replication slot.
112  *
113  * The in-memory data of replication slots follows a locking model based
114  * on two linked concepts:
115  * - A replication slot's in_use flag is switched when added or discarded using
116  * the LWLock ReplicationSlotControlLock, which needs to be hold in exclusive
117  * mode when updating the flag by the backend owning the slot and doing the
118  * operation, while readers (concurrent backends not owning the slot) need
119  * to hold it in shared mode when looking at replication slot data.
120  * - Individual fields are protected by mutex where only the backend owning
121  * the slot is authorized to update the fields from its own slot. The
122  * backend owning the slot does not need to take this lock when reading its
123  * own fields, while concurrent backends not owning this slot should take the
124  * lock when reading this slot's data.
125  */
126 typedef struct ReplicationSlot
127 {
128  /* lock, on same cacheline as effective_xmin */
130 
131  /* is this slot defined */
132  bool in_use;
133 
134  /* Who is streaming out changes for this slot? 0 in unused slots. */
135  pid_t active_pid;
136 
137  /* any outstanding modifications? */
139  bool dirty;
140 
141  /*
142  * For logical decoding, it's extremely important that we never remove any
143  * data that's still needed for decoding purposes, even after a crash;
144  * otherwise, decoding will produce wrong answers. Ordinary streaming
145  * replication also needs to prevent old row versions from being removed
146  * too soon, but the worst consequence we might encounter there is
147  * unwanted query cancellations on the standby. Thus, for logical
148  * decoding, this value represents the latest xmin that has actually been
149  * written to disk, whereas for streaming replication, it's just the same
150  * as the persistent value (data.xmin).
151  */
154 
155  /* data surviving shutdowns and crashes */
157 
158  /* is somebody performing io on this slot? */
160 
161  /* Condition variable signaled when active_pid changes */
163 
164  /* all the remaining data is only used for logical slots */
165 
166  /*
167  * When the client has confirmed flushes >= candidate_xmin_lsn we can
168  * advance the catalog xmin. When restart_valid has been passed,
169  * restart_lsn can be increased.
170  */
176 
177 #define SlotIsPhysical(slot) ((slot)->data.database == InvalidOid)
178 #define SlotIsLogical(slot) ((slot)->data.database != InvalidOid)
179 
180 /*
181  * Shared memory control area for all of replication slots.
182  */
184 {
185  /*
186  * This array should be declared [FLEXIBLE_ARRAY_MEMBER], but for some
187  * reason you can't do that in an otherwise-empty struct.
188  */
189  ReplicationSlot replication_slots[1];
191 
192 /*
193  * Pointers to shared memory
194  */
197 
198 /* GUCs */
200 
201 /* shmem initialization functions */
202 extern Size ReplicationSlotsShmemSize(void);
203 extern void ReplicationSlotsShmemInit(void);
204 
205 /* management of individual slots */
206 extern void ReplicationSlotCreate(const char *name, bool db_specific,
208 extern void ReplicationSlotPersist(void);
209 extern void ReplicationSlotDrop(const char *name, bool nowait);
210 
211 extern int ReplicationSlotAcquire(const char *name, SlotAcquireBehavior behavior);
212 extern void ReplicationSlotRelease(void);
213 extern void ReplicationSlotCleanup(void);
214 extern void ReplicationSlotSave(void);
215 extern void ReplicationSlotMarkDirty(void);
216 
217 /* misc stuff */
218 extern bool ReplicationSlotValidateName(const char *name, int elevel);
219 extern void ReplicationSlotReserveWal(void);
220 extern void ReplicationSlotsComputeRequiredXmin(bool already_locked);
221 extern void ReplicationSlotsComputeRequiredLSN(void);
223 extern bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive);
224 extern void ReplicationSlotsDropDBSlots(Oid dboid);
225 extern void InvalidateObsoleteReplicationSlots(XLogSegNo oldestSegno);
226 extern ReplicationSlot *SearchNamedReplicationSlot(const char *name);
227 extern void ReplicationSlotNameForTablesync(Oid suboid, Oid relid, char *syncslotname, int szslot);
228 extern void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok);
229 
230 extern void StartupReplicationSlots(void);
231 extern void CheckPointReplicationSlots(void);
232 
233 extern void CheckSlotRequirements(void);
234 
235 #endif /* SLOT_H */
void ReplicationSlotRelease(void)
Definition: slot.c:492
int slock_t
Definition: s_lock.h:934
Definition: lwlock.h:31
TransactionId candidate_catalog_xmin
Definition: slot.h:171
WalReceiverConn * wrconn
Definition: worker.c:159
void ReplicationSlotReserveWal(void)
Definition: slot.c:1086
ReplicationSlot * SearchNamedReplicationSlot(const char *name)
Definition: slot.c:351
void CheckPointReplicationSlots(void)
Definition: slot.c:1278
uint32 TransactionId
Definition: c.h:587
ReplicationSlotPersistency persistency
Definition: slot.h:62
ReplicationSlotPersistentData data
Definition: slot.h:156
unsigned int Oid
Definition: postgres_ext.h:31
#define PGDLLIMPORT
Definition: c.h:1317
XLogRecPtr confirmed_flush
Definition: slot.h:92
void ReplicationSlotsShmemInit(void)
Definition: slot.c:134
void CheckSlotRequirements(void)
Definition: slot.c:1061
void ReplicationSlotCleanup(void)
Definition: slot.c:548
PGDLLIMPORT int max_replication_slots
Definition: slot.c:99
void ReplicationSlotPersist(void)
Definition: slot.c:762
Definition: slot.h:44
TransactionId effective_xmin
Definition: slot.h:152
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:784
Definition: c.h:675
struct ReplicationSlot ReplicationSlot
XLogRecPtr candidate_restart_valid
Definition: slot.h:173
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency p, bool two_phase)
Definition: slot.c:228
uint64 XLogSegNo
Definition: xlogdefs.h:48
SlotAcquireBehavior
Definition: slot.h:41
TransactionId catalog_xmin
Definition: slot.h:78
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:834
void ReplicationSlotDrop(const char *name, bool nowait)
Definition: slot.c:586
struct ReplicationSlotPersistentData ReplicationSlotPersistentData
TransactionId xmin
Definition: slot.h:70
void ReplicationSlotNameForTablesync(Oid suboid, Oid relid, char *syncslotname, int szslot)
Definition: tablesync.c:885
XLogRecPtr initial_consistent_point
Definition: slot.h:99
bool in_use
Definition: slot.h:132
static int elevel
Definition: vacuumlazy.c:333
void StartupReplicationSlots(void)
Definition: slot.c:1313
bool just_dirtied
Definition: slot.h:138
TransactionId effective_catalog_xmin
Definition: slot.h:153
void ReplicationSlotMarkDirty(void)
Definition: slot.c:745
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:81
size_t Size
Definition: c.h:540
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:116
ConditionVariable active_cv
Definition: slot.h:162
int ReplicationSlotAcquire(const char *name, SlotAcquireBehavior behavior)
Definition: slot.c:383
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:172
struct ReplicationSlotCtlData ReplicationSlotCtlData
PGDLLIMPORT ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
PGDLLIMPORT ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
ReplicationSlotPersistency
Definition: slot.h:33
pid_t active_pid
Definition: slot.h:135
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:174
Definition: slot.h:43
void InvalidateObsoleteReplicationSlots(XLogSegNo oldestSegno)
Definition: slot.c:1163
XLogRecPtr invalidated_at
Definition: slot.h:84
slock_t mutex
Definition: slot.h:129
void ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok)
void ReplicationSlotSave(void)
Definition: slot.c:727
bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive)
Definition: slot.c:929
bool dirty
Definition: slot.h:139
XLogRecPtr candidate_restart_lsn
Definition: slot.h:174
LWLock io_in_progress_lock
Definition: slot.h:159
void ReplicationSlotsDropDBSlots(Oid dboid)
Definition: slot.c:985
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:877