[PATCH] Fix NULL dereference in subscription REFRESH on concurrent DROP
Hi Hackers,
ALTER SUBSCRIPTION ... REFRESH PUBLICATION may crash if a
subscribed table (or sequence) is dropped concurrently.
In check_publications_origin_tables(), the function iterates over
subrel_local_oids without holding locks on the individual relations.
If a table is dropped by another session between when the OID list was
collected and when get_rel_name() is called, it returns NULL. That
NULL is then passed to quote_literal_cstr(), which dereferences it
unconditionally, causing a segfault. The same pattern exists in
check_publications_origin_sequences() as well.
Attached a patch to fix this by doing a null check after get_rel_name() and
get_namespace_name(), and skip the relation if it's gone.
Thanks,
Satya
On Sun, May 24, 2026 at 5:57 PM SATYANARAYANA NARLAPURAM <
satyanarlapuram@gmail.com> wrote:
Hi Hackers,
ALTER SUBSCRIPTION ... REFRESH PUBLICATION may crash if a
subscribed table (or sequence) is dropped concurrently.In check_publications_origin_tables(), the function iterates over
subrel_local_oids without holding locks on the individual relations.
If a table is dropped by another session between when the OID list was
collected and when get_rel_name() is called, it returns NULL. That
NULL is then passed to quote_literal_cstr(), which dereferences it
unconditionally, causing a segfault. The same pattern exists in
check_publications_origin_sequences() as well.Attached a patch to fix this by doing a null check after get_rel_name() and
get_namespace_name(), and skip the relation if it's gone.Thanks,
Satya
Hi Satya,
I've reviewed the patch and it looks good, just a small comment - instead
of silently continuing after finding a dropped local table, would you want
to log an error message?
regards,
Ajin Cherian
Fujitsu Australia
Hi,
On Sun, May 31, 2026 at 11:30 PM Ajin Cherian <itsajin@gmail.com> wrote:
I've reviewed the patch and it looks good
Thanks Satya for the off-list discussion, and thanks Ajin for the review.
just a small comment - instead of silently continuing after finding a dropped local table, would you want to log an error message?
IMHO, logging a message is unnecessary. The origin check only raises a
WARNING and doesn't drive the actual refresh, so silently skipping a
concurrently dropped relation is harmless.
A similar fix for concurrent relation drops in
pg_get_publication_tables() is in commit 63e7a0d2c, and I'm following
it in using try_table_open() and in not logging for dropped local
relations.
Please find attached the v2 patch. It has the following changes:
1/ Uses try_table_open() for all relations and gets the namespace
using RelationGetNamespace().
2/ Deduplicates the common code that quotes the subscription
relations' schema-qualified names for tables and sequences into a
helper function.
3/ Adds a TAP test with an injection point in 0002 (which I don't
intend to be committed).
We may need to backpatch the tables and sequences fix down to PG19,
and just the tables fix down to PG16 (commit 8756930190).
Thoughts?
--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com
Attachments:
t139735_3v2-0001-Fix-crash-in-subscription-REFRESH-on-concurrent-r.patchapplication/x-patch; name=v2-0001-Fix-crash-in-subscription-REFRESH-on-concurrent-r.patchDownload+42-33
v2-0002-Test-subscription-REFRESH-with-concurrently-dropp.patchapplication/x-patch; name=v2-0002-Test-subscription-REFRESH-with-concurrently-dropp.patchDownload+99-1
On Thu, Jul 30, 2026 at 6:15 PM Bharath Rupireddy
<bharath.rupireddyforpostgres@gmail.com> wrote:
Hi,
On Sun, May 31, 2026 at 11:30 PM Ajin Cherian <itsajin@gmail.com> wrote:
I've reviewed the patch and it looks good
Thanks Satya for the off-list discussion, and thanks Ajin for the review.
just a small comment - instead of silently continuing after finding a dropped local table, would you want to log an error message?
IMHO, logging a message is unnecessary. The origin check only raises a
WARNING and doesn't drive the actual refresh, so silently skipping a
concurrently dropped relation is harmless.A similar fix for concurrent relation drops in
pg_get_publication_tables() is in commit 63e7a0d2c, and I'm following
it in using try_table_open() and in not logging for dropped local
relations.Please find attached the v2 patch. It has the following changes:
Thank you for updating the patch.
1/ Uses try_table_open() for all relations and gets the namespace
using RelationGetNamespace().
I'm not sure that this patch should follow the changes commit
63e7a0d2c did for pg_get_publication_tables() since that function
opened tables if the column list is not specified, meaning that for a
FOR ALL TABLES publication it opened all tables, even before the fix.
On the other hand, check_publications_origin_tables() doesn't do so. I
think we should carefully consider that acquiring AccessShareLock on
all tables could introduce measurable overheads in execution time as
well as relcache bloat. A plain NULL check might work better here like
the v1 patch does. Even with the v1 patch, we could end up getting a
mismatched schema name and table name due to concurrent schema name
change or changing schema of the table. But the result would not be
serious; it just ends up adding a filter that could exclude unexpected
tables and missing some WARNINGs.
2/ Deduplicates the common code that quotes the subscription
relations' schema-qualified names for tables and sequences into a
helper function.
While it works for v19 and master, it doesn't work for v16, v17, and
v18. ISTM it doesn't help simplify the code that much. I guess we can
live with such a duplication.
3/ Adds a TAP test with an injection point in 0002 (which I don't
intend to be committed).
Thank you for creating the test case. I agree that the test for this
issue needs a new injection point and it would not be good for back
branches. I still see value a bit in having the regression test only
for HEAD. Feedback is welcome.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
Hi,
On Tue, Aug 18, 2026 at 11:25 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
Thank you for updating the patch.
Thanks for reviewing it.
1/ Uses try_table_open() for all relations and gets the namespace
using RelationGetNamespace().I'm not sure that this patch should follow the changes commit
63e7a0d2c did for pg_get_publication_tables() since that function
opened tables if the column list is not specifiedOn the other hand, check_publications_origin_tables() doesn't do so.
A plain NULL check might work better here like
the v1 patch does.
Agreed. I complicated this a bit with the additional table_open().
2/ Deduplicates the common code that quotes the subscription
relations' schema-qualified names for tables and sequences into a
helper function.While it works for v19 and master, it doesn't work for v16, v17, and
v18. ISTM it doesn't help simplify the code that much. I guess we can
live with such a duplication.
Agreed.
3/ Adds a TAP test with an injection point in 0002 (which I don't
intend to be committed).Thank you for creating the test case. I agree that the test for this
issue needs a new injection point and it would not be good for back
branches. I still see value a bit in having the regression test only
for HEAD. Feedback is welcome.
As this is a crash fix, having a test makes sense. +1 for HEAD only. I
tried without an injection point, but there's no table_open() or lock
in this area to hold control and concurrently drop in another session.
I moved the TAP test to the existing subscriber TAP test file to avoid
starting another full-blown server just for this. Test now covers both
concurrent table and sequence drop.
Please find the attached v3 patch. I verified the issue goes back to
PG16 where commit 8756930190 introduced the origin check. Patches for
all affected branches are attached.
--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com
Attachments:
t139735_5v3-0001-Fix-crash-in-subscription-REFRESH-on-concurrent-r.patchapplication/octet-stream; name=v3-0001-Fix-crash-in-subscription-REFRESH-on-concurrent-r.patchDownload+99-9
nocfbot-v3-0001-PG19-Fix-crash-in-subscription-REFRESH-on-concurrent-r.patchapplication/octet-stream; name=nocfbot-v3-0001-PG19-Fix-crash-in-subscription-REFRESH-on-concurrent-r.patchDownload+32-9
nocfbot-v3-0001-PG18-PG17-PG16-Fix-crash-in-subscription-REFRESH-on-concurrent-r.patchapplication/octet-stream; name=nocfbot-v3-0001-PG18-PG17-PG16-Fix-crash-in-subscription-REFRESH-on-concurrent-r.patchDownload+16-5
On Wed, Aug 19, 2026 at 6:34 PM Bharath Rupireddy
<bharath.rupireddyforpostgres@gmail.com> wrote:
Hi,
On Tue, Aug 18, 2026 at 11:25 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
Thank you for updating the patch.
Thanks for reviewing it.
1/ Uses try_table_open() for all relations and gets the namespace
using RelationGetNamespace().I'm not sure that this patch should follow the changes commit
63e7a0d2c did for pg_get_publication_tables() since that function
opened tables if the column list is not specifiedOn the other hand, check_publications_origin_tables() doesn't do so.
A plain NULL check might work better here like
the v1 patch does.Agreed. I complicated this a bit with the additional table_open().
2/ Deduplicates the common code that quotes the subscription
relations' schema-qualified names for tables and sequences into a
helper function.While it works for v19 and master, it doesn't work for v16, v17, and
v18. ISTM it doesn't help simplify the code that much. I guess we can
live with such a duplication.Agreed.
3/ Adds a TAP test with an injection point in 0002 (which I don't
intend to be committed).Thank you for creating the test case. I agree that the test for this
issue needs a new injection point and it would not be good for back
branches. I still see value a bit in having the regression test only
for HEAD. Feedback is welcome.As this is a crash fix, having a test makes sense. +1 for HEAD only. I
tried without an injection point, but there's no table_open() or lock
in this area to hold control and concurrently drop in another session.
I moved the TAP test to the existing subscriber TAP test file to avoid
starting another full-blown server just for this. Test now covers both
concurrent table and sequence drop.Please find the attached v3 patch. I verified the issue goes back to
PG16 where commit 8756930190 introduced the origin check. Patches for
all affected branches are attached.
Thank you for updating the patch! Here are some review comments:
--- a/src/test/subscription/t/001_rep_changes.pl
+++ b/src/test/subscription/t/001_rep_changes.pl
@@ -605,6 +605,69 @@ $result = $node_subscriber->safe_psql('postgres',
"SELECT count(*) FROM pg_replication_origin");
is($result, qq(0), 'check replication origin was dropped on subscriber');
+# Test that ALTER SUBSCRIPTION ... REFRESH PUBLICATION skips a subscribed
+# relation that is dropped concurrently during the refresh.
100_bugs.pl seems a better place to have have this regression test.
---
+ $bg->quit;
+
+ is($node_subscriber->safe_psql('postgres', 'SELECT 1;'),
+ '1', 'refresh survived a concurrently dropped table
and sequence');
While the background psql session survives with the proposed fix, the
ALTER SUBSCRIPTION ... REFRESH command still fails since the table and
sequence no longer exist on the subscriber. I think it's better to
make the command done without an error.
---
I think we can have the regression test in v19 too as it can be added easily.
I've made some changes including the above comments and updated the
commit message. I've attached the patch for HEAD. Please review it.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
Hi,
On Thu, Aug 20, 2026 at 5:26 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
Thank you for updating the patch! Here are some review comments:
Thanks for reviewing it.
--- a/src/test/subscription/t/001_rep_changes.pl100_bugs.pl seems a better place to have have this regression test.
Agreed.
--- + $bg->quit; + + is($node_subscriber->safe_psql('postgres', 'SELECT 1;'), + '1', 'refresh survived a concurrently dropped table and sequence');While the background psql session survives with the proposed fix, the
ALTER SUBSCRIPTION ... REFRESH command still fails since the table and
sequence no longer exist on the subscriber. I think it's better to
make the command done without an error.
Ah, good catch. Yes, that's better.
I think we can have the regression test in v19 too as it can be added easily.
I'm fine with it. My thinking is that for back branches, adding tests
for bugs makes sense as long as it doesn't introduce complexity (like
adding an injection point dependency for logical replication TAP
tests). Otherwise the fix itself is sufficient.
I've made some changes including the above comments and updated the
commit message. I've attached the patch for HEAD. Please review it.
The v4 patch looks good to me. pgindent, make check, and make
check-world are all fine.
--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com
On Thu, Aug 20, 2026 at 11:13 PM Bharath Rupireddy
<bharath.rupireddyforpostgres@gmail.com> wrote:
Hi,
On Thu, Aug 20, 2026 at 5:26 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
Thank you for updating the patch! Here are some review comments:
Thanks for reviewing it.
--- a/src/test/subscription/t/001_rep_changes.pl100_bugs.pl seems a better place to have have this regression test.
Agreed.
--- + $bg->quit; + + is($node_subscriber->safe_psql('postgres', 'SELECT 1;'), + '1', 'refresh survived a concurrently dropped table and sequence');While the background psql session survives with the proposed fix, the
ALTER SUBSCRIPTION ... REFRESH command still fails since the table and
sequence no longer exist on the subscriber. I think it's better to
make the command done without an error.Ah, good catch. Yes, that's better.
I think we can have the regression test in v19 too as it can be added easily.
I'm fine with it. My thinking is that for back branches, adding tests
for bugs makes sense as long as it doesn't introduce complexity (like
adding an injection point dependency for logical replication TAP
tests). Otherwise the fix itself is sufficient.I've made some changes including the above comments and updated the
commit message. I've attached the patch for HEAD. Please review it.The v4 patch looks good to me. pgindent, make check, and make
check-world are all fine.
Thank you for reviewing the patch!
I'll prepare patches for backbranches based on the v4 patch and push
them early next week, barring objections.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
On Fri, Aug 21, 2026 at 10:47 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
On Thu, Aug 20, 2026 at 11:13 PM Bharath Rupireddy
<bharath.rupireddyforpostgres@gmail.com> wrote:Hi,
On Thu, Aug 20, 2026 at 5:26 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
Thank you for updating the patch! Here are some review comments:
Thanks for reviewing it.
--- a/src/test/subscription/t/001_rep_changes.pl100_bugs.pl seems a better place to have have this regression test.
Agreed.
--- + $bg->quit; + + is($node_subscriber->safe_psql('postgres', 'SELECT 1;'), + '1', 'refresh survived a concurrently dropped table and sequence');While the background psql session survives with the proposed fix, the
ALTER SUBSCRIPTION ... REFRESH command still fails since the table and
sequence no longer exist on the subscriber. I think it's better to
make the command done without an error.Ah, good catch. Yes, that's better.
I think we can have the regression test in v19 too as it can be added easily.
I'm fine with it. My thinking is that for back branches, adding tests
for bugs makes sense as long as it doesn't introduce complexity (like
adding an injection point dependency for logical replication TAP
tests). Otherwise the fix itself is sufficient.I've made some changes including the above comments and updated the
commit message. I've attached the patch for HEAD. Please review it.The v4 patch looks good to me. pgindent, make check, and make
check-world are all fine.Thank you for reviewing the patch!
I'll prepare patches for backbranches based on the v4 patch and push
them early next week, barring objections.
Pushed.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com