Commit 9989d37d authored by Michael Paquier's avatar Michael Paquier

Remove XLogFileNameP() from the tree

XLogFileNameP() is a wrapper routine able to build a palloc'd string for
a WAL segment name, which is used for error string generation.  There
were several code paths where it gets called in a critical section,
where memory allocation is not allowed.  This results in triggering
an assertion failure instead of generating the wanted error message.

Another, more annoying, problem is that if the allocation to generate
the WAL segment name fails on OOM, then the failure would be escalated
to a PANIC.

This removes the routine and all its callers are replaced with a logic
using a fixed-size buffer.  This way, all the existing mistakes are
fixed and future ones are prevented.

Author: Masahiko Sawada
Reviewed-by: Michael Paquier, Álvaro Herrera
Discussion: https://postgr.es/m/CA+fd4k5gC9H4uoWMLg9K_QfNrnkkdEw+-AFveob9YX7z8JnKTA@mail.gmail.com
parent e5532f19
...@@ -2499,14 +2499,21 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible) ...@@ -2499,14 +2499,21 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
pgstat_report_wait_end(); pgstat_report_wait_end();
if (written <= 0) if (written <= 0)
{ {
char xlogfname[MAXFNAMELEN];
int save_errno;
if (errno == EINTR) if (errno == EINTR)
continue; continue;
save_errno = errno;
XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo,
wal_segment_size);
errno = save_errno;
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not write to log file %s " errmsg("could not write to log file %s "
"at offset %u, length %zu: %m", "at offset %u, length %zu: %m",
XLogFileNameP(ThisTimeLineID, openLogSegNo), xlogfname, startoffset, nleft)));
startoffset, nleft)));
} }
nleft -= written; nleft -= written;
from += written; from += written;
...@@ -3792,10 +3799,17 @@ XLogFileClose(void) ...@@ -3792,10 +3799,17 @@ XLogFileClose(void)
#endif #endif
if (close(openLogFile) != 0) if (close(openLogFile) != 0)
{
char xlogfname[MAXFNAMELEN];
int save_errno = errno;
XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo, wal_segment_size);
errno = save_errno;
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not close file \"%s\": %m", errmsg("could not close file \"%s\": %m", xlogfname)));
XLogFileNameP(ThisTimeLineID, openLogSegNo)))); }
openLogFile = -1; openLogFile = -1;
} }
...@@ -5510,10 +5524,17 @@ exitArchiveRecovery(TimeLineID endTLI, XLogRecPtr endOfLog) ...@@ -5510,10 +5524,17 @@ exitArchiveRecovery(TimeLineID endTLI, XLogRecPtr endOfLog)
fd = XLogFileInit(startLogSegNo, &use_existent, true); fd = XLogFileInit(startLogSegNo, &use_existent, true);
if (close(fd) != 0) if (close(fd) != 0)
{
char xlogfname[MAXFNAMELEN];
int save_errno = errno;
XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo,
wal_segment_size);
errno = save_errno;
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not close file \"%s\": %m", errmsg("could not close file \"%s\": %m", xlogfname)));
XLogFileNameP(ThisTimeLineID, startLogSegNo)))); }
} }
/* /*
...@@ -10079,10 +10100,19 @@ assign_xlog_sync_method(int new_sync_method, void *extra) ...@@ -10079,10 +10100,19 @@ assign_xlog_sync_method(int new_sync_method, void *extra)
{ {
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN);
if (pg_fsync(openLogFile) != 0) if (pg_fsync(openLogFile) != 0)
{
char xlogfname[MAXFNAMELEN];
int save_errno;
save_errno = errno;
XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo,
wal_segment_size);
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", xlogfname)));
XLogFileNameP(ThisTimeLineID, openLogSegNo)))); }
pgstat_report_wait_end(); pgstat_report_wait_end();
if (get_sync_bit(sync_method) != get_sync_bit(new_sync_method)) if (get_sync_bit(sync_method) != get_sync_bit(new_sync_method))
XLogFileClose(); XLogFileClose();
...@@ -10100,32 +10130,25 @@ assign_xlog_sync_method(int new_sync_method, void *extra) ...@@ -10100,32 +10130,25 @@ assign_xlog_sync_method(int new_sync_method, void *extra)
void void
issue_xlog_fsync(int fd, XLogSegNo segno) issue_xlog_fsync(int fd, XLogSegNo segno)
{ {
char *msg = NULL;
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC); pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
switch (sync_method) switch (sync_method)
{ {
case SYNC_METHOD_FSYNC: case SYNC_METHOD_FSYNC:
if (pg_fsync_no_writethrough(fd) != 0) if (pg_fsync_no_writethrough(fd) != 0)
ereport(PANIC, msg = _("could not fsync file \"%s\": %m");
(errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m",
XLogFileNameP(ThisTimeLineID, segno))));
break; break;
#ifdef HAVE_FSYNC_WRITETHROUGH #ifdef HAVE_FSYNC_WRITETHROUGH
case SYNC_METHOD_FSYNC_WRITETHROUGH: case SYNC_METHOD_FSYNC_WRITETHROUGH:
if (pg_fsync_writethrough(fd) != 0) if (pg_fsync_writethrough(fd) != 0)
ereport(PANIC, msg = _("could not fsync write-through file \"%s\": %m");
(errcode_for_file_access(),
errmsg("could not fsync write-through file \"%s\": %m",
XLogFileNameP(ThisTimeLineID, segno))));
break; break;
#endif #endif
#ifdef HAVE_FDATASYNC #ifdef HAVE_FDATASYNC
case SYNC_METHOD_FDATASYNC: case SYNC_METHOD_FDATASYNC:
if (pg_fdatasync(fd) != 0) if (pg_fdatasync(fd) != 0)
ereport(PANIC, msg = _("could not fdatasync file \"%s\": %m");
(errcode_for_file_access(),
errmsg("could not fdatasync file \"%s\": %m",
XLogFileNameP(ThisTimeLineID, segno))));
break; break;
#endif #endif
case SYNC_METHOD_OPEN: case SYNC_METHOD_OPEN:
...@@ -10136,19 +10159,22 @@ issue_xlog_fsync(int fd, XLogSegNo segno) ...@@ -10136,19 +10159,22 @@ issue_xlog_fsync(int fd, XLogSegNo segno)
elog(PANIC, "unrecognized wal_sync_method: %d", sync_method); elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
break; break;
} }
pgstat_report_wait_end();
}
/* /* PANIC if failed to fsync */
* Return the filename of given log segment, as a palloc'd string. if (msg)
*/ {
char * char xlogfname[MAXFNAMELEN];
XLogFileNameP(TimeLineID tli, XLogSegNo segno) int save_errno = errno;
{
char *result = palloc(MAXFNAMELEN);
XLogFileName(result, tli, segno, wal_segment_size); XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo,
return result; wal_segment_size);
errno = save_errno;
ereport(PANIC,
(errcode_for_file_access(),
errmsg(msg, xlogfname)));
}
pgstat_report_wait_end();
} }
/* /*
......
...@@ -776,7 +776,7 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa ...@@ -776,7 +776,7 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa
/* openSegment callback for WALRead */ /* openSegment callback for WALRead */
static int static int
wal_segment_open(XLogSegNo nextSegNo, WALSegmentContext *segcxt, wal_segment_open(XLogSegNo nextSegNo, WALSegmentContext * segcxt,
TimeLineID *tli_p) TimeLineID *tli_p)
{ {
TimeLineID tli = *tli_p; TimeLineID tli = *tli_p;
...@@ -944,7 +944,9 @@ void ...@@ -944,7 +944,9 @@ void
WALReadRaiseError(WALReadError *errinfo) WALReadRaiseError(WALReadError *errinfo)
{ {
WALOpenSegment *seg = &errinfo->wre_seg; WALOpenSegment *seg = &errinfo->wre_seg;
char *fname = XLogFileNameP(seg->ws_tli, seg->ws_segno); char fname[MAXFNAMELEN];
XLogFileName(fname, seg->ws_tli, seg->ws_segno, wal_segment_size);
if (errinfo->wre_read < 0) if (errinfo->wre_read < 0)
{ {
......
...@@ -576,17 +576,17 @@ WalReceiverMain(void) ...@@ -576,17 +576,17 @@ WalReceiverMain(void)
char xlogfname[MAXFNAMELEN]; char xlogfname[MAXFNAMELEN];
XLogWalRcvFlush(false); XLogWalRcvFlush(false);
XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
if (close(recvFile) != 0) if (close(recvFile) != 0)
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not close log segment %s: %m", errmsg("could not close log segment %s: %m",
XLogFileNameP(recvFileTLI, recvSegNo)))); xlogfname)));
/* /*
* Create .done file forcibly to prevent the streamed segment from * Create .done file forcibly to prevent the streamed segment from
* being archived later. * being archived later.
*/ */
XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS) if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS)
XLogArchiveForceDone(xlogfname); XLogArchiveForceDone(xlogfname);
else else
...@@ -900,6 +900,8 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr) ...@@ -900,6 +900,8 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
XLogWalRcvFlush(false); XLogWalRcvFlush(false);
XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
/* /*
* XLOG segment files will be re-read by recovery in startup * XLOG segment files will be re-read by recovery in startup
* process soon, so we don't advise the OS to release cache * process soon, so we don't advise the OS to release cache
...@@ -909,13 +911,12 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr) ...@@ -909,13 +911,12 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not close log segment %s: %m", errmsg("could not close log segment %s: %m",
XLogFileNameP(recvFileTLI, recvSegNo)))); xlogfname)));
/* /*
* Create .done file forcibly to prevent the streamed segment * Create .done file forcibly to prevent the streamed segment
* from being archived later. * from being archived later.
*/ */
XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS) if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS)
XLogArchiveForceDone(xlogfname); XLogArchiveForceDone(xlogfname);
else else
...@@ -943,11 +944,18 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr) ...@@ -943,11 +944,18 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
if (recvOff != startoff) if (recvOff != startoff)
{ {
if (lseek(recvFile, (off_t) startoff, SEEK_SET) < 0) if (lseek(recvFile, (off_t) startoff, SEEK_SET) < 0)
{
char xlogfname[MAXFNAMELEN];
int save_errno = errno;
XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
errno = save_errno;
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not seek in log segment %s to offset %u: %m", errmsg("could not seek in log segment %s to offset %u: %m",
XLogFileNameP(recvFileTLI, recvSegNo), xlogfname, startoff)));
startoff))); }
recvOff = startoff; recvOff = startoff;
} }
...@@ -957,15 +965,21 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr) ...@@ -957,15 +965,21 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
byteswritten = write(recvFile, buf, segbytes); byteswritten = write(recvFile, buf, segbytes);
if (byteswritten <= 0) if (byteswritten <= 0)
{ {
char xlogfname[MAXFNAMELEN];
int save_errno;
/* if write didn't set errno, assume no disk space */ /* if write didn't set errno, assume no disk space */
if (errno == 0) if (errno == 0)
errno = ENOSPC; errno = ENOSPC;
save_errno = errno;
XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
errno = save_errno;
ereport(PANIC, ereport(PANIC,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("could not write to log segment %s " errmsg("could not write to log segment %s "
"at offset %u, length %lu: %m", "at offset %u, length %lu: %m",
XLogFileNameP(recvFileTLI, recvSegNo), xlogfname, recvOff, (unsigned long) segbytes)));
recvOff, (unsigned long) segbytes)));
} }
/* Update state for write */ /* Update state for write */
......
...@@ -2434,10 +2434,17 @@ WalSndSegmentOpen(XLogSegNo nextSegNo, WALSegmentContext *segcxt, ...@@ -2434,10 +2434,17 @@ WalSndSegmentOpen(XLogSegNo nextSegNo, WALSegmentContext *segcxt,
* too old WAL segment that has already been removed or recycled. * too old WAL segment that has already been removed or recycled.
*/ */
if (errno == ENOENT) if (errno == ENOENT)
{
char xlogfname[MAXFNAMELEN];
int save_errno = errno;
XLogFileName(xlogfname, *tli_p, nextSegNo, wal_segment_size);
errno = save_errno;
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
errmsg("requested WAL segment %s has already been removed", errmsg("requested WAL segment %s has already been removed",
XLogFileNameP(*tli_p, nextSegNo)))); xlogfname)));
}
else else
ereport(ERROR, ereport(ERROR,
(errcode_for_file_access(), (errcode_for_file_access(),
......
...@@ -288,7 +288,6 @@ extern bool RecoveryIsPaused(void); ...@@ -288,7 +288,6 @@ extern bool RecoveryIsPaused(void);
extern void SetRecoveryPause(bool recoveryPause); extern void SetRecoveryPause(bool recoveryPause);
extern TimestampTz GetLatestXTime(void); extern TimestampTz GetLatestXTime(void);
extern TimestampTz GetCurrentChunkReplayStartTime(void); extern TimestampTz GetCurrentChunkReplayStartTime(void);
extern char *XLogFileNameP(TimeLineID tli, XLogSegNo segno);
extern void UpdateControlFile(void); extern void UpdateControlFile(void);
extern uint64 GetSystemIdentifier(void); extern uint64 GetSystemIdentifier(void);
......
...@@ -152,6 +152,10 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader; ...@@ -152,6 +152,10 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
/* Length of XLog file name */ /* Length of XLog file name */
#define XLOG_FNAME_LEN 24 #define XLOG_FNAME_LEN 24
/*
* Generate a WAL segment file name. Do not use this macro in a helper
* function allocating the result generated.
*/
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes) \ #define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes) \
snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, \ snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, \
(uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \ (uint32) ((logSegNo) / XLogSegmentsPerXLogId(wal_segsz_bytes)), \
......
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