diff --git a/src/hnsw.h b/src/hnsw.h index 0e1aa54..400eaa0 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -371,6 +371,7 @@ typedef struct HnswScanOpaqueData Datum q; int m; int64 tuples; + double previousDistance; MemoryContext tmpCtx; /* Support functions */ diff --git a/src/hnswscan.c b/src/hnswscan.c index a2f6f8b..7b5c3d7 100644 --- a/src/hnswscan.c +++ b/src/hnswscan.c @@ -1,5 +1,7 @@ #include "postgres.h" +#include + #include "access/relscan.h" #include "hnsw.h" #include "pgstat.h" @@ -149,6 +151,7 @@ hnswrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int no so->first = true; so->tuples = 0; + so->previousDistance = -INFINITY; MemoryContextReset(so->tmpCtx); if (keys && scan->numberOfKeys > 0) @@ -296,6 +299,11 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir) heaptid = &element->heaptids[--element->heaptidsLength]; + if (hc->distance < so->previousDistance) + continue; + + so->previousDistance = hc->distance; + MemoryContextSwitchTo(oldCtx); scan->xs_heaptid = *heaptid;