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

Add Db object to encapsulate PostgreSQL queries and commands.

Migrate functionality from the Perl Db module to C. For now this is just enough to implement the WAL switch check.

Add the dbGet() helper function to get Db objects easily.

Create macros in harnessPq to make writing pq scripts easier by grouping commonly used functions together.

Reviewed by Cynthia Shang.
This commit is contained in:
David Steele
2019-08-01 15:38:27 -04:00
parent f9e1f3a798
commit e4901d50d5
20 changed files with 1180 additions and 12 deletions

View File

@ -9,10 +9,7 @@ Postgres Client
#include "common/log.h"
#include "common/memContext.h"
#include "common/object.h"
#include "common/time.h"
#include "common/type/list.h"
#include "common/type/string.h"
#include "common/type/variantList.h"
#include "common/wait.h"
#include "postgres/client.h"
@ -350,15 +347,36 @@ pgClientClose(PgClient *this)
FUNCTION_LOG_END();
ASSERT(this != NULL);
CHECK(this->connection != NULL);
memContextCallbackClear(this->memContext);
PQfinish(this->connection);
this->connection = NULL;
if (this->connection != NULL)
{
memContextCallbackClear(this->memContext);
PQfinish(this->connection);
this->connection = NULL;
}
FUNCTION_LOG_RETURN_VOID();
}
/***********************************************************************************************************************************
Move the pg client object to a new context
***********************************************************************************************************************************/
PgClient *
pgClientMove(PgClient *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(PG_CLIENT, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Render as string for logging
***********************************************************************************************************************************/

View File

@ -8,6 +8,10 @@ casts to queries to output one of these types.
#ifndef POSTGRES_QUERY_H
#define POSTGRES_QUERY_H
#include "common/type/string.h"
#include "common/type/variantList.h"
#include "common/time.h"
/***********************************************************************************************************************************
Object type
***********************************************************************************************************************************/
@ -29,6 +33,8 @@ PgClient *pgClientOpen(PgClient *this);
VariantList *pgClientQuery(PgClient *this, const String *query);
void pgClientClose(PgClient *this);
PgClient *pgClientMove(PgClient *this, MemContext *parentNew);
/***********************************************************************************************************************************
Destructor
***********************************************************************************************************************************/

View File

@ -14,6 +14,12 @@ PostgreSQL Interface
#include "postgres/version.h"
#include "storage/helper.h"
/***********************************************************************************************************************************
Defines for various Postgres paths and files
***********************************************************************************************************************************/
STRING_EXTERN(PG_NAME_WAL_STR, PG_NAME_WAL);
STRING_EXTERN(PG_NAME_XLOG_STR, PG_NAME_XLOG);
/***********************************************************************************************************************************
Define default wal segment size
@ -35,6 +41,11 @@ something far larger needed but <= the minimum read size on just about any syste
***********************************************************************************************************************************/
#define PG_WAL_HEADER_SIZE ((unsigned int)(512))
/***********************************************************************************************************************************
Name of default PostgreSQL database used for running all queries and commands
***********************************************************************************************************************************/
STRING_EXTERN(PG_DB_POSTGRES_STR, PG_DB_POSTGRES);
/***********************************************************************************************************************************
PostgreSQL interface definitions
@ -407,6 +418,19 @@ pgWalFromFile(const String *walFile)
FUNCTION_LOG_RETURN(PG_WAL, result);
}
/***********************************************************************************************************************************
Get WAL name (wal/xlog) for a PostgreSQL version
***********************************************************************************************************************************/
const String *
pgWalName(unsigned int pgVersion)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
FUNCTION_TEST_END();
FUNCTION_TEST_RETURN(pgVersion >= PG_VERSION_WAL_RENAME ? PG_NAME_WAL_STR : PG_NAME_XLOG_STR);
}
/***********************************************************************************************************************************
Create pg_control for testing
***********************************************************************************************************************************/

View File

@ -17,6 +17,17 @@ Defines for various Postgres paths and files
#define PG_PATH_ARCHIVE_STATUS "archive_status"
#define PG_PATH_GLOBAL "global"
#define PG_NAME_WAL "wal"
STRING_DECLARE(PG_NAME_WAL_STR);
#define PG_NAME_XLOG "xlog"
STRING_DECLARE(PG_NAME_XLOG_STR);
/***********************************************************************************************************************************
Name of default PostgreSQL database used for running all queries and commands
***********************************************************************************************************************************/
#define PG_DB_POSTGRES "postgres"
STRING_DECLARE(PG_DB_POSTGRES_STR);
/***********************************************************************************************************************************
Define default page size
@ -69,6 +80,8 @@ String *pgVersionToStr(unsigned int version);
PgWal pgWalFromFile(const String *walFile);
PgWal pgWalFromBuffer(const Buffer *walBuffer);
const String *pgWalName(unsigned int pgVersion);
/***********************************************************************************************************************************
Test Functions
***********************************************************************************************************************************/

View File

@ -26,6 +26,21 @@ PostgreSQL version constants
#define PG_VERSION_MAX PG_VERSION_11
/***********************************************************************************************************************************
Version where various PostgreSQL capabilities were introduced
***********************************************************************************************************************************/
// application_name can be set to show the application name in pg_stat_activity
#define PG_VERSION_APPLICATION_NAME PG_VERSION_90
// pg_is_in_recovery() supported
#define PG_VERSION_HOT_STANDBY PG_VERSION_91
// pg_create_restore_point() supported
#define PG_VERSION_RESTORE_POINT PG_VERSION_91
// xlog was renamed to wal
#define PG_VERSION_WAL_RENAME PG_VERSION_10
/***********************************************************************************************************************************
PostgreSQL version string constants for use in error messages
***********************************************************************************************************************************/