mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-22 20:15:46 +08:00
Compare commits
1 Commits
amgetbatch
...
sparsevec-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
434ef7a5ab |
55
README.md
55
README.md
@@ -11,8 +11,6 @@ Store your vectors with the rest of your data. Supports:
|
||||
|
||||
Plus [ACID](https://en.wikipedia.org/wiki/ACID) compliance, point-in-time recovery, JOINs, and all of the other [great features](https://www.postgresql.org/about/) of Postgres
|
||||
|
||||
Have a lot of vectors? Use [quantization](#scaling) to scale
|
||||
|
||||
[](https://github.com/pgvector/pgvector/actions)
|
||||
|
||||
## Installation
|
||||
@@ -316,8 +314,6 @@ For a large number of workers, you may need to increase `max_parallel_workers` (
|
||||
|
||||
The [index options](#index-options) also have a significant impact on build time (use the defaults unless seeing low recall)
|
||||
|
||||
Use [binary quantization](#binary-quantization) for faster build times at scale
|
||||
|
||||
### Indexing Progress
|
||||
|
||||
Check [indexing progress](https://www.postgresql.org/docs/current/progress-reporting.html#CREATE-INDEX-PROGRESS-REPORTING)
|
||||
@@ -447,7 +443,13 @@ Exact indexes work well for conditions that match a low percentage of rows. Othe
|
||||
CREATE INDEX ON items USING hnsw (embedding vector_l2_ops);
|
||||
```
|
||||
|
||||
With approximate indexes, filtering is applied *after* the index is scanned. If a condition matches 10% of rows, with HNSW and the default `hnsw.ef_search` of 40, only 4 rows will match on average. For more rows, enable [iterative index scans](#iterative-index-scans), which will automatically scan more of the index when needed.
|
||||
With approximate indexes, filtering is applied *after* the index is scanned. If a condition matches 10% of rows, with HNSW and the default `hnsw.ef_search` of 40, only 4 rows will match on average. For more rows, increase `hnsw.ef_search`.
|
||||
|
||||
```sql
|
||||
SET hnsw.ef_search = 200;
|
||||
```
|
||||
|
||||
Starting with 0.8.0, you can enable [iterative index scans](#iterative-index-scans), which will automatically scan more of the index when needed.
|
||||
|
||||
```sql
|
||||
SET hnsw.iterative_scan = strict_order;
|
||||
@@ -671,10 +673,6 @@ SHOW shared_buffers;
|
||||
|
||||
Be sure to restart Postgres for changes to take effect.
|
||||
|
||||
### Storing
|
||||
|
||||
Use the `halfvec` type instead of `vector` for a smaller working set.
|
||||
|
||||
### Loading
|
||||
|
||||
Use `COPY` for bulk loading data ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/loading/example.py)).
|
||||
@@ -689,8 +687,6 @@ Add any indexes *after* loading the initial data for best performance.
|
||||
|
||||
See index build time for [HNSW](#index-build-time) and [IVFFlat](#index-build-time-1).
|
||||
|
||||
Use [binary quantization](#binary-quantization) for smaller indexes and faster build times at scale.
|
||||
|
||||
In production environments, create indexes concurrently to avoid blocking writes.
|
||||
|
||||
```sql
|
||||
@@ -721,8 +717,6 @@ SELECT * FROM items ORDER BY embedding <#> '[3,1,2]' LIMIT 5;
|
||||
|
||||
#### Approximate Search
|
||||
|
||||
Use [binary quantization](#binary-quantization) with re-ranking to keep indexes in-memory at scale.
|
||||
|
||||
To speed up queries with an IVFFlat index, increase the number of inverted lists (at the expense of recall).
|
||||
|
||||
```sql
|
||||
@@ -738,20 +732,21 @@ REINDEX INDEX CONCURRENTLY index_name;
|
||||
VACUUM table_name;
|
||||
```
|
||||
|
||||
## Scaling
|
||||
|
||||
For a smaller working set:
|
||||
|
||||
1. Use the `halfvec` type instead of `vector` for tables
|
||||
2. Use [binary quantization](#binary-quantization) for indexes (with re-ranking for search)
|
||||
|
||||
Scale vertically by increasing memory, CPU, and storage on a single instance. Use existing tools to [tune parameters](#tuning) and [monitor performance](#monitoring).
|
||||
|
||||
Scale horizontally with [replicas](https://www.postgresql.org/docs/current/hot-standby.html), or use [Citus](https://github.com/citusdata/citus), [PgDog](https://github.com/pgdogdev/pgdog), or another approach for sharding ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/citus/example.py)).
|
||||
|
||||
## Monitoring
|
||||
|
||||
Use existing tools like [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) or [PgHero](https://github.com/ankane/pghero) to monitor performance.
|
||||
Monitor performance with [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) (be sure to add it to `shared_preload_libraries`).
|
||||
|
||||
```sql
|
||||
CREATE EXTENSION pg_stat_statements;
|
||||
```
|
||||
|
||||
Get the most time-consuming queries with:
|
||||
|
||||
```sql
|
||||
SELECT query, calls, ROUND((total_plan_time + total_exec_time) / calls) AS avg_time_ms,
|
||||
ROUND((total_plan_time + total_exec_time) / 60000) AS total_time_min
|
||||
FROM pg_stat_statements ORDER BY total_plan_time + total_exec_time DESC LIMIT 20;
|
||||
```
|
||||
|
||||
Monitor recall by comparing results from approximate search with exact search.
|
||||
|
||||
@@ -762,6 +757,14 @@ SELECT ...
|
||||
COMMIT;
|
||||
```
|
||||
|
||||
## Scaling
|
||||
|
||||
Scale pgvector the same way you scale Postgres.
|
||||
|
||||
Scale vertically by increasing memory, CPU, and storage on a single instance. Use existing tools to [tune parameters](#tuning) and [monitor performance](#monitoring).
|
||||
|
||||
Scale horizontally with [replicas](https://www.postgresql.org/docs/current/hot-standby.html), or use [Citus](https://github.com/citusdata/citus) or another approach for sharding ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/citus/example.py)).
|
||||
|
||||
## Languages
|
||||
|
||||
Use pgvector from any language with a Postgres client. You can even generate and store vectors in one language and query them in another.
|
||||
@@ -875,8 +878,6 @@ No, but like other index types, you’ll likely see better performance if they d
|
||||
SELECT pg_size_pretty(pg_relation_size('index_name'));
|
||||
```
|
||||
|
||||
Use [half-precision indexing](#half-precision-indexing) or [binary quantization](#binary-quantization) for smaller indexes.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
#### Why isn’t a query using an index?
|
||||
|
||||
@@ -169,7 +169,7 @@ BitJaccardDistanceAvx512Popcount(uint32 bytes, unsigned char *ax, unsigned char
|
||||
#endif
|
||||
|
||||
TARGET_XSAVE static bool
|
||||
SupportsAvx512Popcount(void)
|
||||
SupportsAvx512Popcount()
|
||||
{
|
||||
unsigned int exx[4] = {0, 0, 0, 0};
|
||||
|
||||
|
||||
16
src/hnsw.c
16
src/hnsw.c
@@ -13,7 +13,6 @@
|
||||
#include "hnsw.h"
|
||||
#include "miscadmin.h"
|
||||
#include "nodes/pg_list.h"
|
||||
#include "storage/lwlock.h"
|
||||
#include "utils/float.h"
|
||||
#include "utils/guc.h"
|
||||
#include "utils/relcache.h"
|
||||
@@ -258,11 +257,6 @@ hnswvalidate(Oid opclassoid)
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
hnswunguardbatch(IndexScanDesc scan, IndexScanBatch batch)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Define index handler
|
||||
*
|
||||
@@ -284,7 +278,6 @@ hnswhandler(PG_FUNCTION_ARGS)
|
||||
.amconsistentequality = false,
|
||||
.amconsistentordering = false,
|
||||
.amcanbackward = false,
|
||||
.amcanmarkpos = false,
|
||||
.amcanunique = false,
|
||||
.amcanmulticol = false,
|
||||
.amoptionalkey = true,
|
||||
@@ -317,14 +310,11 @@ hnswhandler(PG_FUNCTION_ARGS)
|
||||
.amadjustmembers = NULL,
|
||||
.ambeginscan = hnswbeginscan,
|
||||
.amrescan = hnswrescan,
|
||||
.amgettuple = NULL,
|
||||
.amgetbatch = hnswgetbatch,
|
||||
.amunguardbatch = hnswunguardbatch,
|
||||
.amkillitemsbatch = NULL,
|
||||
.amgettransform = NULL,
|
||||
.amgettuple = hnswgettuple,
|
||||
.amgetbitmap = NULL,
|
||||
.amendscan = hnswendscan,
|
||||
.amposreset = NULL,
|
||||
.ammarkpos = NULL,
|
||||
.amrestrpos = NULL,
|
||||
.amestimateparallelscan = NULL,
|
||||
.aminitparallelscan = NULL,
|
||||
.amparallelrescan = NULL,
|
||||
|
||||
@@ -10,10 +10,6 @@
|
||||
#include "lib/pairingheap.h"
|
||||
#include "nodes/execnodes.h"
|
||||
#include "port.h" /* for random() */
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/condition_variable.h"
|
||||
#include "storage/lwlock.h"
|
||||
#include "storage/s_lock.h"
|
||||
#include "utils/relptr.h"
|
||||
#include "utils/sampling.h"
|
||||
#include "vector.h"
|
||||
@@ -465,7 +461,7 @@ IndexBulkDeleteResult *hnswbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResu
|
||||
IndexBulkDeleteResult *hnswvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats);
|
||||
IndexScanDesc hnswbeginscan(Relation index, int nkeys, int norderbys);
|
||||
void hnswrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys);
|
||||
IndexScanBatch hnswgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir);
|
||||
bool hnswgettuple(IndexScanDesc scan, ScanDirection dir);
|
||||
void hnswendscan(IndexScanDesc scan);
|
||||
|
||||
static inline HnswNeighborArray *
|
||||
|
||||
@@ -54,7 +54,6 @@
|
||||
#include "nodes/execnodes.h"
|
||||
#include "optimizer/optimizer.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/condition_variable.h"
|
||||
#include "tcop/tcopprot.h"
|
||||
#include "utils/datum.h"
|
||||
#include "utils/memutils.h"
|
||||
@@ -803,11 +802,7 @@ HnswParallelScanAndInsert(Relation heapRel, Relation indexRel, HnswShared * hnsw
|
||||
buildstate.hnswarea = hnswarea;
|
||||
InitAllocator(&buildstate.allocator, &HnswSharedMemoryAlloc, &buildstate);
|
||||
scan = table_beginscan_parallel(heapRel,
|
||||
ParallelTableScanFromHnswShared(hnswshared)
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
,SO_NONE
|
||||
#endif
|
||||
);
|
||||
ParallelTableScanFromHnswShared(hnswshared));
|
||||
reltuples = table_index_build_scan(heapRel, indexRel, indexInfo,
|
||||
true, progress, BuildCallback,
|
||||
(void *) &buildstate, scan);
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "nodes/execnodes.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "storage/lwlock.h"
|
||||
#include "utils/datum.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/rel.h"
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <limits.h>
|
||||
|
||||
#include "access/genam.h"
|
||||
#include "access/indexbatch.h"
|
||||
#include "access/relscan.h"
|
||||
#include "hnsw.h"
|
||||
#include "lib/pairingheap.h"
|
||||
@@ -136,11 +135,6 @@ hnswbeginscan(Relation index, int nkeys, int norderbys)
|
||||
double maxMemory;
|
||||
|
||||
scan = RelationGetIndexScan(index, nkeys, norderbys);
|
||||
scan->maxitemsbatch = hnsw_ef_search * HNSW_HEAPTIDS;
|
||||
/* unused but must be > 0 */
|
||||
scan->batch_index_opaque_static = MAXALIGN(1);
|
||||
scan->batch_index_opaque_dyn = 0;
|
||||
scan->batch_tuples_workspace = 0;
|
||||
|
||||
so = (HnswScanOpaque) palloc(sizeof(HnswScanOpaqueData));
|
||||
so->typeInfo = HnswGetTypeInfo(index);
|
||||
@@ -190,13 +184,12 @@ hnswrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int no
|
||||
}
|
||||
|
||||
/*
|
||||
* Fetch the next batch in the given scan
|
||||
* Fetch the next tuple in the given scan
|
||||
*/
|
||||
IndexScanBatch
|
||||
hnswgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir)
|
||||
bool
|
||||
hnswgettuple(IndexScanDesc scan, ScanDirection dir)
|
||||
{
|
||||
HnswScanOpaque so = (HnswScanOpaque) scan->opaque;
|
||||
IndexScanBatch batch = indexam_util_alloc_batch(scan);
|
||||
MemoryContext oldCtx = MemoryContextSwitchTo(so->tmpCtx);
|
||||
|
||||
/*
|
||||
@@ -252,7 +245,6 @@ hnswgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir)
|
||||
HnswSearchCandidate *sc;
|
||||
HnswElement element;
|
||||
ItemPointer heaptid;
|
||||
int nitems = 0;
|
||||
|
||||
if (list_length(so->w) == 0)
|
||||
{
|
||||
@@ -298,64 +290,44 @@ hnswgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir)
|
||||
break;
|
||||
}
|
||||
|
||||
while (list_length(so->w) != 0)
|
||||
sc = llast(so->w);
|
||||
element = HnswPtrAccess(base, sc->element);
|
||||
|
||||
/* Move to next element if no valid heap TIDs */
|
||||
if (element->heaptidsLength == 0)
|
||||
{
|
||||
sc = llast(so->w);
|
||||
element = HnswPtrAccess(base, sc->element);
|
||||
so->w = list_delete_last(so->w);
|
||||
|
||||
/* Move to next element if no valid heap TIDs */
|
||||
if (element->heaptidsLength == 0)
|
||||
/* Mark memory as free for next iteration */
|
||||
if (hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF)
|
||||
{
|
||||
so->w = list_delete_last(so->w);
|
||||
|
||||
/* Mark memory as free for next iteration */
|
||||
if (hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF)
|
||||
{
|
||||
pfree(element);
|
||||
pfree(sc);
|
||||
}
|
||||
|
||||
continue;
|
||||
pfree(element);
|
||||
pfree(sc);
|
||||
}
|
||||
|
||||
heaptid = &element->heaptids[--element->heaptidsLength];
|
||||
|
||||
if (hnsw_iterative_scan == HNSW_ITERATIVE_SCAN_STRICT)
|
||||
{
|
||||
if (sc->distance < so->previousDistance)
|
||||
continue;
|
||||
|
||||
so->previousDistance = sc->distance;
|
||||
}
|
||||
|
||||
batch->items[nitems].tableTid = *heaptid;
|
||||
batch->items[nitems].indexOffset = -1;
|
||||
batch->items[nitems].tupleOffset = 0;
|
||||
nitems++;
|
||||
|
||||
/* Keep batch size flexible */
|
||||
if (nitems == scan->maxitemsbatch)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Needed for strict iterative scans */
|
||||
if (nitems == 0)
|
||||
continue;
|
||||
heaptid = &element->heaptids[--element->heaptidsLength];
|
||||
|
||||
if (hnsw_iterative_scan == HNSW_ITERATIVE_SCAN_STRICT)
|
||||
{
|
||||
if (sc->distance < so->previousDistance)
|
||||
continue;
|
||||
|
||||
so->previousDistance = sc->distance;
|
||||
}
|
||||
|
||||
MemoryContextSwitchTo(oldCtx);
|
||||
|
||||
scan->xs_heaptid = *heaptid;
|
||||
scan->xs_recheck = false;
|
||||
scan->xs_recheckorderby = false;
|
||||
|
||||
batch->firstItem = 0;
|
||||
batch->lastItem = nitems - 1;
|
||||
batch->dir = ForwardScanDirection;
|
||||
return batch;
|
||||
return true;
|
||||
}
|
||||
|
||||
MemoryContextSwitchTo(oldCtx);
|
||||
indexam_util_release_batch(scan, batch);
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
#include "nodes/execnodes.h"
|
||||
#include "optimizer/optimizer.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/condition_variable.h"
|
||||
#include "tcop/tcopprot.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/rel.h"
|
||||
@@ -63,13 +62,15 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
||||
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
||||
|
||||
/*
|
||||
* Check with KMEANS_NORM_PROC that the value can be normalized since
|
||||
* spherical distance function expects unit vectors
|
||||
* Normalize with KMEANS_NORM_PROC since spherical distance function
|
||||
* expects unit vectors
|
||||
*/
|
||||
if (buildstate->kmeansnormprocinfo != NULL)
|
||||
{
|
||||
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
||||
return;
|
||||
|
||||
value = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
||||
}
|
||||
|
||||
if (samples->length < targsamples)
|
||||
@@ -80,7 +81,7 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
||||
else
|
||||
{
|
||||
if (buildstate->rowstoskip < 0)
|
||||
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, buildstate->samplerows, targsamples);
|
||||
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, samples->length, targsamples);
|
||||
|
||||
if (buildstate->rowstoskip <= 0)
|
||||
{
|
||||
@@ -96,9 +97,6 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
||||
|
||||
buildstate->rowstoskip -= 1;
|
||||
}
|
||||
|
||||
/* Increment after reservoir_get_next_S */
|
||||
buildstate->samplerows += 1;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -135,7 +133,6 @@ SampleRows(IvfflatBuildState * buildstate)
|
||||
int targsamples = buildstate->samples->maxlen;
|
||||
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
||||
|
||||
buildstate->samplerows = 0;
|
||||
buildstate->rowstoskip = -1;
|
||||
|
||||
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt());
|
||||
@@ -145,24 +142,8 @@ SampleRows(IvfflatBuildState * buildstate)
|
||||
{
|
||||
BlockNumber targblock = BlockSampler_Next(&buildstate->bs);
|
||||
|
||||
/* Set anyvisible to false like table_index_build_scan */
|
||||
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
||||
false, false, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
||||
}
|
||||
|
||||
/* Normalize if needed */
|
||||
if (buildstate->kmeansnormprocinfo != NULL)
|
||||
{
|
||||
VectorArray samples = buildstate->samples;
|
||||
|
||||
for (int i = 0; i < samples->length; i++)
|
||||
{
|
||||
Datum value = PointerGetDatum(VectorArrayGet(samples, i));
|
||||
Datum normValue = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
||||
|
||||
VectorArraySet(samples, i, DatumGetPointer(normValue));
|
||||
pfree(DatumGetPointer(normValue));
|
||||
}
|
||||
false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,9 +374,6 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In
|
||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 3, "vector", TupleDescAttr(buildstate->tupdesc, 0)->atttypid, -1, 0);
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
TupleDescFinalize(buildstate->sortdesc);
|
||||
#endif
|
||||
|
||||
buildstate->slot = MakeSingleTupleTableSlot(buildstate->sortdesc, &TTSOpsVirtual);
|
||||
|
||||
@@ -457,7 +435,7 @@ ComputeCenters(IvfflatBuildState * buildstate)
|
||||
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions, buildstate->centers->itemsize);
|
||||
if (buildstate->heap != NULL)
|
||||
{
|
||||
IvfflatBench("sample rows", SampleRows(buildstate));
|
||||
SampleRows(buildstate);
|
||||
|
||||
if (buildstate->samples->length < buildstate->lists)
|
||||
{
|
||||
@@ -672,11 +650,7 @@ IvfflatParallelScanAndSort(IvfflatSpool * ivfspool, IvfflatShared * ivfshared, S
|
||||
ivfspool->sortstate = InitBuildSortState(buildstate.sortdesc, sortmem, coordinate);
|
||||
buildstate.sortstate = ivfspool->sortstate;
|
||||
scan = table_beginscan_parallel(ivfspool->heap,
|
||||
ParallelTableScanFromIvfflatShared(ivfshared)
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
,SO_NONE
|
||||
#endif
|
||||
);
|
||||
ParallelTableScanFromIvfflatShared(ivfshared));
|
||||
reltuples = table_index_build_scan(ivfspool->heap, ivfspool->index, indexInfo,
|
||||
true, progress, BuildCallback,
|
||||
(void *) &buildstate, scan);
|
||||
|
||||
@@ -175,11 +175,6 @@ ivfflatvalidate(Oid opclassoid)
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
ivfflatunguardbatch(IndexScanDesc scan, IndexScanBatch batch)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Define index handler
|
||||
*
|
||||
@@ -201,7 +196,6 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
||||
.amconsistentequality = false,
|
||||
.amconsistentordering = false,
|
||||
.amcanbackward = false,
|
||||
.amcanmarkpos = false,
|
||||
.amcanunique = false,
|
||||
.amcanmulticol = false,
|
||||
.amoptionalkey = true,
|
||||
@@ -234,14 +228,11 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
||||
.amadjustmembers = NULL,
|
||||
.ambeginscan = ivfflatbeginscan,
|
||||
.amrescan = ivfflatrescan,
|
||||
.amgettuple = NULL,
|
||||
.amgetbatch = ivfflatgetbatch,
|
||||
.amunguardbatch = ivfflatunguardbatch,
|
||||
.amkillitemsbatch = NULL,
|
||||
.amgettransform = NULL,
|
||||
.amgettuple = ivfflatgettuple,
|
||||
.amgetbitmap = NULL,
|
||||
.amendscan = ivfflatendscan,
|
||||
.amposreset = NULL,
|
||||
.ammarkpos = NULL,
|
||||
.amrestrpos = NULL,
|
||||
.amestimateparallelscan = NULL,
|
||||
.aminitparallelscan = NULL,
|
||||
.amparallelrescan = NULL,
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include "lib/pairingheap.h"
|
||||
#include "nodes/execnodes.h"
|
||||
#include "port.h" /* for random() */
|
||||
#include "storage/condition_variable.h"
|
||||
#include "utils/sampling.h"
|
||||
#include "utils/tuplesort.h"
|
||||
#include "vector.h"
|
||||
@@ -214,8 +213,7 @@ typedef struct IvfflatBuildState
|
||||
/* Sampling */
|
||||
BlockSamplerData bs;
|
||||
ReservoirStateData rstate;
|
||||
double samplerows;
|
||||
double rowstoskip;
|
||||
int rowstoskip;
|
||||
|
||||
/* Sorting */
|
||||
Tuplesortstate *sortstate;
|
||||
@@ -344,7 +342,7 @@ IndexBulkDeleteResult *ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteR
|
||||
IndexBulkDeleteResult *ivfflatvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats);
|
||||
IndexScanDesc ivfflatbeginscan(Relation index, int nkeys, int norderbys);
|
||||
void ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys);
|
||||
IndexScanBatch ivfflatgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir);
|
||||
bool ivfflatgettuple(IndexScanDesc scan, ScanDirection dir);
|
||||
void ivfflatendscan(IndexScanDesc scan);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <float.h>
|
||||
|
||||
#include "access/genam.h"
|
||||
#include "access/indexbatch.h"
|
||||
#include "access/itup.h"
|
||||
#include "access/relscan.h"
|
||||
#include "access/tupdesc.h"
|
||||
@@ -262,11 +261,6 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
||||
MemoryContext oldCtx;
|
||||
|
||||
scan = RelationGetIndexScan(index, nkeys, norderbys);
|
||||
scan->maxitemsbatch = 1000;
|
||||
/* unused but must be > 0 */
|
||||
scan->batch_index_opaque_static = MAXALIGN(1);
|
||||
scan->batch_index_opaque_dyn = 0;
|
||||
scan->batch_tuples_workspace = 0;
|
||||
|
||||
/* Get lists and dimensions from metapage */
|
||||
IvfflatGetMetaPageInfo(index, &lists, &dimensions);
|
||||
@@ -304,9 +298,6 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
||||
so->tupdesc = CreateTemplateTupleDesc(2);
|
||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
TupleDescFinalize(so->tupdesc);
|
||||
#endif
|
||||
|
||||
/* Prep sort */
|
||||
so->sortstate = InitScanSortState(so->tupdesc);
|
||||
@@ -354,16 +345,14 @@ ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int
|
||||
}
|
||||
|
||||
/*
|
||||
* Fetch the next batch in the given scan
|
||||
* Fetch the next tuple in the given scan
|
||||
*/
|
||||
IndexScanBatch
|
||||
ivfflatgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir)
|
||||
bool
|
||||
ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
||||
{
|
||||
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
|
||||
IndexScanBatch batch = indexam_util_alloc_batch(scan);
|
||||
ItemPointer heaptid;
|
||||
bool isnull;
|
||||
int nitems = 0;
|
||||
|
||||
/*
|
||||
* Index can be used to scan backward, but Postgres doesn't support
|
||||
@@ -401,37 +390,17 @@ ivfflatgetbatch(IndexScanDesc scan, IndexScanBatch priorbatch, ScanDirection dir
|
||||
while (!tuplesort_gettupleslot(so->sortstate, true, false, so->mslot, NULL))
|
||||
{
|
||||
if (so->listIndex == so->maxProbes)
|
||||
{
|
||||
indexam_util_release_batch(scan, batch);
|
||||
return NULL;
|
||||
}
|
||||
return false;
|
||||
|
||||
IvfflatBench("GetScanItems", GetScanItems(scan, so->value));
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
heaptid = (ItemPointer) DatumGetPointer(slot_getattr(so->mslot, 2, &isnull));
|
||||
|
||||
batch->items[nitems].tableTid = *heaptid;
|
||||
batch->items[nitems].indexOffset = -1;
|
||||
batch->items[nitems].tupleOffset = 0;
|
||||
nitems++;
|
||||
|
||||
if (nitems == scan->maxitemsbatch)
|
||||
break;
|
||||
|
||||
if (!tuplesort_gettupleslot(so->sortstate, true, false, so->mslot, NULL))
|
||||
break;
|
||||
}
|
||||
heaptid = (ItemPointer) DatumGetPointer(slot_getattr(so->mslot, 2, &isnull));
|
||||
|
||||
scan->xs_heaptid = *heaptid;
|
||||
scan->xs_recheck = false;
|
||||
scan->xs_recheckorderby = false;
|
||||
|
||||
batch->firstItem = 0;
|
||||
batch->lastItem = nitems - 1;
|
||||
batch->dir = ForwardScanDirection;
|
||||
return batch;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -182,10 +182,10 @@ sparsevec_isspace(char ch)
|
||||
static int
|
||||
CompareIndices(const void *a, const void *b)
|
||||
{
|
||||
if (((const SparseInputElement *) a)->index < ((const SparseInputElement *) b)->index)
|
||||
if (((SparseInputElement *) a)->index < ((SparseInputElement *) b)->index)
|
||||
return -1;
|
||||
|
||||
if (((const SparseInputElement *) a)->index > ((const SparseInputElement *) b)->index)
|
||||
if (((SparseInputElement *) a)->index > ((SparseInputElement *) b)->index)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
@@ -895,28 +895,24 @@ SparsevecInnerProduct(SparseVector * a, SparseVector * b)
|
||||
float *ax = SPARSEVEC_VALUES(a);
|
||||
float *bx = SPARSEVEC_VALUES(b);
|
||||
float distance = 0.0;
|
||||
int bpos = 0;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0; i < a->nnz; i++)
|
||||
while (i < a->nnz && j < b->nnz)
|
||||
{
|
||||
int ai = a->indices[i];
|
||||
int bi = b->indices[j];
|
||||
|
||||
for (int j = bpos; j < b->nnz; j++)
|
||||
if (ai == bi)
|
||||
{
|
||||
int bi = b->indices[j];
|
||||
|
||||
/* Only update when the same index */
|
||||
if (ai == bi)
|
||||
distance += ax[i] * bx[j];
|
||||
|
||||
/* Update start for next iteration */
|
||||
if (ai >= bi)
|
||||
bpos = j + 1;
|
||||
|
||||
/* Found or passed it */
|
||||
if (bi >= ai)
|
||||
break;
|
||||
distance += ax[i] * bx[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
else if (ai < bi)
|
||||
i++;
|
||||
else
|
||||
j++;
|
||||
}
|
||||
|
||||
return distance;
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 180000
|
||||
PG_MODULE_MAGIC_EXT(.name = "vector", .version = "0.8.2");
|
||||
PG_MODULE_MAGIC_EXT(.name = "vector",.version = "0.8.2");
|
||||
#else
|
||||
PG_MODULE_MAGIC;
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user