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