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

# 28.3. 預寫式日誌（Write-Ahead Logging, WAL） \#

### 28.3. 預寫式日誌（Write-Ahead Logging, WAL） [#](#wal-intro) <a href="#wal-intro" id="wal-intro"></a>

*預寫式日誌*（Write-Ahead Logging，WAL） 是確保資料完整性的一種標準方法。大多數（若非全部） 探討交易處理的書籍中，都能找到詳細的說明。簡而言之，WAL 的核心 概念是：對資料檔案（資料表與索引所在之處）的變更，必須先經過記錄， 也就是描述該變更的 WAL 紀錄先排清（flush）至永久儲存體之後， 才能寫入資料檔案。若遵循這項程序，就不需要在每次交易確認（commit）時， 都將資料頁面排清至磁碟，因為我們知道，一旦發生當機，仍可 利用日誌復原資料庫：任何尚未套用到資料頁面的變更，都能從 WAL 紀錄重做。（這稱為向前復原，也就是所謂的 REDO。）

#### 提示

由於 WAL 能在當機後還原資料庫檔案的內容，因此針對資料檔案或 WAL 檔案而言，並不需要具日誌功能的檔案系統，也能達成可靠的儲存。 事實上，日誌功能所帶來的額外負擔，可能會降低效能，特別是 當日誌功能導致檔案系統的*資料*被排清至磁碟時。 所幸，在使用日誌式檔案系統時，通常可以透過掛載選項， 停用日誌過程中的資料排清，例如在 Linux ext3 檔案系統上使用 `data=writeback` 選項。日誌式檔案系統確實能提升 當機後的開機速度。

使用 WAL 可以大幅減少磁碟寫入次數，因為只需要將 WAL 檔案 排清至磁碟，即可保證某筆交易已確認，而不必將該交易所變更的 每一個資料檔案都排清。WAL 檔案是循序寫入的， 因此同步 WAL 的成本，遠低於排清資料頁面的成本。對於處理 大量觸及資料儲存體不同部分之小型交易的伺服器而言，這一點尤其 顯著。此外，當伺服器正在處理大量小型並行交易時， 可能只需對 WAL 檔案執行一次 `fsync`， 就足以確認多筆交易。

WAL 也讓系統能夠支援線上備份與時間點復原，如 [25.3 節](https://docs.postgresql.tw/19/server-administration/backup/continuous-archiving.md)所述。透過歸檔 WAL 資料， 我們可以支援還原至現有 WAL 資料所涵蓋範圍內的任何時間點： 只需安裝先前的實體備份，並重播 WAL 直到所需的時間點為止即可。 此外，實體備份也不必是資料庫狀態的瞬間快照——若它是在 一段時間內產生的，那麼重播該段期間的 WAL， 就能修正任何內部不一致之處。

***

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


---

# 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-intro.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.
