From 53093a0405d3b81d45024aefb5cd45a9453cfc2e Mon Sep 17 00:00:00 2001 From: Vignesh C Date: Tue, 13 Oct 2026 10:15:06 +0530 Subject: [PATCH] Avoid cache lookup failure for concurrently dropped relations InvalidatePublicationRels() can receive relation OIDs collected from a catalog snapshot by GetSchemaPublicationRelations(). A relation may be dropped after the snapshot is taken, leaving its OID in the list. Trying to invalidate the relcache for such a relation can then fail with "cache lookup failed for relation". Skip relations that no longer exist in InvalidatePublicationRels(). --- src/backend/commands/publicationcmds.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 301050104dd..35369b9eb3c 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -1205,6 +1205,26 @@ AlterPublicationOptions(ParseState *pstate, AlterPublicationStmt *stmt, InvokeObjectPostAlterHook(PublicationRelationId, pubform->oid, 0); } +/* + * Invalidate the relcache entry of a single relation, ignoring a relation + * that no longer exists. + * + * The relation list is built from a catalog scan that takes no lock on the + * relations, so a relation can be dropped concurrently before we get here. + */ +static void +InvalidatePublicationRel(Oid relid) +{ + HeapTuple tup; + + tup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid)); + if (!HeapTupleIsValid(tup)) + return; + + CacheInvalidateRelcacheByTuple(tup); + ReleaseSysCache(tup); +} + /* * Invalidate the relations. */ @@ -1220,7 +1240,7 @@ InvalidatePublicationRels(List *relids) ListCell *lc; foreach(lc, relids) - CacheInvalidateRelcacheByRelid(lfirst_oid(lc)); + InvalidatePublicationRel(lfirst_oid(lc)); } else CacheInvalidateRelcacheAll(); @@ -1760,6 +1780,9 @@ RemovePublicationRelById(Oid proid) relids = GetPubPartitionOptionRelations(relids, PUBLICATION_PART_ALL, pubrel->prrelid); + /* DEBUG-ONLY (remove): widen the race window for issue2.sh */ + pg_usleep(5000000L); + InvalidatePublicationRels(relids); CatalogTupleDelete(rel, &tup->t_self); -- 2.55.0