Skip to content

Widhian Bramantya

coding is an art form

Menu
  • About Me
Menu
postgresql

Understanding PostgreSQL WAL, Slot, Publication, LSN, and Replication Lag

Posted on October 8, 2025October 8, 2025 by admin

Publication – The Data Publisher (for Logical Replication)

Publication is used in logical replication, where you choose which tables and actions to send to replicas.

On the primary:

CREATE PUBLICATION my_pub FOR TABLE users, orders;

On the replica:

CREATE SUBSCRIPTION my_sub
CONNECTION 'host=10.0.0.1 dbname=app user=replicator password=secret'
PUBLICATION my_pub;

So, Publication = “what to send”
and Subscription = “who receives it”

This allows selective replication — not the entire database, just specific tables or columns.

Analogy

Think of Publication like a newsletter system:

  • The primary is the publisher
  • The replica is the subscriber
  • WAL is the content being delivered
  • The replication slot makes sure no one misses an issue

Replication Lag – The Delay

Replication Lag means the replica is behind the primary, it hasn’t yet replayed all WAL changes that were written on the primary. You can measure lag using:

SELECT pg_wal_lsn_diff(pg_current_wal_lsn(), pg_last_wal_replay_lsn());

If the number is small, then replication is healthy. If it’s large, then the replica is delayed (network, disk, or CPU bottleneck).

Common causes of lag:

  • Slow network between primary and replica
  • Heavy write activity on primary
  • Replica hardware too slow
  • WAL files not being read fast enough

Analogy

Think of replication as a live video stream:

  • The primary is the broadcaster
  • The replica is the viewer
  • If the internet is slow, the replica sees the video a few seconds late — that’s replication lag

The goal is to keep that delay as small as possible.

How They Work Together

flowchart LR
    A[Primary Database] --> B[WAL - Write Ahead Log]
    B --> C[Replication Slot - Keeps old WAL until read]
    C --> D[Publication - Defines data to send]
    D --> E[Replica Database]
    E --> F[LSN - Tracks position of WAL replay]
    F --> G[Replication Lag - Distance between Primary and Replica]

Flow Summary

  1. The primary records every change in the WAL.
  2. Each change has an LSN, a unique position marker.
  3. The replication slot tells the primary how far the replica has read, so old WALs are not deleted too early.
  4. The publication defines what data should be replicated (for logical replication).
  5. The replica reads and replays WAL entries until it reaches the same LSN as the primary.
  6. The replication lag is the gap between the two LSNs.
See also  PostgreSQL Write-Ahead Log (WAL): Durability, Performance Tuning, and Recovery Explained

Relationship Between pg_replication_slots and pg_last_wal_replay_lsn()

These two are related, but they do not always match exactly.

View / FunctionLocationPurpose
pg_replication_slotsOn the primaryKeeps track of how far each replica has read WAL logs, to prevent WAL deletion
pg_last_wal_replay_lsn()On the replicaShows how far the replica has replayed (applied) the WAL changes

Key Points

  • pg_replication_slots stores metadata on the primary (not on the replica).
  • It ensures the primary keeps WAL segments until the replica confirms it has read them.
  • pg_last_wal_replay_lsn() lives on the replica and shows how far it has actually replayed changes.
  • They are often close but not always identical.

Example

SourceLSN PositionMeaning
Primary: restart_lsn0/5000000The oldest WAL the replica still needs
Replica: pg_last_wal_replay_lsn()0/5000500The latest WAL already applied

Replica has moved slightly ahead in applying changes,
but the slot on the primary will still retain older WAL until it’s safe to remove them.

Why They Differ

ReasonDescription
Network delayWAL is sent but not yet replayed
Replay lagReplica reads slower than WAL is produced
Slot retentionSlot keeps older WAL until all replicas are safe
Logical decodingLogical slots track WAL decoding, not physical replay
Recovery or restartReplica catching up after downtime

Analogy

Think of PostgreSQL replication like a newspaper delivery:

ComponentAnalogy
PrimaryNewspaper publisher
ReplicaLocal newsstand reading the papers
WALEach day’s newspaper edition
Replication SlotThe publisher’s note: “Keep all papers from issue #50 onward”
pg_last_wal_replay_lsn()The newsstand’s note: “I’ve already read up to issue #52”

Sometimes the newsstand (replica) has already read further, but the publisher (primary) keeps older copies just in case.

See also  Debezium Architecture – How It Works and Core Components

Summary Table

ConceptRoleAnalogy
WALRecord of all changesJournal or newspaper
LSNPosition markerPage number or issue number
Replication SlotKeeps old WAL until replica reads itBookmark or publisher’s note
PublicationDefines which tables to replicateNewsletter topics
ReplicaReads and replays WALSubscriber
Replication LagDelay between primary and replicaReader delay in receiving new issues

Conclusion

PostgreSQL replication is powered by WAL, tracked with LSNs, managed safely with replication slots,
filtered with publications, and measured through replication lag. Together, they form a robust and reliable system that:

  • Guarantees no data loss between primary and replica
  • Allows both full (physical) and selective (logical) replication
  • Supports monitoring and tuning through LSN and lag tracking

With this understanding, you can visualize how every part, from the WAL journal to the LSN page markers and replication slots, works together to keep PostgreSQL data safe, synchronized, and consistent across all nodes.

Related posts:

Smart Automation in PostgreSQL: Managing Time-Based Data with pg_partman and pg_cron

PostgreSQL Replication Deep Dive: From High Availability to Multi-Master Clusters

PostgreSQL Write-Ahead Log (WAL): Durability, Performance Tuning, and Recovery Explained

Pages: 1 2
Category: PostgreSQL

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Linkedin

Widhian Bramantya

Recent Posts

  • Smart Automation in PostgreSQL: Managing Time-Based Data with pg_partman and pg_cron
  • Understanding PostgreSQL WAL, Slot, Publication, LSN, and Replication Lag
  • PostgreSQL Write-Ahead Log (WAL): Durability, Performance Tuning, and Recovery Explained
  • PostgreSQL Replication Deep Dive: From High Availability to Multi-Master Clusters
  • Finding Nearby Merchants in a Ride-Hailing App Using Elasticsearch Polygon Search
  • Advanced Text Search in Elasticsearch: N-Gram, Reverse, Fuzzy, and Search-as-you-type
  • Understanding and Customizing Analyzers in Elasticsearch
  • Log Management at Scale: Integrating Elasticsearch with Beats, Logstash, and Kibana
  • Index Lifecycle Management (ILM) in Elasticsearch: Automatic Data Control Made Simple
  • Blue-Green Deployment in Elasticsearch: Safe Reindexing and Zero-Downtime Upgrades
  • Maintaining Super Large Datasets in Elasticsearch
  • Elasticsearch Best Practices for Beginners
  • Implementing the Outbox Pattern with Debezium
  • Production-Grade Debezium Connector with Kafka (Postgres Outbox Example – E-Commerce Orders)
  • Connecting Debezium with Kafka for Real-Time Streaming
  • Debezium Architecture – How It Works and Core Components
  • What is Debezium? – An Introduction to Change Data Capture
  • Offset Management and Consumer Groups in Kafka
  • Partitions, Replication, and Fault Tolerance in Kafka
  • Delivery Semantics in Kafka: At Most Once, At Least Once, Exactly Once

Recent Comments

No comments to show.

Archives

  • October 2025
  • September 2025
  • August 2025
  • November 2021
  • October 2021
  • August 2021
  • July 2021
  • June 2021
  • March 2021
  • January 2021

Categories

  • Debezium
  • Devops
  • ElasticSearch
  • Golang
  • Kafka
  • Lua
  • NATS
  • PostgreSQL
  • Programming
  • RabbitMQ
  • Redis
  • VPC
© 2026 Widhian Bramantya | Powered by Minimalist Blog WordPress Theme