PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
libpqwalreceiver.c File Reference
#include "postgres.h"
#include <unistd.h>
#include <sys/time.h>
#include "libpq-fe.h"
#include "pqexpbuffer.h"
#include "access/xlog.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/walreceiver.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/pg_lsn.h"
#include "utils/tuplestore.h"
Include dependency graph for libpqwalreceiver.c:

Go to the source code of this file.

Data Structures

struct  WalReceiverConn
 

Functions

void _PG_init (void)
 
static WalReceiverConnlibpqrcv_connect (const char *conninfo, bool logical, const char *appname, char **err)
 
static void libpqrcv_check_conninfo (const char *conninfo)
 
static char * libpqrcv_get_conninfo (WalReceiverConn *conn)
 
static char * libpqrcv_identify_system (WalReceiverConn *conn, TimeLineID *primary_tli, int *server_version)
 
static void libpqrcv_readtimelinehistoryfile (WalReceiverConn *conn, TimeLineID tli, char **filename, char **content, int *len)
 
static bool libpqrcv_startstreaming (WalReceiverConn *conn, const WalRcvStreamOptions *options)
 
static void libpqrcv_endstreaming (WalReceiverConn *conn, TimeLineID *next_tli)
 
static int libpqrcv_receive (WalReceiverConn *conn, char **buffer, pgsocket *wait_fd)
 
static void libpqrcv_send (WalReceiverConn *conn, const char *buffer, int nbytes)
 
static char * libpqrcv_create_slot (WalReceiverConn *conn, const char *slotname, bool temporary, CRSSnapshotAction snapshot_action, XLogRecPtr *lsn)
 
static WalRcvExecResultlibpqrcv_exec (WalReceiverConn *conn, const char *query, const int nRetTypes, const Oid *retTypes)
 
static void libpqrcv_disconnect (WalReceiverConn *conn)
 
static PGresultlibpqrcv_PQexec (PGconn *streamConn, const char *query)
 
static char * stringlist_to_identifierstr (PGconn *conn, List *strings)
 
static void libpqrcv_processTuples (PGresult *pgres, WalRcvExecResult *walres, const int nRetTypes, const Oid *retTypes)
 

Variables

 PG_MODULE_MAGIC
 
static WalReceiverFunctionsType PQWalReceiverFunctions
 

Function Documentation

void _PG_init ( void  )

Definition at line 104 of file libpqwalreceiver.c.

References elog, ERROR, PQWalReceiverFunctions, and WalReceiverFunctions.

105 {
106  if (WalReceiverFunctions != NULL)
107  elog(ERROR, "libpqwalreceiver already loaded");
109 }
#define ERROR
Definition: elog.h:43
static WalReceiverFunctionsType PQWalReceiverFunctions
WalReceiverFunctionsType * WalReceiverFunctions
Definition: walreceiver.c:80
#define elog
Definition: elog.h:219
static void libpqrcv_check_conninfo ( const char *  conninfo)
static

Definition at line 222 of file libpqwalreceiver.c.

References ereport, errcode(), errmsg(), ERROR, PQconninfoFree(), and PQconninfoParse().

223 {
224  PQconninfoOption *opts = NULL;
225  char *err = NULL;
226 
227  opts = PQconninfoParse(conninfo, &err);
228  if (opts == NULL)
229  ereport(ERROR,
230  (errcode(ERRCODE_SYNTAX_ERROR),
231  errmsg("invalid connection string syntax: %s", err)));
232 
233  PQconninfoFree(opts);
234 }
int errcode(int sqlerrcode)
Definition: elog.c:575
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:4717
#define ERROR
Definition: elog.h:43
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:5947
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
static WalReceiverConn * libpqrcv_connect ( const char *  conninfo,
bool  logical,
const char *  appname,
char **  err 
)
static

Definition at line 117 of file libpqwalreceiver.c.

References Assert, CHECK_FOR_INTERRUPTS, conn, CONNECTION_BAD, CONNECTION_OK, CONNECTION_STARTED, GetDatabaseEncodingName(), i, WalReceiverConn::logical, MyLatch, palloc0(), pchomp(), PGRES_POLLING_FAILED, PGRES_POLLING_OK, PGRES_POLLING_READING, PGRES_POLLING_WRITING, PQconnectPoll(), PQconnectStartParams(), PQerrorMessage(), PQsocket(), PQstatus(), ResetLatch(), status(), WalReceiverConn::streamConn, WAIT_EVENT_LIBPQWALRECEIVER_CONNECT, WaitLatchOrSocket(), WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_SOCKET_CONNECTED, WL_SOCKET_READABLE, and WL_SOCKET_WRITEABLE.

119 {
122  const char *keys[5];
123  const char *vals[5];
124  int i = 0;
125 
126  /*
127  * We use the expand_dbname parameter to process the connection string (or
128  * URI), and pass some extra options. The deliberately undocumented
129  * parameter "replication=true" makes it a replication connection. The
130  * database name is ignored by the server in replication mode, but specify
131  * "replication" for .pgpass lookup.
132  */
133  keys[i] = "dbname";
134  vals[i] = conninfo;
135  keys[++i] = "replication";
136  vals[i] = logical ? "database" : "true";
137  if (!logical)
138  {
139  keys[++i] = "dbname";
140  vals[i] = "replication";
141  }
142  keys[++i] = "fallback_application_name";
143  vals[i] = appname;
144  if (logical)
145  {
146  keys[++i] = "client_encoding";
147  vals[i] = GetDatabaseEncodingName();
148  }
149  keys[++i] = NULL;
150  vals[i] = NULL;
151 
152  Assert(i < sizeof(keys));
153 
154  conn = palloc0(sizeof(WalReceiverConn));
155  conn->streamConn = PQconnectStartParams(keys, vals,
156  /* expand_dbname = */ true);
157  if (PQstatus(conn->streamConn) == CONNECTION_BAD)
158  {
159  *err = pchomp(PQerrorMessage(conn->streamConn));
160  return NULL;
161  }
162 
163  /*
164  * Poll connection until we have OK or FAILED status.
165  *
166  * Per spec for PQconnectPoll, first wait till socket is write-ready.
167  */
168  status = PGRES_POLLING_WRITING;
169  do
170  {
171  int io_flag;
172  int rc;
173 
174  if (status == PGRES_POLLING_READING)
175  io_flag = WL_SOCKET_READABLE;
176 #ifdef WIN32
177  /* Windows needs a different test while waiting for connection-made */
178  else if (PQstatus(conn->streamConn) == CONNECTION_STARTED)
179  io_flag = WL_SOCKET_CONNECTED;
180 #endif
181  else
182  io_flag = WL_SOCKET_WRITEABLE;
183 
186  WL_LATCH_SET | io_flag,
187  PQsocket(conn->streamConn),
188  0,
190 
191  /* Emergency bailout? */
192  if (rc & WL_POSTMASTER_DEATH)
193  exit(1);
194 
195  /* Interrupted? */
196  if (rc & WL_LATCH_SET)
197  {
200  }
201 
202  /* If socket is ready, advance the libpq state machine */
203  if (rc & io_flag)
204  status = PQconnectPoll(conn->streamConn);
205  } while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);
206 
207  if (PQstatus(conn->streamConn) != CONNECTION_OK)
208  {
209  *err = pchomp(PQerrorMessage(conn->streamConn));
210  return NULL;
211  }
212 
213  conn->logical = logical;
214 
215  return conn;
216 }
#define WL_SOCKET_WRITEABLE
Definition: latch.h:126
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
#define WL_SOCKET_READABLE
Definition: latch.h:125
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
char * pchomp(const char *in)
Definition: mcxt.c:1100
PGconn * conn
Definition: streamutil.c:49
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
void * palloc0(Size size)
Definition: mcxt.c:877
PostgresPollingStatusType PQconnectPoll(PGconn *conn)
Definition: fe-connect.c:1997
#define Assert(condition)
Definition: c.h:664
int WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:356
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1021
PostgresPollingStatusType
Definition: libpq-fe.h:72
int i
PGconn * PQconnectStartParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:632
struct Latch * MyLatch
Definition: globals.c:52
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
#define WL_SOCKET_CONNECTED
Definition: latch.h:133
#define WL_LATCH_SET
Definition: latch.h:124
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:6115
static char * libpqrcv_create_slot ( WalReceiverConn conn,
const char *  slotname,
bool  temporary,
CRSSnapshotAction  snapshot_action,
XLogRecPtr lsn 
)
static

Definition at line 764 of file libpqwalreceiver.c.

References appendStringInfo(), appendStringInfoString(), CRS_EXPORT_SNAPSHOT, CRS_NOEXPORT_SNAPSHOT, CRS_USE_SNAPSHOT, CStringGetDatum, StringInfoData::data, DatumGetLSN, DirectFunctionCall1Coll(), ereport, errmsg(), ERROR, initStringInfo(), InvalidOid, libpqrcv_PQexec(), WalReceiverConn::logical, pchomp(), pfree(), pg_lsn_in(), PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQgetisnull(), PQgetvalue(), PQresultStatus(), pstrdup(), and WalReceiverConn::streamConn.

767 {
768  PGresult *res;
769  StringInfoData cmd;
770  char *snapshot;
771 
772  initStringInfo(&cmd);
773 
774  appendStringInfo(&cmd, "CREATE_REPLICATION_SLOT \"%s\"", slotname);
775 
776  if (temporary)
777  appendStringInfoString(&cmd, " TEMPORARY");
778 
779  if (conn->logical)
780  {
781  appendStringInfoString(&cmd, " LOGICAL pgoutput");
782  switch (snapshot_action)
783  {
784  case CRS_EXPORT_SNAPSHOT:
785  appendStringInfoString(&cmd, " EXPORT_SNAPSHOT");
786  break;
788  appendStringInfoString(&cmd, " NOEXPORT_SNAPSHOT");
789  break;
790  case CRS_USE_SNAPSHOT:
791  appendStringInfoString(&cmd, " USE_SNAPSHOT");
792  break;
793  }
794  }
795 
796  res = libpqrcv_PQexec(conn->streamConn, cmd.data);
797  pfree(cmd.data);
798 
799  if (PQresultStatus(res) != PGRES_TUPLES_OK)
800  {
801  PQclear(res);
802  ereport(ERROR,
803  (errmsg("could not create replication slot \"%s\": %s",
804  slotname, pchomp(PQerrorMessage(conn->streamConn)))));
805  }
806 
808  CStringGetDatum(PQgetvalue(res, 0, 1))));
809  if (!PQgetisnull(res, 0, 2))
810  snapshot = pstrdup(PQgetvalue(res, 0, 2));
811  else
812  snapshot = NULL;
813 
814  PQclear(res);
815 
816  return snapshot;
817 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
char * pstrdup(const char *in)
Definition: mcxt.c:1076
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
Datum pg_lsn_in(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:30
char * pchomp(const char *in)
Definition: mcxt.c:1100
void pfree(void *pointer)
Definition: mcxt.c:949
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
#define CStringGetDatum(X)
Definition: postgres.h:584
Datum DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
Definition: fmgr.c:707
#define ereport(elevel, rest)
Definition: elog.h:122
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
static PGresult * libpqrcv_PQexec(PGconn *streamConn, const char *query)
#define InvalidOid
Definition: postgres_ext.h:36
#define DatumGetLSN(X)
Definition: pg_lsn.h:21
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int errmsg(const char *fmt,...)
Definition: elog.c:797
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
static void libpqrcv_disconnect ( WalReceiverConn conn)
static

Definition at line 637 of file libpqwalreceiver.c.

References pfree(), PQfinish(), PQfreemem(), WalReceiverConn::recvBuf, and WalReceiverConn::streamConn.

638 {
639  PQfinish(conn->streamConn);
640  if (conn->recvBuf != NULL)
641  PQfreemem(conn->recvBuf);
642  pfree(conn);
643 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
void pfree(void *pointer)
Definition: mcxt.c:949
void PQfreemem(void *ptr)
Definition: fe-exec.c:3251
static void libpqrcv_endstreaming ( WalReceiverConn conn,
TimeLineID next_tli 
)
static

Definition at line 426 of file libpqwalreceiver.c.

References ereport, errmsg(), ERROR, pchomp(), pg_atoi(), PGRES_COMMAND_OK, PGRES_COPY_OUT, PGRES_TUPLES_OK, PQclear(), PQendcopy(), PQerrorMessage(), PQflush(), PQgetResult(), PQgetvalue(), PQnfields(), PQntuples(), PQputCopyEnd(), PQresultStatus(), and WalReceiverConn::streamConn.

427 {
428  PGresult *res;
429 
430  if (PQputCopyEnd(conn->streamConn, NULL) <= 0 ||
431  PQflush(conn->streamConn))
432  ereport(ERROR,
433  (errmsg("could not send end-of-streaming message to primary: %s",
434  pchomp(PQerrorMessage(conn->streamConn)))));
435 
436  *next_tli = 0;
437 
438  /*
439  * After COPY is finished, we should receive a result set indicating the
440  * next timeline's ID, or just CommandComplete if the server was shut
441  * down.
442  *
443  * If we had not yet received CopyDone from the backend, PGRES_COPY_OUT is
444  * also possible in case we aborted the copy in mid-stream.
445  */
446  res = PQgetResult(conn->streamConn);
447  if (PQresultStatus(res) == PGRES_TUPLES_OK)
448  {
449  /*
450  * Read the next timeline's ID. The server also sends the timeline's
451  * starting point, but it is ignored.
452  */
453  if (PQnfields(res) < 2 || PQntuples(res) != 1)
454  ereport(ERROR,
455  (errmsg("unexpected result set after end-of-streaming")));
456  *next_tli = pg_atoi(PQgetvalue(res, 0, 0), sizeof(uint32), 0);
457  PQclear(res);
458 
459  /* the result set should be followed by CommandComplete */
460  res = PQgetResult(conn->streamConn);
461  }
462  else if (PQresultStatus(res) == PGRES_COPY_OUT)
463  {
464  PQclear(res);
465 
466  /* End the copy */
467  if (PQendcopy(conn->streamConn))
468  ereport(ERROR,
469  (errmsg("error while shutting down streaming COPY: %s",
470  pchomp(PQerrorMessage(conn->streamConn)))));
471 
472  /* CommandComplete should follow */
473  res = PQgetResult(conn->streamConn);
474  }
475 
476  if (PQresultStatus(res) != PGRES_COMMAND_OK)
477  ereport(ERROR,
478  (errmsg("error reading result of streaming command: %s",
479  pchomp(PQerrorMessage(conn->streamConn)))));
480  PQclear(res);
481 
482  /* Verify that there are no more results */
483  res = PQgetResult(conn->streamConn);
484  if (res != NULL)
485  ereport(ERROR,
486  (errmsg("unexpected result after CommandComplete: %s",
487  pchomp(PQerrorMessage(conn->streamConn)))));
488 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2339
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
char * pchomp(const char *in)
Definition: mcxt.c:1100
#define ERROR
Definition: elog.h:43
int PQflush(PGconn *conn)
Definition: fe-exec.c:3238
unsigned int uint32
Definition: c.h:258
#define ereport(elevel, rest)
Definition: elog.h:122
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int PQendcopy(PGconn *conn)
Definition: fe-exec.c:2573
int errmsg(const char *fmt,...)
Definition: elog.c:797
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1753
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:37
static WalRcvExecResult * libpqrcv_exec ( WalReceiverConn conn,
const char *  query,
const int  nRetTypes,
const Oid retTypes 
)
static

Definition at line 898 of file libpqwalreceiver.c.

References _, ereport, WalRcvExecResult::err, errcode(), errmsg(), ERROR, InvalidOid, libpqrcv_PQexec(), libpqrcv_processTuples(), MyDatabaseId, palloc0(), pchomp(), PGRES_BAD_RESPONSE, PGRES_COMMAND_OK, PGRES_COPY_BOTH, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_EMPTY_QUERY, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PGRES_SINGLE_TUPLE, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQresultStatus(), WalRcvExecResult::status, WalReceiverConn::streamConn, WALRCV_ERROR, WALRCV_OK_COMMAND, WALRCV_OK_COPY_BOTH, WALRCV_OK_COPY_IN, WALRCV_OK_COPY_OUT, and WALRCV_OK_TUPLES.

900 {
901  PGresult *pgres = NULL;
902  WalRcvExecResult *walres = palloc0(sizeof(WalRcvExecResult));
903 
904  if (MyDatabaseId == InvalidOid)
905  ereport(ERROR,
906  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
907  errmsg("the query interface requires a database connection")));
908 
909  pgres = libpqrcv_PQexec(conn->streamConn, query);
910 
911  switch (PQresultStatus(pgres))
912  {
913  case PGRES_SINGLE_TUPLE:
914  case PGRES_TUPLES_OK:
915  walres->status = WALRCV_OK_TUPLES;
916  libpqrcv_processTuples(pgres, walres, nRetTypes, retTypes);
917  break;
918 
919  case PGRES_COPY_IN:
920  walres->status = WALRCV_OK_COPY_IN;
921  break;
922 
923  case PGRES_COPY_OUT:
924  walres->status = WALRCV_OK_COPY_OUT;
925  break;
926 
927  case PGRES_COPY_BOTH:
928  walres->status = WALRCV_OK_COPY_BOTH;
929  break;
930 
931  case PGRES_COMMAND_OK:
932  walres->status = WALRCV_OK_COMMAND;
933  break;
934 
935  /* Empty query is considered error. */
936  case PGRES_EMPTY_QUERY:
937  walres->status = WALRCV_ERROR;
938  walres->err = _("empty query");
939  break;
940 
942  case PGRES_FATAL_ERROR:
943  case PGRES_BAD_RESPONSE:
944  walres->status = WALRCV_ERROR;
945  walres->err = pchomp(PQerrorMessage(conn->streamConn));
946  break;
947  }
948 
949  PQclear(pgres);
950 
951  return walres;
952 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
int errcode(int sqlerrcode)
Definition: elog.c:575
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
char * pchomp(const char *in)
Definition: mcxt.c:1100
#define ERROR
Definition: elog.h:43
static void libpqrcv_processTuples(PGresult *pgres, WalRcvExecResult *walres, const int nRetTypes, const Oid *retTypes)
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:877
Oid MyDatabaseId
Definition: globals.c:77
static PGresult * libpqrcv_PQexec(PGconn *streamConn, const char *query)
#define InvalidOid
Definition: postgres_ext.h:36
void PQclear(PGresult *res)
Definition: fe-exec.c:671
WalRcvExecStatus status
Definition: walreceiver.h:187
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define _(x)
Definition: elog.c:84
static char * libpqrcv_get_conninfo ( WalReceiverConn conn)
static

Definition at line 241 of file libpqwalreceiver.c.

References _, appendPQExpBuffer(), Assert, buf, PQExpBufferData::data, _PQconninfoOption::dispchar, ereport, errmsg(), ERROR, initPQExpBuffer(), _PQconninfoOption::keyword, PQExpBufferData::len, PQconninfo(), PQconninfoFree(), PQExpBufferDataBroken, pstrdup(), WalReceiverConn::streamConn, termPQExpBuffer(), and _PQconninfoOption::val.

242 {
243  PQconninfoOption *conn_opts;
244  PQconninfoOption *conn_opt;
246  char *retval;
247 
248  Assert(conn->streamConn != NULL);
249 
250  initPQExpBuffer(&buf);
251  conn_opts = PQconninfo(conn->streamConn);
252 
253  if (conn_opts == NULL)
254  ereport(ERROR,
255  (errmsg("could not parse connection string: %s",
256  _("out of memory"))));
257 
258  /* build a clean connection string from pieces */
259  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
260  {
261  bool obfuscate;
262 
263  /* Skip debug and empty options */
264  if (strchr(conn_opt->dispchar, 'D') ||
265  conn_opt->val == NULL ||
266  conn_opt->val[0] == '\0')
267  continue;
268 
269  /* Obfuscate security-sensitive options */
270  obfuscate = strchr(conn_opt->dispchar, '*') != NULL;
271 
272  appendPQExpBuffer(&buf, "%s%s=%s",
273  buf.len == 0 ? "" : " ",
274  conn_opt->keyword,
275  obfuscate ? "********" : conn_opt->val);
276  }
277 
278  PQconninfoFree(conn_opts);
279 
280  retval = PQExpBufferDataBroken(buf) ? NULL : pstrdup(buf.data);
281  termPQExpBuffer(&buf);
282  return retval;
283 }
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
char * pstrdup(const char *in)
Definition: mcxt.c:1076
#define ERROR
Definition: elog.h:43
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:5947
PQconninfoOption * PQconninfo(PGconn *conn)
Definition: fe-connect.c:5906
#define ereport(elevel, rest)
Definition: elog.h:122
#define PQExpBufferDataBroken(buf)
Definition: pqexpbuffer.h:67
#define Assert(condition)
Definition: c.h:664
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static char * libpqrcv_identify_system ( WalReceiverConn conn,
TimeLineID primary_tli,
int *  server_version 
)
static

Definition at line 290 of file libpqwalreceiver.c.

References ereport, errdetail(), errmsg(), ERROR, libpqrcv_PQexec(), pchomp(), pg_atoi(), PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), PQserverVersion(), pstrdup(), and WalReceiverConn::streamConn.

292 {
293  PGresult *res;
294  char *primary_sysid;
295 
296  /*
297  * Get the system identifier and timeline ID as a DataRow message from the
298  * primary server.
299  */
300  res = libpqrcv_PQexec(conn->streamConn, "IDENTIFY_SYSTEM");
301  if (PQresultStatus(res) != PGRES_TUPLES_OK)
302  {
303  PQclear(res);
304  ereport(ERROR,
305  (errmsg("could not receive database system identifier and timeline ID from "
306  "the primary server: %s",
307  pchomp(PQerrorMessage(conn->streamConn)))));
308  }
309  if (PQnfields(res) < 3 || PQntuples(res) != 1)
310  {
311  int ntuples = PQntuples(res);
312  int nfields = PQnfields(res);
313 
314  PQclear(res);
315  ereport(ERROR,
316  (errmsg("invalid response from primary server"),
317  errdetail("Could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields.",
318  ntuples, nfields, 3, 1)));
319  }
320  primary_sysid = pstrdup(PQgetvalue(res, 0, 0));
321  *primary_tli = pg_atoi(PQgetvalue(res, 0, 1), 4, 0);
322  PQclear(res);
323 
325 
326  return primary_sysid;
327 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
char * pstrdup(const char *in)
Definition: mcxt.c:1076
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6087
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
char * pchomp(const char *in)
Definition: mcxt.c:1100
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
static PGresult * libpqrcv_PQexec(PGconn *streamConn, const char *query)
void PQclear(PGresult *res)
Definition: fe-exec.c:671
static int server_version
Definition: pg_dumpall.c:82
int errmsg(const char *fmt,...)
Definition: elog.c:797
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:37
static PGresult * libpqrcv_PQexec ( PGconn streamConn,
const char *  query 
)
static

Definition at line 549 of file libpqwalreceiver.c.

References CHECK_FOR_INTERRUPTS, CONNECTION_BAD, MyLatch, PGRES_COPY_BOTH, PGRES_COPY_IN, PGRES_COPY_OUT, PQclear(), PQconsumeInput(), PQgetResult(), PQisBusy(), PQresultStatus(), PQsendQuery(), PQsocket(), PQstatus(), ResetLatch(), WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE, WaitLatchOrSocket(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_READABLE.

Referenced by libpqrcv_create_slot(), libpqrcv_exec(), libpqrcv_identify_system(), libpqrcv_readtimelinehistoryfile(), and libpqrcv_startstreaming().

550 {
551  PGresult *result = NULL;
552  PGresult *lastResult = NULL;
553 
554  /*
555  * PQexec() silently discards any prior query results on the connection.
556  * This is not required for this function as it's expected that the caller
557  * (which is this library in all cases) will behave correctly and we don't
558  * have to be backwards compatible with old libpq.
559  */
560 
561  /*
562  * Submit a query. Since we don't use non-blocking mode, this also can
563  * block. But its risk is relatively small, so we ignore that for now.
564  */
565  if (!PQsendQuery(streamConn, query))
566  return NULL;
567 
568  for (;;)
569  {
570  /*
571  * Receive data until PQgetResult is ready to get the result without
572  * blocking.
573  */
574  while (PQisBusy(streamConn))
575  {
576  int rc;
577 
578  /*
579  * We don't need to break down the sleep into smaller increments,
580  * since we'll get interrupted by signals and can either handle
581  * interrupts here or elog(FATAL) within SIGTERM signal handler if
582  * the signal arrives in the middle of establishment of
583  * replication connection.
584  */
587  WL_LATCH_SET,
588  PQsocket(streamConn),
589  0,
591 
592  /* Emergency bailout? */
593  if (rc & WL_POSTMASTER_DEATH)
594  exit(1);
595 
596  /* Interrupted? */
597  if (rc & WL_LATCH_SET)
598  {
601  }
602 
603  /* Consume whatever data is available from the socket */
604  if (PQconsumeInput(streamConn) == 0)
605  {
606  /* trouble; drop whatever we had and return NULL */
607  PQclear(lastResult);
608  return NULL;
609  }
610  }
611 
612  /*
613  * Emulate PQexec()'s behavior of returning the last result when there
614  * are many. We are fine with returning just last error message.
615  */
616  result = PQgetResult(streamConn);
617  if (result == NULL)
618  break; /* query is complete */
619 
620  PQclear(lastResult);
621  lastResult = result;
622 
623  if (PQresultStatus(lastResult) == PGRES_COPY_IN ||
624  PQresultStatus(lastResult) == PGRES_COPY_OUT ||
625  PQresultStatus(lastResult) == PGRES_COPY_BOTH ||
626  PQstatus(streamConn) == CONNECTION_BAD)
627  break;
628  }
629 
630  return lastResult;
631 }
#define WL_SOCKET_READABLE
Definition: latch.h:125
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1183
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1682
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:356
int PQisBusy(PGconn *conn)
Definition: fe-exec.c:1732
struct Latch * MyLatch
Definition: globals.c:52
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
#define WL_LATCH_SET
Definition: latch.h:124
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:6115
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1753
static void libpqrcv_processTuples ( PGresult pgres,
WalRcvExecResult walres,
const int  nRetTypes,
const Oid retTypes 
)
static

Definition at line 823 of file libpqwalreceiver.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), BuildTupleFromCStrings(), CHECK_FOR_INTERRUPTS, CreateTemplateTupleDesc(), CurrentMemoryContext, ereport, errdetail(), errmsg(), ERROR, MaxTupleAttributeNumber, MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), PQfname(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), WalRcvExecResult::tupledesc, TupleDescGetAttInMetadata(), TupleDescInitEntry(), WalRcvExecResult::tuplestore, tuplestore_begin_heap(), tuplestore_puttuple(), and work_mem.

Referenced by libpqrcv_exec().

825 {
826  int tupn;
827  int coln;
828  int nfields = PQnfields(pgres);
829  HeapTuple tuple;
830  AttInMetadata *attinmeta;
831  MemoryContext rowcontext;
832  MemoryContext oldcontext;
833 
834  /* Make sure we got expected number of fields. */
835  if (nfields != nRetTypes)
836  ereport(ERROR,
837  (errmsg("invalid query response"),
838  errdetail("Expected %d fields, got %d fields.",
839  nRetTypes, nfields)));
840 
841  walres->tuplestore = tuplestore_begin_heap(true, false, work_mem);
842 
843  /* Create tuple descriptor corresponding to expected result. */
844  walres->tupledesc = CreateTemplateTupleDesc(nRetTypes, false);
845  for (coln = 0; coln < nRetTypes; coln++)
846  TupleDescInitEntry(walres->tupledesc, (AttrNumber) coln + 1,
847  PQfname(pgres, coln), retTypes[coln], -1, 0);
848  attinmeta = TupleDescGetAttInMetadata(walres->tupledesc);
849 
850  /* No point in doing more here if there were no tuples returned. */
851  if (PQntuples(pgres) == 0)
852  return;
853 
854  /* Create temporary context for local allocations. */
856  "libpqrcv query result context",
858 
859  /* Process returned rows. */
860  for (tupn = 0; tupn < PQntuples(pgres); tupn++)
861  {
862  char *cstrs[MaxTupleAttributeNumber];
863 
865 
866  /* Do the allocations in temporary context. */
867  oldcontext = MemoryContextSwitchTo(rowcontext);
868 
869  /*
870  * Fill cstrs with null-terminated strings of column values.
871  */
872  for (coln = 0; coln < nfields; coln++)
873  {
874  if (PQgetisnull(pgres, tupn, coln))
875  cstrs[coln] = NULL;
876  else
877  cstrs[coln] = PQgetvalue(pgres, tupn, coln);
878  }
879 
880  /* Convert row to a tuple, and add it to the tuplestore */
881  tuple = BuildTupleFromCStrings(attinmeta, cstrs);
882  tuplestore_puttuple(walres->tuplestore, tuple);
883 
884  /* Clean up */
885  MemoryContextSwitchTo(oldcontext);
886  MemoryContextReset(rowcontext);
887  }
888 
889  MemoryContextDelete(rowcontext);
890 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2810
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1118
TupleDesc tupledesc
Definition: walreceiver.h:190
#define ERROR
Definition: elog.h:43
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
int errdetail(const char *fmt,...)
Definition: elog.c:873
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:505
#define ereport(elevel, rest)
Definition: elog.h:122
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
int work_mem
Definition: globals.c:113
Tuplestorestate * tuplestore
Definition: walreceiver.h:189
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:43
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
int16 AttrNumber
Definition: attnum.h:21
static void libpqrcv_readtimelinehistoryfile ( WalReceiverConn conn,
TimeLineID  tli,
char **  filename,
char **  content,
int *  len 
)
static

Definition at line 494 of file libpqwalreceiver.c.

References Assert, ereport, errdetail(), errmsg(), ERROR, libpqrcv_PQexec(), WalReceiverConn::logical, palloc(), pchomp(), PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQgetlength(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), pstrdup(), snprintf(), and WalReceiverConn::streamConn.

497 {
498  PGresult *res;
499  char cmd[64];
500 
501  Assert(!conn->logical);
502 
503  /*
504  * Request the primary to send over the history file for given timeline.
505  */
506  snprintf(cmd, sizeof(cmd), "TIMELINE_HISTORY %u", tli);
507  res = libpqrcv_PQexec(conn->streamConn, cmd);
508  if (PQresultStatus(res) != PGRES_TUPLES_OK)
509  {
510  PQclear(res);
511  ereport(ERROR,
512  (errmsg("could not receive timeline history file from "
513  "the primary server: %s",
514  pchomp(PQerrorMessage(conn->streamConn)))));
515  }
516  if (PQnfields(res) != 2 || PQntuples(res) != 1)
517  {
518  int ntuples = PQntuples(res);
519  int nfields = PQnfields(res);
520 
521  PQclear(res);
522  ereport(ERROR,
523  (errmsg("invalid response from primary server"),
524  errdetail("Expected 1 tuple with 2 fields, got %d tuples with %d fields.",
525  ntuples, nfields)));
526  }
527  *filename = pstrdup(PQgetvalue(res, 0, 0));
528 
529  *len = PQgetlength(res, 0, 1);
530  *content = palloc(*len);
531  memcpy(*content, PQgetvalue(res, 0, 1), *len);
532  PQclear(res);
533 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3129
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
char * pstrdup(const char *in)
Definition: mcxt.c:1076
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
char * pchomp(const char *in)
Definition: mcxt.c:1100
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
static PGresult * libpqrcv_PQexec(PGconn *streamConn, const char *query)
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define Assert(condition)
Definition: c.h:664
static char * filename
Definition: pg_dumpall.c:90
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
static int libpqrcv_receive ( WalReceiverConn conn,
char **  buffer,
pgsocket wait_fd 
)
static

Definition at line 662 of file libpqwalreceiver.c.

References CONNECTION_BAD, ereport, errmsg(), ERROR, pchomp(), PGRES_COMMAND_OK, PGRES_COPY_IN, PQclear(), PQconsumeInput(), PQerrorMessage(), PQfreemem(), PQgetCopyData(), PQgetResult(), PQresultStatus(), PQsocket(), PQstatus(), WalReceiverConn::recvBuf, and WalReceiverConn::streamConn.

664 {
665  int rawlen;
666 
667  if (conn->recvBuf != NULL)
668  PQfreemem(conn->recvBuf);
669  conn->recvBuf = NULL;
670 
671  /* Try to receive a CopyData message */
672  rawlen = PQgetCopyData(conn->streamConn, &conn->recvBuf, 1);
673  if (rawlen == 0)
674  {
675  /* Try consuming some data. */
676  if (PQconsumeInput(conn->streamConn) == 0)
677  ereport(ERROR,
678  (errmsg("could not receive data from WAL stream: %s",
679  pchomp(PQerrorMessage(conn->streamConn)))));
680 
681  /* Now that we've consumed some input, try again */
682  rawlen = PQgetCopyData(conn->streamConn, &conn->recvBuf, 1);
683  if (rawlen == 0)
684  {
685  /* Tell caller to try again when our socket is ready. */
686  *wait_fd = PQsocket(conn->streamConn);
687  return 0;
688  }
689  }
690  if (rawlen == -1) /* end-of-streaming or error */
691  {
692  PGresult *res;
693 
694  res = PQgetResult(conn->streamConn);
695  if (PQresultStatus(res) == PGRES_COMMAND_OK)
696  {
697  PQclear(res);
698 
699  /* Verify that there are no more results. */
700  res = PQgetResult(conn->streamConn);
701  if (res != NULL)
702  {
703  PQclear(res);
704 
705  /*
706  * If the other side closed the connection orderly (otherwise
707  * we'd seen an error, or PGRES_COPY_IN) don't report an error
708  * here, but let callers deal with it.
709  */
710  if (PQstatus(conn->streamConn) == CONNECTION_BAD)
711  return -1;
712 
713  ereport(ERROR,
714  (errmsg("unexpected result after CommandComplete: %s",
715  PQerrorMessage(conn->streamConn))));
716  }
717 
718  return -1;
719  }
720  else if (PQresultStatus(res) == PGRES_COPY_IN)
721  {
722  PQclear(res);
723  return -1;
724  }
725  else
726  {
727  PQclear(res);
728  ereport(ERROR,
729  (errmsg("could not receive data from WAL stream: %s",
730  pchomp(PQerrorMessage(conn->streamConn)))));
731  }
732  }
733  if (rawlen < -1)
734  ereport(ERROR,
735  (errmsg("could not receive data from WAL stream: %s",
736  pchomp(PQerrorMessage(conn->streamConn)))));
737 
738  /* Return received messages to caller */
739  *buffer = conn->recvBuf;
740  return rawlen;
741 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition: fe-exec.c:2428
char * pchomp(const char *in)
Definition: mcxt.c:1100
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1682
void PQclear(PGresult *res)
Definition: fe-exec.c:671
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
int errmsg(const char *fmt,...)
Definition: elog.c:797
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
void PQfreemem(void *ptr)
Definition: fe-exec.c:3251
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:6115
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1753
static void libpqrcv_send ( WalReceiverConn conn,
const char *  buffer,
int  nbytes 
)
static

Definition at line 749 of file libpqwalreceiver.c.

References ereport, errmsg(), ERROR, pchomp(), PQerrorMessage(), PQflush(), PQputCopyData(), and WalReceiverConn::streamConn.

750 {
751  if (PQputCopyData(conn->streamConn, buffer, nbytes) <= 0 ||
752  PQflush(conn->streamConn))
753  ereport(ERROR,
754  (errmsg("could not send data to WAL stream: %s",
755  pchomp(PQerrorMessage(conn->streamConn)))));
756 }
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2272
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
char * pchomp(const char *in)
Definition: mcxt.c:1100
#define ERROR
Definition: elog.h:43
int PQflush(PGconn *conn)
Definition: fe-exec.c:3238
#define ereport(elevel, rest)
Definition: elog.h:122
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
int errmsg(const char *fmt,...)
Definition: elog.c:797
static bool libpqrcv_startstreaming ( WalReceiverConn conn,
const WalRcvStreamOptions options 
)
static

Definition at line 340 of file libpqwalreceiver.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert, StringInfoData::data, ereport, errmsg(), ERROR, initStringInfo(), libpqrcv_PQexec(), WalReceiverConn::logical, WalRcvStreamOptions::logical, pchomp(), pfree(), PGRES_COMMAND_OK, PGRES_COPY_BOTH, WalRcvStreamOptions::physical, PQclear(), PQerrorMessage(), PQescapeLiteral(), PQfreemem(), PQresultStatus(), WalRcvStreamOptions::proto, WalRcvStreamOptions::slotname, WalRcvStreamOptions::startpoint, WalReceiverConn::streamConn, and stringlist_to_identifierstr().

342 {
343  StringInfoData cmd;
344  PGresult *res;
345 
346  Assert(options->logical == conn->logical);
347  Assert(options->slotname || !options->logical);
348 
349  initStringInfo(&cmd);
350 
351  /* Build the command. */
352  appendStringInfoString(&cmd, "START_REPLICATION");
353  if (options->slotname != NULL)
354  appendStringInfo(&cmd, " SLOT \"%s\"",
355  options->slotname);
356 
357  if (options->logical)
358  appendStringInfoString(&cmd, " LOGICAL");
359 
360  appendStringInfo(&cmd, " %X/%X",
361  (uint32) (options->startpoint >> 32),
362  (uint32) options->startpoint);
363 
364  /*
365  * Additional options are different depending on if we are doing logical
366  * or physical replication.
367  */
368  if (options->logical)
369  {
370  char *pubnames_str;
371  List *pubnames;
372  char *pubnames_literal;
373 
374  appendStringInfoString(&cmd, " (");
375 
376  appendStringInfo(&cmd, "proto_version '%u'",
377  options->proto.logical.proto_version);
378 
379  pubnames = options->proto.logical.publication_names;
380  pubnames_str = stringlist_to_identifierstr(conn->streamConn, pubnames);
381  if (!pubnames_str)
382  ereport(ERROR,
383  (errmsg("could not start WAL streaming: %s",
384  pchomp(PQerrorMessage(conn->streamConn)))));
385  pubnames_literal = PQescapeLiteral(conn->streamConn, pubnames_str,
386  strlen(pubnames_str));
387  if (!pubnames_literal)
388  ereport(ERROR,
389  (errmsg("could not start WAL streaming: %s",
390  pchomp(PQerrorMessage(conn->streamConn)))));
391  appendStringInfo(&cmd, ", publication_names %s", pubnames_literal);
392  PQfreemem(pubnames_literal);
393  pfree(pubnames_str);
394 
395  appendStringInfoChar(&cmd, ')');
396  }
397  else
398  appendStringInfo(&cmd, " TIMELINE %u",
399  options->proto.physical.startpointTLI);
400 
401  /* Start streaming. */
402  res = libpqrcv_PQexec(conn->streamConn, cmd.data);
403  pfree(cmd.data);
404 
405  if (PQresultStatus(res) == PGRES_COMMAND_OK)
406  {
407  PQclear(res);
408  return false;
409  }
410  else if (PQresultStatus(res) != PGRES_COPY_BOTH)
411  {
412  PQclear(res);
413  ereport(ERROR,
414  (errmsg("could not start WAL streaming: %s",
415  pchomp(PQerrorMessage(conn->streamConn)))));
416  }
417  PQclear(res);
418  return true;
419 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
char * pchomp(const char *in)
Definition: mcxt.c:1100
void pfree(void *pointer)
Definition: mcxt.c:949
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
XLogRecPtr startpoint
Definition: walreceiver.h:145
unsigned int uint32
Definition: c.h:258
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
union WalRcvStreamOptions::@97 proto
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:3519
static PGresult * libpqrcv_PQexec(PGconn *streamConn, const char *query)
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define Assert(condition)
Definition: c.h:664
static char * stringlist_to_identifierstr(PGconn *conn, List *strings)
int errmsg(const char *fmt,...)
Definition: elog.c:797
struct WalRcvStreamOptions::@97::@98 physical
Definition: pg_list.h:45
void PQfreemem(void *ptr)
Definition: fe-exec.c:3251
static char * stringlist_to_identifierstr ( PGconn conn,
List strings 
)
static

Definition at line 963 of file libpqwalreceiver.c.

References appendStringInfoChar(), appendStringInfoString(), StringInfoData::data, free, initStringInfo(), lfirst, PQescapeIdentifier(), PQfreemem(), strVal, and val.

Referenced by libpqrcv_startstreaming().

964 {
965  ListCell *lc;
966  StringInfoData res;
967  bool first = true;
968 
969  initStringInfo(&res);
970 
971  foreach(lc, strings)
972  {
973  char *val = strVal(lfirst(lc));
974  char *val_escaped;
975 
976  if (first)
977  first = false;
978  else
979  appendStringInfoChar(&res, ',');
980 
981  val_escaped = PQescapeIdentifier(conn, val, strlen(val));
982  if (!val_escaped)
983  {
984  free(res.data);
985  return NULL;
986  }
987  appendStringInfoString(&res, val_escaped);
988  PQfreemem(val_escaped);
989  }
990 
991  return res.data;
992 }
#define strVal(v)
Definition: value.h:54
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:3525
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define free(a)
Definition: header.h:65
#define lfirst(lc)
Definition: pg_list.h:106
void PQfreemem(void *ptr)
Definition: fe-exec.c:3251
long val
Definition: informix.c:689

Variable Documentation

PG_MODULE_MAGIC

Definition at line 36 of file libpqwalreceiver.c.

WalReceiverFunctionsType PQWalReceiverFunctions
static
Initial value:
= {
}
static bool libpqrcv_startstreaming(WalReceiverConn *conn, const WalRcvStreamOptions *options)
static void libpqrcv_check_conninfo(const char *conninfo)
static int libpqrcv_receive(WalReceiverConn *conn, char **buffer, pgsocket *wait_fd)
static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn, TimeLineID tli, char **filename, char **content, int *len)
static void libpqrcv_endstreaming(WalReceiverConn *conn, TimeLineID *next_tli)
static WalRcvExecResult * libpqrcv_exec(WalReceiverConn *conn, const char *query, const int nRetTypes, const Oid *retTypes)
static char * libpqrcv_create_slot(WalReceiverConn *conn, const char *slotname, bool temporary, CRSSnapshotAction snapshot_action, XLogRecPtr *lsn)
static char * libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli, int *server_version)
static void libpqrcv_disconnect(WalReceiverConn *conn)
static WalReceiverConn * libpqrcv_connect(const char *conninfo, bool logical, const char *appname, char **err)
static char * libpqrcv_get_conninfo(WalReceiverConn *conn)
static void libpqrcv_send(WalReceiverConn *conn, const char *buffer, int nbytes)

Definition at line 81 of file libpqwalreceiver.c.

Referenced by _PG_init().