DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • PostgreSQL Performance Metrics
  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • From Concept to Cloud: Building With Cursor and the Heroku MCP Server
  • Database Query Service With OpenAI and PostgreSQL in .NET

Trending

  • Solid Testing Strategies for Salesforce Releases
  • The Role of Retrieval Augmented Generation (RAG) in Development of AI-Infused Enterprise Applications
  • Ensuring Configuration Consistency Across Global Data Centers
  • Grafana Loki Fundamentals and Architecture
  1. DZone
  2. Data Engineering
  3. Databases
  4. Setting Up Failover Slots in PostgreSQL-17

Setting Up Failover Slots in PostgreSQL-17

This article explains how PostgreSQL 17's new failover slots feature simplifies logical replication to ensure seamless replication even during failover scenarios.

By 
Dr. Ibrar Ahmed user avatar
Dr. Ibrar Ahmed
·
Dec. 09, 24 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
2.6K Views

Join the DZone community and get the full member experience.

Join For Free

PostgreSQL 17 introduces failover slots that enhance high-availability setups. A replication slot ensures that data remains reliable and consistent between nodes during replication, whereas a failover slot ensures consistency between nodes, specifically during and after a failover.

Failover slots are a powerful feature that ensures logical replication can continue seamlessly, even after a failover to a standby server. Using failover slots allows logical replication slots to be automatically synchronized across primary and standby nodes, significantly reducing downtime and the need for manual intervention during a failover.

This guide will walk you through setting up a high-availability PostgreSQL cluster using the new failover slots feature. By the end, you'll have a robust replication setup capable of seamlessly handling a failover. 

Why Failover Slots Matter From a Historical Perspective

Challenges in PostgreSQL 15

  • Replication Slots Tied to the Primary Node: In PostgreSQL 15, replication slots were only created on the primary server. All logical replication slots were lost if the primary server failed, leading to significant replication delays and data loss.
  • Manual Failover Management: During failover scenarios, administrators manually recreated replication slots on the new primary server, which increased complexity, introduced errors, and prolonged downtime.
  • No Slot Synchronization: Standby servers had no way of knowing about logical replication slots on the primary. This lack of synchronization led to a complete reset of replication streams if a failover occurred.

Improvements in PostgreSQL 16

Minimal Logical Decoding

PostgreSQL 16 introduced a feature called minimal logical decoding on standbys:

  • Minimal Decoding on Standby: This allowed standby servers to decode WAL logs to prepare for logical replication, enabling pre-warmed slots for use if a failover occurred.
  • Faster Failover: By pre-decoding WAL changes on the standby, it was possible to reduce replication lag when promoting a standby to the primary. However, this still required some manual configuration to ensure smooth failover.

PostgreSQL 17: The Game-Changer - Failover Slots

  • Failover Slots: Introducing failover slots in PostgreSQL 17 eliminates the need for manual intervention by automatically synchronizing logical replication slots between the primary and standby servers.
  • Automatic Synchronization: The new slot sync worker ensures that failover-enabled slots (failover = true) are always synchronized, even while the primary node is active.
  • Seamless Transition: Upon failover, the standby server can take over as the primary without losing any replication slots, ensuring zero data loss and continuous replication.

Feature

PostgreSQL 15

PostgreSQL 16

PostgreSQL 17

Logical Replication

Yes

Yes

Yes

Automatic Slot Synchronization

No

Minimal logical decoding on Standby

Full failover slots

Failover Handling

Manual intervention needed

Pre-warmed slots on standby

Automatic failover slots

Slot Synchronization to Standby

Not supported

Minimal, requires configuration

Automatic with slotsync worker

High Availability for Logical Replication

Limited

Improved with minimal decoding

Seamless with failover slots


Creating a High-Availability Cluster With Failover Slots

This section will walk you through creating a PostgreSQL high-availability cluster with failover slots. In our example, we'll use the following nodes:

  1. NodeA (Primary Server)
  2. NodeB (Physical Standby)
  3. NodeC (Logical Subscriber)

Creating a PostgreSQL high-availability cluster with failover slots


Prerequisites

Before we start, ensure you have:

  • PostgreSQL 17 was installed on all three nodes.
  • Passwordless SSH access between each node.
  • A basic understanding of PostgreSQL, PostgreSQL replication, and PostgreSQL configuration files.

Understanding PostgreSQL, PostgreSQL replication, and PostgreSQL configuration files


Step 1: Configuring the Primary Node (NodeA)

1.1 Initialize the cluster on NodeA

After installing PostgreSQL on the primary node, initialize the cluster; you can use the following commands:

Shell
 
mkdir -p /home/pgedge/nodeA

initdb -D /home/pgedge/nodeA --no-locale -E UTF8 

pg_ctl -D /home/pgedge/nodeA -l /home/pgedge/logs/nodeA.log start 


1.2 Configure replication in the postgresql.conf file

After initializing the cluster, edit the postgresql.conf file, located by default in /home/pgedge/nodeA/postgresql.conf. Set the following parameter values:

Shell
 
wal_level = logical

max_replication_slots = 10

max_wal_senders = 10

synchronous_standby_names = '*'

synchronized_standby_slots = 'sb1_slot'

port = 5432


1.3 Update the pg_hba.conf file allowing for Replication Access

The pg_hba.conf file manages client authentication for the PostgreSQL server.  Add the following entry to /home/pgedge/nodeA/pg_hba.conf to ensure access for a replication user:

Shell
 
host replication replicator 127.0.0.1/32 md5


Then, reload the configuration:

Shell
 
pg_ctl -D /home/pgedge/nodeA reload 


1.4 Create a Replication User

Then, log into PostgreSQL and create the replication user:

Shell
 
psql -d postgres -p 5432 
SQL
 
CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'replicator_password';


1.5 Create a Table and Set Up a Publication

Next, you'll need to create a table and create an associated publication:

SQL
 
CREATE TABLE foo (c1 INT PRIMARY KEY);

GRANT SELECT ON foo TO replicator;

CREATE PUBLICATION mypub FOR TABLE foo;


Step 2: Configuring the Physical Standby (NodeB)

2.1 Initialize NodeB

After installing PostgreSQL, initialize NodeB:

Shell
 
mkdir -p /home/pgedge/nodeB 

initdb -D /home/pgedge/nodeB --no-locale -E UTF8 

pg_ctl -D /home/pgedge/nodeB -l /home/pgedge/logs/nodeB.log start


2.1 Create a Base Backup

Then, use pg_basebackup to take a backup of the cluster:

Shell
 
mkdir -p /home/pgedge/nodeB 

pg_basebackup -D /home/pgedge/nodeB -R -X stream -P -h localhost -p 5432 -U replicator


2.2 Configure postgresql.conf on Node-B

Modify the postgresql.conf file (located in /home/pgedge/nodeB/postgresql.conf), setting:

Shell
 
port = 5433

primary_conninfo = 'host=localhost port=5432 user=replicator password=replicator_password dbname=postgres application_name=sb1_slot'

primary_slot_name = 'sb1_slot'

hot_standby_feedback = on

sync_replication_slots = on


2.3 Enable Failover Slot Synchronization

Use the psql client to log in to NodeB:

Shell
 
psql -d postgres -p 5433 


Then, use the following statements to configure replication for NodeB:

SQL
 
ALTER SYSTEM SET sync_replication_slots = on;

ALTER SYSTEM SET hot_standby_feedback = on;

ALTER SYSTEM SET synchronized_standby_slots = 'sb1_slot';


Exit the psql client and restart NodeB:

Shell
 
pg_ctl -D /home/pgedge/nodeB restart 


2.4 Verify Slot Synchronization

Then, reconnect to NodeB with psql and verify that the slots are synchronized:

SQL
 
SELECT slot_name, failover, synced FROM pg_replication_slots;


Step 3: Setting Up the Logical Subscriber (NodeC)

3.1 Initialize the cluster and configure NodeC

After installing PostgreSQL, initialize the cluster; you can use the following commands:

Shell
 
mkdir -p /home/pgedge/nodeC

initdb -D /home/pgedge/nodeC --no-locale -E UTF8


Then, edit the /home/pgedge/nodeC/postgresql.conf file, setting the following parameter values:

Shell
 
wal_level = logical

max_replication_slots = 10

max_wal_senders = 10

sync_replication_slots = on

port = 5444

After editing the configuration file, start NodeC:

pg_ctl -D /home/pgedge/nodeC -l /home/pgedge/logs/nodeC.log start


3.2 Create a Subscription on NodeC

Use the following command to create a subscription on NodeC:

Shell
 
CREATE SUBSCRIPTION foosub CONNECTION 'dbname=postgres host=localhost port=5432 user=replicator password=replicator_password' PUBLICATION mypub WITH (failover = true);


Step 4: Simulating Failover and Ensuring Continuity

You can use the following commands to simulate a failover and confirm that replication continues and data integrity is preserved.

4.1 Simulating a Failover

Use the following commands to simulate a failure of NodeA, followed by promotion from standby to primary of NodeB:

Shell
 
pg_ctl -D /home/pgedge/nodeA stop

pg_ctl -D /home/pgedge/nodeB promote


4.2 Update the Subscription on NodeC

After promoting nodeB, log in to NodeC and update the connection to reflect that NodeB is now the primary node:

SQL
 
ALTER SUBSCRIPTION foosub DISABLE;

ALTER SUBSCRIPTION foosub CONNECTION 'dbname=postgres host=localhost port=5433 user=replicator password=replicator_password';

ALTER SUBSCRIPTION foosub ENABLE;


4.3 Verify Data Continuity

To test replication, use psql to log in to Node-B (now the primary):

SQL
 
INSERT INTO foo VALUES (3), (4);


Check replication on Node-C:

SQL
 
SELECT * FROM foo;

Conclusion

PostgreSQL 17’s failover slot feature allows for seamless failover in logical replication environments. Following the steps outlined in this guide, you can create a high-availability cluster that ensures uninterrupted data flow, even during a primary server failure.

By optimizing configurations and leveraging PostgreSQL 17’s new capabilities, you can create a resilient and efficient database infrastructure for your mission-critical applications.

Replication (computing) PostgreSQL

Opinions expressed by DZone contributors are their own.

Related

  • PostgreSQL Performance Metrics
  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • From Concept to Cloud: Building With Cursor and the Heroku MCP Server
  • Database Query Service With OpenAI and PostgreSQL in .NET

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!