Introducing Dragonfly Cloud! Learn More

Question: How to stop PostgreSQL replication?

Answer

Stopping PostgreSQL replication involves different steps depending on the type of replication setup (streaming replication, logical replication, etc.) you are using. Below are the general steps for stopping streaming replication, which is one of the most common types of replication setups.

Stopping Streaming Replication

  1. Identify the Standby Servers: First, identify all the standby servers that are connected to the primary server. You can do this by running the following SQL command on the primary server:

    SELECT * FROM pg_stat_replication;
  2. Disconnect Standby Servers: On each standby server, you need to disconnect it from the primary server. This can be achieved by stopping the PostgreSQL service. The command to stop the service varies depending on the operating system. For example, on a Linux system using systemd, you can run:

    sudo systemctl stop postgresql
  3. Edit the Configuration Files: On each standby server, remove or comment out the lines in postgresql.conf and recovery.conf (or standby.signal and the relevant settings in postgresql.conf for versions after PostgreSQL 12) that configure replication. Specifically, look for the primary_conninfo setting in postgresql.conf and any standby or replication-related settings.

  4. Restart the PostgreSQL Service: After removing the replication configuration, restart the PostgreSQL service on the standby servers. Again, this can be done using systemd on Linux:

    sudo systemctl start postgresql
  5. Remove Replication Slots (Optional): If you were using physical replication slots, you should remove them on the primary server to avoid the accumulation of WAL files. Execute the following command on the primary server for each replication slot:

    SELECT pg_drop_replication_slot('slot_name');

Replace 'slot_name' with the name of the replication slot you want to remove.

  1. Clean Up:
    • On the primary server, you might also want to adjust the max_wal_senders setting in postgresql.conf if it was specifically tuned for replication.
    • Ensure that any backup scripts or monitoring tools that were aware of the replication setup are updated or disabled as necessary.

For Logical Replication

If you're using logical replication, the process involves dropping the subscription on the subscriber database and the publication on the publisher database:

  • Drop Subscription:

    DROP SUBSCRIPTION subscription_name;
  • Drop Publication:

    DROP PUBLICATION publication_name;

Remember to replace subscription_name and publication_name with the actual names of your subscription and publication.

Conclusion

Stopping PostgreSQL replication requires careful execution of steps on both the primary and standby servers. It's important to ensure that you have backups and understand the implications of stopping replication, especially if you plan to reconfigure or decommission replication setups.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.