02-initdb-walsegsize-v7.patch
application/octet-stream
Filename: 02-initdb-walsegsize-v7.patch
Type: application/octet-stream
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
Series: patch v7
| File | + | − |
|---|---|---|
| configure | 0 | 51 |
| configure.in | 0 | 27 |
| contrib/pg_standby/pg_standby.c | 80 | 30 |
| doc/src/sgml/backup.sgml | 1 | 1 |
| doc/src/sgml/installation.sgml | 0 | 14 |
| doc/src/sgml/ref/initdb.sgml | 14 | 0 |
| doc/src/sgml/wal.sgml | 6 | 7 |
| src/backend/access/transam/xlog.c | 75 | 17 |
| src/backend/utils/misc/guc.c | 16 | 23 |
| src/backend/utils/misc/postgresql.conf.sample | 1 | 1 |
| src/bin/initdb/initdb.c | 68 | 0 |
| src/bin/pg_basebackup/pg_basebackup.c | 3 | 0 |
| src/bin/pg_basebackup/pg_receivewal.c | 3 | 0 |
| src/bin/pg_basebackup/receivelog.h | 2 | 0 |
| src/bin/pg_basebackup/streamutil.c | 61 | 0 |
| src/bin/pg_basebackup/streamutil.h | 1 | 0 |
| src/bin/pg_controldata/pg_controldata.c | 5 | 0 |
| src/bin/pg_resetwal/pg_resetwal.c | 13 | 1 |
| src/bin/pg_rewind/pg_rewind.c | 4 | 0 |
| src/bin/pg_test_fsync/pg_test_fsync.c | 3 | 3 |
| src/bin/pg_waldump/pg_waldump.c | 54 | 0 |
| src/include/access/xlog.h | 3 | 2 |
| src/include/access/xlog_internal.h | 19 | 1 |
| src/include/pg_config.h.in | 0 | 7 |
| src/include/pg_config_manual.h | 6 | 0 |
| src/include/utils/guc.h | 1 | 1 |
| src/tools/msvc/Solution.pm | 0 | 1 |
diff --git a/configure b/configure
index b5cdebb..6c7de66 100755
--- a/configure
+++ b/configure
@@ -816,7 +816,6 @@ enable_tap_tests
with_blocksize
with_segsize
with_wal_blocksize
-with_wal_segsize
with_CC
enable_depend
enable_cassert
@@ -1508,8 +1507,6 @@ Optional Packages:
--with-segsize=SEGSIZE set table segment size in GB [1]
--with-wal-blocksize=BLOCKSIZE
set WAL block size in kB [8]
- --with-wal-segsize=SEGSIZE
- set WAL segment size in MB [16]
--with-CC=CMD set compiler (deprecated)
--with-tcl build Tcl modules (PL/Tcl)
--with-tclconfig=DIR tclConfig.sh is in DIR
@@ -3668,54 +3665,6 @@ cat >>confdefs.h <<_ACEOF
#define XLOG_BLCKSZ ${XLOG_BLCKSZ}
_ACEOF
-
-#
-# WAL segment size
-#
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for WAL segment size" >&5
-$as_echo_n "checking for WAL segment size... " >&6; }
-
-
-
-# Check whether --with-wal-segsize was given.
-if test "${with_wal_segsize+set}" = set; then :
- withval=$with_wal_segsize;
- case $withval in
- yes)
- as_fn_error $? "argument required for --with-wal-segsize option" "$LINENO" 5
- ;;
- no)
- as_fn_error $? "argument required for --with-wal-segsize option" "$LINENO" 5
- ;;
- *)
- wal_segsize=$withval
- ;;
- esac
-
-else
- wal_segsize=16
-fi
-
-
-case ${wal_segsize} in
- 1) ;;
- 2) ;;
- 4) ;;
- 8) ;;
- 16) ;;
- 32) ;;
- 64) ;;
- *) as_fn_error $? "Invalid WAL segment size. Allowed values are 1,2,4,8,16,32,64." "$LINENO" 5
-esac
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: ${wal_segsize}MB" >&5
-$as_echo "${wal_segsize}MB" >&6; }
-
-
-cat >>confdefs.h <<_ACEOF
-#define XLOG_SEG_SIZE (${wal_segsize} * 1024 * 1024)
-_ACEOF
-
-
#
# C compiler
#
diff --git a/configure.in b/configure.in
index 1d99cda..ea5d02e 100644
--- a/configure.in
+++ b/configure.in
@@ -344,33 +344,6 @@ AC_DEFINE_UNQUOTED([XLOG_BLCKSZ], ${XLOG_BLCKSZ}, [
])
#
-# WAL segment size
-#
-AC_MSG_CHECKING([for WAL segment size])
-PGAC_ARG_REQ(with, wal-segsize, [SEGSIZE], [set WAL segment size in MB [16]],
- [wal_segsize=$withval],
- [wal_segsize=16])
-case ${wal_segsize} in
- 1) ;;
- 2) ;;
- 4) ;;
- 8) ;;
- 16) ;;
- 32) ;;
- 64) ;;
- *) AC_MSG_ERROR([Invalid WAL segment size. Allowed values are 1,2,4,8,16,32,64.])
-esac
-AC_MSG_RESULT([${wal_segsize}MB])
-
-AC_DEFINE_UNQUOTED([XLOG_SEG_SIZE], [(${wal_segsize} * 1024 * 1024)], [
- XLOG_SEG_SIZE is the size of a single WAL file. This must be a power of 2
- and larger than XLOG_BLCKSZ (preferably, a great deal larger than
- XLOG_BLCKSZ).
-
- Changing XLOG_SEG_SIZE requires an initdb.
-])
-
-#
# C compiler
#
diff --git a/contrib/pg_standby/pg_standby.c b/contrib/pg_standby/pg_standby.c
index e4d057e..7ed3522 100644
--- a/contrib/pg_standby/pg_standby.c
+++ b/contrib/pg_standby/pg_standby.c
@@ -33,9 +33,12 @@
#include "pg_getopt.h"
#include "access/xlog_internal.h"
+#include "access/xlogreader.h"
const char *progname;
+uint32 XLogSegSize;
+
/* Options and defaults */
int sleeptime = 5; /* amount of time to sleep between file checks */
int waittime = -1; /* how long we have been waiting, -1 no wait
@@ -100,6 +103,54 @@ int nextWALFileType;
struct stat stat_buf;
+static bool SetWALFileNameForCleanup(void);
+
+/* Set XLogSegSize from the WAL file header */
+static bool
+RetrieveXLogSegSize()
+{
+ int fd;
+ bool ret = false;
+
+ if (stat(WALFilePath, &stat_buf) == 0)
+ {
+ if ((fd = open(WALFilePath, O_RDWR, 0)) >= 0)
+ {
+ char *buf = (char *) malloc(XLOG_BLCKSZ);
+
+ if (read(fd, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
+ {
+ fprintf(stderr, "could not read from log segment %s",
+ nextWALFileName);
+ fflush(stderr);
+ }
+ else
+ {
+ XLogPageHeader hdr = (XLogPageHeader) buf;
+ XLogLongPageHeader NewLongPage = (XLogLongPageHeader) hdr;
+
+ XLogSegSize = NewLongPage->xlp_seg_size;
+ need_cleanup = SetWALFileNameForCleanup();
+
+ if (debug)
+ {
+ fprintf(stderr,
+ _("WAL segment size: %d \n"), NewLongPage->xlp_seg_size);
+ fprintf(stderr, "Keep archive history: ");
+ if (need_cleanup)
+ fprintf(stderr, "%s and later\n", exclusiveCleanupFileName);
+ else
+ fprintf(stderr, "no cleanup required\n");
+ fflush(stderr);
+ }
+ ret = true;
+ }
+ }
+ close(fd);
+ }
+ return ret;
+}
+
/* =====================================================================
*
* Customizable section
@@ -184,42 +235,47 @@ CustomizableNextWALFileReady(void)
nextWALFileType = XLOG_BACKUP_LABEL;
return true;
}
- else if (stat_buf.st_size == XLOG_SEG_SIZE)
+ else
{
+ /* Check if we can read the WAL header and set the XLogSegSize */
+ if (!RetrieveXLogSegSize())
+ return false;
+
+ if (stat_buf.st_size == XLOG_SEG_SIZE)
+ {
#ifdef WIN32
- /*
- * Windows 'cp' sets the final file size before the copy is
- * complete, and not yet ready to be opened by pg_standby. So we
- * wait for sleeptime secs before attempting to restore. If that
- * is not enough, we will rely on the retry/holdoff mechanism.
- * GNUWin32's cp does not have this problem.
- */
- pg_usleep(sleeptime * 1000000L);
+ /*
+ * Windows 'cp' sets the final file size before the copy is
+ * complete, and not yet ready to be opened by pg_standby. So
+ * we wait for sleeptime secs before attempting to restore. If
+ * that is not enough, we will rely on the retry/holdoff
+ * mechanism. GNUWin32's cp does not have this problem.
+ */
+ pg_usleep(sleeptime * 1000000L);
#endif
- nextWALFileType = XLOG_DATA;
- return true;
- }
+ nextWALFileType = XLOG_DATA;
+ return true;
+ }
- /*
- * If still too small, wait until it is the correct size
- */
- if (stat_buf.st_size > XLOG_SEG_SIZE)
- {
- if (debug)
+ /*
+ * If still too small, wait until it is the correct size
+ */
+ if (stat_buf.st_size > XLOG_SEG_SIZE)
{
- fprintf(stderr, "file size greater than expected\n");
- fflush(stderr);
+ if (debug)
+ {
+ fprintf(stderr, "file size greater than expected\n");
+ fflush(stderr);
+ }
+ exit(3);
}
- exit(3);
}
}
return false;
}
-#define MaxSegmentsPerLogFile ( 0xFFFFFFFF / XLOG_SEG_SIZE )
-
static void
CustomizableCleanupPriorWALFiles(void)
{
@@ -315,6 +371,7 @@ SetWALFileNameForCleanup(void)
uint32 log_diff = 0,
seg_diff = 0;
bool cleanup = false;
+ int MaxSegmentsPerLogFile = (0xFFFFFFFF / XLogSegSize);
if (restartWALFileName)
{
@@ -708,8 +765,6 @@ main(int argc, char **argv)
CustomizableInitialize();
- need_cleanup = SetWALFileNameForCleanup();
-
if (debug)
{
fprintf(stderr, "Trigger file: %s\n", triggerPath ? triggerPath : "<not set>");
@@ -721,11 +776,6 @@ main(int argc, char **argv)
fprintf(stderr, "Max wait interval: %d %s\n",
maxwaittime, (maxwaittime > 0 ? "seconds" : "forever"));
fprintf(stderr, "Command for restore: %s\n", restoreCommand);
- fprintf(stderr, "Keep archive history: ");
- if (need_cleanup)
- fprintf(stderr, "%s and later\n", exclusiveCleanupFileName);
- else
- fprintf(stderr, "no cleanup required\n");
fflush(stderr);
}
diff --git a/doc/src/sgml/backup.sgml b/doc/src/sgml/backup.sgml
index 69c599e..aad08de 100644
--- a/doc/src/sgml/backup.sgml
+++ b/doc/src/sgml/backup.sgml
@@ -562,7 +562,7 @@ tar -cf backup.tar /usr/local/pgsql/data
produces an indefinitely long sequence of WAL records. The system
physically divides this sequence into WAL <firstterm>segment
files</>, which are normally 16MB apiece (although the segment size
- can be altered when building <productname>PostgreSQL</>). The segment
+ can be altered during <application>initdb</>). The segment
files are given numeric names that reflect their position in the
abstract WAL sequence. When not using WAL archiving, the system
normally creates just a few segment files and then
diff --git a/doc/src/sgml/installation.sgml b/doc/src/sgml/installation.sgml
index 568995c..5b975b4 100644
--- a/doc/src/sgml/installation.sgml
+++ b/doc/src/sgml/installation.sgml
@@ -1027,20 +1027,6 @@ su - postgres
</varlistentry>
<varlistentry>
- <term><option>--with-wal-segsize=<replaceable>SEGSIZE</replaceable></option></term>
- <listitem>
- <para>
- Set the <firstterm>WAL segment size</>, in megabytes. This is
- the size of each individual file in the WAL log. It may be useful
- to adjust this size to control the granularity of WAL log shipping.
- The default size is 16 megabytes.
- The value must be a power of 2 between 1 and 64 (megabytes).
- Note that changing this value requires an initdb.
- </para>
- </listitem>
- </varlistentry>
-
- <varlistentry>
<term><option>--with-wal-blocksize=<replaceable>BLOCKSIZE</replaceable></option></term>
<listitem>
<para>
diff --git a/doc/src/sgml/ref/initdb.sgml b/doc/src/sgml/ref/initdb.sgml
index d9faa96..f37ba61 100644
--- a/doc/src/sgml/ref/initdb.sgml
+++ b/doc/src/sgml/ref/initdb.sgml
@@ -316,6 +316,20 @@ PostgreSQL documentation
</varlistentry>
<varlistentry>
+ <term><option>--wal-segsize=<replaceable>SEGSIZE</replaceable></option></term>
+ <listitem>
+ <para>
+ Set the <firstterm>WAL segment size</>, in megabytes. This is
+ the size of each individual file in the WAL log. It may be useful
+ to adjust this size to control the granularity of WAL log shipping.
+ The default size is 16 megabytes.
+ The value must be a power of 2 between 1 and 1024 (megabytes).
+ Note that this value is fixed for the lifetime of the database cluster.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><option>-X <replaceable class="parameter">directory</replaceable></option></term>
<term><option>--waldir=<replaceable class="parameter">directory</replaceable></option></term>
<listitem>
diff --git a/doc/src/sgml/wal.sgml b/doc/src/sgml/wal.sgml
index 1468ba5..08ff49d 100644
--- a/doc/src/sgml/wal.sgml
+++ b/doc/src/sgml/wal.sgml
@@ -752,13 +752,12 @@
<acronym>WAL</acronym> logs are stored in the directory
<filename>pg_wal</filename> under the data directory, as a set of
segment files, normally each 16 MB in size (but the size can be changed
- by altering the <option>--with-wal-segsize</> configure option when
- building the server). Each segment is divided into pages, normally
- 8 kB each (this size can be changed via the <option>--with-wal-blocksize</>
- configure option). The log record headers are described in
- <filename>access/xlogrecord.h</filename>; the record content is dependent
- on the type of event that is being logged. Segment files are given
- ever-increasing numbers as names, starting at
+ by altering the <option>--wal-segsize</> initdb option). Each segment is
+ divided into pages, normally 8 kB each (this size can be changed via the
+ <option>--with-wal-blocksize</> configure option). The log record headers
+ are described in <filename>access/xlogrecord.h</filename>; the record content
+ is dependent on the type of event that is being logged. Segment files are
+ given ever-increasing numbers as names, starting at
<filename>000000010000000000000000</filename>. The numbers do not wrap,
but it will take a very, very long time to exhaust the
available stock of numbers.
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 64335f9..0906543 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -86,8 +86,9 @@ extern uint32 bootstrap_data_checksum_version;
/* User-settable parameters */
-int max_wal_size = 64; /* 1 GB */
-int min_wal_size = 5; /* 80 MB */
+int wal_segment_size = DEFAULT_XLOG_SEG_SIZE / XLOG_BLCKSZ;
+int max_wal_size_mb = 1024; /* 1 GB */
+int min_wal_size_mb = 80; /* 80 MB */
int wal_keep_segments = 0;
int XLOGbuffers = -1;
int XLogArchiveTimeout = 0;
@@ -110,6 +111,9 @@ int wal_retrieve_retry_interval = 5000;
bool XLOG_DEBUG = false;
#endif
+uint32 XLogSegSize;
+
+
/*
* Number of WAL insertion locks to use. A higher value allows more insertions
* to happen concurrently, but adds some CPU overhead to flushing the WAL,
@@ -727,10 +731,16 @@ static ControlFileData *ControlFile = NULL;
(((recptr) / XLOG_BLCKSZ) % (XLogCtl->XLogCacheBlck + 1))
/*
- * These are the number of bytes in a WAL page and segment usable for WAL data.
+ * These are the number of bytes in a WAL page usable for WAL data.
*/
#define UsableBytesInPage (XLOG_BLCKSZ - SizeOfXLogShortPHD)
-#define UsableBytesInSegment ((XLOG_SEG_SIZE / XLOG_BLCKSZ) * UsableBytesInPage - (SizeOfXLogLongPHD - SizeOfXLogShortPHD))
+
+ /* Convert the min and max wal_size to equivalent segment count. */
+#define ConvertToXSegs(x) \
+ (x / (wal_segment_size * XLOG_BLCKSZ/ (1024 * 1024)))
+
+/* The number of bytes in a WAL segment usable for WAL data. */
+static int UsableBytesInSegment;
/*
* Private, possibly out-of-date copy of shared LogwrtResult.
@@ -2211,7 +2221,7 @@ CalculateCheckpointSegments(void)
* number of segments consumed between checkpoints.
*-------
*/
- target = (double) max_wal_size / (2.0 + CheckPointCompletionTarget);
+ target = (double) ConvertToXSegs(max_wal_size_mb) / (2.0 + CheckPointCompletionTarget);
/* round down */
CheckPointSegments = (int) target;
@@ -2223,7 +2233,7 @@ CalculateCheckpointSegments(void)
void
assign_max_wal_size(int newval, void *extra)
{
- max_wal_size = newval;
+ max_wal_size_mb = newval;
CalculateCheckpointSegments();
}
@@ -2251,8 +2261,8 @@ XLOGfileslop(XLogRecPtr PriorRedoPtr)
* correspond to. Always recycle enough segments to meet the minimum, and
* remove enough segments to stay below the maximum.
*/
- minSegNo = PriorRedoPtr / XLOG_SEG_SIZE + min_wal_size - 1;
- maxSegNo = PriorRedoPtr / XLOG_SEG_SIZE + max_wal_size - 1;
+ minSegNo = PriorRedoPtr / XLOG_SEG_SIZE + ConvertToXSegs(min_wal_size_mb) - 1;
+ maxSegNo = PriorRedoPtr / XLOG_SEG_SIZE + ConvertToXSegs(max_wal_size_mb) - 1;
/*
* Between those limits, recycle enough segments to get us through to the
@@ -4342,6 +4352,22 @@ rescanLatestTimeLine(void)
}
/*
+ * The Actual XLogSegSize is only set after reading the ControlFile.
+ * Verify that the min and max wal_size meet the minimum requirements.
+ */
+static void
+check_wal_size()
+{
+ if (ConvertToXSegs(min_wal_size_mb) < 2)
+ ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("Insufficient value for \"min_wal_size\"")));
+
+ if (ConvertToXSegs(max_wal_size_mb) < 2)
+ ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("Insufficient value for \"max_wal_size\".")));
+}
+
+/*
* I/O routines for pg_control
*
* *ControlFile is a buffer in shared memory that holds an image of the
@@ -4440,6 +4466,8 @@ ReadControlFile(void)
{
pg_crc32c crc;
int fd;
+ uint32 wal_segsz;
+ static char wal_segsz_str[20];
/*
* Read data...
@@ -4540,13 +4568,6 @@ ReadControlFile(void)
" but the server was compiled with XLOG_BLCKSZ %d.",
ControlFile->xlog_blcksz, XLOG_BLCKSZ),
errhint("It looks like you need to recompile or initdb.")));
- if (ControlFile->xlog_seg_size != XLOG_SEG_SIZE)
- ereport(FATAL,
- (errmsg("database files are incompatible with server"),
- errdetail("The database cluster was initialized with XLOG_SEG_SIZE %d,"
- " but the server was compiled with XLOG_SEG_SIZE %d.",
- ControlFile->xlog_seg_size, XLOG_SEG_SIZE),
- errhint("It looks like you need to recompile or initdb.")));
if (ControlFile->nameDataLen != NAMEDATALEN)
ereport(FATAL,
(errmsg("database files are incompatible with server"),
@@ -4607,10 +4628,21 @@ ReadControlFile(void)
" but the server was compiled without USE_FLOAT8_BYVAL."),
errhint("It looks like you need to recompile or initdb.")));
#endif
+ XLogSegSize = ControlFile->xlog_seg_size;
/* Make the initdb settings visible as GUC variables, too */
SetConfigOption("data_checksums", DataChecksumsEnabled() ? "yes" : "no",
PGC_INTERNAL, PGC_S_OVERRIDE);
+ wal_segsz = XLogSegSize / XLOG_BLCKSZ;
+ snprintf(wal_segsz_str, sizeof(wal_segsz_str), "%d", wal_segsz);
+ SetConfigOption("wal_segment_size", wal_segsz_str, PGC_INTERNAL,
+ PGC_S_OVERRIDE);
+
+ /* Update variables using XLogSegSize */
+ check_wal_size();
+ UsableBytesInSegment = (wal_segment_size * UsableBytesInPage) -
+ (SizeOfXLogLongPHD - SizeOfXLogShortPHD);
+ CalculateCheckpointSegments();
}
void
@@ -4724,10 +4756,11 @@ XLOGChooseNumBuffers(void)
int xbuffers;
xbuffers = NBuffers / 32;
- if (xbuffers > XLOG_SEG_SIZE / XLOG_BLCKSZ)
- xbuffers = XLOG_SEG_SIZE / XLOG_BLCKSZ;
+ if (xbuffers > wal_segment_size)
+ xbuffers = wal_segment_size;
if (xbuffers < 8)
xbuffers = 8;
+
return xbuffers;
}
@@ -4784,6 +4817,18 @@ XLOGShmemSize(void)
{
char buf[32];
+ /*
+ * The calculation of XLOGbuffers requires the run-time parameter
+ * XLogSegSize which is set from the ControlFile. This value is
+ * required to create the shared memory segment. Hence, temporarily
+ * allocating space and reading ControlFile.
+ */
+ if (!IsBootstrapProcessingMode())
+ {
+ ControlFile = palloc(sizeof(ControlFileData));
+ ReadControlFile();
+ pfree(ControlFile);
+ }
snprintf(buf, sizeof(buf), "%d", XLOGChooseNumBuffers());
SetConfigOption("wal_buffers", buf, PGC_POSTMASTER, PGC_S_OVERRIDE);
}
@@ -4929,6 +4974,16 @@ BootStrapXLOG(void)
struct timeval tv;
pg_crc32c crc;
+ /* initdb passes the WAL segment size in an environment variable. */
+ XLogSegSize = pg_atoi(getenv("XLOG_SEG_SIZE"), sizeof(uint32), 0);
+
+ if (!IsValidXLogSegSize(XLogSegSize))
+ {
+ ereport(PANIC,
+ (errcode(ERRCODE_DATA_CORRUPTED),
+ errmsg("Envoirnment variable XLOG_SEG_SIZE is corrupted")));
+ }
+
/*
* Select a hopefully-unique system identifier code for this installation.
* We use the result of gettimeofday(), including the fractional seconds
@@ -8101,6 +8156,9 @@ InitXLOGAccess(void)
ThisTimeLineID = XLogCtl->ThisTimeLineID;
Assert(ThisTimeLineID != 0 || IsBootstrapProcessingMode());
+ /* Set XLogSegSize */
+ XLogSegSize = ControlFile->xlog_seg_size;
+
/* Use GetRedoRecPtr to copy the RedoRecPtr safely */
(void) GetRedoRecPtr();
/* Also update our copy of doPageWrites. */
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 4feb26a..5240354 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -517,7 +517,6 @@ static int block_size;
static int segment_size;
static int wal_block_size;
static bool data_checksums;
-static int wal_segment_size;
static bool integer_datetimes;
static bool assert_enabled;
@@ -715,9 +714,6 @@ typedef struct
#if XLOG_BLCKSZ < 1024 || XLOG_BLCKSZ > (1024*1024)
#error XLOG_BLCKSZ must be between 1KB and 1MB
#endif
-#if XLOG_SEG_SIZE < (1024*1024) || XLOG_SEG_SIZE > (1024*1024*1024)
-#error XLOG_SEG_SIZE must be between 1MB and 1GB
-#endif
static const char *memory_units_hint = gettext_noop("Valid units for this parameter are \"kB\", \"MB\", \"GB\", and \"TB\".");
@@ -728,6 +724,11 @@ static const unit_conversion memory_unit_conversion_table[] =
{"MB", GUC_UNIT_KB, 1024},
{"kB", GUC_UNIT_KB, 1},
+ {"TB", GUC_UNIT_MB, 1024 * 1024},
+ {"GB", GUC_UNIT_MB, 1024},
+ {"MB", GUC_UNIT_MB, 1},
+ {"kB", GUC_UNIT_MB, -1024},
+
{"TB", GUC_UNIT_BLOCKS, (1024 * 1024 * 1024) / (BLCKSZ / 1024)},
{"GB", GUC_UNIT_BLOCKS, (1024 * 1024) / (BLCKSZ / 1024)},
{"MB", GUC_UNIT_BLOCKS, 1024 / (BLCKSZ / 1024)},
@@ -738,11 +739,6 @@ static const unit_conversion memory_unit_conversion_table[] =
{"MB", GUC_UNIT_XBLOCKS, 1024 / (XLOG_BLCKSZ / 1024)},
{"kB", GUC_UNIT_XBLOCKS, -(XLOG_BLCKSZ / 1024)},
- {"TB", GUC_UNIT_XSEGS, (1024 * 1024 * 1024) / (XLOG_SEG_SIZE / 1024)},
- {"GB", GUC_UNIT_XSEGS, (1024 * 1024) / (XLOG_SEG_SIZE / 1024)},
- {"MB", GUC_UNIT_XSEGS, -(XLOG_SEG_SIZE / (1024 * 1024))},
- {"kB", GUC_UNIT_XSEGS, -(XLOG_SEG_SIZE / 1024)},
-
{""} /* end of table marker */
};
@@ -2235,10 +2231,10 @@ static struct config_int ConfigureNamesInt[] =
{"min_wal_size", PGC_SIGHUP, WAL_CHECKPOINTS,
gettext_noop("Sets the minimum size to shrink the WAL to."),
NULL,
- GUC_UNIT_XSEGS
+ GUC_UNIT_MB
},
- &min_wal_size,
- 5, 2, INT_MAX,
+ &min_wal_size_mb,
+ DEFAULT_MIN_WAL_SEGS * 16, 2, INT_MAX,
NULL, NULL, NULL
},
@@ -2246,10 +2242,10 @@ static struct config_int ConfigureNamesInt[] =
{"max_wal_size", PGC_SIGHUP, WAL_CHECKPOINTS,
gettext_noop("Sets the WAL size that triggers a checkpoint."),
NULL,
- GUC_UNIT_XSEGS
+ GUC_UNIT_MB
},
- &max_wal_size,
- 64, 2, INT_MAX,
+ &max_wal_size_mb,
+ DEFAULT_MAX_WAL_SEGS * 16, 2, INT_MAX,
NULL, assign_max_wal_size, NULL
},
@@ -2604,9 +2600,9 @@ static struct config_int ConfigureNamesInt[] =
GUC_UNIT_XBLOCKS | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
},
&wal_segment_size,
- (XLOG_SEG_SIZE / XLOG_BLCKSZ),
- (XLOG_SEG_SIZE / XLOG_BLCKSZ),
- (XLOG_SEG_SIZE / XLOG_BLCKSZ),
+ DEFAULT_XLOG_SEG_SIZE / XLOG_BLCKSZ,
+ 16,
+ INT_MAX,
NULL, NULL, NULL
},
@@ -8066,6 +8062,8 @@ GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
case GUC_UNIT_KB:
values[2] = "kB";
break;
+ case GUC_UNIT_MB:
+ values[2] = "MB";
case GUC_UNIT_BLOCKS:
snprintf(buffer, sizeof(buffer), "%dkB", BLCKSZ / 1024);
values[2] = pstrdup(buffer);
@@ -8074,11 +8072,6 @@ GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
snprintf(buffer, sizeof(buffer), "%dkB", XLOG_BLCKSZ / 1024);
values[2] = pstrdup(buffer);
break;
- case GUC_UNIT_XSEGS:
- snprintf(buffer, sizeof(buffer), "%dMB",
- XLOG_SEG_SIZE / (1024 * 1024));
- values[2] = pstrdup(buffer);
- break;
case GUC_UNIT_MS:
values[2] = "ms";
break;
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index a02b154..8d2b842 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -233,7 +233,7 @@
#max_wal_senders = 10 # max number of walsender processes
# (change requires restart)
-#wal_keep_segments = 0 # in logfile segments, 16MB each; 0 disables
+#wal_keep_segments = 0 # in logfile segments; 0 disables
#wal_sender_timeout = 60s # in milliseconds; 0 disables
#max_replication_slots = 10 # max number of replication slots
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 9b71e00..8d6967d 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -52,6 +52,7 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <math.h>
#include <signal.h>
#include <time.h>
@@ -59,6 +60,7 @@
#include "sys/mman.h"
#endif
+#include "access/xlog_internal.h"
#include "catalog/catalog.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_class.h"
@@ -140,6 +142,8 @@ static bool sync_only = false;
static bool show_setting = false;
static bool data_checksums = false;
static char *xlog_dir = "";
+static char *str_wal_segment_size = "";
+static int wal_segment_size;
/* internal vars */
@@ -999,6 +1003,28 @@ test_config_settings(void)
}
/*
+ * Calculate the default wal_size in proper unit.
+ */
+static char *
+pretty_wal_size(int segment_count)
+{
+ int temp_val = wal_segment_size / (1024 * 1024) * segment_count;
+ char *result = malloc(10);
+
+ /*
+ * The wal_segment_size ranges from 1MB to 1GB and the default
+ * segment_count is 5 for min_wal_size and 64 for max_wal_size, so the
+ * final values can range from 5MB to 64GB.
+ */
+ if (temp_val / 1024 > 0)
+ snprintf(result, 10, "%dGB", temp_val / 1024);
+ else
+ snprintf(result, 10, "%dMB", temp_val);
+
+ return result;
+}
+
+/*
* set up all the config files
*/
static void
@@ -1042,6 +1068,15 @@ setup_config(void)
conflines = replace_token(conflines, "#port = 5432", repltok);
#endif
+ /* Set the default max and min wal size according to the wal_segment_size */
+ snprintf(repltok, sizeof(repltok), "min_wal_size = %s",
+ pretty_wal_size(DEFAULT_MIN_WAL_SEGS));
+ conflines = replace_token(conflines, "#min_wal_size = 80MB", repltok);
+
+ snprintf(repltok, sizeof(repltok), "max_wal_size = %s",
+ pretty_wal_size(DEFAULT_MAX_WAL_SEGS));
+ conflines = replace_token(conflines, "#max_wal_size = 1GB", repltok);
+
snprintf(repltok, sizeof(repltok), "lc_messages = '%s'",
escape_quotes(lc_messages));
conflines = replace_token(conflines, "#lc_messages = 'C'", repltok);
@@ -1349,6 +1384,9 @@ bootstrap_template1(void)
snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
putenv(pg_strdup(cmd));
+ snprintf(cmd, sizeof(cmd), "XLOG_SEG_SIZE=%d", wal_segment_size);
+ putenv(pg_strdup(cmd));
+
unsetenv("LC_ALL");
/* Also ensure backend isn't confused by this environment var: */
@@ -2275,6 +2313,7 @@ usage(const char *progname)
printf(_(" -U, --username=NAME database superuser name\n"));
printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
printf(_(" -X, --waldir=WALDIR location for the write-ahead log directory\n"));
+ printf(_(" --wal-segsize=SIZE size of wal segment size\n"));
printf(_("\nLess commonly used options:\n"));
printf(_(" -d, --debug generate lots of debugging output\n"));
printf(_(" -k, --data-checksums use data page checksums\n"));
@@ -2964,6 +3003,7 @@ main(int argc, char *argv[])
{"no-sync", no_argument, NULL, 'N'},
{"sync-only", no_argument, NULL, 'S'},
{"waldir", required_argument, NULL, 'X'},
+ {"wal-segsize", required_argument, NULL, 12},
{"data-checksums", no_argument, NULL, 'k'},
{NULL, 0, NULL, 0}
};
@@ -3097,6 +3137,9 @@ main(int argc, char *argv[])
case 'X':
xlog_dir = pg_strdup(optarg);
break;
+ case 12:
+ str_wal_segment_size = pg_strdup(optarg);
+ break;
default:
/* getopt_long already emitted a complaint */
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
@@ -3159,6 +3202,31 @@ main(int argc, char *argv[])
check_need_password(authmethodlocal, authmethodhost);
+ if (str_wal_segment_size == NULL || !strlen(str_wal_segment_size))
+ wal_segment_size = DEFAULT_XLOG_SEG_SIZE;
+ else
+ {
+ char *endptr;
+
+ /* Check if argument only has numeric value */
+ wal_segment_size = strtol(str_wal_segment_size, &endptr, 10);
+ if (*endptr != '\0')
+ {
+ fprintf(stderr, _("%s: --wal-segsize only accepts power 2 integer values between 1 and 1024\n"), progname);
+ exit(1);
+ }
+
+ /* Convert wal_segment_size from MB to bytes */
+ wal_segment_size = wal_segment_size * 1024 * 1024;
+
+ /* Check if wal_segment_size is valid */
+ if (!IsValidXLogSegSize(wal_segment_size))
+ {
+ fprintf(stderr, _("%s: Invalid WAL segment size %s\n"), progname, str_wal_segment_size);
+ exit(1);
+ }
+
+ }
get_restricted_token(progname);
setup_pgdata();
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c
index 0a4944d..c9db0ce 100644
--- a/src/bin/pg_basebackup/pg_basebackup.c
+++ b/src/bin/pg_basebackup/pg_basebackup.c
@@ -2388,6 +2388,9 @@ main(int argc, char **argv)
exit(1);
}
+ if (!RetrieveXLogSegSize(conn))
+ disconnect_and_exit(1);
+
/* Create transaction log symlink, if required */
if (strcmp(xlog_dir, "") != 0)
{
diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c
index 15348ad..986c4e6 100644
--- a/src/bin/pg_basebackup/pg_receivewal.c
+++ b/src/bin/pg_basebackup/pg_receivewal.c
@@ -663,6 +663,9 @@ main(int argc, char **argv)
if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
disconnect_and_exit(1);
+ if (!RetrieveXLogSegSize(conn))
+ disconnect_and_exit(1);
+
/*
* Check that there is a database associated with connection, none should
* be defined in this context.
diff --git a/src/bin/pg_basebackup/receivelog.h b/src/bin/pg_basebackup/receivelog.h
index 42e93ac..9265dc0 100644
--- a/src/bin/pg_basebackup/receivelog.h
+++ b/src/bin/pg_basebackup/receivelog.h
@@ -16,6 +16,7 @@
#include "walmethods.h"
#include "access/xlogdefs.h"
+#include "access/xlog_internal.h"
/*
* Called before trying to read more data or when a segment is
@@ -54,4 +55,5 @@ extern bool CheckServerVersionForStreaming(PGconn *conn);
extern bool ReceiveXlogStream(PGconn *conn,
StreamCtl *stream);
+uint32 XLogSegSize;
#endif /* RECEIVELOG_H */
diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c
index 507da5e..020a4ba 100644
--- a/src/bin/pg_basebackup/streamutil.c
+++ b/src/bin/pg_basebackup/streamutil.c
@@ -30,6 +30,9 @@
#define ERRCODE_DUPLICATE_OBJECT "42710"
+/* The SHOW command for replication connection was introduced in version 10 */
+#define MINIMUM_VERSION_FOR_SHOW_CMD 100000
+
const char *progname;
char *connection_string = NULL;
char *dbhost = NULL;
@@ -231,6 +234,64 @@ GetConnection(void)
}
/*
+ * From version 10, explicitly set XLogSegSize using SHOW wal_segment_size
+ * since ControlFile is not accessible here
+ */
+bool
+RetrieveXLogSegSize(PGconn *conn)
+{
+ PGresult *res;
+ char *tmp_result;
+ char xlog_unit[3];
+ int xlog_val,
+ multiplier = 1;
+
+ /* Check connection existence */
+ Assert(conn != NULL);
+
+ /* Previous versions do not need to set XLogSegSize, so return */
+ if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_SHOW_CMD)
+ return true;
+
+ res = PQexec(conn, "SHOW wal_segment_size");
+ if (PQresultStatus(res) != PGRES_TUPLES_OK)
+ {
+ fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
+ progname, "SHOW wal_segment_size", PQerrorMessage(conn));
+
+ PQclear(res);
+ return false;
+ }
+ if (PQntuples(res) != 1 || PQnfields(res) < 1)
+ {
+ fprintf(stderr,
+ _("%s: could not fetch XLogSegSize: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
+ progname, PQntuples(res), PQnfields(res), 1, 1);
+
+ PQclear(res);
+ return false;
+ }
+
+ /* wal_segment_size ranges from 1MB to 1GB */
+ tmp_result = pg_strdup(PQgetvalue(res, 0, 0));
+
+ /* Fetch the xlog value and unit from the result */
+ sscanf(tmp_result, "%d%s", &xlog_val, xlog_unit);
+
+ /* XLogSegSize should be in bytes, set the multiplier based on unit */
+ if (strcmp(xlog_unit, "MB") == 0)
+ multiplier = 1024 * 1024;
+ else if (strcmp(xlog_unit, "GB") == 0)
+ multiplier = 1024 * 1024 * 1024;
+
+ /* Set the XLogSegSize */
+ XLogSegSize = xlog_val * multiplier;
+
+ PQclear(res);
+ return true;
+}
+
+/*
* Run IDENTIFY_SYSTEM through a given connection and give back to caller
* some result information if requested:
* - System identifier
diff --git a/src/bin/pg_basebackup/streamutil.h b/src/bin/pg_basebackup/streamutil.h
index 460dcb5..8b24d74 100644
--- a/src/bin/pg_basebackup/streamutil.h
+++ b/src/bin/pg_basebackup/streamutil.h
@@ -39,6 +39,7 @@ extern bool RunIdentifySystem(PGconn *conn, char **sysid,
TimeLineID *starttli,
XLogRecPtr *startpos,
char **db_name);
+extern bool RetrieveXLogSegSize(PGconn *conn);
extern TimestampTz feGetCurrentTimestamp(void);
extern void feTimestampDifference(TimestampTz start_time, TimestampTz stop_time,
long *secs, int *microsecs);
diff --git a/src/bin/pg_controldata/pg_controldata.c b/src/bin/pg_controldata/pg_controldata.c
index 2ea8931..bbd2c1f 100644
--- a/src/bin/pg_controldata/pg_controldata.c
+++ b/src/bin/pg_controldata/pg_controldata.c
@@ -27,6 +27,8 @@
#include "pg_getopt.h"
+uint32 XLogSegSize;
+
static void
usage(const char *progname)
{
@@ -164,6 +166,9 @@ main(int argc, char *argv[])
"Either the file is corrupt, or it has a different layout than this program\n"
"is expecting. The results below are untrustworthy.\n\n"));
+ /* Set the XLogSegSize */
+ XLogSegSize = ControlFile->xlog_seg_size;
+
/*
* This slightly-chintzy coding will work as long as the control file
* timestamps are within the range of time_t; that should be the case in
diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c
index 27bd9b0..8e61782 100644
--- a/src/bin/pg_resetwal/pg_resetwal.c
+++ b/src/bin/pg_resetwal/pg_resetwal.c
@@ -56,6 +56,7 @@
#include "storage/large_object.h"
#include "pg_getopt.h"
+uint32 XLogSegSize;
static ControlFileData ControlFile; /* pg_control values */
static XLogSegNo newXlogSegNo; /* new XLOG segment # */
@@ -93,6 +94,7 @@ main(int argc, char *argv[])
char *endptr;
char *endptr2;
char *DataDir = NULL;
+ char *log_fname = NULL;
int fd;
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetwal"));
@@ -264,7 +266,12 @@ main(int argc, char *argv[])
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
- XLogFromFileName(optarg, &minXlogTli, &minXlogSegNo);
+
+ /*
+ * XLogFromFileName requires XLogSegSize which is not yet
+ * defined. Hence XLog details are set later on.
+ */
+ log_fname = pg_strdup(optarg);
break;
default:
@@ -346,6 +353,9 @@ main(int argc, char *argv[])
if (!ReadControlFile())
GuessControlValues();
+ if (log_fname != NULL)
+ XLogFromFileName(log_fname, &minXlogTli, &minXlogSegNo);
+
/*
* Also look at existing segment files to set up newXlogSegNo
*/
@@ -509,6 +519,7 @@ ReadControlFile(void)
{
/* Valid data... */
memcpy(&ControlFile, buffer, sizeof(ControlFile));
+ XLogSegSize = ControlFile.xlog_seg_size;
return true;
}
@@ -516,6 +527,7 @@ ReadControlFile(void)
progname);
/* We will use the data anyway, but treat it as guessed. */
memcpy(&ControlFile, buffer, sizeof(ControlFile));
+ XLogSegSize = ControlFile.xlog_seg_size;
guessed = true;
return true;
}
diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c
index 2014fee..47b205a 100644
--- a/src/bin/pg_rewind/pg_rewind.c
+++ b/src/bin/pg_rewind/pg_rewind.c
@@ -44,6 +44,7 @@ static ControlFileData ControlFile_target;
static ControlFileData ControlFile_source;
const char *progname;
+uint32 XLogSegSize;
/* Configuration options */
char *datadir_target = NULL;
@@ -631,6 +632,9 @@ digestControlFile(ControlFileData *ControlFile, char *src, size_t size)
memcpy(ControlFile, src, sizeof(ControlFileData));
+ /* Set XLogSegSize */
+ XLogSegSize = ControlFile->xlog_seg_size;
+
/* Additional checks on control file */
checkControlFile(ControlFile);
}
diff --git a/src/bin/pg_test_fsync/pg_test_fsync.c b/src/bin/pg_test_fsync/pg_test_fsync.c
index d65c0ab..b262ae8 100644
--- a/src/bin/pg_test_fsync/pg_test_fsync.c
+++ b/src/bin/pg_test_fsync/pg_test_fsync.c
@@ -62,7 +62,7 @@ static const char *progname;
static int secs_per_test = 5;
static int needs_unlink = 0;
-static char full_buf[XLOG_SEG_SIZE],
+static char full_buf[DEFAULT_XLOG_SEG_SIZE],
*buf,
*filename = FSYNC_FILENAME;
static struct timeval start_t,
@@ -204,7 +204,7 @@ prepare_buf(void)
int ops;
/* write random data into buffer */
- for (ops = 0; ops < XLOG_SEG_SIZE; ops++)
+ for (ops = 0; ops < DEFAULT_XLOG_SEG_SIZE; ops++)
full_buf[ops] = random();
buf = (char *) TYPEALIGN(XLOG_BLCKSZ, full_buf);
@@ -221,7 +221,7 @@ test_open(void)
if ((tmpfile = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
die("could not open output file");
needs_unlink = 1;
- if (write(tmpfile, full_buf, XLOG_SEG_SIZE) != XLOG_SEG_SIZE)
+ if (write(tmpfile, full_buf, DEFAULT_XLOG_SEG_SIZE) != DEFAULT_XLOG_SEG_SIZE)
die("write failed");
/* fsync now so that dirty buffers don't skew later tests */
diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c
index bfe44b8..909a026 100644
--- a/src/bin/pg_waldump/pg_waldump.c
+++ b/src/bin/pg_waldump/pg_waldump.c
@@ -22,9 +22,11 @@
#include "common/fe_memutils.h"
#include "getopt_long.h"
#include "rmgrdesc.h"
+#include "sys/stat.h"
static const char *progname;
+uint32 XLogSegSize;
typedef struct XLogDumpPrivate
{
@@ -114,6 +116,40 @@ verify_directory(const char *directory)
return true;
}
+ /*
+ * We can neither access the ControlFile nor connect to the server, so set
+ * XLogSegSize from WAL header
+ */
+static bool
+RetrieveXLogSegSize(char *file_path)
+{
+ int fd;
+ struct stat stat_buf;
+ bool ret = false;
+
+ if (stat(file_path, &stat_buf) == 0)
+ {
+ if ((fd = open(file_path, O_RDWR, 0)) >= 0)
+ {
+ char *buf = (char *) malloc(XLOG_BLCKSZ);
+
+ if (read(fd, buf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
+ fprintf(stderr, _("%s could not read from log segment %s"),
+ progname, file_path);
+ else
+ {
+ XLogPageHeader hdr = (XLogPageHeader) buf;
+ XLogLongPageHeader NewLongPage = (XLogLongPageHeader) hdr;
+
+ XLogSegSize = NewLongPage->xlp_seg_size;
+ ret = true;
+ }
+ }
+ close(fd);
+ }
+ return ret;
+}
+
/*
* Split a pathname as dirname(1) and basename(1) would.
*
@@ -902,8 +938,10 @@ main(int argc, char **argv)
{
char *directory = NULL;
char *fname = NULL;
+ char full_path[MAXPGPATH];
int fd;
XLogSegNo segno;
+ struct stat fst;
split_path(argv[optind], &directory, &fname);
@@ -921,6 +959,22 @@ main(int argc, char **argv)
fatal_error("could not open file \"%s\"", fname);
close(fd);
+ if (private.inpath != NULL)
+ sprintf(full_path, "%s/%s", private.inpath, fname);
+ else
+ strcpy(full_path, fname);
+
+ stat(full_path, &fst);
+
+ if (!RetrieveXLogSegSize(full_path))
+ {
+ fprintf(stderr,
+ _("%s: could not set WAL segment size for %s\n"),
+ progname, full_path);
+
+ return EXIT_FAILURE;
+ }
+
/* parse position from file */
XLogFromFileName(fname, &private.timeline, &segno);
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index 104ee7d..e2a5e47 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -94,8 +94,9 @@ extern PGDLLIMPORT XLogRecPtr XactLastCommitEnd;
extern bool reachedConsistency;
/* these variables are GUC parameters related to XLOG */
-extern int min_wal_size;
-extern int max_wal_size;
+extern int wal_segment_size;
+extern int min_wal_size_mb;
+extern int max_wal_size_mb;
extern int wal_keep_segments;
extern int XLOGbuffers;
extern int XLogArchiveTimeout;
diff --git a/src/include/access/xlog_internal.h b/src/include/access/xlog_internal.h
index b8b15f1..d31ccb6d 100644
--- a/src/include/access/xlog_internal.h
+++ b/src/include/access/xlog_internal.h
@@ -89,7 +89,25 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
* The XLOG is split into WAL segments (physical files) of the size indicated
* by XLOG_SEG_SIZE.
*/
-#define XLogSegSize ((uint32) XLOG_SEG_SIZE)
+
+extern uint32 XLogSegSize;
+#define XLOG_SEG_SIZE XLogSegSize
+
+/* XLogSegSize can range from 1MB to 1GB */
+#define XLogSegMinSize 1024 * 1024
+#define XLogSegMaxSize 1024 * 1024 * 1024
+
+/* Default number of min and max wal segments */
+#define DEFAULT_MIN_WAL_SEGS 5
+#define DEFAULT_MAX_WAL_SEGS 64
+
+/* Check if the given size is a valid XLogSegSize */
+#define IsPower2(x) ((x & (x-1)) == 0)
+#define IsValidXLogSegSize(size) \
+ ((size % (1024 * 1024) == 0) && \
+ IsPower2(size / (1024 * 1024)) && \
+ (size >= XLogSegMinSize && size <= XLogSegMaxSize))
+
#define XLogSegmentsPerXLogId (UINT64CONST(0x100000000) / XLOG_SEG_SIZE)
#define XLogSegNoOffsetToRecPtr(segno, offset, dest) \
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 5bcd8a1..6b89d47 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -893,13 +893,6 @@
*/
#undef XLOG_BLCKSZ
-/* XLOG_SEG_SIZE is the size of a single WAL file. This must be a power of 2
- and larger than XLOG_BLCKSZ (preferably, a great deal larger than
- XLOG_BLCKSZ). Changing XLOG_SEG_SIZE requires an initdb. */
-#undef XLOG_SEG_SIZE
-
-
-
/* Number of bits in a file offset, on hosts where this is settable. */
#undef _FILE_OFFSET_BITS
diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h
index f3b3529..9158f77 100644
--- a/src/include/pg_config_manual.h
+++ b/src/include/pg_config_manual.h
@@ -14,6 +14,12 @@
*/
/*
+ * This default value is used for wal_segment_size during initdb when no other
+ * value is specified with --walsegsize option.
+ */
+#define DEFAULT_XLOG_SEG_SIZE (16*1024*1024)
+
+/*
* Maximum length for identifiers (e.g. table names, column names,
* function names). Names actually are limited to one less byte than this,
* because the length must include a trailing zero byte.
diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h
index 7dd3780..87d0741 100644
--- a/src/include/utils/guc.h
+++ b/src/include/utils/guc.h
@@ -218,7 +218,7 @@ typedef enum
#define GUC_UNIT_KB 0x1000 /* value is in kilobytes */
#define GUC_UNIT_BLOCKS 0x2000 /* value is in blocks */
#define GUC_UNIT_XBLOCKS 0x3000 /* value is in xlog blocks */
-#define GUC_UNIT_XSEGS 0x4000 /* value is in xlog segments */
+#define GUC_UNIT_MB 0x4000 /* value is in megabytes */
#define GUC_UNIT_MEMORY 0xF000 /* mask for size-related units */
#define GUC_UNIT_MS 0x10000 /* value is in milliseconds */
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index ff9064f..3fa8ff1 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -178,7 +178,6 @@ s{PG_VERSION_STR "[^"]+"}{__STRINGIFY(x) #x\n#define __STRINGIFY2(z) __STRINGIFY
1024, "\n";
print O "#define XLOG_BLCKSZ ",
1024 * $self->{options}->{wal_blocksize}, "\n";
- print O "#define XLOG_SEG_SIZE (", $self->{options}->{wal_segsize},
" * 1024 * 1024)\n";
if ($self->{options}->{float4byval})