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

499 {
500  PQExpBuffer query;
501  PGresult *res;
502 
503  query = createPQExpBuffer();
504 
505  Assert((is_physical && plugin == NULL) ||
506  (!is_physical && plugin != NULL));
507  Assert(slot_name != NULL);
508 
509  /* Build query */
510  appendPQExpBuffer(query, "CREATE_REPLICATION_SLOT \"%s\"", slot_name);
511  if (is_temporary)
512  appendPQExpBuffer(query, " TEMPORARY");
513  if (is_physical)
514  {
515  appendPQExpBuffer(query, " PHYSICAL");
516  if (reserve_wal)
517  appendPQExpBuffer(query, " RESERVE_WAL");
518  }
519  else
520  {
521  appendPQExpBuffer(query, " LOGICAL \"%s\"", plugin);
522  if (PQserverVersion(conn) >= 100000)
523  /* pg_recvlogical doesn't use an exported snapshot, so suppress */
524  appendPQExpBuffer(query, " NOEXPORT_SNAPSHOT");
525  }
526 
527  res = PQexec(conn, query->data);
528  if (PQresultStatus(res) != PGRES_TUPLES_OK)
529  {
530  const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
531 
532  if (slot_exists_ok &&
533  sqlstate &&
534  strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
535  {
536  destroyPQExpBuffer(query);
537  PQclear(res);
538  return true;
539  }
540  else
541  {
542  fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
543  progname, query->data, PQerrorMessage(conn));
544 
545  destroyPQExpBuffer(query);
546  PQclear(res);
547  return false;
548  }
549  }
550 
551  if (PQntuples(res) != 1 || PQnfields(res) != 4)
552  {
553  fprintf(stderr,
554  _("%s: could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields\n"),
555  progname, slot_name,
556  PQntuples(res), PQnfields(res), 1, 4);
557 
558  destroyPQExpBuffer(query);
559  PQclear(res);
560  return false;
561  }
562 
563  destroyPQExpBuffer(query);
564  PQclear(res);
565  return true;
566 }
static const char * plugin
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6106
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:42
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:46
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:2709
#define Assert(condition)
Definition: c.h:699
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
#define _(x)
Definition: elog.c:84

◆ DropReplicationSlot()

bool DropReplicationSlot ( PGconn conn,
const char *  slot_name 
)

Definition at line 573 of file streamutil.c.

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

574 {
575  PQExpBuffer query;
576  PGresult *res;
577 
578  Assert(slot_name != NULL);
579 
580  query = createPQExpBuffer();
581 
582  /* Build query */
583  appendPQExpBuffer(query, "DROP_REPLICATION_SLOT \"%s\"",
584  slot_name);
585  res = PQexec(conn, query->data);
586  if (PQresultStatus(res) != PGRES_COMMAND_OK)
587  {
588  fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
589  progname, query->data, PQerrorMessage(conn));
590 
591  destroyPQExpBuffer(query);
592  PQclear(res);
593  return false;
594  }
595 
596  if (PQntuples(res) != 0 || PQnfields(res) != 0)
597  {
598  fprintf(stderr,
599  _("%s: could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields\n"),
600  progname, slot_name,
601  PQntuples(res), PQnfields(res), 0, 0);
602 
603  destroyPQExpBuffer(query);
604  PQclear(res);
605  return false;
606  }
607 
608  destroyPQExpBuffer(query);
609  PQclear(res);
610  return true;
611 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
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:46
#define Assert(condition)
Definition: c.h:699
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 684 of file streamutil.c.

References pg_ntoh64.

Referenced by ProcessXLogDataMsg(), and StreamLogicalLog().

685 {
686  uint64 n64;
687 
688  memcpy(&n64, buf, sizeof(n64));
689 
690  return pg_ntoh64(n64);
691 }
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 673 of file streamutil.c.

References pg_hton64.

Referenced by sendFeedback().

674 {
675  uint64 n64 = pg_hton64(i);
676 
677  memcpy(buf, &n64, sizeof(n64));
678 }
#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 619 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().

620 {
621  TimestampTz result;
622  struct timeval tp;
623 
624  gettimeofday(&tp, NULL);
625 
626  result = (TimestampTz) tp.tv_sec -
628  result = (result * USECS_PER_SEC) + tp.tv_usec;
629 
630  return result;
631 }
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 638 of file streamutil.c.

References start_time, and USECS_PER_SEC.

Referenced by CalculateCopyStreamSleeptime(), and StreamLogicalLog().

640 {
641  TimestampTz diff = stop_time - start_time;
642 
643  if (diff <= 0)
644  {
645  *secs = 0;
646  *microsecs = 0;
647  }
648  else
649  {
650  *secs = (long) (diff / USECS_PER_SEC);
651  *microsecs = (int) (diff % USECS_PER_SEC);
652  }
653 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static time_t start_time
Definition: pg_ctl.c:96

◆ feTimestampDifferenceExceeds()

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

Definition at line 660 of file streamutil.c.

References start_time.

Referenced by HandleCopyStream(), and StreamLogicalLog().

663 {
664  TimestampTz diff = stop_time - start_time;
665 
666  return (diff >= msec * INT64CONST(1000));
667 }
int64 TimestampTz
Definition: timestamp.h:39
static time_t start_time
Definition: pg_ctl.c:96

◆ GetConnection()

PGconn* GetConnection ( void  )

Definition at line 63 of file streamutil.c.

References _, ALWAYS_SECURE_SEARCH_PATH_SQL, Assert, CONNECTION_BAD, CONNECTION_OK, connection_string, dbgetpassword, dbhost, dbname, dbport, dbuser, free, have_password, i, _PQconninfoOption::keyword, password, 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.

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

◆ RetrieveWalSegSize()

bool RetrieveWalSegSize ( PGconn conn)

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

284 {
285  PGresult *res;
286  char xlog_unit[3];
287  int xlog_val,
288  multiplier = 1;
289 
290  /* check connection existence */
291  Assert(conn != NULL);
292 
293  /* for previous versions set the default xlog seg size */
295  {
297  return true;
298  }
299 
300  res = PQexec(conn, "SHOW wal_segment_size");
301  if (PQresultStatus(res) != PGRES_TUPLES_OK)
302  {
303  fprintf(stderr, _("%s: could not send replication command \"%s\": %s\n"),
304  progname, "SHOW wal_segment_size", PQerrorMessage(conn));
305 
306  PQclear(res);
307  return false;
308  }
309  if (PQntuples(res) != 1 || PQnfields(res) < 1)
310  {
311  fprintf(stderr,
312  _("%s: could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
313  progname, PQntuples(res), PQnfields(res), 1, 1);
314 
315  PQclear(res);
316  return false;
317  }
318 
319  /* fetch xlog value and unit from the result */
320  if (sscanf(PQgetvalue(res, 0, 0), "%d%s", &xlog_val, xlog_unit) != 2)
321  {
322  fprintf(stderr, _("%s: WAL segment size could not be parsed\n"),
323  progname);
324  return false;
325  }
326 
327  /* set the multiplier based on unit to convert xlog_val to bytes */
328  if (strcmp(xlog_unit, "MB") == 0)
329  multiplier = 1024 * 1024;
330  else if (strcmp(xlog_unit, "GB") == 0)
331  multiplier = 1024 * 1024 * 1024;
332 
333  /* convert and set WalSegSz */
334  WalSegSz = xlog_val * multiplier;
335 
337  {
338  fprintf(stderr,
339  _("%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"),
340  progname, WalSegSz);
341  return false;
342  }
343 
344  PQclear(res);
345  return true;
346 }
#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:6116
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:6106
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:39
void PQclear(PGresult *res)
Definition: fe-exec.c:671
const char * progname
Definition: streamutil.c:46
#define Assert(condition)
Definition: c.h:699
uint32 WalSegSz
Definition: streamutil.c:34
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 415 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().

417 {
418  PGresult *res;
419  uint32 hi,
420  lo;
421 
422  /* Check connection existence */
423  Assert(conn != NULL);
424 
425  res = PQexec(conn, "IDENTIFY_SYSTEM");
426  if (PQresultStatus(res) != PGRES_TUPLES_OK)
427  {
428  fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
429  progname, "IDENTIFY_SYSTEM", PQerrorMessage(conn));
430 
431  PQclear(res);
432  return false;
433  }
434  if (PQntuples(res) != 1 || PQnfields(res) < 3)
435  {
436  fprintf(stderr,
437  _("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
438  progname, PQntuples(res), PQnfields(res), 1, 3);
439 
440  PQclear(res);
441  return false;
442  }
443 
444  /* Get system identifier */
445  if (sysid != NULL)
446  *sysid = pg_strdup(PQgetvalue(res, 0, 0));
447 
448  /* Get timeline ID to start streaming from */
449  if (starttli != NULL)
450  *starttli = atoi(PQgetvalue(res, 0, 1));
451 
452  /* Get LSN start position if necessary */
453  if (startpos != NULL)
454  {
455  if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
456  {
457  fprintf(stderr,
458  _("%s: could not parse write-ahead log location \"%s\"\n"),
459  progname, PQgetvalue(res, 0, 2));
460 
461  PQclear(res);
462  return false;
463  }
464  *startpos = ((uint64) hi) << 32 | lo;
465  }
466 
467  /* Get database name, only available in 9.4 and newer versions */
468  if (db_name != NULL)
469  {
470  *db_name = NULL;
471  if (PQserverVersion(conn) >= 90400)
472  {
473  if (PQnfields(res) < 4)
474  {
475  fprintf(stderr,
476  _("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
477  progname, PQntuples(res), PQnfields(res), 1, 4);
478 
479  PQclear(res);
480  return false;
481  }
482  if (!PQgetisnull(res, 0, 3))
483  *db_name = pg_strdup(PQgetvalue(res, 0, 3));
484  }
485  }
486 
487  PQclear(res);
488  return true;
489 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
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:6106
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:325
void PQclear(PGresult *res)
Definition: fe-exec.c:671
const char * progname
Definition: streamutil.c:46
#define Assert(condition)
Definition: c.h:699
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

PGconn* conn

Definition at line 55 of file streamutil.c.

Referenced by BaseBackup(), build_client_final_message(), build_client_first_message(), check_for_isn_and_int8_passing_mismatch(), check_for_jsonb_9_4_usage(), check_for_pg_role_prefix(), check_for_prepared_transactions(), check_for_reg_data_type_usage(), check_is_install_user(), check_loadable_libraries(), cluster_all_databases(), cluster_one_database(), connectDatabase(), connectMaintenanceDatabase(), connectToServer(), disconnect_and_exit(), doConnect(), dumpBlobs(), dumpDatabase(), dumpDatabaseConfig(), dumpTableData_copy(), ExecuteSqlCommand(), freePGconn(), get_db_conn(), get_db_infos(), get_loadable_libraries(), get_rel_infos(), get_tablespace_paths(), init_slot(), libpqrcv_connect(), main(), makeEmptyPGconn(), new_9_0_populate_pg_largeobject_metadata(), old_9_3_check_for_line_data_type_usage(), old_9_6_check_for_unknown_data_type_usage(), old_9_6_invalidate_hash_indexes(), pg_fe_scram_exchange(), pg_fe_scram_init(), PQconnectdb(), PQconnectdbParams(), PQconnectStart(), PQconnectStartParams(), PQfireResultCreateEvents(), PQgetResult(), PQping(), PQpingParams(), PQregisterEventProc(), PQreset(), PQresetPoll(), PQsetdbLogin(), read_server_final_message(), read_server_first_message(), reindex_all_databases(), reindex_one_database(), reindex_system_catalogs(), run_permutation(), set_frozenxids(), setKeepalivesCount(), setup_connection(), sql_conn(), start_postmaster(), StartLogStreamer(), StreamLog(), StreamLogicalLog(), try_complete_step(), vacuum_all_databases(), vacuum_one_database(), and vacuumlo().

◆ 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 37 of file pg_standby.c.

◆ WalSegSz

uint32 WalSegSz

Definition at line 39 of file pg_standby.c.