1 /*-------------------------------------------------------------------------
4 * postgres OID & XID variables support routines
6 * Copyright (c) 2000, PostgreSQL, Inc
9 * $Header: /cvsroot/pgsql/src/backend/access/transam/varsup.c,v 1.34 2000/11/30 08:46:22 vadim Exp $
11 *-------------------------------------------------------------------------
16 #include "access/transam.h"
17 #include "storage/proc.h"
19 SPINLOCK OidGenLockId;
21 extern SPINLOCK XidGenLockId;
22 extern void XLogPutNextOid(Oid nextOid);
24 /* pointer to "variable cache" in shared memory (set up by shmem.c) */
25 VariableCache ShmemVariableCache = NULL;
28 GetNewTransactionId(TransactionId *xid)
31 * During bootstrap initialization, we return the special
32 * bootstrap transaction id.
36 *xid = AmiTransactionId;
40 SpinAcquire(XidGenLockId);
41 *xid = ShmemVariableCache->nextXid;
42 (ShmemVariableCache->nextXid)++;
44 if (MyProc != (PROC *) NULL)
47 SpinRelease(XidGenLockId);
52 * Like GetNewTransactionId reads nextXid but don't fetch it.
55 ReadNewTransactionId(TransactionId *xid)
59 * During bootstrap initialization, we return the special
60 * bootstrap transaction id.
64 *xid = AmiTransactionId;
68 SpinAcquire(XidGenLockId);
69 *xid = ShmemVariableCache->nextXid;
70 SpinRelease(XidGenLockId);
74 /* ----------------------------------------------------------------
75 * object id generation support
76 * ----------------------------------------------------------------
79 #define VAR_OID_PREFETCH 8192
80 static Oid lastSeenOid = InvalidOid;
83 GetNewObjectId(Oid *oid_return)
85 SpinAcquire(OidGenLockId);
87 /* If we run out of logged for use oids then we log more */
88 if (ShmemVariableCache->oidCount == 0)
90 XLogPutNextOid(ShmemVariableCache->nextOid + VAR_OID_PREFETCH);
91 ShmemVariableCache->oidCount = VAR_OID_PREFETCH;
94 if (PointerIsValid(oid_return))
95 lastSeenOid = (*oid_return) = ShmemVariableCache->nextOid;
97 (ShmemVariableCache->nextOid)++;
98 (ShmemVariableCache->oidCount)--;
100 SpinRelease(OidGenLockId);
104 CheckMaxObjectId(Oid assigned_oid)
107 if (lastSeenOid != InvalidOid && assigned_oid < lastSeenOid)
110 SpinAcquire(OidGenLockId);
111 if (assigned_oid < ShmemVariableCache->nextOid)
113 lastSeenOid = ShmemVariableCache->nextOid - 1;
114 SpinRelease(OidGenLockId);
118 /* If we are in the logged oid range, just bump nextOid up */
119 if (assigned_oid <= ShmemVariableCache->nextOid +
120 ShmemVariableCache->oidCount - 1)
122 ShmemVariableCache->oidCount -=
123 assigned_oid - ShmemVariableCache->nextOid + 1;
124 ShmemVariableCache->nextOid = assigned_oid + 1;
125 SpinRelease(OidGenLockId);
130 * We have exceeded the logged oid range.
131 * We should lock the database and kill all other backends
132 * but we are loading oid's that we can not guarantee are unique
133 * anyway, so we must rely on the user.
136 XLogPutNextOid(assigned_oid + VAR_OID_PREFETCH);
137 ShmemVariableCache->oidCount = VAR_OID_PREFETCH - 1;
138 ShmemVariableCache->nextOid = assigned_oid + 1;
140 SpinRelease(OidGenLockId);