Initial table sync copies the whole table when it is no longer published

Started by shveta malik13 days ago26 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1shveta malik
shveta.malik@gmail.com

Hi All,
(CCing the people involved in commit 7c99dc5, which handled a related case.)

While working on BUG #19864, I found an issue in tablesync that I
think is worth fixing. It is somewhat similar to the case handled by
commit 7c99dc5 (a missing publication in the apply worker), but
happens during the initial sync.

Steps to reproduce:

Pub:
create table tab1(a int, b int, c int);
insert into tab1 select i, i, i from generate_series(1, 5) i;
create publication pub1 for table tab1(c) where (c > 3);

Sub:
create table tab1(a int, b int, c int);
create subscription sub1 connection '...' publication pub1
WITH(enabled='false');

Pub:
drop publication pub1;

Sub:
alter subscription sub1 ENABLE;

-- wait a few seconds for the sync
select * from tab1 order by c;
select srrelid::regclass, srsubstate FROM pg_subscription_rel;

Result:
a | b | c
---+---+---
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
4 | 4 | 4
5 | 5 | 5

srrelid | srsubstate
---------+------------
tab1 | r

Here tablesync ends up copying all the columns and all the rows of a
table, ignoring the publication's column list and row filter. It
happens when the publication is dropped (or the table is removed from
the publication) before the table's initial sync starts. The table is
marked ready and no error is reported on either side. Without the DROP
PUBLICATION step, we get only (NULL, NULL, 4) and (NULL, NULL, 5), as
expected.

The cause is in fetch_remote_table_info(). It fetches the column list
and the row filter from the publisher, one row per publication. When
the publication is gone, both queries return no rows, and that is
treated the same as "no column list" and "no row filter", so the COPY
takes the whole table. Note that a table published without a column
list or row filter still returns one row from these queries, with NULL
values. Only a table that no publication publishes returns no rows.

ALTER SUBSCRIPTION ... REFRESH PUBLICATION does not really help here.
It can't be run on a disabled subscription, and once we enable the
subscription, the sync may copy the wrong data before the refresh
runs.

It is also not just about the ENABLE command; any table still waiting
for its initial sync can hit this when its turn comes, e.g. when many
tables are queued for a limited number of sync workers, even though
the sub was enabled long before the publication was dropped. REFRESH
PUBLICATION can only prevent this if it runs after the drop and before
the table’s sync starts, which is a race the user can’t control.

And if the refresh is run after the sync, it removes the table from
the subscription, but the wrongly copied data stays on the subscriber.

Attached patch fixes this by raising an error when the column-list and
row-filter queries on the publisher return no rows, since that means
none of the subscription's publications publishes the table anymore,
instead of treating it as "no column list" and "no row filter" and
copying everything. Unlike commit 7c99dc5, which skips a missing
publication with a WARNING in the apply worker, I think raising an
error is the right choice here. A tablesync error only affects that
one table’s sync, and the other tables and the apply worker continue.
Skipping, on the other hand, would mean marking the table ready
without copying it, so if the table is published again, its initial
data would be silently missing on the subscriber. Thoughts?

Thanks
Shveta

Attachments:

t259237_1
v1-0001-Do-not-copy-the-whole-table-in-tablesync-when-no-.patchapplication/octet-stream; name=v1-0001-Do-not-copy-the-whole-table-in-tablesync-when-no-.patchDownload+85-1
#2shveta malik
shveta.malik@gmail.com
In reply to: shveta malik (#1)
Re: Initial table sync copies the whole table when it is no longer published

On Mon, Nov 16, 2026 at 3:14 PM shveta malik <shveta.malik@gmail.com> wrote:

Hi All,
(CCing the people involved in commit 7c99dc5, which handled a related case.)

While working on BUG #19864, I found an issue in tablesync that I
think is worth fixing. It is somewhat similar to the case handled by
commit 7c99dc5 (a missing publication in the apply worker), but
happens during the initial sync.

Steps to reproduce:

Pub:
create table tab1(a int, b int, c int);
insert into tab1 select i, i, i from generate_series(1, 5) i;
create publication pub1 for table tab1(c) where (c > 3);

Sub:
create table tab1(a int, b int, c int);
create subscription sub1 connection '...' publication pub1
WITH(enabled='false');

Pub:
drop publication pub1;

Sub:
alter subscription sub1 ENABLE;

-- wait a few seconds for the sync
select * from tab1 order by c;
select srrelid::regclass, srsubstate FROM pg_subscription_rel;

Result:
a | b | c
---+---+---
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
4 | 4 | 4
5 | 5 | 5

srrelid | srsubstate
---------+------------
tab1 | r

Here tablesync ends up copying all the columns and all the rows of a
table, ignoring the publication's column list and row filter. It
happens when the publication is dropped (or the table is removed from
the publication) before the table's initial sync starts. The table is
marked ready and no error is reported on either side. Without the DROP
PUBLICATION step, we get only (NULL, NULL, 4) and (NULL, NULL, 5), as
expected.

The cause is in fetch_remote_table_info(). It fetches the column list
and the row filter from the publisher, one row per publication. When
the publication is gone, both queries return no rows, and that is
treated the same as "no column list" and "no row filter", so the COPY
takes the whole table. Note that a table published without a column
list or row filter still returns one row from these queries, with NULL
values. Only a table that no publication publishes returns no rows.

ALTER SUBSCRIPTION ... REFRESH PUBLICATION does not really help here.
It can't be run on a disabled subscription, and once we enable the
subscription, the sync may copy the wrong data before the refresh
runs.

It is also not just about the ENABLE command; any table still waiting
for its initial sync can hit this when its turn comes, e.g. when many
tables are queued for a limited number of sync workers, even though
the sub was enabled long before the publication was dropped. REFRESH
PUBLICATION can only prevent this if it runs after the drop and before
the table’s sync starts, which is a race the user can’t control.

And if the refresh is run after the sync, it removes the table from
the subscription, but the wrongly copied data stays on the subscriber.

Attached patch fixes this by raising an error when the column-list and
row-filter queries on the publisher return no rows, since that means
none of the subscription's publications publishes the table anymore,
instead of treating it as "no column list" and "no row filter" and
copying everything. Unlike commit 7c99dc5, which skips a missing
publication with a WARNING in the apply worker, I think raising an
error is the right choice here. A tablesync error only affects that
one table’s sync, and the other tables and the apply worker continue.
Skipping, on the other hand, would mean marking the table ready
without copying it, so if the table is published again, its initial
data would be silently missing on the subscriber. Thoughts?

I missed to mention one behaviour change post-patch for partitioned
tables: if publish_via_partition_root is changed after the
subscription is created but before its tables are synced, the
subscription still lists the relations published under the old setting
(the partitions, or the root table), which the publisher no longer
publishes. HEAD copies them anyway, and the data is correct in this
case. With the patch, their initial tablesync errors until REFRESH
PUBLICATION is run. I think it shouldbe acceptable, since a REFRESH is
needed after such a change anyway. Thoughts?

thanks
Shveta

#3Shlok Kyal
shlok.kyal.oss@gmail.com
In reply to: shveta malik (#1)
Re: Initial table sync copies the whole table when it is no longer published

On Mon, 16 Nov 2026 at 15:15, shveta malik <shveta.malik@gmail.com> wrote:

Hi All,
(CCing the people involved in commit 7c99dc5, which handled a related case.)

While working on BUG #19864, I found an issue in tablesync that I
think is worth fixing. It is somewhat similar to the case handled by
commit 7c99dc5 (a missing publication in the apply worker), but
happens during the initial sync.

Steps to reproduce:

Pub:
create table tab1(a int, b int, c int);
insert into tab1 select i, i, i from generate_series(1, 5) i;
create publication pub1 for table tab1(c) where (c > 3);

Sub:
create table tab1(a int, b int, c int);
create subscription sub1 connection '...' publication pub1
WITH(enabled='false');

Pub:
drop publication pub1;

Sub:
alter subscription sub1 ENABLE;

-- wait a few seconds for the sync
select * from tab1 order by c;
select srrelid::regclass, srsubstate FROM pg_subscription_rel;

Result:
a | b | c
---+---+---
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
4 | 4 | 4
5 | 5 | 5

srrelid | srsubstate
---------+------------
tab1 | r

Here tablesync ends up copying all the columns and all the rows of a
table, ignoring the publication's column list and row filter. It
happens when the publication is dropped (or the table is removed from
the publication) before the table's initial sync starts. The table is
marked ready and no error is reported on either side. Without the DROP
PUBLICATION step, we get only (NULL, NULL, 4) and (NULL, NULL, 5), as
expected.

The cause is in fetch_remote_table_info(). It fetches the column list
and the row filter from the publisher, one row per publication. When
the publication is gone, both queries return no rows, and that is
treated the same as "no column list" and "no row filter", so the COPY
takes the whole table. Note that a table published without a column
list or row filter still returns one row from these queries, with NULL
values. Only a table that no publication publishes returns no rows.

ALTER SUBSCRIPTION ... REFRESH PUBLICATION does not really help here.
It can't be run on a disabled subscription, and once we enable the
subscription, the sync may copy the wrong data before the refresh
runs.

It is also not just about the ENABLE command; any table still waiting
for its initial sync can hit this when its turn comes, e.g. when many
tables are queued for a limited number of sync workers, even though
the sub was enabled long before the publication was dropped. REFRESH
PUBLICATION can only prevent this if it runs after the drop and before
the table’s sync starts, which is a race the user can’t control.

And if the refresh is run after the sync, it removes the table from
the subscription, but the wrongly copied data stays on the subscriber.

Hi Shveta,

I am able to reproduce the issue. I also agree with and confirm the
above analysis.
In fetch_remote_table_info both the queries return no row and hence it
is treated the same as "no column list" and "no row filter".
The COPY command formed is: COPY public.tab1 (a, b, c) TO STDOUT.

I am able to reproduce the issue from PG15 till PG19 and on master.

While testing I also encountered one more issue, which has the same root cause.
And the tablesync worker can copy data of UNLOGGED tables.

Pub:
create table tab1(a int, b int, c int);
create publication pub1 for all tables;

Sub:
create table tab1(a int, b int, c int);
create subscription sub1 connection '...' publication pub1
WITH(enabled='false');

Pub:
drop table tab1;
create unlogged table tab1(a int, b int, c int);
insert into tab1 select i, i, i from generate_series(1, 5) i;

Sub:
alter subscription sub1 ENABLE;

-- wait a few seconds for the sync
select * from tab1 order by c;
select srrelid::regclass, srsubstate FROM pg_subscription_rel;

Result:
a | b | c
---+---+---
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
4 | 4 | 4
5 | 5 | 5

srrelid | srsubstate
---------+------------
tab1 | r

In this case as well, in fetch_remote_table_info both the queries
return no row and hence it is treated the same as "no column list" and
"no row filter".
The COPY command formed is: COPY public.tab1 (a, b, c) TO STDOUT.

Attached patch fixes this by raising an error when the column-list and
row-filter queries on the publisher return no rows, since that means
none of the subscription's publications publishes the table anymore,
instead of treating it as "no column list" and "no row filter" and
copying everything. Unlike commit 7c99dc5, which skips a missing
publication with a WARNING in the apply worker, I think raising an
error is the right choice here. A tablesync error only affects that
one table’s sync, and the other tables and the apply worker continue.
Skipping, on the other hand, would mean marking the table ready
without copying it, so if the table is published again, its initial
data would be silently missing on the subscriber. Thoughts?

I have not checked the fix. Will review and test it.

Thanks,
Shlok Kyal

#4Peter Smith
smithpb2250@gmail.com
In reply to: shveta malik (#1)
Re: Initial table sync copies the whole table when it is no longer published

Hi Shveta.

+1 to address this bug.

On Mon, Nov 16, 2026 at 8:45 PM shveta malik <shveta.malik@gmail.com> wrote:

...

It is also not just about the ENABLE command; any table still waiting
for its initial sync can hit this when its turn comes, e.g. when many
tables are queued for a limited number of sync workers, even though
the sub was enabled long before the publication was dropped. REFRESH
PUBLICATION can only prevent this if it runs after the drop and before
the table’s sync starts, which is a race the user can’t control.

And if the refresh is run after the sync, it removes the table from
the subscription, but the wrongly copied data stays on the subscriber.

Attached patch fixes this by raising an error when the column-list and
row-filter queries on the publisher return no rows, since that means
none of the subscription's publications publishes the table anymore,
instead of treating it as "no column list" and "no row filter" and
copying everything. Unlike commit 7c99dc5, which skips a missing
publication with a WARNING in the apply worker, I think raising an
error is the right choice here. A tablesync error only affects that
one table’s sync, and the other tables and the apply worker continue.
Skipping, on the other hand, would mean marking the table ready
without copying it, so if the table is published again, its initial
data would be silently missing on the subscriber. Thoughts?

TBH, I am not 100% sure if ERROR is best. Maybe it is; maybe not.

IIUC you are not going to get 1x ERROR -- you are going to get ERRORs
forever, until it is resolved.

OTOH, if you chose to give a WARNING that initial data was skipped
then it's not really "silently missing" is it. I think this scenario
might be a use-case for the other thread [1]/messages/by-id/OS7PR01MB1831757BF5C212B9E04F7E615F5642@OS7PR01MB18317.jpnprd01.prod.outlook.com proposing "Per-table
resync for logical replication subscriptions". e.g. After seeing such
a warning, (and if that other feature existed) then the specific table
could be refreshed. All without millions of errors.

//////////

Anyway, here are some minor comments for patch v1.

======
src/backend/replication/logical/tablesync.c

1.
fetch_remote_table_info

Now there are multiple places checking the row count, so perhaps a
local var would help. I also think putting an Assert may make
subsequent logic clearer.

e.g.

nrows = tuplestore_tuple_count(pubres->tuplestore);

if (nrows == 0)
ereport(ERROR, ... "table \"%s.%s\" is not published..." ...)

if (nrows > 1)
ereport(ERROR, ... "cannot use different column lists..." ...)

Assert(nrows == 1);
...

======
src/test/subscription/t/024_add_drop_pub.pl

2.
+$result = $node_subscriber->safe_psql('postgres',
+ "SELECT (SELECT count(*) FROM tab_4), (SELECT count(*) FROM tab_5)");
+is($result, qq(0|0),
+ 'check that no data is copied for tables that are no longer published');

Would it be better to also do some publisher-side INSERTS (after the
DROP/ALTER PUBLICATION, but prior to this) just to demonstrate those
are not getting replicated.

~~~

3.
Missing cleanup code for publication "tap_pub_5" ?

======
[1]: /messages/by-id/OS7PR01MB1831757BF5C212B9E04F7E615F5642@OS7PR01MB18317.jpnprd01.prod.outlook.com

Kind Regards,
Peter Smith.
Fujitsu Australia

#5shveta malik
shveta.malik@gmail.com
In reply to: Peter Smith (#4)
Re: Initial table sync copies the whole table when it is no longer published

On Tue, Nov 17, 2026 at 2:10 PM Peter Smith <smithpb2250@gmail.com> wrote:

Hi Shveta.

+1 to address this bug.

On Mon, Nov 16, 2026 at 8:45 PM shveta malik <shveta.malik@gmail.com> wrote:

...

It is also not just about the ENABLE command; any table still waiting
for its initial sync can hit this when its turn comes, e.g. when many
tables are queued for a limited number of sync workers, even though
the sub was enabled long before the publication was dropped. REFRESH
PUBLICATION can only prevent this if it runs after the drop and before
the table’s sync starts, which is a race the user can’t control.

And if the refresh is run after the sync, it removes the table from
the subscription, but the wrongly copied data stays on the subscriber.

Attached patch fixes this by raising an error when the column-list and
row-filter queries on the publisher return no rows, since that means
none of the subscription's publications publishes the table anymore,
instead of treating it as "no column list" and "no row filter" and
copying everything. Unlike commit 7c99dc5, which skips a missing
publication with a WARNING in the apply worker, I think raising an
error is the right choice here. A tablesync error only affects that
one table’s sync, and the other tables and the apply worker continue.
Skipping, on the other hand, would mean marking the table ready
without copying it, so if the table is published again, its initial
data would be silently missing on the subscriber. Thoughts?

TBH, I am not 100% sure if ERROR is best. Maybe it is; maybe not.

IIUC you are not going to get 1x ERROR -- you are going to get ERRORs
forever, until it is resolved.

OTOH, if you chose to give a WARNING that initial data was skipped
then it's not really "silently missing" is it. I think this scenario
might be a use-case for the other thread [1] proposing "Per-table
resync for logical replication subscriptions". e.g. After seeing such
a warning, (and if that other feature existed) then the specific table
could be refreshed. All without millions of errors.

Thanks for the review Peter.

Yes, the ERROR repeats until it is resolved, but that is the same
behaviour as other tablesync errors (e.g. "table \"%s.%s\" not found
on publisher"). The errors are also counted in
pg_stat_subscription_stats, so users can monitor them.

With the ERROR, the table stays in the 'd' (data sync) state, which is
the correct state IMO. With a WARNING and skip, the table would
wrongly be marked 'ready' instead, so its initial data would never be
copied, even after a REFRESH PUBLICATION (provided the table is added
back or the publication is created).

We could instead give a WARNING but keep the table in 'd' rather than
marking it ready, but then it would keep retrying the same way,
restarting the tablesync worker every time, just with WARNINGs instead
of ERRORs, and without the sync_error_count and disable_on_error
handling. Keeping the table in 'd' with a WARNING would also require
extra code to exit the tablesync-worker cleanly from the middle of the
copy, which I don't think is worth it for this rare scenario. But
let's see what others think about this.

The per-table resync feature you mentioned would make recovery easier
if it is committed, but I don’t think this fix should depend on it. If
that feature gets in, we can revisit whether a WARNING would be better
here. Thoughts?

//////////

Anyway, here are some minor comments for patch v1.

Agree with all, addressed in v2.

Show quoted text

======
src/backend/replication/logical/tablesync.c

1.
fetch_remote_table_info

Now there are multiple places checking the row count, so perhaps a
local var would help. I also think putting an Assert may make
subsequent logic clearer.

e.g.

nrows = tuplestore_tuple_count(pubres->tuplestore);

if (nrows == 0)
ereport(ERROR, ... "table \"%s.%s\" is not published..." ...)

if (nrows > 1)
ereport(ERROR, ... "cannot use different column lists..." ...)

Assert(nrows == 1);
...

======
src/test/subscription/t/024_add_drop_pub.pl

2.
+$result = $node_subscriber->safe_psql('postgres',
+ "SELECT (SELECT count(*) FROM tab_4), (SELECT count(*) FROM tab_5)");
+is($result, qq(0|0),
+ 'check that no data is copied for tables that are no longer published');

Would it be better to also do some publisher-side INSERTS (after the
DROP/ALTER PUBLICATION, but prior to this) just to demonstrate those
are not getting replicated.

~~~

3.
Missing cleanup code for publication "tap_pub_5" ?

======
[1] /messages/by-id/OS7PR01MB1831757BF5C212B9E04F7E615F5642@OS7PR01MB18317.jpnprd01.prod.outlook.com

Kind Regards,
Peter Smith.
Fujitsu Australia

Attachments:

t259237_5
v2-0001-Do-not-copy-the-whole-table-in-tablesync-when-no-.patchapplication/octet-stream; name=v2-0001-Do-not-copy-the-whole-table-in-tablesync-when-no-.patchDownload+101-2
#6shveta malik
shveta.malik@gmail.com
In reply to: Shlok Kyal (#3)
Re: Initial table sync copies the whole table when it is no longer published

On Mon, Nov 16, 2026 at 4:08 PM Shlok Kyal <shlok.kyal.oss@gmail.com> wrote:

On Mon, 16 Nov 2026 at 15:15, shveta malik <shveta.malik@gmail.com> wrote:

Hi All,
(CCing the people involved in commit 7c99dc5, which handled a related case.)

While working on BUG #19864, I found an issue in tablesync that I
think is worth fixing. It is somewhat similar to the case handled by
commit 7c99dc5 (a missing publication in the apply worker), but
happens during the initial sync.

Steps to reproduce:

Pub:
create table tab1(a int, b int, c int);
insert into tab1 select i, i, i from generate_series(1, 5) i;
create publication pub1 for table tab1(c) where (c > 3);

Sub:
create table tab1(a int, b int, c int);
create subscription sub1 connection '...' publication pub1
WITH(enabled='false');

Pub:
drop publication pub1;

Sub:
alter subscription sub1 ENABLE;

-- wait a few seconds for the sync
select * from tab1 order by c;
select srrelid::regclass, srsubstate FROM pg_subscription_rel;

Result:
a | b | c
---+---+---
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
4 | 4 | 4
5 | 5 | 5

srrelid | srsubstate
---------+------------
tab1 | r

Here tablesync ends up copying all the columns and all the rows of a
table, ignoring the publication's column list and row filter. It
happens when the publication is dropped (or the table is removed from
the publication) before the table's initial sync starts. The table is
marked ready and no error is reported on either side. Without the DROP
PUBLICATION step, we get only (NULL, NULL, 4) and (NULL, NULL, 5), as
expected.

The cause is in fetch_remote_table_info(). It fetches the column list
and the row filter from the publisher, one row per publication. When
the publication is gone, both queries return no rows, and that is
treated the same as "no column list" and "no row filter", so the COPY
takes the whole table. Note that a table published without a column
list or row filter still returns one row from these queries, with NULL
values. Only a table that no publication publishes returns no rows.

ALTER SUBSCRIPTION ... REFRESH PUBLICATION does not really help here.
It can't be run on a disabled subscription, and once we enable the
subscription, the sync may copy the wrong data before the refresh
runs.

It is also not just about the ENABLE command; any table still waiting
for its initial sync can hit this when its turn comes, e.g. when many
tables are queued for a limited number of sync workers, even though
the sub was enabled long before the publication was dropped. REFRESH
PUBLICATION can only prevent this if it runs after the drop and before
the table’s sync starts, which is a race the user can’t control.

And if the refresh is run after the sync, it removes the table from
the subscription, but the wrongly copied data stays on the subscriber.

Hi Shveta,

I am able to reproduce the issue. I also agree with and confirm the
above analysis.
In fetch_remote_table_info both the queries return no row and hence it
is treated the same as "no column list" and "no row filter".
The COPY command formed is: COPY public.tab1 (a, b, c) TO STDOUT.

I am able to reproduce the issue from PG15 till PG19 and on master.

Thanks Shlok for testing it on different versions.

While testing I also encountered one more issue, which has the same root cause.
And the tablesync worker can copy data of UNLOGGED tables.

Pub:
create table tab1(a int, b int, c int);
create publication pub1 for all tables;

Sub:
create table tab1(a int, b int, c int);
create subscription sub1 connection '...' publication pub1
WITH(enabled='false');

Pub:
drop table tab1;
create unlogged table tab1(a int, b int, c int);
insert into tab1 select i, i, i from generate_series(1, 5) i;

Sub:
alter subscription sub1 ENABLE;

-- wait a few seconds for the sync
select * from tab1 order by c;
select srrelid::regclass, srsubstate FROM pg_subscription_rel;

Result:
a | b | c
---+---+---
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
4 | 4 | 4
5 | 5 | 5

srrelid | srsubstate
---------+------------
tab1 | r

In this case as well, in fetch_remote_table_info both the queries
return no row and hence it is treated the same as "no column list" and
"no row filter".
The COPY command formed is: COPY public.tab1 (a, b, c) TO STDOUT.

Thanks for reporting. Yes, it has the same root cause: the
subscription still lists tab1 from before, and since the new unlogged
table cannot be published, the column-list query returns no rows,
which tablesync interprets as “no column list” and copies evrything.

I can add this case to the patch's test as well if others think so;
otherwise, the test I have added should suffice.

Show quoted text

Attached patch fixes this by raising an error when the column-list and
row-filter queries on the publisher return no rows, since that means
none of the subscription's publications publishes the table anymore,
instead of treating it as "no column list" and "no row filter" and
copying everything. Unlike commit 7c99dc5, which skips a missing
publication with a WARNING in the apply worker, I think raising an
error is the right choice here. A tablesync error only affects that
one table’s sync, and the other tables and the apply worker continue.
Skipping, on the other hand, would mean marking the table ready
without copying it, so if the table is published again, its initial
data would be silently missing on the subscriber. Thoughts?

I have not checked the fix. Will review and test it.

Thanks,
Shlok Kyal

#7Peter Smith
smithpb2250@gmail.com
In reply to: shveta malik (#5)
Re: Initial table sync copies the whole table when it is no longer published

On Tue, Nov 17, 2026 at 9:03 PM shveta malik <shveta.malik@gmail.com> wrote:

On Tue, Nov 17, 2026 at 2:10 PM Peter Smith <smithpb2250@gmail.com> wrote:

Hi Shveta.

+1 to address this bug.

On Mon, Nov 16, 2026 at 8:45 PM shveta malik <shveta.malik@gmail.com> wrote:

...

It is also not just about the ENABLE command; any table still waiting
for its initial sync can hit this when its turn comes, e.g. when many
tables are queued for a limited number of sync workers, even though
the sub was enabled long before the publication was dropped. REFRESH
PUBLICATION can only prevent this if it runs after the drop and before
the table’s sync starts, which is a race the user can’t control.

And if the refresh is run after the sync, it removes the table from
the subscription, but the wrongly copied data stays on the subscriber.

Attached patch fixes this by raising an error when the column-list and
row-filter queries on the publisher return no rows, since that means
none of the subscription's publications publishes the table anymore,
instead of treating it as "no column list" and "no row filter" and
copying everything. Unlike commit 7c99dc5, which skips a missing
publication with a WARNING in the apply worker, I think raising an
error is the right choice here. A tablesync error only affects that
one table’s sync, and the other tables and the apply worker continue.
Skipping, on the other hand, would mean marking the table ready
without copying it, so if the table is published again, its initial
data would be silently missing on the subscriber. Thoughts?

TBH, I am not 100% sure if ERROR is best. Maybe it is; maybe not.

IIUC you are not going to get 1x ERROR -- you are going to get ERRORs
forever, until it is resolved.

OTOH, if you chose to give a WARNING that initial data was skipped
then it's not really "silently missing" is it. I think this scenario
might be a use-case for the other thread [1] proposing "Per-table
resync for logical replication subscriptions". e.g. After seeing such
a warning, (and if that other feature existed) then the specific table
could be refreshed. All without millions of errors.

Thanks for the review Peter.

Yes, the ERROR repeats until it is resolved, but that is the same
behaviour as other tablesync errors (e.g. "table \"%s.%s\" not found
on publisher"). The errors are also counted in
pg_stat_subscription_stats, so users can monitor them.

With the ERROR, the table stays in the 'd' (data sync) state, which is
the correct state IMO. With a WARNING and skip, the table would
wrongly be marked 'ready' instead, so its initial data would never be
copied, even after a REFRESH PUBLICATION (provided the table is added
back or the publication is created).

We could instead give a WARNING but keep the table in 'd' rather than
marking it ready, but then it would keep retrying the same way,
restarting the tablesync worker every time, just with WARNINGs instead
of ERRORs, and without the sync_error_count and disable_on_error
handling. Keeping the table in 'd' with a WARNING would also require
extra code to exit the tablesync-worker cleanly from the middle of the
copy, which I don't think is worth it for this rare scenario. But
let's see what others think about this.

Your justifications for using ERROR instead of WARNING seemed
reasonable to me, so I am happy if that's how it ends up being.

So, the patch v2-0001 LGTM.

I did have one more idea:

IMO the purpose of REFRESH PUBLICATION is for "discovery" of what is
currently published so the `pg_subscription_rel` can be updated
appropriately with the latest list. But in your scenario, you
*already* discovered that this particular table is no longer
published. From that POV why not just remove this table from the
`pg_subscription_rel` right here-and-now.

That avoids the endless ERRORs and the silent-data-loss problem of
marking it READY, plus it saves the user from manually having to do a
REFRESH PUBLICATION to resolve errors just to re-discover this table
is not published (we already know that).

Having said that, it might need additional slot/origin cleanup from
within the worker, and some other complications, so just the plain
looping ERROR may well be the simpler and safer choice. Thoughts?

The per-table resync feature you mentioned would make recovery easier
if it is committed, but I don’t think this fix should depend on it. If
that feature gets in, we can revisit whether a WARNING would be better
here. Thoughts?

I agree your fix should not depend on the other thread. But I think it
would be good if you post to that other thread just to mention this
current issue seems like a valid use-case where that new per-table
refresh feature might be useful.

======
Kind Regards,
Peter Smith.
Fujitsu Australia

#8Ajin Cherian
itsajin@gmail.com
In reply to: shveta malik (#5)
Re: Initial table sync copies the whole table when it is no longer published

On Tue, Nov 17, 2026 at 9:03 PM shveta malik <shveta.malik@gmail.com> wrote:

Yes, the ERROR repeats until it is resolved, but that is the same
behaviour as other tablesync errors (e.g. "table \"%s.%s\" not found
on publisher"). The errors are also counted in
pg_stat_subscription_stats, so users can monitor them.

With the ERROR, the table stays in the 'd' (data sync) state, which is
the correct state IMO. With a WARNING and skip, the table would
wrongly be marked 'ready' instead, so its initial data would never be
copied, even after a REFRESH PUBLICATION (provided the table is added
back or the publication is created).

Hi Shveta,

Just curious: with disable_on_error = true, this ERROR would disable
the whole subscription, and REFRESH PUBLICATION isn't allowed on a
disabled subscription. How would the user get out of that? If the user
re-enables the subscription, it's a race between the tablesync worker
hitting the error again and the user running REFRESH PUBLICATION.

I understand the same is true of other tablesync errors caused by
publisher-side changes (e.g. "table ... not found on publisher"), so
this isn't really related to this patch. But maybe we should document
somewhere how to get out of such situations.

Regards,
Ajin Cherian
Fujitsu Australia

#9shveta malik
shveta.malik@gmail.com
In reply to: Ajin Cherian (#8)
Re: Initial table sync copies the whole table when it is no longer published

On Wed, Nov 18, 2026 at 6:42 AM Ajin Cherian <itsajin@gmail.com> wrote:

On Tue, Nov 17, 2026 at 9:03 PM shveta malik <shveta.malik@gmail.com> wrote:

Yes, the ERROR repeats until it is resolved, but that is the same
behaviour as other tablesync errors (e.g. "table \"%s.%s\" not found
on publisher"). The errors are also counted in
pg_stat_subscription_stats, so users can monitor them.

With the ERROR, the table stays in the 'd' (data sync) state, which is
the correct state IMO. With a WARNING and skip, the table would
wrongly be marked 'ready' instead, so its initial data would never be
copied, even after a REFRESH PUBLICATION (provided the table is added
back or the publication is created).

Hi Shveta,

Just curious: with disable_on_error = true, this ERROR would disable
the whole subscription, and REFRESH PUBLICATION isn't allowed on a
disabled subscription. How would the user get out of that? If the user
re-enables the subscription, it's a race between the tablesync worker
hitting the error again and the user running REFRESH PUBLICATION.

Good point Ajin. I did not think of it earlier, but on analysing it
now, I think there is a way out without the race: since
disable_on_error can be changed on a disabled sub, the user can
temporarily set 'disable_on_error=false' on disabled sub, then ENABLE
the sub, then REFRESH PUBLICATION, and then set disable_on_error back
to true. I tested this with the patch, and it works.

I understand the same is true of other tablesync errors caused by
publisher-side changes (e.g. "table ... not found on publisher"), so
this isn't really related to this patch. But maybe we should document
somewhere how to get out of such situations.

As you said, this applies to all tablesync errors caused by
publisher-side changes, so I think documenting it would be better done
separately,e.g. as a note in the disable_on_error description?
Thoughts? But if others feel that we can combine it in this patch, I
am okay to do that.

thanks
Shveta

#10shveta malik
shveta.malik@gmail.com
In reply to: shveta malik (#9)
Re: Initial table sync copies the whole table when it is no longer published

On Wed, Nov 18, 2026 at 9:08 AM shveta malik <shveta.malik@gmail.com> wrote:

On Wed, Nov 18, 2026 at 6:42 AM Ajin Cherian <itsajin@gmail.com> wrote:

On Tue, Nov 17, 2026 at 9:03 PM shveta malik <shveta.malik@gmail.com> wrote:

Yes, the ERROR repeats until it is resolved, but that is the same
behaviour as other tablesync errors (e.g. "table \"%s.%s\" not found
on publisher"). The errors are also counted in
pg_stat_subscription_stats, so users can monitor them.

With the ERROR, the table stays in the 'd' (data sync) state, which is
the correct state IMO. With a WARNING and skip, the table would
wrongly be marked 'ready' instead, so its initial data would never be
copied, even after a REFRESH PUBLICATION (provided the table is added
back or the publication is created).

Hi Shveta,

Just curious: with disable_on_error = true, this ERROR would disable
the whole subscription, and REFRESH PUBLICATION isn't allowed on a
disabled subscription. How would the user get out of that? If the user
re-enables the subscription, it's a race between the tablesync worker
hitting the error again and the user running REFRESH PUBLICATION.

Good point Ajin. I did not think of it earlier, but on analysing it
now, I think there is a way out without the race: since
disable_on_error can be changed on a disabled sub, the user can
temporarily set 'disable_on_error=false' on disabled sub, then ENABLE
the sub, then REFRESH PUBLICATION, and then set disable_on_error back
to true. I tested this with the patch, and it works.

I missed mentioning that alternatively, the table can be published
again on the publisher before re-enabling (if necessary).

Show quoted text

I understand the same is true of other tablesync errors caused by
publisher-side changes (e.g. "table ... not found on publisher"), so
this isn't really related to this patch. But maybe we should document
somewhere how to get out of such situations.

As you said, this applies to all tablesync errors caused by
publisher-side changes, so I think documenting it would be better done
separately,e.g. as a note in the disable_on_error description?
Thoughts? But if others feel that we can combine it in this patch, I
am okay to do that.

thanks
Shveta

#11Ajin Cherian
itsajin@gmail.com
In reply to: shveta malik (#9)
Re: Initial table sync copies the whole table when it is no longer published

On Wed, Nov 18, 2026 at 2:39 PM shveta malik <shveta.malik@gmail.com> wrote:

Good point Ajin. I did not think of it earlier, but on analysing it
now, I think there is a way out without the race: since
disable_on_error can be changed on a disabled sub, the user can
temporarily set 'disable_on_error=false' on disabled sub, then ENABLE
the sub, then REFRESH PUBLICATION, and then set disable_on_error back
to true. I tested this with the patch, and it works.

Yes, that works as a workaround.

As you said, this applies to all tablesync errors caused by
publisher-side changes, so I think documenting it would be better done
separately,e.g. as a note in the disable_on_error description?
Thoughts? But if others feel that we can combine it in this patch, I
am okay to do that.

Yes, a note in the disable_on_error description sounds good.

On Wed, Nov 18, 2026 at 2:40 PM shveta malik <shveta.malik@gmail.com> wrote:

I missed mentioning that alternatively, the table can be published
again on the publisher before re-enabling (if necessary).

I don't think users would want to do that if they removed the table
from the publication deliberately. It also isn't an option for an
UNLOGGED table like in Shlok's example, which can't be published at
all, or for a table that has been dropped on the publisher.

Regards,
Ajin Cherian
Fujitsu Australia

#12shveta malik
shveta.malik@gmail.com
In reply to: Peter Smith (#7)
Re: Initial table sync copies the whole table when it is no longer published

On Wed, Nov 18, 2026 at 4:46 AM Peter Smith <smithpb2250@gmail.com> wrote:

On Tue, Nov 17, 2026 at 9:03 PM shveta malik <shveta.malik@gmail.com> wrote:

On Tue, Nov 17, 2026 at 2:10 PM Peter Smith <smithpb2250@gmail.com> wrote:

Hi Shveta.

+1 to address this bug.

On Mon, Nov 16, 2026 at 8:45 PM shveta malik <shveta.malik@gmail.com> wrote:

...

It is also not just about the ENABLE command; any table still waiting
for its initial sync can hit this when its turn comes, e.g. when many
tables are queued for a limited number of sync workers, even though
the sub was enabled long before the publication was dropped. REFRESH
PUBLICATION can only prevent this if it runs after the drop and before
the table’s sync starts, which is a race the user can’t control.

And if the refresh is run after the sync, it removes the table from
the subscription, but the wrongly copied data stays on the subscriber.

Attached patch fixes this by raising an error when the column-list and
row-filter queries on the publisher return no rows, since that means
none of the subscription's publications publishes the table anymore,
instead of treating it as "no column list" and "no row filter" and
copying everything. Unlike commit 7c99dc5, which skips a missing
publication with a WARNING in the apply worker, I think raising an
error is the right choice here. A tablesync error only affects that
one table’s sync, and the other tables and the apply worker continue.
Skipping, on the other hand, would mean marking the table ready
without copying it, so if the table is published again, its initial
data would be silently missing on the subscriber. Thoughts?

TBH, I am not 100% sure if ERROR is best. Maybe it is; maybe not.

IIUC you are not going to get 1x ERROR -- you are going to get ERRORs
forever, until it is resolved.

OTOH, if you chose to give a WARNING that initial data was skipped
then it's not really "silently missing" is it. I think this scenario
might be a use-case for the other thread [1] proposing "Per-table
resync for logical replication subscriptions". e.g. After seeing such
a warning, (and if that other feature existed) then the specific table
could be refreshed. All without millions of errors.

Thanks for the review Peter.

Yes, the ERROR repeats until it is resolved, but that is the same
behaviour as other tablesync errors (e.g. "table \"%s.%s\" not found
on publisher"). The errors are also counted in
pg_stat_subscription_stats, so users can monitor them.

With the ERROR, the table stays in the 'd' (data sync) state, which is
the correct state IMO. With a WARNING and skip, the table would
wrongly be marked 'ready' instead, so its initial data would never be
copied, even after a REFRESH PUBLICATION (provided the table is added
back or the publication is created).

We could instead give a WARNING but keep the table in 'd' rather than
marking it ready, but then it would keep retrying the same way,
restarting the tablesync worker every time, just with WARNINGs instead
of ERRORs, and without the sync_error_count and disable_on_error
handling. Keeping the table in 'd' with a WARNING would also require
extra code to exit the tablesync-worker cleanly from the middle of the
copy, which I don't think is worth it for this rare scenario. But
let's see what others think about this.

Your justifications for using ERROR instead of WARNING seemed
reasonable to me, so I am happy if that's how it ends up being.

So, the patch v2-0001 LGTM.

I did have one more idea:

IMO the purpose of REFRESH PUBLICATION is for "discovery" of what is
currently published so the `pg_subscription_rel` can be updated
appropriately with the latest list. But in your scenario, you
*already* discovered that this particular table is no longer
published. From that POV why not just remove this table from the
`pg_subscription_rel` right here-and-now.

That avoids the endless ERRORs and the silent-data-loss problem of
marking it READY, plus it saves the user from manually having to do a
REFRESH PUBLICATION to resolve errors just to re-discover this table
is not published (we already know that).

Having said that, it might need additional slot/origin cleanup from
within the worker, and some other complications, so just the plain
looping ERROR may well be the simpler and safer choice. Thoughts?

Thanks Peter, I gave some thought to your idea, and I agree it would
avoid the repeated errors. But I think it has a similar problem to
marking the table ready: if the table is published again (e.g. the pub
is recreated, or the table is added back), with the ERROR the next
retry simply syncs it, whereas if the worker had already removed it
from pg_subscription_rel, it would stay out of the subscription until
someone runs REFRESH PUBLICATION. We could emit a WARNING with a hint
when removing it, but that could easily go unnoticed, and the table
could then remain unreplicated indefinitely, a bigger problem than the
repeated ERRORs.

Also, currently tables are only added to or removed from
pg_subscription_rel by DDL (CREATE/ALTER/DROP sub, or dropping the
table on the subscriber); the workers only update their sync state.
Having a worker remove tables by itself would be a new behaviour, and
only a partial refresh:
e.g. after a publish_via_partition_root change, it could remove the
partitions but not add the root (the scenario mentioned in [1]/messages/by-id/CAJpy0uDw0crvrUFU=FsqcuvH=dJbqPASkUhnDB=bdHV1SAc+0A@mail.gmail.com), which
only a real REFRESH does. Plus the slot/origin cleanup you mentioned,
which REFRESH PUBLICATION already handles. So, as you said, I think
the plain ERROR is the simpler and safer choice, especially if this is
back-patched.

[1]: /messages/by-id/CAJpy0uDw0crvrUFU=FsqcuvH=dJbqPASkUhnDB=bdHV1SAc+0A@mail.gmail.com

The per-table resync feature you mentioned would make recovery easier
if it is committed, but I don’t think this fix should depend on it. If
that feature gets in, we can revisit whether a WARNING would be better
here. Thoughts?

I agree your fix should not depend on the other thread. But I think it
would be good if you post to that other thread just to mention this
current issue seems like a valid use-case where that new per-table
refresh feature might be useful.

Sure, will do that.

thanks
Shveta

#13shveta malik
shveta.malik@gmail.com
In reply to: Ajin Cherian (#11)
Re: Initial table sync copies the whole table when it is no longer published

On Wed, Nov 18, 2026 at 9:48 AM Ajin Cherian <itsajin@gmail.com> wrote:

On Wed, Nov 18, 2026 at 2:39 PM shveta malik <shveta.malik@gmail.com> wrote:

Good point Ajin. I did not think of it earlier, but on analysing it
now, I think there is a way out without the race: since
disable_on_error can be changed on a disabled sub, the user can
temporarily set 'disable_on_error=false' on disabled sub, then ENABLE
the sub, then REFRESH PUBLICATION, and then set disable_on_error back
to true. I tested this with the patch, and it works.

Yes, that works as a workaround.

As you said, this applies to all tablesync errors caused by
publisher-side changes, so I think documenting it would be better done
separately,e.g. as a note in the disable_on_error description?
Thoughts? But if others feel that we can combine it in this patch, I
am okay to do that.

Yes, a note in the disable_on_error description sounds good.

Done. Attached v3-0002. No change in 0001.

On Wed, Nov 18, 2026 at 2:40 PM shveta malik <shveta.malik@gmail.com> wrote:

I missed mentioning that alternatively, the table can be published
again on the publisher before re-enabling (if necessary).

I don't think users would want to do that if they removed the table
from the publication deliberately. It also isn't an option for an
UNLOGGED table like in Shlok's example, which can't be published at
all, or for a table that has been dropped on the publisher.

Okay, I have still added a generic line about this in the doc without
mentioning any specific resolution in this context.

thanks
Shveta

Attachments:

t259237_13
v3-0002-Doc-Explain-how-to-recover-a-subscription-disable.patchapplication/octet-stream; name=v3-0002-Doc-Explain-how-to-recover-a-subscription-disable.patchDownload+19-1
v3-0001-Do-not-copy-the-whole-table-in-tablesync-when-no-.patchapplication/octet-stream; name=v3-0001-Do-not-copy-the-whole-table-in-tablesync-when-no-.patchDownload+101-2
#14Peter Smith
smithpb2250@gmail.com
In reply to: shveta malik (#13)
Re: Initial table sync copies the whole table when it is no longer published

Hi Shveta.

Patch v3-0001 is unchanged, so it still LGTM.

Below are some comments for v3-0002 (docs).

======
src/sgml/ref/create_subscription.sgml

1.
+         <note>
+          <para>
+           If the error persists, the subscription is disabled again as soon
+           as it is re-enabled.  For example, if initial table synchronization
+           fails because the table was dropped on the publisher or removed
+           from the publication,
+           <link
linkend="sql-altersubscription-params-refresh-publication"><command>ALTER
SUBSCRIPTION ... REFRESH PUBLICATION</command></link>
+           is needed to remove it from the subscription.  However, this
+           command is not allowed while the subscription is disabled.  ...

IMO the "For example" doesn't seem to illustrate the preceding
sentence. The sentence says the subscription keeps getting disabled,
but the example jumps straight to a cause (table dropped/unpublished)
and then to the fix (REFRESH PUBLICATION). It never says that this is
a case where the error recurs, so the subscription keeps disabling
itself.

There're a couple of other minor nits -- e.g. "as soon as it is
re-enabled" is not quite accurate. The subscription is only disabled
again after the worker hits the error again, not at the moment it is
enabled.

My LLM came up with some reworded text split into example and
workaround paragraphs, which I think is better.

SUGGESTION

<note>
<para>
If the underlying cause is not resolved, the subscription is disabled again
shortly after it is re-enabled. For example, if initial table
synchronization fails because the table was dropped on the publisher or
removed from the publication, the same error recurs each time the
synchronization is retried.
</para>
<para>
In that case,
<link linkend="sql-altersubscription-params-refresh-publication"><command>ALTER
SUBSCRIPTION ... REFRESH PUBLICATION</command></link>
is needed to remove the table from the subscription, but it cannot be run
while the subscription is disabled. To work around this, either fix the
cause on the publisher before re-enabling the subscription, or temporarily
set <literal>disable_on_error</literal> to <literal>false</literal>,
re-enable the subscription using
<link linkend="sql-altersubscription-params-enable"><command>ALTER
SUBSCRIPTION ... ENABLE</command></link>,
run <command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION</command>, and then
set <literal>disable_on_error</literal> back to <literal>true</literal>.
</para>
</note>

~~~

2.
But is this the best place for describing the disable_on_error
workaround? A user who hits the REFRESH PUBLICATION restriction is
probably going to be looking at the ALTER SUBSCRIPTION docs, so maybe
the workaround details belong there?

======
Kind Regards,
Peter Smith.
Fujitsu Australia

#15vignesh C
vignesh21@gmail.com
In reply to: shveta malik (#13)
Re: Initial table sync copies the whole table when it is no longer published

On Wed, 18 Nov 2026 at 10:40, shveta malik <shveta.malik@gmail.com> wrote:

On Wed, Nov 18, 2026 at 9:48 AM Ajin Cherian <itsajin@gmail.com> wrote:

On Wed, Nov 18, 2026 at 2:39 PM shveta malik <shveta.malik@gmail.com> wrote:

Good point Ajin. I did not think of it earlier, but on analysing it
now, I think there is a way out without the race: since
disable_on_error can be changed on a disabled sub, the user can
temporarily set 'disable_on_error=false' on disabled sub, then ENABLE
the sub, then REFRESH PUBLICATION, and then set disable_on_error back
to true. I tested this with the patch, and it works.

Yes, that works as a workaround.

As you said, this applies to all tablesync errors caused by
publisher-side changes, so I think documenting it would be better done
separately,e.g. as a note in the disable_on_error description?
Thoughts? But if others feel that we can combine it in this patch, I
am okay to do that.

Yes, a note in the disable_on_error description sounds good.

Done. Attached v3-0002. No change in 0001.

On Wed, Nov 18, 2026 at 2:40 PM shveta malik <shveta.malik@gmail.com> wrote:

I missed mentioning that alternatively, the table can be published
again on the publisher before re-enabling (if necessary).

I don't think users would want to do that if they removed the table
from the publication deliberately. It also isn't an option for an
UNLOGGED table like in Shlok's example, which can't be published at
all, or for a table that has been dropped on the publisher.

Okay, I have still added a generic line about this in the doc without
mentioning any specific resolution in this context.

Thanks for the patch, couple of comments:
1. The inserts after the error can be removed:
+# Changes made on the publisher after that are not replicated either.
+$node_publisher->safe_psql(
+       'postgres', qq(
+       INSERT INTO tab_4 VALUES (6, 6, 6);
+       INSERT INTO tab_5 VALUES (6, 6, 6);
+));
+$node_publisher->wait_for_catchup('tap_sub_2');
+
+$result = $node_subscriber->safe_psql('postgres',
+       "SELECT (SELECT count(*) FROM tab_4), (SELECT count(*) FROM tab_5)");
+is($result, qq(0|0),
+       'check that no data is copied or replicated for tables that
are no longer published'
+);
+

The tablesync worker errors out, so tab_4 and tab_5 never reach the
ready state. Their changes are never applied, and this part doesn't
test the fix. The
two wait_for_log checks already show that the sync failed.

2. The REFRESH PUBLICATION block can be removed:
+# REFRESH PUBLICATION removes the tables from the subscription.
+$node_subscriber->safe_psql('postgres',
+       "ALTER SUBSCRIPTION tap_sub_2 REFRESH PUBLICATION");
+
+$result = $node_subscriber->safe_psql('postgres',
+       "SELECT count(*) FROM pg_subscription_rel WHERE srrelid IN
('tab_4'::regclass, 'tab_5'::regclass)"
+);
+is($result, qq(0),
+       'check that REFRESH PUBLICATION removes the tables from the
subscription');

025_rep_changes_for_schema.pl already checks that tables are removed
from pg_subscription_rel after REFRESH
PUBLICATION.

Regards,
Vignesh

#16shveta malik
shveta.malik@gmail.com
In reply to: vignesh C (#15)
Re: Initial table sync copies the whole table when it is no longer published

On Thu, Nov 19, 2026 at 9:52 AM vignesh C <vignesh21@gmail.com> wrote:

On Wed, 18 Nov 2026 at 10:40, shveta malik <shveta.malik@gmail.com> wrote:

On Wed, Nov 18, 2026 at 9:48 AM Ajin Cherian <itsajin@gmail.com> wrote:

On Wed, Nov 18, 2026 at 2:39 PM shveta malik <shveta.malik@gmail.com> wrote:

Good point Ajin. I did not think of it earlier, but on analysing it
now, I think there is a way out without the race: since
disable_on_error can be changed on a disabled sub, the user can
temporarily set 'disable_on_error=false' on disabled sub, then ENABLE
the sub, then REFRESH PUBLICATION, and then set disable_on_error back
to true. I tested this with the patch, and it works.

Yes, that works as a workaround.

As you said, this applies to all tablesync errors caused by
publisher-side changes, so I think documenting it would be better done
separately,e.g. as a note in the disable_on_error description?
Thoughts? But if others feel that we can combine it in this patch, I
am okay to do that.

Yes, a note in the disable_on_error description sounds good.

Done. Attached v3-0002. No change in 0001.

On Wed, Nov 18, 2026 at 2:40 PM shveta malik <shveta.malik@gmail.com> wrote:

I missed mentioning that alternatively, the table can be published
again on the publisher before re-enabling (if necessary).

I don't think users would want to do that if they removed the table
from the publication deliberately. It also isn't an option for an
UNLOGGED table like in Shlok's example, which can't be published at
all, or for a table that has been dropped on the publisher.

Okay, I have still added a generic line about this in the doc without
mentioning any specific resolution in this context.

Thanks for the patch, couple of comments:
1. The inserts after the error can be removed:
+# Changes made on the publisher after that are not replicated either.
+$node_publisher->safe_psql(
+       'postgres', qq(
+       INSERT INTO tab_4 VALUES (6, 6, 6);
+       INSERT INTO tab_5 VALUES (6, 6, 6);
+));
+$node_publisher->wait_for_catchup('tap_sub_2');
+
+$result = $node_subscriber->safe_psql('postgres',
+       "SELECT (SELECT count(*) FROM tab_4), (SELECT count(*) FROM tab_5)");
+is($result, qq(0|0),
+       'check that no data is copied or replicated for tables that
are no longer published'
+);
+

The tablesync worker errors out, so tab_4 and tab_5 never reach the
ready state. Their changes are never applied, and this part doesn't
test the fix. The
two wait_for_log checks already show that the sync failed.

Agreed that the inserts don’t test the fix, since the tables never
become ready; I have removed them along with the wait_for_catchup.
I’ve kept the count check though: the wait_for_log checks show that
the sync failed, while the count confirms the actual outcome, that
nothing was copied, which is the behaviour being fixed.

2. The REFRESH PUBLICATION block can be removed:
+# REFRESH PUBLICATION removes the tables from the subscription.
+$node_subscriber->safe_psql('postgres',
+       "ALTER SUBSCRIPTION tap_sub_2 REFRESH PUBLICATION");
+
+$result = $node_subscriber->safe_psql('postgres',
+       "SELECT count(*) FROM pg_subscription_rel WHERE srrelid IN
('tab_4'::regclass, 'tab_5'::regclass)"
+);
+is($result, qq(0),
+       'check that REFRESH PUBLICATION removes the tables from the
subscription');

025_rep_changes_for_schema.pl already checks that tables are removed
from pg_subscription_rel after REFRESH
PUBLICATION.

I gave it some thought; I think we should keep it. Both the errhint
and the doc patch suggest REFRESH PUBLICATION as the way to recover,
so it seems good to test that it works in exactly the state this fix
leaves behind, i.e. tables whose initial sync failed and are still in
DATASYNC. 025 only removes tables that are already READY. What do you
say?

Attached v4 with the change.

thanks
Shveta

Attachments:

t259237_16
v4-0001-Do-not-copy-the-whole-table-in-tablesync-when-no-.patchapplication/octet-stream; name=v4-0001-Do-not-copy-the-whole-table-in-tablesync-when-no-.patchDownload+92-2
v4-0002-Doc-Explain-how-to-recover-a-subscription-disable.patchapplication/octet-stream; name=v4-0002-Doc-Explain-how-to-recover-a-subscription-disable.patchDownload+32-1
#17shveta malik
shveta.malik@gmail.com
In reply to: Peter Smith (#14)
Re: Initial table sync copies the whole table when it is no longer published

On Thu, Nov 19, 2026 at 4:32 AM Peter Smith <smithpb2250@gmail.com> wrote:

Hi Shveta.

Patch v3-0001 is unchanged, so it still LGTM.

Below are some comments for v3-0002 (docs).

======
src/sgml/ref/create_subscription.sgml

1.
+         <note>
+          <para>
+           If the error persists, the subscription is disabled again as soon
+           as it is re-enabled.  For example, if initial table synchronization
+           fails because the table was dropped on the publisher or removed
+           from the publication,
+           <link
linkend="sql-altersubscription-params-refresh-publication"><command>ALTER
SUBSCRIPTION ... REFRESH PUBLICATION</command></link>
+           is needed to remove it from the subscription.  However, this
+           command is not allowed while the subscription is disabled.  ...

IMO the "For example" doesn't seem to illustrate the preceding
sentence. The sentence says the subscription keeps getting disabled,
but the example jumps straight to a cause (table dropped/unpublished)
and then to the fix (REFRESH PUBLICATION). It never says that this is
a case where the error recurs, so the subscription keeps disabling
itself.

There're a couple of other minor nits -- e.g. "as soon as it is
re-enabled" is not quite accurate. The subscription is only disabled
again after the worker hits the error again, not at the moment it is
enabled.

My LLM came up with some reworded text split into example and
workaround paragraphs, which I think is better.

SUGGESTION

<note>
<para>
If the underlying cause is not resolved, the subscription is disabled again
shortly after it is re-enabled. For example, if initial table
synchronization fails because the table was dropped on the publisher or
removed from the publication, the same error recurs each time the
synchronization is retried.
</para>
<para>
In that case,
<link linkend="sql-altersubscription-params-refresh-publication"><command>ALTER
SUBSCRIPTION ... REFRESH PUBLICATION</command></link>
is needed to remove the table from the subscription, but it cannot be run
while the subscription is disabled. To work around this, either fix the
cause on the publisher before re-enabling the subscription, or temporarily
set <literal>disable_on_error</literal> to <literal>false</literal>,
re-enable the subscription using
<link linkend="sql-altersubscription-params-enable"><command>ALTER
SUBSCRIPTION ... ENABLE</command></link>,
run <command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION</command>, and then
set <literal>disable_on_error</literal> back to <literal>true</literal>.
</para>
</note>

~~~

2.
But is this the best place for describing the disable_on_error
workaround? A user who hits the REFRESH PUBLICATION restriction is
probably going to be looking at the ALTER SUBSCRIPTION docs, so maybe
the workaround details belong there?

Agreed on both points, and thanks for the suggested text. I’ve used it
with a few small tweaks, split across the two pages; please have a
look.
I used “the same error recurs each time the subscription is
re-enabled” instead of “each time the synchronization is retried”,
since with disable_on_error the sync is not retried until the
subscription is re-enabled.
I also used “To resolve this” instead of “To work around this”, since
one of the two options (fixing the cause on the publisher) is an
actual fix, so “resolve” covers both. Changes are in v4 posted
earlier.

thanks
Shveta

#18vignesh C
vignesh21@gmail.com
In reply to: shveta malik (#16)
Re: Initial table sync copies the whole table when it is no longer published

On Thu, 19 Nov 2026 at 14:52, shveta malik <shveta.malik@gmail.com> wrote:

2. The REFRESH PUBLICATION block can be removed:
+# REFRESH PUBLICATION removes the tables from the subscription.
+$node_subscriber->safe_psql('postgres',
+       "ALTER SUBSCRIPTION tap_sub_2 REFRESH PUBLICATION");
+
+$result = $node_subscriber->safe_psql('postgres',
+       "SELECT count(*) FROM pg_subscription_rel WHERE srrelid IN
('tab_4'::regclass, 'tab_5'::regclass)"
+);
+is($result, qq(0),
+       'check that REFRESH PUBLICATION removes the tables from the
subscription');

025_rep_changes_for_schema.pl already checks that tables are removed
from pg_subscription_rel after REFRESH
PUBLICATION.

I gave it some thought; I think we should keep it. Both the errhint
and the doc patch suggest REFRESH PUBLICATION as the way to recover,
so it seems good to test that it works in exactly the state this fix
leaves behind, i.e. tables whose initial sync failed and are still in
DATASYNC. 025 only removes tables that are already READY. What do you
say?

I'm fine with it.

This issue is applicable to back branches also, can you share back
branch patches too.

Regards,
Vignesh

#19shveta malik
shveta.malik@gmail.com
In reply to: vignesh C (#18)
Re: Initial table sync copies the whole table when it is no longer published

On Thu, Nov 19, 2026 at 4:03 PM vignesh C <vignesh21@gmail.com> wrote:

On Thu, 19 Nov 2026 at 14:52, shveta malik <shveta.malik@gmail.com> wrote:

2. The REFRESH PUBLICATION block can be removed:
+# REFRESH PUBLICATION removes the tables from the subscription.
+$node_subscriber->safe_psql('postgres',
+       "ALTER SUBSCRIPTION tap_sub_2 REFRESH PUBLICATION");
+
+$result = $node_subscriber->safe_psql('postgres',
+       "SELECT count(*) FROM pg_subscription_rel WHERE srrelid IN
('tab_4'::regclass, 'tab_5'::regclass)"
+);
+is($result, qq(0),
+       'check that REFRESH PUBLICATION removes the tables from the
subscription');

025_rep_changes_for_schema.pl already checks that tables are removed
from pg_subscription_rel after REFRESH
PUBLICATION.

I gave it some thought; I think we should keep it. Both the errhint
and the doc patch suggest REFRESH PUBLICATION as the way to recover,
so it seems good to test that it works in exactly the state this fix
leaves behind, i.e. tables whose initial sync failed and are still in
DATASYNC. 025 only removes tables that are already READY. What do you
say?

I'm fine with it.

This issue is applicable to back branches also, can you share back
branch patches too.

Sure, I can share the back-branch patches once a committer approves
the proposed approach. I wanted to check whether anyone has any
objections to the approach itself.

Also, I am not very sure if we really need to backpatch this? I
noticed that a similar fix targeting the apply worker (commit 7c99dc5)
was not backpatched, with the following rationale: "We decided not to
backpatch this as this is a behaviour change, and we don't see field
reports."

So shall we wait little longer for further feedback?

thanks
Shveta

#20vignesh C
vignesh21@gmail.com
In reply to: shveta malik (#19)
Re: Initial table sync copies the whole table when it is no longer published

On Fri, 20 Nov 2026 at 09:02, shveta malik <shveta.malik@gmail.com> wrote:

On Thu, Nov 19, 2026 at 4:03 PM vignesh C <vignesh21@gmail.com> wrote:

On Thu, 19 Nov 2026 at 14:52, shveta malik <shveta.malik@gmail.com> wrote:

2. The REFRESH PUBLICATION block can be removed:
+# REFRESH PUBLICATION removes the tables from the subscription.
+$node_subscriber->safe_psql('postgres',
+       "ALTER SUBSCRIPTION tap_sub_2 REFRESH PUBLICATION");
+
+$result = $node_subscriber->safe_psql('postgres',
+       "SELECT count(*) FROM pg_subscription_rel WHERE srrelid IN
('tab_4'::regclass, 'tab_5'::regclass)"
+);
+is($result, qq(0),
+       'check that REFRESH PUBLICATION removes the tables from the
subscription');

025_rep_changes_for_schema.pl already checks that tables are removed
from pg_subscription_rel after REFRESH
PUBLICATION.

I gave it some thought; I think we should keep it. Both the errhint
and the doc patch suggest REFRESH PUBLICATION as the way to recover,
so it seems good to test that it works in exactly the state this fix
leaves behind, i.e. tables whose initial sync failed and are still in
DATASYNC. 025 only removes tables that are already READY. What do you
say?

I'm fine with it.

This issue is applicable to back branches also, can you share back
branch patches too.

Sure, I can share the back-branch patches once a committer approves
the proposed approach. I wanted to check whether anyone has any
objections to the approach itself.

Also, I am not very sure if we really need to backpatch this? I
noticed that a similar fix targeting the apply worker (commit 7c99dc5)
was not backpatched, with the following rationale: "We decided not to
backpatch this as this is a behaviour change, and we don't see field
reports."

So shall we wait little longer for further feedback?

Yes, that is fine with me. Added a CFentry for this at [1]https://commitfest.postgresql.org/patch/7683/.
[1]: https://commitfest.postgresql.org/patch/7683/

Regards,
Vignesh

#21Peter Smith
smithpb2250@gmail.com
In reply to: shveta malik (#17)
#22shveta malik
shveta.malik@gmail.com
In reply to: vignesh C (#20)
#23shveta malik
shveta.malik@gmail.com
In reply to: Peter Smith (#21)
#24Peter Smith
smithpb2250@gmail.com
In reply to: shveta malik (#23)
#25Shlok Kyal
shlok.kyal.oss@gmail.com
In reply to: shveta malik (#23)
#26shveta malik
shveta.malik@gmail.com
In reply to: Shlok Kyal (#25)