From 2bb3e54862c37ee2a20fed21513a3df309381919 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Thu, 8 Apr 2021 11:10:44 -0400
Subject: [PATCH] Fix race condition in relation_needs_vacanalyze

---
 src/backend/postmaster/autovacuum.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index aef9ac4dd2..96073d4597 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -3223,18 +3223,23 @@ relation_needs_vacanalyze(Oid relid,
 			ListCell   *lc;
 
 			reltuples = 0;
 
-			/* Find all members of inheritance set taking AccessShareLock */
-			children = find_all_inheritors(relid, AccessShareLock, NULL);
+			/*
+			 * Find all members of inheritance set.  Beware that they may
+			 * disappear from under us, since we don't acquire any locks.
+			 */
+			children = find_all_inheritors(relid, NoLock, NULL);
 
 			foreach(lc, children)
 			{
 				Oid			childOID = lfirst_oid(lc);
 				HeapTuple	childtuple;
 				Form_pg_class childclass;
 
 				childtuple = SearchSysCache1(RELOID, ObjectIdGetDatum(childOID));
+				if (childtuple == NULL)
+					continue;
 				childclass = (Form_pg_class) GETSTRUCT(childtuple);
 
 				/* Skip a partitioned table and foreign partitions */
 				if (RELKIND_HAS_STORAGE(childclass->relkind))
-- 
2.20.1

