hash.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*-------------------------------------------------------------------------
  2. *
  3. * hash.h
  4. * header file for postgres hash access method implementation
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/access/hash.h
  11. *
  12. * NOTES
  13. * modeled after Margo Seltzer's hash implementation for unix.
  14. *
  15. *-------------------------------------------------------------------------
  16. */
  17. #ifndef HASH_H
  18. #define HASH_H
  19. #include "access/amapi.h"
  20. #include "access/itup.h"
  21. #include "access/sdir.h"
  22. #include "access/xlogreader.h"
  23. #include "fmgr.h"
  24. #include "lib/stringinfo.h"
  25. #include "storage/bufmgr.h"
  26. #include "storage/lockdefs.h"
  27. #include "utils/relcache.h"
  28. /*
  29. * Mapping from hash bucket number to physical block number of bucket's
  30. * starting page. Beware of multiple evaluations of argument!
  31. */
  32. typedef uint32 Bucket;
  33. #define BUCKET_TO_BLKNO(metap,B) \
  34. ((BlockNumber) ((B) + ((B) ? (metap)->hashm_spares[_hash_log2((B)+1)-1] : 0)) + 1)
  35. /*
  36. * Special space for hash index pages.
  37. *
  38. * hasho_flag tells us which type of page we're looking at. For
  39. * example, knowing overflow pages from bucket pages is necessary
  40. * information when you're deleting tuples from a page. If all the
  41. * tuples are deleted from an overflow page, the overflow is made
  42. * available to other buckets by calling _hash_freeovflpage(). If all
  43. * the tuples are deleted from a bucket page, no additional action is
  44. * necessary.
  45. */
  46. #define LH_UNUSED_PAGE (0)
  47. #define LH_OVERFLOW_PAGE (1 << 0)
  48. #define LH_BUCKET_PAGE (1 << 1)
  49. #define LH_BITMAP_PAGE (1 << 2)
  50. #define LH_META_PAGE (1 << 3)
  51. typedef struct HashPageOpaqueData
  52. {
  53. BlockNumber hasho_prevblkno; /* previous ovfl (or bucket) blkno */
  54. BlockNumber hasho_nextblkno; /* next ovfl blkno */
  55. Bucket hasho_bucket; /* bucket number this pg belongs to */
  56. uint16 hasho_flag; /* page type code, see above */
  57. uint16 hasho_page_id; /* for identification of hash indexes */
  58. } HashPageOpaqueData;
  59. typedef HashPageOpaqueData *HashPageOpaque;
  60. /*
  61. * The page ID is for the convenience of pg_filedump and similar utilities,
  62. * which otherwise would have a hard time telling pages of different index
  63. * types apart. It should be the last 2 bytes on the page. This is more or
  64. * less "free" due to alignment considerations.
  65. */
  66. #define HASHO_PAGE_ID 0xFF80
  67. /*
  68. * HashScanOpaqueData is private state for a hash index scan.
  69. */
  70. typedef struct HashScanOpaqueData
  71. {
  72. /* Hash value of the scan key, ie, the hash key we seek */
  73. uint32 hashso_sk_hash;
  74. /*
  75. * By definition, a hash scan should be examining only one bucket. We
  76. * record the bucket number here as soon as it is known.
  77. */
  78. Bucket hashso_bucket;
  79. bool hashso_bucket_valid;
  80. /*
  81. * If we have a share lock on the bucket, we record it here. When
  82. * hashso_bucket_blkno is zero, we have no such lock.
  83. */
  84. BlockNumber hashso_bucket_blkno;
  85. /*
  86. * We also want to remember which buffer we're currently examining in the
  87. * scan. We keep the buffer pinned (but not locked) across hashgettuple
  88. * calls, in order to avoid doing a ReadBuffer() for every tuple in the
  89. * index.
  90. */
  91. Buffer hashso_curbuf;
  92. /* Current position of the scan, as an index TID */
  93. ItemPointerData hashso_curpos;
  94. /* Current position of the scan, as a heap TID */
  95. ItemPointerData hashso_heappos;
  96. } HashScanOpaqueData;
  97. typedef HashScanOpaqueData *HashScanOpaque;
  98. /*
  99. * Definitions for metapage.
  100. */
  101. #define HASH_METAPAGE 0 /* metapage is always block 0 */
  102. #define HASH_MAGIC 0x6440640
  103. #define HASH_VERSION 2 /* 2 signifies only hash key value is stored */
  104. /*
  105. * Spares[] holds the number of overflow pages currently allocated at or
  106. * before a certain splitpoint. For example, if spares[3] = 7 then there are
  107. * 7 ovflpages before splitpoint 3 (compare BUCKET_TO_BLKNO macro). The
  108. * value in spares[ovflpoint] increases as overflow pages are added at the
  109. * end of the index. Once ovflpoint increases (ie, we have actually allocated
  110. * the bucket pages belonging to that splitpoint) the number of spares at the
  111. * prior splitpoint cannot change anymore.
  112. *
  113. * ovflpages that have been recycled for reuse can be found by looking at
  114. * bitmaps that are stored within ovflpages dedicated for the purpose.
  115. * The blknos of these bitmap pages are kept in bitmaps[]; nmaps is the
  116. * number of currently existing bitmaps.
  117. *
  118. * The limitation on the size of spares[] comes from the fact that there's
  119. * no point in having more than 2^32 buckets with only uint32 hashcodes.
  120. * There is no particular upper limit on the size of mapp[], other than
  121. * needing to fit into the metapage. (With 8K block size, 128 bitmaps
  122. * limit us to 64 Gb of overflow space...)
  123. */
  124. #define HASH_MAX_SPLITPOINTS 32
  125. #define HASH_MAX_BITMAPS 128
  126. typedef struct HashMetaPageData
  127. {
  128. uint32 hashm_magic; /* magic no. for hash tables */
  129. uint32 hashm_version; /* version ID */
  130. double hashm_ntuples; /* number of tuples stored in the table */
  131. uint16 hashm_ffactor; /* target fill factor (tuples/bucket) */
  132. uint16 hashm_bsize; /* index page size (bytes) */
  133. uint16 hashm_bmsize; /* bitmap array size (bytes) - must be a power
  134. * of 2 */
  135. uint16 hashm_bmshift; /* log2(bitmap array size in BITS) */
  136. uint32 hashm_maxbucket; /* ID of maximum bucket in use */
  137. uint32 hashm_highmask; /* mask to modulo into entire table */
  138. uint32 hashm_lowmask; /* mask to modulo into lower half of table */
  139. uint32 hashm_ovflpoint;/* splitpoint from which ovflpgs being
  140. * allocated */
  141. uint32 hashm_firstfree; /* lowest-number free ovflpage (bit#) */
  142. uint32 hashm_nmaps; /* number of bitmap pages */
  143. RegProcedure hashm_procid; /* hash procedure id from pg_proc */
  144. uint32 hashm_spares[HASH_MAX_SPLITPOINTS]; /* spare pages before
  145. * each splitpoint */
  146. BlockNumber hashm_mapp[HASH_MAX_BITMAPS]; /* blknos of ovfl bitmaps */
  147. } HashMetaPageData;
  148. typedef HashMetaPageData *HashMetaPage;
  149. /*
  150. * Maximum size of a hash index item (it's okay to have only one per page)
  151. */
  152. #define HashMaxItemSize(page) \
  153. MAXALIGN_DOWN(PageGetPageSize(page) - \
  154. SizeOfPageHeaderData - \
  155. sizeof(ItemIdData) - \
  156. MAXALIGN(sizeof(HashPageOpaqueData)))
  157. #define HASH_MIN_FILLFACTOR 10
  158. #define HASH_DEFAULT_FILLFACTOR 75
  159. /*
  160. * Constants
  161. */
  162. #define BYTE_TO_BIT 3 /* 2^3 bits/byte */
  163. #define ALL_SET ((uint32) ~0)
  164. /*
  165. * Bitmap pages do not contain tuples. They do contain the standard
  166. * page headers and trailers; however, everything in between is a
  167. * giant bit array. The number of bits that fit on a page obviously
  168. * depends on the page size and the header/trailer overhead. We require
  169. * the number of bits per page to be a power of 2.
  170. */
  171. #define BMPGSZ_BYTE(metap) ((metap)->hashm_bmsize)
  172. #define BMPGSZ_BIT(metap) ((metap)->hashm_bmsize << BYTE_TO_BIT)
  173. #define BMPG_SHIFT(metap) ((metap)->hashm_bmshift)
  174. #define BMPG_MASK(metap) (BMPGSZ_BIT(metap) - 1)
  175. #define HashPageGetBitmap(page) \
  176. ((uint32 *) PageGetContents(page))
  177. #define HashGetMaxBitmapSize(page) \
  178. (PageGetPageSize((Page) page) - \
  179. (MAXALIGN(SizeOfPageHeaderData) + MAXALIGN(sizeof(HashPageOpaqueData))))
  180. #define HashPageGetMeta(page) \
  181. ((HashMetaPage) PageGetContents(page))
  182. /*
  183. * The number of bits in an ovflpage bitmap word.
  184. */
  185. #define BITS_PER_MAP 32 /* Number of bits in uint32 */
  186. /* Given the address of the beginning of a bit map, clear/set the nth bit */
  187. #define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))
  188. #define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))
  189. #define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))
  190. /*
  191. * page-level and high-level locking modes (see README)
  192. */
  193. #define HASH_READ BUFFER_LOCK_SHARE
  194. #define HASH_WRITE BUFFER_LOCK_EXCLUSIVE
  195. #define HASH_NOLOCK (-1)
  196. #define HASH_SHARE ShareLock
  197. #define HASH_EXCLUSIVE ExclusiveLock
  198. /*
  199. * Strategy number. There's only one valid strategy for hashing: equality.
  200. */
  201. #define HTEqualStrategyNumber 1
  202. #define HTMaxStrategyNumber 1
  203. /*
  204. * When a new operator class is declared, we require that the user supply
  205. * us with an amproc procudure for hashing a key of the new type.
  206. * Since we only have one such proc in amproc, it's number 1.
  207. */
  208. #define HASHPROC 1
  209. #define HASHNProcs 1
  210. /* public routines */
  211. extern Datum hashhandler(PG_FUNCTION_ARGS);
  212. extern IndexBuildResult *hashbuild(Relation heap, Relation index,
  213. struct IndexInfo *indexInfo);
  214. extern void hashbuildempty(Relation index);
  215. extern bool hashinsert(Relation rel, Datum *values, bool *isnull,
  216. ItemPointer ht_ctid, Relation heapRel,
  217. IndexUniqueCheck checkUnique);
  218. extern bool hashgettuple(IndexScanDesc scan, ScanDirection dir);
  219. extern int64 hashgetbitmap(IndexScanDesc scan, TIDBitmap *tbm);
  220. extern IndexScanDesc hashbeginscan(Relation rel, int nkeys, int norderbys);
  221. extern void hashrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
  222. ScanKey orderbys, int norderbys);
  223. extern void hashendscan(IndexScanDesc scan);
  224. extern IndexBulkDeleteResult *hashbulkdelete(IndexVacuumInfo *info,
  225. IndexBulkDeleteResult *stats,
  226. IndexBulkDeleteCallback callback,
  227. void *callback_state);
  228. extern IndexBulkDeleteResult *hashvacuumcleanup(IndexVacuumInfo *info,
  229. IndexBulkDeleteResult *stats);
  230. extern bytea *hashoptions(Datum reloptions, bool validate);
  231. extern bool hashvalidate(Oid opclassoid);
  232. /*
  233. * Datatype-specific hash functions in hashfunc.c.
  234. *
  235. * These support both hash indexes and hash joins.
  236. *
  237. * NOTE: some of these are also used by catcache operations, without
  238. * any direct connection to hash indexes. Also, the common hash_any
  239. * routine is also used by dynahash tables.
  240. */
  241. extern Datum hashchar(PG_FUNCTION_ARGS);
  242. extern Datum hashint2(PG_FUNCTION_ARGS);
  243. extern Datum hashint4(PG_FUNCTION_ARGS);
  244. extern Datum hashint8(PG_FUNCTION_ARGS);
  245. extern Datum hashoid(PG_FUNCTION_ARGS);
  246. extern Datum hashenum(PG_FUNCTION_ARGS);
  247. extern Datum hashfloat4(PG_FUNCTION_ARGS);
  248. extern Datum hashfloat8(PG_FUNCTION_ARGS);
  249. extern Datum hashoidvector(PG_FUNCTION_ARGS);
  250. extern Datum hashint2vector(PG_FUNCTION_ARGS);
  251. extern Datum hashname(PG_FUNCTION_ARGS);
  252. extern Datum hashtext(PG_FUNCTION_ARGS);
  253. extern Datum hashvarlena(PG_FUNCTION_ARGS);
  254. extern Datum hash_any(register const unsigned char *k, register int keylen);
  255. extern Datum hash_uint32(uint32 k);
  256. /* private routines */
  257. /* hashinsert.c */
  258. extern void _hash_doinsert(Relation rel, IndexTuple itup);
  259. extern OffsetNumber _hash_pgaddtup(Relation rel, Buffer buf,
  260. Size itemsize, IndexTuple itup);
  261. /* hashovfl.c */
  262. extern Buffer _hash_addovflpage(Relation rel, Buffer metabuf, Buffer buf);
  263. extern BlockNumber _hash_freeovflpage(Relation rel, Buffer ovflbuf,
  264. BufferAccessStrategy bstrategy);
  265. extern void _hash_initbitmap(Relation rel, HashMetaPage metap,
  266. BlockNumber blkno, ForkNumber forkNum);
  267. extern void _hash_squeezebucket(Relation rel,
  268. Bucket bucket, BlockNumber bucket_blkno,
  269. BufferAccessStrategy bstrategy);
  270. /* hashpage.c */
  271. extern void _hash_getlock(Relation rel, BlockNumber whichlock, int access);
  272. extern bool _hash_try_getlock(Relation rel, BlockNumber whichlock, int access);
  273. extern void _hash_droplock(Relation rel, BlockNumber whichlock, int access);
  274. extern Buffer _hash_getbuf(Relation rel, BlockNumber blkno,
  275. int access, int flags);
  276. extern Buffer _hash_getinitbuf(Relation rel, BlockNumber blkno);
  277. extern Buffer _hash_getnewbuf(Relation rel, BlockNumber blkno,
  278. ForkNumber forkNum);
  279. extern Buffer _hash_getbuf_with_strategy(Relation rel, BlockNumber blkno,
  280. int access, int flags,
  281. BufferAccessStrategy bstrategy);
  282. extern void _hash_relbuf(Relation rel, Buffer buf);
  283. extern void _hash_dropbuf(Relation rel, Buffer buf);
  284. extern void _hash_wrtbuf(Relation rel, Buffer buf);
  285. extern void _hash_chgbufaccess(Relation rel, Buffer buf, int from_access,
  286. int to_access);
  287. extern uint32 _hash_metapinit(Relation rel, double num_tuples,
  288. ForkNumber forkNum);
  289. extern void _hash_pageinit(Page page, Size size);
  290. extern void _hash_expandtable(Relation rel, Buffer metabuf);
  291. /* hashscan.c */
  292. extern void _hash_regscan(IndexScanDesc scan);
  293. extern void _hash_dropscan(IndexScanDesc scan);
  294. extern bool _hash_has_active_scan(Relation rel, Bucket bucket);
  295. extern void ReleaseResources_hash(void);
  296. /* hashsearch.c */
  297. extern bool _hash_next(IndexScanDesc scan, ScanDirection dir);
  298. extern bool _hash_first(IndexScanDesc scan, ScanDirection dir);
  299. extern bool _hash_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir);
  300. /* hashsort.c */
  301. typedef struct HSpool HSpool; /* opaque struct in hashsort.c */
  302. extern HSpool *_h_spoolinit(Relation heap, Relation index, uint32 num_buckets);
  303. extern void _h_spooldestroy(HSpool *hspool);
  304. extern void _h_spool(HSpool *hspool, ItemPointer self,
  305. Datum *values, bool *isnull);
  306. extern void _h_indexbuild(HSpool *hspool);
  307. /* hashutil.c */
  308. extern bool _hash_checkqual(IndexScanDesc scan, IndexTuple itup);
  309. extern uint32 _hash_datum2hashkey(Relation rel, Datum key);
  310. extern uint32 _hash_datum2hashkey_type(Relation rel, Datum key, Oid keytype);
  311. extern Bucket _hash_hashkey2bucket(uint32 hashkey, uint32 maxbucket,
  312. uint32 highmask, uint32 lowmask);
  313. extern uint32 _hash_log2(uint32 num);
  314. extern void _hash_checkpage(Relation rel, Buffer buf, int flags);
  315. extern uint32 _hash_get_indextuple_hashkey(IndexTuple itup);
  316. extern bool _hash_convert_tuple(Relation index,
  317. Datum *user_values, bool *user_isnull,
  318. Datum *index_values, bool *index_isnull);
  319. extern OffsetNumber _hash_binsearch(Page page, uint32 hash_value);
  320. extern OffsetNumber _hash_binsearch_last(Page page, uint32 hash_value);
  321. /* hash.c */
  322. extern void hash_redo(XLogReaderState *record);
  323. extern void hash_desc(StringInfo buf, XLogReaderState *record);
  324. extern const char *hash_identify(uint8 info);
  325. #endif /* HASH_H */