PostgreSQL Source Code  git master
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 (const 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

◆ process_file_callback_t

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

◆ copy_executeFileMap()

void copy_executeFileMap ( filemap_t map)

Definition at line 206 of file copy_fetch.c.

References file_entry_t::action, filemap_t::array, close_target_file(), 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(), rewind_copy_file_range(), and truncate_target_file().

Referenced by executeFileMap().

207 {
208  file_entry_t *entry;
209  int i;
210 
211  for (i = 0; i < map->narray; i++)
212  {
213  entry = map->array[i];
214  execute_pagemap(&entry->pagemap, entry->path);
215 
216  switch (entry->action)
217  {
218  case FILE_ACTION_NONE:
219  /* ok, do nothing.. */
220  break;
221 
222  case FILE_ACTION_COPY:
223  rewind_copy_file_range(entry->path, 0, entry->newsize, true);
224  break;
225 
227  truncate_target_file(entry->path, entry->newsize);
228  break;
229 
231  rewind_copy_file_range(entry->path, entry->oldsize,
232  entry->newsize, false);
233  break;
234 
235  case FILE_ACTION_CREATE:
236  create_target(entry);
237  break;
238 
239  case FILE_ACTION_REMOVE:
240  remove_target(entry);
241  break;
242  }
243  }
244 
246 }
static void rewind_copy_file_range(const char *path, off_t begin, off_t end, bool trunc)
Definition: copy_fetch.c:157
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:192
static void execute_pagemap(datapagemap_t *pagemap, const char *path)
Definition: copy_fetch.c:249
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

◆ executeFileMap()

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:206
char * datadir_source
Definition: pg_rewind.c:52
void libpq_executeFileMap(filemap_t *map)
Definition: libpq_fetch.c:425

◆ fetchFile()

char* fetchFile ( const 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:52
char * libpqGetFile(const char *filename, size_t *filesize)
Definition: libpq_fetch.c:353
static char * filename
Definition: pg_dumpall.c:87
char * slurpFile(const char *datadir, const char *path, size_t *filesize)
Definition: file_ops.c:285

◆ fetchSourceFileList()

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:34
char * datadir_source
Definition: pg_rewind.c:52
void libpqProcessFileList(void)
Definition: libpq_fetch.c:152
void process_source_file(const char *path, file_type_t type, size_t newsize, const char *link_target)
Definition: filemap.c:137

◆ libpq_executeFileMap()

void libpq_executeFileMap ( filemap_t map)

Definition at line 425 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().

426 {
427  file_entry_t *entry;
428  const char *sql;
429  PGresult *res;
430  int i;
431 
432  /*
433  * First create a temporary table, and load it with the blocks that we
434  * need to fetch.
435  */
436  sql = "CREATE TEMPORARY TABLE fetchchunks(path text, begin int8, len int4);";
437  res = PQexec(conn, sql);
438 
439  if (PQresultStatus(res) != PGRES_COMMAND_OK)
440  pg_fatal("could not create temporary table: %s",
441  PQresultErrorMessage(res));
442  PQclear(res);
443 
444  sql = "COPY fetchchunks FROM STDIN";
445  res = PQexec(conn, sql);
446 
447  if (PQresultStatus(res) != PGRES_COPY_IN)
448  pg_fatal("could not send file list: %s",
449  PQresultErrorMessage(res));
450  PQclear(res);
451 
452  for (i = 0; i < map->narray; i++)
453  {
454  entry = map->array[i];
455 
456  /* If this is a relation file, copy the modified blocks */
457  execute_pagemap(&entry->pagemap, entry->path);
458 
459  switch (entry->action)
460  {
461  case FILE_ACTION_NONE:
462  /* nothing else to do */
463  break;
464 
465  case FILE_ACTION_COPY:
466  /* Truncate the old file out of the way, if any */
467  open_target_file(entry->path, true);
468  fetch_file_range(entry->path, 0, entry->newsize);
469  break;
470 
472  truncate_target_file(entry->path, entry->newsize);
473  break;
474 
476  fetch_file_range(entry->path, entry->oldsize, entry->newsize);
477  break;
478 
479  case FILE_ACTION_REMOVE:
480  remove_target(entry);
481  break;
482 
483  case FILE_ACTION_CREATE:
484  create_target(entry);
485  break;
486  }
487  }
488 
489  if (PQputCopyEnd(conn, NULL) != 1)
490  pg_fatal("could not send end-of-COPY: %s",
492 
493  while ((res = PQgetResult(conn)) != NULL)
494  {
495  if (PQresultStatus(res) != PGRES_COMMAND_OK)
496  pg_fatal("unexpected result while sending file list: %s",
497  PQresultErrorMessage(res));
498  PQclear(res);
499  }
500 
501  /*
502  * We've now copied the list of file ranges that we need to fetch to the
503  * temporary table. Now, actually fetch all of those ranges.
504  */
505  sql =
506  "SELECT path, begin,\n"
507  " pg_read_binary_file(path, begin, len, true) AS chunk\n"
508  "FROM fetchchunks\n";
509 
510  receiveFileChunks(sql);
511 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
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:514
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:192
file_action_t action
Definition: filemap.h:47
static void receiveFileChunks(const char *sql)
Definition: libpq_fetch.c:237
size_t oldsize
Definition: filemap.h:50
static PGconn * conn
Definition: libpq_fetch.c:29
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:396
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

◆ libpqConnect()

void libpqConnect ( const char *  connstr)

Definition at line 45 of file libpq_fetch.c.

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

Referenced by main().

46 {
47  char *str;
48  PGresult *res;
49 
52  pg_fatal("could not connect to server: %s",
54 
55  pg_log(PG_PROGRESS, "connected to server\n");
56 
58  if (PQresultStatus(res) != PGRES_TUPLES_OK)
59  pg_fatal("could not clear search_path: %s",
61  PQclear(res);
62 
63  /*
64  * Check that the server is not in hot standby mode. There is no
65  * fundamental reason that couldn't be made to work, but it doesn't
66  * currently because we use a temporary table. Better to check for it
67  * explicitly than error out, for a better error message.
68  */
69  str = run_simple_query("SELECT pg_is_in_recovery()");
70  if (strcmp(str, "f") != 0)
71  pg_fatal("source server must not be in recovery mode\n");
72  pg_free(str);
73 
74  /*
75  * Also check that full_page_writes is enabled. We can get torn pages if
76  * a page is modified while we read it with pg_read_binary_file(), and we
77  * rely on full page images to fix them.
78  */
79  str = run_simple_query("SHOW full_page_writes");
80  if (strcmp(str, "on") != 0)
81  pg_fatal("full_page_writes must be enabled in the source server\n");
82  pg_free(str);
83 
84  /*
85  * Although we don't do any "real" updates, we do work with a temporary
86  * table. We don't care about synchronous commit for that. It doesn't
87  * otherwise matter much, but if the server is using synchronous
88  * replication, and replication isn't working for some reason, we don't
89  * want to get stuck, waiting for it to start working again.
90  */
91  res = PQexec(conn, "SET synchronous_commit = off");
92  if (PQresultStatus(res) != PGRES_COMMAND_OK)
93  pg_fatal("could not set up connection context: %s",
95  PQclear(res);
96 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
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:103
static PGconn * conn
Definition: libpq_fetch.c:29
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
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
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:6063
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:592
static char * connstr
Definition: pg_dumpall.c:59

◆ libpqGetCurrentXlogInsertLocation()

XLogRecPtr libpqGetCurrentXlogInsertLocation ( void  )

Definition at line 129 of file libpq_fetch.c.

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

Referenced by main().

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

◆ libpqGetFile()

char* libpqGetFile ( const char *  filename,
size_t *  filesize 
)

Definition at line 353 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().

354 {
355  PGresult *res;
356  char *result;
357  int len;
358  const char *paramValues[1];
359 
360  paramValues[0] = filename;
361  res = PQexecParams(conn, "SELECT pg_read_binary_file($1)",
362  1, NULL, paramValues, NULL, NULL, 1);
363 
364  if (PQresultStatus(res) != PGRES_TUPLES_OK)
365  pg_fatal("could not fetch remote file \"%s\": %s",
367 
368  /* sanity check the result set */
369  if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
370  pg_fatal("unexpected result set while fetching remote file \"%s\"\n",
371  filename);
372 
373  /* Read result to local variables */
374  len = PQgetlength(res, 0, 0);
375  result = pg_malloc(len + 1);
376  memcpy(result, PQgetvalue(res, 0, 0), len);
377  result[len] = '\0';
378 
379  PQclear(res);
380 
381  pg_log(PG_DEBUG, "fetched file \"%s\", length %d\n", filename, len);
382 
383  if (filesize)
384  *filesize = len;
385  return result;
386 }
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:29
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:87
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

◆ libpqProcessFileList()

void libpqProcessFileList ( void  )

Definition at line 152 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(), process_source_file(), and generate_unaccent_rules::type.

Referenced by fetchSourceFileList().

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

◆ traverse_datadir()

void traverse_datadir ( const char *  datadir,
process_file_callback_t  callback 
)

Definition at line 34 of file copy_fetch.c.

References recurse_dir().

Referenced by fetchSourceFileList(), and main().

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