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-2018, 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 {
64 };
65 
66 /*
67  * a single 'change', can be an insert (with one tuple), an update (old, new),
68  * or a delete (old).
69  *
70  * The same struct is also used internally for other purposes but that should
71  * never be visible outside reorderbuffer.c.
72  */
73 typedef struct ReorderBufferChange
74 {
76 
77  /* The type of change. */
79 
81 
82  /*
83  * Context data for the change. Which part of the union is valid depends
84  * on action.
85  */
86  union
87  {
88  /* Old, new tuples when action == *_INSERT|UPDATE|DELETE */
89  struct
90  {
91  /* relation that has been changed */
93 
94  /* no previously reassembled toast chunks are necessary anymore */
96 
97  /* valid for DELETE || UPDATE */
99  /* valid for INSERT || UPDATE */
101  } tp;
102 
103  /*
104  * Truncate data for REORDER_BUFFER_CHANGE_TRUNCATE representing one
105  * set of relations to be truncated.
106  */
107  struct
108  {
110  bool cascade;
113  } truncate;
114 
115  /* Message with arbitrary data. */
116  struct
117  {
118  char *prefix;
120  char *message;
121  } msg;
122 
123  /* New snapshot, set when action == *_INTERNAL_SNAPSHOT */
125 
126  /*
127  * New command id for existing snapshot in a catalog changing tx. Set
128  * when action == *_INTERNAL_COMMAND_ID.
129  */
131 
132  /*
133  * New cid mapping for catalog changing transaction, set when action
134  * == *_INTERNAL_TUPLECID.
135  */
136  struct
137  {
143  } tuplecid;
144  } data;
145 
146  /*
147  * While in use this is how a change is linked into a transactions,
148  * otherwise it's the preallocated list.
149  */
152 
153 typedef struct ReorderBufferTXN
154 {
155  /*
156  * The transactions transaction id, can be a toplevel or sub xid.
157  */
159 
160  /* did the TX have catalog changes */
162 
163  /*
164  * Do we know this is a subxact?
165  */
167 
168  /*
169  * LSN of the first data carrying, WAL record with knowledge about this
170  * xid. This is allowed to *not* be first record adorned with this xid, if
171  * the previous records aren't relevant for logical decoding.
172  */
174 
175  /* ----
176  * LSN of the record that lead to this xact to be committed or
177  * aborted. This can be a
178  * * plain commit record
179  * * plain commit record, of a parent transaction
180  * * prepared transaction commit
181  * * plain abort record
182  * * prepared transaction abort
183  * * error during decoding
184  * * for a crashed transaction, the LSN of the last change, regardless of
185  * what it was.
186  * ----
187  */
189 
190  /*
191  * LSN pointing to the end of the commit record + 1.
192  */
194 
195  /*
196  * LSN of the last lsn at which snapshot information reside, so we can
197  * restart decoding from there and fully recover this transaction from
198  * WAL.
199  */
201 
202  /* origin of the change that caused this transaction */
205 
206  /*
207  * Commit time, only known when we read the actual commit record.
208  */
210 
211  /*
212  * Base snapshot or NULL.
213  */
216 
217  /*
218  * How many ReorderBufferChange's do we have in this txn.
219  *
220  * Changes in subtransactions are *not* included but tracked separately.
221  */
222  uint64 nentries;
223 
224  /*
225  * How many of the above entries are stored in memory in contrast to being
226  * spilled to disk.
227  */
228  uint64 nentries_mem;
229 
230  /*
231  * Has this transaction been spilled to disk? It's not always possible to
232  * deduce that fact by comparing nentries with nentries_mem, because e.g.
233  * subtransactions of a large transaction might get serialized together
234  * with the parent - if they're restored to memory they'd have
235  * nentries_mem == nentries.
236  */
238 
239  /*
240  * List of ReorderBufferChange structs, including new Snapshots and new
241  * CommandIds
242  */
244 
245  /*
246  * List of (relation, ctid) => (cmin, cmax) mappings for catalog tuples.
247  * Those are always assigned to the toplevel transaction. (Keep track of
248  * #entries to create a hash of the right size)
249  */
251  uint64 ntuplecids;
252 
253  /*
254  * On-demand built hash for looking up the above values.
255  */
257 
258  /*
259  * Hash containing (potentially partial) toast entries. NULL if no toast
260  * tuples have been found for the current change.
261  */
263 
264  /*
265  * non-hierarchical list of subtransactions that are *not* aborted. Only
266  * used in toplevel transactions.
267  */
270 
271  /*
272  * Stored cache invalidations. This is not a linked list because we get
273  * all the invalidations at once.
274  */
277 
278  /* ---
279  * Position in one of three lists:
280  * * list of subtransactions if we are *known* to be subxact
281  * * list of toplevel xacts (can be an as-yet unknown subxact)
282  * * list of preallocated ReorderBufferTXNs
283  * ---
284  */
286 
288 
289 /* so we can define the callbacks used inside struct ReorderBuffer itself */
291 
292 /* change callback signature */
293 typedef void (*ReorderBufferApplyChangeCB) (
294  ReorderBuffer *rb,
295  ReorderBufferTXN *txn,
296  Relation relation,
297  ReorderBufferChange *change);
298 
299 /* truncate callback signature */
301  ReorderBuffer *rb,
302  ReorderBufferTXN *txn,
303  int nrelations,
304  Relation relations[],
305  ReorderBufferChange *change);
306 
307 /* begin callback signature */
308 typedef void (*ReorderBufferBeginCB) (
309  ReorderBuffer *rb,
310  ReorderBufferTXN *txn);
311 
312 /* commit callback signature */
313 typedef void (*ReorderBufferCommitCB) (
314  ReorderBuffer *rb,
315  ReorderBufferTXN *txn,
316  XLogRecPtr commit_lsn);
317 
318 /* message callback signature */
319 typedef void (*ReorderBufferMessageCB) (
320  ReorderBuffer *rb,
321  ReorderBufferTXN *txn,
322  XLogRecPtr message_lsn,
323  bool transactional,
324  const char *prefix, Size sz,
325  const char *message);
326 
328 {
329  /*
330  * xid => ReorderBufferTXN lookup table
331  */
333 
334  /*
335  * Transactions that could be a toplevel xact, ordered by LSN of the first
336  * record bearing that xid.
337  */
339 
340  /*
341  * one-entry sized cache for by_txn. Very frequently the same txn gets
342  * looked up over and over again.
343  */
346 
347  /*
348  * Callbacks to be called when a transactions commits.
349  */
355 
356  /*
357  * Pointer that will be passed untouched to the callbacks.
358  */
360 
361  /*
362  * Saved output plugin option
363  */
365 
366  /*
367  * Private memory context.
368  */
370 
371  /*
372  * Memory contexts for specific types objects
373  */
377 
379 
380  /* buffer for disk<->memory conversions */
381  char *outbuf;
383 };
384 
385 
388 
393 
396  bool transactional, const char *prefix,
397  Size message_size, const char *message);
399  XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
400  TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn);
403  XLogRecPtr commit_lsn, XLogRecPtr end_lsn);
407 
411  CommandId cid);
414  CommandId cmin, CommandId cmax, CommandId combocid);
416  Size nmsgs, SharedInvalidationMessage *msgs);
418  SharedInvalidationMessage *invalidations);
423 
425 
427 
428 void StartupReorderBuffer(void);
429 
430 #endif
ReorderBuffer * ReorderBufferAllocate(void)
XLogRecPtr first_lsn
uint32 CommandId
Definition: c.h:488
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:98
void ReorderBufferProcessXid(ReorderBuffer *, TransactionId xid, XLogRecPtr lsn)
uint32 TransactionId
Definition: c.h:474
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 *)
unsigned int Oid
Definition: postgres_ext.h:31
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
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:325
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:80
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:433
SharedInvalidationMessage * invalidations
void ReorderBufferReturnChange(ReorderBuffer *, ReorderBufferChange *)
dlist_head subtxns
void ReorderBufferImmediateInvalidation(ReorderBuffer *, uint32 ninvalidations, SharedInvalidationMessage *invalidations)
void(* ReorderBufferApplyTruncateCB)(ReorderBuffer *rb, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
ReorderBufferApplyTruncateCB apply_truncate
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:92