1 /*-------------------------------------------------------------------------
4 * postgres OID & XID variables support routines
6 * Copyright (c) 2000, PostgreSQL Global Development Group
9 * $Header: /cvsroot/pgsql/src/backend/access/transam/varsup.c,v 1.40 2001/05/25 15:45:32 momjian Exp $
11 *-------------------------------------------------------------------------
16 #include "access/transam.h"
17 #include "access/xlog.h"
18 #include "storage/proc.h"
21 /* Number of XIDs and OIDs to prefetch (preallocate) per XLOG write */
22 #define VAR_XID_PREFETCH 1024
23 #define VAR_OID_PREFETCH 8192
25 /* Spinlocks for serializing generation of XIDs and OIDs, respectively */
26 SPINLOCK XidGenLockId;
27 SPINLOCK OidGenLockId;
29 /* pointer to "variable cache" in shared memory (set up by shmem.c) */
30 VariableCache ShmemVariableCache = NULL;
33 GetNewTransactionId(TransactionId *xid)
37 * During bootstrap initialization, we return the special bootstrap
42 *xid = AmiTransactionId;
46 SpinAcquire(XidGenLockId);
48 *xid = ShmemVariableCache->nextXid;
50 (ShmemVariableCache->nextXid)++;
52 SpinRelease(XidGenLockId);
54 if (MyProc != (PROC *) NULL)
59 * Read nextXid but don't allocate it.
62 ReadNewTransactionId(TransactionId *xid)
66 * During bootstrap initialization, we return the special bootstrap
71 *xid = AmiTransactionId;
75 SpinAcquire(XidGenLockId);
76 *xid = ShmemVariableCache->nextXid;
77 SpinRelease(XidGenLockId);
80 /* ----------------------------------------------------------------
81 * object id generation support
82 * ----------------------------------------------------------------
85 static Oid lastSeenOid = InvalidOid;
88 GetNewObjectId(Oid *oid_return)
90 SpinAcquire(OidGenLockId);
92 /* If we run out of logged for use oids then we must log more */
93 if (ShmemVariableCache->oidCount == 0)
95 XLogPutNextOid(ShmemVariableCache->nextOid + VAR_OID_PREFETCH);
96 ShmemVariableCache->oidCount = VAR_OID_PREFETCH;
99 if (PointerIsValid(oid_return))
100 lastSeenOid = (*oid_return) = ShmemVariableCache->nextOid;
102 (ShmemVariableCache->nextOid)++;
103 (ShmemVariableCache->oidCount)--;
105 SpinRelease(OidGenLockId);
109 CheckMaxObjectId(Oid assigned_oid)
111 if (lastSeenOid != InvalidOid && assigned_oid < lastSeenOid)
114 SpinAcquire(OidGenLockId);
116 if (assigned_oid < ShmemVariableCache->nextOid)
118 lastSeenOid = ShmemVariableCache->nextOid - 1;
119 SpinRelease(OidGenLockId);
123 /* If we are in the logged oid range, just bump nextOid up */
124 if (assigned_oid <= ShmemVariableCache->nextOid +
125 ShmemVariableCache->oidCount - 1)
127 ShmemVariableCache->oidCount -=
128 assigned_oid - ShmemVariableCache->nextOid + 1;
129 ShmemVariableCache->nextOid = assigned_oid + 1;
130 SpinRelease(OidGenLockId);
135 * We have exceeded the logged oid range. We should lock the database
136 * and kill all other backends but we are loading oid's that we can
137 * not guarantee are unique anyway, so we must rely on the user.
140 XLogPutNextOid(assigned_oid + VAR_OID_PREFETCH);
141 ShmemVariableCache->oidCount = VAR_OID_PREFETCH - 1;
142 ShmemVariableCache->nextOid = assigned_oid + 1;
144 SpinRelease(OidGenLockId);