1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-03-05 15:05:48 +02:00

Update db/postgres modules to recent coding standards.

Add const as appropriate and avoid initializing variables if the variable will definitely be set later on or is immediately returned.
This commit is contained in:
David Steele 2024-04-21 13:01:40 +10:00
parent 19411f39d2
commit c6fcc81db6
5 changed files with 66 additions and 64 deletions

View File

@ -61,7 +61,7 @@ dbFreeResource(THIS_VOID)
/**********************************************************************************************************************************/
FN_EXTERN Db *
dbNew(PgClient *client, ProtocolClient *remoteClient, const Storage *const storage, const String *applicationName)
dbNew(PgClient *const client, ProtocolClient *const remoteClient, const Storage *const storage, const String *const applicationName)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(PG_CLIENT, client);
@ -98,7 +98,7 @@ dbNew(PgClient *client, ProtocolClient *remoteClient, const Storage *const stora
Execute a query
***********************************************************************************************************************************/
static Pack *
dbQuery(Db *this, const PgClientQueryResult resultType, const String *const query)
dbQuery(Db *const this, const PgClientQueryResult resultType, const String *const query)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(DB, this);
@ -110,14 +110,14 @@ dbQuery(Db *this, const PgClientQueryResult resultType, const String *const quer
ASSERT(resultType != 0);
ASSERT(query != NULL);
Pack *result = NULL;
Pack *result;
// Query remotely
if (this->remoteClient != NULL)
{
MEM_CONTEXT_TEMP_BEGIN()
{
ProtocolCommand *command = protocolCommandNew(PROTOCOL_COMMAND_DB_QUERY);
ProtocolCommand *const command = protocolCommandNew(PROTOCOL_COMMAND_DB_QUERY);
PackWrite *const param = protocolCommandParam(command);
pckWriteU32P(param, this->remoteIdx);
@ -145,7 +145,7 @@ dbQuery(Db *this, const PgClientQueryResult resultType, const String *const quer
Execute a command that expects no output
***********************************************************************************************************************************/
static void
dbExec(Db *this, const String *command)
dbExec(Db *const this, const String *const command)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(DB, this);
@ -227,7 +227,7 @@ dbIsInRecovery(Db *const this)
/**********************************************************************************************************************************/
FN_EXTERN void
dbOpen(Db *this)
dbOpen(Db *const this)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(DB, this);
@ -329,7 +329,7 @@ dbOpen(Db *this)
/**********************************************************************************************************************************/
// Helper to build start backup query
static String *
dbBackupStartQuery(unsigned int pgVersion, bool startFast)
dbBackupStartQuery(const unsigned int pgVersion, const bool startFast)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
@ -480,14 +480,14 @@ dbBackupStart(Db *const this, const bool startFast, const bool stopAuto, const b
/**********************************************************************************************************************************/
// Helper to build stop backup query
static String *
dbBackupStopQuery(unsigned int pgVersion)
dbBackupStopQuery(const unsigned int pgVersion)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
FUNCTION_TEST_END();
// Build query to return start lsn and WAL segment name
String *result = strCatFmt(
String *const result = strCatFmt(
strNew(),
"select lsn::text as lsn,\n"
" pg_catalog.pg_%sfile_name(lsn)::text as wal_segment_name",
@ -533,7 +533,7 @@ dbBackupStopQuery(unsigned int pgVersion)
}
FN_EXTERN DbBackupStopResult
dbBackupStop(Db *this)
dbBackupStop(Db *const this)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(DB, this);
@ -581,7 +581,7 @@ dbBackupStop(Db *this)
/**********************************************************************************************************************************/
FN_EXTERN Pack *
dbList(Db *this)
dbList(Db *const this)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(DB, this);
@ -626,15 +626,15 @@ dbReplayWait(Db *const this, const String *const targetLsn, const uint32_t targe
// Loop until lsn has been reached or timeout
Wait *wait = waitNew(timeout);
bool targetReached = false;
const char *lsnName = strZ(pgLsnName(dbPgVersion(this)));
const String *replayLsnFunction = strNewFmt(
const char *const lsnName = strZ(pgLsnName(dbPgVersion(this)));
const String *const replayLsnFunction = strNewFmt(
"pg_catalog.pg_last_%s_replay_%s()", strZ(pgWalName(dbPgVersion(this))), lsnName);
const String *replayLsn = NULL;
do
{
// Build the query
String *query = strCatFmt(
String *const query = strCatFmt(
strNew(),
"select replayLsn::text,\n"
" (replayLsn > '%s')::bool as targetReached",
@ -656,7 +656,7 @@ dbReplayWait(Db *const this, const String *const targetLsn, const uint32_t targe
strZ(replayLsnFunction));
// Execute the query and get replayLsn
PackRead *read = dbQueryRow(this, query);
PackRead *const read = dbQueryRow(this, query);
replayLsn = pckReadStrP(read);
// Error when replayLsn is null which indicates that this is not a standby. This should have been sorted out before we
@ -697,14 +697,14 @@ dbReplayWait(Db *const this, const String *const targetLsn, const uint32_t targe
// On PostgreSQL >= 9.6 the checkpoint location can be verified so loop until lsn has been reached or timeout
if (dbPgVersion(this) >= PG_VERSION_96)
{
wait = waitNew(timeout);
Wait *const wait = waitNew(timeout);
targetReached = false;
const String *checkpointLsn = NULL;
do
{
// Build the query
const String *query = strNewFmt(
const String *const query = strNewFmt(
"select (checkpoint_%s > '%s')::bool as targetReached,\n"
" checkpoint_%s::text as checkpointLsn\n"
" from pg_catalog.pg_control_checkpoint()",
@ -754,7 +754,7 @@ dbPing(Db *const this, const bool force)
MEM_CONTEXT_TEMP_BEGIN()
{
// Ping if forced or interval has elapsed
time_t timeNow = time(NULL);
const time_t timeNow = time(NULL);
if (force || timeNow - this->pingTimeLast > DB_PING_SEC)
{
@ -784,7 +784,7 @@ dbPing(Db *const this, const bool force)
/**********************************************************************************************************************************/
FN_EXTERN Pack *
dbTablespaceList(Db *this)
dbTablespaceList(Db *const this)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(DB, this);
@ -797,7 +797,7 @@ dbTablespaceList(Db *this)
/**********************************************************************************************************************************/
FN_EXTERN TimeMSec
dbTimeMSec(Db *this)
dbTimeMSec(Db *const this)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(DB, this);
@ -817,7 +817,7 @@ dbTimeMSec(Db *this)
/**********************************************************************************************************************************/
FN_EXTERN String *
dbWalSwitch(Db *this)
dbWalSwitch(Db *const this)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(DB, this);
@ -825,7 +825,7 @@ dbWalSwitch(Db *this)
ASSERT(this != NULL);
String *result = NULL;
String *result;
MEM_CONTEXT_TEMP_BEGIN()
{
@ -833,8 +833,8 @@ dbWalSwitch(Db *this)
dbQueryColumn(this, STRDEF("select pg_catalog.pg_create_restore_point('" PROJECT_NAME " Archive Check')::text"));
// Request a WAL segment switch
const char *walName = strZ(pgWalName(dbPgVersion(this)));
const String *walFileName = pckReadStrP(
const char *const walName = strZ(pgWalName(dbPgVersion(this)));
const String *const walFileName = pckReadStrP(
dbQueryColumn(this, strNewFmt("select pg_catalog.pg_%sfile_name(pg_catalog.pg_switch_%s())::text", walName, walName)));
// Copy WAL segment name to the prior context

View File

@ -14,7 +14,7 @@ Database Helper
/**********************************************************************************************************************************/
// Helper to get a connection to the specified pg cluster
static Db *
dbGetIdx(unsigned int pgIdx)
dbGetIdx(const unsigned int pgIdx)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(UINT, pgIdx);
@ -22,7 +22,7 @@ dbGetIdx(unsigned int pgIdx)
ASSERT(pgIdx < cfgOptionGroupIdxTotal(cfgOptGrpPg));
Db *result = NULL;
Db *result;
MEM_CONTEXT_TEMP_BEGIN()
{
@ -48,7 +48,7 @@ dbGetIdx(unsigned int pgIdx)
}
FN_EXTERN DbGetResult
dbGet(bool primaryOnly, bool primaryRequired, bool standbyRequired)
dbGet(const bool primaryOnly, const bool primaryRequired, const bool standbyRequired)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(BOOL, primaryOnly);

View File

@ -50,7 +50,7 @@ dbOpenProtocol(PackRead *const param, ProtocolServer *const server)
MEM_CONTEXT_BEGIN(lstMemContext(dbProtocolLocal.pgClientList))
{
// Only a single db is passed to the remote
PgClient *pgClient = pgClientNew(
PgClient *const pgClient = pgClientNew(
cfgOptionStrNull(cfgOptPgSocketPath), cfgOptionUInt(cfgOptPgPort), cfgOptionStr(cfgOptPgDatabase),
cfgOptionStrNull(cfgOptPgUser), cfgOptionUInt64(cfgOptDbTimeout));
pgClientOpen(pgClient);

View File

@ -40,7 +40,9 @@ pgClientFreeResource(THIS_VOID)
/**********************************************************************************************************************************/
FN_EXTERN PgClient *
pgClientNew(const String *host, const unsigned int port, const String *database, const String *user, const TimeMSec timeout)
pgClientNew(
const String *const host, const unsigned int port, const String *const database, const String *const user,
const TimeMSec timeout)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(STRING, host);
@ -76,7 +78,7 @@ pgClientNew(const String *host, const unsigned int port, const String *database,
Just ignore notices and warnings
***********************************************************************************************************************************/
static void
pgClientNoticeProcessor(void *arg, const char *message)
pgClientNoticeProcessor(void *const arg, const char *const message)
{
(void)arg;
(void)message;
@ -86,7 +88,7 @@ pgClientNoticeProcessor(void *arg, const char *message)
Encode string to escape ' and \
***********************************************************************************************************************************/
static String *
pgClientEscape(const String *string)
pgClientEscape(const String *const string)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, string);
@ -94,7 +96,7 @@ pgClientEscape(const String *string)
ASSERT(string != NULL);
String *result = strCatZ(strNew(), "'");
String *const result = strCatZ(strNew(), "'");
// Iterate all characters in the string
for (unsigned stringIdx = 0; stringIdx < strSize(string); stringIdx++)
@ -115,7 +117,7 @@ pgClientEscape(const String *string)
/**********************************************************************************************************************************/
FN_EXTERN PgClient *
pgClientOpen(PgClient *this)
pgClientOpen(PgClient *const this)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(PG_CLIENT, this);
@ -127,7 +129,7 @@ pgClientOpen(PgClient *this)
MEM_CONTEXT_TEMP_BEGIN()
{
// Base connection string
String *connInfo = strCatFmt(
String *const connInfo = strCatFmt(
strNew(), "dbname=%s port=%u", strZ(pgClientEscape(pgClientDatabase(this))), pgClientPort(this));
// Add user if specified
@ -188,7 +190,7 @@ pgClientQuery(PgClient *const this, const String *const query, const PgClientQue
}
// Wait for a result
Wait *wait = waitNew(pgClientTimeout(this));
Wait *const wait = waitNew(pgClientTimeout(this));
bool busy = false;
do
@ -201,7 +203,7 @@ pgClientQuery(PgClient *const this, const String *const query, const PgClientQue
// If the query is still busy after the timeout attempt to cancel
if (busy)
{
PGcancel *cancel = PQgetCancel(this->connection);
PGcancel *const cancel = PQgetCancel(this->connection);
// If cancel is NULL then more than likely the server process crashed or disconnected
if (cancel == NULL)
@ -222,7 +224,7 @@ pgClientQuery(PgClient *const this, const String *const query, const PgClientQue
}
// Get the result (even if query was cancelled -- to prevent the connection being left in a bad state)
PGresult *pgResult = PQgetResult(this->connection);
PGresult *const pgResult = PQgetResult(this->connection);
TRY_BEGIN()
{
@ -231,7 +233,7 @@ pgClientQuery(PgClient *const this, const String *const query, const PgClientQue
THROW_FMT(DbQueryError, "query '%s' timed out after %" PRIu64 "ms", strZ(query), pgClientTimeout(this));
// If this was a command that returned no results then we are done
ExecStatusType resultStatus = PQresultStatus(pgResult);
const ExecStatusType resultStatus = PQresultStatus(pgResult);
if (resultStatus == PGRES_COMMAND_OK)
{
@ -254,8 +256,8 @@ pgClientQuery(PgClient *const this, const String *const query, const PgClientQue
// Fetch row and column values
PackWrite *const pack = pckWriteNewP();
int rowTotal = PQntuples(pgResult);
int columnTotal = PQnfields(pgResult);
const int rowTotal = PQntuples(pgResult);
const int columnTotal = PQnfields(pgResult);
if (resultType != pgClientQueryResultAny)
{
@ -267,7 +269,7 @@ pgClientQuery(PgClient *const this, const String *const query, const PgClientQue
}
// Get column types
Oid *columnType = memNew(sizeof(int) * (size_t)columnTotal);
Oid *const columnType = memNew(sizeof(int) * (size_t)columnTotal);
for (int columnIdx = 0; columnIdx < columnTotal; columnIdx++)
columnType[columnIdx] = PQftype(pgResult, columnIdx);
@ -280,7 +282,7 @@ pgClientQuery(PgClient *const this, const String *const query, const PgClientQue
for (int columnIdx = 0; columnIdx < columnTotal; columnIdx++)
{
char *value = PQgetvalue(pgResult, rowIdx, columnIdx);
const char *const value = PQgetvalue(pgResult, rowIdx, columnIdx);
// If value is zero-length then check if it is null
if (value[0] == '\0' && PQgetisnull(pgResult, rowIdx, columnIdx))
@ -354,7 +356,7 @@ pgClientQuery(PgClient *const this, const String *const query, const PgClientQue
/**********************************************************************************************************************************/
FN_EXTERN void
pgClientClose(PgClient *this)
pgClientClose(PgClient *const this)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(PG_CLIENT, this);

View File

@ -102,7 +102,7 @@ typedef struct PgControlCommon
Get the interface for a PostgreSQL version
***********************************************************************************************************************************/
static const PgInterface *
pgInterfaceVersion(unsigned int pgVersion)
pgInterfaceVersion(const unsigned int pgVersion)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
@ -169,7 +169,7 @@ pgDbIsSystemId(const unsigned int id)
Check expected WAL segment size for older PostgreSQL versions
***********************************************************************************************************************************/
static void
pgWalSegmentSizeCheck(unsigned int pgVersion, unsigned int walSegmentSize)
pgWalSegmentSizeCheck(const unsigned int pgVersion, const unsigned int walSegmentSize)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
@ -278,7 +278,7 @@ pgControlCrcUpdate(Buffer *const controlFile, const unsigned int pgVersion, cons
/**********************************************************************************************************************************/
static PgControl
pgControlFromBuffer(const Buffer *controlFile, const String *const pgVersionForce)
pgControlFromBuffer(const Buffer *const controlFile, const String *const pgVersionForce)
{
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(BUFFER, controlFile);
@ -364,7 +364,7 @@ pgControlBufferFromFile(const Storage *const storage, const String *const pgVers
ASSERT(storage != NULL);
Buffer *result = NULL;
Buffer *result;
MEM_CONTEXT_TEMP_BEGIN()
{
@ -429,7 +429,7 @@ pgControlFromFile(const Storage *const storage, const String *const pgVersionFor
ASSERT(storage != NULL);
Buffer *const buffer = pgControlBufferFromFile(storage, pgVersionForce);
PgControl result = pgControlFromBuffer(buffer, pgVersionForce);
const PgControl result = pgControlFromBuffer(buffer, pgVersionForce);
bufFree(buffer);
@ -459,7 +459,7 @@ pgControlCheckpointInvalidate(Buffer *const buffer, const String *const pgVersio
/**********************************************************************************************************************************/
FN_EXTERN uint32_t
pgControlVersion(unsigned int pgVersion)
pgControlVersion(const unsigned int pgVersion)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
@ -482,7 +482,7 @@ typedef struct PgWalCommon
/**********************************************************************************************************************************/
FN_EXTERN PgWal
pgWalFromBuffer(const Buffer *walBuffer, const String *const pgVersionForce)
pgWalFromBuffer(const Buffer *const walBuffer, const String *const pgVersionForce)
{
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(BUFFER, walBuffer);
@ -535,7 +535,7 @@ pgWalFromBuffer(const Buffer *walBuffer, const String *const pgVersionForce)
}
FN_EXTERN PgWal
pgWalFromFile(const String *walFile, const Storage *storage, const String *const pgVersionForce)
pgWalFromFile(const String *const walFile, const Storage *const storage, const String *const pgVersionForce)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(STRING, walFile);
@ -544,12 +544,12 @@ pgWalFromFile(const String *walFile, const Storage *storage, const String *const
ASSERT(walFile != NULL);
PgWal result = {0};
PgWal result;
MEM_CONTEXT_TEMP_BEGIN()
{
// Read WAL segment header
Buffer *walBuffer = storageGetP(storageNewReadP(storage, walFile), .exactSize = PG_WAL_HEADER_SIZE);
const Buffer *const walBuffer = storageGetP(storageNewReadP(storage, walFile), .exactSize = PG_WAL_HEADER_SIZE);
result = pgWalFromBuffer(walBuffer, pgVersionForce);
}
@ -560,18 +560,18 @@ pgWalFromFile(const String *walFile, const Storage *storage, const String *const
/**********************************************************************************************************************************/
FN_EXTERN String *
pgTablespaceId(unsigned int pgVersion, unsigned int pgCatalogVersion)
pgTablespaceId(const unsigned int pgVersion, const unsigned int pgCatalogVersion)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
FUNCTION_TEST_PARAM(UINT, pgCatalogVersion);
FUNCTION_TEST_END();
String *result = NULL;
String *result;
MEM_CONTEXT_TEMP_BEGIN()
{
String *pgVersionStr = pgVersionToStr(pgVersion);
const String *const pgVersionStr = pgVersionToStr(pgVersion);
MEM_CONTEXT_PRIOR_BEGIN()
{
@ -596,7 +596,7 @@ pgLsnFromStr(const String *lsn)
MEM_CONTEXT_TEMP_BEGIN()
{
StringList *lsnPart = strLstNewSplit(lsn, FSLASH_STR);
const StringList *const lsnPart = strLstNewSplit(lsn, FSLASH_STR);
CHECK(FormatError, strLstSize(lsnPart) == 2, "lsn requires two parts");
@ -608,7 +608,7 @@ pgLsnFromStr(const String *lsn)
}
FN_EXTERN String *
pgLsnToStr(uint64_t lsn)
pgLsnToStr(const uint64_t lsn)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT64, lsn);
@ -619,7 +619,7 @@ pgLsnToStr(uint64_t lsn)
/**********************************************************************************************************************************/
FN_EXTERN String *
pgLsnToWalSegment(uint32_t timeline, uint64_t lsn, unsigned int walSegmentSize)
pgLsnToWalSegment(const uint32_t timeline, const uint64_t lsn, const unsigned int walSegmentSize)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, timeline);
@ -697,7 +697,7 @@ pgLsnRangeToWalSegmentList(
/**********************************************************************************************************************************/
FN_EXTERN const String *
pgLsnName(unsigned int pgVersion)
pgLsnName(const unsigned int pgVersion)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
@ -710,7 +710,7 @@ pgLsnName(unsigned int pgVersion)
Get WAL name (wal/xlog) for a PostgreSQL version
***********************************************************************************************************************************/
FN_EXTERN const String *
pgWalName(unsigned int pgVersion)
pgWalName(const unsigned int pgVersion)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
@ -721,7 +721,7 @@ pgWalName(unsigned int pgVersion)
/**********************************************************************************************************************************/
FN_EXTERN const String *
pgWalPath(unsigned int pgVersion)
pgWalPath(const unsigned int pgVersion)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
@ -732,7 +732,7 @@ pgWalPath(unsigned int pgVersion)
/**********************************************************************************************************************************/
FN_EXTERN const String *
pgXactPath(unsigned int pgVersion)
pgXactPath(const unsigned int pgVersion)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, pgVersion);
@ -767,7 +767,7 @@ pgVersionFromStr(const String *const version)
}
FN_EXTERN String *
pgVersionToStr(unsigned int version)
pgVersionToStr(const unsigned int version)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT, version);