2016-04-01 21:42:24 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* bloom.h
|
|
|
|
* Header for bloom index.
|
|
|
|
*
|
2019-01-03 01:44:25 +08:00
|
|
|
* Copyright (c) 2016-2019, PostgreSQL Global Development Group
|
2016-04-01 21:42:24 +08:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* contrib/bloom/bloom.h
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef _BLOOM_H_
|
|
|
|
#define _BLOOM_H_
|
|
|
|
|
|
|
|
#include "access/amapi.h"
|
|
|
|
#include "access/generic_xlog.h"
|
|
|
|
#include "access/itup.h"
|
|
|
|
#include "access/xlog.h"
|
|
|
|
#include "nodes/relation.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
|
|
|
|
/* Support procedures numbers */
|
|
|
|
#define BLOOM_HASH_PROC 1
|
|
|
|
#define BLOOM_NPROC 1
|
|
|
|
|
|
|
|
/* Scan strategies */
|
|
|
|
#define BLOOM_EQUAL_STRATEGY 1
|
|
|
|
#define BLOOM_NSTRATEGIES 1
|
|
|
|
|
|
|
|
/* Opaque for bloom pages */
|
|
|
|
typedef struct BloomPageOpaqueData
|
|
|
|
{
|
2016-06-10 06:02:36 +08:00
|
|
|
OffsetNumber maxoff; /* number of index tuples on page */
|
|
|
|
uint16 flags; /* see bit definitions below */
|
|
|
|
uint16 unused; /* placeholder to force maxaligning of size of
|
|
|
|
* BloomPageOpaqueData and to place
|
|
|
|
* bloom_page_id exactly at the end of page */
|
|
|
|
uint16 bloom_page_id; /* for identification of BLOOM indexes */
|
|
|
|
} BloomPageOpaqueData;
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
typedef BloomPageOpaqueData *BloomPageOpaque;
|
|
|
|
|
|
|
|
/* Bloom page flags */
|
|
|
|
#define BLOOM_META (1<<0)
|
|
|
|
#define BLOOM_DELETED (2<<0)
|
|
|
|
|
2016-04-12 23:03:01 +08:00
|
|
|
/*
|
|
|
|
* The page ID is for the convenience of pg_filedump and similar utilities,
|
|
|
|
* which otherwise would have a hard time telling pages of different index
|
|
|
|
* types apart. It should be the last 2 bytes on the page. This is more or
|
|
|
|
* less "free" due to alignment considerations.
|
|
|
|
*
|
|
|
|
* See comments above GinPageOpaqueData.
|
|
|
|
*/
|
|
|
|
#define BLOOM_PAGE_ID 0xFF83
|
|
|
|
|
2016-04-01 21:42:24 +08:00
|
|
|
/* Macros for accessing bloom page structures */
|
|
|
|
#define BloomPageGetOpaque(page) ((BloomPageOpaque) PageGetSpecialPointer(page))
|
|
|
|
#define BloomPageGetMaxOffset(page) (BloomPageGetOpaque(page)->maxoff)
|
2016-04-02 01:09:13 +08:00
|
|
|
#define BloomPageIsMeta(page) \
|
|
|
|
((BloomPageGetOpaque(page)->flags & BLOOM_META) != 0)
|
|
|
|
#define BloomPageIsDeleted(page) \
|
|
|
|
((BloomPageGetOpaque(page)->flags & BLOOM_DELETED) != 0)
|
|
|
|
#define BloomPageSetDeleted(page) \
|
|
|
|
(BloomPageGetOpaque(page)->flags |= BLOOM_DELETED)
|
|
|
|
#define BloomPageSetNonDeleted(page) \
|
|
|
|
(BloomPageGetOpaque(page)->flags &= ~BLOOM_DELETED)
|
2016-04-01 21:42:24 +08:00
|
|
|
#define BloomPageGetData(page) ((BloomTuple *)PageGetContents(page))
|
|
|
|
#define BloomPageGetTuple(state, page, offset) \
|
|
|
|
((BloomTuple *)(PageGetContents(page) \
|
|
|
|
+ (state)->sizeOfBloomTuple * ((offset) - 1)))
|
|
|
|
#define BloomPageGetNextTuple(state, tuple) \
|
|
|
|
((BloomTuple *)((Pointer)(tuple) + (state)->sizeOfBloomTuple))
|
|
|
|
|
|
|
|
/* Preserved page numbers */
|
|
|
|
#define BLOOM_METAPAGE_BLKNO (0)
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:18:54 +08:00
|
|
|
#define BLOOM_HEAD_BLKNO (1) /* first data page */
|
2016-04-01 21:42:24 +08:00
|
|
|
|
2016-04-02 18:47:04 +08:00
|
|
|
/*
|
2016-06-03 22:52:36 +08:00
|
|
|
* We store Bloom signatures as arrays of uint16 words.
|
2016-04-02 18:47:04 +08:00
|
|
|
*/
|
2016-06-03 22:52:36 +08:00
|
|
|
typedef uint16 BloomSignatureWord;
|
|
|
|
|
|
|
|
#define SIGNWORDBITS ((int) (BITS_PER_BYTE * sizeof(BloomSignatureWord)))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Default and maximum Bloom signature length in bits.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_BLOOM_LENGTH (5 * SIGNWORDBITS)
|
|
|
|
#define MAX_BLOOM_LENGTH (256 * SIGNWORDBITS)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Default and maximum signature bits generated per index key.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_BLOOM_BITS 2
|
|
|
|
#define MAX_BLOOM_BITS (MAX_BLOOM_LENGTH - 1)
|
2016-04-02 18:47:04 +08:00
|
|
|
|
2016-04-01 21:42:24 +08:00
|
|
|
/* Bloom index options */
|
|
|
|
typedef struct BloomOptions
|
|
|
|
{
|
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2016-06-03 22:52:36 +08:00
|
|
|
int bloomLength; /* length of signature in words (not bits!) */
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:18:54 +08:00
|
|
|
int bitSize[INDEX_MAX_KEYS]; /* # of bits generated for each
|
|
|
|
* index key */
|
2016-06-10 06:02:36 +08:00
|
|
|
} BloomOptions;
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FreeBlockNumberArray - array of block numbers sized so that metadata fill
|
|
|
|
* all space in metapage.
|
|
|
|
*/
|
|
|
|
typedef BlockNumber FreeBlockNumberArray[
|
|
|
|
MAXALIGN_DOWN(
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:35:54 +08:00
|
|
|
BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(BloomPageOpaqueData))
|
|
|
|
- MAXALIGN(sizeof(uint16) * 2 + sizeof(uint32) + sizeof(BloomOptions))
|
2016-04-01 21:42:24 +08:00
|
|
|
) / sizeof(BlockNumber)
|
|
|
|
];
|
|
|
|
|
|
|
|
/* Metadata of bloom index */
|
|
|
|
typedef struct BloomMetaPageData
|
|
|
|
{
|
|
|
|
uint32 magickNumber;
|
|
|
|
uint16 nStart;
|
|
|
|
uint16 nEnd;
|
|
|
|
BloomOptions opts;
|
|
|
|
FreeBlockNumberArray notFullPage;
|
2016-06-10 06:02:36 +08:00
|
|
|
} BloomMetaPageData;
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
/* Magic number to distinguish bloom pages among anothers */
|
|
|
|
#define BLOOM_MAGICK_NUMBER (0xDBAC0DED)
|
|
|
|
|
|
|
|
/* Number of blocks numbers fit in BloomMetaPageData */
|
|
|
|
#define BloomMetaBlockN (sizeof(FreeBlockNumberArray) / sizeof(BlockNumber))
|
|
|
|
|
|
|
|
#define BloomPageGetMeta(page) ((BloomMetaPageData *) PageGetContents(page))
|
|
|
|
|
|
|
|
typedef struct BloomState
|
|
|
|
{
|
|
|
|
FmgrInfo hashFn[INDEX_MAX_KEYS];
|
2016-04-04 03:16:07 +08:00
|
|
|
BloomOptions opts; /* copy of options on index's metapage */
|
2016-04-01 21:42:24 +08:00
|
|
|
int32 nColumns;
|
|
|
|
|
|
|
|
/*
|
2016-04-04 03:16:07 +08:00
|
|
|
* sizeOfBloomTuple is index-specific, and it depends on reloptions, so
|
2016-04-01 21:42:24 +08:00
|
|
|
* precompute it
|
|
|
|
*/
|
2016-04-04 02:17:20 +08:00
|
|
|
Size sizeOfBloomTuple;
|
2016-06-10 06:02:36 +08:00
|
|
|
} BloomState;
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
#define BloomPageGetFreeSpace(state, page) \
|
|
|
|
(BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \
|
|
|
|
- BloomPageGetMaxOffset(page) * (state)->sizeOfBloomTuple \
|
|
|
|
- MAXALIGN(sizeof(BloomPageOpaqueData)))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tuples are very different from all other relations
|
|
|
|
*/
|
|
|
|
typedef struct BloomTuple
|
|
|
|
{
|
|
|
|
ItemPointerData heapPtr;
|
2016-06-03 22:52:36 +08:00
|
|
|
BloomSignatureWord sign[FLEXIBLE_ARRAY_MEMBER];
|
2016-06-10 06:02:36 +08:00
|
|
|
} BloomTuple;
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
#define BLOOMTUPLEHDRSZ offsetof(BloomTuple, sign)
|
|
|
|
|
|
|
|
/* Opaque data structure for bloom index scan */
|
|
|
|
typedef struct BloomScanOpaqueData
|
|
|
|
{
|
2016-06-10 06:02:36 +08:00
|
|
|
BloomSignatureWord *sign; /* Scan signature */
|
2016-04-01 21:42:24 +08:00
|
|
|
BloomState state;
|
2016-06-10 06:02:36 +08:00
|
|
|
} BloomScanOpaqueData;
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
typedef BloomScanOpaqueData *BloomScanOpaque;
|
|
|
|
|
|
|
|
/* blutils.c */
|
|
|
|
extern void _PG_init(void);
|
2016-06-10 06:02:36 +08:00
|
|
|
extern void initBloomState(BloomState *state, Relation index);
|
2016-05-25 09:04:23 +08:00
|
|
|
extern void BloomFillMetapage(Relation index, Page metaPage);
|
2016-04-01 21:42:24 +08:00
|
|
|
extern void BloomInitMetapage(Relation index);
|
|
|
|
extern void BloomInitPage(Page page, uint16 flags);
|
|
|
|
extern Buffer BloomNewBuffer(Relation index);
|
2016-06-10 06:02:36 +08:00
|
|
|
extern void signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int attno);
|
|
|
|
extern BloomTuple *BloomFormTuple(BloomState *state, ItemPointer iptr, Datum *values, bool *isnull);
|
|
|
|
extern bool BloomPageAddItem(BloomState *state, Page page, BloomTuple *tuple);
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
/* blvalidate.c */
|
|
|
|
extern bool blvalidate(Oid opclassoid);
|
|
|
|
|
|
|
|
/* index access method interface functions */
|
|
|
|
extern bool blinsert(Relation index, Datum *values, bool *isnull,
|
|
|
|
ItemPointer ht_ctid, Relation heapRel,
|
Allow index AMs to cache data across aminsert calls within a SQL command.
It's always been possible for index AMs to cache data across successive
amgettuple calls within a single SQL command: the IndexScanDesc.opaque
field is meant for precisely that. However, no comparable facility
exists for amortizing setup work across successive aminsert calls.
This patch adds such a feature and teaches GIN, GIST, and BRIN to use it
to amortize catalog lookups they'd previously been doing on every call.
(The other standard index AMs keep everything they need in the relcache,
so there's little to improve there.)
For GIN, the overall improvement in a statement that inserts many rows
can be as much as 10%, though it seems a bit less for the other two.
In addition, this makes a really significant difference in runtime
for CLOBBER_CACHE_ALWAYS tests, since in those builds the repeated
catalog lookups are vastly more expensive.
The reason this has been hard up to now is that the aminsert function is
not passed any useful place to cache per-statement data. What I chose to
do is to add suitable fields to struct IndexInfo and pass that to aminsert.
That's not widening the index AM API very much because IndexInfo is already
within the ken of ambuild; in fact, by passing the same info to aminsert
as to ambuild, this is really removing an inconsistency in the AM API.
Discussion: https://postgr.es/m/27568.1486508680@sss.pgh.pa.us
2017-02-10 00:52:12 +08:00
|
|
|
IndexUniqueCheck checkUnique,
|
|
|
|
struct IndexInfo *indexInfo);
|
2016-04-01 21:42:24 +08:00
|
|
|
extern IndexScanDesc blbeginscan(Relation r, int nkeys, int norderbys);
|
|
|
|
extern int64 blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm);
|
|
|
|
extern void blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
|
|
|
|
ScanKey orderbys, int norderbys);
|
|
|
|
extern void blendscan(IndexScanDesc scan);
|
|
|
|
extern IndexBuildResult *blbuild(Relation heap, Relation index,
|
|
|
|
struct IndexInfo *indexInfo);
|
|
|
|
extern void blbuildempty(Relation index);
|
|
|
|
extern IndexBulkDeleteResult *blbulkdelete(IndexVacuumInfo *info,
|
|
|
|
IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback,
|
|
|
|
void *callback_state);
|
|
|
|
extern IndexBulkDeleteResult *blvacuumcleanup(IndexVacuumInfo *info,
|
|
|
|
IndexBulkDeleteResult *stats);
|
|
|
|
extern bytea *bloptions(Datum reloptions, bool validate);
|
|
|
|
extern void blcostestimate(PlannerInfo *root, IndexPath *path,
|
|
|
|
double loop_count, Cost *indexStartupCost,
|
|
|
|
Cost *indexTotalCost, Selectivity *indexSelectivity,
|
2017-02-16 02:53:24 +08:00
|
|
|
double *indexCorrelation, double *indexPages);
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
#endif
|