Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
pg_recvlogical.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <limits.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <unistd.h>
#include "common/file_perm.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "getopt_long.h"
#include "libpq-fe.h"
#include "libpq/pqsignal.h"
#include "libpq/protocol.h"
#include "pqexpbuffer.h"
#include "streamutil.h"
Include dependency graph for pg_recvlogical.c:

Go to the source code of this file.

Macros

#define RECONNECT_SLEEP_TIME   5
 

Enumerations

enum  StreamStopReason { STREAM_STOP_NONE , STREAM_STOP_END_OF_WAL , STREAM_STOP_KEEPALIVE , STREAM_STOP_SIGNAL }
 

Functions

static void usage (void)
 
static void StreamLogicalLog (void)
 
static bool flushAndSendFeedback (PGconn *conn, TimestampTz *now)
 
static void prepareToTerminate (PGconn *conn, XLogRecPtr endpos, StreamStopReason reason, XLogRecPtr lsn)
 
static bool sendFeedback (PGconn *conn, TimestampTz now, bool force, bool replyRequested)
 
static void disconnect_atexit (void)
 
static bool OutputFsync (TimestampTz now)
 
static void sigexit_handler (SIGNAL_ARGS)
 
static void sighup_handler (SIGNAL_ARGS)
 
int main (int argc, char **argv)
 

Variables

static char * outfile = NULL
 
static int verbose = 0
 
static bool two_phase = false
 
static bool failover = false
 
static int noloop = 0
 
static int standby_message_timeout = 10 * 1000
 
static int fsync_interval = 10 * 1000
 
static XLogRecPtr startpos = InvalidXLogRecPtr
 
static XLogRecPtr endpos = InvalidXLogRecPtr
 
static bool do_create_slot = false
 
static bool slot_exists_ok = false
 
static bool do_start_slot = false
 
static bool do_drop_slot = false
 
static char * replication_slot = NULL
 
static char ** options
 
static size_t noptions = 0
 
static const char * plugin = "test_decoding"
 
static int outfd = -1
 
static volatile sig_atomic_t time_to_abort = false
 
static volatile sig_atomic_t stop_reason = STREAM_STOP_NONE
 
static volatile sig_atomic_t output_reopen = false
 
static bool output_isfile
 
static TimestampTz output_last_fsync = -1
 
static bool output_needs_fsync = false
 
static XLogRecPtr output_written_lsn = InvalidXLogRecPtr
 
static XLogRecPtr output_fsync_lsn = InvalidXLogRecPtr
 

Macro Definition Documentation

◆ RECONNECT_SLEEP_TIME

#define RECONNECT_SLEEP_TIME   5

Definition at line 32 of file pg_recvlogical.c.

Enumeration Type Documentation

◆ StreamStopReason

Enumerator
STREAM_STOP_NONE 
STREAM_STOP_END_OF_WAL 
STREAM_STOP_KEEPALIVE 
STREAM_STOP_SIGNAL 

Definition at line 34 of file pg_recvlogical.c.

35{
StreamStopReason
@ STREAM_STOP_KEEPALIVE
@ STREAM_STOP_END_OF_WAL
@ STREAM_STOP_NONE
@ STREAM_STOP_SIGNAL

Function Documentation

◆ disconnect_atexit()

static void disconnect_atexit ( void  )
static

Definition at line 181 of file pg_recvlogical.c.

182{
183 if (conn != NULL)
184 PQfinish(conn);
185}
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5305
PGconn * conn
Definition: streamutil.c:52

References conn, and PQfinish().

Referenced by main().

◆ flushAndSendFeedback()

static bool flushAndSendFeedback ( PGconn conn,
TimestampTz now 
)
static

Definition at line 1048 of file pg_recvlogical.c.

1049{
1050 /* flush data to disk, so that we send a recent flush pointer */
1051 if (!OutputFsync(*now))
1052 return false;
1054 if (!sendFeedback(conn, *now, true, false))
1055 return false;
1056
1057 return true;
1058}
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
static bool OutputFsync(TimestampTz now)
static bool sendFeedback(PGconn *conn, TimestampTz now, bool force, bool replyRequested)
TimestampTz feGetCurrentTimestamp(void)
Definition: streamutil.c:803

References conn, feGetCurrentTimestamp(), now(), OutputFsync(), and sendFeedback().

Referenced by StreamLogicalLog().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 696 of file pg_recvlogical.c.

697{
698 static struct option long_options[] = {
699/* general options */
700 {"file", required_argument, NULL, 'f'},
701 {"fsync-interval", required_argument, NULL, 'F'},
702 {"no-loop", no_argument, NULL, 'n'},
703 {"enable-failover", no_argument, NULL, 5},
704 {"enable-two-phase", no_argument, NULL, 't'},
705 {"two-phase", no_argument, NULL, 't'}, /* deprecated */
706 {"verbose", no_argument, NULL, 'v'},
707 {"version", no_argument, NULL, 'V'},
708 {"help", no_argument, NULL, '?'},
709/* connection options */
710 {"dbname", required_argument, NULL, 'd'},
711 {"host", required_argument, NULL, 'h'},
712 {"port", required_argument, NULL, 'p'},
713 {"username", required_argument, NULL, 'U'},
714 {"no-password", no_argument, NULL, 'w'},
715 {"password", no_argument, NULL, 'W'},
716/* replication options */
717 {"startpos", required_argument, NULL, 'I'},
718 {"endpos", required_argument, NULL, 'E'},
719 {"option", required_argument, NULL, 'o'},
720 {"plugin", required_argument, NULL, 'P'},
721 {"status-interval", required_argument, NULL, 's'},
722 {"slot", required_argument, NULL, 'S'},
723/* action */
724 {"create-slot", no_argument, NULL, 1},
725 {"start", no_argument, NULL, 2},
726 {"drop-slot", no_argument, NULL, 3},
727 {"if-not-exists", no_argument, NULL, 4},
728 {NULL, 0, NULL, 0}
729 };
730 int c;
731 int option_index;
732 uint32 hi,
733 lo;
734 char *db_name;
735
736 pg_logging_init(argv[0]);
737 progname = get_progname(argv[0]);
738 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
739
740 if (argc > 1)
741 {
742 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
743 {
744 usage();
745 exit(0);
746 }
747 else if (strcmp(argv[1], "-V") == 0 ||
748 strcmp(argv[1], "--version") == 0)
749 {
750 puts("pg_recvlogical (PostgreSQL) " PG_VERSION);
751 exit(0);
752 }
753 }
754
755 while ((c = getopt_long(argc, argv, "E:f:F:ntvd:h:p:U:wWI:o:P:s:S:",
756 long_options, &option_index)) != -1)
757 {
758 switch (c)
759 {
760/* general options */
761 case 'f':
763 break;
764 case 'F':
765 if (!option_parse_int(optarg, "-F/--fsync-interval", 0,
766 INT_MAX / 1000,
768 exit(1);
769 fsync_interval *= 1000;
770 break;
771 case 'n':
772 noloop = 1;
773 break;
774 case 't':
775 two_phase = true;
776 break;
777 case 'v':
778 verbose++;
779 break;
780 case 5:
781 failover = true;
782 break;
783/* connection options */
784 case 'd':
786 break;
787 case 'h':
789 break;
790 case 'p':
792 break;
793 case 'U':
795 break;
796 case 'w':
797 dbgetpassword = -1;
798 break;
799 case 'W':
800 dbgetpassword = 1;
801 break;
802/* replication options */
803 case 'I':
804 if (sscanf(optarg, "%X/%08X", &hi, &lo) != 2)
805 pg_fatal("could not parse start position \"%s\"", optarg);
806 startpos = ((uint64) hi) << 32 | lo;
807 break;
808 case 'E':
809 if (sscanf(optarg, "%X/%08X", &hi, &lo) != 2)
810 pg_fatal("could not parse end position \"%s\"", optarg);
811 endpos = ((uint64) hi) << 32 | lo;
812 break;
813 case 'o':
814 {
815 char *data = pg_strdup(optarg);
816 char *val = strchr(data, '=');
817
818 if (val != NULL)
819 {
820 /* remove =; separate data from val */
821 *val = '\0';
822 val++;
823 }
824
825 noptions += 1;
826 options = pg_realloc(options, sizeof(char *) * noptions * 2);
827
828 options[(noptions - 1) * 2] = data;
829 options[(noptions - 1) * 2 + 1] = val;
830 }
831
832 break;
833 case 'P':
835 break;
836 case 's':
837 if (!option_parse_int(optarg, "-s/--status-interval", 0,
838 INT_MAX / 1000,
840 exit(1);
842 break;
843 case 'S':
845 break;
846/* action */
847 case 1:
848 do_create_slot = true;
849 break;
850 case 2:
851 do_start_slot = true;
852 break;
853 case 3:
854 do_drop_slot = true;
855 break;
856 case 4:
857 slot_exists_ok = true;
858 break;
859
860 default:
861 /* getopt_long already emitted a complaint */
862 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
863 exit(1);
864 }
865 }
866
867 /*
868 * Any non-option arguments?
869 */
870 if (optind < argc)
871 {
872 pg_log_error("too many command-line arguments (first is \"%s\")",
873 argv[optind]);
874 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
875 exit(1);
876 }
877
878 /*
879 * Required arguments
880 */
881 if (replication_slot == NULL)
882 {
883 pg_log_error("no slot specified");
884 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
885 exit(1);
886 }
887
888 if (do_start_slot && outfile == NULL)
889 {
890 pg_log_error("no target file specified");
891 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
892 exit(1);
893 }
894
895 if (!do_drop_slot && dbname == NULL)
896 {
897 pg_log_error("no database specified");
898 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
899 exit(1);
900 }
901
903 {
904 pg_log_error("at least one action needs to be specified");
905 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
906 exit(1);
907 }
908
910 {
911 pg_log_error("cannot use --create-slot or --start together with --drop-slot");
912 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
913 exit(1);
914 }
915
917 {
918 pg_log_error("cannot use --create-slot or --drop-slot together with --startpos");
919 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
920 exit(1);
921 }
922
924 {
925 pg_log_error("--endpos may only be specified with --start");
926 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
927 exit(1);
928 }
929
930 if (!do_create_slot)
931 {
932 if (two_phase)
933 {
934 pg_log_error("%s may only be specified with --create-slot", "--enable-two-phase");
935 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
936 exit(1);
937 }
938
939 if (failover)
940 {
941 pg_log_error("%s may only be specified with --create-slot", "--enable-failover");
942 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
943 exit(1);
944 }
945 }
946
947 /*
948 * Obtain a connection to server. Notably, if we need a password, we want
949 * to collect it from the user immediately.
950 */
952 if (!conn)
953 /* Error message already written in GetConnection() */
954 exit(1);
955 atexit(disconnect_atexit);
956
957 /*
958 * Trap signals. (Don't do this until after the initial password prompt,
959 * if one is needed, in GetConnection.)
960 */
961#ifndef WIN32
962 pqsignal(SIGINT, sigexit_handler);
963 pqsignal(SIGTERM, sigexit_handler);
965#endif
966
967 /*
968 * Run IDENTIFY_SYSTEM to check the connection type for each action.
969 * --create-slot and --start actions require a database-specific
970 * replication connection because they handle logical replication slots.
971 * --drop-slot can remove replication slots from any replication
972 * connection without this restriction.
973 */
974 if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
975 exit(1);
976
977 if (!do_drop_slot && db_name == NULL)
978 pg_fatal("could not establish database-specific replication connection");
979
980 /*
981 * Set umask so that directories/files are created with the same
982 * permissions as directories/files in the source data directory.
983 *
984 * pg_mode_mask is set to owner-only by default and then updated in
985 * GetConnection() where we get the mode from the server-side with
986 * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
987 */
988 umask(pg_mode_mask);
989
990 /* Drop a replication slot. */
991 if (do_drop_slot)
992 {
993 if (verbose)
994 pg_log_info("dropping replication slot \"%s\"", replication_slot);
995
997 exit(1);
998 }
999
1000 /* Create a replication slot. */
1001 if (do_create_slot)
1002 {
1003 if (verbose)
1004 pg_log_info("creating replication slot \"%s\"", replication_slot);
1005
1007 false, false, slot_exists_ok, two_phase,
1008 failover))
1009 exit(1);
1011 }
1012
1013 if (!do_start_slot)
1014 exit(0);
1015
1016 /* Stream loop */
1017 while (true)
1018 {
1020 if (time_to_abort)
1021 {
1022 /*
1023 * We've been Ctrl-C'ed or reached an exit limit condition. That's
1024 * not an error, so exit without an errorcode.
1025 */
1026 exit(0);
1027 }
1028 else if (noloop)
1029 pg_fatal("disconnected");
1030 else
1031 {
1032 /* translator: check source for value for %d */
1033 pg_log_info("disconnected; waiting %d seconds to try again",
1036 }
1037 }
1038}
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1214
uint64_t uint64
Definition: c.h:540
uint32_t uint32
Definition: c.h:539
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
Definition: connection.c:205
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
int pg_mode_mask
Definition: file_perm.c:25
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:25
#define required_argument
Definition: getopt_long.h:26
long val
Definition: informix.c:689
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
#define pg_log_info(...)
Definition: logging.h:124
const char * progname
Definition: main.c:44
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
Definition: option_utils.c:50
#define pg_fatal(...)
const void * data
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
static int verbose
static int noloop
static bool do_start_slot
static void sighup_handler(SIGNAL_ARGS)
static bool two_phase
static void StreamLogicalLog(void)
static XLogRecPtr endpos
static bool failover
static const char * plugin
static volatile sig_atomic_t time_to_abort
static bool slot_exists_ok
static int fsync_interval
static bool do_drop_slot
static char * replication_slot
static void sigexit_handler(SIGNAL_ARGS)
static size_t noptions
static char * outfile
static void usage(void)
static XLogRecPtr startpos
#define RECONNECT_SLEEP_TIME
static bool do_create_slot
static void disconnect_atexit(void)
static int standby_message_timeout
#define pqsignal
Definition: port.h:531
const char * get_progname(const char *argv0)
Definition: path.c:652
char * c
void pg_usleep(long microsec)
Definition: signal.c:53
int dbgetpassword
Definition: streamutil.c:50
char * dbhost
Definition: streamutil.c:46
char * dbport
Definition: streamutil.c:48
char * dbname
Definition: streamutil.c:49
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:409
char * dbuser
Definition: streamutil.c:47
static void CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
Definition: walsender.c:1181
static void DropReplicationSlot(DropReplicationSlotCmd *cmd)
Definition: walsender.c:1386
#define SIGHUP
Definition: win32_port.h:158
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References conn, CreateReplicationSlot(), data, dbgetpassword, dbhost, dbname, dbport, dbuser, disconnect_atexit(), do_create_slot, do_drop_slot, do_start_slot, DropReplicationSlot(), endpos, failover, fsync_interval, get_progname(), GetConnection(), getopt_long(), InvalidXLogRecPtr, no_argument, noloop, noptions, optarg, optind, option_parse_int(), outfile, pg_fatal, pg_log_error, pg_log_error_hint, pg_log_info, pg_logging_init(), pg_mode_mask, pg_realloc(), pg_strdup(), PG_TEXTDOMAIN, pg_usleep(), plugin, pqsignal, progname, RECONNECT_SLEEP_TIME, replication_slot, required_argument, RunIdentifySystem(), set_pglocale_pgservice(), sigexit_handler(), SIGHUP, sighup_handler(), slot_exists_ok, standby_message_timeout, startpos, StreamLogicalLog(), time_to_abort, two_phase, usage(), val, and verbose.

◆ OutputFsync()

static bool OutputFsync ( TimestampTz  now)
static

Definition at line 188 of file pg_recvlogical.c.

189{
191
193
194 if (fsync_interval <= 0)
195 return true;
196
198 return true;
199
200 output_needs_fsync = false;
201
202 /* can only fsync if it's a regular file */
203 if (!output_isfile)
204 return true;
205
206 if (fsync(outfd) != 0)
207 pg_fatal("could not fsync file \"%s\": %m", outfile);
208
209 return true;
210}
static bool output_needs_fsync
static bool output_isfile
static XLogRecPtr output_written_lsn
static int outfd
static TimestampTz output_last_fsync
static XLogRecPtr output_fsync_lsn
#define fsync(fd)
Definition: win32_port.h:83

References fsync, fsync_interval, now(), outfd, outfile, output_fsync_lsn, output_isfile, output_last_fsync, output_needs_fsync, output_written_lsn, and pg_fatal.

Referenced by flushAndSendFeedback(), and StreamLogicalLog().

◆ prepareToTerminate()

static void prepareToTerminate ( PGconn conn,
XLogRecPtr  endpos,
StreamStopReason  reason,
XLogRecPtr  lsn 
)
static

Definition at line 1065 of file pg_recvlogical.c.

1067{
1068 (void) PQputCopyEnd(conn, NULL);
1069 (void) PQflush(conn);
1070
1071 if (verbose)
1072 {
1073 switch (reason)
1074 {
1075 case STREAM_STOP_SIGNAL:
1076 pg_log_info("received interrupt signal, exiting");
1077 break;
1079 pg_log_info("end position %X/%08X reached by keepalive",
1081 break;
1084 pg_log_info("end position %X/%08X reached by WAL record at %X/%08X",
1086 break;
1087 case STREAM_STOP_NONE:
1088 Assert(false);
1089 break;
1090 }
1091 }
1092}
int PQflush(PGconn *conn)
Definition: fe-exec.c:4011
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2760
Assert(PointerIsAligned(start, uint64))
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:46
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29

References Assert(), conn, endpos, LSN_FORMAT_ARGS, pg_log_info, PQflush(), PQputCopyEnd(), STREAM_STOP_END_OF_WAL, STREAM_STOP_KEEPALIVE, STREAM_STOP_NONE, STREAM_STOP_SIGNAL, verbose, and XLogRecPtrIsInvalid.

Referenced by StreamLogicalLog().

◆ sendFeedback()

static bool sendFeedback ( PGconn conn,
TimestampTz  now,
bool  force,
bool  replyRequested 
)
static

Definition at line 129 of file pg_recvlogical.c.

130{
131 static XLogRecPtr last_written_lsn = InvalidXLogRecPtr;
132 static XLogRecPtr last_fsync_lsn = InvalidXLogRecPtr;
133
134 char replybuf[1 + 8 + 8 + 8 + 8 + 1];
135 int len = 0;
136
137 /*
138 * we normally don't want to send superfluous feedback, but if it's
139 * because of a timeout we need to, otherwise wal_sender_timeout will kill
140 * us.
141 */
142 if (!force &&
143 last_written_lsn == output_written_lsn &&
144 last_fsync_lsn == output_fsync_lsn)
145 return true;
146
147 if (verbose)
148 pg_log_info("confirming write up to %X/%08X, flush to %X/%08X (slot %s)",
152
154 len += 1;
155 fe_sendint64(output_written_lsn, &replybuf[len]); /* write */
156 len += 8;
157 fe_sendint64(output_fsync_lsn, &replybuf[len]); /* flush */
158 len += 8;
159 fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */
160 len += 8;
161 fe_sendint64(now, &replybuf[len]); /* sendTime */
162 len += 8;
163 replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */
164 len += 1;
165
167 last_written_lsn = output_written_lsn;
168 last_fsync_lsn = output_fsync_lsn;
169
170 if (PQputCopyData(conn, replybuf, len) <= 0 || PQflush(conn))
171 {
172 pg_log_error("could not send feedback packet: %s",
174 return false;
175 }
176
177 return true;
178}
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7679
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2706
const void size_t len
#define PqReplMsg_StandbyStatusUpdate
Definition: protocol.h:84
void fe_sendint64(int64 i, char *buf)
Definition: streamutil.c:857
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References conn, fe_sendint64(), InvalidXLogRecPtr, len, LSN_FORMAT_ARGS, now(), output_fsync_lsn, output_written_lsn, pg_log_error, pg_log_info, PQerrorMessage(), PQflush(), PQputCopyData(), PqReplMsg_StandbyStatusUpdate, replication_slot, startpos, and verbose.

Referenced by flushAndSendFeedback(), and StreamLogicalLog().

◆ sigexit_handler()

static void sigexit_handler ( SIGNAL_ARGS  )
static

Definition at line 678 of file pg_recvlogical.c.

679{
681 time_to_abort = true;
682}
static volatile sig_atomic_t stop_reason

References stop_reason, STREAM_STOP_SIGNAL, and time_to_abort.

Referenced by main().

◆ sighup_handler()

static void sighup_handler ( SIGNAL_ARGS  )
static

Definition at line 688 of file pg_recvlogical.c.

689{
690 output_reopen = true;
691}
static volatile sig_atomic_t output_reopen

References output_reopen.

Referenced by main().

◆ StreamLogicalLog()

static void StreamLogicalLog ( void  )
static

Definition at line 216 of file pg_recvlogical.c.

217{
218 PGresult *res;
219 char *copybuf = NULL;
220 TimestampTz last_status = -1;
221 int i;
222 PQExpBuffer query;
223 XLogRecPtr cur_record_lsn;
224
227 cur_record_lsn = InvalidXLogRecPtr;
228
229 /*
230 * Connect in replication mode to the server
231 */
232 if (!conn)
234 if (!conn)
235 /* Error message already written in GetConnection() */
236 return;
237
238 /*
239 * Start the replication
240 */
241 if (verbose)
242 pg_log_info("starting log streaming at %X/%08X (slot %s)",
245
246 /* Initiate the replication stream at specified location */
247 query = createPQExpBuffer();
248 appendPQExpBuffer(query, "START_REPLICATION SLOT \"%s\" LOGICAL %X/%08X",
250
251 /* print options if there are any */
252 if (noptions)
253 appendPQExpBufferStr(query, " (");
254
255 for (i = 0; i < noptions; i++)
256 {
257 /* separator */
258 if (i > 0)
259 appendPQExpBufferStr(query, ", ");
260
261 /* write option name */
262 appendPQExpBuffer(query, "\"%s\"", options[(i * 2)]);
263
264 /* write option value if specified */
265 if (options[(i * 2) + 1] != NULL)
266 appendPQExpBuffer(query, " '%s'", options[(i * 2) + 1]);
267 }
268
269 if (noptions)
270 appendPQExpBufferChar(query, ')');
271
272 res = PQexec(conn, query->data);
274 {
275 pg_log_error("could not send replication command \"%s\": %s",
276 query->data, PQresultErrorMessage(res));
277 PQclear(res);
278 goto error;
279 }
280 PQclear(res);
281 resetPQExpBuffer(query);
282
283 if (verbose)
284 pg_log_info("streaming initiated");
285
286 while (!time_to_abort)
287 {
288 int r;
289 int bytes_left;
290 int bytes_written;
292 int hdr_len;
293
294 cur_record_lsn = InvalidXLogRecPtr;
295
296 if (copybuf != NULL)
297 {
299 copybuf = NULL;
300 }
301
302 /*
303 * Potentially send a status message to the primary.
304 */
306
307 if (outfd != -1 &&
310 {
311 if (!OutputFsync(now))
312 goto error;
313 }
314
315 if (standby_message_timeout > 0 &&
318 {
319 /* Time to send feedback! */
320 if (!sendFeedback(conn, now, true, false))
321 goto error;
322
323 last_status = now;
324 }
325
326 /* got SIGHUP, close output file */
327 if (outfd != -1 && output_reopen && strcmp(outfile, "-") != 0)
328 {
330 if (!OutputFsync(now))
331 goto error;
332 close(outfd);
333 outfd = -1;
334 }
335 output_reopen = false;
336
337 /* open the output file, if not open yet */
338 if (outfd == -1)
339 {
340 struct stat statbuf;
341
342 if (strcmp(outfile, "-") == 0)
343 outfd = fileno(stdout);
344 else
345 outfd = open(outfile, O_CREAT | O_APPEND | O_WRONLY | PG_BINARY,
346 S_IRUSR | S_IWUSR);
347 if (outfd == -1)
348 {
349 pg_log_error("could not open log file \"%s\": %m", outfile);
350 goto error;
351 }
352
353 if (fstat(outfd, &statbuf) != 0)
354 {
355 pg_log_error("could not stat file \"%s\": %m", outfile);
356 goto error;
357 }
358
359 output_isfile = S_ISREG(statbuf.st_mode) && !isatty(outfd);
360 }
361
362 r = PQgetCopyData(conn, &copybuf, 1);
363 if (r == 0)
364 {
365 /*
366 * In async mode, and no data available. We block on reading but
367 * not more than the specified timeout, so that we can send a
368 * response back to the client.
369 */
370 fd_set input_mask;
371 TimestampTz message_target = 0;
372 TimestampTz fsync_target = 0;
373 struct timeval timeout;
374 struct timeval *timeoutptr = NULL;
375
376 if (PQsocket(conn) < 0)
377 {
378 pg_log_error("invalid socket: %s", PQerrorMessage(conn));
379 goto error;
380 }
381
382 FD_ZERO(&input_mask);
383 FD_SET(PQsocket(conn), &input_mask);
384
385 /* Compute when we need to wakeup to send a keepalive message. */
387 message_target = last_status + (standby_message_timeout - 1) *
388 ((int64) 1000);
389
390 /* Compute when we need to wakeup to fsync the output file. */
392 fsync_target = output_last_fsync + (fsync_interval - 1) *
393 ((int64) 1000);
394
395 /* Now compute when to wakeup. */
396 if (message_target > 0 || fsync_target > 0)
397 {
398 TimestampTz targettime;
399 long secs;
400 int usecs;
401
402 targettime = message_target;
403
404 if (fsync_target > 0 && fsync_target < targettime)
405 targettime = fsync_target;
406
408 targettime,
409 &secs,
410 &usecs);
411 if (secs <= 0)
412 timeout.tv_sec = 1; /* Always sleep at least 1 sec */
413 else
414 timeout.tv_sec = secs;
415 timeout.tv_usec = usecs;
416 timeoutptr = &timeout;
417 }
418
419 r = select(PQsocket(conn) + 1, &input_mask, NULL, NULL, timeoutptr);
420 if (r == 0 || (r < 0 && errno == EINTR))
421 {
422 /*
423 * Got a timeout or signal. Continue the loop and either
424 * deliver a status packet to the server or just go back into
425 * blocking.
426 */
427 continue;
428 }
429 else if (r < 0)
430 {
431 pg_log_error("%s() failed: %m", "select");
432 goto error;
433 }
434
435 /* Else there is actually data on the socket */
436 if (PQconsumeInput(conn) == 0)
437 {
438 pg_log_error("could not receive data from WAL stream: %s",
440 goto error;
441 }
442 continue;
443 }
444
445 /* End of copy stream */
446 if (r == -1)
447 break;
448
449 /* Failure while reading the copy stream */
450 if (r == -2)
451 {
452 pg_log_error("could not read COPY data: %s",
454 goto error;
455 }
456
457 /* Check the message type. */
459 {
460 int pos;
461 bool replyRequested;
462 XLogRecPtr walEnd;
463 bool endposReached = false;
464
465 /*
466 * Parse the keepalive message, enclosed in the CopyData message.
467 * We just check if the server requested a reply, and ignore the
468 * rest.
469 */
470 pos = 1; /* skip msgtype PqReplMsg_Keepalive */
471 walEnd = fe_recvint64(&copybuf[pos]);
473
474 pos += 8; /* read walEnd */
475
476 pos += 8; /* skip sendTime */
477
478 if (r < pos + 1)
479 {
480 pg_log_error("streaming header too small: %d", r);
481 goto error;
482 }
483 replyRequested = copybuf[pos];
484
485 if (endpos != InvalidXLogRecPtr && walEnd >= endpos)
486 {
487 /*
488 * If there's nothing to read on the socket until a keepalive
489 * we know that the server has nothing to send us; and if
490 * walEnd has passed endpos, we know nothing else can have
491 * committed before endpos. So we can bail out now.
492 */
493 endposReached = true;
494 }
495
496 /* Send a reply, if necessary */
497 if (replyRequested || endposReached)
498 {
500 goto error;
501 last_status = now;
502 }
503
504 if (endposReached)
505 {
507 time_to_abort = true;
508 break;
509 }
510
511 continue;
512 }
513 else if (copybuf[0] != PqReplMsg_WALData)
514 {
515 pg_log_error("unrecognized streaming header: \"%c\"",
516 copybuf[0]);
517 goto error;
518 }
519
520 /*
521 * Read the header of the WALData message, enclosed in the CopyData
522 * message. We only need the WAL location field (dataStart), the rest
523 * of the header is ignored.
524 */
525 hdr_len = 1; /* msgtype PqReplMsg_WALData */
526 hdr_len += 8; /* dataStart */
527 hdr_len += 8; /* walEnd */
528 hdr_len += 8; /* sendTime */
529 if (r < hdr_len + 1)
530 {
531 pg_log_error("streaming header too small: %d", r);
532 goto error;
533 }
534
535 /* Extract WAL location for this block */
536 cur_record_lsn = fe_recvint64(&copybuf[1]);
537
538 if (endpos != InvalidXLogRecPtr && cur_record_lsn > endpos)
539 {
540 /*
541 * We've read past our endpoint, so prepare to go away being
542 * cautious about what happens to our output data.
543 */
545 goto error;
547 time_to_abort = true;
548 break;
549 }
550
551 output_written_lsn = Max(cur_record_lsn, output_written_lsn);
552
553 bytes_left = r - hdr_len;
554 bytes_written = 0;
555
556 /* signal that a fsync is needed */
557 output_needs_fsync = true;
558
559 while (bytes_left)
560 {
561 int ret;
562
563 ret = write(outfd,
564 copybuf + hdr_len + bytes_written,
565 bytes_left);
566
567 if (ret < 0)
568 {
569 pg_log_error("could not write %d bytes to log file \"%s\": %m",
570 bytes_left, outfile);
571 goto error;
572 }
573
574 /* Write was successful, advance our position */
575 bytes_written += ret;
576 bytes_left -= ret;
577 }
578
579 if (write(outfd, "\n", 1) != 1)
580 {
581 pg_log_error("could not write %d bytes to log file \"%s\": %m",
582 1, outfile);
583 goto error;
584 }
585
586 if (endpos != InvalidXLogRecPtr && cur_record_lsn == endpos)
587 {
588 /* endpos was exactly the record we just processed, we're done */
590 goto error;
592 time_to_abort = true;
593 break;
594 }
595 }
596
597 /* Clean up connection state if stream has been aborted */
598 if (time_to_abort)
599 prepareToTerminate(conn, endpos, stop_reason, cur_record_lsn);
600
601 res = PQgetResult(conn);
602 if (PQresultStatus(res) == PGRES_COPY_OUT)
603 {
604 PQclear(res);
605
606 /*
607 * We're doing a client-initiated clean exit and have sent CopyDone to
608 * the server. Drain any messages, so we don't miss a last-minute
609 * ErrorResponse. The walsender stops generating WALData records once
610 * it sees CopyDone, so expect this to finish quickly. After CopyDone,
611 * it's too late for sendFeedback(), even if this were to take a long
612 * time. Hence, use synchronous-mode PQgetCopyData().
613 */
614 while (1)
615 {
616 int r;
617
618 if (copybuf != NULL)
619 {
621 copybuf = NULL;
622 }
623 r = PQgetCopyData(conn, &copybuf, 0);
624 if (r == -1)
625 break;
626 if (r == -2)
627 {
628 pg_log_error("could not read COPY data: %s",
630 time_to_abort = false; /* unclean exit */
631 goto error;
632 }
633 }
634
635 res = PQgetResult(conn);
636 }
638 {
639 pg_log_error("unexpected termination of replication stream: %s",
641 PQclear(res);
642 goto error;
643 }
644 PQclear(res);
645
646 if (outfd != -1 && strcmp(outfile, "-") != 0)
647 {
649
650 /* no need to jump to error on failure here, we're finishing anyway */
651 OutputFsync(t);
652
653 if (close(outfd) != 0)
654 pg_log_error("could not close file \"%s\": %m", outfile);
655 }
656 outfd = -1;
657error:
658 if (copybuf != NULL)
659 {
661 copybuf = NULL;
662 }
663 destroyPQExpBuffer(query);
664 PQfinish(conn);
665 conn = NULL;
666}
#define Max(x, y)
Definition: c.h:998
int64_t int64
Definition: c.h:536
#define PG_BINARY
Definition: c.h:1273
int64 TimestampTz
Definition: timestamp.h:39
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:7705
void PQfreemem(void *ptr)
Definition: fe-exec.c:4043
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1995
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2273
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition: fe-exec.c:2827
#define close(a)
Definition: win32.h:12
#define write(a, b, c)
Definition: win32.h:14
int i
Definition: isn.c:77
#define PQresultErrorMessage
#define PQgetResult
Definition: libpq-be-fe.h:246
#define PQclear
Definition: libpq-be-fe.h:245
#define PQresultStatus
Definition: libpq-be-fe.h:247
@ PGRES_COPY_BOTH
Definition: libpq-fe.h:137
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:125
@ PGRES_COPY_OUT
Definition: libpq-fe.h:131
static bool flushAndSendFeedback(PGconn *conn, TimestampTz *now)
static void prepareToTerminate(PGconn *conn, XLogRecPtr endpos, StreamStopReason reason, XLogRecPtr lsn)
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
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
#define PqReplMsg_WALData
Definition: protocol.h:77
#define PqReplMsg_Keepalive
Definition: protocol.h:75
static void error(void)
Definition: sql-dyntest.c:147
int64 fe_recvint64(char *buf)
Definition: streamutil.c:868
void feTimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: streamutil.c:822
bool feTimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: streamutil.c:844
static StringInfo copybuf
Definition: tablesync.c:137
#define EINTR
Definition: win32_port.h:364
#define S_IRUSR
Definition: win32_port.h:279
#define fstat
Definition: win32_port.h:273
#define S_ISREG(m)
Definition: win32_port.h:318
#define S_IWUSR
Definition: win32_port.h:282
#define select(n, r, w, e, timeout)
Definition: win32_port.h:503

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), close, conn, copybuf, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), EINTR, endpos, error(), fe_recvint64(), feGetCurrentTimestamp(), feTimestampDifference(), feTimestampDifferenceExceeds(), flushAndSendFeedback(), fstat, fsync_interval, GetConnection(), i, InvalidXLogRecPtr, LSN_FORMAT_ARGS, Max, noptions, now(), outfd, outfile, output_fsync_lsn, output_isfile, output_last_fsync, output_needs_fsync, output_reopen, output_written_lsn, OutputFsync(), PG_BINARY, pg_log_error, pg_log_info, PGRES_COMMAND_OK, PGRES_COPY_BOTH, PGRES_COPY_OUT, PQclear, PQconsumeInput(), PQerrorMessage(), PQexec(), PQfinish(), PQfreemem(), PQgetCopyData(), PQgetResult, PqReplMsg_Keepalive, PqReplMsg_WALData, PQresultErrorMessage, PQresultStatus, PQsocket(), prepareToTerminate(), replication_slot, resetPQExpBuffer(), S_IRUSR, S_ISREG, S_IWUSR, select, sendFeedback(), stat::st_mode, standby_message_timeout, startpos, generate_unaccent_rules::stdout, stop_reason, STREAM_STOP_END_OF_WAL, STREAM_STOP_KEEPALIVE, time_to_abort, verbose, and write.

Referenced by main().

◆ usage()

static void usage ( void  )
static

Definition at line 82 of file pg_recvlogical.c.

83{
84 printf(_("%s controls PostgreSQL logical decoding streams.\n\n"),
85 progname);
86 printf(_("Usage:\n"));
87 printf(_(" %s [OPTION]...\n"), progname);
88 printf(_("\nAction to be performed:\n"));
89 printf(_(" --create-slot create a new replication slot (for the slot's name see --slot)\n"));
90 printf(_(" --drop-slot drop the replication slot (for the slot's name see --slot)\n"));
91 printf(_(" --start start streaming in a replication slot (for the slot's name see --slot)\n"));
92 printf(_("\nOptions:\n"));
93 printf(_(" --enable-failover enable replication slot synchronization to standby servers when\n"
94 " creating a replication slot\n"));
95 printf(_(" -E, --endpos=LSN exit after receiving the specified LSN\n"));
96 printf(_(" -f, --file=FILE receive log into this file, - for stdout\n"));
97 printf(_(" -F --fsync-interval=SECS\n"
98 " time between fsyncs to the output file (default: %d)\n"), (fsync_interval / 1000));
99 printf(_(" --if-not-exists do not error if slot already exists when creating a slot\n"));
100 printf(_(" -I, --startpos=LSN where in an existing slot should the streaming start\n"));
101 printf(_(" -n, --no-loop do not loop on connection lost\n"));
102 printf(_(" -o, --option=NAME[=VALUE]\n"
103 " pass option NAME with optional value VALUE to the\n"
104 " output plugin\n"));
105 printf(_(" -P, --plugin=PLUGIN use output plugin PLUGIN (default: %s)\n"), plugin);
106 printf(_(" -s, --status-interval=SECS\n"
107 " time between status packets sent to server (default: %d)\n"), (standby_message_timeout / 1000));
108 printf(_(" -S, --slot=SLOTNAME name of the logical replication slot\n"));
109 printf(_(" -t, --enable-two-phase enable decoding of prepared transactions when creating a slot\n"));
110 printf(_(" --two-phase (same as --enable-two-phase, deprecated)\n"));
111 printf(_(" -v, --verbose output verbose messages\n"));
112 printf(_(" -V, --version output version information, then exit\n"));
113 printf(_(" -?, --help show this help, then exit\n"));
114 printf(_("\nConnection options:\n"));
115 printf(_(" -d, --dbname=DBNAME database to connect to\n"));
116 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
117 printf(_(" -p, --port=PORT database server port number\n"));
118 printf(_(" -U, --username=NAME connect as specified database user\n"));
119 printf(_(" -w, --no-password never prompt for password\n"));
120 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
121 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
122 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
123}
#define _(x)
Definition: elog.c:91
#define printf(...)
Definition: port.h:245

References _, fsync_interval, plugin, printf, progname, and standby_message_timeout.

Referenced by main().

Variable Documentation

◆ do_create_slot

bool do_create_slot = false
static

Definition at line 52 of file pg_recvlogical.c.

Referenced by main().

◆ do_drop_slot

bool do_drop_slot = false
static

Definition at line 55 of file pg_recvlogical.c.

Referenced by main().

◆ do_start_slot

bool do_start_slot = false
static

Definition at line 54 of file pg_recvlogical.c.

Referenced by main().

◆ endpos

XLogRecPtr endpos = InvalidXLogRecPtr
static

Definition at line 51 of file pg_recvlogical.c.

Referenced by main(), prepareToTerminate(), and StreamLogicalLog().

◆ failover

◆ fsync_interval

int fsync_interval = 10 * 1000
static

Definition at line 49 of file pg_recvlogical.c.

Referenced by main(), OutputFsync(), StreamLogicalLog(), and usage().

◆ noloop

int noloop = 0
static

Definition at line 47 of file pg_recvlogical.c.

Referenced by main().

◆ noptions

◆ options

◆ outfd

int outfd = -1
static

Definition at line 64 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ outfile

char* outfile = NULL
static

◆ output_fsync_lsn

XLogRecPtr output_fsync_lsn = InvalidXLogRecPtr
static

Definition at line 72 of file pg_recvlogical.c.

Referenced by OutputFsync(), sendFeedback(), and StreamLogicalLog().

◆ output_isfile

bool output_isfile
static

Definition at line 68 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ output_last_fsync

TimestampTz output_last_fsync = -1
static

Definition at line 69 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ output_needs_fsync

bool output_needs_fsync = false
static

Definition at line 70 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ output_reopen

volatile sig_atomic_t output_reopen = false
static

Definition at line 67 of file pg_recvlogical.c.

Referenced by sighup_handler(), and StreamLogicalLog().

◆ output_written_lsn

XLogRecPtr output_written_lsn = InvalidXLogRecPtr
static

Definition at line 71 of file pg_recvlogical.c.

Referenced by OutputFsync(), sendFeedback(), and StreamLogicalLog().

◆ plugin

◆ replication_slot

char* replication_slot = NULL
static

Definition at line 56 of file pg_recvlogical.c.

Referenced by main(), sendFeedback(), and StreamLogicalLog().

◆ slot_exists_ok

bool slot_exists_ok = false
static

Definition at line 53 of file pg_recvlogical.c.

Referenced by main().

◆ standby_message_timeout

int standby_message_timeout = 10 * 1000
static

Definition at line 48 of file pg_recvlogical.c.

Referenced by main(), StreamLogicalLog(), and usage().

◆ startpos

◆ stop_reason

volatile sig_atomic_t stop_reason = STREAM_STOP_NONE
static

Definition at line 66 of file pg_recvlogical.c.

Referenced by sigexit_handler(), and StreamLogicalLog().

◆ time_to_abort

volatile sig_atomic_t time_to_abort = false
static

Definition at line 65 of file pg_recvlogical.c.

Referenced by main(), sigexit_handler(), and StreamLogicalLog().

◆ two_phase

◆ verbose

int verbose = 0
static

Definition at line 44 of file pg_recvlogical.c.

Referenced by main(), prepareToTerminate(), sendFeedback(), and StreamLogicalLog().