Sub topic 13
What logical replication does not replicate
This chapter assumes you can create a publication and a subscription, you know what replica identity is, and you have read the earlier chapters on slots and initial sync. It lists what a logical replication link leaves out, so you can plan for the gaps before a cutover or an upgrade.
The core idea: row changes only
Logical replication ships row-level INSERT, UPDATE, DELETE and TRUNCATE events for tables in a publication. It ships nothing else. The publisher decodes its WAL into those row events, and the subscriber applies them as ordinary SQL-level changes.
Anything that is not a row change in a published table must be handled another way. That includes schema, sequence positions, large objects, roles, and the contents of other databases in the cluster.
Summary of gaps
The table covers common PostgreSQL versions. Check the release notes for your version, because several gaps have been narrowed over time.
| Not replicated | What happens instead | What you do |
|---|---|---|
| DDL (`CREATE`, `ALTER`, `DROP`) | The subscriber keeps its old schema | Apply schema changes on both sides yourself |
| Sequence values | Sequences on the subscriber stay where they were | Copy values with `setval` before switchover |
| Large objects | `pg_largeobject` is not decoded | Store the data in `bytea` columns, or move it separately |
| Roles, grants, tablespaces | Not part of any table's data | Create them on the subscriber |
| Views, materialized view contents, foreign tables | Not publishable | Rebuild or refresh on the subscriber |
| Other databases in the cluster | A subscription covers one database | Create one publication and subscription per database |
| Tables not in the publication | Silently skipped | Add them explicitly |
DDL is not replicated
If you add a column on the publisher, the subscriber does not learn about it. Changes for the new column arrive at a subscriber table that lacks it, and the apply worker fails with an error such as logical replication target relation "public.orders" is missing replicated column: "note". Replication then stops and retries until you fix the schema.
The safe order depends on the change:
- Adding a column: add it to the subscriber first, then to the publisher. Make it nullable or give it a default so the subscriber can accept rows from both before and after the change.
- Dropping a column: drop it from the publisher first, then from the subscriber.
- Adding a table to a publication: create the table on the subscriber, add it to the publication, then run
ALTER SUBSCRIPTION ... REFRESH PUBLICATION.
A subscriber table may have extra columns that the publisher does not send. They receive their default value or NULL. It may not lack a column that the publisher sends.
Sequences are not synchronized
The row inserted on the publisher carries its id value, but the sequence that produced that value is not advanced on the subscriber. The subscriber's sequence stays at its starting value. If you promote the subscriber and let applications insert rows, the sequence hands out values that already exist, and you get duplicate key errors.
Before a switchover, stop writes on the publisher, then generate setval statements from the publisher's current state:
-- On the publisher: build one setval statement per sequence that has been used
SELECT format('SELECT setval(%L, %s, true);',
schemaname || '.' || sequencename,
last_value)
FROM pg_sequences
WHERE last_value IS NOT NULL;Run the generated statements on the subscriber. Because the publisher must be quiet when you do this, sequences are a standard step in the cutover plan for a major-version upgrade.
Some newer PostgreSQL releases are adding sequence synchronization. Until your version documents it, assume it does not exist.
Large objects and non-table data
Logical decoding works on table rows. Large objects created with lo_create or lo_import live in pg_largeobject, which is not decoded. A table column holding the object's OID replicates, but the OID points to nothing on the subscriber. If you rely on large objects, migrate them with pg_dump, or switch the column to bytea before using logical replication.
Roles, privileges, tablespaces, extensions, and database-level settings are also outside the stream. Roles are cluster-wide objects, so pg_dumpall --globals-only is the usual way to copy them.
Only tables are published
A publication holds tables. These objects cannot be published directly:
- Views, and the contents of materialized views. Refresh materialized views on the subscriber if you need them there.
- Foreign tables.
- System catalogs.
- Temporary and unlogged tables, because their changes do not reach the WAL in a decodable form.
Partitioned tables need care. The leaf partitions are the tables that hold the rows, and the publish_via_partition_root option decides which name the changes carry. With the default false, changes are published under each leaf partition's own name, so the subscriber needs matching partitions. With true, they are published under the root table's name, so the subscriber's table can be partitioned differently or not partitioned at all. Support for publishing partitioned tables requires PostgreSQL 13 or later.
You can list what a publication actually contains:
-- On the publisher
SELECT pubname, schemaname, tablename
FROM pg_publication_tables
ORDER BY pubname, schemaname, tablename;If a table you expect is missing from this list, it is not being replicated.
Generated columns
Stored generated columns are not sent by default in older releases. The subscriber computes the value itself if its own table defines the column as generated, and otherwise the column is left out. Newer releases add a publication option to publish them. If a column of this kind matters to you, test the behavior on your version before relying on it.
TRUNCATE and the operation list
TRUNCATE replicates by default, but only for tables in the publication. With CASCADE on the publisher, tables that are truncated through foreign keys must also be in the publication. If they are not, the subscriber raises an error when it truncates a table that other tables reference.
If you set publish = 'insert, update, delete' you leave TRUNCATE out on purpose. Check pubtruncate in pg_publication if rows vanish on one side and not the other.
Triggers and rules on the subscriber
By default the apply worker runs with session_replication_role = replica. That means ordinary triggers and rules on the subscriber's tables do not fire for replicated changes. This avoids double work and loops, but it surprises people who use triggers to maintain audit tables or derived columns.
To fire a trigger on replicated changes, mark it explicitly:
-- On the subscriber: fire this trigger for replicated changes too
ALTER TABLE orders ENABLE ALWAYS TRIGGER orders_audit_trg;Foreign key constraints on the subscriber are also not enforced during apply in the same way as for normal sessions, so a subscriber can end up holding rows that violate them if you replicate only part of a related set of tables. Publish related tables together.
Writes on the subscriber and loops
The subscriber's tables are writable. Logical replication does not stop local writes, and it does not coordinate with them. Rows you change locally can conflict with incoming changes. A conflict causes an apply error, and replication stops until you resolve it.
Replication is one-way. If you build two subscriptions in opposite directions, a change can bounce between the nodes. PostgreSQL 16 added the subscription option origin = none, which tells the subscriber to ask only for changes that did not themselves arrive through replication. Even with that option, there is no conflict resolution, so bidirectional setups need strict rules on who writes which rows.
Slots and failover
A replication slot lives on one server. A physical standby of the publisher does not copy the slot in older releases. If you fail over to the standby, the subscriber's slot is gone, and the subscriber cannot resume where it stopped. Newer releases add slot synchronization to standbys through a failover option on the subscription. Without it, plan to re-create the subscription and resynchronize the tables after a failover.
Replica identity limits
A table that publishes UPDATE or DELETE needs a replica identity. Without a primary key, a table fails on those operations until you set REPLICA IDENTITY FULL or point to a unique index. A table published for inserts only does not need one.
A pre-flight checklist
Before relying on a logical replication link, check each of these:
- List published tables with
pg_publication_tablesand compare against what the application writes. - Confirm every updated or deleted table has a replica identity.
- List sequences, views, large objects, roles, and triggers that the subscriber needs, and plan how each will get there.
- Decide how schema changes will be applied on both sides, and in which order.
- Plan what happens to the slot if the publisher fails over.
Exercise
You promote a logical replication subscriber to primary during a major-version upgrade. Rows copied over look correct, but the first application insert fails with a duplicate key error on the id column. What was missed, and how do you fix it?
Show answer
Sequences are not replicated, so the subscriber's sequence still holds its starting value and produces ids that already exist. Before the switchover, with writes stopped on the publisher, read the publisher's sequence values from pg_sequences and run setval for each one on the subscriber. For the failing table, run SELECT setval('orders_id_seq', (SELECT max(id) FROM orders)); to repair it now.
Exercise
A publisher adds the column note text to orders and starts writing values into it. The subscriber's apply worker begins failing repeatedly. Why, and what is the correct order to repair it?
Show answer
DDL is not replicated, so the subscriber's orders table has no note column and cannot accept the incoming rows. Run ALTER TABLE orders ADD COLUMN note text; on the subscriber. The worker retries and resumes without further action. For future additive changes, alter the subscriber first and then the publisher.