PostgreSQL Source Code  git master
pg_recvlogical.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include "streamutil.h"
#include "access/xlog_internal.h"
#include "common/file_perm.h"
#include "common/fe_memutils.h"
#include "getopt_long.h"
#include "libpq-fe.h"
#include "libpq/pqsignal.h"
#include "pqexpbuffer.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 void disconnect_and_exit (int code) pg_attribute_noreturn()
 
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 bool OutputFsync (TimestampTz now)
 
static void sigint_handler (int signum)
 
static void sighup_handler (int signum)
 
int main (int argc, char **argv)
 

Variables

static char * outfile = NULL
 
static int verbose = 0
 
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 35 of file pg_recvlogical.c.

Referenced by main().

Function Documentation

◆ disconnect_and_exit()

static void disconnect_and_exit ( int  code)
static

Definition at line 170 of file pg_recvlogical.c.

References conn, and PQfinish().

Referenced by main().

171 {
172  if (conn != NULL)
173  PQfinish(conn);
174 
175  exit(code);
176 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3638
PGconn * conn
Definition: streamutil.c:55

◆ flushAndSendFeedback()

static bool flushAndSendFeedback ( PGconn conn,
TimestampTz now 
)
static

Definition at line 1038 of file pg_recvlogical.c.

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

Referenced by StreamLogicalLog().

1039 {
1040  /* flush data to disk, so that we send a recent flush pointer */
1041  if (!OutputFsync(*now))
1042  return false;
1044  if (!sendFeedback(conn, *now, true, false))
1045  return false;
1046 
1047  return true;
1048 }
TimestampTz feGetCurrentTimestamp(void)
Definition: streamutil.c:621
static bool OutputFsync(TimestampTz now)
static bool sendFeedback(PGconn *conn, TimestampTz now, bool force, bool replyRequested)
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 674 of file pg_recvlogical.c.

References _, conn, CreateReplicationSlot(), dbgetpassword, dbhost, dbname, dbport, dbuser, disconnect_and_exit(), do_create_slot, do_drop_slot, do_start_slot, DropReplicationSlot(), endpos, fsync_interval, get_progname(), GetConnection(), getopt_long(), InvalidXLogRecPtr, no_argument, noloop, noptions, optarg, optind, outfile, 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(), SIGHUP, sighup_handler(), sigint_handler(), slot_exists_ok, standby_message_timeout, startpos, StreamLogicalLog(), time_to_abort, usage(), val, and verbose.

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  {"version", no_argument, NULL, 'V'},
683  {"help", no_argument, NULL, '?'},
684 /* connection options */
685  {"dbname", required_argument, NULL, 'd'},
686  {"host", required_argument, NULL, 'h'},
687  {"port", required_argument, NULL, 'p'},
688  {"username", required_argument, NULL, 'U'},
689  {"no-password", no_argument, NULL, 'w'},
690  {"password", no_argument, NULL, 'W'},
691 /* replication options */
692  {"startpos", required_argument, NULL, 'I'},
693  {"endpos", required_argument, NULL, 'E'},
694  {"option", required_argument, NULL, 'o'},
695  {"plugin", required_argument, NULL, 'P'},
696  {"status-interval", required_argument, NULL, 's'},
697  {"slot", required_argument, NULL, 'S'},
698 /* action */
699  {"create-slot", no_argument, NULL, 1},
700  {"start", no_argument, NULL, 2},
701  {"drop-slot", no_argument, NULL, 3},
702  {"if-not-exists", no_argument, NULL, 4},
703  {NULL, 0, NULL, 0}
704  };
705  int c;
706  int option_index;
707  uint32 hi,
708  lo;
709  char *db_name;
710 
711  progname = get_progname(argv[0]);
712  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
713 
714  if (argc > 1)
715  {
716  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
717  {
718  usage();
719  exit(0);
720  }
721  else if (strcmp(argv[1], "-V") == 0 ||
722  strcmp(argv[1], "--version") == 0)
723  {
724  puts("pg_recvlogical (PostgreSQL) " PG_VERSION);
725  exit(0);
726  }
727  }
728 
729  while ((c = getopt_long(argc, argv, "E:f:F:nvd:h:p:U:wWI:o:P:s:S:",
730  long_options, &option_index)) != -1)
731  {
732  switch (c)
733  {
734 /* general options */
735  case 'f':
737  break;
738  case 'F':
739  fsync_interval = atoi(optarg) * 1000;
740  if (fsync_interval < 0)
741  {
742  fprintf(stderr, _("%s: invalid fsync interval \"%s\"\n"),
743  progname, optarg);
744  exit(1);
745  }
746  break;
747  case 'n':
748  noloop = 1;
749  break;
750  case 'v':
751  verbose++;
752  break;
753 /* connection options */
754  case 'd':
756  break;
757  case 'h':
759  break;
760  case 'p':
761  if (atoi(optarg) <= 0)
762  {
763  fprintf(stderr, _("%s: invalid port number \"%s\"\n"),
764  progname, optarg);
765  exit(1);
766  }
768  break;
769  case 'U':
771  break;
772  case 'w':
773  dbgetpassword = -1;
774  break;
775  case 'W':
776  dbgetpassword = 1;
777  break;
778 /* replication options */
779  case 'I':
780  if (sscanf(optarg, "%X/%X", &hi, &lo) != 2)
781  {
782  fprintf(stderr,
783  _("%s: could not parse start position \"%s\"\n"),
784  progname, optarg);
785  exit(1);
786  }
787  startpos = ((uint64) hi) << 32 | lo;
788  break;
789  case 'E':
790  if (sscanf(optarg, "%X/%X", &hi, &lo) != 2)
791  {
792  fprintf(stderr,
793  _("%s: could not parse end position \"%s\"\n"),
794  progname, optarg);
795  exit(1);
796  }
797  endpos = ((uint64) hi) << 32 | lo;
798  break;
799  case 'o':
800  {
801  char *data = pg_strdup(optarg);
802  char *val = strchr(data, '=');
803 
804  if (val != NULL)
805  {
806  /* remove =; separate data from val */
807  *val = '\0';
808  val++;
809  }
810 
811  noptions += 1;
812  options = pg_realloc(options, sizeof(char *) * noptions * 2);
813 
814  options[(noptions - 1) * 2] = data;
815  options[(noptions - 1) * 2 + 1] = val;
816  }
817 
818  break;
819  case 'P':
821  break;
822  case 's':
823  standby_message_timeout = atoi(optarg) * 1000;
824  if (standby_message_timeout < 0)
825  {
826  fprintf(stderr, _("%s: invalid status interval \"%s\"\n"),
827  progname, optarg);
828  exit(1);
829  }
830  break;
831  case 'S':
833  break;
834 /* action */
835  case 1:
836  do_create_slot = true;
837  break;
838  case 2:
839  do_start_slot = true;
840  break;
841  case 3:
842  do_drop_slot = true;
843  break;
844  case 4:
845  slot_exists_ok = true;
846  break;
847 
848  default:
849 
850  /*
851  * getopt_long already emitted a complaint
852  */
853  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
854  progname);
855  exit(1);
856  }
857  }
858 
859  /*
860  * Any non-option arguments?
861  */
862  if (optind < argc)
863  {
864  fprintf(stderr,
865  _("%s: too many command-line arguments (first is \"%s\")\n"),
866  progname, argv[optind]);
867  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
868  progname);
869  exit(1);
870  }
871 
872  /*
873  * Required arguments
874  */
875  if (replication_slot == NULL)
876  {
877  fprintf(stderr, _("%s: no slot specified\n"), progname);
878  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
879  progname);
880  exit(1);
881  }
882 
883  if (do_start_slot && outfile == NULL)
884  {
885  fprintf(stderr, _("%s: no target file specified\n"), progname);
886  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
887  progname);
888  exit(1);
889  }
890 
891  if (!do_drop_slot && dbname == NULL)
892  {
893  fprintf(stderr, _("%s: no database specified\n"), progname);
894  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
895  progname);
896  exit(1);
897  }
898 
900  {
901  fprintf(stderr, _("%s: at least one action needs to be specified\n"), progname);
902  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
903  progname);
904  exit(1);
905  }
906 
908  {
909  fprintf(stderr, _("%s: cannot use --create-slot or --start together with --drop-slot\n"), progname);
910  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
911  progname);
912  exit(1);
913  }
914 
916  {
917  fprintf(stderr, _("%s: cannot use --create-slot or --drop-slot together with --startpos\n"), progname);
918  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
919  progname);
920  exit(1);
921  }
922 
924  {
925  fprintf(stderr,
926  _("%s: --endpos may only be specified with --start\n"),
927  progname);
928  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
929  progname);
930  exit(1);
931  }
932 
933 #ifndef WIN32
934  pqsignal(SIGINT, sigint_handler);
936 #endif
937 
938  /*
939  * Obtain a connection to server. This is not really necessary but it
940  * helps to get more precise error messages about authentication, required
941  * GUC parameters and such.
942  */
943  conn = GetConnection();
944  if (!conn)
945  /* Error message already written in GetConnection() */
946  exit(1);
947 
948  /*
949  * Run IDENTIFY_SYSTEM to make sure we connected using a database specific
950  * replication connection.
951  */
952  if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
954 
955  if (db_name == NULL)
956  {
957  fprintf(stderr,
958  _("%s: could not establish database-specific replication connection\n"),
959  progname);
961  }
962 
963  /*
964  * Set umask so that directories/files are created with the same
965  * permissions as directories/files in the source data directory.
966  *
967  * pg_mode_mask is set to owner-only by default and then updated in
968  * GetConnection() where we get the mode from the server-side with
969  * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
970  */
971  umask(pg_mode_mask);
972 
973  /* Drop a replication slot. */
974  if (do_drop_slot)
975  {
976  if (verbose)
977  fprintf(stderr,
978  _("%s: dropping replication slot \"%s\"\n"),
980 
983  }
984 
985  /* Create a replication slot. */
986  if (do_create_slot)
987  {
988  if (verbose)
989  fprintf(stderr,
990  _("%s: creating replication slot \"%s\"\n"),
992 
994  false, false, slot_exists_ok))
997  }
998 
999  if (!do_start_slot)
1001 
1002  /* Stream loop */
1003  while (true)
1004  {
1005  StreamLogicalLog();
1006  if (time_to_abort)
1007  {
1008  /*
1009  * We've been Ctrl-C'ed or reached an exit limit condition. That's
1010  * not an error, so exit without an errorcode.
1011  */
1013  }
1014  else if (noloop)
1015  {
1016  fprintf(stderr, _("%s: disconnected\n"), progname);
1017  exit(1);
1018  }
1019  else
1020  {
1021  fprintf(stderr,
1022  /* translator: check source for value for %d */
1023  _("%s: disconnected; waiting %d seconds to try again\n"),
1025  pg_usleep(RECONNECT_SLEEP_TIME * 1000000);
1026  }
1027  }
1028 }
static const char * plugin
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static volatile sig_atomic_t time_to_abort
static void sigint_handler(int signum)
static int fsync_interval
#define RECONNECT_SLEEP_TIME
const char * get_progname(const char *argv0)
Definition: path.c:453
static void DropReplicationSlot(DropReplicationSlotCmd *cmd)
Definition: walsender.c:1030
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
static void sighup_handler(int signum)
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition: streamutil.c:417
static char * replication_slot
const char * progname
Definition: pg_standby.c:37
static int noloop
static int verbose
static XLogRecPtr endpos
void pg_usleep(long microsec)
Definition: signal.c:53
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
PGconn * conn
Definition: streamutil.c:55
static bool slot_exists_ok
char * c
#define SIGHUP
Definition: win32_port.h:163
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:325
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
int dbgetpassword
Definition: streamutil.c:52
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1054
static void usage(void)
char * dbport
Definition: streamutil.c:50
static bool do_drop_slot
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:107
char * dbhost
Definition: streamutil.c:48
char * dbname
Definition: streamutil.c:51
static XLogRecPtr startpos
static void StreamLogicalLog(void)
static void CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
Definition: walsender.c:836
static void disconnect_and_exit(int code) pg_attribute_noreturn()
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * dbuser
Definition: streamutil.c:49
char * optarg
Definition: getopt.c:53
static bool do_start_slot
static char * outfile
static size_t noptions
#define _(x)
Definition: elog.c:84
int pg_mode_mask
Definition: file_perm.c:25
long val
Definition: informix.c:689
static bool do_create_slot
static int standby_message_timeout

◆ OutputFsync()

static bool OutputFsync ( TimestampTz  now)
static

Definition at line 179 of file pg_recvlogical.c.

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

Referenced by flushAndSendFeedback(), and StreamLogicalLog().

180 {
182 
184 
185  if (fsync_interval <= 0)
186  return true;
187 
188  if (!output_needs_fsync)
189  return true;
190 
191  output_needs_fsync = false;
192 
193  /* can only fsync if it's a regular file */
194  if (!output_isfile)
195  return true;
196 
197  if (fsync(outfd) != 0)
198  {
199  fprintf(stderr,
200  _("%s: could not fsync log file \"%s\": %s\n"),
201  progname, outfile, strerror(errno));
202  return false;
203  }
204 
205  return true;
206 }
static XLogRecPtr output_fsync_lsn
static int fsync_interval
const char * progname
Definition: pg_standby.c:37
#define fsync(fd)
Definition: win32_port.h:63
static bool output_isfile
static int outfd
static XLogRecPtr output_written_lsn
static bool output_needs_fsync
const char * strerror(int errnum)
Definition: strerror.c:19
static TimestampTz output_last_fsync
static char * outfile
#define _(x)
Definition: elog.c:84
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534

◆ prepareToTerminate()

static void prepareToTerminate ( PGconn conn,
XLogRecPtr  endpos,
bool  keepalive,
XLogRecPtr  lsn 
)
static

Definition at line 1055 of file pg_recvlogical.c.

References PQflush(), PQputCopyEnd(), progname, and verbose.

Referenced by StreamLogicalLog().

1056 {
1057  (void) PQputCopyEnd(conn, NULL);
1058  (void) PQflush(conn);
1059 
1060  if (verbose)
1061  {
1062  if (keepalive)
1063  fprintf(stderr, "%s: endpos %X/%X reached by keepalive\n",
1064  progname,
1065  (uint32) (endpos >> 32), (uint32) endpos);
1066  else
1067  fprintf(stderr, "%s: endpos %X/%X reached by record at %X/%X\n",
1068  progname, (uint32) (endpos >> 32), (uint32) (endpos),
1069  (uint32) (lsn >> 32), (uint32) lsn);
1070 
1071  }
1072 }
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2339
const char * progname
Definition: pg_standby.c:37
static int verbose
static XLogRecPtr endpos
int PQflush(PGconn *conn)
Definition: fe-exec.c:3238
unsigned int uint32
Definition: c.h:325

◆ sendFeedback()

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

Definition at line 116 of file pg_recvlogical.c.

References _, fe_sendint64(), InvalidXLogRecPtr, output_fsync_lsn, output_written_lsn, PQerrorMessage(), PQflush(), PQputCopyData(), progname, replication_slot, startpos, and verbose.

Referenced by flushAndSendFeedback(), and StreamLogicalLog().

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  fprintf(stderr,
136  _("%s: confirming write up to %X/%X, flush to %X/%X (slot %s)\n"),
137  progname,
141 
142  replybuf[len] = 'r';
143  len += 1;
144  fe_sendint64(output_written_lsn, &replybuf[len]); /* write */
145  len += 8;
146  fe_sendint64(output_fsync_lsn, &replybuf[len]); /* flush */
147  len += 8;
148  fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */
149  len += 8;
150  fe_sendint64(now, &replybuf[len]); /* sendTime */
151  len += 8;
152  replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */
153  len += 1;
154 
156  last_written_lsn = output_written_lsn;
157  last_fsync_lsn = output_fsync_lsn;
158 
159  if (PQputCopyData(conn, replybuf, len) <= 0 || PQflush(conn))
160  {
161  fprintf(stderr, _("%s: could not send feedback packet: %s"),
162  progname, PQerrorMessage(conn));
163  return false;
164  }
165 
166  return true;
167 }
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2272
static XLogRecPtr output_fsync_lsn
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static char * replication_slot
const char * progname
Definition: pg_standby.c:37
static int verbose
int PQflush(PGconn *conn)
Definition: fe-exec.c:3238
unsigned int uint32
Definition: c.h:325
static XLogRecPtr output_written_lsn
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogRecPtr startpos
void fe_sendint64(int64 i, char *buf)
Definition: streamutil.c:675
#define _(x)
Definition: elog.c:84
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534

◆ sighup_handler()

static void sighup_handler ( int  signum)
static

Definition at line 666 of file pg_recvlogical.c.

References output_reopen.

Referenced by main().

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

◆ sigint_handler()

static void sigint_handler ( int  signum)
static

Definition at line 657 of file pg_recvlogical.c.

References time_to_abort.

Referenced by main().

658 {
659  time_to_abort = true;
660 }
static volatile sig_atomic_t time_to_abort

◆ StreamLogicalLog()

static void StreamLogicalLog ( void  )
static

Definition at line 212 of file pg_recvlogical.c.

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

Referenced by main().

213 {
214  PGresult *res;
215  char *copybuf = NULL;
216  TimestampTz last_status = -1;
217  int i;
218  PQExpBuffer query;
219 
222 
223  query = createPQExpBuffer();
224 
225  /*
226  * Connect in replication mode to the server
227  */
228  if (!conn)
229  conn = GetConnection();
230  if (!conn)
231  /* Error message already written in GetConnection() */
232  return;
233 
234  /*
235  * Start the replication
236  */
237  if (verbose)
238  fprintf(stderr,
239  _("%s: starting log streaming at %X/%X (slot %s)\n"),
240  progname, (uint32) (startpos >> 32), (uint32) startpos,
242 
243  /* Initiate the replication stream at specified location */
244  appendPQExpBuffer(query, "START_REPLICATION SLOT \"%s\" LOGICAL %X/%X",
246 
247  /* print options if there are any */
248  if (noptions)
249  appendPQExpBufferStr(query, " (");
250 
251  for (i = 0; i < noptions; i++)
252  {
253  /* separator */
254  if (i > 0)
255  appendPQExpBufferStr(query, ", ");
256 
257  /* write option name */
258  appendPQExpBuffer(query, "\"%s\"", options[(i * 2)]);
259 
260  /* write option value if specified */
261  if (options[(i * 2) + 1] != NULL)
262  appendPQExpBuffer(query, " '%s'", options[(i * 2) + 1]);
263  }
264 
265  if (noptions)
266  appendPQExpBufferChar(query, ')');
267 
268  res = PQexec(conn, query->data);
269  if (PQresultStatus(res) != PGRES_COPY_BOTH)
270  {
271  fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
272  progname, query->data, PQresultErrorMessage(res));
273  PQclear(res);
274  goto error;
275  }
276  PQclear(res);
277  resetPQExpBuffer(query);
278 
279  if (verbose)
280  fprintf(stderr,
281  _("%s: streaming initiated\n"),
282  progname);
283 
284  while (!time_to_abort)
285  {
286  int r;
287  int bytes_left;
288  int bytes_written;
290  int hdr_len;
291  XLogRecPtr cur_record_lsn = InvalidXLogRecPtr;
292 
293  if (copybuf != NULL)
294  {
295  PQfreemem(copybuf);
296  copybuf = NULL;
297  }
298 
299  /*
300  * Potentially send a status message to the master
301  */
302  now = feGetCurrentTimestamp();
303 
304  if (outfd != -1 &&
307  {
308  if (!OutputFsync(now))
309  goto error;
310  }
311 
312  if (standby_message_timeout > 0 &&
313  feTimestampDifferenceExceeds(last_status, now,
315  {
316  /* Time to send feedback! */
317  if (!sendFeedback(conn, now, true, false))
318  goto error;
319 
320  last_status = now;
321  }
322 
323  /* got SIGHUP, close output file */
324  if (outfd != -1 && output_reopen && strcmp(outfile, "-") != 0)
325  {
326  now = feGetCurrentTimestamp();
327  if (!OutputFsync(now))
328  goto error;
329  close(outfd);
330  outfd = -1;
331  }
332  output_reopen = false;
333 
334  /* open the output file, if not open yet */
335  if (outfd == -1)
336  {
337  struct stat statbuf;
338 
339  if (strcmp(outfile, "-") == 0)
340  outfd = fileno(stdout);
341  else
342  outfd = open(outfile, O_CREAT | O_APPEND | O_WRONLY | PG_BINARY,
343  S_IRUSR | S_IWUSR);
344  if (outfd == -1)
345  {
346  fprintf(stderr,
347  _("%s: could not open log file \"%s\": %s\n"),
348  progname, outfile, strerror(errno));
349  goto error;
350  }
351 
352  if (fstat(outfd, &statbuf) != 0)
353  fprintf(stderr,
354  _("%s: could not stat file \"%s\": %s\n"),
355  progname, outfile, strerror(errno));
356 
357  output_isfile = S_ISREG(statbuf.st_mode) && !isatty(outfd);
358  }
359 
360  r = PQgetCopyData(conn, &copybuf, 1);
361  if (r == 0)
362  {
363  /*
364  * In async mode, and no data available. We block on reading but
365  * not more than the specified timeout, so that we can send a
366  * response back to the client.
367  */
368  fd_set input_mask;
369  TimestampTz message_target = 0;
370  TimestampTz fsync_target = 0;
371  struct timeval timeout;
372  struct timeval *timeoutptr = NULL;
373 
374  if (PQsocket(conn) < 0)
375  {
376  fprintf(stderr,
377  _("%s: invalid socket: %s"),
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  fprintf(stderr, _("%s: select() failed: %s\n"),
432  progname, strerror(errno));
433  goto error;
434  }
435 
436  /* Else there is actually data on the socket */
437  if (PQconsumeInput(conn) == 0)
438  {
439  fprintf(stderr,
440  _("%s: could not receive data from WAL stream: %s"),
442  goto error;
443  }
444  continue;
445  }
446 
447  /* End of copy stream */
448  if (r == -1)
449  break;
450 
451  /* Failure while reading the copy stream */
452  if (r == -2)
453  {
454  fprintf(stderr, _("%s: could not read COPY data: %s"),
456  goto error;
457  }
458 
459  /* Check the message type. */
460  if (copybuf[0] == 'k')
461  {
462  int pos;
463  bool replyRequested;
464  XLogRecPtr walEnd;
465  bool endposReached = false;
466 
467  /*
468  * Parse the keepalive message, enclosed in the CopyData message.
469  * We just check if the server requested a reply, and ignore the
470  * rest.
471  */
472  pos = 1; /* skip msgtype 'k' */
473  walEnd = fe_recvint64(&copybuf[pos]);
475 
476  pos += 8; /* read walEnd */
477 
478  pos += 8; /* skip sendTime */
479 
480  if (r < pos + 1)
481  {
482  fprintf(stderr, _("%s: streaming header too small: %d\n"),
483  progname, r);
484  goto error;
485  }
486  replyRequested = copybuf[pos];
487 
488  if (endpos != InvalidXLogRecPtr && walEnd >= endpos)
489  {
490  /*
491  * If there's nothing to read on the socket until a keepalive
492  * we know that the server has nothing to send us; and if
493  * walEnd has passed endpos, we know nothing else can have
494  * committed before endpos. So we can bail out now.
495  */
496  endposReached = true;
497  }
498 
499  /* Send a reply, if necessary */
500  if (replyRequested || endposReached)
501  {
502  if (!flushAndSendFeedback(conn, &now))
503  goto error;
504  last_status = now;
505  }
506 
507  if (endposReached)
508  {
510  time_to_abort = true;
511  break;
512  }
513 
514  continue;
515  }
516  else if (copybuf[0] != 'w')
517  {
518  fprintf(stderr, _("%s: unrecognized streaming header: \"%c\"\n"),
519  progname, copybuf[0]);
520  goto error;
521  }
522 
523  /*
524  * Read the header of the XLogData message, enclosed in the CopyData
525  * message. We only need the WAL location field (dataStart), the rest
526  * of the header is ignored.
527  */
528  hdr_len = 1; /* msgtype 'w' */
529  hdr_len += 8; /* dataStart */
530  hdr_len += 8; /* walEnd */
531  hdr_len += 8; /* sendTime */
532  if (r < hdr_len + 1)
533  {
534  fprintf(stderr, _("%s: streaming header too small: %d\n"),
535  progname, r);
536  goto error;
537  }
538 
539  /* Extract WAL location for this block */
540  cur_record_lsn = fe_recvint64(&copybuf[1]);
541 
542  if (endpos != InvalidXLogRecPtr && cur_record_lsn > endpos)
543  {
544  /*
545  * We've read past our endpoint, so prepare to go away being
546  * cautious about what happens to our output data.
547  */
548  if (!flushAndSendFeedback(conn, &now))
549  goto error;
550  prepareToTerminate(conn, endpos, false, cur_record_lsn);
551  time_to_abort = true;
552  break;
553  }
554 
555  output_written_lsn = Max(cur_record_lsn, output_written_lsn);
556 
557  bytes_left = r - hdr_len;
558  bytes_written = 0;
559 
560  /* signal that a fsync is needed */
561  output_needs_fsync = true;
562 
563  while (bytes_left)
564  {
565  int ret;
566 
567  ret = write(outfd,
568  copybuf + hdr_len + bytes_written,
569  bytes_left);
570 
571  if (ret < 0)
572  {
573  fprintf(stderr,
574  _("%s: could not write %u bytes to log file \"%s\": %s\n"),
575  progname, bytes_left, outfile,
576  strerror(errno));
577  goto error;
578  }
579 
580  /* Write was successful, advance our position */
581  bytes_written += ret;
582  bytes_left -= ret;
583  }
584 
585  if (write(outfd, "\n", 1) != 1)
586  {
587  fprintf(stderr,
588  _("%s: could not write %u bytes to log file \"%s\": %s\n"),
589  progname, 1, outfile,
590  strerror(errno));
591  goto error;
592  }
593 
594  if (endpos != InvalidXLogRecPtr && cur_record_lsn == endpos)
595  {
596  /* endpos was exactly the record we just processed, we're done */
597  if (!flushAndSendFeedback(conn, &now))
598  goto error;
599  prepareToTerminate(conn, endpos, false, cur_record_lsn);
600  time_to_abort = true;
601  break;
602  }
603  }
604 
605  res = PQgetResult(conn);
606  if (PQresultStatus(res) == PGRES_COPY_OUT)
607  {
608  /*
609  * We're doing a client-initiated clean exit and have sent CopyDone to
610  * the server. We've already sent replay confirmation and fsync'd so
611  * we can just clean up the connection now.
612  */
613  goto error;
614  }
615  else if (PQresultStatus(res) != PGRES_COMMAND_OK)
616  {
617  fprintf(stderr,
618  _("%s: unexpected termination of replication stream: %s"),
620  goto error;
621  }
622  PQclear(res);
623 
624  if (outfd != -1 && strcmp(outfile, "-") != 0)
625  {
627 
628  /* no need to jump to error on failure here, we're finishing anyway */
629  OutputFsync(t);
630 
631  if (close(outfd) != 0)
632  fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
633  progname, outfile, strerror(errno));
634  }
635  outfd = -1;
636 error:
637  if (copybuf != NULL)
638  {
639  PQfreemem(copybuf);
640  copybuf = NULL;
641  }
642  destroyPQExpBuffer(query);
643  PQfinish(conn);
644  conn = NULL;
645 }
static XLogRecPtr output_fsync_lsn
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
TimestampTz feGetCurrentTimestamp(void)
Definition: streamutil.c:621
static volatile sig_atomic_t time_to_abort
static void error(void)
Definition: sql-dyntest.c:147
static int fsync_interval
#define write(a, b, c)
Definition: win32.h:14
static bool OutputFsync(TimestampTz now)
int64 TimestampTz
Definition: timestamp.h:39
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3638
static char * replication_slot
static bool flushAndSendFeedback(PGconn *conn, TimestampTz *now)
const char * progname
Definition: pg_standby.c:37
bool feTimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: streamutil.c:662
#define PG_BINARY
Definition: c.h:1080
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition: fe-exec.c:2428
static int verbose
static XLogRecPtr endpos
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static bool output_isfile
#define S_IWUSR
Definition: win32_port.h:274
PGconn * conn
Definition: streamutil.c:55
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
void feTimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: streamutil.c:640
static int outfd
#define select(n, r, w, e, timeout)
Definition: win32_port.h:447
unsigned int uint32
Definition: c.h:325
static XLogRecPtr output_written_lsn
static bool output_needs_fsync
#define S_ISREG(m)
Definition: win32_port.h:310
static void prepareToTerminate(PGconn *conn, XLogRecPtr endpos, bool keepalive, XLogRecPtr lsn)
#define stat(a, b)
Definition: win32_port.h:266
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
static bool sendFeedback(PGconn *conn, TimestampTz now, bool force, bool replyRequested)
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1682
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
StringInfo copybuf
Definition: tablesync.c:114
void PQclear(PGresult *res)
Definition: fe-exec.c:671
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt)
Definition: connection.c:107
#define Max(x, y)
Definition: c.h:851
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static XLogRecPtr startpos
#define S_IRUSR
Definition: win32_port.h:271
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2663
int64 fe_recvint64(char *buf)
Definition: streamutil.c:686
int i
const char * strerror(int errnum)
Definition: strerror.c:19
static TimestampTz output_last_fsync
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
static char * outfile
#define close(a)
Definition: win32.h:12
#define EINTR
Definition: win32_port.h:334
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
static size_t noptions
#define _(x)
Definition: elog.c:84
void PQfreemem(void *ptr)
Definition: fe-exec.c:3251
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:6134
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1753
static volatile sig_atomic_t output_reopen
static int standby_message_timeout

◆ usage()

static void usage ( void  )
static

Definition at line 74 of file pg_recvlogical.c.

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

Referenced by main().

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

Variable Documentation

◆ do_create_slot

bool do_create_slot = false
static

Definition at line 45 of file pg_recvlogical.c.

Referenced by main().

◆ do_drop_slot

bool do_drop_slot = false
static

Definition at line 48 of file pg_recvlogical.c.

Referenced by main().

◆ do_start_slot

bool do_start_slot = false
static

Definition at line 47 of file pg_recvlogical.c.

Referenced by main().

◆ endpos

XLogRecPtr endpos = InvalidXLogRecPtr
static

Definition at line 44 of file pg_recvlogical.c.

Referenced by main(), and StreamLogicalLog().

◆ fsync_interval

int fsync_interval = 10 * 1000
static

Definition at line 42 of file pg_recvlogical.c.

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

◆ noloop

int noloop = 0
static

Definition at line 40 of file pg_recvlogical.c.

Referenced by main().

◆ noptions

◆ options

◆ outfd

int outfd = -1
static

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

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

◆ output_isfile

bool output_isfile
static

Definition at line 60 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ output_last_fsync

TimestampTz output_last_fsync = -1
static

Definition at line 61 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ output_needs_fsync

bool output_needs_fsync = false
static

Definition at line 62 of file pg_recvlogical.c.

Referenced by OutputFsync(), and StreamLogicalLog().

◆ output_reopen

volatile sig_atomic_t output_reopen = false
static

Definition at line 59 of file pg_recvlogical.c.

Referenced by sighup_handler(), and StreamLogicalLog().

◆ output_written_lsn

XLogRecPtr output_written_lsn = InvalidXLogRecPtr
static

Definition at line 63 of file pg_recvlogical.c.

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

◆ plugin

const char* plugin = "test_decoding"
static

◆ replication_slot

char* replication_slot = NULL
static

Definition at line 49 of file pg_recvlogical.c.

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

◆ slot_exists_ok

bool slot_exists_ok = false
static

Definition at line 46 of file pg_recvlogical.c.

Referenced by main().

◆ standby_message_timeout

int standby_message_timeout = 10 * 1000
static

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

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

◆ verbose

int verbose = 0
static

Definition at line 39 of file pg_recvlogical.c.

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