mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-22 03:57:34 +08:00
Compare commits
30 Commits
v0.8.2
...
hnsw-bench
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02fba79a28 | ||
|
|
32284ba28a | ||
|
|
1f68c73c96 | ||
|
|
f15bc0904f | ||
|
|
2b7ad083dc | ||
|
|
421a35fcdc | ||
|
|
12368bd79c | ||
|
|
88a0085459 | ||
|
|
ea23884efd | ||
|
|
3351f3d43e | ||
|
|
d238409bec | ||
|
|
529f37175b | ||
|
|
bce3946392 | ||
|
|
13cb253d30 | ||
|
|
41b3cdc011 | ||
|
|
ec02a96239 | ||
|
|
610d95b8d2 | ||
|
|
609d01f4c6 | ||
|
|
a7551a61ca | ||
|
|
5c9a97af3b | ||
|
|
8af675cd25 | ||
|
|
97e0ed4464 | ||
|
|
533ad160e0 | ||
|
|
dfbd724a1f | ||
|
|
5f270c9663 | ||
|
|
352ec5be29 | ||
|
|
17916cad00 | ||
|
|
284f2f0fec | ||
|
|
edd49863ba | ||
|
|
411291189d |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@@ -97,7 +97,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
postgres-version: ${{ matrix.postgres }}
|
postgres-version: ${{ matrix.postgres }}
|
||||||
- run: |
|
- run: |
|
||||||
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && ^
|
call "C:\Program Files\Microsoft Visual Studio\${{ matrix.os == 'windows-2025' && 18 || 2022 }}\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && ^
|
||||||
nmake /NOLOGO /F Makefile.win && ^
|
nmake /NOLOGO /F Makefile.win && ^
|
||||||
nmake /NOLOGO /F Makefile.win install && ^
|
nmake /NOLOGO /F Makefile.win install && ^
|
||||||
nmake /NOLOGO /F Makefile.win installcheck ${{ matrix.postgres != 17 && 'PG_REGRESS=$(PGROOT)\bin\pg_regress' || '' }} && ^
|
nmake /NOLOGO /F Makefile.win installcheck ${{ matrix.postgres != 17 && 'PG_REGRESS=$(PGROOT)\bin\pg_regress' || '' }} && ^
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
|
## 0.8.3 (unreleased)
|
||||||
|
|
||||||
|
- Fixed performance regression with Hamming distance and Jaccard distance with Postgres 18
|
||||||
|
|
||||||
## 0.8.2 (2026-02-25)
|
## 0.8.2 (2026-02-25)
|
||||||
|
|
||||||
- Fixed buffer overflow with parallel HNSW index build
|
- Fixed buffer overflow with parallel HNSW index build - [more info](https://github.com/pgvector/pgvector/issues/959)
|
||||||
- Improved `install` target on Windows
|
- Improved `install` target on Windows
|
||||||
- Fixed `Index Searches` in `EXPLAIN` output for Postgres 18
|
- Fixed `Index Searches` in `EXPLAIN` output for Postgres 18
|
||||||
|
|
||||||
|
|||||||
57
README.md
57
README.md
@@ -11,6 +11,8 @@ 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
|
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)
|
[](https://github.com/pgvector/pgvector/actions)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
@@ -314,6 +316,8 @@ 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)
|
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
|
### Indexing Progress
|
||||||
|
|
||||||
Check [indexing progress](https://www.postgresql.org/docs/current/progress-reporting.html#CREATE-INDEX-PROGRESS-REPORTING)
|
Check [indexing progress](https://www.postgresql.org/docs/current/progress-reporting.html#CREATE-INDEX-PROGRESS-REPORTING)
|
||||||
@@ -443,13 +447,7 @@ Exact indexes work well for conditions that match a low percentage of rows. Othe
|
|||||||
CREATE INDEX ON items USING hnsw (embedding vector_l2_ops);
|
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, increase `hnsw.ef_search`.
|
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.
|
||||||
|
|
||||||
```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
|
```sql
|
||||||
SET hnsw.iterative_scan = strict_order;
|
SET hnsw.iterative_scan = strict_order;
|
||||||
@@ -673,6 +671,10 @@ SHOW shared_buffers;
|
|||||||
|
|
||||||
Be sure to restart Postgres for changes to take effect.
|
Be sure to restart Postgres for changes to take effect.
|
||||||
|
|
||||||
|
### Storing
|
||||||
|
|
||||||
|
Use the `halfvec` type instead of `vector` for a smaller working set.
|
||||||
|
|
||||||
### Loading
|
### Loading
|
||||||
|
|
||||||
Use `COPY` for bulk loading data ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/loading/example.py)).
|
Use `COPY` for bulk loading data ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/loading/example.py)).
|
||||||
@@ -687,6 +689,8 @@ 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).
|
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.
|
In production environments, create indexes concurrently to avoid blocking writes.
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
@@ -717,6 +721,8 @@ SELECT * FROM items ORDER BY embedding <#> '[3,1,2]' LIMIT 5;
|
|||||||
|
|
||||||
#### Approximate Search
|
#### 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).
|
To speed up queries with an IVFFlat index, increase the number of inverted lists (at the expense of recall).
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
@@ -732,21 +738,20 @@ REINDEX INDEX CONCURRENTLY index_name;
|
|||||||
VACUUM table_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
|
## Monitoring
|
||||||
|
|
||||||
Monitor performance with [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) (be sure to add it to `shared_preload_libraries`).
|
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.
|
||||||
|
|
||||||
```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.
|
Monitor recall by comparing results from approximate search with exact search.
|
||||||
|
|
||||||
@@ -757,14 +762,6 @@ SELECT ...
|
|||||||
COMMIT;
|
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
|
## 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.
|
Use pgvector from any language with a Postgres client. You can even generate and store vectors in one language and query them in another.
|
||||||
@@ -878,6 +875,8 @@ No, but like other index types, you’ll likely see better performance if they d
|
|||||||
SELECT pg_size_pretty(pg_relation_size('index_name'));
|
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
|
## Troubleshooting
|
||||||
|
|
||||||
#### Why isn’t a query using an index?
|
#### Why isn’t a query using an index?
|
||||||
@@ -1330,7 +1329,7 @@ make clean && PG_CFLAGS="-DUSE_ASSERT_CHECKING" make && make install
|
|||||||
To enable benchmarking:
|
To enable benchmarking:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
make clean && PG_CFLAGS="-DIVFFLAT_BENCH" make && make install
|
make clean && PG_CFLAGS="-DHNSW_BENCH -DIVFFLAT_BENCH" make && make install
|
||||||
```
|
```
|
||||||
|
|
||||||
To show memory usage:
|
To show memory usage:
|
||||||
|
|||||||
@@ -31,10 +31,12 @@
|
|||||||
#define BIT_TARGET_CLONES
|
#define BIT_TARGET_CLONES
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Use built-ins when possible for inlining */
|
/* Use built-ins when possible for Postgres < 19 for inlining */
|
||||||
#if defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_INT_64)
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
#define popcount64(x) pg_popcount64(x)
|
||||||
|
#elif defined(HAVE__BUILTIN_POPCOUNT) && (defined(HAVE_LONG_INT_64) || SIZEOF_LONG == 8)
|
||||||
#define popcount64(x) __builtin_popcountl(x)
|
#define popcount64(x) __builtin_popcountl(x)
|
||||||
#elif defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_LONG_INT_64)
|
#elif defined(HAVE__BUILTIN_POPCOUNT) && (defined(HAVE_LONG_LONG_INT_64) || SIZEOF_LONG_LONG == 8)
|
||||||
#define popcount64(x) __builtin_popcountll(x)
|
#define popcount64(x) __builtin_popcountll(x)
|
||||||
#elif !defined(_MSC_VER)
|
#elif !defined(_MSC_VER)
|
||||||
/* Fails to resolve with MSVC */
|
/* Fails to resolve with MSVC */
|
||||||
@@ -169,7 +171,7 @@ BitJaccardDistanceAvx512Popcount(uint32 bytes, unsigned char *ax, unsigned char
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
TARGET_XSAVE static bool
|
TARGET_XSAVE static bool
|
||||||
SupportsAvx512Popcount()
|
SupportsAvx512Popcount(void)
|
||||||
{
|
{
|
||||||
unsigned int exx[4] = {0, 0, 0, 0};
|
unsigned int exx[4] = {0, 0, 0, 0};
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
#include "nodes/pg_list.h"
|
#include "nodes/pg_list.h"
|
||||||
|
#include "storage/lwlock.h"
|
||||||
#include "utils/float.h"
|
#include "utils/float.h"
|
||||||
#include "utils/guc.h"
|
#include "utils/guc.h"
|
||||||
#include "utils/relcache.h"
|
#include "utils/relcache.h"
|
||||||
|
|||||||
23
src/hnsw.h
23
src/hnsw.h
@@ -10,10 +10,18 @@
|
|||||||
#include "lib/pairingheap.h"
|
#include "lib/pairingheap.h"
|
||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "port.h" /* for random() */
|
#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/relptr.h"
|
||||||
#include "utils/sampling.h"
|
#include "utils/sampling.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
|
#ifdef HNSW_BENCH
|
||||||
|
#include "portability/instr_time.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 190000
|
#if PG_VERSION_NUM >= 190000
|
||||||
typedef Pointer Item;
|
typedef Pointer Item;
|
||||||
#endif
|
#endif
|
||||||
@@ -74,6 +82,21 @@ typedef Pointer Item;
|
|||||||
#define HnswPageGetOpaque(page) ((HnswPageOpaque) PageGetSpecialPointer(page))
|
#define HnswPageGetOpaque(page) ((HnswPageOpaque) PageGetSpecialPointer(page))
|
||||||
#define HnswPageGetMeta(page) ((HnswMetaPageData *) PageGetContents(page))
|
#define HnswPageGetMeta(page) ((HnswMetaPageData *) PageGetContents(page))
|
||||||
|
|
||||||
|
#ifdef HNSW_BENCH
|
||||||
|
#define HnswBench(name, code) \
|
||||||
|
do { \
|
||||||
|
instr_time start; \
|
||||||
|
instr_time duration; \
|
||||||
|
INSTR_TIME_SET_CURRENT(start); \
|
||||||
|
(code); \
|
||||||
|
INSTR_TIME_SET_CURRENT(duration); \
|
||||||
|
INSTR_TIME_SUBTRACT(duration, start); \
|
||||||
|
elog(INFO, "%s: %.3f ms", name, INSTR_TIME_GET_MILLISEC(duration)); \
|
||||||
|
} while (0)
|
||||||
|
#else
|
||||||
|
#define HnswBench(name, code) (code)
|
||||||
|
#endif
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 150000
|
#if PG_VERSION_NUM >= 150000
|
||||||
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
|
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
|
||||||
#define SeedRandom(seed) pg_prng_seed(&pg_global_prng_state, seed)
|
#define SeedRandom(seed) pg_prng_seed(&pg_global_prng_state, seed)
|
||||||
|
|||||||
@@ -36,6 +36,8 @@
|
|||||||
*/
|
*/
|
||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
#include "access/genam.h"
|
||||||
#include "access/parallel.h"
|
#include "access/parallel.h"
|
||||||
#include "access/relscan.h"
|
#include "access/relscan.h"
|
||||||
@@ -52,6 +54,7 @@
|
|||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "optimizer/optimizer.h"
|
#include "optimizer/optimizer.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
|
#include "storage/condition_variable.h"
|
||||||
#include "tcop/tcopprot.h"
|
#include "tcop/tcopprot.h"
|
||||||
#include "utils/datum.h"
|
#include "utils/datum.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
@@ -800,7 +803,11 @@ HnswParallelScanAndInsert(Relation heapRel, Relation indexRel, HnswShared * hnsw
|
|||||||
buildstate.hnswarea = hnswarea;
|
buildstate.hnswarea = hnswarea;
|
||||||
InitAllocator(&buildstate.allocator, &HnswSharedMemoryAlloc, &buildstate);
|
InitAllocator(&buildstate.allocator, &HnswSharedMemoryAlloc, &buildstate);
|
||||||
scan = table_beginscan_parallel(heapRel,
|
scan = table_beginscan_parallel(heapRel,
|
||||||
ParallelTableScanFromHnswShared(hnswshared));
|
ParallelTableScanFromHnswShared(hnswshared)
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
,SO_NONE
|
||||||
|
#endif
|
||||||
|
);
|
||||||
reltuples = table_index_build_scan(heapRel, indexRel, indexInfo,
|
reltuples = table_index_build_scan(heapRel, indexRel, indexInfo,
|
||||||
true, progress, BuildCallback,
|
true, progress, BuildCallback,
|
||||||
(void *) &buildstate, scan);
|
(void *) &buildstate, scan);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "storage/lmgr.h"
|
#include "storage/lmgr.h"
|
||||||
|
#include "storage/lwlock.h"
|
||||||
#include "utils/datum.h"
|
#include "utils/datum.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
#include "utils/rel.h"
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
#include "access/genam.h"
|
||||||
#include "access/relscan.h"
|
#include "access/relscan.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
@@ -151,7 +153,7 @@ hnswbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
/* Calculate max memory */
|
/* Calculate max memory */
|
||||||
/* Add 256 extra bytes to fill last block when close */
|
/* Add 256 extra bytes to fill last block when close */
|
||||||
maxMemory = (double) work_mem * hnsw_scan_mem_multiplier * 1024.0 + 256;
|
maxMemory = (double) work_mem * hnsw_scan_mem_multiplier * 1024.0 + 256;
|
||||||
so->maxMemory = Min(maxMemory, (double) SIZE_MAX);
|
so->maxMemory = Min(maxMemory, (double) (SIZE_MAX / 2));
|
||||||
|
|
||||||
scan->opaque = so;
|
scan->opaque = so;
|
||||||
|
|
||||||
|
|||||||
@@ -634,13 +634,13 @@ hnswbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
|
|||||||
InitVacuumState(&vacuumstate, info, stats, callback, callback_state);
|
InitVacuumState(&vacuumstate, info, stats, callback, callback_state);
|
||||||
|
|
||||||
/* Pass 1: Remove heap TIDs */
|
/* Pass 1: Remove heap TIDs */
|
||||||
RemoveHeapTids(&vacuumstate);
|
HnswBench("RemoveHeapTids", RemoveHeapTids(&vacuumstate));
|
||||||
|
|
||||||
/* Pass 2: Repair graph */
|
/* Pass 2: Repair graph */
|
||||||
RepairGraph(&vacuumstate);
|
HnswBench("RepairGraph", RepairGraph(&vacuumstate));
|
||||||
|
|
||||||
/* Pass 3: Mark as deleted */
|
/* Pass 3: Mark as deleted */
|
||||||
MarkDeleted(&vacuumstate);
|
HnswBench("MarkDeleted", MarkDeleted(&vacuumstate));
|
||||||
|
|
||||||
FreeVacuumState(&vacuumstate);
|
FreeVacuumState(&vacuumstate);
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "optimizer/optimizer.h"
|
#include "optimizer/optimizer.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
|
#include "storage/condition_variable.h"
|
||||||
#include "tcop/tcopprot.h"
|
#include "tcop/tcopprot.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
#include "utils/rel.h"
|
||||||
@@ -62,15 +63,13 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Normalize with KMEANS_NORM_PROC since spherical distance function
|
* Check with KMEANS_NORM_PROC that the value can be normalized since
|
||||||
* expects unit vectors
|
* spherical distance function expects unit vectors
|
||||||
*/
|
*/
|
||||||
if (buildstate->kmeansnormprocinfo != NULL)
|
if (buildstate->kmeansnormprocinfo != NULL)
|
||||||
{
|
{
|
||||||
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
value = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (samples->length < targsamples)
|
if (samples->length < targsamples)
|
||||||
@@ -81,7 +80,7 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (buildstate->rowstoskip < 0)
|
if (buildstate->rowstoskip < 0)
|
||||||
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, samples->length, targsamples);
|
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, buildstate->samplerows, targsamples);
|
||||||
|
|
||||||
if (buildstate->rowstoskip <= 0)
|
if (buildstate->rowstoskip <= 0)
|
||||||
{
|
{
|
||||||
@@ -97,6 +96,9 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
|
|
||||||
buildstate->rowstoskip -= 1;
|
buildstate->rowstoskip -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Increment after reservoir_get_next_S */
|
||||||
|
buildstate->samplerows += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -133,6 +135,7 @@ SampleRows(IvfflatBuildState * buildstate)
|
|||||||
int targsamples = buildstate->samples->maxlen;
|
int targsamples = buildstate->samples->maxlen;
|
||||||
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
||||||
|
|
||||||
|
buildstate->samplerows = 0;
|
||||||
buildstate->rowstoskip = -1;
|
buildstate->rowstoskip = -1;
|
||||||
|
|
||||||
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt());
|
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt());
|
||||||
@@ -142,8 +145,24 @@ SampleRows(IvfflatBuildState * buildstate)
|
|||||||
{
|
{
|
||||||
BlockNumber targblock = BlockSampler_Next(&buildstate->bs);
|
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,
|
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
||||||
false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,6 +393,9 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In
|
|||||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
||||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
||||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 3, "vector", TupleDescAttr(buildstate->tupdesc, 0)->atttypid, -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);
|
buildstate->slot = MakeSingleTupleTableSlot(buildstate->sortdesc, &TTSOpsVirtual);
|
||||||
|
|
||||||
@@ -435,7 +457,7 @@ ComputeCenters(IvfflatBuildState * buildstate)
|
|||||||
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions, buildstate->centers->itemsize);
|
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions, buildstate->centers->itemsize);
|
||||||
if (buildstate->heap != NULL)
|
if (buildstate->heap != NULL)
|
||||||
{
|
{
|
||||||
SampleRows(buildstate);
|
IvfflatBench("sample rows", SampleRows(buildstate));
|
||||||
|
|
||||||
if (buildstate->samples->length < buildstate->lists)
|
if (buildstate->samples->length < buildstate->lists)
|
||||||
{
|
{
|
||||||
@@ -650,7 +672,11 @@ IvfflatParallelScanAndSort(IvfflatSpool * ivfspool, IvfflatShared * ivfshared, S
|
|||||||
ivfspool->sortstate = InitBuildSortState(buildstate.sortdesc, sortmem, coordinate);
|
ivfspool->sortstate = InitBuildSortState(buildstate.sortdesc, sortmem, coordinate);
|
||||||
buildstate.sortstate = ivfspool->sortstate;
|
buildstate.sortstate = ivfspool->sortstate;
|
||||||
scan = table_beginscan_parallel(ivfspool->heap,
|
scan = table_beginscan_parallel(ivfspool->heap,
|
||||||
ParallelTableScanFromIvfflatShared(ivfshared));
|
ParallelTableScanFromIvfflatShared(ivfshared)
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
,SO_NONE
|
||||||
|
#endif
|
||||||
|
);
|
||||||
reltuples = table_index_build_scan(ivfspool->heap, ivfspool->index, indexInfo,
|
reltuples = table_index_build_scan(ivfspool->heap, ivfspool->index, indexInfo,
|
||||||
true, progress, BuildCallback,
|
true, progress, BuildCallback,
|
||||||
(void *) &buildstate, scan);
|
(void *) &buildstate, scan);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "lib/pairingheap.h"
|
#include "lib/pairingheap.h"
|
||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "port.h" /* for random() */
|
#include "port.h" /* for random() */
|
||||||
|
#include "storage/condition_variable.h"
|
||||||
#include "utils/sampling.h"
|
#include "utils/sampling.h"
|
||||||
#include "utils/tuplesort.h"
|
#include "utils/tuplesort.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
@@ -213,7 +214,8 @@ typedef struct IvfflatBuildState
|
|||||||
/* Sampling */
|
/* Sampling */
|
||||||
BlockSamplerData bs;
|
BlockSamplerData bs;
|
||||||
ReservoirStateData rstate;
|
ReservoirStateData rstate;
|
||||||
int rowstoskip;
|
double samplerows;
|
||||||
|
double rowstoskip;
|
||||||
|
|
||||||
/* Sorting */
|
/* Sorting */
|
||||||
Tuplesortstate *sortstate;
|
Tuplesortstate *sortstate;
|
||||||
|
|||||||
@@ -298,6 +298,9 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
so->tupdesc = CreateTemplateTupleDesc(2);
|
so->tupdesc = CreateTemplateTupleDesc(2);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
TupleDescFinalize(so->tupdesc);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Prep sort */
|
/* Prep sort */
|
||||||
so->sortstate = InitScanSortState(so->tupdesc);
|
so->sortstate = InitScanSortState(so->tupdesc);
|
||||||
|
|||||||
@@ -182,10 +182,10 @@ sparsevec_isspace(char ch)
|
|||||||
static int
|
static int
|
||||||
CompareIndices(const void *a, const void *b)
|
CompareIndices(const void *a, const void *b)
|
||||||
{
|
{
|
||||||
if (((SparseInputElement *) a)->index < ((SparseInputElement *) b)->index)
|
if (((const SparseInputElement *) a)->index < ((const SparseInputElement *) b)->index)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (((SparseInputElement *) a)->index > ((SparseInputElement *) b)->index)
|
if (((const SparseInputElement *) a)->index > ((const SparseInputElement *) b)->index)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -40,7 +40,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 180000
|
#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
|
#else
|
||||||
PG_MODULE_MAGIC;
|
PG_MODULE_MAGIC;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user