1 /*-------------------------------------------------------------------------
4 * Functions for fetching files from a remote server.
6 * Copyright (c) 2013-2015, PostgreSQL Global Development Group
8 *-------------------------------------------------------------------------
10 #include "postgres_fe.h"
12 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
22 #include "pg_rewind.h"
23 #include "datapagemap.h"
30 #include "catalog/catalog.h"
31 #include "catalog/pg_type.h"
33 static PGconn *conn = NULL;
36 * Files are fetched max CHUNKSIZE bytes at a time.
38 * (This only applies to files that are copied in whole, or for truncated
39 * files where we copy the tail. Relation files, where we know the individual
40 * blocks that need to be fetched, are fetched in BLCKSZ chunks.)
42 #define CHUNKSIZE 1000000
44 static void receiveFileChunks(const char *sql);
45 static void execute_pagemap(datapagemap_t *pagemap, const char *path);
46 static char *run_simple_query(const char *sql);
49 libpqConnect(const char *connstr)
53 conn = PQconnectdb(connstr);
54 if (PQstatus(conn) == CONNECTION_BAD)
55 pg_fatal("could not connect to server: %s",
56 PQerrorMessage(conn));
58 pg_log(PG_PROGRESS, "connected to server\n");
61 * Check that the server is not in hot standby mode. There is no
62 * fundamental reason that couldn't be made to work, but it doesn't
63 * currently because we use a temporary table. Better to check for it
64 * explicitly than error out, for a better error message.
66 str = run_simple_query("SELECT pg_is_in_recovery()");
67 if (strcmp(str, "f") != 0)
68 pg_fatal("source server must not be in recovery mode\n");
72 * Also check that full_page_writes is enabled. We can get torn pages if
73 * a page is modified while we read it with pg_read_binary_file(), and we
74 * rely on full page images to fix them.
76 str = run_simple_query("SHOW full_page_writes");
77 if (strcmp(str, "on") != 0)
78 pg_fatal("full_page_writes must be enabled in the source server\n");
83 * Runs a query that returns a single value.
84 * The result should be pg_free'd after use.
87 run_simple_query(const char *sql)
92 res = PQexec(conn, sql);
94 if (PQresultStatus(res) != PGRES_TUPLES_OK)
95 pg_fatal("error running query (%s) in source server: %s",
96 sql, PQresultErrorMessage(res));
98 /* sanity check the result set */
99 if (PQnfields(res) != 1 || PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
100 pg_fatal("unexpected result set from query\n");
102 result = pg_strdup(PQgetvalue(res, 0, 0));
110 * Calls pg_current_xlog_insert_location() function
113 libpqGetCurrentXlogInsertLocation(void)
120 val = run_simple_query("SELECT pg_current_xlog_insert_location()");
122 if (sscanf(val, "%X/%X", &hi, &lo) != 2)
123 pg_fatal("unrecognized result \"%s\" for current WAL insert location\n", val);
125 result = ((uint64) hi) << 32 | lo;
133 * Get a list of all files in the data directory.
136 libpqProcessFileList(void)
143 * Create a recursive directory listing of the whole data directory.
145 * The WITH RECURSIVE part does most of the work. The second part gets the
146 * targets of the symlinks in pg_tblspc directory.
148 * XXX: There is no backend function to get a symbolic link's target in
149 * general, so if the admin has put any custom symbolic links in the data
150 * directory, they won't be copied correctly.
153 "WITH RECURSIVE files (path, filename, size, isdir) AS (\n"
154 " SELECT '' AS path, filename, size, isdir FROM\n"
155 " (SELECT pg_ls_dir('.', true, false) AS filename) AS fn,\n"
156 " pg_stat_file(fn.filename, true) AS this\n"
158 " SELECT parent.path || parent.filename || '/' AS path,\n"
159 " fn, this.size, this.isdir\n"
160 " FROM files AS parent,\n"
161 " pg_ls_dir(parent.path || parent.filename, true, false) AS fn,\n"
162 " pg_stat_file(parent.path || parent.filename || '/' || fn, true) AS this\n"
163 " WHERE parent.isdir = 't'\n"
165 "SELECT path || filename, size, isdir,\n"
166 " pg_tablespace_location(pg_tablespace.oid) AS link_target\n"
168 "LEFT OUTER JOIN pg_tablespace ON files.path = 'pg_tblspc/'\n"
169 " AND oid::text = files.filename\n";
170 res = PQexec(conn, sql);
172 if (PQresultStatus(res) != PGRES_TUPLES_OK)
173 pg_fatal("could not fetch file list: %s",
174 PQresultErrorMessage(res));
176 /* sanity check the result set */
177 if (PQnfields(res) != 4)
178 pg_fatal("unexpected result set while fetching file list\n");
180 /* Read result to local variables */
181 for (i = 0; i < PQntuples(res); i++)
183 char *path = PQgetvalue(res, i, 0);
184 int filesize = atoi(PQgetvalue(res, i, 1));
185 bool isdir = (strcmp(PQgetvalue(res, i, 2), "t") == 0);
186 char *link_target = PQgetvalue(res, i, 3);
189 if (PQgetisnull(res, 0, 1))
192 * The file was removed from the server while the query was
193 * running. Ignore it.
199 type = FILE_TYPE_SYMLINK;
201 type = FILE_TYPE_DIRECTORY;
203 type = FILE_TYPE_REGULAR;
205 process_source_file(path, type, filesize, link_target);
211 * Runs a query, which returns pieces of files from the remote source data
212 * directory, and overwrites the corresponding parts of target files with
213 * the received parts. The result set is expected to be of format:
215 * path text -- path in the data directory, e.g "base/1/123"
216 * begin int4 -- offset within the file
217 * chunk bytea -- file content
221 receiveFileChunks(const char *sql)
225 if (PQsendQueryParams(conn, sql, 0, NULL, NULL, NULL, NULL, 1) != 1)
226 pg_fatal("could not send query: %s", PQerrorMessage(conn));
228 pg_log(PG_DEBUG, "getting file chunks\n");
230 if (PQsetSingleRowMode(conn) != 1)
231 pg_fatal("could not set libpq connection to single row mode\n");
233 while ((res = PQgetResult(conn)) != NULL)
241 switch (PQresultStatus(res))
243 case PGRES_SINGLE_TUPLE:
246 case PGRES_TUPLES_OK:
248 continue; /* final zero-row result */
251 pg_fatal("unexpected result while fetching remote files: %s",
252 PQresultErrorMessage(res));
255 /* sanity check the result set */
256 if (PQnfields(res) != 3 || PQntuples(res) != 1)
257 pg_fatal("unexpected result set size while fetching remote files\n");
259 if (PQftype(res, 0) != TEXTOID &&
260 PQftype(res, 1) != INT4OID &&
261 PQftype(res, 2) != BYTEAOID)
263 pg_fatal("unexpected data types in result set while fetching remote files: %u %u %u\n",
264 PQftype(res, 0), PQftype(res, 1), PQftype(res, 2));
267 if (PQfformat(res, 0) != 1 &&
268 PQfformat(res, 1) != 1 &&
269 PQfformat(res, 2) != 1)
271 pg_fatal("unexpected result format while fetching remote files\n");
274 if (PQgetisnull(res, 0, 0) ||
275 PQgetisnull(res, 0, 1))
277 pg_fatal("unexpected null values in result while fetching remote files\n");
280 if (PQgetlength(res, 0, 1) != sizeof(int32))
281 pg_fatal("unexpected result length while fetching remote files\n");
283 /* Read result set to local variables */
284 memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int32));
285 chunkoff = ntohl(chunkoff);
286 chunksize = PQgetlength(res, 0, 2);
288 filenamelen = PQgetlength(res, 0, 0);
289 filename = pg_malloc(filenamelen + 1);
290 memcpy(filename, PQgetvalue(res, 0, 0), filenamelen);
291 filename[filenamelen] = '\0';
293 chunk = PQgetvalue(res, 0, 2);
296 * It's possible that the file was deleted on remote side after we
297 * created the file map. In this case simply ignore it, as if it was
298 * not there in the first place, and move on.
300 if (PQgetisnull(res, 0, 2))
303 "received null value for chunk for file \"%s\", file has been deleted\n",
310 pg_log(PG_DEBUG, "received chunk for file \"%s\", offset %d, size %d\n",
311 filename, chunkoff, chunksize);
313 open_target_file(filename, false);
315 write_target_range(chunk, chunkoff, chunksize);
324 * Receive a single file as a malloc'd buffer.
327 libpqGetFile(const char *filename, size_t *filesize)
332 const char *paramValues[1];
334 paramValues[0] = filename;
335 res = PQexecParams(conn, "SELECT pg_read_binary_file($1)",
336 1, NULL, paramValues, NULL, NULL, 1);
338 if (PQresultStatus(res) != PGRES_TUPLES_OK)
339 pg_fatal("could not fetch remote file \"%s\": %s",
340 filename, PQresultErrorMessage(res));
342 /* sanity check the result set */
343 if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
344 pg_fatal("unexpected result set while fetching remote file \"%s\"\n",
347 /* Read result to local variables */
348 len = PQgetlength(res, 0, 0);
349 result = pg_malloc(len + 1);
350 memcpy(result, PQgetvalue(res, 0, 0), len);
355 pg_log(PG_DEBUG, "fetched file \"%s\", length %d\n", filename, len);
363 * Write a file range to a temporary table in the server.
365 * The range is sent to the server as a COPY formatted line, to be inserted
366 * into the 'fetchchunks' temporary table. It is used in receiveFileChunks()
367 * function to actually fetch the data.
370 fetch_file_range(const char *path, unsigned int begin, unsigned int end)
372 char linebuf[MAXPGPATH + 23];
374 /* Split the range into CHUNKSIZE chunks */
375 while (end - begin > 0)
379 if (end - begin > CHUNKSIZE)
384 snprintf(linebuf, sizeof(linebuf), "%s\t%u\t%u\n", path, begin, len);
386 if (PQputCopyData(conn, linebuf, strlen(linebuf)) != 1)
387 pg_fatal("could not send COPY data: %s",
388 PQerrorMessage(conn));
395 * Fetch all changed blocks from remote source data directory.
398 libpq_executeFileMap(filemap_t *map)
406 * First create a temporary table, and load it with the blocks that we
409 sql = "CREATE TEMPORARY TABLE fetchchunks(path text, begin int4, len int4);";
410 res = PQexec(conn, sql);
412 if (PQresultStatus(res) != PGRES_COMMAND_OK)
413 pg_fatal("could not create temporary table: %s",
414 PQresultErrorMessage(res));
417 sql = "COPY fetchchunks FROM STDIN";
418 res = PQexec(conn, sql);
420 if (PQresultStatus(res) != PGRES_COPY_IN)
421 pg_fatal("could not send file list: %s",
422 PQresultErrorMessage(res));
425 for (i = 0; i < map->narray; i++)
427 entry = map->array[i];
429 /* If this is a relation file, copy the modified blocks */
430 execute_pagemap(&entry->pagemap, entry->path);
432 switch (entry->action)
434 case FILE_ACTION_NONE:
435 /* nothing else to do */
438 case FILE_ACTION_COPY:
439 /* Truncate the old file out of the way, if any */
440 open_target_file(entry->path, true);
441 fetch_file_range(entry->path, 0, entry->newsize);
444 case FILE_ACTION_TRUNCATE:
445 truncate_target_file(entry->path, entry->newsize);
448 case FILE_ACTION_COPY_TAIL:
449 fetch_file_range(entry->path, entry->oldsize, entry->newsize);
452 case FILE_ACTION_REMOVE:
453 remove_target(entry);
456 case FILE_ACTION_CREATE:
457 create_target(entry);
462 if (PQputCopyEnd(conn, NULL) != 1)
463 pg_fatal("could not send end-of-COPY: %s",
464 PQerrorMessage(conn));
466 while ((res = PQgetResult(conn)) != NULL)
468 if (PQresultStatus(res) != PGRES_COMMAND_OK)
469 pg_fatal("unexpected result while sending file list: %s",
470 PQresultErrorMessage(res));
475 * We've now copied the list of file ranges that we need to fetch to the
476 * temporary table. Now, actually fetch all of those ranges.
479 "SELECT path, begin, \n"
480 " pg_read_binary_file(path, begin, len, true) AS chunk\n"
481 "FROM fetchchunks\n";
483 receiveFileChunks(sql);
487 execute_pagemap(datapagemap_t *pagemap, const char *path)
489 datapagemap_iterator_t *iter;
493 iter = datapagemap_iterate(pagemap);
494 while (datapagemap_next(iter, &blkno))
496 offset = blkno * BLCKSZ;
498 fetch_file_range(path, offset, offset + BLCKSZ);