PostgreSQL Source Code  git master
streamutil.h File Reference
#include "libpq-fe.h"
#include "access/xlogdefs.h"
#include "datatype/timestamp.h"
Include dependency graph for streamutil.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PGconnGetConnection (void)
 
bool CreateReplicationSlot (PGconn *conn, const char *slot_name, const char *plugin, bool is_temporary, bool is_physical, bool reserve_wal, bool slot_exists_ok)
 
bool DropReplicationSlot (PGconn *conn, const char *slot_name)
 
bool RunIdentifySystem (PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
 
bool RetrieveWalSegSize (PGconn *conn)
 
TimestampTz feGetCurrentTimestamp (void)
 
void feTimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
bool feTimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
void fe_sendint64 (int64 i, char *buf)
 
int64 fe_recvint64 (char *buf)
 

Variables

const char * progname
 
char * connection_string
 
char * dbhost
 
char * dbuser
 
char * dbport
 
char * dbname
 
int dbgetpassword
 
uint32 WalSegSz
 
PGconnconn
 

Function Documentation

◆ CreateReplicationSlot()

bool CreateReplicationSlot ( PGconn conn,
const char *  slot_name,
const char *  plugin,
bool  is_temporary,
bool  is_physical,
bool  reserve_wal,
bool  slot_exists_ok 
)

Definition at line 397 of file streamutil.c.

References _, appendPQExpBuffer(), Assert, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), ERRCODE_DUPLICATE_OBJECT, PG_DIAG_SQLSTATE, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQnfields(), PQntuples(), PQresultErrorField(), PQresultStatus(), PQserverVersion(), and progname.

400 {
401  PQExpBuffer query;
402  PGresult *res;
403 
404  query = createPQExpBuffer();
405 
406  Assert((is_physical && plugin == NULL) ||
407  (!is_physical && plugin != NULL));
408  Assert(slot_name != NULL);
409 
410  /* Build query */
411  appendPQExpBuffer(query, "CREATE_REPLICATION_SLOT \"%s\"", slot_name);
412  if (is_temporary)
413  appendPQExpBuffer(query, " TEMPORARY");
414  if (is_physical)
415  {
416  appendPQExpBuffer(query, " PHYSICAL");
417  if (reserve_wal)
418  appendPQExpBuffer(query, " RESERVE_WAL");
419  }
420  else
421  {
422  appendPQExpBuffer(query, " LOGICAL \"%s\"", plugin);
423  if (PQserverVersion(conn) >= 100000)
424  /* pg_recvlogical doesn't use an exported snapshot, so suppress */
425  appendPQExpBuffer(query, " NOEXPORT_SNAPSHOT");
426  }
427 
428  res = PQexec(conn, query->data);
429  if (PQresultStatus(res) != PGRES_TUPLES_OK)
430  {
431  const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
432 
433  if (slot_exists_ok &&
434  sqlstate &&
435  strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
436  {
437  destroyPQExpBuffer(query);
438  PQclear(res);
439  return true;
440  }
441  else
442  {
443  fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
444  progname, query->data, PQerrorMessage(conn));
445 
446  destroyPQExpBuffer(query);
447  PQclear(res);
448  return false;
449  }
450  }
451 
452  if (PQntuples(res) != 1 || PQnfields(res) != 4)
453  {
454  fprintf(stderr,
455  _("%s: could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields\n"),
456  progname, slot_name,
457  PQntuples(res), PQnfields(res), 1, 4);
458 
459  destroyPQExpBuffer(query);
460  PQclear(res);
461  return false;
462  }
463 
464  destroyPQExpBuffer(query);
465  PQclear(res);
466  return true;
467 }
static const char * plugin
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6096
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
static bool slot_exists_ok
Definition: pg_receivewal.c:41
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:671
const char * progname
Definition: streamutil.c:37
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:2709
#define Assert(condition)
Definition: c.h:670
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30
#define _(x)
Definition: elog.c:84

◆ DropReplicationSlot()

bool DropReplicationSlot ( PGconn conn,
const char *  slot_name 
)

Definition at line 474 of file streamutil.c.

References _, appendPQExpBuffer(), Assert, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQexec(), PQnfields(), PQntuples(), PQresultStatus(), and progname.

475 {
476  PQExpBuffer query;
477  PGresult *res;
478 
479  Assert(slot_name != NULL);
480 
481  query = createPQExpBuffer();
482 
483  /* Build query */
484  appendPQExpBuffer(query, "DROP_REPLICATION_SLOT \"%s\"",
485  slot_name);
486  res = PQexec(conn, query->data);
487  if (PQresultStatus(res) != PGRES_COMMAND_OK)
488  {
489  fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
490  progname, query->data, PQerrorMessage(conn));
491 
492  destroyPQExpBuffer(query);
493  PQclear(res);
494  return false;
495  }
496 
497  if (PQntuples(res) != 0 || PQnfields(res) != 0)
498  {
499  fprintf(stderr,
500  _("%s: could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields\n"),
501  progname, slot_name,
502  PQntuples(res), PQnfields(res), 0, 0);
503 
504  destroyPQExpBuffer(query);
505  PQclear(res);
506  return false;
507  }
508 
509  destroyPQExpBuffer(query);
510  PQclear(res);
511  return true;
512 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:671
const char * progname
Definition: streamutil.c:37
#define Assert(condition)
Definition: c.h:670
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define _(x)
Definition: elog.c:84

◆ fe_recvint64()

int64 fe_recvint64 ( char *  buf)

Definition at line 585 of file streamutil.c.

References pg_ntoh64.

Referenced by ProcessXLogDataMsg(), and StreamLogicalLog().

586 {
587  uint64 n64;
588 
589  memcpy(&n64, buf, sizeof(n64));
590 
591  return pg_ntoh64(n64);
592 }
static char * buf
Definition: pg_test_fsync.c:67
#define pg_ntoh64(x)
Definition: pg_bswap.h:126

◆ fe_sendint64()

void fe_sendint64 ( int64  i,
char *  buf 
)

Definition at line 574 of file streamutil.c.

References pg_hton64.

Referenced by sendFeedback().

575 {
576  uint64 n64 = pg_hton64(i);
577 
578  memcpy(buf, &n64, sizeof(n64));
579 }
#define pg_hton64(x)
Definition: pg_bswap.h:122
static char * buf
Definition: pg_test_fsync.c:67
int i

◆ feGetCurrentTimestamp()

TimestampTz feGetCurrentTimestamp ( void  )

Definition at line 520 of file streamutil.c.

References gettimeofday(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by flushAndSendFeedback(), HandleCopyStream(), ProcessKeepaliveMsg(), and StreamLogicalLog().

521 {
522  TimestampTz result;
523  struct timeval tp;
524 
525  gettimeofday(&tp, NULL);
526 
527  result = (TimestampTz) tp.tv_sec -
529  result = (result * USECS_PER_SEC) + tp.tv_usec;
530 
531  return result;
532 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ feTimestampDifference()

void feTimestampDifference ( TimestampTz  start_time,
TimestampTz  stop_time,
long *  secs,
int *  microsecs 
)

Definition at line 539 of file streamutil.c.

References start_time, and USECS_PER_SEC.

Referenced by CalculateCopyStreamSleeptime(), and StreamLogicalLog().

541 {
542  TimestampTz diff = stop_time - start_time;
543 
544  if (diff <= 0)
545  {
546  *secs = 0;
547  *microsecs = 0;
548  }
549  else
550  {
551  *secs = (long) (diff / USECS_PER_SEC);
552  *microsecs = (int) (diff % USECS_PER_SEC);
553  }
554 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static time_t start_time
Definition: pg_ctl.c:95

◆ feTimestampDifferenceExceeds()

bool feTimestampDifferenceExceeds ( TimestampTz  start_time,
TimestampTz  stop_time,
int  msec 
)

Definition at line 561 of file streamutil.c.

References start_time.

Referenced by HandleCopyStream(), and StreamLogicalLog().

564 {
565  TimestampTz diff = stop_time - start_time;
566 
567  return (diff >= msec * INT64CONST(1000));
568 }
int64 TimestampTz
Definition: timestamp.h:39
static time_t start_time
Definition: pg_ctl.c:95

◆ GetConnection()

PGconn* GetConnection ( void  )

Definition at line 54 of file streamutil.c.

References _, Assert, CONNECTION_BAD, CONNECTION_OK, connection_string, dbgetpassword, dbhost, dbname, dbport, dbuser, free, have_password, i, _PQconninfoOption::keyword, password, pg_malloc0(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQconninfoFree(), PQconninfoParse(), PQerrorMessage(), PQfinish(), PQparameterStatus(), PQstatus(), progname, simple_prompt(), _PQconninfoOption::val, and values.

55 {
56  PGconn *tmpconn;
57  int argcount = 7; /* dbname, replication, fallback_app_name,
58  * host, user, port, password */
59  int i;
60  const char **keywords;
61  const char **values;
62  const char *tmpparam;
63  bool need_password;
64  PQconninfoOption *conn_opts = NULL;
65  PQconninfoOption *conn_opt;
66  char *err_msg = NULL;
67 
68  /* pg_recvlogical uses dbname only; others use connection_string only. */
69  Assert(dbname == NULL || connection_string == NULL);
70 
71  /*
72  * Merge the connection info inputs given in form of connection string,
73  * options and default values (dbname=replication, replication=true, etc.)
74  * Explicitly discard any dbname value in the connection string;
75  * otherwise, PQconnectdbParams() would interpret that value as being
76  * itself a connection string.
77  */
78  i = 0;
80  {
81  conn_opts = PQconninfoParse(connection_string, &err_msg);
82  if (conn_opts == NULL)
83  {
84  fprintf(stderr, "%s: %s", progname, err_msg);
85  exit(1);
86  }
87 
88  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
89  {
90  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
91  strcmp(conn_opt->keyword, "dbname") != 0)
92  argcount++;
93  }
94 
95  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
96  values = pg_malloc0((argcount + 1) * sizeof(*values));
97 
98  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
99  {
100  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
101  strcmp(conn_opt->keyword, "dbname") != 0)
102  {
103  keywords[i] = conn_opt->keyword;
104  values[i] = conn_opt->val;
105  i++;
106  }
107  }
108  }
109  else
110  {
111  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
112  values = pg_malloc0((argcount + 1) * sizeof(*values));
113  }
114 
115  keywords[i] = "dbname";
116  values[i] = dbname == NULL ? "replication" : dbname;
117  i++;
118  keywords[i] = "replication";
119  values[i] = dbname == NULL ? "true" : "database";
120  i++;
121  keywords[i] = "fallback_application_name";
122  values[i] = progname;
123  i++;
124 
125  if (dbhost)
126  {
127  keywords[i] = "host";
128  values[i] = dbhost;
129  i++;
130  }
131  if (dbuser)
132  {
133  keywords[i] = "user";
134  values[i] = dbuser;
135  i++;
136  }
137  if (dbport)
138  {
139  keywords[i] = "port";
140  values[i] = dbport;
141  i++;
142  }
143 
144  /* If -W was given, force prompt for password, but only the first time */
145  need_password = (dbgetpassword == 1 && !have_password);
146 
147  do
148  {
149  /* Get a new password if appropriate */
150  if (need_password)
151  {
152  simple_prompt("Password: ", password, sizeof(password), false);
153  have_password = true;
154  need_password = false;
155  }
156 
157  /* Use (or reuse, on a subsequent connection) password if we have it */
158  if (have_password)
159  {
160  keywords[i] = "password";
161  values[i] = password;
162  }
163  else
164  {
165  keywords[i] = NULL;
166  values[i] = NULL;
167  }
168 
169  tmpconn = PQconnectdbParams(keywords, values, true);
170 
171  /*
172  * If there is too little memory even to allocate the PGconn object
173  * and PQconnectdbParams returns NULL, we call exit(1) directly.
174  */
175  if (!tmpconn)
176  {
177  fprintf(stderr, _("%s: could not connect to server\n"),
178  progname);
179  exit(1);
180  }
181 
182  /* If we need a password and -w wasn't given, loop back and get one */
183  if (PQstatus(tmpconn) == CONNECTION_BAD &&
184  PQconnectionNeedsPassword(tmpconn) &&
185  dbgetpassword != -1)
186  {
187  PQfinish(tmpconn);
188  need_password = true;
189  }
190  }
191  while (need_password);
192 
193  if (PQstatus(tmpconn) != CONNECTION_OK)
194  {
195  fprintf(stderr, _("%s: could not connect to server: %s"),
196  progname, PQerrorMessage(tmpconn));
197  PQfinish(tmpconn);
198  free(values);
199  free(keywords);
200  if (conn_opts)
201  PQconninfoFree(conn_opts);
202  return NULL;
203  }
204 
205  /* Connection ok! */
206  free(values);
207  free(keywords);
208  if (conn_opts)
209  PQconninfoFree(conn_opts);
210 
211  /*
212  * Ensure we have the same value of integer_datetimes (now always "on") as
213  * the server we are connecting to.
214  */
215  tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
216  if (!tmpparam)
217  {
218  fprintf(stderr,
219  _("%s: could not determine server setting for integer_datetimes\n"),
220  progname);
221  PQfinish(tmpconn);
222  exit(1);
223  }
224 
225  if (strcmp(tmpparam, "on") != 0)
226  {
227  fprintf(stderr,
228  _("%s: integer_datetimes compile flag does not match server\n"),
229  progname);
230  PQfinish(tmpconn);
231  exit(1);
232  }
233 
234  return tmpconn;
235 }
static char password[100]
Definition: streamutil.c:45
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6071
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3629
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:529
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:4726
char * connection_string
Definition: streamutil.c:38
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:5956
int dbgetpassword
Definition: streamutil.c:43
char * dbport
Definition: streamutil.c:41
#define free(a)
Definition: header.h:65
static bool have_password
Definition: streamutil.c:44
const char * progname
Definition: streamutil.c:37
#define Assert(condition)
Definition: c.h:670
char * dbhost
Definition: streamutil.c:39
char * dbname
Definition: streamutil.c:42
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6140
char * dbuser
Definition: streamutil.c:40
int i
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6053
#define _(x)
Definition: elog.c:84

◆ RetrieveWalSegSize()

bool RetrieveWalSegSize ( PGconn conn)

Definition at line 242 of file streamutil.c.

References _, Assert, DEFAULT_XLOG_SEG_SIZE, IsValidWalSegSize, MINIMUM_VERSION_FOR_SHOW_CMD, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), PQserverVersion(), progname, and WalSegSz.

Referenced by main().

243 {
244  PGresult *res;
245  char xlog_unit[3];
246  int xlog_val,
247  multiplier = 1;
248 
249  /* check connection existence */
250  Assert(conn != NULL);
251 
252  /* for previous versions set the default xlog seg size */
254  {
256  return true;
257  }
258 
259  res = PQexec(conn, "SHOW wal_segment_size");
260  if (PQresultStatus(res) != PGRES_TUPLES_OK)
261  {
262  fprintf(stderr, _("%s: could not send replication command \"%s\": %s\n"),
263  progname, "SHOW wal_segment_size", PQerrorMessage(conn));
264 
265  PQclear(res);
266  return false;
267  }
268  if (PQntuples(res) != 1 || PQnfields(res) < 1)
269  {
270  fprintf(stderr,
271  _("%s: could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
272  progname, PQntuples(res), PQnfields(res), 1, 1);
273 
274  PQclear(res);
275  return false;
276  }
277 
278  /* fetch xlog value and unit from the result */
279  if (sscanf(PQgetvalue(res, 0, 0), "%d%s", &xlog_val, xlog_unit) != 2)
280  {
281  fprintf(stderr, _("%s: WAL segment size could not be parsed\n"),
282  progname);
283  return false;
284  }
285 
286  /* set the multiplier based on unit to convert xlog_val to bytes */
287  if (strcmp(xlog_unit, "MB") == 0)
288  multiplier = 1024 * 1024;
289  else if (strcmp(xlog_unit, "GB") == 0)
290  multiplier = 1024 * 1024 * 1024;
291 
292  /* convert and set WalSegSz */
293  WalSegSz = xlog_val * multiplier;
294 
296  {
297  fprintf(stderr,
298  _("%s: WAL segment size must be a power of two between 1MB and 1GB, but the remote server reported a value of %d bytes\n"),
299  progname, WalSegSz);
300  return false;
301  }
302 
303  PQclear(res);
304  return true;
305 }
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:97
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
#define DEFAULT_XLOG_SEG_SIZE
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6096
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
#define MINIMUM_VERSION_FOR_SHOW_CMD
Definition: streamutil.c:35
void PQclear(PGresult *res)
Definition: fe-exec.c:671
const char * progname
Definition: streamutil.c:37
#define Assert(condition)
Definition: c.h:670
uint32 WalSegSz
Definition: streamutil.c:32
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define _(x)
Definition: elog.c:84

◆ RunIdentifySystem()

bool RunIdentifySystem ( PGconn conn,
char **  sysid,
TimeLineID starttli,
XLogRecPtr startpos,
char **  db_name 
)

Definition at line 316 of file streamutil.c.

References _, Assert, pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), PQserverVersion(), and progname.

Referenced by BaseBackup(), main(), and StreamLog().

318 {
319  PGresult *res;
320  uint32 hi,
321  lo;
322 
323  /* Check connection existence */
324  Assert(conn != NULL);
325 
326  res = PQexec(conn, "IDENTIFY_SYSTEM");
327  if (PQresultStatus(res) != PGRES_TUPLES_OK)
328  {
329  fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
330  progname, "IDENTIFY_SYSTEM", PQerrorMessage(conn));
331 
332  PQclear(res);
333  return false;
334  }
335  if (PQntuples(res) != 1 || PQnfields(res) < 3)
336  {
337  fprintf(stderr,
338  _("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
339  progname, PQntuples(res), PQnfields(res), 1, 3);
340 
341  PQclear(res);
342  return false;
343  }
344 
345  /* Get system identifier */
346  if (sysid != NULL)
347  *sysid = pg_strdup(PQgetvalue(res, 0, 0));
348 
349  /* Get timeline ID to start streaming from */
350  if (starttli != NULL)
351  *starttli = atoi(PQgetvalue(res, 0, 1));
352 
353  /* Get LSN start position if necessary */
354  if (startpos != NULL)
355  {
356  if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
357  {
358  fprintf(stderr,
359  _("%s: could not parse write-ahead log location \"%s\"\n"),
360  progname, PQgetvalue(res, 0, 2));
361 
362  PQclear(res);
363  return false;
364  }
365  *startpos = ((uint64) hi) << 32 | lo;
366  }
367 
368  /* Get database name, only available in 9.4 and newer versions */
369  if (db_name != NULL)
370  {
371  *db_name = NULL;
372  if (PQserverVersion(conn) >= 90400)
373  {
374  if (PQnfields(res) < 4)
375  {
376  fprintf(stderr,
377  _("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
378  progname, PQntuples(res), PQnfields(res), 1, 4);
379 
380  PQclear(res);
381  return false;
382  }
383  if (!PQgetisnull(res, 0, 3))
384  *db_name = pg_strdup(PQgetvalue(res, 0, 3));
385  }
386  }
387 
388  PQclear(res);
389  return true;
390 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6096
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:296
void PQclear(PGresult *res)
Definition: fe-exec.c:671
const char * progname
Definition: streamutil.c:37
#define Assert(condition)
Definition: c.h:670
static XLogRecPtr startpos
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
#define _(x)
Definition: elog.c:84

Variable Documentation

◆ conn

◆ connection_string

char* connection_string

Definition at line 38 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbgetpassword

int dbgetpassword

Definition at line 43 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbhost

char* dbhost

Definition at line 39 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbname

◆ dbport

char* dbport

Definition at line 41 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbuser

char* dbuser

Definition at line 40 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ progname

const char* progname

Definition at line 37 of file pg_standby.c.

◆ WalSegSz

uint32 WalSegSz

Definition at line 39 of file pg_standby.c.