Branch data 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
123 : 42881924 : PageXLogRecPtrGet(const volatile PageXLogRecPtr *val)
124 : : {
125 : 42881924 : PageXLogRecPtr tmp = {val->lsn};
126 : :
127 : 42881924 : return (tmp.lsn << 32) | (tmp.lsn >> 32);
128 : : }
129 : :
130 : : static inline void
131 : 37725937 : PageXLogRecPtrSet(volatile PageXLogRecPtr *ptr, XLogRecPtr lsn)
132 : : {
133 : 37725937 : ptr->lsn = (lsn << 32) | (lsn >> 32);
134 : 37725937 : }
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 : :
234 : : /* ----------------------------------------------------------------
235 : : * page support functions
236 : : * ----------------------------------------------------------------
237 : : */
238 : :
239 : : /*
240 : : * line pointer(s) do not count as part of header
241 : : */
242 : : #define SizeOfPageHeaderData (offsetof(PageHeaderData, pd_linp))
243 : :
244 : : /*
245 : : * Returns true if the page header describes a non-empty, in-bounds free space
246 : : * range. This does not prove that the page uses the standard page layout.
247 : : */
248 : : static inline bool
249 : 2748013 : PageFreeSpaceBoundsAreValid(const PageData *page)
250 : : {
251 : 2748013 : const PageHeaderData *phdr = (const PageHeaderData *) page;
252 : :
253 : 5496013 : return phdr->pd_lower >= SizeOfPageHeaderData &&
254 [ + + + + ]: 5493944 : phdr->pd_upper > phdr->pd_lower &&
255 [ + - ]: 2745931 : phdr->pd_upper <= BLCKSZ;
256 : : }
257 : :
258 : : /*
259 : : * PageIsEmpty
260 : : * returns true iff no itemid has been allocated on the page
261 : : */
262 : : static inline bool
263 : 2329939 : PageIsEmpty(const PageData *page)
264 : : {
265 : 2329939 : return ((const PageHeaderData *) page)->pd_lower <= SizeOfPageHeaderData;
266 : : }
267 : :
268 : : /*
269 : : * PageIsNew
270 : : * returns true iff page has not been initialized (by PageInit)
271 : : */
272 : : static inline bool
273 : 60660971 : PageIsNew(const PageData *page)
274 : : {
275 : 60660971 : return ((const PageHeaderData *) page)->pd_upper == 0;
276 : : }
277 : :
278 : : /*
279 : : * PageGetItemId
280 : : * Returns an item identifier of a page.
281 : : */
282 : : static inline ItemId
283 : 1592504241 : PageGetItemId(Page page, OffsetNumber offsetNumber)
284 : : {
285 : 1592504241 : return &((PageHeader) page)->pd_linp[offsetNumber - 1];
286 : : }
287 : :
288 : : /*
289 : : * PageGetContents
290 : : * To be used in cases where the page does not contain line pointers.
291 : : *
292 : : * Note: prior to 8.3 this was not guaranteed to yield a MAXALIGN'd result.
293 : : * Now it is. Beware of old code that might think the offset to the contents
294 : : * is just SizeOfPageHeaderData rather than MAXALIGN(SizeOfPageHeaderData).
295 : : */
296 : : static inline char *
297 : 28315747 : PageGetContents(Page page)
298 : : {
299 : 28315747 : return (char *) page + MAXALIGN(SizeOfPageHeaderData);
300 : : }
301 : :
302 : : /* ----------------
303 : : * functions to access page size info
304 : : * ----------------
305 : : */
306 : :
307 : : /*
308 : : * PageGetPageSize
309 : : * Returns the page size of a page.
310 : : *
311 : : * this can only be called on a formatted page (unlike
312 : : * BufferGetPageSize, which can be called on an unformatted page).
313 : : * however, it can be called on a page that is not stored in a buffer.
314 : : */
315 : : static inline Size
316 : 36968956 : PageGetPageSize(const PageData *page)
317 : : {
318 : 36968956 : return (Size) (((const PageHeaderData *) page)->pd_pagesize_version & (uint16) 0xFF00);
319 : : }
320 : :
321 : : /*
322 : : * PageGetPageLayoutVersion
323 : : * Returns the page layout version of a page.
324 : : */
325 : : static inline uint8
326 : 4 : PageGetPageLayoutVersion(const PageData *page)
327 : : {
328 : 4 : return (((const PageHeaderData *) page)->pd_pagesize_version & 0x00FF);
329 : : }
330 : :
331 : : /*
332 : : * PageSetPageSizeAndVersion
333 : : * Sets the page size and page layout version number of a page.
334 : : *
335 : : * We could support setting these two values separately, but there's
336 : : * no real need for it at the moment.
337 : : */
338 : : static inline void
339 : 449294 : PageSetPageSizeAndVersion(Page page, Size size, uint8 version)
340 : : {
341 : : Assert((size & 0xFF00) == size);
342 : : Assert((version & 0x00FF) == version);
343 : :
344 : 449294 : ((PageHeader) page)->pd_pagesize_version = size | version;
345 : 449294 : }
346 : :
347 : : /* ----------------
348 : : * page special data functions
349 : : * ----------------
350 : : */
351 : : /*
352 : : * PageGetSpecialSize
353 : : * Returns size of special space on a page.
354 : : */
355 : : static inline uint16
356 : 36356541 : PageGetSpecialSize(const PageData *page)
357 : : {
358 : 36356541 : return (PageGetPageSize(page) - ((const PageHeaderData *) page)->pd_special);
359 : : }
360 : :
361 : : /*
362 : : * Using assertions, validate that the page special pointer is OK.
363 : : *
364 : : * This is intended to catch use of the pointer before page initialization.
365 : : */
366 : : static inline void
367 : 404804820 : PageValidateSpecialPointer(const PageData *page)
368 : : {
369 : : Assert(page);
370 : : Assert(((const PageHeaderData *) page)->pd_special <= BLCKSZ);
371 : : Assert(((const PageHeaderData *) page)->pd_special >= SizeOfPageHeaderData);
372 : 404804820 : }
373 : :
374 : : /*
375 : : * PageGetSpecialPointer
376 : : * Returns pointer to special space on a page.
377 : : */
378 : : #define PageGetSpecialPointer(page) \
379 : : ( \
380 : : PageValidateSpecialPointer(page), \
381 : : ((page) + ((const PageHeaderData *) (page))->pd_special) \
382 : : )
383 : :
384 : : /*
385 : : * PageGetItem
386 : : * Retrieves an item on the given page.
387 : : *
388 : : * Note:
389 : : * This does not change the status of any of the resources passed.
390 : : * The semantics may change in the future.
391 : : */
392 : : static inline void *
393 : 745272016 : PageGetItem(PageData *page, const ItemIdData *itemId)
394 : : {
395 : : Assert(page);
396 : : Assert(ItemIdHasStorage(itemId));
397 : :
398 : 745272016 : return (char *) page + ItemIdGetOffset(itemId);
399 : : }
400 : :
401 : : /*
402 : : * PageGetMaxOffsetNumber
403 : : * Returns the maximum offset number used by the given page.
404 : : * Since offset numbers are 1-based, this is also the number
405 : : * of items on the page.
406 : : *
407 : : * NOTE: if the page is not initialized (pd_lower == 0), we must
408 : : * return zero to ensure sane behavior.
409 : : */
410 : : static inline OffsetNumber
411 : 447187685 : PageGetMaxOffsetNumber(const PageData *page)
412 : : {
413 : 447187685 : const PageHeaderData *pageheader = (const PageHeaderData *) page;
414 : :
415 [ + + ]: 447187685 : if (pageheader->pd_lower <= SizeOfPageHeaderData)
416 : 648503 : return 0;
417 : : else
418 : 446539182 : return (pageheader->pd_lower - SizeOfPageHeaderData) / sizeof(ItemIdData);
419 : : }
420 : :
421 : : /*
422 : : * Additional functions for access to page headers.
423 : : */
424 : : static inline XLogRecPtr
425 : 41631970 : PageGetLSN(const PageData *page)
426 : : {
427 : 41631970 : return PageXLogRecPtrGet(&((const PageHeaderData *) page)->pd_lsn);
428 : : }
429 : :
430 : : static inline void
431 : 32707388 : PageSetLSN(Page page, XLogRecPtr lsn)
432 : : {
433 : 32707388 : PageXLogRecPtrSet(&((PageHeader) page)->pd_lsn, lsn);
434 : 32707388 : }
435 : :
436 : : static inline bool
437 : 18599426 : PageHasFreeLinePointers(const PageData *page)
438 : : {
439 : 18599426 : return ((const PageHeaderData *) page)->pd_flags & PD_HAS_FREE_LINES;
440 : : }
441 : : static inline void
442 : 41472 : PageSetHasFreeLinePointers(Page page)
443 : : {
444 : 41472 : ((PageHeader) page)->pd_flags |= PD_HAS_FREE_LINES;
445 : 41472 : }
446 : : static inline void
447 : 4930694 : PageClearHasFreeLinePointers(Page page)
448 : : {
449 : 4930694 : ((PageHeader) page)->pd_flags &= ~PD_HAS_FREE_LINES;
450 : 4930694 : }
451 : :
452 : : static inline bool
453 : 2792812 : PageIsFull(const PageData *page)
454 : : {
455 : 2792812 : return ((const PageHeaderData *) page)->pd_flags & PD_PAGE_FULL;
456 : : }
457 : : static inline void
458 : 2212111 : PageSetFull(Page page)
459 : : {
460 : 2212111 : ((PageHeader) page)->pd_flags |= PD_PAGE_FULL;
461 : 2212111 : }
462 : : static inline void
463 : 5038373 : PageClearFull(Page page)
464 : : {
465 : 5038373 : ((PageHeader) page)->pd_flags &= ~PD_PAGE_FULL;
466 : 5038373 : }
467 : :
468 : : static inline bool
469 : 81545490 : PageIsAllVisible(const PageData *page)
470 : : {
471 : 81545490 : return ((const PageHeaderData *) page)->pd_flags & PD_ALL_VISIBLE;
472 : : }
473 : : static inline void
474 : 105271 : PageSetAllVisible(Page page)
475 : : {
476 : 105271 : ((PageHeader) page)->pd_flags |= PD_ALL_VISIBLE;
477 : 105271 : }
478 : : static inline void
479 : 4875679 : PageClearAllVisible(Page page)
480 : : {
481 : 4875679 : ((PageHeader) page)->pd_flags &= ~PD_ALL_VISIBLE;
482 : 4875679 : }
483 : :
484 : : static inline TransactionId
485 : 13496847 : PageGetPruneXid(const PageData *page)
486 : : {
487 : 13496847 : return ((const PageHeaderData *) page)->pd_prune_xid;
488 : : }
489 : :
490 : : /*
491 : : * These two require "access/transam.h", so left as macros.
492 : : */
493 : : #define PageSetPrunable(page, xid) \
494 : : do { \
495 : : Assert(TransactionIdIsNormal(xid)); \
496 : : if (!TransactionIdIsValid(((PageHeader) (page))->pd_prune_xid) || \
497 : : TransactionIdPrecedes(xid, ((PageHeader) (page))->pd_prune_xid)) \
498 : : ((PageHeader) (page))->pd_prune_xid = (xid); \
499 : : } while (0)
500 : : #define PageClearPrunable(page) \
501 : : (((PageHeader) (page))->pd_prune_xid = InvalidTransactionId)
502 : :
503 : :
504 : : /* ----------------------------------------------------------------
505 : : * extern declarations
506 : : * ----------------------------------------------------------------
507 : : */
508 : :
509 : : /* flags for PageAddItemExtended() */
510 : : #define PAI_OVERWRITE (1 << 0)
511 : : #define PAI_IS_HEAP (1 << 1)
512 : :
513 : : /* flags for PageIsVerified() */
514 : : #define PIV_LOG_WARNING (1 << 0)
515 : : #define PIV_LOG_LOG (1 << 1)
516 : : #define PIV_IGNORE_CHECKSUM_FAILURE (1 << 2)
517 : : #define PIV_ZERO_BUFFERS_ON_ERROR (1 << 3)
518 : :
519 : : #define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap) \
520 : : PageAddItemExtended(page, item, size, offsetNumber, \
521 : : ((overwrite) ? PAI_OVERWRITE : 0) | \
522 : : ((is_heap) ? PAI_IS_HEAP : 0))
523 : :
524 : : /*
525 : : * Check that BLCKSZ is a multiple of sizeof(size_t). In PageIsVerified(), it
526 : : * is much faster to check if a page is full of zeroes using the native word
527 : : * size. Note that this assertion is kept within a header to make sure that
528 : : * StaticAssertDecl() works across various combinations of platforms and
529 : : * compilers.
530 : : */
531 : : StaticAssertDecl(BLCKSZ == ((BLCKSZ / sizeof(size_t)) * sizeof(size_t)),
532 : : "BLCKSZ has to be a multiple of sizeof(size_t)");
533 : :
534 : : extern void PageInit(Page page, Size pageSize, Size specialSize);
535 : : extern bool PageIsVerified(PageData *page, BlockNumber blkno, int flags,
536 : : bool *checksum_failure_p);
537 : : extern OffsetNumber PageAddItemExtended(Page page, const void *item, Size size,
538 : : OffsetNumber offsetNumber, int flags);
539 : : extern Page PageGetTempPage(const PageData *page);
540 : : extern Page PageGetTempPageCopy(const PageData *page);
541 : : extern Page PageGetTempPageCopySpecial(const PageData *page);
542 : : extern void PageRestoreTempPage(Page tempPage, Page oldPage);
543 : : extern void PageRepairFragmentation(Page page);
544 : : extern void PageTruncateLinePointerArray(Page page);
545 : : extern Size PageGetFreeSpace(const PageData *page);
546 : : extern Size PageGetFreeSpaceForMultipleTuples(const PageData *page, int ntups);
547 : : extern Size PageGetExactFreeSpace(const PageData *page);
548 : : extern Size PageGetHeapFreeSpace(const PageData *page);
549 : : extern void PageIndexTupleDelete(Page page, OffsetNumber offnum);
550 : : extern void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems);
551 : : extern void PageIndexTupleDeleteNoCompact(Page page, OffsetNumber offnum);
552 : : extern bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum,
553 : : const void *newtup, Size newsize);
554 : : extern void PageSetChecksum(Page page, BlockNumber blkno);
555 : :
556 : : #endif /* BUFPAGE_H */
|