Patch
Fix stale pgoutput identity after a pending partition detach.
- Branch
- REL_17_STABLE
- Risk
- –
- Base commit
- c87192d13dcd933256b1a2858bc4ddf2c1eb56d7
To apply it to a checkout of REL_17_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1d16e-d8aa-7b93-842d-0ae450945b06 | jq -r .body | git am
From d153e2b0a432f16645908b30a454b2a56c635018 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 982d98d43fa..915d9f690b3 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -19966,6 +19966,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: c87192d13dcd933256b1a2858bc4ddf2c1eb56d7
--
2.53.0