Managing High Availability in PostgreSQL – Part II
Tags: postgres, database, sql, replication
Published: 2019-03-12 16:09:16

---
title: Managing High Availability in PostgreSQL – Part II
published: true
description: Deploying PostgreSQL in the cloud and want to achieve high availability? Learn how to setup with open source tool, Replication Manager (repmgr) by 2ndQuadrant.
tags: PostgreSQL, Database, SQL, Replication,
canonical_url: https://scalegrid.io/blog/managing-high-availability-in-postgresql-part-2/
---
<link rel="canonical" href="https://scalegrid.io/blog/managing-high-availability-in-postgresql-part-2/" />

<a href="https://scalegrid.io/blog/managing-high-availability-in-postgresql-part-2/"><img src="https://scalegrid.io/blog/wp-content/uploads/2019/02/Managing-High-Availability-in-PostgreSQL-Part-2-ScaleGrid-Blog.png"></a>
<p style="text-align: justify;">Are you deploying <a style="color: #2da964;" href="https://scalegrid.io/postgresql.html" target="_blank">PostgreSQL in the cloud</a> and want to understand your options for achieving high availability? In our previous blog post, <a style="color: #2da964;" href="https://scalegrid.io/blog/managing-high-availability-in-postgresql-part-1/" target="_blank">Managing High Availability in PostgreSQL – Part I</a>, we discussed the capabilities and functioning of PostgreSQL Automatic Failover (PAF) by ClusterLabs. In <a href="https://scalegrid.io/blog/managing-high-availability-in-postgresql-part-2/">Part II</a>, we're introducing you to an alternative open source tool, Replication Manager from 2ndQuadrant, to be closely followed by Part III where we dive into our third alternative, Patroni by Zalando.</p>

<h2 style="padding-top: 15px;">Replication Manager (repmgr)</h2>
<p style="text-align: justify;"><a style="color: #2da964;" href="https://repmgr.org/" target="_blank">repmgr</a> is an open-source tool suite developed by 2ndQuadrant for managing replication and failover of your <a style="color: #2da964;" href="https://www.postgresql.org/" target="_blank">PostgreSQL</a> clusters. It provides the tools to setup, configure, manage, and monitor replication of PostgreSQL, and also enables you to perform manual switchover and failover tasks using repmgr utility. This free tool supports and enhances PostgreSQL's built-in <a style="color: #2da964;" title="Getting Started with PostgreSQL Streaming Replication" href="https://scalegrid.io/blog/getting-started-with-postgresql-streaming-replication/" target="_blank">streaming replication</a>.</p>
<p style="text-align: justify;">Replication Manager provides two main tools to manage replication and failover of PostgreSQL.</p>

<h3 style="padding-top: 15px;">repmgr</h3>
<ul>
	<li style="text-align: justify;">A command-line interface utility which enables you to perform various administrative tasks.</li>
	<li style="text-align: justify;">repmgr enables you to setup standby servers, promote standbys, do a switchover, and monitor the status of your PostgreSQL cluster.</li>
	<li style="text-align: justify;">It also provides dry run option for almost all of the administrative commands.</li>
</ul>
<h3 style="padding-top: 15px;">repmgrd</h3>
<p style="text-align: justify;">This is the daemon which:</p>

<ul>
	<li style="text-align: justify;">Actively monitors the PostgreSQL clusters and performs necessary actions based on the state of the cluster.</li>
	<li style="text-align: justify;">Performs automatic failover in case the primary node goes down by promoting the most eligible standby as the new primary.</li>
	<li style="text-align: justify;">Provides an option to monitor and store the data related to replication performance.</li>
	<li style="text-align: justify;">Provides notification by invoking the user scripts for registered events.</li>
</ul>
<h2 style="padding-top: 15px;">How it Works</h2>
<p style="text-align: justify;"><span style="font-weight: 400;">repmrg not only manages the replication of PostgreSQL clusters, but also has capabilities for setting up the standby servers for replication. Following the initial installation, we need to make changes to the repmgr configuration file (repmgr.conf) with the required details on each server. When a server is configured, it needs to be registered with repmgr using repmgr primary/standby register command. First, the primary node is setup and registered. Then, standby servers are created and configured using the repmgr standby clone command which clones the PostgreSQL standby node from another PostgreSQL server.</span></p>
<p style="text-align: justify;">Replication Manager makes use of PostgreSQL extensions feature and creates its own schema on the cluster database to store the cluster-related information. Installation of the extension and creation of the schema happens during the registration of the primary server using repmgr. Once the setup is complete, manual administrative operations such as promote, follow, switchover, etc. can be done using repmgr utility. For switchover operation, it requires passwordless SSH to be setup between the nodes.</p>
<p style="text-align: justify;">Automatic failover can be setup using <a style="color: #2da964;" href="https://repmgr.org/docs/4.0/repmgrd-basic-configuration.html" target="_blank">repmgrd</a>. repmgrd requires a shared library ‘repmgr’ to be loaded at the time of starting the PostgreSQL server. The library name should be mentioned in the <strong>shared_preload_libraries</strong> configuration parameter in the postgresql.conf file. Also, for repmgrd to work, <strong>failover=automatic</strong> parameter need to be set in repmgr.conf file. Once all these parameters are set, repmgrd daemon starts to actively monitor the cluster. If there is any failure in primary node, it will try to reconnect multiple times. When all attempts to connect to primary fail, the most eligible standby is chosen by election as the new primary by repmgrd.</p>
<p style="text-align: justify;">repmgr also supports event notifications. It has a set of predefined events and stores each occurrence of these events in the repmgr.events table. repmgr enables event notifications to be passed to a user-defined program or script which can take further action, such as sending an email or triggering any alert. This is done by setting the <strong>event_notification_command</strong> parameter in repmgr.conf.</p>

<h3 style="padding-top: 15px;">How Does It Handle The Split Brain Scenario?</h3>
<p style="text-align: justify;">repmgr tackles <a style="color: #2da964;" href="https://repmgr.org/docs/4.2/repmgrd-network-split.html" target="_blank">split brain scenarios</a> using the <strong>location</strong> parameter, where each node should specify the location parameter based on the datacenter in which it is placed. In case of any network split, repmgr will ensure the promotion of the node which is in the same location as the primary. If it doesn’t find any node in that location, it will not promote any node in any location.</p>
<p style="text-align: justify;">It also handles network isolation in the event of an even number of servers in a cluster. This is done using an extra node called the <a style="color: #2da964;" href="https://repmgr.org/docs/4.2/repmgr-concepts.html#WITNESS-SERVER" target="_blank">witness server</a>. The witness server is a node which is considered only for the majority vote count. There will be no PostgreSQL installation on that server, and hence, no part to play in replication.</p>

<h3 style="padding-top: 15px;">Are There Any Setup Requirements?</h3>
<ul>
	<li style="text-align: justify;">repmgr will require a <a style="color: #2da964;" href="https://scalegrid.io/pricing.html#section_pricing_dedicated" target="_blank">dedicated database</a> and a user with superuser privileges. However, there's also an option to provide a superuser if you are not willing to give the superuser access to repmgr user.</li>
	<li style="text-align: justify;">If you want repmgr to copy configuration files which are located outside the PostgreSQL data directory, and/or to test switchover functionality, you will also need passwordless SSH connections between both servers, and rsync should be installed.</li>
	<li style="text-align: justify;">If you intend to use service-based commands other than pg_ctl (which is used by repmgr by default) to start, stop, reload, and restart, you can specify them in repmgr configuration file (repmgr.conf).</li>
	<li style="text-align: justify;">The basic configuration parameters required in repmgr configuration file are as follows:
<table class="table table-bordered" style="height: 46px;" width="353">
<tbody>
<tr>
<td style="background-color: #d3d3d3;"><strong>node_id (int)</strong> - A unique integer greater than zero which identifies the node. <strong>node_name (string)</strong> - An arbitrary (but unique) string, using the server's hostname or another identifier unambiguously associated with the server is recommended to avoid confusion.

<strong>conninfo (string)</strong> - Database connection information as a conninfo string. All servers in the cluster must be able to connect to the local node using this string.

<strong>data_directory (string)</strong> - The node's data directory. This is needed by repmgr to perform operations when the PostgreSQL instance is not running and there's no other way of determining the data directory.</td>
</tr>
</tbody>
</table>
</li>
</ul>
<h2 style="padding-top: 15px;">repmgr Pros</h2>
<ul>
	<li style="text-align: justify;">Repmgr provides utilities that help to setup primary and standby nodes and configure replication.</li>
	<li style="text-align: justify;">It doesn’t use any extra ports for communication. If you want to perform switchover, only then will it require passwordless SSH to be configured.</li>
	<li style="text-align: justify;">Provides notification by invoking the user scripts for the registered events.</li>
	<li style="text-align: justify;">Performs automatic failover in case of primary server failure.</li>
</ul>
<h2 style="padding-top: 15px;">repmgr Cons</h2>
<ul>
	<li style="text-align: justify;">repmgr doesn’t detect if the standby is misconfigured with an unknown or non-existent node in recovery configuration. The node will be shown as standby even if it is running without connecting to the primary/cascading standby node.</li>
	<li style="text-align: justify;">Cannot retrieve the status of another node from a node where PostgreSQL service is down. Hence, it doesn't provide a distributed control solution.</li>
	<li style="text-align: justify;">It doesn’t handle recovering the health of individual nodes.</li>
</ul>

<h2 style="padding-top: 15px;">High Availability Test Scenarios</h2>
<p style="text-align: justify;">We conducted a few tests on PostgreSQL high availability management using repmgr. All of these tests were run while the application was running and inserting data to the PostgreSQL database. The application was written using PostgreSQL Java <a style="color: #2da964;" href="https://jdbc.postgresql.org/" target="_blank">JDBC Driver</a> leveraging the connection failover capability.</p>

<h3 style="padding-top: 15px;">Standby Server Tests</h3>
<table class="table table-bordered">
<tbody>
<tr>
<th style="width: 5%; vertical-align: middle; background-color: #d9fce9;">Sl. No</th>
<th style="width: 25%; vertical-align: middle; background-color: #d9fce9;">Test Scenario</th>
<th style="width: 70%; vertical-align: middle; background-color: #d9fce9;">Observation</th>
</tr>
<tr>
<td style="vertical-align: middle;">1</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Kill the PostgreSQL process</td>
<td style="vertical-align: middle;">Standby server was marked as failed. There was no disruption in writer application. Manual intervention was required to start the PostgreSQL process again.</td>
</tr>
<tr>
<td style="vertical-align: middle;">2</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Stop the PostgreSQL process</td>
<td style="vertical-align: middle;">Standby server was marked as failed. There was no disruption in writer application. Manual intervention was required to start the PostgreSQL process again.</td>
</tr>
<tr>
<td style="vertical-align: middle;">3</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Reboot the server</td>
<td style="vertical-align: middle;">Standby server was marked as failed. Once the server came up after reboot, PostgreSQL was started manually and server was marked as running. There was no disruption in writer application.</td>
</tr>
<tr>
<td style="vertical-align: middle;">4</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Stop the repmgrd process</td>
<td style="vertical-align: middle;">The standby server will not be a part of the automated failover situation. PostgreSQL service was found to be running. There was no disruption in writer application.</td>
</tr>
</tbody>
</table>
<h3 style="padding-top: 15px;">Master/Primary Server Tests</h3>
<table class="table table-bordered">
<tbody>
<tr>
<td style="width: 5%; vertical-align: middle; background-color: #d9fce9;"><strong>Sl. No</strong></td>
<td style="width: 25%; vertical-align: middle; background-color: #d9fce9;"><strong>Test Scenario</strong></td>
<td style="width: 70%; vertical-align: middle; background-color: #d9fce9;"><strong>Observation</strong></td>
</tr>
<tr>
<td style="vertical-align: middle;">1</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Kill the PostgreSQL process</td>
<td style="vertical-align: middle;">
<ul>
	<li style="padding-bottom: 10px;">repmgrd started the health check for the primary server connection on all standby servers for a fixed interval.</li>
	<li style="padding-bottom: 10px;">When all retries failed, an election was triggered on all the standby servers. As a result of the election, the standby which had the latest received LSN got promoted.</li>
	<li style="padding-bottom: 10px;">The standby servers which lost the election will wait for the notification from new master node and follow it once they receive the notification.</li>
	<li>There was downtime in the writer application. Manual intervention was required to start the PostgreSQL process again.</li>
</ul>
</td>
</tr>
<tr>
<td style="vertical-align: middle;">2</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Stop the PostgreSQL process and bring it back immediately after health check expiry</td>
<td style="vertical-align: middle;">
<ul>
	<li style="padding-bottom: 10px;">repmgrd started the health check for the primary server connection on all standby servers for a fixed interval.</li>
	<li style="padding-bottom: 10px;">When all retries failed, an election was triggered on all the standby nodes.</li>
	<li style="padding-bottom: 10px;">However, the newly elected master didn’t notify the existing standby servers since the old master was back.</li>
	<li>Cluster was left in an indeterminate state and manual intervention was required.</li>
</ul>
</td>
</tr>
<tr>
<td style="vertical-align: middle;">3</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Reboot the server</td>
<td style="vertical-align: middle;">
<ul>
	<li style="padding-bottom: 10px;">repmgrd started the election when master connection health check failed on all standby servers.</li>
	<li style="padding-bottom: 10px;">The eligible standby was promoted. When this server came back, it didn’t join the cluster and was marked failed.</li>
	<li>repmgr node rejoin command was ran to add the server back to the cluster. There was downtime in the writer application.</li>
</ul>
</td>
</tr>
<tr>
<td style="vertical-align: middle;">4</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Stop the repmgr process</td>
<td style="vertical-align: middle;">
<ul>
	<li style="padding-bottom: 10px;">The primary server will not be a part of the automated failover situation.</li>
	<li>PostgreSQL service was found to be running. There was no disruption in the writer application.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<h3 style="padding-top: 15px;">Network Isolation Tests</h3>
<table class="table table-bordered">
<tbody>
<tr>
<td style="width: 5%; vertical-align: middle; background-color: #d9fce9;"><strong>Sl. No</strong></td>
<td style="width: 25%; vertical-align: middle; background-color: #d9fce9;"><strong>Test Scenario</strong></td>
<td style="width: 70%; vertical-align: middle; background-color: #d9fce9;"><strong>Observation</strong></td>
</tr>
<tr>
<td style="vertical-align: middle;">1</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Network isolate the primary server from other servers (all have same value for location in repmgr configuration)</td>
<td style="vertical-align: middle;">
<ul>
	<li style="padding-bottom: 10px;">repmgrd started the election when master connection health check failed on all standby servers.</li>
	<li style="padding-bottom: 10px;">The eligible standby was promoted, but the PostgreSQL process was still running on the old master node.</li>
	<li>There were two nodes running as master. Manual intervention was required after the network isolation was corrected.</li>
</ul>
</td>
</tr>
<tr>
<td style="vertical-align: middle;">2</td>
<td style="vertical-align: middle; background-color: #f5f5f5;">Network isolate the primary server from other servers (the standby servers has same value for location but primary had a different value for location in repmgr configuration)</td>
<td style="vertical-align: middle;">
<ul>
	<li style="padding-bottom: 10px;">repmgrd started the election when master connection health check failed on all standby servers.</li>
	<li style="padding-bottom: 10px;">But, there was no new master elected since the standby servers had a location different from that of the primary.</li>
	<li>repmgrd went into degrade monitoring mode. PostgreSQL was running on all the nodes and there was only one master in the cluster.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<h2>Inference</h2>
<p style="text-align: justify;">repmgr provides several commands to setup and monitor PostgreSQL replication. It is feature-rich and also eases the job of the database administrator (DBA). However, it's not a full fledged high availability management tool since it will not manage the resources. Manual intervention is required to ensure the resource is in proper state.</p>
<p style="text-align: justify; padding-bottom: 20px;">So, in this post, we’ve discussed the capabilities and workings of Replication Manager by 2ndQuadrant. In our next post, we’ll discuss the same high availability aspects using Patroni by Zalando. For users looking to automate their high availability in the cloud, check out our <a style="color: #2da964;" href="https://scalegrid.io/postgresql/azure.html" target="_blank">PostgreSQL on Azure</a> and <a style="color: #2da964;" href="https://scalegrid.io/postgresql/aws.html" target="_blank">PostgreSQL on AWS</a> fully managed solutions.</p>