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