From abece22dcc24c20ce5c0275dc31bf1e7e607aa35 Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Wed, 6 Dec 2023 14:39:36 +0300
Subject: [PATCH v1 2/2] [relcache.c] Add missing error codes to PANIC/FATAL
 error reports

---
 src/backend/utils/cache/relcache.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index b3faccbefe..95e18eea94 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -351,7 +351,9 @@ ScanPgRelation(Oid targetRelId, bool indexOK, bool force_non_historic)
 	 * it probably means a relcache entry that needs to be nailed isn't.
 	 */
 	if (!OidIsValid(MyDatabaseId))
-		elog(FATAL, "cannot read pg_class without having selected a database");
+		ereport(FATAL,
+				(errcode(ERRCODE_INTERNAL_ERROR),
+				 errmsg("cannot read pg_class without having selected a database")));
 
 	/*
 	 * form a scan key
@@ -4367,7 +4369,9 @@ load_critical_index(Oid indexoid, Oid heapoid)
 	LockRelationOid(indexoid, AccessShareLock);
 	ird = RelationBuildDesc(indexoid, true);
 	if (ird == NULL)
-		elog(PANIC, "could not open critical system index %u", indexoid);
+		ereport(PANIC,
+				(errcode(ERRCODE_DATA_CORRUPTED),
+				 errmsg("could not open critical system index %u", indexoid)));
 	ird->rd_isnailed = true;
 	ird->rd_refcnt = 1;
 	UnlockRelationOid(indexoid, AccessShareLock);
@@ -6530,7 +6534,9 @@ write_relcache_init_file(bool shared)
 	 */
 	magic = RELCACHE_INIT_FILEMAGIC;
 	if (fwrite(&magic, 1, sizeof(magic), fp) != sizeof(magic))
-		elog(FATAL, "could not write init file");
+		ereport(FATAL,
+				(errcode_for_file_access(),
+				 errmsg("could not write init file")));
 
 	/*
 	 * Write all the appropriate reldescs (in no particular order).
@@ -6631,7 +6637,9 @@ write_relcache_init_file(bool shared)
 	}
 
 	if (FreeFile(fp))
-		elog(FATAL, "could not write init file");
+		ereport(FATAL,
+				(errcode_for_file_access(),
+				 errmsg("could not write init file")));
 
 	/*
 	 * Now we have to check whether the data we've so painstakingly
@@ -6681,9 +6689,13 @@ static void
 write_item(const void *data, Size len, FILE *fp)
 {
 	if (fwrite(&len, 1, sizeof(len), fp) != sizeof(len))
-		elog(FATAL, "could not write init file");
+		ereport(FATAL,
+				(errcode_for_file_access(),
+				 errmsg("could not write init file")));
 	if (len > 0 && fwrite(data, 1, len, fp) != len)
-		elog(FATAL, "could not write init file");
+		ereport(FATAL,
+				(errcode_for_file_access(),
+				 errmsg("could not write init file")));
 }
 
 /*
-- 
2.25.1

