Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * bufpage.h
4 : : * Standard POSTGRES buffer page definitions.
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2026, 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/off.h"
20 : :
21 : : /* GUC variable */
22 : : extern PGDLLIMPORT bool ignore_checksum_failure;
23 : :
24 : : /*
25 : : * A postgres disk page is an abstraction layered on top of a postgres
26 : : * disk block (which is simply a unit of i/o, see block.h).
27 : : *
28 : : * specifically, while a disk block can be unformatted, a postgres
29 : : * disk page is always a slotted page of the form:
30 : : *
31 : : * +----------------+---------------------------------+
32 : : * | PageHeaderData | linp1 linp2 linp3 ... |
33 : : * +-----------+----+---------------------------------+
34 : : * | ... linpN | |
35 : : * +-----------+--------------------------------------+
36 : : * | ^ pd_lower |
37 : : * | |
38 : : * | v pd_upper |
39 : : * +-------------+------------------------------------+
40 : : * | | tupleN ... |
41 : : * +-------------+------------------+-----------------+
42 : : * | ... tuple3 tuple2 tuple1 | "special space" |
43 : : * +--------------------------------+-----------------+
44 : : * ^ pd_special
45 : : *
46 : : * a page is full when nothing can be added between pd_lower and
47 : : * pd_upper.
48 : : *
49 : : * all blocks written out by an access method must be disk pages.
50 : : *
51 : : * EXCEPTIONS:
52 : : *
53 : : * obviously, a page is not formatted before it is initialized by
54 : : * a call to PageInit.
55 : : *
56 : : * NOTES:
57 : : *
58 : : * linp1..N form an ItemId (line pointer) array. ItemPointers point
59 : : * to a physical block number and a logical offset (line pointer
60 : : * number) within that block/page. Note that OffsetNumbers
61 : : * conventionally start at 1, not 0.
62 : : *
63 : : * tuple1..N are added "backwards" on the page. Since an ItemPointer
64 : : * offset is used to access an ItemId entry rather than an actual
65 : : * byte-offset position, tuples can be physically shuffled on a page
66 : : * whenever the need arises. This indirection also keeps crash recovery
67 : : * relatively simple, because the low-level details of page space
68 : : * management can be controlled by standard buffer page code during
69 : : * logging, and during recovery.
70 : : *
71 : : * AM-generic per-page information is kept in PageHeaderData.
72 : : *
73 : : * AM-specific per-page data (if any) is kept in the area marked "special
74 : : * space"; each AM has an "opaque" structure defined somewhere that is
75 : : * stored as the page trailer. An access method should always
76 : : * initialize its pages with PageInit and then set its own opaque
77 : : * fields.
78 : : */
79 : :
80 : : typedef char PageData;
81 : : typedef PageData *Page;
82 : :
83 : :
84 : : /*
85 : : * location (byte offset) within a page.
86 : : *
87 : : * note that this is actually limited to 2^15 because we have limited
88 : : * ItemIdData.lp_off and ItemIdData.lp_len to 15 bits (see itemid.h).
89 : : */
90 : : typedef uint16 LocationIndex;
91 : :
92 : :
93 : : /*
94 : : * Store the LSN as a single 64-bit value, to allow atomic loads/stores.
95 : : *
96 : : * For historical reasons, the storage of 64-bit LSN values depends on CPU
97 : : * endianness; PageXLogRecPtr used to be a struct consisting of two 32-bit
98 : : * values. When reading (and writing) the pd_lsn field from page headers, the
99 : : * caller must convert from (and convert to) the platform's native endianness.
100 : : */
101 : : typedef struct
102 : : {
103 : : uint64 lsn;
104 : : } PageXLogRecPtr;
105 : :
106 : : #ifdef WORDS_BIGENDIAN
107 : :
108 : : static inline XLogRecPtr
109 : : PageXLogRecPtrGet(const volatile PageXLogRecPtr *val)
110 : : {
111 : : return val->lsn;
112 : : }
113 : :
114 : : static inline void
115 : : PageXLogRecPtrSet(volatile PageXLogRecPtr *ptr, XLogRecPtr lsn)
116 : : {
117 : : ptr->lsn = lsn;
118 : : }
119 : :
120 : : #else
121 : :
122 : : static inline XLogRecPtr
4 tomas.vondra@postgre 123 :GNC 30602808 : PageXLogRecPtrGet(const volatile PageXLogRecPtr *val)
124 : : {
125 : 30602808 : PageXLogRecPtr tmp = {val->lsn};
126 : :
127 : 30602808 : return (tmp.lsn << 32) | (tmp.lsn >> 32);
128 : : }
129 : :
130 : : static inline void
131 : 25864146 : PageXLogRecPtrSet(volatile PageXLogRecPtr *ptr, XLogRecPtr lsn)
132 : : {
133 : 25864146 : ptr->lsn = (lsn << 32) | (lsn >> 32);
4 tomas.vondra@postgre 134 :GIC 25864146 : }
135 : :
136 : : #endif
137 : :
138 : : /*
139 : : * disk page organization
140 : : *
141 : : * space management information generic to any page
142 : : *
143 : : * pd_lsn - identifies xlog record for last change to this page.
144 : : * pd_checksum - page checksum, if set.
145 : : * pd_flags - flag bits.
146 : : * pd_lower - offset to start of free space.
147 : : * pd_upper - offset to end of free space.
148 : : * pd_special - offset to start of special space.
149 : : * pd_pagesize_version - size in bytes and page layout version number.
150 : : * pd_prune_xid - oldest XID among potentially prunable tuples on page.
151 : : *
152 : : * The LSN is used by the buffer manager to enforce the basic rule of WAL:
153 : : * "thou shalt write xlog before data". A dirty buffer cannot be dumped
154 : : * to disk until xlog has been flushed at least as far as the page's LSN.
155 : : *
156 : : * pd_checksum stores the page checksum, if it has been set for this page;
157 : : * zero is a valid value for a checksum. If a checksum is not in use then
158 : : * we leave the field unset. This will typically mean the field is zero
159 : : * though non-zero values may also be present if databases have been
160 : : * pg_upgraded from releases prior to 9.3, when the same byte offset was
161 : : * used to store the current timelineid when the page was last updated.
162 : : * Note that there is no indication on a page as to whether the checksum
163 : : * is valid or not, a deliberate design choice which avoids the problem
164 : : * of relying on the page contents to decide whether to verify it. Hence
165 : : * there are no flag bits relating to checksums.
166 : : *
167 : : * pd_prune_xid is a hint field that helps determine whether pruning will be
168 : : * useful. It is currently unused in index pages.
169 : : *
170 : : * The page version number and page size are packed together into a single
171 : : * uint16 field. This is for historical reasons: before PostgreSQL 7.3,
172 : : * there was no concept of a page version number, and doing it this way
173 : : * lets us pretend that pre-7.3 databases have page version number zero.
174 : : * We constrain page sizes to be multiples of 256, leaving the low eight
175 : : * bits available for a version number.
176 : : *
177 : : * Minimum possible page size is perhaps 64B to fit page header, opaque space
178 : : * and a minimal tuple; of course, in reality you want it much bigger, so
179 : : * the constraint on pagesize mod 256 is not an important restriction.
180 : : * On the high end, we can only support pages up to 32KB because lp_off/lp_len
181 : : * are 15 bits.
182 : : */
183 : :
184 : : typedef struct PageHeaderData
185 : : {
186 : : /* XXX LSN is member of *any* block, not only page-organized ones */
187 : : PageXLogRecPtr pd_lsn; /* LSN: next byte after last byte of xlog
188 : : * record for last change to this page */
189 : : uint16 pd_checksum; /* checksum */
190 : : uint16 pd_flags; /* flag bits, see below */
191 : : LocationIndex pd_lower; /* offset to start of free space */
192 : : LocationIndex pd_upper; /* offset to end of free space */
193 : : LocationIndex pd_special; /* offset to start of special space */
194 : : uint16 pd_pagesize_version;
195 : : TransactionId pd_prune_xid; /* oldest prunable XID, or zero if none */
196 : : ItemIdData pd_linp[FLEXIBLE_ARRAY_MEMBER]; /* line pointer array */
197 : : } PageHeaderData;
198 : :
199 : : typedef PageHeaderData *PageHeader;
200 : :
201 : : /*
202 : : * pd_flags contains the following flag bits. Undefined bits are initialized
203 : : * to zero and may be used in the future.
204 : : *
205 : : * PD_HAS_FREE_LINES is set if there are any LP_UNUSED line pointers before
206 : : * pd_lower. This should be considered a hint rather than the truth, since
207 : : * changes to it are not WAL-logged.
208 : : *
209 : : * PD_PAGE_FULL is set if an UPDATE doesn't find enough free space in the
210 : : * page for its new tuple version; this suggests that a prune is needed.
211 : : * Again, this is just a hint.
212 : : */
213 : : #define PD_HAS_FREE_LINES 0x0001 /* are there any unused line pointers? */
214 : : #define PD_PAGE_FULL 0x0002 /* not enough free space for new tuple? */
215 : : #define PD_ALL_VISIBLE 0x0004 /* all tuples on page are visible to
216 : : * everyone */
217 : :
218 : : #define PD_VALID_FLAG_BITS 0x0007 /* OR of all valid pd_flags bits */
219 : :
220 : : /*
221 : : * Page layout version number 0 is for pre-7.3 Postgres releases.
222 : : * Releases 7.3 and 7.4 use 1, denoting a new HeapTupleHeader layout.
223 : : * Release 8.0 uses 2; it changed the HeapTupleHeader layout again.
224 : : * Release 8.1 uses 3; it redefined HeapTupleHeader infomask bits.
225 : : * Release 8.3 uses 4; it changed the HeapTupleHeader layout again, and
226 : : * added the pd_flags field (by stealing some bits from pd_tli),
227 : : * as well as adding the pd_prune_xid field (which enlarges the header).
228 : : *
229 : : * As of Release 9.3, the checksum version must also be considered when
230 : : * handling pages.
231 : : */
232 : : #define PG_PAGE_LAYOUT_VERSION 4
233 : : #define PG_DATA_CHECKSUM_VERSION 1
234 : :
235 : : /* ----------------------------------------------------------------
236 : : * page support functions
237 : : * ----------------------------------------------------------------
238 : : */
239 : :
240 : : /*
241 : : * line pointer(s) do not count as part of header
242 : : */
243 : : #define SizeOfPageHeaderData (offsetof(PageHeaderData, pd_linp))
244 : :
245 : : /*
246 : : * PageIsEmpty
247 : : * returns true iff no itemid has been allocated on the page
248 : : */
249 : : static inline bool
419 peter@eisentraut.org 250 :CBC 1368612 : PageIsEmpty(const PageData *page)
251 : : {
252 : 1368612 : return ((const PageHeaderData *) page)->pd_lower <= SizeOfPageHeaderData;
253 : : }
254 : :
255 : : /*
256 : : * PageIsNew
257 : : * returns true iff page has not been initialized (by PageInit)
258 : : */
259 : : static inline bool
260 : 47374981 : PageIsNew(const PageData *page)
261 : : {
262 : 47374981 : return ((const PageHeaderData *) page)->pd_upper == 0;
263 : : }
264 : :
265 : : /*
266 : : * PageGetItemId
267 : : * Returns an item identifier of a page.
268 : : */
269 : : static inline ItemId
1343 270 : 1459907222 : PageGetItemId(Page page, OffsetNumber offsetNumber)
271 : : {
272 : 1459907222 : return &((PageHeader) page)->pd_linp[offsetNumber - 1];
273 : : }
274 : :
275 : : /*
276 : : * PageGetContents
277 : : * To be used in cases where the page does not contain line pointers.
278 : : *
279 : : * Note: prior to 8.3 this was not guaranteed to yield a MAXALIGN'd result.
280 : : * Now it is. Beware of old code that might think the offset to the contents
281 : : * is just SizeOfPageHeaderData rather than MAXALIGN(SizeOfPageHeaderData).
282 : : */
283 : : static inline char *
284 : 22326496 : PageGetContents(Page page)
285 : : {
286 : 22326496 : return (char *) page + MAXALIGN(SizeOfPageHeaderData);
287 : : }
288 : :
289 : : /* ----------------
290 : : * functions to access page size info
291 : : * ----------------
292 : : */
293 : :
294 : : /*
295 : : * PageGetPageSize
296 : : * Returns the page size of a page.
297 : : *
298 : : * this can only be called on a formatted page (unlike
299 : : * BufferGetPageSize, which can be called on an unformatted page).
300 : : * however, it can be called on a page that is not stored in a buffer.
301 : : */
302 : : static inline Size
419 303 : 28142530 : PageGetPageSize(const PageData *page)
304 : : {
305 : 28142530 : return (Size) (((const PageHeaderData *) page)->pd_pagesize_version & (uint16) 0xFF00);
306 : : }
307 : :
308 : : /*
309 : : * PageGetPageLayoutVersion
310 : : * Returns the page layout version of a page.
311 : : */
312 : : static inline uint8
313 : 4 : PageGetPageLayoutVersion(const PageData *page)
314 : : {
315 : 4 : return (((const PageHeaderData *) page)->pd_pagesize_version & 0x00FF);
316 : : }
317 : :
318 : : /*
319 : : * PageSetPageSizeAndVersion
320 : : * Sets the page size and page layout version number of a page.
321 : : *
322 : : * We could support setting these two values separately, but there's
323 : : * no real need for it at the moment.
324 : : */
325 : : static inline void
1343 326 : 362010 : PageSetPageSizeAndVersion(Page page, Size size, uint8 version)
327 : : {
328 [ - + ]: 362010 : Assert((size & 0xFF00) == size);
329 : : Assert((version & 0x00FF) == version);
330 : :
331 : 362010 : ((PageHeader) page)->pd_pagesize_version = size | version;
332 : 362010 : }
333 : :
334 : : /* ----------------
335 : : * page special data functions
336 : : * ----------------
337 : : */
338 : : /*
339 : : * PageGetSpecialSize
340 : : * Returns size of special space on a page.
341 : : */
342 : : static inline uint16
419 343 : 27695931 : PageGetSpecialSize(const PageData *page)
344 : : {
345 : 27695931 : return (PageGetPageSize(page) - ((const PageHeaderData *) page)->pd_special);
346 : : }
347 : :
348 : : /*
349 : : * Using assertions, validate that the page special pointer is OK.
350 : : *
351 : : * This is intended to catch use of the pointer before page initialization.
352 : : */
353 : : static inline void
354 : 479370121 : PageValidateSpecialPointer(const PageData *page)
355 : : {
1343 356 [ - + ]: 479370121 : Assert(page);
419 357 [ - + ]: 479370121 : Assert(((const PageHeaderData *) page)->pd_special <= BLCKSZ);
358 [ - + ]: 479370121 : Assert(((const PageHeaderData *) page)->pd_special >= SizeOfPageHeaderData);
3627 kgrittn@postgresql.o 359 : 479370121 : }
360 : :
361 : : /*
362 : : * PageGetSpecialPointer
363 : : * Returns pointer to special space on a page.
364 : : */
365 : : #define PageGetSpecialPointer(page) \
366 : : ( \
367 : : PageValidateSpecialPointer(page), \
368 : : ((page) + ((PageHeader) (page))->pd_special) \
369 : : )
370 : :
371 : : /*
372 : : * PageGetItem
373 : : * Retrieves an item on the given page.
374 : : *
375 : : * Note:
376 : : * This does not change the status of any of the resources passed.
377 : : * The semantics may change in the future.
378 : : */
379 : : static inline void *
70 peter@eisentraut.org 380 :GNC 724605002 : PageGetItem(PageData *page, const ItemIdData *itemId)
381 : : {
1343 peter@eisentraut.org 382 [ - + ]:CBC 724605002 : Assert(page);
383 [ - + ]: 724605002 : Assert(ItemIdHasStorage(itemId));
384 : :
70 peter@eisentraut.org 385 :GNC 724605002 : return (char *) page + ItemIdGetOffset(itemId);
386 : : }
387 : :
388 : : /*
389 : : * PageGetMaxOffsetNumber
390 : : * Returns the maximum offset number used by the given page.
391 : : * Since offset numbers are 1-based, this is also the number
392 : : * of items on the page.
393 : : *
394 : : * NOTE: if the page is not initialized (pd_lower == 0), we must
395 : : * return zero to ensure sane behavior.
396 : : */
397 : : static inline OffsetNumber
419 peter@eisentraut.org 398 :CBC 551141340 : PageGetMaxOffsetNumber(const PageData *page)
399 : : {
400 : 551141340 : const PageHeaderData *pageheader = (const PageHeaderData *) page;
401 : :
1343 402 [ + + ]: 551141340 : if (pageheader->pd_lower <= SizeOfPageHeaderData)
403 : 503615 : return 0;
404 : : else
405 : 550637725 : return (pageheader->pd_lower - SizeOfPageHeaderData) / sizeof(ItemIdData);
406 : : }
407 : :
408 : : /*
409 : : * Additional functions for access to page headers.
410 : : */
411 : : static inline XLogRecPtr
419 412 : 29585277 : PageGetLSN(const PageData *page)
413 : : {
4 tomas.vondra@postgre 414 :GNC 29585277 : return PageXLogRecPtrGet(&((const PageHeaderData *) page)->pd_lsn);
415 : : }
416 : :
417 : : static inline void
1343 peter@eisentraut.org 418 :CBC 21001644 : PageSetLSN(Page page, XLogRecPtr lsn)
419 : : {
4 tomas.vondra@postgre 420 :GNC 21001644 : PageXLogRecPtrSet(&((PageHeader) page)->pd_lsn, lsn);
1343 peter@eisentraut.org 421 :CBC 21001644 : }
422 : :
423 : : static inline bool
419 424 : 12728035 : PageHasFreeLinePointers(const PageData *page)
425 : : {
426 : 12728035 : return ((const PageHeaderData *) page)->pd_flags & PD_HAS_FREE_LINES;
427 : : }
428 : : static inline void
1343 429 : 31875 : PageSetHasFreeLinePointers(Page page)
430 : : {
431 : 31875 : ((PageHeader) page)->pd_flags |= PD_HAS_FREE_LINES;
432 : 31875 : }
433 : : static inline void
434 : 4755588 : PageClearHasFreeLinePointers(Page page)
435 : : {
436 : 4755588 : ((PageHeader) page)->pd_flags &= ~PD_HAS_FREE_LINES;
437 : 4755588 : }
438 : :
439 : : static inline bool
419 440 : 1394276 : PageIsFull(const PageData *page)
441 : : {
442 : 1394276 : return ((const PageHeaderData *) page)->pd_flags & PD_PAGE_FULL;
443 : : }
444 : : static inline void
1343 445 : 149392 : PageSetFull(Page page)
446 : : {
447 : 149392 : ((PageHeader) page)->pd_flags |= PD_PAGE_FULL;
448 : 149392 : }
449 : : static inline void
450 : 4753780 : PageClearFull(Page page)
451 : : {
452 : 4753780 : ((PageHeader) page)->pd_flags &= ~PD_PAGE_FULL;
453 : 4753780 : }
454 : :
455 : : static inline bool
419 456 : 39163240 : PageIsAllVisible(const PageData *page)
457 : : {
458 : 39163240 : return ((const PageHeaderData *) page)->pd_flags & PD_ALL_VISIBLE;
459 : : }
460 : : static inline void
1343 461 : 63800 : PageSetAllVisible(Page page)
462 : : {
463 : 63800 : ((PageHeader) page)->pd_flags |= PD_ALL_VISIBLE;
464 : 63800 : }
465 : : static inline void
466 : 4720272 : PageClearAllVisible(Page page)
467 : : {
468 : 4720272 : ((PageHeader) page)->pd_flags &= ~PD_ALL_VISIBLE;
469 : 4720272 : }
470 : :
471 : : static inline TransactionId
10 melanieplageman@gmai 472 :GNC 16177728 : PageGetPruneXid(const PageData *page)
473 : : {
474 : 16177728 : return ((const PageHeaderData *) page)->pd_prune_xid;
475 : : }
476 : :
477 : : /*
478 : : * These two require "access/transam.h", so left as macros.
479 : : */
480 : : #define PageSetPrunable(page, xid) \
481 : : do { \
482 : : Assert(TransactionIdIsNormal(xid)); \
483 : : if (!TransactionIdIsValid(((PageHeader) (page))->pd_prune_xid) || \
484 : : TransactionIdPrecedes(xid, ((PageHeader) (page))->pd_prune_xid)) \
485 : : ((PageHeader) (page))->pd_prune_xid = (xid); \
486 : : } while (0)
487 : : #define PageClearPrunable(page) \
488 : : (((PageHeader) (page))->pd_prune_xid = InvalidTransactionId)
489 : :
490 : :
491 : : /* ----------------------------------------------------------------
492 : : * extern declarations
493 : : * ----------------------------------------------------------------
494 : : */
495 : :
496 : : /* flags for PageAddItemExtended() */
497 : : #define PAI_OVERWRITE (1 << 0)
498 : : #define PAI_IS_HEAP (1 << 1)
499 : :
500 : : /* flags for PageIsVerified() */
501 : : #define PIV_LOG_WARNING (1 << 0)
502 : : #define PIV_LOG_LOG (1 << 1)
503 : : #define PIV_IGNORE_CHECKSUM_FAILURE (1 << 2)
504 : :
505 : : #define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap) \
506 : : PageAddItemExtended(page, item, size, offsetNumber, \
507 : : ((overwrite) ? PAI_OVERWRITE : 0) | \
508 : : ((is_heap) ? PAI_IS_HEAP : 0))
509 : :
510 : : /*
511 : : * Check that BLCKSZ is a multiple of sizeof(size_t). In PageIsVerified(), it
512 : : * is much faster to check if a page is full of zeroes using the native word
513 : : * size. Note that this assertion is kept within a header to make sure that
514 : : * StaticAssertDecl() works across various combinations of platforms and
515 : : * compilers.
516 : : */
517 : : StaticAssertDecl(BLCKSZ == ((BLCKSZ / sizeof(size_t)) * sizeof(size_t)),
518 : : "BLCKSZ has to be a multiple of sizeof(size_t)");
519 : :
520 : : extern void PageInit(Page page, Size pageSize, Size specialSize);
521 : : extern bool PageIsVerified(PageData *page, BlockNumber blkno, int flags,
522 : : bool *checksum_failure_p);
523 : : extern OffsetNumber PageAddItemExtended(Page page, const void *item, Size size,
524 : : OffsetNumber offsetNumber, int flags);
525 : : extern Page PageGetTempPage(const PageData *page);
526 : : extern Page PageGetTempPageCopy(const PageData *page);
527 : : extern Page PageGetTempPageCopySpecial(const PageData *page);
528 : : extern void PageRestoreTempPage(Page tempPage, Page oldPage);
529 : : extern void PageRepairFragmentation(Page page);
530 : : extern void PageTruncateLinePointerArray(Page page);
531 : : extern Size PageGetFreeSpace(const PageData *page);
532 : : extern Size PageGetFreeSpaceForMultipleTuples(const PageData *page, int ntups);
533 : : extern Size PageGetExactFreeSpace(const PageData *page);
534 : : extern Size PageGetHeapFreeSpace(const PageData *page);
535 : : extern void PageIndexTupleDelete(Page page, OffsetNumber offnum);
536 : : extern void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems);
537 : : extern void PageIndexTupleDeleteNoCompact(Page page, OffsetNumber offnum);
538 : : extern bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum,
539 : : const void *newtup, Size newsize);
540 : : extern char *PageSetChecksumCopy(Page page, BlockNumber blkno);
541 : : extern void PageSetChecksumInplace(Page page, BlockNumber blkno);
542 : :
543 : : #endif /* BUFPAGE_H */
|