Skip to content

Commit 0c56c61

Browse files
nishamoondCommitfest Bot
authored andcommitted
Introduce inactive_timeout based replication slot invalidation
Tools that create replication slots (e.g., for migrations or upgrades) may fail to remove them if an error occurs, leaving behind unused slots that take up space and resources. Manually cleaning them up can be tedious and error-prone, and without intervention, these lingering slots can cause unnecessary WAL retention and system bloat. Till now, postgres has the ability to invalidate inactive replication slots based on the amount of WAL (set via max_slot_wal_keep_size GUC) that will be needed for the slots in case they become active. However, setting an optimal value for this is tricky since the amount of WAL a database generates, and the allocated storage per instance will vary greatly in production. A high value may allow orphaned slots to persist longer than necessary, leading to system bloat by retaining WAL unnecessarily. This commit introduces idle_replication_slot_timeout, a simpler and more intuitive way to manage inactive slots. Instead of relying on WAL size, users can set a time limit (e.g., 1 or 2 or n days), after which slots that remain idle for longer than this amount of time are automatically invalidated during checkpoints. Note that the idle timeout invalidation mechanism is not applicable for slots that do not reserve WAL or for slots on the standby server that are being synced from the primary server (i.e., standby slots having 'synced' field 'true'). Synced slots are always considered to be inactive because they don't perform logical decoding to produce changes.
1 parent 217919d commit 0c56c61

File tree

15 files changed

+368
-86
lines changed

15 files changed

+368
-86
lines changed

doc/src/sgml/config.sgml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4429,6 +4429,46 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
44294429
</listitem>
44304430
</varlistentry>
44314431

4432+
<varlistentry id="guc-idle-replication-slot-timeout" xreflabel="idle_replication_slot_timeout">
4433+
<term><varname>idle_replication_slot_timeout</varname> (<type>integer</type>)
4434+
<indexterm>
4435+
<primary><varname>idle_replication_slot_timeout</varname> configuration parameter</primary>
4436+
</indexterm>
4437+
</term>
4438+
<listitem>
4439+
<para>
4440+
Invalidate replication slots that have remained idle longer than this
4441+
duration. If this value is specified without units, it is taken as
4442+
minutes. A value of zero (the default) disables the idle timeout
4443+
invalidation mechanism. This parameter can only be set in the
4444+
<filename>postgresql.conf</filename> file or on the server command
4445+
line.
4446+
</para>
4447+
4448+
<para>
4449+
Slot invalidation due to idle timeout occurs during checkpoint.
4450+
Because checkpoints happen at <varname>checkpoint_timeout</varname>
4451+
intervals, there can be some lag between when the
4452+
<varname>idle_replication_slot_timeout</varname> was exceeded and when
4453+
the slot invalidation is triggered at the next checkpoint.
4454+
To avoid such lags, users can force a checkpoint to promptly invalidate
4455+
inactive slots. The duration of slot inactivity is calculated using the
4456+
slot's <link linkend="view-pg-replication-slots">pg_replication_slots</link>.<structfield>inactive_since</structfield>
4457+
value.
4458+
</para>
4459+
4460+
<para>
4461+
Note that the idle timeout invalidation mechanism is not applicable
4462+
for slots that do not reserve WAL or for slots on the standby server
4463+
that are being synced from the primary server (i.e., standby slots
4464+
having <link linkend="view-pg-replication-slots">pg_replication_slots</link>.<structfield>synced</structfield>
4465+
value <literal>true</literal>). Synced slots are always considered to
4466+
be inactive because they don't perform logical decoding to produce
4467+
changes.
4468+
</para>
4469+
</listitem>
4470+
</varlistentry>
4471+
44324472
<varlistentry id="guc-wal-sender-timeout" xreflabel="wal_sender_timeout">
44334473
<term><varname>wal_sender_timeout</varname> (<type>integer</type>)
44344474
<indexterm>

doc/src/sgml/logical-replication.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,11 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
23902390
plus some reserve for table synchronization.
23912391
</para>
23922392

2393+
<para>
2394+
Logical replication slots are also affected by
2395+
<link linkend="guc-idle-replication-slot-timeout"><varname>idle_replication_slot_timeout</varname></link>.
2396+
</para>
2397+
23932398
<para>
23942399
<link linkend="guc-max-wal-senders"><varname>max_wal_senders</varname></link>
23952400
should be set to at least the same as

doc/src/sgml/system-views.sgml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,13 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
26192619
perform logical decoding. It is set only for logical slots.
26202620
</para>
26212621
</listitem>
2622+
<listitem>
2623+
<para>
2624+
<literal>idle_timeout</literal> means that the slot has remained
2625+
idle longer than the configured
2626+
<xref linkend="guc-idle-replication-slot-timeout"/> duration.
2627+
</para>
2628+
</listitem>
26222629
</itemizedlist>
26232630
</para></entry>
26242631
</row>

src/backend/access/transam/xlog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7337,7 +7337,7 @@ CreateCheckPoint(int flags)
73377337
*/
73387338
XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size);
73397339
KeepLogSeg(recptr, &_logSegNo);
7340-
if (InvalidateObsoleteReplicationSlots(RS_INVAL_WAL_REMOVED,
7340+
if (InvalidateObsoleteReplicationSlots(RS_INVAL_WAL_REMOVED | RS_INVAL_IDLE_TIMEOUT,
73417341
_logSegNo, InvalidOid,
73427342
InvalidTransactionId))
73437343
{
@@ -7792,7 +7792,7 @@ CreateRestartPoint(int flags)
77927792
replayPtr = GetXLogReplayRecPtr(&replayTLI);
77937793
endptr = (receivePtr < replayPtr) ? replayPtr : receivePtr;
77947794
KeepLogSeg(endptr, &_logSegNo);
7795-
if (InvalidateObsoleteReplicationSlots(RS_INVAL_WAL_REMOVED,
7795+
if (InvalidateObsoleteReplicationSlots(RS_INVAL_WAL_REMOVED | RS_INVAL_IDLE_TIMEOUT,
77967796
_logSegNo, InvalidOid,
77977797
InvalidTransactionId))
77987798
{

0 commit comments

Comments
 (0)