Debian Codegraph @

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * libpqwalreceiver.c
4 *
5 * This file contains the libpq-specific parts of walreceiver. It's
6 * loaded as a dynamic module to avoid linking the main server binary with
7 * libpq.
8 *
9 * Apart from walreceiver, the libpq-specific routines are now being used by
10 * logical replication workers and slot synchronization.
11 *
12 * Portions Copyright (c) 2010-2025, PostgreSQL Global Development Group
13 *
14 *
15 * IDENTIFICATION
16 * src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
17 *
18 *-------------------------------------------------------------------------
19 */
20 #include "postgres.h"
21
22 #include <unistd.h>
23 #include <sys/time.h>
24
25 #include "common/connect.h"
26 #include "funcapi.h"
27 #include "libpq-fe.h"
28 #include "libpq/libpq-be-fe-helpers.h"
29 #include "mb/pg_wchar.h"
30 #include "miscadmin.h"
31 #include "pgstat.h"
32 #include "pqexpbuffer.h"
33 #include "replication/walreceiver.h"
34 #include "storage/latch.h"
35 #include "utils/builtins.h"
36 #include "utils/memutils.h"
37 #include "utils/pg_lsn.h"
38 #include "utils/tuplestore.h"
39
40 PG_MODULE_MAGIC_EXT(
41 .name = "libpqwalreceiver",
42 .version = PG_VERSION
43 );
44
45 struct WalReceiverConn
46 {
47 /* Current connection to the primary, if any */
48 PGconn *streamConn;
49 /* Used to remember if the connection is logical or physical */
50 bool logical;
51 /* Buffer for currently read records */
52 char *recvBuf;
53 };
54
55 /* Prototypes for interface functions */
56 static WalReceiverConn *libpqrcv_connect(const char *conninfo,
57 bool replication, bool logical,
58 bool must_use_password,
59 const char *appname, char **err);
60 static void libpqrcv_check_conninfo(const char *conninfo,
61 bool must_use_password);
62 static char *libpqrcv_get_conninfo(WalReceiverConn *conn);
63 static void libpqrcv_get_senderinfo(WalReceiverConn *conn,
64 char **sender_host, int *sender_port);
65 static char *libpqrcv_identify_system(WalReceiverConn *conn,
66 TimeLineID *primary_tli);
67 static char *libpqrcv_get_dbname_from_conninfo(const char *connInfo);
68 static char *libpqrcv_get_option_from_conninfo(const char *connInfo,
69 const char *keyword);
70 static int libpqrcv_server_version(WalReceiverConn *conn);
71 static void libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn,
72 TimeLineID tli, char **filename,
73 char **content, int *len);
74 static bool libpqrcv_startstreaming(WalReceiverConn *conn,
75 const WalRcvStreamOptions *options);
76 static void libpqrcv_endstreaming(WalReceiverConn *conn,
77 TimeLineID *next_tli);
78 static int libpqrcv_receive(WalReceiverConn *conn, char **buffer,
79 pgsocket *wait_fd);
80 static void libpqrcv_send(WalReceiverConn *conn, const char *buffer,
81 int nbytes);
82 static char *libpqrcv_create_slot(WalReceiverConn *conn,
83 const char *slotname,
84 bool temporary,
85 bool two_phase,
86 bool failover,
87 CRSSnapshotAction snapshot_action,
88 XLogRecPtr *lsn);
89 static void libpqrcv_alter_slot(WalReceiverConn *conn, const char *slotname,
90 const bool *failover, const bool *two_phase);
91 static pid_t libpqrcv_get_backend_pid(WalReceiverConn *conn);
92 static WalRcvExecResult *libpqrcv_exec(WalReceiverConn *conn,
93 const char *query,
94 const int nRetTypes,
95 const Oid *retTypes);
96 static void libpqrcv_disconnect(WalReceiverConn *conn);
97
98 static WalReceiverFunctionsType PQWalReceiverFunctions = {
99 .walrcv_connect = libpqrcv_connect,
100 .walrcv_check_conninfo = libpqrcv_check_conninfo,
101 .walrcv_get_conninfo = libpqrcv_get_conninfo,
102 .walrcv_get_senderinfo = libpqrcv_get_senderinfo,
103 .walrcv_identify_system = libpqrcv_identify_system,
104 .walrcv_server_version = libpqrcv_server_version,
105 .walrcv_readtimelinehistoryfile = libpqrcv_readtimelinehistoryfile,
106 .walrcv_startstreaming = libpqrcv_startstreaming,
107 .walrcv_endstreaming = libpqrcv_endstreaming,
108 .walrcv_receive = libpqrcv_receive,
109 .walrcv_send = libpqrcv_send,
110 .walrcv_create_slot = libpqrcv_create_slot,
111 .walrcv_alter_slot = libpqrcv_alter_slot,
112 .walrcv_get_dbname_from_conninfo = libpqrcv_get_dbname_from_conninfo,
113 .walrcv_get_backend_pid = libpqrcv_get_backend_pid,
114 .walrcv_exec = libpqrcv_exec,
115 .walrcv_disconnect = libpqrcv_disconnect
116 };
117
118 /* Prototypes for private functions */
119 static char *stringlist_to_identifierstr(List *strings);
120
121 /*
122 * Module initialization function
123 */
124 void
125 _PG_init(void)
126 {
127 if (WalReceiverFunctions != NULL)
128 elog(ERROR, "libpqwalreceiver already loaded");
129 WalReceiverFunctions = &PQWalReceiverFunctions;
130 }
131
132 /*
133 * Establish the connection to the primary server.
134 *
135 * This function can be used for both replication and regular connections.
136 * If it is a replication connection, it could be either logical or physical
137 * based on input argument 'logical'.
138 *
139 * If an error occurs, this function will normally return NULL and set *err
140 * to a palloc'ed error message. However, if must_use_password is true and
141 * the connection fails to use the password, this function will ereport(ERROR).
142 * We do this because in that case the error includes a detail and a hint for
143 * consistency with other parts of the system, and it's not worth adding the
144 * machinery to pass all of those back to the caller just to cover this one
145 * case.
146 */
147 static WalReceiverConn *
148 libpqrcv_connect(const char *conninfo, bool replication, bool logical,
149 bool must_use_password, const char *appname, char **err)
150 {
151 WalReceiverConn *conn;
152 const char *keys[6];
153 const char *vals[6];
154 int i = 0;
155 char *options_val = NULL;
156
157 /*
158 * Re-validate connection string. The validation already happened at DDL
159 * time, but the subscription owner may have changed. If we don't recheck
160 * with the correct must_use_password, it's possible that the connection
161 * will obtain the password from a different source, such as PGPASSFILE or
162 * PGPASSWORD.
163 */
164 libpqrcv_check_conninfo(conninfo, must_use_password);
165
166 /*
167 * We use the expand_dbname parameter to process the connection string (or
168 * URI), and pass some extra options.
169 */
170 keys[i] = "dbname";
171 vals[i] = conninfo;
172
173 /* We can not have logical without replication */
174 Assert(replication || !logical);
175
176 if (replication)
177 {
178 keys[++i] = "replication";
179 vals[i] = logical ? "database" : "true";
180
181 if (logical)
182 {
183 char *opt = NULL;
184
185 /* Tell the publisher to translate to our encoding */
186 keys[++i] = "client_encoding";
187 vals[i] = GetDatabaseEncodingName();
188
189 /*
190 * Force assorted GUC parameters to settings that ensure that the
191 * publisher will output data values in a form that is unambiguous
192 * to the subscriber. (We don't want to modify the subscriber's
193 * GUC settings, since that might surprise user-defined code
194 * running in the subscriber, such as triggers.) This should
195 * match what pg_dump does.
196 */
197 opt = libpqrcv_get_option_from_conninfo(conninfo, "options");
198 options_val = psprintf("%s -c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3",
199 (opt == NULL) ? "" : opt);
200 keys[++i] = "options";
201 vals[i] = options_val;
202 if (opt != NULL)
203 pfree(opt);
204 }
205 else
206 {
207 /*
208 * The database name is ignored by the server in replication mode,
209 * but specify "replication" for .pgpass lookup.
210 */
211 keys[++i] = "dbname";
212 vals[i] = "replication";
213 }
214 }
215
216 keys[++i] = "fallback_application_name";
217 vals[i] = appname;
218
219 keys[++i] = NULL;
220 vals[i] = NULL;
221
222 Assert(i < lengthof(keys));
223
224 conn = palloc0(sizeof(WalReceiverConn));
225 conn->streamConn =
226 libpqsrv_connect_params(keys, vals,
227 /* expand_dbname = */ true,
228 WAIT_EVENT_LIBPQWALRECEIVER_CONNECT);
229
230 if (options_val != NULL)
231 pfree(options_val);
232
233 if (PQstatus(conn->streamConn) != CONNECTION_OK)
234 goto bad_connection_errmsg;
235
236 if (must_use_password && !PQconnectionUsedPassword(conn->streamConn))
237 {
238 libpqsrv_disconnect(conn->streamConn);
239 pfree(conn);
240
241 ereport(ERROR,
242 (errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
243 errmsg("password is required"),
244 errdetail("Non-superuser cannot connect if the server does not request a password."),
245 errhint("Target server's authentication method must be changed, or set password_required=false in the subscription parameters.")));
246 }
247
248 /*
249 * Set always-secure search path for the cases where the connection is
250 * used to run SQL queries, so malicious users can't get control.
251 */
252 if (!replication || logical)
253 {
254 PGresult *res;
255
256 res = libpqsrv_exec(conn->streamConn,
257 ALWAYS_SECURE_SEARCH_PATH_SQL,
258 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
259 if (PQresultStatus(res) != PGRES_TUPLES_OK)
260 {
261 PQclear(res);
262 *err = psprintf(_("could not clear search path: %s"),
263 pchomp(PQerrorMessage(conn->streamConn)));
264 goto bad_connection;
265 }
266 PQclear(res);
267 }
268
269 conn->logical = logical;
270
271 return conn;
272
273 /* error path, using libpq's error message */
274 bad_connection_errmsg:
275 *err = pchomp(PQerrorMessage(conn->streamConn));
276
277 /* error path, error already set */
278 bad_connection:
279 libpqsrv_disconnect(conn->streamConn);
280 pfree(conn);
281 return NULL;
282 }
283
284 /*
285 * Validate connection info string.
286 *
287 * If the connection string can't be parsed, this function will raise
288 * an error. If must_use_password is true, the function raises an error
289 * if no password is provided in the connection string. In any other case
290 * it successfully completes.
291 */
292 static void
293 libpqrcv_check_conninfo(const char *conninfo, bool must_use_password)
294 {
295 PQconninfoOption *opts = NULL;
296 PQconninfoOption *opt;
297 char *err = NULL;
298
299 opts = PQconninfoParse(conninfo, &err);
300 if (opts == NULL)
301 {
302 /* The error string is malloc'd, so we must free it explicitly */
303 char *errcopy = err ? pstrdup(err) : "out of memory";
304
305 PQfreemem(err);
306 ereport(ERROR,
307 (errcode(ERRCODE_SYNTAX_ERROR),
308 errmsg("invalid connection string syntax: %s", errcopy)));
309 }
310
311 if (must_use_password)
312 {
313 bool uses_password = false;
314
315 for (opt = opts; opt->keyword != NULL; ++opt)
316 {
317 /* Ignore connection options that are not present. */
318 if (opt->val == NULL)
319 continue;
320
321 if (strcmp(opt->keyword, "password") == 0 && opt->val[0] != '\0')
322 {
323 uses_password = true;
324 break;
325 }
326 }
327
328 if (!uses_password)
329 {
330 /* malloc'd, so we must free it explicitly */
331 PQconninfoFree(opts);
332
333 ereport(ERROR,
334 (errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
335 errmsg("password is required"),
336 errdetail("Non-superusers must provide a password in the connection string.")));
337 }
338 }
339
340 PQconninfoFree(opts);
341 }
342
343 /*
344 * Return a user-displayable conninfo string. Any security-sensitive fields
345 * are obfuscated.
346 */
347 static char *
348 libpqrcv_get_conninfo(WalReceiverConn *conn)
349 {
350 PQconninfoOption *conn_opts;
351 PQconninfoOption *conn_opt;
352 PQExpBufferData buf;
353 char *retval;
354
355 Assert(conn->streamConn != NULL);
356
357 initPQExpBuffer(&buf);
358 conn_opts = PQconninfo(conn->streamConn);
359
360 if (conn_opts == NULL)
361 ereport(ERROR,
362 (errcode(ERRCODE_OUT_OF_MEMORY),
363 errmsg("could not parse connection string: %s",
364 _("out of memory"))));
365
366 /* build a clean connection string from pieces */
367 for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
368 {
369 bool obfuscate;
370
371 /* Skip debug and empty options */
372 if (strchr(conn_opt->dispchar, 'D') ||
373 conn_opt->val == NULL ||
374 conn_opt->val[0] == '\0')
375 continue;
376
377 /* Obfuscate security-sensitive options */
378 obfuscate = strchr(conn_opt->dispchar, '*') != NULL;
379
380 appendPQExpBuffer(&buf, "%s%s=%s",
381 buf.len == 0 ? "" : " ",
382 conn_opt->keyword,
383 obfuscate ? "********" : conn_opt->val);
384 }
385
386 PQconninfoFree(conn_opts);
387
388 retval = PQExpBufferDataBroken(buf) ? NULL : pstrdup(buf.data);
389 termPQExpBuffer(&buf);
390 return retval;
391 }
392
393 /*
394 * Provides information of sender this WAL receiver is connected to.
395 */
396 static void
397 libpqrcv_get_senderinfo(WalReceiverConn *conn, char **sender_host,
398 int *sender_port)
399 {
400 char *ret = NULL;
401
402 *sender_host = NULL;
403 *sender_port = 0;
404
405 Assert(conn->streamConn != NULL);
406
407 ret = PQhost(conn->streamConn);
408 if (ret && strlen(ret) != 0)
409 *sender_host = pstrdup(ret);
410
411 ret = PQport(conn->streamConn);
412 if (ret && strlen(ret) != 0)
413 *sender_port = atoi(ret);
414 }
415
416 /*
417 * Check that primary's system identifier matches ours, and fetch the current
418 * timeline ID of the primary.
419 */
420 static char *
421 libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli)
422 {
423 PGresult *res;
424 char *primary_sysid;
425
426 /*
427 * Get the system identifier and timeline ID as a DataRow message from the
428 * primary server.
429 */
430 res = libpqsrv_exec(conn->streamConn,
431 "IDENTIFY_SYSTEM",
432 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
433 if (PQresultStatus(res) != PGRES_TUPLES_OK)
434 {
435 PQclear(res);
436 ereport(ERROR,
437 (errcode(ERRCODE_PROTOCOL_VIOLATION),
438 errmsg("could not receive database system identifier and timeline ID from "
439 "the primary server: %s",
440 pchomp(PQerrorMessage(conn->streamConn)))));
441 }
442
443 /*
444 * IDENTIFY_SYSTEM returns 3 columns in 9.3 and earlier, and 4 columns in
445 * 9.4 and onwards.
446 */
447 if (PQnfields(res) < 3 || PQntuples(res) != 1)
448 {
449 int ntuples = PQntuples(res);
450 int nfields = PQnfields(res);
451
452 PQclear(res);
453 ereport(ERROR,
454 (errcode(ERRCODE_PROTOCOL_VIOLATION),
455 errmsg("invalid response from primary server"),
456 errdetail("Could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields.",
457 ntuples, nfields, 1, 3)));
458 }
459 primary_sysid = pstrdup(PQgetvalue(res, 0, 0));
460 *primary_tli = pg_strtoint32(PQgetvalue(res, 0, 1));
461
462 /* Column 2 is the server's current WAL flush position */
463 {
464 uint32 hi,
465 lo;
466
467 if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
468 ereport(ERROR,
469 (errcode(ERRCODE_PROTOCOL_VIOLATION),
470 errmsg("could not parse WAL location \"%s\"",
471 PQgetvalue(res, 0, 2))));
472 WalRcvIdentifySystemLsn = ((uint64) hi) << 32 | lo;
473 }
474
475 PQclear(res);
476
477 return primary_sysid;
478 }
479
480 /*
481 * Thin wrapper around libpq to obtain server version.
482 */
483 static int
484 libpqrcv_server_version(WalReceiverConn *conn)
485 {
486 return PQserverVersion(conn->streamConn);
487 }
488
489 /*
490 * Get database name from the primary server's conninfo.
491 *
492 * If dbname is not found in connInfo, return NULL value.
493 */
494 static char *
495 libpqrcv_get_dbname_from_conninfo(const char *connInfo)
496 {
497 return libpqrcv_get_option_from_conninfo(connInfo, "dbname");
498 }
499
500 /*
501 * Get the value of the option with the given keyword from the primary
502 * server's conninfo.
503 *
504 * If the option is not found in connInfo, return NULL value.
505 */
506 static char *
507 libpqrcv_get_option_from_conninfo(const char *connInfo, const char *keyword)
508 {
509 PQconninfoOption *opts;
510 char *option = NULL;
511 char *err = NULL;
512
513 opts = PQconninfoParse(connInfo, &err);
514 if (opts == NULL)
515 {
516 /* The error string is malloc'd, so we must free it explicitly */
517 char *errcopy = err ? pstrdup(err) : "out of memory";
518
519 PQfreemem(err);
520 ereport(ERROR,
521 (errcode(ERRCODE_SYNTAX_ERROR),
522 errmsg("invalid connection string syntax: %s", errcopy)));
523 }
524
525 for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
526 {
527 /*
528 * If the same option appears multiple times, then the last one will
529 * be returned
530 */
531 if (strcmp(opt->keyword, keyword) == 0 && opt->val &&
532 *opt->val)
533 {
534 if (option)
535 pfree(option);
536
537 option = pstrdup(opt->val);
538 }
539 }
540
541 PQconninfoFree(opts);
542 return option;
543 }
544
545 /*
546 * Append a suitably-quoted identifier or string literal to buf.
547 * "quote" should be either a double-quote or single-quote character.
548 *
549 * Caution: this quoting logic is sufficient for identifiers and literals
550 * in the replication grammar, but not always in regular SQL. Specifically,
551 * it'd fail for a string literal if standard_conforming_strings is off.
552 */
553 static void
554 appendQuotedString(StringInfo buf, const char *str, char quote)
555 {
556 appendStringInfoChar(buf, quote);
557 while (*str)
558 {
559 char c = *str++;
560
561 if (c == quote)
562 appendStringInfoChar(buf, c);
563 appendStringInfoChar(buf, c);
564 }
565 appendStringInfoChar(buf, quote);
566 }
567
568 #define appendQuotedIdentifier(b, s) appendQuotedString(b, s, '"')
569 #define appendQuotedLiteral(b, s) appendQuotedString(b, s, '\'')
570
571 /*
572 * Start streaming WAL data from given streaming options.
573 *
574 * Returns true if we switched successfully to copy-both mode. False
575 * means the server received the command and executed it successfully, but
576 * didn't switch to copy-mode. That means that there was no WAL on the
577 * requested timeline and starting point, because the server switched to
578 * another timeline at or before the requested starting point. On failure,
579 * throws an ERROR.
580 */
581 static bool
582 libpqrcv_startstreaming(WalReceiverConn *conn,
583 const WalRcvStreamOptions *options)
584 {
585 StringInfoData cmd;
586 PGresult *res;
587
588 Assert(options->logical == conn->logical);
589 Assert(options->slotname || !options->logical);
590
591 initStringInfo(&cmd);
592
593 /* Build the command. */
594 appendStringInfoString(&cmd, "START_REPLICATION");
595 if (options->slotname != NULL)
596 {
597 appendStringInfoString(&cmd, " SLOT ");
598 appendQuotedIdentifier(&cmd, options->slotname);
599 }
600
601 if (options->logical)
602 appendStringInfoString(&cmd, " LOGICAL");
603
604 appendStringInfo(&cmd, " %X/%X", LSN_FORMAT_ARGS(options->startpoint));
605
606 /*
607 * Additional options are different depending on if we are doing logical
608 * or physical replication.
609 */
610 if (options->logical)
611 {
612 char *pubnames_str;
613 List *pubnames;
614
615 appendStringInfoString(&cmd, " (");
616
617 appendStringInfo(&cmd, "proto_version '%u'",
618 options->proto.logical.proto_version);
619
620 if (options->proto.logical.streaming_str)
621 {
622 appendStringInfoString(&cmd, ", streaming ");
623 appendQuotedLiteral(&cmd, options->proto.logical.streaming_str);
624 }
625
626 if (options->proto.logical.twophase &&
627 PQserverVersion(conn->streamConn) >= 150000)
628 appendStringInfoString(&cmd, ", two_phase 'on'");
629
630 if (options->proto.logical.origin &&
631 PQserverVersion(conn->streamConn) >= 160000)
632 {
633 appendStringInfoString(&cmd, ", origin ");
634 appendQuotedLiteral(&cmd, options->proto.logical.origin);
635 }
636
637 pubnames = options->proto.logical.publication_names;
638 pubnames_str = stringlist_to_identifierstr(pubnames);
639 appendStringInfoString(&cmd, ", publication_names ");
640 appendQuotedLiteral(&cmd, pubnames_str);
641 pfree(pubnames_str);
642
643 if (options->proto.logical.binary &&
644 PQserverVersion(conn->streamConn) >= 140000)
645 appendStringInfoString(&cmd, ", binary 'true'");
646
647 appendStringInfoChar(&cmd, ')');
648 }
649 else
650 appendStringInfo(&cmd, " TIMELINE %u",
651 options->proto.physical.startpointTLI);
652
653 /* Start streaming. */
654 res = libpqsrv_exec(conn->streamConn,
655 cmd.data,
656 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
657 pfree(cmd.data);
658
659 if (PQresultStatus(res) == PGRES_COMMAND_OK)
660 {
661 PQclear(res);
662 return false;
663 }
664 else if (PQresultStatus(res) != PGRES_COPY_BOTH)
665 {
666 PQclear(res);
667 ereport(ERROR,
668 (errcode(ERRCODE_PROTOCOL_VIOLATION),
669 errmsg("could not start WAL streaming: %s",
670 pchomp(PQerrorMessage(conn->streamConn)))));
671 }
672 PQclear(res);
673 return true;
674 }
675
676 /*
677 * Stop streaming WAL data. Returns the next timeline's ID in *next_tli, as
678 * reported by the server, or 0 if it did not report it.
679 */
680 static void
681 libpqrcv_endstreaming(WalReceiverConn *conn, TimeLineID *next_tli)
682 {
683 PGresult *res;
684
685 /*
686 * Send copy-end message. As in libpqsrv_exec, this could theoretically
687 * block, but the risk seems small.
688 */
689 if (PQputCopyEnd(conn->streamConn, NULL) <= 0 ||
690 PQflush(conn->streamConn))
691 ereport(ERROR,
692 (errcode(ERRCODE_CONNECTION_FAILURE),
693 errmsg("could not send end-of-streaming message to primary: %s",
694 pchomp(PQerrorMessage(conn->streamConn)))));
695
696 *next_tli = 0;
697
698 /*
699 * After COPY is finished, we should receive a result set indicating the
700 * next timeline's ID, or just CommandComplete if the server was shut
701 * down.
702 *
703 * If we had not yet received CopyDone from the backend, PGRES_COPY_OUT is
704 * also possible in case we aborted the copy in mid-stream.
705 */
706 res = libpqsrv_get_result(conn->streamConn,
707 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
708 if (PQresultStatus(res) == PGRES_TUPLES_OK)
709 {
710 /*
711 * Read the next timeline's ID. The server also sends the timeline's
712 * starting point, but it is ignored.
713 */
714 if (PQnfields(res) < 2 || PQntuples(res) != 1)
715 ereport(ERROR,
716 (errcode(ERRCODE_PROTOCOL_VIOLATION),
717 errmsg("unexpected result set after end-of-streaming")));
718 *next_tli = pg_strtoint32(PQgetvalue(res, 0, 0));
719 PQclear(res);
720
721 /* the result set should be followed by CommandComplete */
722 res = libpqsrv_get_result(conn->streamConn,
723 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
724 }
725 else if (PQresultStatus(res) == PGRES_COPY_OUT)
726 {
727 PQclear(res);
728
729 /* End the copy */
730 if (PQendcopy(conn->streamConn))
731 ereport(ERROR,
732 (errcode(ERRCODE_CONNECTION_FAILURE),
733 errmsg("error while shutting down streaming COPY: %s",
734 pchomp(PQerrorMessage(conn->streamConn)))));
735
736 /* CommandComplete should follow */
737 res = libpqsrv_get_result(conn->streamConn,
738 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
739 }
740
741 if (PQresultStatus(res) != PGRES_COMMAND_OK)
742 ereport(ERROR,
743 (errcode(ERRCODE_PROTOCOL_VIOLATION),
744 errmsg("error reading result of streaming command: %s",
745 pchomp(PQerrorMessage(conn->streamConn)))));
746 PQclear(res);
747
748 /* Verify that there are no more results */
749 res = libpqsrv_get_result(conn->streamConn,
750 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
751 if (res != NULL)
752 ereport(ERROR,
753 (errcode(ERRCODE_PROTOCOL_VIOLATION),
754 errmsg("unexpected result after CommandComplete: %s",
755 pchomp(PQerrorMessage(conn->streamConn)))));
756 }
757
758 /*
759 * Fetch the timeline history file for 'tli' from primary.
760 */
761 static void
762 libpqrcv_readtimelinehistoryfile(WalReceiverConn *conn,
763 TimeLineID tli, char **filename,
764 char **content, int *len)
765 {
766 PGresult *res;
767 char cmd[64];
768
769 Assert(!conn->logical);
770
771 /*
772 * Request the primary to send over the history file for given timeline.
773 */
774 snprintf(cmd, sizeof(cmd), "TIMELINE_HISTORY %u", tli);
775 res = libpqsrv_exec(conn->streamConn,
776 cmd,
777 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
778 if (PQresultStatus(res) != PGRES_TUPLES_OK)
779 {
780 PQclear(res);
781 ereport(ERROR,
782 (errcode(ERRCODE_PROTOCOL_VIOLATION),
783 errmsg("could not receive timeline history file from "
784 "the primary server: %s",
785 pchomp(PQerrorMessage(conn->streamConn)))));
786 }
787 if (PQnfields(res) != 2 || PQntuples(res) != 1)
788 {
789 int ntuples = PQntuples(res);
790 int nfields = PQnfields(res);
791
792 PQclear(res);
793 ereport(ERROR,
794 (errcode(ERRCODE_PROTOCOL_VIOLATION),
795 errmsg("invalid response from primary server"),
796 errdetail("Expected 1 tuple with 2 fields, got %d tuples with %d fields.",
797 ntuples, nfields)));
798 }
799 *filename = pstrdup(PQgetvalue(res, 0, 0));
800
801 *len = PQgetlength(res, 0, 1);
802 *content = palloc(*len);
803 memcpy(*content, PQgetvalue(res, 0, 1), *len);
804 PQclear(res);
805 }
806
807 /*
808 * Disconnect connection to primary, if any.
809 */
810 static void
811 libpqrcv_disconnect(WalReceiverConn *conn)
812 {
813 libpqsrv_disconnect(conn->streamConn);
814 PQfreemem(conn->recvBuf);
815 pfree(conn);
816 }
817
818 /*
819 * Receive a message available from XLOG stream.
820 *
821 * Returns:
822 *
823 * If data was received, returns the length of the data. *buffer is set to
824 * point to a buffer holding the received message. The buffer is only valid
825 * until the next libpqrcv_* call.
826 *
827 * If no data was available immediately, returns 0, and *wait_fd is set to a
828 * socket descriptor which can be waited on before trying again.
829 *
830 * -1 if the server ended the COPY.
831 *
832 * ereports on error.
833 */
834 static int
835 libpqrcv_receive(WalReceiverConn *conn, char **buffer,
836 pgsocket *wait_fd)
837 {
838 int rawlen;
839
840 PQfreemem(conn->recvBuf);
841 conn->recvBuf = NULL;
842
843 /* Try to receive a CopyData message */
844 rawlen = PQgetCopyData(conn->streamConn, &conn->recvBuf, 1);
845 if (rawlen == 0)
846 {
847 /* Try consuming some data. */
848 if (PQconsumeInput(conn->streamConn) == 0)
849 ereport(ERROR,
850 (errcode(ERRCODE_CONNECTION_FAILURE),
851 errmsg("could not receive data from WAL stream: %s",
852 pchomp(PQerrorMessage(conn->streamConn)))));
853
854 /* Now that we've consumed some input, try again */
855 rawlen = PQgetCopyData(conn->streamConn, &conn->recvBuf, 1);
856 if (rawlen == 0)
857 {
858 /* Tell caller to try again when our socket is ready. */
859 *wait_fd = PQsocket(conn->streamConn);
860 return 0;
861 }
862 }
863 if (rawlen == -1) /* end-of-streaming or error */
864 {
865 PGresult *res;
866
867 res = libpqsrv_get_result(conn->streamConn,
868 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
869 if (PQresultStatus(res) == PGRES_COMMAND_OK)
870 {
871 PQclear(res);
872
873 /* Verify that there are no more results. */
874 res = libpqsrv_get_result(conn->streamConn,
875 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
876 if (res != NULL)
877 {
878 PQclear(res);
879
880 /*
881 * If the other side closed the connection orderly (otherwise
882 * we'd seen an error, or PGRES_COPY_IN) don't report an error
883 * here, but let callers deal with it.
884 */
885 if (PQstatus(conn->streamConn) == CONNECTION_BAD)
886 return -1;
887
888 ereport(ERROR,
889 (errcode(ERRCODE_PROTOCOL_VIOLATION),
890 errmsg("unexpected result after CommandComplete: %s",
891 PQerrorMessage(conn->streamConn))));
892 }
893
894 return -1;
895 }
896 else if (PQresultStatus(res) == PGRES_COPY_IN)
897 {
898 PQclear(res);
899 return -1;
900 }
901 else
902 {
903 PQclear(res);
904 ereport(ERROR,
905 (errcode(ERRCODE_PROTOCOL_VIOLATION),
906 errmsg("could not receive data from WAL stream: %s",
907 pchomp(PQerrorMessage(conn->streamConn)))));
908 }
909 }
910 if (rawlen < -1)
911 ereport(ERROR,
912 (errcode(ERRCODE_PROTOCOL_VIOLATION),
913 errmsg("could not receive data from WAL stream: %s",
914 pchomp(PQerrorMessage(conn->streamConn)))));
915
916 /* Return received messages to caller */
917 *buffer = conn->recvBuf;
918 return rawlen;
919 }
920
921 /*
922 * Send a message to XLOG stream.
923 *
924 * ereports on error.
925 */
926 static void
927 libpqrcv_send(WalReceiverConn *conn, const char *buffer, int nbytes)
928 {
929 if (PQputCopyData(conn->streamConn, buffer, nbytes) <= 0 ||
930 PQflush(conn->streamConn))
931 ereport(ERROR,
932 (errcode(ERRCODE_CONNECTION_FAILURE),
933 errmsg("could not send data to WAL stream: %s",
934 pchomp(PQerrorMessage(conn->streamConn)))));
935 }
936
937 /*
938 * Create new replication slot.
939 * Returns the name of the exported snapshot for logical slot or NULL for
940 * physical slot.
941 */
942 static char *
943 libpqrcv_create_slot(WalReceiverConn *conn, const char *slotname,
944 bool temporary, bool two_phase, bool failover,
945 CRSSnapshotAction snapshot_action, XLogRecPtr *lsn)
946 {
947 PGresult *res;
948 StringInfoData cmd;
949 char *snapshot;
950 int use_new_options_syntax;
951
952 use_new_options_syntax = (PQserverVersion(conn->streamConn) >= 150000);
953
954 initStringInfo(&cmd);
955
956 appendStringInfoString(&cmd, "CREATE_REPLICATION_SLOT ");
957 appendQuotedIdentifier(&cmd, slotname);
958
959 if (temporary)
960 appendStringInfoString(&cmd, " TEMPORARY");
961
962 if (conn->logical)
963 {
964 appendStringInfoString(&cmd, " LOGICAL pgoutput ");
965 if (use_new_options_syntax)
966 appendStringInfoChar(&cmd, '(');
967 if (two_phase)
968 {
969 appendStringInfoString(&cmd, "TWO_PHASE");
970 if (use_new_options_syntax)
971 appendStringInfoString(&cmd, ", ");
972 else
973 appendStringInfoChar(&cmd, ' ');
974 }
975
976 if (failover)
977 {
978 appendStringInfoString(&cmd, "FAILOVER");
979 if (use_new_options_syntax)
980 appendStringInfoString(&cmd, ", ");
981 else
982 appendStringInfoChar(&cmd, ' ');
983 }
984
985 if (use_new_options_syntax)
986 {
987 switch (snapshot_action)
988 {
989 case CRS_EXPORT_SNAPSHOT:
990 appendStringInfoString(&cmd, "SNAPSHOT 'export'");
991 break;
992 case CRS_NOEXPORT_SNAPSHOT:
993 appendStringInfoString(&cmd, "SNAPSHOT 'nothing'");
994 break;
995 case CRS_USE_SNAPSHOT:
996 appendStringInfoString(&cmd, "SNAPSHOT 'use'");
997 break;
998 }
999 }
1000 else
1001 {
1002 switch (snapshot_action)
1003 {
1004 case CRS_EXPORT_SNAPSHOT:
1005 appendStringInfoString(&cmd, "EXPORT_SNAPSHOT");
1006 break;
1007 case CRS_NOEXPORT_SNAPSHOT:
1008 appendStringInfoString(&cmd, "NOEXPORT_SNAPSHOT");
1009 break;
1010 case CRS_USE_SNAPSHOT:
1011 appendStringInfoString(&cmd, "USE_SNAPSHOT");
1012 break;
1013 }
1014 }
1015
1016 if (use_new_options_syntax)
1017 appendStringInfoChar(&cmd, ')');
1018 }
1019 else
1020 {
1021 if (use_new_options_syntax)
1022 appendStringInfoString(&cmd, " PHYSICAL (RESERVE_WAL)");
1023 else
1024 appendStringInfoString(&cmd, " PHYSICAL RESERVE_WAL");
1025 }
1026
1027 res = libpqsrv_exec(conn->streamConn,
1028 cmd.data,
1029 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
1030 pfree(cmd.data);
1031
1032 if (PQresultStatus(res) != PGRES_TUPLES_OK)
1033 {
1034 PQclear(res);
1035 ereport(ERROR,
1036 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1037 errmsg("could not create replication slot \"%s\": %s",
1038 slotname, pchomp(PQerrorMessage(conn->streamConn)))));
1039 }
1040
1041 /* CREATE_REPLICATION_SLOT returns a single row with four columns */
1042 if (PQnfields(res) != 4 || PQntuples(res) != 1)
1043 ereport(ERROR,
1044 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1045 errmsg("invalid response from primary server"),
1046 errdetail("Could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields.",
1047 slotname, PQntuples(res), PQnfields(res), 1, 4)));
1048
1049 if (lsn)
1050 *lsn = DatumGetLSN(DirectFunctionCall1Coll(pg_lsn_in, InvalidOid,
1051 CStringGetDatum(PQgetvalue(res, 0, 1))));
1052
1053 if (!PQgetisnull(res, 0, 2))
1054 snapshot = pstrdup(PQgetvalue(res, 0, 2));
1055 else
1056 snapshot = NULL;
1057
1058 PQclear(res);
1059
1060 return snapshot;
1061 }
1062
1063 /*
1064 * Change the definition of the replication slot.
1065 */
1066 static void
1067 libpqrcv_alter_slot(WalReceiverConn *conn, const char *slotname,
1068 const bool *failover, const bool *two_phase)
1069 {
1070 StringInfoData cmd;
1071 PGresult *res;
1072
1073 initStringInfo(&cmd);
1074 appendStringInfoString(&cmd, "ALTER_REPLICATION_SLOT ");
1075 appendQuotedIdentifier(&cmd, slotname);
1076 appendStringInfoString(&cmd, " ( ");
1077
1078 if (failover)
1079 appendStringInfo(&cmd, "FAILOVER %s",
1080 *failover ? "true" : "false");
1081
1082 if (failover && two_phase)
1083 appendStringInfoString(&cmd, ", ");
1084
1085 if (two_phase)
1086 appendStringInfo(&cmd, "TWO_PHASE %s",
1087 *two_phase ? "true" : "false");
1088
1089 appendStringInfoString(&cmd, " );");
1090
1091 res = libpqsrv_exec(conn->streamConn, cmd.data,
1092 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
1093 pfree(cmd.data);
1094
1095 if (PQresultStatus(res) != PGRES_COMMAND_OK)
1096 ereport(ERROR,
1097 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1098 errmsg("could not alter replication slot \"%s\": %s",
1099 slotname, pchomp(PQerrorMessage(conn->streamConn)))));
1100
1101 PQclear(res);
1102 }
1103
1104 /*
1105 * Return PID of remote backend process.
1106 */
1107 static pid_t
1108 libpqrcv_get_backend_pid(WalReceiverConn *conn)
1109 {
1110 return PQbackendPID(conn->streamConn);
1111 }
1112
1113 /*
1114 * Convert tuple query result to tuplestore.
1115 */
1116 static void
1117 libpqrcv_processTuples(PGresult *pgres, WalRcvExecResult *walres,
1118 const int nRetTypes, const Oid *retTypes)
1119 {
1120 int tupn;
1121 int coln;
1122 int nfields = PQnfields(pgres);
1123 HeapTuple tuple;
1124 AttInMetadata *attinmeta;
1125 MemoryContext rowcontext;
1126 MemoryContext oldcontext;
1127
1128 /* Make sure we got expected number of fields. */
1129 if (nfields != nRetTypes)
1130 ereport(ERROR,
1131 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1132 errmsg("invalid query response"),
1133 errdetail("Expected %d fields, got %d fields.",
1134 nRetTypes, nfields)));
1135
1136 walres->tuplestore = tuplestore_begin_heap(true, false, work_mem);
1137
1138 /* Create tuple descriptor corresponding to expected result. */
1139 walres->tupledesc = CreateTemplateTupleDesc(nRetTypes);
1140 for (coln = 0; coln < nRetTypes; coln++)
1141 TupleDescInitEntry(walres->tupledesc, (AttrNumber) coln + 1,
1142 PQfname(pgres, coln), retTypes[coln], -1, 0);
1143 attinmeta = TupleDescGetAttInMetadata(walres->tupledesc);
1144
1145 /* No point in doing more here if there were no tuples returned. */
1146 if (PQntuples(pgres) == 0)
1147 return;
1148
1149 /* Create temporary context for local allocations. */
1150 rowcontext = AllocSetContextCreate(CurrentMemoryContext,
1151 "libpqrcv query result context",
1152 ALLOCSET_DEFAULT_SIZES);
1153
1154 /* Process returned rows. */
1155 for (tupn = 0; tupn < PQntuples(pgres); tupn++)
1156 {
1157 char *cstrs[MaxTupleAttributeNumber];
1158
1159 CHECK_FOR_INTERRUPTS();
1160
1161 /* Do the allocations in temporary context. */
1162 oldcontext = MemoryContextSwitchTo(rowcontext);
1163
1164 /*
1165 * Fill cstrs with null-terminated strings of column values.
1166 */
1167 for (coln = 0; coln < nfields; coln++)
1168 {
1169 if (PQgetisnull(pgres, tupn, coln))
1170 cstrs[coln] = NULL;
1171 else
1172 cstrs[coln] = PQgetvalue(pgres, tupn, coln);
1173 }
1174
1175 /* Convert row to a tuple, and add it to the tuplestore */
1176 tuple = BuildTupleFromCStrings(attinmeta, cstrs);
1177 tuplestore_puttuple(walres->tuplestore, tuple);
1178
1179 /* Clean up */
1180 MemoryContextSwitchTo(oldcontext);
1181 MemoryContextReset(rowcontext);
1182 }
1183
1184 MemoryContextDelete(rowcontext);
1185 }
1186
1187 /*
1188 * Public interface for sending generic queries (and commands).
1189 *
1190 * This can only be called from process connected to database.
1191 */
1192 static WalRcvExecResult *
1193 libpqrcv_exec(WalReceiverConn *conn, const char *query,
1194 const int nRetTypes, const Oid *retTypes)
1195 {
1196 PGresult *pgres = NULL;
1197 WalRcvExecResult *walres = palloc0(sizeof(WalRcvExecResult));
1198 char *diag_sqlstate;
1199
1200 if (MyDatabaseId == InvalidOid)
1201 ereport(ERROR,
1202 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1203 errmsg("the query interface requires a database connection")));
1204
1205 pgres = libpqsrv_exec(conn->streamConn,
1206 query,
1207 WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
1208
1209 switch (PQresultStatus(pgres))
1210 {
1211 case PGRES_TUPLES_OK:
1212 case PGRES_SINGLE_TUPLE:
1213 case PGRES_TUPLES_CHUNK:
1214 walres->status = WALRCV_OK_TUPLES;
1215 libpqrcv_processTuples(pgres, walres, nRetTypes, retTypes);
1216 break;
1217
1218 case PGRES_COPY_IN:
1219 walres->status = WALRCV_OK_COPY_IN;
1220 break;
1221
1222 case PGRES_COPY_OUT:
1223 walres->status = WALRCV_OK_COPY_OUT;
1224 break;
1225
1226 case PGRES_COPY_BOTH:
1227 walres->status = WALRCV_OK_COPY_BOTH;
1228 break;
1229
1230 case PGRES_COMMAND_OK:
1231 walres->status = WALRCV_OK_COMMAND;
1232 break;
1233
1234 /* Empty query is considered error. */
1235 case PGRES_EMPTY_QUERY:
1236 walres->status = WALRCV_ERROR;
1237 walres->err = _("empty query");
1238 break;
1239
1240 case PGRES_PIPELINE_SYNC:
1241 case PGRES_PIPELINE_ABORTED:
1242 walres->status = WALRCV_ERROR;
1243 walres->err = _("unexpected pipeline mode");
1244 break;
1245
1246 case PGRES_NONFATAL_ERROR:
1247 case PGRES_FATAL_ERROR:
1248 case PGRES_BAD_RESPONSE:
1249 walres->status = WALRCV_ERROR;
1250 walres->err = pchomp(PQerrorMessage(conn->streamConn));
1251 diag_sqlstate = PQresultErrorField(pgres, PG_DIAG_SQLSTATE);
1252 if (diag_sqlstate)
1253 walres->sqlstate = MAKE_SQLSTATE(diag_sqlstate[0],
1254 diag_sqlstate[1],
1255 diag_sqlstate[2],
1256 diag_sqlstate[3],
1257 diag_sqlstate[4]);
1258 break;
1259 }
1260
1261 PQclear(pgres);
1262
1263 return walres;
1264 }
1265
1266 /*
1267 * Given a List of strings, return it as single comma separated
1268 * string, quoting identifiers as needed.
1269 *
1270 * This is essentially the reverse of SplitIdentifierString.
1271 *
1272 * The caller should pfree the result.
1273 */
1274 static char *
1275 stringlist_to_identifierstr(List *strings)
1276 {
1277 ListCell *lc;
1278 StringInfoData res;
1279 bool first = true;
1280
1281 initStringInfo(&res);
1282
1283 foreach(lc, strings)
1284 {
1285 char *val = strVal(lfirst(lc));
1286
1287 if (first)
1288 first = false;
1289 else
1290 appendStringInfoChar(&res, ',');
1291 appendQuotedIdentifier(&res, val);
1292 }
1293
1294 return res.data;
1295 }