mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-22 20:15:46 +08:00
Compare commits
1 Commits
hnsw-entry
...
ci6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
491804d99f |
78
.github/workflows/build.yml
vendored
78
.github/workflows/build.yml
vendored
@@ -28,10 +28,8 @@ jobs:
|
|||||||
dev-files: true
|
dev-files: true
|
||||||
- run: make
|
- run: make
|
||||||
env:
|
env:
|
||||||
PG_CFLAGS: -DUSE_ASSERT_CHECKING -Wall -Wextra -Werror -Wno-unused-parameter -Wno-sign-compare
|
PG_CFLAGS: -Wall -Wextra -Werror -Wno-unused-parameter -Wno-sign-compare
|
||||||
- run: |
|
- run: sudo make install
|
||||||
export PG_CONFIG=`which pg_config`
|
|
||||||
sudo --preserve-env=PG_CONFIG make install
|
|
||||||
- run: make installcheck
|
- run: make installcheck
|
||||||
- if: ${{ failure() }}
|
- if: ${{ failure() }}
|
||||||
run: cat regression.diffs
|
run: cat regression.diffs
|
||||||
@@ -39,75 +37,3 @@ jobs:
|
|||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install libipc-run-perl
|
sudo apt-get install libipc-run-perl
|
||||||
- run: make prove_installcheck
|
- run: make prove_installcheck
|
||||||
mac:
|
|
||||||
runs-on: macos-latest
|
|
||||||
if: ${{ !startsWith(github.ref_name, 'windows') }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: ankane/setup-postgres@v1
|
|
||||||
with:
|
|
||||||
postgres-version: 14
|
|
||||||
- run: make
|
|
||||||
env:
|
|
||||||
PG_CFLAGS: -DUSE_ASSERT_CHECKING -Wall -Wextra -Werror -Wno-unused-parameter
|
|
||||||
- run: make install
|
|
||||||
- run: make installcheck
|
|
||||||
- if: ${{ failure() }}
|
|
||||||
run: cat regression.diffs
|
|
||||||
- run: |
|
|
||||||
brew install cpanm
|
|
||||||
cpanm --notest IPC::Run
|
|
||||||
wget -q https://github.com/postgres/postgres/archive/refs/tags/REL_14_10.tar.gz
|
|
||||||
tar xf REL_14_10.tar.gz
|
|
||||||
- run: make prove_installcheck PROVE_FLAGS="-I ./postgres-REL_14_10/src/test/perl" PERL5LIB="/Users/runner/perl5/lib/perl5"
|
|
||||||
- run: make clean && /usr/local/opt/llvm@15/bin/scan-build --status-bugs make
|
|
||||||
env:
|
|
||||||
PG_CFLAGS: -DUSE_ASSERT_CHECKING
|
|
||||||
windows:
|
|
||||||
runs-on: windows-latest
|
|
||||||
if: ${{ !startsWith(github.ref_name, 'mac') }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: ankane/setup-postgres@v1
|
|
||||||
with:
|
|
||||||
postgres-version: 14
|
|
||||||
- run: |
|
|
||||||
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && ^
|
|
||||||
nmake /NOLOGO /F Makefile.win && ^
|
|
||||||
nmake /NOLOGO /F Makefile.win install && ^
|
|
||||||
nmake /NOLOGO /F Makefile.win installcheck && ^
|
|
||||||
nmake /NOLOGO /F Makefile.win clean && ^
|
|
||||||
nmake /NOLOGO /F Makefile.win uninstall
|
|
||||||
shell: cmd
|
|
||||||
i386:
|
|
||||||
if: ${{ !startsWith(github.ref_name, 'mac') && !startsWith(github.ref_name, 'windows') }}
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
container:
|
|
||||||
image: debian:12
|
|
||||||
options: --platform linux/386
|
|
||||||
steps:
|
|
||||||
- run: apt-get update && apt-get install -y build-essential git libipc-run-perl postgresql-15 postgresql-server-dev-15 sudo
|
|
||||||
- run: service postgresql start
|
|
||||||
- run: |
|
|
||||||
git clone https://github.com/${{ github.repository }}.git pgvector
|
|
||||||
cd pgvector
|
|
||||||
git fetch origin ${{ github.ref }}
|
|
||||||
git reset --hard FETCH_HEAD
|
|
||||||
make
|
|
||||||
make install
|
|
||||||
chown -R postgres .
|
|
||||||
sudo -u postgres make installcheck
|
|
||||||
sudo -u postgres make prove_installcheck
|
|
||||||
env:
|
|
||||||
PG_CFLAGS: -DUSE_ASSERT_CHECKING -Wall -Wextra -Werror -Wno-unused-parameter -Wno-sign-compare
|
|
||||||
valgrind:
|
|
||||||
if: ${{ !startsWith(github.ref_name, 'mac') && !startsWith(github.ref_name, 'windows') }}
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: ankane/setup-postgres-valgrind@v1
|
|
||||||
with:
|
|
||||||
postgres-version: 16
|
|
||||||
- run: make
|
|
||||||
- run: sudo --preserve-env=PG_CONFIG make install
|
|
||||||
- run: make installcheck
|
|
||||||
|
|||||||
@@ -1,12 +1,6 @@
|
|||||||
## 0.6.2 (unreleased)
|
## 0.6.1 (unreleased)
|
||||||
|
|
||||||
- Reduced lock contention with parallel HNSW index builds
|
|
||||||
|
|
||||||
## 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 vector subtraction being marked as commutative
|
|
||||||
|
|
||||||
## 0.6.0 (2024-01-29)
|
## 0.6.0 (2024-01-29)
|
||||||
|
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
|||||||
Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
|
||||||
|
|
||||||
Portions Copyright (c) 1994, The Regents of the University of California
|
Portions Copyright (c) 1994, The Regents of the University of California
|
||||||
|
|
||||||
|
|||||||
@@ -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.0",
|
||||||
"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.0",
|
||||||
"abstract": "Open-source vector similarity search for Postgres"
|
"abstract": "Open-source vector similarity search for Postgres"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
8
Makefile
8
Makefile
@@ -1,5 +1,5 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.6.1
|
EXTVERSION = 0.6.0
|
||||||
|
|
||||||
MODULE_big = vector
|
MODULE_big = vector
|
||||||
DATA = $(wildcard sql/*--*.sql)
|
DATA = $(wildcard sql/*--*.sql)
|
||||||
@@ -71,9 +71,11 @@ PG_MAJOR ?= 16
|
|||||||
.PHONY: docker
|
.PHONY: docker
|
||||||
|
|
||||||
docker:
|
docker:
|
||||||
docker build --pull --no-cache --build-arg PG_MAJOR=$(PG_MAJOR) -t pgvector/pgvector:pg$(PG_MAJOR) -t pgvector/pgvector:$(EXTVERSION)-pg$(PG_MAJOR) .
|
docker build --pull --no-cache --build-arg PG_MAJOR=$(PG_MAJOR) -t pgvector/pgvector:pg$(PG_MAJOR) .
|
||||||
|
docker build --build-arg PG_MAJOR=$(PG_MAJOR) -t pgvector/pgvector:$(EXTVERSION)-pg$(PG_MAJOR) .
|
||||||
|
|
||||||
.PHONY: docker-release
|
.PHONY: docker-release
|
||||||
|
|
||||||
docker-release:
|
docker-release:
|
||||||
docker buildx build --push --pull --no-cache --platform linux/amd64,linux/arm64 --build-arg PG_MAJOR=$(PG_MAJOR) -t pgvector/pgvector:pg$(PG_MAJOR) -t pgvector/pgvector:$(EXTVERSION)-pg$(PG_MAJOR) .
|
docker buildx build --push --pull --no-cache --platform linux/amd64,linux/arm64 --build-arg PG_MAJOR=$(PG_MAJOR) -t pgvector/pgvector:pg$(PG_MAJOR) .
|
||||||
|
docker buildx build --push --platform linux/amd64,linux/arm64 --build-arg PG_MAJOR=$(PG_MAJOR) -t pgvector/pgvector:$(EXTVERSION)-pg$(PG_MAJOR) .
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.6.1
|
EXTVERSION = 0.6.0
|
||||||
|
|
||||||
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\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
|
||||||
|
|||||||
30
README.md
30
README.md
@@ -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.0 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
make
|
make
|
||||||
make install # may need sudo
|
make install # may need sudo
|
||||||
@@ -44,7 +44,7 @@ Then use `nmake` to build:
|
|||||||
|
|
||||||
```cmd
|
```cmd
|
||||||
set "PGROOT=C:\Program Files\PostgreSQL\16"
|
set "PGROOT=C:\Program Files\PostgreSQL\16"
|
||||||
git clone --branch v0.6.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.6.0 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
|
||||||
@@ -102,12 +102,6 @@ Insert vectors
|
|||||||
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
|
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
|
||||||
```
|
```
|
||||||
|
|
||||||
Or load vectors in bulk using `COPY` ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/bulk_loading.py))
|
|
||||||
|
|
||||||
```sql
|
|
||||||
COPY items (embedding) FROM STDIN WITH (FORMAT BINARY);
|
|
||||||
```
|
|
||||||
|
|
||||||
Upsert vectors
|
Upsert vectors
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
@@ -270,8 +264,6 @@ HINT: Increase maintenance_work_mem to speed up builds.
|
|||||||
|
|
||||||
Note: Do not set `maintenance_work_mem` so high that it exhausts the memory on the server
|
Note: Do not set `maintenance_work_mem` so high that it exhausts the memory on the server
|
||||||
|
|
||||||
Like other index types, it’s faster to create an index after loading your initial data
|
|
||||||
|
|
||||||
Starting with 0.6.0, you can also speed up index creation by increasing the number of parallel workers (2 by default)
|
Starting with 0.6.0, you can also speed up index creation by increasing the number of parallel workers (2 by default)
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
@@ -581,10 +573,6 @@ or choose to store vectors inline:
|
|||||||
ALTER TABLE items ALTER COLUMN embedding SET STORAGE PLAIN;
|
ALTER TABLE items ALTER COLUMN embedding SET STORAGE PLAIN;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Why are there less results for a query after adding an HNSW index?
|
|
||||||
|
|
||||||
Results are limited by the size of the dynamic candidate list (`hnsw.ef_search`). There may be even less results due to dead tuples or filtering conditions in the query. We recommend setting `hnsw.ef_search` to at least twice the `LIMIT` of the query. If you need more than 500 results, use an IVFFlat index instead.
|
|
||||||
|
|
||||||
#### Why are there less results for a query after adding an IVFFlat index?
|
#### Why are there less results for a query after adding an IVFFlat index?
|
||||||
|
|
||||||
The index was likely created with too little data for the number of lists. Drop the index until the table has more data.
|
The index was likely created with too little data for the number of lists. Drop the index until the table has more data.
|
||||||
@@ -593,8 +581,6 @@ The index was likely created with too little data for the number of lists. Drop
|
|||||||
DROP INDEX index_name;
|
DROP INDEX index_name;
|
||||||
```
|
```
|
||||||
|
|
||||||
Results can also be limited by the number of probes (`ivfflat.probes`).
|
|
||||||
|
|
||||||
## Reference
|
## Reference
|
||||||
|
|
||||||
### Vector Type
|
### Vector Type
|
||||||
@@ -670,14 +656,6 @@ Note: Replace `16` with your Postgres server version
|
|||||||
|
|
||||||
If compilation fails and the output includes `warning: no such sysroot directory` on Mac, reinstall Xcode Command Line Tools.
|
If compilation fails and the output includes `warning: no such sysroot directory` on Mac, reinstall Xcode Command Line Tools.
|
||||||
|
|
||||||
### Portability
|
|
||||||
|
|
||||||
To compile for portability, use:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
make OPTFLAGS=""
|
|
||||||
```
|
|
||||||
|
|
||||||
## Additional Installation Methods
|
## Additional Installation Methods
|
||||||
|
|
||||||
### Docker
|
### Docker
|
||||||
@@ -693,7 +671,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.0 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 .
|
||||||
```
|
```
|
||||||
@@ -749,7 +727,7 @@ pkg install postgresql15-pg_vector
|
|||||||
or the port with:
|
or the port with:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd /usr/ports/databases/pgvector
|
cd /usr/ports/databases/pg_vector
|
||||||
make install
|
make install
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
|
||||||
\echo Use "ALTER EXTENSION vector UPDATE TO '0.6.1'" to load this file. \quit
|
|
||||||
|
|
||||||
DROP OPERATOR - (vector, vector);
|
|
||||||
|
|
||||||
CREATE OPERATOR - (
|
|
||||||
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_sub
|
|
||||||
);
|
|
||||||
|
|
||||||
ALTER OPERATOR <= (vector, vector) SET (
|
|
||||||
RESTRICT = scalarlesel, JOIN = scalarlejoinsel
|
|
||||||
);
|
|
||||||
|
|
||||||
ALTER OPERATOR >= (vector, vector) SET (
|
|
||||||
RESTRICT = scalargesel, JOIN = scalargejoinsel
|
|
||||||
);
|
|
||||||
@@ -180,7 +180,8 @@ CREATE OPERATOR + (
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE OPERATOR - (
|
CREATE OPERATOR - (
|
||||||
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_sub
|
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_sub,
|
||||||
|
COMMUTATOR = -
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE OPERATOR * (
|
CREATE OPERATOR * (
|
||||||
@@ -194,10 +195,11 @@ CREATE OPERATOR < (
|
|||||||
RESTRICT = scalarltsel, JOIN = scalarltjoinsel
|
RESTRICT = scalarltsel, JOIN = scalarltjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- should use scalarlesel and scalarlejoinsel, but not supported in Postgres < 11
|
||||||
CREATE OPERATOR <= (
|
CREATE OPERATOR <= (
|
||||||
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_le,
|
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_le,
|
||||||
COMMUTATOR = >= , NEGATOR = > ,
|
COMMUTATOR = >= , NEGATOR = > ,
|
||||||
RESTRICT = scalarlesel, JOIN = scalarlejoinsel
|
RESTRICT = scalarltsel, JOIN = scalarltjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE OPERATOR = (
|
CREATE OPERATOR = (
|
||||||
@@ -212,10 +214,11 @@ CREATE OPERATOR <> (
|
|||||||
RESTRICT = eqsel, JOIN = eqjoinsel
|
RESTRICT = eqsel, JOIN = eqjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- should use scalargesel and scalargejoinsel, but not supported in Postgres < 11
|
||||||
CREATE OPERATOR >= (
|
CREATE OPERATOR >= (
|
||||||
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_ge,
|
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_ge,
|
||||||
COMMUTATOR = <= , NEGATOR = < ,
|
COMMUTATOR = <= , NEGATOR = < ,
|
||||||
RESTRICT = scalargesel, JOIN = scalargejoinsel
|
RESTRICT = scalargtsel, JOIN = scalargtjoinsel
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE OPERATOR > (
|
CREATE OPERATOR > (
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
#include "commands/progress.h"
|
#include "commands/progress.h"
|
||||||
#include "commands/vacuum.h"
|
#include "commands/vacuum.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "miscadmin.h"
|
|
||||||
#include "utils/guc.h"
|
#include "utils/guc.h"
|
||||||
#include "utils/selfuncs.h"
|
#include "utils/selfuncs.h"
|
||||||
|
|
||||||
@@ -29,7 +28,7 @@ static relopt_kind hnsw_relopt_kind;
|
|||||||
* this grows bigger, we should use a shmem_request_hook and
|
* this grows bigger, we should use a shmem_request_hook and
|
||||||
* RequestAddinShmemSpace() to pre-reserve space for this.
|
* RequestAddinShmemSpace() to pre-reserve space for this.
|
||||||
*/
|
*/
|
||||||
void
|
static void
|
||||||
HnswInitLockTranche(void)
|
HnswInitLockTranche(void)
|
||||||
{
|
{
|
||||||
int *tranche_ids;
|
int *tranche_ids;
|
||||||
@@ -54,8 +53,7 @@ HnswInitLockTranche(void)
|
|||||||
void
|
void
|
||||||
HnswInit(void)
|
HnswInit(void)
|
||||||
{
|
{
|
||||||
if (!process_shared_preload_libraries_in_progress)
|
HnswInitLockTranche();
|
||||||
HnswInitLockTranche();
|
|
||||||
|
|
||||||
hnsw_relopt_kind = add_reloption_kind();
|
hnsw_relopt_kind = add_reloption_kind();
|
||||||
add_int_reloption(hnsw_relopt_kind, "m", "Max number of connections",
|
add_int_reloption(hnsw_relopt_kind, "m", "Max number of connections",
|
||||||
|
|||||||
@@ -80,7 +80,7 @@
|
|||||||
|
|
||||||
#if PG_VERSION_NUM < 130000
|
#if PG_VERSION_NUM < 130000
|
||||||
#define list_delete_last(list) list_truncate(list, list_length(list) - 1)
|
#define list_delete_last(list) list_truncate(list, list_length(list) - 1)
|
||||||
#define list_sort(list, cmp) ((list) = list_qsort(list, cmp))
|
#define list_sort(list, cmp) list_qsort(list, cmp)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define HnswIsElementTuple(tup) ((tup)->type == HNSW_ELEMENT_TUPLE_TYPE)
|
#define HnswIsElementTuple(tup) ((tup)->type == HNSW_ELEMENT_TUPLE_TYPE)
|
||||||
@@ -185,7 +185,6 @@ typedef struct HnswGraph
|
|||||||
|
|
||||||
/* Entry state */
|
/* Entry state */
|
||||||
LWLock entryLock;
|
LWLock entryLock;
|
||||||
LWLock entryWaitLock;
|
|
||||||
HnswElementPtr entryPoint;
|
HnswElementPtr entryPoint;
|
||||||
|
|
||||||
/* Allocations state */
|
/* Allocations state */
|
||||||
@@ -390,7 +389,6 @@ void HnswLoadElement(HnswElement element, float *distance, Datum *q, Relation i
|
|||||||
void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element);
|
void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element);
|
||||||
void HnswUpdateConnection(char *base, HnswElement element, HnswCandidate * hc, int lm, int lc, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation);
|
void HnswUpdateConnection(char *base, HnswElement element, HnswCandidate * hc, int lm, int lc, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation);
|
||||||
void HnswLoadNeighbors(HnswElement element, Relation index, int m);
|
void HnswLoadNeighbors(HnswElement element, Relation index, int m);
|
||||||
void HnswInitLockTranche(void);
|
|
||||||
PGDLLEXPORT void HnswParallelBuildMain(dsm_segment *seg, shm_toc *toc);
|
PGDLLEXPORT void HnswParallelBuildMain(dsm_segment *seg, shm_toc *toc);
|
||||||
|
|
||||||
/* Index access methods */
|
/* Index access methods */
|
||||||
|
|||||||
@@ -431,15 +431,10 @@ InsertTupleInMemory(HnswBuildState * buildstate, HnswElement element)
|
|||||||
HnswGraph *graph = buildstate->graph;
|
HnswGraph *graph = buildstate->graph;
|
||||||
HnswElement entryPoint;
|
HnswElement entryPoint;
|
||||||
LWLock *entryLock = &graph->entryLock;
|
LWLock *entryLock = &graph->entryLock;
|
||||||
LWLock *entryWaitLock = &graph->entryWaitLock;
|
|
||||||
int efConstruction = buildstate->efConstruction;
|
int efConstruction = buildstate->efConstruction;
|
||||||
int m = buildstate->m;
|
int m = buildstate->m;
|
||||||
char *base = buildstate->hnswarea;
|
char *base = buildstate->hnswarea;
|
||||||
|
|
||||||
/* Wait if another process needs exclusive lock */
|
|
||||||
LWLockAcquire(entryWaitLock, LW_EXCLUSIVE);
|
|
||||||
LWLockRelease(entryWaitLock);
|
|
||||||
|
|
||||||
/* Get entry point */
|
/* Get entry point */
|
||||||
LWLockAcquire(entryLock, LW_SHARED);
|
LWLockAcquire(entryLock, LW_SHARED);
|
||||||
entryPoint = HnswPtrAccess(base, graph->entryPoint);
|
entryPoint = HnswPtrAccess(base, graph->entryPoint);
|
||||||
@@ -451,9 +446,7 @@ InsertTupleInMemory(HnswBuildState * buildstate, HnswElement element)
|
|||||||
LWLockRelease(entryLock);
|
LWLockRelease(entryLock);
|
||||||
|
|
||||||
/* Get exclusive lock */
|
/* Get exclusive lock */
|
||||||
LWLockAcquire(entryWaitLock, LW_EXCLUSIVE);
|
|
||||||
LWLockAcquire(entryLock, LW_EXCLUSIVE);
|
LWLockAcquire(entryLock, LW_EXCLUSIVE);
|
||||||
LWLockRelease(entryWaitLock);
|
|
||||||
|
|
||||||
/* Get latest entry point after lock is acquired */
|
/* Get latest entry point after lock is acquired */
|
||||||
entryPoint = HnswPtrAccess(base, graph->entryPoint);
|
entryPoint = HnswPtrAccess(base, graph->entryPoint);
|
||||||
@@ -608,9 +601,6 @@ BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values,
|
|||||||
static void
|
static void
|
||||||
InitGraph(HnswGraph * graph, char *base, long memoryTotal)
|
InitGraph(HnswGraph * graph, char *base, long memoryTotal)
|
||||||
{
|
{
|
||||||
/* Initialize the lock tranche if needed */
|
|
||||||
HnswInitLockTranche();
|
|
||||||
|
|
||||||
HnswPtrStore(base, graph->head, (HnswElement) NULL);
|
HnswPtrStore(base, graph->head, (HnswElement) NULL);
|
||||||
HnswPtrStore(base, graph->entryPoint, (HnswElement) NULL);
|
HnswPtrStore(base, graph->entryPoint, (HnswElement) NULL);
|
||||||
graph->memoryUsed = 0;
|
graph->memoryUsed = 0;
|
||||||
@@ -619,7 +609,6 @@ InitGraph(HnswGraph * graph, char *base, long memoryTotal)
|
|||||||
graph->indtuples = 0;
|
graph->indtuples = 0;
|
||||||
SpinLockInit(&graph->lock);
|
SpinLockInit(&graph->lock);
|
||||||
LWLockInitialize(&graph->entryLock, hnsw_lock_tranche_id);
|
LWLockInitialize(&graph->entryLock, hnsw_lock_tranche_id);
|
||||||
LWLockInitialize(&graph->entryWaitLock, hnsw_lock_tranche_id);
|
|
||||||
LWLockInitialize(&graph->allocatorLock, hnsw_lock_tranche_id);
|
LWLockInitialize(&graph->allocatorLock, hnsw_lock_tranche_id);
|
||||||
LWLockInitialize(&graph->flushLock, hnsw_lock_tranche_id);
|
LWLockInitialize(&graph->flushLock, hnsw_lock_tranche_id);
|
||||||
}
|
}
|
||||||
@@ -985,14 +974,6 @@ HnswBeginParallel(HnswBuildState * buildstate, bool isconcurrent, int request)
|
|||||||
/* Report less than allocated so never fails */
|
/* Report less than allocated so never fails */
|
||||||
InitGraph(&hnswshared->graphData, hnswarea, esthnswarea - 1024 * 1024);
|
InitGraph(&hnswshared->graphData, hnswarea, esthnswarea - 1024 * 1024);
|
||||||
|
|
||||||
/*
|
|
||||||
* Avoid base address for relptr for Postgres < 14.5
|
|
||||||
* https://github.com/postgres/postgres/commit/7201cd18627afc64850537806da7f22150d1a83b
|
|
||||||
*/
|
|
||||||
#if PG_VERSION_NUM < 140005
|
|
||||||
hnswshared->graphData.memoryUsed += MAXALIGN(1);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
shm_toc_insert(pcxt->toc, PARALLEL_KEY_HNSW_SHARED, hnswshared);
|
shm_toc_insert(pcxt->toc, PARALLEL_KEY_HNSW_SHARED, hnswshared);
|
||||||
shm_toc_insert(pcxt->toc, PARALLEL_KEY_HNSW_AREA, hnswarea);
|
shm_toc_insert(pcxt->toc, PARALLEL_KEY_HNSW_AREA, hnswarea);
|
||||||
|
|
||||||
|
|||||||
@@ -860,15 +860,12 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
|
|||||||
static int
|
static int
|
||||||
#if PG_VERSION_NUM >= 130000
|
#if PG_VERSION_NUM >= 130000
|
||||||
CompareCandidateDistances(const ListCell *a, const ListCell *b)
|
CompareCandidateDistances(const ListCell *a, const ListCell *b)
|
||||||
{
|
|
||||||
HnswCandidate *hca = lfirst(a);
|
|
||||||
HnswCandidate *hcb = lfirst(b);
|
|
||||||
#else
|
#else
|
||||||
CompareCandidateDistances(const void *a, const void *b)
|
CompareCandidateDistances(const void *a, const void *b)
|
||||||
{
|
|
||||||
HnswCandidate *hca = lfirst(*(ListCell **) a);
|
|
||||||
HnswCandidate *hcb = lfirst(*(ListCell **) b);
|
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
HnswCandidate *hca = lfirst((ListCell *) a);
|
||||||
|
HnswCandidate *hcb = lfirst((ListCell *) b);
|
||||||
|
|
||||||
if (hca->distance < hcb->distance)
|
if (hca->distance < hcb->distance)
|
||||||
return 1;
|
return 1;
|
||||||
@@ -891,15 +888,12 @@ CompareCandidateDistances(const void *a, const void *b)
|
|||||||
static int
|
static int
|
||||||
#if PG_VERSION_NUM >= 130000
|
#if PG_VERSION_NUM >= 130000
|
||||||
CompareCandidateDistancesOffset(const ListCell *a, const ListCell *b)
|
CompareCandidateDistancesOffset(const ListCell *a, const ListCell *b)
|
||||||
{
|
|
||||||
HnswCandidate *hca = lfirst(a);
|
|
||||||
HnswCandidate *hcb = lfirst(b);
|
|
||||||
#else
|
#else
|
||||||
CompareCandidateDistancesOffset(const void *a, const void *b)
|
CompareCandidateDistancesOffset(const void *a, const void *b)
|
||||||
{
|
|
||||||
HnswCandidate *hca = lfirst(*(ListCell **) a);
|
|
||||||
HnswCandidate *hcb = lfirst(*(ListCell **) b);
|
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
HnswCandidate *hca = lfirst((ListCell *) a);
|
||||||
|
HnswCandidate *hcb = lfirst((ListCell *) b);
|
||||||
|
|
||||||
if (hca->distance < hcb->distance)
|
if (hca->distance < hcb->distance)
|
||||||
return 1;
|
return 1;
|
||||||
@@ -958,9 +952,7 @@ SelectNeighbors(char *base, List *c, int lm, int lc, FmgrInfo *procinfo, Oid col
|
|||||||
{
|
{
|
||||||
List *r = NIL;
|
List *r = NIL;
|
||||||
List *w = list_copy(c);
|
List *w = list_copy(c);
|
||||||
HnswCandidate **wd;
|
pairingheap *wd;
|
||||||
int wdlen = 0;
|
|
||||||
int wdoff = 0;
|
|
||||||
HnswNeighborArray *neighbors = HnswGetNeighbors(base, e2, lc);
|
HnswNeighborArray *neighbors = HnswGetNeighbors(base, e2, lc);
|
||||||
bool mustCalculate = !neighbors->closerSet;
|
bool mustCalculate = !neighbors->closerSet;
|
||||||
List *added = NIL;
|
List *added = NIL;
|
||||||
@@ -969,7 +961,7 @@ SelectNeighbors(char *base, List *c, int lm, int lc, FmgrInfo *procinfo, Oid col
|
|||||||
if (list_length(w) <= lm)
|
if (list_length(w) <= lm)
|
||||||
return w;
|
return w;
|
||||||
|
|
||||||
wd = palloc(sizeof(HnswCandidate *) * list_length(w));
|
wd = pairingheap_allocate(CompareNearestCandidates, NULL);
|
||||||
|
|
||||||
/* Ensure order of candidates is deterministic for closer caching */
|
/* Ensure order of candidates is deterministic for closer caching */
|
||||||
if (sortCandidates)
|
if (sortCandidates)
|
||||||
@@ -1035,21 +1027,21 @@ SelectNeighbors(char *base, List *c, int lm, int lc, FmgrInfo *procinfo, Oid col
|
|||||||
if (e->closer)
|
if (e->closer)
|
||||||
r = lappend(r, e);
|
r = lappend(r, e);
|
||||||
else
|
else
|
||||||
wd[wdlen++] = e;
|
pairingheap_add(wd, &(CreatePairingHeapNode(e)->ph_node));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Cached value can only be used in future if sorted deterministically */
|
/* Cached value can only be used in future if sorted deterministically */
|
||||||
neighbors->closerSet = sortCandidates;
|
neighbors->closerSet = sortCandidates;
|
||||||
|
|
||||||
/* Keep pruned connections */
|
/* Keep pruned connections */
|
||||||
while (wdoff < wdlen && list_length(r) < lm)
|
while (!pairingheap_is_empty(wd) && list_length(r) < lm)
|
||||||
r = lappend(r, wd[wdoff++]);
|
r = lappend(r, ((HnswPairingHeapNode *) pairingheap_remove_first(wd))->inner);
|
||||||
|
|
||||||
/* Return pruned for update connections */
|
/* Return pruned for update connections */
|
||||||
if (pruned != NULL)
|
if (pruned != NULL)
|
||||||
{
|
{
|
||||||
if (wdoff < wdlen)
|
if (!pairingheap_is_empty(wd))
|
||||||
*pruned = wd[wdoff];
|
*pruned = ((HnswPairingHeapNode *) pairingheap_first(wd))->inner;
|
||||||
else
|
else
|
||||||
*pruned = linitial(w);
|
*pruned = linitial(w);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.0'
|
||||||
module_pathname = '$libdir/vector'
|
module_pathname = '$libdir/vector'
|
||||||
relocatable = true
|
relocatable = true
|
||||||
|
|||||||
Reference in New Issue
Block a user