Introduction
PostgreSQL replication lag is a common concern for teams running production databases. Replication lag means the standby or replica database is behind the primary database and has not yet applied all recent changes. When this lag suddenly spikes in production, it can impact read replicas, analytics queries, reporting systems, and even failover reliability.
In simple terms, replication lag occurs when the primary database produces changes faster than the replica can receive and apply them. In production systems with real traffic, even small changes in workload, configuration, or infrastructure can cause sudden lag spikes. This article explains the most common reasons behind these spikes, using simple language and real-world examples.
How PostgreSQL Replication Works
PostgreSQL usually uses streaming replication in production setups. The primary database writes changes to WAL (Write-Ahead Log) files. These WAL records are sent to replica servers, which then replay them to stay in sync.
Replication lag increases when:
WAL is generated very fast on the primary
WAL is slow to transfer over the network
WAL is slow to apply to the replica
Understanding where the slowdown happens helps identify the root cause.
Sudden Increase in Write Traffic
One of the most common reasons for a replication lag spike is a sudden increase in write operations on the primary database.
Examples include:
Bulk data imports
Large batch jobs
Backfill scripts
Unexpected traffic spikes
Example scenario:
An e-commerce platform runs a nightly job to update product prices for thousands of rows. This generates a large amount of WAL data in a short time, overwhelming the replica.
You can observe WAL generation using:
SELECT pg_current_wal_lsn();
If WAL is generated faster than the replica can replay it, lag increases quickly.
Long-Running Transactions on the Primary
Long-running transactions can silently cause replication lag issues.
When a transaction stays open for a long time:
WAL cannot be fully recycled
Replicas may wait to apply changes
Vacuum processes are delayed
Example:
A reporting query runs for hours without committing. During this time, the replica keeps receiving WAL but struggles to apply changes efficiently.
To find long-running transactions:
SELECT pid, now() - xact_start AS transaction_duration, query
FROM pg_stat_activity
WHERE state = 'active' AND xact_start IS NOT NULL;
Slow Disk I/O on the Replica
Even if the primary is fast, replication lag can spike if the replica has slow disk performance.
Common causes include:
Shared storage contention
Disk nearing capacity
Cloud volume performance limits
Example:
After scaling up traffic, the replica disk reaches high I/O utilization. WAL replay slows down, causing lag even though network transfer is healthy.
Disk bottlenecks are one of the most overlooked causes of replication lag.
Network Latency or Packet Loss
Replication relies on continuous network streaming. Any network issue between the primary and replica can immediately increase lag.
Possible network problems:
Temporary packet loss
Increased latency between regions
Firewall or load balancer issues

Join the conversation! Your thoughts help the community grow.