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

다음 사항을 전제로 고려하십시오

  1. 외부(출판사) 설정 PostgreSQL wal_level=logical.
  2. 복제를 위해 선택된 모든 테이블에는 기본 키가 포함되어 있거나 REPLICA IDENTITY 설정되어 있어야 합니다.
  3. 외부(출판사) PostgreSQL 에는 PostgreSQL 특권 REPLICATION 권한을 가진 복제 사용자가 있어야 하며, 이 사용자는 데이터베이스에 대한 SELECT 및 USAGE 권한과 함께 REPLICATION 권한을 가지고 있어야 합니다.
  4. 복제 대상인 PostgreSQL 에는 TLS / SSL 기능이 활성화되어 있어야 합니다.
  5. 행 수준 보안이 활성화된 경우, 외부(출판사) PostgreSQL 복제 사용자에게 BYPASSRLS 권한을 부여합니다.

PostgreSQL 문서에 설명된 바와 같이, 논리적 복제에는 본질적인 한계와 몇 가지 제약 사항이 있습니다. 논리적 복제가 사용 사례에 적합한지 결정하기 전에 이 내용을 검토하십시오.

구독자 구성

Databases for PostgreSQL 배포를 구성하고 데이터가 올바르게 복제되는지 확인하려면 다음 사항을 확인합니다.

  1. 복제하려는 데이터베이스와 동일한 이름으로 배치에 데이터베이스를 작성해야 합니다.
  2. 논리 복제는 테이블 레벨에서 작동하므로 논리 복제 프로세스를 시작하기 전에 공개하도록 선택한 모든 테이블을 구독자에 작성해야 합니다. (도움이 필요하시면 pg_dump 를 사용하면 도움이 됩니다.) 구독자의 테이블이 상대 공개자와 동일할 필요는 없습니다. 그러나 최소한 공개자의 테이블에 있는 모든 열이 구독자의 테이블에 포함되어야 합니다. 구독자에 있는 추가 열에는 NOT NULL 또는 기타 제한조건이 없어야 합니다. 있는 경우 복제가 실패합니다.

PostgreSQL 의 기본 제공 구독 명령어는 슈퍼유저 권한이 필요하지만, Databases for PostgreSQL 환경에서는 이러한 권한을 사용할 수 없습니다. 대신, 배치에는 구독을 위해 논리 복제를 설정하고 관리하는 데 사용될 수 있는 함수 세트가 포함되어 있습니다.

Databases for PostgreSQL에서 제공되는 admin 사용자에게만 외부 PostgreSQL 공개자의 컨텐츠를 구독하고 복제할 수 있도록 하는 다음 복제 명령을 실행할 권한이 있습니다.

Subscriber 함수

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를 게시자로 구성하려면 다음 단계를 수행합니다.

  1. 로컬 pg_hba.conf 파일을 편집하고 다음 내용을 추가하세요.

    hostssl    replication            replicator         0.0.0.0/0      md5
    hostssl    all                    replicator         0.0.0.0/0      md5
    

    “replicator” 필드는 PostgreSQL 특권 REPLICATION 에서 설정한 사용자입니다.

  2. postgresql.conf 로컬 파일을 편집하여 필요한 논리적 복제 구성을 설정하십시오. wal_level을 'logical'로 설정하고 모든 호스트로부터의 연결을 허용하도록 listen_addresses='*'를 설정하십시오.

    listen_addresses='*'
    wal_level = logical
    
  3. PostgreSQL 서버를 다시 시작하십시오.

이제 데이터베이스에서 게시자를 정의하고 구독자에게 복제할 테이블을 추가할 수 있습니다.

  1. 복제 사용자로 공개하려는 데이터베이스에 로그인하십시오.

    psql -U replicator -d exampledb
    
  2. 게시 채널을 작성하십시오.

    exampledb=> CREATE PUBLICATION my_publication;
    
  3. 게시자에 테이블을 추가하십시오.

    exampledb=> ALTER PUBLICATION my_publication ADD TABLE my_table;
    

    max_logical_replication_workers 구성 매개변수로 정의된 동기화를 뒷받침하는 작업자 수는 제한되어 있으며 변경할 수 없습니다. 따라서 가능한 최소한의 발행물 수를 사용하고 하나의 발행물에 가능한 한 많은 표를 추가하세요.

구독자에서 논리 복제 설정

Databases for PostgreSQL 배포를 구독자로 구성하려면 다음 단계를 수행하세요.

  1. admin 사용자로 복제용으로 작성된 데이터베이스에 로그인하십시오.

    psql -U admin -d exampledb
    
  2. 다음 조회를 실행하여 create_subscription 함수를 호출하고 구독자 채널을 작성하십시오.

    exampledb=> SELECT create_subscription('subs1','130.215.223.184','5432','admin','password','exampledb','my_publication');
    

복제 모니터링

공개자 또는 구독자에서 다음 조회를 실행하여 공개자 및 구독자 모두에서 논리 복제의 상태를 모니터할 수 있습니다.

exampledb=> SELECT * FROM pg_stat_replication;