pax_global_header00006660000000000000000000000064150012146000014477gustar00rootroot0000000000000052 comment=87f40b58af6e19eefef1090dd1a4853c78223d69 bgw_replstatus-1.0.8/000077500000000000000000000000001500121460000145525ustar00rootroot00000000000000bgw_replstatus-1.0.8/.github/000077500000000000000000000000001500121460000161125ustar00rootroot00000000000000bgw_replstatus-1.0.8/.github/workflows/000077500000000000000000000000001500121460000201475ustar00rootroot00000000000000bgw_replstatus-1.0.8/.github/workflows/installcheck.yml000066400000000000000000000017141500121460000233410ustar00rootroot00000000000000name: Build on: [push, pull_request] jobs: build: runs-on: ubuntu-latest defaults: run: shell: sh strategy: matrix: pgversion: - 9.5 - 9.6 - 10 - 11 - 12 - 13 - 14 - 15 - 16 env: PGVERSION: ${{ matrix.pgversion }} steps: - name: checkout uses: actions/checkout@v2 - name: install pg run: | sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -v $PGVERSION -p -i sudo -u postgres createuser -s "$USER" - name: build run: | make PROFILE="-Werror" sudo -E make install - name: test run: | sudo pg_conftool set shared_preload_libraries bgw_replstatus sudo pg_ctlcluster $PGVERSION main restart make installcheck - name: show regression diffs if: ${{ failure() }} run: | cat regression.diffs bgw_replstatus-1.0.8/LICENSE000066400000000000000000000001621500121460000155560ustar00rootroot00000000000000This plugins is released under the PostgreSQL License. See https://www.postgresql.org/about/licence/ for details.bgw_replstatus-1.0.8/Makefile000066400000000000000000000004051500121460000162110ustar00rootroot00000000000000MODULES = bgw_replstatus PG_CONFIG=pg_config PGXS := $(shell $(PG_CONFIG) --pgxs) include $(PGXS) NC = nc NC_ARGS = -dq1 LISTEN_ADDRESS = 127.0.0.1 LISTEN_PORT = 5400 installcheck: [ "$(shell $(NC) $(NC_ARGS) $(LISTEN_ADDRESS) $(LISTEN_PORT))" = "MASTER" ] bgw_replstatus-1.0.8/README.md000066400000000000000000000053231500121460000160340ustar00rootroot00000000000000bgw_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. bgw_replstatus-1.0.8/bgw_replstatus.c000066400000000000000000000111231500121460000177610ustar00rootroot00000000000000#include "postgres.h" #include "miscadmin.h" #include "postmaster/bgworker.h" #include "storage/ipc.h" #include "storage/latch.h" #include "access/xlog.h" #include "utils/guc.h" #include "pgstat.h" #include #include PG_MODULE_MAGIC; void _PG_init(void); extern PGDLLEXPORT void bgw_replstatus_main(Datum d); /* flags set by signal handlers */ static volatile sig_atomic_t got_sigterm = false; /* config */ static int portnum = 5400; static char *bindaddr = NULL; /* * Perform a clean shutdown on SIGTERM. To do that, just * set a boolean in the sig handler and then set our own * latch to break the main loop. */ static void bgw_replstatus_sigterm(SIGNAL_ARGS) { int save_errno = errno; got_sigterm = true; SetLatch(MyLatch); errno = save_errno; } void bgw_replstatus_main(Datum d) { int enable = 1; int listensocket; struct sockaddr_in addr; pqsignal(SIGTERM, bgw_replstatus_sigterm); BackgroundWorkerUnblockSignals(); /* Setup our listening socket */ listensocket = socket(AF_INET, SOCK_STREAM, 0); if (listensocket == -1) ereport(ERROR, (errmsg("bgw_replstatus: could not create socket: %m"))); if (setsockopt(listensocket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) != 0) ereport(ERROR, (errmsg("bgw_replstatus: could not set socket option: %m"))); if (!pg_set_noblock(listensocket)) ereport(ERROR, (errmsg("bgw_replstatus: could not set non blocking socket: %m"))); memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(portnum); if (bindaddr == NULL || strlen(bindaddr) == 0) addr.sin_addr.s_addr = INADDR_ANY; else { if (inet_aton(bindaddr, &addr.sin_addr) == 0) ereport(ERROR, (errmsg("bgw_replstatus: could not translate IP address '%s'", bindaddr))); } if (bind(listensocket, &addr, sizeof(addr)) != 0) ereport(ERROR, (errmsg("bgw_replstatus: could not bind socket: %m"))); if (listen(listensocket, 5) != 0) ereport(ERROR, (errmsg("bgw_replstatus: could not listen on socket: %m"))); /* * Loop forever looking for new connections. Terminate on SIGTERM, * which is sent by the postmaster when it wants us to shut down. * XXX: we don't currently support changing the port at runtime. */ while (!got_sigterm) { int rc; rc = WaitLatchOrSocket(MyLatch, WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE, listensocket, -1 #if PG_VERSION_NUM >= 100000 /* 10.0 introduced PG_WAIT_EXTENSION */ ,PG_WAIT_EXTENSION); #else ); #endif ResetLatch(MyLatch); if (rc & WL_POSTMASTER_DEATH) proc_exit(1); else if (rc & WL_SOCKET_READABLE) { char *status_str; socklen_t addrsize = sizeof(addr); int worksock = accept4(listensocket, &addr, &addrsize, SOCK_NONBLOCK); if (worksock == -1) { ereport(LOG, (errmsg("bgw_replstatus: could not accept socket: %m"))); continue; } status_str = RecoveryInProgress() ? "STANDBY" : "MASTER"; if (write(worksock, status_str, strlen(status_str)) != strlen(status_str)) { ereport(LOG, (errmsg("bgw_replstatus: could not write %s: %m", status_str))); close(worksock); continue; } if (close(worksock) != 0) { ereport(LOG, (errmsg("bgw_replstatus: could not close working socket: %m"))); continue; } } } ereport(LOG, (errmsg("bgw_replstatus: shutting down"))); close(listensocket); proc_exit(0); } /* * Initialization entrypoint */ void _PG_init(void) { BackgroundWorker worker; /* * Define our GUCs so we can get the configuration values. */ DefineCustomIntVariable("bgw_replstatus.port", "TCP port to bind to", NULL, &portnum, 5400, 1025, 65535, PGC_POSTMASTER, 0, NULL, NULL, NULL); DefineCustomStringVariable("bgw_replstatus.bind", "IP address to bind to", NULL, &bindaddr, "", PGC_POSTMASTER, 0, NULL, NULL, NULL); if (!process_shared_preload_libraries_in_progress) return; /* * Set up our bgworker */ MemSet(&worker, 0, sizeof(worker)); worker.bgw_flags = BGWORKER_SHMEM_ACCESS; worker.bgw_start_time = BgWorkerStart_PostmasterStart; worker.bgw_restart_time = 30; /* Restart after 30 seconds -- just so we don't end up in a hard loop if something fails */ sprintf(worker.bgw_library_name, "bgw_replstatus"); sprintf(worker.bgw_function_name, "bgw_replstatus_main"); worker.bgw_notify_pid = 0; snprintf(worker.bgw_name, BGW_MAXLEN, "bgw_replstatus"); RegisterBackgroundWorker(&worker); } bgw_replstatus-1.0.8/debian/000077500000000000000000000000001500121460000157745ustar00rootroot00000000000000bgw_replstatus-1.0.8/debian/changelog000066400000000000000000000053171500121460000176540ustar00rootroot00000000000000bgw-replstatus (1.0.8-1) unstable; urgency=medium * Support PostgreSQL 18. -- Christoph Berg Sun, 20 Apr 2025 17:57:24 +0200 bgw-replstatus (1.0.7-3) unstable; urgency=medium * Upload for PostgreSQL 17. * Restrict to 64-bit architectures. * Mark postgresql-all as . -- Christoph Berg Tue, 10 Sep 2024 13:32:04 +0200 bgw-replstatus (1.0.7-2) unstable; urgency=medium * Upload for PostgreSQL 16. * Use ${postgresql:Depends}. -- Christoph Berg Sat, 16 Sep 2023 14:42:46 +0200 bgw-replstatus (1.0.7-1) unstable; urgency=medium * Mark bgw_replstatus_main extern PGDLLEXPORT so PG16 can find it. -- Christoph Berg Sun, 02 Jul 2023 23:00:08 +0200 bgw-replstatus (1.0.6-3) unstable; urgency=medium * Upload for PostgreSQL 15. -- Christoph Berg Thu, 20 Oct 2022 15:21:32 +0200 bgw-replstatus (1.0.6-2) unstable; urgency=medium * Fix GitHub watch file. -- Christoph Berg Tue, 04 Jan 2022 16:34:43 +0100 bgw-replstatus (1.0.6-1) unstable; urgency=medium * Upload for PostgreSQL 14. -- Christoph Berg Mon, 11 Oct 2021 10:33:45 +0200 bgw-replstatus (1.0.5) unstable; urgency=medium [ Debian Janitor ] * Use secure copyright file specification URI. * Bump debhelper from deprecated 9 to 12. * Set debhelper-compat version in Build-Depends. * Update standards version to 4.2.1, no changes needed. [ Christoph Berg ] * Upload for PostgreSQL 13. * Use dh --with pgxs. * Build-depend on netcat-openbsd and run tests at build time as well. * DH 13. * R³: no. * debian/tests: Use 'make' instead of postgresql-server-dev-all. * Add myself to uploaders. -- Christoph Berg Sun, 18 Oct 2020 21:51:33 +0200 bgw-replstatus (1.0.4) unstable; urgency=medium * Team upload for PostgreSQL 12. -- Christoph Berg Tue, 29 Oct 2019 09:40:31 +0100 bgw-replstatus (1.0.3) unstable; urgency=medium * Team upload for PostgreSQL 11. * Priority: optional. * Update PostgreSQL team address. * Bump S-V. -- Christoph Berg Thu, 11 Oct 2018 22:15:39 +0200 bgw-replstatus (1.0.2) unstable; urgency=medium * Team upload for PostgreSQL 10. -- Christoph Berg Thu, 21 Sep 2017 18:42:06 +0200 bgw-replstatus (1.0.1) unstable; urgency=medium * Team upload. * Use netcat to run basic functionality test on installcheck. * Add copyright file and long package description. -- Christoph Berg Fri, 31 Mar 2017 20:24:44 +0200 bgw-replstatus (1.0.0) unstable; urgency=medium * Initial release. -- Magnus Hagander Fri, 31 Mar 2017 14:01:35 +0200 bgw_replstatus-1.0.8/debian/control000066400000000000000000000022561500121460000174040ustar00rootroot00000000000000Source: bgw-replstatus Section: database Priority: optional Maintainer: Magnus Hagander Uploaders: Debian PostgreSQL Maintainers , Christoph Berg , Build-Depends: architecture-is-64-bit , debhelper-compat (= 13), netcat-openbsd, postgresql-all , postgresql-server-dev-all (>= 217~), Standards-Version: 4.7.0 Rules-Requires-Root: no Homepage: https://github.com/mhagander/bgw_replstatus Vcs-Git: https://github.com/mhagander/bgw_replstatus.git Vcs-Browser: https://github.com/mhagander/bgw_replstatus Package: postgresql-17-bgw-replstatus Architecture: any Depends: ${misc:Depends}, ${postgresql:Depends}, ${shlibs:Depends}, Description: report whether PostgreSQL node is master or standby bgw_replstatus is a tiny PostgreSQL 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 TCP port. A connection to this port will get a TCP response back saying either MASTER or STANDBY depending on the current state of the node. bgw_replstatus-1.0.8/debian/control.in000066400000000000000000000022651500121460000200110ustar00rootroot00000000000000Source: bgw-replstatus Section: database Priority: optional Maintainer: Magnus Hagander Uploaders: Debian PostgreSQL Maintainers , Christoph Berg , Build-Depends: architecture-is-64-bit , debhelper-compat (= 13), netcat-openbsd, postgresql-all , postgresql-server-dev-all (>= 217~), Standards-Version: 4.7.0 Rules-Requires-Root: no Homepage: https://github.com/mhagander/bgw_replstatus Vcs-Git: https://github.com/mhagander/bgw_replstatus.git Vcs-Browser: https://github.com/mhagander/bgw_replstatus Package: postgresql-PGVERSION-bgw-replstatus Architecture: any Depends: ${misc:Depends}, ${postgresql:Depends}, ${shlibs:Depends}, Description: report whether PostgreSQL node is master or standby bgw_replstatus is a tiny PostgreSQL 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 TCP port. A connection to this port will get a TCP response back saying either MASTER or STANDBY depending on the current state of the node. bgw_replstatus-1.0.8/debian/copyright000066400000000000000000000024611500121460000177320ustar00rootroot00000000000000Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Upstream-Name: bgw_replstatus Source: https://github.com/mhagander/bgw_replstatus Files: * Copyright: Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group Portions Copyright (c) 1994, The Regents of the University of California License: PostgreSQL Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies. . IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. . THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. bgw_replstatus-1.0.8/debian/pgversions000066400000000000000000000000051500121460000201110ustar00rootroot000000000000009.5+ bgw_replstatus-1.0.8/debian/rules000077500000000000000000000003441500121460000170550ustar00rootroot00000000000000#!/usr/bin/make -f override_dh_installdocs: dh_installdocs --all README.* override_dh_pgxs_test: +pg_buildext -o 'shared_preload_libraries=bgw_replstatus' installcheck . . postgresql-%v-bgw-replstatus %: dh $@ --with pgxs bgw_replstatus-1.0.8/debian/source/000077500000000000000000000000001500121460000172745ustar00rootroot00000000000000bgw_replstatus-1.0.8/debian/source/format000066400000000000000000000000141500121460000205020ustar00rootroot000000000000003.0 (quilt) bgw_replstatus-1.0.8/debian/tests/000077500000000000000000000000001500121460000171365ustar00rootroot00000000000000bgw_replstatus-1.0.8/debian/tests/control000066400000000000000000000000651500121460000205420ustar00rootroot00000000000000Depends: @, make, netcat-openbsd Tests: installcheck bgw_replstatus-1.0.8/debian/tests/installcheck000077500000000000000000000001211500121460000215220ustar00rootroot00000000000000#!/bin/sh pg_buildext -o 'shared_preload_libraries=bgw_replstatus' installcheck bgw_replstatus-1.0.8/debian/watch000066400000000000000000000001171500121460000170240ustar00rootroot00000000000000version=4 https://github.com/mhagander/bgw_replstatus/tags .*/([0-9.]+).tar.gz