PostgreSQL Source Code  git master
streamutil.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * streamutil.c - utility functions for pg_basebackup, pg_receivewal and
4  * pg_recvlogical
5  *
6  * Author: Magnus Hagander <magnus@hagander.net>
7  *
8  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
9  *
10  * IDENTIFICATION
11  * src/bin/pg_basebackup/streamutil.c
12  *-------------------------------------------------------------------------
13  */
14 
15 #include "postgres_fe.h"
16 
17 #include <sys/time.h>
18 #include <unistd.h>
19 
20 #include "access/xlog_internal.h"
21 #include "common/connect.h"
22 #include "common/fe_memutils.h"
23 #include "common/file_perm.h"
24 #include "common/logging.h"
25 #include "common/string.h"
26 #include "datatype/timestamp.h"
27 #include "port/pg_bswap.h"
28 #include "pqexpbuffer.h"
29 #include "receivelog.h"
30 #include "streamutil.h"
31 
32 #define ERRCODE_DUPLICATE_OBJECT "42710"
33 
35 
37 
38 /* SHOW command for replication connection was introduced in version 10 */
39 #define MINIMUM_VERSION_FOR_SHOW_CMD 100000
40 
41 /*
42  * Group access is supported from version 11.
43  */
44 #define MINIMUM_VERSION_FOR_GROUP_ACCESS 110000
45 
46 const char *progname;
47 char *connection_string = NULL;
48 char *dbhost = NULL;
49 char *dbuser = NULL;
50 char *dbport = NULL;
51 char *dbname = NULL;
52 int dbgetpassword = 0; /* 0=auto, -1=never, 1=always */
53 static char *password = NULL;
54 PGconn *conn = NULL;
55 
56 /*
57  * Connect to the server. Returns a valid PGconn pointer if connected,
58  * or NULL on non-permanent error. On permanent error, the function will
59  * call exit(1) directly.
60  */
61 PGconn *
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  pg_fatal("%s", err_msg);
92 
93  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
94  {
95  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
96  strcmp(conn_opt->keyword, "dbname") != 0)
97  argcount++;
98  }
99 
100  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
101  values = pg_malloc0((argcount + 1) * sizeof(*values));
102 
103  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
104  {
105  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
106  strcmp(conn_opt->keyword, "dbname") != 0)
107  {
108  keywords[i] = conn_opt->keyword;
109  values[i] = conn_opt->val;
110  i++;
111  }
112  }
113  }
114  else
115  {
116  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
117  values = pg_malloc0((argcount + 1) * sizeof(*values));
118  }
119 
120  keywords[i] = "dbname";
121  values[i] = dbname == NULL ? "replication" : dbname;
122  i++;
123  keywords[i] = "replication";
124  values[i] = dbname == NULL ? "true" : "database";
125  i++;
126  keywords[i] = "fallback_application_name";
127  values[i] = progname;
128  i++;
129 
130  if (dbhost)
131  {
132  keywords[i] = "host";
133  values[i] = dbhost;
134  i++;
135  }
136  if (dbuser)
137  {
138  keywords[i] = "user";
139  values[i] = dbuser;
140  i++;
141  }
142  if (dbport)
143  {
144  keywords[i] = "port";
145  values[i] = dbport;
146  i++;
147  }
148 
149  /* If -W was given, force prompt for password, but only the first time */
150  need_password = (dbgetpassword == 1 && !password);
151 
152  do
153  {
154  /* Get a new password if appropriate */
155  if (need_password)
156  {
157  free(password);
158  password = simple_prompt("Password: ", false);
159  need_password = false;
160  }
161 
162  /* Use (or reuse, on a subsequent connection) password if we have it */
163  if (password)
164  {
165  keywords[i] = "password";
166  values[i] = password;
167  }
168  else
169  {
170  keywords[i] = NULL;
171  values[i] = NULL;
172  }
173 
174  tmpconn = PQconnectdbParams(keywords, values, true);
175 
176  /*
177  * If there is too little memory even to allocate the PGconn object
178  * and PQconnectdbParams returns NULL, we call exit(1) directly.
179  */
180  if (!tmpconn)
181  pg_fatal("could not connect to server");
182 
183  /* If we need a password and -w wasn't given, loop back and get one */
184  if (PQstatus(tmpconn) == CONNECTION_BAD &&
185  PQconnectionNeedsPassword(tmpconn) &&
186  dbgetpassword != -1)
187  {
188  PQfinish(tmpconn);
189  need_password = true;
190  }
191  }
192  while (need_password);
193 
194  if (PQstatus(tmpconn) != CONNECTION_OK)
195  {
196  pg_log_error("%s", PQerrorMessage(tmpconn));
197  PQfinish(tmpconn);
198  free(values);
199  free(keywords);
200  PQconninfoFree(conn_opts);
201  return NULL;
202  }
203 
204  /* Connection ok! */
205  free(values);
206  free(keywords);
207  PQconninfoFree(conn_opts);
208 
209  /*
210  * Set always-secure search path, so malicious users can't get control.
211  * The capacity to run normal SQL queries was added in PostgreSQL 10, so
212  * the search path cannot be changed (by us or attackers) on earlier
213  * versions.
214  */
215  if (dbname != NULL && PQserverVersion(tmpconn) >= 100000)
216  {
217  PGresult *res;
218 
221  {
222  pg_log_error("could not clear search_path: %s",
223  PQerrorMessage(tmpconn));
224  PQclear(res);
225  PQfinish(tmpconn);
226  exit(1);
227  }
228  PQclear(res);
229  }
230 
231  /*
232  * Ensure we have the same value of integer_datetimes (now always "on") as
233  * the server we are connecting to.
234  */
235  tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
236  if (!tmpparam)
237  {
238  pg_log_error("could not determine server setting for integer_datetimes");
239  PQfinish(tmpconn);
240  exit(1);
241  }
242 
243  if (strcmp(tmpparam, "on") != 0)
244  {
245  pg_log_error("integer_datetimes compile flag does not match server");
246  PQfinish(tmpconn);
247  exit(1);
248  }
249 
250  /*
251  * Retrieve the source data directory mode and use it to construct a umask
252  * for creating directories and files.
253  */
254  if (!RetrieveDataDirCreatePerm(tmpconn))
255  {
256  PQfinish(tmpconn);
257  exit(1);
258  }
259 
260  return tmpconn;
261 }
262 
263 /*
264  * From version 10, explicitly set wal segment size using SHOW wal_segment_size
265  * since ControlFile is not accessible here.
266  */
267 bool
269 {
270  PGresult *res;
271  char xlog_unit[3];
272  int xlog_val,
273  multiplier = 1;
274 
275  /* check connection existence */
276  Assert(conn != NULL);
277 
278  /* for previous versions set the default xlog seg size */
280  {
282  return true;
283  }
284 
285  res = PQexec(conn, "SHOW wal_segment_size");
287  {
288  pg_log_error("could not send replication command \"%s\": %s",
289  "SHOW wal_segment_size", PQerrorMessage(conn));
290 
291  PQclear(res);
292  return false;
293  }
294  if (PQntuples(res) != 1 || PQnfields(res) < 1)
295  {
296  pg_log_error("could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields",
297  PQntuples(res), PQnfields(res), 1, 1);
298 
299  PQclear(res);
300  return false;
301  }
302 
303  /* fetch xlog value and unit from the result */
304  if (sscanf(PQgetvalue(res, 0, 0), "%d%2s", &xlog_val, xlog_unit) != 2)
305  {
306  pg_log_error("WAL segment size could not be parsed");
307  PQclear(res);
308  return false;
309  }
310 
311  PQclear(res);
312 
313  /* set the multiplier based on unit to convert xlog_val to bytes */
314  if (strcmp(xlog_unit, "MB") == 0)
315  multiplier = 1024 * 1024;
316  else if (strcmp(xlog_unit, "GB") == 0)
317  multiplier = 1024 * 1024 * 1024;
318 
319  /* convert and set WalSegSz */
320  WalSegSz = xlog_val * multiplier;
321 
323  {
324  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",
325  "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",
326  WalSegSz),
327  WalSegSz);
328  return false;
329  }
330 
331  return true;
332 }
333 
334 /*
335  * RetrieveDataDirCreatePerm
336  *
337  * This function is used to determine the privileges on the server's PG data
338  * directory and, based on that, set what the permissions will be for
339  * directories and files we create.
340  *
341  * PG11 added support for (optionally) group read/execute rights to be set on
342  * the data directory. Prior to PG11, only the owner was allowed to have rights
343  * on the data directory.
344  */
345 static bool
347 {
348  PGresult *res;
350 
351  /* check connection existence */
352  Assert(conn != NULL);
353 
354  /* for previous versions leave the default group access */
356  return true;
357 
358  res = PQexec(conn, "SHOW data_directory_mode");
360  {
361  pg_log_error("could not send replication command \"%s\": %s",
362  "SHOW data_directory_mode", PQerrorMessage(conn));
363 
364  PQclear(res);
365  return false;
366  }
367  if (PQntuples(res) != 1 || PQnfields(res) < 1)
368  {
369  pg_log_error("could not fetch group access flag: got %d rows and %d fields, expected %d rows and %d or more fields",
370  PQntuples(res), PQnfields(res), 1, 1);
371 
372  PQclear(res);
373  return false;
374  }
375 
376  if (sscanf(PQgetvalue(res, 0, 0), "%o", &data_directory_mode) != 1)
377  {
378  pg_log_error("group access flag could not be parsed: %s",
379  PQgetvalue(res, 0, 0));
380 
381  PQclear(res);
382  return false;
383  }
384 
386 
387  PQclear(res);
388  return true;
389 }
390 
391 /*
392  * Run IDENTIFY_SYSTEM through a given connection and give back to caller
393  * some result information if requested:
394  * - System identifier
395  * - Current timeline ID
396  * - Start LSN position
397  * - Database name (NULL in servers prior to 9.4)
398  */
399 bool
400 RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli,
401  XLogRecPtr *startpos, char **db_name)
402 {
403  PGresult *res;
404  uint32 hi,
405  lo;
406 
407  /* Check connection existence */
408  Assert(conn != NULL);
409 
410  res = PQexec(conn, "IDENTIFY_SYSTEM");
412  {
413  pg_log_error("could not send replication command \"%s\": %s",
414  "IDENTIFY_SYSTEM", PQerrorMessage(conn));
415 
416  PQclear(res);
417  return false;
418  }
419  if (PQntuples(res) != 1 || PQnfields(res) < 3)
420  {
421  pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields",
422  PQntuples(res), PQnfields(res), 1, 3);
423 
424  PQclear(res);
425  return false;
426  }
427 
428  /* Get system identifier */
429  if (sysid != NULL)
430  *sysid = pg_strdup(PQgetvalue(res, 0, 0));
431 
432  /* Get timeline ID to start streaming from */
433  if (starttli != NULL)
434  *starttli = atoi(PQgetvalue(res, 0, 1));
435 
436  /* Get LSN start position if necessary */
437  if (startpos != NULL)
438  {
439  if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
440  {
441  pg_log_error("could not parse write-ahead log location \"%s\"",
442  PQgetvalue(res, 0, 2));
443 
444  PQclear(res);
445  return false;
446  }
447  *startpos = ((uint64) hi) << 32 | lo;
448  }
449 
450  /* Get database name, only available in 9.4 and newer versions */
451  if (db_name != NULL)
452  {
453  *db_name = NULL;
454  if (PQserverVersion(conn) >= 90400)
455  {
456  if (PQnfields(res) < 4)
457  {
458  pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields",
459  PQntuples(res), PQnfields(res), 1, 4);
460 
461  PQclear(res);
462  return false;
463  }
464  if (!PQgetisnull(res, 0, 3))
465  *db_name = pg_strdup(PQgetvalue(res, 0, 3));
466  }
467  }
468 
469  PQclear(res);
470  return true;
471 }
472 
473 /*
474  * Run READ_REPLICATION_SLOT through a given connection and give back to
475  * caller some result information if requested for this slot:
476  * - Start LSN position, InvalidXLogRecPtr if unknown.
477  * - Current timeline ID, 0 if unknown.
478  * Returns false on failure, and true otherwise.
479  */
480 bool
481 GetSlotInformation(PGconn *conn, const char *slot_name,
482  XLogRecPtr *restart_lsn, TimeLineID *restart_tli)
483 {
484  PGresult *res;
485  PQExpBuffer query;
486  XLogRecPtr lsn_loc = InvalidXLogRecPtr;
487  TimeLineID tli_loc = 0;
488 
489  if (restart_lsn)
490  *restart_lsn = lsn_loc;
491  if (restart_tli)
492  *restart_tli = tli_loc;
493 
494  query = createPQExpBuffer();
495  appendPQExpBuffer(query, "READ_REPLICATION_SLOT %s", slot_name);
496  res = PQexec(conn, query->data);
497  destroyPQExpBuffer(query);
498 
500  {
501  pg_log_error("could not send replication command \"%s\": %s",
502  "READ_REPLICATION_SLOT", PQerrorMessage(conn));
503  PQclear(res);
504  return false;
505  }
506 
507  /* The command should always return precisely one tuple and three fields */
508  if (PQntuples(res) != 1 || PQnfields(res) != 3)
509  {
510  pg_log_error("could not read replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
511  slot_name, PQntuples(res), PQnfields(res), 1, 3);
512  PQclear(res);
513  return false;
514  }
515 
516  /*
517  * When the slot doesn't exist, the command returns a tuple with NULL
518  * values. This checks only the slot type field.
519  */
520  if (PQgetisnull(res, 0, 0))
521  {
522  pg_log_error("replication slot \"%s\" does not exist", slot_name);
523  PQclear(res);
524  return false;
525  }
526 
527  /*
528  * Note that this cannot happen as READ_REPLICATION_SLOT supports only
529  * physical slots, but play it safe.
530  */
531  if (strcmp(PQgetvalue(res, 0, 0), "physical") != 0)
532  {
533  pg_log_error("expected a physical replication slot, got type \"%s\" instead",
534  PQgetvalue(res, 0, 0));
535  PQclear(res);
536  return false;
537  }
538 
539  /* restart LSN */
540  if (!PQgetisnull(res, 0, 1))
541  {
542  uint32 hi,
543  lo;
544 
545  if (sscanf(PQgetvalue(res, 0, 1), "%X/%X", &hi, &lo) != 2)
546  {
547  pg_log_error("could not parse restart_lsn \"%s\" for replication slot \"%s\"",
548  PQgetvalue(res, 0, 1), slot_name);
549  PQclear(res);
550  return false;
551  }
552  lsn_loc = ((uint64) hi) << 32 | lo;
553  }
554 
555  /* current TLI */
556  if (!PQgetisnull(res, 0, 2))
557  tli_loc = (TimeLineID) atol(PQgetvalue(res, 0, 2));
558 
559  PQclear(res);
560 
561  /* Assign results if requested */
562  if (restart_lsn)
563  *restart_lsn = lsn_loc;
564  if (restart_tli)
565  *restart_tli = tli_loc;
566 
567  return true;
568 }
569 
570 /*
571  * Create a replication slot for the given connection. This function
572  * returns true in case of success.
573  */
574 bool
575 CreateReplicationSlot(PGconn *conn, const char *slot_name, const char *plugin,
576  bool is_temporary, bool is_physical, bool reserve_wal,
577  bool slot_exists_ok, bool two_phase)
578 {
579  PQExpBuffer query;
580  PGresult *res;
581  bool use_new_option_syntax = (PQserverVersion(conn) >= 150000);
582 
583  query = createPQExpBuffer();
584 
585  Assert((is_physical && plugin == NULL) ||
586  (!is_physical && plugin != NULL));
587  Assert(!(two_phase && is_physical));
588  Assert(slot_name != NULL);
589 
590  /* Build base portion of query */
591  appendPQExpBuffer(query, "CREATE_REPLICATION_SLOT \"%s\"", slot_name);
592  if (is_temporary)
593  appendPQExpBufferStr(query, " TEMPORARY");
594  if (is_physical)
595  appendPQExpBufferStr(query, " PHYSICAL");
596  else
597  appendPQExpBuffer(query, " LOGICAL \"%s\"", plugin);
598 
599  /* Add any requested options */
600  if (use_new_option_syntax)
601  appendPQExpBufferStr(query, " (");
602  if (is_physical)
603  {
604  if (reserve_wal)
605  AppendPlainCommandOption(query, use_new_option_syntax,
606  "RESERVE_WAL");
607  }
608  else
609  {
610  if (two_phase && PQserverVersion(conn) >= 150000)
611  AppendPlainCommandOption(query, use_new_option_syntax,
612  "TWO_PHASE");
613 
614  if (PQserverVersion(conn) >= 100000)
615  {
616  /* pg_recvlogical doesn't use an exported snapshot, so suppress */
617  if (use_new_option_syntax)
618  AppendStringCommandOption(query, use_new_option_syntax,
619  "SNAPSHOT", "nothing");
620  else
621  AppendPlainCommandOption(query, use_new_option_syntax,
622  "NOEXPORT_SNAPSHOT");
623  }
624  }
625  if (use_new_option_syntax)
626  {
627  /* Suppress option list if it would be empty, otherwise terminate */
628  if (query->data[query->len - 1] == '(')
629  {
630  query->len -= 2;
631  query->data[query->len] = '\0';
632  }
633  else
634  appendPQExpBufferChar(query, ')');
635  }
636 
637  /* Now run the query */
638  res = PQexec(conn, query->data);
640  {
641  const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
642 
643  if (slot_exists_ok &&
644  sqlstate &&
645  strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
646  {
647  destroyPQExpBuffer(query);
648  PQclear(res);
649  return true;
650  }
651  else
652  {
653  pg_log_error("could not send replication command \"%s\": %s",
654  query->data, PQerrorMessage(conn));
655 
656  destroyPQExpBuffer(query);
657  PQclear(res);
658  return false;
659  }
660  }
661 
662  if (PQntuples(res) != 1 || PQnfields(res) != 4)
663  {
664  pg_log_error("could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
665  slot_name,
666  PQntuples(res), PQnfields(res), 1, 4);
667 
668  destroyPQExpBuffer(query);
669  PQclear(res);
670  return false;
671  }
672 
673  destroyPQExpBuffer(query);
674  PQclear(res);
675  return true;
676 }
677 
678 /*
679  * Drop a replication slot for the given connection. This function
680  * returns true in case of success.
681  */
682 bool
683 DropReplicationSlot(PGconn *conn, const char *slot_name)
684 {
685  PQExpBuffer query;
686  PGresult *res;
687 
688  Assert(slot_name != NULL);
689 
690  query = createPQExpBuffer();
691 
692  /* Build query */
693  appendPQExpBuffer(query, "DROP_REPLICATION_SLOT \"%s\"",
694  slot_name);
695  res = PQexec(conn, query->data);
697  {
698  pg_log_error("could not send replication command \"%s\": %s",
699  query->data, PQerrorMessage(conn));
700 
701  destroyPQExpBuffer(query);
702  PQclear(res);
703  return false;
704  }
705 
706  if (PQntuples(res) != 0 || PQnfields(res) != 0)
707  {
708  pg_log_error("could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
709  slot_name,
710  PQntuples(res), PQnfields(res), 0, 0);
711 
712  destroyPQExpBuffer(query);
713  PQclear(res);
714  return false;
715  }
716 
717  destroyPQExpBuffer(query);
718  PQclear(res);
719  return true;
720 }
721 
722 /*
723  * Append a "plain" option - one with no value - to a server command that
724  * is being constructed.
725  *
726  * In the old syntax, all options were parser keywords, so you could just
727  * write things like SOME_COMMAND OPTION1 OPTION2 'opt2value' OPTION3 42. The
728  * new syntax uses a comma-separated list surrounded by parentheses, so the
729  * equivalent is SOME_COMMAND (OPTION1, OPTION2 'optvalue', OPTION3 42).
730  */
731 void
732 AppendPlainCommandOption(PQExpBuffer buf, bool use_new_option_syntax,
733  char *option_name)
734 {
735  if (buf->len > 0 && buf->data[buf->len - 1] != '(')
736  {
737  if (use_new_option_syntax)
738  appendPQExpBufferStr(buf, ", ");
739  else
741  }
742 
743  appendPQExpBuffer(buf, " %s", option_name);
744 }
745 
746 /*
747  * Append an option with an associated string value to a server command that
748  * is being constructed.
749  *
750  * See comments for AppendPlainCommandOption, above.
751  */
752 void
753 AppendStringCommandOption(PQExpBuffer buf, bool use_new_option_syntax,
754  char *option_name, char *option_value)
755 {
756  AppendPlainCommandOption(buf, use_new_option_syntax, option_name);
757 
758  if (option_value != NULL)
759  {
760  size_t length = strlen(option_value);
761  char *escaped_value = palloc(1 + 2 * length);
762 
763  PQescapeStringConn(conn, escaped_value, option_value, length, NULL);
764  appendPQExpBuffer(buf, " '%s'", escaped_value);
765  pfree(escaped_value);
766  }
767 }
768 
769 /*
770  * Append an option with an associated integer value to a server command
771  * is being constructed.
772  *
773  * See comments for AppendPlainCommandOption, above.
774  */
775 void
776 AppendIntegerCommandOption(PQExpBuffer buf, bool use_new_option_syntax,
777  char *option_name, int32 option_value)
778 {
779  AppendPlainCommandOption(buf, use_new_option_syntax, option_name);
780 
781  appendPQExpBuffer(buf, " %d", option_value);
782 }
783 
784 /*
785  * Frontend version of GetCurrentTimestamp(), since we are not linked with
786  * backend code.
787  */
790 {
791  TimestampTz result;
792  struct timeval tp;
793 
794  gettimeofday(&tp, NULL);
795 
796  result = (TimestampTz) tp.tv_sec -
798  result = (result * USECS_PER_SEC) + tp.tv_usec;
799 
800  return result;
801 }
802 
803 /*
804  * Frontend version of TimestampDifference(), since we are not linked with
805  * backend code.
806  */
807 void
809  long *secs, int *microsecs)
810 {
811  TimestampTz diff = stop_time - start_time;
812 
813  if (diff <= 0)
814  {
815  *secs = 0;
816  *microsecs = 0;
817  }
818  else
819  {
820  *secs = (long) (diff / USECS_PER_SEC);
821  *microsecs = (int) (diff % USECS_PER_SEC);
822  }
823 }
824 
825 /*
826  * Frontend version of TimestampDifferenceExceeds(), since we are not
827  * linked with backend code.
828  */
829 bool
831  TimestampTz stop_time,
832  int msec)
833 {
834  TimestampTz diff = stop_time - start_time;
835 
836  return (diff >= msec * INT64CONST(1000));
837 }
838 
839 /*
840  * Converts an int64 to network byte order.
841  */
842 void
843 fe_sendint64(int64 i, char *buf)
844 {
845  uint64 n64 = pg_hton64(i);
846 
847  memcpy(buf, &n64, sizeof(n64));
848 }
849 
850 /*
851  * Converts an int64 from network byte order to native format.
852  */
853 int64
855 {
856  uint64 n64;
857 
858  memcpy(&n64, buf, sizeof(n64));
859 
860  return pg_ntoh64(n64);
861 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
unsigned int uint32
Definition: c.h:490
signed int int32
Definition: c.h:478
#define ngettext(s, p, n)
Definition: c.h:1189
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
int64 TimestampTz
Definition: timestamp.h:39
#define USECS_PER_SEC
Definition: timestamp.h:133
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:208
#define SECS_PER_DAY
Definition: timestamp.h:125
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:209
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7210
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7235
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:5826
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:678
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:7078
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7296
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7245
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7192
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4599
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error)
Definition: fe-exec.c:3982
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3244
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3314
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2228
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3709
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3734
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3299
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3322
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
int data_directory_mode
Definition: globals.c:72
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:73
@ CONNECTION_BAD
Definition: libpq-fe.h:61
@ CONNECTION_OK
Definition: libpq-fe.h:60
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:100
Assert(fmt[strlen(fmt) - 1] !='\n')
exit(1)
#define pg_log_error(...)
Definition: logging.h:106
void pfree(void *pointer)
Definition: mcxt.c:1456
void * palloc(Size size)
Definition: mcxt.c:1226
#define pg_fatal(...)
#define pg_hton64(x)
Definition: pg_bswap.h:122
#define pg_ntoh64(x)
Definition: pg_bswap.h:126
#define DEFAULT_XLOG_SEG_SIZE
static time_t start_time
Definition: pg_ctl.c:94
static bool slot_exists_ok
Definition: pg_receivewal.c:50
static bool two_phase
static const char * plugin
static XLogRecPtr startpos
static char * buf
Definition: pg_test_fsync.c:67
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:56
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
void AppendIntegerCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name, int32 option_value)
Definition: streamutil.c:776
int dbgetpassword
Definition: streamutil.c:52
bool RetrieveWalSegSize(PGconn *conn)
Definition: streamutil.c:268
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
int WalSegSz
Definition: streamutil.c:34
#define MINIMUM_VERSION_FOR_SHOW_CMD
Definition: streamutil.c:39
char * dbhost
Definition: streamutil.c:48
int64 fe_recvint64(char *buf)
Definition: streamutil.c:854
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: streamutil.c:575
static char * password
Definition: streamutil.c:53
TimestampTz feGetCurrentTimestamp(void)
Definition: streamutil.c:789
char * dbport
Definition: streamutil.c:50
#define MINIMUM_VERSION_FOR_GROUP_ACCESS
Definition: streamutil.c:44
void feTimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: streamutil.c:808
void AppendPlainCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name)
Definition: streamutil.c:732
void AppendStringCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name, char *option_value)
Definition: streamutil.c:753
void fe_sendint64(int64 i, char *buf)
Definition: streamutil.c:843
char * connection_string
Definition: streamutil.c:47
PGconn * GetConnection(void)
Definition: streamutil.c:62
bool feTimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: streamutil.c:830
bool GetSlotInformation(PGconn *conn, const char *slot_name, XLogRecPtr *restart_lsn, TimeLineID *restart_tli)
Definition: streamutil.c:481
const char * progname
Definition: streamutil.c:46
static bool RetrieveDataDirCreatePerm(PGconn *conn)
Definition: streamutil.c:346
bool DropReplicationSlot(PGconn *conn, const char *slot_name)
Definition: streamutil.c:683
char * dbname
Definition: streamutil.c:51
PGconn * conn
Definition: streamutil.c:54
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:400
char * dbuser
Definition: streamutil.c:49
int gettimeofday(struct timeval *tp, void *tzp)
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint32 TimeLineID
Definition: xlogdefs.h:59