Compare commits

..

1 Commits
bas ... bound2

Author SHA1 Message Date
Andrew Kane
5f66019f49 Added bound option [skip ci] 2023-04-10 16:06:31 -07:00
6 changed files with 20 additions and 20 deletions

View File

@@ -1,7 +1,3 @@
## 0.4.2 (unreleased)
- Added notice when index created with little data
## 0.4.1 (2023-03-21)
- Improved performance of cosine distance

View File

@@ -153,11 +153,15 @@ By default, pgvector performs exact nearest neighbor search, which provides perf
You can add an index to use approximate nearest neighbor search, which trades some recall for performance. Unlike typical indexes, you will see different results for queries after adding an approximate index.
Three keys to achieving good recall are:
Two keys to achieving good recall are:
1. Create the index *after* the table has some data
2. Choose an appropriate number of lists (a good place to start is `rows / 1000` for up to 1M rows and `sqrt(rows)` for over 1M rows)
3. When querying, specify an appropriate number of [probes](#query-options) (higher is better for recall, lower is better for speed)
2. Choose an appropriate number of lists (lower is better for recall, higher is better for speed)
A good place to start is:
- `rows / 1000` for up to 1M rows
- `sqrt(rows)` for over 1M rows
Add an index for each distance function you want to use.
@@ -186,7 +190,7 @@ Vectors with up to 2,000 dimensions can be indexed.
Specify the number of probes (1 by default)
```sql
SET ivfflat.probes = 10;
SET ivfflat.probes = 1;
```
A higher value provides better recall at the cost of speed, and it can be set to the number of lists for exact nearest neighbor search (at which point the planner wont use the index)
@@ -195,7 +199,7 @@ Use `SET LOCAL` inside a transaction to set it for a single query
```sql
BEGIN;
SET LOCAL ivfflat.probes = 10;
SET LOCAL ivfflat.probes = 1;
SELECT ...
COMMIT;
```

View File

@@ -431,18 +431,8 @@ ComputeCenters(IvfflatBuildState * buildstate)
/* TODO Ensure within maintenance_work_mem */
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions);
if (buildstate->heap != NULL)
{
SampleRows(buildstate);
if (buildstate->samples->length < buildstate->lists)
{
ereport(NOTICE,
(errmsg("ivfflat index created with little data"),
errdetail("this will cause poor recall"),
errhint("drop the index until the table has more data")));
}
}
/* Calculate centers */
IvfflatBench("k-means", IvfflatKmeans(buildstate->index, buildstate->samples, buildstate->centers));

View File

@@ -13,6 +13,7 @@
#endif
int ivfflat_probes;
int ivfflat_bound;
static relopt_kind ivfflat_relopt_kind;
/*
@@ -32,6 +33,10 @@ _PG_init(void)
DefineCustomIntVariable("ivfflat.probes", "Sets the number of probes",
"Valid range is 1..lists.", &ivfflat_probes,
1, 1, IVFFLAT_MAX_LISTS, PGC_USERSET, 0, NULL, NULL, NULL);
DefineCustomIntVariable("ivfflat.bound", "Sets the max results from index (experimental)",
NULL, &ivfflat_bound,
0, 0, INT_MAX, PGC_USERSET, 0, NULL, NULL, NULL);
}
/*

View File

@@ -78,6 +78,7 @@
/* Variables */
extern int ivfflat_probes;
extern int ivfflat_bound;
/* Exported functions */
PGDLLEXPORT void _PG_init(void);

View File

@@ -124,7 +124,11 @@ GetScanItems(IndexScanDesc scan, Datum value)
*
* See postgres/src/backend/storage/buffer/README for description
*/
BufferAccessStrategy bas = GetAccessStrategy(BAS_NORMAL);
BufferAccessStrategy bas = GetAccessStrategy(BAS_BULKREAD);
/* Set the max number of results */
if (ivfflat_bound > 0)
tuplesort_set_bound(so->sortstate, ivfflat_bound);
/* Search closest probes lists */
while (!pairingheap_is_empty(so->listQueue))