Logical replication commands
Commands for managing your logical replication configuration
This topic provides commands for managing publications, subscriptions, and replication slots.
For step-by-step setup instructions, refer to our logical replication guides.
Publications
This section outlines how to manage publications in your replication setup.
Create a publication
This command creates a publication named my_publication
that will track changes made to the users
table:
This command creates a publication that publishes all changes in two tables:
This command creates a publication that only publishes INSERT
and UPDATE
operations. Delete operations will not be published.
Add a table to a publication
This command adds a table to a publication:
Remove a table from a publication
This command removes a table from a publication:
Remove a publication
This command removes a publication:
Recreate a publication
This command recreates a publication within a single transaction:
Subscriptions
This section outlines how to manage subscriptions in your replication setup.
Create a subscription
Building on the my_publication
example in the preceding section, here’s how you can create a subscription:
A subscription requires a unique name, a database connection string, the name and password of your replication role, and the name of the publication that it subscribes to.
In the example above, my_subscription
is the name of the subscription that connects to a publication named my_publication
. In the example above, you would replace the connection details with your Neon database connection string, which you'll find in the Connection Details widget on the Neon Dashboard.
Create a subscription with two publications
This command creates a subscription that receives data from two publications:
A single subscriber can maintain multiple subscriptions, including multiple subscriptions to the same publisher.
Create a subscription to be enabled later
This command creates a subscription with enabled = false
so that you can enable the scription at a later time:
Change the publication subscribed to
This command modifies an existing subscription to set it to a different publication:
Change the subscription connection
This command updates the connection details for a subscription:
Disable a subscription
This command disables an existing subscription:
Drop a subscription
This command drops an existing subscription:
Replication slots
Replication slots are created on the publisher database to track replication progress, ensuring that no data in the WAL is purged before the subscriber has successfully replicated it. This mechanism serves to maintain data consistency and prevent data loss in cases of network interruption or subscriber downtime.
important
To prevent storage bloat, Neon automatically removes inactive replication slots after a period of time if there are other active replication slots. If you have or intend on having more than one replication slot, please see Unused replication slots to learn more.
Create a replication slot
Replication slots are typically created automatically with new subscriptions, but they can be created manually using the pg_create_logical_replication_slot
function. Some "subscriber" data services and platforms require that you create a dedicated replication slot. This is accomplished using the following syntax:
The first value, my_replication_slot
is the name given to the replication slot. The second value is the decoder plugin the slot should use.
The max_replication_slots
configuration parameter defines the maximum number of replication slots that can be used to manage database replication connections. Each replication slot tracks changes in the publisher database to ensure that the connected subscriber stays up to date. You'll want a replication slot for each replication connection. For example, if you expect to have 10 separate subscribers replicating from your database, you would set max_replication_slots
to 10 to accommodate each connection.
The max_replication_slots
configuration parameter on Neon is set to 10
by default.
Remove a replication slot
To drop a logical replication slot that you created, you can use the pg_drop_replication_slot()
function. For example, if you've already created a replication slot named my_replication_slot
using pg_create_logical_replication_slot()
, you can drop it by executing the following SQL command:
This command removes the specified replication slot (my_replication_slot
in this case) from your database. It's important to ensure that the replication slot is no longer in use or required before dropping it, as this action is irreversible and could affect replication processes relying on this slot.
Data Definition Language (DDL) operations
Logical replication in Postgres primarily handles Data Manipulation Language (DML) operations like INSERT
, UPDATE
, and DELETE
. However, it does not automatically replicate Data Definition Language (DDL) operations such as CREATE TABLE
, ALTER TABLE
, or DROP TABLE
. This means that schema changes in the publisher database are not directly replicated to the subscriber database.
Manual intervention is required to replicate DDL changes. This can be done by applying the DDL changes separately in both the publisher and subscriber databases or by using third-party tools that can handle DDL replication.
Monitoring replication
To ensure that your logical replication setup is running as expected, you should monitor replication processes regularly. The pg_stat_replication view displays information about each active replication connection to the publisher.
The view provides details like the state of the replication, the last received WAL location, sent location, write location, and the delay between the publisher and subscriber.
Additionally, the pg_replication_slots view shows information about the current replication slots on the publisher, including their size.
It's important to keep an eye on replication lag, which indicates how far behind the subscriber is from the publisher. A significant replication lag could mean that the subscriber isn't receiving updates in a timely manner, which could lead to data inconsistencies.