pglogical
timescaleDB
logical replication
PostgreSQL
database replication

Is logical replication using pglogical possible with timescaleDB?

System Design practice on Codemia

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

Practice system design

In this article, we will explore the possibility of using logical replication with TimescaleDB through pglogical. Logical replication enables you to replicate data changes selectively from one database to another, allowing for diverse topologies and use cases beyond simple data redundancy. pglogical is one of the popular extensions for PostgreSQL to achieve logical replication. We'll examine how this can be configured with TimescaleDB, which is an extension of PostgreSQL optimized for time-series data.

Understanding TimescaleDB

TimescaleDB is an open-source database designed for time-series data, enabling efficient querying, insertion, and data retention features. By leveraging the underlying PostgreSQL architecture, it adds additional capabilities through custom hypertables to handle large-scale time-series datasets.

Introduction to pglogical

pglogical provides powerful logical replication capabilities for PostgreSQL databases. Unlike physical replication, logical replication allows for:

  • Selective table replication.
  • Version upgrades with minimal downtime.
  • Diverse topologies such as multi-source, downstream replicas, etc.

pglogical achieves this by using WAL (Write-Ahead Log) decoding to transform the database changes into logical changesets that can be applied at the destination.

Configuring pglogical with TimescaleDB

To enable logical replication between two TimescaleDB instances using pglogical, follow these steps:

Prerequisites

  • PostgreSQL 9.4 or later, with TimescaleDB installed.
  • pglogical extension installed on both source and target databases.
  • Network connectivity between the source and target instances.

Steps to Set Up pglogical Replication

1. Enable Required Extensions

First, ensure that the pglogical and TimescaleDB extensions are enabled on both the source and destination databases:

sql
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
CREATE EXTENSION IF NOT EXISTS pglogical;

2. Configure the Source Database

On the source database, set up a provider node:

sql
1SELECT pglogical.create_node(
2    node_name := 'provider_node',
3    dsn := 'host=source_host dbname=source_db user=replicator'
4);

3. Add Replication Set

Define which tables or sets of tables should be replicated. With TimescaleDB, you might want to replicate specific hypertables or continuous aggregates:

sql
1SELECT pglogical.create_replication_set('replication_set_name');
2SELECT pglogical.replication_set_add_table(
3    set_name := 'replication_set_name',
4    relation := 'public.my_hypertable',
5    synchronize_data := true
6);

4. Configure the Destination Database

On the target database, set up a subscriber node:

sql
1SELECT pglogical.create_node(
2    node_name := 'subscriber_node',
3    dsn := 'host=destination_host dbname=destination_db user=replicator'
4);

5. Create Subscription

Finally, create a subscription from the subscriber_node to consume changes from the provider_node:

sql
1SELECT pglogical.create_subscription(
2    subscription_name := 'subscription_name',
3    provider_dsn := 'host=source_host dbname=source_db user=replicator',
4    replication_sets := ARRAY['default', 'replication_set_name']
5);

With these steps, logical replication should be configured. Data changes on the source tables will propagate to the destination.

Key Considerations

  • Compatibility: Ensure that the PostgreSQL versions and TimescaleDB versions are compatible to avoid replication conflicts.
  • Data Integrity: Logical replication can sometimes lead to conflicts if concurrent changes occur at multiple nodes.
  • Performance: Logical replication emphasizes high performance, but the replication overhead can impact write-heavy workloads.
  • Monitoring: Proper monitoring setup is essential to detect replication lags or failures.

Summary Table

FeatureTimescaleDB + pglogical
Replication TypeLogical
Replication GranularityTable-level (Supports Hypertables)
DowntimeMinimal (during setup)
Use CasesCross-region replicas, Schema changes, etc.
DependencyPostgreSQL-compatible versions

Conclusion

Logical replication using pglogical is indeed feasible with TimescaleDB, enabling flexible replication topologies along with robust time-series management. Whether it's for ensuring high availability, real-time analytics, or seamless updates between environments, harnessing logical replication can significantly enhance data workflows in PostgreSQL-based systems.

By integrating TimescaleDB with pglogical, organizations can leverage the best of both time-series capabilities and sophisticated replication tactics to meet modern data demands.


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.