僅供讀取複本上的邏輯解碼 - Amazon Relational Database Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

僅供讀取複本上的邏輯解碼

RDS for PostgreSQL 支援使用 PostgreSQL 16.1 從待命複本進行邏輯複寫 這可讓您從唯讀待命複本建立邏輯解碼,以減輕主要資料庫執行個體上的負載。您可以為需要在多個系統間同步資料的應用程式實現更高的可用性。此功能可提升資料倉儲和資料分析的效能。

此外,指定待命複本上的複寫插槽在該待命複本提升為主要複本後會保存下來。這表示,在發生主要資料庫執行個體容錯移轉,或待命複本提升為新的主要複本後,複寫插槽會保存下來,且之前的待命複本訂閱者不會受到影響。

在僅供讀取複本上建立邏輯解碼
  1. 開啟邏輯複寫 – 若要在待命複本上建立邏輯解碼,您必須在來源資料庫執行個體及其實體複本上開啟邏輯複寫。如需詳細資訊,請參閱使用 PostgreSQL 的僅供讀取複本組態

    • 若要為新建立的 RDS for PostgreSQL 資料庫執行個體開啟邏輯複寫 – 建立新的資料庫自訂參數群組,並將靜態參數 rds.logical_replication 設定為 1。然後,將此資料庫參數群組與來源資料庫執行個體及其實體僅供讀取複本建立關聯。如需詳細資訊,請參閱將資料庫參數群組與 Amazon RDS 中的資料庫執行個體建立關聯

    • 若要為現有的 RDS for PostgreSQL 資料庫執行個體開啟邏輯複寫 – 修改來源資料庫執行個體及其實體僅供讀取複本的資料庫自訂參數群組,將靜態參數 rds.logical_replication 設定為 1。如需詳細資訊,請參閱修改 Amazon RDS 中的資料庫參數群組中的參數

    注意

    您必須將資料庫執行個體重新開機,以套用這些參數變更。

    您可以使用下列查詢,驗證來源資料庫執行個體及其實體僅供讀取複本上的 wal_levelrds.logical_replication 的值。

    Postgres=>SELECT name,setting FROM pg_settings WHERE name IN ('wal_level','rds.logical_replication'); name | setting -------------------------+--------- rds.logical_replication | on wal_level | logical (2 rows)
  2. 在來源資料庫中建立資料表 – 連線至來源資料庫執行個體中的資料庫。如需詳細資訊,請參閱連線至執行 PostgreSQL 資料庫引擎的資料庫執行個體

    使用下列查詢在來源資料庫中建立資料表,並插入值:

    Postgres=>CREATE TABLE LR_test (a int PRIMARY KEY); CREATE TABLE
    Postgres=>INSERT INTO LR_test VALUES (generate_series(1,10000)); INSERT 0 10000
  3. 建立來源資料表的發行集 – 使用下列查詢,在來源資料庫執行個體上建立資料表的發行集。

    Postgres=>CREATE PUBLICATION testpub FOR TABLE LR_test; CREATE PUBLICATION

    使用 SELECT 查詢,詳細驗證在來源資料庫執行個體和實體僅供讀取複本執行個體上建立的發行集。

    Postgres=>SELECT * from pg_publication; oid | pubname | pubowner | puballtables | pubinsert | pubupdate | pubdelete | pubtruncate | pubviaroot -------+---------+----------+--------------+-----------+-----------+-----------+-------------+------------ 16429 | testpub | 16413 | f | t | t | t | t | f (1 row)
  4. 從邏輯複本執行個體建立訂閱 – 建立另一個 RDS for PostgreSQL 資料庫執行個體,做為邏輯複本執行個體。請確認 VPC 已設定正確,以確保此邏輯複本執行個體可存取實體僅供讀取複本執行個體。如需詳細資訊,請參閱Amazon VPC 和 Amazon RDS。如果您的來源資料庫執行個體處於閒置狀態,可能會發生連線問題,主要複本不會將資料傳送至待命複本。

    Postgres=>CREATE SUBSCRIPTION testsub CONNECTION 'host=Physical replica host name port=port dbname=source_db_name user=user password=password' PUBLICATION testpub; NOTICE: created replication slot "testsub" on publisher CREATE SUBSCRIPTION
    Postgres=>CREATE TABLE LR_test (a int PRIMARY KEY); CREATE TABLE

    使用 SELECT 查詢,詳細驗證邏輯複本執行個體上的訂閱。

    Postgres=>SELECT oid,subname,subenabled,subslotname,subpublications FROM pg_subscription; oid | subname | subenabled | subslotname | subpublications -------+---------+------------+-------------+----------------- 16429 | testsub | t | testsub | {testpub} (1 row) postgres=> select count(*) from LR_test; count ------- 10000 (1 row)
  5. 檢查邏輯複寫插槽狀態 – 您只能查看來源資料庫執行個體上的實體複寫插槽。

    Postgres=>select slot_name, slot_type, confirmed_flush_lsn from pg_replication_slots; slot_name | slot_type | confirmed_flush_lsn ---------------------------------------------+-----------+--------------------- rds_us_west_2_db_dhqfsmo5wbbjqrn3m6b6ivdhu4 | physical | (1 row)

    不過,在僅供讀取複本執行個體上,您可以查看邏輯複寫插槽,且 confirmed_flush_lsn 值會隨著應用程式主動取用邏輯變更而變更。

    Postgres=>select slot_name, slot_type, confirmed_flush_lsn from pg_replication_slots; slot_name | slot_type | confirmed_flush_lsn -----------+-----------+--------------------- testsub | logical | 0/500002F0 (1 row)
    Postgres=>select slot_name, slot_type, confirmed_flush_lsn from pg_replication_slots; slot_name | slot_type | confirmed_flush_lsn -----------+-----------+--------------------- testsub | logical | 0/5413F5C0 (1 row)