Databases for PostgreSQL作為邏輯複製目標
IBM Cloud® Databases for PostgreSQL 支援 邏輯複製,您可以建立子訂閱者或發佈者。 您也可以將外部 PostgreSQL 設定為發佈者,並將 Databases for PostgreSQL 部署設定為訂閱者,然後將資料從外部資料庫複製到部署中。
邏輯複製功能僅適用於運行 PostgreSQL 10或更高版本的部署環境。 PostgreSQL 文件中的連結將引導您前往 PostgreSQL 的最新版本。 如果您需要特定版本的文件,可在 PostgreSQL 文件頁面上找到各版本 PostgreSQL 的連結。
配置發布者
外部PostgreSQL實例是發布者,需要進行配置,以便您的Databases for PostgreSQL部署能夠連接並能夠正確提取資料。
發行商功能
create_publisher
在每個資料庫中,您可以建立多個刊物,將變更發佈到不同的子訂閱器中。
Arguments:
publisher_name The Unique name of publisher.
for table To publish single/list of tables
for all table To publish all tables, along with future tables.
for all tables in schema To publish all tables in schema, along wth future tables.
Usage:
exampledb=> CREATE PUBLICATION my_publication;
CREATE PUBLICATION
list_publisher
列出每個資料庫中執行的出版品數量。
Usage:
exampledb=# select * from pg_publication;
oid | pubname | pubowner | puballtables | pubinsert | pubupdate | pubdelete | pubtruncate
-------+----------------+----------+--------------+-----------+-----------+-----------+-------------
16401 | my_publication | 10 | t | t | t | t | t
alter_publisher
修改出版物的定義。
Syntax:
ALTER PUBLICATION name ADD publication_object [, ...]
ALTER PUBLICATION name SET publication_object [, ...]
ALTER PUBLICATION name DROP publication_object [, ...]
ALTER PUBLICATION name SET ( publication_parameter [= value] [, ... ] )
ALTER PUBLICATION name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
ALTER PUBLICATION name RENAME TO new_name
Usage:
exampledb=# ALTER PUBLICATION my_publication RENAME TO my_publication_new;
ALTER PUBLICATION
drop_publisher
放下/移除不使用的出版物。
Usage:
exampledb=# drop publication my_publication_new;
DROP PUBLICATION
將下列內容視為先決條件:
- 設定外部 (出版商) PostgreSQL
wal_level=logical. - 每個被複製的表都需要包含一個 主索引鍵,或者有
REPLICA IDENTITY設置。 - 您的外部 (publisher) PostgreSQL 需要有一個具有 PostgreSQL 特權
REPLICATION的複製使用者,該使用者需要在資料庫上具有 SELECT 和 USAGE 權限以及 REPLICATION 權限。 - 您正在進行複製的 PostgreSQL 網站必須已 啟用 TLS / SSL 功能。
- 如果啟用了行層級安全機制,為外部 (publisher) PostgreSQL 複製使用者提供 BYPASSRLS 權限。
如《 PostgreSQL 》文件所述,邏輯複製存在固有限制及若干限制條件。 在決定邏輯複製是否適合您的使用個案之前,請先檢閱這些內容。
配置訂閱者
要配置您的Databases for PostgreSQL部署並確保正確複製您的數據,請確保滿足下列條件。
- 您需要在部署中建立一個與要複製的資料庫同名的資料庫。
- 邏輯複製在表格層級運作,因此您選擇發布的每個表都需要在開始邏輯複製過程之前在訂閱者中建立。 (您可以使用
pg_dump來提供協助。) 訂閱者上的表格不需要與其發佈者對應的表格相同。 但是,訂閱者上的表必須至少包含發布者上的表中存在的每一列。 訂閱者中存在的附加欄位不得具有 NOT NULL 或其他限制。 如果這樣做,複製就會失敗。
PostgreSQL 的原生訂閱指令需要超級使用者權限,而此類權限在 Databases for PostgreSQL 的部署環境中並不可用。 相反,您的部署包含一組可用於設定和管理訂閱的邏輯複製的功能。
只有由 Databases for PostgreSQL 提供的管理員使用者,才有權執行以下複製命令,讓您能夠訂閱並複製來自外部 PostgreSQL 發佈者的內容。
訂閱者功能
create_subscription
Arguments:
subscription_name Unique name to create the subscription channel with
host_ip Publisher hostname or public IP address
port Port number publisher is running on
password Password of the `admin` user on the publisher
username `admin` user created on the publisher
db_name The name of the database to be replicated
publisher_name The name of publisher channel on the publisher
These additional configuration options are only supported in PostgreSQL 17 and later.
copy_data The copy_data parameter (true or false, default: true)
determines whether the initial data from the publication
should be copied to the subscriber when the subscription
is created.
origin The origin parameter (ANY or NONE, default: NONE) allows
filtering of changes based on their origin. It helps exclude
or include changes coming from specific nodes in multi-node or
cascading replication setups.
failover The failover parameter (true or false, default: false) enables
support for automatic failover of the subscription between replicated
nodes, ensuring continuity during primary node transitions.
Usage:
exampledb=> SELECT create_subscription('subs1','130.215.223.184','5432','password','admin','exampledb','my_publication');
PostgreSQL 17 and later
exampledb=> SELECT create_subscription('subs1','130.215.223.184','5432','password','admin','exampledb','my_publication' 'true', 'ANY' , 'true');
delete_subscription
Arguments:
subscription_name Name the subscription channel to delete
db_name The name of the replicated database
Usage:
exampledb=> SELECT delete_subscription('subs1', 'exampledb');
list_subscriptions
Arguments:
None
Usage:
exampledb=> SELECT * FROM list_subscriptions();
disable_subscription
Arguments:
subscription_name Name the subscription channel to disable
db_name The name of the replicated database
Usage:
exampledb=> SELECT disable_subscription('subs1','exampledb');
enable_subscription
Arguments:
subscription_name Name the subscription channel to enable
db_name The name of the replicated database
Usage:
exampledb=> SELECT enable_subscription('subs1','exampledb');
subscription_slot_none
此函數用於將訂閱的槽名稱設定為NONE。 這是刪除訂閱所必需的,以便遠端複製槽無法被刪除、不存在或從未存在。
Arguments:
subscription_name Name the subscription channel to alter
db_name The name of the replicated database
Usage:
exampledb=> SELECT subscription_slot_none('subs1','exampledb');
refresh_subscription
此函數用於在發布者上進行更改(例如新增或刪除表)後刷新訂閱者上的訂閱。
Arguments:
subscription_name Name the subscription channel to refresh
db_name The name of the replicated database
Usage:
exampledb=> SELECT refresh_subscription('subs1','exampledb');
在發布者上設定邏輯複製
若要將外部PostgreSQL配置為發布者,請執行下列步驟。
-
編輯您的本地
pg_hba.conf並添加以下內容。hostssl replication replicator 0.0.0.0/0 md5 hostssl all replicator 0.0.0.0/0 md5「replicator」欄位是您設定的具有 PostgreSQL權限
REPLICATION的使用者。 -
使用所需的 邏輯複製配置編輯本機
postgresql.conf。 將wal_level設為“邏輯”,並將listen_addresses='*'設定為接受來自任何主機的連線。listen_addresses='*' wal_level = logical -
重新啟動您的 PostgreSQL 伺服器。
現在您可以在資料庫上定義發布者並將要複製的表格新增至訂閱者。
-
使用複製使用者登入要從中發布的資料庫。
psql -U replicator -d exampledb -
建立發布管道。
exampledb=> CREATE PUBLICATION my_publication; -
將表格新增至發布者。
exampledb=> ALTER PUBLICATION my_publication ADD TABLE my_table;max_logical_replication_workers配置參數定義的支援同步的工作執行緒數量是有限的且無法變更。 因此,請使用盡可能少的發布,並向一個發布添加盡可能多的表。
在訂閱者上設定邏輯複製
若要將您的Databases for PostgreSQL部署配置為訂閱者,請執行下列步驟。
-
使用
admin使用者登入為複製而建立的資料庫。psql -U admin -d exampledb -
執行以下查詢來呼叫
create_subscription函數並建立訂閱者通道。exampledb=> SELECT create_subscription('subs1','130.215.223.184','5432','admin','password','exampledb','my_publication');
監視抄寫
您可以透過在發布者和訂閱者上執行以下查詢來監視邏輯複製的狀態。
exampledb=> SELECT * FROM pg_stat_replication;