skip to content

The directory holding a database's write-ahead log segments keeps growing until the disk is nearly full, even though the database's data size is stable. What causes log segments to accumulate, and how do you reason about it?

level: seniorimportance: should knowfreq 44%

answer

  1. segments recycled when no consumer needs them
  2. abandoned replication slot = unbounded retention
  3. idle-in-transaction pins the log
  4. archiver failing → nothing recycled
  5. full log disk = writes stop, not just disk alert

basics

~20 s

Log segments are recycled only once nothing still needs them. Something is holding a required position: an unfinished checkpoint, a long-running transaction, a lagging or disconnected replica, a failing archive process, or a stalled backup. Find the oldest required log position and its owner.

solid answer

~60 s

The log is written as fixed-size segment files and normally recycled: once every change in a segment is safely in the data files and nobody else needs those bytes, the file is reused or deleted. Accumulation means some consumer is pinning an old position. The usual holders: - **Checkpoint not progressing** — nothing has pushed dirty pages out, so old log is still needed for redo. - **A very old open transaction** — in engines that keep undo in the log, its records cannot be discarded. - **A replica or replication slot that is behind or gone** — the primary retains log the consumer has not confirmed, and a forgotten slot retains it forever. - **Log archiving failing** — segments are kept until successfully archived; a broken destination means retention grows without bound. - **A long-running backup** holding a start position. The method is the same regardless of engine: find the oldest log position still required, identify which consumer requires it, and fix or remove that consumer. A full log disk usually stops writes entirely, so this is an availability incident, not just a space one.

go deeper

for a junior

Know that the log is separate from the data files, is recycled in segments, and can fill a disk on its own.

for a middle

List the common holders — checkpoint, long transaction, lagging replica, archiving — and know the fix is to address the holder.

for a senior

Run the diagnosis: find the oldest required position, attribute it to a consumer, remediate safely, and know that manual deletion breaks recovery and replicas.

for a principal

Set the policy: retention caps versus guaranteed catch-up, log-volume monitoring and alerting standards, filesystem sizing for bulk-load bursts, and timeouts that prevent abandoned transactions and consumers in the first place.

## How the log is stored The write-ahead log is not one endless file. It is a sequence of fixed-size segment files written in order. Once the engine is certain a segment is no longer needed, the file is deleted or (more often) renamed and reused for future writes, so steady-state log space is roughly constant. \"No longer needed\" is the whole question. A segment is retained while any of several consumers still requires the bytes in it. ## The consumers that pin log **Crash recovery.** Redo must start from the last checkpoint, so every segment from that point forward must exist. If checkpoints are not completing — because the interval or size threshold is huge, because the checkpointer is starved by I/O, or because it is blocked — the required start position stops advancing and the log piles up. Conversely, a long checkpoint interval is a deliberate tradeoff: less write amplification, more log retained, longer recovery. **Open transactions.** In engines that keep undo information in the same log, a transaction open for hours pins everything from its first change onward, because rollback might still need it. Even in engines with separate undo storage, an old transaction pins that structure instead and blocks reclamation there. \"Idle in transaction\" sessions — an application that began a transaction and then went to sleep, often through a connection pool — are the classic cause. **Replication consumers.** A physical or logical replica reports how far it has received and applied. The primary keeps log until the consumer confirms. Where the retention is expressed as a durable, named subscription (a slot), retention is unbounded by design: if that replica dies and nobody removes the slot, the primary retains log forever until the disk fills. This is the single most common cause of a log disk filling in a replicated deployment. Where retention is instead expressed as \"keep at most N segments\", you get the opposite failure mode: the replica falls too far behind, the segment it needs is gone, and it must be rebuilt. **Archiving / continuous backup.** If the engine is configured to ship each segment to archival storage before recycling it, a broken destination (bad credentials, full bucket, wrong path, hung command) pauses recycling entirely while writes continue. Retention grows at exactly the write rate. **Backups in progress.** A base backup pins the log from its start position so the backup can be made consistent. A backup that hangs for a day pins a day of log. ## Diagnosing it The portable method has three steps. 1. **Find the oldest required log position.** Every engine exposes this: the checkpoint's redo start, the oldest slot/consumer position, the oldest active transaction's start position, the last archived segment. 2. **Attribute it.** Whichever of those is furthest behind is the culprit; the distance from the current write position tells you how much log it is pinning. 3. **Fix the owner, not the symptom.** Reconnect or drop the abandoned replica/slot; kill or fix the ancient open transaction; repair the archive destination; let the backup finish or abort it. Deleting segment files by hand is destructive — it can make the database unrecoverable and break every consumer at once. Emergency space: if the disk is truly about to fill, buying time usually means removing the pinning consumer (dropping a dead slot is safe and instant) or adding space, not deleting log. ## Why it is urgent When the log device fills, the engine cannot write log records, and since no change may proceed without logging, writes stop. Many engines shut down or enter a read-only-ish panic state. So log growth is a countdown to an outage, and it is usually silent until the last few percent — which is why the right monitoring is \"log retention size and oldest required position\", alerted on trend, not just \"disk free\". ## Prevention Monitor retained log volume and the lag of every consumer in log bytes. Cap or alert on any durable retention mechanism (slot lag limits, maximum retention size). Alert on transactions open longer than a few minutes, and set server-side timeouts for idle-in-transaction sessions. Monitor archiving success as a first-class signal. Size the log filesystem for the worst realistic burst — a bulk load or index build can generate far more log per hour than the steady-state workload. ## Interview framing \"Segments are recycled when nothing needs them. Growth means something needs them: checkpoint, old transaction, lagging or abandoned replication consumer, failing archiver, or a running backup. Find the oldest required position, find its owner, fix that.\"

  • Why is manually deleting old log segment files to free space a dangerous fix?
    Those segments may be exactly what crash recovery, a replica, or point-in-time restore still needs. Deleting them can leave the database unable to start after a crash, force a full rebuild of every replica, and silently break the backup chain so that recovery to any recent point becomes impossible. The correct move is to remove or repair the consumer that is pinning the log.
  • What is the tradeoff between retention expressed as a durable slot per replica and retention expressed as 'keep at most N segments'?
    A durable slot guarantees the replica can always catch up, because the primary keeps whatever it has not consumed — at the risk of unbounded retention if that replica disappears. A fixed segment cap bounds disk usage but lets a slow or disconnected replica fall off the end and require a rebuild. Mature setups use slots with an explicit maximum retention size, plus alerting on consumer lag, so neither failure mode is silent.

saying these in an interview costs you the question

  • Reaching straight for deleting log files to free space
  • Assuming log size tracks database size
  • Not knowing that an abandoned replication slot or subscription retains log indefinitely
  • Ignoring 'idle in transaction' sessions as a cause
  • Treating a full log disk as a disk-space ticket rather than an imminent write outage

context