mirror of
https://github.com/postgres/postgres.git
synced 2025-06-27 23:21:58 +03:00
Avoid allocations in critical sections.
If a palloc in a critical section fails, it becomes a PANIC.
This commit is contained in:
@ -739,9 +739,8 @@ XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata)
|
||||
|
||||
if (rechdr == NULL)
|
||||
{
|
||||
rechdr = malloc(SizeOfXLogRecord);
|
||||
if (rechdr == NULL)
|
||||
elog(ERROR, "out of memory");
|
||||
static char rechdrbuf[SizeOfXLogRecord + MAXIMUM_ALIGNOF];
|
||||
rechdr = (XLogRecord *) MAXALIGN(&rechdrbuf);
|
||||
MemSet(rechdr, 0, SizeOfXLogRecord);
|
||||
}
|
||||
|
||||
@ -2248,6 +2247,7 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
|
||||
{
|
||||
char path[MAXPGPATH];
|
||||
char tmppath[MAXPGPATH];
|
||||
char zbuffer_raw[BLCKSZ + MAXIMUM_ALIGNOF];
|
||||
char *zbuffer;
|
||||
XLogSegNo installed_segno;
|
||||
int max_advance;
|
||||
@ -2286,16 +2286,6 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
|
||||
|
||||
unlink(tmppath);
|
||||
|
||||
/*
|
||||
* Allocate a buffer full of zeros. This is done before opening the file
|
||||
* so that we don't leak the file descriptor if palloc fails.
|
||||
*
|
||||
* Note: palloc zbuffer, instead of just using a local char array, to
|
||||
* ensure it is reasonably well-aligned; this may save a few cycles
|
||||
* transferring data to the kernel.
|
||||
*/
|
||||
zbuffer = (char *) palloc0(XLOG_BLCKSZ);
|
||||
|
||||
/* do not use get_sync_bit() here --- want to fsync only at end of fill */
|
||||
fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
|
||||
S_IRUSR | S_IWUSR);
|
||||
@ -2312,7 +2302,12 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
|
||||
* fsync below) that all the indirect blocks are down on disk. Therefore,
|
||||
* fdatasync(2) or O_DSYNC will be sufficient to sync future writes to the
|
||||
* log file.
|
||||
*
|
||||
* Note: ensure the buffer is reasonably well-aligned; this may save a few
|
||||
* cycles transferring data to the kernel.
|
||||
*/
|
||||
zbuffer = (char *) MAXALIGN(zbuffer_raw);
|
||||
memset(zbuffer, 0, BLCKSZ);
|
||||
for (nbytes = 0; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
|
||||
{
|
||||
errno = 0;
|
||||
@ -2335,7 +2330,6 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
|
||||
errmsg("could not write to file \"%s\": %m", tmppath)));
|
||||
}
|
||||
}
|
||||
pfree(zbuffer);
|
||||
|
||||
if (pg_fsync(fd) != 0)
|
||||
{
|
||||
|
Reference in New Issue
Block a user