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

# 26.3. 失效切換 \#

### 26.3. 失效切換 [#](#warm-standby-failover) <a href="#warm-standby-failover" id="warm-standby-failover"></a>

若主要伺服器失效，待命伺服器應開始進行失效切換程序。

若待命伺服器失效，則不需要進行失效切換。如果待命伺服器能夠重新啟動—— 即使是過一段時間之後——那麼復原程序也可以立即重新啟動， 善用可重新啟動的復原機制。如果待命伺服器無法重新啟動， 則應建立一個全新的待命伺服器實例。

若主要伺服器失效、待命伺服器成為新的主要伺服器，而舊的主要伺服器 之後又重新啟動，你必須具備一種機制，用來告知舊的主要伺服器 它已不再是主要伺服器。這有時稱為 STONITH（Shoot The Other Node In The Head，即「射擊另一節點的頭部」），目的是避免兩個系統都認為 自己是主要伺服器的情況，因為這會導致混亂、並最終造成資料遺失。

許多失效切換系統只使用兩個系統：主要伺服器與待命伺服器， 透過某種心跳機制持續確認兩者之間的連線狀態，以及主要伺服器的 存活狀態。也可以使用第三個系統（稱為見證伺服器，witness server） 來防止某些不當失效切換的情況發生，但除非該系統經過充分細心的 設置與嚴謹的測試，否則增加的複雜度可能不划算。

PostgreSQL 並不提供識別主要伺服器失效、並通知待命資料庫伺服器 所需的系統軟體。有許多這類工具存在，並且與成功進行失效切換所需的 作業系統機制（例如 IP 位址遷移）有良好的整合。

一旦失效切換到待命伺服器完成，系統中就只會剩下單一伺服器在運作， 這稱為退化狀態（degenerate state）。原本的待命伺服器現在成為 主要伺服器，而原本的主要伺服器則已下線，且可能會持續處於停機狀態。 若要恢復正常運作，必須重新建立一個待命伺服器——可以在原主要伺服器 系統恢復運作時，於其上重新建立，也可以在第三個、可能是全新的系統上 建立。[pg\_rewind](https://docs.postgresql.tw/19/reference/reference-server/app-pgrewind.md) 工具程式可用來加快此程序在大型叢集上的速度。完成之後，即可視為 主要伺服器與待命伺服器已互換角色。有些人會選擇使用第三台伺服器， 在新的待命伺服器重新建立完成之前，為新的主要伺服器提供備援， 不過這顯然會使系統組態與作業流程更加複雜。

因此，從主要伺服器切換到待命伺服器可以很快完成，但重新準備失效切換 叢集則需要一些時間。定期地在主要伺服器與待命伺服器之間切換是有用的 做法，因為這可以讓每個系統定期停機進行維護；這同時也是測試失效切換 機制的方式，以確保在真正需要時它確實能夠運作。建議撰寫書面的管理程序。

若你已選擇啟用邏輯複寫插槽同步（請參閱 [第 47.2.3 節](https://docs.postgresql.tw/19/server-programming/logicaldecoding/logicaldecoding-explanation.md#logicaldecoding-replication-slots-synchronization)）， 那麼在切換到待命伺服器之前，建議先檢查待命伺服器上已同步的邏輯插槽 是否已準備好進行失效切換。你可以依照 [第 29.3 節](https://docs.postgresql.tw/19/server-administration/logical-replication/logical-replication-failover.md) 所述的步驟來進行檢查。

若要觸發日誌傳送（log-shipping）待命伺服器的失效切換，請執行 `pg_ctl promote`，或呼叫 `pg_promote()`。 如果你設置的報表伺服器僅用於分擔主要伺服器的唯讀查詢負載， 而非用於高可用性目的，則不需要進行提升（promote）。

***

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


---

# 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/high-availability/warm-standby-failover.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.
