pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: VladlenPopolitov (@VladlenPopolitov) <noreply+VladlenPopolitov@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] PR #524: Add search option to better process queries with WHERE clause (relaxed monotonicity)
Date: Thu, 18 Apr 2024 13:44:59 +0000
Message-ID: <gh-pgvector-pgvector-524@github.com> (raw)

The current state: pgvector uses HNSW algorithm, that returns k-TOP nearest neighbours. If SQL query has WHERE clause, k-TOP neighbours could not satisfy the filter condition, and the query does not return any records. 

The proposal: I propose to add an option in HNSW search to use the relaxed monotonicity proposed in the article https://www.usenix.org/system/files/osdi23-zhang-qianxi_1.pdf . The implementation does not affect the index build, it changes the greedy search algorithm only. If SQL query does not have WHERE clause, user could use standard greedy search (less overhead, more quick). If SQL query has WHERE clause (and even does not have), the user could use relaxed monotonicity algorithm (higher recall guaranteed). This choice is implemented by GUC variable easily. 

In the proposed pull request the option  is activated by setting `SET hnsw.use_relaxed=ON` . When this option is activated, HNSW returns nearest vectors until needed records are found using Open-Next interface, instead of efSearch-TOP records in the standard implementation. If `hnsw.use_relaxed=OFF` (default), HNSW behaves as usual.

Changes: 
hnsw.c: PostgreSQL variable `hnsw.use_relaxed` and corresponding C variable `bool hnsw_use_relaxed` are added.
hnsw.h: Open-Next interface requires to store the search state between gettuple() calls, this state is stored in the members fields of the `struct HnswScanOpaqueData`
hnswscan.c: gettuple() was modified. If `hnsw.use_relaxed=ON` , it calls function OpenScanItemsRelaxed() instead of GetScanItems(), then it call NextScanItemsRelaxed() to get a next tuple.
hnswutils.c : HnswSearchLayerRelaxedNext(), HnswVisitedRelaxedNext() , HnswSearchLayerRelaxed() , NextScanItemsRelaxed() are added to implement Open-Next interface for greedy search.  
tests/sql/hnsw_relaxed.sql and tests/expected/hnsw_relaxed.out added to check the work of HNSW search with and without hnsw.use_relaxed option. These files contain also the tests from hnsw_vector_l2 and hnsw_vector_ip to ensure the standard test execution, when the option in ON.
README.md was NOT updated until final commit (GUC variable names could be changed or added, comments are expected) 

The example to demonstrate, that pgvector can work with WHERE condition using hnsw.use_relaxed option:
```SQL
SELECT t, 
Array[t,t+1,t+2]::vector(3) AS val 
INTO test_where 
FROM generate_series(1, 2000) as t;
SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2;
  t   
------
 1501
 1502
(2 rows)

CREATE INDEX ON test_where USING hnsw (val vector_l2_ops) WITH (m=10);
set hnsw.use_relaxed=off;
set hnsw.ef_search=100;
SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2;
 t 
---
(0 rows)

set hnsw.use_relaxed=on;
SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2;
  t   
------
 1501
 1502
(2 rows)
```
ANN-benchmark without WHERE (see picture) condition in query shows the decrease the query per seconds rate (additional overhead to support Next interface), that in many cases compensated by increase of the recall (more vectors in the index are checked). Taking into consideration WHERE condition, pgvector with hnsw.use_relaxed option has superior recall comparing to standard HNSW implementation of all programs.

I am open for comments and discussions regarding this pull request.

Vladlen Popolitov.

![pgvector_standard_vs_relaxed_lastfm](https://github.com/pgvector/pgvector/assets/21155639/0777ed19-5ab6-4846-9642-12caa62843cf)

inline patch 1: src/hnsw.c, src/hnsw.h, src/hnswscan.c and 3 more (+548/-20 lines) download inline-1.patchdiff --git a/src/hnsw.c b/src/hnsw.c index a7b1e5fcc..1a7bf06f6 100644 --- a/src/hnsw.c +++ b/src/hnsw.c @@ -20,6 +20,7 @@ int hnsw_ef_search; int hnsw_lock_tranche_id; static relopt_kind hnsw_relopt_kind; +bool hnsw_use_relaxed; /* * Assign a tranche ID for our LWLocks. This only needs to be done by one @@ -67,6 +68,9 @@ HnswInit(void) DefineCustomIntVariable("hnsw.ef_search", "Sets the size of the dynamic candidate list for search", "Valid range is 1..1000.", &hnsw_ef_search, HNSW_DEFAULT_EF_SEARCH, HNSW_MIN_EF_SEARCH, HNSW_MAX_EF_SEARCH, PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomBoolVariable("hnsw.use_relaxed", "Sets the flag to use Relaxed monotonicity algorithm during search", + "Valid values are ON, OFF, FALSE, TRUE.", &hnsw_use_relaxed, + false, PGC_USERSET, 0, NULL, NULL, NULL); MarkGUCPrefixReserved("hnsw"); } diff --git a/src/hnsw.h b/src/hnsw.h index 2f4503905..62132d235 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -107,6 +107,7 @@ /* Variables */ extern int hnsw_ef_search; extern int hnsw_lock_tranche_id; +extern bool hnsw_use_relaxed; typedef struct HnswElementData HnswElementData; typedef struct HnswNeighborArray HnswNeighborArray; @@ -324,6 +325,12 @@ typedef struct HnswNeighborTupleData typedef HnswNeighborTupleData * HnswNeighborTuple; +typedef struct HnswNextElement +{ + HnswCandidate *nextFromVisited, + *nextFromSearch; +} HnswNextElement; + typedef struct HnswScanOpaqueData { const HnswTypeInfo *typeInfo; @@ -335,6 +342,14 @@ typedef struct HnswScanOpaqueData FmgrInfo *procinfo; FmgrInfo *normprocinfo; Oid collation; + char *base; + Datum q; + int m; + pairingheap *allVisited, + *candidates; + void *visitedFlag; + bool use_relaxed; + HnswNextElement next; } HnswScanOpaqueData; typedef HnswScanOpaqueData * HnswScanOpaque; @@ -375,6 +390,8 @@ Buffer HnswNewBuffer(Relation index, ForkNumber forkNum); void HnswInitPage(Buffer buf, Page page); void HnswInit(void); List *HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *procinfo, Oid collation, int m, bool inserting, HnswElement skipElement); +bool HnswSearchLayerRelaxed(IndexScanDesc scan, List *ep, int ef); +ItemPointerData NextScanItemsRelaxed(IndexScanDesc scan); HnswElement HnswGetEntryPoint(Relation index); void HnswGetMetaPageInfo(Relation index, int *m, HnswElement * entryPoint); void *HnswAlloc(HnswAllocator * allocator, Size size); diff --git a/src/hnswscan.c b/src/hnswscan.c index 0463a898c..5247e8ccd 100644 --- a/src/hnswscan.c +++ b/src/hnswscan.c @@ -40,6 +40,42 @@ GetScanItems(IndexScanDesc scan, Datum q) return HnswSearchLayer(base, q, ep, hnsw_ef_search, 0, index, procinfo, collation, m, false, NULL); } +/* + * Algorithm 5 from paper with Relaxed option + */ +static bool +OpenScanItemsRelaxed(IndexScanDesc scan, Datum q) +{ + HnswScanOpaque so = (HnswScanOpaque) scan->opaque; + Relation index = scan->indexRelation; + FmgrInfo *procinfo = so->procinfo; + Oid collation = so->collation; + List *ep; + List *w; + int m; + HnswElement entryPoint; + char *base = NULL; + + /* Get m and entry point */ + HnswGetMetaPageInfo(index, &m, &entryPoint); + + if (entryPoint == NULL) + return false; + + ep = list_make1(HnswEntryCandidate(base, entryPoint, q, index, procinfo, collation, false)); + + for (int lc = entryPoint->level; lc >= 1; lc--) + { + w = HnswSearchLayer(base, q, ep, 1, lc, index, procinfo, collation, m, false, NULL); + ep = w; + } + /* save values to use them in NextCalls */ + so->base = base; + so->q = q; + so->m = m; + return HnswSearchLayerRelaxed(scan, ep, hnsw_ef_search); +} + /* * Get scan value */ @@ -152,8 +188,18 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir) * before marking tuples as deleted. */ LockPage(scan->indexRelation, HNSW_SCAN_LOCK, ShareLock); - - so->w = GetScanItems(scan, value); + so->use_relaxed = hnsw_use_relaxed; + if (so->use_relaxed) + { + if (!OpenScanItemsRelaxed(scan, value)) + { + return false; + } + } + else + { + so->w = GetScanItems(scan, value); + } /* Release shared lock */ UnlockPage(scan->indexRelation, HNSW_SCAN_LOCK, ShareLock); @@ -164,29 +210,49 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir) elog(INFO, "memory: %zu MB", MemoryContextMemAllocated(so->tmpCtx, false) / (1024 * 1024)); #endif } - - while (list_length(so->w) > 0) + if (!so->use_relaxed) { - char *base = NULL; - HnswCandidate *hc = llast(so->w); - HnswElement element = HnswPtrAccess(base, hc->element); - ItemPointer heaptid; - - /* Move to next element if no valid heap TIDs */ - if (element->heaptidsLength == 0) + while (list_length(so->w) > 0) { - so->w = list_delete_last(so->w); - continue; + char *base = NULL; + HnswCandidate *hc = llast(so->w); + HnswElement element = HnswPtrAccess(base, hc->element); + ItemPointer heaptid; + + /* Move to next element if no valid heap TIDs */ + if (element->heaptidsLength == 0) + { + so->w = list_delete_last(so->w); + continue; + } + + heaptid = &element->heaptids[--element->heaptidsLength]; + + MemoryContextSwitchTo(oldCtx); + + scan->xs_heaptid = *heaptid; + scan->xs_recheck = false; + scan->xs_recheckorderby = false; + return true; } + } + else + { + ItemPointerData heaptiddata; - heaptid = &element->heaptids[--element->heaptidsLength]; - + heaptiddata = NextScanItemsRelaxed(scan); MemoryContextSwitchTo(oldCtx); - - scan->xs_heaptid = *heaptid; - scan->xs_recheck = false; - scan->xs_recheckorderby = false; - return true; + if (ItemPointerIsValid(&heaptiddata)) + { + scan->xs_heaptid = heaptiddata; + scan->xs_recheck = false; + scan->xs_recheckorderby = false; + return true; + } + else + { + return false; + } } MemoryContextSwitchTo(oldCtx); diff --git a/src/hnswutils.c b/src/hnswutils.c index f69c057ad..0f133555b 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -14,6 +14,9 @@ #include "utils/datum.h" #include "utils/memdebug.h" #include "utils/rel.h" +#include "utils/syscache.h" +#include "access/relscan.h" +#include "vector.h" #if PG_VERSION_NUM < 170000 static inline uint64 @@ -1406,3 +1409,294 @@ hnsw_sparsevec_support(PG_FUNCTION_ARGS) PG_RETURN_POINTER(&typeInfo); }; + +/* + * Algorithm 2 from paper with adoptation from Relaxed monotonicity + */ +static HnswCandidate * +HnswSearchLayerRelaxedNext(char *base, Datum q, Relation index, + FmgrInfo *procinfo, Oid collation, int m, + pairingheap *C, visited_hash * v) +{ + HnswNeighborArray *neighborhoodData = NULL; + Size neighborhoodSize; + HnswCandidate *c = NULL; + + /* Create local memory for neighborhood if needed */ + if (index == NULL) + { + neighborhoodSize = HNSW_NEIGHBOR_ARRAY_SIZE(HnswGetLayerM(m, 0)); + neighborhoodData = palloc(neighborhoodSize); + } + + + if (!pairingheap_is_empty(C)) + { + HnswNeighborArray *neighborhood; + HnswElement cElement; + + c = HnswGetPairingHeapCandidate(c_node, pairingheap_remove_first(C)); + + cElement = HnswPtrAccess(base, c->element); + + if (HnswPtrIsNull(base, cElement->neighbors)) + HnswLoadNeighbors(cElement, index, m); + + /* Get the neighborhood at layer lc */ + neighborhood = HnswGetNeighbors(base, cElement, 0); + + /* Copy neighborhood to local memory if needed */ + if (index == NULL) + { + LWLockAcquire(&cElement->lock, LW_SHARED); + memcpy(neighborhoodData, neighborhood, neighborhoodSize); + LWLockRelease(&cElement->lock); + neighborhood = neighborhoodData; + } + + for (int i = 0; i < neighborhood->length; i++) + { + HnswCandidate *e = &neighborhood->items[i]; + bool visited; + + AddToVisited(base, v, e->element, index, &visited); + + if (!visited) + { + float eDistance; + HnswElement eElement = HnswPtrAccess(base, e->element); + HnswCandidate *ec; + + if (index == NULL) + eDistance = GetElementDistance(base, e->element.ptr , q, procinfo, collation) ; + else + HnswLoadElement(eElement, &eDistance, &q, index, procinfo, collation, false, NULL); + + Assert(!eElement->deleted); + ec = palloc(sizeof(HnswCandidate)); + + HnswPtrStore(base, ec->element, eElement); + ec->distance = eDistance; + + pairingheap_add(C, &(CreatePairingHeapNode(ec)->c_node)); + } + } + } + return c; +} + +static HnswCandidate * +HnswVisitedRelaxedNext(char *base, pairingheap *all_visited) +{ + while (!pairingheap_is_empty(all_visited)) + { + HnswCandidate *candidate = HnswGetPairingHeapCandidate(c_node, pairingheap_remove_first(all_visited)); + HnswElement element = HnswPtrAccess(base, candidate->element); + + if (element->heaptidsLength > 0) + { + return candidate; + } + } + return NULL; +} + + +/* + * Algorithm 2 from paper with adoptation from Relaxed monotonicity + */ +bool +HnswSearchLayerRelaxed(IndexScanDesc scan, List *ep, int ef) +{ + HnswScanOpaque so = (HnswScanOpaque) scan->opaque; + Relation index = scan->indexRelation; + FmgrInfo *procinfo = so->procinfo; + Oid collation = so->collation; + char *base = so->base; + Datum q = so->q; + int m = so->m; + pairingheap *C = pairingheap_allocate(CompareNearestCandidates, NULL); + pairingheap *W = pairingheap_allocate(CompareFurthestCandidates, NULL); + pairingheap *all_visited = pairingheap_allocate(CompareNearestCandidates, NULL); + int wlen = 0; + visited_hash *v = (visited_hash *) palloc(sizeof(visited_hash)); + ListCell *lc2; + HnswNeighborArray *neighborhoodData = NULL; + Size neighborhoodSize; + + InitVisited(base, v, index, ef, m); + + /* Create local memory for neighborhood if needed */ + if (index == NULL) + { + neighborhoodSize = HNSW_NEIGHBOR_ARRAY_SIZE(HnswGetLayerM(m, 0)); + neighborhoodData = palloc(neighborhoodSize); + } + + /* Add entry points to v, C, and W */ + foreach(lc2, ep) + { + HnswCandidate *hc = (HnswCandidate *) lfirst(lc2); + bool found; + + AddToVisited(base, v, hc->element, index, &found); + + pairingheap_add(C, &(CreatePairingHeapNode(hc)->c_node)); + pairingheap_add(W, &(CreatePairingHeapNode(hc)->w_node)); + wlen++; + } + + while (!pairingheap_is_empty(C)) + { + HnswNeighborArray *neighborhood; + HnswCandidate *c = HnswGetPairingHeapCandidate(c_node, pairingheap_remove_first(C)); + HnswCandidate *f = HnswGetPairingHeapCandidate(w_node, pairingheap_first(W)); + HnswElement cElement; + + cElement = HnswPtrAccess(base, c->element); + + if (HnswPtrIsNull(base, cElement->neighbors)) + HnswLoadNeighbors(cElement, index, m); + + /* Get the neighborhood at layer 0 */ + neighborhood = HnswGetNeighbors(base, cElement, 0); + + /* Copy neighborhood to local memory if needed */ + if (index == NULL) + { + LWLockAcquire(&cElement->lock, LW_SHARED); + memcpy(neighborhoodData, neighborhood, neighborhoodSize); + LWLockRelease(&cElement->lock); + neighborhood = neighborhoodData; + } + + for (int i = 0; i < neighborhood->length; i++) + { + HnswCandidate *e = &neighborhood->items[i]; + bool visited; + + AddToVisited(base, v, e->element, index, &visited); + + if (!visited) + { + float eDistance; + HnswElement eElement = HnswPtrAccess(base, e->element); + HnswCandidate *ec; + + if (index == NULL) + eDistance = GetElementDistance(base, e->element.ptr , q, procinfo, collation); + else + HnswLoadElement(eElement, &eDistance, &q, index, procinfo, collation, false, NULL); + + Assert(!eElement->deleted); + ec = palloc(sizeof(HnswCandidate)); + + HnswPtrStore(base, ec->element, eElement); + ec->distance = eDistance; + + pairingheap_add(C, &(CreatePairingHeapNode(ec)->c_node)); + } + } + pairingheap_add(all_visited, &(CreatePairingHeapNode(c)->c_node)); + +/* + * Check the condition to exit the loop: + * 1) the best candidates queue (W) is full + * 2) the current candidate (c) distance is further than the furthest in W + * 3) the next candidate from candidate set (C) is further than the furthest in W or candidate set (C) is empty + */ + if ((wlen == ef) && + f->distance < c->distance && + (pairingheap_is_empty(C) || + (f->distance < HnswGetPairingHeapCandidate(c_node, pairingheap_first(C))->distance) + ) + ) + { + break; + } + if (wlen == ef) + { + HnswGetPairingHeapCandidate(w_node, pairingheap_remove_first(W)); + wlen--; + } + pairingheap_add(W, &(CreatePairingHeapNode(c)->w_node)); + wlen++; + } + /* save all variables stored search state */ + so->next.nextFromSearch = HnswSearchLayerRelaxedNext(base, q, index, procinfo, collation, m, C, (visited_hash *) v); + so->next.nextFromVisited = HnswVisitedRelaxedNext(base, all_visited); + so->visitedFlag = v; + so->candidates = C; + so->allVisited = all_visited; + return true; +} + + +ItemPointerData +NextScanItemsRelaxed(IndexScanDesc scan) +{ + HnswScanOpaque so = (HnswScanOpaque) scan->opaque; + char *base = so->base; + Datum q = so->q; + Relation index = scan->indexRelation; + FmgrInfo *procinfo = so->procinfo; + Oid collation = so->collation; + int m = so->m; + pairingheap *C = so->candidates; + void *v = so->visitedFlag; + pairingheap *all_visited = so->allVisited; + HnswNextElement *next = &so->next; + + ItemPointerData heaptid; + + if (next->nextFromVisited && next->nextFromSearch) + { + if (next->nextFromVisited->distance < next->nextFromSearch->distance) + { + HnswElement element = HnswPtrAccess(base, next->nextFromVisited->element); + + heaptid = element->heaptids[--element->heaptidsLength]; + if (element->heaptidsLength == 0) + { + next->nextFromVisited = HnswVisitedRelaxedNext(base, all_visited); + } + } + else + { + HnswElement element = HnswPtrAccess(base, next->nextFromSearch->element); + + heaptid = element->heaptids[--element->heaptidsLength]; + if (element->heaptidsLength == 0) + { + next->nextFromSearch = HnswSearchLayerRelaxedNext(base, q, index, procinfo, collation, m, C, (visited_hash *) v); + } + } + } + else if (next->nextFromVisited) + { + HnswElement element = HnswPtrAccess(base, next->nextFromVisited->element); + + heaptid = element->heaptids[--element->heaptidsLength]; + if (element->heaptidsLength == 0) + { + next->nextFromVisited = HnswVisitedRelaxedNext(base, all_visited); + } + } + else if (next->nextFromSearch) + { + HnswElement element = HnswPtrAccess(base, next->nextFromSearch->element); + + heaptid = element->heaptids[--element->heaptidsLength]; + if (element->heaptidsLength == 0) + { + next->nextFromSearch = HnswSearchLayerRelaxedNext(base, q, index, procinfo, collation, m, C, (visited_hash *) v); + } + } + else + { + /* tid with ip_posid==0 is invalid */ + ItemPointerSet(&heaptid, InvalidBlockNumber, 0); + } + + return heaptid; +} diff --git a/test/expected/hnsw_relaxed.out b/test/expected/hnsw_relaxed.out new file mode 100644 index 000000000..443d5d46e --- /dev/null +++ b/test/expected/hnsw_relaxed.out @@ -0,0 +1,100 @@ +SELECT t, +Array[t,t+1,t+2]::vector(3) AS val +INTO test_where +FROM generate_series(1, 2000) as t; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; + t +------ + 1501 + 1502 +(2 rows) + +CREATE INDEX ON test_where USING hnsw (val vector_l2_ops) WITH (m=10); +set hnsw.use_relaxed=off; +set hnsw.ef_search=100; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; + t +--- +(0 rows) + +set hnsw.use_relaxed=on; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; + t +------ + 1501 + 1502 +(2 rows) + +SELECT t FROM test_where WHERE t>2000 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; + t +--- +(0 rows) + +DELETE FROM test_where; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; + t +--- +(0 rows) + +TRUNCATE test_where; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; + t +--- +(0 rows) + +DROP TABLE test_where; +SET enable_seqscan = off; +CREATE TABLE t (val vector(3)); +INSERT INTO t (val) VALUES ('[0,0,0]'), ('[1,2,3]'), ('[1,1,1]'), (NULL); +CREATE INDEX ON t USING hnsw (val vector_l2_ops); +INSERT INTO t (val) VALUES ('[1,2,4]'); +SELECT * FROM t ORDER BY val <-> '[3,3,3]'; + val +--------- + [1,2,3] + [1,2,4] + [1,1,1] + [0,0,0] +(4 rows) + +SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <-> (SELECT NULL::vector)) t2; + count +------- + 4 +(1 row) + +SELECT COUNT(*) FROM t; + count +------- + 5 +(1 row) + +TRUNCATE t; +SELECT * FROM t ORDER BY val <-> '[3,3,3]'; + val +----- +(0 rows) + +DROP TABLE t; +SET enable_seqscan = off; +CREATE TABLE t (val vector(3)); +INSERT INTO t (val) VALUES ('[0,0,0]'), ('[1,2,3]'), ('[1,1,1]'), (NULL); +CREATE INDEX ON t USING hnsw (val vector_ip_ops); +INSERT INTO t (val) VALUES ('[1,2,4]'); +SELECT * FROM t ORDER BY val <#> '[3,3,3]'; + val +--------- + [1,2,4] + [1,2,3] + [1,1,1] + [0,0,0] +(4 rows) + +SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <#> (SELECT NULL::vector)) t2; + count +------- + 4 +(1 row) + +DROP TABLE t; +set hnsw.use_relaxed=off; diff --git a/test/sql/hnsw_relaxed.sql b/test/sql/hnsw_relaxed.sql new file mode 100644 index 000000000..d19100869 --- /dev/null +++ b/test/sql/hnsw_relaxed.sql @@ -0,0 +1,47 @@ +SELECT t, +Array[t,t+1,t+2]::vector(3) AS val +INTO test_where +FROM generate_series(1, 2000) as t; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; +CREATE INDEX ON test_where USING hnsw (val vector_l2_ops) WITH (m=10); +set hnsw.use_relaxed=off; +set hnsw.ef_search=100; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; +set hnsw.use_relaxed=on; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; +SELECT t FROM test_where WHERE t>2000 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; +DELETE FROM test_where; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; +TRUNCATE test_where; +SELECT t FROM test_where WHERE t>1500 ORDER BY val <-> '[1000.2,1000,1000.1]' LIMIT 2; +DROP TABLE test_where; +SET enable_seqscan = off; + +CREATE TABLE t (val vector(3)); +INSERT INTO t (val) VALUES ('[0,0,0]'), ('[1,2,3]'), ('[1,1,1]'), (NULL); +CREATE INDEX ON t USING hnsw (val vector_l2_ops); + +INSERT INTO t (val) VALUES ('[1,2,4]'); + +SELECT * FROM t ORDER BY val <-> '[3,3,3]'; +SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <-> (SELECT NULL::vector)) t2; +SELECT COUNT(*) FROM t; + +TRUNCATE t; +SELECT * FROM t ORDER BY val <-> '[3,3,3]'; + +DROP TABLE t; +SET enable_seqscan = off; + +CREATE TABLE t (val vector(3)); +INSERT INTO t (val) VALUES ('[0,0,0]'), ('[1,2,3]'), ('[1,1,1]'), (NULL); +CREATE INDEX ON t USING hnsw (val vector_ip_ops); + +INSERT INTO t (val) VALUES ('[1,2,4]'); + +SELECT * FROM t ORDER BY val <#> '[3,3,3]'; +SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <#> (SELECT NULL::vector)) t2; + +DROP TABLE t; + +set hnsw.use_relaxed=off;

view thread (15+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-524@github.com>
Permalink:  ../gh-pgvector-pgvector-524@github.com/

 ·  · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox