From 489087cc776f7166995b98245830af06aeeb9485 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Mon, 15 Mar 2021 12:22:22 -0700
Subject: [PATCH v6 2/2] Fixing a confusing amcheck corruption message.

The verify_heapam() function from contrib/amcheck reports a
corruption message when toasted values have the wrong number of
chunks in the toast table, but this was being reported even when the
toasted value is entirely missing from the toast table, which is
confusing.  Fixing it so it only prints a message about the toasted
value being missing from the table in such cases.
---
 contrib/amcheck/verify_heapam.c           | 8 ++++----
 src/bin/pg_amcheck/t/004_verify_heapam.pl | 3 +--
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c
index 49f5ca0ef2..e614c12a14 100644
--- a/contrib/amcheck/verify_heapam.c
+++ b/contrib/amcheck/verify_heapam.c
@@ -1100,14 +1100,14 @@ check_tuple_attribute(HeapCheckContext *ctx)
 		check_toast_tuple(toasttup, ctx);
 		ctx->chunkno++;
 	}
-	if (ctx->chunkno != (ctx->endchunk + 1))
-		report_corruption(ctx,
-						  psprintf("final toast chunk number %u differs from expected value %u",
-								   ctx->chunkno, (ctx->endchunk + 1)));
 	if (!found_toasttup)
 		report_corruption(ctx,
 						  psprintf("toasted value for attribute %u missing from toast table",
 								   ctx->attnum));
+	else if (ctx->chunkno != (ctx->endchunk + 1))
+		report_corruption(ctx,
+						  psprintf("final toast chunk number %u differs from expected value %u",
+								   ctx->chunkno, (ctx->endchunk + 1)));
 	systable_endscan_ordered(toastscan);
 
 	return true;
diff --git a/src/bin/pg_amcheck/t/004_verify_heapam.pl b/src/bin/pg_amcheck/t/004_verify_heapam.pl
index 9f7114cd62..16574cb1f8 100644
--- a/src/bin/pg_amcheck/t/004_verify_heapam.pl
+++ b/src/bin/pg_amcheck/t/004_verify_heapam.pl
@@ -296,7 +296,7 @@ close($file)
 $node->start;
 
 # Ok, Xids and page layout look ok.  We can run corruption tests.
-plan tests => 20;
+plan tests => 19;
 
 # Check that pg_amcheck runs against the uncorrupted table without error.
 $node->command_ok(['pg_amcheck', '-p', $port, 'postgres'],
@@ -489,7 +489,6 @@ for (my $tupidx = 0; $tupidx < ROWCOUNT; $tupidx++)
 
 		$header = header(0, $offnum, 2);
 		push @expected,
-			qr/${header}final toast chunk number 0 differs from expected value \d+/,
 			qr/${header}toasted value for attribute 2 missing from toast table/;
 	}
 	elsif ($offnum == 14)
-- 
2.21.1 (Apple Git-122.3)

