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

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