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 "access/xlog_internal.h"
#include "common/fe_memutils.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 "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
 

Functions

static void usage (void)
 
static void StreamLogicalLog (void)
 
static bool flushAndSendFeedback (PGconn *conn, TimestampTz *now)
 
static void prepareToTerminate (PGconn *conn, XLogRecPtr endpos, bool keepalive, 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 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 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 33 of file pg_recvlogical.c.

Function Documentation

◆ disconnect_atexit()

static void disconnect_atexit ( void  )
static

Definition at line 168 of file pg_recvlogical.c.

169 {
170  if (conn != NULL)
171  PQfinish(conn);
172 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4599
PGconn * conn
Definition: streamutil.c:54

References conn, and PQfinish().

Referenced by main().

◆ flushAndSendFeedback()

static bool flushAndSendFeedback ( PGconn conn,
TimestampTz now 
)
static

Definition at line 1007 of file pg_recvlogical.c.

1008 {
1009  /* flush data to disk, so that we send a recent flush pointer */
1010  if (!OutputFsync(*now))
1011  return false;
1013  if (!sendFeedback(conn, *now, true, false))
1014  return false;
1015 
1016  return true;
1017 }
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547
static bool OutputFsync(TimestampTz now)
static bool sendFeedback(PGconn *conn, TimestampTz now, bool force, bool replyRequested)
TimestampTz feGetCurrentTimestamp(void)
Definition: streamutil.c:789

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

Referenced by StreamLogicalLog().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 674 of file pg_recvlogical.c.

675 {
676  static struct option long_options[] = {
677 /* general options */
678  {"file", required_argument, NULL, 'f'},
679  {"fsync-interval", required_argument, NULL, 'F'},
680  {"no-loop", no_argument, NULL, 'n'},
681  {"verbose", no_argument, NULL, 'v'},
682  {"two-phase", no_argument, NULL, 't'},
683  {"version", no_argument, NULL, 'V'},
684  {"help", no_argument, NULL, '?'},
685 /* connection options */
686  {"dbname", required_argument, NULL, 'd'},
687  {"host", required_argument, NULL, 'h'},
688  {"port", required_argument, NULL, 'p'},
689  {"username", required_argument, NULL, 'U'},
690  {"no-password", no_argument, NULL, 'w'},
691  {"password", no_argument, NULL, 'W'},
692 /* replication options */
693  {"startpos", required_argument, NULL, 'I'},
694  {"endpos", required_argument, NULL, 'E'},
695  {"option", required_argument, NULL, 'o'},
696  {"plugin", required_argument, NULL, 'P'},
697  {"status-interval", required_argument, NULL, 's'},
698  {"slot", required_argument, NULL, 'S'},
699 /* action */
700  {"create-slot", no_argument, NULL, 1},
701  {"start", no_argument, NULL, 2},
702  {"drop-slot", no_argument, NULL, 3},
703  {"if-not-exists", no_argument, NULL, 4},
704  {NULL, 0, NULL, 0}
705  };
706  int c;
707  int option_index;
708  uint32 hi,
709  lo;
710  char *db_name;
711 
712  pg_logging_init(argv[0]);
713  progname = get_progname(argv[0]);
714  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
715 
716  if (argc > 1)
717  {
718  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
719  {
720  usage();
721  exit(0);
722  }
723  else if (strcmp(argv[1], "-V") == 0 ||
724  strcmp(argv[1], "--version") == 0)
725  {
726  puts("pg_recvlogical (PostgreSQL) " PG_VERSION);
727  exit(0);
728  }
729  }
730 
731  while ((c = getopt_long(argc, argv, "E:f:F:ntvd:h:p:U:wWI:o:P:s:S:",
732  long_options, &option_index)) != -1)
733  {
734  switch (c)
735  {
736 /* general options */
737  case 'f':
739  break;
740  case 'F':
741  if (!option_parse_int(optarg, "-F/--fsync-interval", 0,
742  INT_MAX / 1000,
743  &fsync_interval))
744  exit(1);
745  fsync_interval *= 1000;
746  break;
747  case 'n':
748  noloop = 1;
749  break;
750  case 't':
751  two_phase = true;
752  break;
753  case 'v':
754  verbose++;
755  break;
756 /* connection options */
757  case 'd':
759  break;
760  case 'h':
762  break;
763  case 'p':
765  break;
766  case 'U':
768  break;
769  case 'w':
770  dbgetpassword = -1;
771  break;
772  case 'W':
773  dbgetpassword = 1;
774  break;
775 /* replication options */
776  case 'I':
777  if (sscanf(optarg, "%X/%X", &hi, &lo) != 2)
778  pg_fatal("could not parse start position \"%s\"", optarg);
779  startpos = ((uint64) hi) << 32 | lo;
780  break;
781  case 'E':
782  if (sscanf(optarg, "%X/%X", &hi, &lo) != 2)
783  pg_fatal("could not parse end position \"%s\"", optarg);
784  endpos = ((uint64) hi) << 32 | lo;
785  break;
786  case 'o':
787  {
788  char *data = pg_strdup(optarg);
789  char *val = strchr(data, '=');
790 
791  if (val != NULL)
792  {
793  /* remove =; separate data from val */
794  *val = '\0';
795  val++;
796  }
797 
798  noptions += 1;
799  options = pg_realloc(options, sizeof(char *) * noptions * 2);
800 
801  options[(noptions - 1) * 2] = data;
802  options[(noptions - 1) * 2 + 1] = val;
803  }
804 
805  break;
806  case 'P':
808  break;
809  case 's':
810  if (!option_parse_int(optarg, "-s/--status-interval", 0,
811  INT_MAX / 1000,
813  exit(1);
814  standby_message_timeout *= 1000;
815  break;
816  case 'S':
818  break;
819 /* action */
820  case 1:
821  do_create_slot = true;
822  break;
823  case 2:
824  do_start_slot = true;
825  break;
826  case 3:
827  do_drop_slot = true;
828  break;
829  case 4:
830  slot_exists_ok = true;
831  break;
832 
833  default:
834  /* getopt_long already emitted a complaint */
835  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
836  exit(1);
837  }
838  }
839 
840  /*
841  * Any non-option arguments?
842  */
843  if (optind < argc)
844  {
845  pg_log_error("too many command-line arguments (first is \"%s\")",
846  argv[optind]);
847  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
848  exit(1);
849  }
850 
851  /*
852  * Required arguments
853  */
854  if (replication_slot == NULL)
855  {
856  pg_log_error("no slot specified");
857  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
858  exit(1);
859  }
860 
861  if (do_start_slot && outfile == NULL)
862  {
863  pg_log_error("no target file specified");
864  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
865  exit(1);
866  }
867 
868  if (!do_drop_slot && dbname == NULL)
869  {
870  pg_log_error("no database specified");
871  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
872  exit(1);
873  }
874 
876  {
877  pg_log_error("at least one action needs to be specified");
878  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
879  exit(1);
880  }
881 
883  {
884  pg_log_error("cannot use --create-slot or --start together with --drop-slot");
885  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
886  exit(1);
887  }
888 
890  {
891  pg_log_error("cannot use --create-slot or --drop-slot together with --startpos");
892  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
893  exit(1);
894  }
895 
897  {
898  pg_log_error("--endpos may only be specified with --start");
899  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
900  exit(1);
901  }
902 
903  if (two_phase && !do_create_slot)
904  {
905  pg_log_error("--two-phase may only be specified with --create-slot");
906  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
907  exit(1);
908  }
909 
910  /*
911  * Obtain a connection to server. Notably, if we need a password, we want
912  * to collect it from the user immediately.
913  */
914  conn = GetConnection();
915  if (!conn)
916  /* Error message already written in GetConnection() */
917  exit(1);
918  atexit(disconnect_atexit);
919 
920  /*
921  * Trap signals. (Don't do this until after the initial password prompt,
922  * if one is needed, in GetConnection.)
923  */
924 #ifndef WIN32
925  pqsignal(SIGINT, sigexit_handler);
926  pqsignal(SIGTERM, sigexit_handler);
928 #endif
929 
930  /*
931  * Run IDENTIFY_SYSTEM to make sure we connected using a database specific
932  * replication connection.
933  */
934  if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
935  exit(1);
936 
937  if (db_name == NULL)
938  pg_fatal("could not establish database-specific replication connection");
939 
940  /*
941  * Set umask so that directories/files are created with the same
942  * permissions as directories/files in the source data directory.
943  *
944  * pg_mode_mask is set to owner-only by default and then updated in
945  * GetConnection() where we get the mode from the server-side with
946  * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
947  */
948  umask(pg_mode_mask);
949 
950  /* Drop a replication slot. */
951  if (do_drop_slot)
952  {
953  if (verbose)
954  pg_log_info("dropping replication slot \"%s\"", replication_slot);
955 
957  exit(1);
958  }
959 
960  /* Create a replication slot. */
961  if (do_create_slot)
962  {
963  if (verbose)
964  pg_log_info("creating replication slot \"%s\"", replication_slot);
965 
967  false, false, slot_exists_ok, two_phase))
968  exit(1);
970  }
971 
972  if (!do_start_slot)
973  exit(0);
974 
975  /* Stream loop */
976  while (true)
977  {
979  if (time_to_abort)
980  {
981  /*
982  * We've been Ctrl-C'ed or reached an exit limit condition. That's
983  * not an error, so exit without an errorcode.
984  */
985  exit(0);
986  }
987  else if (noloop)
988  pg_fatal("disconnected");
989  else
990  {
991  /* translator: check source for value for %d */
992  pg_log_info("disconnected; waiting %d seconds to try again",
994  pg_usleep(RECONNECT_SLEEP_TIME * 1000000);
995  }
996  }
997 }
unsigned int uint32
Definition: c.h:490
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1222
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:436
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
Definition: connection.c:172
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
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:57
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
long val
Definition: informix.c:664
exit(1)
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:45
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:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
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 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
const char * get_progname(const char *argv0)
Definition: path.c:574
pqsigfunc pqsignal(int signo, pqsigfunc func)
char * c
void pg_usleep(long microsec)
Definition: signal.c:53
int dbgetpassword
Definition: streamutil.c:52
char * dbhost
Definition: streamutil.c:48
char * dbport
Definition: streamutil.c:50
char * dbname
Definition: streamutil.c:51
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:400
char * dbuser
Definition: streamutil.c:49
static void CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
Definition: walsender.c:1041
static void DropReplicationSlot(DropReplicationSlotCmd *cmd)
Definition: walsender.c:1245
#define SIGHUP
Definition: win32_port.h:176
#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, exit(), 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 175 of file pg_recvlogical.c.

176 {
178 
180 
181  if (fsync_interval <= 0)
182  return true;
183 
184  if (!output_needs_fsync)
185  return true;
186 
187  output_needs_fsync = false;
188 
189  /* can only fsync if it's a regular file */
190  if (!output_isfile)
191  return true;
192 
193  if (fsync(outfd) != 0)
194  pg_fatal("could not fsync file \"%s\": %m", outfile);
195 
196  return true;
197 }
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:85

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,
bool  keepalive,
XLogRecPtr  lsn 
)
static

Definition at line 1024 of file pg_recvlogical.c.

1025 {
1026  (void) PQputCopyEnd(conn, NULL);
1027  (void) PQflush(conn);
1028 
1029  if (verbose)
1030  {
1031  if (keepalive)
1032  pg_log_info("end position %X/%X reached by keepalive",
1034  else
1035  pg_log_info("end position %X/%X reached by WAL record at %X/%X",
1037  }
1038 }
int PQflush(PGconn *conn)
Definition: fe-exec.c:3837
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2634
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43

References conn, endpos, LSN_FORMAT_ARGS, pg_log_info, PQflush(), PQputCopyEnd(), and verbose.

Referenced by StreamLogicalLog().

◆ sendFeedback()

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

Definition at line 116 of file pg_recvlogical.c.

117 {
118  static XLogRecPtr last_written_lsn = InvalidXLogRecPtr;
119  static XLogRecPtr last_fsync_lsn = InvalidXLogRecPtr;
120 
121  char replybuf[1 + 8 + 8 + 8 + 8 + 1];
122  int len = 0;
123 
124  /*
125  * we normally don't want to send superfluous feedback, but if it's
126  * because of a timeout we need to, otherwise wal_sender_timeout will kill
127  * us.
128  */
129  if (!force &&
130  last_written_lsn == output_written_lsn &&
131  last_fsync_lsn == output_fsync_lsn)
132  return true;
133 
134  if (verbose)
135  pg_log_info("confirming write up to %X/%X, flush to %X/%X (slot %s)",
139 
140  replybuf[len] = 'r';
141  len += 1;
142  fe_sendint64(output_written_lsn, &replybuf[len]); /* write */
143  len += 8;
144  fe_sendint64(output_fsync_lsn, &replybuf[len]); /* flush */
145  len += 8;
146  fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */
147  len += 8;
148  fe_sendint64(now, &replybuf[len]); /* sendTime */
149  len += 8;
150  replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */
151  len += 1;
152 
154  last_written_lsn = output_written_lsn;
155  last_fsync_lsn = output_fsync_lsn;
156 
157  if (PQputCopyData(conn, replybuf, len) <= 0 || PQflush(conn))
158  {
159  pg_log_error("could not send feedback packet: %s",
161  return false;
162  }
163 
164  return true;
165 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7245
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2579
const void size_t len
void fe_sendint64(int64 i, char *buf)
Definition: streamutil.c:843
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(), replication_slot, startpos, and verbose.

Referenced by flushAndSendFeedback(), and StreamLogicalLog().

◆ sigexit_handler()

static void sigexit_handler ( SIGNAL_ARGS  )
static

Definition at line 657 of file pg_recvlogical.c.

658 {
659  time_to_abort = true;
660 }

References time_to_abort.

Referenced by main().

◆ sighup_handler()

static void sighup_handler ( SIGNAL_ARGS  )
static

Definition at line 666 of file pg_recvlogical.c.

667 {
668  output_reopen = true;
669 }
static volatile sig_atomic_t output_reopen

References output_reopen.

Referenced by main().

◆ StreamLogicalLog()

static void StreamLogicalLog ( void  )
static

Definition at line 203 of file pg_recvlogical.c.

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

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(), PQresultErrorMessage(), PQresultStatus(), PQsocket(), prepareToTerminate(), replication_slot, res, resetPQExpBuffer(), S_IRUSR, S_ISREG, S_IWUSR, select, sendFeedback(), stat::st_mode, standby_message_timeout, startpos, generate_unaccent_rules::stdout, time_to_abort, verbose, and write.

Referenced by main().

◆ usage()

static void usage ( void  )
static

Definition at line 72 of file pg_recvlogical.c.

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

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 44 of file pg_recvlogical.c.

Referenced by main().

◆ do_drop_slot

bool do_drop_slot = false
static

Definition at line 47 of file pg_recvlogical.c.

Referenced by main().

◆ do_start_slot

bool do_start_slot = false
static

Definition at line 46 of file pg_recvlogical.c.

Referenced by main().

◆ endpos

XLogRecPtr endpos = InvalidXLogRecPtr
static

Definition at line 43 of file pg_recvlogical.c.

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

◆ fsync_interval

int fsync_interval = 10 * 1000
static

Definition at line 41 of file pg_recvlogical.c.

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

◆ noloop

int noloop = 0
static

Definition at line 39 of file pg_recvlogical.c.

Referenced by main().

◆ noptions

◆ options

◆ outfd

int outfd = -1
static

Definition at line 56 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 63 of file pg_recvlogical.c.

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

◆ output_isfile

bool output_isfile
static

Definition at line 59 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ output_last_fsync

TimestampTz output_last_fsync = -1
static

Definition at line 60 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ output_needs_fsync

bool output_needs_fsync = false
static

Definition at line 61 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ output_reopen

volatile sig_atomic_t output_reopen = false
static

Definition at line 58 of file pg_recvlogical.c.

Referenced by sighup_handler(), and StreamLogicalLog().

◆ output_written_lsn

XLogRecPtr output_written_lsn = InvalidXLogRecPtr
static

Definition at line 62 of file pg_recvlogical.c.

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

◆ plugin

◆ replication_slot

char* replication_slot = NULL
static

Definition at line 48 of file pg_recvlogical.c.

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

◆ slot_exists_ok

bool slot_exists_ok = false
static

Definition at line 45 of file pg_recvlogical.c.

Referenced by main().

◆ standby_message_timeout

int standby_message_timeout = 10 * 1000
static

Definition at line 40 of file pg_recvlogical.c.

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

◆ startpos

◆ time_to_abort

volatile sig_atomic_t time_to_abort = false
static

Definition at line 57 of file pg_recvlogical.c.

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

◆ two_phase

◆ verbose

int verbose = 0
static

Definition at line 37 of file pg_recvlogical.c.

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