From d5634cb189ab8cdbae01640dbeb47616441bc1c6 Mon Sep 17 00:00:00 2001 From: Tim Chang Date: Mon, 6 Nov 2023 17:07:25 +0000 Subject: [PATCH] Added comments, added OID macros --- src/backend/access/transam/varsup.c | 46 ++++++++++++++++------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/backend/access/transam/varsup.c b/src/backend/access/transam/varsup.c index be48823cbff..640d4c183ae 100644 --- a/src/backend/access/transam/varsup.c +++ b/src/backend/access/transam/varsup.c @@ -13,6 +13,8 @@ #include "postgres.h" +#include + #include "access/clog.h" #include "access/commit_ts.h" #include "access/subtrans.h" @@ -31,6 +33,9 @@ /* Number of OIDs to prefetch (preallocate) per XLOG write */ #define VAR_OID_PREFETCH 8192 +#define OID_TO_BUFFER_START(oid) ((oid) + INT_MIN) +#define BUFFER_START_TO_OID ((Oid) (temp_oid_buffer_start) - INT_MIN) + /* pointer to "variable cache" in shared memory (set up by shmem.c) */ VariableCache ShmemVariableCache = NULL; @@ -520,7 +525,6 @@ GetNewTempObjectId(void) { Oid result; Oid tempOidStart; - Oid real_temp_oid_buffer_start; static Oid nextTempOid = InvalidOid; /* safety check, we should never get this far in a HS standby */ @@ -531,47 +535,57 @@ GetNewTempObjectId(void) { /* First check to see if another connection has already picked a start, then update. */ LWLockAcquire(OidGenLock, LW_EXCLUSIVE); - if (OidIsValid(ShmemVariableCache->tempOidStart) && (ShmemVariableCache->tempOidBufferSize == temp_oid_buffer_size)) + if (OidIsValid(ShmemVariableCache->tempOidStart)) { - /* If ShmemVariableCache->tempOidStart is valid, it could also mean the buffer size GUC has been altered, and we need to pick a new start. */ - temp_oid_buffer_start = ShmemVariableCache->tempOidStart + INT_MIN; + temp_oid_buffer_start = OID_TO_BUFFER_START(ShmemVariableCache->tempOidStart); nextTempOid = ShmemVariableCache->tempOidStart; } else { /* We need to pick a new start for the buffer range. */ tempOidStart = ShmemVariableCache->nextOid; + + /* + * Decrement ShmemVariableCache->oidCount to take into account the new buffer we're allocating + */ if (ShmemVariableCache->oidCount < temp_oid_buffer_size) ShmemVariableCache->oidCount = 0; else ShmemVariableCache->oidCount -= temp_oid_buffer_size; + /* + * If ShmemVariableCache->nextOid is below FirstNormalObjectId then we can start at FirstNormalObjectId here and + * GetNewObjectId will return the right value on the next call. + */ + if (tempOidStart < FirstNormalObjectId) + tempOidStart = FirstNormalObjectId; + /* If the OID range would wraparound, start from beginning instead. */ if (tempOidStart + temp_oid_buffer_size < tempOidStart) - { tempOidStart = FirstNormalObjectId; - } - temp_oid_buffer_start = tempOidStart + INT_MIN; + + temp_oid_buffer_start = OID_TO_BUFFER_START(tempOidStart); ShmemVariableCache->tempOidStart = tempOidStart; ShmemVariableCache->tempOidBufferSize = temp_oid_buffer_size; nextTempOid = (Oid) tempOidStart; + + /* Skip nextOid ahead to end of range here as well. */ + ShmemVariableCache->nextOid = (Oid) (tempOidStart + temp_oid_buffer_size); } LWLockRelease(OidGenLock); } - real_temp_oid_buffer_start = temp_oid_buffer_start - INT_MIN; - /* * Check for wraparound of the temp OID buffer. */ - if (nextTempOid >= (Oid) (real_temp_oid_buffer_start + temp_oid_buffer_size) || nextTempOid < (Oid) real_temp_oid_buffer_start) + if (nextTempOid >= (Oid) (BUFFER_START_TO_OID + temp_oid_buffer_size) + || nextTempOid < BUFFER_START_TO_OID) { - nextTempOid = (Oid) real_temp_oid_buffer_start; + nextTempOid = BUFFER_START_TO_OID; } result = nextTempOid; - nextTempOid++; return result; @@ -598,14 +612,6 @@ GetNewObjectId(void) LWLockAcquire(OidGenLock, LW_EXCLUSIVE); - /* If we're in tempOid buffer range, skip ahead. */ - if (OidIsValid(temp_oid_buffer_start) - && ShmemVariableCache->nextOid >= (Oid) (temp_oid_buffer_start - INT_MIN) - && ShmemVariableCache->nextOid < (Oid) (temp_oid_buffer_start + temp_oid_buffer_size - INT_MIN)) - { - ShmemVariableCache->nextOid = (Oid) (temp_oid_buffer_start + temp_oid_buffer_size - INT_MIN); - } - /* * Check for wraparound of the OID counter. We *must* not return 0 * (InvalidOid), and in normal operation we mustn't return anything below