diff --git a/src/bin/pg_rewind/libpq_fetch.c b/src/bin/pg_rewind/libpq_fetch.c
index 37eccc3126..18740c15d0 100644
--- a/src/bin/pg_rewind/libpq_fetch.c
+++ b/src/bin/pg_rewind/libpq_fetch.c
@@ -54,6 +54,13 @@ libpqConnect(const char *connstr)
 	if (showprogress)
 		pg_log_info("connected to server");
 
+	/* We don't want our queries cancelled due to statement timeout */
+	res = PQexec(conn, "SET statement_timeout = 0");
+	if (PQresultStatus(res) != PGRES_COMMAND_OK)
+		pg_fatal("could not set up statement_timeout: %s",
+				 PQresultErrorMessage(res));
+	PQclear(res);
+
 	res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 		pg_fatal("could not clear search_path: %s",
