Skip to content

Commit

Permalink
Added comments, added OID macros
Browse files Browse the repository at this point in the history
  • Loading branch information
timchang514 committed Nov 6, 2023
1 parent 8129b5f commit 81072c0
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/backend/access/transam/varsup.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "postgres.h"

#include <unistd.h>

#include "access/clog.h"
#include "access/commit_ts.h"
#include "access/subtrans.h"
Expand All @@ -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;

Expand Down Expand Up @@ -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 */
Expand All @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 81072c0

Please sign in to comment.