diff --git a/src/backend/access/transam/varsup.c b/src/backend/access/transam/varsup.c index 8b6cc1c458f..5d4c492bf02 100644 --- a/src/backend/access/transam/varsup.c +++ b/src/backend/access/transam/varsup.c @@ -555,17 +555,13 @@ GetNewTempObjectId(void) temp_oid_buffer_start = tempOidStart + INT_MIN; ShmemVariableCache->tempOidStart = tempOidStart; ShmemVariableCache->tempOidBufferSize = temp_oid_buffer_size; - TempVariableCache = (VariableCache) palloc0(sizeof(*TempVariableCache)); - TempVariableCache->nextOid = (Oid) tempOidStart; nextTempOid = (Oid) tempOidStart; - - result = tempOidStart; } LWLockRelease(OidGenLock); } - real_temp_oid_buffer_start = temp_oid_buffer_start + INT_MIN; + real_temp_oid_buffer_start = temp_oid_buffer_start - INT_MIN; /* * Check for wraparound of the temp OID buffer. @@ -605,10 +601,10 @@ GetNewObjectId(void) /* 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 - 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); + ShmemVariableCache->nextOid = (Oid) (temp_oid_buffer_start + temp_oid_buffer_size - INT_MIN); } /* diff --git a/src/backend/catalog/catalog.c b/src/backend/catalog/catalog.c index ffff673eb22..fb2d5822c98 100644 --- a/src/backend/catalog/catalog.c +++ b/src/backend/catalog/catalog.c @@ -434,15 +434,7 @@ GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn) { CHECK_FOR_INTERRUPTS(); - if (relation->rd_rel->relpersistence == RELPERSISTENCE_TEMP && sql_dialect == SQL_DIALECT_TSQL) - { - /* Temp tables use temp OID logic */ - newOid = GetNewTempObjectId(); - } - else - { - newOid = GetNewObjectId(); - } + newOid = GetNewObjectId(); ScanKeyInit(&key, oidcolumn, @@ -451,14 +443,12 @@ GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn) /* see notes above about using SnapshotAny */ scan = systable_beginscan(relation, indexId, true, - SnapshotAny, 1, &key); + SnapshotAny, 1, &key); collides = HeapTupleIsValid(systable_getnext(scan)); systable_endscan(scan); - - /* * Log that we iterate more than GETNEWOID_LOG_THRESHOLD but have not * yet found OID unused in the relation. Then repeat logging with @@ -515,7 +505,6 @@ GetNewTempOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn) ScanKeyData key; bool collides; uint64 retries = 0; - uint64 retries_before_log = GETNEWOID_LOG_THRESHOLD; /* Only system relations are supported */ Assert(IsSystemRelation(relation)); @@ -538,7 +527,6 @@ GetNewTempOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn) CHECK_FOR_INTERRUPTS(); newOid = GetNewTempObjectId(); - // elog(WARNING, "GetNewTempOidWithIndex got a newOid of %d for indexId %d", newOid, indexId); ScanKeyInit(&key, oidcolumn, @@ -547,14 +535,12 @@ GetNewTempOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn) /* see notes above about using SnapshotAny */ scan = systable_beginscan(relation, indexId, true, - SnapshotAny, 1, &key); + SnapshotAny, 1, &key); collides = HeapTupleIsValid(systable_getnext(scan)); systable_endscan(scan); - - /* * Log that we iterate more than GETNEWOID_LOG_THRESHOLD but have not * yet found OID unused in the relation. Then repeat logging with @@ -564,42 +550,15 @@ GetNewTempOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn) * logic is necessary not to fill up the server log with the similar * messages. */ - if (retries >= retries_before_log) + if (OidIsValid((Oid) temp_oid_buffer_start) && retries >= temp_oid_buffer_size) { - ereport(LOG, - (errmsg("still searching for an unused OID in relation \"%s\"", - RelationGetRelationName(relation)), - errdetail_plural("OID candidates have been checked %llu time, but no unused OID has been found yet.", - "OID candidates have been checked %llu times, but no unused OID has been found yet.", - retries, - (unsigned long long) retries))); - - /* - * Double the number of retries to do before logging next until it - * reaches GETNEWOID_LOG_MAX_INTERVAL. - */ - if (retries_before_log * 2 <= GETNEWOID_LOG_MAX_INTERVAL) - retries_before_log *= 2; - else - retries_before_log += GETNEWOID_LOG_MAX_INTERVAL; + ereport(ERROR, + (errmsg("Unable to allocate oid for temp table. Drop some temporary tables or start a new session."))); } retries++; } while (collides); - /* - * If at least one log message is emitted, also log the completion of OID - * assignment. - */ - if (retries > GETNEWOID_LOG_THRESHOLD) - { - ereport(LOG, - (errmsg_plural("new OID has been assigned in relation \"%s\" after %llu retry", - "new OID has been assigned in relation \"%s\" after %llu retries", - retries, - RelationGetRelationName(relation), (unsigned long long) retries))); - } - return newOid; } @@ -626,6 +585,7 @@ GetNewRelFileNode(Oid reltablespace, Relation pg_class, char relpersistence) char *rpath; bool collides; BackendId backend; + int tries = 0; /* * If we ever get here during pg_upgrade, there's something wrong; all @@ -711,7 +671,14 @@ GetNewRelFileNode(Oid reltablespace, Relation pg_class, char relpersistence) collides = false; } + if (OidIsValid((Oid) temp_oid_buffer_start) && tries > temp_oid_buffer_size) + { + ereport(ERROR, + (errmsg("Unable to allocate oid for temp table. Drop some temporary tables or start a new session."))); + } + pfree(rpath); + tries++; } while (collides); return rnode.node.relNode; diff --git a/src/backend/catalog/toasting.c b/src/backend/catalog/toasting.c index fab22ee1ba3..56fa1a29acf 100644 --- a/src/backend/catalog/toasting.c +++ b/src/backend/catalog/toasting.c @@ -198,9 +198,12 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, /* * Create the toast table and its index */ - if (sql_dialect == SQL_DIALECT_TSQL && (RelationIsBBFTableVariable(rel) || RelationIsBBFTempTable(rel))) + if (sql_dialect == SQL_DIALECT_TSQL && RelationIsBBFTableVariable(rel)) pg_toast_prefix = "@pg_toast"; + if (sql_dialect == SQL_DIALECT_TSQL && RelationIsBBFTempTable(rel)) + pg_toast_prefix = "#pg_toast"; + snprintf(toast_relname, sizeof(toast_relname), "%s_%u", pg_toast_prefix, relOid); snprintf(toast_idxname, sizeof(toast_idxname),