| From: | bolyki(at)bolyki(dot)eu |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | Logical replication: replicated TRUNCATE crash-loops a subscriber with a foreign partition lacking TRUNCATE support |
| Date: | 2026-11-30 22:14:36 |
| Message-ID: | CADaX-_YrNJAj29Ph1eje92ctGVoAeY4=-1ROKX1wDex-DkXmcg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
Replicated TRUNCATE crash-loops a subscriber with a foreign partition
Summary. apply_handle_truncate() in replication/logical/worker.c
expands a partitioned target with find_all_inheritors() and hands every
partition to ExecuteTruncateGuts() after TargetPrivilegesCheck() only.
The SQL command first runs truncate_check_rel(), which rejects a foreign
table whose FDW has no ExecForeignTruncate callback ("cannot truncate
foreign table"). ExecuteTruncateGuts() relies on that:
/* truncate_check_rel() has checked that already */
Assert(routine->ExecForeignTruncate != NULL);
routine->ExecForeignTruncate(ft_info->rels, behavior, restart_seqs);
The apply worker never makes the check, so a production build calls a
NULL pointer: the worker dies with SIGSEGV, the postmaster terminates
every backend, and the restarted worker gets the same transaction again.
Who can trigger it. A role on the publisher that holds TRUNCATE on a
published table; no REPLICATION attribute, no ownership and no access
to the subscriber are needed. The subscriber's copy of that table must
be partitioned and have a foreign table among its partitions, served by
an FDW without TRUNCATE support: file_fdw in core (for example a
partition of archived rows kept in a file), or a third-party FDW
without the callback. I ran it with the publisher's table partitioned
and published with publish_via_partition_root (attached) and with a
plain publisher table (18.7, not attached). One TRUNCATE on the
publisher then takes down every session of the subscriber cluster,
again and again, until an administrator disables the subscription
between two restarts or detaches the partition. No memory disclosure,
no code execution: a server on which the role has no login goes down.
Observed (macOS arm64, -O2 builds, logs attached). In the 8 seconds
the script waits, the subscriber logged "terminated by signal 11" 55
times on master ca492a9197a4 (run-master.log), 71 on 19.0, 51, 43 and
70 on the 17.12, 16.16 and 15.20 branch tips and 62 on 18.7 as shipped
by Homebrew (run-branches.log; a busy machine, read them as "many").
19.0 with --enable-cassert stops at TRAP: failed
Assert("routine->ExecForeignTruncate != NULL"), tablecmds.c line 2315
(run-19.0-cassert.log). The callback arrived in 14, so 15 to 19 and
master are affected; 14 is end of life and has the same code.
Reproduction. "sh reproduce.sh.txt <install-prefix>" makes a throwaway
publisher and subscriber in a new temporary directory (Unix sockets
only), applies publisher_setup.sql.txt and subscriber_setup.sql.txt,
runs attacker.sql.txt as role "app" (INSERT and TRUNCATE on the table
only), counts the crashes and removes everything; exit status 1 means
the subscriber crashed. The role's whole input is
TRUNCATE tab;
Proposed fix (fix.patch, against master ca492a9197a4; run on master
only). In apply_handle_truncate(), for a partition that is a foreign
table, look up the FDW routine and raise the same error as
truncate_check_rel() when ExecForeignTruncate is missing. The apply
worker then logs 'cannot truncate foreign table "tab_old"' and retries,
as for any other apply error; nothing crashes (run-master.log).
Partitions of FDWs that support TRUNCATE keep today's behaviour (by
reading). Alternatives you may prefer: CheckSubscriptionTargetRel() for
every partition, as the tuple-routing paths do (refuses all foreign
partitions); an extern truncate_check_rel(); or an elog(ERROR) instead
of the Assert in ExecuteTruncateGuts(). The patch adds
src/test/subscription/t/039_truncate_foreign_partition.pl (needs
contrib/file_fdw); it fails on unpatched master and passes with the
patch (test-before-after.log).
Prior discussion. The INSERT and UPDATE form of this crash was fixed in
2022 ("Segfault on logical replication to partitioned table with
foreign children" on pgsql-hackers, commit be541efbfd, announced in
1787918(dot)1667407062(at)sss(dot)pgh(dot)pa(dot)us) with a relkind check in the
tuple-routing code; TRUNCATE is not mentioned there. In the "Per-table
resync for logical replication subscriptions" thread, message
CAA36msodPo-2JHL-G9yn-zU_80sg9OSiTFKoeySafYtDSWRHrA(at)mail(dot)gmail(dot)com of
1 September 2026 notes that apply_handle_truncate() calls none of the
truncate_check_*() functions, but only for the object access hook; it
does not mention foreign tables. I found no report of this crash on
pgsql-hackers, pgsql-bugs, pgsql-committers or ai-annex.j-davis.com.
I post it here because your security page does not normally count
denial of service by an authenticated SQL statement as a vulnerability
and the 2022 case was handled in public.
Credit: György Bolyki, Magrathean UK Ltd
Found with the help of Mythos 5.2 (AI-assisted code analysis); every
claim, the PoC and the patch were verified by hand on real builds.
| Attachment | Content-Type | Size |
|---|---|---|
| attacker.sql.txt | text/plain | 123 bytes |
| fix.patch | text/plain | 5.9 KB |
| publisher_setup.sql.txt | text/plain | 429 bytes |
| reproduce.sh.txt | text/plain | 2.4 KB |
| run-19.0-cassert.log | text/plain | 1000 bytes |
| run-branches.log | text/plain | 5.4 KB |
| run-master.log | text/plain | 1.9 KB |
| subscriber_setup.sql.txt | text/plain | 618 bytes |
| test-before-after.log | text/plain | 867 bytes |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Zsolt Parragi | 2026-11-30 22:27:45 | Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance |
| Previous Message | Robert Haas | 2026-11-30 21:59:04 | Re: BUG #19519: REPACK can fail due to missing chunk for toast value |