PostgreSQL Source Code  git master
nbtxlog.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nbtxlog.h
4  * header file for postgres btree xlog routines
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/access/nbtxlog.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef NBTXLOG_H
14 #define NBTXLOG_H
15 
16 #include "access/xlogreader.h"
17 #include "lib/stringinfo.h"
18 #include "storage/off.h"
19 
20 /*
21  * XLOG records for btree operations
22  *
23  * XLOG allows to store some information in high 4 bits of log
24  * record xl_info field
25  */
26 #define XLOG_BTREE_INSERT_LEAF 0x00 /* add index tuple without split */
27 #define XLOG_BTREE_INSERT_UPPER 0x10 /* same, on a non-leaf page */
28 #define XLOG_BTREE_INSERT_META 0x20 /* same, plus update metapage */
29 #define XLOG_BTREE_SPLIT_L 0x30 /* add index tuple with split */
30 #define XLOG_BTREE_SPLIT_R 0x40 /* as above, new item on right */
31 /* 0x50 and 0x60 are unused */
32 #define XLOG_BTREE_DELETE 0x70 /* delete leaf index tuples for a page */
33 #define XLOG_BTREE_UNLINK_PAGE 0x80 /* delete a half-dead page */
34 #define XLOG_BTREE_UNLINK_PAGE_META 0x90 /* same, and update metapage */
35 #define XLOG_BTREE_NEWROOT 0xA0 /* new root page */
36 #define XLOG_BTREE_MARK_PAGE_HALFDEAD 0xB0 /* mark a leaf as half-dead */
37 #define XLOG_BTREE_VACUUM 0xC0 /* delete entries on a page during
38  * vacuum */
39 #define XLOG_BTREE_REUSE_PAGE 0xD0 /* old page is about to be reused from
40  * FSM */
41 
42 /*
43  * All that we need to regenerate the meta-data page
44  */
45 typedef struct xl_btree_metadata
46 {
52 
53 /*
54  * This is what we need to know about simple (without split) insert.
55  *
56  * This data record is used for INSERT_LEAF, INSERT_UPPER, INSERT_META.
57  * Note that INSERT_META implies it's not a leaf page.
58  *
59  * Backup Blk 0: original page (data contains the inserted tuple)
60  * Backup Blk 1: child's left sibling, if INSERT_UPPER or INSERT_META
61  * Backup Blk 2: xl_btree_metadata, if INSERT_META
62  */
63 typedef struct xl_btree_insert
64 {
67 
68 #define SizeOfBtreeInsert (offsetof(xl_btree_insert, offnum) + sizeof(OffsetNumber))
69 
70 /*
71  * On insert with split, we save all the items going into the right sibling
72  * so that we can restore it completely from the log record. This way takes
73  * less xlog space than the normal approach, because if we did it standardly,
74  * XLogInsert would almost always think the right page is new and store its
75  * whole page image. The left page, however, is handled in the normal
76  * incremental-update fashion.
77  *
78  * Note: the four XLOG_BTREE_SPLIT xl_info codes all use this data record.
79  * The _L and _R variants indicate whether the inserted tuple went into the
80  * left or right split page (and thus, whether newitemoff and the new item
81  * are stored or not). The _ROOT variants indicate that we are splitting
82  * the root page, and thus that a newroot record rather than an insert or
83  * split record should follow. Note that a split record never carries a
84  * metapage update --- we'll do that in the parent-level update.
85  *
86  * Backup Blk 0: original page / new left page
87  *
88  * The left page's data portion contains the new item, if it's the _L variant.
89  * (In the _R variants, the new item is one of the right page's tuples.)
90  * If level > 0, an IndexTuple representing the HIKEY of the left page
91  * follows. We don't need this on leaf pages, because it's the same as the
92  * leftmost key in the new right page.
93  *
94  * Backup Blk 1: new right page
95  *
96  * The right page's data portion contains the right page's tuples in the
97  * form used by _bt_restore_page.
98  *
99  * Backup Blk 2: next block (orig page's rightlink), if any
100  * Backup Blk 3: child's left sibling, if non-leaf split
101  */
102 typedef struct xl_btree_split
103 {
104  uint32 level; /* tree level of page being split */
105  OffsetNumber firstright; /* first item moved to right page */
106  OffsetNumber newitemoff; /* new item's offset (if placed on left page) */
108 
109 #define SizeOfBtreeSplit (offsetof(xl_btree_split, newitemoff) + sizeof(OffsetNumber))
110 
111 /*
112  * This is what we need to know about delete of individual leaf index tuples.
113  * The WAL record can represent deletion of any number of index tuples on a
114  * single index page when *not* executed by VACUUM.
115  *
116  * Backup Blk 0: index page
117  */
118 typedef struct xl_btree_delete
119 {
120  RelFileNode hnode; /* RelFileNode of the heap the index currently
121  * points at */
122  int nitems;
123 
124  /* TARGET OFFSET NUMBERS FOLLOW AT THE END */
126 
127 #define SizeOfBtreeDelete (offsetof(xl_btree_delete, nitems) + sizeof(int))
128 
129 /*
130  * This is what we need to know about page reuse within btree.
131  */
132 typedef struct xl_btree_reuse_page
133 {
138 
139 #define SizeOfBtreeReusePage (sizeof(xl_btree_reuse_page))
140 
141 /*
142  * This is what we need to know about vacuum of individual leaf index tuples.
143  * The WAL record can represent deletion of any number of index tuples on a
144  * single index page when executed by VACUUM.
145  *
146  * For MVCC scans, lastBlockVacuumed will be set to InvalidBlockNumber.
147  * For a non-MVCC index scans there is an additional correctness requirement
148  * for applying these changes during recovery, which is that we must do one
149  * of these two things for every block in the index:
150  * * lock the block for cleanup and apply any required changes
151  * * EnsureBlockUnpinned()
152  * The purpose of this is to ensure that no index scans started before we
153  * finish scanning the index are still running by the time we begin to remove
154  * heap tuples.
155  *
156  * Any changes to any one block are registered on just one WAL record. All
157  * blocks that we need to run EnsureBlockUnpinned() are listed as a block range
158  * starting from the last block vacuumed through until this one. Individual
159  * block numbers aren't given.
160  *
161  * Note that the *last* WAL record in any vacuum of an index is allowed to
162  * have a zero length array of offsets. Earlier records must have at least one.
163  */
164 typedef struct xl_btree_vacuum
165 {
167 
168  /* TARGET OFFSET NUMBERS FOLLOW */
170 
171 #define SizeOfBtreeVacuum (offsetof(xl_btree_vacuum, lastBlockVacuumed) + sizeof(BlockNumber))
172 
173 /*
174  * This is what we need to know about marking an empty branch for deletion.
175  * The target identifies the tuple removed from the parent page (note that we
176  * remove this tuple's downlink and the *following* tuple's key). Note that
177  * the leaf page is empty, so we don't need to store its content --- it is
178  * just reinitialized during recovery using the rest of the fields.
179  *
180  * Backup Blk 0: leaf block
181  * Backup Blk 1: top parent
182  */
184 {
185  OffsetNumber poffset; /* deleted tuple id in parent page */
186 
187  /* information needed to recreate the leaf page: */
188  BlockNumber leafblk; /* leaf block ultimately being deleted */
189  BlockNumber leftblk; /* leaf block's left sibling, if any */
190  BlockNumber rightblk; /* leaf block's right sibling */
191  BlockNumber topparent; /* topmost internal page in the branch */
193 
194 #define SizeOfBtreeMarkPageHalfDead (offsetof(xl_btree_mark_page_halfdead, topparent) + sizeof(BlockNumber))
195 
196 /*
197  * This is what we need to know about deletion of a btree page. Note we do
198  * not store any content for the deleted page --- it is just rewritten as empty
199  * during recovery, apart from resetting the btpo.xact.
200  *
201  * Backup Blk 0: target block being deleted
202  * Backup Blk 1: target block's left sibling, if any
203  * Backup Blk 2: target block's right sibling
204  * Backup Blk 3: leaf block (if different from target)
205  * Backup Blk 4: metapage (if rightsib becomes new fast root)
206  */
207 typedef struct xl_btree_unlink_page
208 {
209  BlockNumber leftsib; /* target block's left sibling, if any */
210  BlockNumber rightsib; /* target block's right sibling */
211 
212  /*
213  * Information needed to recreate the leaf page, when target is an
214  * internal page.
215  */
218  BlockNumber topparent; /* next child down in the branch */
219 
220  TransactionId btpo_xact; /* value of btpo.xact for use in recovery */
221  /* xl_btree_metadata FOLLOWS IF XLOG_BTREE_UNLINK_PAGE_META */
223 
224 #define SizeOfBtreeUnlinkPage (offsetof(xl_btree_unlink_page, btpo_xact) + sizeof(TransactionId))
225 
226 /*
227  * New root log record. There are zero tuples if this is to establish an
228  * empty root, or two if it is the result of splitting an old root.
229  *
230  * Note that although this implies rewriting the metadata page, we don't need
231  * an xl_btree_metadata record --- the rootblk and level are sufficient.
232  *
233  * Backup Blk 0: new root page (2 tuples as payload, if splitting old root)
234  * Backup Blk 1: left child (if splitting an old root)
235  * Backup Blk 2: metapage
236  */
237 typedef struct xl_btree_newroot
238 {
239  BlockNumber rootblk; /* location of new root (redundant with blk 0) */
240  uint32 level; /* its tree level */
242 
243 #define SizeOfBtreeNewroot (offsetof(xl_btree_newroot, level) + sizeof(uint32))
244 
245 
246 /*
247  * prototypes for functions in nbtxlog.c
248  */
249 extern void btree_redo(XLogReaderState *record);
250 extern void btree_desc(StringInfo buf, XLogReaderState *record);
251 extern const char *btree_identify(uint8 info);
252 extern void btree_mask(char *pagedata, BlockNumber blkno);
253 
254 #endif /* NBXLOG_H */
BlockNumber lastBlockVacuumed
Definition: nbtxlog.h:166
void btree_mask(char *pagedata, BlockNumber blkno)
Definition: nbtxlog.c:1033
BlockNumber rootblk
Definition: nbtxlog.h:239
void btree_desc(StringInfo buf, XLogReaderState *record)
Definition: nbtdesc.c:20
uint32 TransactionId
Definition: c.h:445
struct xl_btree_split xl_btree_split
struct xl_btree_reuse_page xl_btree_reuse_page
RelFileNode hnode
Definition: nbtxlog.h:120
struct xl_btree_metadata xl_btree_metadata
BlockNumber root
Definition: nbtxlog.h:47
unsigned char uint8
Definition: c.h:294
RelFileNode node
Definition: nbtxlog.h:134
uint32 level
Definition: nbtxlog.h:240
uint32 BlockNumber
Definition: block.h:31
struct xl_btree_newroot xl_btree_newroot
struct xl_btree_insert xl_btree_insert
uint16 OffsetNumber
Definition: off.h:24
struct xl_btree_delete xl_btree_delete
BlockNumber block
Definition: nbtxlog.h:135
const char * btree_identify(uint8 info)
Definition: nbtdesc.c:101
OffsetNumber newitemoff
Definition: nbtxlog.h:106
struct xl_btree_mark_page_halfdead xl_btree_mark_page_halfdead
static char * buf
Definition: pg_test_fsync.c:67
unsigned int uint32
Definition: c.h:296
struct xl_btree_vacuum xl_btree_vacuum
uint32 level
Definition: nbtxlog.h:104
OffsetNumber offnum
Definition: nbtxlog.h:65
OffsetNumber firstright
Definition: nbtxlog.h:105
uint32 fastlevel
Definition: nbtxlog.h:50
struct xl_btree_unlink_page xl_btree_unlink_page
uint32 level
Definition: nbtxlog.h:48
BlockNumber fastroot
Definition: nbtxlog.h:49
TransactionId latestRemovedXid
Definition: nbtxlog.h:136
void btree_redo(XLogReaderState *record)
Definition: nbtxlog.c:984