| From: | Ajin Cherian <itsajin(at)gmail(dot)com> |
|---|---|
| To: | bolyki(at)bolyki(dot)eu |
| Cc: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | Re: Logical replication: replicated TRUNCATE crash-loops a subscriber with a foreign partition lacking TRUNCATE support |
| Date: | 2026-12-03 04:15:09 |
| Message-ID: | CAFPTHDa4fgRWQWdMPdxefmYbxOSASnvSNpLA+SQ3Qn3J8Orqdw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On Thu, Dec 3, 2026 at 3:12 PM <bolyki(at)bolyki(dot)eu> wrote:
>
> Replicated TRUNCATE crash-loops a subscriber with a foreign partition
>
>
> 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.
Hi György,
Thanks for the detailed report and the patch. I could reproduce the
crash on master without the worker.c change, the new test hits TRAP:
failed Assert("routine->ExecForeignTruncate != NULL") in tablecmds.c
and fails 2 of 3 subtests. With fix.patch applied, the build is clean
and both 039_truncate_foreign_partition.pl and 013_partition.pl pass.
Your analysis matches the code: the top-level relation is already
rejected by CheckSubscriptionTargetRel() via logicalrep_rel_open(), so
only partitions added by find_all_inheritors() reach
ExecuteTruncateGuts() without the truncate_check_rel() checks.
Could you please post the fix as a proper patch generated with git
format-patch, with a commit message, and register it in the next
commitfest (https://commitfest.postgresql.org/) pointing to this
thread? That way cfbot will build and test it on all platforms and it
won't get lost. A few comments for the next version:
1.
+ if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
+ {
+ FdwRoutine *fdwroutine;
Rather than duplicating the check and the error message from
truncate_check_rel(), I think it would be better to make
truncate_check_rel() extern and call it with rel->rd_rel for both the
top-level relation and each partition in apply_handle_truncate(). That
also takes care of the missing InvokeObjectTruncateHook() call that
was noted in the per-table resync thread, and the other checks it does
are harmless here.
2.
In addition, consider replacing the Assert in ExecuteTruncateGuts()
with an elog(ERROR). ExecuteTruncateGuts() is exported and the
requirement that callers check ExecForeignTruncate is only documented
in a comment, so a cheap runtime check would protect against the next
caller that misses it.
3.
The patch keeps the current behaviour for partitions whose FDW does
support TRUNCATE, and I think that is the right choice for this fix.
But note that it leaves an inconsistency: I tested with a postgres_fdw
foreign partition on the subscriber, and a replicated TRUNCATE emptied
the table on the remote server, while a replicated INSERT routed to
the same partition fails with ERROR: cannot use relation
"public.tab_old" as logical replication target. Whether TRUNCATE
should be refused for all foreign partitions, as
CheckSubscriptionTargetRel() does for INSERT/UPDATE, is probably worth
a separate discussion, since changing it could break existing setups.
4.
In the test, the polling loop and the if ($crashed) branch exist only
to handle an unpatched server. This can be simplified to:
$node_subscriber->wait_for_log(qr/cannot truncate foreign table
"tab_old"/, $log_offset);
5. (minor)
Consider adding the test case to the existing 013_partition.pl (which
already covers TRUNCATE of partitioned tables) or 100_bugs.pl, instead
of a new test file that sets up two more clusters for a single
scenario. The 039 number may also clash with other pending patches.
Regards,
Ajin Cherian
Fujitsu Australia
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Fujii Masao | 2026-12-03 04:24:36 | Re: BUG #19967: ecpg crashes after "cursor does not exist" on FETCH or MOVE |
| Previous Message | shihao zhong | 2026-12-03 03:03:16 | Re: BUG #17545: Incorrect selectivity for IS NOT DISTINCT FROM and NULLs |