mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-22 20:15:46 +08:00
Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b125a1956 | ||
|
|
fcd655d2a3 | ||
|
|
97fe28940d | ||
|
|
23c5bf6ef6 | ||
|
|
9ed4303a5e | ||
|
|
acd066463a | ||
|
|
be936075eb | ||
|
|
a9959fede2 | ||
|
|
02c4f4884c | ||
|
|
791fc2436f | ||
|
|
e7a7936bb2 | ||
|
|
ce2ba65906 | ||
|
|
023633a274 | ||
|
|
9baa051b5b | ||
|
|
ac94ac7cf1 | ||
|
|
8b819dfdc2 | ||
|
|
d9ca850faf | ||
|
|
131782999b | ||
|
|
d57ef873c2 | ||
|
|
30c86fb05a | ||
|
|
833f379ebe | ||
|
|
709fc75ce0 | ||
|
|
2bc959b3eb | ||
|
|
ec9e13b5fb | ||
|
|
95e476d570 | ||
|
|
6bc0c47a0a | ||
|
|
58eeefeef4 | ||
|
|
263e684824 | ||
|
|
21dfed5719 | ||
|
|
f3aec9fd03 | ||
|
|
31e41b3ba9 | ||
|
|
903a925662 | ||
|
|
96ff19be44 | ||
|
|
6c969bebad |
@@ -1,15 +1,18 @@
|
|||||||
## 0.7.0 (unreleased)
|
## 0.7.0 (unreleased)
|
||||||
|
|
||||||
- Added concatenate operator for vectors
|
- Added support for binary vectors to HNSW
|
||||||
|
- Added `hamming_distance` function
|
||||||
|
- Added `jaccard_distance` function
|
||||||
|
- Added `quantize_binary` function
|
||||||
|
|
||||||
## 0.6.2 (unreleased)
|
## 0.6.2 (2024-03-18)
|
||||||
|
|
||||||
- Reduced lock contention with parallel HNSW index builds
|
- Reduced lock contention with parallel HNSW index builds
|
||||||
|
|
||||||
## 0.6.1 (2024-03-04)
|
## 0.6.1 (2024-03-04)
|
||||||
|
|
||||||
- Fixed error with `ANALYZE` and vectors with different dimensions
|
- Fixed error with `ANALYZE` and vectors with different dimensions
|
||||||
- Fixed error with `shared_preload_libraries`
|
- Fixed segmentation fault with `shared_preload_libraries`
|
||||||
- Fixed vector subtraction being marked as commutative
|
- Fixed vector subtraction being marked as commutative
|
||||||
|
|
||||||
## 0.6.0 (2024-01-29)
|
## 0.6.0 (2024-01-29)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "vector",
|
"name": "vector",
|
||||||
"abstract": "Open-source vector similarity search for Postgres",
|
"abstract": "Open-source vector similarity search for Postgres",
|
||||||
"description": "Supports L2 distance, inner product, and cosine distance",
|
"description": "Supports L2 distance, inner product, and cosine distance",
|
||||||
"version": "0.6.1",
|
"version": "0.6.2",
|
||||||
"maintainer": [
|
"maintainer": [
|
||||||
"Andrew Kane <andrew@ankane.org>"
|
"Andrew Kane <andrew@ankane.org>"
|
||||||
],
|
],
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
"vector": {
|
"vector": {
|
||||||
"file": "sql/vector.sql",
|
"file": "sql/vector.sql",
|
||||||
"docfile": "README.md",
|
"docfile": "README.md",
|
||||||
"version": "0.6.1",
|
"version": "0.6.2",
|
||||||
"abstract": "Open-source vector similarity search for Postgres"
|
"abstract": "Open-source vector similarity search for Postgres"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
4
Makefile
4
Makefile
@@ -1,9 +1,9 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.6.1
|
EXTVERSION = 0.6.2
|
||||||
|
|
||||||
MODULE_big = vector
|
MODULE_big = vector
|
||||||
DATA = $(wildcard sql/*--*.sql)
|
DATA = $(wildcard sql/*--*.sql)
|
||||||
OBJS = src/hnsw.o src/hnswbuild.o src/hnswinsert.o src/hnswscan.o src/hnswutils.o src/hnswvacuum.o src/ivfbuild.o src/ivfflat.o src/ivfinsert.o src/ivfkmeans.o src/ivfscan.o src/ivfutils.o src/ivfvacuum.o src/vector.o
|
OBJS = src/bitvector.o src/hnsw.o src/hnswbuild.o src/hnswinsert.o src/hnswscan.o src/hnswutils.o src/hnswvacuum.o src/ivfbuild.o src/ivfflat.o src/ivfinsert.o src/ivfkmeans.o src/ivfscan.o src/ivfutils.o src/ivfvacuum.o src/vector.o
|
||||||
HEADERS = src/vector.h
|
HEADERS = src/vector.h
|
||||||
|
|
||||||
TESTS = $(wildcard test/sql/*.sql)
|
TESTS = $(wildcard test/sql/*.sql)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.6.1
|
EXTVERSION = 0.6.2
|
||||||
|
|
||||||
OBJS = src\hnsw.obj src\hnswbuild.obj src\hnswinsert.obj src\hnswscan.obj src\hnswutils.obj src\hnswvacuum.obj src\ivfbuild.obj src\ivfflat.obj src\ivfinsert.obj src\ivfkmeans.obj src\ivfscan.obj src\ivfutils.obj src\ivfvacuum.obj src\vector.obj
|
OBJS = src\bitvector.obj src\hnsw.obj src\hnswbuild.obj src\hnswinsert.obj src\hnswscan.obj src\hnswutils.obj src\hnswvacuum.obj src\ivfbuild.obj src\ivfflat.obj src\ivfinsert.obj src\ivfkmeans.obj src\ivfscan.obj src\ivfutils.obj src\ivfvacuum.obj src\vector.obj
|
||||||
HEADERS = src\vector.h
|
HEADERS = src\vector.h
|
||||||
|
|
||||||
REGRESS = btree cast copy functions input ivfflat_cosine ivfflat_ip ivfflat_l2 ivfflat_options ivfflat_unlogged
|
REGRESS = btree cast copy functions input ivfflat_cosine ivfflat_ip ivfflat_l2 ivfflat_options ivfflat_unlogged
|
||||||
|
|||||||
50
README.md
50
README.md
@@ -5,7 +5,7 @@ Open-source vector similarity search for Postgres
|
|||||||
Store your vectors with the rest of your data. Supports:
|
Store your vectors with the rest of your data. Supports:
|
||||||
|
|
||||||
- exact and approximate nearest neighbor search
|
- exact and approximate nearest neighbor search
|
||||||
- L2 distance, inner product, and cosine distance
|
- L2 distance, inner product, cosine distance, and more
|
||||||
- any [language](#languages) with a Postgres client
|
- any [language](#languages) with a Postgres client
|
||||||
|
|
||||||
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
|
||||||
@@ -20,7 +20,7 @@ Compile and install the extension (supports Postgres 12+)
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd /tmp
|
cd /tmp
|
||||||
git clone --branch v0.6.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.6.2 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
make
|
make
|
||||||
make install # may need sudo
|
make install # may need sudo
|
||||||
@@ -45,7 +45,7 @@ Then use `nmake` to build:
|
|||||||
```cmd
|
```cmd
|
||||||
set "PGROOT=C:\Program Files\PostgreSQL\16"
|
set "PGROOT=C:\Program Files\PostgreSQL\16"
|
||||||
cd %TEMP%
|
cd %TEMP%
|
||||||
git clone --branch v0.6.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.6.2 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
nmake /F Makefile.win
|
nmake /F Makefile.win
|
||||||
nmake /F Makefile.win install
|
nmake /F Makefile.win install
|
||||||
@@ -221,7 +221,19 @@ Cosine distance
|
|||||||
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
|
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
|
||||||
```
|
```
|
||||||
|
|
||||||
Vectors with up to 2,000 dimensions can be indexed.
|
Hamming distance - added in 0.7.0
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE INDEX ON items USING hnsw (embedding bit_hamming_ops);
|
||||||
|
```
|
||||||
|
|
||||||
|
Jaccard distance - added in 0.7.0
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE INDEX ON items USING hnsw (embedding bit_jaccard_ops);
|
||||||
|
```
|
||||||
|
|
||||||
|
Vectors with up to 2,000 dimensions can be indexed, or bit vectors with up to 64,000 dimensions.
|
||||||
|
|
||||||
### Index Options
|
### Index Options
|
||||||
|
|
||||||
@@ -604,6 +616,18 @@ and query with:
|
|||||||
SELECT * FROM items ORDER BY embedding::vector(3) <-> '[3,1,2]' LIMIT 5;
|
SELECT * FROM items ORDER BY embedding::vector(3) <-> '[3,1,2]' LIMIT 5;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Are binary vectors supported?
|
||||||
|
|
||||||
|
You can store binary vectors and perform exact nearest neighbor search by Hamming distance in Postgres without an extension ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/hash_image_search.py)).
|
||||||
|
|
||||||
|
```tsql
|
||||||
|
CREATE TABLE items (id bigserial PRIMARY KEY, embedding bit(3));
|
||||||
|
INSERT INTO items (embedding) VALUES (B'000'), (B'111');
|
||||||
|
SELECT * FROM items ORDER BY bit_count(embedding # B'101') LIMIT 5;
|
||||||
|
```
|
||||||
|
|
||||||
|
Indexing is not currently supported.
|
||||||
|
|
||||||
#### Do indexes need to fit into memory?
|
#### Do indexes need to fit into memory?
|
||||||
|
|
||||||
No, but like other index types, you’ll likely see better performance if they do. You can get the size of an index with:
|
No, but like other index types, you’ll likely see better performance if they do. You can get the size of an index with:
|
||||||
@@ -682,7 +706,6 @@ Operator | Description | Added
|
|||||||
\+ | element-wise addition |
|
\+ | element-wise addition |
|
||||||
\- | element-wise subtraction |
|
\- | element-wise subtraction |
|
||||||
\* | element-wise multiplication | 0.5.0
|
\* | element-wise multiplication | 0.5.0
|
||||||
\|\| | concatenate | 0.7.0
|
|
||||||
<-> | Euclidean distance |
|
<-> | Euclidean distance |
|
||||||
<#> | negative inner product |
|
<#> | negative inner product |
|
||||||
<=> | cosine distance |
|
<=> | cosine distance |
|
||||||
@@ -695,6 +718,7 @@ cosine_distance(vector, vector) → double precision | cosine distance |
|
|||||||
inner_product(vector, vector) → double precision | inner product |
|
inner_product(vector, vector) → double precision | inner product |
|
||||||
l2_distance(vector, vector) → double precision | Euclidean distance |
|
l2_distance(vector, vector) → double precision | Euclidean distance |
|
||||||
l1_distance(vector, vector) → double precision | taxicab distance | 0.5.0
|
l1_distance(vector, vector) → double precision | taxicab distance | 0.5.0
|
||||||
|
quantize_binary(vector) → bit | quantize | 0.7.0
|
||||||
vector_dims(vector) → integer | number of dimensions |
|
vector_dims(vector) → integer | number of dimensions |
|
||||||
vector_norm(vector) → double precision | Euclidean norm |
|
vector_norm(vector) → double precision | Euclidean norm |
|
||||||
|
|
||||||
@@ -705,6 +729,20 @@ Function | Description | Added
|
|||||||
avg(vector) → vector | average |
|
avg(vector) → vector | average |
|
||||||
sum(vector) → vector | sum | 0.5.0
|
sum(vector) → vector | sum | 0.5.0
|
||||||
|
|
||||||
|
### Bit Operators
|
||||||
|
|
||||||
|
Operator | Description | Added
|
||||||
|
--- | --- | ---
|
||||||
|
<~> | Hamming distance | 0.7.0
|
||||||
|
<%> | Jaccard distance | 0.7.0
|
||||||
|
|
||||||
|
### Bit Functions
|
||||||
|
|
||||||
|
Function | Description | Added
|
||||||
|
--- | --- | ---
|
||||||
|
hamming_distance(bit, bit) → double precision | Hamming distance | 0.7.0
|
||||||
|
jaccard_distance(bit, bit) → double precision | Jaccard distance | 0.7.0
|
||||||
|
|
||||||
## Installation Notes - Linux and Mac
|
## Installation Notes - Linux and Mac
|
||||||
|
|
||||||
### Postgres Location
|
### Postgres Location
|
||||||
@@ -780,7 +818,7 @@ This adds pgvector to the [Postgres image](https://hub.docker.com/_/postgres) (r
|
|||||||
You can also build the image manually:
|
You can also build the image manually:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone --branch v0.6.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.6.2 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
docker build --build-arg PG_MAJOR=16 -t myuser/pgvector .
|
docker build --build-arg PG_MAJOR=16 -t myuser/pgvector .
|
||||||
```
|
```
|
||||||
|
|||||||
2
sql/vector--0.6.1--0.6.2.sql
Normal file
2
sql/vector--0.6.1--0.6.2.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||||
|
\echo Use "ALTER EXTENSION vector UPDATE TO '0.6.2'" to load this file. \quit
|
||||||
@@ -1,9 +1,31 @@
|
|||||||
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||||
\echo Use "ALTER EXTENSION vector UPDATE TO '0.7.0'" to load this file. \quit
|
\echo Use "ALTER EXTENSION vector UPDATE TO '0.7.0'" to load this file. \quit
|
||||||
|
|
||||||
CREATE FUNCTION vector_concat(vector, vector) RETURNS vector
|
CREATE FUNCTION quantize_binary(vector) RETURNS bit
|
||||||
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
CREATE OPERATOR || (
|
CREATE FUNCTION hamming_distance(bit, bit) RETURNS float8
|
||||||
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_concat
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION jaccard_distance(bit, bit) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE OPERATOR <~> (
|
||||||
|
LEFTARG = bit, RIGHTARG = bit, PROCEDURE = hamming_distance,
|
||||||
|
COMMUTATOR = '<~>'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE OPERATOR <%> (
|
||||||
|
LEFTARG = bit, RIGHTARG = bit, PROCEDURE = jaccard_distance,
|
||||||
|
COMMUTATOR = '<%>'
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE OPERATOR CLASS bit_hamming_ops
|
||||||
|
FOR TYPE bit USING hnsw AS
|
||||||
|
OPERATOR 1 <~> (bit, bit) FOR ORDER BY float_ops,
|
||||||
|
FUNCTION 1 hamming_distance(bit, bit);
|
||||||
|
|
||||||
|
CREATE OPERATOR CLASS bit_jaccard_ops
|
||||||
|
FOR TYPE bit USING hnsw AS
|
||||||
|
OPERATOR 1 <%> (bit, bit) FOR ORDER BY float_ops,
|
||||||
|
FUNCTION 1 jaccard_distance(bit, bit);
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ CREATE FUNCTION vector_sub(vector, vector) RETURNS vector
|
|||||||
CREATE FUNCTION vector_mul(vector, vector) RETURNS vector
|
CREATE FUNCTION vector_mul(vector, vector) RETURNS vector
|
||||||
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION quantize_binary(vector) RETURNS bit
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
-- private functions
|
-- private functions
|
||||||
|
|
||||||
CREATE FUNCTION vector_lt(vector, vector) RETURNS bool
|
CREATE FUNCTION vector_lt(vector, vector) RETURNS bool
|
||||||
@@ -99,9 +102,6 @@ CREATE FUNCTION vector_avg(double precision[]) RETURNS vector
|
|||||||
CREATE FUNCTION vector_combine(double precision[], double precision[]) RETURNS double precision[]
|
CREATE FUNCTION vector_combine(double precision[], double precision[]) RETURNS double precision[]
|
||||||
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
CREATE FUNCTION vector_concat(vector, vector) RETURNS vector
|
|
||||||
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
|
||||||
|
|
||||||
-- aggregates
|
-- aggregates
|
||||||
|
|
||||||
CREATE AGGREGATE avg(vector) (
|
CREATE AGGREGATE avg(vector) (
|
||||||
@@ -227,10 +227,6 @@ CREATE OPERATOR > (
|
|||||||
RESTRICT = scalargtsel, JOIN = scalargtjoinsel
|
RESTRICT = scalargtsel, JOIN = scalargtjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE OPERATOR || (
|
|
||||||
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_concat
|
|
||||||
);
|
|
||||||
|
|
||||||
-- access methods
|
-- access methods
|
||||||
|
|
||||||
CREATE FUNCTION ivfflathandler(internal) RETURNS index_am_handler
|
CREATE FUNCTION ivfflathandler(internal) RETURNS index_am_handler
|
||||||
@@ -294,3 +290,31 @@ CREATE OPERATOR CLASS vector_cosine_ops
|
|||||||
OPERATOR 1 <=> (vector, vector) FOR ORDER BY float_ops,
|
OPERATOR 1 <=> (vector, vector) FOR ORDER BY float_ops,
|
||||||
FUNCTION 1 vector_negative_inner_product(vector, vector),
|
FUNCTION 1 vector_negative_inner_product(vector, vector),
|
||||||
FUNCTION 2 vector_norm(vector);
|
FUNCTION 2 vector_norm(vector);
|
||||||
|
|
||||||
|
-- bit functions
|
||||||
|
|
||||||
|
CREATE FUNCTION hamming_distance(bit, bit) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION jaccard_distance(bit, bit) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE OPERATOR <~> (
|
||||||
|
LEFTARG = bit, RIGHTARG = bit, PROCEDURE = hamming_distance,
|
||||||
|
COMMUTATOR = '<~>'
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE OPERATOR <%> (
|
||||||
|
LEFTARG = bit, RIGHTARG = bit, PROCEDURE = jaccard_distance,
|
||||||
|
COMMUTATOR = '<%>'
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE OPERATOR CLASS bit_hamming_ops
|
||||||
|
FOR TYPE bit USING hnsw AS
|
||||||
|
OPERATOR 1 <~> (bit, bit) FOR ORDER BY float_ops,
|
||||||
|
FUNCTION 1 hamming_distance(bit, bit);
|
||||||
|
|
||||||
|
CREATE OPERATOR CLASS bit_jaccard_ops
|
||||||
|
FOR TYPE bit USING hnsw AS
|
||||||
|
OPERATOR 1 <%> (bit, bit) FOR ORDER BY float_ops,
|
||||||
|
FUNCTION 1 jaccard_distance(bit, bit);
|
||||||
|
|||||||
90
src/bitvector.c
Normal file
90
src/bitvector.c
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
#include "postgres.h"
|
||||||
|
|
||||||
|
#include "bitvector.h"
|
||||||
|
#include "port/pg_bitutils.h"
|
||||||
|
#include "utils/varbit.h"
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 160000
|
||||||
|
#include "varatt.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Allocate and initialize a new bit vector
|
||||||
|
*/
|
||||||
|
VarBit *
|
||||||
|
InitBitVector(int dim)
|
||||||
|
{
|
||||||
|
VarBit *result;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
size = VARBITTOTALLEN(dim);
|
||||||
|
result = (VarBit *) palloc0(size);
|
||||||
|
SET_VARSIZE(result, size);
|
||||||
|
VARBITLEN(result) = dim;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Ensure same number of bits
|
||||||
|
*/
|
||||||
|
static inline void
|
||||||
|
CheckBitLengths(uint32 aLen, uint32 bLen)
|
||||||
|
{
|
||||||
|
if (aLen != bLen)
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_DATA_EXCEPTION),
|
||||||
|
errmsg("different bit lengths %u and %u", aLen, bLen)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the Hamming distance between two bit strings
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(hamming_distance);
|
||||||
|
Datum
|
||||||
|
hamming_distance(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
VarBit *a = PG_GETARG_VARBIT_P(0);
|
||||||
|
VarBit *b = PG_GETARG_VARBIT_P(1);
|
||||||
|
unsigned char *ax = VARBITS(a);
|
||||||
|
unsigned char *bx = VARBITS(b);
|
||||||
|
uint64 distance = 0;
|
||||||
|
|
||||||
|
CheckBitLengths(VARBITLEN(a), VARBITLEN(b));
|
||||||
|
|
||||||
|
/* TODO Improve performance */
|
||||||
|
for (uint32 i = 0; i < VARBITBYTES(a); i++)
|
||||||
|
distance += pg_number_of_ones[ax[i] ^ bx[i]];
|
||||||
|
|
||||||
|
PG_RETURN_FLOAT8((double) distance);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the Jaccard distance between two bit strings
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(jaccard_distance);
|
||||||
|
Datum
|
||||||
|
jaccard_distance(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
VarBit *a = PG_GETARG_VARBIT_P(0);
|
||||||
|
VarBit *b = PG_GETARG_VARBIT_P(1);
|
||||||
|
unsigned char *ax = VARBITS(a);
|
||||||
|
unsigned char *bx = VARBITS(b);
|
||||||
|
uint64 ab = 0;
|
||||||
|
uint64 aa;
|
||||||
|
uint64 bb;
|
||||||
|
|
||||||
|
CheckBitLengths(VARBITLEN(a), VARBITLEN(b));
|
||||||
|
|
||||||
|
/* TODO Improve performance */
|
||||||
|
for (uint32 i = 0; i < VARBITBYTES(a); i++)
|
||||||
|
ab += pg_number_of_ones[ax[i] & bx[i]];
|
||||||
|
|
||||||
|
if (ab == 0)
|
||||||
|
PG_RETURN_FLOAT8(1);
|
||||||
|
|
||||||
|
aa = pg_popcount((char *) ax, VARBITBYTES(a));
|
||||||
|
bb = pg_popcount((char *) bx, VARBITBYTES(b));
|
||||||
|
|
||||||
|
PG_RETURN_FLOAT8(1 - (ab / ((double) (aa + bb - ab))));
|
||||||
|
}
|
||||||
8
src/bitvector.h
Normal file
8
src/bitvector.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef BITVECTOR_H
|
||||||
|
#define BITVECTOR_H
|
||||||
|
|
||||||
|
#include "utils/varbit.h"
|
||||||
|
|
||||||
|
VarBit *InitBitVector(int dim);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -44,6 +44,7 @@
|
|||||||
#include "access/xact.h"
|
#include "access/xact.h"
|
||||||
#include "access/xloginsert.h"
|
#include "access/xloginsert.h"
|
||||||
#include "catalog/index.h"
|
#include "catalog/index.h"
|
||||||
|
#include "catalog/pg_type_d.h"
|
||||||
#include "commands/progress.h"
|
#include "commands/progress.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
@@ -671,6 +672,12 @@ HnswSharedMemoryAlloc(Size size, void *state)
|
|||||||
static void
|
static void
|
||||||
InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, IndexInfo *indexInfo, ForkNumber forkNum)
|
InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, IndexInfo *indexInfo, ForkNumber forkNum)
|
||||||
{
|
{
|
||||||
|
int maxDimensions = HNSW_MAX_DIM;
|
||||||
|
Oid typid = TupleDescAttr(index->rd_att, 0)->atttypid;
|
||||||
|
|
||||||
|
if (typid == BITOID || typid == VARBITOID)
|
||||||
|
maxDimensions *= 32;
|
||||||
|
|
||||||
buildstate->heap = heap;
|
buildstate->heap = heap;
|
||||||
buildstate->index = index;
|
buildstate->index = index;
|
||||||
buildstate->indexInfo = indexInfo;
|
buildstate->indexInfo = indexInfo;
|
||||||
@@ -684,8 +691,8 @@ InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, Index
|
|||||||
if (buildstate->dimensions < 0)
|
if (buildstate->dimensions < 0)
|
||||||
elog(ERROR, "column does not have dimensions");
|
elog(ERROR, "column does not have dimensions");
|
||||||
|
|
||||||
if (buildstate->dimensions > HNSW_MAX_DIM)
|
if (buildstate->dimensions > maxDimensions)
|
||||||
elog(ERROR, "column cannot have more than %d dimensions for hnsw index", HNSW_MAX_DIM);
|
elog(ERROR, "column cannot have more than %d dimensions for hnsw index", maxDimensions);
|
||||||
|
|
||||||
if (buildstate->efConstruction < 2 * buildstate->m)
|
if (buildstate->efConstruction < 2 * buildstate->m)
|
||||||
elog(ERROR, "ef_construction must be greater than or equal to 2 * m");
|
elog(ERROR, "ef_construction must be greater than or equal to 2 * m");
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include "access/relscan.h"
|
#include "access/relscan.h"
|
||||||
|
#include "bitvector.h"
|
||||||
|
#include "catalog/pg_type_d.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "pgstat.h"
|
#include "pgstat.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
@@ -73,7 +75,15 @@ GetScanValue(IndexScanDesc scan)
|
|||||||
Datum value;
|
Datum value;
|
||||||
|
|
||||||
if (scan->orderByData->sk_flags & SK_ISNULL)
|
if (scan->orderByData->sk_flags & SK_ISNULL)
|
||||||
value = PointerGetDatum(InitVector(GetDimensions(scan->indexRelation)));
|
{
|
||||||
|
Oid typid = TupleDescAttr(scan->indexRelation->rd_att, 0)->atttypid;
|
||||||
|
int dimensions = GetDimensions(scan->indexRelation);
|
||||||
|
|
||||||
|
if (typid == BITOID || typid == VARBITOID)
|
||||||
|
value = PointerGetDatum(InitBitVector(dimensions));
|
||||||
|
else
|
||||||
|
value = PointerGetDatum(InitVector(dimensions));
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
value = scan->orderByData->sk_argument;
|
value = scan->orderByData->sk_argument;
|
||||||
|
|||||||
46
src/vector.c
46
src/vector.c
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
#include "bitvector.h"
|
||||||
#include "catalog/pg_type.h"
|
#include "catalog/pg_type.h"
|
||||||
#include "common/shortest_dec.h"
|
#include "common/shortest_dec.h"
|
||||||
#include "fmgr.h"
|
#include "fmgr.h"
|
||||||
@@ -860,6 +861,26 @@ vector_mul(PG_FUNCTION_ARGS)
|
|||||||
PG_RETURN_POINTER(result);
|
PG_RETURN_POINTER(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Quantize a vector
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(quantize_binary);
|
||||||
|
Datum
|
||||||
|
quantize_binary(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
Vector *a = PG_GETARG_VECTOR_P(0);
|
||||||
|
float *ax = a->x;
|
||||||
|
VarBit *result = InitBitVector(a->dim);
|
||||||
|
unsigned char *rx = VARBITS(result);
|
||||||
|
|
||||||
|
/* TODO Improve */
|
||||||
|
for (int i = 0; i < a->dim; i++)
|
||||||
|
rx[i / 8] |= (ax[i] > 0) << (7 - (i % 8));
|
||||||
|
|
||||||
|
PG_RETURN_VARBIT_P(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Internal helper to compare vectors
|
* Internal helper to compare vectors
|
||||||
*/
|
*/
|
||||||
@@ -1160,28 +1181,3 @@ vector_avg(PG_FUNCTION_ARGS)
|
|||||||
|
|
||||||
PG_RETURN_POINTER(result);
|
PG_RETURN_POINTER(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Concat vectors
|
|
||||||
*/
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(vector_concat);
|
|
||||||
Datum
|
|
||||||
vector_concat(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
Vector *a = PG_GETARG_VECTOR_P(0);
|
|
||||||
Vector *b = PG_GETARG_VECTOR_P(1);
|
|
||||||
Vector *result;
|
|
||||||
int dim = a->dim + b->dim;
|
|
||||||
|
|
||||||
CheckDim(dim);
|
|
||||||
|
|
||||||
result = InitVector(dim);
|
|
||||||
|
|
||||||
for (int i = 0; i < a->dim; i++)
|
|
||||||
result->x[i] = a->x[i];
|
|
||||||
|
|
||||||
for (int i = 0; i < b->dim; i++)
|
|
||||||
result->x[i + a->dim] = b->x[i];
|
|
||||||
|
|
||||||
PG_RETURN_POINTER(result);
|
|
||||||
}
|
|
||||||
|
|||||||
64
test/expected/bit_functions.out
Normal file
64
test/expected/bit_functions.out
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
SELECT hamming_distance(B'111', B'111');
|
||||||
|
hamming_distance
|
||||||
|
------------------
|
||||||
|
0
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT hamming_distance(B'111', B'110');
|
||||||
|
hamming_distance
|
||||||
|
------------------
|
||||||
|
1
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT hamming_distance(B'111', B'100');
|
||||||
|
hamming_distance
|
||||||
|
------------------
|
||||||
|
2
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT hamming_distance(B'111', B'000');
|
||||||
|
hamming_distance
|
||||||
|
------------------
|
||||||
|
3
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT hamming_distance(B'111', B'00');
|
||||||
|
ERROR: different bit lengths 3 and 2
|
||||||
|
SELECT jaccard_distance(B'1111', B'1111');
|
||||||
|
jaccard_distance
|
||||||
|
------------------
|
||||||
|
0
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT jaccard_distance(B'1111', B'1110');
|
||||||
|
jaccard_distance
|
||||||
|
------------------
|
||||||
|
0.25
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT jaccard_distance(B'1111', B'1100');
|
||||||
|
jaccard_distance
|
||||||
|
------------------
|
||||||
|
0.5
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT jaccard_distance(B'1111', B'1000');
|
||||||
|
jaccard_distance
|
||||||
|
------------------
|
||||||
|
0.75
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT jaccard_distance(B'1111', B'0000');
|
||||||
|
jaccard_distance
|
||||||
|
------------------
|
||||||
|
1
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT jaccard_distance(B'1100', B'1000');
|
||||||
|
jaccard_distance
|
||||||
|
------------------
|
||||||
|
0.5
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT jaccard_distance(B'1111', B'000');
|
||||||
|
ERROR: different bit lengths 4 and 3
|
||||||
@@ -24,14 +24,6 @@ SELECT '[1e37]'::vector * '[1e37]';
|
|||||||
ERROR: value out of range: overflow
|
ERROR: value out of range: overflow
|
||||||
SELECT '[1e-37]'::vector * '[1e-37]';
|
SELECT '[1e-37]'::vector * '[1e-37]';
|
||||||
ERROR: value out of range: underflow
|
ERROR: value out of range: underflow
|
||||||
SELECT '[1,2,3]'::vector || '[4,5]'::vector;
|
|
||||||
?column?
|
|
||||||
-------------
|
|
||||||
[1,2,3,4,5]
|
|
||||||
(1 row)
|
|
||||||
|
|
||||||
SELECT array_fill(0, ARRAY[16000])::vector || '[1]'::vector;
|
|
||||||
ERROR: vector cannot have more than 16000 dimensions
|
|
||||||
SELECT '[1,2,3]'::vector = '[1,2,3]';
|
SELECT '[1,2,3]'::vector = '[1,2,3]';
|
||||||
?column?
|
?column?
|
||||||
----------
|
----------
|
||||||
@@ -216,6 +208,18 @@ SELECT l1_distance('[3e38]', '[-3e38]');
|
|||||||
Infinity
|
Infinity
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
|
SELECT quantize_binary('[1,0,-1]');
|
||||||
|
quantize_binary
|
||||||
|
-----------------
|
||||||
|
100
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT quantize_binary('[0,0.1,-0.2,-0.3,0.4,0.5,0.6,-0.7,0.8,-0.9,1]');
|
||||||
|
quantize_binary
|
||||||
|
-----------------
|
||||||
|
01001110101
|
||||||
|
(1 row)
|
||||||
|
|
||||||
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
|
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
|
||||||
avg
|
avg
|
||||||
-----------
|
-----------
|
||||||
|
|||||||
21
test/expected/hnsw_hamming.out
Normal file
21
test/expected/hnsw_hamming.out
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
SET enable_seqscan = off;
|
||||||
|
CREATE TABLE t (val bit(3));
|
||||||
|
INSERT INTO t (val) VALUES (B'000'), (B'100'), (B'111'), (NULL);
|
||||||
|
CREATE INDEX ON t USING hnsw (val bit_hamming_ops);
|
||||||
|
INSERT INTO t (val) VALUES (B'110');
|
||||||
|
SELECT * FROM t ORDER BY val <~> B'111';
|
||||||
|
val
|
||||||
|
-----
|
||||||
|
111
|
||||||
|
110
|
||||||
|
100
|
||||||
|
000
|
||||||
|
(4 rows)
|
||||||
|
|
||||||
|
SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <~> (SELECT NULL::bit)) t2;
|
||||||
|
count
|
||||||
|
-------
|
||||||
|
4
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
DROP TABLE t;
|
||||||
21
test/expected/hnsw_jaccard.out
Normal file
21
test/expected/hnsw_jaccard.out
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
SET enable_seqscan = off;
|
||||||
|
CREATE TABLE t (val bit(4));
|
||||||
|
INSERT INTO t (val) VALUES (B'0000'), (B'1100'), (B'1111'), (NULL);
|
||||||
|
CREATE INDEX ON t USING hnsw (val bit_jaccard_ops);
|
||||||
|
INSERT INTO t (val) VALUES (B'1110');
|
||||||
|
SELECT * FROM t ORDER BY val <%> B'1111';
|
||||||
|
val
|
||||||
|
------
|
||||||
|
1111
|
||||||
|
1110
|
||||||
|
1100
|
||||||
|
0000
|
||||||
|
(4 rows)
|
||||||
|
|
||||||
|
SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <%> (SELECT NULL::bit)) t2;
|
||||||
|
count
|
||||||
|
-------
|
||||||
|
4
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
DROP TABLE t;
|
||||||
@@ -116,8 +116,30 @@ SELECT '[1, ,3]'::vector;
|
|||||||
ERROR: invalid input syntax for type vector: "[1, ,3]"
|
ERROR: invalid input syntax for type vector: "[1, ,3]"
|
||||||
LINE 1: SELECT '[1, ,3]'::vector;
|
LINE 1: SELECT '[1, ,3]'::vector;
|
||||||
^
|
^
|
||||||
|
SELECT '[1,2,3]'::vector(3);
|
||||||
|
vector
|
||||||
|
---------
|
||||||
|
[1,2,3]
|
||||||
|
(1 row)
|
||||||
|
|
||||||
SELECT '[1,2,3]'::vector(2);
|
SELECT '[1,2,3]'::vector(2);
|
||||||
ERROR: expected 2 dimensions, not 3
|
ERROR: expected 2 dimensions, not 3
|
||||||
|
SELECT '[1,2,3]'::vector(3, 2);
|
||||||
|
ERROR: invalid type modifier
|
||||||
|
LINE 1: SELECT '[1,2,3]'::vector(3, 2);
|
||||||
|
^
|
||||||
|
SELECT '[1,2,3]'::vector('a');
|
||||||
|
ERROR: invalid input syntax for type integer: "a"
|
||||||
|
LINE 1: SELECT '[1,2,3]'::vector('a');
|
||||||
|
^
|
||||||
|
SELECT '[1,2,3]'::vector(0);
|
||||||
|
ERROR: dimensions for type vector must be at least 1
|
||||||
|
LINE 1: SELECT '[1,2,3]'::vector(0);
|
||||||
|
^
|
||||||
|
SELECT '[1,2,3]'::vector(16001);
|
||||||
|
ERROR: dimensions for type vector cannot exceed 16000
|
||||||
|
LINE 1: SELECT '[1,2,3]'::vector(16001);
|
||||||
|
^
|
||||||
SELECT unnest('{"[1,2,3]", "[4,5,6]"}'::vector[]);
|
SELECT unnest('{"[1,2,3]", "[4,5,6]"}'::vector[]);
|
||||||
unnest
|
unnest
|
||||||
---------
|
---------
|
||||||
|
|||||||
13
test/sql/bit_functions.sql
Normal file
13
test/sql/bit_functions.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
SELECT hamming_distance(B'111', B'111');
|
||||||
|
SELECT hamming_distance(B'111', B'110');
|
||||||
|
SELECT hamming_distance(B'111', B'100');
|
||||||
|
SELECT hamming_distance(B'111', B'000');
|
||||||
|
SELECT hamming_distance(B'111', B'00');
|
||||||
|
|
||||||
|
SELECT jaccard_distance(B'1111', B'1111');
|
||||||
|
SELECT jaccard_distance(B'1111', B'1110');
|
||||||
|
SELECT jaccard_distance(B'1111', B'1100');
|
||||||
|
SELECT jaccard_distance(B'1111', B'1000');
|
||||||
|
SELECT jaccard_distance(B'1111', B'0000');
|
||||||
|
SELECT jaccard_distance(B'1100', B'1000');
|
||||||
|
SELECT jaccard_distance(B'1111', B'000');
|
||||||
@@ -6,9 +6,6 @@ SELECT '[1,2,3]'::vector * '[4,5,6]';
|
|||||||
SELECT '[1e37]'::vector * '[1e37]';
|
SELECT '[1e37]'::vector * '[1e37]';
|
||||||
SELECT '[1e-37]'::vector * '[1e-37]';
|
SELECT '[1e-37]'::vector * '[1e-37]';
|
||||||
|
|
||||||
SELECT '[1,2,3]'::vector || '[4,5]'::vector;
|
|
||||||
SELECT array_fill(0, ARRAY[16000])::vector || '[1]'::vector;
|
|
||||||
|
|
||||||
SELECT '[1,2,3]'::vector = '[1,2,3]';
|
SELECT '[1,2,3]'::vector = '[1,2,3]';
|
||||||
SELECT '[1,2,3]'::vector = '[1,2]';
|
SELECT '[1,2,3]'::vector = '[1,2]';
|
||||||
|
|
||||||
@@ -51,6 +48,9 @@ SELECT l1_distance('[0,0]', '[0,1]');
|
|||||||
SELECT l1_distance('[1,2]', '[3]');
|
SELECT l1_distance('[1,2]', '[3]');
|
||||||
SELECT l1_distance('[3e38]', '[-3e38]');
|
SELECT l1_distance('[3e38]', '[-3e38]');
|
||||||
|
|
||||||
|
SELECT quantize_binary('[1,0,-1]');
|
||||||
|
SELECT quantize_binary('[0,0.1,-0.2,-0.3,0.4,0.5,0.6,-0.7,0.8,-0.9,1]');
|
||||||
|
|
||||||
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
|
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
|
||||||
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]', NULL]) v;
|
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]', NULL]) v;
|
||||||
SELECT avg(v) FROM unnest(ARRAY[]::vector[]) v;
|
SELECT avg(v) FROM unnest(ARRAY[]::vector[]) v;
|
||||||
|
|||||||
12
test/sql/hnsw_hamming.sql
Normal file
12
test/sql/hnsw_hamming.sql
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
SET enable_seqscan = off;
|
||||||
|
|
||||||
|
CREATE TABLE t (val bit(3));
|
||||||
|
INSERT INTO t (val) VALUES (B'000'), (B'100'), (B'111'), (NULL);
|
||||||
|
CREATE INDEX ON t USING hnsw (val bit_hamming_ops);
|
||||||
|
|
||||||
|
INSERT INTO t (val) VALUES (B'110');
|
||||||
|
|
||||||
|
SELECT * FROM t ORDER BY val <~> B'111';
|
||||||
|
SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <~> (SELECT NULL::bit)) t2;
|
||||||
|
|
||||||
|
DROP TABLE t;
|
||||||
12
test/sql/hnsw_jaccard.sql
Normal file
12
test/sql/hnsw_jaccard.sql
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
SET enable_seqscan = off;
|
||||||
|
|
||||||
|
CREATE TABLE t (val bit(4));
|
||||||
|
INSERT INTO t (val) VALUES (B'0000'), (B'1100'), (B'1111'), (NULL);
|
||||||
|
CREATE INDEX ON t USING hnsw (val bit_jaccard_ops);
|
||||||
|
|
||||||
|
INSERT INTO t (val) VALUES (B'1110');
|
||||||
|
|
||||||
|
SELECT * FROM t ORDER BY val <%> B'1111';
|
||||||
|
SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <%> (SELECT NULL::bit)) t2;
|
||||||
|
|
||||||
|
DROP TABLE t;
|
||||||
@@ -22,7 +22,13 @@ SELECT '[1,]'::vector;
|
|||||||
SELECT '[1a]'::vector;
|
SELECT '[1a]'::vector;
|
||||||
SELECT '[1,,3]'::vector;
|
SELECT '[1,,3]'::vector;
|
||||||
SELECT '[1, ,3]'::vector;
|
SELECT '[1, ,3]'::vector;
|
||||||
|
|
||||||
|
SELECT '[1,2,3]'::vector(3);
|
||||||
SELECT '[1,2,3]'::vector(2);
|
SELECT '[1,2,3]'::vector(2);
|
||||||
|
SELECT '[1,2,3]'::vector(3, 2);
|
||||||
|
SELECT '[1,2,3]'::vector('a');
|
||||||
|
SELECT '[1,2,3]'::vector(0);
|
||||||
|
SELECT '[1,2,3]'::vector(16001);
|
||||||
|
|
||||||
SELECT unnest('{"[1,2,3]", "[4,5,6]"}'::vector[]);
|
SELECT unnest('{"[1,2,3]", "[4,5,6]"}'::vector[]);
|
||||||
SELECT '{"[1,2,3]"}'::vector(2)[];
|
SELECT '{"[1,2,3]"}'::vector(2)[];
|
||||||
|
|||||||
137
test/t/020_hnsw_bit_build_recall.pl
Normal file
137
test/t/020_hnsw_bit_build_recall.pl
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use PostgresNode;
|
||||||
|
use TestLib;
|
||||||
|
use Test::More;
|
||||||
|
|
||||||
|
my $node;
|
||||||
|
my @queries = ();
|
||||||
|
my @expected;
|
||||||
|
my $limit = 20;
|
||||||
|
my $dim = 52;
|
||||||
|
my $max = 2**$dim;
|
||||||
|
|
||||||
|
sub test_recall
|
||||||
|
{
|
||||||
|
my ($min, $operator) = @_;
|
||||||
|
my $correct = 0;
|
||||||
|
my $total = 0;
|
||||||
|
|
||||||
|
my $explain = $node->safe_psql("postgres", qq(
|
||||||
|
SET enable_seqscan = off;
|
||||||
|
SET hnsw.ef_search = 100;
|
||||||
|
EXPLAIN ANALYZE SELECT i FROM tst ORDER BY v $operator $queries[0] LIMIT $limit;
|
||||||
|
));
|
||||||
|
like($explain, qr/Index Scan/);
|
||||||
|
|
||||||
|
for my $i (0 .. $#queries)
|
||||||
|
{
|
||||||
|
my $actual = $node->safe_psql("postgres", qq(
|
||||||
|
SET enable_seqscan = off;
|
||||||
|
SET hnsw.ef_search = 100;
|
||||||
|
SELECT i FROM tst ORDER BY v $operator $queries[$i] LIMIT $limit;
|
||||||
|
));
|
||||||
|
my @actual_ids = split("\n", $actual);
|
||||||
|
|
||||||
|
my @expected_ids = split("\n", $expected[$i]);
|
||||||
|
my %expected_set = map { $_ => 1 } @expected_ids;
|
||||||
|
|
||||||
|
foreach (@actual_ids)
|
||||||
|
{
|
||||||
|
if (exists($expected_set{$_}))
|
||||||
|
{
|
||||||
|
$correct++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$total += $limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp_ok($correct / $total, ">=", $min, $operator);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Initialize node
|
||||||
|
$node = get_new_node('node');
|
||||||
|
$node->init;
|
||||||
|
$node->start;
|
||||||
|
|
||||||
|
# Create table
|
||||||
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
|
$node->safe_psql("postgres", "CREATE TABLE tst (i int4, v bit($dim));");
|
||||||
|
$node->safe_psql("postgres",
|
||||||
|
"INSERT INTO tst SELECT i, (random() * $max)::bigint::bit($dim) FROM generate_series(1, 10000) i;"
|
||||||
|
);
|
||||||
|
|
||||||
|
# Generate queries
|
||||||
|
for (1 .. 20)
|
||||||
|
{
|
||||||
|
my $r = int(rand() * $max);
|
||||||
|
push(@queries, "${r}::bigint::bit($dim)");
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check each index type
|
||||||
|
my @operators = ("<~>", "<\%>");
|
||||||
|
my @opclasses = ("bit_hamming_ops", "bit_jaccard_ops");
|
||||||
|
|
||||||
|
for my $i (0 .. $#operators)
|
||||||
|
{
|
||||||
|
my $operator = $operators[$i];
|
||||||
|
my $opclass = $opclasses[$i];
|
||||||
|
|
||||||
|
# Get exact results
|
||||||
|
@expected = ();
|
||||||
|
foreach (@queries)
|
||||||
|
{
|
||||||
|
# Handle ties
|
||||||
|
my $res = $node->safe_psql("postgres", qq(
|
||||||
|
WITH top AS (
|
||||||
|
SELECT v $operator $_ AS distance FROM tst ORDER BY v $operator $_ LIMIT $limit
|
||||||
|
)
|
||||||
|
SELECT i FROM tst WHERE (v $operator $_) <= (SELECT MAX(distance) FROM top)
|
||||||
|
));
|
||||||
|
push(@expected, $res);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Build index serially
|
||||||
|
$node->safe_psql("postgres", qq(
|
||||||
|
SET max_parallel_maintenance_workers = 0;
|
||||||
|
CREATE INDEX idx ON tst USING hnsw (v $opclass);
|
||||||
|
));
|
||||||
|
|
||||||
|
# Test approximate results
|
||||||
|
my $min = $operator eq "<\%>" ? 0.96 : 0.99;
|
||||||
|
test_recall($min, $operator);
|
||||||
|
|
||||||
|
$node->safe_psql("postgres", "DROP INDEX idx;");
|
||||||
|
|
||||||
|
# Build index in parallel in memory
|
||||||
|
my ($ret, $stdout, $stderr) = $node->psql("postgres", qq(
|
||||||
|
SET client_min_messages = DEBUG;
|
||||||
|
SET min_parallel_table_scan_size = 1;
|
||||||
|
CREATE INDEX idx ON tst USING hnsw (v $opclass);
|
||||||
|
));
|
||||||
|
is($ret, 0, $stderr);
|
||||||
|
like($stderr, qr/using \d+ parallel workers/);
|
||||||
|
|
||||||
|
# Test approximate results
|
||||||
|
test_recall($min, $operator);
|
||||||
|
|
||||||
|
$node->safe_psql("postgres", "DROP INDEX idx;");
|
||||||
|
|
||||||
|
# Build index in parallel on disk
|
||||||
|
# Set parallel_workers on table to use workers with low maintenance_work_mem
|
||||||
|
($ret, $stdout, $stderr) = $node->psql("postgres", qq(
|
||||||
|
ALTER TABLE tst SET (parallel_workers = 2);
|
||||||
|
SET client_min_messages = DEBUG;
|
||||||
|
SET maintenance_work_mem = '4MB';
|
||||||
|
CREATE INDEX idx ON tst USING hnsw (v $opclass);
|
||||||
|
ALTER TABLE tst RESET (parallel_workers);
|
||||||
|
));
|
||||||
|
is($ret, 0, $stderr);
|
||||||
|
like($stderr, qr/using \d+ parallel workers/);
|
||||||
|
like($stderr, qr/hnsw graph no longer fits into maintenance_work_mem/);
|
||||||
|
|
||||||
|
$node->safe_psql("postgres", "DROP INDEX idx;");
|
||||||
|
}
|
||||||
|
|
||||||
|
done_testing();
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
comment = 'vector data type and ivfflat and hnsw access methods'
|
comment = 'vector data type and ivfflat and hnsw access methods'
|
||||||
default_version = '0.6.1'
|
default_version = '0.6.2'
|
||||||
module_pathname = '$libdir/vector'
|
module_pathname = '$libdir/vector'
|
||||||
relocatable = true
|
relocatable = true
|
||||||
|
|||||||
Reference in New Issue
Block a user