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-2019, 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 /*
40  * On-Disk data of a replication slot, preserved across restarts.
41  */
43 {
44  /* The slot's identifier */
46 
47  /* database the slot is active on */
49 
50  /*
51  * The slot's behaviour when being dropped (or restored after a crash).
52  */
54 
55  /*
56  * xmin horizon for data
57  *
58  * NB: This may represent a value that hasn't been written to disk yet;
59  * see notes for effective_xmin, below.
60  */
62 
63  /*
64  * xmin horizon for catalog tuples
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  /* oldest LSN that might be required by this replication slot */
73 
74  /*
75  * Oldest LSN that the client has acked receipt for. This is used as the
76  * start_lsn point in case the client doesn't specify one, and also as a
77  * safety measure to jump forwards in case the client specifies a
78  * start_lsn that's further in the past than this value.
79  */
81 
82  /* plugin name */
85 
86 /*
87  * Shared memory state of a single replication slot.
88  *
89  * The in-memory data of replication slots follows a locking model based
90  * on two linked concepts:
91  * - A replication slot's in_use flag is switched when added or discarded using
92  * the LWLock ReplicationSlotControlLock, which needs to be hold in exclusive
93  * mode when updating the flag by the backend owning the slot and doing the
94  * operation, while readers (concurrent backends not owning the slot) need
95  * to hold it in shared mode when looking at replication slot data.
96  * - Individual fields are protected by mutex where only the backend owning
97  * the slot is authorized to update the fields from its own slot. The
98  * backend owning the slot does not need to take this lock when reading its
99  * own fields, while concurrent backends not owning this slot should take the
100  * lock when reading this slot's data.
101  */
102 typedef struct ReplicationSlot
103 {
104  /* lock, on same cacheline as effective_xmin */
106 
107  /* is this slot defined */
108  bool in_use;
109 
110  /* Who is streaming out changes for this slot? 0 in unused slots. */
111  pid_t active_pid;
112 
113  /* any outstanding modifications? */
115  bool dirty;
116 
117  /*
118  * For logical decoding, it's extremely important that we never remove any
119  * data that's still needed for decoding purposes, even after a crash;
120  * otherwise, decoding will produce wrong answers. Ordinary streaming
121  * replication also needs to prevent old row versions from being removed
122  * too soon, but the worst consequence we might encounter there is
123  * unwanted query cancellations on the standby. Thus, for logical
124  * decoding, this value represents the latest xmin that has actually been
125  * written to disk, whereas for streaming replication, it's just the same
126  * as the persistent value (data.xmin).
127  */
130 
131  /* data surviving shutdowns and crashes */
133 
134  /* is somebody performing io on this slot? */
136 
137  /* Condition variable signalled when active_pid changes */
139 
140  /* all the remaining data is only used for logical slots */
141 
142  /*
143  * When the client has confirmed flushes >= candidate_xmin_lsn we can
144  * advance the catalog xmin. When restart_valid has been passed,
145  * restart_lsn can be increased.
146  */
152 
153 #define SlotIsPhysical(slot) (slot->data.database == InvalidOid)
154 #define SlotIsLogical(slot) (slot->data.database != InvalidOid)
155 
156 /*
157  * Shared memory control area for all of replication slots.
158  */
160 {
161  /*
162  * This array should be declared [FLEXIBLE_ARRAY_MEMBER], but for some
163  * reason you can't do that in an otherwise-empty struct.
164  */
165  ReplicationSlot replication_slots[1];
167 
168 /*
169  * Pointers to shared memory
170  */
173 
174 /* GUCs */
176 
177 /* shmem initialization functions */
178 extern Size ReplicationSlotsShmemSize(void);
179 extern void ReplicationSlotsShmemInit(void);
180 
181 /* management of individual slots */
182 extern void ReplicationSlotCreate(const char *name, bool db_specific,
184 extern void ReplicationSlotPersist(void);
185 extern void ReplicationSlotDrop(const char *name, bool nowait);
186 
187 extern void ReplicationSlotAcquire(const char *name, bool nowait);
188 extern void ReplicationSlotRelease(void);
189 extern void ReplicationSlotCleanup(void);
190 extern void ReplicationSlotSave(void);
191 extern void ReplicationSlotMarkDirty(void);
192 
193 /* misc stuff */
194 extern bool ReplicationSlotValidateName(const char *name, int elevel);
195 extern void ReplicationSlotReserveWal(void);
196 extern void ReplicationSlotsComputeRequiredXmin(bool already_locked);
197 extern void ReplicationSlotsComputeRequiredLSN(void);
199 extern bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive);
200 extern void ReplicationSlotsDropDBSlots(Oid dboid);
201 
202 extern void StartupReplicationSlots(void);
203 extern void CheckPointReplicationSlots(void);
204 
205 extern void CheckSlotRequirements(void);
206 
207 #endif /* SLOT_H */
void ReplicationSlotRelease(void)
Definition: slot.c:424
int slock_t
Definition: s_lock.h:934
Definition: lwlock.h:32
TransactionId candidate_catalog_xmin
Definition: slot.h:147
void ReplicationSlotReserveWal(void)
Definition: slot.c:997
void CheckPointReplicationSlots(void)
Definition: slot.c:1074
uint32 TransactionId
Definition: c.h:508
ReplicationSlotPersistency persistency
Definition: slot.h:53
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency p)
Definition: slot.c:221
ReplicationSlotPersistentData data
Definition: slot.h:132
unsigned int Oid
Definition: postgres_ext.h:31
#define PGDLLIMPORT
Definition: c.h:1266
XLogRecPtr confirmed_flush
Definition: slot.h:80
void ReplicationSlotsShmemInit(void)
Definition: slot.c:132
void CheckSlotRequirements(void)
Definition: slot.c:972
void ReplicationSlotCleanup(void)
Definition: slot.c:479
PGDLLIMPORT int max_replication_slots
Definition: slot.c:99
void ReplicationSlotPersist(void)
Definition: slot.c:680
TransactionId effective_xmin
Definition: slot.h:128
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:702
Definition: c.h:604
struct ReplicationSlot ReplicationSlot
XLogRecPtr candidate_restart_valid
Definition: slot.h:149
void ReplicationSlotAcquire(const char *name, bool nowait)
Definition: slot.c:330
TransactionId catalog_xmin
Definition: slot.h:69
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:748
void ReplicationSlotDrop(const char *name, bool nowait)
Definition: slot.c:517
struct ReplicationSlotPersistentData ReplicationSlotPersistentData
TransactionId xmin
Definition: slot.h:61
bool in_use
Definition: slot.h:108
static int elevel
Definition: vacuumlazy.c:143
void StartupReplicationSlots(void)
Definition: slot.c:1109
bool just_dirtied
Definition: slot.h:114
TransactionId effective_catalog_xmin
Definition: slot.h:129
void ReplicationSlotMarkDirty(void)
Definition: slot.c:663
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:72
size_t Size
Definition: c.h:467
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:114
ConditionVariable active_cv
Definition: slot.h:138
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:148
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:111
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:174
slock_t mutex
Definition: slot.h:105
void ReplicationSlotSave(void)
Definition: slot.c:645
bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive)
Definition: slot.c:840
bool dirty
Definition: slot.h:115
XLogRecPtr candidate_restart_lsn
Definition: slot.h:150
LWLock io_in_progress_lock
Definition: slot.h:135
void ReplicationSlotsDropDBSlots(Oid dboid)
Definition: slot.c:896
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:791