From c01e76f2faf65410979a2071e45d571da4847727 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 28 Sep 2024 11:31:03 -0700 Subject: [PATCH] Added strict ordering [skip ci] --- src/hnsw.h | 1 + src/hnswscan.c | 8 ++++++++ 2 files changed, 9 insertions(+) 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;