Logical replication-change replication identity (REPLICA IDENTITY)

Article Directory


Logical replication-change replication identity (REPLICA IDENTITY)

Copy logo introduction

Logical replication is a method of replicating data objects and their changes based on their replication identifier (usually the primary key).

That is to say, the replication identification is a basis for logical replication

The following are the knowledge points for copying the logo

  • In order to be able to replicate UPDATE and DELETE operations, the published table must be configured with a replication identifier, so that the subscriber side can identify the appropriate row for update or delete.
  • By default, the replication ID is the primary key (if there is a primary key). You can also set another unique index on the replication ID (with specific additional requirements). If the table does not have a suitable key, you can set the replication flag "full", which means that the entire row becomes that key. However, this is very inefficient and should only be used when there are no other solutions.
  • If a replication flag other than "full" is set on the publisher side, a replication flag must also be set on the subscriber side, which should consist of the same or fewer columns.
  • If a table without a replication identifier is added to the publication of a replication UPDATE or DELETE operation, subsequent UPDATE or DELETE operations on the subscriber will cause an error. The INSERT operation can continue regardless of whether there is a replication flag.

Change the replication flag

The default replication identifier uses the primary key. If the primary key needs to be deleted, replaced, rebuilt, and other changes, the replication identifier needs to be replaced.

For example, the reconstruction operation of the primary key will affect the business. Need to plan idle windows. Because in the process of rebuilding the primary key, the primary database cannot perform delete and update operations. At this time, replace a replication ID, use a unique index instead of the primary key, as a transit. Can reduce the impact of the business. After the primary key is rebuilt, you can modify it back.

  1. The syntax is as follows
ALTER TABLE [IF EXISTS] [ONLY] name [*] 
    action [, ... ]... where action is one of the following: ... 
    REPLICA IDENTITY {DEFAULT | USING INDEX index_name | FULL | NOTHING}

ref REPLICA IDENTITY

  1. Change the replication flag

(1)查询表当前复制标识

test=# select relreplident from pg_class where relname='product';
 relreplident--------------
 d(1 row)d = 默认 (主键,如果存在),n = 无,f = 所有列
i = 索引的indisreplident被设置或者为默认

(2)更改复制标识

create Unique Index p_name_idx_unq On product(product_name);    --创建唯一索引;alter table product REPLICA IDENTITY USING INDEX p_name_idx_unq;    --更改复制标识为唯一索引

(3)重建主键,并将复制标识改为主键

alter table test.product drop constraint product_id_pk; --删除主键alter table test.product add constraint product_id_pk_re PRIMARY KEY (product_id);  --新建主键alter table product REPLICA IDENTITY default;   -将复制标识指向主键

⚠️ 注意,在订阅者端也必须设置一个复制标识,它应该由相同的或者少一些的列组成。

问题

订阅端没有复制标识,或复制标识不一致情况会导致报错

2021-03-29 15:26:54.880 CST,,,11861,,6061813e.2e55,2,,2021-03-29 15:26:54 CST,3/706,0,ERROR,55000,"publisher did not send replica identity column expected by the logical replication target relation ""test.product""",,,,,,,,,"","logical replication worker"

解决方案

在订阅端执行主库修改复制标识的操作。


Guess you like

Origin blog.51cto.com/13646489/2679478