Commit 47ac2033 authored by Michael Paquier's avatar Michael Paquier

Simplify some ERROR paths clearing wait events and transient files

Transient files and wait events get normally cleaned up when seeing an
exception (be it in the context of a transaction for a backend or
another process like the checkpointer), hence there is little point in
complicating error code paths to do this work.  This shaves a bit of
code, and removes some extra handling with errno which needed to be
preserved during the cleanup steps done.

Reported-by: Masahiko Sawada
Author: Michael Paquier
Reviewed-by: Tom Lane, Masahiko Sawada
Discussion: https://postgr.es/m/CAD21AoDhHYVq5KkXfkaHhmjA-zJYj-e4teiRAJefvXuKJz1tKQ@mail.gmail.com
parent a6dcf9df
...@@ -1693,26 +1693,18 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len) ...@@ -1693,26 +1693,18 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
pgstat_report_wait_start(WAIT_EVENT_TWOPHASE_FILE_WRITE); pgstat_report_wait_start(WAIT_EVENT_TWOPHASE_FILE_WRITE);
if (write(fd, content, len) != len) if (write(fd, content, len) != len)
{ {
int save_errno = errno;
pgstat_report_wait_end();
CloseTransientFile(fd);
/* if write didn't set errno, assume problem is no disk space */ /* if write didn't set errno, assume problem is no disk space */
errno = save_errno ? save_errno : ENOSPC; if (errno == 0)
errno = ENOSPC;
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not write file \"%s\": %m", path))); errmsg("could not write file \"%s\": %m", path)));
} }
if (write(fd, &statefile_crc, sizeof(pg_crc32c)) != sizeof(pg_crc32c)) if (write(fd, &statefile_crc, sizeof(pg_crc32c)) != sizeof(pg_crc32c))
{ {
int save_errno = errno;
pgstat_report_wait_end();
CloseTransientFile(fd);
/* if write didn't set errno, assume problem is no disk space */ /* if write didn't set errno, assume problem is no disk space */
errno = save_errno ? save_errno : ENOSPC; if (errno == 0)
errno = ENOSPC;
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not write file \"%s\": %m", path))); errmsg("could not write file \"%s\": %m", path)));
...@@ -1725,15 +1717,9 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len) ...@@ -1725,15 +1717,9 @@ RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
*/ */
pgstat_report_wait_start(WAIT_EVENT_TWOPHASE_FILE_SYNC); pgstat_report_wait_start(WAIT_EVENT_TWOPHASE_FILE_SYNC);
if (pg_fsync(fd) != 0) if (pg_fsync(fd) != 0)
{
int save_errno = errno;
CloseTransientFile(fd);
errno = save_errno;
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m", path))); errmsg("could not fsync file \"%s\": %m", path)));
}
pgstat_report_wait_end(); pgstat_report_wait_end();
if (CloseTransientFile(fd) != 0) if (CloseTransientFile(fd) != 0)
......
...@@ -579,12 +579,9 @@ CheckPointReplicationOrigin(void) ...@@ -579,12 +579,9 @@ CheckPointReplicationOrigin(void)
errno = 0; errno = 0;
if ((write(tmpfd, &magic, sizeof(magic))) != sizeof(magic)) if ((write(tmpfd, &magic, sizeof(magic))) != sizeof(magic))
{ {
int save_errno = errno;
CloseTransientFile(tmpfd);
/* if write didn't set errno, assume problem is no disk space */ /* if write didn't set errno, assume problem is no disk space */
errno = save_errno ? save_errno : ENOSPC; if (errno == 0)
errno = ENOSPC;
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not write to file \"%s\": %m", errmsg("could not write to file \"%s\": %m",
...@@ -624,12 +621,9 @@ CheckPointReplicationOrigin(void) ...@@ -624,12 +621,9 @@ CheckPointReplicationOrigin(void)
if ((write(tmpfd, &disk_state, sizeof(disk_state))) != if ((write(tmpfd, &disk_state, sizeof(disk_state))) !=
sizeof(disk_state)) sizeof(disk_state))
{ {
int save_errno = errno;
CloseTransientFile(tmpfd);
/* if write didn't set errno, assume problem is no disk space */ /* if write didn't set errno, assume problem is no disk space */
errno = save_errno ? save_errno : ENOSPC; if (errno == 0)
errno = ENOSPC;
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not write to file \"%s\": %m", errmsg("could not write to file \"%s\": %m",
...@@ -646,12 +640,9 @@ CheckPointReplicationOrigin(void) ...@@ -646,12 +640,9 @@ CheckPointReplicationOrigin(void)
errno = 0; errno = 0;
if ((write(tmpfd, &crc, sizeof(crc))) != sizeof(crc)) if ((write(tmpfd, &crc, sizeof(crc))) != sizeof(crc))
{ {
int save_errno = errno;
CloseTransientFile(tmpfd);
/* if write didn't set errno, assume problem is no disk space */ /* if write didn't set errno, assume problem is no disk space */
errno = save_errno ? save_errno : ENOSPC; if (errno == 0)
errno = ENOSPC;
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not write to file \"%s\": %m", errmsg("could not write to file \"%s\": %m",
......
...@@ -1398,16 +1398,10 @@ RestoreSlotFromDisk(const char *name) ...@@ -1398,16 +1398,10 @@ RestoreSlotFromDisk(const char *name)
*/ */
pgstat_report_wait_start(WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC); pgstat_report_wait_start(WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC);
if (pg_fsync(fd) != 0) if (pg_fsync(fd) != 0)
{
int save_errno = errno;
CloseTransientFile(fd);
errno = save_errno;
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m", errmsg("could not fsync file \"%s\": %m",
path))); path)));
}
pgstat_report_wait_end(); pgstat_report_wait_end();
/* Also sync the parent directory */ /* Also sync the parent directory */
...@@ -1421,10 +1415,6 @@ RestoreSlotFromDisk(const char *name) ...@@ -1421,10 +1415,6 @@ RestoreSlotFromDisk(const char *name)
pgstat_report_wait_end(); pgstat_report_wait_end();
if (readBytes != ReplicationSlotOnDiskConstantSize) if (readBytes != ReplicationSlotOnDiskConstantSize)
{ {
int saved_errno = errno;
CloseTransientFile(fd);
errno = saved_errno;
if (readBytes < 0) if (readBytes < 0)
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
...@@ -1466,10 +1456,6 @@ RestoreSlotFromDisk(const char *name) ...@@ -1466,10 +1456,6 @@ RestoreSlotFromDisk(const char *name)
pgstat_report_wait_end(); pgstat_report_wait_end();
if (readBytes != cp.length) if (readBytes != cp.length)
{ {
int saved_errno = errno;
CloseTransientFile(fd);
errno = saved_errno;
if (readBytes < 0) if (readBytes < 0)
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
......
...@@ -199,7 +199,6 @@ copy_file(char *fromfile, char *tofile) ...@@ -199,7 +199,6 @@ copy_file(char *fromfile, char *tofile)
pgstat_report_wait_start(WAIT_EVENT_COPY_FILE_WRITE); pgstat_report_wait_start(WAIT_EVENT_COPY_FILE_WRITE);
if ((int) write(dstfd, buffer, nbytes) != nbytes) if ((int) write(dstfd, buffer, nbytes) != nbytes)
{ {
pgstat_report_wait_end();
/* if write didn't set errno, assume problem is no disk space */ /* if write didn't set errno, assume problem is no disk space */
if (errno == 0) if (errno == 0)
errno = ENOSPC; errno = ENOSPC;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment