]> granicus.if.org Git - postgresql/blob - src/include/access/nbtxlog.h
Make heap TID a tiebreaker nbtree index column.
[postgresql] / src / include / access / nbtxlog.h
1 /*-------------------------------------------------------------------------
2  *
3  * nbtxlog.h
4  *        header file for postgres btree xlog routines
5  *
6  * Portions Copyright (c) 1996-2019, 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 #define XLOG_BTREE_META_CLEANUP 0xE0    /* update cleanup-related data in the
42                                                                                  * metapage */
43
44 /*
45  * All that we need to regenerate the meta-data page
46  */
47 typedef struct xl_btree_metadata
48 {
49         uint32          version;
50         BlockNumber root;
51         uint32          level;
52         BlockNumber fastroot;
53         uint32          fastlevel;
54         TransactionId oldest_btpo_xact;
55         float8          last_cleanup_num_heap_tuples;
56 } xl_btree_metadata;
57
58 /*
59  * This is what we need to know about simple (without split) insert.
60  *
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.
63  *
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
67  */
68 typedef struct xl_btree_insert
69 {
70         OffsetNumber offnum;
71 } xl_btree_insert;
72
73 #define SizeOfBtreeInsert       (offsetof(xl_btree_insert, offnum) + sizeof(OffsetNumber))
74
75 /*
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.
82  *
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
91  * page).
92  *
93  * Backup Blk 0: original page / new left page
94  *
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
97  * either variant.
98  *
99  * Backup Blk 1: new right page
100  *
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.
107  *
108  * Backup Blk 2: next block (orig page's rightlink), if any
109  * Backup Blk 3: child's left sibling, if non-leaf split
110  */
111 typedef struct xl_btree_split
112 {
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) */
116 } xl_btree_split;
117
118 #define SizeOfBtreeSplit        (offsetof(xl_btree_split, newitemoff) + sizeof(OffsetNumber))
119
120 /*
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.
124  *
125  * Backup Blk 0: index page
126  */
127 typedef struct xl_btree_delete
128 {
129         RelFileNode hnode;                      /* RelFileNode of the heap the index currently
130                                                                  * points at */
131         int                     nitems;
132
133         /* TARGET OFFSET NUMBERS FOLLOW AT THE END */
134 } xl_btree_delete;
135
136 #define SizeOfBtreeDelete       (offsetof(xl_btree_delete, nitems) + sizeof(int))
137
138 /*
139  * This is what we need to know about page reuse within btree.
140  */
141 typedef struct xl_btree_reuse_page
142 {
143         RelFileNode node;
144         BlockNumber block;
145         TransactionId latestRemovedXid;
146 } xl_btree_reuse_page;
147
148 #define SizeOfBtreeReusePage    (sizeof(xl_btree_reuse_page))
149
150 /*
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.
154  *
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
163  * heap tuples.
164  *
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.
169  *
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.
172  */
173 typedef struct xl_btree_vacuum
174 {
175         BlockNumber lastBlockVacuumed;
176
177         /* TARGET OFFSET NUMBERS FOLLOW */
178 } xl_btree_vacuum;
179
180 #define SizeOfBtreeVacuum       (offsetof(xl_btree_vacuum, lastBlockVacuumed) + sizeof(BlockNumber))
181
182 /*
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.
188  *
189  * Backup Blk 0: leaf block
190  * Backup Blk 1: top parent
191  */
192 typedef struct xl_btree_mark_page_halfdead
193 {
194         OffsetNumber poffset;           /* deleted tuple id in parent page */
195
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;
202
203 #define SizeOfBtreeMarkPageHalfDead (offsetof(xl_btree_mark_page_halfdead, topparent) + sizeof(BlockNumber))
204
205 /*
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.
209  *
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)
215  */
216 typedef struct xl_btree_unlink_page
217 {
218         BlockNumber leftsib;            /* target block's left sibling, if any */
219         BlockNumber rightsib;           /* target block's right sibling */
220
221         /*
222          * Information needed to recreate the leaf page, when target is an
223          * internal page.
224          */
225         BlockNumber leafleftsib;
226         BlockNumber leafrightsib;
227         BlockNumber topparent;          /* next child down in the branch */
228
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;
232
233 #define SizeOfBtreeUnlinkPage   (offsetof(xl_btree_unlink_page, btpo_xact) + sizeof(TransactionId))
234
235 /*
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.
238  *
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.
241  *
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
245  */
246 typedef struct xl_btree_newroot
247 {
248         BlockNumber rootblk;            /* location of new root (redundant with blk 0) */
249         uint32          level;                  /* its tree level */
250 } xl_btree_newroot;
251
252 #define SizeOfBtreeNewroot      (offsetof(xl_btree_newroot, level) + sizeof(uint32))
253
254
255 /*
256  * prototypes for functions in nbtxlog.c
257  */
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);
262
263 #endif                                                  /* NBXLOG_H */