Skip to content

Commit d9c7352

Browse files
author
Artur Zakirov
committed
De-duplicate entries
1 parent eb1f875 commit d9c7352

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

rum.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,19 +612,21 @@ typedef struct RumScanEntryData
612612
TBMIterator *matchIterator;
613613
TBMIterateResult *matchResult;
614614

615+
/* for full-scan query with order-by */
616+
RumBtreeStack *stack;
617+
bool scanWithAddInfo;
618+
615619
/* used for Posting list and one page in Posting tree */
616620
RumKey *list;
617621
MemoryContext context;
618622
uint32 nlist;
619-
uint32 nalloc;
620623
OffsetNumber offset;
621624

622625
bool isFinished;
623626
bool reduceResult;
624627
bool preValue;
625628
uint32 predictNumberResult;
626629
RumPostingTreeScan *gdi;
627-
RumBtreeStack *stack;
628630
} RumScanEntryData;
629631

630632
typedef struct

rumget.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ startScanEntry(RumState * rumstate, RumScanEntry entry)
462462

463463
if (entry->isPartialMatch ||
464464
(entry->queryCategory == RUM_CAT_EMPTY_QUERY &&
465-
entry->searchMode != GIN_SEARCH_MODE_EVERYTHING))
465+
!entry->scanWithAddInfo))
466466
{
467467
/*
468468
* btreeEntry.findItem locates the first item >= given search key.
@@ -500,7 +500,7 @@ startScanEntry(RumState * rumstate, RumScanEntry entry)
500500
}
501501
else if (btreeEntry.findItem(&btreeEntry, stackEntry) ||
502502
(entry->queryCategory == RUM_CAT_EMPTY_QUERY &&
503-
entry->searchMode == GIN_SEARCH_MODE_EVERYTHING))
503+
entry->scanWithAddInfo))
504504
{
505505
IndexTuple itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, stackEntry->off));
506506

@@ -572,7 +572,7 @@ startScanEntry(RumState * rumstate, RumScanEntry entry)
572572
}
573573

574574
if (entry->queryCategory == RUM_CAT_EMPTY_QUERY &&
575-
entry->searchMode == GIN_SEARCH_MODE_EVERYTHING)
575+
entry->scanWithAddInfo)
576576
entry->stack = stackEntry;
577577
}
578578

@@ -850,7 +850,6 @@ entryGetNextItemList(RumState * rumstate, RumScanEntry entry)
850850
pfree(entry->list);
851851
entry->list = NULL;
852852
entry->nlist = 0;
853-
entry->nalloc = 0;
854853
}
855854
entry->matchBitmap = NULL;
856855
entry->matchResult = NULL;
@@ -2551,11 +2550,19 @@ rumgettuple(IndexScanDesc scan, ScanDirection direction)
25512550
}
25522551

25532552
item = rum_tuplesort_getrum(so->sortstate, true, &should_free);
2554-
if (item)
2553+
while (item)
25552554
{
25562555
uint32 i,
25572556
j = 0;
25582557

2558+
if (rumCompareItemPointers(&scan->xs_ctup.t_self, &item->iptr) == 0)
2559+
{
2560+
if (should_free)
2561+
pfree(item);
2562+
item = rum_tuplesort_getrum(so->sortstate, true, &should_free);
2563+
continue;
2564+
}
2565+
25592566
scan->xs_ctup.t_self = item->iptr;
25602567
scan->xs_recheck = item->recheck;
25612568
scan->xs_recheckorderby = false;
@@ -2574,8 +2581,6 @@ rumgettuple(IndexScanDesc scan, ScanDirection direction)
25742581
pfree(item);
25752582
PG_RETURN_BOOL(true);
25762583
}
2577-
else
2578-
{
2579-
PG_RETURN_BOOL(false);
2580-
}
2584+
2585+
PG_RETURN_BOOL(false);
25812586
}

rumscan.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ rumFillScanEntry(RumScanOpaque so, OffsetNumber attnum,
111111
scanEntry->matchBitmap = NULL;
112112
scanEntry->matchIterator = NULL;
113113
scanEntry->matchResult = NULL;
114+
scanEntry->stack = NULL;
115+
scanEntry->scanWithAddInfo = false;
114116
scanEntry->list = NULL;
115117
scanEntry->nlist = 0;
116-
scanEntry->nalloc = 0;
117118
scanEntry->offset = InvalidOffsetNumber;
118119
scanEntry->isFinished = false;
119120
scanEntry->reduceResult = false;
@@ -390,6 +391,7 @@ rumNewScanKey(IndexScanDesc scan)
390391
RumScanOpaque so = (RumScanOpaque) scan->opaque;
391392
int i;
392393
bool hasNullQuery = false;
394+
bool checkEmptyEntry = false;
393395
MemoryContext oldCtx;
394396

395397
/*
@@ -433,6 +435,7 @@ rumNewScanKey(IndexScanDesc scan)
433435
GIN_SEARCH_MODE_EVERYTHING,
434436
(Datum) 0, 0,
435437
NULL, NULL, NULL, NULL, false);
438+
checkEmptyEntry = true;
436439
}
437440

438441
for (i = 0; i < scan->numberOfOrderBys; i++)
@@ -442,6 +445,16 @@ rumNewScanKey(IndexScanDesc scan)
442445
break;
443446
}
444447

448+
/*
449+
* If there are order-by keys, mark empty entry for scan with add info.
450+
* If so->nkeys > 1 then there are order-by keys.
451+
*/
452+
if (checkEmptyEntry && so->nkeys > 1)
453+
{
454+
Assert(so->totalentries > 0);
455+
so->entries[0]->scanWithAddInfo = true;
456+
}
457+
445458
if (scan->numberOfOrderBys > 0)
446459
{
447460
scan->xs_orderbyvals = palloc0(sizeof(Datum) * scan->numberOfOrderBys);

rumsort.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3849,6 +3849,8 @@ comparetup_rum(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
38493849
float8 v1 = DatumGetFloat8(a->datum1);
38503850
float8 v2 = DatumGetFloat8(b->datum1);
38513851
int i;
3852+
IndexTuple tuple1;
3853+
IndexTuple tuple2;
38523854

38533855
if (v1 < v2)
38543856
return -1;
@@ -3864,6 +3866,28 @@ comparetup_rum(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
38643866
else if (i1->data[i] > i2->data[i])
38653867
return 1;
38663868
}
3869+
3870+
/*
3871+
* If key values are equal, we sort on ItemPointer. This does not affect
3872+
* validity of the finished index, but it may be useful to have index
3873+
* scans in physical order.
3874+
*/
3875+
tuple1 = (IndexTuple) a->tuple;
3876+
tuple2 = (IndexTuple) b->tuple;
3877+
{
3878+
BlockNumber blk1 = ItemPointerGetBlockNumber(&tuple1->t_tid);
3879+
BlockNumber blk2 = ItemPointerGetBlockNumber(&tuple2->t_tid);
3880+
3881+
if (blk1 != blk2)
3882+
return (blk1 < blk2) ? -1 : 1;
3883+
}
3884+
{
3885+
OffsetNumber pos1 = ItemPointerGetOffsetNumber(&tuple1->t_tid);
3886+
OffsetNumber pos2 = ItemPointerGetOffsetNumber(&tuple2->t_tid);
3887+
3888+
if (pos1 != pos2)
3889+
return (pos1 < pos2) ? -1 : 1;
3890+
}
38673891
return 0;
38683892
}
38693893

0 commit comments

Comments
 (0)