Skip to content

Commit bf4a867

Browse files
committed
pg_resetwal: Allow users to change the WAL segment size
This adds a new option --wal-segsize (analogous to initdb) that changes the WAL segment size in pg_control. Author: Nathan Bossart <bossartn@amazon.com>
1 parent 8ad8d91 commit bf4a867

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

doc/src/sgml/ref/pg_resetwal.sgml

+22
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,28 @@ PostgreSQL documentation
275275
</listitem>
276276
</varlistentry>
277277

278+
<varlistentry>
279+
<term><option>--wal-segsize=<replaceable class="parameter">wal_segment_size</replaceable></option></term>
280+
<listitem>
281+
<para>
282+
Set the new WAL segment size, in megabytes. The value must be set to a
283+
power of 2 between 1 and 1024 (megabytes). See the same option of <xref
284+
linkend="app-initdb"/> for more information.
285+
</para>
286+
287+
<note>
288+
<para>
289+
While <command>pg_resetwal</command> will set the WAL starting address
290+
beyond the latest existing WAL segment file, some segment size changes
291+
can cause previous WAL file names to be reused. It is recommended to
292+
use <option>-l</option> together with this option to manually set the
293+
WAL starting address if WAL file name overlap will cause problems with
294+
your archiving strategy.
295+
</para>
296+
</note>
297+
</listitem>
298+
</varlistentry>
299+
278300
<varlistentry>
279301
<term><option>-x <replaceable class="parameter">xid</replaceable></option></term>
280302
<term><option>--next-transaction-id=<replaceable class="parameter">xid</replaceable></option></term>

src/bin/pg_resetwal/pg_resetwal.c

+43-9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ static MultiXactOffset set_mxoff = (MultiXactOffset) -1;
7272
static uint32 minXlogTli = 0;
7373
static XLogSegNo minXlogSegNo = 0;
7474
static int WalSegSz;
75+
static int set_wal_segsize;
7576

7677
static void CheckDataVersion(void);
7778
static bool ReadControlFile(void);
@@ -100,6 +101,7 @@ main(int argc, char *argv[])
100101
{"next-oid", required_argument, NULL, 'o'},
101102
{"multixact-offset", required_argument, NULL, 'O'},
102103
{"next-transaction-id", required_argument, NULL, 'x'},
104+
{"wal-segsize", required_argument, NULL, 1},
103105
{NULL, 0, NULL, 0}
104106
};
105107

@@ -290,6 +292,24 @@ main(int argc, char *argv[])
290292
log_fname = pg_strdup(optarg);
291293
break;
292294

295+
case 1:
296+
set_wal_segsize = strtol(optarg, &endptr, 10) * 1024 * 1024;
297+
if (endptr == optarg || *endptr != '\0')
298+
{
299+
fprintf(stderr,
300+
_("%s: argument of --wal-segsize must be a number\n"),
301+
progname);
302+
exit(1);
303+
}
304+
if (!IsValidWalSegSize(set_wal_segsize))
305+
{
306+
fprintf(stderr,
307+
_("%s: argument of --wal-segsize must be a power of 2 between 1 and 1024\n"),
308+
progname);
309+
exit(1);
310+
}
311+
break;
312+
293313
default:
294314
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
295315
exit(1);
@@ -372,6 +392,14 @@ main(int argc, char *argv[])
372392
if (!ReadControlFile())
373393
GuessControlValues();
374394

395+
/*
396+
* If no new WAL segment size was specified, use the control file value.
397+
*/
398+
if (set_wal_segsize != 0)
399+
WalSegSz = set_wal_segsize;
400+
else
401+
WalSegSz = ControlFile.xlog_seg_size;
402+
375403
if (log_fname != NULL)
376404
XLogFromFileName(log_fname, &minXlogTli, &minXlogSegNo, WalSegSz);
377405

@@ -438,6 +466,9 @@ main(int argc, char *argv[])
438466
ControlFile.checkPointCopy.PrevTimeLineID = minXlogTli;
439467
}
440468

469+
if (set_wal_segsize != 0)
470+
ControlFile.xlog_seg_size = WalSegSz;
471+
441472
if (minXlogSegNo > newXlogSegNo)
442473
newXlogSegNo = minXlogSegNo;
443474

@@ -608,14 +639,13 @@ ReadControlFile(void)
608639
}
609640

610641
memcpy(&ControlFile, buffer, sizeof(ControlFile));
611-
WalSegSz = ControlFile.xlog_seg_size;
612642

613-
/* return false if WalSegSz is not valid */
614-
if (!IsValidWalSegSize(WalSegSz))
643+
/* return false if WAL segment size is not valid */
644+
if (!IsValidWalSegSize(ControlFile.xlog_seg_size))
615645
{
616646
fprintf(stderr,
617647
_("%s: pg_control specifies invalid WAL segment size (%d bytes); proceed with caution \n"),
618-
progname, WalSegSz);
648+
progname, ControlFile.xlog_seg_size);
619649
return false;
620650
}
621651

@@ -694,7 +724,7 @@ GuessControlValues(void)
694724
ControlFile.blcksz = BLCKSZ;
695725
ControlFile.relseg_size = RELSEG_SIZE;
696726
ControlFile.xlog_blcksz = XLOG_BLCKSZ;
697-
WalSegSz = ControlFile.xlog_seg_size = DEFAULT_XLOG_SEG_SIZE;
727+
ControlFile.xlog_seg_size = DEFAULT_XLOG_SEG_SIZE;
698728
ControlFile.nameDataLen = NAMEDATALEN;
699729
ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
700730
ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
@@ -859,6 +889,12 @@ PrintNewControlValues(void)
859889
printf(_("newestCommitTsXid: %u\n"),
860890
ControlFile.checkPointCopy.newestCommitTsXid);
861891
}
892+
893+
if (set_wal_segsize != 0)
894+
{
895+
printf(_("Bytes per WAL segment: %u\n"),
896+
ControlFile.xlog_seg_size);
897+
}
862898
}
863899

864900

@@ -910,9 +946,6 @@ RewriteControlFile(void)
910946
ControlFile.max_prepared_xacts = 0;
911947
ControlFile.max_locks_per_xact = 64;
912948

913-
/* Now we can force the recorded xlog seg size to the right thing. */
914-
ControlFile.xlog_seg_size = WalSegSz;
915-
916949
/* Contents are protected with a CRC */
917950
INIT_CRC32C(ControlFile.crc);
918951
COMP_CRC32C(ControlFile.crc,
@@ -1048,7 +1081,7 @@ FindEndOfXLOG(void)
10481081
* are in virgin territory.
10491082
*/
10501083
xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
1051-
newXlogSegNo = (xlogbytepos + WalSegSz - 1) / WalSegSz;
1084+
newXlogSegNo = (xlogbytepos + ControlFile.xlog_seg_size - 1) / WalSegSz;
10521085
newXlogSegNo++;
10531086
}
10541087

@@ -1279,6 +1312,7 @@ usage(void)
12791312
printf(_(" -O, --multixact-offset=OFFSET set next multitransaction offset\n"));
12801313
printf(_(" -V, --version output version information, then exit\n"));
12811314
printf(_(" -x, --next-transaction-id=XID set next transaction ID\n"));
1315+
printf(_(" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
12821316
printf(_(" -?, --help show this help, then exit\n"));
12831317
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
12841318
}

0 commit comments

Comments
 (0)