From a31d7b15d9e2f7717d644df26eea1c5b088381ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C3=B6rgy=20Bolyki?= Date: Thu, 3 Dec 2026 06:22:29 +0000 Subject: [PATCH v2] Fix apply worker crash on TRUNCATE of table with foreign partition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a logical replication apply worker applies a TRUNCATE to a partitioned table, apply_handle_truncate() adds all partitions of the table to the list of relations it passes to ExecuteTruncateGuts(). A partition can be a foreign table. For foreign tables, ExecuteTruncateGuts() calls the ExecForeignTruncate callback of the foreign data wrapper, relying on its caller to have verified with truncate_check_rel() that the callback exists. The apply worker never made that check. So, if the foreign data wrapper of such a partition does not support TRUNCATE, as is the case for file_fdw, the worker called a NULL function pointer, or failed an assertion in an assert-enabled build. As the restarted worker receives the same transaction again, the subscriber kept crashing until the subscription was disabled or the partition detached. To fix, make truncate_check_rel() extern and call it in apply_handle_truncate() for the relation named by the publisher and for each of its partitions, as ExecuteTruncate() does. The apply worker now fails with the same error as a TRUNCATE command run on the subscriber, and retries as it does after any other error. As a side effect, the apply worker now invokes the object access hook for TRUNCATE, which it did not do before. truncate_check_perms() and truncate_check_activity() remain uncalled there: TargetPrivilegesCheck() already checks the TRUNCATE privilege and the worker already skips temporary partitions of other sessions, while the check for other uses of the table within the transaction is not needed to fix this crash and would be a change of behavior. In addition, replace the Assert in ExecuteTruncateGuts() with a run-time check, so that any other caller that omits the check gets an error instead of a crash. This does not change what happens to foreign partitions whose foreign data wrapper supports TRUNCATE. They are still truncated, even though the apply worker refuses to insert into or update a foreign partition. Whether that should change is left for a separate discussion. Foreign tables can be truncated since v14, and the apply worker has had this problem since then, so back-patch to all supported branches. Reported-by: György Bolyki Author: György Bolyki Reviewed-by: Ajin Cherian Discussion: https://postgr.es/m/CADaX-_YrNJAj29Ph1eje92ctGVoAeY4=-1ROKX1wDex-DkXmcg@mail.gmail.com Backpatch-through: 15 --- src/backend/commands/tablecmds.c | 17 ++++++--- src/backend/replication/logical/worker.c | 10 +++++ src/include/commands/tablecmds.h | 2 + src/test/subscription/Makefile | 2 +- src/test/subscription/t/013_partition.pl | 47 ++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 6 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 132388e8..2ce63014 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -359,7 +359,6 @@ typedef enum addFkConstraintSides #define child_dependency_type(child_is_partition) \ ((child_is_partition) ? DEPENDENCY_AUTO : DEPENDENCY_NORMAL) -static void truncate_check_rel(Oid relid, Form_pg_class reltuple); static void truncate_check_perms(Oid relid, Form_pg_class reltuple); static void truncate_check_activity(Relation rel); static void RangeVarCallbackForTruncate(const RangeVar *relation, @@ -2102,8 +2101,14 @@ ExecuteTruncateGuts(List *explicit_rels, { FdwRoutine *routine = GetFdwRoutineByServerId(ft_info->serverid); - /* truncate_check_rel() has checked that already */ - Assert(routine->ExecForeignTruncate != NULL); + /* + * Callers should have checked that already, as + * truncate_check_rel() does, but calling a missing callback + * would crash the server, so make sure. + */ + if (routine->ExecForeignTruncate == NULL) + elog(ERROR, "foreign-data wrapper of server %u does not support TRUNCATE", + ft_info->serverid); routine->ExecForeignTruncate(ft_info->rels, behavior, @@ -2194,9 +2199,11 @@ ExecuteTruncateGuts(List *explicit_rels, /* * Check that a given relation is safe to truncate. Subroutine for - * ExecuteTruncate() and RangeVarCallbackForTruncate(). + * ExecuteTruncate() and RangeVarCallbackForTruncate(). Other callers of + * ExecuteTruncateGuts(), such as the logical replication apply worker, use + * it too. */ -static void +void truncate_check_rel(Oid relid, Form_pg_class reltuple) { char *relname = NameStr(reltuple->relname); diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index eccf6d48..2424d458 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -2425,6 +2425,15 @@ apply_handle_truncate(StringInfo s) } remote_rels = lappend(remote_rels, rel); + + /* + * Make the same checks as the TRUNCATE command does before calling + * ExecuteTruncateGuts(), which relies on them. In particular, this + * rejects a foreign table whose foreign data wrapper does not support + * TRUNCATE. The relation named by the publisher cannot be a foreign + * table, but one of its partitions can, see below. + */ + truncate_check_rel(rel->localreloid, rel->localrel->rd_rel); TargetPrivilegesCheck(rel->localrel, ACL_TRUNCATE); rels = lappend(rels, rel->localrel); relids = lappend_oid(relids, rel->localreloid); @@ -2463,6 +2472,7 @@ apply_handle_truncate(StringInfo s) continue; } + truncate_check_rel(childrelid, childrel->rd_rel); TargetPrivilegesCheck(childrel, ACL_TRUNCATE); rels = lappend(rels, childrel); part_rels = lappend(part_rels, childrel); diff --git a/src/include/commands/tablecmds.h b/src/include/commands/tablecmds.h index 5d4037f2..1ef131ca 100644 --- a/src/include/commands/tablecmds.h +++ b/src/include/commands/tablecmds.h @@ -17,6 +17,7 @@ #include "access/htup.h" #include "catalog/dependency.h" #include "catalog/objectaddress.h" +#include "catalog/pg_class.h" #include "nodes/parsenodes.h" #include "storage/lock.h" #include "utils/relcache.h" @@ -61,6 +62,7 @@ extern void ExecuteTruncateGuts(List *explicit_rels, List *relids_logged, DropBehavior behavior, bool restart_seqs); +extern void truncate_check_rel(Oid relid, Form_pg_class reltuple); extern void SetRelationHasSubclass(Oid relationId, bool relhassubclass); diff --git a/src/test/subscription/Makefile b/src/test/subscription/Makefile index 5f2b3a5c..839bddc8 100644 --- a/src/test/subscription/Makefile +++ b/src/test/subscription/Makefile @@ -13,7 +13,7 @@ subdir = src/test/subscription top_builddir = ../../.. include $(top_builddir)/src/Makefile.global -EXTRA_INSTALL = contrib/hstore +EXTRA_INSTALL = contrib/file_fdw contrib/hstore export with_icu diff --git a/src/test/subscription/t/013_partition.pl b/src/test/subscription/t/013_partition.pl index b80615f4..fb6d342b 100644 --- a/src/test/subscription/t/013_partition.pl +++ b/src/test/subscription/t/013_partition.pl @@ -877,6 +877,53 @@ $result = $node_subscriber2->safe_psql('postgres', "SELECT a, b, c FROM tab5_1 ORDER BY 1"); is($result, qq(4||1), 'updates of tab5 replicated correctly'); +# Test that a replicated TRUNCATE fails cleanly, instead of crashing the +# apply worker, when the target table has a foreign partition whose foreign +# data wrapper does not support TRUNCATE, which is the case for file_fdw. +# The data file is never read, so it does not need to exist. +$node_publisher->safe_psql('postgres', "CREATE TABLE tab6 (a int)"); + +$node_subscriber2->safe_psql( + 'postgres', qq( + CREATE EXTENSION file_fdw; + CREATE SERVER file_server FOREIGN DATA WRAPPER file_fdw; + CREATE TABLE tab6 (a int) PARTITION BY LIST (a); + CREATE TABLE tab6_1 PARTITION OF tab6 FOR VALUES IN (1); + CREATE FOREIGN TABLE tab6_2 PARTITION OF tab6 FOR VALUES IN (2) + SERVER file_server OPTIONS (filename 'tab6_2.csv'); +)); + +$node_subscriber2->safe_psql('postgres', + "ALTER SUBSCRIPTION sub2 REFRESH PUBLICATION"); + +$node_subscriber2->wait_for_subscription_sync; + +$node_publisher->safe_psql('postgres', "INSERT INTO tab6 VALUES (1)"); + +$node_publisher->wait_for_catchup('sub2'); + +$result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab6_1"); +is($result, qq(1), 'insert into tab6 replicated'); + +$log_location = -s $node_subscriber2->logfile; + +$node_publisher->safe_psql('postgres', "TRUNCATE tab6"); + +$node_subscriber2->wait_for_log( + qr/ERROR: ( [A-Z0-9]+:)? cannot truncate foreign table "tab6_2"/, + $log_location); + +# The apply worker retries the transaction until it succeeds, which it does +# once the foreign partition is detached. +$node_subscriber2->safe_psql('postgres', + "ALTER TABLE tab6 DETACH PARTITION tab6_2"); + +$node_publisher->wait_for_catchup('sub2'); + +$result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab6"); +is($result, qq(), + 'truncate of tab6 replicated after detaching the foreign partition'); + # Validate we didn't neglect to cleanup any resources on either subscriber. foreach my $node ($node_subscriber1, $node_subscriber2) { -- 2.56.1