diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index 6d522573..722c8395 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -263,6 +263,7 @@ #include "commands/trigger.h" #include "executor/executor.h" #include "executor/execPartition.h" +#include "foreign/fdwapi.h" #include "libpq/pqformat.h" #include "miscadmin.h" #include "optimizer/optimizer.h" @@ -3840,6 +3841,25 @@ apply_handle_truncate(StringInfo s) continue; } + /* + * A partition can be a foreign table. ExecuteTruncateGuts() + * relies on its caller to have verified that the foreign + * data wrapper supports TRUNCATE, as truncate_check_rel() + * does for the SQL command; without that it would call a + * NULL ExecForeignTruncate callback. + */ + if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE) + { + FdwRoutine *fdwroutine; + + fdwroutine = GetFdwRoutineForRelation(childrel, false); + if (fdwroutine->ExecForeignTruncate == NULL) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot truncate foreign table \"%s\"", + RelationGetRelationName(childrel)))); + } + TargetPrivilegesCheck(childrel, ACL_TRUNCATE); rels = lappend(rels, childrel); part_rels = lappend(part_rels, childrel); diff --git a/src/test/subscription/Makefile b/src/test/subscription/Makefile index 1b22703d..a6384761 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/meson.build b/src/test/subscription/meson.build index e71e95c6..836145fd 100644 --- a/src/test/subscription/meson.build +++ b/src/test/subscription/meson.build @@ -48,6 +48,7 @@ tests += { 't/036_sequences.pl', 't/037_except.pl', 't/038_walsnd_shutdown_timeout.pl', + 't/039_truncate_foreign_partition.pl', 't/100_bugs.pl', ], }, diff --git a/src/test/subscription/t/039_truncate_foreign_partition.pl b/src/test/subscription/t/039_truncate_foreign_partition.pl new file mode 100644 index 00000000..145e4977 --- /dev/null +++ b/src/test/subscription/t/039_truncate_foreign_partition.pl @@ -0,0 +1,92 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Test that a replicated TRUNCATE of a partitioned table does not crash the +# subscriber when one of its partitions there is a foreign table whose +# foreign data wrapper has no TRUNCATE support. +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; +use Time::HiRes qw(usleep); + +my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); +$node_publisher->init(allows_streaming => 'logical'); +$node_publisher->start; + +my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); +$node_subscriber->init; +$node_subscriber->start; + +$node_publisher->safe_psql( + 'postgres', qq( + CREATE TABLE tab (a text, b int) PARTITION BY LIST (a); + CREATE TABLE tab_live PARTITION OF tab FOR VALUES IN ('live'); + CREATE TABLE tab_old PARTITION OF tab FOR VALUES IN ('old'); + CREATE PUBLICATION pub FOR TABLE tab + WITH (publish_via_partition_root = true); +)); + +# On the subscriber the partition for old rows is a file_fdw foreign table. +# file_fdw does not implement ExecForeignTruncate. +my $datafile = $node_subscriber->data_dir . '/tab_old.csv'; +append_to_file($datafile, ''); + +my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; +$node_subscriber->safe_psql( + 'postgres', qq( + CREATE EXTENSION file_fdw; + CREATE SERVER file_server FOREIGN DATA WRAPPER file_fdw; + CREATE TABLE tab (a text, b int) PARTITION BY LIST (a); + CREATE TABLE tab_live PARTITION OF tab FOR VALUES IN ('live'); + CREATE FOREIGN TABLE tab_old PARTITION OF tab FOR VALUES IN ('old') + SERVER file_server OPTIONS (filename '$datafile', format 'csv'); + CREATE SUBSCRIPTION sub CONNECTION '$publisher_connstr' PUBLICATION pub + WITH (copy_data = false); +)); +$node_subscriber->wait_for_subscription_sync($node_publisher, 'sub'); + +$node_publisher->safe_psql('postgres', "INSERT INTO tab VALUES ('live', 1)"); +$node_publisher->wait_for_catchup('sub'); +is( $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_live"), + '1', + 'row replicated into the regular partition'); + +# The replicated TRUNCATE must fail cleanly in the apply worker, as the same +# command does when run on the subscriber directly. +my $log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', "TRUNCATE tab"); + +my ($crashed, $rejected) = (0, 0); +foreach my $i (1 .. 10 * $PostgreSQL::Test::Utils::timeout_default) +{ + my $log = slurp_file($node_subscriber->logfile, $log_offset); + $crashed = 1 if $log =~ /was terminated by signal/; + $rejected = 1 if $log =~ /cannot truncate foreign table "tab_old"/; + last if $crashed || $rejected; + usleep(100_000); +} +ok(!$crashed, 'apply worker did not crash on replicated TRUNCATE'); +ok($rejected, 'replicated TRUNCATE rejected for the foreign partition'); + +if ($crashed) +{ + # The worker would crash again on every restart; give up here. + $node_subscriber->stop('immediate', fail_ok => 1); + $node_publisher->stop('fast'); + done_testing(); + exit; +} + +# Once the foreign partition is out of the way, the TRUNCATE is applied. +$node_subscriber->safe_psql('postgres', + "ALTER TABLE tab DETACH PARTITION tab_old"); +$node_publisher->wait_for_catchup('sub'); +is( $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_live"), + '0', + 'TRUNCATE applied after detaching the foreign partition'); + +$node_subscriber->stop('fast'); +$node_publisher->stop('fast'); + +done_testing();