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

# 28.4. 非同步確認 \#

### 28.4. 非同步確認 [#](#wal-async-commit) <a href="#wal-async-commit" id="wal-async-commit"></a>

*非同步確認*（asynchronous commit）是一項選項，能讓交易更快完成， 代價是若資料庫發生當機，最近的幾筆交易可能會遺失。 在許多應用情境中，這是可以接受的取捨。

如前一節所述，交易確認（commit）通常是*同步*進行的： 伺服器會等待該交易的 WAL 紀錄排清至永久儲存體之後， 才會向用戶端回報成功。因此，用戶端可以確信， 一旦回報某筆交易已確認，即使伺服器隨即當機，該交易仍會被保留下來。 不過，對於短交易而言，這段等待時間，佔整體交易時間的 很大一部分。選擇非同步確認模式，意味著伺服器一旦 在邏輯上完成該交易，就會立即回報成功，而不必等到 它所產生的 WAL 紀錄，實際寫入磁碟。對於小型交易而言， 這能大幅提升輸出量（throughput）。

非同步確認會帶來資料遺失的風險。在向用戶端回報交易完成， 與該交易真正確認完成（也就是保證即使伺服器當機也不會遺失）之間， 存在著一段短暫的時間窗口。因此，若用戶端會依據「該交易必定會被記住」 這項假設，去執行外部動作，就不應該使用非同步確認。 舉例來說，銀行在記錄自動櫃員機（ATM）的提款交易時， 必定不會使用非同步確認。但在許多情境中， 例如事件記錄，並不需要這種強而有力的保證。

使用非同步確認所承擔的風險，是資料遺失，而非資料損毀。 若資料庫發生當機，它會透過重播 WAL，直到最後一筆 已排清的紀錄為止，以進行復原。因此，資料庫會被還原到 自我一致的狀態，但任何尚未排清至磁碟的交易， 都不會反映在該狀態中。因此，最終的結果，就是遺失 最後幾筆交易。由於這些交易是依確認順序重播的， 因此不會產生不一致——舉例來說，若交易 B 所做的變更， 依賴於先前交易 A 的效果，就不可能發生 A 的效果遺失、 而 B 的效果卻被保留下來的情況。

使用者可以針對每筆交易，個別選擇其確認模式，因此 可以讓同步與非同步確認的交易同時並行執行。這讓 效能與交易持久性保證之間，能夠有彈性地取捨。 確認模式由使用者可設定的參數 [synchronous\_commit](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-wal/synchronous-commit.md#guc-synchronous-commit) 控制， 可以透過任何一種設定組態參數的方式來變更。任何一筆交易 所使用的模式，取決於該交易開始確認時， `synchronous_commit` 的值。

某些工具程式指令，例如 `DROP TABLE`， 無論 `synchronous_commit` 的設定為何，都會被強制以 同步方式確認。這是為了確保伺服器的檔案系統， 與資料庫的邏輯狀態保持一致。支援兩階段確認的指令， 例如 `PREPARE TRANSACTION`，也一律是同步的。

若資料庫在非同步確認、與該交易的 WAL 紀錄寫入之間 的風險期間發生當機，則該交易期間所做的變更， *將會*遺失。這段風險期間的長度是有限制的， 因為有一個背景程序（「WAL 寫入器」） 會每隔 [wal\_writer\_delay](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-wal.md#GUC-WAL-WRITER-DELAY) 毫秒， 將尚未寫入的 WAL 紀錄排清至磁碟。風險期間的實際最大長度， 是 `wal_writer_delay` 的三倍，因為 WAL 寫入器 在忙碌期間，其設計會傾向於一次寫入整個頁面。

#### 注意

immediate 模式的關閉，等同於伺服器當機， 因此會導致任何尚未排清的非同步確認遺失。

非同步確認所展現的行為，與設定 [fsync](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-wal.md#GUC-FSYNC) = off 並不相同。 `fsync` 是一項伺服器全域設定， 會改變所有交易的行為。它會停用 PostgreSQL 內部 所有嘗試同步不同資料庫部分寫入動作的邏輯， 因此系統當機（也就是硬體或作業系統當機， 而非 PostgreSQL 本身的故障）可能導致資料庫狀態出現 任意程度的嚴重損毀。在許多情境中，非同步確認 所帶來的效能提升，幾乎等同於關閉 `fsync` 所能達到的效果，但卻不會有資料損毀的風險。

[commit\_delay](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-wal.md#GUC-COMMIT-DELAY) 聽起來也與非同步確認 非常相似，但它實際上是一種同步確認的方法 （事實上，在非同步確認期間，`commit_delay` 會被忽略）。`commit_delay` 會在某筆交易將 WAL 排清至磁碟之前，造成一段延遲，期望藉由這一次排清動作， 同時服務其他大約在同一時間確認的交易。可以將這項設定， 視為一種延長時間窗口的方式，讓更多交易能夠加入 即將共同參與同一次排清的群組， 藉此將排清成本分攤給多筆交易。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/wal-async-commit.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-async-commit.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.
