PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
libpq_fetch.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "pg_rewind.h"
#include "datapagemap.h"
#include "fetch.h"
#include "file_ops.h"
#include "filemap.h"
#include "logging.h"
#include "libpq-fe.h"
#include "catalog/catalog.h"
#include "catalog/pg_type.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)
 
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, unsigned int begin, unsigned int end)
 
void libpq_executeFileMap (filemap_t *map)
 

Variables

static PGconnconn = NULL
 

Macro Definition Documentation

#define CHUNKSIZE   1000000

Definition at line 41 of file libpq_fetch.c.

Referenced by fetch_file_range().

Function Documentation

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

Definition at line 500 of file libpq_fetch.c.

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

Referenced by libpq_executeFileMap().

501 {
503  BlockNumber blkno;
504  off_t offset;
505 
506  iter = datapagemap_iterate(pagemap);
507  while (datapagemap_next(iter, &blkno))
508  {
509  offset = blkno * BLCKSZ;
510 
511  fetch_file_range(path, offset, offset + BLCKSZ);
512  }
513  pg_free(iter);
514 }
static void fetch_file_range(const char *path, unsigned int begin, unsigned int end)
Definition: libpq_fetch.c:383
uint32 BlockNumber
Definition: block.h:31
bool datapagemap_next(datapagemap_iterator_t *iter, BlockNumber *blkno)
Definition: datapagemap.c:87
datapagemap_iterator_t * datapagemap_iterate(datapagemap_t *map)
Definition: datapagemap.c:75
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static void fetch_file_range ( const char *  path,
unsigned int  begin,
unsigned int  end 
)
static

Definition at line 383 of file libpq_fetch.c.

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

Referenced by execute_pagemap(), and libpq_executeFileMap().

384 {
385  char linebuf[MAXPGPATH + 23];
386 
387  /* Split the range into CHUNKSIZE chunks */
388  while (end - begin > 0)
389  {
390  unsigned int len;
391 
392  if (end - begin > CHUNKSIZE)
393  len = CHUNKSIZE;
394  else
395  len = end - begin;
396 
397  snprintf(linebuf, sizeof(linebuf), "%s\t%u\t%u\n", path, begin, len);
398 
399  if (PQputCopyData(conn, linebuf, strlen(linebuf)) != 1)
400  pg_fatal("could not send COPY data: %s",
402 
403  begin += len;
404  }
405 }
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2221
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define CHUNKSIZE
Definition: libpq_fetch.c:41
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
#define MAXPGPATH
static PGconn * conn
Definition: libpq_fetch.c:32
void libpq_executeFileMap ( filemap_t map)

Definition at line 411 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, NULL, 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().

412 {
413  file_entry_t *entry;
414  const char *sql;
415  PGresult *res;
416  int i;
417 
418  /*
419  * First create a temporary table, and load it with the blocks that we
420  * need to fetch.
421  */
422  sql = "CREATE TEMPORARY TABLE fetchchunks(path text, begin int4, len int4);";
423  res = PQexec(conn, sql);
424 
425  if (PQresultStatus(res) != PGRES_COMMAND_OK)
426  pg_fatal("could not create temporary table: %s",
427  PQresultErrorMessage(res));
428  PQclear(res);
429 
430  sql = "COPY fetchchunks FROM STDIN";
431  res = PQexec(conn, sql);
432 
433  if (PQresultStatus(res) != PGRES_COPY_IN)
434  pg_fatal("could not send file list: %s",
435  PQresultErrorMessage(res));
436  PQclear(res);
437 
438  for (i = 0; i < map->narray; i++)
439  {
440  entry = map->array[i];
441 
442  /* If this is a relation file, copy the modified blocks */
443  execute_pagemap(&entry->pagemap, entry->path);
444 
445  switch (entry->action)
446  {
447  case FILE_ACTION_NONE:
448  /* nothing else to do */
449  break;
450 
451  case FILE_ACTION_COPY:
452  /* Truncate the old file out of the way, if any */
453  open_target_file(entry->path, true);
454  fetch_file_range(entry->path, 0, entry->newsize);
455  break;
456 
458  truncate_target_file(entry->path, entry->newsize);
459  break;
460 
462  fetch_file_range(entry->path, entry->oldsize, entry->newsize);
463  break;
464 
465  case FILE_ACTION_REMOVE:
466  remove_target(entry);
467  break;
468 
469  case FILE_ACTION_CREATE:
470  create_target(entry);
471  break;
472  }
473  }
474 
475  if (PQputCopyEnd(conn, NULL) != 1)
476  pg_fatal("could not send end-of-COPY: %s",
478 
479  while ((res = PQgetResult(conn)) != NULL)
480  {
481  if (PQresultStatus(res) != PGRES_COMMAND_OK)
482  pg_fatal("unexpected result while sending file list: %s",
483  PQresultErrorMessage(res));
484  PQclear(res);
485  }
486 
487  /*
488  * We've now copied the list of file ranges that we need to fetch to the
489  * temporary table. Now, actually fetch all of those ranges.
490  */
491  sql =
492  "SELECT path, begin, \n"
493  " pg_read_binary_file(path, begin, len, true) AS chunk\n"
494  "FROM fetchchunks\n";
495 
496  receiveFileChunks(sql);
497 }
static void fetch_file_range(const char *path, unsigned int begin, unsigned int end)
Definition: libpq_fetch.c:383
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
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:500
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2288
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:2596
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:234
size_t oldsize
Definition: filemap.h:50
static PGconn * conn
Definition: libpq_fetch.c:32
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
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:2612
int i
Definition: filemap.h:42
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
void create_target(file_entry_t *entry)
Definition: file_ops.c:147
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1702
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:6011
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
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:650
void pg_free(void *ptr)
Definition: fe_memutils.c:105
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2612
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5958
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:526
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(), result, run_simple_query(), and val.

Referenced by main().

127 {
129  uint32 hi;
130  uint32 lo;
131  char *val;
132 
133  val = run_simple_query("SELECT pg_current_wal_insert_location()");
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 }
return result
Definition: formatting.c:1618
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:268
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 340 of file libpq_fetch.c.

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

Referenced by fetchFile().

341 {
342  PGresult *res;
343  char *result;
344  int len;
345  const char *paramValues[1];
346 
347  paramValues[0] = filename;
348  res = PQexecParams(conn, "SELECT pg_read_binary_file($1)",
349  1, NULL, paramValues, NULL, NULL, 1);
350 
351  if (PQresultStatus(res) != PGRES_TUPLES_OK)
352  pg_fatal("could not fetch remote file \"%s\": %s",
354 
355  /* sanity check the result set */
356  if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
357  pg_fatal("unexpected result set while fetching remote file \"%s\"\n",
358  filename);
359 
360  /* Read result to local variables */
361  len = PQgetlength(res, 0, 0);
362  result = pg_malloc(len + 1);
363  memcpy(result, PQgetvalue(res, 0, 0), len);
364  result[len] = '\0';
365 
366  PQclear(res);
367 
368  pg_log(PG_DEBUG, "fetched file \"%s\", length %d\n", filename, len);
369 
370  if (filesize)
371  *filesize = len;
372  return result;
373 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3078
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
return result
Definition: formatting.c:1618
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
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:650
#define NULL
Definition: c.h:229
static char * filename
Definition: pg_dumpall.c:87
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2612
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:1860
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092
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  int filesize = atoi(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:2681
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static PGconn * conn
Definition: libpq_fetch.c:32
void PQclear(PGresult *res)
Definition: fe-exec.c:650
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:2612
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
file_type_t
Definition: filemap.h:35
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092
static void receiveFileChunks ( const char *  sql)
static

Definition at line 234 of file libpq_fetch.c.

References BYTEAOID, filename, INT4OID, NULL, open_target_file(), PG_DEBUG, pg_fatal(), pg_free(), pg_log(), pg_malloc(), PGRES_SINGLE_TUPLE, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQfformat(), PQftype(), PQgetisnull(), PQgetlength(), PQgetResult(), PQgetvalue(), PQnfields(), PQntuples(), PQresultErrorMessage(), PQresultStatus(), PQsendQueryParams(), PQsetSingleRowMode(), TEXTOID, and write_target_range().

Referenced by libpq_executeFileMap().

235 {
236  PGresult *res;
237 
238  if (PQsendQueryParams(conn, sql, 0, NULL, NULL, NULL, NULL, 1) != 1)
239  pg_fatal("could not send query: %s", PQerrorMessage(conn));
240 
241  pg_log(PG_DEBUG, "getting file chunks\n");
242 
243  if (PQsetSingleRowMode(conn) != 1)
244  pg_fatal("could not set libpq connection to single row mode\n");
245 
246  while ((res = PQgetResult(conn)) != NULL)
247  {
248  char *filename;
249  int filenamelen;
250  int chunkoff;
251  int chunksize;
252  char *chunk;
253 
254  switch (PQresultStatus(res))
255  {
256  case PGRES_SINGLE_TUPLE:
257  break;
258 
259  case PGRES_TUPLES_OK:
260  PQclear(res);
261  continue; /* final zero-row result */
262 
263  default:
264  pg_fatal("unexpected result while fetching remote files: %s",
265  PQresultErrorMessage(res));
266  }
267 
268  /* sanity check the result set */
269  if (PQnfields(res) != 3 || PQntuples(res) != 1)
270  pg_fatal("unexpected result set size while fetching remote files\n");
271 
272  if (PQftype(res, 0) != TEXTOID &&
273  PQftype(res, 1) != INT4OID &&
274  PQftype(res, 2) != BYTEAOID)
275  {
276  pg_fatal("unexpected data types in result set while fetching remote files: %u %u %u\n",
277  PQftype(res, 0), PQftype(res, 1), PQftype(res, 2));
278  }
279 
280  if (PQfformat(res, 0) != 1 &&
281  PQfformat(res, 1) != 1 &&
282  PQfformat(res, 2) != 1)
283  {
284  pg_fatal("unexpected result format while fetching remote files\n");
285  }
286 
287  if (PQgetisnull(res, 0, 0) ||
288  PQgetisnull(res, 0, 1))
289  {
290  pg_fatal("unexpected null values in result while fetching remote files\n");
291  }
292 
293  if (PQgetlength(res, 0, 1) != sizeof(int32))
294  pg_fatal("unexpected result length while fetching remote files\n");
295 
296  /* Read result set to local variables */
297  memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int32));
298  chunkoff = ntohl(chunkoff);
299  chunksize = PQgetlength(res, 0, 2);
300 
301  filenamelen = PQgetlength(res, 0, 0);
302  filename = pg_malloc(filenamelen + 1);
303  memcpy(filename, PQgetvalue(res, 0, 0), filenamelen);
304  filename[filenamelen] = '\0';
305 
306  chunk = PQgetvalue(res, 0, 2);
307 
308  /*
309  * It's possible that the file was deleted on remote side after we
310  * created the file map. In this case simply ignore it, as if it was
311  * not there in the first place, and move on.
312  */
313  if (PQgetisnull(res, 0, 2))
314  {
316  "received null value for chunk for file \"%s\", file has been deleted\n",
317  filename);
318  pg_free(filename);
319  PQclear(res);
320  continue;
321  }
322 
323  pg_log(PG_DEBUG, "received chunk for file \"%s\", offset %d, size %d\n",
324  filename, chunkoff, chunksize);
325 
326  open_target_file(filename, false);
327 
328  write_target_range(chunk, chunkoff, chunksize);
329 
330  pg_free(filename);
331 
332  PQclear(res);
333  }
334 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3078
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2681
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
void open_target_file(const char *path, bool trunc)
Definition: file_ops.c:43
void write_target_range(char *buf, off_t begin, size_t size)
Definition: file_ops.c:84
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:1183
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define TEXTOID
Definition: pg_type.h:324
#define INT4OID
Definition: pg_type.h:316
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
signed int int32
Definition: c.h:256
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:2911
int PQsetSingleRowMode(PGconn *conn)
Definition: fe-exec.c:1604
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:650
#define NULL
Definition: c.h:229
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define BYTEAOID
Definition: pg_type.h:292
static char * filename
Definition: pg_dumpall.c:87
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2612
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092
int PQfformat(const PGresult *res, int field_num)
Definition: fe-exec.c:2900
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1702
static char * run_simple_query ( const char *  sql)
static

Definition at line 100 of file libpq_fetch.c.

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

Referenced by libpqConnect(), and libpqGetCurrentXlogInsertLocation().

101 {
102  PGresult *res;
103  char *result;
104 
105  res = PQexec(conn, sql);
106 
107  if (PQresultStatus(res) != PGRES_TUPLES_OK)
108  pg_fatal("error running query (%s) in source server: %s",
109  sql, PQresultErrorMessage(res));
110 
111  /* sanity check the result set */
112  if (PQnfields(res) != 1 || PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
113  pg_fatal("unexpected result set from query\n");
114 
115  result = pg_strdup(PQgetvalue(res, 0, 0));
116 
117  PQclear(res);
118 
119  return result;
120 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2681
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
return result
Definition: formatting.c:1618
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static PGconn * conn
Definition: libpq_fetch.c:32
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2612
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092

Variable Documentation

PGconn* conn = NULL
static

Definition at line 32 of file libpq_fetch.c.