commit 6dcc4fee1668c1df88ad63e2b1857694a4e2ee9c
Author: mithun
Date: Fri Mar 24 01:15:45 2017 +0530
Expand the bucket efficiently

Mithun C Y
diff git a/contrib/pageinspect/expected/hash.out b/contrib/pageinspect/expected/hash.out
index 3ba01f6..c97b279 100644
 a/contrib/pageinspect/expected/hash.out
+++ b/contrib/pageinspect/expected/hash.out
@@ 51,13 +51,13 @@ bsize  8152
bmsize  4096
bmshift  15
maxbucket  3
highmask  7
lowmask  3
ovflpoint  2
+highmask  3
+lowmask  1
+ovflpoint  3
firstfree  0
nmaps  1
procid  450
spares  {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
+spares  {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
mapp  {5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
SELECT magic, version, ntuples, bsize, bmsize, bmshift, maxbucket, highmask,
diff git a/src/backend/access/hash/README b/src/backend/access/hash/README
index 1541438..8789805 100644
 a/src/backend/access/hash/README
+++ b/src/backend/access/hash/README
@@ 58,35 +58,50 @@ rules to support a variable number of overflow pages while not having to
move primary bucket pages around after they are created.
Primary bucket pages (henceforth just "bucket pages") are allocated in
powerof2 groups, called "split points" in the code. Buckets 0 and 1
are created when the index is initialized. At the first split, buckets 2
and 3 are allocated; when bucket 4 is needed, buckets 47 are allocated;
when bucket 8 is needed, buckets 815 are allocated; etc. All the bucket
pages of a powerof2 group appear consecutively in the index. This
addressing scheme allows the physical location of a bucket page to be
computed from the bucket number relatively easily, using only a small
amount of control information. We take the log2() of the bucket number
to determine which split point S the bucket belongs to, and then simply
add "hashm_spares[S] + 1" (where hashm_spares[] is an array stored in the
metapage) to compute the physical address. hashm_spares[S] can be
interpreted as the total number of overflow pages that have been allocated
before the bucket pages of splitpoint S. hashm_spares[0] is always 0,
so that buckets 0 and 1 (which belong to splitpoint 0) always appear at
block numbers 1 and 2, just after the meta page. We always have
+powerof2 groups, called "split points" in the code. That means on every new
+split points we double the existing number of buckets. And, it seems bad to
+allocate huge chunks of bucket pages all at once and we take ages to consume it.
+To avoid this exponential growth of index size, we did a trick to breakup
+allocation of buckets at splitpoint into 4 equal phases. If 2^x is the total
+buckets need to be allocated at a splitpoint (from now on we shall call this
+as splitpoint group), then we allocate 1/4th (2^(x  2)) of total buckets at
+each phase of splitpoint group. Next quarter of allocation will only happen if
+buckets of previous phase has been already consumed. Since for buckets number
+< 4 we cannot further divide it in to multiple phases, the first splitpoint
+group 0's allocation is done as follows {1, 1, 1, 1} = 4 buckets in total, the
+numbers in curly braces indicate number of buckets allocated within each phase
+of splitpoint group 0. In next splitpoint group 1 the allocation phases will
+be as follow {1, 1, 1, 1} = 8 buckets in total. And, for splitpoint group 2
+and 3 allocation phase will be {2, 2, 2, 2} = 16 buckets in total and
+{4, 4, 4, 4} = 32 buckets in total. We can see that at each splitpoint group
+we double the total number of buckets from previous group but in a incremental
+phase. The bucket pages allocated within one phase of a splitpoint group will
+appear consecutively in the index. This addressing scheme allows the physical
+location of a bucket page to be computed from the bucket number relatively
+easily, using only a small amount of control information. If we look at the
+function _hash_spareindex for a given bucket number we first compute splitpoint
+group it belongs to and then the phase with in it to which the bucket belongs
+to. Adding them we get the global splitpoint phase number S to which the
+bucket belongs and then simply add "hashm_spares[S] + 1" (where hashm_spares[] is
+an array stored in the metapage) with given bucket number to compute its
+physical address. The hashm_spares[S] can be interpreted as the total number
+of overflow pages that have been allocated before the bucket pages of
+splitpoint phase S. The hashm_spares[0] is always 0, so that buckets 0 and 1
+(which belong to splitpoint group 0's phase 1 and phase 2 respectively) always
+appear at block numbers 1 and 2, just after the meta page. We always have
hashm_spares[N] <= hashm_spares[N+1], since the latter count includes the
former. The difference between the two represents the number of overflow
pages appearing between the bucket page groups of splitpoints N and N+1.

+former. The difference between the two represents the number of overflow
+pages appearing between the bucket page groups of splitpoints phase N and N+1.
(Note: the above describes what happens when filling an initially minimally
sized hash index. In practice, we try to estimate the required index size
and allocate a suitable number of splitpoints immediately, to avoid
+and allocate a suitable number of splitpoints phases immediately, to avoid
expensive resplitting during initial index build.)
When S splitpoints exist altogether, the array entries hashm_spares[0]
through hashm_spares[S] are valid; hashm_spares[S] records the current
total number of overflow pages. New overflow pages are created as needed
at the end of the index, and recorded by incrementing hashm_spares[S].
When it is time to create a new splitpoint's worth of bucket pages, we
+When it is time to create a new splitpoint phase's worth of bucket pages, we
copy hashm_spares[S] into hashm_spares[S+1] and increment S (which is
stored in the hashm_ovflpoint field of the meta page). This has the
effect of reserving the correct number of bucket pages at the end of the
@@ 101,7 +116,7 @@ We have to allow the case "greater than" because it's possible that during
an index extension we crash after allocating filesystem space and before
updating the metapage. Note that on filesystems that allow "holes" in
files, it's entirely likely that pages before the logical EOF are not yet
allocated: when we allocate a new splitpoint's worth of bucket pages, we
+allocated: when we allocate a new splitpoint phase's worth of bucket pages, we
physically zero the last such page to force the EOF up, and the first such
page will be used immediately, but the intervening pages are not written
until needed.
diff git a/src/backend/access/hash/hashovfl.c b/src/backend/access/hash/hashovfl.c
index a3cae21..fb8ea49 100644
 a/src/backend/access/hash/hashovfl.c
+++ b/src/backend/access/hash/hashovfl.c
@@ 49,7 +49,7 @@ bitno_to_blkno(HashMetaPage metap, uint32 ovflbitnum)
* Convert to absolute page number by adding the number of bucket pages
* that exist before this split point.
*/
 return (BlockNumber) ((1 << i) + ovflbitnum);
+ return (BlockNumber) (_hash_get_totalbuckets(i) + ovflbitnum);
}
/*
@@ 67,9 +67,9 @@ _hash_ovflblkno_to_bitno(HashMetaPage metap, BlockNumber ovflblkno)
/* Determine the split number containing this page */
for (i = 1; i <= splitnum; i++)
{
 if (ovflblkno <= (BlockNumber) (1 << i))
+ if (ovflblkno <= (BlockNumber) _hash_get_totalbuckets(i))
break; /* oops */
 bitnum = ovflblkno  (1 << i);
+ bitnum = ovflblkno  _hash_get_totalbuckets(i);
/*
* bitnum has to be greater than number of overflow page added in
diff git a/src/backend/access/hash/hashpage.c b/src/backend/access/hash/hashpage.c
index 622cc4b..bd16e56 100644
 a/src/backend/access/hash/hashpage.c
+++ b/src/backend/access/hash/hashpage.c
@@ 502,14 +502,15 @@ _hash_init_metabuffer(Buffer buf, double num_tuples, RegProcedure procid,
Page page;
double dnumbuckets;
uint32 num_buckets;
 uint32 log2_num_buckets;
+ uint32 spare_index;
uint32 i;
/*
* Choose the number of initial bucket pages to match the fill factor
 * given the estimated number of tuples. We round up the result to the
 * next power of 2, however, and always force at least 2 bucket pages. The
 * upper limit is determined by considerations explained in
+ * given the estimated number of tuples. We round up the result to total
+ * the number of buckets which has to be allocated before using its
+ * _hashm_spares index slot, however, and always force at least 2 bucket
+ * pages. The upper limit is determined by considerations explained in
* _hash_expandtable().
*/
dnumbuckets = num_tuples / ffactor;
@@ 518,11 +519,10 @@ _hash_init_metabuffer(Buffer buf, double num_tuples, RegProcedure procid,
else if (dnumbuckets >= (double) 0x40000000)
num_buckets = 0x40000000;
else
 num_buckets = ((uint32) 1) << _hash_log2((uint32) dnumbuckets);
+ num_buckets = _hash_get_totalbuckets(_hash_spareindex(dnumbuckets));
 log2_num_buckets = _hash_log2(num_buckets);
 Assert(num_buckets == (((uint32) 1) << log2_num_buckets));
 Assert(log2_num_buckets < HASH_MAX_SPLITPOINTS);
+ spare_index = _hash_spareindex(num_buckets);
+ Assert(spare_index < HASH_MAX_SPLITPOINTS);
page = BufferGetPage(buf);
if (initpage)
@@ 563,18 +563,20 @@ _hash_init_metabuffer(Buffer buf, double num_tuples, RegProcedure procid,
/*
* We initialize the index with N buckets, 0 .. N1, occupying physical
 * blocks 1 to N. The first freespace bitmap page is in block N+1. Since
 * N is a power of 2, we can set the masks this way:
+ * blocks 1 to N. The first freespace bitmap page is in block N+1.
*/
 metap>hashm_maxbucket = metap>hashm_lowmask = num_buckets  1;
 metap>hashm_highmask = (num_buckets << 1)  1;
+ metap>hashm_maxbucket = num_buckets  1;
+
+ /* set hishmask, which should be sufficient to cover num_buckets. */
+ metap>hashm_highmask = (1 << (_hash_log2(num_buckets)))  1;
+ metap>hashm_lowmask = (metap>hashm_highmask >> 1);
MemSet(metap>hashm_spares, 0, sizeof(metap>hashm_spares));
MemSet(metap>hashm_mapp, 0, sizeof(metap>hashm_mapp));
/* Set up mapping for one spare page after the initial splitpoints */
 metap>hashm_spares[log2_num_buckets] = 1;
 metap>hashm_ovflpoint = log2_num_buckets;
+ metap>hashm_spares[spare_index] = 1;
+ metap>hashm_ovflpoint = spare_index;
metap>hashm_firstfree = 0;
/*
@@ 773,25 +775,40 @@ restart_expand:
start_nblkno = BUCKET_TO_BLKNO(metap, new_bucket);
/*
 * If the split point is increasing (hashm_maxbucket's log base 2
 * increases), we need to allocate a new batch of bucket pages.
+ * If the split point is increasing we need to allocate a new batch of
+ * bucket pages.
*/
 spare_ndx = _hash_log2(new_bucket + 1);
+ spare_ndx = _hash_spareindex(new_bucket + 1);
if (spare_ndx > metap>hashm_ovflpoint)
{
+ uint32 buckets_toadd = 0;
+ uint32 splitpoint_group = 0;
+
Assert(spare_ndx == metap>hashm_ovflpoint + 1);
/*
 * The number of buckets in the new splitpoint is equal to the total
 * number already in existence, i.e. new_bucket. Currently this maps
 * onetoone to blocks required, but someday we may need a more
 * complicated calculation here. We treat allocation of buckets as a
 * separate WALlogged action. Even if we fail after this operation,
+ * The number of buckets in the new splitpoint group is equal to the
+ * total number already in existence, i.e. new_bucket. But we do not
+ * allocate them at once. Each splitpoint group will have 4 slots, we
+ * distribute the buckets equally among them. So we allocate only one
+ * fourth of total buckets in new splitpoint group at a time to consume
+ * one phase after another. We treat allocation of buckets as a
+ * separate WALlogged action. Even if we fail after this operation,
* won't leak bucket pages; rather, the next split will consume this
* space. In any case, even without failure we don't use all the space
* in one split operation.
*/
 if (!_hash_alloc_buckets(rel, start_nblkno, new_bucket))
+ splitpoint_group = (spare_ndx >> 2);
+
+ /*
+ * Each phase in the splitpoint_group will allocate
+ * 2^(splitpoint_group  1) buckets if we divide buckets among 4
+ * slots. The 0th group is a special case where we allocate 1 bucket
+ * per slot as we cannot reduce it any further. See README for more
+ * details.
+ */
+ buckets_toadd = (splitpoint_group) ? (1 << (splitpoint_group  1)) : 1;
+ if (!_hash_alloc_buckets(rel, start_nblkno, buckets_toadd))
{
/* can't split due to BlockNumber overflow */
_hash_relbuf(rel, buf_oblkno);
@@ 836,10 +853,9 @@ restart_expand:
}
/*
 * If the split point is increasing (hashm_maxbucket's log base 2
 * increases), we need to adjust the hashm_spares[] array and
 * hashm_ovflpoint so that future overflow pages will be created beyond
 * this new batch of bucket pages.
+ * If the split point is increasing we need to adjust the hashm_spares[]
+ * array and hashm_ovflpoint so that future overflow pages will be created
+ * beyond this new batch of bucket pages.
*/
if (spare_ndx > metap>hashm_ovflpoint)
{
diff git a/src/backend/access/hash/hashsort.c b/src/backend/access/hash/hashsort.c
index 0e0f393..8aa8769 100644
 a/src/backend/access/hash/hashsort.c
+++ b/src/backend/access/hash/hashsort.c
@@ 56,9 +56,8 @@ _h_spoolinit(Relation heap, Relation index, uint32 num_buckets)
* num_buckets buckets in the index, the appropriate mask can be computed
* as follows.
*
 * Note: at present, the passedin num_buckets is always a power of 2, so
 * we could just compute num_buckets  1. We prefer not to assume that
 * here, though.
+ * NOTE : This hash_mask calculation should be in sync with similar
+ * calculation in _hash_init_metabuffer.
*/
hspool>hash_mask = (((uint32) 1) << _hash_log2(num_buckets))  1;
diff git a/src/backend/access/hash/hashutil.c b/src/backend/access/hash/hashutil.c
index 2e99719..6f1943b 100644
 a/src/backend/access/hash/hashutil.c
+++ b/src/backend/access/hash/hashutil.c
@@ 149,6 +149,87 @@ _hash_log2(uint32 num)
return i;
}
+#define SPLITPOINT_PHASES_PER_GRP 4
+#define SPLITPOINT_PHASE_MASK (SPLITPOINT_PHASES_PER_GRP  1)
+
+#define TOTAL_SPLITPOINT_PHASES_BEFORE_GROUP(sp_g) (sp_g > 0 ? (sp_g << 2) : 0)
+
+/*
+ * This is just a trick to save a division operation. If you look into the
+ * bitmap of 0based bucket_num 2nd and 3rd most significant bit will indicate
+ * which phase of allocation the bucket_num belogs to with in the group. This
+ * is because at every splitpoint group we allocate 2^x buckets and we have
+ * divided the allocation process into 4 equal phases. This macro returns value
+ * from 0 to 3.
+ */
+#define SPLITPOINT_PHASES_WITHIN_GROUP(sp_g, bucket_num) \
+ ((sp_g > 0) ? (((bucket_num) >> (sp_g  1)) & SPLITPOINT_PHASE_MASK) : \
+ (bucket_num))
+
+/*
+ * At splitpoint group 0 we have 2^(0 + 2) = 4 buckets, then at splitpoint
+ * group 1 we have 2^(1 + 2) = 8 total buckets. As the doubling continues at
+ * splitpoint group "x" we will have 2^(x + 2) total buckets. Total buckets
+ * before x splitpoint group will be 2^(x + 1). At each phase of allocation
+ * within splitpoint group we add 2^(x  1) buckets, as we have to divide the
+ * task of allocation of 2^(x + 1) buckets among 4 phases.
+ */
+#define BUCKETS_BEFORE_SP_GRP(sp_g) ((sp_g == 0) ? 0 : (1 << (sp_g + 1)))
+#define BUCKETS_WITHIN_SP_GRP(sp_g, nphase) \
+ ((nphase) * ((sp_g == 0) ? 1 : (1 << (sp_g  1))))
+
+/*
+ * _hash_spareindex  returns spare index / global splitpoint phase of the
+ * bucket
+ */
+uint32
+_hash_spareindex(uint32 num_bucket)
+{
+ uint32 splitpoint_group;
+
+ /*
+ * The first 4 bucket belongs to first splitpoint group 0. And since group
+ * 0 have 4 = 2^2 buckets, we double them in group 1. So total buckets
+ * after group 1 is 8 = 2^3. Then again at group 2 we add another 2^3
+ * buckets to double the total number of buckets, which will become 2^4. I
+ * think by this time we can see a pattern which say if num_bucket > 4
+ * splitpoint group = log2(num_bucket)  2
+ */
+ if (num_bucket <= 4)
+ splitpoint_group = 0; /* converted to base 0. */
+ else
+ splitpoint_group = _hash_log2(num_bucket)  2;
+
+ return TOTAL_SPLITPOINT_PHASES_BEFORE_GROUP(splitpoint_group) +
+ SPLITPOINT_PHASES_WITHIN_GROUP(splitpoint_group,
+ num_bucket  1); /* make it 0based */
+}
+
+/*
+ * _hash_get_totalbuckets  returns total number of buckets allocated till
+ * the given splitpoint phase.
+ */
+uint32
+_hash_get_totalbuckets(uint32 splitpoint_phase)
+{
+ uint32 splitpoint_group;
+
+ /*
+ * Every 4 consecutive phases makes one group and group's are numbered
+ * from 0
+ */
+ splitpoint_group = (splitpoint_phase / SPLITPOINT_PHASES_PER_GRP);
+
+ /*
+ * total_buckets = total number of buckets before its splitpoint group +
+ * total buckets within its splitpoint group until given splitpoint_phase.
+ */
+ return BUCKETS_BEFORE_SP_GRP(splitpoint_group) +
+ BUCKETS_WITHIN_SP_GRP(splitpoint_group,
+ (splitpoint_phase % SPLITPOINT_PHASES_PER_GRP)
+ + 1);
+}
+
/*
* _hash_checkpage  sanity checks on the format of all hash pages
*
diff git a/src/include/access/hash.h b/src/include/access/hash.h
index eb1df57..ac502ef 100644
 a/src/include/access/hash.h
+++ b/src/include/access/hash.h
@@ 36,7 +36,7 @@ typedef uint32 Bucket;
#define InvalidBucket ((Bucket) 0xFFFFFFFF)
#define BUCKET_TO_BLKNO(metap,B) \
 ((BlockNumber) ((B) + ((B) ? (metap)>hashm_spares[_hash_log2((B)+1)1] : 0)) + 1)
+ ((BlockNumber) ((B) + ((B) ? (metap)>hashm_spares[_hash_spareindex((B)+1)1] : 0)) + 1)
/*
* Special space for hash index pages.
@@ 180,7 +180,7 @@ typedef HashScanOpaqueData *HashScanOpaque;
* needing to fit into the metapage. (With 8K block size, 128 bitmaps
* limit us to 64 GB of overflow space...)
*/
#define HASH_MAX_SPLITPOINTS 32
+#define HASH_MAX_SPLITPOINTS 128
#define HASH_MAX_BITMAPS 128
typedef struct HashMetaPageData
@@ 382,6 +382,8 @@ extern uint32 _hash_datum2hashkey_type(Relation rel, Datum key, Oid keytype);
extern Bucket _hash_hashkey2bucket(uint32 hashkey, uint32 maxbucket,
uint32 highmask, uint32 lowmask);
extern uint32 _hash_log2(uint32 num);
+extern uint32 _hash_spareindex(uint32 num_bucket);
+extern uint32 _hash_get_totalbuckets(uint32 splitpoint_phase);
extern void _hash_checkpage(Relation rel, Buffer buf, int flags);
extern uint32 _hash_get_indextuple_hashkey(IndexTuple itup);
extern bool _hash_convert_tuple(Relation index,