2016-04-01 21:42:24 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* blscan.c
|
|
|
|
* Bloom index scan functions.
|
|
|
|
*
|
2022-01-08 08:04:57 +08:00
|
|
|
* Copyright (c) 2016-2022, PostgreSQL Global Development Group
|
2016-04-01 21:42:24 +08:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* contrib/bloom/blscan.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/relscan.h"
|
2019-10-23 11:56:22 +08:00
|
|
|
#include "bloom.h"
|
2016-04-01 21:42:24 +08:00
|
|
|
#include "miscadmin.h"
|
2019-10-23 11:56:22 +08:00
|
|
|
#include "pgstat.h"
|
2016-04-01 21:42:24 +08:00
|
|
|
#include "storage/bufmgr.h"
|
|
|
|
#include "storage/lmgr.h"
|
|
|
|
#include "utils/memutils.h"
|
|
|
|
#include "utils/rel.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Begin scan of bloom index.
|
|
|
|
*/
|
|
|
|
IndexScanDesc
|
|
|
|
blbeginscan(Relation r, int nkeys, int norderbys)
|
|
|
|
{
|
|
|
|
IndexScanDesc scan;
|
2016-05-18 05:01:18 +08:00
|
|
|
BloomScanOpaque so;
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
scan = RelationGetIndexScan(r, nkeys, norderbys);
|
|
|
|
|
2016-05-18 05:01:18 +08:00
|
|
|
so = (BloomScanOpaque) palloc(sizeof(BloomScanOpaqueData));
|
|
|
|
initBloomState(&so->state, scan->indexRelation);
|
|
|
|
so->sign = NULL;
|
|
|
|
|
|
|
|
scan->opaque = so;
|
|
|
|
|
2016-04-01 21:42:24 +08:00
|
|
|
return scan;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rescan a bloom index.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
|
|
|
|
ScanKey orderbys, int norderbys)
|
|
|
|
{
|
2016-05-18 05:01:18 +08:00
|
|
|
BloomScanOpaque so = (BloomScanOpaque) scan->opaque;
|
2016-04-01 21:42:24 +08:00
|
|
|
|
2016-05-18 05:01:18 +08:00
|
|
|
if (so->sign)
|
|
|
|
pfree(so->sign);
|
2016-04-01 21:42:24 +08:00
|
|
|
so->sign = NULL;
|
|
|
|
|
|
|
|
if (scankey && scan->numberOfKeys > 0)
|
|
|
|
{
|
|
|
|
memmove(scan->keyData, scankey,
|
|
|
|
scan->numberOfKeys * sizeof(ScanKeyData));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End scan of bloom index.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
blendscan(IndexScanDesc scan)
|
|
|
|
{
|
|
|
|
BloomScanOpaque so = (BloomScanOpaque) scan->opaque;
|
|
|
|
|
|
|
|
if (so->sign)
|
|
|
|
pfree(so->sign);
|
|
|
|
so->sign = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-07-09 21:10:44 +08:00
|
|
|
* Insert all matching tuples into a bitmap.
|
2016-04-01 21:42:24 +08:00
|
|
|
*/
|
|
|
|
int64
|
|
|
|
blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
|
|
|
|
{
|
|
|
|
int64 ntids = 0;
|
|
|
|
BlockNumber blkno = BLOOM_HEAD_BLKNO,
|
|
|
|
npages;
|
|
|
|
int i;
|
|
|
|
BufferAccessStrategy bas;
|
|
|
|
BloomScanOpaque so = (BloomScanOpaque) scan->opaque;
|
|
|
|
|
2016-04-04 02:17:20 +08:00
|
|
|
if (so->sign == NULL)
|
2016-04-01 21:42:24 +08:00
|
|
|
{
|
|
|
|
/* New search: have to calculate search signature */
|
|
|
|
ScanKey skey = scan->keyData;
|
|
|
|
|
2016-06-03 22:52:36 +08:00
|
|
|
so->sign = palloc0(sizeof(BloomSignatureWord) * so->state.opts.bloomLength);
|
2016-04-01 21:42:24 +08:00
|
|
|
|
|
|
|
for (i = 0; i < scan->numberOfKeys; i++)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Assume bloom-indexable operators to be strict, so nothing could
|
|
|
|
* be found for NULL key.
|
|
|
|
*/
|
|
|
|
if (skey->sk_flags & SK_ISNULL)
|
|
|
|
{
|
|
|
|
pfree(so->sign);
|
|
|
|
so->sign = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add next value to the signature */
|
|
|
|
signValue(&so->state, so->sign, skey->sk_argument,
|
|
|
|
skey->sk_attno - 1);
|
|
|
|
|
|
|
|
skey++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're going to read the whole index. This is why we use appropriate
|
|
|
|
* buffer access strategy.
|
|
|
|
*/
|
|
|
|
bas = GetAccessStrategy(BAS_BULKREAD);
|
|
|
|
npages = RelationGetNumberOfBlocks(scan->indexRelation);
|
|
|
|
|
|
|
|
for (blkno = BLOOM_HEAD_BLKNO; blkno < npages; blkno++)
|
|
|
|
{
|
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
|
|
|
|
buffer = ReadBufferExtended(scan->indexRelation, MAIN_FORKNUM,
|
|
|
|
blkno, RBM_NORMAL, bas);
|
|
|
|
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2016-04-20 21:31:19 +08:00
|
|
|
page = BufferGetPage(buffer);
|
|
|
|
TestForOldSnapshot(scan->xs_snapshot, scan->indexRelation, page);
|
2016-04-01 21:42:24 +08:00
|
|
|
|
Fix assorted bugs in contrib/bloom.
In blinsert(), cope with the possibility that a page we pull from the
notFullPage list is marked BLOOM_DELETED. This could happen if VACUUM
recently marked it deleted but hasn't (yet) updated the metapage.
We can re-use such a page safely, but we *must* reinitialize it so that
it's no longer marked deleted.
Fix blvacuum() so that it updates the notFullPage list even if it's
going to update it to empty. The previous "optimization" of skipping
the update seems pretty dubious, since it means that the next blinsert()
will uselessly visit whatever pages we left in the list.
Uniformly treat PageIsNew pages the same as deleted pages. This should
allow proper recovery if a crash occurs just after relation extension.
Properly use vacuum_delay_point, not assorted ad-hoc CHECK_FOR_INTERRUPTS
calls, in the blvacuum() main loop.
Fix broken tuple-counting logic: blvacuum.c counted the number of live
index tuples over again in each scan, leading to VACUUM VERBOSE reporting
some multiple of the actual number of surviving index tuples after any
vacuum that removed any tuples (since they'd be counted in blvacuum, maybe
more than once, and then again in blvacuumcleanup, without ever zeroing the
counter). It's sufficient to count them in blvacuumcleanup.
stats->estimated_count is a boolean, not a counter, and we don't want
to set it true, so don't add tuple counts to it.
Add a couple of Asserts that we don't overrun available space on a bloom
page. I don't think there's any bug there today, but the way the
FreeBlockNumberArray size calculation is set up is scarily fragile, and
BloomPageGetFreeSpace isn't much better. The Asserts should help catch
any future mistakes.
Per investigation of a report from Jeff Janes. I think the first item
above may explain his report; the other changes were things I noticed
while casting about for an explanation.
Report: <CAMkU=1xEUuBphDwDmB1WjN4+td4kpnEniFaTBxnk1xzHCw8_OQ@mail.gmail.com>
2016-08-14 10:24:48 +08:00
|
|
|
if (!PageIsNew(page) && !BloomPageIsDeleted(page))
|
2016-04-01 21:42:24 +08:00
|
|
|
{
|
|
|
|
OffsetNumber offset,
|
|
|
|
maxOffset = BloomPageGetMaxOffset(page);
|
|
|
|
|
|
|
|
for (offset = 1; offset <= maxOffset; offset++)
|
|
|
|
{
|
|
|
|
BloomTuple *itup = BloomPageGetTuple(&so->state, page, offset);
|
|
|
|
bool res = true;
|
|
|
|
|
|
|
|
/* Check index signature with scan signature */
|
2016-04-04 03:16:07 +08:00
|
|
|
for (i = 0; i < so->state.opts.bloomLength; i++)
|
2016-04-01 21:42:24 +08:00
|
|
|
{
|
|
|
|
if ((itup->sign[i] & so->sign[i]) != so->sign[i])
|
2016-04-04 02:17:20 +08:00
|
|
|
{
|
2016-04-01 21:42:24 +08:00
|
|
|
res = false;
|
2016-04-04 02:17:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-01 21:42:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add matching tuples to bitmap */
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
tbm_add_tuples(tbm, &itup->heapPtr, 1, true);
|
|
|
|
ntids++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnlockReleaseBuffer(buffer);
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
}
|
|
|
|
FreeAccessStrategy(bas);
|
|
|
|
|
|
|
|
return ntids;
|
|
|
|
}
|