88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtpage.c,v 1.21 1999/05/2516:07:26 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtpage.c,v 1.22 1999/05/2518:20:29 vadim Exp $
1212 *
1313 *NOTES
1414 * Postgres btree pages look like ordinary relation pages.The opaque
2727#include <storage/bufpage.h>
2828#include <access/nbtree.h>
2929#include <miscadmin.h>
30- #include <storage/bufmgr.h>
3130#include <storage/lmgr.h>
3231
3332#ifndef HAVE_MEMMOVE
3635#include <string.h>
3736#endif
3837
39- static void _bt_setpagelock (Relation rel ,BlockNumber blkno ,int access );
40- static void _bt_unsetpagelock (Relation rel ,BlockNumber blkno ,int access );
41-
4238#define BTREE_METAPAGE 0
4339#define BTREE_MAGIC 0x053162
4440
45- #ifdef BTREE_VERSION_1
4641#define BTREE_VERSION 1
47- #else
48- #define BTREE_VERSION 0
49- #endif
5042
5143typedef struct BTMetaPageData
5244{
5345uint32 btm_magic ;
5446uint32 btm_version ;
5547BlockNumber btm_root ;
56- #ifdef BTREE_VERSION_1
5748int32 btm_level ;
58- #endif
5949}BTMetaPageData ;
6050
6151#define BTPageGetMeta (p ) \
@@ -108,9 +98,7 @@ _bt_metapinit(Relation rel)
10898metad .btm_magic = BTREE_MAGIC ;
10999metad .btm_version = BTREE_VERSION ;
110100metad .btm_root = P_NONE ;
111- #ifdef BTREE_VERSION_1
112101metad .btm_level = 0 ;
113- #endif
114102memmove ((char * )BTPageGetMeta (pg ), (char * )& metad ,sizeof (metad ));
115103
116104op = (BTPageOpaque )PageGetSpecialPointer (pg );
@@ -246,9 +234,7 @@ _bt_getroot(Relation rel, int access)
246234rootblkno = BufferGetBlockNumber (rootbuf );
247235rootpg = BufferGetPage (rootbuf );
248236metad -> btm_root = rootblkno ;
249- #ifdef BTREE_VERSION_1
250237metad -> btm_level = 1 ;
251- #endif
252238_bt_pageinit (rootpg ,BufferGetPageSize (rootbuf ));
253239rootopaque = (BTPageOpaque )PageGetSpecialPointer (rootpg );
254240rootopaque -> btpo_flags |= (BTP_LEAF |BTP_ROOT );
@@ -257,8 +243,8 @@ _bt_getroot(Relation rel, int access)
257243/* swap write lock for read lock, if appropriate */
258244if (access != BT_WRITE )
259245{
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 );
262248}
263249
264250/* okay, metadata is correct */
@@ -322,31 +308,24 @@ _bt_getbuf(Relation rel, BlockNumber blkno, int access)
322308Buffer buf ;
323309Page page ;
324310
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-
330311if (blkno != P_NEW )
331312{
332- if (access == BT_WRITE )
333- _bt_setpagelock (rel ,blkno ,BT_WRITE );
334- else
335- _bt_setpagelock (rel ,blkno ,BT_READ );
336-
337313buf = ReadBuffer (rel ,blkno );
314+ LockBuffer (buf ,access );
338315}
339316else
340317{
318+ /*
319+ * Extend bufmgr code is unclean and so we have to
320+ * use locking here.
321+ */
322+ LockPage (rel ,0 ,ExclusiveLock );
341323buf = ReadBuffer (rel ,blkno );
324+ UnlockPage (rel ,0 ,ExclusiveLock );
342325blkno = BufferGetBlockNumber (buf );
343326page = BufferGetPage (buf );
344327_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 );
350329}
351330
352331/* ref count and lock type are correct */
@@ -359,16 +338,7 @@ _bt_getbuf(Relation rel, BlockNumber blkno, int access)
359338void
360339_bt_relbuf (Relation rel ,Buffer buf ,int access )
361340{
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 );
372342ReleaseBuffer (buf );
373343}
374344
@@ -382,11 +352,8 @@ _bt_relbuf(Relation rel, Buffer buf, int access)
382352void
383353_bt_wrtbuf (Relation rel ,Buffer buf )
384354{
385- BlockNumber blkno ;
386-
387- blkno = BufferGetBlockNumber (buf );
355+ LockBuffer (buf ,BUFFER_LOCK_UNLOCK );
388356WriteBuffer (buf );
389- _bt_unsetpagelock (rel ,blkno ,BT_WRITE );
390357}
391358
392359/*
@@ -399,9 +366,6 @@ _bt_wrtbuf(Relation rel, Buffer buf)
399366void
400367_bt_wrtnorelbuf (Relation rel ,Buffer buf )
401368{
402- BlockNumber blkno ;
403-
404- blkno = BufferGetBlockNumber (buf );
405369WriteNoReleaseBuffer (buf );
406370}
407371
@@ -452,12 +416,10 @@ _bt_metaproot(Relation rel, BlockNumber rootbknum, int level)
452416Assert (metaopaque -> btpo_flags & BTP_META );
453417metad = BTPageGetMeta (metap );
454418metad -> btm_root = rootbknum ;
455- #ifdef BTREE_VERSION_1
456- if (level == 0 )/* called from _do_insert */
419+ if (level == 0 )/* called from _do_insert */
457420metad -> btm_level += 1 ;
458421else
459422metad -> btm_level = level ;/* called from btsort */
460- #endif
461423_bt_wrtbuf (rel ,metabuf );
462424}
463425
@@ -582,32 +544,6 @@ _bt_getstackbuf(Relation rel, BTStack stack, int access)
582544}
583545}
584546
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-
611547void
612548_bt_pagedel (Relation rel ,ItemPointer tid )
613549{