Postgres 13 + pglogical 2.3.4 on an existing dataset

Solution for Postgres 13 + pglogical 2.3.4 on an existing dataset
is Given Below:

I’m experiencing an issue trying to setup pglogical between a provider and a subscriber, both already have some data in common.

This is a minimal script to reproduce what I’m trying to do:

CREATE DATABASE db1;
c db1;
CREATE EXTENSION pglogical;
CREATE TABLE table1(pk_field int primary key , value_field int);
INSERT INTO table1 VALUES(1,1);
INSERT INTO table1 VALUES(2,2);

SELECT pglogical.create_node(
    node_name := 'db1_node',
    dsn := 'host=localhost port=5432 dbname=db1 '
);
SELECT pglogical.replication_set_add_all_tables('default', ARRAY['public']);

CREATE DATABASE db2;
c db2;
CREATE EXTENSION pglogical;
CREATE TABLE table1(pk_field int primary key , value_field int);
INSERT INTO table1 VALUES(1,1);

SELECT pglogical.create_node(
    node_name := 'db2_node',
    dsn := 'host=localhost port=5432 dbname=db2'
);
SELECT pglogical.create_subscription(
    subscription_name := 'subscription1',
    provider_dsn := 'host=localhost port=5432 dbname=db1'
);

I thought that with conflict resolution set to ‘apply_remote’ pglogical would re-insert on db2 the existing row with PK 1 and insert the new row with PK2.
But db2.table1 remains with a single row with PK 1.

Postgres log show this:

2021-08-03 01:46:46.771 UTC [4496] ERROR: duplicate key value violates unique constraint "table1_pkey"
2021-08-03 01:46:46.771 UTC [4496] DETAIL: Key (pk_field)=(1) already exists.
2021-08-03 01:46:46.771 UTC [4496] CONTEXT: COPY table1, line 1
2021-08-03 01:46:46.771 UTC [4496] STATEMENT: COPY "public"."table1" ("pk_field","value_field") FROM stdin
2021-08-03 01:47:42.885 UTC [4507] LOG: starting apply for subscription subscription1
2021-08-03 01:47:42.885 UTC [4507] ERROR: subscriber subscription1 initialization failed during nonrecoverable step (d), please try the setup again
2021-08-03 01:47:42.885 UTC [4507] LOG: apply worker [4507] at slot 2 generation 124 exiting with error
2021-08-03 01:47:42.885 UTC [1] LOG: background worker "pglogical apply 30304:1763399739" (PID 4507) exited with exit code 1

conflic resultion setting:

show pglogical.conflict_resolution;
apply_remote

I also tried with:

pglogical.conflict_resolution last_update_wins 

After seeing the COPY fail on the logs and reading on the documentation that the batch would internally use a COPY command, I tried disabling it:

pglogical.batch_inserts off

It still didn’t work

I’m I doing something wrong or I’m misunderstanding how it is supposed to work?
Perhaps conflict resolution only applies after the initial replication batch is executed?

Note: If the duplicate row is inserted after initial replication it works fine:

2021-08-03 10:39:30.568 UTC [5309] LOG:  CONFLICT: remote INSERT on relation public.table1 (local index table1_pkey). Resolution: apply_remote.
2021-08-03 10:39:30.568 UTC [5309] DETAIL:  existing local tuple {pk_field[int4]:2 value_field[int4]:2} xid=81770,origin=-1,timestamp=; remote tuple {pk_field[int4]:2 value_field[int4]:3} in xact origin=4,timestamp=2021-08-03 10:39:30.566666+00,commit_lsn=0/FEB6918

I’m posting an answer in case it might help someone else.
The initial data sync will not perform a conflict resolution, in case of a clash, the replication will abort.

Thanks to the pglogical team for helping out on this question:
https://github.com/2ndQuadrant/pglogical/issues/328