]> granicus.if.org Git - postgresql/commitdiff
Add proper errcodes to new error messages for read() failures
authorMichael Paquier <michael@paquier.xyz>
Mon, 23 Jul 2018 00:37:36 +0000 (09:37 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 23 Jul 2018 00:37:36 +0000 (09:37 +0900)
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.

While on the way, fix one errcode_for_file_access missing in origin.c
since the code has been created, and remove one assignment of errno to 0
before calling read(), as this was around to fit with what was present
before 811b6e36 where errno would not be set when not enough bytes are
read.  I have noticed the first one, and Tom has pinged me about the
second one.

Author: Michael Paquier
Reported-by: Tom Lane
Discussion: https://postgr.es/m/27265.1531925836@sss.pgh.pa.us

src/backend/access/transam/xlog.c
src/backend/replication/logical/origin.c
src/backend/replication/logical/snapbuild.c
src/backend/replication/slot.c
src/backend/replication/walsender.c
src/backend/utils/cache/relmapper.c
src/common/controldata_utils.c

index 9f8ae9dc756a2a24a180cb7f2c4a2ae248187e4d..335b4a573d8b52d0b5635567bda39534447301f4 100644 (file)
@@ -3412,7 +3412,6 @@ XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno,
 
                        if (nread > sizeof(buffer))
                                nread = sizeof(buffer);
-                       errno = 0;
                        pgstat_report_wait_start(WAIT_EVENT_WAL_COPY_READ);
                        r = read(srcfd, buffer, nread);
                        if (r != nread)
@@ -3424,7 +3423,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 +4564,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 +11830,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;
        }
index 2d05d04b872237b18a1f8b32f223639dd531ebc8..822c96d1c2f82cd8c2548d8aa35175d5312122f6 100644 (file)
@@ -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));
index 7bd969b0a1cf418b7afaca41eddffd69d899dcc3..1359d9b20a3578f4d59374ee68acc8d44e216925 100644 (file)
@@ -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);
index 271af08572add1bc0bb1985995c6d6e0130a877c..6c3639805833992e76d43c3aff50a4bf33780d88 100644 (file)
@@ -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)));
        }
 
index e8f4f37e5ce696bfa00af5c04528663ef830980d..d60026dfd1a0c3ccce5e5a190813274eb236024c 100644 (file)
@@ -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)));
                }
index 2d31f9f912a30c27a96a88e73a52e9705121e69c..c7f0e6f6d4af8b72a2c96bf3eb6418bf7ae15457 100644 (file)
@@ -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();
index 60197b244065aa17a24a836036e4c5aca7e2f432..e24af48f52eeac927cc59e254b4b67d6ba0c1123 100644 (file)
@@ -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
                {