PostgreSQL Source Code  git master
streamutil.c File Reference
#include "postgres_fe.h"
#include <sys/time.h>
#include <unistd.h>
#include "receivelog.h"
#include "streamutil.h"
#include "access/xlog_internal.h"
#include "common/fe_memutils.h"
#include "common/file_perm.h"
#include "common/logging.h"
#include "datatype/timestamp.h"
#include "fe_utils/connect.h"
#include "port/pg_bswap.h"
#include "pqexpbuffer.h"
Include dependency graph for streamutil.c:

Go to the source code of this file.

Macros

#define ERRCODE_DUPLICATE_OBJECT   "42710"
 
#define MINIMUM_VERSION_FOR_SHOW_CMD   100000
 
#define MINIMUM_VERSION_FOR_GROUP_ACCESS   110000
 

Functions

static bool RetrieveDataDirCreatePerm (PGconn *conn)
 
PGconnGetConnection (void)
 
bool RetrieveWalSegSize (PGconn *conn)
 
bool RunIdentifySystem (PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
 
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)
 
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

uint32 WalSegSz
 
const char * progname
 
char * connection_string = NULL
 
char * dbhost = NULL
 
char * dbuser = NULL
 
char * dbport = NULL
 
char * dbname = NULL
 
int dbgetpassword = 0
 
static bool have_password = false
 
static char password [100]
 
PGconnconn = NULL
 

Macro Definition Documentation

◆ ERRCODE_DUPLICATE_OBJECT

◆ MINIMUM_VERSION_FOR_GROUP_ACCESS

#define MINIMUM_VERSION_FOR_GROUP_ACCESS   110000

Definition at line 45 of file streamutil.c.

Referenced by RetrieveDataDirCreatePerm().

◆ MINIMUM_VERSION_FOR_SHOW_CMD

#define MINIMUM_VERSION_FOR_SHOW_CMD   100000

Definition at line 40 of file streamutil.c.

Referenced by RetrieveWalSegSize().

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 487 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().

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

◆ DropReplicationSlot()

bool DropReplicationSlot ( PGconn conn,
const char *  slot_name 
)

Definition at line 563 of file streamutil.c.

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

564 {
565  PQExpBuffer query;
566  PGresult *res;
567 
568  Assert(slot_name != NULL);
569 
570  query = createPQExpBuffer();
571 
572  /* Build query */
573  appendPQExpBuffer(query, "DROP_REPLICATION_SLOT \"%s\"",
574  slot_name);
575  res = PQexec(conn, query->data);
576  if (PQresultStatus(res) != PGRES_COMMAND_OK)
577  {
578  pg_log_error("could not send replication command \"%s\": %s",
579  query->data, PQerrorMessage(conn));
580 
581  destroyPQExpBuffer(query);
582  PQclear(res);
583  return false;
584  }
585 
586  if (PQntuples(res) != 0 || PQnfields(res) != 0)
587  {
588  pg_log_error("could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
589  slot_name,
590  PQntuples(res), PQnfields(res), 0, 0);
591 
592  destroyPQExpBuffer(query);
593  PQclear(res);
594  return false;
595  }
596 
597  destroyPQExpBuffer(query);
598  PQclear(res);
599  return true;
600 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2778
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6578
#define pg_log_error(...)
Definition: logging.h:79
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
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:695
#define Assert(condition)
Definition: c.h:732
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940

◆ fe_recvint64()

int64 fe_recvint64 ( char *  buf)

Definition at line 673 of file streamutil.c.

References pg_ntoh64.

Referenced by ProcessXLogDataMsg(), and StreamLogicalLog().

674 {
675  uint64 n64;
676 
677  memcpy(&n64, buf, sizeof(n64));
678 
679  return pg_ntoh64(n64);
680 }
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 662 of file streamutil.c.

References pg_hton64.

Referenced by sendFeedback().

663 {
664  uint64 n64 = pg_hton64(i);
665 
666  memcpy(buf, &n64, sizeof(n64));
667 }
#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 608 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().

609 {
610  TimestampTz result;
611  struct timeval tp;
612 
613  gettimeofday(&tp, NULL);
614 
615  result = (TimestampTz) tp.tv_sec -
617  result = (result * USECS_PER_SEC) + tp.tv_usec;
618 
619  return result;
620 }
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 627 of file streamutil.c.

References start_time, and USECS_PER_SEC.

Referenced by CalculateCopyStreamSleeptime(), and StreamLogicalLog().

629 {
630  TimestampTz diff = stop_time - start_time;
631 
632  if (diff <= 0)
633  {
634  *secs = 0;
635  *microsecs = 0;
636  }
637  else
638  {
639  *secs = (long) (diff / USECS_PER_SEC);
640  *microsecs = (int) (diff % USECS_PER_SEC);
641  }
642 }
#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 649 of file streamutil.c.

References start_time.

Referenced by HandleCopyStream(), and StreamLogicalLog().

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

◆ GetConnection()

PGconn* GetConnection ( void  )

Definition at line 64 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_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.

65 {
66  PGconn *tmpconn;
67  int argcount = 7; /* dbname, replication, fallback_app_name,
68  * host, user, port, password */
69  int i;
70  const char **keywords;
71  const char **values;
72  const char *tmpparam;
73  bool need_password;
74  PQconninfoOption *conn_opts = NULL;
75  PQconninfoOption *conn_opt;
76  char *err_msg = NULL;
77 
78  /* pg_recvlogical uses dbname only; others use connection_string only. */
79  Assert(dbname == NULL || connection_string == NULL);
80 
81  /*
82  * Merge the connection info inputs given in form of connection string,
83  * options and default values (dbname=replication, replication=true, etc.)
84  * Explicitly discard any dbname value in the connection string;
85  * otherwise, PQconnectdbParams() would interpret that value as being
86  * itself a connection string.
87  */
88  i = 0;
90  {
91  conn_opts = PQconninfoParse(connection_string, &err_msg);
92  if (conn_opts == NULL)
93  {
94  pg_log_error("%s", err_msg);
95  exit(1);
96  }
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  argcount++;
103  }
104 
105  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
106  values = pg_malloc0((argcount + 1) * sizeof(*values));
107 
108  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
109  {
110  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
111  strcmp(conn_opt->keyword, "dbname") != 0)
112  {
113  keywords[i] = conn_opt->keyword;
114  values[i] = conn_opt->val;
115  i++;
116  }
117  }
118  }
119  else
120  {
121  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
122  values = pg_malloc0((argcount + 1) * sizeof(*values));
123  }
124 
125  keywords[i] = "dbname";
126  values[i] = dbname == NULL ? "replication" : dbname;
127  i++;
128  keywords[i] = "replication";
129  values[i] = dbname == NULL ? "true" : "database";
130  i++;
131  keywords[i] = "fallback_application_name";
132  values[i] = progname;
133  i++;
134 
135  if (dbhost)
136  {
137  keywords[i] = "host";
138  values[i] = dbhost;
139  i++;
140  }
141  if (dbuser)
142  {
143  keywords[i] = "user";
144  values[i] = dbuser;
145  i++;
146  }
147  if (dbport)
148  {
149  keywords[i] = "port";
150  values[i] = dbport;
151  i++;
152  }
153 
154  /* If -W was given, force prompt for password, but only the first time */
155  need_password = (dbgetpassword == 1 && !have_password);
156 
157  do
158  {
159  /* Get a new password if appropriate */
160  if (need_password)
161  {
162  simple_prompt("Password: ", password, sizeof(password), false);
163  have_password = true;
164  need_password = false;
165  }
166 
167  /* Use (or reuse, on a subsequent connection) password if we have it */
168  if (have_password)
169  {
170  keywords[i] = "password";
171  values[i] = password;
172  }
173  else
174  {
175  keywords[i] = NULL;
176  values[i] = NULL;
177  }
178 
179  tmpconn = PQconnectdbParams(keywords, values, true);
180 
181  /*
182  * If there is too little memory even to allocate the PGconn object
183  * and PQconnectdbParams returns NULL, we call exit(1) directly.
184  */
185  if (!tmpconn)
186  {
187  pg_log_error("could not connect to server");
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  pg_log_error("could not connect to server: %s",
205  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 10, so
223  * the search path cannot be changed (by us or attackers) on earlier
224  * versions.
225  */
226  if (dbname != NULL && PQserverVersion(tmpconn) >= 100000)
227  {
228  PGresult *res;
229 
230  res = PQexec(tmpconn, ALWAYS_SECURE_SEARCH_PATH_SQL);
231  if (PQresultStatus(res) != PGRES_TUPLES_OK)
232  {
233  pg_log_error("could not clear search_path: %s",
234  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  pg_log_error("could not determine server setting for integer_datetimes");
250  PQfinish(tmpconn);
251  exit(1);
252  }
253 
254  if (strcmp(tmpparam, "on") != 0)
255  {
256  pg_log_error("integer_datetimes compile flag does not match server");
257  PQfinish(tmpconn);
258  exit(1);
259  }
260 
261  /*
262  * Retrieve the source data directory mode and use it to construct a umask
263  * for creating directories and files.
264  */
265  if (!RetrieveDataDirCreatePerm(tmpconn))
266  {
267  PQfinish(tmpconn);
268  exit(1);
269  }
270 
271  return tmpconn;
272 }
static char password[100]
Definition: streamutil.c:55
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6578
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6543
#define pg_log_error(...)
Definition: logging.h:79
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4080
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6568
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:613
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:5180
char * connection_string
Definition: streamutil.c:48
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:6410
int dbgetpassword
Definition: streamutil.c:53
char * dbport
Definition: streamutil.c:51
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define free(a)
Definition: header.h:65
static bool have_password
Definition: streamutil.c:54
const char * progname
Definition: streamutil.c:47
#define Assert(condition)
Definition: c.h:732
char * dbhost
Definition: streamutil.c:49
char * dbname
Definition: streamutil.c:52
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6612
char * dbuser
Definition: streamutil.c:50
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940
static bool RetrieveDataDirCreatePerm(PGconn *conn)
Definition: streamutil.c:355
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6525

◆ RetrieveDataDirCreatePerm()

static bool RetrieveDataDirCreatePerm ( PGconn conn)
static

Definition at line 355 of file streamutil.c.

References Assert, data_directory_mode, MINIMUM_VERSION_FOR_GROUP_ACCESS, pg_log_error, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), PQserverVersion(), and SetDataDirectoryCreatePerm().

Referenced by GetConnection().

356 {
357  PGresult *res;
359 
360  /* check connection existence */
361  Assert(conn != NULL);
362 
363  /* for previous versions leave the default group access */
365  return true;
366 
367  res = PQexec(conn, "SHOW data_directory_mode");
368  if (PQresultStatus(res) != PGRES_TUPLES_OK)
369  {
370  pg_log_error("could not send replication command \"%s\": %s",
371  "SHOW data_directory_mode", PQerrorMessage(conn));
372 
373  PQclear(res);
374  return false;
375  }
376  if (PQntuples(res) != 1 || PQnfields(res) < 1)
377  {
378  pg_log_error("could not fetch group access flag: got %d rows and %d fields, expected %d rows and %d or more fields",
379  PQntuples(res), PQnfields(res), 1, 1);
380 
381  PQclear(res);
382  return false;
383  }
384 
385  if (sscanf(PQgetvalue(res, 0, 0), "%o", &data_directory_mode) != 1)
386  {
387  pg_log_error("group access flag could not be parsed: %s",
388  PQgetvalue(res, 0, 0));
389 
390  PQclear(res);
391  return false;
392  }
393 
394  SetDataDirectoryCreatePerm(data_directory_mode);
395 
396  PQclear(res);
397  return true;
398 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2778
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6578
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
#define pg_log_error(...)
Definition: logging.h:79
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6568
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define Assert(condition)
Definition: c.h:732
#define MINIMUM_VERSION_FOR_GROUP_ACCESS
Definition: streamutil.c:45
int data_directory_mode
Definition: globals.c:68
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940

◆ RetrieveWalSegSize()

bool RetrieveWalSegSize ( PGconn conn)

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

280 {
281  PGresult *res;
282  char xlog_unit[3];
283  int xlog_val,
284  multiplier = 1;
285 
286  /* check connection existence */
287  Assert(conn != NULL);
288 
289  /* for previous versions set the default xlog seg size */
291  {
293  return true;
294  }
295 
296  res = PQexec(conn, "SHOW wal_segment_size");
297  if (PQresultStatus(res) != PGRES_TUPLES_OK)
298  {
299  pg_log_error("could not send replication command \"%s\": %s",
300  "SHOW wal_segment_size", PQerrorMessage(conn));
301 
302  PQclear(res);
303  return false;
304  }
305  if (PQntuples(res) != 1 || PQnfields(res) < 1)
306  {
307  pg_log_error("could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields",
308  PQntuples(res), PQnfields(res), 1, 1);
309 
310  PQclear(res);
311  return false;
312  }
313 
314  /* fetch xlog value and unit from the result */
315  if (sscanf(PQgetvalue(res, 0, 0), "%d%s", &xlog_val, xlog_unit) != 2)
316  {
317  pg_log_error("WAL segment size could not be parsed");
318  return false;
319  }
320 
321  /* set the multiplier based on unit to convert xlog_val to bytes */
322  if (strcmp(xlog_unit, "MB") == 0)
323  multiplier = 1024 * 1024;
324  else if (strcmp(xlog_unit, "GB") == 0)
325  multiplier = 1024 * 1024 * 1024;
326 
327  /* convert and set WalSegSz */
328  WalSegSz = xlog_val * multiplier;
329 
331  {
332  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",
333  "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",
334  WalSegSz),
335  WalSegSz);
336  return false;
337  }
338 
339  PQclear(res);
340  return true;
341 }
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:97
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2778
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6578
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
#define pg_log_error(...)
Definition: logging.h:79
#define DEFAULT_XLOG_SEG_SIZE
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6568
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
#define MINIMUM_VERSION_FOR_SHOW_CMD
Definition: streamutil.c:40
#define ngettext(s, p, n)
Definition: c.h:1103
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define Assert(condition)
Definition: c.h:732
uint32 WalSegSz
Definition: streamutil.c:35
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940

◆ RunIdentifySystem()

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

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

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

Variable Documentation

◆ conn

PGconn* conn = NULL

Definition at line 56 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_for_tables_with_oids(), check_is_install_user(), check_loadable_libraries(), cluster_all_databases(), cluster_one_database(), connectDatabase(), connectMaintenanceDatabase(), connectToServer(), disconnect_atexit(), 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(), ParallelSlotsTerminate(), pg_fe_scram_exchange(), pg_fe_scram_init(), pgtls_write(), 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(), run_permutation(), set_frozenxids(), setKeepalivesCount(), setup_connection(), sql_conn(), start_postmaster(), StartLogStreamer(), StreamLog(), StreamLogicalLog(), try_complete_step(), vacuum_all_databases(), vacuum_one_database(), vacuumlo(), and WriteRecoveryConf().

◆ connection_string

char* connection_string = NULL

Definition at line 48 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbgetpassword

int dbgetpassword = 0

Definition at line 53 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbhost

char* dbhost = NULL

Definition at line 49 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbname

◆ dbport

char* dbport = NULL

Definition at line 51 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbuser

char* dbuser = NULL

Definition at line 50 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ have_password

bool have_password = false
static

Definition at line 54 of file streamutil.c.

Referenced by connectDatabase(), doConnect(), GetConnection(), main(), sql_conn(), and vacuumlo().

◆ password

◆ progname

const char* progname

Definition at line 47 of file streamutil.c.

Referenced by GetConnection().

◆ WalSegSz

uint32 WalSegSz

Definition at line 35 of file streamutil.c.

Referenced by RetrieveWalSegSize().