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.
26  *
27  * EPHEMERAL slots can be made PERSISTENT by calling ReplicationSlotPersist().
28  */
30 {
35 
36 /*
37  * On-Disk data of a replication slot, preserved across restarts.
38  */
40 {
41  /* The slot's identifier */
43 
44  /* database the slot is active on */
46 
47  /*
48  * The slot's behaviour when being dropped (or restored after a crash).
49  */
51 
52  /*
53  * xmin horizon for data
54  *
55  * NB: This may represent a value that hasn't been written to disk yet;
56  * see notes for effective_xmin, below.
57  */
59 
60  /*
61  * xmin horizon for catalog tuples
62  *
63  * NB: This may represent a value that hasn't been written to disk yet;
64  * see notes for effective_xmin, below.
65  */
67 
68  /* oldest LSN that might be required by this replication slot */
70 
71  /*
72  * Oldest LSN that the client has acked receipt for. This is used as the
73  * start_lsn point in case the client doesn't specify one, and also as a
74  * safety measure to jump forwards in case the client specifies a
75  * start_lsn that's further in the past than this value.
76  */
78 
79  /* plugin name */
82 
83 /*
84  * Shared memory state of a single replication slot.
85  */
86 typedef struct ReplicationSlot
87 {
88  /* lock, on same cacheline as effective_xmin */
90 
91  /* is this slot defined */
92  bool in_use;
93 
94  /* Who is streaming out changes for this slot? 0 in unused slots. */
95  pid_t active_pid;
96 
97  /* any outstanding modifications? */
99  bool dirty;
100 
101  /*
102  * For logical decoding, it's extremely important that we never remove any
103  * data that's still needed for decoding purposes, even after a crash;
104  * otherwise, decoding will produce wrong answers. Ordinary streaming
105  * replication also needs to prevent old row versions from being removed
106  * too soon, but the worst consequence we might encounter there is
107  * unwanted query cancellations on the standby. Thus, for logical
108  * decoding, this value represents the latest xmin that has actually been
109  * written to disk, whereas for streaming replication, it's just the same
110  * as the persistent value (data.xmin).
111  */
114 
115  /* data surviving shutdowns and crashes */
117 
118  /* is somebody performing io on this slot? */
120 
121  /* Condition variable signalled when active_pid changes */
123 
124  /* all the remaining data is only used for logical slots */
125 
126  /*
127  * When the client has confirmed flushes >= candidate_xmin_lsn we can
128  * advance the catalog xmin. When restart_valid has been passed,
129  * restart_lsn can be increased.
130  */
136 
137 #define SlotIsPhysical(slot) (slot->data.database == InvalidOid)
138 #define SlotIsLogical(slot) (slot->data.database != InvalidOid)
139 
140 /*
141  * Shared memory control area for all of replication slots.
142  */
144 {
145  /*
146  * This array should be declared [FLEXIBLE_ARRAY_MEMBER], but for some
147  * reason you can't do that in an otherwise-empty struct.
148  */
151 
152 /*
153  * Pointers to shared memory
154  */
157 
158 /* GUCs */
160 
161 /* shmem initialization functions */
162 extern Size ReplicationSlotsShmemSize(void);
163 extern void ReplicationSlotsShmemInit(void);
164 
165 /* management of individual slots */
166 extern void ReplicationSlotCreate(const char *name, bool db_specific,
168 extern void ReplicationSlotPersist(void);
169 extern void ReplicationSlotDrop(const char *name, bool nowait);
170 
171 extern void ReplicationSlotAcquire(const char *name, bool nowait);
172 extern void ReplicationSlotRelease(void);
173 extern void ReplicationSlotCleanup(void);
174 extern void ReplicationSlotSave(void);
175 extern void ReplicationSlotMarkDirty(void);
176 
177 /* misc stuff */
178 extern bool ReplicationSlotValidateName(const char *name, int elevel);
179 extern void ReplicationSlotReserveWal(void);
180 extern void ReplicationSlotsComputeRequiredXmin(bool already_locked);
181 extern void ReplicationSlotsComputeRequiredLSN(void);
183 extern bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive);
184 extern void ReplicationSlotsDropDBSlots(Oid dboid);
185 
186 extern void StartupReplicationSlots(void);
187 extern void CheckPointReplicationSlots(void);
188 
189 extern void CheckSlotRequirements(void);
190 
191 #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:131
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:397
ReplicationSlotPersistency persistency
Definition: slot.h:50
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency p)
Definition: slot.c:221
ReplicationSlotPersistentData data
Definition: slot.h:116
unsigned int Oid
Definition: postgres_ext.h:31
#define PGDLLIMPORT
Definition: c.h:1064
XLogRecPtr confirmed_flush
Definition: slot.h:77
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:112
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:695
Definition: c.h:493
struct ReplicationSlot ReplicationSlot
XLogRecPtr candidate_restart_valid
Definition: slot.h:133
void ReplicationSlotAcquire(const char *name, bool nowait)
Definition: slot.c:330
TransactionId catalog_xmin
Definition: slot.h:66
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:58
bool in_use
Definition: slot.h:92
static int elevel
Definition: vacuumlazy.c:136
void StartupReplicationSlots(void)
Definition: slot.c:1090
bool just_dirtied
Definition: slot.h:98
TransactionId effective_catalog_xmin
Definition: slot.h:113
void ReplicationSlotMarkDirty(void)
Definition: slot.c:656
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:69
size_t Size
Definition: c.h:356
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:114
ConditionVariable active_cv
Definition: slot.h:122
const char * name
Definition: encode.c:521
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:132
struct ReplicationSlotCtlData ReplicationSlotCtlData
PGDLLIMPORT ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
ReplicationSlotPersistency
Definition: slot.h:29
pid_t active_pid
Definition: slot.h:95
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:174
ReplicationSlot replication_slots[1]
Definition: slot.h:149
slock_t mutex
Definition: slot.h:89
void ReplicationSlotSave(void)
Definition: slot.c:638
bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive)
Definition: slot.c:833
bool dirty
Definition: slot.h:99
XLogRecPtr candidate_restart_lsn
Definition: slot.h:134
LWLock io_in_progress_lock
Definition: slot.h:119
void ReplicationSlotsDropDBSlots(Oid dboid)
Definition: slot.c:889
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:784