Code Monkey home page Code Monkey logo

bgw_replstatus's Introduction

bgw_replstatus

bgw_replstatus is a tiny background worker to cheaply report the replication status of a node. It's intended to be polled by a load balancer such as haproxy.

When installed, a background worker will be started that listens on a defined TCP port (configured bgw_replstatus.port). Any connection to this port will get a TCP response back (no request necessary, response will be sent immediately on connect) saying either MASTER or STANDBY depending on the current state of the node. The connection is then automatically closed.

Using a background worker like this will make polling a lot more light weight than making a full PostgreSQL connection, logging in, and checking the status.

Installing

Build and install is done using PGXS. As long as pg_config is available in the path, build and install using:

$ make
$ make install

Once the binary is installed, it needs to be enabled in shared_preload_libraries in postgresql.conf:

shared_preload_libraries = 'bgw_replstatus'

If other libraries are already configured for loading, it can be appended to the end of the list. Order should not matter.

Configuration

By default, the background worker will listen to port 5400 on a wild card IP address. There is no verification of the source done, so protect the port with a proper host firewall!!!

To change the port, set the value of bgw_replstatus.port to another value. Any TCP port above 1024 will work (but don't pick the same one as PostgreSQL itself...).

To change the socket to bind to a specific IP address, set bgw_replstatus.bind to an IP address, which will cause the background worker to bind to this IP on the defined port.

There is no support for multiple ports or multiple IP addresses.

Example usage

In it's simplest form, you can just verify the status of your system with nc or telnet:

$ nc localhost 5400
MASTER

Since the text coming back is easily identifiable, it's easy enough to integrate with a load balancer such as haproxy. This example haproxy configuration will show how to ensure that haproxy is connected to the master node of the cluster, and automatically switches over to the backup node if the master goes down and the backup is promoted. Multiple backups can be used.

frontend test
	bind 127.0.0.1:5999
	default_backend pgcluster

backend pgcluster
	mode tcp
	option tcp-check
	tcp-check expect string MASTER
	server s1 127.0.0.1:5500 check port 5400
	server s2 127.0.0.1:5501 check port 5401 backup
	server s3 127.0.0.1:5502 check port 5402 backup

In this example all nodes are local, with postgres running on ports 5500/5501/5502 with bgw_replstatus bound to ports 5400/5401/5402 respectively.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.