mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-14 00:26:55 +08:00
Improved performance of index scans for IVFFlat after updates and deletes
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
## 0.5.1 (unreleased)
|
||||||
|
|
||||||
|
- Improved performance of index scans for IVFFlat after updates and deletes
|
||||||
|
|
||||||
## 0.5.0 (2023-08-28)
|
## 0.5.0 (2023-08-28)
|
||||||
|
|
||||||
- Added HNSW index type
|
- Added HNSW index type
|
||||||
|
|||||||
@@ -247,6 +247,7 @@ typedef struct IvfflatScanOpaqueData
|
|||||||
int dimensions;
|
int dimensions;
|
||||||
bool first;
|
bool first;
|
||||||
Buffer buf;
|
Buffer buf;
|
||||||
|
ItemPointerData heaptid;
|
||||||
|
|
||||||
/* Sorting */
|
/* Sorting */
|
||||||
Tuplesortstate *sortstate;
|
Tuplesortstate *sortstate;
|
||||||
|
|||||||
@@ -141,8 +141,13 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
IndexTuple itup;
|
IndexTuple itup;
|
||||||
Datum datum;
|
Datum datum;
|
||||||
bool isnull;
|
bool isnull;
|
||||||
|
ItemId itemid = PageGetItemId(page, offno);
|
||||||
|
|
||||||
itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offno));
|
/* Skip dead tuples */
|
||||||
|
if (scan->ignore_killed_tuples && ItemIdIsDead(itemid))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
itup = (IndexTuple) PageGetItem(page, itemid);
|
||||||
datum = index_getattr(itup, 1, tupdesc, &isnull);
|
datum = index_getattr(itup, 1, tupdesc, &isnull);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -182,6 +187,55 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
tuplesort_performsort(so->sortstate);
|
tuplesort_performsort(so->sortstate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Mark prior tuple as dead
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
MarkPriorTupleDead(IndexScanDesc scan)
|
||||||
|
{
|
||||||
|
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
|
||||||
|
Buffer buf = so->buf;
|
||||||
|
Page page;
|
||||||
|
OffsetNumber maxoffno;
|
||||||
|
|
||||||
|
/* Safety check */
|
||||||
|
if (!BufferIsValid(so->buf) || !ItemPointerIsValid(&so->heaptid))
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Only a shared locked is needed for ItemIdMarkDead */
|
||||||
|
LockBuffer(buf, BUFFER_LOCK_SHARE);
|
||||||
|
page = BufferGetPage(buf);
|
||||||
|
maxoffno = PageGetMaxOffsetNumber(page);
|
||||||
|
|
||||||
|
for (OffsetNumber offno = FirstOffsetNumber; offno <= maxoffno; offno = OffsetNumberNext(offno))
|
||||||
|
{
|
||||||
|
ItemId itemid = PageGetItemId(page, offno);
|
||||||
|
IndexTuple itup = (IndexTuple) PageGetItem(page, itemid);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Find tuple. Since buffer has been pinned, tuple cannot have been
|
||||||
|
* vacuumed (and heap TID reused).
|
||||||
|
*/
|
||||||
|
if (ItemPointerEquals(&itup->t_tid, &so->heaptid))
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Make sure tuple has not already been marked dead to avoid extra
|
||||||
|
* WAL if wal_log_hints or data checksums enabled
|
||||||
|
*/
|
||||||
|
if (!ItemIdIsDead(itemid))
|
||||||
|
{
|
||||||
|
ItemIdMarkDead(itemid);
|
||||||
|
MarkBufferDirtyHint(buf, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Unlock buffer */
|
||||||
|
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prepare for an index scan
|
* Prepare for an index scan
|
||||||
*/
|
*/
|
||||||
@@ -209,6 +263,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
so = (IvfflatScanOpaque) palloc(offsetof(IvfflatScanOpaqueData, lists) + probes * sizeof(IvfflatScanList));
|
so = (IvfflatScanOpaque) palloc(offsetof(IvfflatScanOpaqueData, lists) + probes * sizeof(IvfflatScanList));
|
||||||
so->buf = InvalidBuffer;
|
so->buf = InvalidBuffer;
|
||||||
so->first = true;
|
so->first = true;
|
||||||
|
ItemPointerSetInvalid(&so->heaptid);
|
||||||
so->probes = probes;
|
so->probes = probes;
|
||||||
so->dimensions = dimensions;
|
so->dimensions = dimensions;
|
||||||
|
|
||||||
@@ -257,6 +312,7 @@ ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
so->first = true;
|
so->first = true;
|
||||||
|
ItemPointerSetInvalid(&so->heaptid);
|
||||||
pairingheap_reset(so->listQueue);
|
pairingheap_reset(so->listQueue);
|
||||||
|
|
||||||
if (keys && scan->numberOfKeys > 0)
|
if (keys && scan->numberOfKeys > 0)
|
||||||
@@ -314,6 +370,12 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
if (value != scan->orderByData->sk_argument)
|
if (value != scan->orderByData->sk_argument)
|
||||||
pfree(DatumGetPointer(value));
|
pfree(DatumGetPointer(value));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Mark prior tuple as dead */
|
||||||
|
if (scan->kill_prior_tuple)
|
||||||
|
MarkPriorTupleDead(scan);
|
||||||
|
}
|
||||||
|
|
||||||
if (tuplesort_gettupleslot(so->sortstate, true, false, so->slot, NULL))
|
if (tuplesort_gettupleslot(so->sortstate, true, false, so->slot, NULL))
|
||||||
{
|
{
|
||||||
@@ -326,6 +388,9 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
scan->xs_ctup.t_self = *tid;
|
scan->xs_ctup.t_self = *tid;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Keep track of info needed to mark tuple as dead */
|
||||||
|
so->heaptid = *tid;
|
||||||
|
|
||||||
/* Unpin buffer */
|
/* Unpin buffer */
|
||||||
if (BufferIsValid(so->buf))
|
if (BufferIsValid(so->buf))
|
||||||
ReleaseBuffer(so->buf);
|
ReleaseBuffer(so->buf);
|
||||||
|
|||||||
Reference in New Issue
Block a user