PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
bufpage.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * bufpage.h
4  * Standard POSTGRES buffer page definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/bufpage.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef BUFPAGE_H
15 #define BUFPAGE_H
16 
17 #include "access/xlogdefs.h"
18 #include "storage/block.h"
19 #include "storage/item.h"
20 #include "storage/off.h"
21 
22 /*
23  * A postgres disk page is an abstraction layered on top of a postgres
24  * disk block (which is simply a unit of i/o, see block.h).
25  *
26  * specifically, while a disk block can be unformatted, a postgres
27  * disk page is always a slotted page of the form:
28  *
29  * +----------------+---------------------------------+
30  * | PageHeaderData | linp1 linp2 linp3 ... |
31  * +-----------+----+---------------------------------+
32  * | ... linpN | |
33  * +-----------+--------------------------------------+
34  * | ^ pd_lower |
35  * | |
36  * | v pd_upper |
37  * +-------------+------------------------------------+
38  * | | tupleN ... |
39  * +-------------+------------------+-----------------+
40  * | ... tuple3 tuple2 tuple1 | "special space" |
41  * +--------------------------------+-----------------+
42  * ^ pd_special
43  *
44  * a page is full when nothing can be added between pd_lower and
45  * pd_upper.
46  *
47  * all blocks written out by an access method must be disk pages.
48  *
49  * EXCEPTIONS:
50  *
51  * obviously, a page is not formatted before it is initialized by
52  * a call to PageInit.
53  *
54  * NOTES:
55  *
56  * linp1..N form an ItemId array. ItemPointers point into this array
57  * rather than pointing directly to a tuple. Note that OffsetNumbers
58  * conventionally start at 1, not 0.
59  *
60  * tuple1..N are added "backwards" on the page. because a tuple's
61  * ItemPointer points to its ItemId entry rather than its actual
62  * byte-offset position, tuples can be physically shuffled on a page
63  * whenever the need arises.
64  *
65  * AM-generic per-page information is kept in PageHeaderData.
66  *
67  * AM-specific per-page data (if any) is kept in the area marked "special
68  * space"; each AM has an "opaque" structure defined somewhere that is
69  * stored as the page trailer. an access method should always
70  * initialize its pages with PageInit and then set its own opaque
71  * fields.
72  */
73 
74 typedef Pointer Page;
75 
76 
77 /*
78  * location (byte offset) within a page.
79  *
80  * note that this is actually limited to 2^15 because we have limited
81  * ItemIdData.lp_off and ItemIdData.lp_len to 15 bits (see itemid.h).
82  */
84 
85 
86 /*
87  * For historical reasons, the 64-bit LSN value is stored as two 32-bit
88  * values.
89  */
90 typedef struct
91 {
92  uint32 xlogid; /* high bits */
93  uint32 xrecoff; /* low bits */
95 
96 #define PageXLogRecPtrGet(val) \
97  ((uint64) (val).xlogid << 32 | (val).xrecoff)
98 #define PageXLogRecPtrSet(ptr, lsn) \
99  ((ptr).xlogid = (uint32) ((lsn) >> 32), (ptr).xrecoff = (uint32) (lsn))
100 
101 /*
102  * disk page organization
103  *
104  * space management information generic to any page
105  *
106  * pd_lsn - identifies xlog record for last change to this page.
107  * pd_checksum - page checksum, if set.
108  * pd_flags - flag bits.
109  * pd_lower - offset to start of free space.
110  * pd_upper - offset to end of free space.
111  * pd_special - offset to start of special space.
112  * pd_pagesize_version - size in bytes and page layout version number.
113  * pd_prune_xid - oldest XID among potentially prunable tuples on page.
114  *
115  * The LSN is used by the buffer manager to enforce the basic rule of WAL:
116  * "thou shalt write xlog before data". A dirty buffer cannot be dumped
117  * to disk until xlog has been flushed at least as far as the page's LSN.
118  *
119  * pd_checksum stores the page checksum, if it has been set for this page;
120  * zero is a valid value for a checksum. If a checksum is not in use then
121  * we leave the field unset. This will typically mean the field is zero
122  * though non-zero values may also be present if databases have been
123  * pg_upgraded from releases prior to 9.3, when the same byte offset was
124  * used to store the current timelineid when the page was last updated.
125  * Note that there is no indication on a page as to whether the checksum
126  * is valid or not, a deliberate design choice which avoids the problem
127  * of relying on the page contents to decide whether to verify it. Hence
128  * there are no flag bits relating to checksums.
129  *
130  * pd_prune_xid is a hint field that helps determine whether pruning will be
131  * useful. It is currently unused in index pages.
132  *
133  * The page version number and page size are packed together into a single
134  * uint16 field. This is for historical reasons: before PostgreSQL 7.3,
135  * there was no concept of a page version number, and doing it this way
136  * lets us pretend that pre-7.3 databases have page version number zero.
137  * We constrain page sizes to be multiples of 256, leaving the low eight
138  * bits available for a version number.
139  *
140  * Minimum possible page size is perhaps 64B to fit page header, opaque space
141  * and a minimal tuple; of course, in reality you want it much bigger, so
142  * the constraint on pagesize mod 256 is not an important restriction.
143  * On the high end, we can only support pages up to 32KB because lp_off/lp_len
144  * are 15 bits.
145  */
146 
147 typedef struct PageHeaderData
148 {
149  /* XXX LSN is member of *any* block, not only page-organized ones */
150  PageXLogRecPtr pd_lsn; /* LSN: next byte after last byte of xlog
151  * record for last change to this page */
152  uint16 pd_checksum; /* checksum */
153  uint16 pd_flags; /* flag bits, see below */
154  LocationIndex pd_lower; /* offset to start of free space */
155  LocationIndex pd_upper; /* offset to end of free space */
156  LocationIndex pd_special; /* offset to start of special space */
158  TransactionId pd_prune_xid; /* oldest prunable XID, or zero if none */
159  ItemIdData pd_linp[FLEXIBLE_ARRAY_MEMBER]; /* line pointer array */
161 
163 
164 /*
165  * pd_flags contains the following flag bits. Undefined bits are initialized
166  * to zero and may be used in the future.
167  *
168  * PD_HAS_FREE_LINES is set if there are any LP_UNUSED line pointers before
169  * pd_lower. This should be considered a hint rather than the truth, since
170  * changes to it are not WAL-logged.
171  *
172  * PD_PAGE_FULL is set if an UPDATE doesn't find enough free space in the
173  * page for its new tuple version; this suggests that a prune is needed.
174  * Again, this is just a hint.
175  */
176 #define PD_HAS_FREE_LINES 0x0001 /* are there any unused line pointers? */
177 #define PD_PAGE_FULL 0x0002 /* not enough free space for new
178  * tuple? */
179 #define PD_ALL_VISIBLE 0x0004 /* all tuples on page are visible to
180  * everyone */
181 
182 #define PD_VALID_FLAG_BITS 0x0007 /* OR of all valid pd_flags bits */
183 
184 /*
185  * Page layout version number 0 is for pre-7.3 Postgres releases.
186  * Releases 7.3 and 7.4 use 1, denoting a new HeapTupleHeader layout.
187  * Release 8.0 uses 2; it changed the HeapTupleHeader layout again.
188  * Release 8.1 uses 3; it redefined HeapTupleHeader infomask bits.
189  * Release 8.3 uses 4; it changed the HeapTupleHeader layout again, and
190  * added the pd_flags field (by stealing some bits from pd_tli),
191  * as well as adding the pd_prune_xid field (which enlarges the header).
192  *
193  * As of Release 9.3, the checksum version must also be considered when
194  * handling pages.
195  */
196 #define PG_PAGE_LAYOUT_VERSION 4
197 #define PG_DATA_CHECKSUM_VERSION 1
198 
199 /* ----------------------------------------------------------------
200  * page support macros
201  * ----------------------------------------------------------------
202  */
203 
204 /*
205  * PageIsValid
206  * True iff page is valid.
207  */
208 #define PageIsValid(page) PointerIsValid(page)
209 
210 /*
211  * line pointer(s) do not count as part of header
212  */
213 #define SizeOfPageHeaderData (offsetof(PageHeaderData, pd_linp))
214 
215 /*
216  * PageIsEmpty
217  * returns true iff no itemid has been allocated on the page
218  */
219 #define PageIsEmpty(page) \
220  (((PageHeader) (page))->pd_lower <= SizeOfPageHeaderData)
221 
222 /*
223  * PageIsNew
224  * returns true iff page has not been initialized (by PageInit)
225  */
226 #define PageIsNew(page) (((PageHeader) (page))->pd_upper == 0)
227 
228 /*
229  * PageGetItemId
230  * Returns an item identifier of a page.
231  */
232 #define PageGetItemId(page, offsetNumber) \
233  ((ItemId) (&((PageHeader) (page))->pd_linp[(offsetNumber) - 1]))
234 
235 /*
236  * PageGetContents
237  * To be used in case the page does not contain item pointers.
238  *
239  * Note: prior to 8.3 this was not guaranteed to yield a MAXALIGN'd result.
240  * Now it is. Beware of old code that might think the offset to the contents
241  * is just SizeOfPageHeaderData rather than MAXALIGN(SizeOfPageHeaderData).
242  */
243 #define PageGetContents(page) \
244  ((char *) (page) + MAXALIGN(SizeOfPageHeaderData))
245 
246 /* ----------------
247  * macros to access page size info
248  * ----------------
249  */
250 
251 /*
252  * PageSizeIsValid
253  * True iff the page size is valid.
254  */
255 #define PageSizeIsValid(pageSize) ((pageSize) == BLCKSZ)
256 
257 /*
258  * PageGetPageSize
259  * Returns the page size of a page.
260  *
261  * this can only be called on a formatted page (unlike
262  * BufferGetPageSize, which can be called on an unformatted page).
263  * however, it can be called on a page that is not stored in a buffer.
264  */
265 #define PageGetPageSize(page) \
266  ((Size) (((PageHeader) (page))->pd_pagesize_version & (uint16) 0xFF00))
267 
268 /*
269  * PageGetPageLayoutVersion
270  * Returns the page layout version of a page.
271  */
272 #define PageGetPageLayoutVersion(page) \
273  (((PageHeader) (page))->pd_pagesize_version & 0x00FF)
274 
275 /*
276  * PageSetPageSizeAndVersion
277  * Sets the page size and page layout version number of a page.
278  *
279  * We could support setting these two values separately, but there's
280  * no real need for it at the moment.
281  */
282 #define PageSetPageSizeAndVersion(page, size, version) \
283 ( \
284  AssertMacro(((size) & 0xFF00) == (size)), \
285  AssertMacro(((version) & 0x00FF) == (version)), \
286  ((PageHeader) (page))->pd_pagesize_version = (size) | (version) \
287 )
288 
289 /* ----------------
290  * page special data macros
291  * ----------------
292  */
293 /*
294  * PageGetSpecialSize
295  * Returns size of special space on a page.
296  */
297 #define PageGetSpecialSize(page) \
298  ((uint16) (PageGetPageSize(page) - ((PageHeader)(page))->pd_special))
299 
300 /*
301  * Using assertions, validate that the page special pointer is OK.
302  *
303  * This is intended to catch use of the pointer before page initialization.
304  * It is implemented as a function due to the limitations of the MSVC
305  * compiler, which choked on doing all these tests within another macro. We
306  * return true so that MacroAssert() can be used while still getting the
307  * specifics from the macro failure within this function.
308  */
309 static inline bool
311 {
312  Assert(PageIsValid(page));
313  Assert(((PageHeader) (page))->pd_special <= BLCKSZ);
314  Assert(((PageHeader) (page))->pd_special >= SizeOfPageHeaderData);
315 
316  return true;
317 }
318 
319 /*
320  * PageGetSpecialPointer
321  * Returns pointer to special space on a page.
322  */
323 #define PageGetSpecialPointer(page) \
324 ( \
325  AssertMacro(PageValidateSpecialPointer(page)), \
326  (char *) ((char *) (page) + ((PageHeader) (page))->pd_special) \
327 )
328 
329 /*
330  * PageGetItem
331  * Retrieves an item on the given page.
332  *
333  * Note:
334  * This does not change the status of any of the resources passed.
335  * The semantics may change in the future.
336  */
337 #define PageGetItem(page, itemId) \
338 ( \
339  AssertMacro(PageIsValid(page)), \
340  AssertMacro(ItemIdHasStorage(itemId)), \
341  (Item)(((char *)(page)) + ItemIdGetOffset(itemId)) \
342 )
343 
344 /*
345  * PageGetMaxOffsetNumber
346  * Returns the maximum offset number used by the given page.
347  * Since offset numbers are 1-based, this is also the number
348  * of items on the page.
349  *
350  * NOTE: if the page is not initialized (pd_lower == 0), we must
351  * return zero to ensure sane behavior. Accept double evaluation
352  * of the argument so that we can ensure this.
353  */
354 #define PageGetMaxOffsetNumber(page) \
355  (((PageHeader) (page))->pd_lower <= SizeOfPageHeaderData ? 0 : \
356  ((((PageHeader) (page))->pd_lower - SizeOfPageHeaderData) \
357  / sizeof(ItemIdData)))
358 
359 /*
360  * Additional macros for access to page headers. (Beware multiple evaluation
361  * of the arguments!)
362  */
363 #define PageGetLSN(page) \
364  PageXLogRecPtrGet(((PageHeader) (page))->pd_lsn)
365 #define PageSetLSN(page, lsn) \
366  PageXLogRecPtrSet(((PageHeader) (page))->pd_lsn, lsn)
367 
368 #define PageHasFreeLinePointers(page) \
369  (((PageHeader) (page))->pd_flags & PD_HAS_FREE_LINES)
370 #define PageSetHasFreeLinePointers(page) \
371  (((PageHeader) (page))->pd_flags |= PD_HAS_FREE_LINES)
372 #define PageClearHasFreeLinePointers(page) \
373  (((PageHeader) (page))->pd_flags &= ~PD_HAS_FREE_LINES)
374 
375 #define PageIsFull(page) \
376  (((PageHeader) (page))->pd_flags & PD_PAGE_FULL)
377 #define PageSetFull(page) \
378  (((PageHeader) (page))->pd_flags |= PD_PAGE_FULL)
379 #define PageClearFull(page) \
380  (((PageHeader) (page))->pd_flags &= ~PD_PAGE_FULL)
381 
382 #define PageIsAllVisible(page) \
383  (((PageHeader) (page))->pd_flags & PD_ALL_VISIBLE)
384 #define PageSetAllVisible(page) \
385  (((PageHeader) (page))->pd_flags |= PD_ALL_VISIBLE)
386 #define PageClearAllVisible(page) \
387  (((PageHeader) (page))->pd_flags &= ~PD_ALL_VISIBLE)
388 
389 #define PageIsPrunable(page, oldestxmin) \
390 ( \
391  AssertMacro(TransactionIdIsNormal(oldestxmin)), \
392  TransactionIdIsValid(((PageHeader) (page))->pd_prune_xid) && \
393  TransactionIdPrecedes(((PageHeader) (page))->pd_prune_xid, oldestxmin) \
394 )
395 #define PageSetPrunable(page, xid) \
396 do { \
397  Assert(TransactionIdIsNormal(xid)); \
398  if (!TransactionIdIsValid(((PageHeader) (page))->pd_prune_xid) || \
399  TransactionIdPrecedes(xid, ((PageHeader) (page))->pd_prune_xid)) \
400  ((PageHeader) (page))->pd_prune_xid = (xid); \
401 } while (0)
402 #define PageClearPrunable(page) \
403  (((PageHeader) (page))->pd_prune_xid = InvalidTransactionId)
404 
405 
406 /* ----------------------------------------------------------------
407  * extern declarations
408  * ----------------------------------------------------------------
409  */
410 #define PAI_OVERWRITE (1 << 0)
411 #define PAI_IS_HEAP (1 << 1)
412 
413 #define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap) \
414  PageAddItemExtended(page, item, size, offsetNumber, \
415  ((overwrite) ? PAI_OVERWRITE : 0) | \
416  ((is_heap) ? PAI_IS_HEAP : 0))
417 
418 extern void PageInit(Page page, Size pageSize, Size specialSize);
419 extern bool PageIsVerified(Page page, BlockNumber blkno);
420 extern OffsetNumber PageAddItemExtended(Page page, Item item, Size size,
421  OffsetNumber offsetNumber, int flags);
422 extern Page PageGetTempPage(Page page);
423 extern Page PageGetTempPageCopy(Page page);
425 extern void PageRestoreTempPage(Page tempPage, Page oldPage);
426 extern void PageRepairFragmentation(Page page);
427 extern Size PageGetFreeSpace(Page page);
428 extern Size PageGetExactFreeSpace(Page page);
429 extern Size PageGetHeapFreeSpace(Page page);
430 extern void PageIndexTupleDelete(Page page, OffsetNumber offset);
431 extern void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems);
432 extern void PageIndexTupleDeleteNoCompact(Page page, OffsetNumber offset);
433 extern bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum,
434  Item newtup, Size newsize);
435 extern char *PageSetChecksumCopy(Page page, BlockNumber blkno);
436 extern void PageSetChecksumInplace(Page page, BlockNumber blkno);
437 
438 #endif /* BUFPAGE_H */
void PageIndexTupleDelete(Page page, OffsetNumber offset)
Definition: bufpage.c:700
uint32 TransactionId
Definition: c.h:394
uint16 pd_flags
Definition: bufpage.h:153
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:809
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1172
Pointer Item
Definition: item.h:17
static bool PageValidateSpecialPointer(Page page)
Definition: bufpage.h:310
void PageRepairFragmentation(Page page)
Definition: bufpage.c:479
uint32 BlockNumber
Definition: block.h:31
uint32 xlogid
Definition: bufpage.h:92
#define PageIsValid(page)
Definition: bufpage.h:208
#define SizeOfPageHeaderData
Definition: bufpage.h:213
uint16 OffsetNumber
Definition: off.h:24
uint16 pd_checksum
Definition: bufpage.h:152
OffsetNumber PageAddItemExtended(Page page, Item item, Size size, OffsetNumber offsetNumber, int flags)
Definition: bufpage.c:190
unsigned short uint16
Definition: c.h:264
Page PageGetTempPage(Page page)
Definition: bufpage.c:348
char * Pointer
Definition: c.h:242
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:606
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:41
Page PageGetTempPageCopy(Page page)
Definition: bufpage.c:365
unsigned int uint32
Definition: c.h:265
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:582
uint16 pd_pagesize_version
Definition: bufpage.h:157
Page PageGetTempPageCopySpecial(Page page)
Definition: bufpage.c:385
LocationIndex pd_special
Definition: bufpage.h:156
uint16 LocationIndex
Definition: bufpage.h:83
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:407
bool PageIsVerified(Page page, BlockNumber blkno)
Definition: bufpage.c:81
struct PageHeaderData PageHeaderData
PageHeaderData * PageHeader
Definition: bufpage.h:162
#define Assert(condition)
Definition: c.h:671
void PageIndexTupleDeleteNoCompact(Page page, OffsetNumber offset)
Definition: bufpage.c:931
TransactionId pd_prune_xid
Definition: bufpage.h:158
size_t Size
Definition: c.h:353
char * PageSetChecksumCopy(Page page, BlockNumber blkno)
Definition: bufpage.c:1143
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:639
ItemIdData pd_linp[FLEXIBLE_ARRAY_MEMBER]
Definition: bufpage.h:159
bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum, Item newtup, Size newsize)
Definition: bufpage.c:1039
uint32 xrecoff
Definition: bufpage.h:93
LocationIndex pd_upper
Definition: bufpage.h:155
PageXLogRecPtr pd_lsn
Definition: bufpage.h:150
Pointer Page
Definition: bufpage.h:74
LocationIndex pd_lower
Definition: bufpage.h:154