From 197f997dc08dac80407c6158c8e61b1394702d1b Mon Sep 17 00:00:00 2001
From: Andrey <amborodin@acm.org>
Date: Thu, 20 Jun 2019 14:40:52 +0500
Subject: [PATCH] Log corruption error codes

In some cases we have elog(ERROR) while corruption is certain and
we can give clear error code ERRCODE_DATA_CORRUPTED or
ERRCODE_INDEX_CORRUPTED.
---
 src/backend/access/heap/tuptoaster.c  | 25 +++++++++++++++----------
 src/backend/access/nbtree/nbtinsert.c |  5 +++--
 src/backend/access/nbtree/nbtpage.c   |  5 +++--
 src/backend/access/nbtree/nbtsearch.c |  5 +++--
 4 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/src/backend/access/heap/tuptoaster.c b/src/backend/access/heap/tuptoaster.c
index 55d6e91d1c..539842d46e 100644
--- a/src/backend/access/heap/tuptoaster.c
+++ b/src/backend/access/heap/tuptoaster.c
@@ -1966,35 +1966,39 @@ toast_fetch_datum(struct varlena *attr)
 		 * Some checks on the data we've found
 		 */
 		if (residx != nextidx)
-			elog(ERROR, "unexpected chunk number %d (expected %d) for toast value %u in %s",
+			ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED),
+				 errmsg("unexpected chunk number %d (expected %d) for toast value %u in %s",
 				 residx, nextidx,
 				 toast_pointer.va_valueid,
-				 RelationGetRelationName(toastrel));
+				 RelationGetRelationName(toastrel))));
 		if (residx < numchunks - 1)
 		{
 			if (chunksize != TOAST_MAX_CHUNK_SIZE)
-				elog(ERROR, "unexpected chunk size %d (expected %d) in chunk %d of %d for toast value %u in %s",
+				ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("unexpected chunk size %d (expected %d) in chunk %d of %d for toast value %u in %s",
 					 chunksize, (int) TOAST_MAX_CHUNK_SIZE,
 					 residx, numchunks,
 					 toast_pointer.va_valueid,
-					 RelationGetRelationName(toastrel));
+					 RelationGetRelationName(toastrel))));
 		}
 		else if (residx == numchunks - 1)
 		{
 			if ((residx * TOAST_MAX_CHUNK_SIZE + chunksize) != ressize)
-				elog(ERROR, "unexpected chunk size %d (expected %d) in final chunk %d for toast value %u in %s",
+				ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("unexpected chunk size %d (expected %d) in final chunk %d for toast value %u in %s",
 					 chunksize,
 					 (int) (ressize - residx * TOAST_MAX_CHUNK_SIZE),
 					 residx,
 					 toast_pointer.va_valueid,
-					 RelationGetRelationName(toastrel));
+					 RelationGetRelationName(toastrel))));
 		}
 		else
-			elog(ERROR, "unexpected chunk number %d (out of range %d..%d) for toast value %u in %s",
+			ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED),
+				 errmsg("unexpected chunk number %d (out of range %d..%d) for toast value %u in %s",
 				 residx,
 				 0, numchunks - 1,
 				 toast_pointer.va_valueid,
-				 RelationGetRelationName(toastrel));
+				 RelationGetRelationName(toastrel))));
 
 		/*
 		 * Copy the data into proper place in our result
@@ -2010,10 +2014,11 @@ toast_fetch_datum(struct varlena *attr)
 	 * Final checks that we successfully fetched the datum
 	 */
 	if (nextidx != numchunks)
-		elog(ERROR, "missing chunk number %d for toast value %u in %s",
+		ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED),
+			 errmsg("missing chunk number %d for toast value %u in %s",
 			 nextidx,
 			 toast_pointer.va_valueid,
-			 RelationGetRelationName(toastrel));
+			 RelationGetRelationName(toastrel))));
 
 	/*
 	 * End scan and close relations
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 602f8849d4..af56f747df 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -1568,10 +1568,11 @@ _bt_split(Relation rel, BTScanInsert itup_key, Buffer buf, Buffer cbuf,
 		if (sopaque->btpo_prev != origpagenumber)
 		{
 			memset(rightpage, 0, BufferGetPageSize(rbuf));
-			elog(ERROR, "right sibling's left-link doesn't match: "
+			ereport(ERROR, (errcode(ERRCODE_INDEX_CORRUPTED),
+				 errmsg("right sibling's left-link doesn't match: "
 				 "block %u links to %u instead of expected %u in index \"%s\"",
 				 oopaque->btpo_next, sopaque->btpo_prev, origpagenumber,
-				 RelationGetRelationName(rel));
+				 RelationGetRelationName(rel))));
 		}
 
 		/*
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index de4d4efc46..f050b4a343 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -1952,10 +1952,11 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, bool *rightsib_empty)
 	page = BufferGetPage(rbuf);
 	opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 	if (opaque->btpo_prev != target)
-		elog(ERROR, "right sibling's left-link doesn't match: "
+		ereport(ERROR, (errcode(ERRCODE_INDEX_CORRUPTED),
+			 errmsg("right sibling's left-link doesn't match: "
 			 "block %u links to %u instead of expected %u in index \"%s\"",
 			 rightsib, opaque->btpo_prev, target,
-			 RelationGetRelationName(rel));
+			 RelationGetRelationName(rel))));
 	rightsib_is_rightmost = P_RIGHTMOST(opaque);
 	*rightsib_empty = (P_FIRSTDATAKEY(opaque) > PageGetMaxOffsetNumber(page));
 
diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c
index 1f809c24a1..0e148a31c5 100644
--- a/src/backend/access/nbtree/nbtsearch.c
+++ b/src/backend/access/nbtree/nbtsearch.c
@@ -2113,8 +2113,9 @@ _bt_get_endpoint(Relation rel, uint32 level, bool rightmost,
 		if (opaque->btpo.level == level)
 			break;
 		if (opaque->btpo.level < level)
-			elog(ERROR, "btree level %u not found in index \"%s\"",
-				 level, RelationGetRelationName(rel));
+			ereport(ERROR, (errcode(ERRCODE_INDEX_CORRUPTED),
+			 errmsg("btree level %u not found in index \"%s\"",
+				 level, RelationGetRelationName(rel))));
 
 		/* Descend to leftmost or rightmost child page */
 		if (rightmost)
-- 
2.20.1

