1 /*-------------------------------------------------------------------------
4 * header file for postgres btree xlog routines
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * src/include/access/nbtxlog.h
11 *-------------------------------------------------------------------------
16 #include "access/xlogreader.h"
17 #include "lib/stringinfo.h"
18 #include "storage/off.h"
21 * XLOG records for btree operations
23 * XLOG allows to store some information in high 4 bits of log
24 * record xl_info field
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
39 #define XLOG_BTREE_REUSE_PAGE 0xD0 /* old page is about to be reused from
41 #define XLOG_BTREE_META_CLEANUP 0xE0 /* update cleanup-related data in the
45 * All that we need to regenerate the meta-data page
47 typedef struct xl_btree_metadata
54 TransactionId oldest_btpo_xact;
55 float8 last_cleanup_num_heap_tuples;
59 * This is what we need to know about simple (without split) insert.
61 * This data record is used for INSERT_LEAF, INSERT_UPPER, INSERT_META.
62 * Note that INSERT_META implies it's not a leaf page.
64 * Backup Blk 0: original page (data contains the inserted tuple)
65 * Backup Blk 1: child's left sibling, if INSERT_UPPER or INSERT_META
66 * Backup Blk 2: xl_btree_metadata, if INSERT_META
68 typedef struct xl_btree_insert
73 #define SizeOfBtreeInsert (offsetof(xl_btree_insert, offnum) + sizeof(OffsetNumber))
76 * On insert with split, we save all the items going into the right sibling
77 * so that we can restore it completely from the log record. This way takes
78 * less xlog space than the normal approach, because if we did it standardly,
79 * XLogInsert would almost always think the right page is new and store its
80 * whole page image. The left page, however, is handled in the normal
81 * incremental-update fashion.
83 * Note: XLOG_BTREE_SPLIT_L and XLOG_BTREE_SPLIT_R share this data record.
84 * There are two variants to indicate whether the inserted tuple went into the
85 * left or right split page (and thus, whether newitemoff and the new item are
86 * stored or not). We always log the left page high key because suffix
87 * truncation can generate a new leaf high key using user-defined code. This
88 * is also necessary on internal pages, since the first right item that the
89 * left page's high key was based on will have been truncated to zero
90 * attributes in the right page (the original is unavailable from the right
93 * Backup Blk 0: original page / new left page
95 * The left page's data portion contains the new item, if it's the _L variant.
96 * An IndexTuple representing the high key of the left page must follow with
99 * Backup Blk 1: new right page
101 * The right page's data portion contains the right page's tuples in the form
102 * used by _bt_restore_page. This includes the new item, if it's the _R
103 * variant. The right page's tuples also include the right page's high key
104 * with either variant (moved from the left/original page during the split),
105 * unless the split happened to be of the rightmost page on its level, where
106 * there is no high key for new right page.
108 * Backup Blk 2: next block (orig page's rightlink), if any
109 * Backup Blk 3: child's left sibling, if non-leaf split
111 typedef struct xl_btree_split
113 uint32 level; /* tree level of page being split */
114 OffsetNumber firstright; /* first item moved to right page */
115 OffsetNumber newitemoff; /* new item's offset (if placed on left page) */
118 #define SizeOfBtreeSplit (offsetof(xl_btree_split, newitemoff) + sizeof(OffsetNumber))
121 * This is what we need to know about delete of individual leaf index tuples.
122 * The WAL record can represent deletion of any number of index tuples on a
123 * single index page when *not* executed by VACUUM.
125 * Backup Blk 0: index page
127 typedef struct xl_btree_delete
129 RelFileNode hnode; /* RelFileNode of the heap the index currently
133 /* TARGET OFFSET NUMBERS FOLLOW AT THE END */
136 #define SizeOfBtreeDelete (offsetof(xl_btree_delete, nitems) + sizeof(int))
139 * This is what we need to know about page reuse within btree.
141 typedef struct xl_btree_reuse_page
145 TransactionId latestRemovedXid;
146 } xl_btree_reuse_page;
148 #define SizeOfBtreeReusePage (sizeof(xl_btree_reuse_page))
151 * This is what we need to know about vacuum of individual leaf index tuples.
152 * The WAL record can represent deletion of any number of index tuples on a
153 * single index page when executed by VACUUM.
155 * For MVCC scans, lastBlockVacuumed will be set to InvalidBlockNumber.
156 * For a non-MVCC index scans there is an additional correctness requirement
157 * for applying these changes during recovery, which is that we must do one
158 * of these two things for every block in the index:
159 * * lock the block for cleanup and apply any required changes
160 * * EnsureBlockUnpinned()
161 * The purpose of this is to ensure that no index scans started before we
162 * finish scanning the index are still running by the time we begin to remove
165 * Any changes to any one block are registered on just one WAL record. All
166 * blocks that we need to run EnsureBlockUnpinned() are listed as a block range
167 * starting from the last block vacuumed through until this one. Individual
168 * block numbers aren't given.
170 * Note that the *last* WAL record in any vacuum of an index is allowed to
171 * have a zero length array of offsets. Earlier records must have at least one.
173 typedef struct xl_btree_vacuum
175 BlockNumber lastBlockVacuumed;
177 /* TARGET OFFSET NUMBERS FOLLOW */
180 #define SizeOfBtreeVacuum (offsetof(xl_btree_vacuum, lastBlockVacuumed) + sizeof(BlockNumber))
183 * This is what we need to know about marking an empty branch for deletion.
184 * The target identifies the tuple removed from the parent page (note that we
185 * remove this tuple's downlink and the *following* tuple's key). Note that
186 * the leaf page is empty, so we don't need to store its content --- it is
187 * just reinitialized during recovery using the rest of the fields.
189 * Backup Blk 0: leaf block
190 * Backup Blk 1: top parent
192 typedef struct xl_btree_mark_page_halfdead
194 OffsetNumber poffset; /* deleted tuple id in parent page */
196 /* information needed to recreate the leaf page: */
197 BlockNumber leafblk; /* leaf block ultimately being deleted */
198 BlockNumber leftblk; /* leaf block's left sibling, if any */
199 BlockNumber rightblk; /* leaf block's right sibling */
200 BlockNumber topparent; /* topmost internal page in the branch */
201 } xl_btree_mark_page_halfdead;
203 #define SizeOfBtreeMarkPageHalfDead (offsetof(xl_btree_mark_page_halfdead, topparent) + sizeof(BlockNumber))
206 * This is what we need to know about deletion of a btree page. Note we do
207 * not store any content for the deleted page --- it is just rewritten as empty
208 * during recovery, apart from resetting the btpo.xact.
210 * Backup Blk 0: target block being deleted
211 * Backup Blk 1: target block's left sibling, if any
212 * Backup Blk 2: target block's right sibling
213 * Backup Blk 3: leaf block (if different from target)
214 * Backup Blk 4: metapage (if rightsib becomes new fast root)
216 typedef struct xl_btree_unlink_page
218 BlockNumber leftsib; /* target block's left sibling, if any */
219 BlockNumber rightsib; /* target block's right sibling */
222 * Information needed to recreate the leaf page, when target is an
225 BlockNumber leafleftsib;
226 BlockNumber leafrightsib;
227 BlockNumber topparent; /* next child down in the branch */
229 TransactionId btpo_xact; /* value of btpo.xact for use in recovery */
230 /* xl_btree_metadata FOLLOWS IF XLOG_BTREE_UNLINK_PAGE_META */
231 } xl_btree_unlink_page;
233 #define SizeOfBtreeUnlinkPage (offsetof(xl_btree_unlink_page, btpo_xact) + sizeof(TransactionId))
236 * New root log record. There are zero tuples if this is to establish an
237 * empty root, or two if it is the result of splitting an old root.
239 * Note that although this implies rewriting the metadata page, we don't need
240 * an xl_btree_metadata record --- the rootblk and level are sufficient.
242 * Backup Blk 0: new root page (2 tuples as payload, if splitting old root)
243 * Backup Blk 1: left child (if splitting an old root)
244 * Backup Blk 2: metapage
246 typedef struct xl_btree_newroot
248 BlockNumber rootblk; /* location of new root (redundant with blk 0) */
249 uint32 level; /* its tree level */
252 #define SizeOfBtreeNewroot (offsetof(xl_btree_newroot, level) + sizeof(uint32))
256 * prototypes for functions in nbtxlog.c
258 extern void btree_redo(XLogReaderState *record);
259 extern void btree_desc(StringInfo buf, XLogReaderState *record);
260 extern const char *btree_identify(uint8 info);
261 extern void btree_mask(char *pagedata, BlockNumber blkno);
263 #endif /* NBXLOG_H */