mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-23 15:16:29 +00:00
Compare commits
42 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efd50f11ac | ||
|
|
45a39084ed | ||
|
|
94c73a016f | ||
|
|
be5cbe4ddd | ||
|
|
30d35d5b4c | ||
|
|
fa889a11ac | ||
|
|
f4087d0a32 | ||
|
|
a55d7a4bd3 | ||
|
|
5d8cf6abe0 | ||
|
|
9caa243354 | ||
|
|
6880483947 | ||
|
|
3d89fdadab | ||
|
|
6e9e4e05ae | ||
|
|
17a160e970 | ||
|
|
e0e01aa9db | ||
|
|
b09eff9f76 | ||
|
|
3c5d82b9ef | ||
|
|
257dbc4f42 | ||
|
|
2a64099163 | ||
|
|
41c05bea7b | ||
|
|
7d76d86e19 | ||
|
|
36d5b5bc24 | ||
|
|
c543402d65 | ||
|
|
d0959b953e | ||
|
|
0660bded0b | ||
|
|
209a0c64d2 | ||
|
|
fd76ec6283 | ||
|
|
7d579cf71f | ||
|
|
d790ef740b | ||
|
|
aa6633b027 | ||
|
|
c3bffce379 | ||
|
|
78aea00a6d | ||
|
|
91601204b5 | ||
|
|
c91ddc2f5e | ||
|
|
72f74dd7a7 | ||
|
|
901d07fa92 | ||
|
|
f0e609bcd4 | ||
|
|
94c9c3a5c6 | ||
|
|
3af5243bcc | ||
|
|
85bbae462a | ||
|
|
14e49d41c2 | ||
|
|
1bd8a703c8 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,9 +1,6 @@
|
|||||||
*~
|
*~
|
||||||
*.o
|
*.o
|
||||||
*.so
|
|
||||||
repmgr
|
repmgr
|
||||||
repmgrd
|
repmgrd
|
||||||
README.htm*
|
README.htm*
|
||||||
README.pdf
|
README.pdf
|
||||||
sql/repmgr_funcs.so
|
|
||||||
sql/repmgr_funcs.sql
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2010-2014, 2ndQuadrant Limited
|
Copyright (c) 2010-2011, 2ndQuadrant Limited
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
|||||||
6
CREDITS
6
CREDITS
@@ -10,7 +10,5 @@ Hannu Krosing <hannu@2ndQuadrant.com>
|
|||||||
Cédric Villemain <cedric@2ndquadrant.com>
|
Cédric Villemain <cedric@2ndquadrant.com>
|
||||||
Charles Duffy <charles@dyfis.net>
|
Charles Duffy <charles@dyfis.net>
|
||||||
Daniel Farina <daniel@heroku.com>
|
Daniel Farina <daniel@heroku.com>
|
||||||
Shawn Ellis <shawn.ellis17@gmail.com>
|
Marco Nenciarini <marco.nenciarini@2ndquadrant.it>
|
||||||
Jay Taylor <jay@jaytaylor.com>
|
Carlo Ascani <carlo.ascani@2ndquadrant.it>
|
||||||
Christian Kruse <christian@2ndQuadrant.com>
|
|
||||||
Krzysztof Gajdemski <songo@debian.org.pl>
|
|
||||||
|
|||||||
29
HISTORY
29
HISTORY
@@ -1,30 +1,3 @@
|
|||||||
2.0stable 2014-01-30
|
|
||||||
Documentation fixes (Christian)
|
|
||||||
General refactoring, code quality improvements and stabilization work (Christian)
|
|
||||||
Added proper daemonizing (-d/--daemonize) (Christian)
|
|
||||||
Added PID file handling (-p/--pid-file) (Christian)
|
|
||||||
New config option: monitor_interval_secs (Christian)
|
|
||||||
New config option: retry_promote_interval (Christian)
|
|
||||||
New config option: logfile (Christian)
|
|
||||||
New config option: pg_bindir (Christian)
|
|
||||||
New config option: pgctl_options (Christian)
|
|
||||||
|
|
||||||
2.0beta2 2013-12-19
|
|
||||||
Improve autofailover logic and algorithms (Jaime, Andres)
|
|
||||||
Ignore pg_log when cloning (Jaime)
|
|
||||||
Add timestamps to log line in stderr (Christian)
|
|
||||||
Correctly check wal_keep_segments (Jay Taylor)
|
|
||||||
Add a ssh_options parameter (Jay Taylor)
|
|
||||||
|
|
||||||
2.0beta1 2012-07-27
|
|
||||||
Make CLONE command try to make an exact copy including $PGDATA location (Cedric)
|
|
||||||
Add detection of master failure (Jaime)
|
|
||||||
Add the notion of a witness server (Jaime)
|
|
||||||
Add autofailover capabilities (Jaime)
|
|
||||||
Add a configuration parameter to indicate the script to execute on failover or follow (Jaime)
|
|
||||||
Make the monitoring optional and turned off by default, it can be turned on with --monitoring-history switch (Jaime)
|
|
||||||
Add tunables to specify number of retries to reconnect to master and the time between them (Jaime)
|
|
||||||
|
|
||||||
1.2.0 2012-07-27
|
1.2.0 2012-07-27
|
||||||
Test ssh connection before trying to rsync (Cédric)
|
Test ssh connection before trying to rsync (Cédric)
|
||||||
Add CLUSTER SHOW command (Carlo)
|
Add CLUSTER SHOW command (Carlo)
|
||||||
@@ -36,7 +9,7 @@
|
|||||||
1.1.1 2012-04-18
|
1.1.1 2012-04-18
|
||||||
Add --ignore-rsync-warning (Cédric)
|
Add --ignore-rsync-warning (Cédric)
|
||||||
Add strnlen for compatibility with OS X (Greg)
|
Add strnlen for compatibility with OS X (Greg)
|
||||||
Improve performance of the repl_status view (Jaime)
|
Improve performance of repl_status view (Jaime)
|
||||||
Remove last argument from log_err (Jaime, Reported by Jeroen Dekkers)
|
Remove last argument from log_err (Jaime, Reported by Jeroen Dekkers)
|
||||||
Complete documentation about possible error conditions (Jaime)
|
Complete documentation about possible error conditions (Jaime)
|
||||||
Document how to clean history (Jaime)
|
Document how to clean history (Jaime)
|
||||||
|
|||||||
21
Makefile
21
Makefile
@@ -1,6 +1,6 @@
|
|||||||
#
|
#
|
||||||
# Makefile
|
# Makefile
|
||||||
# Copyright (c) 2ndQuadrant, 2010-2014
|
# Copyright (c) 2ndQuadrant, 2010-2011
|
||||||
|
|
||||||
repmgrd_OBJS = dbutils.o config.o repmgrd.o log.o strutil.o
|
repmgrd_OBJS = dbutils.o config.o repmgrd.o log.o strutil.o
|
||||||
repmgr_OBJS = dbutils.o check_dir.o config.o repmgr.o log.o strutil.o
|
repmgr_OBJS = dbutils.o check_dir.o config.o repmgr.o log.o strutil.o
|
||||||
@@ -11,18 +11,15 @@ PG_CPPFLAGS = -I$(libpq_srcdir)
|
|||||||
PG_LIBS = $(libpq_pgport)
|
PG_LIBS = $(libpq_pgport)
|
||||||
|
|
||||||
all: repmgrd repmgr
|
all: repmgrd repmgr
|
||||||
$(MAKE) -C sql
|
|
||||||
|
|
||||||
repmgrd: $(repmgrd_OBJS)
|
repmgrd: $(repmgrd_OBJS)
|
||||||
$(CC) $(CFLAGS) $(repmgrd_OBJS) $(PG_LIBS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o repmgrd
|
$(CC) $(CFLAGS) $(repmgrd_OBJS) $(PG_LIBS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o repmgrd
|
||||||
$(MAKE) -C sql
|
|
||||||
|
|
||||||
repmgr: $(repmgr_OBJS)
|
repmgr: $(repmgr_OBJS)
|
||||||
$(CC) $(CFLAGS) $(repmgr_OBJS) $(PG_LIBS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o repmgr
|
$(CC) $(CFLAGS) $(repmgr_OBJS) $(PG_LIBS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o repmgr
|
||||||
|
|
||||||
ifdef USE_PGXS
|
ifdef USE_PGXS
|
||||||
PG_CONFIG = pg_config
|
PGXS := $(shell pg_config --pgxs)
|
||||||
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
|
||||||
include $(PGXS)
|
include $(PGXS)
|
||||||
else
|
else
|
||||||
subdir = contrib/repmgr
|
subdir = contrib/repmgr
|
||||||
@@ -33,15 +30,10 @@ endif
|
|||||||
|
|
||||||
# XXX: Try to use PROGRAM construct (see pgxs.mk) someday. Right now
|
# XXX: Try to use PROGRAM construct (see pgxs.mk) someday. Right now
|
||||||
# is overriding pgxs install.
|
# is overriding pgxs install.
|
||||||
install: install_prog install_ext
|
install:
|
||||||
|
|
||||||
install_prog:
|
|
||||||
$(INSTALL_PROGRAM) repmgrd$(X) '$(DESTDIR)$(bindir)'
|
$(INSTALL_PROGRAM) repmgrd$(X) '$(DESTDIR)$(bindir)'
|
||||||
$(INSTALL_PROGRAM) repmgr$(X) '$(DESTDIR)$(bindir)'
|
$(INSTALL_PROGRAM) repmgr$(X) '$(DESTDIR)$(bindir)'
|
||||||
|
|
||||||
install_ext:
|
|
||||||
$(MAKE) -C sql install
|
|
||||||
|
|
||||||
ifneq (,$(DATA)$(DATA_built))
|
ifneq (,$(DATA)$(DATA_built))
|
||||||
@for file in $(addprefix $(srcdir)/, $(DATA)) $(DATA_built); do \
|
@for file in $(addprefix $(srcdir)/, $(DATA)) $(DATA_built); do \
|
||||||
echo "$(INSTALL_DATA) $$file '$(DESTDIR)$(datadir)/$(datamoduledir)'"; \
|
echo "$(INSTALL_DATA) $$file '$(DESTDIR)$(datadir)/$(datamoduledir)'"; \
|
||||||
@@ -53,17 +45,10 @@ clean:
|
|||||||
rm -f *.o
|
rm -f *.o
|
||||||
rm -f repmgrd
|
rm -f repmgrd
|
||||||
rm -f repmgr
|
rm -f repmgr
|
||||||
$(MAKE) -C sql clean
|
|
||||||
|
|
||||||
deb: repmgrd repmgr
|
deb: repmgrd repmgr
|
||||||
mkdir -p ./debian/usr/bin
|
mkdir -p ./debian/usr/bin
|
||||||
cp repmgrd repmgr ./debian/usr/bin/
|
cp repmgrd repmgr ./debian/usr/bin/
|
||||||
mkdir -p ./debian/usr/share/postgresql/9.0/contrib/
|
|
||||||
cp sql/repmgr_funcs.sql ./debian/usr/share/postgresql/9.0/contrib/
|
|
||||||
cp sql/uninstall_repmgr_funcs.sql ./debian/usr/share/postgresql/9.0/contrib/
|
|
||||||
mkdir -p ./debian/usr/lib/postgresql/9.0/lib/
|
|
||||||
cp sql/repmgr_funcs.so ./debian/usr/lib/postgresql/9.0/lib/
|
|
||||||
dpkg-deb --build debian
|
dpkg-deb --build debian
|
||||||
mv debian.deb ../postgresql-repmgr-9.0_1.0.0.deb
|
mv debian.deb ../postgresql-repmgr-9.0_1.0.0.deb
|
||||||
rm -rf ./debian/usr
|
|
||||||
|
|
||||||
|
|||||||
92
README.rst
92
README.rst
@@ -5,7 +5,7 @@ repmgr: Replication Manager for PostgreSQL clusters
|
|||||||
Introduction
|
Introduction
|
||||||
============
|
============
|
||||||
|
|
||||||
PostgreSQL 9+ allow us to have replicated Hot Standby servers
|
PostgreSQL 9.0 allow us to have replicated Hot Standby servers
|
||||||
which we can query and/or use for high availability.
|
which we can query and/or use for high availability.
|
||||||
|
|
||||||
While the main components of the feature are included with
|
While the main components of the feature are included with
|
||||||
@@ -20,17 +20,6 @@ databases as a single cluster. repmgr includes two components:
|
|||||||
* repmgrd: management and monitoring daemon that watches the cluster
|
* repmgrd: management and monitoring daemon that watches the cluster
|
||||||
and can automate remote actions.
|
and can automate remote actions.
|
||||||
|
|
||||||
Supported Releases
|
|
||||||
------------------
|
|
||||||
|
|
||||||
repmgr works with PostgreSQL versions 9.0 and superior.
|
|
||||||
|
|
||||||
There are currently no incompatibilities when upgrading repmgr from 9.0 to 9.1,
|
|
||||||
so your 9.0 configuration will work with 9.1
|
|
||||||
|
|
||||||
Additional parameters must be added to postgresql.conf to take advantage of
|
|
||||||
the new 9.1 features such as synchronous replication or hot standby feedback.
|
|
||||||
|
|
||||||
Requirements
|
Requirements
|
||||||
------------
|
------------
|
||||||
|
|
||||||
@@ -320,7 +309,7 @@ keys and a maching authorization file to a privledged user on the other system::
|
|||||||
[postgres@node1]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
|
[postgres@node1]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
|
||||||
[postgres@node1]$ chmod go-rwx ~/.ssh/*
|
[postgres@node1]$ chmod go-rwx ~/.ssh/*
|
||||||
[postgres@node1]$ cd ~/.ssh
|
[postgres@node1]$ cd ~/.ssh
|
||||||
[postgres@node1]$ scp id_rsa.pub id_rsa authorized_keys user@node2:
|
[postgres@node1]$ scp id_rsa.pub id_rsa authorized_keys postgres@node2:
|
||||||
|
|
||||||
Login as a user on the other system, and install the files into the postgres
|
Login as a user on the other system, and install the files into the postgres
|
||||||
user's account::
|
user's account::
|
||||||
@@ -389,7 +378,7 @@ walkthrough assumes the following setup:
|
|||||||
|
|
||||||
* Another standby server called "node3" with a similar configuration to "node2".
|
* Another standby server called "node3" with a similar configuration to "node2".
|
||||||
|
|
||||||
* The Postgres installation in each of the above is defined as $PGDATA,
|
* The Postgress installation in each of the above is defined as $PGDATA,
|
||||||
which is represented here as ``/var/lib/pgsql/9.0/data``
|
which is represented here as ``/var/lib/pgsql/9.0/data``
|
||||||
|
|
||||||
Creating some sample data
|
Creating some sample data
|
||||||
@@ -514,14 +503,12 @@ following the standard directory structure of a RHEL system. It should contain:
|
|||||||
|
|
||||||
cluster=test
|
cluster=test
|
||||||
node=1
|
node=1
|
||||||
node_name=earth
|
|
||||||
conninfo='host=node1 user=repmgr dbname=pgbench'
|
conninfo='host=node1 user=repmgr dbname=pgbench'
|
||||||
|
|
||||||
On "node2" create the file ``/var/lib/pgsql/repmgr/repmgr.conf`` with::
|
On "node2" create the file ``/var/lib/pgsql/repmgr/repmgr.conf`` with::
|
||||||
|
|
||||||
cluster=test
|
cluster=test
|
||||||
node=2
|
node=2
|
||||||
node_name=mars
|
|
||||||
conninfo='host=node2 user=repmgr dbname=pgbench'
|
conninfo='host=node2 user=repmgr dbname=pgbench'
|
||||||
|
|
||||||
The STANDBY CLONE process should have created a recovery.conf file on
|
The STANDBY CLONE process should have created a recovery.conf file on
|
||||||
@@ -625,18 +612,18 @@ Now restore to the original configuration by stopping
|
|||||||
primary server, then bringing up "node2" as a standby with a valid
|
primary server, then bringing up "node2" as a standby with a valid
|
||||||
``recovery.conf`` file.
|
``recovery.conf`` file.
|
||||||
|
|
||||||
Stop the "node2" server and type the following on "node1" server::
|
Stop the "node2" server::
|
||||||
|
|
||||||
repmgr -f /var/lib/pgsql/repmgr/repmgr.conf standby promote
|
repmgr -f /var/lib/pgsql/repmgr/repmgr.conf standby promote
|
||||||
|
|
||||||
Now the original primary, "node1", is acting again as primary.
|
Now the original primary, "node1" is acting again as primary.
|
||||||
|
|
||||||
Start the "node2" server and type this on "node2"::
|
Start the "node2" server and type this on "node1"::
|
||||||
|
|
||||||
repmgr standby clone --force -h node2 -p 5432 -U postgres -R postgres --verbose
|
repmgr standby clone --force -h node2 -p 5432 -U postgres -R postgres --verbose
|
||||||
|
|
||||||
Verify the roles have reversed by attempting to insert a record on "node1"
|
Verify the roles have reversed by attempting to insert a record on "node"
|
||||||
and on "node2".
|
and on "node1".
|
||||||
|
|
||||||
The servers are now again acting as primary on "node1" and standby on "node2".
|
The servers are now again acting as primary on "node1" and standby on "node2".
|
||||||
|
|
||||||
@@ -714,14 +701,12 @@ and it should contain::
|
|||||||
|
|
||||||
cluster=test
|
cluster=test
|
||||||
node=1
|
node=1
|
||||||
node_name=earth
|
|
||||||
conninfo='host=127.0.0.1 dbname=testdb'
|
conninfo='host=127.0.0.1 dbname=testdb'
|
||||||
|
|
||||||
On "standby" create the file ``/home/standby/repmgr/repmgr.conf`` with::
|
On "standby" create the file ``/home/standby/repmgr/repmgr.conf`` with::
|
||||||
|
|
||||||
cluster=test
|
cluster=test
|
||||||
node=2
|
node=2
|
||||||
node_name=mars
|
|
||||||
conninfo='host=127.0.0.1 dbname=testdb'
|
conninfo='host=127.0.0.1 dbname=testdb'
|
||||||
|
|
||||||
Next, with "prime" server running, we want to use the ``clone standby`` command
|
Next, with "prime" server running, we want to use the ``clone standby`` command
|
||||||
@@ -839,11 +824,12 @@ Also, if you don't do anything about it the monitor history will keep growing.
|
|||||||
For both of those reasons you sometime want to make some maintainance of the
|
For both of those reasons you sometime want to make some maintainance of the
|
||||||
``repl_monitor`` table.
|
``repl_monitor`` table.
|
||||||
|
|
||||||
If you want to clean the history after a few days you can execute the
|
If you want to clean the history after a few days you can execute a
|
||||||
CLUSTER CLEANUP command in a cron. For example to keep just one day of history
|
truncate/delete (wheter you want to completely clean history or want to keep
|
||||||
|
a few days of history) in a cron. For example to keep just one day of history
|
||||||
you can put this in your crontab::
|
you can put this in your crontab::
|
||||||
|
|
||||||
0 1 * * * repmgr cluster cleanup -k 1 -f ~/repmgr.conf
|
0 1 * * * psql -c "DELETE FROM repmgr_schema.repl_monitor where now() - last_monitor_time >= '1 day'::interval;" postgres
|
||||||
|
|
||||||
Configuration and command reference
|
Configuration and command reference
|
||||||
===================================
|
===================================
|
||||||
@@ -876,6 +862,7 @@ The output from this program looks like this::
|
|||||||
Usage:
|
Usage:
|
||||||
repmgr [OPTIONS] master {register}
|
repmgr [OPTIONS] master {register}
|
||||||
repmgr [OPTIONS] standby {register|clone|promote|follow}
|
repmgr [OPTIONS] standby {register|clone|promote|follow}
|
||||||
|
repmgr [OPTIONS] cluster {show|cleanup}
|
||||||
|
|
||||||
General options:
|
General options:
|
||||||
--help show this help, then exit
|
--help show this help, then exit
|
||||||
@@ -893,8 +880,9 @@ The output from this program looks like this::
|
|||||||
-f, --config_file=PATH path to the configuration file
|
-f, --config_file=PATH path to the configuration file
|
||||||
-R, --remote-user=USERNAME database server username for rsync
|
-R, --remote-user=USERNAME database server username for rsync
|
||||||
-w, --wal-keep-segments=VALUE minimum value for the GUC wal_keep_segments (default: 5000)
|
-w, --wal-keep-segments=VALUE minimum value for the GUC wal_keep_segments (default: 5000)
|
||||||
-I, --ignore-rsync-warning ignore rsync partial transfer warning
|
|
||||||
-F, --force force potentially dangerous operations to happen
|
-F, --force force potentially dangerous operations to happen
|
||||||
|
-I, --ignore-rsync-warning Ignore partial transfert warning
|
||||||
|
-k, --keep-history keeps indicated number of days of history
|
||||||
|
|
||||||
repmgr performs some tasks like clone a node, promote it or making follow another node and then exits.
|
repmgr performs some tasks like clone a node, promote it or making follow another node and then exits.
|
||||||
COMMANDS:
|
COMMANDS:
|
||||||
@@ -903,6 +891,8 @@ The output from this program looks like this::
|
|||||||
standby clone [node] - allows creation of a new standby
|
standby clone [node] - allows creation of a new standby
|
||||||
standby promote - allows manual promotion of a specific standby into a new master in the event of a failover
|
standby promote - allows manual promotion of a specific standby into a new master in the event of a failover
|
||||||
standby follow - allows the standby to re-point itself to a new master
|
standby follow - allows the standby to re-point itself to a new master
|
||||||
|
cluster show - print node informations
|
||||||
|
cluster cleanup - cleans monitor's history
|
||||||
|
|
||||||
The ``--verbose`` option can be useful in troubleshooting issues with
|
The ``--verbose`` option can be useful in troubleshooting issues with
|
||||||
the program.
|
the program.
|
||||||
@@ -1013,7 +1003,6 @@ The output from this program looks like this::
|
|||||||
--help show this help, then exit
|
--help show this help, then exit
|
||||||
--version output version information, then exit
|
--version output version information, then exit
|
||||||
--verbose output verbose activity information
|
--verbose output verbose activity information
|
||||||
--monitoring-history track advance or lag of the replication in every standby in repl_monitor
|
|
||||||
-f, --config_file=PATH database to connect to
|
-f, --config_file=PATH database to connect to
|
||||||
|
|
||||||
repmgrd monitors a cluster of servers.
|
repmgrd monitors a cluster of servers.
|
||||||
@@ -1044,10 +1033,6 @@ Lag monitoring
|
|||||||
repmgrd helps monitor a set of master and standby servers. You can
|
repmgrd helps monitor a set of master and standby servers. You can
|
||||||
see which node is the current master, as well as how far behind each
|
see which node is the current master, as well as how far behind each
|
||||||
is from current.
|
is from current.
|
||||||
To activate the monitor capabilities of repmgr you must include the
|
|
||||||
option --monitoring-history when running it::
|
|
||||||
|
|
||||||
repmgrd --monitoring-history --config-file=/path/to/repmgr.conf &
|
|
||||||
|
|
||||||
To look at the current lag between primary and each node listed
|
To look at the current lag between primary and each node listed
|
||||||
in ``repl_node``, consult the ``repl_status`` view::
|
in ``repl_node``, consult the ``repl_status`` view::
|
||||||
@@ -1080,21 +1065,16 @@ following
|
|||||||
* ERR_DB_QUERY 7: Error executing a database query.
|
* ERR_DB_QUERY 7: Error executing a database query.
|
||||||
* ERR_PROMOTED 8: Exiting program because the node has been promoted to master.
|
* ERR_PROMOTED 8: Exiting program because the node has been promoted to master.
|
||||||
* ERR_BAD_PASSWORD 9: Password used to connect to a database was rejected.
|
* ERR_BAD_PASSWORD 9: Password used to connect to a database was rejected.
|
||||||
|
* ERR_STR_OVERFLOW 10: A string was larger than expected.
|
||||||
|
|
||||||
License and Contributions
|
License and Contributions
|
||||||
=========================
|
=========================
|
||||||
|
|
||||||
repmgr is licensed under the GPL v3. All of its code and documentation is
|
repmgr is licensed under the GPL v3. All of its code and documentation is
|
||||||
Copyright 2010-2014, 2ndQuadrant Limited. See the files COPYRIGHT and LICENSE for
|
Copyright 2010-2011, 2ndQuadrant Limited. See the files COPYRIGHT and LICENSE for
|
||||||
details.
|
details.
|
||||||
|
|
||||||
Main sponsorship of repmgr has been from 2ndQuadrant customers.
|
Contributions to repmgr are welcome, and listed in the file CREDITS.
|
||||||
|
|
||||||
Additional work has been sponsored by the 4CaaST project for cloud computing,
|
|
||||||
which has received funding from the European Union's Seventh Framework Programme
|
|
||||||
(FP7/2007-2013) under grant agreement 258862.
|
|
||||||
|
|
||||||
Contributions to repmgr are welcome, and will be listed in the file CREDITS.
|
|
||||||
2ndQuadrant Limited requires that any contributions provide a copyright
|
2ndQuadrant Limited requires that any contributions provide a copyright
|
||||||
assignment and a disclaimer of any work-for-hire ownership claims from the
|
assignment and a disclaimer of any work-for-hire ownership claims from the
|
||||||
employer of the developer. This lets us make sure that all of the repmgr
|
employer of the developer. This lets us make sure that all of the repmgr
|
||||||
@@ -1110,35 +1090,3 @@ Code in repmgr is formatted to a consistent style using the following command::
|
|||||||
|
|
||||||
Contributors should reformat their code similarly before submitting code to
|
Contributors should reformat their code similarly before submitting code to
|
||||||
the project, in order to minimize merge conflicts with other work.
|
the project, in order to minimize merge conflicts with other work.
|
||||||
|
|
||||||
Support and Assistance
|
|
||||||
======================
|
|
||||||
|
|
||||||
2ndQuadrant provides 24x7 production support for repmgr, as well as help you
|
|
||||||
configure it correctly, verify an installation and train you in running a
|
|
||||||
robust replication cluster.
|
|
||||||
|
|
||||||
There is a mailing list/forum to discuss contributions or issues
|
|
||||||
http://groups.google.com/group/repmgr
|
|
||||||
|
|
||||||
#repmgr is registered in freenode IRC
|
|
||||||
|
|
||||||
Further information is available at http://www.repmgr.org/
|
|
||||||
|
|
||||||
We'd love to hear from you about how you use repmgr. Case studies and
|
|
||||||
news are always welcome. Send us an email at info@2ndQuadrant.com, or
|
|
||||||
send a postcard to
|
|
||||||
|
|
||||||
repmgr
|
|
||||||
c/o 2ndQuadrant
|
|
||||||
7200 The Quorum
|
|
||||||
Oxford Business Park North
|
|
||||||
Oxford
|
|
||||||
OX4 2JZ
|
|
||||||
|
|
||||||
Thanks from the repmgr core team
|
|
||||||
|
|
||||||
Jaime Casanova
|
|
||||||
Simon Riggs
|
|
||||||
Greg Smith
|
|
||||||
Cedric Villemain
|
|
||||||
|
|||||||
18
TODO
18
TODO
@@ -1,18 +1,14 @@
|
|||||||
Known issues in repmgr
|
Known issues in repmgr
|
||||||
======================
|
======================
|
||||||
|
|
||||||
|
* The check for whether ``wal_keep_segments`` is considered large enough
|
||||||
|
does a string comparison rather than an integer one. It can give both
|
||||||
|
false positive (setting is large enough but flagged as too small) and
|
||||||
|
false negative (setting is too small but not noted as such) errors.
|
||||||
|
|
||||||
* When running repmgr against a remote machine, operations that start
|
* When running repmgr against a remote machine, operations that start
|
||||||
the database server using the ``pg_ctl`` command may accidentally
|
the database server using the ``pg_ctl`` command may accidentally
|
||||||
terminate after their associated ssh session ends.
|
terminate after their associated ssh session ends.
|
||||||
|
|
||||||
Planned feature improvements
|
* After running repmgrd as a regular foreground application, hitting
|
||||||
============================
|
control-C causes the program to crash.
|
||||||
|
|
||||||
* Timeline increases when promoting a standby
|
|
||||||
|
|
||||||
* A better check which standby did receive most of the data
|
|
||||||
|
|
||||||
* Make the fact that a standby may be delayed a factor in the voting
|
|
||||||
algorithm
|
|
||||||
|
|
||||||
* include support for delayed standbys
|
|
||||||
|
|||||||
@@ -1,225 +0,0 @@
|
|||||||
=====================================================
|
|
||||||
PostgreSQL Automatic Fail-Over - User Documentation
|
|
||||||
=====================================================
|
|
||||||
|
|
||||||
Automatic Failover
|
|
||||||
==================
|
|
||||||
|
|
||||||
repmgr allows setups for automatic failover when it detects the failure of the master node.
|
|
||||||
Following is a quick setup for this.
|
|
||||||
|
|
||||||
Installation
|
|
||||||
============
|
|
||||||
|
|
||||||
For convenience, we define:
|
|
||||||
|
|
||||||
**node1**
|
|
||||||
is the hostname fully qualified of the Master server, IP 192.168.1.10
|
|
||||||
**node2**
|
|
||||||
is the hostname fully qualified of the Standby server, IP 192.168.1.11
|
|
||||||
**witness**
|
|
||||||
is the hostname fully qualified of the server used for witness, IP 192.168.1.12
|
|
||||||
|
|
||||||
**Note:** It is not recommanded to use name defining status of a server like «masterserver»,
|
|
||||||
this is a name leading to confusion once a failover take place and the Master is
|
|
||||||
now on the «standbyserver».
|
|
||||||
|
|
||||||
Summary
|
|
||||||
-------
|
|
||||||
|
|
||||||
2 PostgreSQL servers are involved in the replication. Automatic fail-over need
|
|
||||||
to vote to decide what server it should promote, thus an odd number is required
|
|
||||||
and a witness-repmgrd is installed in a third server where it uses a PostgreSQL
|
|
||||||
cluster to communicate with other repmgrd daemons.
|
|
||||||
|
|
||||||
1. Install PostgreSQL in all the servers involved (including the server used for
|
|
||||||
witness)
|
|
||||||
|
|
||||||
2. Install repmgr in all the servers involved (including the server used for witness)
|
|
||||||
|
|
||||||
3. Configure the Master PostreSQL
|
|
||||||
|
|
||||||
4. Clone the Master to the Standby using "repmgr standby clone" command
|
|
||||||
|
|
||||||
5. Configure repmgr in all the servers involved (including the server used for witness)
|
|
||||||
|
|
||||||
6. Register Master and Standby nodes
|
|
||||||
|
|
||||||
7. Initiate witness server
|
|
||||||
|
|
||||||
8. Start the repmgrd daemons in all nodes
|
|
||||||
|
|
||||||
**Note** A complete Hight-Availability design need at least 3 servers to still have
|
|
||||||
a backup node after a first failure.
|
|
||||||
|
|
||||||
Install PostgreSQL
|
|
||||||
------------------
|
|
||||||
|
|
||||||
You can install PostgreSQL using any of the recommended methods. You should ensure
|
|
||||||
it's 9.0 or superior.
|
|
||||||
|
|
||||||
Install repmgr
|
|
||||||
--------------
|
|
||||||
|
|
||||||
Install repmgr following the steps in the README.
|
|
||||||
|
|
||||||
Configure PostreSQL
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
Log in node1.
|
|
||||||
|
|
||||||
Edit the file postgresql.conf and modify the parameters::
|
|
||||||
|
|
||||||
listen_addresses='*'
|
|
||||||
wal_level = 'hot_standby'
|
|
||||||
archive_mode = on
|
|
||||||
archive_command = 'cd .' # we can also use exit 0, anything that
|
|
||||||
# just does nothing
|
|
||||||
max_wal_senders = 10
|
|
||||||
wal_keep_segments = 5000 # 80 GB required on pg_xlog
|
|
||||||
hot_standby = on
|
|
||||||
shared_preload_libraries = 'repmgr_funcs'
|
|
||||||
|
|
||||||
Edit the file pg_hba.conf and add lines for the replication::
|
|
||||||
|
|
||||||
host repmgr repmgr 127.0.0.1/32 trust
|
|
||||||
host repmgr repmgr 192.168.1.10/30 trust
|
|
||||||
host replication all 192.168.1.10/30 trust
|
|
||||||
|
|
||||||
**Note:** It is also possible to use a password authentication (md5), .pgpass file
|
|
||||||
should be edited to allow connection between each node.
|
|
||||||
|
|
||||||
Create the user and database to manage replication::
|
|
||||||
|
|
||||||
su - postgres
|
|
||||||
createuser -s repmgr
|
|
||||||
createdb -O repmgr repmgr
|
|
||||||
psql -f /usr/share/postgresql/9.0/contrib/repmgr_funcs.sql repmgr
|
|
||||||
|
|
||||||
Restart the PostgreSQL server::
|
|
||||||
|
|
||||||
pg_ctl -D $PGDATA restart
|
|
||||||
|
|
||||||
And check everything is fine in the server log.
|
|
||||||
|
|
||||||
Create the ssh-key for the postgres user and copy it to other servers::
|
|
||||||
|
|
||||||
su - postgres
|
|
||||||
ssh-keygen # /!\ do not use a passphrase /!\
|
|
||||||
cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys
|
|
||||||
chmod 600 ~/.ssh/authorized_keys
|
|
||||||
exit
|
|
||||||
rsync -avz ~postgres/.ssh/authorized_keys node2:~postgres/.ssh/
|
|
||||||
rsync -avz ~postgres/.ssh/authorized_keys witness:~postgres/.ssh/
|
|
||||||
rsync -avz ~postgres/.ssh/id_rsa* node2:~postgres/.ssh/
|
|
||||||
rsync -avz ~postgres/.ssh/id_rsa* witness:~postgres/.ssh/
|
|
||||||
|
|
||||||
Clone Master
|
|
||||||
------------
|
|
||||||
|
|
||||||
Log in node2.
|
|
||||||
|
|
||||||
Clone the node1 (the current Master)::
|
|
||||||
|
|
||||||
su - postgres
|
|
||||||
repmgr -d repmgr -U repmgr -h node1 standby clone
|
|
||||||
|
|
||||||
Start the PostgreSQL server::
|
|
||||||
|
|
||||||
pg_ctl -D $PGDATA start
|
|
||||||
|
|
||||||
And check everything is fine in the server log.
|
|
||||||
|
|
||||||
Configure repmgr
|
|
||||||
----------------
|
|
||||||
|
|
||||||
Log in each server and configure repmgr by editing the file
|
|
||||||
/etc/repmgr/repmgr.conf::
|
|
||||||
|
|
||||||
cluster=my_cluster
|
|
||||||
node=1
|
|
||||||
node_name=earth
|
|
||||||
conninfo='host=192.168.1.10 dbname=repmgr user=repmgr'
|
|
||||||
master_response_timeout=60
|
|
||||||
reconnect_attempts=6
|
|
||||||
reconnect_interval=10
|
|
||||||
failover=automatic
|
|
||||||
promote_command='promote_command.sh'
|
|
||||||
follow_command='repmgr standby follow -f /etc/repmgr/repmgr.conf'
|
|
||||||
|
|
||||||
**cluster**
|
|
||||||
is the name of the current replication.
|
|
||||||
**node**
|
|
||||||
is the number of the current node (1, 2 or 3 in the current example).
|
|
||||||
**node_name**
|
|
||||||
is an identifier for every node.
|
|
||||||
**conninfo**
|
|
||||||
is used to connect to the local PostgreSQL server (where the configuration file is) from any node. In the witness server configuration it is needed to add a 'port=5499' to the conninfo.
|
|
||||||
**master_response_timeout**
|
|
||||||
is the maximum amount of time we are going to wait before deciding the master has died and start failover procedure.
|
|
||||||
**reconnect_attempts**
|
|
||||||
is the number of times we will try to reconnect to master after a failure has been detected and before start failover procedure.
|
|
||||||
**reconnect_interval**
|
|
||||||
is the amount of time between retries to reconnect to master after a failure has been detected and before start failover procedure.
|
|
||||||
**failover**
|
|
||||||
configure behavior: *manual* or *automatic*.
|
|
||||||
**promote_command**
|
|
||||||
the command executed to do the failover (including the PostgreSQL failover itself). The command must return 0 on success.
|
|
||||||
**follow_command**
|
|
||||||
the command executed to address the current standby to another Master. The command must return 0 on success.
|
|
||||||
|
|
||||||
Register Master and Standby
|
|
||||||
---------------------------
|
|
||||||
|
|
||||||
Log in node1.
|
|
||||||
|
|
||||||
Register the node as Master::
|
|
||||||
|
|
||||||
su - postgres
|
|
||||||
repmgr -f /etc/repmgr/repmgr.conf master register
|
|
||||||
|
|
||||||
Log in node2. Register it as a standby::
|
|
||||||
|
|
||||||
su - postgres
|
|
||||||
repmgr -f /etc/repmgr/repmgr.conf standby register
|
|
||||||
|
|
||||||
Initialize witness server
|
|
||||||
-------------------------
|
|
||||||
|
|
||||||
Log in witness.
|
|
||||||
|
|
||||||
Initialize the witness server::
|
|
||||||
|
|
||||||
su - postgres
|
|
||||||
repmgr -d repmgr -U repmgr -h 192.168.1.10 -D $WITNESS_PGDATA -f /etc/repmgr/repmgr.conf witness create
|
|
||||||
|
|
||||||
It needs information to connect to the master to copy the configuration of the cluster, also it needs to know where it should initialize it's own $PGDATA.
|
|
||||||
As part of the procees it also ask for the superuser password so it can connect when needed.
|
|
||||||
|
|
||||||
Start the repmgrd daemons
|
|
||||||
-------------------------
|
|
||||||
|
|
||||||
Log in node2 and witness.
|
|
||||||
|
|
||||||
su - postgres
|
|
||||||
repmgrd -f /etc/repmgr/repmgr.conf > /var/log/postgresql/repmgr.log 2>&1
|
|
||||||
|
|
||||||
**Note:** The Master does not need a repmgrd daemon.
|
|
||||||
|
|
||||||
|
|
||||||
Suspend Automatic behavior
|
|
||||||
==========================
|
|
||||||
|
|
||||||
Edit the repmgr.conf of the node to remove from automatic processing and change::
|
|
||||||
|
|
||||||
failover=manual
|
|
||||||
|
|
||||||
Then, signal repmgrd daemon::
|
|
||||||
|
|
||||||
su - postgres
|
|
||||||
kill -HUP `pidoff repmgrd`
|
|
||||||
|
|
||||||
Usage
|
|
||||||
=====
|
|
||||||
|
|
||||||
The repmgr documentation is in the README file (how to build, options, etc.)
|
|
||||||
91
check_dir.c
91
check_dir.c
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* check_dir.c - Directories management functions
|
* check_dir.c - Directories management functions
|
||||||
* Copyright (C) 2ndQuadrant, 2010-2014
|
* Copyright (C) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -31,6 +31,8 @@
|
|||||||
#include "strutil.h"
|
#include "strutil.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
static int mkdir_p(char *path, mode_t omode);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* make sure the directory either doesn't exist or is empty
|
* make sure the directory either doesn't exist or is empty
|
||||||
* we use this function to check the new data directory and
|
* we use this function to check the new data directory and
|
||||||
@@ -122,15 +124,15 @@ set_directory_permissions(char *dir)
|
|||||||
* note that on failure, the path arg has been modified to show the particular
|
* note that on failure, the path arg has been modified to show the particular
|
||||||
* directory level we had problems with.
|
* directory level we had problems with.
|
||||||
*/
|
*/
|
||||||
int
|
static int
|
||||||
mkdir_p(char *path, mode_t omode)
|
mkdir_p(char *path, mode_t omode)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
mode_t numask,
|
mode_t numask,
|
||||||
oumask;
|
oumask;
|
||||||
int first,
|
int first,
|
||||||
last,
|
last,
|
||||||
retval;
|
retval;
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
p = path;
|
p = path;
|
||||||
@@ -223,85 +225,8 @@ is_pg_dir(char *dir)
|
|||||||
const size_t buf_sz = 8192;
|
const size_t buf_sz = 8192;
|
||||||
char path[buf_sz];
|
char path[buf_sz];
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
int r;
|
|
||||||
|
|
||||||
/* test pgdata */
|
|
||||||
xsnprintf(path, buf_sz, "%s/PG_VERSION", dir);
|
xsnprintf(path, buf_sz, "%s/PG_VERSION", dir);
|
||||||
if (stat(path, &sb) == 0)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
/* test tablespace dir */
|
return (stat(path, &sb) == 0) ? true : false;
|
||||||
sprintf(path, "ls %s/PG_*/ -I*", dir);
|
|
||||||
r = system(path);
|
|
||||||
if (r == 0)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool
|
|
||||||
create_pgdir(char *dir, bool force)
|
|
||||||
{
|
|
||||||
bool pg_dir = false;
|
|
||||||
|
|
||||||
/* Check this directory could be used as a PGDATA dir */
|
|
||||||
switch (check_dir(dir))
|
|
||||||
{
|
|
||||||
case 0:
|
|
||||||
/* dir not there, must create it */
|
|
||||||
log_info(_("creating directory \"%s\"...\n"), dir);
|
|
||||||
|
|
||||||
if (!create_directory(dir))
|
|
||||||
{
|
|
||||||
log_err(_("couldn't create directory \"%s\"...\n"),
|
|
||||||
dir);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
/* Present but empty, fix permissions and use it */
|
|
||||||
log_info(_("checking and correcting permissions on existing directory %s ...\n"),
|
|
||||||
dir);
|
|
||||||
|
|
||||||
if (!set_directory_permissions(dir))
|
|
||||||
{
|
|
||||||
log_err(_("could not change permissions of directory \"%s\": %s\n"),
|
|
||||||
dir, strerror(errno));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
/* Present and not empty */
|
|
||||||
log_warning(_("directory \"%s\" exists but is not empty\n"),
|
|
||||||
dir);
|
|
||||||
|
|
||||||
pg_dir = is_pg_dir(dir);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* we use force to reduce the time needed to restore a node which
|
|
||||||
* turn async after a failover or anything else
|
|
||||||
*/
|
|
||||||
if (pg_dir && force)
|
|
||||||
{
|
|
||||||
/* Let it continue */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (pg_dir && !force)
|
|
||||||
{
|
|
||||||
log_warning(_("\nThis looks like a PostgreSQL directory.\n"
|
|
||||||
"If you are sure you want to clone here, "
|
|
||||||
"please check there is no PostgreSQL server "
|
|
||||||
"running and use the --force option\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
default:
|
|
||||||
/* Trouble accessing directory */
|
|
||||||
log_err(_("could not access directory \"%s\": %s\n"),
|
|
||||||
dir, strerror(errno));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* check_dir.h
|
* check_dir.h
|
||||||
* Copyright (c) 2ndQuadrant, 2010-2014
|
* Copyright (c) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -20,11 +20,9 @@
|
|||||||
#ifndef _REPMGR_CHECK_DIR_H_
|
#ifndef _REPMGR_CHECK_DIR_H_
|
||||||
#define _REPMGR_CHECK_DIR_H_
|
#define _REPMGR_CHECK_DIR_H_
|
||||||
|
|
||||||
int mkdir_p(char *path, mode_t omode);
|
|
||||||
int check_dir(char *dir);
|
int check_dir(char *dir);
|
||||||
bool create_directory(char *dir);
|
bool create_directory(char *dir);
|
||||||
bool set_directory_permissions(char *dir);
|
bool set_directory_permissions(char *dir);
|
||||||
bool is_pg_dir(char *dir);
|
bool is_pg_dir(char *dir);
|
||||||
bool create_pgdir(char *dir, bool force);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
202
config.c
202
config.c
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* config.c - Functions to parse the config file
|
* config.c - Functions to parse the config file
|
||||||
* Copyright (C) 2ndQuadrant, 2010-2014
|
* Copyright (C) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -18,12 +18,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "log.h"
|
|
||||||
#include "strutil.h"
|
|
||||||
#include "repmgr.h"
|
#include "repmgr.h"
|
||||||
|
#include "strutil.h"
|
||||||
|
|
||||||
void
|
void
|
||||||
parse_config(const char *config_file, t_configuration_options *options)
|
parse_config(const char* config_file, t_configuration_options* options)
|
||||||
{
|
{
|
||||||
char *s, buff[MAXLINELENGTH];
|
char *s, buff[MAXLINELENGTH];
|
||||||
char name[MAXLEN];
|
char name[MAXLEN];
|
||||||
@@ -35,25 +34,7 @@ parse_config(const char *config_file, t_configuration_options *options)
|
|||||||
memset(options->cluster_name, 0, sizeof(options->cluster_name));
|
memset(options->cluster_name, 0, sizeof(options->cluster_name));
|
||||||
options->node = -1;
|
options->node = -1;
|
||||||
memset(options->conninfo, 0, sizeof(options->conninfo));
|
memset(options->conninfo, 0, sizeof(options->conninfo));
|
||||||
options->failover = MANUAL_FAILOVER;
|
|
||||||
options->priority = 0;
|
|
||||||
memset(options->node_name, 0, sizeof(options->node_name));
|
|
||||||
memset(options->promote_command, 0, sizeof(options->promote_command));
|
|
||||||
memset(options->follow_command, 0, sizeof(options->follow_command));
|
|
||||||
memset(options->rsync_options, 0, sizeof(options->rsync_options));
|
memset(options->rsync_options, 0, sizeof(options->rsync_options));
|
||||||
memset(options->ssh_options, 0, sizeof(options->ssh_options));
|
|
||||||
memset(options->pg_bindir, 0, sizeof(options->pg_bindir));
|
|
||||||
memset(options->pgctl_options, 0, sizeof(options->pgctl_options));
|
|
||||||
|
|
||||||
/* if nothing has been provided defaults to 60 */
|
|
||||||
options->master_response_timeout = 60;
|
|
||||||
|
|
||||||
/* it defaults to 6 retries with a time between retries of 10s */
|
|
||||||
options->reconnect_attempts = 6;
|
|
||||||
options->reconnect_intvl = 10;
|
|
||||||
|
|
||||||
options->monitor_interval_secs = 2;
|
|
||||||
options->retry_promote_interval_secs = 300;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Since some commands don't require a config file at all, not
|
* Since some commands don't require a config file at all, not
|
||||||
@@ -61,7 +42,7 @@ parse_config(const char *config_file, t_configuration_options *options)
|
|||||||
*/
|
*/
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
{
|
{
|
||||||
log_err(_("Did not find the configuration file '%s', continuing\n"), config_file);
|
fprintf(stderr, _("Did not find the configuration file '%s', continuing\n"), config_file);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,53 +65,12 @@ parse_config(const char *config_file, t_configuration_options *options)
|
|||||||
strncpy (options->conninfo, value, MAXLEN);
|
strncpy (options->conninfo, value, MAXLEN);
|
||||||
else if (strcmp(name, "rsync_options") == 0)
|
else if (strcmp(name, "rsync_options") == 0)
|
||||||
strncpy (options->rsync_options, value, QUERY_STR_LEN);
|
strncpy (options->rsync_options, value, QUERY_STR_LEN);
|
||||||
else if (strcmp(name, "ssh_options") == 0)
|
|
||||||
strncpy (options->ssh_options, value, QUERY_STR_LEN);
|
|
||||||
else if (strcmp(name, "loglevel") == 0)
|
else if (strcmp(name, "loglevel") == 0)
|
||||||
strncpy (options->loglevel, value, MAXLEN);
|
strncpy (options->loglevel, value, MAXLEN);
|
||||||
else if (strcmp(name, "logfacility") == 0)
|
else if (strcmp(name, "logfacility") == 0)
|
||||||
strncpy (options->logfacility, value, MAXLEN);
|
strncpy (options->logfacility, value, MAXLEN);
|
||||||
else if (strcmp(name, "failover") == 0)
|
|
||||||
{
|
|
||||||
char failoverstr[MAXLEN];
|
|
||||||
strncpy(failoverstr, value, MAXLEN);
|
|
||||||
|
|
||||||
if (strcmp(failoverstr, "manual") == 0)
|
|
||||||
options->failover = MANUAL_FAILOVER;
|
|
||||||
else if (strcmp(failoverstr, "automatic") == 0)
|
|
||||||
options->failover = AUTOMATIC_FAILOVER;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
log_warning(_("value for failover option is incorrect, it should be automatic or manual. Defaulting to manual.\n"));
|
|
||||||
options->failover = MANUAL_FAILOVER;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (strcmp(name, "priority") == 0)
|
|
||||||
options->priority = atoi(value);
|
|
||||||
else if (strcmp(name, "node_name") == 0)
|
|
||||||
strncpy(options->node_name, value, MAXLEN);
|
|
||||||
else if (strcmp(name, "promote_command") == 0)
|
|
||||||
strncpy(options->promote_command, value, MAXLEN);
|
|
||||||
else if (strcmp(name, "follow_command") == 0)
|
|
||||||
strncpy(options->follow_command, value, MAXLEN);
|
|
||||||
else if (strcmp(name, "master_response_timeout") == 0)
|
|
||||||
options->master_response_timeout = atoi(value);
|
|
||||||
else if (strcmp(name, "reconnect_attempts") == 0)
|
|
||||||
options->reconnect_attempts = atoi(value);
|
|
||||||
else if (strcmp(name, "reconnect_interval") == 0)
|
|
||||||
options->reconnect_intvl = atoi(value);
|
|
||||||
else if (strcmp(name, "pg_bindir") == 0)
|
|
||||||
strncpy (options->pg_bindir, value, MAXLEN);
|
|
||||||
else if (strcmp(name, "pg_ctl_options") == 0)
|
|
||||||
strncpy (options->pgctl_options, value, MAXLEN);
|
|
||||||
else if (strcmp(name, "logfile") == 0)
|
|
||||||
strncpy(options->logfile, value, MAXLEN);
|
|
||||||
else if (strcmp(name, "monitor_interval_secs") == 0)
|
|
||||||
options->monitor_interval_secs = atoi(value);
|
|
||||||
else if (strcmp(name, "retry_promote_interval_secs") == 0)
|
|
||||||
options->retry_promote_interval_secs = atoi(value);
|
|
||||||
else
|
else
|
||||||
log_warning(_("%s/%s: Unknown name/value pair!\n"), name, value);
|
printf ("WARNING: %s/%s: Unknown name/value pair!\n", name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Close file */
|
/* Close file */
|
||||||
@@ -139,42 +79,19 @@ parse_config(const char *config_file, t_configuration_options *options)
|
|||||||
/* Check config settings */
|
/* Check config settings */
|
||||||
if (strnlen(options->cluster_name, MAXLEN)==0)
|
if (strnlen(options->cluster_name, MAXLEN)==0)
|
||||||
{
|
{
|
||||||
log_err(_("Cluster name is missing. Check the configuration file.\n"));
|
fprintf(stderr, "Cluster name is missing. "
|
||||||
|
"Check the configuration file.\n");
|
||||||
exit(ERR_BAD_CONFIG);
|
exit(ERR_BAD_CONFIG);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options->node == -1)
|
if (options->node == -1)
|
||||||
{
|
{
|
||||||
log_err(_("Node information is missing. Check the configuration file.\n"));
|
fprintf(stderr, "Node information is missing. "
|
||||||
exit(ERR_BAD_CONFIG);
|
"Check the configuration file.\n");
|
||||||
}
|
|
||||||
|
|
||||||
if (options->master_response_timeout <= 0)
|
|
||||||
{
|
|
||||||
log_err(_("Master response timeout must be greater than zero. Check the configuration file.\n"));
|
|
||||||
exit(ERR_BAD_CONFIG);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options->reconnect_attempts < 0)
|
|
||||||
{
|
|
||||||
log_err(_("Reconnect attempts must be zero or greater. Check the configuration file.\n"));
|
|
||||||
exit(ERR_BAD_CONFIG);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options->reconnect_intvl <= 0)
|
|
||||||
{
|
|
||||||
log_err(_("Reconnect intervals must be zero or greater. Check the configuration file.\n"));
|
|
||||||
exit(ERR_BAD_CONFIG);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*options->pg_bindir == '\0')
|
|
||||||
{
|
|
||||||
log_err(_("pg_bindir config value not found. Check the configuration file.\n"));
|
|
||||||
exit(ERR_BAD_CONFIG);
|
exit(ERR_BAD_CONFIG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char *
|
char *
|
||||||
trim (char *s)
|
trim (char *s)
|
||||||
{
|
{
|
||||||
@@ -191,9 +108,7 @@ trim (char *s)
|
|||||||
++s1;
|
++s1;
|
||||||
|
|
||||||
/* Copy finished string */
|
/* Copy finished string */
|
||||||
memmove (s, s1, s2 - s1);
|
strcpy (s, s1);
|
||||||
s[s2 - s1 + 1] = '\0';
|
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,100 +144,3 @@ parse_line(char *buff, char *name, char *value)
|
|||||||
value[j] = '\0';
|
value[j] = '\0';
|
||||||
trim(value);
|
trim(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
|
||||||
reload_configuration(char *config_file, t_configuration_options *orig_options)
|
|
||||||
{
|
|
||||||
PGconn *conn;
|
|
||||||
|
|
||||||
t_configuration_options new_options;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Re-read the configuration file: repmgr.conf
|
|
||||||
*/
|
|
||||||
log_info(_("Reloading configuration file and updating repmgr tables\n"));
|
|
||||||
parse_config(config_file, &new_options);
|
|
||||||
if (new_options.node == -1)
|
|
||||||
{
|
|
||||||
log_warning(_("Cannot load new configuration, will keep current one.\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(new_options.cluster_name, orig_options->cluster_name) != 0)
|
|
||||||
{
|
|
||||||
log_warning(_("Cannot change cluster name, will keep current configuration.\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_options.node != orig_options->node)
|
|
||||||
{
|
|
||||||
log_warning(_("Cannot change node number, will keep current configuration.\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(new_options.node_name, orig_options->node_name) != 0)
|
|
||||||
{
|
|
||||||
log_warning(_("Cannot change standby name, will keep current configuration.\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_options.failover != MANUAL_FAILOVER && new_options.failover != AUTOMATIC_FAILOVER)
|
|
||||||
{
|
|
||||||
log_warning(_("New value for failover is not valid. Should be MANUAL or AUTOMATIC.\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_options.master_response_timeout <= 0)
|
|
||||||
{
|
|
||||||
log_warning(_("New value for master_response_timeout is not valid. Should be greater than zero.\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_options.reconnect_attempts < 0)
|
|
||||||
{
|
|
||||||
log_warning(_("New value for reconnect_attempts is not valid. Should be greater or equal than zero.\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_options.reconnect_intvl < 0)
|
|
||||||
{
|
|
||||||
log_warning(_("New value for reconnect_interval is not valid. Should be greater or equal than zero.\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Test conninfo string */
|
|
||||||
conn = establishDBConnection(new_options.conninfo, false);
|
|
||||||
if (!conn || (PQstatus(conn) != CONNECTION_OK))
|
|
||||||
{
|
|
||||||
log_warning(_("conninfo string is not valid, will keep current configuration.\n"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
PQfinish(conn);
|
|
||||||
|
|
||||||
/* Configuration seems ok, will load new values */
|
|
||||||
strcpy(orig_options->cluster_name, new_options.cluster_name);
|
|
||||||
orig_options->node = new_options.node;
|
|
||||||
strcpy(orig_options->conninfo, new_options.conninfo);
|
|
||||||
orig_options->failover = new_options.failover;
|
|
||||||
orig_options->priority = new_options.priority;
|
|
||||||
strcpy(orig_options->node_name, new_options.node_name);
|
|
||||||
strcpy(orig_options->promote_command, new_options.promote_command);
|
|
||||||
strcpy(orig_options->follow_command, new_options.follow_command);
|
|
||||||
strcpy(orig_options->rsync_options, new_options.rsync_options);
|
|
||||||
strcpy(orig_options->ssh_options, new_options.ssh_options);
|
|
||||||
orig_options->master_response_timeout = new_options.master_response_timeout;
|
|
||||||
orig_options->reconnect_attempts = new_options.reconnect_attempts;
|
|
||||||
orig_options->reconnect_intvl = new_options.reconnect_intvl;
|
|
||||||
/*
|
|
||||||
* XXX These ones can change with a simple SIGHUP?
|
|
||||||
|
|
||||||
strcpy (orig_options->loglevel, new_options.loglevel);
|
|
||||||
strcpy (orig_options->logfacility, new_options.logfacility);
|
|
||||||
|
|
||||||
logger_shutdown();
|
|
||||||
XXX do we have progname here ?
|
|
||||||
logger_init(progname, orig_options.loglevel, orig_options.logfacility);
|
|
||||||
*/
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|||||||
21
config.h
21
config.h
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* config.h
|
* config.h
|
||||||
* Copyright (c) 2ndQuadrant, 2010-2014
|
* Copyright (c) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -28,30 +28,13 @@ typedef struct
|
|||||||
char cluster_name[MAXLEN];
|
char cluster_name[MAXLEN];
|
||||||
int node;
|
int node;
|
||||||
char conninfo[MAXLEN];
|
char conninfo[MAXLEN];
|
||||||
int failover;
|
|
||||||
int priority;
|
|
||||||
char node_name[MAXLEN];
|
|
||||||
char promote_command[MAXLEN];
|
|
||||||
char follow_command[MAXLEN];
|
|
||||||
char loglevel[MAXLEN];
|
char loglevel[MAXLEN];
|
||||||
char logfacility[MAXLEN];
|
char logfacility[MAXLEN];
|
||||||
char rsync_options[QUERY_STR_LEN];
|
char rsync_options[QUERY_STR_LEN];
|
||||||
char ssh_options[QUERY_STR_LEN];
|
|
||||||
int master_response_timeout;
|
|
||||||
int reconnect_attempts;
|
|
||||||
int reconnect_intvl;
|
|
||||||
char pg_bindir[MAXLEN];
|
|
||||||
char pgctl_options[MAXLEN];
|
|
||||||
char logfile[MAXLEN];
|
|
||||||
int monitor_interval_secs;
|
|
||||||
int retry_promote_interval_secs;
|
|
||||||
} t_configuration_options;
|
} t_configuration_options;
|
||||||
|
|
||||||
#define T_CONFIGURATION_OPTIONS_INITIALIZER { "", -1, "", MANUAL_FAILOVER, -1, "", "", "", "", "", "", "", -1, -1, -1, "", "", "", 0, 0 }
|
void parse_config(const char* config_file, t_configuration_options* options);
|
||||||
|
|
||||||
void parse_config(const char *config_file, t_configuration_options *options);
|
|
||||||
void parse_line(char *buff, char *name, char *value);
|
void parse_line(char *buff, char *name, char *value);
|
||||||
char *trim(char *s);
|
char *trim(char *s);
|
||||||
bool reload_configuration(char *config_file, t_configuration_options *orig_options);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
287
dbutils.c
287
dbutils.c
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* dbutils.c - Database connection/management functions
|
* dbutils.c - Database connection/management functions
|
||||||
* Copyright (C) 2ndQuadrant, 2010-2014
|
* Copyright (C) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -17,10 +17,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
#include "repmgr.h"
|
#include "repmgr.h"
|
||||||
#include "strutil.h"
|
#include "strutil.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
@@ -29,12 +25,7 @@ PGconn *
|
|||||||
establishDBConnection(const char *conninfo, const bool exit_on_error)
|
establishDBConnection(const char *conninfo, const bool exit_on_error)
|
||||||
{
|
{
|
||||||
/* Make a connection to the database */
|
/* Make a connection to the database */
|
||||||
PGconn *conn = NULL;
|
PGconn *conn = PQconnectdb(conninfo);
|
||||||
char connection_string[MAXLEN];
|
|
||||||
|
|
||||||
strcpy(connection_string, conninfo);
|
|
||||||
strcat(connection_string, " fallback_application_name='repmgr'");
|
|
||||||
conn = PQconnectdb(connection_string);
|
|
||||||
|
|
||||||
/* Check to see that the backend connection was successfully made */
|
/* Check to see that the backend connection was successfully made */
|
||||||
if ((PQstatus(conn) != CONNECTION_OK))
|
if ((PQstatus(conn) != CONNECTION_OK))
|
||||||
@@ -73,104 +64,33 @@ establishDBConnectionByParams(const char *keywords[], const char *values[],const
|
|||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
bool
|
||||||
is_standby(PGconn *conn)
|
is_standby(PGconn *conn)
|
||||||
{
|
{
|
||||||
PGresult *res;
|
PGresult *res;
|
||||||
int result = 0;
|
bool result;
|
||||||
|
|
||||||
res = PQexec(conn, "SELECT pg_is_in_recovery()");
|
res = PQexec(conn, "SELECT pg_is_in_recovery()");
|
||||||
|
|
||||||
if (res == NULL || PQresultStatus(res) != PGRES_TUPLES_OK)
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||||
{
|
{
|
||||||
log_err(_("Can't query server mode: %s"),
|
log_err(_("Can't query server mode: %s"),
|
||||||
PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
result = -1;
|
PQclear(res);
|
||||||
|
PQfinish(conn);
|
||||||
|
exit(ERR_DB_QUERY);
|
||||||
}
|
}
|
||||||
else if (PQntuples(res) == 1 && strcmp(PQgetvalue(res, 0, 0), "t") == 0)
|
|
||||||
result = 1;
|
if (strcmp(PQgetvalue(res, 0, 0), "f") == 0)
|
||||||
|
result = false;
|
||||||
|
else
|
||||||
|
result = true;
|
||||||
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int
|
|
||||||
is_witness(PGconn *conn, char *schema, char *cluster, int node_id)
|
|
||||||
{
|
|
||||||
PGresult *res;
|
|
||||||
int result = 0;
|
|
||||||
char sqlquery[QUERY_STR_LEN];
|
|
||||||
|
|
||||||
sqlquery_snprintf(sqlquery, "SELECT witness from %s.repl_nodes where cluster = '%s' and id = %d",
|
|
||||||
schema, cluster, node_id);
|
|
||||||
res = PQexec(conn, sqlquery);
|
|
||||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
||||||
{
|
|
||||||
log_err(_("Can't query server mode: %s"), PQerrorMessage(conn));
|
|
||||||
result = -1;
|
|
||||||
}
|
|
||||||
else if (PQntuples(res) == 1 && strcmp(PQgetvalue(res, 0, 0), "t") == 0)
|
|
||||||
result = 1;
|
|
||||||
|
|
||||||
PQclear(res);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* check the PQStatus and try to 'select 1' to confirm good connection */
|
|
||||||
bool
|
|
||||||
is_pgup(PGconn *conn, int timeout)
|
|
||||||
{
|
|
||||||
char sqlquery[QUERY_STR_LEN];
|
|
||||||
/* Check the connection status twice in case it changes after reset */
|
|
||||||
bool twice = false;
|
|
||||||
|
|
||||||
/* Check the connection status twice in case it changes after reset */
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (PQstatus(conn) != CONNECTION_OK)
|
|
||||||
{
|
|
||||||
if (twice)
|
|
||||||
return false;
|
|
||||||
PQreset(conn); /* reconnect */
|
|
||||||
twice = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* Send a SELECT 1 just to check if the connection is OK
|
|
||||||
*/
|
|
||||||
if (!CancelQuery(conn, timeout))
|
|
||||||
goto failed;
|
|
||||||
if (wait_connection_availability(conn, timeout) != 1)
|
|
||||||
goto failed;
|
|
||||||
|
|
||||||
sqlquery_snprintf(sqlquery, "SELECT 1");
|
|
||||||
if (PQsendQuery(conn, sqlquery) == 0)
|
|
||||||
{
|
|
||||||
log_warning(_("PQsendQuery: Query could not be sent to primary. %s\n"),
|
|
||||||
PQerrorMessage(conn));
|
|
||||||
goto failed;
|
|
||||||
}
|
|
||||||
if (wait_connection_availability(conn, timeout) != 1)
|
|
||||||
goto failed;
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
failed:
|
|
||||||
/* we need to retry, because we might just have loose the connection once */
|
|
||||||
if (twice)
|
|
||||||
return false;
|
|
||||||
PQreset(conn); /* reconnect */
|
|
||||||
twice = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If postgreSQL version is 9 or superior returns the major version
|
* If postgreSQL version is 9 or superior returns the major version
|
||||||
* if 8 or inferior returns an empty string
|
* if 8 or inferior returns an empty string
|
||||||
@@ -194,7 +114,8 @@ pg_version(PGconn *conn, char* major_version)
|
|||||||
log_err(_("Version check PQexec failed: %s"),
|
log_err(_("Version check PQexec failed: %s"),
|
||||||
PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
return NULL;
|
PQfinish(conn);
|
||||||
|
exit(ERR_DB_QUERY);
|
||||||
}
|
}
|
||||||
|
|
||||||
major_version1 = atoi(PQgetvalue(res, 0, 0));
|
major_version1 = atoi(PQgetvalue(res, 0, 0));
|
||||||
@@ -215,13 +136,12 @@ pg_version(PGconn *conn, char* major_version)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
bool
|
||||||
guc_set(PGconn *conn, const char *parameter, const char *op,
|
guc_setted(PGconn *conn, const char *parameter, const char *op,
|
||||||
const char *value)
|
const char *value)
|
||||||
{
|
{
|
||||||
PGresult *res;
|
PGresult *res;
|
||||||
char sqlquery[QUERY_STR_LEN];
|
char sqlquery[QUERY_STR_LEN];
|
||||||
int retval = 1;
|
|
||||||
|
|
||||||
sqlquery_snprintf(sqlquery, "SELECT true FROM pg_settings "
|
sqlquery_snprintf(sqlquery, "SELECT true FROM pg_settings "
|
||||||
" WHERE name = '%s' AND setting %s '%s'",
|
" WHERE name = '%s' AND setting %s '%s'",
|
||||||
@@ -232,49 +152,18 @@ guc_set(PGconn *conn, const char *parameter, const char *op,
|
|||||||
{
|
{
|
||||||
log_err(_("GUC setting check PQexec failed: %s"),
|
log_err(_("GUC setting check PQexec failed: %s"),
|
||||||
PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
retval = -1;
|
PQclear(res);
|
||||||
|
PQfinish(conn);
|
||||||
|
exit(ERR_DB_QUERY);
|
||||||
}
|
}
|
||||||
else if (PQntuples(res) == 0)
|
if (PQntuples(res) == 0)
|
||||||
{
|
{
|
||||||
retval = 0;
|
PQclear(res);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
|
|
||||||
return retval;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Just like guc_set except with an extra parameter containing the name of
|
|
||||||
* the pg datatype so that the comparison can be done properly.
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
guc_set_typed(PGconn *conn, const char *parameter, const char *op,
|
|
||||||
const char *value, const char *datatype)
|
|
||||||
{
|
|
||||||
PGresult *res;
|
|
||||||
char sqlquery[QUERY_STR_LEN];
|
|
||||||
int retval = 1;
|
|
||||||
|
|
||||||
sqlquery_snprintf(sqlquery, "SELECT true FROM pg_settings "
|
|
||||||
" WHERE name = '%s' AND setting::%s %s '%s'::%s",
|
|
||||||
parameter, datatype, op, value, datatype);
|
|
||||||
|
|
||||||
res = PQexec(conn, sqlquery);
|
|
||||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
||||||
{
|
|
||||||
log_err(_("GUC setting check PQexec failed: %s"),
|
|
||||||
PQerrorMessage(conn));
|
|
||||||
retval = -1;
|
|
||||||
}
|
|
||||||
else if (PQntuples(res) == 0)
|
|
||||||
{
|
|
||||||
retval = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
PQclear(res);
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -282,7 +171,7 @@ const char *
|
|||||||
get_cluster_size(PGconn *conn)
|
get_cluster_size(PGconn *conn)
|
||||||
{
|
{
|
||||||
PGresult *res;
|
PGresult *res;
|
||||||
const char *size = NULL;
|
const char *size;
|
||||||
char sqlquery[QUERY_STR_LEN];
|
char sqlquery[QUERY_STR_LEN];
|
||||||
|
|
||||||
sqlquery_snprintf(
|
sqlquery_snprintf(
|
||||||
@@ -295,12 +184,11 @@ get_cluster_size(PGconn *conn)
|
|||||||
{
|
{
|
||||||
log_err(_("Get cluster size PQexec failed: %s"),
|
log_err(_("Get cluster size PQexec failed: %s"),
|
||||||
PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
|
PQclear(res);
|
||||||
|
PQfinish(conn);
|
||||||
|
exit(ERR_DB_QUERY);
|
||||||
}
|
}
|
||||||
else
|
size = PQgetvalue(res, 0, 0);
|
||||||
{
|
|
||||||
size = PQgetvalue(res, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
@@ -314,7 +202,7 @@ get_cluster_size(PGconn *conn)
|
|||||||
* connection string is placed there.
|
* connection string is placed there.
|
||||||
*/
|
*/
|
||||||
PGconn *
|
PGconn *
|
||||||
getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
getMasterConnection(PGconn *standby_conn, char *cluster,
|
||||||
int *master_id, char *master_conninfo_out)
|
int *master_id, char *master_conninfo_out)
|
||||||
{
|
{
|
||||||
PGconn *master_conn = NULL;
|
PGconn *master_conn = NULL;
|
||||||
@@ -323,6 +211,7 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
|||||||
char sqlquery[QUERY_STR_LEN];
|
char sqlquery[QUERY_STR_LEN];
|
||||||
char master_conninfo_stack[MAXCONNINFO];
|
char master_conninfo_stack[MAXCONNINFO];
|
||||||
char *master_conninfo = &*master_conninfo_stack;
|
char *master_conninfo = &*master_conninfo_stack;
|
||||||
|
char schema_str[MAXLEN];
|
||||||
char schema_quoted[MAXLEN];
|
char schema_quoted[MAXLEN];
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
@@ -339,9 +228,10 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
|||||||
*
|
*
|
||||||
* Assemble the unquoted schema name
|
* Assemble the unquoted schema name
|
||||||
*/
|
*/
|
||||||
|
maxlen_snprintf(schema_str, "repmgr_%s", cluster);
|
||||||
{
|
{
|
||||||
char *identifier = PQescapeIdentifier(standby_conn, schema,
|
char *identifier = PQescapeIdentifier(standby_conn, schema_str,
|
||||||
strlen(schema));
|
strlen(schema_str));
|
||||||
|
|
||||||
maxlen_snprintf(schema_quoted, "%s", identifier);
|
maxlen_snprintf(schema_quoted, "%s", identifier);
|
||||||
PQfreemem(identifier);
|
PQfreemem(identifier);
|
||||||
@@ -351,8 +241,8 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
|||||||
log_info(_("finding node list for cluster '%s'\n"),
|
log_info(_("finding node list for cluster '%s'\n"),
|
||||||
cluster);
|
cluster);
|
||||||
|
|
||||||
sqlquery_snprintf(sqlquery, "SELECT id, conninfo FROM %s.repl_nodes "
|
sqlquery_snprintf(sqlquery, "SELECT * FROM %s.repl_nodes "
|
||||||
" WHERE cluster = '%s' and not witness",
|
" WHERE cluster = '%s'",
|
||||||
schema_quoted, cluster);
|
schema_quoted, cluster);
|
||||||
|
|
||||||
res1 = PQexec(standby_conn, sqlquery);
|
res1 = PQexec(standby_conn, sqlquery);
|
||||||
@@ -361,14 +251,15 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
|||||||
log_err(_("Can't get nodes info: %s\n"),
|
log_err(_("Can't get nodes info: %s\n"),
|
||||||
PQerrorMessage(standby_conn));
|
PQerrorMessage(standby_conn));
|
||||||
PQclear(res1);
|
PQclear(res1);
|
||||||
return NULL;
|
PQfinish(standby_conn);
|
||||||
|
exit(ERR_DB_QUERY);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < PQntuples(res1); i++)
|
for (i = 0; i < PQntuples(res1); i++)
|
||||||
{
|
{
|
||||||
/* initialize with the values of the current node being processed */
|
/* initialize with the values of the current node being processed */
|
||||||
*master_id = atoi(PQgetvalue(res1, i, 0));
|
*master_id = atoi(PQgetvalue(res1, i, 0));
|
||||||
strncpy(master_conninfo, PQgetvalue(res1, i, 1), MAXCONNINFO);
|
strncpy(master_conninfo, PQgetvalue(res1, i, 2), MAXCONNINFO);
|
||||||
log_info(_("checking role of cluster node '%s'\n"),
|
log_info(_("checking role of cluster node '%s'\n"),
|
||||||
master_conninfo);
|
master_conninfo);
|
||||||
master_conn = establishDBConnection(master_conninfo, false);
|
master_conn = establishDBConnection(master_conninfo, false);
|
||||||
@@ -420,103 +311,3 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
|||||||
PQclear(res1);
|
PQclear(res1);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* wait until current query finishes ignoring any results, this could be an async command
|
|
||||||
* or a cancelation of a query
|
|
||||||
* return 1 if Ok; 0 if any error ocurred; -1 if timeout reached
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
wait_connection_availability(PGconn *conn, long long timeout)
|
|
||||||
{
|
|
||||||
PGresult *res;
|
|
||||||
fd_set read_set;
|
|
||||||
int sock = PQsocket(conn);
|
|
||||||
struct timeval tmout, before, after;
|
|
||||||
struct timezone tz;
|
|
||||||
|
|
||||||
/* recalc to microseconds */
|
|
||||||
timeout *= 1000000;
|
|
||||||
|
|
||||||
while (timeout > 0)
|
|
||||||
{
|
|
||||||
if (PQconsumeInput(conn) == 0)
|
|
||||||
{
|
|
||||||
log_warning(_("wait_connection_availability: could not receive data from connection. %s\n"),
|
|
||||||
PQerrorMessage(conn));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (PQisBusy(conn) == 0)
|
|
||||||
{
|
|
||||||
do {
|
|
||||||
res = PQgetResult(conn);
|
|
||||||
PQclear(res);
|
|
||||||
} while(res != NULL);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
tmout.tv_sec = 0;
|
|
||||||
tmout.tv_usec = 250000;
|
|
||||||
|
|
||||||
FD_ZERO(&read_set);
|
|
||||||
FD_SET(sock, &read_set);
|
|
||||||
|
|
||||||
gettimeofday(&before, &tz);
|
|
||||||
if (select(sock, &read_set, NULL, NULL, &tmout) == -1)
|
|
||||||
{
|
|
||||||
log_warning(
|
|
||||||
_("wait_connection_availability: select() returned with error: %s"),
|
|
||||||
strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
gettimeofday(&after, &tz);
|
|
||||||
|
|
||||||
timeout -= (after.tv_sec * 1000000 + after.tv_usec) -
|
|
||||||
(before.tv_sec * 1000000 + before.tv_usec);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (timeout >= 0)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
log_warning(_("wait_connection_availability: timeout reached"));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool
|
|
||||||
CancelQuery(PGconn *conn, int timeout)
|
|
||||||
{
|
|
||||||
char errbuf[ERRBUFF_SIZE];
|
|
||||||
PGcancel *pgcancel;
|
|
||||||
|
|
||||||
if (wait_connection_availability(conn, timeout) != 1)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
pgcancel = PQgetCancel(conn);
|
|
||||||
|
|
||||||
if (pgcancel == NULL)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* PQcancel can only return 0 if socket()/connect()/send()
|
|
||||||
* fails, in any of those cases we can assume something
|
|
||||||
* bad happened to the connection
|
|
||||||
*/
|
|
||||||
if (PQcancel(pgcancel, errbuf, ERRBUFF_SIZE) == 0)
|
|
||||||
{
|
|
||||||
log_warning(_("Can't stop current query: %s\n"), errbuf);
|
|
||||||
PQfreeCancel(pgcancel);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
PQfreeCancel(pgcancel);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|||||||
15
dbutils.h
15
dbutils.h
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* dbutils.h
|
* dbutils.h
|
||||||
* Copyright (c) 2ndQuadrant, 2010-2014
|
* Copyright (c) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -26,19 +26,12 @@ PGconn *establishDBConnection(const char *conninfo, const bool exit_on_error);
|
|||||||
PGconn *establishDBConnectionByParams(const char *keywords[],
|
PGconn *establishDBConnectionByParams(const char *keywords[],
|
||||||
const char *values[],
|
const char *values[],
|
||||||
const bool exit_on_error);
|
const bool exit_on_error);
|
||||||
int is_standby(PGconn *conn);
|
bool is_standby(PGconn *conn);
|
||||||
int is_witness(PGconn *conn, char *schema, char *cluster, int node_id);
|
|
||||||
bool is_pgup(PGconn *conn, int timeout);
|
|
||||||
char *pg_version(PGconn *conn, char* major_version);
|
char *pg_version(PGconn *conn, char* major_version);
|
||||||
int guc_set(PGconn *conn, const char *parameter, const char *op,
|
bool guc_setted(PGconn *conn, const char *parameter, const char *op,
|
||||||
const char *value);
|
const char *value);
|
||||||
int guc_set_typed(PGconn *conn, const char *parameter, const char *op,
|
|
||||||
const char *value, const char *datatype);
|
|
||||||
|
|
||||||
const char *get_cluster_size(PGconn *conn);
|
const char *get_cluster_size(PGconn *conn);
|
||||||
PGconn *getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
PGconn *getMasterConnection(PGconn *standby_conn, char *cluster,
|
||||||
int *master_id, char *master_conninfo_out);
|
int *master_id, char *master_conninfo_out);
|
||||||
|
|
||||||
int wait_connection_availability(PGconn *conn, long long timeout);
|
|
||||||
bool CancelQuery(PGconn *conn, int timeout);
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
8
debian/DEBIAN/control
vendored
8
debian/DEBIAN/control
vendored
@@ -1,9 +1,9 @@
|
|||||||
Package: repmgr-auto
|
Package: repmgr
|
||||||
Version: 2.0beta2
|
Version: 1.0-1
|
||||||
Section: database
|
Section: database
|
||||||
Priority: optional
|
Priority: optional
|
||||||
Architecture: all
|
Architecture: all
|
||||||
Depends: rsync, postgresql-9.0 | postgresql-9.1 | postgresql-9.2 | postgresql-9.3
|
Depends: rsync, postgresql-9.0
|
||||||
Maintainer: Jaime Casanova <jaime@2ndQuadrant.com>
|
Maintainer: Greg Smith <greg@2ndQuadrant.com>
|
||||||
Description: PostgreSQL replication setup, magament and monitoring
|
Description: PostgreSQL replication setup, magament and monitoring
|
||||||
has two main executables
|
has two main executables
|
||||||
|
|||||||
18
debian/repmgr.repmgrd.default
vendored
18
debian/repmgr.repmgrd.default
vendored
@@ -1,18 +0,0 @@
|
|||||||
# default settings for repmgrd. This file is source by /bin/sh from
|
|
||||||
# /etc/init.d/repmgrd
|
|
||||||
|
|
||||||
# disable repmgrd by default so it won't get started upon installation
|
|
||||||
# valid values: yes/no
|
|
||||||
REPMGRD_ENABLED=no
|
|
||||||
|
|
||||||
# Options for repmgrd (required)
|
|
||||||
#REPMGRD_OPTS="--config_file /path/to/repmgr.conf"
|
|
||||||
|
|
||||||
# User to run repmgrd as
|
|
||||||
#REPMGRD_USER=postgres
|
|
||||||
|
|
||||||
# repmgrd binary
|
|
||||||
#REPMGR_BIN=/usr/bin/repmgr
|
|
||||||
|
|
||||||
# pid file
|
|
||||||
#REPMGR_PIDFILE=/var/run/repmgrd.pid
|
|
||||||
101
debian/repmgr.repmgrd.init
vendored
101
debian/repmgr.repmgrd.init
vendored
@@ -1,101 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
### BEGIN INIT INFO
|
|
||||||
# Provides: repmgrd
|
|
||||||
# Required-Start: $local_fs $remote_fs $network $syslog postgresql
|
|
||||||
# Required-Stop: $local_fs $remote_fs $network $syslog postgresql
|
|
||||||
# Should-Start: $syslog postgresql
|
|
||||||
# Default-Start: 2 3 4 5
|
|
||||||
# Default-Stop: 0 1 6
|
|
||||||
# Short-Description: Start/stop repmgrd
|
|
||||||
# Description: Enable repmgrd replication management and monitoring daemon for PostgreSQL
|
|
||||||
### END INIT INFO
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
DESC="PostgreSQL replication management and monitoring daemon"
|
|
||||||
NAME=repmgrd
|
|
||||||
|
|
||||||
REPMGRD_ENABLED=no
|
|
||||||
REPMGRD_OPTS=
|
|
||||||
REPMGRD_USER=postgres
|
|
||||||
REPMGRD_BIN=/usr/bin/repmgrd
|
|
||||||
REPMGRD_PIDFILE=/var/run/repmgrd.pid
|
|
||||||
|
|
||||||
# Read configuration variable file if it is present
|
|
||||||
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
|
|
||||||
|
|
||||||
test -x $REPMGRD_BIN || exit 0
|
|
||||||
|
|
||||||
case "$REPMGRD_ENABLED" in
|
|
||||||
[Yy]*)
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# Define LSB log_* functions.
|
|
||||||
. /lib/lsb/init-functions
|
|
||||||
|
|
||||||
if [ -z "$REPMGRD_OPTS" ]
|
|
||||||
then
|
|
||||||
log_warning_msg "Not starting $NAME, REPMGRD_OPTS not set in /etc/default/$NAME"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
do_start()
|
|
||||||
{
|
|
||||||
# Return
|
|
||||||
# 0 if daemon has been started
|
|
||||||
# 1 if daemon was already running
|
|
||||||
# other if daemon could not be started or a failure occured
|
|
||||||
start-stop-daemon --start --quiet --chuid $REPMGRD_USER --make-pidfile --pidfile $REPMGRD_PIDFILE --exec $REPMGRD_BIN -- $REPMGRD_OPTS
|
|
||||||
}
|
|
||||||
|
|
||||||
do_stop()
|
|
||||||
{
|
|
||||||
# Return
|
|
||||||
# 0 if daemon has been stopped
|
|
||||||
# 1 if daemon was already stopped
|
|
||||||
# other if daemon could not be stopped or a failure occurred
|
|
||||||
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $REPMGRD_PIDFILE --exec $REPMGRD_BIN
|
|
||||||
}
|
|
||||||
|
|
||||||
case "$1" in
|
|
||||||
start)
|
|
||||||
log_daemon_msg "Starting $DESC" "$NAME"
|
|
||||||
do_start
|
|
||||||
case "$?" in
|
|
||||||
0) log_end_msg 0 ;;
|
|
||||||
1) log_progress_msg "already started"
|
|
||||||
log_end_msg 0 ;;
|
|
||||||
*) log_end_msg 1 ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
;;
|
|
||||||
stop)
|
|
||||||
log_daemon_msg "Stopping $DESC" "$NAME"
|
|
||||||
do_stop
|
|
||||||
case "$?" in
|
|
||||||
0) log_end_msg 0 ;;
|
|
||||||
1) log_progress_msg "already stopped"
|
|
||||||
log_end_msg 0 ;;
|
|
||||||
*) log_end_msg 1 ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
;;
|
|
||||||
restart|force-reload)
|
|
||||||
$0 stop
|
|
||||||
$0 start
|
|
||||||
;;
|
|
||||||
status)
|
|
||||||
status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
|
|
||||||
exit 3
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
exit 0
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* errcode.h
|
* errcode.h
|
||||||
* Copyright (C) 2ndQuadrant, 2010-2014
|
* Copyright (C) 2ndQuadrant, 2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -33,8 +33,5 @@
|
|||||||
#define ERR_PROMOTED 8
|
#define ERR_PROMOTED 8
|
||||||
#define ERR_BAD_PASSWORD 9
|
#define ERR_BAD_PASSWORD 9
|
||||||
#define ERR_STR_OVERFLOW 10
|
#define ERR_STR_OVERFLOW 10
|
||||||
#define ERR_FAILOVER_FAIL 11
|
|
||||||
#define ERR_BAD_SSH 12
|
|
||||||
#define ERR_SYS_FAILURE 13
|
|
||||||
|
|
||||||
#endif /* _ERRCODE_H_ */
|
#endif /* _ERRCODE_H_ */
|
||||||
|
|||||||
45
log.c
45
log.c
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* log.c - Logging methods
|
* log.c - Logging methods
|
||||||
* Copyright (C) 2ndQuadrant, 2010-2014
|
* Copyright (C) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
* This module is a set of methods for logging (currently only syslog)
|
* This module is a set of methods for logging (currently only syslog)
|
||||||
*
|
*
|
||||||
@@ -25,10 +25,8 @@
|
|||||||
|
|
||||||
#ifdef HAVE_SYSLOG
|
#ifdef HAVE_SYSLOG
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <time.h>
|
#endif
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
@@ -39,38 +37,13 @@
|
|||||||
|
|
||||||
/* #define REPMGR_DEBUG */
|
/* #define REPMGR_DEBUG */
|
||||||
|
|
||||||
void stderr_log_with_level(const char *level_name, int level, const char *fmt, ...) {
|
|
||||||
size_t len = strlen(fmt);
|
|
||||||
char fmt1[len + 150];
|
|
||||||
time_t t;
|
|
||||||
struct tm *tm;
|
|
||||||
char buff[100];
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
if(log_level >= level) {
|
|
||||||
time(&t);
|
|
||||||
tm = localtime(&t);
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
|
|
||||||
strftime(buff, 100, "[%Y-%m-%d %H:%M:%S]", tm);
|
|
||||||
snprintf(fmt1, len + 150, "%s [%s] %s", buff, level_name, fmt);
|
|
||||||
vfprintf(stderr, fmt1, ap);
|
|
||||||
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
fflush(stderr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int detect_log_level(const char* level);
|
static int detect_log_level(const char* level);
|
||||||
static int detect_log_facility(const char* facility);
|
static int detect_log_facility(const char* facility);
|
||||||
|
|
||||||
int log_type = REPMGR_STDERR;
|
int log_type = REPMGR_STDERR;
|
||||||
int log_level = LOG_NOTICE;
|
int log_level = LOG_NOTICE;
|
||||||
|
|
||||||
bool logger_init(t_configuration_options *opts, const char* ident, const char* level, const char* facility)
|
bool logger_init(const char* ident, const char* level, const char* facility)
|
||||||
{
|
{
|
||||||
|
|
||||||
int l;
|
int l;
|
||||||
@@ -142,18 +115,6 @@ bool logger_init(t_configuration_options *opts, const char* ident, const char* l
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (*opts->logfile)
|
|
||||||
{
|
|
||||||
FILE *fd;
|
|
||||||
fd = freopen(opts->logfile, "a", stderr);
|
|
||||||
|
|
||||||
if (fd == NULL)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "error reopening stderr to '%s': %s",
|
|
||||||
opts->logfile, strerror(errno));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
22
log.h
22
log.h
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* log.h
|
* log.h
|
||||||
* Copyright (c) 2ndQuadrant, 2010-2014
|
* Copyright (c) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -25,17 +25,15 @@
|
|||||||
#define REPMGR_SYSLOG 1
|
#define REPMGR_SYSLOG 1
|
||||||
#define REPMGR_STDERR 2
|
#define REPMGR_STDERR 2
|
||||||
|
|
||||||
void stderr_log_with_level(const char *level_name, int level, const char *fmt, ...) __attribute__ ((format (PG_PRINTF_ATTRIBUTE, 3, 4)));
|
|
||||||
|
|
||||||
/* Standard error logging */
|
/* Standard error logging */
|
||||||
#define stderr_log_debug(...) stderr_log_with_level("DEBUG", LOG_DEBUG, __VA_ARGS__)
|
#define stderr_log_debug(...) if (log_level >= LOG_DEBUG) fprintf(stderr, __VA_ARGS__)
|
||||||
#define stderr_log_info(...) stderr_log_with_level("INFO", LOG_INFO, __VA_ARGS__)
|
#define stderr_log_info(...) if (log_level >= LOG_INFO) fprintf(stderr, __VA_ARGS__)
|
||||||
#define stderr_log_notice(...) stderr_log_with_level("NOTICE", LOG_NOTICE, __VA_ARGS__)
|
#define stderr_log_notice(...) if (log_level >= LOG_NOTICE) fprintf(stderr, __VA_ARGS__)
|
||||||
#define stderr_log_warning(...) stderr_log_with_level("WARNING", LOG_WARNING, __VA_ARGS__)
|
#define stderr_log_warning(...) if (log_level >= LOG_WARNING) fprintf(stderr, __VA_ARGS__)
|
||||||
#define stderr_log_err(...) stderr_log_with_level("ERROR", LOG_ERR, __VA_ARGS__)
|
#define stderr_log_err(...) if (log_level >= LOG_ERR) fprintf(stderr, __VA_ARGS__)
|
||||||
#define stderr_log_crit(...) stderr_log_with_level("CRITICAL", LOG_CRIT, __VA_ARGS__)
|
#define stderr_log_crit(...) if (log_level >= LOG_CRIT) fprintf(stderr, __VA_ARGS__)
|
||||||
#define stderr_log_alert(...) stderr_log_with_level("ALERT", LOG_ALERT, __VA_ARGS__)
|
#define stderr_log_alert(...) if (log_level >= LOG_ALERT) fprintf(stderr, __VA_ARGS__)
|
||||||
#define stderr_log_emerg(...) stderr_log_with_level("EMERGENCY", LOG_EMERG, __VA_ARGS__)
|
#define stderr_log_emerg(...) if (log_level >= LOG_EMERG) fprintf(stderr, __VA_ARGS__)
|
||||||
|
|
||||||
#ifdef HAVE_SYSLOG
|
#ifdef HAVE_SYSLOG
|
||||||
|
|
||||||
@@ -114,7 +112,7 @@ void stderr_log_with_level(const char *level_name, int level, const char *fmt, .
|
|||||||
|
|
||||||
/* Logger initialisation and shutdown */
|
/* Logger initialisation and shutdown */
|
||||||
bool logger_shutdown(void);
|
bool logger_shutdown(void);
|
||||||
bool logger_init(t_configuration_options *opts, const char* ident, const char* level, const char* facility);
|
bool logger_init(const char* ident, const char* level, const char* facility);
|
||||||
void logger_min_verbose(int minimum);
|
void logger_min_verbose(int minimum);
|
||||||
|
|
||||||
extern int log_type;
|
extern int log_type;
|
||||||
|
|||||||
21
repmgr.conf
Normal file
21
repmgr.conf
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
###################################################
|
||||||
|
# Replication Manager configuration file
|
||||||
|
###################################################
|
||||||
|
|
||||||
|
# Cluster name
|
||||||
|
cluster=test
|
||||||
|
|
||||||
|
# Node ID
|
||||||
|
node=2
|
||||||
|
|
||||||
|
# Connection information
|
||||||
|
conninfo='host=192.168.204.104'
|
||||||
|
rsync_options=--archive --checksum --compress --progress --rsh=ssh
|
||||||
|
|
||||||
|
# Log level: possible values are DEBUG, INFO, NOTICE, WARNING, ERR, ALERT, CRIT or EMERG
|
||||||
|
# Default: NOTICE
|
||||||
|
loglevel=NOTICE
|
||||||
|
|
||||||
|
# Logging facility: possible values are STDERR or - for Syslog integration - one of LOCAL0, LOCAL1, ..., LOCAL7, USER
|
||||||
|
# Default: STDERR
|
||||||
|
logfacility=STDERR
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
###################################################
|
|
||||||
# Replication Manager configuration file
|
|
||||||
###################################################
|
|
||||||
|
|
||||||
# Cluster name
|
|
||||||
cluster=test
|
|
||||||
|
|
||||||
# Node ID
|
|
||||||
node=2
|
|
||||||
node_name=standby2
|
|
||||||
|
|
||||||
# Connection information
|
|
||||||
conninfo='host=192.168.204.104'
|
|
||||||
rsync_options=--archive --checksum --compress --progress --rsh="ssh -o \"StrictHostKeyChecking no\""
|
|
||||||
ssh_options=-o "StrictHostKeyChecking no"
|
|
||||||
|
|
||||||
# How many seconds we wait for master response before declaring master failure
|
|
||||||
master_response_timeout=60
|
|
||||||
|
|
||||||
# How many time we try to reconnect to master before starting failover procedure
|
|
||||||
reconnect_attempts=6
|
|
||||||
reconnect_interval=10
|
|
||||||
|
|
||||||
# Autofailover options
|
|
||||||
failover=manual
|
|
||||||
priority=-1
|
|
||||||
promote_command='repmgr standby promote -f /path/to/repmgr.conf'
|
|
||||||
follow_command='repmgr standby follow -f /path/to/repmgr.conf -W'
|
|
||||||
|
|
||||||
# Log level: possible values are DEBUG, INFO, NOTICE, WARNING, ERR, ALERT, CRIT or EMERG
|
|
||||||
# Default: NOTICE
|
|
||||||
loglevel=NOTICE
|
|
||||||
|
|
||||||
# Logging facility: possible values are STDERR or - for Syslog integration - one of LOCAL0, LOCAL1, ..., LOCAL7, USER
|
|
||||||
# Default: STDERR
|
|
||||||
logfacility=STDERR
|
|
||||||
|
|
||||||
# path to pg_ctl executable
|
|
||||||
pg_bindir=/usr/bin/
|
|
||||||
|
|
||||||
#
|
|
||||||
# you may add command line arguments for pg_ctl
|
|
||||||
#
|
|
||||||
# pg_ctl_options='-s'
|
|
||||||
|
|
||||||
#
|
|
||||||
# redirect stderr to a logfile
|
|
||||||
#
|
|
||||||
# logfile='/var/log/repmgr.log'
|
|
||||||
|
|
||||||
#
|
|
||||||
# change monitoring interval; default is 2s
|
|
||||||
#
|
|
||||||
# monitor_interval_secs=2
|
|
||||||
|
|
||||||
#
|
|
||||||
# change wait time for master; before we bail out and exit when the
|
|
||||||
# master disappears, we wait 6 * retry_promote_interval_secs seconds;
|
|
||||||
# by default this would be half an hour (since sleep_delay default
|
|
||||||
# value is 300)
|
|
||||||
#
|
|
||||||
# retry_promote_interval_secs=300
|
|
||||||
12
repmgr.h
12
repmgr.h
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* repmgr.h
|
* repmgr.h
|
||||||
* Copyright (c) 2ndQuadrant, 2010-2014
|
* Copyright (c) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -30,7 +30,6 @@
|
|||||||
|
|
||||||
#define PRIMARY_MODE 0
|
#define PRIMARY_MODE 0
|
||||||
#define STANDBY_MODE 1
|
#define STANDBY_MODE 1
|
||||||
#define WITNESS_MODE 2
|
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#define MAXFILENAME 1024
|
#define MAXFILENAME 1024
|
||||||
@@ -43,9 +42,6 @@
|
|||||||
#define DEFAULT_DBNAME "postgres"
|
#define DEFAULT_DBNAME "postgres"
|
||||||
#define DEFAULT_REPMGR_SCHEMA_PREFIX "repmgr_"
|
#define DEFAULT_REPMGR_SCHEMA_PREFIX "repmgr_"
|
||||||
|
|
||||||
#define MANUAL_FAILOVER 0
|
|
||||||
#define AUTOMATIC_FAILOVER 1
|
|
||||||
|
|
||||||
/* Run time options type */
|
/* Run time options type */
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@@ -59,16 +55,12 @@ typedef struct
|
|||||||
char wal_keep_segments[MAXLEN];
|
char wal_keep_segments[MAXLEN];
|
||||||
bool verbose;
|
bool verbose;
|
||||||
bool force;
|
bool force;
|
||||||
bool wait_for_master;
|
|
||||||
bool ignore_rsync_warn;
|
bool ignore_rsync_warn;
|
||||||
|
|
||||||
char masterport[MAXLEN];
|
char masterport[MAXLEN];
|
||||||
char localport[MAXLEN];
|
|
||||||
|
|
||||||
/* parameter used by CLUSTER CLEANUP */
|
/* parameter used by CLUSTER CLEANUP */
|
||||||
int keep_history;
|
int keep_history;
|
||||||
} t_runtime_options;
|
} t_runtime_options;
|
||||||
|
|
||||||
#define T_RUNTIME_OPTIONS_INITIALIZER { "", "", "", "", "", "", DEFAULT_WAL_KEEP_SEGMENTS, false, false, false, false, "", "", 0 }
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
24
repmgr.sql
24
repmgr.sql
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* repmgr.sql
|
* repmgr.sql
|
||||||
*
|
*
|
||||||
* Copyright (C) 2ndQuadrant, 2010-2014
|
* Copyright (C) 2ndQuadrant, 2011
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -14,11 +14,8 @@ CREATE SCHEMA repmgr;
|
|||||||
*/
|
*/
|
||||||
CREATE TABLE repl_nodes (
|
CREATE TABLE repl_nodes (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
cluster text not null, -- Name to identify the cluster
|
cluster text not null, -- Name to identify the cluster
|
||||||
name text not null,
|
conninfo text not null
|
||||||
conninfo text not null,
|
|
||||||
priority integer not null,
|
|
||||||
witness boolean not null default false
|
|
||||||
);
|
);
|
||||||
ALTER TABLE repl_nodes OWNER TO repmgr;
|
ALTER TABLE repl_nodes OWNER TO repmgr;
|
||||||
|
|
||||||
@@ -31,12 +28,13 @@ CREATE TABLE repl_monitor (
|
|||||||
standby_node INTEGER NOT NULL,
|
standby_node INTEGER NOT NULL,
|
||||||
last_monitor_time TIMESTAMP WITH TIME ZONE NOT NULL,
|
last_monitor_time TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||||
last_wal_primary_location TEXT NOT NULL,
|
last_wal_primary_location TEXT NOT NULL,
|
||||||
last_wal_standby_location TEXT, -- In case of a witness server this will be NULL
|
last_wal_standby_location TEXT NOT NULL,
|
||||||
replication_lag BIGINT NOT NULL,
|
replication_lag BIGINT NOT NULL,
|
||||||
apply_lag BIGINT NOT NULL
|
apply_lag BIGINT NOT NULL
|
||||||
);
|
);
|
||||||
ALTER TABLE repl_monitor OWNER TO repmgr;
|
ALTER TABLE repl_monitor OWNER TO repmgr;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This view shows the latest monitor info about every node.
|
* This view shows the latest monitor info about every node.
|
||||||
* Interesting thing to see:
|
* Interesting thing to see:
|
||||||
@@ -48,14 +46,14 @@ ALTER TABLE repl_monitor OWNER TO repmgr;
|
|||||||
* time_lag: how many seconds are we from being up-to-date with master
|
* time_lag: how many seconds are we from being up-to-date with master
|
||||||
*/
|
*/
|
||||||
CREATE VIEW repl_status AS
|
CREATE VIEW repl_status AS
|
||||||
SELECT primary_node, standby_node, name AS standby_name, last_monitor_time, last_wal_primary_location,
|
WITH monitor_info AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY primary_node, standby_node
|
||||||
|
ORDER BY last_monitor_time desc)
|
||||||
|
FROM repl_monitor)
|
||||||
|
SELECT primary_node, standby_node, last_monitor_time, last_wal_primary_location,
|
||||||
last_wal_standby_location, pg_size_pretty(replication_lag) replication_lag,
|
last_wal_standby_location, pg_size_pretty(replication_lag) replication_lag,
|
||||||
pg_size_pretty(apply_lag) apply_lag,
|
pg_size_pretty(apply_lag) apply_lag,
|
||||||
age(now(), last_monitor_time) AS time_lag
|
age(now(), last_monitor_time) AS time_lag
|
||||||
FROM repl_monitor JOIN repl_nodes ON standby_node = id
|
FROM monitor_info a
|
||||||
WHERE (standby_node, last_monitor_time) IN (SELECT standby_node, MAX(last_monitor_time)
|
WHERE row_number = 1;
|
||||||
FROM repl_monitor GROUP BY 1);
|
|
||||||
|
|
||||||
ALTER VIEW repl_status OWNER TO repmgr;
|
ALTER VIEW repl_status OWNER TO repmgr;
|
||||||
|
|
||||||
CREATE INDEX idx_repl_status_sort ON repl_monitor(last_monitor_time, standby_node);
|
|
||||||
|
|||||||
20
sql/Makefile
20
sql/Makefile
@@ -1,20 +0,0 @@
|
|||||||
#
|
|
||||||
# Makefile
|
|
||||||
# Copyright (c) 2ndQuadrant, 2010
|
|
||||||
#
|
|
||||||
|
|
||||||
MODULE_big = repmgr_funcs
|
|
||||||
DATA_built=repmgr_funcs.sql
|
|
||||||
DATA=uninstall_repmgr_funcs.sql
|
|
||||||
OBJS=repmgr_funcs.o
|
|
||||||
|
|
||||||
ifdef USE_PGXS
|
|
||||||
PG_CONFIG = pg_config
|
|
||||||
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
|
||||||
include $(PGXS)
|
|
||||||
else
|
|
||||||
subdir = contrib/repmgr/sql
|
|
||||||
top_builddir = ../../..
|
|
||||||
include $(top_builddir)/src/Makefile.global
|
|
||||||
include $(top_srcdir)/contrib/contrib-global.mk
|
|
||||||
endif
|
|
||||||
@@ -1,232 +0,0 @@
|
|||||||
/*
|
|
||||||
* repmgr_funcs.c
|
|
||||||
* Copyright (c) 2ndQuadrant, 2010
|
|
||||||
*
|
|
||||||
* Shared memory state management and some backend functions in SQL
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "postgres.h"
|
|
||||||
#include "fmgr.h"
|
|
||||||
#include "access/xlog.h"
|
|
||||||
#include "miscadmin.h"
|
|
||||||
#include "storage/ipc.h"
|
|
||||||
#include "storage/lwlock.h"
|
|
||||||
#include "storage/procarray.h"
|
|
||||||
#include "storage/shmem.h"
|
|
||||||
#include "storage/spin.h"
|
|
||||||
#include "utils/builtins.h"
|
|
||||||
#include "utils/timestamp.h"
|
|
||||||
|
|
||||||
/* same definition as the one in xlog_internal.h */
|
|
||||||
#define MAXFNAMELEN 64
|
|
||||||
|
|
||||||
PG_MODULE_MAGIC;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Global shared state
|
|
||||||
*/
|
|
||||||
typedef struct repmgrSharedState
|
|
||||||
{
|
|
||||||
LWLockId lock; /* protects search/modification */
|
|
||||||
char location[MAXFNAMELEN]; /* last known xlog location */
|
|
||||||
TimestampTz last_updated;
|
|
||||||
} repmgrSharedState;
|
|
||||||
|
|
||||||
/* Links to shared memory state */
|
|
||||||
static repmgrSharedState *shared_state = NULL;
|
|
||||||
|
|
||||||
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
|
|
||||||
|
|
||||||
void _PG_init(void);
|
|
||||||
void _PG_fini(void);
|
|
||||||
|
|
||||||
static void repmgr_shmem_startup(void);
|
|
||||||
static Size repmgr_memsize(void);
|
|
||||||
|
|
||||||
static bool repmgr_set_standby_location(char *locationstr);
|
|
||||||
|
|
||||||
Datum repmgr_update_standby_location(PG_FUNCTION_ARGS);
|
|
||||||
Datum repmgr_get_last_standby_location(PG_FUNCTION_ARGS);
|
|
||||||
|
|
||||||
PG_FUNCTION_INFO_V1(repmgr_update_standby_location);
|
|
||||||
PG_FUNCTION_INFO_V1(repmgr_get_last_standby_location);
|
|
||||||
|
|
||||||
Datum repmgr_update_last_updated(PG_FUNCTION_ARGS);
|
|
||||||
Datum repmgr_get_last_updated(PG_FUNCTION_ARGS);
|
|
||||||
|
|
||||||
PG_FUNCTION_INFO_V1(repmgr_update_last_updated);
|
|
||||||
PG_FUNCTION_INFO_V1(repmgr_get_last_updated);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Module load callback
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
_PG_init(void)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* In order to create our shared memory area, we have to be loaded via
|
|
||||||
* shared_preload_libraries. If not, fall out without hooking into any of
|
|
||||||
* the main system. (We don't throw error here because it seems useful to
|
|
||||||
* allow the repmgr functions to be created even when the
|
|
||||||
* module isn't active. The functions must protect themselves against
|
|
||||||
* being called then, however.)
|
|
||||||
*/
|
|
||||||
if (!process_shared_preload_libraries_in_progress)
|
|
||||||
return;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Request additional shared resources. (These are no-ops if we're not in
|
|
||||||
* the postmaster process.) We'll allocate or attach to the shared
|
|
||||||
* resources in repmgr_shmem_startup().
|
|
||||||
*/
|
|
||||||
RequestAddinShmemSpace(repmgr_memsize());
|
|
||||||
RequestAddinLWLocks(1);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Install hooks.
|
|
||||||
*/
|
|
||||||
prev_shmem_startup_hook = shmem_startup_hook;
|
|
||||||
shmem_startup_hook = repmgr_shmem_startup;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Module unload callback
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
_PG_fini(void)
|
|
||||||
{
|
|
||||||
/* Uninstall hooks. */
|
|
||||||
shmem_startup_hook = prev_shmem_startup_hook;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* shmem_startup hook: allocate or attach to shared memory,
|
|
||||||
*/
|
|
||||||
static void
|
|
||||||
repmgr_shmem_startup(void)
|
|
||||||
{
|
|
||||||
bool found;
|
|
||||||
|
|
||||||
if (prev_shmem_startup_hook)
|
|
||||||
prev_shmem_startup_hook();
|
|
||||||
|
|
||||||
/* reset in case this is a restart within the postmaster */
|
|
||||||
shared_state = NULL;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Create or attach to the shared memory state, including hash table
|
|
||||||
*/
|
|
||||||
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
|
|
||||||
|
|
||||||
shared_state = ShmemInitStruct("repmgr shared state",
|
|
||||||
sizeof(repmgrSharedState),
|
|
||||||
&found);
|
|
||||||
|
|
||||||
if (!found)
|
|
||||||
{
|
|
||||||
/* First time through ... */
|
|
||||||
shared_state->lock = LWLockAssign();
|
|
||||||
snprintf(shared_state->location,
|
|
||||||
sizeof(shared_state->location), "%X/%X", 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
LWLockRelease(AddinShmemInitLock);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Estimate shared memory space needed.
|
|
||||||
*/
|
|
||||||
static Size
|
|
||||||
repmgr_memsize(void)
|
|
||||||
{
|
|
||||||
return MAXALIGN(sizeof(repmgrSharedState));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static bool
|
|
||||||
repmgr_set_standby_location(char *locationstr)
|
|
||||||
{
|
|
||||||
/* Safety check... */
|
|
||||||
if (!shared_state)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
LWLockAcquire(shared_state->lock, LW_EXCLUSIVE);
|
|
||||||
strncpy(shared_state->location, locationstr, MAXFNAMELEN);
|
|
||||||
LWLockRelease(shared_state->lock);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* SQL Functions */
|
|
||||||
|
|
||||||
/* Read last xlog location reported by this standby from shared memory */
|
|
||||||
Datum
|
|
||||||
repmgr_get_last_standby_location(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
char location[MAXFNAMELEN];
|
|
||||||
|
|
||||||
/* Safety check... */
|
|
||||||
if (!shared_state)
|
|
||||||
PG_RETURN_NULL();
|
|
||||||
|
|
||||||
LWLockAcquire(shared_state->lock, LW_SHARED);
|
|
||||||
strncpy(location, shared_state->location, MAXFNAMELEN);
|
|
||||||
LWLockRelease(shared_state->lock);
|
|
||||||
|
|
||||||
PG_RETURN_TEXT_P(cstring_to_text(location));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Set update last xlog location reported by this standby to shared memory */
|
|
||||||
Datum
|
|
||||||
repmgr_update_standby_location(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
text *location = PG_GETARG_TEXT_P(0);
|
|
||||||
char *locationstr;
|
|
||||||
|
|
||||||
/* Safety check... */
|
|
||||||
if (!shared_state)
|
|
||||||
PG_RETURN_BOOL(false);
|
|
||||||
|
|
||||||
locationstr = text_to_cstring(location);
|
|
||||||
|
|
||||||
PG_RETURN_BOOL(repmgr_set_standby_location(locationstr));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* update and return last updated with current timestamp */
|
|
||||||
Datum
|
|
||||||
repmgr_update_last_updated(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
TimestampTz last_updated = GetCurrentTimestamp();
|
|
||||||
|
|
||||||
/* Safety check... */
|
|
||||||
if (!shared_state)
|
|
||||||
PG_RETURN_NULL();
|
|
||||||
|
|
||||||
LWLockAcquire(shared_state->lock, LW_SHARED);
|
|
||||||
shared_state->last_updated = last_updated;
|
|
||||||
LWLockRelease(shared_state->lock);
|
|
||||||
|
|
||||||
PG_RETURN_TIMESTAMPTZ(last_updated);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* get last updated timestamp */
|
|
||||||
Datum
|
|
||||||
repmgr_get_last_updated(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
TimestampTz last_updated;
|
|
||||||
|
|
||||||
/* Safety check... */
|
|
||||||
if (!shared_state)
|
|
||||||
PG_RETURN_NULL();
|
|
||||||
|
|
||||||
LWLockAcquire(shared_state->lock, LW_EXCLUSIVE);
|
|
||||||
last_updated = shared_state->last_updated;
|
|
||||||
LWLockRelease(shared_state->lock);
|
|
||||||
|
|
||||||
PG_RETURN_TIMESTAMPTZ(last_updated);
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
/*
|
|
||||||
* repmgr_function.sql
|
|
||||||
* Copyright (c) 2ndQuadrant, 2010-2014
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
-- SET SEARCH_PATH TO 'repmgr';
|
|
||||||
|
|
||||||
CREATE FUNCTION repmgr_update_standby_location(text) RETURNS boolean
|
|
||||||
AS 'MODULE_PATHNAME', 'repmgr_update_standby_location'
|
|
||||||
LANGUAGE C STRICT;
|
|
||||||
|
|
||||||
CREATE FUNCTION repmgr_get_last_standby_location() RETURNS text
|
|
||||||
AS 'MODULE_PATHNAME', 'repmgr_get_last_standby_location'
|
|
||||||
LANGUAGE C STRICT;
|
|
||||||
|
|
||||||
CREATE FUNCTION repmgr_update_last_updated() RETURNS TIMESTAMP WITH TIME ZONE
|
|
||||||
AS 'MODULE_PATHNAME', 'repmgr_update_last_updated'
|
|
||||||
LANGUAGE C STRICT;
|
|
||||||
|
|
||||||
CREATE FUNCTION repmgr_get_last_updated() RETURNS TIMESTAMP WITH TIME ZONE
|
|
||||||
AS 'MODULE_PATHNAME', 'repmgr_get_last_updated'
|
|
||||||
LANGUAGE C STRICT;
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
/*
|
|
||||||
* uninstall_repmgr_funcs.sql
|
|
||||||
* Copyright (c) 2ndQuadrant, 2010-2014
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
DROP FUNCTION repmgr_update_standby_location(text);
|
|
||||||
DROP FUNCTION repmgr_get_last_standby_location();
|
|
||||||
|
|
||||||
DROP FUNCTION repmgr_update_last_updated();
|
|
||||||
DROP FUNCTION repmgr_get_last_updated();
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* strutil.c
|
* strutil.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2ndQuadrant, 2010-2014
|
* Copyright (C) 2ndQuadrant, 2011
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "strutil.h"
|
#include "strutil.h"
|
||||||
|
|
||||||
static int xvsnprintf(char *str, size_t size, const char *format, va_list ap) __attribute__ ((format (PG_PRINTF_ATTRIBUTE, 3, 0)));
|
static int xvsnprintf(char *str, size_t size, const char *format, va_list ap);
|
||||||
|
|
||||||
/* Add strnlen on platforms that don't have it, like OS X */
|
/* Add strnlen on platforms that don't have it, like OS X */
|
||||||
#ifndef strnlen
|
#ifndef strnlen
|
||||||
@@ -44,7 +44,7 @@ xvsnprintf(char *str, size_t size, const char *format, va_list ap)
|
|||||||
|
|
||||||
retval = vsnprintf(str, size, format, ap);
|
retval = vsnprintf(str, size, format, ap);
|
||||||
|
|
||||||
if (retval >= (int)size)
|
if (retval >= size)
|
||||||
{
|
{
|
||||||
log_err(_("Buffer of size not large enough to format entire string '%s'\n"),
|
log_err(_("Buffer of size not large enough to format entire string '%s'\n"),
|
||||||
str);
|
str);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* strutil.h
|
* strutil.h
|
||||||
* Copyright (C) 2ndQuadrant, 2010-2014
|
* Copyright (C) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
@@ -31,9 +31,9 @@
|
|||||||
#define MAXCONNINFO 1024
|
#define MAXCONNINFO 1024
|
||||||
|
|
||||||
|
|
||||||
extern int xsnprintf(char *str, size_t size, const char *format, ...) __attribute__ ((format (PG_PRINTF_ATTRIBUTE, 3, 4)));
|
extern int xsnprintf(char *str, size_t size, const char *format, ...);
|
||||||
extern int sqlquery_snprintf(char *str, const char *format, ...) __attribute__ ((format (PG_PRINTF_ATTRIBUTE, 2, 3)));
|
extern int sqlquery_snprintf(char *str, const char *format, ...);
|
||||||
extern int maxlen_snprintf(char *str, const char *format, ...) __attribute__ ((format (PG_PRINTF_ATTRIBUTE, 2, 3)));
|
extern int maxlen_snprintf(char *str, const char *format, ...);
|
||||||
|
|
||||||
/* Add strnlen on platforms that don't have it, like OS X */
|
/* Add strnlen on platforms that don't have it, like OS X */
|
||||||
#ifndef strnlen
|
#ifndef strnlen
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* uninstall_repmgr.sql
|
* uninstall_repmgr.sql
|
||||||
*
|
*
|
||||||
* Copyright (C) 2ndQuadrant, 2010-2014
|
* Copyright (C) 2ndQuadrant, 2010-2011
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user