1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-13 01:00:23 +02:00

Make notion of current PostgreSQL info ID in C align with Perl.

The C code was assuming that the current PostgreSQL version in archive.info/backup.info was the most recent item in the history, but this is not always the case with some stanza-upgrade scenarios.  If a cluster is restored from before the upgrade and stanza-upgrade is run again, it will revert db-id to the original history item.

Instead, load db-id from the db section explicitly as the Perl code does.

This did not affect archive-get since it does a reverse scan through the history versions and does not rely on the current version.
This commit is contained in:
David Steele
2019-03-16 15:27:38 +04:00
parent b2b2cf0511
commit 66c2f4cd2e
5 changed files with 53 additions and 8 deletions

View File

@ -41,6 +41,10 @@
<p>Add <id>CIFS</id> storage driver.</p> <p>Add <id>CIFS</id> storage driver.</p>
</release-item> </release-item>
<release-item>
<p>Make notion of current <postgres/> info ID in C align with Perl.</p>
</release-item>
<release-item> <release-item>
<p>Add <code>httpHeaderDup()</code>.</p> <p>Add <code>httpHeaderDup()</code>.</p>
</release-item> </release-item>

View File

@ -69,7 +69,7 @@ infoArchiveNew(const Storage *storage, const String *fileName, bool ignoreMissin
TRY_END(); TRY_END();
// Store the archiveId for the current PG db-version db-id // Store the archiveId for the current PG db-version db-id
this->archiveId = infoPgArchiveId(this->infoPg, 0); this->archiveId = infoPgArchiveId(this->infoPg, infoPgDataCurrentId(this->infoPg));
} }
MEM_CONTEXT_NEW_END(); MEM_CONTEXT_NEW_END();
@ -94,7 +94,7 @@ infoArchiveCheckPg(const InfoArchive *this, unsigned int pgVersion, uint64_t pgS
String *errorMsg = NULL; String *errorMsg = NULL;
InfoPgData archivePg = infoPgDataCurrent(this->infoPg); InfoPgData archivePg = infoPgData(this->infoPg, infoPgDataCurrentId(this->infoPg));
if (archivePg.version != pgVersion) if (archivePg.version != pgVersion)
{ {

View File

@ -22,6 +22,7 @@ PostgreSQL Info Handler
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Internal constants Internal constants
***********************************************************************************************************************************/ ***********************************************************************************************************************************/
STRING_STATIC(INFO_SECTION_DB_STR, "db");
STRING_STATIC(INFO_SECTION_DB_HISTORY_STR, "db:history"); STRING_STATIC(INFO_SECTION_DB_HISTORY_STR, "db:history");
STRING_EXTERN(INFO_KEY_DB_ID_STR, INFO_KEY_DB_ID); STRING_EXTERN(INFO_KEY_DB_ID_STR, INFO_KEY_DB_ID);
@ -37,6 +38,7 @@ struct InfoPg
{ {
MemContext *memContext; // Context that contains the infoPg MemContext *memContext; // Context that contains the infoPg
List *history; // A list of InfoPgData List *history; // A list of InfoPgData
unsigned int historyCurrent; // Index of the current history item
Info *info; // Info contents Info *info; // Info contents
}; };
@ -76,12 +78,16 @@ infoPgNew(const Storage *storage, const String *fileName, InfoPgType type, Ciphe
MEM_CONTEXT_TEMP_BEGIN() MEM_CONTEXT_TEMP_BEGIN()
{ {
const Ini *infoPgIni = infoIni(this->info); const Ini *infoPgIni = infoIni(this->info);
const String *pgSection = INFO_SECTION_DB_STR;
const String *pgHistorySection = INFO_SECTION_DB_HISTORY_STR; const String *pgHistorySection = INFO_SECTION_DB_HISTORY_STR;
const StringList *pgHistoryKey = iniSectionKeyList(infoPgIni, pgHistorySection); const StringList *pgHistoryKey = iniSectionKeyList(infoPgIni, pgHistorySection);
const Variant *idKey = varNewStr(INFO_KEY_DB_ID_STR); const Variant *idKey = varNewStr(INFO_KEY_DB_ID_STR);
const Variant *systemIdKey = varNewStr(INFO_KEY_DB_SYSTEM_ID_STR); const Variant *systemIdKey = varNewStr(INFO_KEY_DB_SYSTEM_ID_STR);
const Variant *versionKey = varNewStr(INFO_KEY_DB_VERSION_STR); const Variant *versionKey = varNewStr(INFO_KEY_DB_VERSION_STR);
// Get the current history id
unsigned int pgId = (unsigned int)varUInt64Force(iniGet(infoPgIni, pgSection, varStr(idKey)));
// History must include at least one item or the file is corrupt // History must include at least one item or the file is corrupt
ASSERT(strLstSize(pgHistoryKey) > 0); ASSERT(strLstSize(pgHistoryKey) > 0);
@ -104,6 +110,10 @@ infoPgNew(const Storage *storage, const String *fileName, InfoPgType type, Ciphe
.systemId = varUInt64Force(kvGet(pgDataKv, type == infoPgArchive ? idKey : systemIdKey)), .systemId = varUInt64Force(kvGet(pgDataKv, type == infoPgArchive ? idKey : systemIdKey)),
}; };
// Set index if this is the current history item
if (infoPgData.id == pgId)
this->historyCurrent = lstSize(this->history);
// Get values that are only in backup and manifest files. These are really vestigial since stanza-create verifies // Get values that are only in backup and manifest files. These are really vestigial since stanza-create verifies
// the control and catalog versions so there is no good reason to store them. However, for backward compatability // the control and catalog versions so there is no good reason to store them. However, for backward compatability
// we must write them at least, even if we give up reading them. // we must write them at least, even if we give up reading them.
@ -210,7 +220,22 @@ infoPgDataCurrent(const InfoPg *this)
ASSERT(this != NULL); ASSERT(this != NULL);
FUNCTION_LOG_RETURN(INFO_PG_DATA, infoPgData(this, 0)); FUNCTION_LOG_RETURN(INFO_PG_DATA, infoPgData(this, infoPgDataCurrentId(this)));
}
/***********************************************************************************************************************************
Return the current history index
***********************************************************************************************************************************/
unsigned int
infoPgDataCurrentId(const InfoPg *this)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(INFO_PG, this);
FUNCTION_LOG_END();
ASSERT(this != NULL);
FUNCTION_LOG_RETURN(UINT, this->historyCurrent);
} }
/*********************************************************************************************************************************** /***********************************************************************************************************************************

View File

@ -60,6 +60,7 @@ String *infoPgArchiveId(const InfoPg *this, unsigned int pgDataIdx);
const String *infoPgCipherPass(const InfoPg *this); const String *infoPgCipherPass(const InfoPg *this);
InfoPgData infoPgData(const InfoPg *this, unsigned int pgDataIdx); InfoPgData infoPgData(const InfoPg *this, unsigned int pgDataIdx);
InfoPgData infoPgDataCurrent(const InfoPg *this); InfoPgData infoPgDataCurrent(const InfoPg *this);
unsigned int infoPgDataCurrentId(const InfoPg *this);
unsigned int infoPgDataTotal(const InfoPg *this); unsigned int infoPgDataTotal(const InfoPg *this);
Ini *infoPgIni(const InfoPg *this); Ini *infoPgIni(const InfoPg *this);

View File

@ -55,10 +55,13 @@ testRun(void)
storageNewWriteNP(storageTest, strNew("repo/archive/test1/archive.info")), storageNewWriteNP(storageTest, strNew("repo/archive/test1/archive.info")),
bufNewZ( bufNewZ(
"[backrest]\n" "[backrest]\n"
"backrest-checksum=\"0a415a03fa3faccb4ac171759895478469e9e19e\"\n" "backrest-checksum=\"806471e1481804dc3ddf8dc6f1da7c34939420a8\"\n"
"backrest-format=5\n" "backrest-format=5\n"
"backrest-version=\"2.06\"\n" "backrest-version=\"2.06\"\n"
"\n" "\n"
"[db]\n"
"db-id=1\n"
"\n"
"[db:history]\n" "[db:history]\n"
"1={\"db-id\":5555555555555555555,\"db-version\":\"9.4\"}\n")); "1={\"db-id\":5555555555555555555,\"db-version\":\"9.4\"}\n"));
@ -72,10 +75,13 @@ testRun(void)
storageNewWriteNP(storageTest, strNew("repo/archive/test1/archive.info")), storageNewWriteNP(storageTest, strNew("repo/archive/test1/archive.info")),
bufNewZ( bufNewZ(
"[backrest]\n" "[backrest]\n"
"backrest-checksum=\"f7617b5c4c9f212f40b9bc3d8ec7f97edbbf96af\"\n" "backrest-checksum=\"4120e2a5c3918a480af951fb99bee7dc091f080a\"\n"
"backrest-format=5\n" "backrest-format=5\n"
"backrest-version=\"2.06\"\n" "backrest-version=\"2.06\"\n"
"\n" "\n"
"[db]\n"
"db-id=3\n"
"\n"
"[db:history]\n" "[db:history]\n"
"1={\"db-id\":5555555555555555555,\"db-version\":\"9.4\"}\n" "1={\"db-id\":5555555555555555555,\"db-version\":\"9.4\"}\n"
"2={\"db-id\":18072658121562454734,\"db-version\":\"10\"}\n" "2={\"db-id\":18072658121562454734,\"db-version\":\"10\"}\n"
@ -147,10 +153,13 @@ testRun(void)
storageNewWriteNP(storageTest, strNew("repo/archive/test1/archive.info")), storageNewWriteNP(storageTest, strNew("repo/archive/test1/archive.info")),
bufNewZ( bufNewZ(
"[backrest]\n" "[backrest]\n"
"backrest-checksum=\"8a041a4128eaa2c08a23dd1f04934627795946ff\"\n" "backrest-checksum=\"bf9d69c6131e906898511432b4445335f377b12b\"\n"
"backrest-format=5\n" "backrest-format=5\n"
"backrest-version=\"2.06\"\n" "backrest-version=\"2.06\"\n"
"\n" "\n"
"[db]\n"
"db-id=1\n"
"\n"
"[db:history]\n" "[db:history]\n"
"1={\"db-id\":18072658121562454734,\"db-version\":\"10\"}")); "1={\"db-id\":18072658121562454734,\"db-version\":\"10\"}"));
@ -201,13 +210,16 @@ testRun(void)
infoWrite, infoWrite,
bufNewZ( bufNewZ(
"[backrest]\n" "[backrest]\n"
"backrest-checksum=\"60bfcb0a5a2c91d203c11d7f1924e99dcdfa0b80\"\n" "backrest-checksum=\"5e4bf8cafff30b488c01574411e858c7866df4a2\"\n"
"backrest-format=5\n" "backrest-format=5\n"
"backrest-version=\"2.06\"\n" "backrest-version=\"2.06\"\n"
"\n" "\n"
"[cipher]\n" "[cipher]\n"
"cipher-pass=\"worstpassphraseever\"\n" "cipher-pass=\"worstpassphraseever\"\n"
"\n" "\n"
"[db]\n"
"db-id=1\n"
"\n"
"[db:history]\n" "[db:history]\n"
"1={\"db-id\":18072658121562454734,\"db-version\":\"10\"}")); "1={\"db-id\":18072658121562454734,\"db-version\":\"10\"}"));
@ -362,10 +374,13 @@ testRun(void)
storageNewWriteNP(storageTest, strNew("repo/archive/test2/archive.info")), storageNewWriteNP(storageTest, strNew("repo/archive/test2/archive.info")),
bufNewZ( bufNewZ(
"[backrest]\n" "[backrest]\n"
"backrest-checksum=\"d962d8d7311d0ae5dc0b05892c15cfa2009d051e\"\n" "backrest-checksum=\"398aa38312ce69e3625b2ecfb4dfc6387e5ce7d3\"\n"
"backrest-format=5\n" "backrest-format=5\n"
"backrest-version=\"2.11\"\n" "backrest-version=\"2.11\"\n"
"\n" "\n"
"[db]\n"
"db-id=1\n"
"\n"
"[db:history]\n" "[db:history]\n"
"1={\"db-id\":18072658121562454734,\"db-version\":\"10\"}\n")); "1={\"db-id\":18072658121562454734,\"db-version\":\"10\"}\n"));