PostgreSQL Source Code git master
Loading...
Searching...
No Matches
receivelog.h File Reference
#include "access/xlogdefs.h"
#include "libpq-fe.h"
#include "walmethods.h"
Include dependency graph for receivelog.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  StreamCtl
 

Typedefs

typedef bool(* stream_stop_callback) (XLogRecPtr segendpos, uint32 timeline, bool segment_finished)
 
typedef struct StreamCtl StreamCtl
 

Functions

bool CheckServerVersionForStreaming (PGconn *conn)
 
bool ReceiveXlogStream (PGconn *conn, StreamCtl *stream)
 

Typedef Documentation

◆ stream_stop_callback

typedef bool(* stream_stop_callback) (XLogRecPtr segendpos, uint32 timeline, bool segment_finished)

Definition at line 23 of file receivelog.h.

◆ StreamCtl

Function Documentation

◆ CheckServerVersionForStreaming()

bool CheckServerVersionForStreaming ( PGconn conn)
extern

Definition at line 375 of file receivelog.c.

376{
377 int minServerMajor,
379 int serverMajor;
380
381 /*
382 * The message format used in streaming replication changed in 9.3, so we
383 * cannot stream from older servers. And we don't support servers newer
384 * than the client; it might work, but we don't know, so err on the safe
385 * side.
386 */
387 minServerMajor = 903;
391 {
392 const char *serverver = PQparameterStatus(conn, "server_version");
393
394 pg_log_error("incompatible server version %s; client does not support streaming from server versions older than %s",
395 serverver ? serverver : "'unknown'",
396 "9.3");
397 return false;
398 }
399 else if (serverMajor > maxServerMajor)
400 {
401 const char *serverver = PQparameterStatus(conn, "server_version");
402
403 pg_log_error("incompatible server version %s; client does not support streaming from server versions newer than %s",
404 serverver ? serverver : "'unknown'",
405 PG_VERSION);
406 return false;
407 }
408 return true;
409}
int PQserverVersion(const PGconn *conn)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
#define pg_log_error(...)
Definition logging.h:108
static int fb(int x)
PGconn * conn
Definition streamutil.c:52

References conn, fb(), pg_log_error, PQparameterStatus(), and PQserverVersion().

Referenced by BaseBackup(), ReceiveXlogStream(), and StreamLog().

◆ ReceiveXlogStream()

bool ReceiveXlogStream ( PGconn conn,
StreamCtl stream 
)
extern

Definition at line 453 of file receivelog.c.

454{
455 PQExpBuffer query;
456 PGresult *res;
458
459 /*
460 * The caller should've checked the server version already, but doesn't do
461 * any harm to check it here too.
462 */
464 return false;
465
466 /*
467 * Decide whether we want to report the flush position. If we report the
468 * flush position, the primary will know what WAL we'll possibly
469 * re-request, and it can then remove older WAL safely. We must always do
470 * that when we are using slots.
471 *
472 * Reporting the flush position makes one eligible as a synchronous
473 * replica. People shouldn't include generic names in
474 * synchronous_standby_names, but we've protected them against it so far,
475 * so let's continue to do so unless specifically requested.
476 */
477 if (stream->replication_slot != NULL)
478 {
479 reportFlushPosition = true;
480 }
481 else
482 {
483 if (stream->synchronous)
484 reportFlushPosition = true;
485 else
486 reportFlushPosition = false;
487 }
488
489 if (stream->sysidentifier != NULL)
490 {
491 char *sysidentifier = NULL;
493
494 /*
495 * Get the server system identifier and timeline, and validate them.
496 */
497 if (!RunIdentifySystem(conn, &sysidentifier, &servertli, NULL, NULL))
498 {
499 pg_free(sysidentifier);
500 return false;
501 }
502
503 if (strcmp(stream->sysidentifier, sysidentifier) != 0)
504 {
505 pg_log_error("system identifier does not match between base backup and streaming connection");
506 pg_free(sysidentifier);
507 return false;
508 }
509 pg_free(sysidentifier);
510
511 if (stream->timeline > servertli)
512 {
513 pg_log_error("starting timeline %u is not present in the server",
514 stream->timeline);
515 return false;
516 }
517 }
518
519 /*
520 * initialize flush position to starting point, it's the caller's
521 * responsibility that that's sane.
522 */
523 lastFlushPosition = stream->startpos;
524
525 while (1)
526 {
527 /*
528 * Fetch the timeline history file for this timeline, if we don't have
529 * it already. When streaming log to tar, this will always return
530 * false, as we are never streaming into an existing file and
531 * therefore there can be no pre-existing timeline history file.
532 */
533 if (!existsTimeLineHistoryFile(stream))
534 {
535 query = createPQExpBuffer();
536 appendPQExpBuffer(query, "TIMELINE_HISTORY %u", stream->timeline);
537 res = PQexec(conn, query->data);
538 destroyPQExpBuffer(query);
540 {
541 /* FIXME: we might send it ok, but get an error */
542 pg_log_error("could not send replication command \"%s\": %s",
543 "TIMELINE_HISTORY", PQresultErrorMessage(res));
544 PQclear(res);
545 return false;
546 }
547
548 /*
549 * The response to TIMELINE_HISTORY is a single row result set
550 * with two fields: filename and content
551 */
552 if (PQnfields(res) != 2 || PQntuples(res) != 1)
553 {
554 pg_log_warning("unexpected response to TIMELINE_HISTORY command: got %d rows and %d fields, expected %d rows and %d fields",
555 PQntuples(res), PQnfields(res), 1, 2);
556 }
557
558 /* Write the history file to disk */
560 PQgetvalue(res, 0, 0),
561 PQgetvalue(res, 0, 1));
562
563 PQclear(res);
564 }
565
566 /*
567 * Before we start streaming from the requested location, check if the
568 * callback tells us to stop here.
569 */
570 if (stream->stream_stop(stream->startpos, stream->timeline, false))
571 return true;
572
573 /* Initiate the replication stream at specified location */
574 query = createPQExpBuffer();
575 appendPQExpBufferStr(query, "START_REPLICATION");
576 if (stream->replication_slot != NULL)
577 {
578 appendPQExpBufferStr(query, " SLOT ");
580 }
581 appendPQExpBuffer(query, " %X/%08X TIMELINE %u",
582 LSN_FORMAT_ARGS(stream->startpos),
583 stream->timeline);
584 res = PQexec(conn, query->data);
585 destroyPQExpBuffer(query);
587 {
588 pg_log_error("could not send replication command \"%s\": %s",
589 "START_REPLICATION", PQresultErrorMessage(res));
590 PQclear(res);
591 return false;
592 }
593 PQclear(res);
594
595 /* Stream the WAL */
596 res = HandleCopyStream(conn, stream, &stoppos);
597 if (res == NULL)
598 goto error;
599
600 /*
601 * Streaming finished.
602 *
603 * There are two possible reasons for that: a controlled shutdown, or
604 * we reached the end of the current timeline. In case of
605 * end-of-timeline, the server sends a result set after Copy has
606 * finished, containing information about the next timeline. Read
607 * that, and restart streaming from the next timeline. In case of
608 * controlled shutdown, stop here.
609 */
611 {
612 /*
613 * End-of-timeline. Read the next timeline's ID and starting
614 * position. Usually, the starting position will match the end of
615 * the previous timeline, but there are corner cases like if the
616 * server had sent us half of a WAL record, when it was promoted.
617 * The new timeline will begin at the end of the last complete
618 * record in that case, overlapping the partial WAL record on the
619 * old timeline.
620 */
622 bool parsed;
623
625 PQclear(res);
626 if (!parsed)
627 goto error;
628
629 /* Sanity check the values the server gave us */
630 if (newtimeline <= stream->timeline)
631 {
632 pg_log_error("server reported unexpected next timeline %u, following timeline %u",
633 newtimeline, stream->timeline);
634 goto error;
635 }
636 if (stream->startpos > stoppos)
637 {
638 pg_log_error("server stopped streaming timeline %u at %X/%08X, but reported next timeline %u to begin at %X/%08X",
641 goto error;
642 }
643
644 /* Read the final result, which should be CommandComplete. */
645 res = PQgetResult(conn);
647 {
648 pg_log_error("unexpected termination of replication stream: %s",
650 PQclear(res);
651 goto error;
652 }
653 PQclear(res);
654
655 /*
656 * Loop back to start streaming from the new timeline. Always
657 * start streaming at the beginning of a segment.
658 */
659 stream->timeline = newtimeline;
660 stream->startpos = stream->startpos -
662 continue;
663 }
664 else if (PQresultStatus(res) == PGRES_COMMAND_OK)
665 {
666 PQclear(res);
667
668 /*
669 * End of replication (ie. controlled shut down of the server).
670 *
671 * Check if the callback thinks it's OK to stop here. If not,
672 * complain.
673 */
674 if (stream->stream_stop(stoppos, stream->timeline, false))
675 return true;
676 else
677 {
678 pg_log_error("replication stream was terminated before stop point");
679 goto error;
680 }
681 }
682 else
683 {
684 /* Server returned an error. */
685 pg_log_error("unexpected termination of replication stream: %s",
687 PQclear(res);
688 goto error;
689 }
690 }
691
692error:
693 if (walfile != NULL && stream->walmethod->ops->close(walfile, CLOSE_NO_RENAME) != 0)
694 pg_log_error("could not close file \"%s\": %s",
696 walfile = NULL;
697 return false;
698}
uint32_t uint32
Definition c.h:683
PGresult * PQexec(PGconn *conn, const char *query)
Definition fe-exec.c:2279
void pg_free(void *ptr)
#define PQresultErrorMessage
#define PQgetvalue
#define PQgetResult
#define PQclear
#define PQnfields
#define PQresultStatus
#define PQntuples
@ PGRES_COPY_BOTH
Definition libpq-fe.h:143
@ PGRES_COMMAND_OK
Definition libpq-fe.h:131
@ PGRES_TUPLES_OK
Definition libpq-fe.h:134
#define pg_log_warning(...)
Definition pgfnames.c:24
PQExpBuffer createPQExpBuffer(void)
Definition pqexpbuffer.c:72
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
static PGresult * HandleCopyStream(PGconn *conn, StreamCtl *stream, XLogRecPtr *stoppos)
Definition receivelog.c:751
static bool reportFlushPosition
Definition receivelog.c:30
static bool existsTimeLineHistoryFile(StreamCtl *stream)
Definition receivelog.c:258
static bool writeTimeLineHistoryFile(StreamCtl *stream, char *filename, char *content)
Definition receivelog.c:275
static Walfile * walfile
Definition receivelog.c:29
bool CheckServerVersionForStreaming(PGconn *conn)
Definition receivelog.c:375
static XLogRecPtr lastFlushPosition
Definition receivelog.c:31
static bool ReadEndOfStreamingResult(PGresult *res, XLogRecPtr *startpos, uint32 *timeline)
Definition receivelog.c:705
static void error(void)
int WalSegSz
Definition streamutil.c:32
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
Definition streamutil.c:409
#define AppendQuotedIdentifier(b, s)
Definition streamutil.h:47
char * sysidentifier
Definition receivelog.h:33
TimeLineID timeline
Definition receivelog.h:32
stream_stop_callback stream_stop
Definition receivelog.h:41
char * replication_slot
Definition receivelog.h:48
XLogRecPtr startpos
Definition receivelog.h:31
WalWriteMethod * walmethod
Definition receivelog.h:46
bool synchronous
Definition receivelog.h:36
int(* close)(Walfile *f, WalCloseMethod method)
Definition walmethods.h:55
const WalWriteMethodOps * ops
Definition walmethods.h:105
char * pathname
Definition walmethods.h:21
const char * GetLastWalMethodError(WalWriteMethod *wwmethod)
@ CLOSE_NO_RENAME
Definition walmethods.h:35
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
uint64 XLogRecPtr
Definition xlogdefs.h:21
uint32 TimeLineID
Definition xlogdefs.h:63

References appendPQExpBuffer(), appendPQExpBufferStr(), AppendQuotedIdentifier, CheckServerVersionForStreaming(), WalWriteMethodOps::close, CLOSE_NO_RENAME, conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), error(), existsTimeLineHistoryFile(), fb(), GetLastWalMethodError(), HandleCopyStream(), lastFlushPosition, LSN_FORMAT_ARGS, WalWriteMethod::ops, Walfile::pathname, pg_free(), pg_log_error, pg_log_warning, PGRES_COMMAND_OK, PGRES_COPY_BOTH, PGRES_TUPLES_OK, PQclear, PQexec(), PQgetResult, PQgetvalue, PQnfields, PQntuples, PQresultErrorMessage, PQresultStatus, ReadEndOfStreamingResult(), StreamCtl::replication_slot, reportFlushPosition, RunIdentifySystem(), StreamCtl::startpos, StreamCtl::stream_stop, StreamCtl::synchronous, StreamCtl::sysidentifier, StreamCtl::timeline, walfile, StreamCtl::walmethod, WalSegSz, writeTimeLineHistoryFile(), and XLogSegmentOffset.

Referenced by LogStreamerMain(), and StreamLog().