PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
slot.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  * slot.h
3  * Replication slot management.
4  *
5  * Copyright (c) 2012-2017, PostgreSQL Global Development Group
6  *
7  *-------------------------------------------------------------------------
8  */
9 #ifndef SLOT_H
10 #define SLOT_H
11 
12 #include "fmgr.h"
13 #include "access/xlog.h"
14 #include "access/xlogreader.h"
16 #include "storage/lwlock.h"
17 #include "storage/shmem.h"
18 #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 /*
41  * On-Disk data of a replication slot, preserved across restarts.
42  */
44 {
45  /* The slot's identifier */
47 
48  /* database the slot is active on */
50 
51  /*
52  * The slot's behaviour when being dropped (or restored after a crash).
53  */
55 
56  /*
57  * xmin horizon for data
58  *
59  * NB: This may represent a value that hasn't been written to disk yet;
60  * see notes for effective_xmin, below.
61  */
63 
64  /*
65  * xmin horizon for catalog tuples
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  /* oldest LSN that might be required by this replication slot */
74 
75  /*
76  * Oldest LSN that the client has acked receipt for. This is used as the
77  * start_lsn point in case the client doesn't specify one, and also as a
78  * safety measure to jump forwards in case the client specifies a
79  * start_lsn that's further in the past than this value.
80  */
82 
83  /* plugin name */
86 
87 /*
88  * Shared memory state of a single replication slot.
89  */
90 typedef struct ReplicationSlot
91 {
92  /* lock, on same cacheline as effective_xmin */
94 
95  /* is this slot defined */
96  bool in_use;
97 
98  /* Who is streaming out changes for this slot? 0 in unused slots. */
99  pid_t active_pid;
100 
101  /* any outstanding modifications? */
103  bool dirty;
104 
105  /*
106  * For logical decoding, it's extremely important that we never remove any
107  * data that's still needed for decoding purposes, even after a crash;
108  * otherwise, decoding will produce wrong answers. Ordinary streaming
109  * replication also needs to prevent old row versions from being removed
110  * too soon, but the worst consequence we might encounter there is
111  * unwanted query cancellations on the standby. Thus, for logical
112  * decoding, this value represents the latest xmin that has actually been
113  * written to disk, whereas for streaming replication, it's just the same
114  * as the persistent value (data.xmin).
115  */
118 
119  /* data surviving shutdowns and crashes */
121 
122  /* is somebody performing io on this slot? */
124 
125  /* Condition variable signalled when active_pid changes */
127 
128  /* all the remaining data is only used for logical slots */
129 
130  /*
131  * When the client has confirmed flushes >= candidate_xmin_lsn we can
132  * advance the catalog xmin. When restart_valid has been passed,
133  * restart_lsn can be increased.
134  */
140 
141 #define SlotIsPhysical(slot) (slot->data.database == InvalidOid)
142 #define SlotIsLogical(slot) (slot->data.database != InvalidOid)
143 
144 /*
145  * Shared memory control area for all of replication slots.
146  */
148 {
149  /*
150  * This array should be declared [FLEXIBLE_ARRAY_MEMBER], but for some
151  * reason you can't do that in an otherwise-empty struct.
152  */
155 
156 /*
157  * Pointers to shared memory
158  */
161 
162 /* GUCs */
164 
165 /* shmem initialization functions */
166 extern Size ReplicationSlotsShmemSize(void);
167 extern void ReplicationSlotsShmemInit(void);
168 
169 /* management of individual slots */
170 extern void ReplicationSlotCreate(const char *name, bool db_specific,
172 extern void ReplicationSlotPersist(void);
173 extern void ReplicationSlotDrop(const char *name, bool nowait);
174 
175 extern void ReplicationSlotAcquire(const char *name, bool nowait);
176 extern void ReplicationSlotRelease(void);
177 extern void ReplicationSlotCleanup(void);
178 extern void ReplicationSlotSave(void);
179 extern void ReplicationSlotMarkDirty(void);
180 
181 /* misc stuff */
182 extern bool ReplicationSlotValidateName(const char *name, int elevel);
183 extern void ReplicationSlotReserveWal(void);
184 extern void ReplicationSlotsComputeRequiredXmin(bool already_locked);
185 extern void ReplicationSlotsComputeRequiredLSN(void);
187 extern bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive);
188 extern void ReplicationSlotsDropDBSlots(Oid dboid);
189 
190 extern void StartupReplicationSlots(void);
191 extern void CheckPointReplicationSlots(void);
192 
193 extern void CheckSlotRequirements(void);
194 
195 #endif /* SLOT_H */
void ReplicationSlotRelease(void)
Definition: slot.c:416
int slock_t
Definition: s_lock.h:888
Definition: lwlock.h:32
TransactionId candidate_catalog_xmin
Definition: slot.h:135
void ReplicationSlotReserveWal(void)
Definition: slot.c:985
void CheckPointReplicationSlots(void)
Definition: slot.c:1055
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
uint32 TransactionId
Definition: c.h:391
ReplicationSlotPersistency persistency
Definition: slot.h:54
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency p)
Definition: slot.c:221
ReplicationSlotPersistentData data
Definition: slot.h:120
unsigned int Oid
Definition: postgres_ext.h:31
#define PGDLLIMPORT
Definition: c.h:1069
XLogRecPtr confirmed_flush
Definition: slot.h:81
void ReplicationSlotsShmemInit(void)
Definition: slot.c:132
void CheckSlotRequirements(void)
Definition: slot.c:965
void ReplicationSlotCleanup(void)
Definition: slot.c:471
PGDLLIMPORT int max_replication_slots
Definition: slot.c:99
void ReplicationSlotPersist(void)
Definition: slot.c:673
TransactionId effective_xmin
Definition: slot.h:116
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:695
Definition: c.h:487
struct ReplicationSlot ReplicationSlot
XLogRecPtr candidate_restart_valid
Definition: slot.h:137
void ReplicationSlotAcquire(const char *name, bool nowait)
Definition: slot.c:330
TransactionId catalog_xmin
Definition: slot.h:70
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:741
void ReplicationSlotDrop(const char *name, bool nowait)
Definition: slot.c:509
struct ReplicationSlotPersistentData ReplicationSlotPersistentData
TransactionId xmin
Definition: slot.h:62
bool in_use
Definition: slot.h:96
static int elevel
Definition: vacuumlazy.c:136
void StartupReplicationSlots(void)
Definition: slot.c:1090
bool just_dirtied
Definition: slot.h:102
TransactionId effective_catalog_xmin
Definition: slot.h:117
void ReplicationSlotMarkDirty(void)
Definition: slot.c:656
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:73
size_t Size
Definition: c.h:350
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:114
ConditionVariable active_cv
Definition: slot.h:126
const char * name
Definition: encode.c:521
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:136
struct ReplicationSlotCtlData ReplicationSlotCtlData
PGDLLIMPORT ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
ReplicationSlotPersistency
Definition: slot.h:33
pid_t active_pid
Definition: slot.h:99
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:174
ReplicationSlot replication_slots[1]
Definition: slot.h:153
slock_t mutex
Definition: slot.h:93
void ReplicationSlotSave(void)
Definition: slot.c:638
bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive)
Definition: slot.c:833
bool dirty
Definition: slot.h:103
XLogRecPtr candidate_restart_lsn
Definition: slot.h:138
LWLock io_in_progress_lock
Definition: slot.h:123
void ReplicationSlotsDropDBSlots(Oid dboid)
Definition: slot.c:889
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:784