1 /*-------------------------------------------------------------------------
4 * Hash table page management code for the Postgres hash access method
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/access/hash/hashpage.c,v 1.26 2000/01/26 05:55:55 momjian Exp $
14 * Postgres hash pages look like ordinary relation pages. The opaque
15 * data at high addresses includes information about the page including
16 * whether a page is an overflow page or a true bucket, the block
17 * numbers of the preceding and following pages, and the overflow
18 * address of the page if it is an overflow page.
20 * The first page in a hash relation, page zero, is special -- it stores
21 * information describing the hash table; it is referred to as teh
22 * "meta page." Pages one and higher store the actual data.
24 *-------------------------------------------------------------------------
29 #include "access/genam.h"
30 #include "access/hash.h"
31 #include "miscadmin.h"
34 static void _hash_setpagelock(Relation rel, BlockNumber blkno, int access);
35 static void _hash_unsetpagelock(Relation rel, BlockNumber blkno, int access);
36 static void _hash_splitpage(Relation rel, Buffer metabuf, Bucket obucket, Bucket nbucket);
39 * We use high-concurrency locking on hash indices. There are two cases in
40 * which we don't do locking. One is when we're building the index.
41 * Since the creating transaction has not committed, no one can see
42 * the index, and there's no reason to share locks. The second case
43 * is when we're just starting up the database system. We use some
44 * special-purpose initialization code in the relation cache manager
45 * (see utils/cache/relcache.c) to allow us to do indexed scans on
46 * the system catalogs before we'd normally be able to. This happens
47 * before the lock table is fully initialized, so we can't use it.
48 * Strictly speaking, this violates 2pl, but we don't do 2pl on the
49 * system catalogs anyway.
53 #define USELOCKING (!BuildingHash && !IsInitProcessingMode())
57 * _hash_metapinit() -- Initialize the metadata page of a hash index,
58 * the two buckets that we begin with and the initial
62 _hash_metapinit(Relation rel)
65 HashPageOpaque pageopaque;
70 uint32 nelem; /* number elements */
71 uint32 lg2nelem; /* _hash_log2(nelem) */
75 /* can't be sharing this with anyone, now... */
77 LockRelation(rel, AccessExclusiveLock);
79 if ((nblocks = RelationGetNumberOfBlocks(rel)) != 0)
81 elog(ERROR, "Cannot initialize non-empty hash table %s",
82 RelationGetRelationName(rel));
85 metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_WRITE);
86 pg = BufferGetPage(metabuf);
87 metap = (HashMetaPage) pg;
88 _hash_pageinit(pg, BufferGetPageSize(metabuf));
90 metap->hashm_magic = HASH_MAGIC;
91 metap->hashm_version = HASH_VERSION;
92 metap->hashm_nkeys = 0;
93 metap->hashm_nmaps = 0;
94 metap->hashm_ffactor = DEFAULT_FFACTOR;
95 metap->hashm_bsize = BufferGetPageSize(metabuf);
96 metap->hashm_bshift = _hash_log2(metap->hashm_bsize);
97 for (i = metap->hashm_bshift; i > 0; --i)
99 if ((1 << i) < (metap->hashm_bsize -
100 (MAXALIGN(sizeof(PageHeaderData)) +
101 MAXALIGN(sizeof(HashPageOpaqueData)))))
105 metap->hashm_bmsize = 1 << i;
106 metap->hashm_procid = index_getprocid(rel, 1, HASHPROC);
109 * Make nelem = 2 rather than 0 so that we end up allocating space for
110 * the next greater power of two number of buckets.
113 lg2nelem = 1; /* _hash_log2(MAX(nelem, 2)) */
114 nbuckets = 2; /* 1 << lg2nelem */
116 MemSet((char *) metap->hashm_spares, 0, sizeof(metap->hashm_spares));
117 MemSet((char *) metap->hashm_mapp, 0, sizeof(metap->hashm_mapp));
119 metap->hashm_spares[lg2nelem] = 2; /* lg2nelem + 1 */
120 metap->hashm_spares[lg2nelem + 1] = 2; /* lg2nelem + 1 */
121 metap->hashm_ovflpoint = 1; /* lg2nelem */
122 metap->hashm_lastfreed = 2;
124 metap->hashm_maxbucket = metap->hashm_lowmask = 1; /* nbuckets - 1 */
125 metap->hashm_highmask = 3; /* (nbuckets << 1) - 1 */
127 pageopaque = (HashPageOpaque) PageGetSpecialPointer(pg);
128 pageopaque->hasho_oaddr = InvalidOvflAddress;
129 pageopaque->hasho_prevblkno = InvalidBlockNumber;
130 pageopaque->hasho_nextblkno = InvalidBlockNumber;
131 pageopaque->hasho_flag = LH_META_PAGE;
132 pageopaque->hasho_bucket = -1;
135 * First bitmap page is at: splitpoint lg2nelem page offset 1 which
136 * turns out to be page 3. Couldn't initialize page 3 until we
137 * created the first two buckets above.
139 if (_hash_initbitmap(rel, metap, OADDR_OF(lg2nelem, 1), lg2nelem + 1, 0))
140 elog(ERROR, "Problem with _hash_initbitmap.");
143 _hash_wrtnorelbuf(rel, metabuf);
146 * initialize the first two buckets
148 for (i = 0; i <= 1; i++)
150 buf = _hash_getbuf(rel, BUCKET_TO_BLKNO(i), HASH_WRITE);
151 pg = BufferGetPage(buf);
152 _hash_pageinit(pg, BufferGetPageSize(buf));
153 pageopaque = (HashPageOpaque) PageGetSpecialPointer(pg);
154 pageopaque->hasho_oaddr = InvalidOvflAddress;
155 pageopaque->hasho_prevblkno = InvalidBlockNumber;
156 pageopaque->hasho_nextblkno = InvalidBlockNumber;
157 pageopaque->hasho_flag = LH_BUCKET_PAGE;
158 pageopaque->hasho_bucket = i;
159 _hash_wrtbuf(rel, buf);
162 _hash_relbuf(rel, metabuf, HASH_WRITE);
165 UnlockRelation(rel, AccessExclusiveLock);
169 * _hash_getbuf() -- Get a buffer by block number for read or write.
171 * When this routine returns, the appropriate lock is set on the
172 * requested buffer its reference count is correct.
174 * XXX P_NEW is not used because, unlike the tree structures, we
175 * need the bucket blocks to be at certain block numbers. we must
176 * depend on the caller to call _hash_pageinit on the block if it
177 * knows that this is a new block.
180 _hash_getbuf(Relation rel, BlockNumber blkno, int access)
185 elog(ERROR, "_hash_getbuf: internal error: hash AM does not use P_NEW");
190 _hash_setpagelock(rel, blkno, access);
193 elog(ERROR, "_hash_getbuf: invalid access (%d) on new blk: %s",
194 access, RelationGetRelationName(rel));
197 buf = ReadBuffer(rel, blkno);
199 /* ref count and lock type are correct */
204 * _hash_relbuf() -- release a locked buffer.
207 _hash_relbuf(Relation rel, Buffer buf, int access)
211 blkno = BufferGetBlockNumber(buf);
217 _hash_unsetpagelock(rel, blkno, access);
220 elog(ERROR, "_hash_relbuf: invalid access (%d) on blk %x: %s",
221 access, blkno, RelationGetRelationName(rel));
228 * _hash_wrtbuf() -- write a hash page to disk.
230 * This routine releases the lock held on the buffer and our reference
231 * to it. It is an error to call _hash_wrtbuf() without a write lock
232 * or a reference to the buffer.
235 _hash_wrtbuf(Relation rel, Buffer buf)
239 blkno = BufferGetBlockNumber(buf);
241 _hash_unsetpagelock(rel, blkno, HASH_WRITE);
245 * _hash_wrtnorelbuf() -- write a hash page to disk, but do not release
246 * our reference or lock.
248 * It is an error to call _hash_wrtnorelbuf() without a write lock
249 * or a reference to the buffer.
252 _hash_wrtnorelbuf(Relation rel, Buffer buf)
256 blkno = BufferGetBlockNumber(buf);
257 WriteNoReleaseBuffer(buf);
261 _hash_chgbufaccess(Relation rel,
268 blkno = BufferGetBlockNumber(*bufp);
273 _hash_wrtbuf(rel, *bufp);
276 _hash_relbuf(rel, *bufp, from_access);
279 elog(ERROR, "_hash_chgbufaccess: invalid access (%d) on blk %x: %s",
280 from_access, blkno, RelationGetRelationName(rel));
283 *bufp = _hash_getbuf(rel, blkno, to_access);
284 return BufferGetPage(*bufp);
288 * _hash_pageinit() -- Initialize a new page.
291 _hash_pageinit(Page page, Size size)
293 Assert(((PageHeader) page)->pd_lower == 0);
294 Assert(((PageHeader) page)->pd_upper == 0);
295 Assert(((PageHeader) page)->pd_special == 0);
298 * Cargo-cult programming -- don't really need this to be zero, but
299 * creating new pages is an infrequent occurrence and it makes me feel
300 * good when I know they're empty.
302 MemSet(page, 0, size);
304 PageInit(page, size, sizeof(HashPageOpaqueData));
308 _hash_setpagelock(Relation rel,
318 LockPage(rel, blkno, ExclusiveLock);
321 LockPage(rel, blkno, ShareLock);
324 elog(ERROR, "_hash_setpagelock: invalid access (%d) on blk %x: %s",
325 access, blkno, RelationGetRelationName(rel));
332 _hash_unsetpagelock(Relation rel,
342 UnlockPage(rel, blkno, ExclusiveLock);
345 UnlockPage(rel, blkno, ShareLock);
348 elog(ERROR, "_hash_unsetpagelock: invalid access (%d) on blk %x: %s",
349 access, blkno, RelationGetRelationName(rel));
356 _hash_pagedel(Relation rel, ItemPointer tid)
364 HashPageOpaque opaque;
366 blkno = ItemPointerGetBlockNumber(tid);
367 offno = ItemPointerGetOffsetNumber(tid);
369 buf = _hash_getbuf(rel, blkno, HASH_WRITE);
370 page = BufferGetPage(buf);
371 _hash_checkpage(page, LH_BUCKET_PAGE | LH_OVERFLOW_PAGE);
372 opaque = (HashPageOpaque) PageGetSpecialPointer(page);
374 PageIndexTupleDelete(page, offno);
375 _hash_wrtnorelbuf(rel, buf);
377 if (PageIsEmpty(page) && (opaque->hasho_flag & LH_OVERFLOW_PAGE))
379 buf = _hash_freeovflpage(rel, buf);
380 if (BufferIsValid(buf))
381 _hash_relbuf(rel, buf, HASH_WRITE);
384 _hash_relbuf(rel, buf, HASH_WRITE);
386 metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_WRITE);
387 metap = (HashMetaPage) BufferGetPage(metabuf);
388 _hash_checkpage((Page) metap, LH_META_PAGE);
389 ++metap->hashm_nkeys;
390 _hash_wrtbuf(rel, metabuf);
394 _hash_expandtable(Relation rel, Buffer metabuf)
401 /* elog(DEBUG, "_hash_expandtable: expanding..."); */
403 metap = (HashMetaPage) BufferGetPage(metabuf);
404 _hash_checkpage((Page) metap, LH_META_PAGE);
406 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_READ, HASH_WRITE);
407 new_bucket = ++metap->MAX_BUCKET;
408 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_WRITE, HASH_READ);
409 old_bucket = (metap->MAX_BUCKET & metap->LOW_MASK);
412 * If the split point is increasing (MAX_BUCKET's log base 2 *
413 * increases), we need to copy the current contents of the spare split
414 * bucket to the next bucket.
416 spare_ndx = _hash_log2(metap->MAX_BUCKET + 1);
417 if (spare_ndx > metap->OVFL_POINT)
420 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_READ, HASH_WRITE);
421 metap->SPARES[spare_ndx] = metap->SPARES[metap->OVFL_POINT];
422 metap->OVFL_POINT = spare_ndx;
423 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_WRITE, HASH_READ);
426 if (new_bucket > metap->HIGH_MASK)
429 /* Starting a new doubling */
430 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_READ, HASH_WRITE);
431 metap->LOW_MASK = metap->HIGH_MASK;
432 metap->HIGH_MASK = new_bucket | metap->LOW_MASK;
433 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_WRITE, HASH_READ);
436 /* Relocate records to the new bucket */
437 _hash_splitpage(rel, metabuf, old_bucket, new_bucket);
442 * _hash_splitpage -- split 'obucket' into 'obucket' and 'nbucket'
444 * this routine is actually misnamed -- we are splitting a bucket that
445 * consists of a base bucket page and zero or more overflow (bucket
449 _hash_splitpage(Relation rel,
463 HashPageOpaque oopaque;
464 HashPageOpaque nopaque;
468 OffsetNumber ooffnum;
469 OffsetNumber noffnum;
470 OffsetNumber omaxoffnum;
475 /* elog(DEBUG, "_hash_splitpage: splitting %d into %d,%d",
476 obucket, obucket, nbucket);
478 metap = (HashMetaPage) BufferGetPage(metabuf);
479 _hash_checkpage((Page) metap, LH_META_PAGE);
481 /* get the buffers & pages */
482 oblkno = BUCKET_TO_BLKNO(obucket);
483 nblkno = BUCKET_TO_BLKNO(nbucket);
484 obuf = _hash_getbuf(rel, oblkno, HASH_WRITE);
485 nbuf = _hash_getbuf(rel, nblkno, HASH_WRITE);
486 opage = BufferGetPage(obuf);
487 npage = BufferGetPage(nbuf);
489 /* initialize the new bucket */
490 _hash_pageinit(npage, BufferGetPageSize(nbuf));
491 nopaque = (HashPageOpaque) PageGetSpecialPointer(npage);
492 nopaque->hasho_prevblkno = InvalidBlockNumber;
493 nopaque->hasho_nextblkno = InvalidBlockNumber;
494 nopaque->hasho_flag = LH_BUCKET_PAGE;
495 nopaque->hasho_oaddr = InvalidOvflAddress;
496 nopaque->hasho_bucket = nbucket;
497 _hash_wrtnorelbuf(rel, nbuf);
500 * make sure the old bucket isn't empty. advance 'opage' and friends
501 * through the overflow bucket chain until we find a non-empty page.
503 * XXX we should only need this once, if we are careful to preserve the
504 * invariant that overflow pages are never empty.
506 _hash_checkpage(opage, LH_BUCKET_PAGE);
507 oopaque = (HashPageOpaque) PageGetSpecialPointer(opage);
508 if (PageIsEmpty(opage))
510 oblkno = oopaque->hasho_nextblkno;
511 _hash_relbuf(rel, obuf, HASH_WRITE);
512 if (!BlockNumberIsValid(oblkno))
516 * the old bucket is completely empty; of course, the new
517 * bucket will be as well, but since it's a base bucket page
520 _hash_relbuf(rel, nbuf, HASH_WRITE);
523 obuf = _hash_getbuf(rel, oblkno, HASH_WRITE);
524 opage = BufferGetPage(obuf);
525 _hash_checkpage(opage, LH_OVERFLOW_PAGE);
526 if (PageIsEmpty(opage))
527 elog(ERROR, "_hash_splitpage: empty overflow page %d", oblkno);
528 oopaque = (HashPageOpaque) PageGetSpecialPointer(opage);
532 * we are now guaranteed that 'opage' is not empty. partition the
533 * tuples in the old bucket between the old bucket and the new bucket,
534 * advancing along their respective overflow bucket chains and adding
535 * overflow pages as needed.
537 ooffnum = FirstOffsetNumber;
538 omaxoffnum = PageGetMaxOffsetNumber(opage);
543 * at each iteration through this loop, each of these variables
544 * should be up-to-date: obuf opage oopaque ooffnum omaxoffnum
547 /* check if we're at the end of the page */
548 if (ooffnum > omaxoffnum)
550 /* at end of page, but check for overflow page */
551 oblkno = oopaque->hasho_nextblkno;
552 if (BlockNumberIsValid(oblkno))
556 * we ran out of tuples on this particular page, but we
557 * have more overflow pages; re-init values.
559 _hash_wrtbuf(rel, obuf);
560 obuf = _hash_getbuf(rel, oblkno, HASH_WRITE);
561 opage = BufferGetPage(obuf);
562 _hash_checkpage(opage, LH_OVERFLOW_PAGE);
563 oopaque = (HashPageOpaque) PageGetSpecialPointer(opage);
565 /* we're guaranteed that an ovfl page has at least 1 tuple */
566 if (PageIsEmpty(opage))
568 elog(ERROR, "_hash_splitpage: empty ovfl page %d!",
571 ooffnum = FirstOffsetNumber;
572 omaxoffnum = PageGetMaxOffsetNumber(opage);
578 * we're at the end of the bucket chain, so now we're
579 * really done with everything. before quitting, call
580 * _hash_squeezebucket to ensure the tuples in the bucket
581 * (including the overflow pages) are packed as tightly as
584 _hash_wrtbuf(rel, obuf);
585 _hash_wrtbuf(rel, nbuf);
586 _hash_squeezebucket(rel, metap, obucket);
591 /* hash on the tuple */
592 hitem = (HashItem) PageGetItem(opage, PageGetItemId(opage, ooffnum));
593 itup = &(hitem->hash_itup);
594 itupdesc = RelationGetDescr(rel);
595 datum = index_getattr(itup, 1, itupdesc, &null);
596 bucket = _hash_call(rel, metap, datum);
598 if (bucket == nbucket)
602 * insert the tuple into the new bucket. if it doesn't fit on
603 * the current page in the new bucket, we must allocate a new
604 * overflow page and place the tuple on that page instead.
606 itemsz = IndexTupleDSize(hitem->hash_itup)
607 + (sizeof(HashItemData) - sizeof(IndexTupleData));
609 itemsz = MAXALIGN(itemsz);
611 if (PageGetFreeSpace(npage) < itemsz)
613 ovflbuf = _hash_addovflpage(rel, &metabuf, nbuf);
614 _hash_wrtbuf(rel, nbuf);
616 npage = BufferGetPage(nbuf);
617 _hash_checkpage(npage, LH_BUCKET_PAGE | LH_OVERFLOW_PAGE);
620 noffnum = OffsetNumberNext(PageGetMaxOffsetNumber(npage));
621 PageAddItem(npage, (Item) hitem, itemsz, noffnum, LP_USED);
622 _hash_wrtnorelbuf(rel, nbuf);
625 * now delete the tuple from the old bucket. after this
626 * section of code, 'ooffnum' will actually point to the
627 * ItemId to which we would point if we had advanced it before
628 * the deletion (PageIndexTupleDelete repacks the ItemId
629 * array). this also means that 'omaxoffnum' is exactly one
630 * less than it used to be, so we really can just decrement it
631 * instead of calling PageGetMaxOffsetNumber.
633 PageIndexTupleDelete(opage, ooffnum);
634 _hash_wrtnorelbuf(rel, obuf);
635 omaxoffnum = OffsetNumberPrev(omaxoffnum);
638 * tidy up. if the old page was an overflow page and it is
639 * now empty, we must free it (we want to preserve the
640 * invariant that overflow pages cannot be empty).
642 if (PageIsEmpty(opage) &&
643 (oopaque->hasho_flag & LH_OVERFLOW_PAGE))
645 obuf = _hash_freeovflpage(rel, obuf);
647 /* check that we're not through the bucket chain */
648 if (BufferIsInvalid(obuf))
650 _hash_wrtbuf(rel, nbuf);
651 _hash_squeezebucket(rel, metap, obucket);
656 * re-init. again, we're guaranteed that an ovfl page has
657 * at least one tuple.
659 opage = BufferGetPage(obuf);
660 _hash_checkpage(opage, LH_OVERFLOW_PAGE);
661 oblkno = BufferGetBlockNumber(obuf);
662 oopaque = (HashPageOpaque) PageGetSpecialPointer(opage);
663 if (PageIsEmpty(opage))
665 elog(ERROR, "_hash_splitpage: empty overflow page %d",
668 ooffnum = FirstOffsetNumber;
669 omaxoffnum = PageGetMaxOffsetNumber(opage);
676 * the tuple stays on this page. we didn't move anything, so
677 * we didn't delete anything and therefore we don't have to
678 * change 'omaxoffnum'.
680 * XXX any hash value from [0, nbucket-1] will map to this
681 * bucket, which doesn't make sense to me.
683 ooffnum = OffsetNumberNext(ooffnum);