I am new to PostgreSQL logical replication. I did test, after I added a new table to a publication, I found the replication didn't work, until I re-created subscription, I am sure it's not the best practice to re-create subscription, could you please advise how make subscriber apply transactions for the new table?
Test as below:
Create the first table on primary and replicate side:
create table rep_test (a int primary key, b int);
Create publication on primary side:
CREATE PUBLICATION rep_test_pub FOR table public.rep_test;
Create subscription on replicate side:
CREATE SUBSCRIPTION rep_test_sub CONNECTION 'host=XXX port=5432 dbname=rocket user=XXX password=XXX' PUBLICATION rep_test_pub WITH (copy_data = false);
Test the replication, the replication works. Primary side:
insert into rep_test values (1, 1); insert into rep_test values (2, 1);
Replicate:
select * from rep_test; *---*---* | a | b | *---*---* | 1 | 1 | | 2 | 1 | *---*---*
Create a new table on both primary and replicate side
create table rep_test (a int primary key, b text);
Add new table to publication on primary side
alter publication rep_test_pub add table public.rep_test2;
Test the replication, the replication is not working. Primary:
insert into rep_test values (3, 1); insert into rep_test2 values (1,'text');
Replicate:
select * from rep_test;
*---*---*
| a | b |
*---*---*
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
*---*---*
select * from rep_test2;
Note: No data in rep_test2
, the replication didn't replicate rep_test2
.
Reboot replicate postgres, the replication still not working.
Drop and re-create subscription, the replication works.
Primary:
truncate table rep_test; truncate table rep_test2;
Replicate:
drop subscription rep_test_sub;
CREATE SUBSCRIPTION rep_test_sub CONNECTION 'host=XXX port=5432 dbname=rocket user=XXX password=XXX' PUBLICATION rep_test_pub WITH (copy_data = false);
Primary:
insert into rep_test values (1, 1); insert into rep_test2 values (1, 'text');
Replicate:
select * from rep_test;select * from rep_test2;
*---*---*
| a | b |
*---*---*
| 1 | 1 |
*---*---*
(1 row)
*---*-----*
| a | b |
*---*-----*
| 1 | text|
*---*-----*
(1 row)
The replication works after re-created subscription.
Could you please advise is there another way to make subscriber apply transactions to the new table?
BTW, my version:
PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
Thanks