OSDN Git Service

pgindent run. Make it all clean.
[pg-rex/syncrep.git] / 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.38 2001/03/22 03:59:17 momjian Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13
14 #include "postgres.h"
15
16 #include "access/transam.h"
17 #include "access/xlog.h"
18 #include "storage/proc.h"
19
20
21 /* Number of XIDs and OIDs to prefetch (preallocate) per XLOG write */
22 #define VAR_XID_PREFETCH                1024
23 #define VAR_OID_PREFETCH                8192
24
25 /* Spinlocks for serializing generation of XIDs and OIDs, respectively */
26 SPINLOCK        XidGenLockId;
27 SPINLOCK        OidGenLockId;
28
29 /* pointer to "variable cache" in shared memory (set up by shmem.c) */
30 VariableCache ShmemVariableCache = NULL;
31
32 void
33 GetNewTransactionId(TransactionId *xid)
34 {
35
36         /*
37          * During bootstrap initialization, we return the special bootstrap
38          * transaction id.
39          */
40         if (AMI_OVERRIDE)
41         {
42                 *xid = AmiTransactionId;
43                 return;
44         }
45
46         SpinAcquire(XidGenLockId);
47
48         *xid = ShmemVariableCache->nextXid;
49
50         (ShmemVariableCache->nextXid)++;
51
52         SpinRelease(XidGenLockId);
53
54         if (MyProc != (PROC *) NULL)
55                 MyProc->xid = *xid;
56 }
57
58 /*
59  * Read nextXid but don't allocate it.
60  */
61 void
62 ReadNewTransactionId(TransactionId *xid)
63 {
64
65         /*
66          * During bootstrap initialization, we return the special bootstrap
67          * transaction id.
68          */
69         if (AMI_OVERRIDE)
70         {
71                 *xid = AmiTransactionId;
72                 return;
73         }
74
75         SpinAcquire(XidGenLockId);
76         *xid = ShmemVariableCache->nextXid;
77         SpinRelease(XidGenLockId);
78 }
79
80 /* ----------------------------------------------------------------
81  *                                      object id generation support
82  * ----------------------------------------------------------------
83  */
84
85 static Oid      lastSeenOid = InvalidOid;
86
87 void
88 GetNewObjectId(Oid *oid_return)
89 {
90         SpinAcquire(OidGenLockId);
91
92         /* If we run out of logged for use oids then we must log more */
93         if (ShmemVariableCache->oidCount == 0)
94         {
95                 XLogPutNextOid(ShmemVariableCache->nextOid + VAR_OID_PREFETCH);
96                 ShmemVariableCache->oidCount = VAR_OID_PREFETCH;
97         }
98
99         if (PointerIsValid(oid_return))
100                 lastSeenOid = (*oid_return) = ShmemVariableCache->nextOid;
101
102         (ShmemVariableCache->nextOid)++;
103         (ShmemVariableCache->oidCount)--;
104
105         SpinRelease(OidGenLockId);
106 }
107
108 void
109 CheckMaxObjectId(Oid assigned_oid)
110 {
111         if (lastSeenOid != InvalidOid && assigned_oid < lastSeenOid)
112                 return;
113
114         SpinAcquire(OidGenLockId);
115
116         if (assigned_oid < ShmemVariableCache->nextOid)
117         {
118                 lastSeenOid = ShmemVariableCache->nextOid - 1;
119                 SpinRelease(OidGenLockId);
120                 return;
121         }
122
123         /* If we are in the logged oid range, just bump nextOid up */
124         if (assigned_oid <= ShmemVariableCache->nextOid +
125                 ShmemVariableCache->oidCount - 1)
126         {
127                 ShmemVariableCache->oidCount -=
128                         assigned_oid - ShmemVariableCache->nextOid + 1;
129                 ShmemVariableCache->nextOid = assigned_oid + 1;
130                 SpinRelease(OidGenLockId);
131                 return;
132         }
133
134         /*
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.
138          */
139
140         XLogPutNextOid(assigned_oid + VAR_OID_PREFETCH);
141         ShmemVariableCache->oidCount = VAR_OID_PREFETCH - 1;
142         ShmemVariableCache->nextOid = assigned_oid + 1;
143
144         SpinRelease(OidGenLockId);
145 }