1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-06-18 23:57:33 +02:00

Add pgControlInfo() to read pg_control and determine the PostgreSQL version.

This commit is contained in:
David Steele
2018-04-29 11:20:51 -04:00
parent 8c6e2bdbc7
commit be02c67503
7 changed files with 219 additions and 0 deletions

View File

@ -86,6 +86,10 @@
<p>Improve <code>Buffer</code> object. Add <code>bufNewC()</code>, <code>bufEq()</code> and <code>bufCat()</code>. Only reallocate buffer when the size has changed.</p>
</release-item>
<release-item>
<p>Add <code>pgControlInfo()</code> to read <file>pg_control</file> and determine the <postgres/> version.</p>
</release-item>
<release-item>
<p>Add <code>THROWP_</code>* macro variants for error handling. These macros allow an <code>ErrorType</code> pointer to be passed and are required for functions that may return different errors based on a parameter.</p>
</release-item>

77
src/postgres/info.c Normal file
View File

@ -0,0 +1,77 @@
/***********************************************************************************************************************************
PostgreSQL Info
***********************************************************************************************************************************/
#include "common/memContext.h"
#include "postgres/info.h"
#include "postgres/type.h"
#include "postgres/version.h"
#include "storage/helper.h"
/***********************************************************************************************************************************
Map control/catalog version to PostgreSQL version
***********************************************************************************************************************************/
static uint
pgVersionMap(uint32_t controlVersion, uint32_t catalogVersion)
{
uint result = 0;
if (controlVersion == 1002 && catalogVersion == 201707211)
result = PG_VERSION_10;
else if (controlVersion == 960 && catalogVersion == 201608131)
result = PG_VERSION_96;
else if (controlVersion == 942 && catalogVersion == 201510051)
result = PG_VERSION_95;
else if (controlVersion == 942 && catalogVersion == 201409291)
result = PG_VERSION_94;
else if (controlVersion == 937 && catalogVersion == 201306121)
result = PG_VERSION_93;
else if (controlVersion == 922 && catalogVersion == 201204301)
result = PG_VERSION_92;
else if (controlVersion == 903 && catalogVersion == 201105231)
result = PG_VERSION_91;
else if (controlVersion == 903 && catalogVersion == 201008051)
result = PG_VERSION_90;
else if (controlVersion == 843 && catalogVersion == 200904091)
result = PG_VERSION_84;
else if (controlVersion == 833 && catalogVersion == 200711281)
result = PG_VERSION_83;
else
{
THROW(
VersionNotSupportedError,
"unexpected control version = %u and catalog version = %u\n"
"HINT: is this version of PostgreSQL supported?",
controlVersion, catalogVersion);
}
return result;
}
/***********************************************************************************************************************************
Get info from pg_control
***********************************************************************************************************************************/
PgControlInfo
pgControlInfo(const String *pgPath)
{
PgControlInfo result = {0};
MEM_CONTEXT_TEMP_BEGIN()
{
// Open control file for read
StorageFileRead *controlRead = storageNewReadNP(
storageLocal(), strNewFmt("%s/" PG_PATH_GLOBAL "/" PG_FILE_PGCONTROL, strPtr(pgPath)));
storageFileReadOpen(controlRead);
// Read contents
PgControlFile *control = (PgControlFile *)bufPtr(storageFileRead(controlRead));
// Copy to result structure and get PostgreSQL version
result.systemId = control->systemId;
result.controlVersion = control->controlVersion;
result.catalogVersion = control->catalogVersion;
result.version = pgVersionMap(result.controlVersion, result.catalogVersion);
}
MEM_CONTEXT_TEMP_END();
return result;
}

28
src/postgres/info.h Normal file
View File

@ -0,0 +1,28 @@
/***********************************************************************************************************************************
PostgreSQL Info
***********************************************************************************************************************************/
#ifndef POSTGRES_INFO_H
#define POSTGRES_INFO_H
#include <stdint.h>
#include <sys/types.h>
/***********************************************************************************************************************************
PostgreSQL Control File Info
***********************************************************************************************************************************/
typedef struct PgControlInfo
{
uint64_t systemId;
uint32_t controlVersion;
uint32_t catalogVersion;
uint version;
} PgControlInfo;
#include "common/type/string.h"
/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
PgControlInfo pgControlInfo(const String *pgPath);
#endif

24
src/postgres/type.h Normal file
View File

@ -0,0 +1,24 @@
/***********************************************************************************************************************************
PostreSQL Types
***********************************************************************************************************************************/
#ifndef POSTGRES_TYPE_H
#define POSTGRES_TYPE_H
/***********************************************************************************************************************************
Defines for various Postgres paths and files
***********************************************************************************************************************************/
#define PG_FILE_PGCONTROL "pg_control"
#define PG_PATH_GLOBAL "global"
/***********************************************************************************************************************************
pg_control file data
***********************************************************************************************************************************/
typedef struct PgControlFile
{
uint64_t systemId;
uint32_t controlVersion;
uint32_t catalogVersion;
} PgControlFile;
#endif

22
src/postgres/version.h Normal file
View File

@ -0,0 +1,22 @@
/***********************************************************************************************************************************
PostreSQL Version Constants
***********************************************************************************************************************************/
#ifndef POSTGRES_VERSION_H
#define POSTGRES_VERSION_H
/***********************************************************************************************************************************
PostgreSQL version constants
***********************************************************************************************************************************/
#define PG_VERSION_83 80300
#define PG_VERSION_84 80400
#define PG_VERSION_90 90000
#define PG_VERSION_91 90100
#define PG_VERSION_92 90200
#define PG_VERSION_93 90300
#define PG_VERSION_94 90400
#define PG_VERSION_95 90500
#define PG_VERSION_96 90600
#define PG_VERSION_10 100000
#define PG_VERSION_11 110000
#endif

View File

@ -274,6 +274,13 @@ unit:
- name: postgres
test:
# ----------------------------------------------------------------------------------------------------------------------------
- name: info
total: 2
coverage:
postgres/info: full
# ----------------------------------------------------------------------------------------------------------------------------
- name: page-checksum
total: 3

View File

@ -0,0 +1,57 @@
/***********************************************************************************************************************************
Test PostgreSQL Info
***********************************************************************************************************************************/
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
void
testRun()
{
Storage *storageTest = storageNewP(strNew(testPath()), .write = true);
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("pgVersionMap()"))
{
TEST_RESULT_INT(pgVersionMap( 833, 200711281), PG_VERSION_83, " check version 8.3");
TEST_RESULT_INT(pgVersionMap( 843, 200904091), PG_VERSION_84, " check version 8.4");
TEST_RESULT_INT(pgVersionMap( 903, 201008051), PG_VERSION_90, " check version 9.0");
TEST_RESULT_INT(pgVersionMap( 903, 201105231), PG_VERSION_91, " check version 9.1");
TEST_RESULT_INT(pgVersionMap( 922, 201204301), PG_VERSION_92, " check version 9.2");
TEST_RESULT_INT(pgVersionMap( 937, 201306121), PG_VERSION_93, " check version 9.3");
TEST_RESULT_INT(pgVersionMap( 942, 201409291), PG_VERSION_94, " check version 9.4");
TEST_RESULT_INT(pgVersionMap( 942, 201510051), PG_VERSION_95, " check version 9.5");
TEST_RESULT_INT(pgVersionMap( 960, 201608131), PG_VERSION_96, " check version 9.6");
TEST_RESULT_INT(pgVersionMap(1002, 201707211), PG_VERSION_10, " check version 10");
// -------------------------------------------------------------------------------------------------------------------------
#define MAP_ERROR \
"unexpected control version = %d and catalog version = 0\n" \
"HINT: is this version of PostgreSQL supported?"
TEST_ERROR_FMT(pgVersionMap( 0, 0), VersionNotSupportedError, MAP_ERROR, 0);
TEST_ERROR_FMT(pgVersionMap( 833, 0), VersionNotSupportedError, MAP_ERROR, 833);
TEST_ERROR_FMT(pgVersionMap( 843, 0), VersionNotSupportedError, MAP_ERROR, 843);
TEST_ERROR_FMT(pgVersionMap( 903, 0), VersionNotSupportedError, MAP_ERROR, 903);
TEST_ERROR_FMT(pgVersionMap( 922, 0), VersionNotSupportedError, MAP_ERROR, 922);
TEST_ERROR_FMT(pgVersionMap( 937, 0), VersionNotSupportedError, MAP_ERROR, 937);
TEST_ERROR_FMT(pgVersionMap( 942, 0), VersionNotSupportedError, MAP_ERROR, 942);
TEST_ERROR_FMT(pgVersionMap( 960, 0), VersionNotSupportedError, MAP_ERROR, 960);
TEST_ERROR_FMT(pgVersionMap(1002, 0), VersionNotSupportedError, MAP_ERROR, 1002);
}
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("pgControlInfo()"))
{
String *controlFile = strNew(PG_PATH_GLOBAL "/" PG_FILE_PGCONTROL);
PgControlFile control = {.systemId = 0xFACEFACE, .controlVersion = 833, .catalogVersion = 200711281};
storagePutNP(storageNewWriteNP(storageTest, controlFile), bufNewC(sizeof(PgControlFile), &control));
PgControlInfo info = {0};
TEST_ASSIGN(info, pgControlInfo(strNew(testPath())), "get control info");
TEST_RESULT_INT(info.systemId, 0xFACEFACE, " check system id");
TEST_RESULT_INT(info.controlVersion, 833, " check control version");
TEST_RESULT_INT(info.catalogVersion, 200711281, " check catalog version");
TEST_RESULT_INT(info.version, PG_VERSION_83, " check version");
}
}