From d01e42456778bb0247bc7471040b0d1cd5437ee5 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Thu, 19 Jul 2018 12:08:30 +0900
Subject: [PATCH 2/2] Add proper errcodes to new error messages for read()
 failures

Those would use the default ERRCODE_INTERNAL_ERROR, but for foreseeable
failures an errcode ought to be set, ERRCODE_DATA_CORRUPTED making the
most sense here.

Per complain from Tom Lane.
---
 src/backend/access/transam/xlog.c           |  9 ++++++---
 src/backend/replication/logical/origin.c    |  6 ++++--
 src/backend/replication/logical/snapbuild.c | 12 ++++++++----
 src/backend/replication/slot.c              |  6 ++++--
 src/backend/replication/walsender.c         |  6 ++++--
 src/backend/utils/cache/relmapper.c         |  3 ++-
 src/common/controldata_utils.c              |  3 ++-
 7 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 9f8ae9dc75..db34349a24 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -3424,7 +3424,8 @@ XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno,
 									path)));
 				else
 					ereport(ERROR,
-							(errmsg("could not read file \"%s\": read %d of %zu",
+							(errcode(ERRCODE_DATA_CORRUPTED),
+							 errmsg("could not read file \"%s\": read %d of %zu",
 									path, r, (Size) nread)));
 			}
 			pgstat_report_wait_end();
@@ -4564,7 +4565,8 @@ ReadControlFile(void)
 							XLOG_CONTROL_FILE)));
 		else
 			ereport(PANIC,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							XLOG_CONTROL_FILE, r, sizeof(ControlFileData))));
 	}
 	pgstat_report_wait_end();
@@ -11829,7 +11831,8 @@ retry:
 		}
 		else
 			ereport(emode_for_corrupt_record(emode, targetPagePtr + reqLen),
-					(errmsg("could not read from log segment %s, offset %u: read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read from log segment %s, offset %u: read %d of %zu",
 							fname, readOff, r, (Size) XLOG_BLCKSZ)));
 		goto next_record_is_invalid;
 	}
diff --git a/src/backend/replication/logical/origin.c b/src/backend/replication/logical/origin.c
index 2d05d04b87..822c96d1c2 100644
--- a/src/backend/replication/logical/origin.c
+++ b/src/backend/replication/logical/origin.c
@@ -715,11 +715,13 @@ StartupReplicationOrigin(void)
 	{
 		if (readBytes < 0)
 			ereport(PANIC,
-					(errmsg("could not read file \"%s\": %m",
+					(errcode_for_file_access(),
+					 errmsg("could not read file \"%s\": %m",
 							path)));
 		else
 			ereport(PANIC,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							path, readBytes, sizeof(magic))));
 	}
 	COMP_CRC32C(crc, &magic, sizeof(magic));
diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index 7bd969b0a1..1359d9b20a 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -1736,7 +1736,8 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
 		}
 		else
 			ereport(ERROR,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							path, readBytes,
 							(Size) SnapBuildOnDiskConstantSize)));
 	}
@@ -1775,7 +1776,8 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
 		}
 		else
 			ereport(ERROR,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							path, readBytes, sizeof(SnapBuild))));
 	}
 	COMP_CRC32C(checksum, &ondisk.builder, sizeof(SnapBuild));
@@ -1802,7 +1804,8 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
 		}
 		else
 			ereport(ERROR,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							path, readBytes, sz)));
 	}
 	COMP_CRC32C(checksum, ondisk.builder.was_running.was_xip, sz);
@@ -1828,7 +1831,8 @@ SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
 		}
 		else
 			ereport(ERROR,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							path, readBytes, sz)));
 	}
 	COMP_CRC32C(checksum, ondisk.builder.committed.xip, sz);
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index 271af08572..6c36398058 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -1420,7 +1420,8 @@ RestoreSlotFromDisk(const char *name)
 					 errmsg("could not read file \"%s\": %m", path)));
 		else
 			ereport(PANIC,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							path, readBytes,
 							(Size) ReplicationSlotOnDiskConstantSize)));
 	}
@@ -1464,7 +1465,8 @@ RestoreSlotFromDisk(const char *name)
 					 errmsg("could not read file \"%s\": %m", path)));
 		else
 			ereport(PANIC,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							path, readBytes, (Size) cp.length)));
 	}
 
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index e8f4f37e5c..d60026dfd1 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -509,7 +509,8 @@ SendTimeLineHistory(TimeLineHistoryCmd *cmd)
 							path)));
 		else if (nread == 0)
 			ereport(ERROR,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							path, nread, (Size) bytesleft)));
 
 		pq_sendbytes(&buf, rbuf, nread);
@@ -2442,7 +2443,8 @@ retry:
 		else if (readbytes == 0)
 		{
 			ereport(ERROR,
-					(errmsg("could not read from log segment %s, offset %u: read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read from log segment %s, offset %u: read %d of %zu",
 							XLogFileNameP(curFileTimeLine, sendSegNo),
 							sendOff, readbytes, (Size) segbytes)));
 		}
diff --git a/src/backend/utils/cache/relmapper.c b/src/backend/utils/cache/relmapper.c
index 2d31f9f912..c7f0e6f6d4 100644
--- a/src/backend/utils/cache/relmapper.c
+++ b/src/backend/utils/cache/relmapper.c
@@ -669,7 +669,8 @@ load_relmap_file(bool shared)
 					 errmsg("could not read file \"%s\": %m", mapfilename)));
 		else
 			ereport(FATAL,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							mapfilename, r, sizeof(RelMapFile))));
 	}
 	pgstat_report_wait_end();
diff --git a/src/common/controldata_utils.c b/src/common/controldata_utils.c
index 60197b2440..e24af48f52 100644
--- a/src/common/controldata_utils.c
+++ b/src/common/controldata_utils.c
@@ -83,7 +83,8 @@ get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
 		else
 #ifndef FRONTEND
 			ereport(ERROR,
-					(errmsg("could not read file \"%s\": read %d of %zu",
+					(errcode(ERRCODE_DATA_CORRUPTED),
+					 errmsg("could not read file \"%s\": read %d of %zu",
 							ControlFilePath, r, sizeof(ControlFileData))));
 #else
 		{
-- 
2.18.0

