]> granicus.if.org Git - postgresql/blob - src/backend/access/transam/varsup.c
Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[postgresql] / src / backend / access / transam / varsup.c
1 /*-------------------------------------------------------------------------
2  *
3  * varsup.c
4  *        postgres OID & XID variables support routines
5  *
6  * Copyright (c) 2000, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  *        $Header: /cvsroot/pgsql/src/backend/access/transam/varsup.c,v 1.35 2001/01/24 19:42:51 momjian Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13
14 #include "postgres.h"
15
16 #include "access/transam.h"
17 #include "storage/proc.h"
18
19 SPINLOCK OidGenLockId;
20
21 extern SPINLOCK XidGenLockId;
22 extern void XLogPutNextOid(Oid nextOid);
23
24 /* pointer to "variable cache" in shared memory (set up by shmem.c) */
25 VariableCache ShmemVariableCache = NULL;
26
27 void
28 GetNewTransactionId(TransactionId *xid)
29 {
30         /*
31          * During bootstrap initialization, we return the special
32          * bootstrap transaction id.
33          */
34         if (AMI_OVERRIDE)
35         {
36                 *xid = AmiTransactionId;
37                 return;
38         }
39
40         SpinAcquire(XidGenLockId);
41         *xid = ShmemVariableCache->nextXid;
42         (ShmemVariableCache->nextXid)++;
43
44         if (MyProc != (PROC *) NULL)
45                 MyProc->xid = *xid;
46
47         SpinRelease(XidGenLockId);
48
49 }
50
51 /*
52  * Like GetNewTransactionId reads nextXid but don't fetch it.
53  */
54 void
55 ReadNewTransactionId(TransactionId *xid)
56 {
57
58         /*
59          * During bootstrap initialization, we return the special
60          * bootstrap transaction id.
61          */
62         if (AMI_OVERRIDE)
63         {
64                 *xid = AmiTransactionId;
65                 return;
66         }
67
68         SpinAcquire(XidGenLockId);
69         *xid = ShmemVariableCache->nextXid;
70         SpinRelease(XidGenLockId);
71
72 }
73
74 /* ----------------------------------------------------------------
75  *                                      object id generation support
76  * ----------------------------------------------------------------
77  */
78
79 #define VAR_OID_PREFETCH                8192
80 static Oid lastSeenOid = InvalidOid;
81
82 void
83 GetNewObjectId(Oid *oid_return)
84 {
85         SpinAcquire(OidGenLockId);
86
87         /* If we run out of logged for use oids then we log more */
88         if (ShmemVariableCache->oidCount == 0)
89         {
90                 XLogPutNextOid(ShmemVariableCache->nextOid + VAR_OID_PREFETCH);
91                 ShmemVariableCache->oidCount = VAR_OID_PREFETCH;
92         }
93
94         if (PointerIsValid(oid_return))
95                 lastSeenOid = (*oid_return) = ShmemVariableCache->nextOid;
96
97         (ShmemVariableCache->nextOid)++;
98         (ShmemVariableCache->oidCount)--;
99
100         SpinRelease(OidGenLockId);
101 }
102
103 void
104 CheckMaxObjectId(Oid assigned_oid)
105 {
106
107         if (lastSeenOid != InvalidOid && assigned_oid < lastSeenOid)
108                 return;
109
110         SpinAcquire(OidGenLockId);
111         if (assigned_oid < ShmemVariableCache->nextOid)
112         {
113                 lastSeenOid = ShmemVariableCache->nextOid - 1;
114                 SpinRelease(OidGenLockId);
115                 return;
116         }
117
118         /* If we are in the logged oid range, just bump nextOid up */
119         if (assigned_oid <= ShmemVariableCache->nextOid + 
120                                                 ShmemVariableCache->oidCount - 1)
121         {
122                 ShmemVariableCache->oidCount -= 
123                         assigned_oid - ShmemVariableCache->nextOid + 1;
124                 ShmemVariableCache->nextOid = assigned_oid + 1;
125                 SpinRelease(OidGenLockId);
126                 return;
127         }
128
129         /*
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.
134          */
135
136         XLogPutNextOid(assigned_oid + VAR_OID_PREFETCH);
137         ShmemVariableCache->oidCount = VAR_OID_PREFETCH - 1;
138         ShmemVariableCache->nextOid = assigned_oid + 1;
139
140         SpinRelease(OidGenLockId);
141
142 }