Skip to content

Commit

Permalink
add full range of OID
Browse files Browse the repository at this point in the history
  • Loading branch information
timchang514 committed Oct 13, 2023
1 parent c90f6c4 commit 2e0ce1b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/backend/access/transam/varsup.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,25 +521,26 @@ 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 */
if (RecoveryInProgress())
elog(ERROR, "cannot assign OIDs during recovery");

if (!OidIsValid((Oid) temp_oid_buffer_start)) /* If this is the first time initializing */
if (!OidIsValid((Oid) temp_oid_buffer_start)) /* InvalidOid means it needs assigning */
{
/* 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 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;
temp_oid_buffer_start = ShmemVariableCache->tempOidStart + INT_MIN;
nextTempOid = ShmemVariableCache->tempOidStart;
}
else
{
/* The first time this is called, we need to pick a start for the buffer range. */
/* We need to pick a new start for the buffer range. */
tempOidStart = ShmemVariableCache->nextOid;
if (ShmemVariableCache->oidCount < temp_oid_buffer_size)
ShmemVariableCache->oidCount = 0;
Expand All @@ -551,7 +552,7 @@ GetNewTempObjectId(void)
{
tempOidStart = FirstNormalObjectId;
}
temp_oid_buffer_start = tempOidStart;
temp_oid_buffer_start = tempOidStart + INT_MIN;
ShmemVariableCache->tempOidStart = tempOidStart;
ShmemVariableCache->tempOidBufferSize = temp_oid_buffer_size;
TempVariableCache = (VariableCache) palloc0(sizeof(*TempVariableCache));
Expand All @@ -564,12 +565,14 @@ GetNewTempObjectId(void)
LWLockRelease(OidGenLock);
}

real_temp_oid_buffer_start = temp_oid_buffer_start + INT_MIN;

/*
* Check for wraparound of the temp OID buffer.
*/
if (nextTempOid >= (Oid) (temp_oid_buffer_start + temp_oid_buffer_size) || nextTempOid < (Oid) temp_oid_buffer_start)
if (nextTempOid >= (Oid) (real_temp_oid_buffer_start + temp_oid_buffer_size) || nextTempOid < (Oid) real_temp_oid_buffer_start)
{
nextTempOid = (Oid) temp_oid_buffer_start;
nextTempOid = (Oid) real_temp_oid_buffer_start;
}

result = nextTempOid;
Expand Down Expand Up @@ -602,10 +605,10 @@ GetNewObjectId(void)

/* If we're in tempOid buffer range, skip ahead. */
if (OidIsValid(temp_oid_buffer_start)
&& ShmemVariableCache->nextOid >= temp_oid_buffer_start
&& ShmemVariableCache->nextOid < (temp_oid_buffer_start + temp_oid_buffer_size))
&& ShmemVariableCache->nextOid >= (Oid) (temp_oid_buffer_start + INT_MIN)
&& ShmemVariableCache->nextOid < (Oid) (temp_oid_buffer_start + temp_oid_buffer_size + INT_MIN))
{
ShmemVariableCache->nextOid = temp_oid_buffer_start + temp_oid_buffer_size;
ShmemVariableCache->nextOid = (Oid) (temp_oid_buffer_start + temp_oid_buffer_size + INT_MIN);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/backend/utils/misc/guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ int huge_pages;
int huge_page_size;

/*
* OIDs are stored as uint32, so we have to store it in a double.
* OIDs are stored as uint32, so we will add INT_MIN to match the range.
*/
int temp_oid_buffer_start;
int temp_oid_buffer_size;
Expand Down

0 comments on commit 2e0ce1b

Please sign in to comment.