PostgreSQL Source Code  git master
streamutil.h File Reference
#include "access/xlogdefs.h"
#include "datatype/timestamp.h"
#include "libpq-fe.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 488 of file streamutil.c.

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

491 {
492  PQExpBuffer query;
493  PGresult *res;
494 
495  query = createPQExpBuffer();
496 
497  Assert((is_physical && plugin == NULL) ||
498  (!is_physical && plugin != NULL));
499  Assert(slot_name != NULL);
500 
501  /* Build query */
502  appendPQExpBuffer(query, "CREATE_REPLICATION_SLOT \"%s\"", slot_name);
503  if (is_temporary)
504  appendPQExpBufferStr(query, " TEMPORARY");
505  if (is_physical)
506  {
507  appendPQExpBufferStr(query, " PHYSICAL");
508  if (reserve_wal)
509  appendPQExpBufferStr(query, " RESERVE_WAL");
510  }
511  else
512  {
513  appendPQExpBuffer(query, " LOGICAL \"%s\"", plugin);
514  if (PQserverVersion(conn) >= 100000)
515  /* pg_recvlogical doesn't use an exported snapshot, so suppress */
516  appendPQExpBufferStr(query, " NOEXPORT_SNAPSHOT");
517  }
518 
519  res = PQexec(conn, query->data);
520  if (PQresultStatus(res) != PGRES_TUPLES_OK)
521  {
522  const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
523 
524  if (slot_exists_ok &&
525  sqlstate &&
526  strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
527  {
528  destroyPQExpBuffer(query);
529  PQclear(res);
530  return true;
531  }
532  else
533  {
534  pg_log_error("could not send replication command \"%s\": %s",
535  query->data, PQerrorMessage(conn));
536 
537  destroyPQExpBuffer(query);
538  PQclear(res);
539  return false;
540  }
541  }
542 
543  if (PQntuples(res) != 1 || PQnfields(res) != 4)
544  {
545  pg_log_error("could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
546  slot_name,
547  PQntuples(res), PQnfields(res), 1, 4);
548 
549  destroyPQExpBuffer(query);
550  PQclear(res);
551  return false;
552  }
553 
554  destroyPQExpBuffer(query);
555  PQclear(res);
556  return true;
557 }
static const char * plugin
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6669
#define pg_log_error(...)
Definition: logging.h:80
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6659
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
static bool slot_exists_ok
Definition: pg_receivewal.c:41
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:2754
#define Assert(condition)
Definition: c.h:746
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32

◆ DropReplicationSlot()

bool DropReplicationSlot ( PGconn conn,
const char *  slot_name 
)

Definition at line 564 of file streamutil.c.

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

565 {
566  PQExpBuffer query;
567  PGresult *res;
568 
569  Assert(slot_name != NULL);
570 
571  query = createPQExpBuffer();
572 
573  /* Build query */
574  appendPQExpBuffer(query, "DROP_REPLICATION_SLOT \"%s\"",
575  slot_name);
576  res = PQexec(conn, query->data);
577  if (PQresultStatus(res) != PGRES_COMMAND_OK)
578  {
579  pg_log_error("could not send replication command \"%s\": %s",
580  query->data, PQerrorMessage(conn));
581 
582  destroyPQExpBuffer(query);
583  PQclear(res);
584  return false;
585  }
586 
587  if (PQntuples(res) != 0 || PQnfields(res) != 0)
588  {
589  pg_log_error("could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
590  slot_name,
591  PQntuples(res), PQnfields(res), 0, 0);
592 
593  destroyPQExpBuffer(query);
594  PQclear(res);
595  return false;
596  }
597 
598  destroyPQExpBuffer(query);
599  PQclear(res);
600  return true;
601 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6669
#define pg_log_error(...)
Definition: logging.h:80
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define Assert(condition)
Definition: c.h:746
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939

◆ fe_recvint64()

int64 fe_recvint64 ( char *  buf)

Definition at line 674 of file streamutil.c.

References pg_ntoh64.

Referenced by ProcessXLogDataMsg(), and StreamLogicalLog().

675 {
676  uint64 n64;
677 
678  memcpy(&n64, buf, sizeof(n64));
679 
680  return pg_ntoh64(n64);
681 }
static char * buf
Definition: pg_test_fsync.c:68
#define pg_ntoh64(x)
Definition: pg_bswap.h:126

◆ fe_sendint64()

void fe_sendint64 ( int64  i,
char *  buf 
)

Definition at line 663 of file streamutil.c.

References pg_hton64.

Referenced by sendFeedback().

664 {
665  uint64 n64 = pg_hton64(i);
666 
667  memcpy(buf, &n64, sizeof(n64));
668 }
#define pg_hton64(x)
Definition: pg_bswap.h:122
static char * buf
Definition: pg_test_fsync.c:68
int i

◆ feGetCurrentTimestamp()

TimestampTz feGetCurrentTimestamp ( void  )

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

610 {
611  TimestampTz result;
612  struct timeval tp;
613 
614  gettimeofday(&tp, NULL);
615 
616  result = (TimestampTz) tp.tv_sec -
618  result = (result * USECS_PER_SEC) + tp.tv_usec;
619 
620  return result;
621 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:104
#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 628 of file streamutil.c.

References start_time, and USECS_PER_SEC.

Referenced by CalculateCopyStreamSleeptime(), and StreamLogicalLog().

630 {
631  TimestampTz diff = stop_time - start_time;
632 
633  if (diff <= 0)
634  {
635  *secs = 0;
636  *microsecs = 0;
637  }
638  else
639  {
640  *secs = (long) (diff / USECS_PER_SEC);
641  *microsecs = (int) (diff % USECS_PER_SEC);
642  }
643 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static time_t start_time
Definition: pg_ctl.c:99

◆ feTimestampDifferenceExceeds()

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

Definition at line 650 of file streamutil.c.

References start_time.

Referenced by HandleCopyStream(), and StreamLogicalLog().

653 {
654  TimestampTz diff = stop_time - start_time;
655 
656  return (diff >= msec * INT64CONST(1000));
657 }
int64 TimestampTz
Definition: timestamp.h:39
static time_t start_time
Definition: pg_ctl.c:99

◆ GetConnection()

PGconn* GetConnection ( void  )

Definition at line 62 of file streamutil.c.

References ALWAYS_SECURE_SEARCH_PATH_SQL, Assert, CONNECTION_BAD, CONNECTION_OK, connection_string, dbgetpassword, dbhost, dbname, dbport, dbuser, free, i, _PQconninfoOption::keyword, password, pg_log_error, pg_malloc0(), PGRES_TUPLES_OK, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQconninfoFree(), PQconninfoParse(), PQerrorMessage(), PQexec(), PQfinish(), PQparameterStatus(), PQresultStatus(), PQserverVersion(), PQstatus(), progname, RetrieveDataDirCreatePerm(), simple_prompt(), _PQconninfoOption::val, and values.

63 {
64  PGconn *tmpconn;
65  int argcount = 7; /* dbname, replication, fallback_app_name,
66  * host, user, port, password */
67  int i;
68  const char **keywords;
69  const char **values;
70  const char *tmpparam;
71  bool need_password;
72  PQconninfoOption *conn_opts = NULL;
73  PQconninfoOption *conn_opt;
74  char *err_msg = NULL;
75 
76  /* pg_recvlogical uses dbname only; others use connection_string only. */
77  Assert(dbname == NULL || connection_string == NULL);
78 
79  /*
80  * Merge the connection info inputs given in form of connection string,
81  * options and default values (dbname=replication, replication=true, etc.)
82  * Explicitly discard any dbname value in the connection string;
83  * otherwise, PQconnectdbParams() would interpret that value as being
84  * itself a connection string.
85  */
86  i = 0;
88  {
89  conn_opts = PQconninfoParse(connection_string, &err_msg);
90  if (conn_opts == NULL)
91  {
92  pg_log_error("%s", err_msg);
93  exit(1);
94  }
95 
96  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
97  {
98  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
99  strcmp(conn_opt->keyword, "dbname") != 0)
100  argcount++;
101  }
102 
103  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
104  values = pg_malloc0((argcount + 1) * sizeof(*values));
105 
106  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
107  {
108  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
109  strcmp(conn_opt->keyword, "dbname") != 0)
110  {
111  keywords[i] = conn_opt->keyword;
112  values[i] = conn_opt->val;
113  i++;
114  }
115  }
116  }
117  else
118  {
119  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
120  values = pg_malloc0((argcount + 1) * sizeof(*values));
121  }
122 
123  keywords[i] = "dbname";
124  values[i] = dbname == NULL ? "replication" : dbname;
125  i++;
126  keywords[i] = "replication";
127  values[i] = dbname == NULL ? "true" : "database";
128  i++;
129  keywords[i] = "fallback_application_name";
130  values[i] = progname;
131  i++;
132 
133  if (dbhost)
134  {
135  keywords[i] = "host";
136  values[i] = dbhost;
137  i++;
138  }
139  if (dbuser)
140  {
141  keywords[i] = "user";
142  values[i] = dbuser;
143  i++;
144  }
145  if (dbport)
146  {
147  keywords[i] = "port";
148  values[i] = dbport;
149  i++;
150  }
151 
152  /* If -W was given, force prompt for password, but only the first time */
153  need_password = (dbgetpassword == 1 && !password);
154 
155  do
156  {
157  /* Get a new password if appropriate */
158  if (need_password)
159  {
160  if (password)
161  free(password);
162  password = simple_prompt("Password: ", false);
163  need_password = false;
164  }
165 
166  /* Use (or reuse, on a subsequent connection) password if we have it */
167  if (password)
168  {
169  keywords[i] = "password";
170  values[i] = password;
171  }
172  else
173  {
174  keywords[i] = NULL;
175  values[i] = NULL;
176  }
177 
178  tmpconn = PQconnectdbParams(keywords, values, true);
179 
180  /*
181  * If there is too little memory even to allocate the PGconn object
182  * and PQconnectdbParams returns NULL, we call exit(1) directly.
183  */
184  if (!tmpconn)
185  {
186  pg_log_error("could not connect to server");
187  exit(1);
188  }
189 
190  /* If we need a password and -w wasn't given, loop back and get one */
191  if (PQstatus(tmpconn) == CONNECTION_BAD &&
192  PQconnectionNeedsPassword(tmpconn) &&
193  dbgetpassword != -1)
194  {
195  PQfinish(tmpconn);
196  need_password = true;
197  }
198  }
199  while (need_password);
200 
201  if (PQstatus(tmpconn) != CONNECTION_OK)
202  {
203  pg_log_error("could not connect to server: %s",
204  PQerrorMessage(tmpconn));
205  PQfinish(tmpconn);
206  free(values);
207  free(keywords);
208  if (conn_opts)
209  PQconninfoFree(conn_opts);
210  return NULL;
211  }
212 
213  /* Connection ok! */
214  free(values);
215  free(keywords);
216  if (conn_opts)
217  PQconninfoFree(conn_opts);
218 
219  /*
220  * Set always-secure search path, so malicious users can't get control.
221  * The capacity to run normal SQL queries was added in PostgreSQL 10, so
222  * the search path cannot be changed (by us or attackers) on earlier
223  * versions.
224  */
225  if (dbname != NULL && PQserverVersion(tmpconn) >= 100000)
226  {
227  PGresult *res;
228 
229  res = PQexec(tmpconn, ALWAYS_SECURE_SEARCH_PATH_SQL);
230  if (PQresultStatus(res) != PGRES_TUPLES_OK)
231  {
232  pg_log_error("could not clear search_path: %s",
233  PQerrorMessage(tmpconn));
234  PQclear(res);
235  PQfinish(tmpconn);
236  exit(1);
237  }
238  PQclear(res);
239  }
240 
241  /*
242  * Ensure we have the same value of integer_datetimes (now always "on") as
243  * the server we are connecting to.
244  */
245  tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
246  if (!tmpparam)
247  {
248  pg_log_error("could not determine server setting for integer_datetimes");
249  PQfinish(tmpconn);
250  exit(1);
251  }
252 
253  if (strcmp(tmpparam, "on") != 0)
254  {
255  pg_log_error("integer_datetimes compile flag does not match server");
256  PQfinish(tmpconn);
257  exit(1);
258  }
259 
260  /*
261  * Retrieve the source data directory mode and use it to construct a umask
262  * for creating directories and files.
263  */
264  if (!RetrieveDataDirCreatePerm(tmpconn))
265  {
266  PQfinish(tmpconn);
267  exit(1);
268  }
269 
270  return tmpconn;
271 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6669
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6634
#define pg_log_error(...)
Definition: logging.h:80
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4174
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6659
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:647
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:5271
char * connection_string
Definition: streamutil.c:47
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:6501
static char * password
Definition: streamutil.c:53
int dbgetpassword
Definition: streamutil.c:52
char * dbport
Definition: streamutil.c:50
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
const char * progname
Definition: streamutil.c:46
#define Assert(condition)
Definition: c.h:746
char * dbhost
Definition: streamutil.c:48
char * dbname
Definition: streamutil.c:51
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:165
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6703
char * dbuser
Definition: streamutil.c:49
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
static bool RetrieveDataDirCreatePerm(PGconn *conn)
Definition: streamutil.c:356
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6616

◆ RetrieveWalSegSize()

bool RetrieveWalSegSize ( PGconn conn)

Definition at line 278 of file streamutil.c.

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

Referenced by main().

279 {
280  PGresult *res;
281  char xlog_unit[3];
282  int xlog_val,
283  multiplier = 1;
284 
285  /* check connection existence */
286  Assert(conn != NULL);
287 
288  /* for previous versions set the default xlog seg size */
290  {
292  return true;
293  }
294 
295  res = PQexec(conn, "SHOW wal_segment_size");
296  if (PQresultStatus(res) != PGRES_TUPLES_OK)
297  {
298  pg_log_error("could not send replication command \"%s\": %s",
299  "SHOW wal_segment_size", PQerrorMessage(conn));
300 
301  PQclear(res);
302  return false;
303  }
304  if (PQntuples(res) != 1 || PQnfields(res) < 1)
305  {
306  pg_log_error("could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields",
307  PQntuples(res), PQnfields(res), 1, 1);
308 
309  PQclear(res);
310  return false;
311  }
312 
313  /* fetch xlog value and unit from the result */
314  if (sscanf(PQgetvalue(res, 0, 0), "%d%s", &xlog_val, xlog_unit) != 2)
315  {
316  pg_log_error("WAL segment size could not be parsed");
317  PQclear(res);
318  return false;
319  }
320 
321  PQclear(res);
322 
323  /* set the multiplier based on unit to convert xlog_val to bytes */
324  if (strcmp(xlog_unit, "MB") == 0)
325  multiplier = 1024 * 1024;
326  else if (strcmp(xlog_unit, "GB") == 0)
327  multiplier = 1024 * 1024 * 1024;
328 
329  /* convert and set WalSegSz */
330  WalSegSz = xlog_val * multiplier;
331 
333  {
334  pg_log_error(ngettext("WAL segment size must be a power of two between 1 MB and 1 GB, but the remote server reported a value of %d byte",
335  "WAL segment size must be a power of two between 1 MB and 1 GB, but the remote server reported a value of %d bytes",
336  WalSegSz),
337  WalSegSz);
338  return false;
339  }
340 
341  return true;
342 }
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:94
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6669
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
#define pg_log_error(...)
Definition: logging.h:80
#define DEFAULT_XLOG_SEG_SIZE
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6659
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#define MINIMUM_VERSION_FOR_SHOW_CMD
Definition: streamutil.c:39
#define ngettext(s, p, n)
Definition: c.h:1124
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define Assert(condition)
Definition: c.h:746
uint32 WalSegSz
Definition: streamutil.c:34
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939

◆ RunIdentifySystem()

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

Definition at line 410 of file streamutil.c.

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

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

412 {
413  PGresult *res;
414  uint32 hi,
415  lo;
416 
417  /* Check connection existence */
418  Assert(conn != NULL);
419 
420  res = PQexec(conn, "IDENTIFY_SYSTEM");
421  if (PQresultStatus(res) != PGRES_TUPLES_OK)
422  {
423  pg_log_error("could not send replication command \"%s\": %s",
424  "IDENTIFY_SYSTEM", PQerrorMessage(conn));
425 
426  PQclear(res);
427  return false;
428  }
429  if (PQntuples(res) != 1 || PQnfields(res) < 3)
430  {
431  pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields",
432  PQntuples(res), PQnfields(res), 1, 3);
433 
434  PQclear(res);
435  return false;
436  }
437 
438  /* Get system identifier */
439  if (sysid != NULL)
440  *sysid = pg_strdup(PQgetvalue(res, 0, 0));
441 
442  /* Get timeline ID to start streaming from */
443  if (starttli != NULL)
444  *starttli = atoi(PQgetvalue(res, 0, 1));
445 
446  /* Get LSN start position if necessary */
447  if (startpos != NULL)
448  {
449  if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
450  {
451  pg_log_error("could not parse write-ahead log location \"%s\"",
452  PQgetvalue(res, 0, 2));
453 
454  PQclear(res);
455  return false;
456  }
457  *startpos = ((uint64) hi) << 32 | lo;
458  }
459 
460  /* Get database name, only available in 9.4 and newer versions */
461  if (db_name != NULL)
462  {
463  *db_name = NULL;
464  if (PQserverVersion(conn) >= 90400)
465  {
466  if (PQnfields(res) < 4)
467  {
468  pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields",
469  PQntuples(res), PQnfields(res), 1, 4);
470 
471  PQclear(res);
472  return false;
473  }
474  if (!PQgetisnull(res, 0, 3))
475  *db_name = pg_strdup(PQgetvalue(res, 0, 3));
476  }
477  }
478 
479  PQclear(res);
480  return true;
481 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6669
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
#define pg_log_error(...)
Definition: logging.h:80
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6659
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:375
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define Assert(condition)
Definition: c.h:746
static XLogRecPtr startpos
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188

Variable Documentation

◆ conn

PGconn* conn

Definition at line 54 of file streamutil.c.

◆ connection_string

char* connection_string

Definition at line 47 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbgetpassword

int dbgetpassword

Definition at line 52 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbhost

char* dbhost

Definition at line 48 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbname

◆ dbport

char* dbport

Definition at line 50 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbuser

char* dbuser

Definition at line 49 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ progname

const char* progname

Definition at line 36 of file pg_standby.c.

◆ WalSegSz

uint32 WalSegSz

Definition at line 38 of file pg_standby.c.