1 /*-------------------------------------------------------------------------
4 * Hash table page management code for the Postgres hash access method
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/access/hash/hashpage.c,v 1.38 2003/08/04 02:39:57 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 the
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"
32 #include "storage/lmgr.h"
35 static void _hash_setpagelock(Relation rel, BlockNumber blkno, int access);
36 static void _hash_unsetpagelock(Relation rel, BlockNumber blkno, int access);
37 static void _hash_splitpage(Relation rel, Buffer metabuf, Bucket obucket, Bucket nbucket);
40 * We use high-concurrency locking on hash indices. There are two cases in
41 * which we don't do locking. One is when we're building the index.
42 * Since the creating transaction has not committed, no one can see
43 * the index, and there's no reason to share locks. The second case
44 * is when we're just starting up the database system. We use some
45 * special-purpose initialization code in the relation cache manager
46 * (see utils/cache/relcache.c) to allow us to do indexed scans on
47 * the system catalogs before we'd normally be able to. This happens
48 * before the lock table is fully initialized, so we can't use it.
49 * Strictly speaking, this violates 2pl, but we don't do 2pl on the
50 * system catalogs anyway.
52 * Note that our page locks are actual lockmanager locks, not buffer
53 * locks (as are used by btree, for example). This is a good idea because
54 * the algorithms are not deadlock-free, and we'd better be able to detect
55 * and recover from deadlocks.
57 * Another important difference from btree is that a hash indexscan
58 * retains both a lock and a buffer pin on the current index page
59 * between hashgettuple() calls (btree keeps only a buffer pin).
60 * Because of this, it's safe to do item deletions with only a regular
61 * write lock on a hash page --- there cannot be an indexscan stopped on
62 * the page being deleted, other than an indexscan of our own backend,
63 * which will be taken care of by _hash_adjscans.
67 #define USELOCKING (!BuildingHash && !IsInitProcessingMode())
71 * _hash_metapinit() -- Initialize the metadata page of a hash index,
72 * the two buckets that we begin with and the initial
76 _hash_metapinit(Relation rel)
79 HashPageOpaque pageopaque;
84 uint32 nelem; /* number elements */
85 uint32 lg2nelem; /* _hash_log2(nelem) */
88 /* can't be sharing this with anyone, now... */
90 LockRelation(rel, AccessExclusiveLock);
92 if (RelationGetNumberOfBlocks(rel) != 0)
93 elog(ERROR, "cannot initialize non-empty hash index \"%s\"",
94 RelationGetRelationName(rel));
96 metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_WRITE);
97 pg = BufferGetPage(metabuf);
98 metap = (HashMetaPage) pg;
99 _hash_pageinit(pg, BufferGetPageSize(metabuf));
101 metap->hashm_magic = HASH_MAGIC;
102 metap->hashm_version = HASH_VERSION;
103 metap->hashm_nkeys = 0;
104 metap->hashm_nmaps = 0;
105 metap->hashm_ffactor = DEFAULT_FFACTOR;
106 metap->hashm_bsize = BufferGetPageSize(metabuf);
107 metap->hashm_bshift = _hash_log2(metap->hashm_bsize);
108 for (i = metap->hashm_bshift; i > 0; --i)
110 if ((1 << i) < (metap->hashm_bsize -
111 (MAXALIGN(sizeof(PageHeaderData)) +
112 MAXALIGN(sizeof(HashPageOpaqueData)))))
116 metap->hashm_bmsize = 1 << i;
117 metap->hashm_procid = index_getprocid(rel, 1, HASHPROC);
120 * Make nelem = 2 rather than 0 so that we end up allocating space for
121 * the next greater power of two number of buckets.
124 lg2nelem = 1; /* _hash_log2(MAX(nelem, 2)) */
125 nbuckets = 2; /* 1 << lg2nelem */
127 MemSet((char *) metap->hashm_spares, 0, sizeof(metap->hashm_spares));
128 MemSet((char *) metap->hashm_mapp, 0, sizeof(metap->hashm_mapp));
130 metap->hashm_spares[lg2nelem] = 2; /* lg2nelem + 1 */
131 metap->hashm_spares[lg2nelem + 1] = 2; /* lg2nelem + 1 */
132 metap->hashm_ovflpoint = 1; /* lg2nelem */
133 metap->hashm_lastfreed = 2;
135 metap->hashm_maxbucket = metap->hashm_lowmask = 1; /* nbuckets - 1 */
136 metap->hashm_highmask = 3; /* (nbuckets << 1) - 1 */
138 pageopaque = (HashPageOpaque) PageGetSpecialPointer(pg);
139 pageopaque->hasho_oaddr = InvalidOvflAddress;
140 pageopaque->hasho_prevblkno = InvalidBlockNumber;
141 pageopaque->hasho_nextblkno = InvalidBlockNumber;
142 pageopaque->hasho_flag = LH_META_PAGE;
143 pageopaque->hasho_bucket = -1;
146 * First bitmap page is at: splitpoint lg2nelem page offset 1 which
147 * turns out to be page 3. Couldn't initialize page 3 until we
148 * created the first two buckets above.
150 if (_hash_initbitmap(rel, metap, OADDR_OF(lg2nelem, 1), lg2nelem + 1, 0))
151 elog(ERROR, "_hash_initbitmap failed");
154 _hash_wrtnorelbuf(metabuf);
157 * initialize the first two buckets
159 for (i = 0; i <= 1; i++)
161 buf = _hash_getbuf(rel, BUCKET_TO_BLKNO(i), HASH_WRITE);
162 pg = BufferGetPage(buf);
163 _hash_pageinit(pg, BufferGetPageSize(buf));
164 pageopaque = (HashPageOpaque) PageGetSpecialPointer(pg);
165 pageopaque->hasho_oaddr = InvalidOvflAddress;
166 pageopaque->hasho_prevblkno = InvalidBlockNumber;
167 pageopaque->hasho_nextblkno = InvalidBlockNumber;
168 pageopaque->hasho_flag = LH_BUCKET_PAGE;
169 pageopaque->hasho_bucket = i;
170 _hash_wrtbuf(rel, buf);
173 _hash_relbuf(rel, metabuf, HASH_WRITE);
176 UnlockRelation(rel, AccessExclusiveLock);
180 * _hash_getbuf() -- Get a buffer by block number for read or write.
182 * When this routine returns, the appropriate lock is set on the
183 * requested buffer its reference count is correct.
185 * XXX P_NEW is not used because, unlike the tree structures, we
186 * need the bucket blocks to be at certain block numbers. we must
187 * depend on the caller to call _hash_pageinit on the block if it
188 * knows that this is a new block.
191 _hash_getbuf(Relation rel, BlockNumber blkno, int access)
196 elog(ERROR, "hash AM does not use P_NEW");
201 _hash_setpagelock(rel, blkno, access);
204 elog(ERROR, "unrecognized hash access code: %d", access);
207 buf = ReadBuffer(rel, blkno);
209 /* ref count and lock type are correct */
214 * _hash_relbuf() -- release a locked buffer.
217 _hash_relbuf(Relation rel, Buffer buf, int access)
221 blkno = BufferGetBlockNumber(buf);
227 _hash_unsetpagelock(rel, blkno, access);
230 elog(ERROR, "unrecognized hash access code: %d", access);
238 * _hash_wrtbuf() -- write a hash page to disk.
240 * This routine releases the lock held on the buffer and our reference
241 * to it. It is an error to call _hash_wrtbuf() without a write lock
242 * or a reference to the buffer.
245 _hash_wrtbuf(Relation rel, Buffer buf)
249 blkno = BufferGetBlockNumber(buf);
251 _hash_unsetpagelock(rel, blkno, HASH_WRITE);
255 * _hash_wrtnorelbuf() -- write a hash page to disk, but do not release
256 * our reference or lock.
258 * It is an error to call _hash_wrtnorelbuf() without a write lock
259 * or a reference to the buffer.
262 _hash_wrtnorelbuf(Buffer buf)
266 blkno = BufferGetBlockNumber(buf);
267 WriteNoReleaseBuffer(buf);
271 _hash_chgbufaccess(Relation rel,
278 blkno = BufferGetBlockNumber(*bufp);
283 _hash_wrtbuf(rel, *bufp);
286 _hash_relbuf(rel, *bufp, from_access);
289 elog(ERROR, "unrecognized hash access code: %d", from_access);
292 *bufp = _hash_getbuf(rel, blkno, to_access);
293 return BufferGetPage(*bufp);
297 * _hash_pageinit() -- Initialize a new page.
300 _hash_pageinit(Page page, Size size)
302 Assert(PageIsNew(page));
303 PageInit(page, size, sizeof(HashPageOpaqueData));
307 _hash_setpagelock(Relation rel,
317 LockPage(rel, blkno, ExclusiveLock);
320 LockPage(rel, blkno, ShareLock);
323 elog(ERROR, "unrecognized hash access code: %d", access);
330 _hash_unsetpagelock(Relation rel,
340 UnlockPage(rel, blkno, ExclusiveLock);
343 UnlockPage(rel, blkno, ShareLock);
346 elog(ERROR, "unrecognized hash access code: %d", access);
353 * Delete a hash index item.
355 * It is safe to delete an item after acquiring a regular WRITE lock on
356 * the page, because no other backend can hold a READ lock on the page,
357 * and that means no other backend currently has an indexscan stopped on
358 * any item of the item being deleted. Our own backend might have such
359 * an indexscan (in fact *will*, since that's how VACUUM found the item
360 * in the first place), but _hash_adjscans will fix the scan position.
363 _hash_pagedel(Relation rel, ItemPointer tid)
371 HashPageOpaque opaque;
373 blkno = ItemPointerGetBlockNumber(tid);
374 offno = ItemPointerGetOffsetNumber(tid);
376 buf = _hash_getbuf(rel, blkno, HASH_WRITE);
377 page = BufferGetPage(buf);
378 _hash_checkpage(page, LH_BUCKET_PAGE | LH_OVERFLOW_PAGE);
379 opaque = (HashPageOpaque) PageGetSpecialPointer(page);
381 PageIndexTupleDelete(page, offno);
382 _hash_wrtnorelbuf(buf);
384 if (PageIsEmpty(page) && (opaque->hasho_flag & LH_OVERFLOW_PAGE))
386 buf = _hash_freeovflpage(rel, buf);
387 if (BufferIsValid(buf))
388 _hash_relbuf(rel, buf, HASH_WRITE);
391 _hash_relbuf(rel, buf, HASH_WRITE);
393 metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_WRITE);
394 metap = (HashMetaPage) BufferGetPage(metabuf);
395 _hash_checkpage((Page) metap, LH_META_PAGE);
396 metap->hashm_nkeys--;
397 _hash_wrtbuf(rel, metabuf);
401 _hash_expandtable(Relation rel, Buffer metabuf)
408 metap = (HashMetaPage) BufferGetPage(metabuf);
409 _hash_checkpage((Page) metap, LH_META_PAGE);
411 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_READ, HASH_WRITE);
412 new_bucket = ++metap->hashm_maxbucket;
413 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_WRITE, HASH_READ);
414 old_bucket = (metap->hashm_maxbucket & metap->hashm_lowmask);
417 * If the split point is increasing (hashm_maxbucket's log base 2 *
418 * increases), we need to copy the current contents of the spare split
419 * bucket to the next bucket.
421 spare_ndx = _hash_log2(metap->hashm_maxbucket + 1);
422 if (spare_ndx > metap->hashm_ovflpoint)
425 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_READ, HASH_WRITE);
426 metap->hashm_spares[spare_ndx] = metap->hashm_spares[metap->hashm_ovflpoint];
427 metap->hashm_ovflpoint = spare_ndx;
428 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_WRITE, HASH_READ);
431 if (new_bucket > metap->hashm_highmask)
434 /* Starting a new doubling */
435 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_READ, HASH_WRITE);
436 metap->hashm_lowmask = metap->hashm_highmask;
437 metap->hashm_highmask = new_bucket | metap->hashm_lowmask;
438 metap = (HashMetaPage) _hash_chgbufaccess(rel, &metabuf, HASH_WRITE, HASH_READ);
441 /* Relocate records to the new bucket */
442 _hash_splitpage(rel, metabuf, old_bucket, new_bucket);
447 * _hash_splitpage -- split 'obucket' into 'obucket' and 'nbucket'
449 * this routine is actually misnamed -- we are splitting a bucket that
450 * consists of a base bucket page and zero or more overflow (bucket
454 _hash_splitpage(Relation rel,
468 HashPageOpaque oopaque;
469 HashPageOpaque nopaque;
473 OffsetNumber ooffnum;
474 OffsetNumber noffnum;
475 OffsetNumber omaxoffnum;
480 metap = (HashMetaPage) BufferGetPage(metabuf);
481 _hash_checkpage((Page) metap, LH_META_PAGE);
483 /* get the buffers & pages */
484 oblkno = BUCKET_TO_BLKNO(obucket);
485 nblkno = BUCKET_TO_BLKNO(nbucket);
486 obuf = _hash_getbuf(rel, oblkno, HASH_WRITE);
487 nbuf = _hash_getbuf(rel, nblkno, HASH_WRITE);
488 opage = BufferGetPage(obuf);
489 npage = BufferGetPage(nbuf);
491 /* initialize the new bucket */
492 _hash_pageinit(npage, BufferGetPageSize(nbuf));
493 nopaque = (HashPageOpaque) PageGetSpecialPointer(npage);
494 nopaque->hasho_prevblkno = InvalidBlockNumber;
495 nopaque->hasho_nextblkno = InvalidBlockNumber;
496 nopaque->hasho_flag = LH_BUCKET_PAGE;
497 nopaque->hasho_oaddr = InvalidOvflAddress;
498 nopaque->hasho_bucket = nbucket;
499 _hash_wrtnorelbuf(nbuf);
502 * make sure the old bucket isn't empty. advance 'opage' and friends
503 * through the overflow bucket chain until we find a non-empty page.
505 * XXX we should only need this once, if we are careful to preserve the
506 * invariant that overflow pages are never empty.
508 _hash_checkpage(opage, LH_BUCKET_PAGE);
509 oopaque = (HashPageOpaque) PageGetSpecialPointer(opage);
510 if (PageIsEmpty(opage))
512 oblkno = oopaque->hasho_nextblkno;
513 _hash_relbuf(rel, obuf, HASH_WRITE);
514 if (!BlockNumberIsValid(oblkno))
517 * the old bucket is completely empty; of course, the new
518 * bucket will be as well, but since it's a base bucket page
521 _hash_relbuf(rel, nbuf, HASH_WRITE);
524 obuf = _hash_getbuf(rel, oblkno, HASH_WRITE);
525 opage = BufferGetPage(obuf);
526 _hash_checkpage(opage, LH_OVERFLOW_PAGE);
527 if (PageIsEmpty(opage))
528 elog(ERROR, "empty hash overflow page %u", oblkno);
529 oopaque = (HashPageOpaque) PageGetSpecialPointer(opage);
533 * we are now guaranteed that 'opage' is not empty. partition the
534 * tuples in the old bucket between the old bucket and the new bucket,
535 * advancing along their respective overflow bucket chains and adding
536 * overflow pages as needed.
538 ooffnum = FirstOffsetNumber;
539 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))
555 * we ran out of tuples on this particular page, but we
556 * have more overflow pages; re-init values.
558 _hash_wrtbuf(rel, obuf);
559 obuf = _hash_getbuf(rel, oblkno, HASH_WRITE);
560 opage = BufferGetPage(obuf);
561 _hash_checkpage(opage, LH_OVERFLOW_PAGE);
562 oopaque = (HashPageOpaque) PageGetSpecialPointer(opage);
563 /* we're guaranteed that an ovfl page has at least 1 tuple */
564 if (PageIsEmpty(opage))
565 elog(ERROR, "empty hash overflow page %u", oblkno);
566 ooffnum = FirstOffsetNumber;
567 omaxoffnum = PageGetMaxOffsetNumber(opage);
572 * we're at the end of the bucket chain, so now we're
573 * really done with everything. before quitting, call
574 * _hash_squeezebucket to ensure the tuples in the bucket
575 * (including the overflow pages) are packed as tightly as
578 _hash_wrtbuf(rel, obuf);
579 _hash_wrtbuf(rel, nbuf);
580 _hash_squeezebucket(rel, metap, obucket);
585 /* hash on the tuple */
586 hitem = (HashItem) PageGetItem(opage, PageGetItemId(opage, ooffnum));
587 itup = &(hitem->hash_itup);
588 itupdesc = RelationGetDescr(rel);
589 datum = index_getattr(itup, 1, itupdesc, &null);
590 bucket = _hash_call(rel, metap, datum);
592 if (bucket == nbucket)
595 * insert the tuple into the new bucket. if it doesn't fit on
596 * the current page in the new bucket, we must allocate a new
597 * overflow page and place the tuple on that page instead.
599 itemsz = IndexTupleDSize(hitem->hash_itup)
600 + (sizeof(HashItemData) - sizeof(IndexTupleData));
602 itemsz = MAXALIGN(itemsz);
604 if (PageGetFreeSpace(npage) < itemsz)
606 ovflbuf = _hash_addovflpage(rel, &metabuf, nbuf);
607 _hash_wrtbuf(rel, nbuf);
609 npage = BufferGetPage(nbuf);
610 _hash_checkpage(npage, LH_BUCKET_PAGE | LH_OVERFLOW_PAGE);
613 noffnum = OffsetNumberNext(PageGetMaxOffsetNumber(npage));
614 if (PageAddItem(npage, (Item) hitem, itemsz, noffnum, LP_USED)
615 == InvalidOffsetNumber)
616 elog(ERROR, "failed to add index item to \"%s\"",
617 RelationGetRelationName(rel));
618 _hash_wrtnorelbuf(nbuf);
621 * now delete the tuple from the old bucket. after this
622 * section of code, 'ooffnum' will actually point to the
623 * ItemId to which we would point if we had advanced it before
624 * the deletion (PageIndexTupleDelete repacks the ItemId
625 * array). this also means that 'omaxoffnum' is exactly one
626 * less than it used to be, so we really can just decrement it
627 * instead of calling PageGetMaxOffsetNumber.
629 PageIndexTupleDelete(opage, ooffnum);
630 _hash_wrtnorelbuf(obuf);
631 omaxoffnum = OffsetNumberPrev(omaxoffnum);
634 * tidy up. if the old page was an overflow page and it is
635 * now empty, we must free it (we want to preserve the
636 * invariant that overflow pages cannot be empty).
638 if (PageIsEmpty(opage) &&
639 (oopaque->hasho_flag & LH_OVERFLOW_PAGE))
641 obuf = _hash_freeovflpage(rel, obuf);
643 /* check that we're not through the bucket chain */
644 if (BufferIsInvalid(obuf))
646 _hash_wrtbuf(rel, nbuf);
647 _hash_squeezebucket(rel, metap, obucket);
652 * re-init. again, we're guaranteed that an ovfl page has
653 * at least one tuple.
655 opage = BufferGetPage(obuf);
656 _hash_checkpage(opage, LH_OVERFLOW_PAGE);
657 oblkno = BufferGetBlockNumber(obuf);
658 oopaque = (HashPageOpaque) PageGetSpecialPointer(opage);
659 if (PageIsEmpty(opage))
660 elog(ERROR, "empty hash overflow page %u", oblkno);
661 ooffnum = FirstOffsetNumber;
662 omaxoffnum = PageGetMaxOffsetNumber(opage);
668 * the tuple stays on this page. we didn't move anything, so
669 * we didn't delete anything and therefore we don't have to
670 * change 'omaxoffnum'.
672 * XXX any hash value from [0, nbucket-1] will map to this
673 * bucket, which doesn't make sense to me.
675 ooffnum = OffsetNumberNext(ooffnum);