Sub topic 2
How the WAL Becomes a Stream of Row Changes
This chapter assumes that you know basic SQL, can run psql, and understand that PostgreSQL’s write-ahead log (WAL) records changes before PostgreSQL writes them to data files. You should also know the basics of database roles and privileges.
From physical records to logical changes
The WAL is a durable record of database activity. PostgreSQL uses it for crash recovery and physical replication. A physical replica replays low-level changes to pages and files. It must use a compatible PostgreSQL version and generally contains the same database cluster as the publisher.
Logical replication uses the WAL differently. A logical decoder reads WAL records and reconstructs changes to rows and transactions. It converts internal records into a logical stream such as:
- Insert this row into
orders. - Update the
statuscolumn for this row. - Delete this row.
- Commit the transaction.
The stream describes database changes rather than page changes. A subscriber can therefore have a different physical layout, and logical replication can connect selected tables instead of copying the entire cluster.
Logical decoding preserves transaction boundaries and commit order. Changes from a transaction become available as a committed unit. A decoder does not normally expose an incomplete transaction to a consumer.
| Layer | What it represents | Typical consumer |
|---|---|---|
| WAL record | Internal page and transaction changes | Crash recovery or physical replica |
| Logical change | Row-level insert, update, or delete | Logical replication subscriber |
| Replication protocol | Encoded logical messages | `pgoutput` or another output plugin |
The decoding path
The publisher writes WAL whenever a transaction changes data. With logical decoding enabled, PostgreSQL can read those WAL records and produce logical messages.
A replication slot marks the point in the WAL stream that a consumer still needs. The slot asks PostgreSQL to retain required WAL until the consumer confirms that it has received and processed the changes. This protects a slow or temporarily disconnected consumer, but an abandoned slot can consume disk space.
The following diagram shows the path from a client transaction to a subscriber.
LSN means log sequence number. It identifies a position in the WAL stream. A consumer reports progress using LSN values so PostgreSQL knows which WAL is no longer needed for that consumer.
Enabling logical decoding
The publisher must use a suitable wal_level. Logical decoding requires wal_level = logical, and changing this setting requires a server restart.
-- Run on the publisher as a role with configuration privileges.
ALTER SYSTEM SET wal_level = 'logical';
-- Restart the PostgreSQL server, then verify the setting.
SHOW wal_level;
-- Create a logical slot after logical decoding is enabled.
SELECT *
FROM pg_create_logical_replication_slot('orders_slot', 'pgoutput');
-- Inspect the slot and the WAL it may retain.
SELECT slot_name, plugin, active, restart_lsn
FROM pg_replication_slots
WHERE slot_name = 'orders_slot';The pgoutput plugin is PostgreSQL’s built-in output plugin for native logical replication. It formats decoded changes for publications and subscriptions. Other output plugins can format changes for applications such as event pipelines, but those applications must understand the plugin’s message format.
> callout note > Creating a logical slot is a resource decision. If no consumer advances or removes the slot, the publisher may retain increasing amounts of WAL. Create slots deliberately and monitor them.
A logical slot is independent of a publication. A slot tracks WAL consumption, while a publication later defines which tables and operations native logical replication exposes. A subscription connects those pieces to a consumer.
Exercise
Why can a logical subscriber use a different physical storage layout from the publisher?
Show answer
Logical decoding converts WAL records into row and transaction changes instead of sending page-level or file-level changes.