Skip to content

Commit 19bfa15

Browse files
committed
Disable timeouts when running pg_rewind with online source cluster
In this case, the transfer uses a libpq connection, which is subject to the timeout parameters set at system level, and this can make the rewind operation suddenly canceled which is not good for automation. One workaround to such issues would be to use PGOPTIONS to enforce the wanted timeout parameters, but that's annoying, and for example pg_dump, which can run potentially long-running queries disables all types of timeouts. lock_timeout and statement_timeout are the ones which can cause problems now. Note that pg_rewind does not use transactions, so disabling idle_in_transaction_session_timeout is optional, but it feels safer to do so for the future. This is back-patched down to 9.5. idle_in_transaction_session_timeout is only present since 9.6. Author: Alexander Kukushkin Discussion: https://postgr.es/m/CAFh8B=krcVXksxiwVQh1SoY+ziJ-JC=6FcuoBL3yce_40Es5_g@mail.gmail.com Backpatch-through: 9.5
1 parent e872432 commit 19bfa15

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/bin/pg_rewind/libpq_fetch.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static PGconn *conn = NULL;
4444
static void receiveFileChunks(const char *sql);
4545
static void execute_pagemap(datapagemap_t *pagemap, const char *path);
4646
static char *run_simple_query(const char *sql);
47+
static void run_simple_command(const char *sql);
4748

4849
void
4950
libpqConnect(const char *connstr)
@@ -58,6 +59,11 @@ libpqConnect(const char *connstr)
5859

5960
pg_log(PG_PROGRESS, "connected to server\n");
6061

62+
/* disable all types of timeouts */
63+
run_simple_command("SET statement_timeout = 0");
64+
run_simple_command("SET lock_timeout = 0");
65+
run_simple_command("SET idle_in_transaction_session_timeout = 0");
66+
6167
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
6268
if (PQresultStatus(res) != PGRES_TUPLES_OK)
6369
pg_fatal("could not clear search_path: %s",
@@ -92,11 +98,7 @@ libpqConnect(const char *connstr)
9298
* replication, and replication isn't working for some reason, we don't
9399
* want to get stuck, waiting for it to start working again.
94100
*/
95-
res = PQexec(conn, "SET synchronous_commit = off");
96-
if (PQresultStatus(res) != PGRES_COMMAND_OK)
97-
pg_fatal("could not set up connection context: %s",
98-
PQresultErrorMessage(res));
99-
PQclear(res);
101+
run_simple_command("SET synchronous_commit = off");
100102
}
101103

102104
/*
@@ -126,6 +128,24 @@ run_simple_query(const char *sql)
126128
return result;
127129
}
128130

131+
/*
132+
* Runs a command.
133+
* In the event of a failure, exit immediately.
134+
*/
135+
static void
136+
run_simple_command(const char *sql)
137+
{
138+
PGresult *res;
139+
140+
res = PQexec(conn, sql);
141+
142+
if (PQresultStatus(res) != PGRES_COMMAND_OK)
143+
pg_fatal("error running query (%s) in source server: %s",
144+
sql, PQresultErrorMessage(res));
145+
146+
PQclear(res);
147+
}
148+
129149
/*
130150
* Calls pg_current_wal_insert_lsn() function
131151
*/
@@ -460,12 +480,7 @@ libpq_executeFileMap(filemap_t *map)
460480
* need to fetch.
461481
*/
462482
sql = "CREATE TEMPORARY TABLE fetchchunks(path text, begin int8, len int4);";
463-
res = PQexec(conn, sql);
464-
465-
if (PQresultStatus(res) != PGRES_COMMAND_OK)
466-
pg_fatal("could not create temporary table: %s",
467-
PQresultErrorMessage(res));
468-
PQclear(res);
483+
run_simple_command(sql);
469484

470485
sql = "COPY fetchchunks FROM STDIN";
471486
res = PQexec(conn, sql);

0 commit comments

Comments
 (0)