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/connect.h"
#include "common/fe_memutils.h"
#include "common/file_perm.h"
#include "common/logging.h"
#include "common/string.h"
#include "datatype/timestamp.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 GetSlotInformation (PGconn *conn, const char *slot_name, XLogRecPtr *restart_lsn, TimeLineID *restart_tli)
 
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 two_phase)
 
bool DropReplicationSlot (PGconn *conn, const char *slot_name)
 
void AppendPlainCommandOption (PQExpBuffer buf, bool use_new_option_syntax, char *option_name)
 
void AppendStringCommandOption (PQExpBuffer buf, bool use_new_option_syntax, char *option_name, char *option_value)
 
void AppendIntegerCommandOption (PQExpBuffer buf, bool use_new_option_syntax, char *option_name, int32 option_value)
 
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 char * password = NULL
 
PGconnconn = NULL
 

Macro Definition Documentation

◆ ERRCODE_DUPLICATE_OBJECT

#define ERRCODE_DUPLICATE_OBJECT   "42710"

◆ MINIMUM_VERSION_FOR_GROUP_ACCESS

#define MINIMUM_VERSION_FOR_GROUP_ACCESS   110000

Definition at line 44 of file streamutil.c.

Referenced by RetrieveDataDirCreatePerm().

◆ MINIMUM_VERSION_FOR_SHOW_CMD

#define MINIMUM_VERSION_FOR_SHOW_CMD   100000

Definition at line 39 of file streamutil.c.

Referenced by RetrieveWalSegSize().

Function Documentation

◆ AppendIntegerCommandOption()

void AppendIntegerCommandOption ( PQExpBuffer  buf,
bool  use_new_option_syntax,
char *  option_name,
int32  option_value 
)

Definition at line 785 of file streamutil.c.

References AppendPlainCommandOption(), and appendPQExpBuffer().

Referenced by BaseBackup().

787 {
788  AppendPlainCommandOption(buf, use_new_option_syntax, option_name);
789 
790  appendPQExpBuffer(buf, " %d", option_value);
791 }
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
void AppendPlainCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name)
Definition: streamutil.c:741

◆ AppendPlainCommandOption()

void AppendPlainCommandOption ( PQExpBuffer  buf,
bool  use_new_option_syntax,
char *  option_name 
)

Definition at line 741 of file streamutil.c.

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), PQExpBufferData::data, and PQExpBufferData::len.

Referenced by AppendIntegerCommandOption(), AppendStringCommandOption(), BaseBackup(), and CreateReplicationSlot().

743 {
744  if (buf->len > 0 && buf->data[buf->len - 1] != '(')
745  {
746  if (use_new_option_syntax)
747  appendPQExpBufferStr(buf, ", ");
748  else
749  appendPQExpBufferChar(buf, ' ');
750  }
751 
752  appendPQExpBuffer(buf, " %s", option_name);
753 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380

◆ AppendStringCommandOption()

void AppendStringCommandOption ( PQExpBuffer  buf,
bool  use_new_option_syntax,
char *  option_name,
char *  option_value 
)

Definition at line 762 of file streamutil.c.

References AppendPlainCommandOption(), appendPQExpBuffer(), palloc(), pfree(), and PQescapeStringConn().

Referenced by BaseBackup(), and CreateReplicationSlot().

764 {
765  AppendPlainCommandOption(buf, use_new_option_syntax, option_name);
766 
767  if (option_value != NULL)
768  {
769  size_t length = strlen(option_value);
770  char *escaped_value = palloc(1 + 2 * length);
771 
772  PQescapeStringConn(conn, escaped_value, option_value, length, NULL);
773  appendPQExpBuffer(buf, " '%s'", escaped_value);
774  pfree(escaped_value);
775  }
776 }
void pfree(void *pointer)
Definition: mcxt.c:1169
PGconn * conn
Definition: streamutil.c:54
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error)
Definition: fe-exec.c:3910
void AppendPlainCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name)
Definition: streamutil.c:741
void * palloc(Size size)
Definition: mcxt.c:1062

◆ 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,
bool  two_phase 
)

Definition at line 584 of file streamutil.c.

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

587 {
588  PQExpBuffer query;
589  PGresult *res;
590  bool use_new_option_syntax = (PQserverVersion(conn) >= 150000);
591 
592  query = createPQExpBuffer();
593 
594  Assert((is_physical && plugin == NULL) ||
595  (!is_physical && plugin != NULL));
596  Assert(!(two_phase && is_physical));
597  Assert(slot_name != NULL);
598 
599  /* Build base portion of query */
600  appendPQExpBuffer(query, "CREATE_REPLICATION_SLOT \"%s\"", slot_name);
601  if (is_temporary)
602  appendPQExpBufferStr(query, " TEMPORARY");
603  if (is_physical)
604  appendPQExpBufferStr(query, " PHYSICAL");
605  else
606  appendPQExpBuffer(query, " LOGICAL \"%s\"", plugin);
607 
608  /* Add any requested options */
609  if (use_new_option_syntax)
610  appendPQExpBufferStr(query, " (");
611  if (is_physical)
612  {
613  if (reserve_wal)
614  AppendPlainCommandOption(query, use_new_option_syntax,
615  "RESERVE_WAL");
616  }
617  else
618  {
619  if (two_phase && PQserverVersion(conn) >= 150000)
620  AppendPlainCommandOption(query, use_new_option_syntax,
621  "TWO_PHASE");
622 
623  if (PQserverVersion(conn) >= 100000)
624  {
625  /* pg_recvlogical doesn't use an exported snapshot, so suppress */
626  if (use_new_option_syntax)
627  AppendStringCommandOption(query, use_new_option_syntax,
628  "SNAPSHOT", "nothing");
629  else
630  AppendPlainCommandOption(query, use_new_option_syntax,
631  "NOEXPORT_SNAPSHOT");
632  }
633  }
634  if (use_new_option_syntax)
635  {
636  /* Suppress option list if it would be empty, otherwise terminate */
637  if (query->data[query->len - 1] == '(')
638  {
639  query->len -= 2;
640  query->data[query->len] = '\0';
641  }
642  else
643  appendPQExpBufferChar(query, ')');
644  }
645 
646  /* Now run the query */
647  res = PQexec(conn, query->data);
648  if (PQresultStatus(res) != PGRES_TUPLES_OK)
649  {
650  const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
651 
652  if (slot_exists_ok &&
653  sqlstate &&
654  strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
655  {
656  destroyPQExpBuffer(query);
657  PQclear(res);
658  return true;
659  }
660  else
661  {
662  pg_log_error("could not send replication command \"%s\": %s",
663  query->data, PQerrorMessage(conn));
664 
665  destroyPQExpBuffer(query);
666  PQclear(res);
667  return false;
668  }
669  }
670 
671  if (PQntuples(res) != 1 || PQnfields(res) != 4)
672  {
673  pg_log_error("could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
674  slot_name,
675  PQntuples(res), PQnfields(res), 1, 4);
676 
677  destroyPQExpBuffer(query);
678  PQclear(res);
679  return false;
680  }
681 
682  destroyPQExpBuffer(query);
683  PQclear(res);
684  return true;
685 }
static const char * plugin
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3256
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
#define pg_log_error(...)
Definition: logging.h:80
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void AppendStringCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name, char *option_value)
Definition: streamutil.c:762
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6734
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
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 appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
static bool two_phase
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3233
#define Assert(condition)
Definition: c.h:804
void AppendPlainCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name)
Definition: streamutil.c:741
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32

◆ DropReplicationSlot()

bool DropReplicationSlot ( PGconn conn,
const char *  slot_name 
)

Definition at line 692 of file streamutil.c.

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

693 {
694  PQExpBuffer query;
695  PGresult *res;
696 
697  Assert(slot_name != NULL);
698 
699  query = createPQExpBuffer();
700 
701  /* Build query */
702  appendPQExpBuffer(query, "DROP_REPLICATION_SLOT \"%s\"",
703  slot_name);
704  res = PQexec(conn, query->data);
705  if (PQresultStatus(res) != PGRES_COMMAND_OK)
706  {
707  pg_log_error("could not send replication command \"%s\": %s",
708  query->data, PQerrorMessage(conn));
709 
710  destroyPQExpBuffer(query);
711  PQclear(res);
712  return false;
713  }
714 
715  if (PQntuples(res) != 0 || PQnfields(res) != 0)
716  {
717  pg_log_error("could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
718  slot_name,
719  PQntuples(res), PQnfields(res), 0, 0);
720 
721  destroyPQExpBuffer(query);
722  PQclear(res);
723  return false;
724  }
725 
726  destroyPQExpBuffer(query);
727  PQclear(res);
728  return true;
729 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3256
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
#define pg_log_error(...)
Definition: logging.h:80
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
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:804
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193

◆ fe_recvint64()

int64 fe_recvint64 ( char *  buf)

Definition at line 863 of file streamutil.c.

References pg_ntoh64.

Referenced by ProcessXLogDataMsg(), and StreamLogicalLog().

864 {
865  uint64 n64;
866 
867  memcpy(&n64, buf, sizeof(n64));
868 
869  return pg_ntoh64(n64);
870 }
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 852 of file streamutil.c.

References pg_hton64.

Referenced by sendFeedback().

853 {
854  uint64 n64 = pg_hton64(i);
855 
856  memcpy(buf, &n64, sizeof(n64));
857 }
#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 798 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().

799 {
800  TimestampTz result;
801  struct timeval tp;
802 
803  gettimeofday(&tp, NULL);
804 
805  result = (TimestampTz) tp.tv_sec -
807  result = (result * USECS_PER_SEC) + tp.tv_usec;
808 
809  return result;
810 }
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 817 of file streamutil.c.

References start_time, and USECS_PER_SEC.

Referenced by CalculateCopyStreamSleeptime(), and StreamLogicalLog().

819 {
820  TimestampTz diff = stop_time - start_time;
821 
822  if (diff <= 0)
823  {
824  *secs = 0;
825  *microsecs = 0;
826  }
827  else
828  {
829  *secs = (long) (diff / USECS_PER_SEC);
830  *microsecs = (int) (diff % USECS_PER_SEC);
831  }
832 }
#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 839 of file streamutil.c.

References start_time.

Referenced by HandleCopyStream(), and StreamLogicalLog().

842 {
843  TimestampTz diff = stop_time - start_time;
844 
845  return (diff >= msec * INT64CONST(1000));
846 }
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("%s", 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 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6709
#define pg_log_error(...)
Definition: logging.h:80
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4231
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6734
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:657
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:5337
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:6572
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:804
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:156
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6795
char * dbuser
Definition: streamutil.c:49
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
static bool RetrieveDataDirCreatePerm(PGconn *conn)
Definition: streamutil.c:355
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6691

◆ GetSlotInformation()

bool GetSlotInformation ( PGconn conn,
const char *  slot_name,
XLogRecPtr restart_lsn,
TimeLineID restart_tli 
)

Definition at line 490 of file streamutil.c.

References appendPQExpBuffer(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), InvalidXLogRecPtr, pg_log_error, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), and PQresultStatus().

Referenced by StreamLog().

492 {
493  PGresult *res;
494  PQExpBuffer query;
495  XLogRecPtr lsn_loc = InvalidXLogRecPtr;
496  TimeLineID tli_loc = 0;
497 
498  if (restart_lsn)
499  *restart_lsn = lsn_loc;
500  if (restart_tli)
501  *restart_tli = tli_loc;
502 
503  query = createPQExpBuffer();
504  appendPQExpBuffer(query, "READ_REPLICATION_SLOT %s", slot_name);
505  res = PQexec(conn, query->data);
506  destroyPQExpBuffer(query);
507 
508  if (PQresultStatus(res) != PGRES_TUPLES_OK)
509  {
510  pg_log_error("could not send replication command \"%s\": %s",
511  "READ_REPLICATION_SLOT", PQerrorMessage(conn));
512  PQclear(res);
513  return false;
514  }
515 
516  /* The command should always return precisely one tuple and three fields */
517  if (PQntuples(res) != 1 || PQnfields(res) != 3)
518  {
519  pg_log_error("could not read replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
520  slot_name, PQntuples(res), PQnfields(res), 1, 3);
521  PQclear(res);
522  return false;
523  }
524 
525  /*
526  * When the slot doesn't exist, the command returns a tuple with NULL
527  * values. This checks only the slot type field.
528  */
529  if (PQgetisnull(res, 0, 0))
530  {
531  pg_log_error("could not find replication slot \"%s\"", slot_name);
532  PQclear(res);
533  return false;
534  }
535 
536  /*
537  * Note that this cannot happen as READ_REPLICATION_SLOT supports only
538  * physical slots, but play it safe.
539  */
540  if (strcmp(PQgetvalue(res, 0, 0), "physical") != 0)
541  {
542  pg_log_error("expected a physical replication slot, got type \"%s\" instead",
543  PQgetvalue(res, 0, 0));
544  PQclear(res);
545  return false;
546  }
547 
548  /* restart LSN */
549  if (!PQgetisnull(res, 0, 1))
550  {
551  uint32 hi,
552  lo;
553 
554  if (sscanf(PQgetvalue(res, 0, 1), "%X/%X", &hi, &lo) != 2)
555  {
556  pg_log_error("could not parse restart_lsn \"%s\" for replication slot \"%s\"",
557  PQgetvalue(res, 0, 1), slot_name);
558  PQclear(res);
559  return false;
560  }
561  lsn_loc = ((uint64) hi) << 32 | lo;
562  }
563 
564  /* current TLI */
565  if (!PQgetisnull(res, 0, 2))
566  tli_loc = (TimeLineID) atol(PQgetvalue(res, 0, 2));
567 
568  PQclear(res);
569 
570  /* Assign results if requested */
571  if (restart_lsn)
572  *restart_lsn = lsn_loc;
573  if (restart_tli)
574  *restart_tli = tli_loc;
575 
576  return true;
577 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3256
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint32 TimeLineID
Definition: xlogdefs.h:59
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
#define pg_log_error(...)
Definition: logging.h:80
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
unsigned int uint32
Definition: c.h:441
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void PQclear(PGresult *res)
Definition: fe-exec.c:694
uint64 XLogRecPtr
Definition: xlogdefs.h:21
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3667

◆ 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:3256
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
#define pg_log_error(...)
Definition: logging.h:80
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6734
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define Assert(condition)
Definition: c.h:804
#define MINIMUM_VERSION_FOR_GROUP_ACCESS
Definition: streamutil.c:44
int data_directory_mode
Definition: globals.c:71
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193

◆ 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%2s", &xlog_val, xlog_unit) != 2)
314  {
315  pg_log_error("WAL segment size could not be parsed");
316  PQclear(res);
317  return false;
318  }
319 
320  PQclear(res);
321 
322  /* set the multiplier based on unit to convert xlog_val to bytes */
323  if (strcmp(xlog_unit, "MB") == 0)
324  multiplier = 1024 * 1024;
325  else if (strcmp(xlog_unit, "GB") == 0)
326  multiplier = 1024 * 1024 * 1024;
327 
328  /* convert and set WalSegSz */
329  WalSegSz = xlog_val * multiplier;
330 
332  {
333  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",
334  "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",
335  WalSegSz),
336  WalSegSz);
337  return false;
338  }
339 
340  return true;
341 }
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:96
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3256
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
#define pg_log_error(...)
Definition: logging.h:80
#define DEFAULT_XLOG_SEG_SIZE
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6734
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
#define MINIMUM_VERSION_FOR_SHOW_CMD
Definition: streamutil.c:39
#define ngettext(s, p, n)
Definition: c.h:1182
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define Assert(condition)
Definition: c.h:804
uint32 WalSegSz
Definition: streamutil.c:34
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193

◆ 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(), ReceiveXlogStream(), 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:3256
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
#define pg_log_error(...)
Definition: logging.h:80
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6734
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:441
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define Assert(condition)
Definition: c.h:804
static XLogRecPtr startpos
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3667

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_types_usage(), check_for_isn_and_int8_passing_mismatch(), check_for_pg_role_prefix(), check_for_prepared_transactions(), check_for_tables_with_oids(), check_for_user_defined_encoding_conversions(), check_for_user_defined_postfix_ops(), 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_libpq_source(), libpq_fetch_file(), libpq_get_current_wal_insert_lsn(), libpq_traverse_files(), libpqrcv_connect(), main(), makeEmptyPGconn(), new_9_0_populate_pg_largeobject_metadata(), old_9_6_invalidate_hash_indexes(), ParallelSlotClearHandler(), ParallelSlotsAdoptConn(), ParallelSlotsTerminate(), 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(), report_extension_updates(), scram_exchange(), scram_init(), set_frozenxids(), setKeepalivesCount(), setup_connection(), sql_conn(), start_postmaster(), StartLogStreamer(), StreamLog(), StreamLogicalLog(), vacuum_all_databases(), vacuum_one_database(), and vacuumlo().

◆ connection_string

char* connection_string = NULL

Definition at line 47 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbgetpassword

int dbgetpassword = 0

Definition at line 52 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbhost

char* dbhost = NULL

Definition at line 48 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbname

◆ dbport

char* dbport = NULL

Definition at line 50 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbuser

char* dbuser = NULL

Definition at line 49 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ password

◆ progname

const char* progname

Definition at line 46 of file streamutil.c.

Referenced by GetConnection().

◆ WalSegSz