diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index f54e7d531a..45aed74019 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -3058,7 +3058,7 @@ static void
 insert_rel_type_cache_if_needed(TypeCacheEntry *typentry)
 {
 	/* Immediately quit for non-composite types */
-	if (typentry->typtype != TYPTYPE_COMPOSITE)
+	if (!typentry || typentry->typtype != TYPTYPE_COMPOSITE)
 		return;
 
 	/* typrelid should be given for composite types */
@@ -3147,8 +3147,13 @@ delete_rel_type_cache_if_needed(TypeCacheEntry *typentry)
 	}
 }
 
+/*
+ * Add into the accessory hash table entries, added into TypCache and not added
+ * into the RelIdToTypeId matching hash table.
+ * It may happen in case of an error raised during the lookup_type_cache call.
+ */
 static void
-cleanup_in_progress_typentries(void)
+finalize_in_progress_typentries(void)
 {
 	int			i;
 
@@ -3168,11 +3173,11 @@ cleanup_in_progress_typentries(void)
 void
 AtEOXact_TypeCache(void)
 {
-	cleanup_in_progress_typentries();
+	finalize_in_progress_typentries();
 }
 
 void
 AtEOSubXact_TypeCache(void)
 {
-	cleanup_in_progress_typentries();
+	finalize_in_progress_typentries();
 }
