2018-04-29 11:20:51 -04:00
|
|
|
/***********************************************************************************************************************************
|
2018-09-25 10:24:42 +01:00
|
|
|
PostgreSQL Interface
|
2018-04-29 11:20:51 -04:00
|
|
|
***********************************************************************************************************************************/
|
2018-09-25 10:24:42 +01:00
|
|
|
#ifndef POSTGRES_INTERFACE_H
|
|
|
|
#define POSTGRES_INTERFACE_H
|
2018-04-29 11:20:51 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2019-12-02 11:54:45 -05:00
|
|
|
#include "common/debug.h"
|
2018-09-25 10:24:42 +01:00
|
|
|
#include "common/type/string.h"
|
2019-08-21 11:29:30 -04:00
|
|
|
#include "storage/storage.h"
|
2018-09-25 10:24:42 +01:00
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Defines for various Postgres paths and files
|
|
|
|
***********************************************************************************************************************************/
|
2019-12-01 16:28:33 -05:00
|
|
|
#define PG_FILE_BACKUPLABEL "backup_label"
|
|
|
|
#define PG_FILE_BACKUPLABELOLD "backup_label.old"
|
2020-09-14 10:15:40 -04:00
|
|
|
#define PG_FILE_BACKUPMANIFEST "backup_manifest"
|
|
|
|
#define PG_FILE_BACKUPMANIFEST_TMP "backup_manifest.tmp"
|
2018-09-25 10:24:42 +01:00
|
|
|
#define PG_FILE_PGCONTROL "pg_control"
|
2019-12-01 16:28:33 -05:00
|
|
|
#define PG_FILE_PGFILENODEMAP "pg_filenode.map"
|
|
|
|
#define PG_FILE_PGINTERNALINIT "pg_internal.init"
|
2019-09-26 07:52:02 -04:00
|
|
|
#define PG_FILE_PGVERSION "PG_VERSION"
|
|
|
|
STRING_DECLARE(PG_FILE_PGVERSION_STR);
|
2019-10-01 13:20:43 -04:00
|
|
|
#define PG_FILE_POSTGRESQLAUTOCONF "postgresql.auto.conf"
|
|
|
|
STRING_DECLARE(PG_FILE_POSTGRESQLAUTOCONF_STR);
|
2019-12-01 16:28:33 -05:00
|
|
|
#define PG_FILE_POSTGRESQLAUTOCONFTMP "postgresql.auto.conf.tmp"
|
2021-10-13 19:36:59 -04:00
|
|
|
#define PG_FILE_POSTMTROPTS "postmas""ter.opts"
|
|
|
|
#define PG_FILE_POSTMTRPID "postmas""ter.pid"
|
|
|
|
STRING_DECLARE(PG_FILE_POSTMTRPID_STR);
|
2019-09-26 07:52:02 -04:00
|
|
|
#define PG_FILE_RECOVERYCONF "recovery.conf"
|
|
|
|
STRING_DECLARE(PG_FILE_RECOVERYCONF_STR);
|
2019-12-01 16:28:33 -05:00
|
|
|
#define PG_FILE_RECOVERYDONE "recovery.done"
|
|
|
|
STRING_DECLARE(PG_FILE_RECOVERYDONE_STR);
|
2019-10-01 13:20:43 -04:00
|
|
|
#define PG_FILE_RECOVERYSIGNAL "recovery.signal"
|
|
|
|
STRING_DECLARE(PG_FILE_RECOVERYSIGNAL_STR);
|
|
|
|
#define PG_FILE_STANDBYSIGNAL "standby.signal"
|
|
|
|
STRING_DECLARE(PG_FILE_STANDBYSIGNAL_STR);
|
2019-09-26 07:52:02 -04:00
|
|
|
#define PG_FILE_TABLESPACEMAP "tablespace_map"
|
2018-09-25 10:24:42 +01:00
|
|
|
|
2019-03-29 13:26:33 +00:00
|
|
|
#define PG_PATH_ARCHIVE_STATUS "archive_status"
|
2019-09-26 07:52:02 -04:00
|
|
|
#define PG_PATH_BASE "base"
|
2018-09-25 10:24:42 +01:00
|
|
|
#define PG_PATH_GLOBAL "global"
|
2019-09-26 07:52:02 -04:00
|
|
|
STRING_DECLARE(PG_PATH_GLOBAL_STR);
|
2019-12-01 16:28:33 -05:00
|
|
|
#define PG_PATH_PGMULTIXACT "pg_multixact"
|
|
|
|
#define PG_PATH_PGDYNSHMEM "pg_dynshmem"
|
|
|
|
#define PG_PATH_PGNOTIFY "pg_notify"
|
|
|
|
#define PG_PATH_PGREPLSLOT "pg_replslot"
|
|
|
|
#define PG_PATH_PGSERIAL "pg_serial"
|
|
|
|
#define PG_PATH_PGSNAPSHOTS "pg_snapshots"
|
|
|
|
#define PG_PATH_PGSTATTMP "pg_stat_tmp"
|
|
|
|
#define PG_PATH_PGSUBTRANS "pg_subtrans"
|
2019-12-07 17:42:42 -05:00
|
|
|
#define PG_PATH_PGTBLSPC "pg_tblspc"
|
2019-12-01 16:28:33 -05:00
|
|
|
|
|
|
|
#define PG_PREFIX_PGSQLTMP "pgsql_tmp"
|
2018-09-25 10:24:42 +01:00
|
|
|
|
2019-08-01 15:38:27 -04:00
|
|
|
#define PG_NAME_WAL "wal"
|
|
|
|
STRING_DECLARE(PG_NAME_WAL_STR);
|
|
|
|
#define PG_NAME_XLOG "xlog"
|
|
|
|
STRING_DECLARE(PG_NAME_XLOG_STR);
|
|
|
|
|
2019-06-17 07:52:03 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Define default page size
|
|
|
|
|
|
|
|
Page size can only be changed at compile time and is not known to be well-tested, so only the default page size is supported.
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
#define PG_PAGE_SIZE_DEFAULT ((unsigned int)(8 * 1024))
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Define default segment size and pages per segment
|
|
|
|
|
|
|
|
Segment size can only be changed at compile time and is not known to be well-tested, so only the default segment size is supported.
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
#define PG_SEGMENT_SIZE_DEFAULT ((unsigned int)(1 * 1024 * 1024 * 1024))
|
|
|
|
#define PG_SEGMENT_PAGE_DEFAULT (PG_SEGMENT_SIZE_DEFAULT / PG_PAGE_SIZE_DEFAULT)
|
|
|
|
|
2020-09-08 12:44:08 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
WAL header size. It doesn't seem worth tracking the exact size of the WAL header across versions of PostgreSQL so just set it to
|
|
|
|
something far larger needed but <= the minimum read size on just about any system.
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
#define PG_WAL_HEADER_SIZE ((unsigned int)(512))
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Define default wal segment size
|
|
|
|
|
|
|
|
Before PostgreSQL 11 WAL segment size could only be changed at compile time and is not known to be well-tested, so only the default
|
|
|
|
WAL segment size is supported for versions below 11.
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
#define PG_WAL_SEGMENT_SIZE_DEFAULT ((unsigned int)(16 * 1024 * 1024))
|
|
|
|
|
2018-04-29 11:20:51 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
PostgreSQL Control File Info
|
|
|
|
***********************************************************************************************************************************/
|
2018-09-25 10:24:42 +01:00
|
|
|
typedef struct PgControl
|
2018-04-29 11:20:51 -04:00
|
|
|
{
|
2018-09-25 10:24:42 +01:00
|
|
|
unsigned int version;
|
2018-04-29 11:20:51 -04:00
|
|
|
uint64_t systemId;
|
2020-09-18 16:55:26 -04:00
|
|
|
unsigned int catalogVersion;
|
2018-09-25 10:24:42 +01:00
|
|
|
|
2021-12-07 09:21:07 -05:00
|
|
|
uint64_t checkpoint; // Last checkpoint LSN
|
|
|
|
uint32_t timeline; // Current timeline
|
|
|
|
|
2018-09-25 10:24:42 +01:00
|
|
|
unsigned int pageSize;
|
|
|
|
unsigned int walSegmentSize;
|
|
|
|
|
|
|
|
bool pageChecksum;
|
|
|
|
} PgControl;
|
2018-04-29 11:20:51 -04:00
|
|
|
|
2019-03-19 19:44:06 +04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
PostgreSQL WAL Info
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
typedef struct PgWal
|
|
|
|
{
|
|
|
|
unsigned int version;
|
2020-07-09 17:32:36 -04:00
|
|
|
unsigned int size;
|
2019-03-19 19:44:06 +04:00
|
|
|
uint64_t systemId;
|
|
|
|
} PgWal;
|
|
|
|
|
2018-04-29 11:20:51 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Functions
|
|
|
|
***********************************************************************************************************************************/
|
2022-05-04 08:22:45 -04:00
|
|
|
// Is this a template database?
|
|
|
|
bool pgDbIsTemplate(const String *name);
|
|
|
|
|
|
|
|
// Is this a system database, i.e. template or postgres?
|
|
|
|
bool pgDbIsSystem(const String *name);
|
|
|
|
|
|
|
|
// Does this database have a system id, i.e. less than the minimum assignable user id?
|
|
|
|
bool pgDbIsSystemId(unsigned int id);
|
|
|
|
|
2020-04-03 18:01:28 -04:00
|
|
|
// Get info from pg_control
|
2019-10-03 11:14:22 -04:00
|
|
|
PgControl pgControlFromFile(const Storage *storage);
|
2020-04-03 18:01:28 -04:00
|
|
|
|
|
|
|
// Get the control version for a PostgreSQL version
|
2019-09-07 18:04:39 -04:00
|
|
|
uint32_t pgControlVersion(unsigned int pgVersion);
|
2020-04-03 18:01:28 -04:00
|
|
|
|
|
|
|
// Convert version string to version number and vice versa
|
2018-09-06 10:12:14 -07:00
|
|
|
unsigned int pgVersionFromStr(const String *version);
|
|
|
|
String *pgVersionToStr(unsigned int version);
|
2018-04-29 11:20:51 -04:00
|
|
|
|
2020-04-03 18:01:28 -04:00
|
|
|
// Get info from WAL header
|
2020-07-21 16:28:05 -04:00
|
|
|
PgWal pgWalFromFile(const String *walFile, const Storage *storage);
|
2019-03-19 19:44:06 +04:00
|
|
|
PgWal pgWalFromBuffer(const Buffer *walBuffer);
|
|
|
|
|
2022-12-20 12:20:47 +07:00
|
|
|
// Get the tablespace identifier used to distinguish versions in a tablespace directory, e.g. PG_15_202209061
|
2020-09-18 16:55:26 -04:00
|
|
|
String *pgTablespaceId(unsigned int pgVersion, unsigned int pgCatalogVersion);
|
2019-09-08 06:53:23 -04:00
|
|
|
|
2019-12-08 14:19:47 -05:00
|
|
|
// Convert a string to an lsn and vice versa
|
|
|
|
uint64_t pgLsnFromStr(const String *lsn);
|
|
|
|
String *pgLsnToStr(uint64_t lsn);
|
|
|
|
|
2020-07-08 12:25:39 -04:00
|
|
|
// Convert a timeline and lsn to a wal segment and vice versa
|
2019-12-08 14:19:47 -05:00
|
|
|
String *pgLsnToWalSegment(uint32_t timeline, uint64_t lsn, unsigned int walSegmentSize);
|
2020-07-08 12:25:39 -04:00
|
|
|
uint64_t pgLsnFromWalSegment(const String *walSegment, unsigned int walSegmentSize);
|
2019-12-08 14:19:47 -05:00
|
|
|
|
2021-12-07 09:21:07 -05:00
|
|
|
// Get timeline from WAL segment name
|
|
|
|
uint32_t pgTimelineFromWalSegment(const String *walSegment);
|
|
|
|
|
2019-12-12 16:43:34 -05:00
|
|
|
// Convert a timeline and lsn range to a list of wal segments
|
2022-12-20 12:20:47 +07:00
|
|
|
StringList *pgLsnRangeToWalSegmentList(uint32_t timeline, uint64_t lsnStart, uint64_t lsnStop, unsigned int walSegmentSize);
|
2019-12-12 16:43:34 -05:00
|
|
|
|
2019-12-01 15:49:34 -05:00
|
|
|
// Get name used for lsn in functions (this was changed in PostgreSQL 10 for consistency since lots of names were changing)
|
|
|
|
const String *pgLsnName(unsigned int pgVersion);
|
|
|
|
|
2020-03-05 14:23:01 -05:00
|
|
|
// Calculate the checksum for a page. Page cannot be const because the page header is temporarily modified during processing.
|
|
|
|
uint16_t pgPageChecksum(unsigned char *page, uint32_t blockNo);
|
|
|
|
|
2019-08-01 15:38:27 -04:00
|
|
|
const String *pgWalName(unsigned int pgVersion);
|
|
|
|
|
2019-12-12 16:11:09 -05:00
|
|
|
// Get wal path (this was changed in PostgreSQL 10 to avoid including "log" in the name)
|
|
|
|
const String *pgWalPath(unsigned int pgVersion);
|
|
|
|
|
2019-12-01 15:49:34 -05:00
|
|
|
// Get transaction commit log path (this was changed in PostgreSQL 10 to avoid including "log" in the name)
|
|
|
|
const String *pgXactPath(unsigned int pgVersion);
|
|
|
|
|
2018-09-25 10:24:42 +01:00
|
|
|
/***********************************************************************************************************************************
|
2018-05-18 11:57:32 -04:00
|
|
|
Macros for function logging
|
|
|
|
***********************************************************************************************************************************/
|
2018-09-25 10:24:42 +01:00
|
|
|
String *pgControlToLog(const PgControl *pgControl);
|
2019-03-19 19:44:06 +04:00
|
|
|
String *pgWalToLog(const PgWal *pgWal);
|
2018-09-25 10:24:42 +01:00
|
|
|
|
2019-01-21 17:41:59 +02:00
|
|
|
#define FUNCTION_LOG_PG_CONTROL_TYPE \
|
2018-09-25 10:24:42 +01:00
|
|
|
PgControl
|
2019-01-21 17:41:59 +02:00
|
|
|
#define FUNCTION_LOG_PG_CONTROL_FORMAT(value, buffer, bufferSize) \
|
|
|
|
FUNCTION_LOG_STRING_OBJECT_FORMAT(&value, pgControlToLog, buffer, bufferSize)
|
2018-05-18 11:57:32 -04:00
|
|
|
|
2019-03-19 19:44:06 +04:00
|
|
|
#define FUNCTION_LOG_PG_WAL_TYPE \
|
|
|
|
PgWal
|
|
|
|
#define FUNCTION_LOG_PG_WAL_FORMAT(value, buffer, bufferSize) \
|
|
|
|
FUNCTION_LOG_STRING_OBJECT_FORMAT(&value, pgWalToLog, buffer, bufferSize)
|
|
|
|
|
2018-04-29 11:20:51 -04:00
|
|
|
#endif
|