diff --git a/src/bin/pg_rewind/libpq_fetch.c b/src/bin/pg_rewind/libpq_fetch.c
index c25367f..6a88d9f 100644
--- a/src/bin/pg_rewind/libpq_fetch.c
+++ b/src/bin/pg_rewind/libpq_fetch.c
@@ -220,6 +220,14 @@ libpqProcessFileList(void)
 	PQclear(res);
 }
 
+uint64_t ntohll(uint64_t value)
+{
+	const uint32_t high = ntohl((uint32_t)(value >> 32));
+	const uint32_t low = ntohl((uint32_t)(value & 0xFFFFFFFFLL));
+
+	return ((uint64_t)(low) << 32) | high;
+}
+
 /*----
  * Runs a query, which returns pieces of files from the remote source data
  * directory, and overwrites the corresponding parts of target files with
@@ -247,7 +255,7 @@ receiveFileChunks(const char *sql)
 	{
 		char	   *filename;
 		int			filenamelen;
-		int			chunkoff;
+		long long int			chunkoff;
 		int			chunksize;
 		char	   *chunk;
 
@@ -290,12 +298,12 @@ receiveFileChunks(const char *sql)
 			pg_fatal("unexpected null values in result while fetching remote files\n");
 		}
 
-		if (PQgetlength(res, 0, 1) != sizeof(int32))
+		if (PQgetlength(res, 0, 1) != sizeof(long long int))
 			pg_fatal("unexpected result length while fetching remote files\n");
 
 		/* Read result set to local variables */
-		memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int32));
-		chunkoff = ntohl(chunkoff);
+		memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(long long int));
+		chunkoff = ntohll(chunkoff);
 		chunksize = PQgetlength(res, 0, 2);
 
 		filenamelen = PQgetlength(res, 0, 0);
@@ -320,7 +328,7 @@ receiveFileChunks(const char *sql)
 			continue;
 		}
 
-		pg_log(PG_DEBUG, "received chunk for file \"%s\", offset %d, size %d\n",
+		pg_log(PG_DEBUG, "received chunk for file \"%s\", offset %lld, size %d\n",
 			   filename, chunkoff, chunksize);
 
 		open_target_file(filename, false);
@@ -419,7 +427,7 @@ libpq_executeFileMap(filemap_t *map)
 	 * First create a temporary table, and load it with the blocks that we
 	 * need to fetch.
 	 */
-	sql = "CREATE TEMPORARY TABLE fetchchunks(path text, begin int4, len int4);";
+	sql = "CREATE TEMPORARY TABLE fetchchunks(path text, begin bigint, len int4);";
 	res = PQexec(conn, sql);
 
 	if (PQresultStatus(res) != PGRES_COMMAND_OK)
