mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-22 22:56:29 +00:00
Compare commits
50 Commits
v3.3.1
...
REL3_3_STA
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ae8f5780b | ||
|
|
dd9df04334 | ||
|
|
5411225b6f | ||
|
|
cf90bc3224 | ||
|
|
6ba9077ba5 | ||
|
|
ead4866719 | ||
|
|
a0937e959f | ||
|
|
00391ba95d | ||
|
|
01edae1b20 | ||
|
|
b92d0cc696 | ||
|
|
2264848601 | ||
|
|
657125a3fb | ||
|
|
8fefb799ee | ||
|
|
37b458dfcd | ||
|
|
72b14a7274 | ||
|
|
19684f965b | ||
|
|
9690aeb030 | ||
|
|
774a3abf24 | ||
|
|
95d6f08ff4 | ||
|
|
33af998a1e | ||
|
|
18a56b266b | ||
|
|
b7d1e7a091 | ||
|
|
c7f9fbf524 | ||
|
|
e0ea9c3be4 | ||
|
|
318f1dac40 | ||
|
|
bda4b0995c | ||
|
|
c14449f0a7 | ||
|
|
557e34b70c | ||
|
|
333083869b | ||
|
|
57fae00844 | ||
|
|
3de336f1c0 | ||
|
|
5493b57443 | ||
|
|
e53f1bf844 | ||
|
|
90638811c8 | ||
|
|
892e3b93d1 | ||
|
|
6f15a7e52e | ||
|
|
98998f73bf | ||
|
|
34ac2d8141 | ||
|
|
c820b61f28 | ||
|
|
9e620656c5 | ||
|
|
2fa277cc53 | ||
|
|
6a4f5944a1 | ||
|
|
c02a12a113 | ||
|
|
01b3933922 | ||
|
|
39b3b32814 | ||
|
|
846e0f73b2 | ||
|
|
7467525c8d | ||
|
|
b27a94ccbe | ||
|
|
2e69d155da | ||
|
|
870a367d3b |
@@ -21,9 +21,11 @@ copy of the relevant Copyright Assignment Form.
|
||||
Code style
|
||||
----------
|
||||
|
||||
Code in repmgr is formatted to a consistent style using the following command:
|
||||
Code in repmgr should be formatted to the same standards as the main PostgreSQL
|
||||
project. For more details see:
|
||||
|
||||
astyle --style=ansi --indent=tab --suffix=none *.c *.h
|
||||
https://www.postgresql.org/docs/current/static/source-format.html
|
||||
|
||||
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.
|
||||
|
||||
|
||||
16
HISTORY
16
HISTORY
@@ -1,4 +1,18 @@
|
||||
3.3.1 2017-03-
|
||||
3.3.3 2017-06
|
||||
repmgr: fix `standby register --force` when updating existing node record (Ian)
|
||||
|
||||
3.3.2 2017-06-01
|
||||
Add support for PostgreSQL 10 (Ian)
|
||||
repmgr: ensure --replication-user option is honoured when passing database
|
||||
connection parameters as a conninfo string (Ian)
|
||||
repmgr: improve detection of pg_rewind on remote server (Ian)
|
||||
repmgr: add DETAIL log output for additional clarification of error messages (Ian)
|
||||
repmgr: suppress various spurious error messages in `standby follow` and
|
||||
`standby switchover` (Ian)
|
||||
repmgr: add missing `-P` option (Ian)
|
||||
repmgrd: monitoring statistic reporting fixes (Ian)
|
||||
|
||||
3.3.1 2017-03-13
|
||||
repmgrd: prevent invalid apply lag value being written to the
|
||||
monitoring table (Ian)
|
||||
repmgrd: fix error in XLogRecPtr conversion when calculating
|
||||
|
||||
7
Makefile
7
Makefile
@@ -8,8 +8,9 @@ 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 dirmod.o compat.o
|
||||
|
||||
DATA = repmgr.sql uninstall_repmgr.sql
|
||||
REGRESS = repmgr_funcs repmgr_test
|
||||
|
||||
PG_CPPFLAGS = -I$(libpq_srcdir)
|
||||
PG_CPPFLAGS = -I$(includedir_internal) -I$(libpq_srcdir)
|
||||
PG_LIBS = $(libpq_pgport)
|
||||
|
||||
|
||||
@@ -17,11 +18,11 @@ all: repmgrd repmgr
|
||||
$(MAKE) -C sql
|
||||
|
||||
repmgrd: $(repmgrd_OBJS)
|
||||
$(CC) -o repmgrd $(CFLAGS) $(repmgrd_OBJS) $(PG_LIBS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS)
|
||||
$(CC) -o repmgrd $(CFLAGS) $(repmgrd_OBJS) $(PG_LIBS) $(LDFLAGS) $(LDFLAGS_EX)
|
||||
$(MAKE) -C sql
|
||||
|
||||
repmgr: $(repmgr_OBJS)
|
||||
$(CC) -o repmgr $(CFLAGS) $(repmgr_OBJS) $(PG_LIBS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS)
|
||||
$(CC) -o repmgr $(CFLAGS) $(repmgr_OBJS) $(PG_LIBS) $(LDFLAGS) $(LDFLAGS_EX)
|
||||
|
||||
# Make all objects depend on all include files. This is a bit of a
|
||||
# shotgun approach, but the codebase is small enough that a complete rebuild
|
||||
|
||||
43
README.md
43
README.md
@@ -189,6 +189,14 @@ system.
|
||||
Instructions can be found in the APT section of the PostgreSQL Wiki
|
||||
( https://wiki.postgresql.org/wiki/Apt ).
|
||||
|
||||
*NOTE*: repmgr 3.3 packages are now only available via a 2ndQuadrant-hosted
|
||||
repository which can be installed like this:
|
||||
|
||||
apt-key adv --fetch-keys http://packages.2ndquadrant.com/repmgr3/apt/0xD3FA41F6.asc
|
||||
|
||||
echo deb http://packages.2ndquadrant.com/repmgr3/apt/ $(lsb_release -cs)-2ndquadrant main > /etc/apt/sources.list.d/repmgr3.list
|
||||
|
||||
|
||||
See `PACKAGES.md` for details on building .deb and .rpm packages from the
|
||||
`repmgr` source code.
|
||||
|
||||
@@ -202,7 +210,7 @@ See `PACKAGES.md` for details on building .deb and .rpm packages from the
|
||||
Release tarballs are also available:
|
||||
|
||||
https://github.com/2ndQuadrant/repmgr/releases
|
||||
http://repmgr.org/downloads.php
|
||||
http://repmgr.org/
|
||||
|
||||
`repmgr` is compiled in the same way as a PostgreSQL extension using the PGXS
|
||||
infrastructure, e.g.:
|
||||
@@ -314,7 +322,13 @@ The following replication settings may need to be adjusted:
|
||||
max_wal_senders = 10
|
||||
|
||||
# Ensure WAL files contain enough information to enable read-only queries
|
||||
# on the standby
|
||||
# on the standby.
|
||||
#
|
||||
# PostgreSQL 9.5 and earlier: one of 'hot_standby' or 'logical'
|
||||
# PostgreSQL 9.6 and later: one of 'replica' or 'logical'
|
||||
# ('hot_standby' will still be accepted as an alias for 'replica')
|
||||
#
|
||||
# See: https://www.postgresql.org/docs/current/static/runtime-config-wal.html#GUC-WAL-LEVEL
|
||||
|
||||
wal_level = 'hot_standby'
|
||||
|
||||
@@ -400,7 +414,8 @@ least the following parameters:
|
||||
- `conninfo`: a valid connection string for the `repmgr` database on the
|
||||
*current* server. (On the standby, the database will not yet exist, but
|
||||
`repmgr` needs to know the connection details to complete the setup
|
||||
process).
|
||||
process). *NOTE* this must be a keyword/value string, not a connection
|
||||
URI; this limitation will be removed in a future `repmgr` version.
|
||||
|
||||
`repmgr.conf` should not be stored inside the PostgreSQL data directory,
|
||||
as it could be overwritten when setting up or reinitialising the PostgreSQL
|
||||
@@ -425,7 +440,7 @@ to include this schema name, e.g.
|
||||
### Initialise the master server
|
||||
|
||||
To enable `repmgr` to support a replication cluster, the master node must
|
||||
be registered with `repmgr`, which creates the `repmgr` database and adds
|
||||
be registered with `repmgr`, which creates the `repmgr` metadatabase and adds
|
||||
a metadata record for the server:
|
||||
|
||||
$ repmgr -f repmgr.conf master register
|
||||
@@ -504,7 +519,7 @@ place. To ensure this happens when using the default `pg_basebackup` method,
|
||||
which will ensure all WAL files generated during the cloning process are
|
||||
streamed in parallel with the main backup. Note that this requires two
|
||||
replication connections to be available (`repmgr` will verify sufficient
|
||||
connections are available before attempting to clonse).
|
||||
connections are available before attempting to clone).
|
||||
|
||||
To override this behaviour, in `repmgr.conf` set `pg_basebackup`'s
|
||||
`--xlog-method` parameter to `fetch`:
|
||||
@@ -631,7 +646,7 @@ In order to enable Barman support for `repmgr standby clone`, you must
|
||||
ensure that:
|
||||
|
||||
- the name of the server configured in Barman is equal to the
|
||||
`cluster_name` setting in `repmgr.conf`;
|
||||
`cluster` setting in `repmgr.conf`;
|
||||
- the `barman_server` setting in `repmgr.conf` is set to the SSH
|
||||
hostname of the Barman server;
|
||||
- the `restore_command` setting in `repmgr.conf` is configured to
|
||||
@@ -996,6 +1011,13 @@ both passwordless SSH access and the path of `repmgr.conf` on that server.
|
||||
> careful preparation and with adequate attention. In particular you should
|
||||
> be confident that your network environment is stable and reliable.
|
||||
>
|
||||
> Additionally you should be sure that the current master can be shut down
|
||||
> quickly and cleanly. In particular, access from applications should be
|
||||
> minimalized or preferably blocked completely. Also check that there is
|
||||
> no backlog of files waiting to be archived, as PostgreSQL will not shut
|
||||
> down until archiving completes, and that any standbys attached to the
|
||||
> current primary don't have a significant amount of replication lag.
|
||||
>
|
||||
> We recommend running `repmgr standby switchover` at the most verbose
|
||||
> logging level (`--log-level DEBUG --verbose`) and capturing all output
|
||||
> to assist troubleshooting any problems.
|
||||
@@ -1062,7 +1084,7 @@ should have been updated to reflect this:
|
||||
|
||||
### Caveats
|
||||
|
||||
- The functionality provided `repmgr standby switchover` is primarily aimed
|
||||
- The functionality provided by `repmgr standby switchover` is primarily aimed
|
||||
at a two-server master/standby replication cluster and currently does
|
||||
not support additional standbys.
|
||||
- `repmgr standby switchover` is designed to use the `pg_rewind` utility,
|
||||
@@ -1076,11 +1098,6 @@ should have been updated to reflect this:
|
||||
the `repmgrd` may try and promote a standby by itself.
|
||||
- Any other standbys attached to the old master will need to be manually
|
||||
instructed to point to the new master (e.g. with `repmgr standby follow`).
|
||||
- You must ensure that following a server start using `pg_ctl`, log output
|
||||
is not send to STDERR (the default behaviour). If logging is not configured,
|
||||
we recommend setting `logging_collector=on` in `postgresql.conf` and
|
||||
providing an explicit `-l/--log` setting in `repmgr.conf`'s `pg_ctl_options`
|
||||
parameter.
|
||||
|
||||
We hope to remove some of these restrictions in future versions of `repmgr`.
|
||||
|
||||
@@ -1610,7 +1627,7 @@ which contains connection details for the local database.
|
||||
|
||||
Creates a witness server as a separate PostgreSQL instance. This instance
|
||||
can be on a separate server or a server running an existing node. The
|
||||
witness server contain a copy of the repmgr metadata tables but will not
|
||||
witness server contains a copy of the repmgr metadata tables but will not
|
||||
be set up as a standby; instead it will update its metadata copy each
|
||||
time a failover occurs.
|
||||
|
||||
|
||||
2
config.c
2
config.c
@@ -30,7 +30,7 @@ static void tablespace_list_append(t_configuration_options *options, const char
|
||||
static void exit_with_errors(ItemList *config_errors);
|
||||
|
||||
const static char *_progname = NULL;
|
||||
static char config_file_path[MAXPGPATH];
|
||||
static char config_file_path[MAXPGPATH] = "";
|
||||
static bool config_file_provided = false;
|
||||
bool config_file_found = false;
|
||||
|
||||
|
||||
61
dbutils.c
61
dbutils.c
@@ -428,6 +428,8 @@ int
|
||||
get_server_version(PGconn *conn, char *server_version)
|
||||
{
|
||||
PGresult *res;
|
||||
int server_version_num;
|
||||
|
||||
res = PQexec(conn,
|
||||
"SELECT current_setting('server_version_num'), "
|
||||
" current_setting('server_version')");
|
||||
@@ -441,9 +443,12 @@ get_server_version(PGconn *conn, char *server_version)
|
||||
}
|
||||
|
||||
if (server_version != NULL)
|
||||
strcpy(server_version, PQgetvalue(res, 0, 0));
|
||||
strcpy(server_version, PQgetvalue(res, 0, 1));
|
||||
|
||||
return atoi(PQgetvalue(res, 0, 0));
|
||||
server_version_num = atoi(PQgetvalue(res, 0, 0));
|
||||
|
||||
PQclear(res);
|
||||
return server_version_num;
|
||||
}
|
||||
|
||||
|
||||
@@ -1132,15 +1137,25 @@ drop_replication_slot(PGconn *conn, char *slot_name)
|
||||
|
||||
|
||||
bool
|
||||
start_backup(PGconn *conn, char *first_wal_segment, bool fast_checkpoint)
|
||||
start_backup(PGconn *conn, char *first_wal_segment, bool fast_checkpoint, int server_version_num)
|
||||
{
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
PGresult *res;
|
||||
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT pg_catalog.pg_xlogfile_name(pg_catalog.pg_start_backup('repmgr_standby_clone_%ld', %s))",
|
||||
time(NULL),
|
||||
fast_checkpoint ? "TRUE" : "FALSE");
|
||||
if (server_version_num >= 100000)
|
||||
{
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT pg_catalog.pg_walfile_name(pg_catalog.pg_start_backup('repmgr_standby_clone_%ld', %s))",
|
||||
time(NULL),
|
||||
fast_checkpoint ? "TRUE" : "FALSE");
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT pg_catalog.pg_xlogfile_name(pg_catalog.pg_start_backup('repmgr_standby_clone_%ld', %s))",
|
||||
time(NULL),
|
||||
fast_checkpoint ? "TRUE" : "FALSE");
|
||||
}
|
||||
|
||||
log_verbose(LOG_DEBUG, "start_backup():\n%s\n", sqlquery);
|
||||
|
||||
@@ -1168,12 +1183,19 @@ start_backup(PGconn *conn, char *first_wal_segment, bool fast_checkpoint)
|
||||
|
||||
|
||||
bool
|
||||
stop_backup(PGconn *conn, char *last_wal_segment)
|
||||
stop_backup(PGconn *conn, char *last_wal_segment, int server_version_num)
|
||||
{
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
PGresult *res;
|
||||
|
||||
sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_xlogfile_name(pg_catalog.pg_stop_backup())");
|
||||
if (server_version_num >= 100000)
|
||||
{
|
||||
sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_walfile_name(pg_catalog.pg_stop_backup())");
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_xlogfile_name(pg_catalog.pg_stop_backup())");
|
||||
}
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
@@ -1699,6 +1721,27 @@ create_event_record(PGconn *conn, t_configuration_options *options, int node_id,
|
||||
return success;
|
||||
}
|
||||
|
||||
void
|
||||
create_checkpoint(PGconn *conn)
|
||||
{
|
||||
char sqlquery[MAXLEN];
|
||||
PGresult *res;
|
||||
|
||||
sqlquery_snprintf(sqlquery, "CHECKPOINT");
|
||||
log_verbose(LOG_DEBUG, "checkpoint:\n%s\n", sqlquery);
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
log_err(_("Unable to create CHECKPOINT:\n%s\n"),
|
||||
PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
exit(ERR_DB_QUERY);
|
||||
}
|
||||
|
||||
log_notice(_("CHECKPOINT created\n"));
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
update_node_record(PGconn *conn, char *action, int node, char *type, int upstream_node, char *cluster_name, char *node_name, char *conninfo, int priority, char *slot_name, bool active)
|
||||
|
||||
@@ -122,8 +122,8 @@ char *get_repmgr_schema_quoted(PGconn *conn);
|
||||
bool create_replication_slot(PGconn *conn, char *slot_name, int server_version_num, PQExpBufferData *error_msg);
|
||||
int get_slot_record(PGconn *conn, char *slot_name, t_replication_slot *record);
|
||||
bool drop_replication_slot(PGconn *conn, char *slot_name);
|
||||
bool start_backup(PGconn *conn, char *first_wal_segment, bool fast_checkpoint);
|
||||
bool stop_backup(PGconn *conn, char *last_wal_segment);
|
||||
bool start_backup(PGconn *conn, char *first_wal_segment, bool fast_checkpoint, int server_version_num);
|
||||
bool stop_backup(PGconn *conn, char *last_wal_segment, int server_version_num);
|
||||
bool set_config(PGconn *conn, const char *config_param, const char *config_value);
|
||||
bool set_config_bool(PGconn *conn, const char *config_param, bool state);
|
||||
bool witness_copy_node_records(PGconn *masterconn, PGconn *witnessconn, char *cluster_name);
|
||||
@@ -135,9 +135,9 @@ bool update_node_record(PGconn *conn, char *action, int node, char *type,
|
||||
bool update_node_record_status(PGconn *conn, char *cluster_name, int this_node_id, char *type, int upstream_node_id, bool active);
|
||||
bool update_node_record_set_upstream(PGconn *conn, char *cluster_name, int this_node_id, int new_upstream_node_id);
|
||||
bool create_event_record(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details);
|
||||
void create_checkpoint(PGconn *conn);
|
||||
|
||||
int get_node_replication_state(PGconn *conn, char *node_name, char *output);
|
||||
t_server_type parse_node_type(const char *type);
|
||||
int get_data_checksum_version(const char *data_directory);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ will be carried out:
|
||||
e.g. if a replication cluster is spread over multiple data centres, a split-brain
|
||||
situation does not occur if there is a network failure between datacentres. Note
|
||||
that if nodes are split evenly between data centres, a witness server can be
|
||||
used to establish the "majority" daat centre.
|
||||
used to establish the "majority" data centre.
|
||||
|
||||
* `repmgrd` polls all visible servers and waits for each node to return a valid LSN;
|
||||
it updates the LSN previously stored for this node if it has increased since
|
||||
|
||||
@@ -49,6 +49,14 @@ the `%include` directive (available from PgBouncer 1.6) to include a separate
|
||||
configuration file, `/etc/pgbouncer.database.ini`, which will be modified by
|
||||
`repmgr`.
|
||||
|
||||
* * *
|
||||
|
||||
> *NOTE*: in this self-contained demonstration, `pgbouncer` is running on the
|
||||
> database servers, however in a production environment it will make more
|
||||
> sense to run `pgbouncer` on either separate nodes or the application server.
|
||||
|
||||
* * *
|
||||
|
||||
`/etc/pgbouncer.ini` should look something like this:
|
||||
|
||||
[pgbouncer]
|
||||
@@ -125,7 +133,7 @@ The actual script is as follows; adjust the configurable items as appropriate:
|
||||
|
||||
psql -d $REPMGR_DB -U $REPMGR_USER -t -A \
|
||||
-c "SELECT '${PGBOUNCER_DATABASE}-ro= ' || conninfo || ' application_name=pgbouncer_${HOST}' \
|
||||
FROM $REPMGR_SCHEMA.repl_nodes \
|
||||
FROM ${REPMGR_SCHEMA}.repl_nodes \
|
||||
WHERE node_name='${HOST}'" >> $PGBOUNCER_DATABASE_INI_NEW
|
||||
|
||||
rsync $PGBOUNCER_DATABASE_INI_NEW $HOST:$PGBOUNCER_DATABASE_INI
|
||||
|
||||
18
expected/repmgr_funcs.out
Normal file
18
expected/repmgr_funcs.out
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* repmgr_function.sql
|
||||
* Copyright (c) 2ndQuadrant, 2010-2017
|
||||
*
|
||||
*/
|
||||
-- SET SEARCH_PATH TO 'repmgr';
|
||||
CREATE FUNCTION repmgr_update_standby_location(text) RETURNS boolean
|
||||
AS '$libdir/repmgr_funcs', 'repmgr_update_standby_location'
|
||||
LANGUAGE C STRICT;
|
||||
CREATE FUNCTION repmgr_get_last_standby_location() RETURNS text
|
||||
AS '$libdir/repmgr_funcs', 'repmgr_get_last_standby_location'
|
||||
LANGUAGE C STRICT;
|
||||
CREATE FUNCTION repmgr_update_last_updated() RETURNS TIMESTAMP WITH TIME ZONE
|
||||
AS '$libdir/repmgr_funcs', 'repmgr_update_last_updated'
|
||||
LANGUAGE C STRICT;
|
||||
CREATE FUNCTION repmgr_get_last_updated() RETURNS TIMESTAMP WITH TIME ZONE
|
||||
AS '$libdir/repmgr_funcs', 'repmgr_get_last_updated'
|
||||
LANGUAGE C STRICT;
|
||||
24
expected/repmgr_test.out
Normal file
24
expected/repmgr_test.out
Normal file
@@ -0,0 +1,24 @@
|
||||
select * from repmgr_update_standby_location('');
|
||||
repmgr_update_standby_location
|
||||
--------------------------------
|
||||
f
|
||||
(1 row)
|
||||
|
||||
select * from repmgr_get_last_standby_location();
|
||||
repmgr_get_last_standby_location
|
||||
----------------------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
select * from repmgr_update_last_updated();
|
||||
repmgr_update_last_updated
|
||||
----------------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
select * from repmgr_get_last_updated();
|
||||
repmgr_get_last_updated
|
||||
-------------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
16
log.c
16
log.c
@@ -71,7 +71,7 @@ _stderr_log_with_level(const char *level_name, int level, const char *fmt, va_li
|
||||
|
||||
/*
|
||||
* Store the requested level so that if there's a subsequent
|
||||
* log_hint(), we can suppress that if appropriate.
|
||||
* log_hint() or log_detail(), we can suppress that if appropriate.
|
||||
*/
|
||||
last_log_level = level;
|
||||
|
||||
@@ -113,6 +113,20 @@ log_hint(const char *fmt, ...)
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
log_detail(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
if (terse_logging == false)
|
||||
{
|
||||
va_start(ap, fmt);
|
||||
_stderr_log_with_level("DETAIL", last_log_level, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
log_verbose(int level, const char *fmt, ...)
|
||||
{
|
||||
|
||||
2
log.h
2
log.h
@@ -126,6 +126,8 @@ bool logger_shutdown(void);
|
||||
void logger_set_verbose(void);
|
||||
void logger_set_terse(void);
|
||||
|
||||
void log_detail(const char *fmt, ...)
|
||||
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
|
||||
void log_hint(const char *fmt, ...)
|
||||
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
|
||||
void log_verbose(int level, const char *fmt, ...)
|
||||
|
||||
310
repmgr.c
310
repmgr.c
@@ -96,7 +96,7 @@
|
||||
static int test_ssh_connection(char *host, char *remote_user);
|
||||
static int copy_remote_files(char *host, char *remote_user, char *remote_path,
|
||||
char *local_path, bool is_directory, int server_version_num);
|
||||
static int run_basebackup(const char *data_dir, int server_version);
|
||||
static int run_basebackup(const char *data_dir, int server_version_num);
|
||||
static void check_parameters_for_action(const int action);
|
||||
static bool create_schema(PGconn *conn);
|
||||
static bool create_recovery_file(const char *data_dir, t_conninfo_param_list *recovery_conninfo);
|
||||
@@ -158,7 +158,8 @@ static void param_set(t_conninfo_param_list *param_list, const char *param, cons
|
||||
static char *param_get(t_conninfo_param_list *param_list, const char *param);
|
||||
static bool parse_conninfo_string(const char *conninfo_str, t_conninfo_param_list *param_list, char *errmsg, bool ignore_application_name);
|
||||
static void conn_to_param_list(PGconn *conn, t_conninfo_param_list *param_list);
|
||||
static void parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_options *backup_options);
|
||||
static char *param_list_to_string(t_conninfo_param_list *param_list);
|
||||
static bool parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_options *backup_options, int server_version_num, ItemList *error_list);
|
||||
|
||||
static void config_file_list_init(t_configfile_list *list, int max_size);
|
||||
static void config_file_list_add(t_configfile_list *list, const char *file, const char *filename, bool in_data_dir);
|
||||
@@ -220,14 +221,14 @@ main(int argc, char **argv)
|
||||
{"rsync-only", no_argument, NULL, 'r'},
|
||||
{"fast-checkpoint", no_argument, NULL, 'c'},
|
||||
{"log-level", required_argument, NULL, 'L'},
|
||||
{"terse", required_argument, NULL, 't'},
|
||||
{"terse", no_argument, NULL, 't'},
|
||||
{"mode", required_argument, NULL, 'm'},
|
||||
{"pwprompt", no_argument, NULL, 'P'},
|
||||
{"remote-config-file", required_argument, NULL, 'C'},
|
||||
{"help", no_argument, NULL, OPT_HELP},
|
||||
{"check-upstream-config", no_argument, NULL, OPT_CHECK_UPSTREAM_CONFIG},
|
||||
{"recovery-min-apply-delay", required_argument, NULL, OPT_RECOVERY_MIN_APPLY_DELAY},
|
||||
{"pg_rewind", optional_argument, NULL, OPT_PG_REWIND},
|
||||
{"pwprompt", optional_argument, NULL, OPT_PWPROMPT},
|
||||
{"csv", no_argument, NULL, OPT_CSV},
|
||||
{"node", required_argument, NULL, OPT_NODE},
|
||||
{"without-barman", no_argument, NULL, OPT_WITHOUT_BARMAN},
|
||||
@@ -282,7 +283,11 @@ main(int argc, char **argv)
|
||||
|
||||
/*
|
||||
* Pre-set any defaults , which can be overwritten if matching
|
||||
* command line parameters are provided
|
||||
* command line parameters are provided.
|
||||
*
|
||||
* Note: PQconndefaults() does not provide a default value for
|
||||
* "dbname", but if none is provided will default to "username"
|
||||
* when the connection is made.
|
||||
*/
|
||||
|
||||
for (c = 0; c < source_conninfo.size && source_conninfo.keywords[c]; c++)
|
||||
@@ -315,7 +320,6 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
/* set default user for -R/--remote-user */
|
||||
|
||||
{
|
||||
struct passwd *pw = NULL;
|
||||
|
||||
@@ -329,16 +333,7 @@ main(int argc, char **argv)
|
||||
strncpy(runtime_options.username, pw->pw_name, MAXLEN);
|
||||
}
|
||||
|
||||
/*
|
||||
* Though libpq will default to the username as dbname, PQconndefaults()
|
||||
* doesn't return this
|
||||
*/
|
||||
if (runtime_options.dbname[0] == '\0')
|
||||
{
|
||||
strncpy(runtime_options.dbname, runtime_options.username, MAXLEN);
|
||||
}
|
||||
|
||||
while ((c = getopt_long(argc, argv, "?Vd:h:p:U:S:D:f:R:w:k:FWIvb:rcL:tm:C:l:", long_options,
|
||||
while ((c = getopt_long(argc, argv, "?Vd:h:p:U:S:D:f:R:w:k:FWIvb:rcL:tm:C:l:P", long_options,
|
||||
&optindex)) != -1)
|
||||
{
|
||||
/*
|
||||
@@ -464,6 +459,9 @@ main(int argc, char **argv)
|
||||
case 'C':
|
||||
strncpy(runtime_options.remote_config_file, optarg, MAXLEN);
|
||||
break;
|
||||
case 'P':
|
||||
runtime_options.witness_pwprompt = true;
|
||||
break;
|
||||
case OPT_CHECK_UPSTREAM_CONFIG:
|
||||
check_upstream_config = true;
|
||||
break;
|
||||
@@ -514,9 +512,6 @@ main(int argc, char **argv)
|
||||
}
|
||||
runtime_options.pg_rewind_supplied = true;
|
||||
break;
|
||||
case OPT_PWPROMPT:
|
||||
runtime_options.witness_pwprompt = true;
|
||||
break;
|
||||
case OPT_CSV:
|
||||
runtime_options.csv_mode = true;
|
||||
break;
|
||||
@@ -877,7 +872,6 @@ main(int argc, char **argv)
|
||||
if (runtime_options.terse)
|
||||
logger_set_terse();
|
||||
|
||||
|
||||
/*
|
||||
* Node configuration information is not needed for all actions, with
|
||||
* STANDBY CLONE being the main exception.
|
||||
@@ -1307,7 +1301,7 @@ build_cluster_matrix(t_node_matrix_rec ***matrix_rec_dest, int *name_length)
|
||||
*/
|
||||
appendPQExpBuffer(&command,
|
||||
"\"%s -d '%s' --cluster '%s' ",
|
||||
make_pg_path("repmgr"),
|
||||
make_pg_path((char *)progname()),
|
||||
PQgetvalue(res, i, 0),
|
||||
PQgetvalue(res, i, 5));
|
||||
|
||||
@@ -1572,7 +1566,7 @@ build_cluster_crosscheck(t_node_status_cube ***dest_cube, int *name_length)
|
||||
|
||||
appendPQExpBuffer(&command,
|
||||
"%s -d '%s' --cluster '%s' --node=%i ",
|
||||
make_pg_path("repmgr"),
|
||||
make_pg_path((char *)progname()),
|
||||
PQgetvalue(res, i, 0),
|
||||
options.cluster_name,
|
||||
remote_node_id);
|
||||
@@ -1899,7 +1893,7 @@ do_master_register(void)
|
||||
|
||||
if (!schema_exists)
|
||||
{
|
||||
log_info(_("master register: creating database objects inside the %s schema\n"),
|
||||
log_info(_("master register: creating database objects inside the '%s' schema\n"),
|
||||
get_repmgr_schema());
|
||||
|
||||
begin_transaction(conn);
|
||||
@@ -1946,8 +1940,6 @@ do_master_register(void)
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
|
||||
begin_transaction(conn);
|
||||
|
||||
/*
|
||||
* Check whether there's an existing record for this node, and
|
||||
* update it if --force set
|
||||
@@ -2011,7 +2003,7 @@ do_master_register(void)
|
||||
|
||||
PQfinish(conn);
|
||||
|
||||
log_notice(_("master node correctly registered for cluster %s with id %d (conninfo: %s)\n"),
|
||||
log_notice(_("master node correctly registered for cluster '%s' with id %d (conninfo: %s)\n"),
|
||||
options.cluster_name, options.node, options.conninfo);
|
||||
return;
|
||||
}
|
||||
@@ -2121,7 +2113,7 @@ do_standby_register(void)
|
||||
|
||||
if (node_result)
|
||||
{
|
||||
if (node_record.active == true)
|
||||
if (node_record.active == true && node_record.node_id != options.node)
|
||||
{
|
||||
log_err(_("Node %i exists already with node_name \"%s\"\n"),
|
||||
node_record.node_id,
|
||||
@@ -2152,7 +2144,7 @@ do_standby_register(void)
|
||||
log_err(_("no record found for upstream node %i\n"),
|
||||
options.upstream_node);
|
||||
/* footgun alert - only do this if you know what you're doing */
|
||||
log_hint(_("use option -F/--force to create a dummy upstream record"));
|
||||
log_hint(_("use option -F/--force to create a dummy upstream record\n"));
|
||||
PQfinish(master_conn);
|
||||
if (PQstatus(conn) == CONNECTION_OK)
|
||||
PQfinish(conn);
|
||||
@@ -2640,6 +2632,7 @@ get_tablespace_data_barman
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
get_barman_property(char *dst, char *name, char *local_repmgr_directory)
|
||||
{
|
||||
@@ -3645,7 +3638,18 @@ do_standby_clone(void)
|
||||
initPQExpBuffer(&tablespace_map);
|
||||
}
|
||||
|
||||
if (start_backup(source_conn, first_wal_segment, runtime_options.fast_checkpoint) == false)
|
||||
/*
|
||||
* From 9.1 default is to wait for a sync standby to ack, avoid that by
|
||||
* turning off sync rep for this session
|
||||
*/
|
||||
if (set_config_bool(source_conn, "synchronous_commit", false) == false)
|
||||
{
|
||||
r = ERR_BAD_CONFIG;
|
||||
retval = ERR_BAD_CONFIG;
|
||||
goto stop_backup;
|
||||
}
|
||||
|
||||
if (start_backup(source_conn, first_wal_segment, runtime_options.fast_checkpoint, server_version_num) == false)
|
||||
{
|
||||
r = ERR_BAD_BASEBACKUP;
|
||||
retval = ERR_BAD_BASEBACKUP;
|
||||
@@ -3991,7 +3995,7 @@ stop_backup:
|
||||
if (mode == rsync && pg_start_backup_executed)
|
||||
{
|
||||
log_notice(_("notifying master about backup completion...\n"));
|
||||
if (stop_backup(source_conn, last_wal_segment) == false)
|
||||
if (stop_backup(source_conn, last_wal_segment, server_version_num) == false)
|
||||
{
|
||||
r = ERR_BAD_BASEBACKUP;
|
||||
retval = ERR_BAD_BASEBACKUP;
|
||||
@@ -4083,7 +4087,16 @@ stop_backup:
|
||||
|
||||
/* Finally, write the recovery.conf file */
|
||||
|
||||
create_recovery_file(local_data_directory, &recovery_conninfo);
|
||||
if (create_recovery_file( local_data_directory, &recovery_conninfo) == false)
|
||||
{
|
||||
/* create_recovery_file() will log an error */
|
||||
log_notice(_("unable to create recovery.conf; see preceding error messages\n"));
|
||||
log_hint(_("data directory (\"%s\") may need to be cleaned up manually\n"),
|
||||
local_data_directory);
|
||||
|
||||
PQfinish(source_conn);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
|
||||
if (mode == barman)
|
||||
{
|
||||
@@ -4502,6 +4515,12 @@ do_standby_promote(void)
|
||||
|
||||
log_notice(_("STANDBY PROMOTE successful\n"));
|
||||
|
||||
/*
|
||||
* Force a checkpoint so that pg_rewind on former master can tell that the
|
||||
* servers have diverged.
|
||||
*/
|
||||
create_checkpoint(conn);
|
||||
|
||||
/* Log the event */
|
||||
create_event_record(conn,
|
||||
&options,
|
||||
@@ -4764,7 +4783,7 @@ do_standby_follow(void)
|
||||
* (a former standby) exists on the former upstream, drop it.
|
||||
*/
|
||||
|
||||
if (options.use_replication_slots && original_upstream_node_id != UNKNOWN_NODE_ID)
|
||||
if (options.use_replication_slots && runtime_options.host_param_provided == false && original_upstream_node_id != UNKNOWN_NODE_ID)
|
||||
{
|
||||
t_node_info upstream_node_record = T_NODE_INFO_INITIALIZER;
|
||||
int upstream_query_result;
|
||||
@@ -4969,7 +4988,9 @@ do_standby_switchover(void)
|
||||
|
||||
if (r != 0)
|
||||
{
|
||||
log_err(_("unable to connect via SSH to host %s, user %s\n"), remote_host, runtime_options.remote_user);
|
||||
log_err(_("unable to connect via SSH to host \"%s\", user \"%s\"\n"),
|
||||
remote_host, runtime_options.remote_user);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
|
||||
if (get_pg_setting(remote_conn, "data_directory", remote_data_directory) == false)
|
||||
@@ -5068,7 +5089,11 @@ do_standby_switchover(void)
|
||||
|
||||
initPQExpBuffer(&remote_command_str);
|
||||
|
||||
appendPQExpBuffer(&remote_command_str, "ls ");
|
||||
if (strcmp(remote_pg_rewind, "pg_rewind") == 0)
|
||||
appendPQExpBuffer(&remote_command_str, "which ");
|
||||
else
|
||||
appendPQExpBuffer(&remote_command_str, "ls ");
|
||||
|
||||
appendShellString(&remote_command_str, remote_pg_rewind);
|
||||
appendPQExpBuffer(&remote_command_str, " >/dev/null 2>&1 && echo 1 || echo 0");
|
||||
|
||||
@@ -5085,7 +5110,11 @@ do_standby_switchover(void)
|
||||
if (*command_output.data == '0')
|
||||
{
|
||||
log_err(_("unable to find pg_rewind on the remote server\n"));
|
||||
log_err(_("expected location is: %s\n"), remote_pg_rewind);
|
||||
if (strcmp(remote_pg_rewind, "pg_rewind") == 0)
|
||||
log_hint(_("set pg_bindir in repmgr.conf or provide with -b/--pg_bindir\n"));
|
||||
else
|
||||
log_detail(_("expected location is: %s\n"), remote_pg_rewind);
|
||||
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
|
||||
@@ -5264,8 +5293,8 @@ do_standby_switchover(void)
|
||||
initPQExpBuffer(&remote_command_str);
|
||||
appendPQExpBuffer(&remote_command_str,
|
||||
"%s standby archive-config -f ",
|
||||
make_pg_path("repmgr"));
|
||||
appendShellString(&remote_command_str, runtime_options.remote_config_file);
|
||||
make_pg_path((char *)progname()));
|
||||
appendShellString(&remote_command_str, runtime_options.remote_config_file);
|
||||
appendPQExpBuffer(&remote_command_str,
|
||||
" --config-archive-dir=");
|
||||
appendShellString(&remote_command_str, remote_archive_config_dir);
|
||||
@@ -5439,12 +5468,22 @@ do_standby_switchover(void)
|
||||
/* Restore any previously archived config files */
|
||||
initPQExpBuffer(&remote_command_str);
|
||||
|
||||
/* --force */
|
||||
appendPQExpBuffer(&remote_command_str,
|
||||
"%s standby restore-config -D ",
|
||||
make_pg_path("repmgr"));
|
||||
make_pg_path((char *)progname()));
|
||||
appendShellString(&remote_command_str, remote_data_directory);
|
||||
|
||||
/*
|
||||
* append pass the configuration file to prevent spurious errors
|
||||
* about missing cluster_name
|
||||
*/
|
||||
appendPQExpBuffer(&remote_command_str,
|
||||
" --config-archive-dir=");
|
||||
" -f ");
|
||||
appendShellString(&remote_command_str, runtime_options.remote_config_file);
|
||||
|
||||
appendPQExpBuffer(&remote_command_str,
|
||||
" --config-archive-dir=");
|
||||
appendShellString(&remote_command_str, remote_archive_config_dir);
|
||||
|
||||
initPQExpBuffer(&command_output);
|
||||
@@ -5507,14 +5546,20 @@ do_standby_switchover(void)
|
||||
|
||||
appendPQExpBuffer(&remote_command_str,
|
||||
"%s -D ",
|
||||
make_pg_path("repmgr"));
|
||||
make_pg_path((char *)progname()));
|
||||
appendShellString(&remote_command_str, remote_data_directory);
|
||||
appendPQExpBuffer(&remote_command_str, " -f ");
|
||||
appendShellString(&remote_command_str, runtime_options.remote_config_file);
|
||||
appendPQExpBuffer(&remote_command_str,
|
||||
" %s --rsync-only --force --ignore-external-config-files standby clone",
|
||||
" %s --rsync-only --force standby clone",
|
||||
repmgr_db_cli_params);
|
||||
|
||||
if (runtime_options.copy_external_config_files == true)
|
||||
{
|
||||
appendPQExpBuffer(&remote_command_str,
|
||||
" --copy-external-config-files");
|
||||
}
|
||||
|
||||
log_debug("Executing:\n%s\n", remote_command_str.data);
|
||||
|
||||
initPQExpBuffer(&command_output);
|
||||
@@ -5538,7 +5583,7 @@ do_standby_switchover(void)
|
||||
initPQExpBuffer(&remote_command_str);
|
||||
appendPQExpBuffer(&remote_command_str,
|
||||
"%s -D ",
|
||||
make_pg_path("repmgr"));
|
||||
make_pg_path((char *)progname()));
|
||||
appendShellString(&remote_command_str, remote_data_directory);
|
||||
appendPQExpBuffer(&remote_command_str, " -f ");
|
||||
appendShellString(&remote_command_str, runtime_options.remote_config_file);
|
||||
@@ -5837,7 +5882,8 @@ do_standby_restore_config(void)
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
|
||||
while ((arcdir_ent = readdir(arcdir)) != NULL) {
|
||||
while ((arcdir_ent = readdir(arcdir)) != NULL)
|
||||
{
|
||||
struct stat statbuf;
|
||||
char arcdir_ent_path[MAXPGPATH];
|
||||
PQExpBufferData src_file;
|
||||
@@ -5898,11 +5944,13 @@ do_standby_restore_config(void)
|
||||
|
||||
if (rmdir(runtime_options.config_archive_dir) != 0 && errno != EEXIST)
|
||||
{
|
||||
log_err(_("Unable to delete %s\n"), runtime_options.config_archive_dir);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
log_warning(_("unable to delete %s\n"), runtime_options.config_archive_dir);
|
||||
log_detail(_("directory may need to be manually removed\n"));
|
||||
}
|
||||
else
|
||||
{
|
||||
log_verbose(LOG_NOTICE, "directory %s deleted\n", runtime_options.config_archive_dir);
|
||||
}
|
||||
|
||||
log_verbose(LOG_NOTICE, "Directory %s deleted\n", runtime_options.config_archive_dir);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -5923,9 +5971,9 @@ do_witness_create(void)
|
||||
char master_hba_file[MAXLEN];
|
||||
bool success;
|
||||
|
||||
char witness_port[MAXLEN];
|
||||
char repmgr_user[MAXLEN];
|
||||
char repmgr_db[MAXLEN];
|
||||
char witness_port[MAXLEN] = "";
|
||||
char repmgr_user[MAXLEN] = "";
|
||||
char repmgr_db[MAXLEN] = "";
|
||||
|
||||
/*
|
||||
* Extract the repmgr user and database names from the conninfo string
|
||||
@@ -6988,7 +7036,7 @@ copy_remote_files(char *host, char *remote_user, char *remote_path,
|
||||
|
||||
|
||||
static int
|
||||
run_basebackup(const char *data_dir, int server_version)
|
||||
run_basebackup(const char *data_dir, int server_version_num)
|
||||
{
|
||||
char script[MAXLEN];
|
||||
int r = 0;
|
||||
@@ -7000,7 +7048,7 @@ run_basebackup(const char *data_dir, int server_version)
|
||||
* Parse the pg_basebackup_options provided in repmgr.conf - we'll want
|
||||
* to check later whether certain options were set by the user
|
||||
*/
|
||||
parse_pg_basebackup_options(options.pg_basebackup_options, &backup_options);
|
||||
parse_pg_basebackup_options(options.pg_basebackup_options, &backup_options, server_version_num, NULL);
|
||||
|
||||
/* Create pg_basebackup command line options */
|
||||
|
||||
@@ -7016,7 +7064,22 @@ run_basebackup(const char *data_dir, int server_version)
|
||||
*/
|
||||
if (runtime_options.conninfo_provided == true)
|
||||
{
|
||||
appendPQExpBuffer(¶ms, " -d '%s'", runtime_options.dbname);
|
||||
t_conninfo_param_list conninfo;
|
||||
char *conninfo_str;
|
||||
|
||||
initialize_conninfo_params(&conninfo, false);
|
||||
|
||||
/* string will already have been parsed */
|
||||
(void) parse_conninfo_string(runtime_options.dbname, &conninfo, NULL, false);
|
||||
|
||||
if (*runtime_options.replication_user)
|
||||
param_set(&conninfo, "user", runtime_options.replication_user);
|
||||
|
||||
conninfo_str = param_list_to_string(&conninfo);
|
||||
|
||||
appendPQExpBuffer(¶ms, " -d '%s'", conninfo_str);
|
||||
|
||||
pfree(conninfo_str);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -7064,6 +7127,7 @@ run_basebackup(const char *data_dir, int server_version)
|
||||
* From 9.6, if replication slots are in use, we'll have previously
|
||||
* created a slot with reserved LSN, and will stream from that slot to avoid
|
||||
* WAL buildup on the master using the -S/--slot, which requires -X/--xlog-method=stream
|
||||
* (from 10, -X/--wal-method=stream)
|
||||
*/
|
||||
if (!strlen(backup_options.xlog_method))
|
||||
{
|
||||
@@ -7081,17 +7145,17 @@ run_basebackup(const char *data_dir, int server_version)
|
||||
*
|
||||
* NOTE:
|
||||
* It's possible to set 'pg_basebackup_options' with an invalid combination
|
||||
* of values for --xlog-method and --slot - we're not checking that, just that
|
||||
* of values for --wal-method (--xlog-method) and --slot - we're not checking that, just that
|
||||
* we're not overriding any user-supplied values
|
||||
*/
|
||||
if (server_version >= 90600 && options.use_replication_slots)
|
||||
if (server_version_num >= 90600 && options.use_replication_slots)
|
||||
{
|
||||
bool slot_add = true;
|
||||
|
||||
/*
|
||||
* Check whether 'pg_basebackup_options' in repmgr.conf has the --slot option set,
|
||||
* or if --xlog-method is set to a value other than "stream" (in which case we can't
|
||||
* use --slot).
|
||||
* or if --wal-method (--xlog-method) is set to a value other than "stream"
|
||||
* (in which case we can't use --slot).
|
||||
*/
|
||||
if (strlen(backup_options.slot) || (strlen(backup_options.xlog_method) && strcmp(backup_options.xlog_method, "stream") != 0)) {
|
||||
slot_add = false;
|
||||
@@ -7326,7 +7390,8 @@ check_parameters_for_action(const int action)
|
||||
item_list_append(&cli_warnings, _("-c/--fast-checkpoint can only be used when executing STANDBY CLONE"));
|
||||
}
|
||||
|
||||
if (runtime_options.copy_external_config_files)
|
||||
/* can be used for "standby switchover" too */
|
||||
if (action != STANDBY_SWITCHOVER && runtime_options.copy_external_config_files)
|
||||
{
|
||||
item_list_append(&cli_warnings, _("--copy-external-config-files can only be used when executing STANDBY CLONE"));
|
||||
}
|
||||
@@ -7719,7 +7784,7 @@ create_schema(PGconn *conn)
|
||||
* to perform additional cleanup
|
||||
*
|
||||
* char *server_version_string
|
||||
* passed to get_server_version(), which will place the human-readble
|
||||
* passed to get_server_version(), which will place the human-readable
|
||||
* server version string there (e.g. "9.4.0")
|
||||
*/
|
||||
static int
|
||||
@@ -7807,6 +7872,8 @@ check_upstream_config(PGconn *conn, int server_version_num, bool exit_on_error)
|
||||
bool config_ok = true;
|
||||
char *wal_error_message = NULL;
|
||||
t_basebackup_options backup_options = T_BASEBACKUP_OPTIONS_INITIALIZER;
|
||||
bool backup_options_ok = true;
|
||||
ItemList backup_option_errors = { NULL, NULL };
|
||||
bool xlog_stream = true;
|
||||
|
||||
enum {
|
||||
@@ -7832,7 +7899,24 @@ check_upstream_config(PGconn *conn, int server_version_num, bool exit_on_error)
|
||||
* this will influence some checks
|
||||
*/
|
||||
|
||||
parse_pg_basebackup_options(options.pg_basebackup_options, &backup_options);
|
||||
backup_options_ok = parse_pg_basebackup_options(
|
||||
options.pg_basebackup_options,
|
||||
&backup_options, server_version_num,
|
||||
&backup_option_errors);
|
||||
|
||||
if (backup_options_ok == false)
|
||||
{
|
||||
if (exit_on_error == true)
|
||||
{
|
||||
log_err(_("error(s) encountered parsing 'pg_basebackup_options'\n"));
|
||||
print_error_list(&backup_option_errors, LOG_ERR);
|
||||
log_hint(_("'pg_basebackup_options' is: '%s'\n"), options.pg_basebackup_options);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
|
||||
config_ok = false;
|
||||
}
|
||||
|
||||
if (strlen(backup_options.xlog_method) && strcmp(backup_options.xlog_method, "stream") != 0)
|
||||
xlog_stream = false;
|
||||
|
||||
@@ -8354,17 +8438,12 @@ remote_command(const char *host, const char *user, const char *command, PQExpBuf
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* When executed remotely, repmgr commands which execute pg_ctl (particularly
|
||||
* `repmgr standby follow`) will see the pg_ctl command appear to fail with a
|
||||
* non-zero return code when the output from the executed pg_ctl command
|
||||
* has nowhere to go, even though the command actually succeeds. We'll consume an
|
||||
* arbitrary amount of output and throw it away to work around this.
|
||||
*/
|
||||
int i = 0;
|
||||
while (fgets(output, MAXLEN, fp) != NULL && i < 10)
|
||||
while (fgets(output, MAXLEN, fp) != NULL)
|
||||
{
|
||||
i++;
|
||||
if (!feof(fp))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8682,8 +8761,43 @@ conn_to_param_list(PGconn *conn, t_conninfo_param_list *param_list)
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_options *backup_options)
|
||||
static char *
|
||||
param_list_to_string(t_conninfo_param_list *param_list)
|
||||
{
|
||||
int c;
|
||||
PQExpBufferData conninfo_buf;
|
||||
char *conninfo_str;
|
||||
int len;
|
||||
|
||||
initPQExpBuffer(&conninfo_buf);
|
||||
|
||||
for (c = 0; c < param_list->size && param_list->keywords[c] != NULL; c++)
|
||||
{
|
||||
if (param_list->values[c] != NULL && param_list->values[c][0] != '\0')
|
||||
{
|
||||
if (c > 0)
|
||||
appendPQExpBufferChar(&conninfo_buf, ' ');
|
||||
|
||||
appendPQExpBuffer(&conninfo_buf,
|
||||
"%s=%s",
|
||||
param_list->keywords[c],
|
||||
param_list->values[c]);
|
||||
}
|
||||
}
|
||||
|
||||
len = strlen(conninfo_buf.data) + 1;
|
||||
conninfo_str = pg_malloc0(len);
|
||||
|
||||
strncpy(conninfo_str, conninfo_buf.data, len);
|
||||
|
||||
termPQExpBuffer(&conninfo_buf);
|
||||
|
||||
return conninfo_str;
|
||||
}
|
||||
|
||||
|
||||
static bool
|
||||
parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_options *backup_options, int server_version_num, ItemList *error_list)
|
||||
{
|
||||
int options_len = strlen(pg_basebackup_options) + 1;
|
||||
char *options_string = pg_malloc(options_len);
|
||||
@@ -8703,17 +8817,38 @@ parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_opti
|
||||
|
||||
int optindex = 0;
|
||||
|
||||
struct option *long_options;
|
||||
|
||||
bool backup_options_ok = true;
|
||||
|
||||
/* We're only interested in these options */
|
||||
static struct option long_options[] =
|
||||
static struct option long_options_9[] =
|
||||
{
|
||||
{"slot", required_argument, NULL, 'S'},
|
||||
{"xlog-method", required_argument, NULL, 'X'},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
/*
|
||||
* From PostgreSQL 10, --xlog-method is renamed --wal-method
|
||||
* and there's also --no-slot, which we'll want to consider.
|
||||
*/
|
||||
static struct option long_options_10[] =
|
||||
{
|
||||
{"slot", required_argument, NULL, 'S'},
|
||||
{"wal-method", required_argument, NULL, 'X'},
|
||||
{"no-slot", no_argument, NULL, 1},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
/* Don't attempt to tokenise an empty string */
|
||||
if (!strlen(pg_basebackup_options))
|
||||
return;
|
||||
return backup_options_ok;
|
||||
|
||||
if (server_version_num >= 100000)
|
||||
long_options = long_options_10;
|
||||
else
|
||||
long_options = long_options_9;
|
||||
|
||||
/* Copy the string before operating on it with strtok() */
|
||||
strncpy(options_string, pg_basebackup_options, options_len);
|
||||
@@ -8760,6 +8895,9 @@ parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_opti
|
||||
/* Reset getopt's optind variable */
|
||||
optind = 0;
|
||||
|
||||
/* Prevent getopt from emitting errors */
|
||||
opterr = 0;
|
||||
|
||||
while ((c = getopt_long(argc_item, argv_array, "S:X:", long_options,
|
||||
&optindex)) != -1)
|
||||
{
|
||||
@@ -8771,10 +8909,32 @@ parse_pg_basebackup_options(const char *pg_basebackup_options, t_basebackup_opti
|
||||
case 'X':
|
||||
strncpy(backup_options->xlog_method, optarg, MAXLEN);
|
||||
break;
|
||||
case 1:
|
||||
backup_options->no_slot = true;
|
||||
break;
|
||||
case '?':
|
||||
if (server_version_num >= 100000 && optopt == 1)
|
||||
{
|
||||
if (error_list != NULL)
|
||||
{
|
||||
item_list_append(error_list, "invalid use of --no-slot");
|
||||
}
|
||||
backup_options_ok = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
if (backup_options->no_slot == true && backup_options->slot[0] != '\0')
|
||||
{
|
||||
if (error_list != NULL)
|
||||
{
|
||||
item_list_append(error_list, "--no-slot cannot be used with -S/--slot");
|
||||
}
|
||||
backup_options_ok = false;
|
||||
}
|
||||
|
||||
return backup_options_ok;
|
||||
}
|
||||
|
||||
static void
|
||||
|
||||
@@ -26,11 +26,14 @@
|
||||
# the server's hostname or another identifier unambiguously
|
||||
# associated with the server to avoid confusion
|
||||
|
||||
# Database connection information as a conninfo string
|
||||
# This must be accessible to all servers in the cluster; for details see:
|
||||
# Database connection information as a conninfo string (this must be a
|
||||
# keyword/value string, not a connection URI).
|
||||
#
|
||||
# https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
|
||||
#
|
||||
# All servers in the cluster must be able to access the database
|
||||
# using this connection string.
|
||||
#
|
||||
#conninfo='host=192.168.204.104 dbname=repmgr user=repmgr'
|
||||
#
|
||||
# If repmgrd is in use, consider explicitly setting `connect_timeout` in the
|
||||
|
||||
6
repmgr.h
6
repmgr.h
@@ -55,7 +55,6 @@
|
||||
#define OPT_COPY_EXTERNAL_CONFIG_FILES 4
|
||||
#define OPT_CONFIG_ARCHIVE_DIR 5
|
||||
#define OPT_PG_REWIND 6
|
||||
#define OPT_PWPROMPT 7
|
||||
#define OPT_CSV 8
|
||||
#define OPT_NODE 9
|
||||
#define OPT_WITHOUT_BARMAN 10
|
||||
@@ -118,7 +117,7 @@ typedef struct
|
||||
|
||||
char recovery_min_apply_delay[MAXLEN];
|
||||
|
||||
/* standby register paarameters */
|
||||
/* standby register parameters */
|
||||
bool wait_register_sync;
|
||||
int wait_register_sync_seconds;
|
||||
|
||||
@@ -193,9 +192,10 @@ typedef struct
|
||||
{
|
||||
char slot[MAXLEN];
|
||||
char xlog_method[MAXLEN];
|
||||
bool no_slot; /* from PostgreSQL 10 */
|
||||
} t_basebackup_options;
|
||||
|
||||
#define T_BASEBACKUP_OPTIONS_INITIALIZER { "", "" }
|
||||
#define T_BASEBACKUP_OPTIONS_INITIALIZER { "", "", false }
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
||||
150
repmgrd.c
150
repmgrd.c
@@ -30,18 +30,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
|
||||
#include "repmgr.h"
|
||||
#include "config.h"
|
||||
#include "log.h"
|
||||
#include "strutil.h"
|
||||
#include "version.h"
|
||||
|
||||
/* Required PostgreSQL headers */
|
||||
#include "access/xlogdefs.h"
|
||||
#include "pqexpbuffer.h"
|
||||
|
||||
/* Message strings passed in repmgrSharedState->location */
|
||||
|
||||
#define PASSIVE_NODE "PASSIVE_NODE"
|
||||
@@ -71,6 +63,7 @@ bool failover_done = false;
|
||||
bool manual_mode_upstream_disconnected = false;
|
||||
|
||||
char *pid_file = NULL;
|
||||
int server_version_num = 0;
|
||||
|
||||
static void help(void);
|
||||
static void usage(void);
|
||||
@@ -145,8 +138,6 @@ main(int argc, char **argv)
|
||||
|
||||
FILE *fd;
|
||||
|
||||
int server_version_num = 0;
|
||||
|
||||
set_progname(argv[0]);
|
||||
|
||||
/* Disallow running as root to prevent directory ownership problems */
|
||||
@@ -718,26 +709,46 @@ witness_monitor(void)
|
||||
return;
|
||||
}
|
||||
|
||||
strcpy(monitor_witness_timestamp, PQgetvalue(res, 0, 0));
|
||||
strncpy(monitor_witness_timestamp, PQgetvalue(res, 0, 0), MAXLEN);
|
||||
PQclear(res);
|
||||
|
||||
/*
|
||||
* Build the SQL to execute on master
|
||||
*/
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"INSERT INTO %s.repl_monitor "
|
||||
" (primary_node, standby_node, "
|
||||
" last_monitor_time, last_apply_time, "
|
||||
" last_wal_primary_location, last_wal_standby_location, "
|
||||
" replication_lag, apply_lag )"
|
||||
" VALUES(%d, %d, "
|
||||
" '%s'::TIMESTAMP WITH TIME ZONE, NULL, "
|
||||
" pg_catalog.pg_current_xlog_location(), NULL, "
|
||||
" 0, 0) ",
|
||||
get_repmgr_schema_quoted(my_local_conn),
|
||||
master_options.node,
|
||||
local_options.node,
|
||||
monitor_witness_timestamp);
|
||||
if (server_version_num >= 100000)
|
||||
{
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"INSERT INTO %s.repl_monitor "
|
||||
" (primary_node, standby_node, "
|
||||
" last_monitor_time, last_apply_time, "
|
||||
" last_wal_primary_location, last_wal_standby_location, "
|
||||
" replication_lag, apply_lag )"
|
||||
" VALUES(%d, %d, "
|
||||
" '%s'::TIMESTAMP WITH TIME ZONE, NULL, "
|
||||
" pg_catalog.pg_current_wal_lsn(), NULL, "
|
||||
" 0, 0) ",
|
||||
get_repmgr_schema_quoted(my_local_conn),
|
||||
master_options.node,
|
||||
local_options.node,
|
||||
monitor_witness_timestamp);
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"INSERT INTO %s.repl_monitor "
|
||||
" (primary_node, standby_node, "
|
||||
" last_monitor_time, last_apply_time, "
|
||||
" last_wal_primary_location, last_wal_standby_location, "
|
||||
" replication_lag, apply_lag )"
|
||||
" VALUES(%d, %d, "
|
||||
" '%s'::TIMESTAMP WITH TIME ZONE, NULL, "
|
||||
" pg_catalog.pg_current_xlog_location(), NULL, "
|
||||
" 0, 0) ",
|
||||
get_repmgr_schema_quoted(my_local_conn),
|
||||
master_options.node,
|
||||
local_options.node,
|
||||
monitor_witness_timestamp);
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute the query asynchronously, but don't check for a result. We will
|
||||
@@ -1125,21 +1136,42 @@ standby_monitor(void)
|
||||
* If receive_location is less than replay location, we were streaming WAL but are
|
||||
* somehow disconnected and evidently in archive recovery
|
||||
*/
|
||||
sqlquery_snprintf(sqlquery,
|
||||
" SELECT ts, "
|
||||
" CASE WHEN (receive_location IS NULL OR receive_location < replay_location) "
|
||||
" THEN replay_location "
|
||||
" ELSE receive_location"
|
||||
" END AS receive_location,"
|
||||
" replay_location, "
|
||||
" replay_timestamp, "
|
||||
" COALESCE(receive_location, '0/0') >= replay_location AS receiving_streamed_wal "
|
||||
" FROM (SELECT CURRENT_TIMESTAMP AS ts, "
|
||||
" pg_catalog.pg_last_xlog_receive_location() AS receive_location, "
|
||||
" pg_catalog.pg_last_xlog_replay_location() AS replay_location, "
|
||||
" pg_catalog.pg_last_xact_replay_timestamp() AS replay_timestamp "
|
||||
" ) q ");
|
||||
|
||||
if (server_version_num >= 100000)
|
||||
{
|
||||
sqlquery_snprintf(sqlquery,
|
||||
" SELECT ts, "
|
||||
" CASE WHEN (receive_location IS NULL OR receive_location < replay_location) "
|
||||
" THEN replay_location "
|
||||
" ELSE receive_location"
|
||||
" END AS receive_location,"
|
||||
" replay_location, "
|
||||
" replay_timestamp, "
|
||||
" COALESCE(receive_location, '0/0') >= replay_location AS receiving_streamed_wal "
|
||||
" FROM (SELECT CURRENT_TIMESTAMP AS ts, "
|
||||
" pg_catalog.pg_last_wal_receive_lsn() AS receive_location, "
|
||||
" pg_catalog.pg_last_wal_replay_lsn() AS replay_location, "
|
||||
" pg_catalog.pg_last_xact_replay_timestamp() AS replay_timestamp "
|
||||
" ) q ");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlquery_snprintf(sqlquery,
|
||||
" SELECT ts, "
|
||||
" CASE WHEN (receive_location IS NULL OR receive_location < replay_location) "
|
||||
" THEN replay_location "
|
||||
" ELSE receive_location"
|
||||
" END AS receive_location,"
|
||||
" replay_location, "
|
||||
" replay_timestamp, "
|
||||
" COALESCE(receive_location, '0/0') >= replay_location AS receiving_streamed_wal "
|
||||
" FROM (SELECT CURRENT_TIMESTAMP AS ts, "
|
||||
" pg_catalog.pg_last_xlog_receive_location() AS receive_location, "
|
||||
" pg_catalog.pg_last_xlog_replay_location() AS replay_location, "
|
||||
" pg_catalog.pg_last_xact_replay_timestamp() AS replay_timestamp "
|
||||
" ) q ");
|
||||
}
|
||||
|
||||
|
||||
res = PQexec(my_local_conn, sqlquery);
|
||||
@@ -1151,9 +1183,9 @@ standby_monitor(void)
|
||||
return;
|
||||
}
|
||||
|
||||
strncpy(monitor_standby_timestamp, PQgetvalue(res, 0, 0), MAXLEN);
|
||||
strncpy(monitor_standby_timestamp, PQgetvalue(res, 0, 0), MAXLEN);
|
||||
strncpy(last_xlog_receive_location, PQgetvalue(res, 0, 1), MAXLEN);
|
||||
strncpy(last_xlog_replay_location, PQgetvalue(res, 0, 2), MAXLEN);
|
||||
strncpy(last_xlog_replay_location, PQgetvalue(res, 0, 2), MAXLEN);
|
||||
strncpy(last_xact_replay_timestamp, PQgetvalue(res, 0, 3), MAXLEN);
|
||||
|
||||
receiving_streamed_wal = (strcmp(PQgetvalue(res, 0, 4), "t") == 0)
|
||||
@@ -1173,7 +1205,11 @@ standby_monitor(void)
|
||||
* TODO: investigate whether pg_current_xlog_insert_location() would be a better
|
||||
* choice; see: https://github.com/2ndQuadrant/repmgr/issues/189
|
||||
*/
|
||||
sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_current_xlog_location()");
|
||||
|
||||
if (server_version_num >= 100000)
|
||||
sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_current_wal_lsn()");
|
||||
else
|
||||
sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_current_xlog_location()");
|
||||
|
||||
res = PQexec(master_conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
@@ -1256,8 +1292,23 @@ standby_monitor(void)
|
||||
log_verbose(LOG_DEBUG, "standby_monitor:() %s\n", sqlquery);
|
||||
|
||||
if (PQsendQuery(master_conn, sqlquery) == 0)
|
||||
log_warning(_("query could not be sent to master. %s\n"),
|
||||
{
|
||||
log_warning(_("query could not be sent to master: %s\n"),
|
||||
PQerrorMessage(master_conn));
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT %s.repmgr_update_last_updated();",
|
||||
get_repmgr_schema_quoted(my_local_conn));
|
||||
res = PQexec(my_local_conn, sqlquery);
|
||||
|
||||
/* not critical if the above query fails*/
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
log_warning(_("unable to set last_updated: %s\n"), PQerrorMessage(my_local_conn));
|
||||
|
||||
PQclear(res);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1449,7 +1500,11 @@ do_master_failover(void)
|
||||
terminate(ERR_FAILOVER_FAIL);
|
||||
}
|
||||
|
||||
sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_last_xlog_receive_location()");
|
||||
if (server_version_num >= 100000)
|
||||
sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_last_wal_receive_lsn()");
|
||||
else
|
||||
sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_last_xlog_receive_location()");
|
||||
|
||||
res = PQexec(node_conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
@@ -1481,7 +1536,12 @@ do_master_failover(void)
|
||||
}
|
||||
|
||||
/* last we get info about this node, and update shared memory */
|
||||
sprintf(sqlquery, "SELECT pg_catalog.pg_last_xlog_receive_location()");
|
||||
|
||||
if (server_version_num >= 100000)
|
||||
sprintf(sqlquery, "SELECT pg_catalog.pg_last_wal_receive_lsn()");
|
||||
else
|
||||
sprintf(sqlquery, "SELECT pg_catalog.pg_last_xlog_receive_location()");
|
||||
|
||||
res = PQexec(my_local_conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
|
||||
4
sql/repmgr_test.sql
Normal file
4
sql/repmgr_test.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
select * from repmgr_update_standby_location('');
|
||||
select * from repmgr_get_last_standby_location();
|
||||
select * from repmgr_update_last_updated();
|
||||
select * from repmgr_get_last_updated();
|
||||
Reference in New Issue
Block a user