Initial table sync copies the whole table when it is no longer published
Initial table sync copies the whole table when it is no longer published
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
Re: Initial table sync copies the whole table when it is no longer published
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Mon, Nov 16, 2026 at 3:14 PM shveta malik 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
Re: Initial table sync copies the whole table when it is no longer published
От:
Shlok Kyal <shlok.kyal.oss@gmail.com>
Дата:
On Mon, 16 Nov 2026 at 15:15, shveta malik 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
Re: Initial table sync copies the whole table when it is no longer published
От:
Peter Smith <smithpb2250@gmail.com>
Дата:
Hi Shveta.
+1 to address this bug.
On Mon, Nov 16, 2026 at 8:45 PM shveta malik 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.
//////////
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] https://www.postgresql.org/message-id/flat/OS7PR01MB1831757BF5C212B9E04F7E615F5642%40OS7PR01MB18317.jpnprd01.prod.outlook.com#827e952b0eff4db144d28498004cf3d1
Kind Regards,
Peter Smith.
Fujitsu Australia
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 wrote:
>
> Hi Shveta.
>
> +1 to address this bug.
>
> On Mon, Nov 16, 2026 at 8:45 PM shveta malik 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.
> ======
> 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] https://www.postgresql.org/message-id/flat/OS7PR01MB1831757BF5C212B9E04F7E615F5642%40OS7PR01MB18317.jpnprd01.prod.outlook.com#827e952b0eff4db144d28498004cf3d1
>
> Kind Regards,
> Peter Smith.
> Fujitsu Australia
Re: Initial table sync copies the whole table when it is no longer published
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Mon, Nov 16, 2026 at 4:08 PM Shlok Kyal wrote: > > On Mon, 16 Nov 2026 at 15:15, shveta malik 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. > > > 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
Re: Initial table sync copies the whole table when it is no longer published
От:
Peter Smith <smithpb2250@gmail.com>
Дата:
On Tue, Nov 17, 2026 at 9:03 PM shveta malik wrote: > > On Tue, Nov 17, 2026 at 2:10 PM Peter Smith wrote: > > > > Hi Shveta. > > > > +1 to address this bug. > > > > On Mon, Nov 16, 2026 at 8:45 PM shveta malik 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
Re: Initial table sync copies the whole table when it is no longer published
От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Tue, Nov 17, 2026 at 9:03 PM shveta malik 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
Re: Initial table sync copies the whole table when it is no longer published
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Wed, Nov 18, 2026 at 6:42 AM Ajin Cherian wrote: > > On Tue, Nov 17, 2026 at 9:03 PM shveta malik 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
Re: Initial table sync copies the whole table when it is no longer published
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Wed, Nov 18, 2026 at 9:08 AM shveta malik wrote: > > On Wed, Nov 18, 2026 at 6:42 AM Ajin Cherian wrote: > > > > On Tue, Nov 17, 2026 at 9:03 PM shveta malik 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). > > 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
Re: Initial table sync copies the whole table when it is no longer published
От:
Ajin Cherian <itsajin@gmail.com>
Дата:
On Wed, Nov 18, 2026 at 2:39 PM shveta malik 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 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
Re: Initial table sync copies the whole table when it is no longer published
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Wed, Nov 18, 2026 at 4:46 AM Peter Smith wrote: > > On Tue, Nov 17, 2026 at 9:03 PM shveta malik wrote: > > > > On Tue, Nov 17, 2026 at 2:10 PM Peter Smith wrote: > > > > > > Hi Shveta. > > > > > > +1 to address this bug. > > > > > > On Mon, Nov 16, 2026 at 8:45 PM shveta malik 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]), 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]: https://www.postgresql.org/message-id/CAJpy0uDw0crvrUFU%3DFsqcuvH%3DdJbqPASkUhnDB%3DbdHV1SAc%2B0A%40mail.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
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 wrote: > > On Wed, Nov 18, 2026 at 2:39 PM shveta malik 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 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
Re: Initial table sync copies the whole table when it is no longer published
От:
Peter Smith <smithpb2250@gmail.com>
Дата:
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. + + + 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, + ALTER SUBSCRIPTION ... REFRESH PUBLICATION + 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 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. In that case, ALTER SUBSCRIPTION ... REFRESH PUBLICATION 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 disable_on_error to false, re-enable the subscription using ALTER SUBSCRIPTION ... ENABLE, run ALTER SUBSCRIPTION ... REFRESH PUBLICATION, and then set disable_on_error back to true. ~~~ 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
Re: Initial table sync copies the whole table when it is no longer published
От:
vignesh C <vignesh21@gmail.com>
Дата:
On Wed, 18 Nov 2026 at 10:40, shveta malik wrote:
>
> On Wed, Nov 18, 2026 at 9:48 AM Ajin Cherian wrote:
> >
> > On Wed, Nov 18, 2026 at 2:39 PM shveta malik 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 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
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 wrote:
>
> On Wed, 18 Nov 2026 at 10:40, shveta malik wrote:
> >
> > On Wed, Nov 18, 2026 at 9:48 AM Ajin Cherian wrote:
> > >
> > > On Wed, Nov 18, 2026 at 2:39 PM shveta malik 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 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
Re: Initial table sync copies the whole table when it is no longer published
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Thu, Nov 19, 2026 at 4:32 AM Peter Smith 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. > + > + > + 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, > + linkend="sql-altersubscription-params-refresh-publication">ALTER > SUBSCRIPTION ... REFRESH PUBLICATION > + 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 > > > > 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. > > > In that case, > ALTER > SUBSCRIPTION ... REFRESH PUBLICATION > 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 disable_on_error to false, > re-enable the subscription using > ALTER > SUBSCRIPTION ... ENABLE, > run ALTER SUBSCRIPTION ... REFRESH PUBLICATION, and then > set disable_on_error back to true. > > > > ~~~ > > 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
Re: Initial table sync copies the whole table when it is no longer published
От:
vignesh C <vignesh21@gmail.com>
Дата:
On Thu, 19 Nov 2026 at 14:52, shveta malik 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
Re: Initial table sync copies the whole table when it is no longer published
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Thu, Nov 19, 2026 at 4:03 PM vignesh C wrote:
>
> On Thu, 19 Nov 2026 at 14:52, shveta malik 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
Re: Initial table sync copies the whole table when it is no longer published
От:
vignesh C <vignesh21@gmail.com>
Дата:
On Fri, 20 Nov 2026 at 09:02, shveta malik wrote:
>
> On Thu, Nov 19, 2026 at 4:03 PM vignesh C wrote:
> >
> > On Thu, 19 Nov 2026 at 14:52, shveta malik 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].
[1] - https://commitfest.postgresql.org/patch/7683/
Regards,
Vignesh
Re: Initial table sync copies the whole table when it is no longer published
От:
Peter Smith <smithpb2250@gmail.com>
Дата:
Some review comments for v4 PATCH v4-0002 (docs) ====== doc/src/sgml/ref/alter_subscription.sgml 1. + + + This command cannot be run while the subscription is disabled. If the + subscription was disabled by + disable_on_error + because initial table synchronization keeps failing, for example + because the table or the publication was dropped on the publisher, or + the table was removed from the publication, this command is needed to + remove the table from the subscription. To resolve this, either fix + the cause on the publisher before re-enabling the subscription, or + temporarily set + disable_on_error to false, + re-enable the subscription using + ALTER SUBSCRIPTION ... ENABLE, + run ALTER SUBSCRIPTION ... REFRESH PUBLICATION, and + then set disable_on_error back to + true. + + It's OK, but IMO it wasn't necessary to give so many details about the kind of error that might have led to this. IIUC, the main point is that you may need to turn off `disable_on_error temporarily. Also, it seemed a bit wordy to spell out the full commands for "ALTER SUBSCRIPTION ... ENABLE" (which is elsewhere on the same page) and for "ALTER SUBSCRIPTION ... REFRESH PUBLICATION" (since this note is already within the context of that command). Here is some shorter text for consideration. It keeps the essential steps but avoids enumerating failure causes, which may not be exhaustive anyway. This command cannot be run while the subscription is disabled. If the subscription was disabled by disable_on_error, temporarily set it to false, re-enable the subscription, perform the refresh, and then set it back to true. YMMV. ====== Kind Regards, Peter Smith. Fujitsu Australia
Re: Initial table sync copies the whole table when it is no longer published
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Sun, Nov 22, 2026 at 9:31 AM vignesh C wrote: > > > > > 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]. > [1] - https://commitfest.postgresql.org/patch/7683/ > Thanks Vignesh. thanks Shveta
Re: Initial table sync copies the whole table when it is no longer published
On Mon, Nov 23, 2026 at 8:18 AM Peter Smith wrote: > > Some review comments for v4 > > PATCH v4-0002 (docs) > > ====== > doc/src/sgml/ref/alter_subscription.sgml > > 1. > + > + > + This command cannot be run while the subscription is disabled. If the > + subscription was disabled by > + disable_on_error > + because initial table synchronization keeps failing, for example > + because the table or the publication was dropped on the publisher, or > + the table was removed from the publication, this command is needed to > + remove the table from the subscription. To resolve this, either fix > + the cause on the publisher before re-enabling the subscription, or > + temporarily set > + disable_on_error to false, > + re-enable the subscription using > + ALTER > SUBSCRIPTION ... ENABLE, > + run ALTER SUBSCRIPTION ... REFRESH PUBLICATION, and > + then set disable_on_error back to > + true. > + > + > > It's OK, but IMO it wasn't necessary to give so many details about the > kind of error that might have led to this. IIUC, the main point is > that you may need to turn off `disable_on_error temporarily. > > Also, it seemed a bit wordy to spell out the full commands for "ALTER > SUBSCRIPTION ... ENABLE" (which is elsewhere on the same page) and for > "ALTER SUBSCRIPTION ... REFRESH PUBLICATION" (since this note is > already within the context of that command). > > Here is some shorter text for consideration. It keeps the essential > steps but avoids enumerating failure causes, which may not be > exhaustive anyway. > > > This command cannot be run while the subscription is disabled. If the > subscription was disabled by > disable_on_error, > temporarily set it to false, > re-enable > the subscription, perform the refresh, and then set it back to > true. > > > YMMV. I agree with the suggestion. I have incorporated the suggested shorter text with a few tweaks. Please have a look. thanks Shveta
Re: Initial table sync copies the whole table when it is no longer published
От:
Peter Smith <smithpb2250@gmail.com>
Дата:
On Mon, Nov 23, 2026 at 3:44 PM shveta malik wrote: ... > I agree with the suggestion. I have incorporated the suggested shorter > text with a few tweaks. Please have a look. > Patch v5-0002 (docs) LGTM. ====== Kind Regards, Peter Smith. Fujitsu Australia
Re: Initial table sync copies the whole table when it is no longer published
От:
Shlok Kyal <shlok.kyal.oss@gmail.com>
Дата:
>
> I agree with the suggestion. I have incorporated the suggested shorter
> text with a few tweaks. Please have a look.
>
Hi Shveta,
I continue testing for similar cases to unlogged tables (reported in
[1]). I found similar issues can happen with View, Materialized View,
Foreign table.
Tablesync can copy data from view/ materialized view/ foreign table on
publisher to the subscriber table on HEAD,
Here are the steps:
Pub:
create table tab1(a int, b int, c int);
create publication pub1 for table tab1;
Sub:
create table tab1(a int, b int, c int);
create subscription sub1 connection '...' publication pub1
WITH(enabled='false');
Pub:
drop table tab1;
--The table sync can copy View, Materialized View, Foreign table.
-- For View:
create view tab1 as select 1 as a, 2 as b, 3 as c;
-- For Materialized view
-- create materialized view tab1 as select 1 as a, 2 as b, 3 as c;
-- For foreign table
-- create extension postgres_fdw;
-- create table backing_tab1 as select 1 as a, 2 as b, 3 as c;
-- create server loopback_server foreign data wrapper postgres_fdw
options (host 'localhost', port '5432', dbname 'postgres');
-- create user mapping for current_user server loopback_server;
-- create foreign table tab1(a int, b int, c int) server
loopback_server options (table_name 'backing_tab1');
Sub:
alter subscription sub1 ENABLE;
-- For all the three case: 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 | 2 | 3
srrelid | srsubstate
---------+------------
tab1 | r
I applied v5-0001 and tested for the above scenarios. I confirmed that
the patch handles unlogged tables, views, materialized views, and
foreign tables by reporting appropriate errors.
I have one question about the initial relation lookup in
fetch_remote_table_info():
```
appendStringInfo(&cmd, "SELECT c.oid, c.relreplident, c.relkind"
" FROM pg_catalog.pg_class c"
" INNER JOIN pg_catalog.pg_namespace n"
" ON (c.relnamespace = n.oid)"
" WHERE n.nspname = %s"
" AND c.relname = %s",
quote_literal_cstr(nspname),
quote_literal_cstr(relname));
```
Would it make sense to add the following conditions?
" AND c.relkind IN ('r', 'p')"
" AND c.relpersistence = 'p'",
This would restrict the lookup to permanent ordinary and partitioned
tables, allowing us to reject unsupported relations at this stage
rather than during the subsequent publication-membership checks.
Thoughts?
[1]: https://www.postgresql.org/message-id/CANhcyEWcx%3DndzUW4GXxu-dFsWXnO-%2BxWhJLLnW5B4R_wPx8pcQ%40mail.gmail.com
Thanks,
Shlok Kyal