diff --git a/src/backend/access/hash/README b/src/backend/access/hash/README index 01ea115..e4c5bd0 100644 --- a/src/backend/access/hash/README +++ b/src/backend/access/hash/README @@ -188,17 +188,8 @@ track of available overflow pages. The reader algorithm is: - pin meta page and take buffer content lock in shared mode - loop: - compute bucket number for target hash key - release meta page buffer content lock - if (correct bucket page is already locked) - break - release any existing bucket page buffer content lock (if a concurrent - split happened) - take the buffer content lock on bucket page in shared mode - retake meta page buffer content lock in shared mode - release pin on metapage + given a hashkey get the target bucket page with the read lock, using + cached metapage; the usage of cached metapage is explained later. if the target bucket is still being populated by a split: release the buffer content lock on current bucket page pin and acquire the buffer content lock on old bucket in shared mode @@ -238,17 +229,8 @@ which this bucket is formed by split. The insertion algorithm is rather similar: - pin meta page and take buffer content lock in shared mode - loop: - compute bucket number for target hash key - release meta page buffer content lock - if (correct bucket page is already locked) - break - release any existing bucket page buffer content lock (if a concurrent - split happened) - take the buffer content lock on bucket page in exclusive mode - retake meta page buffer content lock in shared mode - release pin on metapage + given a hashkey get the target bucket page with the write lock, using + cached metapage; the usage of cached metapage is explained later. -- (so far same as reader, except for acquisition of buffer content lock in exclusive mode on primary bucket page) if the bucket-being-split flag is set for a bucket and pin count on it is @@ -290,6 +272,20 @@ When an inserter cannot find space in any existing page of a bucket, it must obtain an overflow page and add that page to the bucket's chain. Details of that part of the algorithm appear later. +The usage of cached metapage is explained as below. + + if metapage cache is not set, read the meta page data; and set the cache; + hold meta page pin. + Loop: + compute bucket number for target hash key; take the buffer content + lock on bucket page in the read/write mode as requested by + reader/insert algorithm. + if (target bucket is split before metapage data was cached) + break; + release any existing bucket page buffer content lock; update the + metapage cache with latest metapage data. + release if any pin on metapage + The page split algorithm is entered whenever an inserter observes that the index is overfull (has a higher-than-wanted ratio of tuples to buckets). The algorithm attempts, but does not necessarily succeed, to split one diff --git a/src/backend/access/hash/hash.c b/src/backend/access/hash/hash.c index 0cbf6b0..96b417a 100644 --- a/src/backend/access/hash/hash.c +++ b/src/backend/access/hash/hash.c @@ -505,26 +505,22 @@ hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, Bucket cur_bucket; Buffer metabuf; HashMetaPage metap; - HashMetaPageData local_metapage; + HashMetaPage cachedmetap; tuples_removed = 0; num_index_tuples = 0; /* - * Read the metapage to fetch original bucket and tuple counts. Also, we - * keep a copy of the last-seen metapage so that we can use its - * hashm_spares[] values to compute bucket page addresses. This is a bit - * hokey but perfectly safe, since the interesting entries in the spares - * array cannot change under us; and it beats rereading the metapage for - * each bucket. + * Read the metapage to fetch original bucket and tuple counts. We use the + * cached meta page data so that we can use its hashm_spares[] values to + * compute bucket page addresses. This is a bit hokey but perfectly safe, + * since the interesting entries in the spares array cannot change under + * us; and it beats rereading the metapage for each bucket. */ - metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_READ, LH_META_PAGE); - metap = HashPageGetMeta(BufferGetPage(metabuf)); - orig_maxbucket = metap->hashm_maxbucket; - orig_ntuples = metap->hashm_ntuples; - memcpy(&local_metapage, metap, sizeof(local_metapage)); - /* release the lock, but keep pin */ - LockBuffer(metabuf, BUFFER_LOCK_UNLOCK); + metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_NOLOCK, LH_META_PAGE); + cachedmetap = _hash_getcachedmetap(rel, metabuf); + orig_maxbucket = cachedmetap->hashm_maxbucket; + orig_ntuples = cachedmetap->hashm_ntuples; /* Scan the buckets that we know exist */ cur_bucket = 0; @@ -542,7 +538,7 @@ loop_top: bool split_cleanup = false; /* Get address of bucket's start page */ - bucket_blkno = BUCKET_TO_BLKNO(&local_metapage, cur_bucket); + bucket_blkno = BUCKET_TO_BLKNO(cachedmetap, cur_bucket); blkno = bucket_blkno; @@ -574,19 +570,19 @@ loop_top: * tuples left behind by the most recent split. To prevent that, * now that the primary page of the target bucket has been locked * (and thus can't be further split), update our cached metapage - * data. + * data in such case. */ - LockBuffer(metabuf, BUFFER_LOCK_SHARE); - memcpy(&local_metapage, metap, sizeof(local_metapage)); - LockBuffer(metabuf, BUFFER_LOCK_UNLOCK); + if (bucket_opaque->hasho_prevblkno != InvalidBlockNumber && + bucket_opaque->hasho_prevblkno > cachedmetap->hashm_maxbucket) + cachedmetap = _hash_getcachedmetap(rel, metabuf); } bucket_buf = buf; hashbucketcleanup(rel, cur_bucket, bucket_buf, blkno, info->strategy, - local_metapage.hashm_maxbucket, - local_metapage.hashm_highmask, - local_metapage.hashm_lowmask, &tuples_removed, + cachedmetap->hashm_maxbucket, + cachedmetap->hashm_highmask, + cachedmetap->hashm_lowmask, &tuples_removed, &num_index_tuples, split_cleanup, callback, callback_state); @@ -603,9 +599,9 @@ loop_top: if (cur_maxbucket != metap->hashm_maxbucket) { /* There's been a split, so process the additional bucket(s) */ - cur_maxbucket = metap->hashm_maxbucket; - memcpy(&local_metapage, metap, sizeof(local_metapage)); LockBuffer(metabuf, BUFFER_LOCK_UNLOCK); + cachedmetap = _hash_getcachedmetap(rel, metabuf); + cur_maxbucket = cachedmetap->hashm_maxbucket; goto loop_top; } diff --git a/src/backend/access/hash/hashinsert.c b/src/backend/access/hash/hashinsert.c index 39c70d3..bec5ef3 100644 --- a/src/backend/access/hash/hashinsert.c +++ b/src/backend/access/hash/hashinsert.c @@ -32,9 +32,7 @@ _hash_doinsert(Relation rel, IndexTuple itup) Buffer bucket_buf; Buffer metabuf; HashMetaPage metap; - BlockNumber blkno; - BlockNumber oldblkno; - bool retry; + HashMetaPage usedmetap = NULL; Page metapage; Page page; HashPageOpaque pageopaque; @@ -42,9 +40,6 @@ _hash_doinsert(Relation rel, IndexTuple itup) bool do_expand; uint32 hashkey; Bucket bucket; - uint32 maxbucket; - uint32 highmask; - uint32 lowmask; /* * Get the hash key for the item (it's stored in the index tuple itself). @@ -57,10 +52,15 @@ _hash_doinsert(Relation rel, IndexTuple itup) * need to be consistent */ restart_insert: - /* Read the metapage */ - metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_READ, LH_META_PAGE); + + /* + * Load the metapage. No need to lock as of now because we only access + * page header element pd_pagesize_version in HashMaxItemSize(), this + * element is constant and will not move while accessing. But we hold the + * pin so we can use the metabuf while writing into it below. + */ + metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_NOLOCK, LH_META_PAGE); metapage = BufferGetPage(metabuf); - metap = HashPageGetMeta(metapage); /* * Check whether the item can fit on a hash page at all. (Eventually, we @@ -76,66 +76,21 @@ restart_insert: itemsz, HashMaxItemSize(metapage)), errhint("Values larger than a buffer page cannot be indexed."))); - oldblkno = InvalidBlockNumber; - retry = false; - - /* - * Loop until we get a lock on the correct target bucket. - */ - for (;;) - { - /* - * Compute the target bucket number, and convert to block number. - */ - bucket = _hash_hashkey2bucket(hashkey, - metap->hashm_maxbucket, - metap->hashm_highmask, - metap->hashm_lowmask); - - blkno = BUCKET_TO_BLKNO(metap, bucket); - - /* - * Copy bucket mapping info now; refer the comment in - * _hash_expandtable where we copy this information before calling - * _hash_splitbucket to see why this is okay. - */ - maxbucket = metap->hashm_maxbucket; - highmask = metap->hashm_highmask; - lowmask = metap->hashm_lowmask; - - /* Release metapage lock, but keep pin. */ - LockBuffer(metabuf, BUFFER_LOCK_UNLOCK); - - /* - * If the previous iteration of this loop locked the primary page of - * what is still the correct target bucket, we are done. Otherwise, - * drop any old lock before acquiring the new one. - */ - if (retry) - { - if (oldblkno == blkno) - break; - _hash_relbuf(rel, buf); - } - - /* Fetch and lock the primary bucket page for the target bucket */ - buf = _hash_getbuf(rel, blkno, HASH_WRITE, LH_BUCKET_PAGE); - - /* - * Reacquire metapage lock and check that no bucket split has taken - * place while we were awaiting the bucket lock. - */ - LockBuffer(metabuf, BUFFER_LOCK_SHARE); - oldblkno = blkno; - retry = true; - } + buf = _hash_getbucketbuf_from_hashkey(rel, hashkey, HASH_WRITE, + &usedmetap); + Assert(usedmetap != NULL); /* remember the primary bucket buffer to release the pin on it at end. */ bucket_buf = buf; page = BufferGetPage(buf); pageopaque = (HashPageOpaque) PageGetSpecialPointer(page); - Assert(pageopaque->hasho_bucket == bucket); + + /* + * In _hash_getbucketbuf_from_hashkey we have verified the hasho_bucket. + * Should be safe to use further. + */ + bucket = pageopaque->hasho_bucket; /* * If this bucket is in the process of being split, try to finish the @@ -151,8 +106,10 @@ restart_insert: /* release the lock on bucket buffer, before completing the split. */ LockBuffer(buf, BUFFER_LOCK_UNLOCK); - _hash_finish_split(rel, metabuf, buf, pageopaque->hasho_bucket, - maxbucket, highmask, lowmask); + _hash_finish_split(rel, metabuf, buf, bucket, + usedmetap->hashm_maxbucket, + usedmetap->hashm_highmask, + usedmetap->hashm_lowmask); /* release the pin on old and meta buffer. retry for insert. */ _hash_dropbuf(rel, buf); @@ -225,6 +182,7 @@ restart_insert: */ LockBuffer(metabuf, BUFFER_LOCK_EXCLUSIVE); + metap = HashPageGetMeta(metapage); metap->hashm_ntuples += 1; /* Make sure this stays in sync with _hash_expandtable() */ diff --git a/src/backend/access/hash/hashpage.c b/src/backend/access/hash/hashpage.c index 9430794..27b3285 100644 --- a/src/backend/access/hash/hashpage.c +++ b/src/backend/access/hash/hashpage.c @@ -434,7 +434,13 @@ _hash_metapinit(Relation rel, double num_tuples, ForkNumber forkNum) buf = _hash_getnewbuf(rel, BUCKET_TO_BLKNO(metap, i), forkNum); pg = BufferGetPage(buf); pageopaque = (HashPageOpaque) PageGetSpecialPointer(pg); - pageopaque->hasho_prevblkno = InvalidBlockNumber; + + /* + * Set hasho_prevblkno with current hashm_maxbucket. This value will + * be used to validate cached HashMetaPageData. See + * _hash_getbucketbuf_from_hashkey(). + */ + pageopaque->hasho_prevblkno = metap->hashm_maxbucket; pageopaque->hasho_nextblkno = InvalidBlockNumber; pageopaque->hasho_bucket = i; pageopaque->hasho_flag = LH_BUCKET_PAGE; @@ -845,6 +851,12 @@ _hash_splitbucket(Relation rel, */ oopaque->hasho_flag |= LH_BUCKET_BEING_SPLIT; + /* + * Setting hasho_prevblkno of bucket page with latest maxbucket number to + * indicate bucket has been split and need to reconstruct + * HashMetaPageData. Below same is done for new bucket page. + */ + oopaque->hasho_prevblkno = maxbucket; npage = BufferGetPage(nbuf); /* @@ -852,7 +864,7 @@ _hash_splitbucket(Relation rel, * split is in progress. */ nopaque = (HashPageOpaque) PageGetSpecialPointer(npage); - nopaque->hasho_prevblkno = InvalidBlockNumber; + nopaque->hasho_prevblkno = maxbucket; nopaque->hasho_nextblkno = InvalidBlockNumber; nopaque->hasho_bucket = nbucket; nopaque->hasho_flag = LH_BUCKET_PAGE | LH_BUCKET_BEING_POPULATED; @@ -1191,3 +1203,126 @@ _hash_finish_split(Relation rel, Buffer metabuf, Buffer obuf, Bucket obucket, LockBuffer(obuf, BUFFER_LOCK_UNLOCK); hash_destroy(tidhtab); } + +/* + * _hash_getcachedmetap() -- Returns cached metapage data. + * + * metabuf : If set caller must hold a pin, but no lock, on the metapage. Read + * from metabuf and set the rd_amcache. + * + */ +HashMetaPage +_hash_getcachedmetap(Relation rel, Buffer metabuf) +{ + Page page; + + if (BufferIsInvalid(metabuf)) + return (HashMetaPage) rel->rd_amcache; + + if (rel->rd_amcache == NULL) + rel->rd_amcache = MemoryContextAlloc(rel->rd_indexcxt, + sizeof(HashMetaPageData)); + + /* Read the metapage. */ + LockBuffer(metabuf, BUFFER_LOCK_SHARE); + page = BufferGetPage(metabuf); + memcpy(rel->rd_amcache, HashPageGetMeta(page), sizeof(HashMetaPageData)); + + /* Release metapage lock. Keep the pin. */ + LockBuffer(metabuf, BUFFER_LOCK_UNLOCK); + return (HashMetaPage) rel->rd_amcache; +} + +/* + * _hash_getbucketbuf_from_hashkey() -- Get the bucket's buffer for the given + * hashkey. + * + * Bucket Pages do not move or get removed once they are allocated. This give + * us an opportunity to use the previously saved metapage contents to reach + * the target bucket buffer, instead of every time reading from the metapage + * buffer. This saves one buffer access everytime we want to reach the target + * bucket buffer, which is very helpful savings in bufmgr traffic and + * contention. + * + * The access type parameter (HASH_READ or HASH_WRITE) indicates whether the + * bucket buffer has to be locked for reading or writing. + * + * The out parameter cachedmetap is set with metapage contents used for + * hashkey to bucket buffer mapping. Some callers need this info to reach the + * old bucket in case of bucket split, see _hash_doinsert(). + */ +Buffer +_hash_getbucketbuf_from_hashkey(Relation rel, uint32 hashkey, int access, + HashMetaPage *cachedmetap) +{ + HashMetaPage metap; + Buffer buf; + Buffer metabuf = InvalidBuffer; + Page page; + Bucket bucket; + BlockNumber blkno; + HashPageOpaque opaque; + + /* We read from target bucket buffer, hence locking is must. */ + Assert(access == HASH_READ || access == HASH_WRITE); + + if (!(metap = _hash_getcachedmetap(rel, InvalidBuffer))) + { + metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_NOLOCK, LH_META_PAGE); + metap = _hash_getcachedmetap(rel, metabuf); + Assert(metap != NULL); + } + + /* + * Loop until we get a lock on the correct target bucket. + */ + for (;;) + { + /* + * Compute the target bucket number, and convert to block number. + */ + bucket = _hash_hashkey2bucket(hashkey, + metap->hashm_maxbucket, + metap->hashm_highmask, + metap->hashm_lowmask); + + blkno = BUCKET_TO_BLKNO(metap, bucket); + + /* Fetch the primary bucket page for the bucket */ + buf = _hash_getbuf(rel, blkno, access, LH_BUCKET_PAGE); + page = BufferGetPage(buf); + opaque = (HashPageOpaque) PageGetSpecialPointer(page); + Assert(opaque->hasho_bucket == bucket); + + /* + * Check if this bucket is split after we have cached the + * HashMetaPageData by comparing their respective hashm_maxbucket. If + * so we need to read the metapage and recompute the bucket number + * again. + */ + if (opaque->hasho_prevblkno == InvalidBlockNumber || + opaque->hasho_prevblkno <= metap->hashm_maxbucket) + { + /* Ok now we have the right bucket proceed to search in it. */ + break; + } + + /* First drop any locks held on bucket buffers. */ + _hash_relbuf(rel, buf); + + /* Update the cached meta page data. */ + if (BufferIsInvalid(metabuf)) + metabuf = + _hash_getbuf(rel, HASH_METAPAGE, HASH_NOLOCK, LH_META_PAGE); + metap = _hash_getcachedmetap(rel, metabuf); + Assert(metap != NULL); + } + + if (BufferIsValid(metabuf)) + _hash_dropbuf(rel, metabuf); + + if (cachedmetap) + *cachedmetap = metap; + + return buf; +} diff --git a/src/backend/access/hash/hashsearch.c b/src/backend/access/hash/hashsearch.c index c0bdfe6..922143d 100644 --- a/src/backend/access/hash/hashsearch.c +++ b/src/backend/access/hash/hashsearch.c @@ -139,6 +139,7 @@ _hash_readprev(IndexScanDesc scan, BlockNumber blkno; Relation rel = scan->indexRelation; HashScanOpaque so = (HashScanOpaque) scan->opaque; + bool haveprevblk = true; blkno = (*opaquep)->hasho_prevblkno; @@ -147,15 +148,20 @@ _hash_readprev(IndexScanDesc scan, * comments in _hash_first to know the reason of retaining pin. */ if (*bufp == so->hashso_bucket_buf || *bufp == so->hashso_split_bucket_buf) + { LockBuffer(*bufp, BUFFER_LOCK_UNLOCK); + haveprevblk = false; + } else _hash_relbuf(rel, *bufp); *bufp = InvalidBuffer; /* check for interrupts while we're not holding any buffer lock */ CHECK_FOR_INTERRUPTS(); - if (BlockNumberIsValid(blkno)) + + if (haveprevblk) { + Assert(BlockNumberIsValid(blkno)); *bufp = _hash_getbuf(rel, blkno, HASH_READ, LH_BUCKET_PAGE | LH_OVERFLOW_PAGE); *pagep = BufferGetPage(*bufp); @@ -215,14 +221,9 @@ _hash_first(IndexScanDesc scan, ScanDirection dir) ScanKey cur; uint32 hashkey; Bucket bucket; - BlockNumber blkno; - BlockNumber oldblkno = InvalidBuffer; - bool retry = false; Buffer buf; - Buffer metabuf; Page page; HashPageOpaque opaque; - HashMetaPage metap; IndexTuple itup; ItemPointer current; OffsetNumber offnum; @@ -277,59 +278,15 @@ _hash_first(IndexScanDesc scan, ScanDirection dir) so->hashso_sk_hash = hashkey; - /* Read the metapage */ - metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_READ, LH_META_PAGE); - page = BufferGetPage(metabuf); - metap = HashPageGetMeta(page); + buf = _hash_getbucketbuf_from_hashkey(rel, hashkey, HASH_READ, NULL); + page = BufferGetPage(buf); + opaque = (HashPageOpaque) PageGetSpecialPointer(page); /* - * Loop until we get a lock on the correct target bucket. + * In _hash_getbucketbuf_from_hashkey we have verified the hasho_bucket. + * Should be safe to use further. */ - for (;;) - { - /* - * Compute the target bucket number, and convert to block number. - */ - bucket = _hash_hashkey2bucket(hashkey, - metap->hashm_maxbucket, - metap->hashm_highmask, - metap->hashm_lowmask); - - blkno = BUCKET_TO_BLKNO(metap, bucket); - - /* Release metapage lock, but keep pin. */ - LockBuffer(metabuf, BUFFER_LOCK_UNLOCK); - - /* - * If the previous iteration of this loop locked what is still the - * correct target bucket, we are done. Otherwise, drop any old lock - * and lock what now appears to be the correct bucket. - */ - if (retry) - { - if (oldblkno == blkno) - break; - _hash_relbuf(rel, buf); - } - - /* Fetch the primary bucket page for the bucket */ - buf = _hash_getbuf(rel, blkno, HASH_READ, LH_BUCKET_PAGE); - - /* - * Reacquire metapage lock and check that no bucket split has taken - * place while we were awaiting the bucket lock. - */ - LockBuffer(metabuf, BUFFER_LOCK_SHARE); - oldblkno = blkno; - retry = true; - } - - /* done with the metapage */ - _hash_dropbuf(rel, metabuf); - - page = BufferGetPage(buf); - opaque = (HashPageOpaque) PageGetSpecialPointer(page); - Assert(opaque->hasho_bucket == bucket); + bucket = opaque->hasho_bucket; so->hashso_bucket_buf = buf; diff --git a/src/include/access/hash.h b/src/include/access/hash.h index b0a1131..2e1a6c5 100644 --- a/src/include/access/hash.h +++ b/src/include/access/hash.h @@ -60,6 +60,13 @@ typedef uint32 Bucket; typedef struct HashPageOpaqueData { + /* + * If this is an ovfl page this stores previous ovfl (or bucket) blkno. + * Else if this is a bucket page we use this for a special purpose. We + * store hashm_maxbucket value, whenever this page is initialized or + * split. So this helps us to know whether the bucket has been split after + * caching the HashMetaPageData. See _hash_getbucketbuf_from_hashkey(). + */ BlockNumber hasho_prevblkno; /* previous ovfl (or bucket) blkno */ BlockNumber hasho_nextblkno; /* next ovfl blkno */ Bucket hasho_bucket; /* bucket number this pg belongs to */ @@ -327,6 +334,10 @@ extern Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags); extern Buffer _hash_getbuf_with_condlock_cleanup(Relation rel, BlockNumber blkno, int flags); +extern HashMetaPage _hash_getcachedmetap(Relation rel, Buffer metabuf); +extern Buffer _hash_getbucketbuf_from_hashkey(Relation rel, uint32 hashkey, + int access, + HashMetaPage *cachedmetap); extern Buffer _hash_getinitbuf(Relation rel, BlockNumber blkno); extern Buffer _hash_getnewbuf(Relation rel, BlockNumber blkno, ForkNumber forkNum);