Sub topic 7
Initial Table Synchronization
This chapter assumes that you know basic SQL, can run psql, and understand PostgreSQL roles, primary keys, and the write-ahead log (WAL). You also need a publisher with a publication and a subscriber with a compatible table definition.
Creating a subscription does more than start streaming new changes. PostgreSQL first copies the rows that already exist in each published table. This process is called initial table synchronization. After the copy finishes, the subscriber applies changes that occurred during the copy and then continues receiving new changes.
Prepare the subscriber table
Logical replication copies data, not table definitions. Create the table on the subscriber before creating the subscription or before refreshing it.
The subscriber table should have compatible columns and data types. It does not need to have exactly the same indexes, defaults, or constraints, but differences can cause errors or different behavior.
-- Run on the subscriber
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
total numeric(10,2) NOT NULL
);The table should normally be empty before the first synchronization. If it already contains rows with the same primary keys as rows from the publisher, the apply worker can fail with a duplicate-key error.
Create the subscription
Create the subscription on the subscriber. The subscription connects to the publisher and names the publication to consume.
CREATE SUBSCRIPTION orders_sub
CONNECTION 'host=publisher.example.com port=5432 dbname=shop user=repl_user password=change_me'
PUBLICATION orders_pub;When PostgreSQL creates the subscription, it normally performs these steps:
- It connects to the publisher using the connection string.
- It creates a logical replication slot on the publisher.
- It starts a table synchronization worker for each table that needs an initial copy.
- It copies the existing rows.
- It applies changes that were committed while the copy was in progress.
- It switches the table to normal change streaming.
The replication slot remembers the publisher's position. This prevents PostgreSQL from removing WAL that is still needed by the subscriber.
Track synchronization progress
Use the subscriber's statistics view to inspect subscription workers.
SELECT subname,
pid,
received_lsn,
latest_end_lsn,
latest_end_time
FROM pg_stat_subscription;The result shows the main worker and, during synchronization, additional table sync workers. A NULL process ID can mean that a worker is not currently running. Check the PostgreSQL server log for connection failures, permission errors, or apply errors.
On the subscriber, inspect the subscription state for each table:
SELECT srrelid::regclass AS table_name,
srsubstate,
srsublsn
FROM pg_subscription_rel;The state code indicates synchronization progress. The exact internal codes are less important than whether a table eventually reaches the ready state. A table that remains in a synchronization state needs investigation.
Add another table later
DDL is not replicated. If you add a table to an existing publication, create the matching table on the subscriber first, then refresh the subscription.
-- On the publisher
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;Refreshing the publication discovers the new table and starts its initial synchronization. Existing subscriber tables are not recopied unless you explicitly manage their subscription state.
| Situation | Expected action |
|---|---|
| New table added to a publication | Create it on the subscriber, then refresh the subscription |
| Subscriber table contains conflicting rows | Remove or reconcile the rows before copying |
| Copy is slow | Check table size, network bandwidth, locks, and worker activity |
| Slot retains growing WAL | Restore subscriber progress or disable and clean up the subscription safely |
Exercise
Why must the subscriber table be created before initial synchronization, and what can happen if it already contains conflicting rows?
Show answer
Logical replication does not copy DDL, so the table must exist with compatible columns. Conflicting rows, especially duplicate primary keys, can cause the apply worker to fail.