PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
fetch.h File Reference
#include "access/xlogdefs.h"
#include "filemap.h"
Include dependency graph for fetch.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef void(* process_file_callback_t )(const char *path, file_type_t type, size_t size, const char *link_target)
 

Functions

void fetchSourceFileList (void)
 
char * fetchFile (char *filename, size_t *filesize)
 
void executeFileMap (void)
 
void libpqProcessFileList (void)
 
char * libpqGetFile (const char *filename, size_t *filesize)
 
void libpq_executeFileMap (filemap_t *map)
 
void libpqConnect (const char *connstr)
 
XLogRecPtr libpqGetCurrentXlogInsertLocation (void)
 
void copy_executeFileMap (filemap_t *map)
 
void traverse_datadir (const char *datadir, process_file_callback_t callback)
 

Typedef Documentation

typedef void(* process_file_callback_t)(const char *path, file_type_t type, size_t size, const char *link_target)

Definition at line 41 of file fetch.h.

Function Documentation

void copy_executeFileMap ( filemap_t map)

Definition at line 208 of file copy_fetch.c.

References file_entry_t::action, filemap_t::array, close_target_file(), copy_file_range(), create_target(), execute_pagemap(), FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_ACTION_CREATE, FILE_ACTION_NONE, FILE_ACTION_REMOVE, FILE_ACTION_TRUNCATE, i, filemap_t::narray, file_entry_t::newsize, file_entry_t::oldsize, file_entry_t::pagemap, file_entry_t::path, remove_target(), and truncate_target_file().

Referenced by executeFileMap().

209 {
210  file_entry_t *entry;
211  int i;
212 
213  for (i = 0; i < map->narray; i++)
214  {
215  entry = map->array[i];
216  execute_pagemap(&entry->pagemap, entry->path);
217 
218  switch (entry->action)
219  {
220  case FILE_ACTION_NONE:
221  /* ok, do nothing.. */
222  break;
223 
224  case FILE_ACTION_COPY:
225  copy_file_range(entry->path, 0, entry->newsize, true);
226  break;
227 
229  truncate_target_file(entry->path, entry->newsize);
230  break;
231 
233  copy_file_range(entry->path, entry->oldsize, entry->newsize, false);
234  break;
235 
236  case FILE_ACTION_CREATE:
237  create_target(entry);
238  break;
239 
240  case FILE_ACTION_REMOVE:
241  remove_target(entry);
242  break;
243  }
244  }
245 
247 }
file_entry_t ** array
Definition: filemap.h:79
size_t newsize
Definition: filemap.h:51
datapagemap_t pagemap
Definition: filemap.h:54
int narray
Definition: filemap.h:80
void truncate_target_file(const char *path, off_t newsize)
Definition: file_ops.c:183
static void execute_pagemap(datapagemap_t *pagemap, const char *path)
Definition: copy_fetch.c:250
file_action_t action
Definition: filemap.h:47
size_t oldsize
Definition: filemap.h:50
void remove_target(file_entry_t *entry)
Definition: file_ops.c:126
char * path
Definition: filemap.h:44
void close_target_file(void)
Definition: file_ops.c:71
int i
Definition: filemap.h:42
void create_target(file_entry_t *entry)
Definition: file_ops.c:147
static void copy_file_range(const char *path, off_t begin, off_t end, bool trunc)
Definition: copy_fetch.c:159
void executeFileMap ( void  )

Definition at line 40 of file fetch.c.

References copy_executeFileMap(), datadir_source, filemap, and libpq_executeFileMap().

Referenced by main().

41 {
42  if (datadir_source)
44  else
46 }
filemap_t * filemap
Definition: filemap.c:25
void copy_executeFileMap(filemap_t *map)
Definition: copy_fetch.c:208
char * datadir_source
Definition: pg_rewind.c:51
void libpq_executeFileMap(filemap_t *map)
Definition: libpq_fetch.c:440
char* fetchFile ( char *  filename,
size_t *  filesize 
)

Definition at line 54 of file fetch.c.

References datadir_source, libpqGetFile(), and slurpFile().

Referenced by getTimelineHistory(), and main().

55 {
56  if (datadir_source)
57  return slurpFile(datadir_source, filename, filesize);
58  else
59  return libpqGetFile(filename, filesize);
60 }
char * datadir_source
Definition: pg_rewind.c:51
char * libpqGetFile(const char *filename, size_t *filesize)
Definition: libpq_fetch.c:368
static char * filename
Definition: pg_dumpall.c:90
char * slurpFile(const char *datadir, const char *path, size_t *filesize)
Definition: file_ops.c:276
void fetchSourceFileList ( void  )

Definition at line 28 of file fetch.c.

References datadir_source, libpqProcessFileList(), process_source_file(), and traverse_datadir().

Referenced by main().

29 {
30  if (datadir_source)
32  else
34 }
void traverse_datadir(const char *datadir, process_file_callback_t callback)
Definition: copy_fetch.c:36
char * datadir_source
Definition: pg_rewind.c:51
void libpqProcessFileList(void)
Definition: libpq_fetch.c:149
void process_source_file(const char *path, file_type_t type, size_t newsize, const char *link_target)
Definition: filemap.c:60
void libpq_executeFileMap ( filemap_t map)

Definition at line 440 of file libpq_fetch.c.

References file_entry_t::action, filemap_t::array, create_target(), execute_pagemap(), fetch_file_range(), FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_ACTION_CREATE, FILE_ACTION_NONE, FILE_ACTION_REMOVE, FILE_ACTION_TRUNCATE, i, filemap_t::narray, file_entry_t::newsize, file_entry_t::oldsize, open_target_file(), file_entry_t::pagemap, file_entry_t::path, pg_fatal(), PGRES_COMMAND_OK, PGRES_COPY_IN, PQclear(), PQerrorMessage(), PQexec(), PQgetResult(), PQputCopyEnd(), PQresultErrorMessage(), PQresultStatus(), receiveFileChunks(), remove_target(), and truncate_target_file().

Referenced by executeFileMap().

441 {
442  file_entry_t *entry;
443  const char *sql;
444  PGresult *res;
445  int i;
446 
447  /*
448  * First create a temporary table, and load it with the blocks that we
449  * need to fetch.
450  */
451  sql = "CREATE TEMPORARY TABLE fetchchunks(path text, begin int8, len int4);";
452  res = PQexec(conn, sql);
453 
454  if (PQresultStatus(res) != PGRES_COMMAND_OK)
455  pg_fatal("could not create temporary table: %s",
456  PQresultErrorMessage(res));
457  PQclear(res);
458 
459  sql = "COPY fetchchunks FROM STDIN";
460  res = PQexec(conn, sql);
461 
462  if (PQresultStatus(res) != PGRES_COPY_IN)
463  pg_fatal("could not send file list: %s",
464  PQresultErrorMessage(res));
465  PQclear(res);
466 
467  for (i = 0; i < map->narray; i++)
468  {
469  entry = map->array[i];
470 
471  /* If this is a relation file, copy the modified blocks */
472  execute_pagemap(&entry->pagemap, entry->path);
473 
474  switch (entry->action)
475  {
476  case FILE_ACTION_NONE:
477  /* nothing else to do */
478  break;
479 
480  case FILE_ACTION_COPY:
481  /* Truncate the old file out of the way, if any */
482  open_target_file(entry->path, true);
483  fetch_file_range(entry->path, 0, entry->newsize);
484  break;
485 
487  truncate_target_file(entry->path, entry->newsize);
488  break;
489 
491  fetch_file_range(entry->path, entry->oldsize, entry->newsize);
492  break;
493 
494  case FILE_ACTION_REMOVE:
495  remove_target(entry);
496  break;
497 
498  case FILE_ACTION_CREATE:
499  create_target(entry);
500  break;
501  }
502  }
503 
504  if (PQputCopyEnd(conn, NULL) != 1)
505  pg_fatal("could not send end-of-COPY: %s",
507 
508  while ((res = PQgetResult(conn)) != NULL)
509  {
510  if (PQresultStatus(res) != PGRES_COMMAND_OK)
511  pg_fatal("unexpected result while sending file list: %s",
512  PQresultErrorMessage(res));
513  PQclear(res);
514  }
515 
516  /*
517  * We've now copied the list of file ranges that we need to fetch to the
518  * temporary table. Now, actually fetch all of those ranges.
519  */
520  sql =
521  "SELECT path, begin,\n"
522  " pg_read_binary_file(path, begin, len, true) AS chunk\n"
523  "FROM fetchchunks\n";
524 
525  receiveFileChunks(sql);
526 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
void open_target_file(const char *path, bool trunc)
Definition: file_ops.c:43
file_entry_t ** array
Definition: filemap.h:79
size_t newsize
Definition: filemap.h:51
static void execute_pagemap(datapagemap_t *pagemap, const char *path)
Definition: libpq_fetch.c:529
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2339
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
datapagemap_t pagemap
Definition: filemap.h:54
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
int narray
Definition: filemap.h:80
void truncate_target_file(const char *path, off_t newsize)
Definition: file_ops.c:183
file_action_t action
Definition: filemap.h:47
static void receiveFileChunks(const char *sql)
Definition: libpq_fetch.c:256
size_t oldsize
Definition: filemap.h:50
static PGconn * conn
Definition: libpq_fetch.c:32
void PQclear(PGresult *res)
Definition: fe-exec.c:671
void remove_target(file_entry_t *entry)
Definition: file_ops.c:126
char * path
Definition: filemap.h:44
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2663
static void fetch_file_range(const char *path, uint64 begin, uint64 end)
Definition: libpq_fetch.c:411
int i
Definition: filemap.h:42
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
void create_target(file_entry_t *entry)
Definition: file_ops.c:147
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1753
void libpqConnect ( const char *  connstr)

Definition at line 48 of file libpq_fetch.c.

References CONNECTION_BAD, pg_fatal(), pg_free(), pg_log(), PG_PROGRESS, PGRES_COMMAND_OK, PQclear(), PQconnectdb(), PQerrorMessage(), PQexec(), PQresultErrorMessage(), PQresultStatus(), PQstatus(), and run_simple_query().

Referenced by main().

49 {
50  char *str;
51  PGresult *res;
52 
55  pg_fatal("could not connect to server: %s",
57 
58  pg_log(PG_PROGRESS, "connected to server\n");
59 
60  /*
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.
65  */
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");
69  pg_free(str);
70 
71  /*
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.
75  */
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");
79  pg_free(str);
80 
81  /*
82  * Although we don't do any "real" updates, we do work with a temporary
83  * table. We don't care about synchronous commit for that. It doesn't
84  * otherwise matter much, but if the server is using synchronous
85  * replication, and replication isn't working for some reason, we don't
86  * want to get stuck, waiting for it to start working again.
87  */
88  res = PQexec(conn, "SET synchronous_commit = off");
89  if (PQresultStatus(res) != PGRES_COMMAND_OK)
90  pg_fatal("could not set up connection context: %s",
92  PQclear(res);
93 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
static char * run_simple_query(const char *sql)
Definition: libpq_fetch.c:100
static PGconn * conn
Definition: libpq_fetch.c:32
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
void PQclear(PGresult *res)
Definition: fe-exec.c:671
void pg_free(void *ptr)
Definition: fe_memutils.c:105
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2663
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:585
static char * connstr
Definition: pg_dumpall.c:64
XLogRecPtr libpqGetCurrentXlogInsertLocation ( void  )

Definition at line 126 of file libpq_fetch.c.

References pg_fatal(), pg_free(), run_simple_query(), and val.

Referenced by main().

127 {
128  XLogRecPtr result;
129  uint32 hi;
130  uint32 lo;
131  char *val;
132 
133  val = run_simple_query("SELECT pg_current_wal_insert_lsn()");
134 
135  if (sscanf(val, "%X/%X", &hi, &lo) != 2)
136  pg_fatal("unrecognized result \"%s\" for current WAL insert location\n", val);
137 
138  result = ((uint64) hi) << 32 | lo;
139 
140  pg_free(val);
141 
142  return result;
143 }
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
static char * run_simple_query(const char *sql)
Definition: libpq_fetch.c:100
unsigned int uint32
Definition: c.h:258
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void pg_free(void *ptr)
Definition: fe_memutils.c:105
long val
Definition: informix.c:689
char* libpqGetFile ( const char *  filename,
size_t *  filesize 
)

Definition at line 368 of file libpq_fetch.c.

References filename, PG_DEBUG, pg_fatal(), pg_log(), pg_malloc(), PGRES_TUPLES_OK, PQclear(), PQexecParams(), PQgetisnull(), PQgetlength(), PQgetvalue(), PQntuples(), PQresultErrorMessage(), and PQresultStatus().

Referenced by fetchFile().

369 {
370  PGresult *res;
371  char *result;
372  int len;
373  const char *paramValues[1];
374 
375  paramValues[0] = filename;
376  res = PQexecParams(conn, "SELECT pg_read_binary_file($1)",
377  1, NULL, paramValues, NULL, NULL, 1);
378 
379  if (PQresultStatus(res) != PGRES_TUPLES_OK)
380  pg_fatal("could not fetch remote file \"%s\": %s",
382 
383  /* sanity check the result set */
384  if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
385  pg_fatal("unexpected result set while fetching remote file \"%s\"\n",
386  filename);
387 
388  /* Read result to local variables */
389  len = PQgetlength(res, 0, 0);
390  result = pg_malloc(len + 1);
391  memcpy(result, PQgetvalue(res, 0, 0), len);
392  result[len] = '\0';
393 
394  PQclear(res);
395 
396  pg_log(PG_DEBUG, "fetched file \"%s\", length %d\n", filename, len);
397 
398  if (filesize)
399  *filesize = len;
400  return result;
401 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3129
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
static PGconn * conn
Definition: libpq_fetch.c:32
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
void PQclear(PGresult *res)
Definition: fe-exec.c:671
static char * filename
Definition: pg_dumpall.c:90
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2663
PGresult * PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:1911
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
void libpqProcessFileList ( void  )

Definition at line 149 of file libpq_fetch.c.

References FILE_TYPE_DIRECTORY, FILE_TYPE_REGULAR, FILE_TYPE_SYMLINK, i, pg_fatal(), PGRES_TUPLES_OK, PQclear(), PQexec(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), PQresultErrorMessage(), PQresultStatus(), and process_source_file().

Referenced by fetchSourceFileList().

150 {
151  PGresult *res;
152  const char *sql;
153  int i;
154 
155  /*
156  * Create a recursive directory listing of the whole data directory.
157  *
158  * The WITH RECURSIVE part does most of the work. The second part gets the
159  * targets of the symlinks in pg_tblspc directory.
160  *
161  * XXX: There is no backend function to get a symbolic link's target in
162  * general, so if the admin has put any custom symbolic links in the data
163  * directory, they won't be copied correctly.
164  */
165  sql =
166  "WITH RECURSIVE files (path, filename, size, isdir) AS (\n"
167  " SELECT '' AS path, filename, size, isdir FROM\n"
168  " (SELECT pg_ls_dir('.', true, false) AS filename) AS fn,\n"
169  " pg_stat_file(fn.filename, true) AS this\n"
170  " UNION ALL\n"
171  " SELECT parent.path || parent.filename || '/' AS path,\n"
172  " fn, this.size, this.isdir\n"
173  " FROM files AS parent,\n"
174  " pg_ls_dir(parent.path || parent.filename, true, false) AS fn,\n"
175  " pg_stat_file(parent.path || parent.filename || '/' || fn, true) AS this\n"
176  " WHERE parent.isdir = 't'\n"
177  ")\n"
178  "SELECT path || filename, size, isdir,\n"
179  " pg_tablespace_location(pg_tablespace.oid) AS link_target\n"
180  "FROM files\n"
181  "LEFT OUTER JOIN pg_tablespace ON files.path = 'pg_tblspc/'\n"
182  " AND oid::text = files.filename\n";
183  res = PQexec(conn, sql);
184 
185  if (PQresultStatus(res) != PGRES_TUPLES_OK)
186  pg_fatal("could not fetch file list: %s",
187  PQresultErrorMessage(res));
188 
189  /* sanity check the result set */
190  if (PQnfields(res) != 4)
191  pg_fatal("unexpected result set while fetching file list\n");
192 
193  /* Read result to local variables */
194  for (i = 0; i < PQntuples(res); i++)
195  {
196  char *path = PQgetvalue(res, i, 0);
197  int64 filesize = atol(PQgetvalue(res, i, 1));
198  bool isdir = (strcmp(PQgetvalue(res, i, 2), "t") == 0);
199  char *link_target = PQgetvalue(res, i, 3);
200  file_type_t type;
201 
202  if (PQgetisnull(res, 0, 1))
203  {
204  /*
205  * The file was removed from the server while the query was
206  * running. Ignore it.
207  */
208  continue;
209  }
210 
211  if (link_target[0])
212  type = FILE_TYPE_SYMLINK;
213  else if (isdir)
214  type = FILE_TYPE_DIRECTORY;
215  else
216  type = FILE_TYPE_REGULAR;
217 
218  process_source_file(path, type, filesize, link_target);
219  }
220  PQclear(res);
221 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
static PGconn * conn
Definition: libpq_fetch.c:32
void PQclear(PGresult *res)
Definition: fe-exec.c:671
void process_source_file(const char *path, file_type_t type, size_t newsize, const char *link_target)
Definition: filemap.c:60
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2663
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
file_type_t
Definition: filemap.h:35
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
void traverse_datadir ( const char *  datadir,
process_file_callback_t  callback 
)

Definition at line 36 of file copy_fetch.c.

References recurse_dir().

Referenced by fetchSourceFileList(), and main().

37 {
39 }
static void recurse_dir(const char *datadir, const char *path, process_file_callback_t callback)
Definition: copy_fetch.c:48
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
char * datadir