PostgreSQL Source Code  git master
reorderbuffer.h
Go to the documentation of this file.
1 /*
2  * reorderbuffer.h
3  * PostgreSQL logical replay/reorder buffer management.
4  *
5  * Copyright (c) 2012-2017, PostgreSQL Global Development Group
6  *
7  * src/include/replication/reorderbuffer.h
8  */
9 #ifndef REORDERBUFFER_H
10 #define REORDERBUFFER_H
11 
12 #include "access/htup_details.h"
13 #include "lib/ilist.h"
14 #include "storage/sinval.h"
15 #include "utils/hsearch.h"
16 #include "utils/relcache.h"
17 #include "utils/snapshot.h"
18 #include "utils/timestamp.h"
19 
20 /* an individual tuple, stored in one chunk of memory */
21 typedef struct ReorderBufferTupleBuf
22 {
23  /* position in preallocated list */
25 
26  /* tuple header, the interesting bit for users of logical decoding */
28 
29  /* pre-allocated size of tuple buffer, different from tuple size */
31 
32  /* actual tuple data follows */
34 
35 /* pointer to the data stored in a TupleBuf */
36 #define ReorderBufferTupleBufData(p) \
37  ((HeapTupleHeader) MAXALIGN(((char *) p) + sizeof(ReorderBufferTupleBuf)))
38 
39 /*
40  * Types of the change passed to a 'change' callback.
41  *
42  * For efficiency and simplicity reasons we want to keep Snapshots, CommandIds
43  * and ComboCids in the same list with the user visible INSERT/UPDATE/DELETE
44  * changes. Users of the decoding facilities will never see changes with
45  * *_INTERNAL_* actions.
46  *
47  * The INTERNAL_SPEC_INSERT and INTERNAL_SPEC_CONFIRM changes concern
48  * "speculative insertions", and their confirmation respectively. They're
49  * used by INSERT .. ON CONFLICT .. UPDATE. Users of logical decoding don't
50  * have to care about these.
51  */
53 {
63 };
64 
65 /*
66  * a single 'change', can be an insert (with one tuple), an update (old, new),
67  * or a delete (old).
68  *
69  * The same struct is also used internally for other purposes but that should
70  * never be visible outside reorderbuffer.c.
71  */
72 typedef struct ReorderBufferChange
73 {
75 
76  /* The type of change. */
78 
80 
81  /*
82  * Context data for the change. Which part of the union is valid depends
83  * on action.
84  */
85  union
86  {
87  /* Old, new tuples when action == *_INSERT|UPDATE|DELETE */
88  struct
89  {
90  /* relation that has been changed */
92 
93  /* no previously reassembled toast chunks are necessary anymore */
95 
96  /* valid for DELETE || UPDATE */
98  /* valid for INSERT || UPDATE */
100  } tp;
101 
102  /* Message with arbitrary data. */
103  struct
104  {
105  char *prefix;
107  char *message;
108  } msg;
109 
110  /* New snapshot, set when action == *_INTERNAL_SNAPSHOT */
112 
113  /*
114  * New command id for existing snapshot in a catalog changing tx. Set
115  * when action == *_INTERNAL_COMMAND_ID.
116  */
118 
119  /*
120  * New cid mapping for catalog changing transaction, set when action
121  * == *_INTERNAL_TUPLECID.
122  */
123  struct
124  {
130  } tuplecid;
131  } data;
132 
133  /*
134  * While in use this is how a change is linked into a transactions,
135  * otherwise it's the preallocated list.
136  */
139 
140 typedef struct ReorderBufferTXN
141 {
142  /*
143  * The transactions transaction id, can be a toplevel or sub xid.
144  */
146 
147  /* did the TX have catalog changes */
149 
150  /*
151  * Do we know this is a subxact?
152  */
154 
155  /*
156  * LSN of the first data carrying, WAL record with knowledge about this
157  * xid. This is allowed to *not* be first record adorned with this xid, if
158  * the previous records aren't relevant for logical decoding.
159  */
161 
162  /* ----
163  * LSN of the record that lead to this xact to be committed or
164  * aborted. This can be a
165  * * plain commit record
166  * * plain commit record, of a parent transaction
167  * * prepared transaction commit
168  * * plain abort record
169  * * prepared transaction abort
170  * * error during decoding
171  * ----
172  */
174 
175  /*
176  * LSN pointing to the end of the commit record + 1.
177  */
179 
180  /*
181  * LSN of the last lsn at which snapshot information reside, so we can
182  * restart decoding from there and fully recover this transaction from
183  * WAL.
184  */
186 
187  /* origin of the change that caused this transaction */
190 
191  /*
192  * Commit time, only known when we read the actual commit record.
193  */
195 
196  /*
197  * Base snapshot or NULL.
198  */
201 
202  /*
203  * How many ReorderBufferChange's do we have in this txn.
204  *
205  * Changes in subtransactions are *not* included but tracked separately.
206  */
207  uint64 nentries;
208 
209  /*
210  * How many of the above entries are stored in memory in contrast to being
211  * spilled to disk.
212  */
213  uint64 nentries_mem;
214 
215  /*
216  * Has this transaction been spilled to disk? It's not always possible to
217  * deduce that fact by comparing nentries with nentries_mem, because e.g.
218  * subtransactions of a large transaction might get serialized together
219  * with the parent - if they're restored to memory they'd have
220  * nentries_mem == nentries.
221  */
223 
224  /*
225  * List of ReorderBufferChange structs, including new Snapshots and new
226  * CommandIds
227  */
229 
230  /*
231  * List of (relation, ctid) => (cmin, cmax) mappings for catalog tuples.
232  * Those are always assigned to the toplevel transaction. (Keep track of
233  * #entries to create a hash of the right size)
234  */
236  uint64 ntuplecids;
237 
238  /*
239  * On-demand built hash for looking up the above values.
240  */
242 
243  /*
244  * Hash containing (potentially partial) toast entries. NULL if no toast
245  * tuples have been found for the current change.
246  */
248 
249  /*
250  * non-hierarchical list of subtransactions that are *not* aborted. Only
251  * used in toplevel transactions.
252  */
255 
256  /*
257  * Stored cache invalidations. This is not a linked list because we get
258  * all the invalidations at once.
259  */
262 
263  /* ---
264  * Position in one of three lists:
265  * * list of subtransactions if we are *known* to be subxact
266  * * list of toplevel xacts (can be an as-yet unknown subxact)
267  * * list of preallocated ReorderBufferTXNs
268  * ---
269  */
271 
273 
274 /* so we can define the callbacks used inside struct ReorderBuffer itself */
276 
277 /* change callback signature */
278 typedef void (*ReorderBufferApplyChangeCB) (
279  ReorderBuffer *rb,
280  ReorderBufferTXN *txn,
281  Relation relation,
282  ReorderBufferChange *change);
283 
284 /* begin callback signature */
285 typedef void (*ReorderBufferBeginCB) (
286  ReorderBuffer *rb,
287  ReorderBufferTXN *txn);
288 
289 /* commit callback signature */
290 typedef void (*ReorderBufferCommitCB) (
291  ReorderBuffer *rb,
292  ReorderBufferTXN *txn,
293  XLogRecPtr commit_lsn);
294 
295 /* message callback signature */
296 typedef void (*ReorderBufferMessageCB) (
297  ReorderBuffer *rb,
298  ReorderBufferTXN *txn,
299  XLogRecPtr message_lsn,
300  bool transactional,
301  const char *prefix, Size sz,
302  const char *message);
303 
305 {
306  /*
307  * xid => ReorderBufferTXN lookup table
308  */
310 
311  /*
312  * Transactions that could be a toplevel xact, ordered by LSN of the first
313  * record bearing that xid.
314  */
316 
317  /*
318  * one-entry sized cache for by_txn. Very frequently the same txn gets
319  * looked up over and over again.
320  */
323 
324  /*
325  * Callbacks to be called when a transactions commits.
326  */
331 
332  /*
333  * Pointer that will be passed untouched to the callbacks.
334  */
336 
337  /*
338  * Private memory context.
339  */
341 
342  /*
343  * Memory contexts for specific types objects
344  */
348 
350 
351  /* buffer for disk<->memory conversions */
352  char *outbuf;
354 };
355 
356 
359 
364 
367  bool transactional, const char *prefix,
368  Size message_size, const char *message);
370  XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
371  TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn);
374  XLogRecPtr commit_lsn, XLogRecPtr end_lsn);
378 
382  CommandId cid);
385  CommandId cmin, CommandId cmax, CommandId combocid);
387  Size nmsgs, SharedInvalidationMessage *msgs);
389  SharedInvalidationMessage *invalidations);
394 
396 
398 
399 void StartupReorderBuffer(void);
400 
401 #endif
ReorderBuffer * ReorderBufferAllocate(void)
XLogRecPtr first_lsn
uint32 CommandId
Definition: c.h:469
TimestampTz commit_time
ReorderBufferTupleBuf * ReorderBufferGetTupleBuf(ReorderBuffer *, Size tuple_len)
void ReorderBufferCommit(ReorderBuffer *, TransactionId, XLogRecPtr commit_lsn, XLogRecPtr end_lsn, TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn)
Snapshot base_snapshot
bool ReorderBufferXidHasCatalogChanges(ReorderBuffer *, TransactionId xid)
void ReorderBufferQueueMessage(ReorderBuffer *, TransactionId, Snapshot snapshot, XLogRecPtr lsn, bool transactional, const char *prefix, Size message_size, const char *message)
ReorderBufferApplyChangeCB apply_change
void * private_data
RepOriginId origin_id
ReorderBufferTupleBuf * oldtuple
Definition: reorderbuffer.h:97
void ReorderBufferProcessXid(ReorderBuffer *, TransactionId xid, XLogRecPtr lsn)
uint32 TransactionId
Definition: c.h:455
bool ReorderBufferXidHasBaseSnapshot(ReorderBuffer *, TransactionId xid)
void ReorderBufferAssignChild(ReorderBuffer *, TransactionId, TransactionId, XLogRecPtr commit_lsn)
TransactionId by_txn_last_xid
void ReorderBufferAddSnapshot(ReorderBuffer *, TransactionId, XLogRecPtr lsn, struct SnapshotData *snap)
int64 TimestampTz
Definition: timestamp.h:39
XLogRecPtr current_restart_decoding_lsn
void(* ReorderBufferBeginCB)(ReorderBuffer *rb, ReorderBufferTXN *txn)
void StartupReorderBuffer(void)
uint16 RepOriginId
Definition: xlogdefs.h:51
void ReorderBufferForget(ReorderBuffer *, TransactionId, XLogRecPtr lsn)
ReorderBufferCommitCB commit
void ReorderBufferQueueChange(ReorderBuffer *, TransactionId, XLogRecPtr lsn, ReorderBufferChange *)
XLogRecPtr base_snapshot_lsn
MemoryContext change_context
void ReorderBufferAddInvalidations(ReorderBuffer *, TransactionId, XLogRecPtr lsn, Size nmsgs, SharedInvalidationMessage *msgs)
XLogRecPtr origin_lsn
void ReorderBufferCommitChild(ReorderBuffer *, TransactionId, TransactionId, XLogRecPtr commit_lsn, XLogRecPtr end_lsn)
void ReorderBufferReturnTupleBuf(ReorderBuffer *, ReorderBufferTupleBuf *tuple)
ReorderBufferChange * ReorderBufferGetChange(ReorderBuffer *)
Definition: dynahash.c:208
ReorderBufferChangeType
Definition: reorderbuffer.h:52
ReorderBufferTupleBuf * newtuple
Definition: reorderbuffer.h:99
dlist_head changes
void ReorderBufferSetBaseSnapshot(ReorderBuffer *, TransactionId, XLogRecPtr lsn, struct SnapshotData *snap)
void ReorderBufferSetRestartPoint(ReorderBuffer *, XLogRecPtr ptr)
HeapTupleData tuple
Definition: reorderbuffer.h:27
unsigned int uint32
Definition: c.h:306
XLogRecPtr final_lsn
ItemPointerData tid
ReorderBufferMessageCB message
void(* ReorderBufferCommitCB)(ReorderBuffer *rb, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
void ReorderBufferXidSetCatalogChanges(ReorderBuffer *, TransactionId xid, XLogRecPtr lsn)
void(* ReorderBufferApplyChangeCB)(ReorderBuffer *rb, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
RepOriginId origin_id
Definition: reorderbuffer.h:79
void ReorderBufferAddNewTupleCids(ReorderBuffer *, TransactionId, XLogRecPtr lsn, RelFileNode node, ItemPointerData pt, CommandId cmin, CommandId cmax, CommandId combocid)
MemoryContext context
void ReorderBufferFree(ReorderBuffer *)
ReorderBufferTXN * by_txn_last_txn
void ReorderBufferAbort(ReorderBuffer *, TransactionId, XLogRecPtr lsn)
dlist_head toplevel_by_lsn
TransactionId xid
ReorderBufferTXN * ReorderBufferGetOldestTXN(ReorderBuffer *)
void ReorderBufferAddNewCommandId(ReorderBuffer *, TransactionId, XLogRecPtr lsn, CommandId cid)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
struct ReorderBufferTupleBuf ReorderBufferTupleBuf
XLogRecPtr end_lsn
void ReorderBufferAbortOld(ReorderBuffer *, TransactionId xid)
void(* ReorderBufferMessageCB)(ReorderBuffer *rb, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, const char *message)
size_t Size
Definition: c.h:414
SharedInvalidationMessage * invalidations
void ReorderBufferReturnChange(ReorderBuffer *, ReorderBufferChange *)
dlist_head subtxns
void ReorderBufferImmediateInvalidation(ReorderBuffer *, uint32 ninvalidations, SharedInvalidationMessage *invalidations)
XLogRecPtr restart_decoding_lsn
MemoryContext tup_context
ReorderBufferBeginCB begin
MemoryContext txn_context
dlist_head tuplecids
struct ReorderBufferTXN ReorderBufferTXN
struct ReorderBufferChange ReorderBufferChange
RelFileNode relnode
Definition: reorderbuffer.h:91