Patch
Fix stale pgoutput identity after a pending partition detach.
- Branch
- master
- Risk
- –
- Base commit
- 8e27c9261c9dd6127e16ced998d37ed7a8cb2b7d
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1d16e-d8aa-7968-b17e-38d90b47658b | jq -r .body | git am
From 78813eb7806ab0c2566807196f21459bfcf99430 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Thu, 12 Nov 2026 12:07:43 -0800
Subject: [PATCH] Fix stale pgoutput identity after a pending partition detach.
DETACH PARTITION CONCURRENTLY commits inhdetachpending in its first
transaction and invalidates only the parent's relcache. pgoutput
rebuilds RelationSyncEntry only for the relation named in a relcache
invalidation, so a walsender that has already published the partition
keeps publish_as_relid set to the former parent. A slot started
afterward sees no ancestors and does not publish the partition through
that parent. The subscriber therefore keeps applying rows the
publisher no longer returns from the parent.
Invalidate the partition and its descendants before the first
transaction commits. Descendants are included because
get_partition_ancestors() stops at an ancestor that is pending detach.
Discussion: https://www.postgresql.org/message-id/CAJpy0uA6womAP7fkmT=sSfKG_0CfKUSb7HhP_J9mw071MXc39w@mail.gmail.com
---
src/backend/commands/tablecmds.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index b6700929459..d014e998c0c 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -22453,6 +22453,30 @@ ATExecDetachPartition(List **wqueue, AlteredTableInfo *tab, Relation rel,
/* Invalidate relcache entries for the parent -- must be before close */
CacheInvalidateRelcache(rel);
+ /*
+ * inhdetachpending is stored only in pg_inherits, so this phase does
+ * not update the partition's pg_class row and would not otherwise
+ * invalidate its relcache. pgoutput rebuilds a RelationSyncEntry
+ * only when that relation is invalidated. Without an invalidation, a
+ * walsender that has already published the partition keeps sending
+ * its changes through the former parent, while a new slot treats the
+ * pending detach as standalone.
+ *
+ * Descendants are included because get_partition_ancestors() stops at
+ * an ancestor that is pending detach. find_all_inheritors() returns
+ * this partition too and locks only the descendants.
+ */
+ {
+ List *invalrels;
+ ListCell *lc;
+
+ invalrels = find_all_inheritors(RelationGetRelid(partRel),
+ AccessShareLock, NULL);
+ foreach(lc, invalrels)
+ CacheInvalidateRelcacheByRelid(lfirst_oid(lc));
+ list_free(invalrels);
+ }
+
table_close(partRel, NoLock);
table_close(rel, NoLock);
tab->rel = NULL;
base-commit: 8e27c9261c9dd6127e16ced998d37ed7a8cb2b7d
--
2.53.0