Sub topic 10
Handling Schema Changes in PostgreSQL Logical Replication
This chapter assumes that you understand tables, ALTER TABLE, primary keys, and the basic roles of a publication and subscription. Logical replication copies row changes, but it does not copy data definition language (DDL). DDL is SQL that changes database structure, such as CREATE TABLE, ALTER TABLE, and DROP TABLE.
Why schema changes need coordination
A publication sends changes for selected tables. A subscription applies those changes to tables with matching names and compatible columns. The subscription does not automatically run the publisher's schema-changing commands.
For example, adding a column on the publisher does not automatically add that column on the subscriber. Apply the same structural change manually, usually on the subscriber first or as part of a coordinated deployment.
-- Run on the subscriber before the application starts using the column
ALTER TABLE orders
ADD COLUMN shipped_at timestamptz;Then apply the corresponding change on the publisher:
-- Run on the publisher
ALTER TABLE orders
ADD COLUMN shipped_at timestamptz;Column names matter. Logical replication matches columns by name, so the columns do not have to appear in the same physical order. A subscriber column that is missing from the publisher is allowed, but it needs a default value or must allow NULL when inserted rows are applied.
| Change | Replicated automatically? | Usual action |
|---|---|---|
| Insert, update, or delete a row | Yes | Monitor the apply worker |
| Add or remove a column | No | Apply compatible DDL manually |
| Create a new table | No | Create it, publish it, then refresh |
| Change a column type | No | Coordinate a compatible migration |
Adding a new table
To replicate a new table, complete the steps in order:
- Create the table on the subscriber.
- Create the table on the publisher.
- Add the table to the publication.
- Refresh the subscription.
The subscriber table must have compatible columns before the refresh:
-- On the publisher
CREATE TABLE order_items (
id bigint PRIMARY KEY,
order_id bigint NOT NULL,
sku text NOT NULL,
quantity integer NOT NULL
);
ALTER PUBLICATION orders_pub ADD TABLE order_items;
-- On the subscriber
CREATE TABLE order_items (
id bigint PRIMARY KEY,
order_id bigint NOT NULL,
sku text NOT NULL,
quantity integer NOT NULL
);
ALTER SUBSCRIPTION orders_sub REFRESH PUBLICATION;The refresh starts the initial copy for the new table and then streams later changes. Existing rows inserted between the publication change and the refresh are handled by the synchronization process.
Safe migration patterns
Avoid changing a column in a way that makes incoming rows impossible to apply. For a type change, first add a new compatible column, populate it, and deploy application code that writes both columns. After the subscriber catches up, migrate reads and remove the old column later.
A column rename also needs care. Replication identifies columns by name, so an immediate rename can leave the publisher and subscriber with different mappings. Treat a rename as an add-and-migrate operation instead.
Detecting a failed schema change
If an apply worker encounters an error, it usually pauses rather than skipping the problematic change. Check the subscriber's PostgreSQL server log for the exact error. Also inspect subscription status:
SELECT subname,
pid,
received_lsn,
latest_end_lsn,
latest_end_time
FROM pg_stat_subscription;Correct the schema or data conflict, then resume the subscription:
ALTER SUBSCRIPTION orders_sub ENABLE;Do not manually skip changes unless you understand the resulting data difference and have a recovery plan.
Exercise
You add table order_items to the publisher's publication, but the subscriber has no such table. What must you do next?
Show answer
Create a compatible orderitems table on the subscriber, then run ALTER SUBSCRIPTION orderssub REFRESH PUBLICATION. DDL is not replicated automatically.