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-2025, 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
21#include "common/connect.h"
22#include "common/file_perm.h"
23#include "common/logging.h"
24#include "common/string.h"
25#include "datatype/timestamp.h"
26#include "port/pg_bswap.h"
27#include "pqexpbuffer.h"
28#include "streamutil.h"
29
30#define ERRCODE_DUPLICATE_OBJECT "42710"
31
33
35
36/* SHOW command for replication connection was introduced in version 10 */
37#define MINIMUM_VERSION_FOR_SHOW_CMD 100000
38
39/*
40 * Group access is supported from version 11.
41 */
42#define MINIMUM_VERSION_FOR_GROUP_ACCESS 110000
43
44const char *progname;
45char *connection_string = NULL;
46char *dbhost = NULL;
47char *dbuser = NULL;
48char *dbport = NULL;
49char *dbname = NULL;
50int dbgetpassword = 0; /* 0=auto, -1=never, 1=always */
51static char *password = NULL;
52PGconn *conn = NULL;
53
54/*
55 * Connect to the server. Returns a valid PGconn pointer if connected,
56 * or NULL on non-permanent error. On permanent error, the function will
57 * call exit(1) directly.
58 */
59PGconn *
61{
62 PGconn *tmpconn;
63 int argcount = 7; /* dbname, replication, fallback_app_name,
64 * host, user, port, password */
65 int i;
66 const char **keywords;
67 const char **values;
68 const char *tmpparam;
69 bool need_password;
70 PQconninfoOption *conn_opts = NULL;
71 PQconninfoOption *conn_opt;
72 char *err_msg = NULL;
73
74 /*
75 * pg_recvlogical uses dbname only; others use connection_string only.
76 * (Note: both variables will be NULL if there's no command line options.)
77 */
78 Assert(dbname == NULL || connection_string == NULL);
79
80 /*
81 * Merge the connection info inputs given in form of connection string,
82 * options and default values (dbname=replication, replication=true, etc.)
83 */
84 i = 0;
86 {
87 conn_opts = PQconninfoParse(connection_string, &err_msg);
88 if (conn_opts == NULL)
89 pg_fatal("%s", err_msg);
90
91 for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
92 {
93 if (conn_opt->val != NULL && conn_opt->val[0] != '\0')
94 argcount++;
95 }
96
97 keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
98 values = pg_malloc0((argcount + 1) * sizeof(*values));
99
100 /*
101 * Set dbname here already, so it can be overridden by a dbname in the
102 * connection string.
103 */
104 keywords[i] = "dbname";
105 values[i] = "replication";
106 i++;
107
108 for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
109 {
110 if (conn_opt->val != NULL && conn_opt->val[0] != '\0')
111 {
112 keywords[i] = conn_opt->keyword;
113 values[i] = conn_opt->val;
114 i++;
115 }
116 }
117 }
118 else
119 {
120 keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
121 values = pg_malloc0((argcount + 1) * sizeof(*values));
122 keywords[i] = "dbname";
123 values[i] = (dbname == NULL) ? "replication" : dbname;
124 i++;
125 }
126
127 keywords[i] = "replication";
128 values[i] = (dbname == NULL) ? "true" : "database";
129 i++;
130 keywords[i] = "fallback_application_name";
131 values[i] = progname;
132 i++;
133
134 if (dbhost)
135 {
136 keywords[i] = "host";
137 values[i] = dbhost;
138 i++;
139 }
140 if (dbuser)
141 {
142 keywords[i] = "user";
143 values[i] = dbuser;
144 i++;
145 }
146 if (dbport)
147 {
148 keywords[i] = "port";
149 values[i] = dbport;
150 i++;
151 }
152
153 /* If -W was given, force prompt for password, but only the first time */
154 need_password = (dbgetpassword == 1 && !password);
155
156 do
157 {
158 /* Get a new password if appropriate */
159 if (need_password)
160 {
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 /*
179 * Only expand dbname when we did not already parse the argument as a
180 * connection string ourselves.
181 */
183
184 /*
185 * If there is too little memory even to allocate the PGconn object
186 * and PQconnectdbParams returns NULL, we call exit(1) directly.
187 */
188 if (!tmpconn)
189 pg_fatal("could not connect to server");
190
191 /* If we need a password and -w wasn't given, loop back and get one */
192 if (PQstatus(tmpconn) == CONNECTION_BAD &&
193 PQconnectionNeedsPassword(tmpconn) &&
194 dbgetpassword != -1)
195 {
196 PQfinish(tmpconn);
197 need_password = true;
198 }
199 }
200 while (need_password);
201
202 if (PQstatus(tmpconn) != CONNECTION_OK)
203 {
204 pg_log_error("%s", PQerrorMessage(tmpconn));
205 PQfinish(tmpconn);
206 free(values);
207 free(keywords);
208 PQconninfoFree(conn_opts);
209 return NULL;
210 }
211
212 /* Connection ok! */
213 free(values);
214 free(keywords);
215 PQconninfoFree(conn_opts);
216
217 /*
218 * Set always-secure search path, so malicious users can't get control.
219 * The capacity to run normal SQL queries was added in PostgreSQL 10, so
220 * the search path cannot be changed (by us or attackers) on earlier
221 * versions.
222 */
223 if (dbname != NULL && PQserverVersion(tmpconn) >= 100000)
224 {
225 PGresult *res;
226
229 {
230 pg_log_error("could not clear \"search_path\": %s",
231 PQerrorMessage(tmpconn));
232 PQclear(res);
233 PQfinish(tmpconn);
234 exit(1);
235 }
236 PQclear(res);
237 }
238
239 /*
240 * Ensure we have the same value of integer_datetimes (now always "on") as
241 * the server we are connecting to.
242 */
243 tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
244 if (!tmpparam)
245 {
246 pg_log_error("could not determine server setting for \"integer_datetimes\"");
247 PQfinish(tmpconn);
248 exit(1);
249 }
250
251 if (strcmp(tmpparam, "on") != 0)
252 {
253 pg_log_error("\"integer_datetimes\" compile flag does not match server");
254 PQfinish(tmpconn);
255 exit(1);
256 }
257
258 /*
259 * Retrieve the source data directory mode and use it to construct a umask
260 * for creating directories and files.
261 */
262 if (!RetrieveDataDirCreatePerm(tmpconn))
263 {
264 PQfinish(tmpconn);
265 exit(1);
266 }
267
268 return tmpconn;
269}
270
271/*
272 * From version 10, explicitly set wal segment size using SHOW wal_segment_size
273 * since ControlFile is not accessible here.
274 */
275bool
277{
278 PGresult *res;
279 char xlog_unit[3];
280 int xlog_val,
281 multiplier = 1;
282
283 /* check connection existence */
284 Assert(conn != NULL);
285
286 /* for previous versions set the default xlog seg size */
288 {
290 return true;
291 }
292
293 res = PQexec(conn, "SHOW wal_segment_size");
295 {
296 pg_log_error("could not send replication command \"%s\": %s",
297 "SHOW wal_segment_size", PQerrorMessage(conn));
298
299 PQclear(res);
300 return false;
301 }
302 if (PQntuples(res) != 1 || PQnfields(res) < 1)
303 {
304 pg_log_error("could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields",
305 PQntuples(res), PQnfields(res), 1, 1);
306
307 PQclear(res);
308 return false;
309 }
310
311 /* fetch xlog value and unit from the result */
312 if (sscanf(PQgetvalue(res, 0, 0), "%d%2s", &xlog_val, xlog_unit) != 2)
313 {
314 pg_log_error("WAL segment size could not be parsed");
315 PQclear(res);
316 return false;
317 }
318
319 PQclear(res);
320
321 /* set the multiplier based on unit to convert xlog_val to bytes */
322 if (strcmp(xlog_unit, "MB") == 0)
323 multiplier = 1024 * 1024;
324 else if (strcmp(xlog_unit, "GB") == 0)
325 multiplier = 1024 * 1024 * 1024;
326
327 /* convert and set WalSegSz */
328 WalSegSz = xlog_val * multiplier;
329
331 {
332 pg_log_error(ngettext("remote server reported invalid WAL segment size (%d byte)",
333 "remote server reported invalid WAL segment size (%d bytes)",
334 WalSegSz),
335 WalSegSz);
336 pg_log_error_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
337 return false;
338 }
339
340 return true;
341}
342
343/*
344 * RetrieveDataDirCreatePerm
345 *
346 * This function is used to determine the privileges on the server's PG data
347 * directory and, based on that, set what the permissions will be for
348 * directories and files we create.
349 *
350 * PG11 added support for (optionally) group read/execute rights to be set on
351 * the data directory. Prior to PG11, only the owner was allowed to have rights
352 * on the data directory.
353 */
354static bool
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");
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
395
396 PQclear(res);
397 return true;
398}
399
400/*
401 * Run IDENTIFY_SYSTEM through a given connection and give back to caller
402 * some result information if requested:
403 * - System identifier
404 * - Current timeline ID
405 * - Start LSN position
406 * - Database name (NULL in servers prior to 9.4)
407 */
408bool
409RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli,
410 XLogRecPtr *startpos, char **db_name)
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");
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}
481
482/*
483 * Run READ_REPLICATION_SLOT through a given connection and give back to
484 * caller some result information if requested for this slot:
485 * - Start LSN position, InvalidXLogRecPtr if unknown.
486 * - Current timeline ID, 0 if unknown.
487 * Returns false on failure, and true otherwise.
488 */
489bool
490GetSlotInformation(PGconn *conn, const char *slot_name,
491 XLogRecPtr *restart_lsn, TimeLineID *restart_tli)
492{
493 PGresult *res;
494 PQExpBuffer query;
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
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("replication slot \"%s\" does not exist", 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) atoll(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}
578
579/*
580 * Create a replication slot for the given connection. This function
581 * returns true in case of success.
582 */
583bool
584CreateReplicationSlot(PGconn *conn, const char *slot_name, const char *plugin,
585 bool is_temporary, bool is_physical, bool reserve_wal,
586 bool slot_exists_ok, bool two_phase)
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);
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}
686
687/*
688 * Drop a replication slot for the given connection. This function
689 * returns true in case of success.
690 */
691bool
692DropReplicationSlot(PGconn *conn, const char *slot_name)
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);
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}
730
731/*
732 * Append a "plain" option - one with no value - to a server command that
733 * is being constructed.
734 *
735 * In the old syntax, all options were parser keywords, so you could just
736 * write things like SOME_COMMAND OPTION1 OPTION2 'opt2value' OPTION3 42. The
737 * new syntax uses a comma-separated list surrounded by parentheses, so the
738 * equivalent is SOME_COMMAND (OPTION1, OPTION2 'optvalue', OPTION3 42).
739 */
740void
741AppendPlainCommandOption(PQExpBuffer buf, bool use_new_option_syntax,
742 char *option_name)
743{
744 if (buf->len > 0 && buf->data[buf->len - 1] != '(')
745 {
746 if (use_new_option_syntax)
748 else
750 }
751
752 appendPQExpBuffer(buf, " %s", option_name);
753}
754
755/*
756 * Append an option with an associated string value to a server command that
757 * is being constructed.
758 *
759 * See comments for AppendPlainCommandOption, above.
760 */
761void
762AppendStringCommandOption(PQExpBuffer buf, bool use_new_option_syntax,
763 char *option_name, char *option_value)
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}
777
778/*
779 * Append an option with an associated integer value to a server command
780 * is being constructed.
781 *
782 * See comments for AppendPlainCommandOption, above.
783 */
784void
785AppendIntegerCommandOption(PQExpBuffer buf, bool use_new_option_syntax,
786 char *option_name, int32 option_value)
787{
788 AppendPlainCommandOption(buf, use_new_option_syntax, option_name);
789
790 appendPQExpBuffer(buf, " %d", option_value);
791}
792
793/*
794 * Frontend version of GetCurrentTimestamp(), since we are not linked with
795 * backend code.
796 */
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}
811
812/*
813 * Frontend version of TimestampDifference(), since we are not linked with
814 * backend code.
815 */
816void
818 long *secs, int *microsecs)
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}
833
834/*
835 * Frontend version of TimestampDifferenceExceeds(), since we are not
836 * linked with backend code.
837 */
838bool
840 TimestampTz stop_time,
841 int msec)
842{
843 TimestampTz diff = stop_time - start_time;
844
845 return (diff >= msec * INT64CONST(1000));
846}
847
848/*
849 * Converts an int64 to network byte order.
850 */
851void
853{
854 uint64 n64 = pg_hton64(i);
855
856 memcpy(buf, &n64, sizeof(n64));
857}
858
859/*
860 * Converts an int64 from network byte order to native format.
861 */
862int64
864{
865 uint64 n64;
866
867 memcpy(&n64, buf, sizeof(n64));
868
869 return pg_ntoh64(n64);
870}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define INT64CONST(x)
Definition: c.h:516
#define ngettext(s, p, n)
Definition: c.h:1152
int64_t int64
Definition: c.h:499
int32_t int32
Definition: c.h:498
uint64_t uint64
Definition: c.h:503
uint32_t uint32
Definition: c.h:502
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
int64 TimestampTz
Definition: timestamp.h:39
#define USECS_PER_SEC
Definition: timestamp.h:134
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:234
#define SECS_PER_DAY
Definition: timestamp.h:126
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7543
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:7368
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:6084
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7508
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7606
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7490
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5224
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7553
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:737
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error)
Definition: fe-exec.c:4176
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
void PQclear(PGresult *res)
Definition: fe-exec.c:721
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3466
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3489
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
int data_directory_mode
Definition: globals.c:76
Assert(PointerIsAligned(start, uint64))
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:74
static const JsonPathKeyword keywords[]
@ CONNECTION_BAD
Definition: libpq-fe.h:84
@ CONNECTION_OK
Definition: libpq-fe.h:83
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:124
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:127
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_detail(...)
Definition: logging.h:109
void pfree(void *pointer)
Definition: mcxt.c:1524
void * palloc(Size size)
Definition: mcxt.c:1317
#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:95
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:72
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
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:785
int dbgetpassword
Definition: streamutil.c:50
PGconn * GetConnection(void)
Definition: streamutil.c:60
bool RetrieveWalSegSize(PGconn *conn)
Definition: streamutil.c:276
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30
int WalSegSz
Definition: streamutil.c:32
#define MINIMUM_VERSION_FOR_SHOW_CMD
Definition: streamutil.c:37
char * dbhost
Definition: streamutil.c:46
int64 fe_recvint64(char *buf)
Definition: streamutil.c:863
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:584
static char * password
Definition: streamutil.c:51
TimestampTz feGetCurrentTimestamp(void)
Definition: streamutil.c:798
char * dbport
Definition: streamutil.c:48
#define MINIMUM_VERSION_FOR_GROUP_ACCESS
Definition: streamutil.c:42
void feTimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: streamutil.c:817
void AppendPlainCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name)
Definition: streamutil.c:741
void AppendStringCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name, char *option_value)
Definition: streamutil.c:762
void fe_sendint64(int64 i, char *buf)
Definition: streamutil.c:852
char * connection_string
Definition: streamutil.c:45
bool feTimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: streamutil.c:839
bool GetSlotInformation(PGconn *conn, const char *slot_name, XLogRecPtr *restart_lsn, TimeLineID *restart_tli)
Definition: streamutil.c:490
const char * progname
Definition: streamutil.c:44
static bool RetrieveDataDirCreatePerm(PGconn *conn)
Definition: streamutil.c:355
bool DropReplicationSlot(PGconn *conn, const char *slot_name)
Definition: streamutil.c:692
char * dbname
Definition: streamutil.c:49
PGconn * conn
Definition: streamutil.c:52
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:409
char * dbuser
Definition: streamutil.c:47
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