8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtpage.c,v 1.21 1999/05/25 16:07:26 momjian Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtpage.c,v 1.22 1999/05/25 18:20:29 vadim Exp $
12
12
*
13
13
* NOTES
14
14
* Postgres btree pages look like ordinary relation pages. The opaque
27
27
#include <storage/bufpage.h>
28
28
#include <access/nbtree.h>
29
29
#include <miscadmin.h>
30
- #include <storage/bufmgr.h>
31
30
#include <storage/lmgr.h>
32
31
33
32
#ifndef HAVE_MEMMOVE
36
35
#include <string.h>
37
36
#endif
38
37
39
- static void _bt_setpagelock (Relation rel , BlockNumber blkno , int access );
40
- static void _bt_unsetpagelock (Relation rel , BlockNumber blkno , int access );
41
-
42
38
#define BTREE_METAPAGE 0
43
39
#define BTREE_MAGIC 0x053162
44
40
45
- #ifdef BTREE_VERSION_1
46
41
#define BTREE_VERSION 1
47
- #else
48
- #define BTREE_VERSION 0
49
- #endif
50
42
51
43
typedef struct BTMetaPageData
52
44
{
53
45
uint32 btm_magic ;
54
46
uint32 btm_version ;
55
47
BlockNumber btm_root ;
56
- #ifdef BTREE_VERSION_1
57
48
int32 btm_level ;
58
- #endif
59
49
} BTMetaPageData ;
60
50
61
51
#define BTPageGetMeta (p ) \
@@ -108,9 +98,7 @@ _bt_metapinit(Relation rel)
108
98
metad .btm_magic = BTREE_MAGIC ;
109
99
metad .btm_version = BTREE_VERSION ;
110
100
metad .btm_root = P_NONE ;
111
- #ifdef BTREE_VERSION_1
112
101
metad .btm_level = 0 ;
113
- #endif
114
102
memmove ((char * ) BTPageGetMeta (pg ), (char * ) & metad , sizeof (metad ));
115
103
116
104
op = (BTPageOpaque ) PageGetSpecialPointer (pg );
@@ -246,9 +234,7 @@ _bt_getroot(Relation rel, int access)
246
234
rootblkno = BufferGetBlockNumber (rootbuf );
247
235
rootpg = BufferGetPage (rootbuf );
248
236
metad -> btm_root = rootblkno ;
249
- #ifdef BTREE_VERSION_1
250
237
metad -> btm_level = 1 ;
251
- #endif
252
238
_bt_pageinit (rootpg , BufferGetPageSize (rootbuf ));
253
239
rootopaque = (BTPageOpaque ) PageGetSpecialPointer (rootpg );
254
240
rootopaque -> btpo_flags |= (BTP_LEAF | BTP_ROOT );
@@ -257,8 +243,8 @@ _bt_getroot(Relation rel, int access)
257
243
/* swap write lock for read lock, if appropriate */
258
244
if (access != BT_WRITE )
259
245
{
260
- _bt_setpagelock ( rel , rootblkno , BT_READ );
261
- _bt_unsetpagelock ( rel , rootblkno , BT_WRITE );
246
+ LockBuffer ( rootbuf , BUFFER_LOCK_UNLOCK );
247
+ LockBuffer ( rootbuf , BT_READ );
262
248
}
263
249
264
250
/* okay, metadata is correct */
@@ -322,31 +308,24 @@ _bt_getbuf(Relation rel, BlockNumber blkno, int access)
322
308
Buffer buf ;
323
309
Page page ;
324
310
325
- /*
326
- * If we want a new block, we can't set a lock of the appropriate type
327
- * until we've instantiated the buffer.
328
- */
329
-
330
311
if (blkno != P_NEW )
331
312
{
332
- if (access == BT_WRITE )
333
- _bt_setpagelock (rel , blkno , BT_WRITE );
334
- else
335
- _bt_setpagelock (rel , blkno , BT_READ );
336
-
337
313
buf = ReadBuffer (rel , blkno );
314
+ LockBuffer (buf , access );
338
315
}
339
316
else
340
317
{
318
+ /*
319
+ * Extend bufmgr code is unclean and so we have to
320
+ * use locking here.
321
+ */
322
+ LockPage (rel , 0 , ExclusiveLock );
341
323
buf = ReadBuffer (rel , blkno );
324
+ UnlockPage (rel , 0 , ExclusiveLock );
342
325
blkno = BufferGetBlockNumber (buf );
343
326
page = BufferGetPage (buf );
344
327
_bt_pageinit (page , BufferGetPageSize (buf ));
345
-
346
- if (access == BT_WRITE )
347
- _bt_setpagelock (rel , blkno , BT_WRITE );
348
- else
349
- _bt_setpagelock (rel , blkno , BT_READ );
328
+ LockBuffer (buf , access );
350
329
}
351
330
352
331
/* ref count and lock type are correct */
@@ -359,16 +338,7 @@ _bt_getbuf(Relation rel, BlockNumber blkno, int access)
359
338
void
360
339
_bt_relbuf (Relation rel , Buffer buf , int access )
361
340
{
362
- BlockNumber blkno ;
363
-
364
- blkno = BufferGetBlockNumber (buf );
365
-
366
- /* access had better be one of read or write */
367
- if (access == BT_WRITE )
368
- _bt_unsetpagelock (rel , blkno , BT_WRITE );
369
- else
370
- _bt_unsetpagelock (rel , blkno , BT_READ );
371
-
341
+ LockBuffer (buf , BUFFER_LOCK_UNLOCK );
372
342
ReleaseBuffer (buf );
373
343
}
374
344
@@ -382,11 +352,8 @@ _bt_relbuf(Relation rel, Buffer buf, int access)
382
352
void
383
353
_bt_wrtbuf (Relation rel , Buffer buf )
384
354
{
385
- BlockNumber blkno ;
386
-
387
- blkno = BufferGetBlockNumber (buf );
355
+ LockBuffer (buf , BUFFER_LOCK_UNLOCK );
388
356
WriteBuffer (buf );
389
- _bt_unsetpagelock (rel , blkno , BT_WRITE );
390
357
}
391
358
392
359
/*
@@ -399,9 +366,6 @@ _bt_wrtbuf(Relation rel, Buffer buf)
399
366
void
400
367
_bt_wrtnorelbuf (Relation rel , Buffer buf )
401
368
{
402
- BlockNumber blkno ;
403
-
404
- blkno = BufferGetBlockNumber (buf );
405
369
WriteNoReleaseBuffer (buf );
406
370
}
407
371
@@ -452,12 +416,10 @@ _bt_metaproot(Relation rel, BlockNumber rootbknum, int level)
452
416
Assert (metaopaque -> btpo_flags & BTP_META );
453
417
metad = BTPageGetMeta (metap );
454
418
metad -> btm_root = rootbknum ;
455
- #ifdef BTREE_VERSION_1
456
- if (level == 0 ) /* called from _do_insert */
419
+ if (level == 0 ) /* called from _do_insert */
457
420
metad -> btm_level += 1 ;
458
421
else
459
422
metad -> btm_level = level ; /* called from btsort */
460
- #endif
461
423
_bt_wrtbuf (rel , metabuf );
462
424
}
463
425
@@ -582,32 +544,6 @@ _bt_getstackbuf(Relation rel, BTStack stack, int access)
582
544
}
583
545
}
584
546
585
- static void
586
- _bt_setpagelock (Relation rel , BlockNumber blkno , int access )
587
- {
588
-
589
- if (USELOCKING )
590
- {
591
- if (access == BT_WRITE )
592
- LockPage (rel , blkno , ExclusiveLock );
593
- else
594
- LockPage (rel , blkno , ShareLock );
595
- }
596
- }
597
-
598
- static void
599
- _bt_unsetpagelock (Relation rel , BlockNumber blkno , int access )
600
- {
601
-
602
- if (USELOCKING )
603
- {
604
- if (access == BT_WRITE )
605
- UnlockPage (rel , blkno , ExclusiveLock );
606
- else
607
- UnlockPage (rel , blkno , ShareLock );
608
- }
609
- }
610
-
611
547
void
612
548
_bt_pagedel (Relation rel , ItemPointer tid )
613
549
{
0 commit comments