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-2020, 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"
18 
19 /*
20  * Behaviour of replication slots, upon release or crash.
21  *
22  * Slots marked as PERSISTENT are crash-safe and will not be dropped when
23  * released. Slots marked as EPHEMERAL will be dropped when released or after
24  * restarts. Slots marked TEMPORARY will be dropped at the end of a session
25  * or on error.
26  *
27  * EPHEMERAL is used as a not-quite-ready state when creating persistent
28  * slots. EPHEMERAL slots can be made PERSISTENT by calling
29  * ReplicationSlotPersist(). For a slot that goes away at the end of a
30  * session, TEMPORARY is the appropriate choice.
31  */
33 {
38 
39 /* For ReplicationSlotAcquire, q.v. */
40 typedef enum SlotAcquireBehavior
41 {
46 
47 /*
48  * On-Disk data of a replication slot, preserved across restarts.
49  */
51 {
52  /* The slot's identifier */
54 
55  /* database the slot is active on */
57 
58  /*
59  * The slot's behaviour when being dropped (or restored after a crash).
60  */
62 
63  /*
64  * xmin horizon for data
65  *
66  * NB: This may represent a value that hasn't been written to disk yet;
67  * see notes for effective_xmin, below.
68  */
70 
71  /*
72  * xmin horizon for catalog tuples
73  *
74  * NB: This may represent a value that hasn't been written to disk yet;
75  * see notes for effective_xmin, below.
76  */
78 
79  /* oldest LSN that might be required by this replication slot */
81 
82  /*
83  * Oldest LSN that the client has acked receipt for. This is used as the
84  * start_lsn point in case the client doesn't specify one, and also as a
85  * safety measure to jump forwards in case the client specifies a
86  * start_lsn that's further in the past than this value.
87  */
89 
90  /* plugin name */
93 
94 /*
95  * Shared memory state of a single replication slot.
96  *
97  * The in-memory data of replication slots follows a locking model based
98  * on two linked concepts:
99  * - A replication slot's in_use flag is switched when added or discarded using
100  * the LWLock ReplicationSlotControlLock, which needs to be hold in exclusive
101  * mode when updating the flag by the backend owning the slot and doing the
102  * operation, while readers (concurrent backends not owning the slot) need
103  * to hold it in shared mode when looking at replication slot data.
104  * - Individual fields are protected by mutex where only the backend owning
105  * the slot is authorized to update the fields from its own slot. The
106  * backend owning the slot does not need to take this lock when reading its
107  * own fields, while concurrent backends not owning this slot should take the
108  * lock when reading this slot's data.
109  */
110 typedef struct ReplicationSlot
111 {
112  /* lock, on same cacheline as effective_xmin */
114 
115  /* is this slot defined */
116  bool in_use;
117 
118  /* Who is streaming out changes for this slot? 0 in unused slots. */
119  pid_t active_pid;
120 
121  /* any outstanding modifications? */
123  bool dirty;
124 
125  /*
126  * For logical decoding, it's extremely important that we never remove any
127  * data that's still needed for decoding purposes, even after a crash;
128  * otherwise, decoding will produce wrong answers. Ordinary streaming
129  * replication also needs to prevent old row versions from being removed
130  * too soon, but the worst consequence we might encounter there is
131  * unwanted query cancellations on the standby. Thus, for logical
132  * decoding, this value represents the latest xmin that has actually been
133  * written to disk, whereas for streaming replication, it's just the same
134  * as the persistent value (data.xmin).
135  */
138 
139  /* data surviving shutdowns and crashes */
141 
142  /* is somebody performing io on this slot? */
144 
145  /* Condition variable signalled when active_pid changes */
147 
148  /* all the remaining data is only used for logical slots */
149 
150  /*
151  * When the client has confirmed flushes >= candidate_xmin_lsn we can
152  * advance the catalog xmin. When restart_valid has been passed,
153  * restart_lsn can be increased.
154  */
160 
161 #define SlotIsPhysical(slot) (slot->data.database == InvalidOid)
162 #define SlotIsLogical(slot) (slot->data.database != InvalidOid)
163 
164 /*
165  * Shared memory control area for all of replication slots.
166  */
168 {
169  /*
170  * This array should be declared [FLEXIBLE_ARRAY_MEMBER], but for some
171  * reason you can't do that in an otherwise-empty struct.
172  */
173  ReplicationSlot replication_slots[1];
175 
176 /*
177  * Pointers to shared memory
178  */
181 
182 /* GUCs */
184 
185 /* shmem initialization functions */
186 extern Size ReplicationSlotsShmemSize(void);
187 extern void ReplicationSlotsShmemInit(void);
188 
189 /* management of individual slots */
190 extern void ReplicationSlotCreate(const char *name, bool db_specific,
192 extern void ReplicationSlotPersist(void);
193 extern void ReplicationSlotDrop(const char *name, bool nowait);
194 
195 extern int ReplicationSlotAcquire(const char *name, SlotAcquireBehavior behavior);
196 extern void ReplicationSlotRelease(void);
197 extern void ReplicationSlotCleanup(void);
198 extern void ReplicationSlotSave(void);
199 extern void ReplicationSlotMarkDirty(void);
200 
201 /* misc stuff */
202 extern bool ReplicationSlotValidateName(const char *name, int elevel);
203 extern void ReplicationSlotReserveWal(void);
204 extern void ReplicationSlotsComputeRequiredXmin(bool already_locked);
205 extern void ReplicationSlotsComputeRequiredLSN(void);
207 extern bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive);
208 extern void ReplicationSlotsDropDBSlots(Oid dboid);
209 extern void InvalidateObsoleteReplicationSlots(XLogSegNo oldestSegno);
210 
211 extern void StartupReplicationSlots(void);
212 extern void CheckPointReplicationSlots(void);
213 
214 extern void CheckSlotRequirements(void);
215 
216 #endif /* SLOT_H */
void ReplicationSlotRelease(void)
Definition: slot.c:433
int slock_t
Definition: s_lock.h:934
Definition: lwlock.h:32
TransactionId candidate_catalog_xmin
Definition: slot.h:155
void ReplicationSlotReserveWal(void)
Definition: slot.c:1013
void CheckPointReplicationSlots(void)
Definition: slot.c:1164
uint32 TransactionId
Definition: c.h:513
ReplicationSlotPersistency persistency
Definition: slot.h:61
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency p)
Definition: slot.c:219
ReplicationSlotPersistentData data
Definition: slot.h:140
unsigned int Oid
Definition: postgres_ext.h:31
#define PGDLLIMPORT
Definition: c.h:1280
XLogRecPtr confirmed_flush
Definition: slot.h:88
void ReplicationSlotsShmemInit(void)
Definition: slot.c:132
void CheckSlotRequirements(void)
Definition: slot.c:988
void ReplicationSlotCleanup(void)
Definition: slot.c:488
PGDLLIMPORT int max_replication_slots
Definition: slot.c:99
void ReplicationSlotPersist(void)
Definition: slot.c:689
Definition: slot.h:43
TransactionId effective_xmin
Definition: slot.h:136
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:711
Definition: c.h:609
struct ReplicationSlot ReplicationSlot
XLogRecPtr candidate_restart_valid
Definition: slot.h:157
uint64 XLogSegNo
Definition: xlogdefs.h:41
SlotAcquireBehavior
Definition: slot.h:40
TransactionId catalog_xmin
Definition: slot.h:77
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:761
void ReplicationSlotDrop(const char *name, bool nowait)
Definition: slot.c:526
struct ReplicationSlotPersistentData ReplicationSlotPersistentData
TransactionId xmin
Definition: slot.h:69
bool in_use
Definition: slot.h:116
static int elevel
Definition: vacuumlazy.c:323
void StartupReplicationSlots(void)
Definition: slot.c:1199
bool just_dirtied
Definition: slot.h:122
TransactionId effective_catalog_xmin
Definition: slot.h:137
void ReplicationSlotMarkDirty(void)
Definition: slot.c:672
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:80
size_t Size
Definition: c.h:466
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:114
ConditionVariable active_cv
Definition: slot.h:146
int ReplicationSlotAcquire(const char *name, SlotAcquireBehavior behavior)
Definition: slot.c:334
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:156
struct ReplicationSlotCtlData ReplicationSlotCtlData
PGDLLIMPORT ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
PGDLLIMPORT ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
ReplicationSlotPersistency
Definition: slot.h:32
pid_t active_pid
Definition: slot.h:119
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:172
Definition: slot.h:42
void InvalidateObsoleteReplicationSlots(XLogSegNo oldestSegno)
Definition: slot.c:1090
slock_t mutex
Definition: slot.h:113
void ReplicationSlotSave(void)
Definition: slot.c:654
bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive)
Definition: slot.c:856
bool dirty
Definition: slot.h:123
XLogRecPtr candidate_restart_lsn
Definition: slot.h:158
LWLock io_in_progress_lock
Definition: slot.h:143
void ReplicationSlotsDropDBSlots(Oid dboid)
Definition: slot.c:912
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:804