]> granicus.if.org Git - postgresql/blob - src/backend/storage/lmgr/lmgr.c
Mark functions as static and ifdef NOT_USED as appropriate.
[postgresql] / src / backend / storage / lmgr / lmgr.c
1 /*-------------------------------------------------------------------------
2  *
3  * lmgr.c
4  *        POSTGRES lock manager code
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lmgr.c,v 1.41 2000/06/08 22:37:24 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include "access/transam.h"
19 #include "catalog/catalog.h"
20 #include "miscadmin.h"
21 #include "utils/inval.h"
22
23
24 static LOCKMASK LockConflicts[] = {
25         (int) NULL,
26
27 /* AccessShareLock */
28         (1 << AccessExclusiveLock),
29
30 /* RowShareLock */
31         (1 << ExclusiveLock) | (1 << AccessExclusiveLock),
32
33 /* RowExclusiveLock */
34         (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) | (1 << ShareLock) |
35         (1 << AccessExclusiveLock),
36
37 /* ShareLock */
38         (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) |
39         (1 << RowExclusiveLock) | (1 << AccessExclusiveLock),
40
41 /* ShareRowExclusiveLock */
42         (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) |
43         (1 << ShareLock) | (1 << RowExclusiveLock) | (1 << AccessExclusiveLock),
44
45 /* ExclusiveLock */
46         (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) | (1 << ShareLock) |
47         (1 << RowExclusiveLock) | (1 << RowShareLock) | (1 << AccessExclusiveLock),
48
49 /* AccessExclusiveLock */
50         (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) | (1 << ShareLock) |
51         (1 << RowExclusiveLock) | (1 << RowShareLock) | (1 << AccessExclusiveLock) |
52         (1 << AccessShareLock),
53
54 };
55
56 static int      LockPrios[] = {
57         (int) NULL,
58         1,
59         2,
60         3,
61         4,
62         5,
63         6,
64         7
65 };
66
67 LOCKMETHOD      LockTableId = (LOCKMETHOD) NULL;
68 LOCKMETHOD      LongTermTableId = (LOCKMETHOD) NULL;
69
70 /*
71  * Create the lock table described by LockConflicts and LockPrios.
72  */
73 LOCKMETHOD
74 InitLockTable()
75 {
76         int                     lockmethod;
77
78         lockmethod = LockMethodTableInit("LockTable",
79                                                         LockConflicts, LockPrios, MAX_LOCKMODES - 1);
80         LockTableId = lockmethod;
81
82         if (!(LockTableId))
83                 elog(ERROR, "InitLockTable: couldnt initialize lock table");
84
85 #ifdef USER_LOCKS
86
87         /*
88          * Allocate another tableId for long-term locks
89          */
90         LongTermTableId = LockMethodTableRename(LockTableId);
91         if (!(LongTermTableId))
92         {
93                 elog(ERROR,
94                          "InitLockTable: couldn't rename long-term lock table");
95         }
96 #endif
97
98         return LockTableId;
99 }
100
101 /*
102  * RelationInitLockInfo
103  *              Initializes the lock information in a relation descriptor.
104  *
105  *              relcache.c must call this during creation of any reldesc.
106  */
107 void
108 RelationInitLockInfo(Relation relation)
109 {
110         char       *relname;
111
112         Assert(RelationIsValid(relation));
113         Assert(OidIsValid(RelationGetRelid(relation)));
114
115         relname = (char *) RelationGetPhysicalRelationName(relation);
116
117         relation->rd_lockInfo.lockRelId.relId = RelationGetRelid(relation);
118
119         if (IsSharedSystemRelationName(relname))
120                 relation->rd_lockInfo.lockRelId.dbId = InvalidOid;
121         else
122                 relation->rd_lockInfo.lockRelId.dbId = MyDatabaseId;
123 }
124
125 /*
126  *              LockRelation
127  */
128 void
129 LockRelation(Relation relation, LOCKMODE lockmode)
130 {
131         LOCKTAG         tag;
132
133         if (LockingDisabled())
134                 return;
135
136         MemSet(&tag, 0, sizeof(tag));
137         tag.relId = relation->rd_lockInfo.lockRelId.relId;
138         tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
139         tag.objId.blkno = InvalidBlockNumber;
140
141         if (!LockAcquire(LockTableId, &tag, lockmode))
142                 elog(ERROR, "LockRelation: LockAcquire failed");
143
144         /*
145          * Check to see if the relcache entry has been invalidated while we
146          * were waiting to lock it.  If so, rebuild it, or elog() trying.
147          * Increment the refcount to ensure that RelationFlushRelation will
148          * rebuild it and not just delete it.
149          */
150         RelationIncrementReferenceCount(relation);
151         DiscardInvalid();
152         RelationDecrementReferenceCount(relation);
153 }
154
155 /*
156  *              UnlockRelation
157  */
158 void
159 UnlockRelation(Relation relation, LOCKMODE lockmode)
160 {
161         LOCKTAG         tag;
162
163         if (LockingDisabled())
164                 return;
165
166         MemSet(&tag, 0, sizeof(tag));
167         tag.relId = relation->rd_lockInfo.lockRelId.relId;
168         tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
169         tag.objId.blkno = InvalidBlockNumber;
170
171         LockRelease(LockTableId, &tag, lockmode);
172 }
173
174 /*
175  *              LockPage
176  */
177 void
178 LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
179 {
180         LOCKTAG         tag;
181
182         if (LockingDisabled())
183                 return;
184
185         MemSet(&tag, 0, sizeof(tag));
186         tag.relId = relation->rd_lockInfo.lockRelId.relId;
187         tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
188         tag.objId.blkno = blkno;
189
190         if (!LockAcquire(LockTableId, &tag, lockmode))
191                 elog(ERROR, "LockPage: LockAcquire failed");
192 }
193
194 /*
195  *              UnlockPage
196  */
197 void
198 UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
199 {
200         LOCKTAG         tag;
201
202         if (LockingDisabled())
203                 return;
204
205         MemSet(&tag, 0, sizeof(tag));
206         tag.relId = relation->rd_lockInfo.lockRelId.relId;
207         tag.dbId = relation->rd_lockInfo.lockRelId.dbId;
208         tag.objId.blkno = blkno;
209
210         LockRelease(LockTableId, &tag, lockmode);
211 }
212
213 void
214 XactLockTableInsert(TransactionId xid)
215 {
216         LOCKTAG         tag;
217
218         if (LockingDisabled())
219                 return;
220
221         MemSet(&tag, 0, sizeof(tag));
222         tag.relId = XactLockTableId;
223         tag.dbId = InvalidOid;
224         tag.objId.xid = xid;
225
226         if (!LockAcquire(LockTableId, &tag, ExclusiveLock))
227                 elog(ERROR, "XactLockTableInsert: LockAcquire failed");
228 }
229
230 #ifdef NOT_USED
231 void
232 XactLockTableDelete(TransactionId xid)
233 {
234         LOCKTAG         tag;
235
236         if (LockingDisabled())
237                 return;
238
239         MemSet(&tag, 0, sizeof(tag));
240         tag.relId = XactLockTableId;
241         tag.dbId = InvalidOid;
242         tag.objId.xid = xid;
243
244         LockRelease(LockTableId, &tag, ExclusiveLock);
245 }
246 #endif
247
248 void
249 XactLockTableWait(TransactionId xid)
250 {
251         LOCKTAG         tag;
252
253         if (LockingDisabled())
254                 return;
255
256         MemSet(&tag, 0, sizeof(tag));
257         tag.relId = XactLockTableId;
258         tag.dbId = InvalidOid;
259         tag.objId.xid = xid;
260
261         if (!LockAcquire(LockTableId, &tag, ShareLock))
262                 elog(ERROR, "XactLockTableWait: LockAcquire failed");
263
264         LockRelease(LockTableId, &tag, ShareLock);
265
266         /*
267          * Transaction was committed/aborted/crashed - we have to update
268          * pg_log if transaction is still marked as running.
269          */
270         if (!TransactionIdDidCommit(xid) && !TransactionIdDidAbort(xid))
271                 TransactionIdAbort(xid);
272 }