Skip to content

Commit 3dad73e

Browse files
committed
Add -F option to pg_receivexlog, for specifying fsync interval.
This allows us to specify the maximum time to issue fsync to ensure the received WAL file is safely flushed to disk. Without this, pg_receivexlog always flushes WAL file only when it's closed and which can cause WAL data to be lost at the event of a crash. Furuya Osamu, heavily modified by me.
1 parent 1add956 commit 3dad73e

File tree

5 files changed

+195
-70
lines changed

5 files changed

+195
-70
lines changed

doc/src/sgml/ref/pg_receivexlog.sgml

+15
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ PostgreSQL documentation
105105
</listitem>
106106
</varlistentry>
107107

108+
<varlistentry>
109+
<term><option>-F <replaceable class="parameter">interval</replaceable></option></term>
110+
<term><option>--fsync-interval=<replaceable class="parameter">interval</replaceable></option></term>
111+
<listitem>
112+
<para>
113+
Specifies the maximum time to issue sync commands to ensure the
114+
received WAL file is safely flushed to disk, in seconds. The default
115+
value is zero, which disables issuing fsyncs except when WAL file is
116+
closed. If <literal>-1</literal> is specified, WAL file is flushed as
117+
soon as possible, that is, as soon as there are WAL data which has
118+
not been flushed yet.
119+
</para>
120+
</listitem>
121+
</varlistentry>
122+
108123
<varlistentry>
109124
<term><option>-v</option></term>
110125
<term><option>--verbose</option></term>

src/bin/pg_basebackup/pg_basebackup.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ LogStreamerMain(logstreamer_param *param)
371371
if (!ReceiveXlogStream(param->bgconn, param->startptr, param->timeline,
372372
param->sysidentifier, param->xlogdir,
373373
reached_end_position, standby_message_timeout,
374-
NULL))
374+
NULL, 0))
375375

376376
/*
377377
* Any errors will already have been reported in the function process,

src/bin/pg_basebackup/pg_receivexlog.c

+16-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static char *basedir = NULL;
3636
static int verbose = 0;
3737
static int noloop = 0;
3838
static int standby_message_timeout = 10 * 1000; /* 10 sec = default */
39+
static int fsync_interval = 0; /* 0 = default */
3940
static volatile bool time_to_abort = false;
4041

4142

@@ -62,6 +63,8 @@ usage(void)
6263
printf(_("\nOptions:\n"));
6364
printf(_(" -D, --directory=DIR receive transaction log files into this directory\n"));
6465
printf(_(" -n, --no-loop do not loop on connection lost\n"));
66+
printf(_(" -F --fsync-interval=INTERVAL\n"
67+
" frequency of syncs to transaction log files (in seconds)\n"));
6568
printf(_(" -v, --verbose output verbose messages\n"));
6669
printf(_(" -V, --version output version information, then exit\n"));
6770
printf(_(" -?, --help show this help, then exit\n"));
@@ -330,7 +333,8 @@ StreamLog(void)
330333
starttli);
331334

332335
ReceiveXlogStream(conn, startpos, starttli, NULL, basedir,
333-
stop_streaming, standby_message_timeout, ".partial");
336+
stop_streaming, standby_message_timeout, ".partial",
337+
fsync_interval);
334338

335339
PQfinish(conn);
336340
}
@@ -360,6 +364,7 @@ main(int argc, char **argv)
360364
{"port", required_argument, NULL, 'p'},
361365
{"username", required_argument, NULL, 'U'},
362366
{"no-loop", no_argument, NULL, 'n'},
367+
{"fsync-interval", required_argument, NULL, 'F'},
363368
{"no-password", no_argument, NULL, 'w'},
364369
{"password", no_argument, NULL, 'W'},
365370
{"status-interval", required_argument, NULL, 's'},
@@ -389,7 +394,7 @@ main(int argc, char **argv)
389394
}
390395
}
391396

392-
while ((c = getopt_long(argc, argv, "D:d:h:p:U:s:nwWv",
397+
while ((c = getopt_long(argc, argv, "D:d:h:p:U:s:nF:wWv",
393398
long_options, &option_index)) != -1)
394399
{
395400
switch (c)
@@ -436,6 +441,15 @@ main(int argc, char **argv)
436441
case 'n':
437442
noloop = 1;
438443
break;
444+
case 'F':
445+
fsync_interval = atoi(optarg) * 1000;
446+
if (fsync_interval < -1000)
447+
{
448+
fprintf(stderr, _("%s: invalid fsync interval \"%s\"\n"),
449+
progname, optarg);
450+
exit(1);
451+
}
452+
break;
439453
case 'v':
440454
verbose++;
441455
break;

0 commit comments

Comments
 (0)