PostgreSQL Source Code  git master
streamutil.c File Reference
#include "postgres_fe.h"
#include <sys/time.h>
#include <unistd.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 "receivelog.h"
#include "streamutil.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 43 of file streamutil.c.

Referenced by RetrieveDataDirCreatePerm().

◆ MINIMUM_VERSION_FOR_SHOW_CMD

#define MINIMUM_VERSION_FOR_SHOW_CMD   100000

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

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

◆ DropReplicationSlot()

bool DropReplicationSlot ( PGconn conn,
const char *  slot_name 
)

Definition at line 561 of file streamutil.c.

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

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

◆ fe_recvint64()

int64 fe_recvint64 ( char *  buf)

Definition at line 671 of file streamutil.c.

References pg_ntoh64.

Referenced by ProcessXLogDataMsg(), and StreamLogicalLog().

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

References pg_hton64.

Referenced by sendFeedback().

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

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

References start_time, and USECS_PER_SEC.

Referenced by CalculateCopyStreamSleeptime(), and StreamLogicalLog().

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

References start_time.

Referenced by HandleCopyStream(), and StreamLogicalLog().

650 {
651  TimestampTz diff = stop_time - start_time;
652 
653  return (diff >= msec * INT64CONST(1000));
654 }
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, 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.

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 && !have_password);
154 
155  do
156  {
157  /* Get a new password if appropriate */
158  if (need_password)
159  {
160  simple_prompt("Password: ", password, sizeof(password), false);
161  have_password = true;
162  need_password = false;
163  }
164 
165  /* Use (or reuse, on a subsequent connection) password if we have it */
166  if (have_password)
167  {
168  keywords[i] = "password";
169  values[i] = password;
170  }
171  else
172  {
173  keywords[i] = NULL;
174  values[i] = NULL;
175  }
176 
177  tmpconn = PQconnectdbParams(keywords, values, true);
178 
179  /*
180  * If there is too little memory even to allocate the PGconn object
181  * and PQconnectdbParams returns NULL, we call exit(1) directly.
182  */
183  if (!tmpconn)
184  {
185  pg_log_error("could not connect to server");
186  exit(1);
187  }
188 
189  /* If we need a password and -w wasn't given, loop back and get one */
190  if (PQstatus(tmpconn) == CONNECTION_BAD &&
191  PQconnectionNeedsPassword(tmpconn) &&
192  dbgetpassword != -1)
193  {
194  PQfinish(tmpconn);
195  need_password = true;
196  }
197  }
198  while (need_password);
199 
200  if (PQstatus(tmpconn) != CONNECTION_OK)
201  {
202  pg_log_error("could not connect to server: %s",
203  PQerrorMessage(tmpconn));
204  PQfinish(tmpconn);
205  free(values);
206  free(keywords);
207  if (conn_opts)
208  PQconninfoFree(conn_opts);
209  return NULL;
210  }
211 
212  /* Connection ok! */
213  free(values);
214  free(keywords);
215  if (conn_opts)
216  PQconninfoFree(conn_opts);
217 
218  /*
219  * Set always-secure search path, so malicious users can't get control.
220  * The capacity to run normal SQL queries was added in PostgreSQL 10, so
221  * the search path cannot be changed (by us or attackers) on earlier
222  * versions.
223  */
224  if (dbname != NULL && PQserverVersion(tmpconn) >= 100000)
225  {
226  PGresult *res;
227 
228  res = PQexec(tmpconn, ALWAYS_SECURE_SEARCH_PATH_SQL);
229  if (PQresultStatus(res) != PGRES_TUPLES_OK)
230  {
231  pg_log_error("could not clear search_path: %s",
232  PQerrorMessage(tmpconn));
233  PQclear(res);
234  PQfinish(tmpconn);
235  exit(1);
236  }
237  PQclear(res);
238  }
239 
240  /*
241  * Ensure we have the same value of integer_datetimes (now always "on") as
242  * the server we are connecting to.
243  */
244  tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
245  if (!tmpparam)
246  {
247  pg_log_error("could not determine server setting for integer_datetimes");
248  PQfinish(tmpconn);
249  exit(1);
250  }
251 
252  if (strcmp(tmpparam, "on") != 0)
253  {
254  pg_log_error("integer_datetimes compile flag does not match server");
255  PQfinish(tmpconn);
256  exit(1);
257  }
258 
259  /*
260  * Retrieve the source data directory mode and use it to construct a umask
261  * for creating directories and files.
262  */
263  if (!RetrieveDataDirCreatePerm(tmpconn))
264  {
265  PQfinish(tmpconn);
266  exit(1);
267  }
268 
269  return tmpconn;
270 }
static char password[100]
Definition: streamutil.c:53
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6623
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6588
#define pg_log_error(...)
Definition: logging.h:79
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4125
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6613
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:624
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:5225
char * connection_string
Definition: streamutil.c:46
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:6455
int dbgetpassword
Definition: streamutil.c:51
char * dbport
Definition: streamutil.c:49
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
static bool have_password
Definition: streamutil.c:52
const char * progname
Definition: streamutil.c:45
#define Assert(condition)
Definition: c.h:739
char * dbhost
Definition: streamutil.c:47
char * dbname
Definition: streamutil.c:50
#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:6657
char * dbuser
Definition: streamutil.c:48
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
static bool RetrieveDataDirCreatePerm(PGconn *conn)
Definition: streamutil.c:353
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6570

◆ RetrieveDataDirCreatePerm()

static bool RetrieveDataDirCreatePerm ( PGconn conn)
static

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

354 {
355  PGresult *res;
357 
358  /* check connection existence */
359  Assert(conn != NULL);
360 
361  /* for previous versions leave the default group access */
363  return true;
364 
365  res = PQexec(conn, "SHOW data_directory_mode");
366  if (PQresultStatus(res) != PGRES_TUPLES_OK)
367  {
368  pg_log_error("could not send replication command \"%s\": %s",
369  "SHOW data_directory_mode", PQerrorMessage(conn));
370 
371  PQclear(res);
372  return false;
373  }
374  if (PQntuples(res) != 1 || PQnfields(res) < 1)
375  {
376  pg_log_error("could not fetch group access flag: got %d rows and %d fields, expected %d rows and %d or more fields",
377  PQntuples(res), PQnfields(res), 1, 1);
378 
379  PQclear(res);
380  return false;
381  }
382 
383  if (sscanf(PQgetvalue(res, 0, 0), "%o", &data_directory_mode) != 1)
384  {
385  pg_log_error("group access flag could not be parsed: %s",
386  PQgetvalue(res, 0, 0));
387 
388  PQclear(res);
389  return false;
390  }
391 
392  SetDataDirectoryCreatePerm(data_directory_mode);
393 
394  PQclear(res);
395  return true;
396 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6623
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
#define pg_log_error(...)
Definition: logging.h:79
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6613
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define Assert(condition)
Definition: c.h:739
#define MINIMUM_VERSION_FOR_GROUP_ACCESS
Definition: streamutil.c:43
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:1939

◆ RetrieveWalSegSize()

bool RetrieveWalSegSize ( PGconn conn)

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

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

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

Definition at line 54 of file streamutil.c.

Referenced by BaseBackup(), build_client_final_message(), build_client_first_message(), check_for_data_type_usage(), 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_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(), and vacuumlo().

◆ connection_string

char* connection_string = NULL

Definition at line 46 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbgetpassword

int dbgetpassword = 0

Definition at line 51 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbhost

char* dbhost = NULL

Definition at line 47 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbname

◆ dbport

char* dbport = NULL

Definition at line 49 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbuser

char* dbuser = NULL

Definition at line 48 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ have_password

bool have_password = false
static

Definition at line 52 of file streamutil.c.

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

◆ password

◆ progname

const char* progname

Definition at line 45 of file streamutil.c.

Referenced by GetConnection().

◆ WalSegSz

uint32 WalSegSz

Definition at line 33 of file streamutil.c.

Referenced by RetrieveWalSegSize().