1996-08-28 03:59:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* bufpage.h
|
1997-09-07 07:04:48 +02:00
|
|
|
* Standard POSTGRES buffer page definitions.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*
|
2003-08-04 04:40:20 +02:00
|
|
|
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2004-08-04 23:34:35 +02:00
|
|
|
* $PostgreSQL: pgsql/src/include/storage/bufpage.h,v 1.61 2004/08/04 21:34:25 tgl Exp $
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifndef BUFPAGE_H
|
1996-08-28 03:59:28 +02:00
|
|
|
#define BUFPAGE_H
|
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "storage/buf.h"
|
|
|
|
#include "storage/bufmgr.h"
|
1999-07-16 19:07:40 +02:00
|
|
|
#include "storage/item.h"
|
|
|
|
#include "storage/itemid.h"
|
|
|
|
#include "storage/off.h"
|
2000-10-20 13:01:21 +02:00
|
|
|
#include "access/xlog.h"
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/*
|
2001-02-21 20:07:04 +01:00
|
|
|
* A postgres disk page is an abstraction layered on top of a postgres
|
1996-08-28 03:59:28 +02:00
|
|
|
* disk block (which is simply a unit of i/o, see block.h).
|
|
|
|
*
|
|
|
|
* specifically, while a disk block can be unformatted, a postgres
|
|
|
|
* disk page is always a slotted page of the form:
|
|
|
|
*
|
|
|
|
* +----------------+---------------------------------+
|
2000-01-08 22:59:55 +01:00
|
|
|
* | PageHeaderData | linp1 linp2 linp3 ... |
|
1996-08-28 03:59:28 +02:00
|
|
|
* +-----------+----+---------------------------------+
|
1997-09-07 07:04:48 +02:00
|
|
|
* | ... linpN | |
|
1996-08-28 03:59:28 +02:00
|
|
|
* +-----------+--------------------------------------+
|
1997-09-07 07:04:48 +02:00
|
|
|
* | ^ pd_lower |
|
|
|
|
* | |
|
|
|
|
* | v pd_upper |
|
1996-08-28 03:59:28 +02:00
|
|
|
* +-------------+------------------------------------+
|
1997-09-07 07:04:48 +02:00
|
|
|
* | | tupleN ... |
|
1996-08-28 03:59:28 +02:00
|
|
|
* +-------------+------------------+-----------------+
|
2000-01-08 22:59:55 +01:00
|
|
|
* | ... tuple3 tuple2 tuple1 | "special space" |
|
1996-08-28 03:59:28 +02:00
|
|
|
* +--------------------------------+-----------------+
|
1997-09-07 07:04:48 +02:00
|
|
|
* ^ pd_special
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* a page is full when nothing can be added between pd_lower and
|
|
|
|
* pd_upper.
|
|
|
|
*
|
|
|
|
* all blocks written out by an access method must be disk pages.
|
|
|
|
*
|
|
|
|
* EXCEPTIONS:
|
|
|
|
*
|
|
|
|
* obviously, a page is not formatted before it is initialized with by
|
|
|
|
* a call to PageInit.
|
|
|
|
*
|
|
|
|
* NOTES:
|
|
|
|
*
|
2000-01-08 22:59:55 +01:00
|
|
|
* linp1..N form an ItemId array. ItemPointers point into this array
|
|
|
|
* rather than pointing directly to a tuple. Note that OffsetNumbers
|
|
|
|
* conventionally start at 1, not 0.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2000-01-08 22:59:55 +01:00
|
|
|
* tuple1..N are added "backwards" on the page. because a tuple's
|
1996-08-28 03:59:28 +02:00
|
|
|
* ItemPointer points to its ItemId entry rather than its actual
|
|
|
|
* byte-offset position, tuples can be physically shuffled on a page
|
|
|
|
* whenever the need arises.
|
|
|
|
*
|
2002-07-02 08:18:57 +02:00
|
|
|
* AM-generic per-page information is kept in PageHeaderData.
|
2000-01-08 22:59:55 +01:00
|
|
|
*
|
|
|
|
* AM-specific per-page data (if any) is kept in the area marked "special
|
1996-08-28 03:59:28 +02:00
|
|
|
* space"; each AM has an "opaque" structure defined somewhere that is
|
1997-09-07 07:04:48 +02:00
|
|
|
* stored as the page trailer. an access method should always
|
1996-08-28 03:59:28 +02:00
|
|
|
* initialize its pages with PageInit and then set its own opaque
|
|
|
|
* fields.
|
|
|
|
*/
|
|
|
|
|
2003-03-28 21:17:13 +01:00
|
|
|
typedef Pointer Page;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* location (byte offset) within a page.
|
|
|
|
*
|
2000-01-08 22:59:55 +01:00
|
|
|
* note that this is actually limited to 2^15 because we have limited
|
|
|
|
* ItemIdData.lp_off and ItemIdData.lp_len to 15 bits (see itemid.h).
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
typedef uint16 LocationIndex;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2002-07-02 08:18:57 +02:00
|
|
|
* disk page organization
|
2004-07-22 00:31:26 +02:00
|
|
|
*
|
1996-08-28 03:59:28 +02:00
|
|
|
* space management information generic to any page
|
|
|
|
*
|
2004-07-22 00:31:26 +02:00
|
|
|
* pd_lsn - identifies xlog record for last change to this page.
|
|
|
|
* pd_tli - ditto.
|
2002-09-04 22:31:48 +02:00
|
|
|
* pd_lower - offset to start of free space.
|
|
|
|
* pd_upper - offset to end of free space.
|
|
|
|
* pd_special - offset to start of special space.
|
|
|
|
* pd_pagesize_version - size in bytes and page layout version number.
|
2002-09-02 03:05:06 +02:00
|
|
|
*
|
2004-07-22 00:31:26 +02:00
|
|
|
* The LSN is used by the buffer manager to enforce the basic rule of WAL:
|
|
|
|
* "thou shalt write xlog before data". A dirty buffer cannot be dumped
|
|
|
|
* to disk until xlog has been flushed at least as far as the page's LSN.
|
|
|
|
* We also store the TLI for identification purposes (it is not clear that
|
|
|
|
* this is actually necessary, but it seems like a good idea).
|
|
|
|
*
|
2002-09-02 03:05:06 +02:00
|
|
|
* The page version number and page size are packed together into a single
|
|
|
|
* uint16 field. This is for historical reasons: before PostgreSQL 7.3,
|
|
|
|
* there was no concept of a page version number, and doing it this way
|
|
|
|
* lets us pretend that pre-7.3 databases have page version number zero.
|
|
|
|
* We constrain page sizes to be multiples of 256, leaving the low eight
|
2003-12-11 22:21:55 +01:00
|
|
|
* bits available for a version number.
|
2002-09-02 03:05:06 +02:00
|
|
|
*
|
|
|
|
* Minimum possible page size is perhaps 64B to fit page header, opaque space
|
|
|
|
* and a minimal tuple; of course, in reality you want it much bigger, so
|
|
|
|
* the constraint on pagesize mod 256 is not an important restriction.
|
|
|
|
* On the high end, we can only support pages up to 32KB because lp_off/lp_len
|
|
|
|
* are 15 bits.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct PageHeaderData
|
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
/* XXX LSN is member of *any* block, not only page-organized ones */
|
|
|
|
XLogRecPtr pd_lsn; /* LSN: next byte after last byte of xlog
|
|
|
|
* record for last change to this page */
|
|
|
|
TimeLineID pd_tli; /* TLI of last change */
|
1997-09-08 04:41:22 +02:00
|
|
|
LocationIndex pd_lower; /* offset to start of free space */
|
|
|
|
LocationIndex pd_upper; /* offset to end of free space */
|
|
|
|
LocationIndex pd_special; /* offset to start of special space */
|
2002-09-02 03:05:06 +02:00
|
|
|
uint16 pd_pagesize_version;
|
2000-01-08 22:59:55 +01:00
|
|
|
ItemIdData pd_linp[1]; /* beginning of line pointer array */
|
1997-09-08 23:56:23 +02:00
|
|
|
} PageHeaderData;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
typedef PageHeaderData *PageHeader;
|
|
|
|
|
2002-09-02 03:05:06 +02:00
|
|
|
/*
|
2004-07-01 02:52:04 +02:00
|
|
|
* Page layout version number 0 is for pre-7.3 Postgres releases.
|
|
|
|
* Releases 7.3 and 7.4 use 1, denoting a new HeapTupleHeader layout.
|
2004-08-04 23:34:35 +02:00
|
|
|
* Release 8.0 changed the HeapTupleHeader layout again.
|
2002-09-02 03:05:06 +02:00
|
|
|
*/
|
2004-07-01 02:52:04 +02:00
|
|
|
#define PG_PAGE_LAYOUT_VERSION 2
|
2002-09-02 03:05:06 +02:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* page support macros
|
1996-08-28 03:59:28 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2003-03-28 21:17:13 +01:00
|
|
|
* PageIsValid
|
|
|
|
* True iff page is valid.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
2003-03-28 21:17:13 +01:00
|
|
|
#define PageIsValid(page) PointerIsValid(page)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2002-07-02 07:48:44 +02:00
|
|
|
/*
|
|
|
|
* line pointer does not count as part of header
|
|
|
|
*/
|
|
|
|
#define SizeOfPageHeaderData (offsetof(PageHeaderData, pd_linp[0]))
|
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* PageIsEmpty
|
1997-09-07 07:04:48 +02:00
|
|
|
* returns true iff no itemid has been allocated on the page
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
#define PageIsEmpty(page) \
|
2002-07-02 07:48:44 +02:00
|
|
|
(((PageHeader) (page))->pd_lower <= SizeOfPageHeaderData)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1996-10-18 09:39:07 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* PageIsNew
|
2003-03-28 21:17:13 +01:00
|
|
|
* returns true iff page has not been initialized (by PageInit)
|
1996-10-18 09:39:07 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#define PageIsNew(page) (((PageHeader) (page))->pd_upper == 0)
|
1996-10-18 09:39:07 +02:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* PageGetItemId
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns an item identifier of a page.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
#define PageGetItemId(page, offsetNumber) \
|
2000-01-08 22:59:55 +01:00
|
|
|
((ItemId) (&((PageHeader) (page))->pd_linp[(offsetNumber) - 1]))
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2002-07-02 07:48:44 +02:00
|
|
|
/*
|
|
|
|
* PageGetContents
|
|
|
|
* To be used in case the page does not contain item pointers.
|
|
|
|
*/
|
|
|
|
#define PageGetContents(page) \
|
|
|
|
((char *) (&((PageHeader) (page))->pd_linp[0]))
|
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/* ----------------
|
2002-09-02 03:05:06 +02:00
|
|
|
* macros to access page size info
|
1996-08-28 03:59:28 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* PageSizeIsValid
|
1997-09-07 07:04:48 +02:00
|
|
|
* True iff the page size is valid.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
1998-01-13 05:05:12 +01:00
|
|
|
#define PageSizeIsValid(pageSize) ((pageSize) == BLCKSZ)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* PageGetPageSize
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns the page size of a page.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* this can only be called on a formatted page (unlike
|
|
|
|
* BufferGetPageSize, which can be called on an unformatted page).
|
2003-03-28 21:17:13 +01:00
|
|
|
* however, it can be called on a page that is not stored in a buffer.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
#define PageGetPageSize(page) \
|
2002-09-02 03:05:06 +02:00
|
|
|
((Size) (((PageHeader) (page))->pd_pagesize_version & (uint16) 0xFF00))
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/*
|
2002-09-02 03:05:06 +02:00
|
|
|
* PageGetPageLayoutVersion
|
|
|
|
* Returns the page layout version of a page.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
2002-09-02 03:05:06 +02:00
|
|
|
#define PageGetPageLayoutVersion(page) \
|
|
|
|
(((PageHeader) (page))->pd_pagesize_version & 0x00FF)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PageSetPageSizeAndVersion
|
|
|
|
* Sets the page size and page layout version number of a page.
|
|
|
|
*
|
|
|
|
* We could support setting these two values separately, but there's
|
|
|
|
* no real need for it at the moment.
|
|
|
|
*/
|
|
|
|
#define PageSetPageSizeAndVersion(page, size, version) \
|
|
|
|
( \
|
|
|
|
AssertMacro(((size) & 0xFF00) == (size)), \
|
|
|
|
AssertMacro(((version) & 0x00FF) == (version)), \
|
|
|
|
((PageHeader) (page))->pd_pagesize_version = (size) | (version) \
|
|
|
|
)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* page special data macros
|
1996-08-28 03:59:28 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* PageGetSpecialSize
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns size of special space on a page.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
#define PageGetSpecialSize(page) \
|
1997-09-07 07:04:48 +02:00
|
|
|
((uint16) (PageGetPageSize(page) - ((PageHeader)(page))->pd_special))
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* PageGetSpecialPointer
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns pointer to special space on a page.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
#define PageGetSpecialPointer(page) \
|
1998-06-15 20:40:05 +02:00
|
|
|
( \
|
|
|
|
AssertMacro(PageIsValid(page)), \
|
|
|
|
(char *) ((char *) (page) + ((PageHeader) (page))->pd_special) \
|
|
|
|
)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-08-25 01:08:01 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* PageGetItem
|
1997-09-07 07:04:48 +02:00
|
|
|
* Retrieves an item on the given page.
|
1997-08-25 01:08:01 +02:00
|
|
|
*
|
|
|
|
* Note:
|
2000-01-08 22:59:55 +01:00
|
|
|
* This does not change the status of any of the resources passed.
|
1997-09-07 07:04:48 +02:00
|
|
|
* The semantics may change in the future.
|
1997-08-25 01:08:01 +02:00
|
|
|
*/
|
|
|
|
#define PageGetItem(page, itemId) \
|
1998-06-15 20:40:05 +02:00
|
|
|
( \
|
|
|
|
AssertMacro(PageIsValid(page)), \
|
|
|
|
AssertMacro((itemId)->lp_flags & LP_USED), \
|
|
|
|
(Item)(((char *)(page)) + (itemId)->lp_off) \
|
|
|
|
)
|
1997-08-25 01:08:01 +02:00
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* BufferGetPageSize
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns the page size within a buffer.
|
1997-08-25 01:08:01 +02:00
|
|
|
*
|
|
|
|
* Notes:
|
1997-09-07 07:04:48 +02:00
|
|
|
* Assumes buffer is valid.
|
1997-08-25 01:08:01 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* The buffer can be a raw disk block and need not contain a valid
|
|
|
|
* (formatted) disk page.
|
1997-08-25 01:08:01 +02:00
|
|
|
*/
|
2000-01-08 22:59:55 +01:00
|
|
|
/* XXX should dig out of buffer descriptor */
|
1997-08-25 01:08:01 +02:00
|
|
|
#define BufferGetPageSize(buffer) \
|
1998-06-15 20:40:05 +02:00
|
|
|
( \
|
|
|
|
AssertMacro(BufferIsValid(buffer)), \
|
|
|
|
(Size)BLCKSZ \
|
|
|
|
)
|
1997-08-25 01:08:01 +02:00
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* BufferGetPage
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns the page associated with a buffer.
|
1997-08-25 01:08:01 +02:00
|
|
|
*/
|
|
|
|
#define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
|
|
|
|
|
1998-04-24 16:43:33 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* PageGetMaxOffsetNumber
|
1998-04-24 16:43:33 +02:00
|
|
|
* Returns the maximum offset number used by the given page.
|
2000-01-08 22:59:55 +01:00
|
|
|
* Since offset numbers are 1-based, this is also the number
|
|
|
|
* of items on the page.
|
1998-04-24 16:43:33 +02:00
|
|
|
*
|
2004-06-05 19:42:46 +02:00
|
|
|
* NOTE: if the page is not initialized (pd_lower == 0), we must
|
|
|
|
* return zero to ensure sane behavior. Accept double evaluation
|
|
|
|
* of the argument so that we can ensure this.
|
1998-04-24 16:43:33 +02:00
|
|
|
*/
|
|
|
|
#define PageGetMaxOffsetNumber(page) \
|
2004-06-05 19:42:46 +02:00
|
|
|
(((PageHeader) (page))->pd_lower <= SizeOfPageHeaderData ? 0 : \
|
|
|
|
((((PageHeader) (page))->pd_lower - SizeOfPageHeaderData) \
|
|
|
|
/ sizeof(ItemIdData)))
|
1998-04-24 16:43:33 +02:00
|
|
|
|
2000-09-07 11:58:38 +02:00
|
|
|
#define PageGetLSN(page) \
|
|
|
|
(((PageHeader) (page))->pd_lsn)
|
|
|
|
#define PageSetLSN(page, lsn) \
|
2000-11-20 22:12:26 +01:00
|
|
|
(((PageHeader) (page))->pd_lsn = (lsn))
|
2000-09-07 11:58:38 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
#define PageGetTLI(page) \
|
|
|
|
(((PageHeader) (page))->pd_tli)
|
|
|
|
#define PageSetTLI(page, tli) \
|
|
|
|
(((PageHeader) (page))->pd_tli = (tli))
|
2000-09-07 11:58:38 +02:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* extern declarations
|
1996-08-28 03:59:28 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void PageInit(Page page, Size pageSize, Size specialSize);
|
2003-03-28 21:17:13 +01:00
|
|
|
extern bool PageHeaderIsValid(PageHeader page);
|
1998-09-01 06:40:42 +02:00
|
|
|
extern OffsetNumber PageAddItem(Page page, Item item, Size size,
|
1997-09-07 07:04:48 +02:00
|
|
|
OffsetNumber offsetNumber, ItemIdFlags flags);
|
1997-09-08 04:41:22 +02:00
|
|
|
extern Page PageGetTempPage(Page page, Size specialSize);
|
|
|
|
extern void PageRestoreTempPage(Page tempPage, Page oldPage);
|
2001-03-22 05:01:46 +01:00
|
|
|
extern int PageRepairFragmentation(Page page, OffsetNumber *unused);
|
1997-09-08 04:41:22 +02:00
|
|
|
extern Size PageGetFreeSpace(Page page);
|
|
|
|
extern void PageIndexTupleDelete(Page page, OffsetNumber offset);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* BUFPAGE_H */
|