PostgreSQL Source Code  git master
heapam_xlog.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * heapam_xlog.h
4  * POSTGRES heap access XLOG definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/access/heapam_xlog.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef HEAPAM_XLOG_H
15 #define HEAPAM_XLOG_H
16 
17 #include "access/htup.h"
18 #include "access/xlogreader.h"
19 #include "lib/stringinfo.h"
20 #include "storage/buf.h"
21 #include "storage/bufpage.h"
22 #include "storage/relfilenode.h"
23 #include "utils/relcache.h"
24 
25 
26 /*
27  * WAL record definitions for heapam.c's WAL operations
28  *
29  * XLOG allows to store some information in high 4 bits of log
30  * record xl_info field. We use 3 for opcode and one for init bit.
31  */
32 #define XLOG_HEAP_INSERT 0x00
33 #define XLOG_HEAP_DELETE 0x10
34 #define XLOG_HEAP_UPDATE 0x20
35 #define XLOG_HEAP_TRUNCATE 0x30
36 #define XLOG_HEAP_HOT_UPDATE 0x40
37 #define XLOG_HEAP_CONFIRM 0x50
38 #define XLOG_HEAP_LOCK 0x60
39 #define XLOG_HEAP_INPLACE 0x70
40 
41 #define XLOG_HEAP_OPMASK 0x70
42 /*
43  * When we insert 1st item on new page in INSERT, UPDATE, HOT_UPDATE,
44  * or MULTI_INSERT, we can (and we do) restore entire page in redo
45  */
46 #define XLOG_HEAP_INIT_PAGE 0x80
47 /*
48  * We ran out of opcodes, so heapam.c now has a second RmgrId. These opcodes
49  * are associated with RM_HEAP2_ID, but are not logically different from
50  * the ones above associated with RM_HEAP_ID. XLOG_HEAP_OPMASK applies to
51  * these, too.
52  */
53 #define XLOG_HEAP2_REWRITE 0x00
54 #define XLOG_HEAP2_CLEAN 0x10
55 #define XLOG_HEAP2_FREEZE_PAGE 0x20
56 #define XLOG_HEAP2_CLEANUP_INFO 0x30
57 #define XLOG_HEAP2_VISIBLE 0x40
58 #define XLOG_HEAP2_MULTI_INSERT 0x50
59 #define XLOG_HEAP2_LOCK_UPDATED 0x60
60 #define XLOG_HEAP2_NEW_CID 0x70
61 
62 /*
63  * xl_heap_insert/xl_heap_multi_insert flag values, 8 bits are available.
64  */
65 /* PD_ALL_VISIBLE was cleared */
66 #define XLH_INSERT_ALL_VISIBLE_CLEARED (1<<0)
67 #define XLH_INSERT_LAST_IN_MULTI (1<<1)
68 #define XLH_INSERT_IS_SPECULATIVE (1<<2)
69 #define XLH_INSERT_CONTAINS_NEW_TUPLE (1<<3)
70 #define XLH_INSERT_ON_TOAST_RELATION (1<<4)
71 
72 /*
73  * xl_heap_update flag values, 8 bits are available.
74  */
75 /* PD_ALL_VISIBLE was cleared */
76 #define XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED (1<<0)
77 /* PD_ALL_VISIBLE was cleared in the 2nd page */
78 #define XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED (1<<1)
79 #define XLH_UPDATE_CONTAINS_OLD_TUPLE (1<<2)
80 #define XLH_UPDATE_CONTAINS_OLD_KEY (1<<3)
81 #define XLH_UPDATE_CONTAINS_NEW_TUPLE (1<<4)
82 #define XLH_UPDATE_PREFIX_FROM_OLD (1<<5)
83 #define XLH_UPDATE_SUFFIX_FROM_OLD (1<<6)
84 
85 /* convenience macro for checking whether any form of old tuple was logged */
86 #define XLH_UPDATE_CONTAINS_OLD \
87  (XLH_UPDATE_CONTAINS_OLD_TUPLE | XLH_UPDATE_CONTAINS_OLD_KEY)
88 
89 /*
90  * xl_heap_delete flag values, 8 bits are available.
91  */
92 /* PD_ALL_VISIBLE was cleared */
93 #define XLH_DELETE_ALL_VISIBLE_CLEARED (1<<0)
94 #define XLH_DELETE_CONTAINS_OLD_TUPLE (1<<1)
95 #define XLH_DELETE_CONTAINS_OLD_KEY (1<<2)
96 #define XLH_DELETE_IS_SUPER (1<<3)
97 #define XLH_DELETE_IS_PARTITION_MOVE (1<<4)
98 
99 /* convenience macro for checking whether any form of old tuple was logged */
100 #define XLH_DELETE_CONTAINS_OLD \
101  (XLH_DELETE_CONTAINS_OLD_TUPLE | XLH_DELETE_CONTAINS_OLD_KEY)
102 
103 /* This is what we need to know about delete */
104 typedef struct xl_heap_delete
105 {
106  TransactionId xmax; /* xmax of the deleted tuple */
107  OffsetNumber offnum; /* deleted tuple's offset */
108  uint8 infobits_set; /* infomask bits */
111 
112 #define SizeOfHeapDelete (offsetof(xl_heap_delete, flags) + sizeof(uint8))
113 
114 /*
115  * xl_heap_truncate flag values, 8 bits are available.
116  */
117 #define XLH_TRUNCATE_CASCADE (1<<0)
118 #define XLH_TRUNCATE_RESTART_SEQS (1<<1)
119 
120 /*
121  * For truncate we list all truncated relids in an array, followed by all
122  * sequence relids that need to be restarted, if any.
123  * All rels are always within the same database, so we just list dbid once.
124  */
125 typedef struct xl_heap_truncate
126 {
132 
133 #define SizeOfHeapTruncate (offsetof(xl_heap_truncate, relids))
134 
135 /*
136  * We don't store the whole fixed part (HeapTupleHeaderData) of an inserted
137  * or updated tuple in WAL; we can save a few bytes by reconstructing the
138  * fields that are available elsewhere in the WAL record, or perhaps just
139  * plain needn't be reconstructed. These are the fields we must store.
140  */
141 typedef struct xl_heap_header
142 {
147 
148 #define SizeOfHeapHeader (offsetof(xl_heap_header, t_hoff) + sizeof(uint8))
149 
150 /* This is what we need to know about insert */
151 typedef struct xl_heap_insert
152 {
153  OffsetNumber offnum; /* inserted tuple's offset */
155 
156  /* xl_heap_header & TUPLE DATA in backup block 0 */
158 
159 #define SizeOfHeapInsert (offsetof(xl_heap_insert, flags) + sizeof(uint8))
160 
161 /*
162  * This is what we need to know about a multi-insert.
163  *
164  * The main data of the record consists of this xl_heap_multi_insert header.
165  * 'offsets' array is omitted if the whole page is reinitialized
166  * (XLOG_HEAP_INIT_PAGE).
167  *
168  * In block 0's data portion, there is an xl_multi_insert_tuple struct,
169  * followed by the tuple data for each tuple. There is padding to align
170  * each xl_multi_insert_tuple struct.
171  */
172 typedef struct xl_heap_multi_insert
173 {
178 
179 #define SizeOfHeapMultiInsert offsetof(xl_heap_multi_insert, offsets)
180 
181 typedef struct xl_multi_insert_tuple
182 {
183  uint16 datalen; /* size of tuple data that follows */
187  /* TUPLE DATA FOLLOWS AT END OF STRUCT */
189 
190 #define SizeOfMultiInsertTuple (offsetof(xl_multi_insert_tuple, t_hoff) + sizeof(uint8))
191 
192 /*
193  * This is what we need to know about update|hot_update
194  *
195  * Backup blk 0: new page
196  *
197  * If XLH_UPDATE_PREFIX_FROM_OLD or XLH_UPDATE_SUFFIX_FROM_OLD flags are set,
198  * the prefix and/or suffix come first, as one or two uint16s.
199  *
200  * After that, xl_heap_header and new tuple data follow. The new tuple
201  * data doesn't include the prefix and suffix, which are copied from the
202  * old tuple on replay.
203  *
204  * If XLH_UPDATE_CONTAINS_NEW_TUPLE flag is given, the tuple data is
205  * included even if a full-page image was taken.
206  *
207  * Backup blk 1: old page, if different. (no data, just a reference to the blk)
208  */
209 typedef struct xl_heap_update
210 {
211  TransactionId old_xmax; /* xmax of the old tuple */
212  OffsetNumber old_offnum; /* old tuple's offset */
213  uint8 old_infobits_set; /* infomask bits to set on old tuple */
215  TransactionId new_xmax; /* xmax of the new tuple */
216  OffsetNumber new_offnum; /* new tuple's offset */
217 
218  /*
219  * If XLH_UPDATE_CONTAINS_OLD_TUPLE or XLH_UPDATE_CONTAINS_OLD_KEY flags
220  * are set, xl_heap_header and tuple data for the old tuple follow.
221  */
223 
224 #define SizeOfHeapUpdate (offsetof(xl_heap_update, new_offnum) + sizeof(OffsetNumber))
225 
226 /*
227  * This is what we need to know about vacuum page cleanup/redirect
228  *
229  * The array of OffsetNumbers following the fixed part of the record contains:
230  * * for each redirected item: the item offset, then the offset redirected to
231  * * for each now-dead item: the item offset
232  * * for each now-unused item: the item offset
233  * The total number of OffsetNumbers is therefore 2*nredirected+ndead+nunused.
234  * Note that nunused is not explicitly stored, but may be found by reference
235  * to the total record length.
236  */
237 typedef struct xl_heap_clean
238 {
242  /* OFFSET NUMBERS are in the block reference 0 */
243 } xl_heap_clean;
244 
245 #define SizeOfHeapClean (offsetof(xl_heap_clean, ndead) + sizeof(uint16))
246 
247 /*
248  * Cleanup_info is required in some cases during a lazy VACUUM.
249  * Used for reporting the results of HeapTupleHeaderAdvanceLatestRemovedXid()
250  * see vacuumlazy.c for full explanation
251  */
252 typedef struct xl_heap_cleanup_info
253 {
257 
258 #define SizeOfHeapCleanupInfo (sizeof(xl_heap_cleanup_info))
259 
260 /* flags for infobits_set */
261 #define XLHL_XMAX_IS_MULTI 0x01
262 #define XLHL_XMAX_LOCK_ONLY 0x02
263 #define XLHL_XMAX_EXCL_LOCK 0x04
264 #define XLHL_XMAX_KEYSHR_LOCK 0x08
265 #define XLHL_KEYS_UPDATED 0x10
266 
267 /* flag bits for xl_heap_lock / xl_heap_lock_updated's flag field */
268 #define XLH_LOCK_ALL_FROZEN_CLEARED 0x01
269 
270 /* This is what we need to know about lock */
271 typedef struct xl_heap_lock
272 {
273  TransactionId locking_xid; /* might be a MultiXactId not xid */
274  OffsetNumber offnum; /* locked tuple's offset on page */
275  int8 infobits_set; /* infomask and infomask2 bits to set */
276  uint8 flags; /* XLH_LOCK_* flag bits */
277 } xl_heap_lock;
278 
279 #define SizeOfHeapLock (offsetof(xl_heap_lock, flags) + sizeof(int8))
280 
281 /* This is what we need to know about locking an updated version of a row */
282 typedef struct xl_heap_lock_updated
283 {
289 
290 #define SizeOfHeapLockUpdated (offsetof(xl_heap_lock_updated, flags) + sizeof(uint8))
291 
292 /* This is what we need to know about confirmation of speculative insertion */
293 typedef struct xl_heap_confirm
294 {
295  OffsetNumber offnum; /* confirmed tuple's offset on page */
297 
298 #define SizeOfHeapConfirm (offsetof(xl_heap_confirm, offnum) + sizeof(OffsetNumber))
299 
300 /* This is what we need to know about in-place update */
301 typedef struct xl_heap_inplace
302 {
303  OffsetNumber offnum; /* updated tuple's offset on page */
304  /* TUPLE DATA FOLLOWS AT END OF STRUCT */
306 
307 #define SizeOfHeapInplace (offsetof(xl_heap_inplace, offnum) + sizeof(OffsetNumber))
308 
309 /*
310  * This struct represents a 'freeze plan', which is what we need to know about
311  * a single tuple being frozen during vacuum.
312  */
313 /* 0x01 was XLH_FREEZE_XMIN */
314 #define XLH_FREEZE_XVAC 0x02
315 #define XLH_INVALID_XVAC 0x04
316 
317 typedef struct xl_heap_freeze_tuple
318 {
325 
326 /*
327  * This is what we need to know about a block being frozen during vacuum
328  *
329  * Backup block 0's data contains an array of xl_heap_freeze_tuple structs,
330  * one for each tuple.
331  */
332 typedef struct xl_heap_freeze_page
333 {
337 
338 #define SizeOfHeapFreezePage (offsetof(xl_heap_freeze_page, ntuples) + sizeof(uint16))
339 
340 /*
341  * This is what we need to know about setting a visibility map bit
342  *
343  * Backup blk 0: visibility map buffer
344  * Backup blk 1: heap buffer
345  */
346 typedef struct xl_heap_visible
347 {
351 
352 #define SizeOfHeapVisible (offsetof(xl_heap_visible, flags) + sizeof(uint8))
353 
354 typedef struct xl_heap_new_cid
355 {
356  /*
357  * store toplevel xid so we don't have to merge cids from different
358  * transactions
359  */
363  CommandId combocid; /* just for debugging */
364 
365  /*
366  * Store the relfilenode/ctid pair to facilitate lookups.
367  */
371 
372 #define SizeOfHeapNewCid (offsetof(xl_heap_new_cid, target_tid) + sizeof(ItemPointerData))
373 
374 /* logical rewrite xlog record header */
376 {
377  TransactionId mapped_xid; /* xid that might need to see the row */
378  Oid mapped_db; /* DbOid or InvalidOid for shared rels */
379  Oid mapped_rel; /* Oid of the mapped relation */
380  off_t offset; /* How far have we written so far */
381  uint32 num_mappings; /* Number of in-memory mappings */
382  XLogRecPtr start_lsn; /* Insert LSN at begin of rewrite */
384 
386  TransactionId *latestRemovedXid);
387 
388 extern void heap_redo(XLogReaderState *record);
389 extern void heap_desc(StringInfo buf, XLogReaderState *record);
390 extern const char *heap_identify(uint8 info);
391 extern void heap_mask(char *pagedata, BlockNumber blkno);
392 extern void heap2_redo(XLogReaderState *record);
393 extern void heap2_desc(StringInfo buf, XLogReaderState *record);
394 extern const char *heap2_identify(uint8 info);
396 
398  TransactionId latestRemovedXid);
399 extern XLogRecPtr log_heap_clean(Relation reln, Buffer buffer,
400  OffsetNumber *redirected, int nredirected,
401  OffsetNumber *nowdead, int ndead,
402  OffsetNumber *nowunused, int nunused,
403  TransactionId latestRemovedXid);
404 extern XLogRecPtr log_heap_freeze(Relation reln, Buffer buffer,
405  TransactionId cutoff_xid, xl_heap_freeze_tuple *tuples,
406  int ntuples);
408  TransactionId relfrozenxid,
409  TransactionId relminmxid,
410  TransactionId cutoff_xid,
411  TransactionId cutoff_multi,
413  bool *totally_frozen);
415  xl_heap_freeze_tuple *xlrec_tp);
416 extern XLogRecPtr log_heap_visible(RelFileNode rnode, Buffer heap_buffer,
417  Buffer vm_buffer, TransactionId cutoff_xid, uint8 flags);
418 
419 #endif /* HEAPAM_XLOG_H */
uint32 CommandId
Definition: c.h:535
struct xl_heap_freeze_page xl_heap_freeze_page
uint8 old_infobits_set
Definition: heapam_xlog.h:213
OffsetNumber offnum
Definition: heapam_xlog.h:295
OffsetNumber offnum
Definition: heapam_xlog.h:274
OffsetNumber offset
Definition: heapam_xlog.h:320
XLogRecPtr log_heap_clean(Relation reln, Buffer buffer, OffsetNumber *redirected, int nredirected, OffsetNumber *nowdead, int ndead, OffsetNumber *nowunused, int nunused, TransactionId latestRemovedXid)
Definition: heapam.c:7173
TransactionId locking_xid
Definition: heapam_xlog.h:273
uint32 TransactionId
Definition: c.h:521
void heap_xlog_logical_rewrite(XLogReaderState *r)
Definition: rewriteheap.c:1112
uint16 nredirected
Definition: heapam_xlog.h:240
CommandId combocid
Definition: heapam_xlog.h:363
struct xl_heap_multi_insert xl_heap_multi_insert
TransactionId cutoff_xid
Definition: heapam_xlog.h:334
void heap_desc(StringInfo buf, XLogReaderState *record)
Definition: heapdesc.c:35
struct xl_heap_insert xl_heap_insert
unsigned char uint8
Definition: c.h:373
struct xl_multi_insert_tuple xl_multi_insert_tuple
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:284
uint16 t_infomask2
Definition: heapam_xlog.h:143
CommandId cmax
Definition: heapam_xlog.h:362
ItemPointerData target_tid
Definition: heapam_xlog.h:369
uint32 BlockNumber
Definition: block.h:31
OffsetNumber old_offnum
Definition: heapam_xlog.h:212
unsigned int Oid
Definition: postgres_ext.h:31
OffsetNumber offnum
Definition: heapam_xlog.h:285
struct xl_heap_lock xl_heap_lock
struct xl_heap_confirm xl_heap_confirm
struct xl_heap_inplace xl_heap_inplace
OffsetNumber offnum
Definition: heapam_xlog.h:107
struct xl_heap_rewrite_mapping xl_heap_rewrite_mapping
uint16 OffsetNumber
Definition: off.h:24
unsigned short uint16
Definition: c.h:374
struct xl_heap_cleanup_info xl_heap_cleanup_info
XLogRecPtr log_heap_visible(RelFileNode rnode, Buffer heap_buffer, Buffer vm_buffer, TransactionId cutoff_xid, uint8 flags)
Definition: heapam.c:7265
TransactionId new_xmax
Definition: heapam_xlog.h:215
int8 infobits_set
Definition: heapam_xlog.h:275
TransactionId cutoff_xid
Definition: heapam_xlog.h:348
TransactionId xmax
Definition: heapam_xlog.h:106
XLogRecPtr log_heap_cleanup_info(RelFileNode rnode, TransactionId latestRemovedXid)
Definition: heapam.c:7144
static char * buf
Definition: pg_test_fsync.c:68
struct xl_heap_freeze_tuple xl_heap_freeze_tuple
struct xl_heap_delete xl_heap_delete
void heap2_redo(XLogReaderState *record)
Definition: heapam.c:8880
unsigned int uint32
Definition: c.h:375
TransactionId xmax
Definition: heapam_xlog.h:284
signed char int8
Definition: c.h:361
RelFileNode target_node
Definition: heapam_xlog.h:368
struct xl_heap_lock_updated xl_heap_lock_updated
struct xl_heap_header xl_heap_header
struct xl_heap_visible xl_heap_visible
OffsetNumber offnum
Definition: heapam_xlog.h:303
void HeapTupleHeaderAdvanceLatestRemovedXid(HeapTupleHeader tuple, TransactionId *latestRemovedXid)
Definition: heapam.c:6903
struct xl_heap_new_cid xl_heap_new_cid
void heap_redo(XLogReaderState *record)
Definition: heapam.c:8834
TransactionId mapped_xid
Definition: heapam_xlog.h:377
void heap_execute_freeze_tuple(HeapTupleHeader tuple, xl_heap_freeze_tuple *xlrec_tp)
Definition: heapam.c:6355
void heap2_desc(StringInfo buf, XLogReaderState *record)
Definition: heapdesc.c:118
uint64 XLogRecPtr
Definition: xlogdefs.h:21
uint8 infobits_set
Definition: heapam_xlog.h:108
const char * heap_identify(uint8 info)
Definition: heapdesc.c:181
uint16 t_infomask
Definition: heapam_xlog.h:144
const char * heap2_identify(uint8 info)
Definition: heapdesc.c:226
bool heap_prepare_freeze_tuple(HeapTupleHeader tuple, TransactionId relfrozenxid, TransactionId relminmxid, TransactionId cutoff_xid, TransactionId cutoff_multi, xl_heap_freeze_tuple *frz, bool *totally_frozen)
Definition: heapam.c:6126
TransactionId latestRemovedXid
Definition: heapam_xlog.h:239
void heap_mask(char *pagedata, BlockNumber blkno)
Definition: heapam.c:8923
TransactionId xmax
Definition: heapam_xlog.h:319
struct xl_heap_update xl_heap_update
CommandId cmin
Definition: heapam_xlog.h:361
OffsetNumber new_offnum
Definition: heapam_xlog.h:216
int Buffer
Definition: buf.h:23
TransactionId latestRemovedXid
Definition: heapam_xlog.h:255
OffsetNumber offnum
Definition: heapam_xlog.h:153
XLogRecPtr log_heap_freeze(Relation reln, Buffer buffer, TransactionId cutoff_xid, xl_heap_freeze_tuple *tuples, int ntuples)
Definition: heapam.c:7224
TransactionId old_xmax
Definition: heapam_xlog.h:211
struct xl_heap_clean xl_heap_clean
struct xl_heap_truncate xl_heap_truncate
TransactionId top_xid
Definition: heapam_xlog.h:360