Sub topic 13
What Logical Replication Does Not Replicate
Logical replication sends selected row changes from a publisher to a subscriber. It does not make the subscriber an exact copy of the publisher. The two databases have separate schemas, settings, objects, and transaction histories.
This chapter assumes that you know basic SQL, PostgreSQL roles, primary keys, and the purpose of the write-ahead log (WAL).
Row Changes Are the Boundary
A publication normally sends INSERT, UPDATE, DELETE, and optionally TRUNCATE changes for selected tables. The subscriber applies those changes to tables that you create separately.
Logical replication does not automatically copy these database objects or settings:
- Table definitions and other data definition language (DDL), such as
CREATE TABLEorALTER TABLE - Indexes, except for indexes you create manually
- Constraints, triggers, rules, and generated columns
- Roles, grants, databases, extensions, functions, and views
- Sequence state
- Large objects
- Server configuration, including
postgresql.conf - Changes to temporary or unlogged tables through an ordinary table publication
A row inserted into a table with a serial or identity column includes its generated number in the replicated row. However, the sequence that generates future numbers is not kept synchronized. After a failover or promotion, the subscriber's sequence can produce a value that already exists.
-- Run on the subscriber after planned promotion.
-- Check the next sequence value against the table's largest ID.
SELECT setval(
'orders_id_seq',
COALESCE((SELECT max(id) FROM orders), 1),
true
);The sequence name and column type must match your schema. Test this procedure before using it in an outage.
Schema Changes and Subscriber Writes
Because DDL is not replicated, apply schema changes to both databases. Add a new column in a compatible way, deploy the application, and then change the publication or subscription if needed.
For example, adding a required column without a default can cause the subscriber to reject incoming rows. A column list or row filter can also make the publisher and subscriber behave differently. Keep the replicated table definitions compatible, even when the subscriber has extra indexes or local reporting objects.
The subscriber is writable by default. Local writes are not sent back to the publisher, and they can conflict with incoming changes. A local row with the same primary key can cause an insert to fail. A local update can also be overwritten by a later replicated update.
Choosing Logical Replication
Logical replication fits when you need selected tables, cross-version migration, regional data distribution, or a controlled online migration. It can keep a new database nearly current while applications continue writing to the old one.
Physical streaming replication is usually a better fit for a complete standby. It copies the whole PostgreSQL cluster at the storage and WAL level, including database objects and sequence state, and it normally requires compatible major versions.
| Requirement | Better fit |
|---|---|
| Entire cluster standby for disaster recovery | Physical replication |
| Selected tables or columns | Logical replication |
| Online major-version migration | Logical replication |
| Automatic DDL and object copying | Migration tooling plus manual preparation |
Logical replication also has operational limits. A large initial table copy can consume network, CPU, and disk resources. A slow subscriber retains WAL on the publisher through its replication slot. Some workloads need extensions or object types that require separate migration methods.
Practical Decision
Before choosing logical replication, list everything the target system needs:
- Data rows and ongoing changes
- Table definitions and indexes
- Sequences and identity generation
- Roles, permissions, extensions, and functions
- DDL deployment
- Conflict and failover procedures
If only the first item is planned, the migration is incomplete. Logical replication supplies the data-change stream, while your deployment and operations process must supply the rest.
Exercise
You replicate an orders table to a new major PostgreSQL version. What must you plan separately from the publication and subscription?
Show answer
Plan the schema and DDL, indexes and constraints, roles and permissions, extensions and functions, sequence synchronization, and a procedure for conflicts and promotion.