mirror of
https://github.com/postgres/postgres.git
synced 2025-06-29 10:41:53 +03:00
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
This commit is contained in:
@ -2499,14 +2499,21 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
|
||||
pgstat_report_wait_end();
|
||||
if (written <= 0)
|
||||
{
|
||||
char xlogfname[MAXFNAMELEN];
|
||||
int save_errno;
|
||||
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
|
||||
save_errno = errno;
|
||||
XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo,
|
||||
wal_segment_size);
|
||||
errno = save_errno;
|
||||
ereport(PANIC,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not write to log file %s "
|
||||
"at offset %u, length %zu: %m",
|
||||
XLogFileNameP(ThisTimeLineID, openLogSegNo),
|
||||
startoffset, nleft)));
|
||||
xlogfname, startoffset, nleft)));
|
||||
}
|
||||
nleft -= written;
|
||||
from += written;
|
||||
@ -3792,10 +3799,17 @@ XLogFileClose(void)
|
||||
#endif
|
||||
|
||||
if (close(openLogFile) != 0)
|
||||
{
|
||||
char xlogfname[MAXFNAMELEN];
|
||||
int save_errno = errno;
|
||||
|
||||
XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo, wal_segment_size);
|
||||
errno = save_errno;
|
||||
ereport(PANIC,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not close file \"%s\": %m",
|
||||
XLogFileNameP(ThisTimeLineID, openLogSegNo))));
|
||||
errmsg("could not close file \"%s\": %m", xlogfname)));
|
||||
}
|
||||
|
||||
openLogFile = -1;
|
||||
}
|
||||
|
||||
@ -5510,10 +5524,17 @@ exitArchiveRecovery(TimeLineID endTLI, XLogRecPtr endOfLog)
|
||||
fd = XLogFileInit(startLogSegNo, &use_existent, true);
|
||||
|
||||
if (close(fd) != 0)
|
||||
{
|
||||
char xlogfname[MAXFNAMELEN];
|
||||
int save_errno = errno;
|
||||
|
||||
XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo,
|
||||
wal_segment_size);
|
||||
errno = save_errno;
|
||||
ereport(ERROR,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not close file \"%s\": %m",
|
||||
XLogFileNameP(ThisTimeLineID, startLogSegNo))));
|
||||
errmsg("could not close file \"%s\": %m", xlogfname)));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -10079,10 +10100,19 @@ assign_xlog_sync_method(int new_sync_method, void *extra)
|
||||
{
|
||||
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN);
|
||||
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,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not fsync file \"%s\": %m",
|
||||
XLogFileNameP(ThisTimeLineID, openLogSegNo))));
|
||||
errmsg("could not fsync file \"%s\": %m", xlogfname)));
|
||||
}
|
||||
|
||||
pgstat_report_wait_end();
|
||||
if (get_sync_bit(sync_method) != get_sync_bit(new_sync_method))
|
||||
XLogFileClose();
|
||||
@ -10100,32 +10130,25 @@ assign_xlog_sync_method(int new_sync_method, void *extra)
|
||||
void
|
||||
issue_xlog_fsync(int fd, XLogSegNo segno)
|
||||
{
|
||||
char *msg = NULL;
|
||||
|
||||
pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
|
||||
switch (sync_method)
|
||||
{
|
||||
case SYNC_METHOD_FSYNC:
|
||||
if (pg_fsync_no_writethrough(fd) != 0)
|
||||
ereport(PANIC,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not fsync file \"%s\": %m",
|
||||
XLogFileNameP(ThisTimeLineID, segno))));
|
||||
msg = _("could not fsync file \"%s\": %m");
|
||||
break;
|
||||
#ifdef HAVE_FSYNC_WRITETHROUGH
|
||||
case SYNC_METHOD_FSYNC_WRITETHROUGH:
|
||||
if (pg_fsync_writethrough(fd) != 0)
|
||||
ereport(PANIC,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not fsync write-through file \"%s\": %m",
|
||||
XLogFileNameP(ThisTimeLineID, segno))));
|
||||
msg = _("could not fsync write-through file \"%s\": %m");
|
||||
break;
|
||||
#endif
|
||||
#ifdef HAVE_FDATASYNC
|
||||
case SYNC_METHOD_FDATASYNC:
|
||||
if (pg_fdatasync(fd) != 0)
|
||||
ereport(PANIC,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not fdatasync file \"%s\": %m",
|
||||
XLogFileNameP(ThisTimeLineID, segno))));
|
||||
msg = _("could not fdatasync file \"%s\": %m");
|
||||
break;
|
||||
#endif
|
||||
case SYNC_METHOD_OPEN:
|
||||
@ -10136,21 +10159,24 @@ issue_xlog_fsync(int fd, XLogSegNo segno)
|
||||
elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
|
||||
break;
|
||||
}
|
||||
|
||||
/* PANIC if failed to fsync */
|
||||
if (msg)
|
||||
{
|
||||
char xlogfname[MAXFNAMELEN];
|
||||
int save_errno = errno;
|
||||
|
||||
XLogFileName(xlogfname, ThisTimeLineID, openLogSegNo,
|
||||
wal_segment_size);
|
||||
errno = save_errno;
|
||||
ereport(PANIC,
|
||||
(errcode_for_file_access(),
|
||||
errmsg(msg, xlogfname)));
|
||||
}
|
||||
|
||||
pgstat_report_wait_end();
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the filename of given log segment, as a palloc'd string.
|
||||
*/
|
||||
char *
|
||||
XLogFileNameP(TimeLineID tli, XLogSegNo segno)
|
||||
{
|
||||
char *result = palloc(MAXFNAMELEN);
|
||||
|
||||
XLogFileName(result, tli, segno, wal_segment_size);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* do_pg_start_backup
|
||||
*
|
||||
|
Reference in New Issue
Block a user