Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit475861b

Browse files
committed
Add wal_recycle and wal_init_zero GUCs.
On at least ZFS, it can be beneficial to create new WAL files everytime and not to bother zero-filling them. Since it's not clear whichother filesystems might benefit from one or both of those things,add individual GUCs to control those two behaviors independently andmake only very general statements in the docs.Author: Jerry Jelinek, with some adjustments by Thomas MunroReviewed-by: Alvaro Herrera, Andres Freund, Tomas Vondra, Robert Haas and othersDiscussion:https://postgr.es/m/CACPQ5Fo00QR7LNAcd1ZjgoBi4y97%2BK760YABs0vQHH5dLdkkMA%40mail.gmail.com
1 parent4b82664 commit475861b

File tree

5 files changed

+123
-33
lines changed

5 files changed

+123
-33
lines changed

‎doc/src/sgml/config.sgml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,6 +3590,41 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
35903590
</listitem>
35913591
</varlistentry>
35923592

3593+
<varlistentry id="guc-wal-init-zero" xreflabel="wal_init_zero">
3594+
<term><varname>wal_init_zero</varname> (<type>boolean</type>)
3595+
<indexterm>
3596+
<primary><varname>wal_init_zero</varname> configuration parameter</primary>
3597+
</indexterm>
3598+
</term>
3599+
<listitem>
3600+
<para>
3601+
If set to <literal>on</literal> (the default), this option causes new
3602+
WAL files to be filled with zeroes. On some filesystems, this ensures
3603+
that space is allocated before we need to write WAL records. However,
3604+
<firstterm>Copy-On-Write</firstterm> (COW) filesystems may not benefit
3605+
from this technique, so the option is given to skip the unnecessary
3606+
work. If set to <literal>off</literal>, only the final byte is written
3607+
when the file is created so that it has the expected size.
3608+
</para>
3609+
</listitem>
3610+
</varlistentry>
3611+
3612+
<varlistentry id="guc-wal-recycle" xreflabel="wal_recycle">
3613+
<term><varname>wal_recycle</varname> (<type>boolean</type>)
3614+
<indexterm>
3615+
<primary><varname>wal_recycle</varname> configuration parameter</primary>
3616+
</indexterm>
3617+
</term>
3618+
<listitem>
3619+
<para>
3620+
If set to <literal>on</literal> (the default), this option causes WAL
3621+
files to be recycled by renaming them, avoiding the need to create new
3622+
ones. On COW filesystems, it may be faster to create new ones, so the
3623+
option is given to disable this behavior.
3624+
</para>
3625+
</listitem>
3626+
</varlistentry>
3627+
35933628
<varlistentry id="guc-wal-sender-timeout" xreflabel="wal_sender_timeout">
35943629
<term><varname>wal_sender_timeout</varname> (<type>integer</type>)
35953630
<indexterm>

‎src/backend/access/transam/xlog.c

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ boolwal_log_hints = false;
9595
boolwal_compression= false;
9696
char*wal_consistency_checking_string=NULL;
9797
bool*wal_consistency_checking=NULL;
98+
boolwal_init_zero= true;
99+
boolwal_recycle= true;
98100
boollog_checkpoints= false;
99101
intsync_method=DEFAULT_SYNC_METHOD;
100102
intwal_level=WAL_LEVEL_MINIMAL;
@@ -3209,6 +3211,7 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
32093211
XLogSegNomax_segno;
32103212
intfd;
32113213
intnbytes;
3214+
intsave_errno;
32123215

32133216
XLogFilePath(path,ThisTimeLineID,logsegno,wal_segment_size);
32143217

@@ -3248,39 +3251,61 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
32483251
(errcode_for_file_access(),
32493252
errmsg("could not create file \"%s\": %m",tmppath)));
32503253

3251-
/*
3252-
* Zero-fill the file. We have to do this the hard way to ensure that all
3253-
* the file space has really been allocated --- on platforms that allow
3254-
* "holes" in files, just seeking to the end doesn't allocate intermediate
3255-
* space. This way, we know that we have all the space and (after the
3256-
* fsync below) that all the indirect blocks are down on disk. Therefore,
3257-
* fdatasync(2) or O_DSYNC will be sufficient to sync future writes to the
3258-
* log file.
3259-
*/
32603254
memset(zbuffer.data,0,XLOG_BLCKSZ);
3261-
for (nbytes=0;nbytes<wal_segment_size;nbytes+=XLOG_BLCKSZ)
3255+
3256+
pgstat_report_wait_start(WAIT_EVENT_WAL_INIT_WRITE);
3257+
save_errno=0;
3258+
if (wal_init_zero)
32623259
{
3260+
/*
3261+
* Zero-fill the file. With this setting, we do this the hard way to
3262+
* ensure that all the file space has really been allocated. On
3263+
* platforms that allow "holes" in files, just seeking to the end
3264+
* doesn't allocate intermediate space. This way, we know that we
3265+
* have all the space and (after the fsync below) that all the
3266+
* indirect blocks are down on disk. Therefore, fdatasync(2) or
3267+
* O_DSYNC will be sufficient to sync future writes to the log file.
3268+
*/
3269+
for (nbytes=0;nbytes<wal_segment_size;nbytes+=XLOG_BLCKSZ)
3270+
{
3271+
errno=0;
3272+
if (write(fd,zbuffer.data,XLOG_BLCKSZ)!=XLOG_BLCKSZ)
3273+
{
3274+
/* if write didn't set errno, assume no disk space */
3275+
save_errno=errno ?errno :ENOSPC;
3276+
break;
3277+
}
3278+
}
3279+
}
3280+
else
3281+
{
3282+
/*
3283+
* Otherwise, seeking to the end and writing a solitary byte is
3284+
* enough.
3285+
*/
32633286
errno=0;
3264-
pgstat_report_wait_start(WAIT_EVENT_WAL_INIT_WRITE);
3265-
if ((int)write(fd,zbuffer.data,XLOG_BLCKSZ)!= (int)XLOG_BLCKSZ)
3287+
if (pg_pwrite(fd,zbuffer.data,1,wal_segment_size-1)!=1)
32663288
{
3267-
intsave_errno=errno;
3289+
/* if write didn't set errno, assume no disk space */
3290+
save_errno=errno ?errno :ENOSPC;
3291+
}
3292+
}
3293+
pgstat_report_wait_end();
32683294

3269-
/*
3270-
* If we fail to make the file, delete it to release disk space
3271-
*/
3272-
unlink(tmppath);
3295+
if (save_errno)
3296+
{
3297+
/*
3298+
* If we fail to make the file, delete it to release disk space
3299+
*/
3300+
unlink(tmppath);
32733301

3274-
close(fd);
3302+
close(fd);
32753303

3276-
/* if write didn't set errno, assume problem is no disk space */
3277-
errno=save_errno ?save_errno :ENOSPC;
3304+
errno=save_errno;
32783305

3279-
ereport(ERROR,
3280-
(errcode_for_file_access(),
3281-
errmsg("could not write to file \"%s\": %m",tmppath)));
3282-
}
3283-
pgstat_report_wait_end();
3306+
ereport(ERROR,
3307+
(errcode_for_file_access(),
3308+
errmsg("could not write to file \"%s\": %m",tmppath)));
32843309
}
32853310

32863311
pgstat_report_wait_start(WAIT_EVENT_WAL_INIT_SYNC);
@@ -4049,14 +4074,19 @@ RemoveXlogFile(const char *segname, XLogRecPtr RedoRecPtr, XLogRecPtr endptr)
40494074
XLogSegNoendlogSegNo;
40504075
XLogSegNorecycleSegNo;
40514076

4052-
/*
4053-
* Initialize info about where to try to recycle to.
4054-
*/
4055-
XLByteToSeg(endptr,endlogSegNo,wal_segment_size);
4056-
if (RedoRecPtr==InvalidXLogRecPtr)
4057-
recycleSegNo=endlogSegNo+10;
4077+
if (wal_recycle)
4078+
{
4079+
/*
4080+
* Initialize info about where to try to recycle to.
4081+
*/
4082+
XLByteToSeg(endptr,endlogSegNo,wal_segment_size);
4083+
if (RedoRecPtr==InvalidXLogRecPtr)
4084+
recycleSegNo=endlogSegNo+10;
4085+
else
4086+
recycleSegNo=XLOGfileslop(RedoRecPtr);
4087+
}
40584088
else
4059-
recycleSegNo=XLOGfileslop(RedoRecPtr);
4089+
recycleSegNo=0;/* keep compiler quiet */
40604090

40614091
snprintf(path,MAXPGPATH,XLOGDIR"/%s",segname);
40624092

@@ -4065,7 +4095,8 @@ RemoveXlogFile(const char *segname, XLogRecPtr RedoRecPtr, XLogRecPtr endptr)
40654095
* segment. Only recycle normal files, pg_standby for example can create
40664096
* symbolic links pointing to a separate archive directory.
40674097
*/
4068-
if (endlogSegNo <=recycleSegNo&&
4098+
if (wal_recycle&&
4099+
endlogSegNo <=recycleSegNo&&
40694100
lstat(path,&statbuf)==0&&S_ISREG(statbuf.st_mode)&&
40704101
InstallXLogFileSegment(&endlogSegNo,path,
40714102
true,recycleSegNo, true))

‎src/backend/utils/misc/guc.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,26 @@ static struct config_bool ConfigureNamesBool[] =
11741174
NULL,NULL,NULL
11751175
},
11761176

1177+
{
1178+
{"wal_init_zero",PGC_SUSET,WAL_SETTINGS,
1179+
gettext_noop("Writes zeroes to new WAL files before first use."),
1180+
NULL
1181+
},
1182+
&wal_init_zero,
1183+
true,
1184+
NULL,NULL,NULL
1185+
},
1186+
1187+
{
1188+
{"wal_recycle",PGC_SUSET,WAL_SETTINGS,
1189+
gettext_noop("Recycles WAL files by renaming them."),
1190+
NULL
1191+
},
1192+
&wal_recycle,
1193+
true,
1194+
NULL,NULL,NULL
1195+
},
1196+
11771197
{
11781198
{"log_checkpoints",PGC_SIGHUP,LOGGING_WHAT,
11791199
gettext_noop("Logs each checkpoint."),

‎src/backend/utils/misc/postgresql.conf.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@
206206
#wal_compression = off# enable compression of full-page writes
207207
#wal_log_hints = off# also do full page writes of non-critical updates
208208
# (change requires restart)
209+
#wal_init_zero = on# zero-fill new WAL files
210+
#wal_recycle = on# recycle WAL files
209211
#wal_buffers = -1# min 32kB, -1 sets based on shared_buffers
210212
# (change requires restart)
211213
#wal_writer_delay = 200ms# 1-10000 milliseconds

‎src/include/access/xlog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ extern bool EnableHotStandby;
116116
externboolfullPageWrites;
117117
externboolwal_log_hints;
118118
externboolwal_compression;
119+
externboolwal_init_zero;
120+
externboolwal_recycle;
119121
externbool*wal_consistency_checking;
120122
externchar*wal_consistency_checking_string;
121123
externboollog_checkpoints;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp