@@ -1528,14 +1528,18 @@ wait_wal_lsn(XLogRecPtr target_lsn, bool is_start_lsn, TimeLineID tli,
15281528 * If we failed to get target LSN in a reasonable time, try
15291529 * to get LSN of last valid record prior to the target LSN. But only
15301530 * in case of a backup from a replica.
1531-
1531+ * Note, that with NullOffset target_lsn we do not wait
1532+ * for 'timeout / 2' seconds before going for previous record,
1533+ * because such LSN cannot be delivered at all.
1534+ *
15321535 * There are two cases for this:
15331536 * 1. Replica returned readpoint LSN which just do not exists. We want to look
15341537 * for previous record in the same(!) WAL segment which endpoint points to this LSN.
15351538 * 2. Replica returened endpoint LSN with 0 offset. We want to look
15361539 * for previous record which endpoint points greater or equal LSN in previous WAL segment.
15371540 */
1538- if (!exclusive_backup && current .from_replica && try_count > timeout / 2 )
1541+ if (current .from_replica &&
1542+ (XRecOffIsNull (target_lsn ) || try_count > timeout / 2 ))
15391543 {
15401544 XLogRecPtr res ;
15411545
@@ -1641,7 +1645,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
16411645 * Only if backup is from master.
16421646 * For PG 9.5 create restore point only if pguser is superuser.
16431647 */
1644- if (backup != NULL && !current . from_replica &&
1648+ if (backup != NULL && !backup -> from_replica &&
16451649 !(nodeInfo -> server_version < 90600 &&
16461650 !nodeInfo -> is_superuser ))
16471651 {
@@ -1677,7 +1681,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
16771681 * In case of backup from replica >= 9.6 we do not trust minRecPoint
16781682 * and stop_backup LSN, so we use latest replayed LSN as STOP LSN.
16791683 */
1680- if (current . from_replica )
1684+ if (backup -> from_replica )
16811685 stop_backup_query = "SELECT"
16821686 " pg_catalog.txid_snapshot_xmax(pg_catalog.txid_current_snapshot()),"
16831687 " current_timestamp(0)::timestamptz,"
@@ -1799,8 +1803,8 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
17991803
18001804 if (!XRecOffIsValid (stop_backup_lsn_tmp ))
18011805 {
1802- /* Replica returned STOP LSN with null offset */
1803- if (XRecOffIsNull (stop_backup_lsn_tmp ))
1806+ /* It is ok for replica to return STOP LSN with null offset */
1807+ if (backup -> from_replica && XRecOffIsNull (stop_backup_lsn_tmp ))
18041808 {
18051809 char * xlog_path ,
18061810 stream_xlog_path [MAXPGPATH ];
@@ -1888,7 +1892,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
18881892 stop_backup_lsn = lsn_tmp ;
18891893 stop_lsn_exists = true;
18901894 }
1891- /* Replica returned something very illegal, error out */
1895+ /* PostgreSQL returned something very illegal as STOP_LSN , error out */
18921896 else
18931897 elog (ERROR , "Invalid stop_backup_lsn value %X/%X" ,
18941898 (uint32 ) (stop_backup_lsn_tmp >> 32 ), (uint32 ) (stop_backup_lsn_tmp ));
@@ -1898,7 +1902,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn,
18981902 if (!exclusive_backup )
18991903 {
19001904 Assert (PQnfields (res ) >= 4 );
1901- pgBackupGetPath (& current , path , lengthof (path ), DATABASE_DIR );
1905+ pgBackupGetPath (backup , path , lengthof (path ), DATABASE_DIR );
19021906
19031907 /* Write backup_label */
19041908 join_path_components (backup_label , path , PG_BACKUP_LABEL_FILE );
0 commit comments