> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/internals/storage/storage-page-layout.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 66.6. 資料庫頁面配置 \#

### 66.6. 資料庫頁面配置 [#](#storage-page-layout) <a href="#storage-page-layout" id="storage-page-layout"></a>

[66.6.1. 資料表資料列配置](https://docs.postgresql.tw/19/internals/storage/storage-page-layout/storage-tuple-layout.md#storage-tuple-layout)

本節概述 PostgreSQL 資料表與索引中所使用的頁面格式。\[19]序列與 TOAST 資料表的格式與一般資料表完全相同。

在以下的說明中，假設一個*位元組*包含 8 個位元。此外，\*項目（item）\*一詞指的是儲存在頁面上的個別資料值。在資料表中，一個項目就是一筆資料列；在索引中，一個項目就是一筆索引項目。

每個資料表與索引都儲存為一個由固定大小之*頁面*組成的陣列（通常為 8 kB，不過在編譯伺服器時可以選擇不同的頁面大小）。在資料表中，所有頁面在邏輯上都是相等的，因此特定的項目（資料列）可以儲存在任何頁面中。在索引中，第一個頁面通常保留作為存放控制資訊的*中繼頁面（metapage）*，而索引內可能會有不同類型的頁面，視索引存取方法而定。

[表 66.2](#page-table) 顯示頁面的整體配置。每個頁面都有五個部分。

#### 表 66.2. 頁面整體配置 [#](#page-table) <a href="#page-table" id="page-table"></a>

| 項目                  | 說明                                           |
| ------------------- | -------------------------------------------- |
| PageHeaderData      | 長度為 24 位元組。包含頁面的一般資訊，包括可用空間指標。               |
| ItemIdData          | 指向實際項目的項目識別碼陣列。每個項目是一組（位移, 長度）配對。每個項目 4 位元組。 |
| 可用空間（Free space）    | 尚未配置的空間。新的項目識別碼從這個區域的開頭配置，新的項目則從結尾配置。        |
| 項目（Items）           | 實際的項目本身。                                     |
| 特殊空間（Special space） | 索引存取方法專屬的資料。不同的方法儲存不同的資料。在一般資料表中為空。          |

<br>

每個頁面的前 24 個位元組是頁面標頭（`PageHeaderData`）。其格式詳見[表 66.3](#pageheaderdata-table)。第一個欄位追蹤與此頁面相關的最近一筆 WAL 項目。若啟用了[資料檢查碼](https://docs.postgresql.tw/19/server-administration/wal/checksums.md)，第二個欄位會包含頁面檢查碼。接下來是一個包含旗標位元的 2 位元組欄位。之後是三個 2 位元組的整數欄位（`pd_lower`、`pd_upper` 與 `pd_special`）。它們分別包含從頁面開頭到未配置空間開頭、到未配置空間結尾，以及到特殊空間開頭的位元組位移。頁面標頭接下來的 2 個位元組 `pd_pagesize_version`，同時儲存頁面大小與版本標示。從 PostgreSQL 8.3 開始，版本號碼為 4；PostgreSQL 8.1 與 8.2 使用版本號碼 3；PostgreSQL 8.0 使用版本號碼 2；PostgreSQL 7.3 與 7.4 使用版本號碼 1；更早的版本使用版本號碼 0。（在這些版本中，大多數版本的基本頁面配置與標頭格式都沒有改變，但堆積資料列標頭的配置有所改變。）頁面大小基本上只是用來交叉核對；一個安裝中不支援使用超過一種頁面大小。最後一個欄位是一個提示，顯示修剪（prune）該頁面是否可能有益：它追蹤頁面上最舊的未修剪 XMAX。

#### 表 66.3. PageHeaderData 配置 [#](#pageheaderdata-table) <a href="#pageheaderdata-table" id="pageheaderdata-table"></a>

| 欄位                    | 型別             | 長度    | 說明                                  |
| --------------------- | -------------- | ----- | ----------------------------------- |
| pd\_lsn               | PageXLogRecPtr | 8 位元組 | LSN：此頁面最後一次變更之 WAL 紀錄最後一個位元組的下一個位元組 |
| pd\_checksum          | uint16         | 2 位元組 | 頁面檢查碼                               |
| pd\_flags             | uint16         | 2 位元組 | 旗標位元                                |
| pd\_lower             | LocationIndex  | 2 位元組 | 到可用空間開頭的位移                          |
| pd\_upper             | LocationIndex  | 2 位元組 | 到可用空間結尾的位移                          |
| pd\_special           | LocationIndex  | 2 位元組 | 到特殊空間開頭的位移                          |
| pd\_pagesize\_version | uint16         | 2 位元組 | 頁面大小與配置版本號碼資訊                       |
| pd\_prune\_xid        | TransactionId  | 4 位元組 | 頁面上最舊的未修剪 XMAX，若沒有則為零               |

<br>

所有細節都可以在 `src/include/storage/bufpage.h` 中找到。

頁面標頭之後是項目識別碼（`ItemIdData`），每個需要四個位元組。項目識別碼包含到項目開頭的位元組位移、項目以位元組計的長度，以及一些會影響其解讀方式的屬性位元。新的項目識別碼會視需要從未配置空間的開頭配置。目前存在的項目識別碼數量可以透過查看 `pd_lower` 來判斷，配置新的識別碼時會增加這個值。由於項目識別碼在被釋放之前永遠不會移動，因此即使項目本身為了壓縮可用空間而在頁面上移動，仍可長期使用該識別碼的索引來參照該項目。事實上，PostgreSQL 所建立的每一個指向項目的指標（`ItemPointer`，也稱為 `CTID`）都是由頁面編號與項目識別碼的索引所組成。

項目本身儲存在從未配置空間結尾往回配置的空間中。確切的結構會因資料表要包含的內容而異。資料表與序列都使用名為 `HeapTupleHeaderData` 的結構，說明如下。

最後一個區段是「特殊區段」，可以包含存取方法想要儲存的任何內容。例如，b-tree 索引會儲存指向頁面左右相鄰頁面的連結，以及一些與索引結構相關的其他資料。一般資料表完全不使用特殊區段（藉由將 `pd_special` 設定為等於頁面大小來表示）。

[圖 66.1](#storage-page-layout-figure) 說明這些部分在頁面中的配置方式。

#### 圖 66.1. 頁面配置 [#](#storage-page-layout-figure) <a href="#storage-page-layout-figure" id="storage-page-layout-figure"></a>

<figure><img src="https://253668857-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F892nl51gFukGPxnuxeE0%2Fuploads%2Fgit-blob-ccc504cdabc6e67d1c1b6d4e7e87003dca498325%2F68.6.pagelayout.png?alt=media" alt="圖 66.1. 頁面配置"><figcaption></figcaption></figure>

<br>

#### 66.6.1. 資料表資料列配置 [#](#storage-tuple-layout) <a href="#storage-tuple-layout" id="storage-tuple-layout"></a>

完整內容請見 [66.6.1. 資料表資料列配置](https://docs.postgresql.tw/19/internals/storage/storage-page-layout/storage-tuple-layout.md#storage-tuple-layout)。

<br>

***

[\[19\]](#id-1.10.18.8.2.2) 實際上，無論是資料表存取方法還是索引存取方法，都不一定要使用這種頁面格式。`heap` 資料表存取方法一律使用這種格式。所有現有的索引方法也都使用這種基本格式，但保存在索引中繼頁面上的資料通常不遵循項目配置規則。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/storage-page-layout.html)（原文版本：18.6；核對日期：2026-10-17）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/internals/storage/storage-page-layout.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
