PostgreSQL Source Code  git master
libpq_fetch.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include "pg_rewind.h"
#include "datapagemap.h"
#include "fetch.h"
#include "file_ops.h"
#include "filemap.h"
#include "catalog/pg_type_d.h"
#include "fe_utils/connect.h"
#include "port/pg_bswap.h"
Include dependency graph for libpq_fetch.c:

Go to the source code of this file.

Macros

#define CHUNKSIZE   1000000
 

Functions

static void receiveFileChunks (const char *sql)
 
static void execute_pagemap (datapagemap_t *pagemap, const char *path)
 
static char * run_simple_query (const char *sql)
 
static void run_simple_command (const char *sql)
 
void libpqConnect (const char *connstr)
 
XLogRecPtr libpqGetCurrentXlogInsertLocation (void)
 
void libpqProcessFileList (void)
 
char * libpqGetFile (const char *filename, size_t *filesize)
 
static void fetch_file_range (const char *path, uint64 begin, uint64 end)
 
void libpq_executeFileMap (filemap_t *map)
 

Variables

PGconnconn = NULL
 

Macro Definition Documentation

◆ CHUNKSIZE

#define CHUNKSIZE   1000000

Definition at line 36 of file libpq_fetch.c.

Referenced by fetch_file_range().

Function Documentation

◆ execute_pagemap()

static void execute_pagemap ( datapagemap_t pagemap,
const char *  path 
)
static

Definition at line 521 of file libpq_fetch.c.

References datapagemap_iterate(), datapagemap_next(), fetch_file_range(), and pg_free().

Referenced by libpq_executeFileMap().

522 {
524  BlockNumber blkno;
525  off_t offset;
526 
527  iter = datapagemap_iterate(pagemap);
528  while (datapagemap_next(iter, &blkno))
529  {
530  offset = blkno * BLCKSZ;
531 
532  fetch_file_range(path, offset, offset + BLCKSZ);
533  }
534  pg_free(iter);
535 }
uint32 BlockNumber
Definition: block.h:31
bool datapagemap_next(datapagemap_iterator_t *iter, BlockNumber *blkno)
Definition: datapagemap.c:88
datapagemap_iterator_t * datapagemap_iterate(datapagemap_t *map)
Definition: datapagemap.c:76
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static void fetch_file_range(const char *path, uint64 begin, uint64 end)
Definition: libpq_fetch.c:408

◆ fetch_file_range()

static void fetch_file_range ( const char *  path,
uint64  begin,
uint64  end 
)
static

Definition at line 408 of file libpq_fetch.c.

References CHUNKSIZE, MAXPGPATH, pg_fatal, PQerrorMessage(), PQputCopyData(), snprintf, and UINT64_FORMAT.

Referenced by execute_pagemap(), and libpq_executeFileMap().

409 {
410  char linebuf[MAXPGPATH + 23];
411 
412  /* Split the range into CHUNKSIZE chunks */
413  while (end - begin > 0)
414  {
415  unsigned int len;
416 
417  /* Fine as long as CHUNKSIZE is not bigger than UINT32_MAX */
418  if (end - begin > CHUNKSIZE)
419  len = CHUNKSIZE;
420  else
421  len = (unsigned int) (end - begin);
422 
423  snprintf(linebuf, sizeof(linebuf), "%s\t" UINT64_FORMAT "\t%u\n", path, begin, len);
424 
425  if (PQputCopyData(conn, linebuf, strlen(linebuf)) != 1)
426  pg_fatal("could not send COPY data: %s",
428 
429  begin += len;
430  }
431 }
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2318
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6596
#define pg_fatal(...)
Definition: pg_rewind.h:43
#define CHUNKSIZE
Definition: libpq_fetch.c:36
#define MAXPGPATH
PGconn * conn
Definition: libpq_fetch.c:27
#define snprintf
Definition: port.h:192
#define UINT64_FORMAT
Definition: c.h:401

◆ libpq_executeFileMap()

void libpq_executeFileMap ( filemap_t map)

Definition at line 437 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(), run_simple_command(), and truncate_target_file().

Referenced by executeFileMap().

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

◆ libpqConnect()

void libpqConnect ( const char *  connstr)

Definition at line 44 of file libpq_fetch.c.

References ALWAYS_SECURE_SEARCH_PATH_SQL, CONNECTION_BAD, pg_fatal, pg_free(), pg_log_info, PGRES_TUPLES_OK, PQclear(), PQconnectdb(), PQerrorMessage(), PQexec(), PQresultErrorMessage(), PQresultStatus(), PQstatus(), run_simple_command(), run_simple_query(), showprogress, and generate_unaccent_rules::str.

Referenced by main().

45 {
46  char *str;
47  PGresult *res;
48 
51  pg_fatal("could not connect to server: %s",
53 
54  if (showprogress)
55  pg_log_info("connected to server");
56 
57  /* disable all types of timeouts */
58  run_simple_command("SET statement_timeout = 0");
59  run_simple_command("SET lock_timeout = 0");
60  run_simple_command("SET idle_in_transaction_session_timeout = 0");
61 
63  if (PQresultStatus(res) != PGRES_TUPLES_OK)
64  pg_fatal("could not clear search_path: %s",
66  PQclear(res);
67 
68  /*
69  * Check that the server is not in hot standby mode. There is no
70  * fundamental reason that couldn't be made to work, but it doesn't
71  * currently because we use a temporary table. Better to check for it
72  * explicitly than error out, for a better error message.
73  */
74  str = run_simple_query("SELECT pg_is_in_recovery()");
75  if (strcmp(str, "f") != 0)
76  pg_fatal("source server must not be in recovery mode");
77  pg_free(str);
78 
79  /*
80  * Also check that full_page_writes is enabled. We can get torn pages if
81  * a page is modified while we read it with pg_read_binary_file(), and we
82  * rely on full page images to fix them.
83  */
84  str = run_simple_query("SHOW full_page_writes");
85  if (strcmp(str, "on") != 0)
86  pg_fatal("full_page_writes must be enabled in the source server");
87  pg_free(str);
88 
89  /*
90  * Although we don't do any "real" updates, we do work with a temporary
91  * table. We don't care about synchronous commit for that. It doesn't
92  * otherwise matter much, but if the server is using synchronous
93  * replication, and replication isn't working for some reason, we don't
94  * want to get stuck, waiting for it to start working again.
95  */
96  run_simple_command("SET synchronous_commit = off");
97 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6596
#define pg_fatal(...)
Definition: pg_rewind.h:43
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
static char * run_simple_query(const char *sql)
Definition: libpq_fetch.c:104
PGconn * conn
Definition: libpq_fetch.c:27
static bool showprogress
Definition: pg_basebackup.c:89
static void run_simple_command(const char *sql)
Definition: libpq_fetch.c:131
void PQclear(PGresult *res)
Definition: fe-exec.c:695
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:2709
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6543
#define pg_log_info(...)
Definition: logging.h:87
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:678
static char * connstr
Definition: pg_dumpall.c:62

◆ libpqGetCurrentXlogInsertLocation()

XLogRecPtr libpqGetCurrentXlogInsertLocation ( void  )

Definition at line 148 of file libpq_fetch.c.

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

Referenced by main().

149 {
150  XLogRecPtr result;
151  uint32 hi;
152  uint32 lo;
153  char *val;
154 
155  val = run_simple_query("SELECT pg_current_wal_insert_lsn()");
156 
157  if (sscanf(val, "%X/%X", &hi, &lo) != 2)
158  pg_fatal("unrecognized result \"%s\" for current WAL insert location", val);
159 
160  result = ((uint64) hi) << 32 | lo;
161 
162  pg_free(val);
163 
164  return result;
165 }
#define pg_fatal(...)
Definition: pg_rewind.h:43
static char * run_simple_query(const char *sql)
Definition: libpq_fetch.c:104
unsigned int uint32
Definition: c.h:358
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void pg_free(void *ptr)
Definition: fe_memutils.c:105
long val
Definition: informix.c:684

◆ libpqGetFile()

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

Definition at line 365 of file libpq_fetch.c.

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

Referenced by fetchFile().

366 {
367  PGresult *res;
368  char *result;
369  int len;
370  const char *paramValues[1];
371 
372  paramValues[0] = filename;
373  res = PQexecParams(conn, "SELECT pg_read_binary_file($1)",
374  1, NULL, paramValues, NULL, NULL, 1);
375 
376  if (PQresultStatus(res) != PGRES_TUPLES_OK)
377  pg_fatal("could not fetch remote file \"%s\": %s",
379 
380  /* sanity check the result set */
381  if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
382  pg_fatal("unexpected result set while fetching remote file \"%s\"",
383  filename);
384 
385  /* Read result to local variables */
386  len = PQgetlength(res, 0, 0);
387  result = pg_malloc(len + 1);
388  memcpy(result, PQgetvalue(res, 0, 0), len);
389  result[len] = '\0';
390 
391  PQclear(res);
392 
393  pg_log_debug("fetched file \"%s\", length %d", filename, len);
394 
395  if (filesize)
396  *filesize = len;
397  return result;
398 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3175
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define pg_fatal(...)
Definition: pg_rewind.h:43
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
#define pg_log_debug(...)
Definition: logging.h:91
PGconn * conn
Definition: libpq_fetch.c:27
void PQclear(PGresult *res)
Definition: fe-exec.c:695
static char * filename
Definition: pg_dumpall.c:91
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2709
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:1954
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3189

◆ libpqProcessFileList()

void libpqProcessFileList ( void  )

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

172 {
173  PGresult *res;
174  const char *sql;
175  int i;
176 
177  /*
178  * Create a recursive directory listing of the whole data directory.
179  *
180  * The WITH RECURSIVE part does most of the work. The second part gets the
181  * targets of the symlinks in pg_tblspc directory.
182  *
183  * XXX: There is no backend function to get a symbolic link's target in
184  * general, so if the admin has put any custom symbolic links in the data
185  * directory, they won't be copied correctly.
186  */
187  sql =
188  "WITH RECURSIVE files (path, filename, size, isdir) AS (\n"
189  " SELECT '' AS path, filename, size, isdir FROM\n"
190  " (SELECT pg_ls_dir('.', true, false) AS filename) AS fn,\n"
191  " pg_stat_file(fn.filename, true) AS this\n"
192  " UNION ALL\n"
193  " SELECT parent.path || parent.filename || '/' AS path,\n"
194  " fn, this.size, this.isdir\n"
195  " FROM files AS parent,\n"
196  " pg_ls_dir(parent.path || parent.filename, true, false) AS fn,\n"
197  " pg_stat_file(parent.path || parent.filename || '/' || fn, true) AS this\n"
198  " WHERE parent.isdir = 't'\n"
199  ")\n"
200  "SELECT path || filename, size, isdir,\n"
201  " pg_tablespace_location(pg_tablespace.oid) AS link_target\n"
202  "FROM files\n"
203  "LEFT OUTER JOIN pg_tablespace ON files.path = 'pg_tblspc/'\n"
204  " AND oid::text = files.filename\n";
205  res = PQexec(conn, sql);
206 
207  if (PQresultStatus(res) != PGRES_TUPLES_OK)
208  pg_fatal("could not fetch file list: %s",
209  PQresultErrorMessage(res));
210 
211  /* sanity check the result set */
212  if (PQnfields(res) != 4)
213  pg_fatal("unexpected result set while fetching file list");
214 
215  /* Read result to local variables */
216  for (i = 0; i < PQntuples(res); i++)
217  {
218  char *path = PQgetvalue(res, i, 0);
219  int64 filesize = atol(PQgetvalue(res, i, 1));
220  bool isdir = (strcmp(PQgetvalue(res, i, 2), "t") == 0);
221  char *link_target = PQgetvalue(res, i, 3);
223 
224  if (PQgetisnull(res, 0, 1))
225  {
226  /*
227  * The file was removed from the server while the query was
228  * running. Ignore it.
229  */
230  continue;
231  }
232 
233  if (link_target[0])
234  type = FILE_TYPE_SYMLINK;
235  else if (isdir)
236  type = FILE_TYPE_DIRECTORY;
237  else
238  type = FILE_TYPE_REGULAR;
239 
240  process_source_file(path, type, filesize, link_target);
241  }
242  PQclear(res);
243 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2778
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
#define pg_fatal(...)
Definition: pg_rewind.h:43
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
PGconn * conn
Definition: libpq_fetch.c:27
void PQclear(PGresult *res)
Definition: fe-exec.c:695
void process_source_file(const char *path, file_type_t type, size_t newsize, const char *link_target)
Definition: filemap.c:136
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2709
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940
file_type_t
Definition: filemap.h:35
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3189

◆ receiveFileChunks()

static void receiveFileChunks ( const char *  sql)
static

Definition at line 256 of file libpq_fetch.c.

References filename, open_target_file(), pg_fatal, pg_free(), pg_log_debug, pg_malloc(), pg_ntoh64, PGRES_SINGLE_TUPLE, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQfformat(), PQftype(), PQgetisnull(), PQgetlength(), PQgetResult(), PQgetvalue(), PQnfields(), PQntuples(), PQresultErrorMessage(), PQresultStatus(), PQsendQueryParams(), PQsetSingleRowMode(), remove_target_file(), and write_target_range().

Referenced by libpq_executeFileMap().

257 {
258  PGresult *res;
259 
260  if (PQsendQueryParams(conn, sql, 0, NULL, NULL, NULL, NULL, 1) != 1)
261  pg_fatal("could not send query: %s", PQerrorMessage(conn));
262 
263  pg_log_debug("getting file chunks");
264 
265  if (PQsetSingleRowMode(conn) != 1)
266  pg_fatal("could not set libpq connection to single row mode");
267 
268  while ((res = PQgetResult(conn)) != NULL)
269  {
270  char *filename;
271  int filenamelen;
272  int64 chunkoff;
273  int chunksize;
274  char *chunk;
275 
276  switch (PQresultStatus(res))
277  {
278  case PGRES_SINGLE_TUPLE:
279  break;
280 
281  case PGRES_TUPLES_OK:
282  PQclear(res);
283  continue; /* final zero-row result */
284 
285  default:
286  pg_fatal("unexpected result while fetching remote files: %s",
287  PQresultErrorMessage(res));
288  }
289 
290  /* sanity check the result set */
291  if (PQnfields(res) != 3 || PQntuples(res) != 1)
292  pg_fatal("unexpected result set size while fetching remote files");
293 
294  if (PQftype(res, 0) != TEXTOID ||
295  PQftype(res, 1) != INT8OID ||
296  PQftype(res, 2) != BYTEAOID)
297  {
298  pg_fatal("unexpected data types in result set while fetching remote files: %u %u %u",
299  PQftype(res, 0), PQftype(res, 1), PQftype(res, 2));
300  }
301 
302  if (PQfformat(res, 0) != 1 &&
303  PQfformat(res, 1) != 1 &&
304  PQfformat(res, 2) != 1)
305  {
306  pg_fatal("unexpected result format while fetching remote files");
307  }
308 
309  if (PQgetisnull(res, 0, 0) ||
310  PQgetisnull(res, 0, 1))
311  {
312  pg_fatal("unexpected null values in result while fetching remote files");
313  }
314 
315  if (PQgetlength(res, 0, 1) != sizeof(int64))
316  pg_fatal("unexpected result length while fetching remote files");
317 
318  /* Read result set to local variables */
319  memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int64));
320  chunkoff = pg_ntoh64(chunkoff);
321  chunksize = PQgetlength(res, 0, 2);
322 
323  filenamelen = PQgetlength(res, 0, 0);
324  filename = pg_malloc(filenamelen + 1);
325  memcpy(filename, PQgetvalue(res, 0, 0), filenamelen);
326  filename[filenamelen] = '\0';
327 
328  chunk = PQgetvalue(res, 0, 2);
329 
330  /*
331  * If a file has been deleted on the source, remove it on the target
332  * as well. Note that multiple unlink() calls may happen on the same
333  * file if multiple data chunks are associated with it, hence ignore
334  * unconditionally anything missing. If this file is not a relation
335  * data file, then it has been already truncated when creating the
336  * file chunk list at the previous execution of the filemap.
337  */
338  if (PQgetisnull(res, 0, 2))
339  {
340  pg_log_debug("received null value for chunk for file \"%s\", file has been deleted",
341  filename);
342  remove_target_file(filename, true);
343  pg_free(filename);
344  PQclear(res);
345  continue;
346  }
347 
348  pg_log_debug("received chunk for file \"%s\", offset %lld, size %d",
349  filename, (long long int) chunkoff, chunksize);
350 
351  open_target_file(filename, false);
352 
353  write_target_range(chunk, chunkoff, chunksize);
354 
355  pg_free(filename);
356 
357  PQclear(res);
358  }
359 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3175
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2778
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6596
void open_target_file(const char *path, bool trunc)
Definition: file_ops.c:42
void write_target_range(char *buf, off_t begin, size_t size)
Definition: file_ops.c:83
int PQsendQueryParams(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:1286
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void remove_target_file(const char *path, bool missing_ok)
Definition: file_ops.c:172
#define pg_fatal(...)
Definition: pg_rewind.h:43
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
#define pg_log_debug(...)
Definition: logging.h:91
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3008
int PQsetSingleRowMode(PGconn *conn)
Definition: fe-exec.c:1678
PGconn * conn
Definition: libpq_fetch.c:27
void PQclear(PGresult *res)
Definition: fe-exec.c:695
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static char * filename
Definition: pg_dumpall.c:91
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2709
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3189
int PQfformat(const PGresult *res, int field_num)
Definition: fe-exec.c:2997
#define pg_ntoh64(x)
Definition: pg_bswap.h:126
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1779

◆ run_simple_command()

static void run_simple_command ( const char *  sql)
static

Definition at line 131 of file libpq_fetch.c.

References pg_fatal, PGRES_COMMAND_OK, PQclear(), PQexec(), PQresultErrorMessage(), and PQresultStatus().

Referenced by libpq_executeFileMap(), and libpqConnect().

132 {
133  PGresult *res;
134 
135  res = PQexec(conn, sql);
136 
137  if (PQresultStatus(res) != PGRES_COMMAND_OK)
138  pg_fatal("error running query (%s) in source server: %s",
139  sql, PQresultErrorMessage(res));
140 
141  PQclear(res);
142 }
#define pg_fatal(...)
Definition: pg_rewind.h:43
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
PGconn * conn
Definition: libpq_fetch.c:27
void PQclear(PGresult *res)
Definition: fe-exec.c:695
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2709
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940

◆ run_simple_query()

static char * run_simple_query ( const char *  sql)
static

Definition at line 104 of file libpq_fetch.c.

References pg_fatal, pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQexec(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), PQresultErrorMessage(), and PQresultStatus().

Referenced by libpqConnect(), and libpqGetCurrentXlogInsertLocation().

105 {
106  PGresult *res;
107  char *result;
108 
109  res = PQexec(conn, sql);
110 
111  if (PQresultStatus(res) != PGRES_TUPLES_OK)
112  pg_fatal("error running query (%s) in source server: %s",
113  sql, PQresultErrorMessage(res));
114 
115  /* sanity check the result set */
116  if (PQnfields(res) != 1 || PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
117  pg_fatal("unexpected result set from query");
118 
119  result = pg_strdup(PQgetvalue(res, 0, 0));
120 
121  PQclear(res);
122 
123  return result;
124 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2778
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
#define pg_fatal(...)
Definition: pg_rewind.h:43
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PGconn * conn
Definition: libpq_fetch.c:27
void PQclear(PGresult *res)
Definition: fe-exec.c:695
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2709
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3189

Variable Documentation

◆ conn

PGconn* conn = NULL

Definition at line 27 of file libpq_fetch.c.