From 7cb5c1b7fdf02f49fd1d023dc1dc10bc8bccbce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C3=B6rgy=20Bolyki?= Date: Fri, 4 Dec 2026 07:23:45 +0000 Subject: [PATCH v3] Reject foreign partitions in replicated TRUNCATE 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(), without checking them. 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. 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. Logical replication otherwise never writes to foreign tables: a foreign table is rejected as a subscription target, and since commit be541efbfd, apply_handle_tuple_routing() rejects a foreign partition for INSERT, UPDATE and DELETE. To fix, make apply_handle_truncate() call CheckSubscriptionTargetRel() for each partition as well, so that a TRUNCATE is refused in the same way. This is a change of behavior: a replicated TRUNCATE no longer truncates a foreign partition whose foreign data wrapper supports TRUNCATE, such as postgres_fdw. It now fails with the same error as other replicated changes to that partition, and the worker retries it as after any other error. Also 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. Besides making the checks that ExecuteTruncateGuts() relies on, this makes the apply worker invoke 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 would be a change of behavior this fix does not need. 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. 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 | 17 +++++++++ src/include/commands/tablecmds.h | 2 + src/test/subscription/Makefile | 3 +- src/test/subscription/t/013_partition.pl | 48 ++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 6 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index c6436820..f15c17b0 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -365,7 +365,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, @@ -2220,8 +2219,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, @@ -2319,9 +2324,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 1554523b..b65a07c7 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -3234,6 +3234,12 @@ 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. + */ + 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); @@ -3272,6 +3278,17 @@ apply_handle_truncate(StringInfo s) continue; } + /* + * Check for supported relkind, as + * apply_handle_tuple_routing() does for the partitions it + * routes changes to. A partition can be a foreign table, and + * logical replication does not write to foreign tables. + */ + CheckSubscriptionTargetRel(childrelid, + get_namespace_name(RelationGetNamespace(childrel)), + RelationGetRelationName(childrel)); + + 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 85cbad3d..b4710a45 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" @@ -64,6 +65,7 @@ extern void ExecuteTruncateGuts(List *explicit_rels, DropBehavior behavior, bool restart_seqs, bool run_as_table_owner); +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 bb9ded31..33044454 100644 --- a/src/test/subscription/Makefile +++ b/src/test/subscription/Makefile @@ -13,7 +13,8 @@ subdir = src/test/subscription top_builddir = ../../.. include $(top_builddir)/src/Makefile.global -EXTRA_INSTALL = contrib/hstore \ +EXTRA_INSTALL = contrib/file_fdw \ + contrib/hstore \ src/test/modules/injection_points export with_icu diff --git a/src/test/subscription/t/013_partition.pl b/src/test/subscription/t/013_partition.pl index d03a4a52..cc0f99b1 100644 --- a/src/test/subscription/t/013_partition.pl +++ b/src/test/subscription/t/013_partition.pl @@ -890,6 +890,54 @@ $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 is refused when the target table has a +# foreign partition, as INSERT, UPDATE and DELETE are. Without that check, +# the apply worker crashed if the foreign data wrapper of the partition did +# 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 use relation "public.tab6_2" as logical replication target/, + $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