diff --git a/src/common/backend/utils/init/globals.cpp b/src/common/backend/utils/init/globals.cpp index e363e82a2..699992831 100644 --- a/src/common/backend/utils/init/globals.cpp +++ b/src/common/backend/utils/init/globals.cpp @@ -132,6 +132,8 @@ bool InplaceUpgradePrecommit = false; const uint32 DISASTER_READ_VERSION_NUM = 92592; +const uint32 PITR_INIT_VERSION_NUM = 92599; + #ifdef PGXC bool useLocalXid = false; #endif diff --git a/src/gausskernel/process/postmaster/pgarch.cpp b/src/gausskernel/process/postmaster/pgarch.cpp index fa0266749..99126ca6f 100755 --- a/src/gausskernel/process/postmaster/pgarch.cpp +++ b/src/gausskernel/process/postmaster/pgarch.cpp @@ -1200,6 +1200,26 @@ static WalSnd* pgarch_chooseWalsnd(XLogRecPtr targetLsn) return NULL; } +static XLogRecPtr GetLastTaskLsnFromServer(ArchiveSlotConfig* obs_archive_slot) +{ + ArchiveXlogMessage obs_archive_info; + XLogRecPtr pitr_task_last_lsn; + + if (archive_replication_get_last_xlog(&obs_archive_info, &obs_archive_slot->archive_config) == 0) { + pitr_task_last_lsn = obs_archive_info.targetLsn; + ereport(LOG, + (errmsg("initLastTaskLsn update lsn to %X/%X from server", (uint32)(pitr_task_last_lsn >> 32), + (uint32)(pitr_task_last_lsn)))); + } else { + XLogRecPtr targetLsn = GetFlushRecPtr(); + pitr_task_last_lsn = targetLsn - (targetLsn % XLogSegSize); + ereport(LOG, + (errmsg("initLastTaskLsn update lsn to %X/%X from local", (uint32)(pitr_task_last_lsn >> 32), + (uint32)(pitr_task_last_lsn)))); + } + return pitr_task_last_lsn; +} + static void InitArchiverLastTaskLsn(ArchiveSlotConfig* obs_archive_slot) { struct timeval tv; @@ -1216,8 +1236,19 @@ static void InitArchiverLastTaskLsn(ArchiveSlotConfig* obs_archive_slot) ReplicationSlot *slot = &t_thrd.slot_cxt.ReplicationSlotCtl->replication_slots[*slot_idx]; SpinLockAcquire(&slot->mutex); if (slot->in_use == true && slot->archive_config != NULL) { - t_thrd.arch.pitr_task_last_lsn = slot->data.restart_lsn; - SpinLockRelease(&slot->mutex); + /* + * In old version(<92599), the last task lsn is initialized from archive server or current flush + * position, but in new version is initialized from local slot. + * During the upgrade, the local restart lsn may be 0, so initialize it with old version way. + */ + if (slot->data.restart_lsn == InvalidXLogRecPtr && + t_thrd.proc->workingVersionNum < PITR_INIT_VERSION_NUM) { + SpinLockRelease(&slot->mutex); + t_thrd.arch.pitr_task_last_lsn = GetLastTaskLsnFromServer(obs_archive_slot); + } else { + t_thrd.arch.pitr_task_last_lsn = slot->data.restart_lsn; + SpinLockRelease(&slot->mutex); + } } else { SpinLockRelease(&slot->mutex); ereport(ERROR, (errcode_for_file_access(), errmsg("slot idx not valid, obs slot %X/%X not advance ", diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h index 02e605df4..f02eca6b4 100644 --- a/src/include/miscadmin.h +++ b/src/include/miscadmin.h @@ -91,6 +91,7 @@ extern const uint32 SCAN_BATCH_MODE_VERSION_NUM; extern const uint32 PUBLICATION_VERSION_NUM; extern const uint32 ANALYZER_HOOK_VERSION_NUM; extern const uint32 SUPPORT_HASH_XLOG_VERSION_NUM; +extern const uint32 PITR_INIT_VERSION_NUM; extern void register_backend_version(uint32 backend_version); extern bool contain_backend_version(uint32 version_number);