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 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

◆ 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:208
char * datadir_source
Definition: pg_rewind.c:51
void libpq_executeFileMap(filemap_t *map)
Definition: libpq_fetch.c:415

◆ 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:51
char * libpqGetFile(const char *filename, size_t *filesize)
Definition: libpq_fetch.c:343
static char * filename
Definition: pg_dumpall.c:90
char * slurpFile(const char *datadir, const char *path, size_t *filesize)
Definition: file_ops.c:276

◆ 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:36
char * datadir_source
Definition: pg_rewind.c:51
void libpqProcessFileList(void)
Definition: libpq_fetch.c:146
void process_source_file(const char *path, file_type_t type, size_t newsize, const char *link_target)
Definition: filemap.c:60

◆ libpq_executeFileMap()

void libpq_executeFileMap ( filemap_t map)

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

416 {
417  file_entry_t *entry;
418  const char *sql;
419  PGresult *res;
420  int i;
421 
422  /*
423  * First create a temporary table, and load it with the blocks that we
424  * need to fetch.
425  */
426  sql = "CREATE TEMPORARY TABLE fetchchunks(path text, begin int8, len int4);";
427  res = PQexec(conn, sql);
428 
429  if (PQresultStatus(res) != PGRES_COMMAND_OK)
430  pg_fatal("could not create temporary table: %s",
431  PQresultErrorMessage(res));
432  PQclear(res);
433 
434  sql = "COPY fetchchunks FROM STDIN";
435  res = PQexec(conn, sql);
436 
437  if (PQresultStatus(res) != PGRES_COPY_IN)
438  pg_fatal("could not send file list: %s",
439  PQresultErrorMessage(res));
440  PQclear(res);
441 
442  for (i = 0; i < map->narray; i++)
443  {
444  entry = map->array[i];
445 
446  /* If this is a relation file, copy the modified blocks */
447  execute_pagemap(&entry->pagemap, entry->path);
448 
449  switch (entry->action)
450  {
451  case FILE_ACTION_NONE:
452  /* nothing else to do */
453  break;
454 
455  case FILE_ACTION_COPY:
456  /* Truncate the old file out of the way, if any */
457  open_target_file(entry->path, true);
458  fetch_file_range(entry->path, 0, entry->newsize);
459  break;
460 
462  truncate_target_file(entry->path, entry->newsize);
463  break;
464 
466  fetch_file_range(entry->path, entry->oldsize, entry->newsize);
467  break;
468 
469  case FILE_ACTION_REMOVE:
470  remove_target(entry);
471  break;
472 
473  case FILE_ACTION_CREATE:
474  create_target(entry);
475  break;
476  }
477  }
478 
479  if (PQputCopyEnd(conn, NULL) != 1)
480  pg_fatal("could not send end-of-COPY: %s",
482 
483  while ((res = PQgetResult(conn)) != NULL)
484  {
485  if (PQresultStatus(res) != PGRES_COMMAND_OK)
486  pg_fatal("unexpected result while sending file list: %s",
487  PQresultErrorMessage(res));
488  PQclear(res);
489  }
490 
491  /*
492  * We've now copied the list of file ranges that we need to fetch to the
493  * temporary table. Now, actually fetch all of those ranges.
494  */
495  sql =
496  "SELECT path, begin,\n"
497  " pg_read_binary_file(path, begin, len, true) AS chunk\n"
498  "FROM fetchchunks\n";
499 
500  receiveFileChunks(sql);
501 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
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:504
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:231
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:386
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 CONNECTION_BAD, pg_fatal(), pg_free(), pg_log(), PG_PROGRESS, PGRES_COMMAND_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 
57  /*
58  * Check that the server is not in hot standby mode. There is no
59  * fundamental reason that couldn't be made to work, but it doesn't
60  * currently because we use a temporary table. Better to check for it
61  * explicitly than error out, for a better error message.
62  */
63  str = run_simple_query("SELECT pg_is_in_recovery()");
64  if (strcmp(str, "f") != 0)
65  pg_fatal("source server must not be in recovery mode\n");
66  pg_free(str);
67 
68  /*
69  * Also check that full_page_writes is enabled. We can get torn pages if
70  * a page is modified while we read it with pg_read_binary_file(), and we
71  * rely on full page images to fix them.
72  */
73  str = run_simple_query("SHOW full_page_writes");
74  if (strcmp(str, "on") != 0)
75  pg_fatal("full_page_writes must be enabled in the source server\n");
76  pg_free(str);
77 
78  /*
79  * Although we don't do any "real" updates, we do work with a temporary
80  * table. We don't care about synchronous commit for that. It doesn't
81  * otherwise matter much, but if the server is using synchronous
82  * replication, and replication isn't working for some reason, we don't
83  * want to get stuck, waiting for it to start working again.
84  */
85  res = PQexec(conn, "SET synchronous_commit = off");
86  if (PQresultStatus(res) != PGRES_COMMAND_OK)
87  pg_fatal("could not set up connection context: %s",
89  PQclear(res);
90 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
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:97
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
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:6053
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:585
static char * connstr
Definition: pg_dumpall.c:64

◆ libpqGetCurrentXlogInsertLocation()

XLogRecPtr libpqGetCurrentXlogInsertLocation ( void  )

Definition at line 123 of file libpq_fetch.c.

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

Referenced by main().

124 {
125  XLogRecPtr result;
126  uint32 hi;
127  uint32 lo;
128  char *val;
129 
130  val = run_simple_query("SELECT pg_current_wal_insert_lsn()");
131 
132  if (sscanf(val, "%X/%X", &hi, &lo) != 2)
133  pg_fatal("unrecognized result \"%s\" for current WAL insert location\n", val);
134 
135  result = ((uint64) hi) << 32 | lo;
136 
137  pg_free(val);
138 
139  return result;
140 }
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
static char * run_simple_query(const char *sql)
Definition: libpq_fetch.c:97
unsigned int uint32
Definition: c.h:296
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 343 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().

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

◆ libpqProcessFileList()

void libpqProcessFileList ( void  )

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

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

◆ traverse_datadir()

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