From c7a165adc64e3e67e0dcee4088d84a0638b3515a Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 2 Sep 2002 01:05:06 +0000 Subject: Code review for HeapTupleHeader changes. Add version number to page headers (overlaying low byte of page size) and add HEAP_HASOID bit to t_infomask, per earlier discussion. Simplify scheme for overlaying fields in tuple header (no need for cmax to live in more than one place). Don't try to clear infomask status bits in tqual.c --- not safe to do it there. Don't try to force output table of a SELECT INTO to have OIDs, either. Get rid of unnecessarily complex three-state scheme for TupleDesc.tdhasoids, which has already caused one recent failure. Improve documentation. --- doc/src/sgml/page.sgml | 159 +++++++++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 65 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/page.sgml b/doc/src/sgml/page.sgml index 7551085dc94..d7096a4bbe1 100644 --- a/doc/src/sgml/page.sgml +++ b/doc/src/sgml/page.sgml @@ -4,13 +4,17 @@ -A description of the database file default page format. +A description of the database file page format. -This section provides an overview of the page format used by PostgreSQL -tables. User-defined access methods need not use this page format. +This section provides an overview of the page format used by +PostgreSQL tables and indexes. (Index +access methods need not use this page format. At present, all index +methods do use this basic format, but the data kept on index metapages +usually doesn't follow the item layout rules exactly.) TOAST tables +and sequences are formatted just like a regular table. @@ -18,15 +22,13 @@ In the following explanation, a byte is assumed to contain 8 bits. In addition, the term item -refers to data that is stored in PostgreSQL tables. +refers to an individual data value that is stored on a page. In a table, +an item is a tuple (row); in an index, an item is an index entry. - shows how pages in both normal - PostgreSQL tables and - PostgreSQL indexes (e.g., a B-tree index) -are structured. This structure is also used for toast tables and sequences. + shows the basic layout of a page. There are five parts to each page. @@ -48,12 +50,13 @@ Item PageHeaderData - 20 bytes long. Contains general information about the page to allow to access it. + 20 bytes long. Contains general information about the page, including +free space pointers. -itemPointerData -List of (offset,length) pairs pointing to the actual item. +ItemPointerData +Array of (offset,length) pairs pointing to the actual items. @@ -62,13 +65,14 @@ Item -items -The actual items themselves. Different access method have different data here. +Items +The actual items themselves. Special Space -Access method specific data. Different method store different data. Unused by normal tables. +Index access method specific data. Different methods store different +data. Empty in ordinary tables. @@ -78,11 +82,12 @@ Item The first 20 bytes of each page consists of a page header - (PageHeaderData). It's format is detailed in . The first two fields deal with WAL related stuff. This is followed by three 2-byte integer fields - (lower, upper, and - special). These represent byte offsets to the start + (pd_lower, pd_upper, + and pd_special). These represent byte offsets to + the start of unallocated space, to the end of unallocated space, and to the start of the special space. @@ -104,7 +109,7 @@ Item pd_lsn XLogRecPtr - 6 bytes + 8 bytes LSN: next byte after last byte of xlog @@ -132,38 +137,51 @@ Item Offset to start of special space. - pd_opaque - OpaqueData + pd_pagesize_version + uint16 2 bytes - AM-generic information. Currently just stores the page size. + Page size and layout version number information. + + All the details may be found in src/include/storage/bufpage.h. + + Special space is a region at the end of the page that is allocated at page initialization time and contains information specific to an access method. - The last 2 bytes of the page header, opaque, - currently only stores the page size. Page size is stored in each page - because frames in the buffer pool may be subdivided into equal sized pages - on a frame by frame basis within a table (is this true? - mvo). - + The last 2 bytes of the page header, + pd_pagesize_version, store both the page size + and a version indicator. Beginning with + PostgreSQL 7.3 the version number is 1; prior + releases used version number 0. (The basic page layout and header format + has not changed, but the layout of heap tuple headers has.) The page size + is basically only present as a cross-check; there is no support for having + more than one page size in an installation. Following the page header are item identifiers - (ItemIdData). New item identifiers are allocated - from the first four bytes of unallocated space. Because an item - identifier is never moved until it is freed, its index may be used to - indicate the location of an item on a page. In fact, every pointer to an - item (ItemPointer, also know as - CTID) created by - PostgreSQL consists of a frame number and an - index of an item identifier. An item identifier contains a byte-offset to + (ItemIdData), each requiring four bytes. + An item identifier contains a byte-offset to the start of an item, its length in bytes, and a set of attribute bits which affect its interpretation. + New item identifiers are allocated + as needed from the beginning of the unallocated space. + The number of item identifiers present can be determined by looking at + pd_lower, which is increased to allocate a new identifier. + Because an item + identifier is never moved until it is freed, its index may be used on a + long-term basis to reference an item, even when the item itself is moved + around on the page to compact free space. In fact, every pointer to an + item (ItemPointer, also known as + CTID) created by + PostgreSQL consists of a page number and the + index of an item identifier. @@ -171,8 +189,8 @@ Item The items themselves are stored in space allocated backwards from the end of unallocated space. The exact structure varies depending on what the - table is to contain. Sequences and tables both use a structure named - HeapTupleHeaderData, describe below. + table is to contain. Tables and sequences both use a structure named + HeapTupleHeaderData, described below. @@ -180,20 +198,33 @@ Item The final section is the "special section" which may contain anything the access method wishes to store. Ordinary tables do not use this at all - (indicated by setting the offset to the pagesize). + (indicated by setting pd_special to equal the pagesize). - All tuples are structured the same way. A header of around 31 bytes - followed by an optional null bitmask and the data. The header is detailed - below in . The null bitmask is - only present if the HEAP_HASNULL bit is set in the - t_infomask. If it is present it takes up the space - between the end of the header and the beginning of the data, as indicated - by the t_hoff field. In this list of bits, a 1 bit - indicates not-null, a 0 bit is a null. + All table tuples are structured the same way. There is a fixed-size + header (occupying 23 bytes on most machines), followed by an optional null + bitmap, an optional object ID field, and the user data. The header is + detailed + in . The actual user data + (fields of the tuple) begins at the offset indicated by + t_hoff, which must always be a multiple of the MAXALIGN + distance for the platform. + The null bitmap is + only present if the HEAP_HASNULL bit is set in + t_infomask. If it is present it begins just after + the fixed header and occupies enough bytes to have one bit per data column + (that is, t_natts bits altogether). In this list of bits, a + 1 bit indicates not-null, a 0 bit is a null. When the bitmap is not + present, all columns are assumed not-null. + The object ID is only present if the HEAP_HASOID bit + is set in t_infomask. If present, it appears just + before the t_hoff boundary. Any padding needed to make + t_hoff a MAXALIGN multiple will appear between the null + bitmap and the object ID. (This in turn ensures that the object ID is + suitably aligned.) @@ -211,34 +242,34 @@ Item - t_oid - Oid + t_xmin + TransactionId 4 bytes - OID of this tuple + insert XID stamp t_cmin CommandId 4 bytes - insert CID stamp + insert CID stamp (overlays with t_xmax) - t_cmax - CommandId + t_xmax + TransactionId 4 bytes - delete CID stamp + delete XID stamp - t_xmin - TransactionId + t_cmax + CommandId 4 bytes - insert XID stamp + delete CID stamp (overlays with t_xvac) - t_xmax + t_xvac TransactionId 4 bytes - delete XID stamp + XID for VACUUM operation moving tuple t_ctid @@ -256,30 +287,28 @@ Item t_infomask uint16 2 bytes - Various flags + various flags t_hoff uint8 1 byte - length of tuple header. Also offset of data. + offset to user data - - All the details may be found in src/include/storage/bufpage.h. - + All the details may be found in src/include/access/htup.h. Interpreting the actual data can only be done with information obtained from other tables, mostly pg_attribute. The - particular fields are attlen and - attalign. There is no way to directly get a + particular fields are attlen and + attalign. There is no way to directly get a particular attribute, except when there are only fixed width fields and no NULLs. All this trickery is wrapped up in the functions heap_getattr, fastgetattr @@ -293,7 +322,7 @@ Item the next. Then make sure you have the right alignment. If the field is a fixed width field, then all the bytes are simply placed. If it's a variable length field (attlen == -1) then it's a bit more complicated, - using the variable length structure varattrib. + using the variable length structure varattrib. Depending on the flags, the data may be either inline, compressed or in another table (TOAST). -- cgit v1.2.3