> 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/server-administration/wal/wal-internals.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 28.6. WAL 內部運作 \#

### 28.6. WAL 內部運作 [#](#wal-internals) <a href="#wal-internals" id="wal-internals"></a>

WAL 會自動啟用；除了確保 WAL 檔案的磁碟空間需求 獲得滿足，以及完成任何必要的調校（請參閱 [28.5 節](https://docs.postgresql.tw/19/server-administration/wal/wal-configuration.md)）之外，管理者不需要採取任何其他動作。

每當寫入一筆新的 WAL 紀錄時，該紀錄就會附加到 WAL 檔案中。插入位置是以日誌序號（Log Sequence Number，LSN）表示， 它是 WAL 中的位元組位移量，會隨著每筆新紀錄單調遞增。 LSN 值是以 [`pg_lsn`](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-pg-lsn.md) 資料型別傳回。 這些值可以互相比較，以計算兩者之間相隔的 WAL 資料量， 因此可用來衡量複寫與復原的進度。

WAL 檔案儲存在資料目錄底下的 `pg_wal` 目錄中，形式為一組區段檔案，通常每個檔案大小為 16 MB （但可透過修改 initdb 的 `--wal-segsize` 選項變更此大小）。 每個區段又分為多個頁面，通常每個頁面 8 kB（此大小可透過 `--with-wal-blocksize` configure 選項變更）。WAL 紀錄標頭的說明位於 `access/xlogrecord.h`；紀錄內容則取決於所記錄事件的 型別。區段檔案的檔名採用遞增編號，從 `000000010000000000000001` 開始。這些編號不會回捲， 但要耗盡可用的編號範圍，需要非常、非常久的時間。

若 WAL 位於與主要資料庫檔案不同的磁碟上，會相當有利。 做法是將 `pg_wal` 目錄搬移到另一個位置 （當然，這必須在伺服器關閉的情況下進行）， 再從主要資料目錄中的原始位置，建立一個指向新位置的符號連結。

WAL 的目的，是確保日誌會在資料庫紀錄變更之前先寫入， 但若磁碟機向核心回報寫入成功， 實際上卻只是將資料快取起來、尚未真正寫入磁碟， 這項保證就可能被破壞。在這種情況下若發生電源故障， 可能導致無法復原的資料損毀。管理者應設法確保， 存放 PostgreSQL WAL 檔案的磁碟不會做出這類不實回報。 （請參閱[28.1 節](https://docs.postgresql.tw/19/server-administration/wal/wal-reliability.md)。）

在完成某次檢查點（checkpoint）且 WAL 已安全刷寫至磁碟之後， 該檢查點的位置會儲存於 `pg_control` 檔案中。 因此，在復原開始時，伺服器會先讀取 `pg_control`， 接著讀取檢查點紀錄；然後從檢查點紀錄所指出的 WAL 位置開始 向前掃描，執行 REDO 操作。由於在檢查點之後、資料頁面第一次 被修改時，該頁面的完整內容都會被寫入 WAL （假設 [full\_page\_writes](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-wal.md#GUC-FULL-PAGE-WRITES) 未被停用）， 因此自該檢查點以來變更過的所有頁面，都能被還原到一致的 狀態。

為因應 `pg_control` 損毀的情況，我們應支援以反向順序——由新到舊—— 掃描既有 WAL 區段，以尋找最新檢查點的可能性。 但這項功能目前尚未實作。`pg_control` 的檔案大小 極小（不到一個磁碟頁面），因此不會有部分寫入的問題； 截至撰寫本文為止，尚未有任何資料庫故障案例， 是單純因為無法讀取 `pg_control` 本身所導致。 因此，雖然理論上它是一個弱點，但 `pg_control` 在實務上似乎並不構成問題。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/wal-internals.html)（原文版本：18.6；核對日期：2026-09-24）


---

# 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/server-administration/wal/wal-internals.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.
