Using replication slots

Relying on transaction log files is a risky endeavour. If the primary server deletes one before a replica can process it, we may need to rebuild the replica outright. If we're using PostgreSQL 9.4 or higher, we can prevent that kind of mishap by using replication slots instead.

To start, we would need to create a replication slot on the primary server itself with this SQL:

SELECT * FROM pg_create_physical_replication_slot('pg2_slot');

Then on the replica, we would add this line to its recovery.conf before starting (or restarting) the instance:

primary_slot_name = 'pg2_slot' 

Now our replica can't fall behind. Though, we should be careful that replica outages are limited. Otherwise the primary could accumulate too ...

Get PostgreSQL High Availability Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.