diff --git a/src/backend/utils/adt/acl.c b/src/backend/utils/adt/acl.c
index dc10b4a483..0ed3ba866e 100644
--- a/src/backend/utils/adt/acl.c
+++ b/src/backend/utils/adt/acl.c
@@ -5062,8 +5062,8 @@ roles_is_member_of(Oid roleid, enum RoleRecurseType type,
 			 * While otherid==InvalidOid shouldn't appear in the catalog, the
 			 * OidIsValid() avoids crashing if that arises.
 			 */
-			if (otherid == admin_of && form->admin_option &&
-				OidIsValid(admin_of) && !OidIsValid(*admin_role))
+			if (admin_role != NULL && *admin_role == InvalidOid &&
+				otherid == admin_of && form->admin_option && OidIsValid(admin_of))
 				*admin_role = memberid;
 
 			/* If we're supposed to ignore non-heritable grants, do so. */