Dragonfly

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:

```sql
SELECT * FROM pg_stat_replication;
```

  1. 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:

```bash
sudo systemctl stop postgresql
```

  1. 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.
  2. 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:

```bash
sudo systemctl start postgresql
```

  1. 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:

```sql
SELECT pg_drop_replication_slot('slot_name');
```

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

  1. Clean Up:

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:

```sql
DROP SUBSCRIPTION subscription_name;
```

```sql
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?

Other Common PostgreSQL Questions (and Answers)

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

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost