Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f5dff45

Browse files
committed
Rename wal_keep_segments to wal_keep_size.
max_slot_wal_keep_size that was added in v13 and wal_keep_segments are the GUC parameters to specify how much WAL files to retain for the standby servers. While max_slot_wal_keep_size accepts the number of bytes of WAL files, wal_keep_segments accepts the number of WAL files. This difference of setting units between those similar parameters could be confusing to users. To alleviate this situation, this commit renames wal_keep_segments to wal_keep_size, and make users specify the WAL size in it instead of the number of WAL files. There was also the idea to rename max_slot_wal_keep_size to max_slot_wal_keep_segments, in the discussion. But we have been moving away from measuring in segments, for example, checkpoint_segments was replaced by max_wal_size. So we concluded to rename wal_keep_segments to wal_keep_size. Back-patch to v13 where max_slot_wal_keep_size was added. Author: Fujii Masao Reviewed-by: Álvaro Herrera, Kyotaro Horiguchi, David Steele Discussion: https://postgr.es/m/574b4ea3-e0f9-b175-ead2-ebea7faea855@oss.nttdata.com
1 parent 4a1ae21 commit f5dff45

File tree

12 files changed

+58
-48
lines changed

12 files changed

+58
-48
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11249,7 +11249,7 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
1124911249
<para><literal>extended</literal> means
1125011250
that <varname>max_wal_size</varname> is exceeded but the files are
1125111251
still retained, either by the replication slot or
11252-
by <varname>wal_keep_segments</varname>.
11252+
by <varname>wal_keep_size</varname>.
1125311253
</para>
1125411254
</listitem>
1125511255
<listitem>

doc/src/sgml/config.sgml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,7 +3124,7 @@ include_dir 'conf.d'
31243124
checkpoints. This is a soft limit; WAL size can exceed
31253125
<varname>max_wal_size</varname> under special circumstances, such as
31263126
heavy load, a failing <varname>archive_command</varname>, or a high
3127-
<varname>wal_keep_segments</varname> setting.
3127+
<varname>wal_keep_size</varname> setting.
31283128
If this value is specified without units, it is taken as megabytes.
31293129
The default is 1 GB.
31303130
Increasing this parameter can increase the amount of time needed for
@@ -3751,36 +3751,37 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
37513751
</listitem>
37523752
</varlistentry>
37533753

3754-
<varlistentry id="guc-wal-keep-segments" xreflabel="wal_keep_segments">
3755-
<term><varname>wal_keep_segments</varname> (<type>integer</type>)
3754+
<varlistentry id="guc-wal-keep-size" xreflabel="wal_keep_size">
3755+
<term><varname>wal_keep_size</varname> (<type>integer</type>)
37563756
<indexterm>
3757-
<primary><varname>wal_keep_segments</varname> configuration parameter</primary>
3757+
<primary><varname>wal_keep_size</varname> configuration parameter</primary>
37583758
</indexterm>
37593759
</term>
37603760
<listitem>
37613761
<para>
3762-
Specifies the minimum number of past log file segments kept in the
3762+
Specifies the minimum size of past log file segments kept in the
37633763
<filename>pg_wal</filename>
37643764
directory, in case a standby server needs to fetch them for streaming
3765-
replication. Each segment is normally 16 megabytes. If a standby
3765+
replication. If a standby
37663766
server connected to the sending server falls behind by more than
3767-
<varname>wal_keep_segments</varname> segments, the sending server might remove
3768-
a WAL segment still needed by the standby, in which case the
3767+
<varname>wal_keep_size</varname> megabytes, the sending server might
3768+
remove a WAL segment still needed by the standby, in which case the
37693769
replication connection will be terminated. Downstream connections
37703770
will also eventually fail as a result. (However, the standby
37713771
server can recover by fetching the segment from archive, if WAL
37723772
archiving is in use.)
37733773
</para>
37743774

37753775
<para>
3776-
This sets only the minimum number of segments retained in
3776+
This sets only the minimum size of segments retained in
37773777
<filename>pg_wal</filename>; the system might need to retain more segments
37783778
for WAL archival or to recover from a checkpoint. If
3779-
<varname>wal_keep_segments</varname> is zero (the default), the system
3779+
<varname>wal_keep_size</varname> is zero (the default), the system
37803780
doesn't keep any extra segments for standby purposes, so the number
37813781
of old WAL segments available to standby servers is a function of
37823782
the location of the previous checkpoint and status of WAL
37833783
archiving.
3784+
If this value is specified without units, it is taken as megabytes.
37843785
This parameter can only be set in the
37853786
<filename>postgresql.conf</filename> file or on the server command line.
37863787
</para>

doc/src/sgml/high-availability.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ archive_cleanup_command = 'pg_archivecleanup /path/to/archive %r'
785785
archiving, the server might recycle old WAL segments before the standby
786786
has received them. If this occurs, the standby will need to be
787787
reinitialized from a new base backup. You can avoid this by setting
788-
<varname>wal_keep_segments</varname> to a value large enough to ensure that
788+
<varname>wal_keep_size</varname> to a value large enough to ensure that
789789
WAL segments are not recycled too early, or by configuring a replication
790790
slot for the standby. If you set up a WAL archive that's accessible from
791791
the standby, these solutions are not required, since the standby can
@@ -929,7 +929,7 @@ primary_conninfo = 'host=192.168.1.50 port=5432 user=foo password=foopass'
929929
</para>
930930
<para>
931931
In lieu of using replication slots, it is possible to prevent the removal
932-
of old WAL segments using <xref linkend="guc-wal-keep-segments"/>, or by
932+
of old WAL segments using <xref linkend="guc-wal-keep-size"/>, or by
933933
storing the segments in an archive using
934934
<xref linkend="guc-archive-command"/>.
935935
However, these methods often result in retaining more WAL segments than

doc/src/sgml/ref/pg_basebackup.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ PostgreSQL documentation
305305
<para>
306306
The write-ahead log files are collected at the end of the backup.
307307
Therefore, it is necessary for the
308-
<xref linkend="guc-wal-keep-segments"/> parameter to be set high
308+
<xref linkend="guc-wal-keep-size"/> parameter to be set high
309309
enough that the log is not removed before the end of the backup.
310310
If the log has been rotated when it's time to transfer it, the
311311
backup will fail and be unusable.

doc/src/sgml/wal.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,8 @@
578578

579579
<para>
580580
Independently of <varname>max_wal_size</varname>,
581-
<xref linkend="guc-wal-keep-segments"/> + 1 most recent WAL files are
581+
the most recent <xref linkend="guc-wal-keep-size"/> megabytes of
582+
WAL files plus one additional WAL file are
582583
kept at all times. Also, if WAL archiving is used, old segments can not be
583584
removed or recycled until they are archived. If WAL archiving cannot keep up
584585
with the pace that WAL is generated, or if <varname>archive_command</varname>

src/backend/access/transam/xlog.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ extern uint32 bootstrap_data_checksum_version;
8888
/* User-settable parameters */
8989
int max_wal_size_mb = 1024; /* 1 GB */
9090
int min_wal_size_mb = 80; /* 80 MB */
91-
int wal_keep_segments = 0;
91+
int wal_keep_size_mb = 0;
9292
int XLOGbuffers = -1;
9393
int XLogArchiveTimeout = 0;
9494
int XLogArchiveMode = ARCHIVE_MODE_OFF;
@@ -9523,7 +9523,7 @@ GetWALAvailability(XLogRecPtr targetLSN)
95239523

95249524
/*
95259525
* Calculate the oldest segment currently reserved by all slots,
9526-
* considering wal_keep_segments and max_slot_wal_keep_size. Initialize
9526+
* considering wal_keep_size and max_slot_wal_keep_size. Initialize
95279527
* oldestSlotSeg to the current segment.
95289528
*/
95299529
currpos = GetXLogWriteRecPtr();
@@ -9574,9 +9574,9 @@ GetWALAvailability(XLogRecPtr targetLSN)
95749574

95759575
/*
95769576
* Retreat *logSegNo to the last segment that we need to retain because of
9577-
* either wal_keep_segments or replication slots.
9577+
* either wal_keep_size or replication slots.
95789578
*
9579-
* This is calculated by subtracting wal_keep_segments from the given xlog
9579+
* This is calculated by subtracting wal_keep_size from the given xlog
95809580
* location, recptr and by making sure that that result is below the
95819581
* requirement of replication slots. For the latter criterion we do consider
95829582
* the effects of max_slot_wal_keep_size: reserve at most that much space back
@@ -9614,14 +9614,20 @@ KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
96149614
}
96159615
}
96169616

9617-
/* but, keep at least wal_keep_segments if that's set */
9618-
if (wal_keep_segments > 0 && currSegNo - segno < wal_keep_segments)
9617+
/* but, keep at least wal_keep_size if that's set */
9618+
if (wal_keep_size_mb > 0)
96199619
{
9620-
/* avoid underflow, don't go below 1 */
9621-
if (currSegNo <= wal_keep_segments)
9622-
segno = 1;
9623-
else
9624-
segno = currSegNo - wal_keep_segments;
9620+
uint64 keep_segs;
9621+
9622+
keep_segs = ConvertToXSegs(wal_keep_size_mb, wal_segment_size);
9623+
if (currSegNo - segno < keep_segs)
9624+
{
9625+
/* avoid underflow, don't go below 1 */
9626+
if (currSegNo <= keep_segs)
9627+
segno = 1;
9628+
else
9629+
segno = currSegNo - keep_segs;
9630+
}
96259631
}
96269632

96279633
/* don't delete WAL segments newer than the calculated segment */
@@ -11336,7 +11342,7 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
1133611342
* If archiving is enabled, wait for all the required WAL files to be
1133711343
* archived before returning. If archiving isn't enabled, the required WAL
1133811344
* needs to be transported via streaming replication (hopefully with
11339-
* wal_keep_segments set high enough), or some more exotic mechanism like
11345+
* wal_keep_size set high enough), or some more exotic mechanism like
1134011346
* polling and copying files from pg_wal with script. We have no knowledge
1134111347
* of those mechanisms, so it's up to the user to ensure that he gets all
1134211348
* the required WAL.

src/backend/replication/slotfuncs.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,19 +413,20 @@ pg_get_replication_slots(PG_FUNCTION_ARGS)
413413
else
414414
{
415415
XLogSegNo targetSeg;
416-
XLogSegNo keepSegs;
416+
uint64 slotKeepSegs;
417+
uint64 keepSegs;
417418
XLogSegNo failSeg;
418419
XLogRecPtr failLSN;
419420

420421
XLByteToSeg(slot_contents.data.restart_lsn, targetSeg, wal_segment_size);
421422

422-
/* determine how many segments slots can be kept by slots ... */
423-
keepSegs = XLogMBVarToSegs(max_slot_wal_keep_size_mb, wal_segment_size);
424-
/* ... and override by wal_keep_segments as needed */
425-
keepSegs = Max(keepSegs, wal_keep_segments);
423+
/* determine how many segments slots can be kept by slots */
424+
slotKeepSegs = XLogMBVarToSegs(max_slot_wal_keep_size_mb, wal_segment_size);
425+
/* ditto for wal_keep_size */
426+
keepSegs = XLogMBVarToSegs(wal_keep_size_mb, wal_segment_size);
426427

427428
/* if currpos reaches failLSN, we lose our segment */
428-
failSeg = targetSeg + keepSegs + 1;
429+
failSeg = targetSeg + Max(slotKeepSegs, keepSegs) + 1;
429430
XLogSegNoOffsetToRecPtr(failSeg, 0, wal_segment_size, failLSN);
430431

431432
values[i++] = Int64GetDatum(failLSN - currlsn);

src/backend/utils/misc/guc.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,12 +2640,13 @@ static struct config_int ConfigureNamesInt[] =
26402640
},
26412641

26422642
{
2643-
{"wal_keep_segments", PGC_SIGHUP, REPLICATION_SENDING,
2644-
gettext_noop("Sets the number of WAL files held for standby servers."),
2645-
NULL
2643+
{"wal_keep_size", PGC_SIGHUP, REPLICATION_SENDING,
2644+
gettext_noop("Sets the size of WAL files held for standby servers."),
2645+
NULL,
2646+
GUC_UNIT_MB
26462647
},
2647-
&wal_keep_segments,
2648-
0, 0, INT_MAX,
2648+
&wal_keep_size_mb,
2649+
0, 0, MAX_KILOBYTES,
26492650
NULL, NULL, NULL
26502651
},
26512652

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@
288288

289289
#max_wal_senders = 10 # max number of walsender processes
290290
# (change requires restart)
291-
#wal_keep_segments = 0 # in logfile segments; 0 disables
291+
#wal_keep_size = 0 # in megabytes; 0 disables
292292
#max_slot_wal_keep_size = -1 # in megabytes; -1 disables
293293
#wal_sender_timeout = 60s # in milliseconds; 0 disables
294294

src/bin/pg_rewind/t/RewindTest.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ sub setup_cluster
135135
extra => $extra,
136136
auth_extra => [ '--create-role', 'rewind_user' ]);
137137

138-
# Set wal_keep_segments to prevent WAL segment recycling after enforced
138+
# Set wal_keep_size to prevent WAL segment recycling after enforced
139139
# checkpoints in the tests.
140140
$node_master->append_conf(
141141
'postgresql.conf', qq(
142-
wal_keep_segments = 20
142+
wal_keep_size = 320MB
143143
));
144144
return;
145145
}

src/include/access/xlog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ extern bool reachedConsistency;
107107
extern int wal_segment_size;
108108
extern int min_wal_size_mb;
109109
extern int max_wal_size_mb;
110-
extern int wal_keep_segments;
110+
extern int wal_keep_size_mb;
111111
extern int max_slot_wal_keep_size_mb;
112112
extern int XLOGbuffers;
113113
extern int XLogArchiveTimeout;
@@ -272,7 +272,7 @@ typedef enum WALAvailability
272272
WALAVAIL_INVALID_LSN, /* parameter error */
273273
WALAVAIL_RESERVED, /* WAL segment is within max_wal_size */
274274
WALAVAIL_EXTENDED, /* WAL segment is reserved by a slot or
275-
* wal_keep_segments */
275+
* wal_keep_size */
276276
WALAVAIL_UNRESERVED, /* no longer reserved, but not removed yet */
277277
WALAVAIL_REMOVED /* WAL segment has been removed */
278278
} WALAvailability;

src/test/recovery/t/019_replslot_limit.pl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,19 @@
116116
$node_master->wait_for_catchup($node_standby, 'replay', $start_lsn);
117117
$node_standby->stop;
118118

119-
# wal_keep_segments overrides max_slot_wal_keep_size
119+
# wal_keep_size overrides max_slot_wal_keep_size
120120
$result = $node_master->safe_psql('postgres',
121-
"ALTER SYSTEM SET wal_keep_segments to 8; SELECT pg_reload_conf();");
121+
"ALTER SYSTEM SET wal_keep_size to '8MB'; SELECT pg_reload_conf();");
122122
# Advance WAL again then checkpoint, reducing remain by 6 MB.
123123
advance_wal($node_master, 6);
124124
$result = $node_master->safe_psql('postgres',
125125
"SELECT wal_status as remain FROM pg_replication_slots WHERE slot_name = 'rep1'"
126126
);
127127
is($result, "extended",
128-
'check that wal_keep_segments overrides max_slot_wal_keep_size');
129-
# restore wal_keep_segments
128+
'check that wal_keep_size overrides max_slot_wal_keep_size');
129+
# restore wal_keep_size
130130
$result = $node_master->safe_psql('postgres',
131-
"ALTER SYSTEM SET wal_keep_segments to 0; SELECT pg_reload_conf();");
131+
"ALTER SYSTEM SET wal_keep_size to 0; SELECT pg_reload_conf();");
132132

133133
# The standby can reconnect to master
134134
$node_standby->start;

0 commit comments

Comments
 (0)