PostgreSQL
replication
pglogical
error handling
troubleshooting

Postgres Replication with pglogical ERROR connection to other side has died

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

PostgreSQL replication is a critical component for maintaining data redundancy, high availability, and load balancing across database systems. Among the various replication methods available, pglogical offers a logical replication solution that is highly flexible and efficient. However, setting up and maintaining pglogical can present challenges, such as the dreaded error message: ERROR: connection to other side has died. This article delves into understanding this error, with technical explanations and practical solutions.

Understanding Pglogical

pglogical is a PostgreSQL extension that provides logical replication functionality, allowing fine-grained control over what gets replicated. Unlike physical replication, which copies the entire database state at the block level, logical replication operates at the level of SQL statements, tables, or even specific columns. This allows:

  • Selective data replication across multiple databases or schemas.
  • Replication between different versions of PostgreSQL.
  • Extended replication capabilities across different geographical locations.

Causes of "ERROR: Connection to other side has died"

Encountering the error ERROR: connection to other side has died could be indicative of several underlying issues. Primary reasons include:

1. Network Issues

A common cause of this error is network instability or misconfiguration that leads to dropped connections. To address this:

  • Ensure that the network link between the publisher and subscriber databases is reliable.
  • Use packet capturing tools like Wireshark to diagnose and resolve network connectivity issues.

2. Incorrect Configuration

Improper configuration of replication roles, which include publishers and subscribers, often leads to this error. Checking configurations ensures proper setup:

  • Verify that the replication identities (host, dbname, username, and password) specified in the pglogical subscription match those on the publisher.
  • Ensure that pg_hba.conf is appropriately configured to allow replication traffic.

3. Version Compatibility Issues

Incompatibility between PostgreSQL versions or pglogical extensions on the publisher and subscriber nodes can also trigger this error.

  • Confirm that the versions of pglogical are the same across both nodes.
  • While logical replication supports version mismatches, ensure compatibility guidelines are adhered to in the PostgreSQL documentation.

How to Diagnose and Resolve the Error

Step 1: Check Connection Settings

Start by verifying connection configurations:

sql
1-- Check connection settings in pglogical.nodes
2SELECT * FROM pglogical.show_subscription_conflist();
3
4-- If using pg_trgm or other extensions, ensure they are present on both sides.

Step 2: Review Logs

Inspect logs for detailed error messages:

bash
$ tail -f /var/log/postgresql/postgresql.log

Look for:

  • Authentication errors
  • Host unreachable messages

Step 3: Network Diagnostics

Check connectivity using ping or telnet:

bash
$ ping <subscriber_host>
$ telnet <subscriber_host> <port>

Ensure that port 5432 (or whichever is used) is open and accessible.

Step 4: Revalidate Subscriptions

Revalidate the subscriptions and apply fixes:

sql
-- Disable and re-enable subscription:
ALTER SUBSCRIPTION my_subscription DISABLE;
ALTER SUBSCRIPTION my_subscription ENABLE;

Example Pglogical Setup

To set up pglogical, follow these basic steps:

  1. Install pglogical on both publisher and subscriber nodes:
bash
   $ sudo apt-get install postgresql-<version>-pglogical
  1. Configure postgresql.conf on both nodes:
plaintext
1   wal_level = 'logical'
2   max_replication_slots = 4
3   max_wal_senders = 4
4   shared_preload_libraries = 'pglogical'
  1. Initialize pglogical on the publisher:
sql
   SELECT pglogical.create_node(
       node_name := 'publisher',
       dsn := 'host=publisher_host dbname=mydb user=myuser password=mypass');
  1. Create a subscription on the subscriber:
sql
   SELECT pglogical.create_subscription(
       subscription_name := 'subscription1',
       provider_dsn := 'host=publisher_host dbname=mydb user=myuser password=mypass');

Key Points Summary Table

Issue DetectedCommon CausesPotential Solutions
Connection DiedNetwork instability, Incorrect configsVerify network, pg_hba.conf, and configurations
Version ErrorsMismatched versions or extensionsEnsure compatibility and upgrade extensions
Log MessagesAuthentication issuesCheck logs, verify credentials and permissions

Conclusion

The ERROR: connection to other side has died in pglogical can be frustrating, but addressing it typically involves checking various points along the data replication pathway. By scrutinizing network connectivity, verifying pglogical and PostgreSQL configurations, and ensuring version compatibility, most issues can be resolved. Always remember to consult the latest PostgreSQL documentation and pglogical release notes for updates that might influence compatibility or introduce new features to enhance replication stability and performance.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.