Skip to content

Commit

Permalink
Use pg_pwrite_zeros() in walmethods.c
Browse files Browse the repository at this point in the history
This change impacts pg_receivewal and pg_basebackup, for the pre-padding
with zeros of all the new non-compressed WAL segments, so as the code is
more robust on partial writes.  This makes the code consistent with the
backend (XLogFileInitInternal) when wal_init_zeros is enabled for the
WAL segment initialization.

Author: Bharath Rupireddy
Reviewed-by: Nathan Bossart, Andres Freund, Thomas Munro, Michael
Paquier
Discussion: https://postgr.es/m/CALj2ACUq7nAb7=bJNbK3yYmp-SZhJcXFR_pLk8un6XgDzDF3OA@mail.gmail.com
  • Loading branch information
michaelpq committed Nov 8, 2022
1 parent 3bdbdf5 commit 28cc297
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/bin/pg_basebackup/walmethods.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,21 @@ dir_open_for_write(WalWriteMethod *wwmethod, const char *pathname,
/* Do pre-padding on non-compressed files */
if (pad_to_size && wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
{
PGAlignedXLogBlock zerobuf;
int bytes;
ssize_t rc;

memset(zerobuf.data, 0, XLOG_BLCKSZ);
for (bytes = 0; bytes < pad_to_size; bytes += XLOG_BLCKSZ)
rc = pg_pwrite_zeros(fd, pad_to_size);

if (rc < 0)
{
errno = 0;
if (write(fd, zerobuf.data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
{
/* If write didn't set errno, assume problem is no disk space */
wwmethod->lasterrno = errno ? errno : ENOSPC;
close(fd);
return NULL;
}
wwmethod->lasterrno = errno;
close(fd);
return NULL;
}

/*
* pg_pwrite() (called via pg_pwrite_zeros()) may have moved the file
* position, so reset it (see win32pwrite.c).
*/
if (lseek(fd, 0, SEEK_SET) != 0)
{
wwmethod->lasterrno = errno;
Expand Down

0 comments on commit 28cc297

Please sign in to comment.