# 29. 邏輯複寫（Logical Replication）

邏輯複寫（Logical Replication）是一種依據複寫指標（通常是主鍵）複製資料物件及其更新的方法。我們使用術語邏輯與物理複寫相對比，物理複寫使用確切的區塊位址進行每一個字元組的複寫。PostgreSQL 同時支持這兩種機制，請參閱[第 27 章](/server-administration/high-availability-load-balancing-and-replication.md)。邏輯複寫允許對資料複寫和安全性進行更精細的控制。

邏輯複寫使用 publish（發佈）和 subscribe（訂閱）模式，其中一個或多個訂閱者 subscribe 發布者節點上的一個或多個 publish。 訂閱戶從他們訂閱的 publish 中提取資料，並可能隨後重新發布資料以允許串聯複寫或更複雜的複寫架構。

資料表的邏輯複寫通常始於對發佈者資料庫上的資料進行快照並將其複寫到訂閱伺服器。一旦完成，發佈者的變化就會即時發送給訂閱者。訂閱者按照與發佈者相同的順序變動資料，以確保單個訂閱內的發佈的交易事務一致性。這種資料複寫方法有時被稱為交易事務複寫。

典型的邏輯複寫情況有：

* 在訂閱者時常向單個資料庫或資料庫的子集發送增量變更時。
* 當訂閱者收到個別的變更時能觸發事件。
* 將多個資料庫合併成一個資料庫（例如為了分析的需求）。
* 在不同的 PostgreSQL 版本之間複寫。
* 在不同平台（例如 Linux 到 Windows）上的 PostgreSQL 伺服器之間的複寫
* 將複寫的資料給予不同的使用者群組存取權限。
* 在多個資料庫之間共享資料庫的一部份。

訂閱戶資料庫的行為與任何其他 PostgreSQL 的行為相同，並且可以透過定義其自己的發佈來用作其他資料庫的發佈者。當訂閱者被應用程序視為唯讀時，單個訂閱就不會發生衝突。另一方面，如果應用程序或其他使用者對同一組資料表執行了其他的寫入操作，則可能會出現衝突。


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.postgresql.tw/server-administration/logical-replication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
