diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index 3c732d8f8014a6c5f6d31933499adbd2711650bd..06a16d1fb7d73ddcb4f892452940627de7574ac0 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -1565,6 +1565,7 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
 						path)));
 
 	/* Write content and CRC */
+	errno = 0;
 	if (write(fd, content, len) != len)
 	{
 		int			save_errno = errno;
diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c
index 3e12d63530e288764743dbd53645c3cbb24e66a4..b2891431eae000330a70dd6af6a3af3130d655b2 100644
--- a/src/bin/pg_basebackup/receivelog.c
+++ b/src/bin/pg_basebackup/receivelog.c
@@ -138,6 +138,7 @@ open_walfile(XLogRecPtr startpoint, uint32 timeline, char *basedir,
 	zerobuf = pg_malloc0(XLOG_BLCKSZ);
 	for (bytes = 0; bytes < XLogSegSize; bytes += XLOG_BLCKSZ)
 	{
+		errno = 0;
 		if (write(f, zerobuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
 		{
 			/* if write didn't set errno, assume problem is no disk space */
@@ -1129,6 +1130,7 @@ HandleCopyStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
 					}
 				}
 
+				errno = 0;
 				if (write(walfile,
 						  copybuf + hdr_len + bytes_written,
 						  bytes_to_write) != bytes_to_write)