Postgres logical replication db table grows indefinitely
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Logical replication in PostgreSQL is a powerful feature that allows you to mirror data changes asynchronously between databases. It is primarily used for scaling out, high availability, and seamlessly integrating data into heterogeneous systems. However, like any other sophisticated system, it has its pitfalls and requires proper understanding and maintenance. One commonly reported issue in PostgreSQL logical replication is when a database table grows indefinitely. This article delves into reasons why this occurs, how to diagnose it, and strategies to mitigate or prevent it.
Understanding PostgreSQL Logical Replication
Logical replication in PostgreSQL is a process where changes (data modifications) within a table are captured and transmitted from a publisher to a subscriber. The core concept here is the change data capture model, which integrates the actual data changes rather than the entire dataset.
Key Components
- Publication: A database object that contains a list of changes (data modifications) from a set of tables whose replication is desired.
- Subscription: A database object within a subscriber that mirrors the changes reported by a publisher.
- Logical Decoding: A mechanism that extracts data changes in a format suitable to be applied on a different system.
Table Growth in Logical Replication
Indefinite growth of tables in PostgreSQL logical replication setups can be attributed to several factors.
Common Causes
- Unconsumed Replication Slots:
- Replication slots are used to ensure changes are retained until they are consumed by all subscribers. If a replication slot is not being consumed, data meant for replication accumulates, leading to excessive storage usage.
- Bloating and Dead Tuples:
- PostgreSQL utilizes a Multi-Version Concurrency Control (MVCC) system, where deleted or updated tuples (rows) are marked for later cleanup. If not managed, this leads to bloating and unnecessary table size growth.
- Inadequate Vacuuming:
- Autovacuum may not keep up with cleaning dead tuples, exacerbating table growth especially in high transaction environments.
- Unbounded Logical Replication:
- Replicating large datasets without bounding the operations (e.g., no proper filtering on data changes) can result in volume increases disproportionately at the subscribers.
Diagnosing the Issue
A systematic approach is required to diagnose the indefinite growth of tables:
- Check Replication Slot Activity:
- Inspect Table Bloat:
- Extensions like
pgstattuplecan be used to provide insight into table bloating.
- Evaluate Autovacuum Efficiency:
Mitigation Strategies
There are numerous strategies to tackle this issue effectively:
- Regular Maintenance of Replication Slots:
- Ensure replication slots are monitored and unused slots are dropped:
- Optimize Autovacuum Settings:
- Tuning autovacuum settings can significantly improve dead tuple cleanup:
- Partitioning Strategies:
- Introduce table partitioning to manage large tables more effectively, potentially reducing bloat.
- Selective Replication:
- Limit columns or rows put onto the replication channel using filtering conditions at the publisher.
- Periodical Table Reorganization:
- Use
CLUSTERorVACUUM FULLon the tables, though these are more intrusive and may require downtime.
Table Summary of Key Points
| Feature / Issue | Description |
| Replication Slots | Ensure slots are used and monitored. |
| Table Bloat | Frequent updates/deletes cause excessive bloat. |
| Autovacuum | Adjust settings to improve vacuuming process. |
| Logical Replication | Use selective replication to manage data. |
| Maintenance | Regular maintenance and partitioning help. |
Conclusion
Understanding and managing logical replication in PostgreSQL is crucial for maintaining the performance and health of your database system. Addressing issues such as table bloating and unconsumed slots proactively can significantly mitigate the risk of your tables growing indefinitely. Optimization strategies like refined configurations of autovacuum, regular slot maintenance, and tactical use of replication filtering play pivotal roles in the effective handling of logical replication setups in PostgreSQL. Empowered with this knowledge, database administrators can ensure their systems remain efficient, scalable, and robust.
Related reading
- Postgres Replication and Temporary Tables
- Postgres Replication with pglogical ERROR connection to other side has died
- Postgresql 9.2 failover
- postgresql 9.4 high availability topology
- Postgres Sql could not determine data type of parameter by Hibernate
- PostgreSQL asymmetric replication
- Postgresql replication in rails with data-fabric gem
- PostgreSQL restoration throwing error replication slot does not exist

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.