23 #include "catalog/pg_am_d.h"
37 #define InvalidBucket ((Bucket) 0xFFFFFFFF)
39 #define BUCKET_TO_BLKNO(metap,B) \
40 ((BlockNumber) ((B) + ((B) ? (metap)->hashm_spares[_hash_spareindex((B)+1)-1] : 0)) + 1)
53 #define LH_UNUSED_PAGE (0)
54 #define LH_OVERFLOW_PAGE (1 << 0)
55 #define LH_BUCKET_PAGE (1 << 1)
56 #define LH_BITMAP_PAGE (1 << 2)
57 #define LH_META_PAGE (1 << 3)
58 #define LH_BUCKET_BEING_POPULATED (1 << 4)
59 #define LH_BUCKET_BEING_SPLIT (1 << 5)
60 #define LH_BUCKET_NEEDS_SPLIT_CLEANUP (1 << 6)
61 #define LH_PAGE_HAS_DEAD_TUPLES (1 << 7)
63 #define LH_PAGE_TYPE \
64 (LH_OVERFLOW_PAGE | LH_BUCKET_PAGE | LH_BITMAP_PAGE | LH_META_PAGE)
88 #define HashPageGetOpaque(page) ((HashPageOpaque) PageGetSpecialPointer(page))
90 #define H_NEEDS_SPLIT_CLEANUP(opaque) (((opaque)->hasho_flag & LH_BUCKET_NEEDS_SPLIT_CLEANUP) != 0)
91 #define H_BUCKET_BEING_SPLIT(opaque) (((opaque)->hasho_flag & LH_BUCKET_BEING_SPLIT) != 0)
92 #define H_BUCKET_BEING_POPULATED(opaque) (((opaque)->hasho_flag & LH_BUCKET_BEING_POPULATED) != 0)
93 #define H_HAS_DEAD_TUPLES(opaque) (((opaque)->hasho_flag & LH_PAGE_HAS_DEAD_TUPLES) != 0)
101 #define HASHO_PAGE_ID 0xFF80
130 #define HashScanPosIsPinned(scanpos) \
132 AssertMacro(BlockNumberIsValid((scanpos).currPage) || \
133 !BufferIsValid((scanpos).buf)), \
134 BufferIsValid((scanpos).buf) \
137 #define HashScanPosIsValid(scanpos) \
139 AssertMacro(BlockNumberIsValid((scanpos).currPage) || \
140 !BufferIsValid((scanpos).buf)), \
141 BlockNumberIsValid((scanpos).currPage) \
144 #define HashScanPosInvalidate(scanpos) \
146 (scanpos).buf = InvalidBuffer; \
147 (scanpos).currPage = InvalidBlockNumber; \
148 (scanpos).nextPage = InvalidBlockNumber; \
149 (scanpos).prevPage = InvalidBlockNumber; \
150 (scanpos).firstItem = 0; \
151 (scanpos).lastItem = 0; \
152 (scanpos).itemIndex = 0; \
198 #define HASH_METAPAGE 0
200 #define HASH_MAGIC 0x6440640
201 #define HASH_VERSION 4
230 #define HASH_MAX_BITMAPS Min(BLCKSZ / 8, 1024)
232 #define HASH_SPLITPOINT_PHASE_BITS 2
233 #define HASH_SPLITPOINT_PHASES_PER_GRP (1 << HASH_SPLITPOINT_PHASE_BITS)
234 #define HASH_SPLITPOINT_PHASE_MASK (HASH_SPLITPOINT_PHASES_PER_GRP - 1)
235 #define HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE 10
238 #define HASH_MAX_SPLITPOINT_GROUP 32
239 #define HASH_MAX_SPLITPOINTS \
240 (((HASH_MAX_SPLITPOINT_GROUP - HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE) * \
241 HASH_SPLITPOINT_PHASES_PER_GRP) + \
242 HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE)
275 #define HashGetFillFactor(relation) \
276 (AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
277 relation->rd_rel->relam == HASH_AM_OID), \
278 (relation)->rd_options ? \
279 ((HashOptions *) (relation)->rd_options)->fillfactor : \
280 HASH_DEFAULT_FILLFACTOR)
281 #define HashGetTargetPageUsage(relation) \
282 (BLCKSZ * HashGetFillFactor(relation) / 100)
287 #define HashMaxItemSize(page) \
288 MAXALIGN_DOWN(PageGetPageSize(page) - \
289 SizeOfPageHeaderData - \
290 sizeof(ItemIdData) - \
291 MAXALIGN(sizeof(HashPageOpaqueData)))
293 #define INDEX_MOVED_BY_SPLIT_MASK INDEX_AM_RESERVED_BIT
295 #define HASH_MIN_FILLFACTOR 10
296 #define HASH_DEFAULT_FILLFACTOR 75
301 #define BYTE_TO_BIT 3
302 #define ALL_SET ((uint32) ~0)
311 #define BMPGSZ_BYTE(metap) ((metap)->hashm_bmsize)
312 #define BMPGSZ_BIT(metap) ((metap)->hashm_bmsize << BYTE_TO_BIT)
313 #define BMPG_SHIFT(metap) ((metap)->hashm_bmshift)
314 #define BMPG_MASK(metap) (BMPGSZ_BIT(metap) - 1)
316 #define HashPageGetBitmap(page) \
317 ((uint32 *) PageGetContents(page))
319 #define HashGetMaxBitmapSize(page) \
320 (PageGetPageSize((Page) page) - \
321 (MAXALIGN(SizeOfPageHeaderData) + MAXALIGN(sizeof(HashPageOpaqueData))))
323 #define HashPageGetMeta(page) \
324 ((HashMetaPage) PageGetContents(page))
329 #define BITS_PER_MAP 32
332 #define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))
333 #define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))
334 #define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))
339 #define HASH_READ BUFFER_LOCK_SHARE
340 #define HASH_WRITE BUFFER_LOCK_EXCLUSIVE
341 #define HASH_NOLOCK (-1)
355 #define HASHSTANDARD_PROC 1
356 #define HASHEXTENDED_PROC 2
357 #define HASHOPTIONS_PROC 3
375 ScanKey orderbys,
int norderbys);
380 void *callback_state);
468 Datum *user_values,
bool *user_isnull,
469 Datum *index_values,
bool *index_isnull);
483 double *tuples_removed,
double *num_index_tuples,
static Datum values[MAXATTR]
bool(* IndexBulkDeleteCallback)(ItemPointer itemptr, void *state)
void _h_spool(HSpool *hspool, ItemPointer self, const Datum *values, const bool *isnull)
void _hash_doinsert(Relation rel, IndexTuple itup, Relation heapRel, bool sorted)
Buffer _hash_getinitbuf(Relation rel, BlockNumber blkno)
IndexBulkDeleteResult * hashvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
bool _hash_first(IndexScanDesc scan, ScanDirection dir)
HashMetaPage _hash_getcachedmetap(Relation rel, Buffer *metabuf, bool force_refresh)
HashPageOpaqueData * HashPageOpaque
void _hash_initbuf(Buffer buf, uint32 max_bucket, uint32 num_bucket, uint32 flag, bool initpage)
void _hash_squeezebucket(Relation rel, Bucket bucket, BlockNumber bucket_blkno, Buffer bucket_buf, BufferAccessStrategy bstrategy)
uint32 _hash_spareindex(uint32 num_bucket)
void _hash_relbuf(Relation rel, Buffer buf)
void _h_indexbuild(HSpool *hspool, Relation heapRel)
void hashadjustmembers(Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
BlockNumber _hash_get_newblock_from_oldbucket(Relation rel, Bucket old_bucket)
IndexBuildResult * hashbuild(Relation heap, Relation index, struct IndexInfo *indexInfo)
IndexBulkDeleteResult * hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
uint32 _hash_get_totalbuckets(uint32 splitpoint_phase)
Buffer _hash_getbuf_with_condlock_cleanup(Relation rel, BlockNumber blkno, int flags)
HSpool * _h_spoolinit(Relation heap, Relation index, uint32 num_buckets)
bool hashgettuple(IndexScanDesc scan, ScanDirection dir)
struct HashScanPosData HashScanPosData
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
bool hashvalidate(Oid opclassoid)
bool _hash_checkqual(IndexScanDesc scan, IndexTuple itup)
struct HashScanPosItem HashScanPosItem
OffsetNumber _hash_pgaddtup(Relation rel, Buffer buf, Size itemsize, IndexTuple itup, bool appendtup)
#define HASH_MAX_SPLITPOINTS
OffsetNumber _hash_binsearch(Page page, uint32 hash_value)
uint32 _hash_datum2hashkey(Relation rel, Datum key)
Bucket _hash_hashkey2bucket(uint32 hashkey, uint32 maxbucket, uint32 highmask, uint32 lowmask)
OffsetNumber _hash_binsearch_last(Page page, uint32 hash_value)
void _hash_checkpage(Relation rel, Buffer buf, int flags)
void _hash_pageinit(Page page, Size size)
Bucket _hash_get_newbucket_from_oldbucket(Relation rel, Bucket old_bucket, uint32 lowmask, uint32 maxbucket)
uint32 _hash_init(Relation rel, double num_tuples, ForkNumber forkNum)
bool hashinsert(Relation rel, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, struct IndexInfo *indexInfo)
bool _hash_next(IndexScanDesc scan, ScanDirection dir)
void hashbuildempty(Relation index)
IndexScanDesc hashbeginscan(Relation rel, int nkeys, int norderbys)
void _hash_pgaddmultitup(Relation rel, Buffer buf, IndexTuple *itups, OffsetNumber *itup_offsets, uint16 nitups)
HashMetaPageData * HashMetaPage
uint32 _hash_ovflblkno_to_bitno(HashMetaPage metap, BlockNumber ovflblkno)
BlockNumber _hash_get_oldblock_from_newbucket(Relation rel, Bucket new_bucket)
void _hash_dropbuf(Relation rel, Buffer buf)
BlockNumber _hash_freeovflpage(Relation rel, Buffer bucketbuf, Buffer ovflbuf, Buffer wbuf, IndexTuple *itups, OffsetNumber *itup_offsets, Size *tups_size, uint16 nitups, BufferAccessStrategy bstrategy)
void _hash_dropscanbuf(Relation rel, HashScanOpaque so)
void _hash_initbitmapbuffer(Buffer buf, uint16 bmsize, bool initpage)
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
uint32 _hash_datum2hashkey_type(Relation rel, Datum key, Oid keytype)
struct HashScanOpaqueData HashScanOpaqueData
struct HashMetaPageData HashMetaPageData
void _hash_finish_split(Relation rel, Buffer metabuf, Buffer obuf, Bucket obucket, uint32 maxbucket, uint32 highmask, uint32 lowmask)
Buffer _hash_getbucketbuf_from_hashkey(Relation rel, uint32 hashkey, int access, HashMetaPage *cachedmetap)
void _hash_kill_items(IndexScanDesc scan)
void _hash_init_metabuffer(Buffer buf, double num_tuples, RegProcedure procid, uint16 ffactor, bool initpage)
void hashrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
bytea * hashoptions(Datum reloptions, bool validate)
void hashendscan(IndexScanDesc scan)
HashScanOpaqueData * HashScanOpaque
Buffer _hash_addovflpage(Relation rel, Buffer metabuf, Buffer buf, bool retain_pin)
Buffer _hash_getbuf_with_strategy(Relation rel, BlockNumber blkno, int access, int flags, BufferAccessStrategy bstrategy)
Buffer _hash_getnewbuf(Relation rel, BlockNumber blkno, ForkNumber forkNum)
int64 hashgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
void _hash_expandtable(Relation rel, Buffer metabuf)
void _h_spooldestroy(HSpool *hspool)
struct HashPageOpaqueData HashPageOpaqueData
bool _hash_convert_tuple(Relation index, Datum *user_values, bool *user_isnull, Datum *index_values, bool *index_isnull)
struct HashOptions HashOptions
void hashbucketcleanup(Relation rel, Bucket cur_bucket, Buffer bucket_buf, BlockNumber bucket_blkno, BufferAccessStrategy bstrategy, uint32 maxbucket, uint32 highmask, uint32 lowmask, double *tuples_removed, double *num_index_tuples, bool split_cleanup, IndexBulkDeleteCallback callback, void *callback_state)
#define MaxIndexTuplesPerPage
static const struct fns functions
static pg_noinline void Size size
BlockNumber hashm_mapp[HASH_MAX_BITMAPS]
RegProcedure hashm_procid
uint32 hashm_spares[HASH_MAX_SPLITPOINTS]
BlockNumber hasho_nextblkno
BlockNumber hasho_prevblkno
bool hashso_buc_populated
Buffer hashso_split_bucket_buf
HashScanPosItem items[MaxIndexTuplesPerPage]
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)