mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-22 03:57:34 +08:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
007defb6dd | ||
|
|
627d3fcc27 | ||
|
|
8983103d58 | ||
|
|
6aba9f4422 | ||
|
|
364e4149a6 | ||
|
|
08412c5704 | ||
|
|
88f9c495ba | ||
|
|
fc8c736858 | ||
|
|
a233d68365 | ||
|
|
831ba3f3de | ||
|
|
0c5e1e89d3 | ||
|
|
eda94a46d8 | ||
|
|
8ca5e37c83 | ||
|
|
18f8562ce9 |
82
.github/workflows/build.yml
vendored
82
.github/workflows/build.yml
vendored
@@ -23,7 +23,7 @@ jobs:
|
||||
- postgres: 13
|
||||
os: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions/checkout@v5
|
||||
- uses: ankane/setup-postgres@v1
|
||||
with:
|
||||
postgres-version: ${{ matrix.postgres }}
|
||||
@@ -37,10 +37,10 @@ jobs:
|
||||
- run: make installcheck
|
||||
- if: ${{ failure() }}
|
||||
run: cat regression.diffs
|
||||
- run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install libipc-run-perl
|
||||
- run: make prove_installcheck
|
||||
# - run: |
|
||||
# sudo apt-get update
|
||||
# sudo apt-get install libipc-run-perl
|
||||
# - run: make prove_installcheck
|
||||
mac:
|
||||
runs-on: ${{ matrix.os }}
|
||||
if: ${{ !startsWith(github.ref_name, 'windows') }}
|
||||
@@ -53,7 +53,7 @@ jobs:
|
||||
- postgres: 14
|
||||
os: macos-15-intel
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions/checkout@v5
|
||||
- uses: ankane/setup-postgres@v1
|
||||
with:
|
||||
postgres-version: ${{ matrix.postgres }}
|
||||
@@ -65,21 +65,21 @@ jobs:
|
||||
- if: ${{ failure() }}
|
||||
run: cat regression.diffs
|
||||
# Homebrew Postgres does not enable TAP tests, so need to download
|
||||
- run: |
|
||||
brew install cpanm
|
||||
cpanm --notest IPC::Run
|
||||
wget -q https://github.com/postgres/postgres/archive/refs/tags/$TAG.tar.gz
|
||||
tar xf $TAG.tar.gz
|
||||
mv postgres-$TAG postgres
|
||||
env:
|
||||
TAG: ${{ matrix.postgres == 18 && 'REL_18_2' || 'REL_14_21' }}
|
||||
- run: make prove_installcheck PROVE_FLAGS="-I ./postgres/src/test/perl -I ./test/perl"
|
||||
env:
|
||||
PERL5LIB: /Users/runner/perl5/lib/perl5
|
||||
- run: make clean && $(brew --prefix llvm@$LLVM_VERSION)/bin/scan-build --status-bugs make
|
||||
env:
|
||||
LLVM_VERSION: ${{ matrix.os == 'macos-26' && 20 || 18 }}
|
||||
PG_CFLAGS: -DUSE_ASSERT_CHECKING
|
||||
# - run: |
|
||||
# brew install cpanm
|
||||
# cpanm --notest IPC::Run
|
||||
# wget -q https://github.com/postgres/postgres/archive/refs/tags/$TAG.tar.gz
|
||||
# tar xf $TAG.tar.gz
|
||||
# mv postgres-$TAG postgres
|
||||
# env:
|
||||
# TAG: ${{ matrix.postgres == 18 && 'REL_18_0' || 'REL_14_19' }}
|
||||
# - run: make prove_installcheck PROVE_FLAGS="-I ./postgres/src/test/perl -I ./test/perl"
|
||||
# env:
|
||||
# PERL5LIB: /Users/runner/perl5/lib/perl5
|
||||
# - run: make clean && $(brew --prefix llvm@$LLVM_VERSION)/bin/scan-build --status-bugs make
|
||||
# env:
|
||||
# LLVM_VERSION: ${{ matrix.os == 'macos-26' && 20 || 18 }}
|
||||
# PG_CFLAGS: -DUSE_ASSERT_CHECKING
|
||||
windows:
|
||||
runs-on: ${{ matrix.os }}
|
||||
if: ${{ !startsWith(github.ref_name, 'mac') }}
|
||||
@@ -92,12 +92,12 @@ jobs:
|
||||
- postgres: 14
|
||||
os: windows-2022
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions/checkout@v5
|
||||
- uses: ankane/setup-postgres@v1
|
||||
with:
|
||||
postgres-version: ${{ matrix.postgres }}
|
||||
- run: |
|
||||
call "C:\Program Files\Microsoft Visual Studio\${{ matrix.os == 'windows-2025' && 18 || 2022 }}\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && ^
|
||||
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 ${{ matrix.postgres != 17 && 'PG_REGRESS=$(PGROOT)\bin\pg_regress' || '' }} && ^
|
||||
@@ -106,38 +106,4 @@ jobs:
|
||||
shell: cmd
|
||||
- if: ${{ failure() }}
|
||||
run: cat regression.diffs
|
||||
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
|
||||
- if: ${{ failure() }}
|
||||
run: cat pgvector/regression.diffs
|
||||
valgrind:
|
||||
if: ${{ !startsWith(github.ref_name, 'mac') && !startsWith(github.ref_name, 'windows') }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: ankane/setup-postgres-valgrind@v1
|
||||
with:
|
||||
postgres-version: 18
|
||||
check-ub: yes
|
||||
- run: make OPTFLAGS=""
|
||||
- run: sudo --preserve-env=PG_CONFIG make install
|
||||
- run: make installcheck
|
||||
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
## 0.8.3 (2026-06-17)
|
||||
## 0.8.2 (unreleased)
|
||||
|
||||
- Fixed possible index corruption with HNSW vacuuming
|
||||
- Fixed performance regression with Hamming distance and Jaccard distance with Postgres 18
|
||||
|
||||
## 0.8.2 (2026-02-25)
|
||||
|
||||
- Fixed buffer overflow with parallel HNSW index build - [more info](https://github.com/pgvector/pgvector/issues/959)
|
||||
- Improved `install` target on Windows
|
||||
- Fixed `Index Searches` in `EXPLAIN` output for Postgres 18
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ ARG DEBIAN_CODENAME=bookworm
|
||||
FROM postgres:$PG_MAJOR-$DEBIAN_CODENAME
|
||||
ARG PG_MAJOR
|
||||
|
||||
ADD https://github.com/pgvector/pgvector.git#v0.8.3 /tmp/pgvector
|
||||
ADD https://github.com/pgvector/pgvector.git#v0.8.1 /tmp/pgvector
|
||||
|
||||
RUN apt-get update && \
|
||||
apt-mark hold locales && \
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
|
||||
Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
|
||||
|
||||
Portions Copyright (c) 1994, The Regents of the University of California
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "vector",
|
||||
"abstract": "Open-source vector similarity search for Postgres",
|
||||
"description": "Supports L2 distance, inner product, and cosine distance",
|
||||
"version": "0.8.3",
|
||||
"version": "0.8.1",
|
||||
"maintainer": [
|
||||
"Andrew Kane <andrew@ankane.org>"
|
||||
],
|
||||
@@ -20,7 +20,7 @@
|
||||
"vector": {
|
||||
"file": "sql/vector.sql",
|
||||
"docfile": "README.md",
|
||||
"version": "0.8.3",
|
||||
"version": "0.8.1",
|
||||
"abstract": "Open-source vector similarity search for Postgres"
|
||||
}
|
||||
},
|
||||
|
||||
7
Makefile
7
Makefile
@@ -1,5 +1,5 @@
|
||||
EXTENSION = vector
|
||||
EXTVERSION = 0.8.3
|
||||
EXTVERSION = 0.8.1
|
||||
|
||||
MODULE_big = vector
|
||||
DATA = $(wildcard sql/*--*--*.sql)
|
||||
@@ -27,11 +27,6 @@ ifneq ($(filter ppc64%, $(shell uname -m)), )
|
||||
OPTFLAGS =
|
||||
endif
|
||||
|
||||
# RISC-V64 doesn't support -march=native
|
||||
ifeq ($(shell uname -m), riscv64)
|
||||
OPTFLAGS =
|
||||
endif
|
||||
|
||||
# For auto-vectorization:
|
||||
# - GCC (needs -ftree-vectorize OR -O3) - https://gcc.gnu.org/projects/tree-ssa/vectorization.html
|
||||
# - Clang (could use pragma instead) - https://llvm.org/docs/Vectorizers.html
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
EXTENSION = vector
|
||||
EXTVERSION = 0.8.3
|
||||
EXTVERSION = 0.8.1
|
||||
|
||||
DATA_built = sql\$(EXTENSION)--$(EXTVERSION).sql
|
||||
OBJS = src\bitutils.obj src\bitvec.obj src\halfutils.obj src\halfvec.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\sparsevec.obj src\vector.obj
|
||||
|
||||
88
README.md
88
README.md
@@ -11,8 +11,6 @@ Store your vectors with the rest of your data. Supports:
|
||||
|
||||
Plus [ACID](https://en.wikipedia.org/wiki/ACID) compliance, point-in-time recovery, JOINs, and all of the other [great features](https://www.postgresql.org/about/) of Postgres
|
||||
|
||||
Have a lot of vectors? Use [quantization](#scaling) to scale
|
||||
|
||||
[](https://github.com/pgvector/pgvector/actions)
|
||||
|
||||
## Installation
|
||||
@@ -23,7 +21,7 @@ Compile and install the extension (supports Postgres 13+)
|
||||
|
||||
```sh
|
||||
cd /tmp
|
||||
git clone --branch v0.8.3 https://github.com/pgvector/pgvector.git
|
||||
git clone --branch v0.8.1 https://github.com/pgvector/pgvector.git
|
||||
cd pgvector
|
||||
make
|
||||
make install # may need sudo
|
||||
@@ -40,7 +38,7 @@ Ensure [C++ support in Visual Studio](https://learn.microsoft.com/en-us/cpp/buil
|
||||
```cmd
|
||||
set "PGROOT=C:\Program Files\PostgreSQL\18"
|
||||
cd %TEMP%
|
||||
git clone --branch v0.8.3 https://github.com/pgvector/pgvector.git
|
||||
git clone --branch v0.8.1 https://github.com/pgvector/pgvector.git
|
||||
cd pgvector
|
||||
nmake /F Makefile.win
|
||||
nmake /F Makefile.win install
|
||||
@@ -316,8 +314,6 @@ For a large number of workers, you may need to increase `max_parallel_workers` (
|
||||
|
||||
The [index options](#index-options) also have a significant impact on build time (use the defaults unless seeing low recall)
|
||||
|
||||
Use [binary quantization](#binary-quantization) for faster build times at scale
|
||||
|
||||
### Indexing Progress
|
||||
|
||||
Check [indexing progress](https://www.postgresql.org/docs/current/progress-reporting.html#CREATE-INDEX-PROGRESS-REPORTING)
|
||||
@@ -447,7 +443,13 @@ Exact indexes work well for conditions that match a low percentage of rows. Othe
|
||||
CREATE INDEX ON items USING hnsw (embedding vector_l2_ops);
|
||||
```
|
||||
|
||||
With approximate indexes, filtering is applied *after* the index is scanned. If a condition matches 10% of rows, with HNSW and the default `hnsw.ef_search` of 40, only 4 rows will match on average. For more rows, enable [iterative index scans](#iterative-index-scans), which will automatically scan more of the index when needed.
|
||||
With approximate indexes, filtering is applied *after* the index is scanned. If a condition matches 10% of rows, with HNSW and the default `hnsw.ef_search` of 40, only 4 rows will match on average. For more rows, increase `hnsw.ef_search`.
|
||||
|
||||
```sql
|
||||
SET hnsw.ef_search = 200;
|
||||
```
|
||||
|
||||
Starting with 0.8.0, you can enable [iterative index scans](#iterative-index-scans), which will automatically scan more of the index when needed.
|
||||
|
||||
```sql
|
||||
SET hnsw.iterative_scan = strict_order;
|
||||
@@ -671,10 +673,6 @@ SHOW shared_buffers;
|
||||
|
||||
Be sure to restart Postgres for changes to take effect.
|
||||
|
||||
### Storing
|
||||
|
||||
Use the `halfvec` type instead of `vector` for a smaller working set.
|
||||
|
||||
### Loading
|
||||
|
||||
Use `COPY` for bulk loading data ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/loading/example.py)).
|
||||
@@ -689,8 +687,6 @@ Add any indexes *after* loading the initial data for best performance.
|
||||
|
||||
See index build time for [HNSW](#index-build-time) and [IVFFlat](#index-build-time-1).
|
||||
|
||||
Use [binary quantization](#binary-quantization) for smaller indexes and faster build times at scale.
|
||||
|
||||
In production environments, create indexes concurrently to avoid blocking writes.
|
||||
|
||||
```sql
|
||||
@@ -721,8 +717,6 @@ SELECT * FROM items ORDER BY embedding <#> '[3,1,2]' LIMIT 5;
|
||||
|
||||
#### Approximate Search
|
||||
|
||||
Use [binary quantization](#binary-quantization) with re-ranking to keep indexes in-memory at scale.
|
||||
|
||||
To speed up queries with an IVFFlat index, increase the number of inverted lists (at the expense of recall).
|
||||
|
||||
```sql
|
||||
@@ -738,20 +732,21 @@ REINDEX INDEX CONCURRENTLY index_name;
|
||||
VACUUM table_name;
|
||||
```
|
||||
|
||||
## Scaling
|
||||
|
||||
For a smaller working set:
|
||||
|
||||
1. Use the `halfvec` type instead of `vector` for tables
|
||||
2. Use [binary quantization](#binary-quantization) for indexes (with re-ranking for search)
|
||||
|
||||
Scale vertically by increasing memory, CPU, and storage on a single instance. Use existing tools to [tune parameters](#tuning) and [monitor performance](#monitoring).
|
||||
|
||||
Scale horizontally with [replicas](https://www.postgresql.org/docs/current/hot-standby.html), or use [Citus](https://github.com/citusdata/citus), [PgDog](https://github.com/pgdogdev/pgdog), or another approach for sharding ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/citus/example.py)).
|
||||
|
||||
## Monitoring
|
||||
|
||||
Use existing tools like [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) or [PgHero](https://github.com/ankane/pghero) to monitor performance.
|
||||
Monitor performance with [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) (be sure to add it to `shared_preload_libraries`).
|
||||
|
||||
```sql
|
||||
CREATE EXTENSION pg_stat_statements;
|
||||
```
|
||||
|
||||
Get the most time-consuming queries with:
|
||||
|
||||
```sql
|
||||
SELECT query, calls, ROUND((total_plan_time + total_exec_time) / calls) AS avg_time_ms,
|
||||
ROUND((total_plan_time + total_exec_time) / 60000) AS total_time_min
|
||||
FROM pg_stat_statements ORDER BY total_plan_time + total_exec_time DESC LIMIT 20;
|
||||
```
|
||||
|
||||
Monitor recall by comparing results from approximate search with exact search.
|
||||
|
||||
@@ -762,6 +757,14 @@ SELECT ...
|
||||
COMMIT;
|
||||
```
|
||||
|
||||
## Scaling
|
||||
|
||||
Scale pgvector the same way you scale Postgres.
|
||||
|
||||
Scale vertically by increasing memory, CPU, and storage on a single instance. Use existing tools to [tune parameters](#tuning) and [monitor performance](#monitoring).
|
||||
|
||||
Scale horizontally with [replicas](https://www.postgresql.org/docs/current/hot-standby.html), or use [Citus](https://github.com/citusdata/citus) or another approach for sharding ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/citus/example.py)).
|
||||
|
||||
## Languages
|
||||
|
||||
Use pgvector from any language with a Postgres client. You can even generate and store vectors in one language and query them in another.
|
||||
@@ -773,7 +776,6 @@ Algol | [pgvector-algol](https://github.com/pgvector/pgvector-algol)
|
||||
C | [pgvector-c](https://github.com/pgvector/pgvector-c)
|
||||
C++ | [pgvector-cpp](https://github.com/pgvector/pgvector-cpp)
|
||||
C#, F#, Visual Basic | [pgvector-dotnet](https://github.com/pgvector/pgvector-dotnet)
|
||||
COBOL | [pgvector-cobol](https://github.com/pgvector/pgvector-cobol)
|
||||
Crystal | [pgvector-crystal](https://github.com/pgvector/pgvector-crystal)
|
||||
D | [pgvector-d](https://github.com/pgvector/pgvector-d)
|
||||
Dart | [pgvector-dart](https://github.com/pgvector/pgvector-dart)
|
||||
@@ -875,8 +877,6 @@ No, but like other index types, you’ll likely see better performance if they d
|
||||
SELECT pg_size_pretty(pg_relation_size('index_name'));
|
||||
```
|
||||
|
||||
Use [half-precision indexing](#half-precision-indexing) or [binary quantization](#binary-quantization) for smaller indexes.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
#### Why isn’t a query using an index?
|
||||
@@ -1151,23 +1151,23 @@ This adds pgvector to the [Postgres image](https://hub.docker.com/_/postgres) (r
|
||||
|
||||
Supported tags are:
|
||||
|
||||
- `pg18-trixie`, `0.8.3-pg18-trixie`
|
||||
- `pg18-bookworm`, `0.8.3-pg18-bookworm`, `pg18`, `0.8.3-pg18`
|
||||
- `pg17-trixie`, `0.8.3-pg17-trixie`
|
||||
- `pg17-bookworm`, `0.8.3-pg17-bookworm`, `pg17`, `0.8.3-pg17`
|
||||
- `pg16-trixie`, `0.8.3-pg16-trixie`
|
||||
- `pg16-bookworm`, `0.8.3-pg16-bookworm`, `pg16`, `0.8.3-pg16`
|
||||
- `pg15-trixie`, `0.8.3-pg15-trixie`
|
||||
- `pg15-bookworm`, `0.8.3-pg15-bookworm`, `pg15`, `0.8.3-pg15`
|
||||
- `pg14-trixie`, `0.8.3-pg14-trixie`
|
||||
- `pg14-bookworm`, `0.8.3-pg14-bookworm`, `pg14`, `0.8.3-pg14`
|
||||
- `pg13-trixie`, `0.8.3-pg13-trixie`
|
||||
- `pg13-bookworm`, `0.8.3-pg13-bookworm`, `pg13`, `0.8.3-pg13`
|
||||
- `pg18-trixie`, `0.8.1-pg18-trixie`
|
||||
- `pg18-bookworm`, `0.8.1-pg18-bookworm`, `pg18`, `0.8.1-pg18`
|
||||
- `pg17-trixie`, `0.8.1-pg17-trixie`
|
||||
- `pg17-bookworm`, `0.8.1-pg17-bookworm`, `pg17`, `0.8.1-pg17`
|
||||
- `pg16-trixie`, `0.8.1-pg16-trixie`
|
||||
- `pg16-bookworm`, `0.8.1-pg16-bookworm`, `pg16`, `0.8.1-pg16`
|
||||
- `pg15-trixie`, `0.8.1-pg15-trixie`
|
||||
- `pg15-bookworm`, `0.8.1-pg15-bookworm`, `pg15`, `0.8.1-pg15`
|
||||
- `pg14-trixie`, `0.8.1-pg14-trixie`
|
||||
- `pg14-bookworm`, `0.8.1-pg14-bookworm`, `pg14`, `0.8.1-pg14`
|
||||
- `pg13-trixie`, `0.8.1-pg13-trixie`
|
||||
- `pg13-bookworm`, `0.8.1-pg13-bookworm`, `pg13`, `0.8.1-pg13`
|
||||
|
||||
You can also build the image manually:
|
||||
|
||||
```sh
|
||||
git clone --branch v0.8.3 https://github.com/pgvector/pgvector.git
|
||||
git clone --branch v0.8.1 https://github.com/pgvector/pgvector.git
|
||||
cd pgvector
|
||||
docker build --pull --build-arg PG_MAJOR=18 -t myuser/pgvector .
|
||||
```
|
||||
@@ -1329,7 +1329,7 @@ make clean && PG_CFLAGS="-DUSE_ASSERT_CHECKING" make && make install
|
||||
To enable benchmarking:
|
||||
|
||||
```sh
|
||||
make clean && PG_CFLAGS="-DHNSW_BENCH -DIVFFLAT_BENCH" make && make install
|
||||
make clean && PG_CFLAGS="-DIVFFLAT_BENCH" make && make install
|
||||
```
|
||||
|
||||
To show memory usage:
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||
\echo Use "ALTER EXTENSION vector UPDATE TO '0.8.2'" to load this file. \quit
|
||||
@@ -1,2 +0,0 @@
|
||||
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||
\echo Use "ALTER EXTENSION vector UPDATE TO '0.8.3'" to load this file. \quit
|
||||
@@ -31,12 +31,10 @@
|
||||
#define BIT_TARGET_CLONES
|
||||
#endif
|
||||
|
||||
/* Use built-ins when possible for Postgres < 19 for inlining */
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
#define popcount64(x) pg_popcount64(x)
|
||||
#elif defined(HAVE__BUILTIN_POPCOUNT) && (defined(HAVE_LONG_INT_64) || SIZEOF_LONG == 8)
|
||||
/* Use built-ins when possible for inlining */
|
||||
#if defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_INT_64)
|
||||
#define popcount64(x) __builtin_popcountl(x)
|
||||
#elif defined(HAVE__BUILTIN_POPCOUNT) && (defined(HAVE_LONG_LONG_INT_64) || SIZEOF_LONG_LONG == 8)
|
||||
#elif defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_LONG_INT_64)
|
||||
#define popcount64(x) __builtin_popcountll(x)
|
||||
#elif !defined(_MSC_VER)
|
||||
/* Fails to resolve with MSVC */
|
||||
@@ -171,7 +169,7 @@ BitJaccardDistanceAvx512Popcount(uint32 bytes, unsigned char *ax, unsigned char
|
||||
#endif
|
||||
|
||||
TARGET_XSAVE static bool
|
||||
SupportsAvx512Popcount(void)
|
||||
SupportsAvx512Popcount()
|
||||
{
|
||||
unsigned int exx[4] = {0, 0, 0, 0};
|
||||
|
||||
|
||||
@@ -23,10 +23,6 @@
|
||||
#include "varatt.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 170000
|
||||
#include "parser/scansup.h"
|
||||
#endif
|
||||
|
||||
#define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1)
|
||||
#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1))
|
||||
|
||||
@@ -137,9 +133,9 @@ InitHalfVector(int dim)
|
||||
return result;
|
||||
}
|
||||
|
||||
#if PG_VERSION_NUM >= 170000
|
||||
#define halfvec_isspace(ch) scanner_isspace(ch)
|
||||
#else
|
||||
/*
|
||||
* Check for whitespace, since array_isspace() is static
|
||||
*/
|
||||
static inline bool
|
||||
halfvec_isspace(char ch)
|
||||
{
|
||||
@@ -152,7 +148,6 @@ halfvec_isspace(char ch)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Check state array
|
||||
|
||||
64
src/hnsw.c
64
src/hnsw.c
@@ -12,8 +12,11 @@
|
||||
#include "fmgr.h"
|
||||
#include "hnsw.h"
|
||||
#include "miscadmin.h"
|
||||
#include "nodes/nodes.h"
|
||||
#include "nodes/pathnodes.h"
|
||||
#include "nodes/pg_list.h"
|
||||
#include "storage/lwlock.h"
|
||||
#include "storage/lockdefs.h"
|
||||
#include "storage/shmem.h"
|
||||
#include "utils/float.h"
|
||||
#include "utils/guc.h"
|
||||
#include "utils/relcache.h"
|
||||
@@ -267,64 +270,6 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(hnswhandler);
|
||||
Datum
|
||||
hnswhandler(PG_FUNCTION_ARGS)
|
||||
{
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
static const IndexAmRoutine amroutine = {
|
||||
.type = T_IndexAmRoutine,
|
||||
.amstrategies = 0,
|
||||
.amsupport = 3,
|
||||
.amoptsprocnum = 0,
|
||||
.amcanorder = false,
|
||||
.amcanorderbyop = true,
|
||||
.amcanhash = false,
|
||||
.amconsistentequality = false,
|
||||
.amconsistentordering = false,
|
||||
.amcanbackward = false,
|
||||
.amcanunique = false,
|
||||
.amcanmulticol = false,
|
||||
.amoptionalkey = true,
|
||||
.amsearcharray = false,
|
||||
.amsearchnulls = false,
|
||||
.amstorage = false,
|
||||
.amclusterable = false,
|
||||
.ampredlocks = false,
|
||||
.amcanparallel = false,
|
||||
.amcanbuildparallel = true,
|
||||
.amcaninclude = false,
|
||||
.amusemaintenanceworkmem = false,
|
||||
.amsummarizing = false,
|
||||
.amparallelvacuumoptions = VACUUM_OPTION_PARALLEL_BULKDEL,
|
||||
.amkeytype = InvalidOid,
|
||||
|
||||
.ambuild = hnswbuild,
|
||||
.ambuildempty = hnswbuildempty,
|
||||
.aminsert = hnswinsert,
|
||||
.aminsertcleanup = NULL,
|
||||
.ambulkdelete = hnswbulkdelete,
|
||||
.amvacuumcleanup = hnswvacuumcleanup,
|
||||
.amcanreturn = NULL,
|
||||
.amcostestimate = hnswcostestimate,
|
||||
.amgettreeheight = NULL,
|
||||
.amoptions = hnswoptions,
|
||||
.amproperty = NULL,
|
||||
.ambuildphasename = hnswbuildphasename,
|
||||
.amvalidate = hnswvalidate,
|
||||
.amadjustmembers = NULL,
|
||||
.ambeginscan = hnswbeginscan,
|
||||
.amrescan = hnswrescan,
|
||||
.amgettuple = hnswgettuple,
|
||||
.amgetbitmap = NULL,
|
||||
.amendscan = hnswendscan,
|
||||
.ammarkpos = NULL,
|
||||
.amrestrpos = NULL,
|
||||
.amestimateparallelscan = NULL,
|
||||
.aminitparallelscan = NULL,
|
||||
.amparallelrescan = NULL,
|
||||
.amtranslatestrategy = NULL,
|
||||
.amtranslatecmptype = NULL,
|
||||
};
|
||||
|
||||
PG_RETURN_POINTER(&amroutine);
|
||||
#else
|
||||
IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
|
||||
|
||||
amroutine->amstrategies = 0;
|
||||
@@ -398,5 +343,4 @@ hnswhandler(PG_FUNCTION_ARGS)
|
||||
#endif
|
||||
|
||||
PG_RETURN_POINTER(amroutine);
|
||||
#endif
|
||||
}
|
||||
|
||||
24
src/hnsw.h
24
src/hnsw.h
@@ -10,18 +10,10 @@
|
||||
#include "lib/pairingheap.h"
|
||||
#include "nodes/execnodes.h"
|
||||
#include "port.h" /* for random() */
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/condition_variable.h"
|
||||
#include "storage/lwlock.h"
|
||||
#include "storage/s_lock.h"
|
||||
#include "utils/relptr.h"
|
||||
#include "utils/sampling.h"
|
||||
#include "vector.h"
|
||||
|
||||
#ifdef HNSW_BENCH
|
||||
#include "portability/instr_time.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
typedef Pointer Item;
|
||||
#endif
|
||||
@@ -82,21 +74,6 @@ typedef Pointer Item;
|
||||
#define HnswPageGetOpaque(page) ((HnswPageOpaque) PageGetSpecialPointer(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
|
||||
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
|
||||
#define SeedRandom(seed) pg_prng_seed(&pg_global_prng_state, seed)
|
||||
@@ -431,7 +408,6 @@ typedef struct HnswVacuumState
|
||||
BufferAccessStrategy bas;
|
||||
HnswNeighborTuple ntup;
|
||||
HnswElementData highestPoint;
|
||||
HnswElementData fallbackPoint;
|
||||
|
||||
/* Memory */
|
||||
MemoryContext tmpCtx;
|
||||
|
||||
@@ -36,8 +36,6 @@
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "access/genam.h"
|
||||
#include "access/parallel.h"
|
||||
#include "access/relscan.h"
|
||||
@@ -49,17 +47,36 @@
|
||||
#include "catalog/index.h"
|
||||
#include "catalog/pg_type_d.h"
|
||||
#include "commands/progress.h"
|
||||
#include "common/relpath.h"
|
||||
#include "hnsw.h"
|
||||
#include "miscadmin.h"
|
||||
#include "nodes/execnodes.h"
|
||||
#include "optimizer/optimizer.h"
|
||||
#include "storage/block.h"
|
||||
#include "storage/buf.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/condition_variable.h"
|
||||
#include "storage/dsm.h"
|
||||
#include "storage/itemid.h"
|
||||
#include "storage/itemptr.h"
|
||||
#include "storage/lockdefs.h"
|
||||
#include "storage/lwlock.h"
|
||||
#include "storage/off.h"
|
||||
#include "storage/shm_toc.h"
|
||||
#include "storage/shmem.h"
|
||||
#include "storage/spin.h"
|
||||
#include "tcop/tcopprot.h"
|
||||
#include "utils/datum.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/rel.h"
|
||||
#include "utils/relcache.h"
|
||||
#include "utils/snapmgr.h"
|
||||
#include "utils/snapshot.h"
|
||||
|
||||
#if PG_VERSION_NUM < 190000
|
||||
#include "storage/item.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 160000
|
||||
#include "varatt.h"
|
||||
@@ -73,6 +90,11 @@
|
||||
|
||||
#if PG_VERSION_NUM >= 140000
|
||||
#include "utils/backend_status.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 170000
|
||||
#include "utils/wait_event_types.h"
|
||||
#elif PG_VERSION_NUM >= 140000
|
||||
#include "utils/wait_event.h"
|
||||
#endif
|
||||
|
||||
@@ -80,8 +102,6 @@
|
||||
#define PARALLEL_KEY_HNSW_AREA UINT64CONST(0xA000000000000002)
|
||||
#define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xA000000000000003)
|
||||
|
||||
#define HNSW_MAX_GRAPH_MEMORY (SIZE_MAX / 2)
|
||||
|
||||
/*
|
||||
* Create the metapage
|
||||
*/
|
||||
@@ -494,7 +514,6 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
|
||||
LWLock *flushLock = &graph->flushLock;
|
||||
char *base = buildstate->hnswarea;
|
||||
Datum value;
|
||||
Size memoryMargin;
|
||||
|
||||
/* Form index value */
|
||||
if (!HnswFormIndexValue(&value, values, isnull, buildstate->typeInfo, support))
|
||||
@@ -503,9 +522,6 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
|
||||
/* Get datum size */
|
||||
valueSize = VARSIZE_ANY(DatumGetPointer(value));
|
||||
|
||||
/* In a parallel build, add a margin so allocations never fail */
|
||||
memoryMargin = base == NULL ? 0 : 1024 * 1024;
|
||||
|
||||
/* Ensure graph not flushed when inserting */
|
||||
LWLockAcquire(flushLock, LW_SHARED);
|
||||
|
||||
@@ -527,7 +543,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
|
||||
* Check that we have enough memory available for the new element now that
|
||||
* we have the allocator lock, and flush pages if needed.
|
||||
*/
|
||||
if (graph->memoryUsed + memoryMargin >= graph->memoryTotal)
|
||||
if (graph->memoryUsed >= graph->memoryTotal)
|
||||
{
|
||||
LWLockRelease(&graph->allocatorLock);
|
||||
|
||||
@@ -620,7 +636,7 @@ InitGraph(HnswGraph * graph, char *base, Size memoryTotal)
|
||||
HnswPtrStore(base, graph->head, (HnswElement) NULL);
|
||||
HnswPtrStore(base, graph->entryPoint, (HnswElement) NULL);
|
||||
graph->memoryUsed = 0;
|
||||
graph->memoryTotal = Min(memoryTotal, HNSW_MAX_GRAPH_MEMORY);
|
||||
graph->memoryTotal = memoryTotal;
|
||||
graph->flushed = false;
|
||||
graph->indtuples = 0;
|
||||
SpinLockInit(&graph->lock);
|
||||
@@ -661,17 +677,9 @@ static void *
|
||||
HnswSharedMemoryAlloc(Size size, void *state)
|
||||
{
|
||||
HnswBuildState *buildstate = (HnswBuildState *) state;
|
||||
Size alignedSize = MAXALIGN(size);
|
||||
void *chunk;
|
||||
void *chunk = buildstate->hnswarea + buildstate->graph->memoryUsed;
|
||||
|
||||
if (alignedSize > 1024 * 1024)
|
||||
elog(ERROR, "hnsw allocation too large");
|
||||
|
||||
if (buildstate->graph->memoryUsed + alignedSize > buildstate->graph->memoryTotal)
|
||||
elog(ERROR, "hnsw allocator out of memory");
|
||||
|
||||
chunk = buildstate->hnswarea + buildstate->graph->memoryUsed;
|
||||
buildstate->graph->memoryUsed += alignedSize;
|
||||
buildstate->graph->memoryUsed += MAXALIGN(size);
|
||||
return chunk;
|
||||
}
|
||||
|
||||
@@ -803,11 +811,7 @@ HnswParallelScanAndInsert(Relation heapRel, Relation indexRel, HnswShared * hnsw
|
||||
buildstate.hnswarea = hnswarea;
|
||||
InitAllocator(&buildstate.allocator, &HnswSharedMemoryAlloc, &buildstate);
|
||||
scan = table_beginscan_parallel(heapRel,
|
||||
ParallelTableScanFromHnswShared(hnswshared)
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
,SO_NONE
|
||||
#endif
|
||||
);
|
||||
ParallelTableScanFromHnswShared(hnswshared));
|
||||
reltuples = table_index_build_scan(heapRel, indexRel, indexInfo,
|
||||
true, progress, BuildCallback,
|
||||
(void *) &buildstate, scan);
|
||||
@@ -961,8 +965,6 @@ HnswBeginParallel(HnswBuildState * buildstate, bool isconcurrent, int request)
|
||||
if (esthnswarea > estother)
|
||||
esthnswarea -= estother;
|
||||
|
||||
esthnswarea = Min(esthnswarea, HNSW_MAX_GRAPH_MEMORY);
|
||||
|
||||
shm_toc_estimate_chunk(&pcxt->estimator, esthnswarea);
|
||||
shm_toc_estimate_keys(&pcxt->estimator, 2);
|
||||
|
||||
@@ -1005,7 +1007,8 @@ HnswBeginParallel(HnswBuildState * buildstate, bool isconcurrent, int request)
|
||||
snapshot);
|
||||
|
||||
hnswarea = (char *) shm_toc_allocate(pcxt->toc, esthnswarea);
|
||||
InitGraph(&hnswshared->graphData, hnswarea, esthnswarea);
|
||||
/* Report less than allocated so never fails */
|
||||
InitGraph(&hnswshared->graphData, hnswarea, esthnswarea - 1024 * 1024);
|
||||
|
||||
/*
|
||||
* Avoid base address for relptr for Postgres < 14.5
|
||||
|
||||
@@ -2,14 +2,26 @@
|
||||
|
||||
#include "access/genam.h"
|
||||
#include "access/generic_xlog.h"
|
||||
#include "common/relpath.h"
|
||||
#include "hnsw.h"
|
||||
#include "nodes/execnodes.h"
|
||||
#include "storage/block.h"
|
||||
#include "storage/buf.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/itemid.h"
|
||||
#include "storage/itemptr.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "storage/lwlock.h"
|
||||
#include "storage/lockdefs.h"
|
||||
#include "storage/off.h"
|
||||
#include "utils/datum.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/rel.h"
|
||||
#include "utils/relcache.h"
|
||||
|
||||
#if PG_VERSION_NUM < 190000
|
||||
#include "storage/item.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 160000
|
||||
#include "varatt.h"
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
#include "postgres.h"
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "access/genam.h"
|
||||
#include "access/relscan.h"
|
||||
#include "access/sdir.h"
|
||||
#include "access/skey.h"
|
||||
#include "hnsw.h"
|
||||
#include "lib/pairingheap.h"
|
||||
#include "miscadmin.h"
|
||||
#include "nodes/pg_list.h"
|
||||
#include "pgstat.h"
|
||||
#include "storage/itemptr.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "storage/lockdefs.h"
|
||||
#include "utils/float.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/relcache.h"
|
||||
@@ -153,7 +155,7 @@ hnswbeginscan(Relation index, int nkeys, int norderbys)
|
||||
/* Calculate max memory */
|
||||
/* Add 256 extra bytes to fill last block when close */
|
||||
maxMemory = (double) work_mem * hnsw_scan_mem_multiplier * 1024.0 + 256;
|
||||
so->maxMemory = Min(maxMemory, (double) (SIZE_MAX / 2));
|
||||
so->maxMemory = Min(maxMemory, (double) SIZE_MAX);
|
||||
|
||||
scan->opaque = so;
|
||||
|
||||
|
||||
@@ -5,16 +5,24 @@
|
||||
#include "access/genam.h"
|
||||
#include "access/generic_xlog.h"
|
||||
#include "common/hashfn.h"
|
||||
#include "common/relpath.h"
|
||||
#include "fmgr.h"
|
||||
#include "hnsw.h"
|
||||
#include "lib/pairingheap.h"
|
||||
#include "nodes/pg_list.h"
|
||||
#include "port/atomics.h"
|
||||
#include "sparsevec.h"
|
||||
#include "storage/block.h"
|
||||
#include "storage/buf.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/itemptr.h"
|
||||
#include "storage/lwlock.h"
|
||||
#include "storage/off.h"
|
||||
#include "utils/datum.h"
|
||||
#include "utils/memdebug.h"
|
||||
#include "utils/rel.h"
|
||||
#include "utils/relcache.h"
|
||||
#include "vector.h"
|
||||
|
||||
#if PG_VERSION_NUM >= 160000
|
||||
@@ -546,9 +554,6 @@ HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, Hns
|
||||
|
||||
Assert(HnswIsElementTuple(etup));
|
||||
|
||||
if (unlikely(etup->deleted))
|
||||
elog(ERROR, "cannot load deleted element");
|
||||
|
||||
/* Calculate distance */
|
||||
if (distance != NULL)
|
||||
{
|
||||
|
||||
175
src/hnswvacuum.c
175
src/hnswvacuum.c
@@ -3,12 +3,24 @@
|
||||
#include "access/genam.h"
|
||||
#include "access/generic_xlog.h"
|
||||
#include "commands/vacuum.h"
|
||||
#include "common/relpath.h"
|
||||
#include "hnsw.h"
|
||||
#include "nodes/pg_list.h"
|
||||
#include "storage/block.h"
|
||||
#include "storage/buf.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/itemptr.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "storage/lockdefs.h"
|
||||
#include "storage/off.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/rel.h"
|
||||
#include "utils/relcache.h"
|
||||
|
||||
#if PG_VERSION_NUM < 190000
|
||||
#include "storage/item.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 160000
|
||||
#include "varatt.h"
|
||||
@@ -37,20 +49,17 @@ RemoveHeapTids(HnswVacuumState * vacuumstate)
|
||||
{
|
||||
BlockNumber blkno = HNSW_HEAD_BLKNO;
|
||||
HnswElement highestPoint = &vacuumstate->highestPoint;
|
||||
HnswElement fallbackPoint = &vacuumstate->fallbackPoint;
|
||||
Relation index = vacuumstate->index;
|
||||
BufferAccessStrategy bas = vacuumstate->bas;
|
||||
HnswElement entryPoint = HnswGetEntryPoint(vacuumstate->index);
|
||||
IndexBulkDeleteResult *stats = vacuumstate->stats;
|
||||
|
||||
/* Store separately since HnswElement level is uint8 */
|
||||
/* Store separately since highestPoint.level is uint8 */
|
||||
int highestLevel = -1;
|
||||
int fallbackLevel = -1;
|
||||
|
||||
/* Initialize highest point and fallback point */
|
||||
/* Initialize highest point */
|
||||
highestPoint->blkno = InvalidBlockNumber;
|
||||
highestPoint->offno = InvalidOffsetNumber;
|
||||
fallbackPoint->blkno = InvalidBlockNumber;
|
||||
fallbackPoint->offno = InvalidOffsetNumber;
|
||||
|
||||
while (BlockNumberIsValid(blkno))
|
||||
{
|
||||
@@ -122,31 +131,14 @@ RemoveHeapTids(HnswVacuumState * vacuumstate)
|
||||
tidhash_insert(vacuumstate->deleted, ip, &found);
|
||||
Assert(!found);
|
||||
}
|
||||
else if (etup->level > highestLevel)
|
||||
else if (etup->level > highestLevel && !(entryPoint != NULL && blkno == entryPoint->blkno && offno == entryPoint->offno))
|
||||
{
|
||||
if (BlockNumberIsValid(highestPoint->blkno))
|
||||
{
|
||||
/* Current highest point becomes fallback */
|
||||
fallbackPoint->blkno = highestPoint->blkno;
|
||||
fallbackPoint->offno = highestPoint->offno;
|
||||
fallbackPoint->level = highestPoint->level;
|
||||
fallbackLevel = highestLevel;
|
||||
}
|
||||
|
||||
/* Keep track of highest point */
|
||||
/* Keep track of highest non-entry point */
|
||||
highestPoint->blkno = blkno;
|
||||
highestPoint->offno = offno;
|
||||
highestPoint->level = etup->level;
|
||||
highestLevel = etup->level;
|
||||
}
|
||||
else if (etup->level > fallbackLevel)
|
||||
{
|
||||
/* Keep track of second highest point */
|
||||
fallbackPoint->blkno = blkno;
|
||||
fallbackPoint->offno = offno;
|
||||
fallbackPoint->level = etup->level;
|
||||
fallbackLevel = etup->level;
|
||||
}
|
||||
}
|
||||
|
||||
blkno = HnswPageGetOpaque(page)->nextblkno;
|
||||
@@ -158,10 +150,6 @@ RemoveHeapTids(HnswVacuumState * vacuumstate)
|
||||
|
||||
UnlockReleaseBuffer(buf);
|
||||
}
|
||||
|
||||
#ifdef HNSW_MEMORY
|
||||
elog(INFO, "memory: %zu KB", MemoryContextMemAllocated(CurrentMemoryContext, true) / 1024);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -293,27 +281,12 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate)
|
||||
/* Get a shared lock */
|
||||
LockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
||||
|
||||
/* Get latest entry point */
|
||||
entryPoint = HnswGetEntryPoint(index);
|
||||
/* Load element */
|
||||
HnswLoadElement(highestPoint, NULL, NULL, index, support, true, NULL);
|
||||
|
||||
/* Use fallback point if highest point is entry point */
|
||||
if (entryPoint != NULL && entryPoint->blkno == highestPoint->blkno && entryPoint->offno == highestPoint->offno)
|
||||
{
|
||||
highestPoint = &vacuumstate->fallbackPoint;
|
||||
|
||||
if (!BlockNumberIsValid(highestPoint->blkno))
|
||||
highestPoint = NULL;
|
||||
}
|
||||
|
||||
if (highestPoint != NULL)
|
||||
{
|
||||
/* Load element */
|
||||
HnswLoadElement(highestPoint, NULL, NULL, index, support, true, NULL);
|
||||
|
||||
/* Repair if needed */
|
||||
if (NeedsUpdated(vacuumstate, highestPoint))
|
||||
RepairGraphElement(vacuumstate, highestPoint, entryPoint);
|
||||
}
|
||||
/* Repair if needed */
|
||||
if (NeedsUpdated(vacuumstate, highestPoint))
|
||||
RepairGraphElement(vacuumstate, highestPoint, HnswGetEntryPoint(index));
|
||||
|
||||
/* Release lock */
|
||||
UnlockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
||||
@@ -480,99 +453,6 @@ RepairGraph(HnswVacuumState * vacuumstate)
|
||||
/* Reset memory context */
|
||||
MemoryContextSwitchTo(oldCtx);
|
||||
MemoryContextReset(vacuumstate->tmpCtx);
|
||||
|
||||
#ifdef HNSW_VACUUM_PROGRESS
|
||||
if (!BlockNumberIsValid(blkno) || (blkno - HNSW_HEAD_BLKNO) % 1000 == 0)
|
||||
{
|
||||
BlockNumber totalBlocks = RelationGetNumberOfBlocks(index);
|
||||
BlockNumber currentBlocks = BlockNumberIsValid(blkno) ? blkno : totalBlocks;
|
||||
|
||||
elog(INFO, "hnsw vacuum progress: %.1f%%", 100.0 * currentBlocks / totalBlocks);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Confirm graph was repaired
|
||||
*/
|
||||
static void
|
||||
ConfirmRepaired(HnswVacuumState * vacuumstate)
|
||||
{
|
||||
BlockNumber blkno = HNSW_HEAD_BLKNO;
|
||||
Relation index = vacuumstate->index;
|
||||
BufferAccessStrategy bas = vacuumstate->bas;
|
||||
|
||||
while (BlockNumberIsValid(blkno))
|
||||
{
|
||||
Buffer buf;
|
||||
Page page;
|
||||
OffsetNumber offno;
|
||||
OffsetNumber maxoffno;
|
||||
|
||||
vacuum_delay_point();
|
||||
|
||||
buf = ReadBufferExtended(index, MAIN_FORKNUM, blkno, RBM_NORMAL, bas);
|
||||
LockBuffer(buf, BUFFER_LOCK_SHARE);
|
||||
page = BufferGetPage(buf);
|
||||
maxoffno = PageGetMaxOffsetNumber(page);
|
||||
|
||||
/* Iterate over nodes */
|
||||
for (offno = FirstOffsetNumber; offno <= maxoffno; offno = OffsetNumberNext(offno))
|
||||
{
|
||||
HnswElementTuple etup = (HnswElementTuple) PageGetItem(page, PageGetItemId(page, offno));
|
||||
HnswNeighborTuple ntup;
|
||||
Buffer nbuf;
|
||||
Page npage;
|
||||
BlockNumber neighborPage;
|
||||
OffsetNumber neighborOffno;
|
||||
|
||||
/* Skip neighbor tuples */
|
||||
if (!HnswIsElementTuple(etup))
|
||||
continue;
|
||||
|
||||
/* Skip if being deleted */
|
||||
if (!ItemPointerIsValid(&etup->heaptids[0]))
|
||||
continue;
|
||||
|
||||
/* Get neighbor page */
|
||||
neighborPage = ItemPointerGetBlockNumber(&etup->neighbortid);
|
||||
neighborOffno = ItemPointerGetOffsetNumber(&etup->neighbortid);
|
||||
|
||||
if (neighborPage == blkno)
|
||||
{
|
||||
nbuf = buf;
|
||||
npage = page;
|
||||
}
|
||||
else
|
||||
{
|
||||
nbuf = ReadBufferExtended(index, MAIN_FORKNUM, neighborPage, RBM_NORMAL, bas);
|
||||
LockBuffer(nbuf, BUFFER_LOCK_SHARE);
|
||||
npage = BufferGetPage(nbuf);
|
||||
}
|
||||
|
||||
ntup = (HnswNeighborTuple) PageGetItem(npage, PageGetItemId(npage, neighborOffno));
|
||||
|
||||
/* Check neighbors */
|
||||
for (int i = 0; i < ntup->count; i++)
|
||||
{
|
||||
ItemPointer indextid = &ntup->indextids[i];
|
||||
|
||||
if (!ItemPointerIsValid(indextid))
|
||||
continue;
|
||||
|
||||
/* Check if in deleted list */
|
||||
if (DeletedContains(vacuumstate->deleted, indextid))
|
||||
elog(ERROR, "hnsw graph not repaired");
|
||||
}
|
||||
|
||||
if (nbuf != buf)
|
||||
UnlockReleaseBuffer(nbuf);
|
||||
}
|
||||
|
||||
blkno = HnswPageGetOpaque(page)->nextblkno;
|
||||
|
||||
UnlockReleaseBuffer(buf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -766,16 +646,13 @@ hnswbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
|
||||
InitVacuumState(&vacuumstate, info, stats, callback, callback_state);
|
||||
|
||||
/* Pass 1: Remove heap TIDs */
|
||||
HnswBench("RemoveHeapTids", RemoveHeapTids(&vacuumstate));
|
||||
RemoveHeapTids(&vacuumstate);
|
||||
|
||||
/* Pass 2: Repair graph */
|
||||
HnswBench("RepairGraph", RepairGraph(&vacuumstate));
|
||||
RepairGraph(&vacuumstate);
|
||||
|
||||
/* Pass 3: Confirm repaired */
|
||||
HnswBench("ConfirmRepaired", ConfirmRepaired(&vacuumstate));
|
||||
|
||||
/* Pass 4: Mark as deleted */
|
||||
HnswBench("MarkDeleted", MarkDeleted(&vacuumstate));
|
||||
/* Pass 3: Mark as deleted */
|
||||
MarkDeleted(&vacuumstate);
|
||||
|
||||
FreeVacuumState(&vacuumstate);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <float.h>
|
||||
|
||||
#include "access/attnum.h"
|
||||
#include "access/genam.h"
|
||||
#include "access/generic_xlog.h"
|
||||
#include "access/itup.h"
|
||||
@@ -16,20 +17,38 @@
|
||||
#include "catalog/pg_operator_d.h"
|
||||
#include "catalog/pg_type_d.h"
|
||||
#include "commands/progress.h"
|
||||
#include "common/relpath.h"
|
||||
#include "executor/tuptable.h"
|
||||
#include "fmgr.h"
|
||||
#include "ivfflat.h"
|
||||
#include "miscadmin.h"
|
||||
#include "nodes/execnodes.h"
|
||||
#include "optimizer/optimizer.h"
|
||||
#include "storage/block.h"
|
||||
#include "storage/buf.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/condition_variable.h"
|
||||
#include "storage/dsm.h"
|
||||
#include "storage/itemptr.h"
|
||||
#include "storage/lockdefs.h"
|
||||
#include "storage/off.h"
|
||||
#include "storage/shm_toc.h"
|
||||
#include "storage/shmem.h"
|
||||
#include "storage/spin.h"
|
||||
#include "tcop/tcopprot.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/rel.h"
|
||||
#include "utils/relcache.h"
|
||||
#include "utils/sampling.h"
|
||||
#include "utils/snapmgr.h"
|
||||
#include "utils/snapshot.h"
|
||||
#include "utils/tuplesort.h"
|
||||
|
||||
#if PG_VERSION_NUM < 190000
|
||||
#include "storage/item.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 160000
|
||||
#include "varatt.h"
|
||||
#endif
|
||||
@@ -42,6 +61,11 @@
|
||||
|
||||
#if PG_VERSION_NUM >= 140000
|
||||
#include "utils/backend_status.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 170000
|
||||
#include "utils/wait_event_types.h"
|
||||
#elif PG_VERSION_NUM >= 140000
|
||||
#include "utils/wait_event.h"
|
||||
#endif
|
||||
|
||||
@@ -63,13 +87,15 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
||||
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
||||
|
||||
/*
|
||||
* Check with KMEANS_NORM_PROC that the value can be normalized since
|
||||
* spherical distance function expects unit vectors
|
||||
* Normalize with KMEANS_NORM_PROC since spherical distance function
|
||||
* expects unit vectors
|
||||
*/
|
||||
if (buildstate->kmeansnormprocinfo != NULL)
|
||||
{
|
||||
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
||||
return;
|
||||
|
||||
value = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
||||
}
|
||||
|
||||
if (samples->length < targsamples)
|
||||
@@ -80,7 +106,7 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
||||
else
|
||||
{
|
||||
if (buildstate->rowstoskip < 0)
|
||||
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, buildstate->samplerows, targsamples);
|
||||
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, samples->length, targsamples);
|
||||
|
||||
if (buildstate->rowstoskip <= 0)
|
||||
{
|
||||
@@ -96,9 +122,6 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
||||
|
||||
buildstate->rowstoskip -= 1;
|
||||
}
|
||||
|
||||
/* Increment after reservoir_get_next_S */
|
||||
buildstate->samplerows += 1;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -135,7 +158,6 @@ SampleRows(IvfflatBuildState * buildstate)
|
||||
int targsamples = buildstate->samples->maxlen;
|
||||
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
||||
|
||||
buildstate->samplerows = 0;
|
||||
buildstate->rowstoskip = -1;
|
||||
|
||||
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt());
|
||||
@@ -145,24 +167,8 @@ SampleRows(IvfflatBuildState * buildstate)
|
||||
{
|
||||
BlockNumber targblock = BlockSampler_Next(&buildstate->bs);
|
||||
|
||||
/* Set anyvisible to false like table_index_build_scan */
|
||||
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
||||
false, false, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
||||
}
|
||||
|
||||
/* Normalize if needed */
|
||||
if (buildstate->kmeansnormprocinfo != NULL)
|
||||
{
|
||||
VectorArray samples = buildstate->samples;
|
||||
|
||||
for (int i = 0; i < samples->length; i++)
|
||||
{
|
||||
Datum value = PointerGetDatum(VectorArrayGet(samples, i));
|
||||
Datum normValue = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
||||
|
||||
VectorArraySet(samples, i, DatumGetPointer(normValue));
|
||||
pfree(DatumGetPointer(normValue));
|
||||
}
|
||||
false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,13 +399,9 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In
|
||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 3, "vector", TupleDescAttr(buildstate->tupdesc, 0)->atttypid, -1, 0);
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
TupleDescFinalize(buildstate->sortdesc);
|
||||
#endif
|
||||
|
||||
buildstate->slot = MakeSingleTupleTableSlot(buildstate->sortdesc, &TTSOpsVirtual);
|
||||
|
||||
/* TODO Ensure within maintenance_work_mem */
|
||||
buildstate->centers = VectorArrayInit(buildstate->lists, buildstate->dimensions, buildstate->typeInfo->itemSize(buildstate->dimensions));
|
||||
buildstate->listInfo = palloc(sizeof(ListInfo) * buildstate->lists);
|
||||
|
||||
@@ -458,7 +460,7 @@ ComputeCenters(IvfflatBuildState * buildstate)
|
||||
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions, buildstate->centers->itemsize);
|
||||
if (buildstate->heap != NULL)
|
||||
{
|
||||
IvfflatBench("sample rows", SampleRows(buildstate));
|
||||
SampleRows(buildstate);
|
||||
|
||||
if (buildstate->samples->length < buildstate->lists)
|
||||
{
|
||||
@@ -673,11 +675,7 @@ IvfflatParallelScanAndSort(IvfflatSpool * ivfspool, IvfflatShared * ivfshared, S
|
||||
ivfspool->sortstate = InitBuildSortState(buildstate.sortdesc, sortmem, coordinate);
|
||||
buildstate.sortstate = ivfspool->sortstate;
|
||||
scan = table_beginscan_parallel(ivfspool->heap,
|
||||
ParallelTableScanFromIvfflatShared(ivfshared)
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
,SO_NONE
|
||||
#endif
|
||||
);
|
||||
ParallelTableScanFromIvfflatShared(ivfshared));
|
||||
reltuples = table_index_build_scan(ivfspool->heap, ivfspool->index, indexInfo,
|
||||
true, progress, BuildCallback,
|
||||
(void *) &buildstate, scan);
|
||||
|
||||
@@ -9,7 +9,10 @@
|
||||
#include "commands/vacuum.h"
|
||||
#include "fmgr.h"
|
||||
#include "ivfflat.h"
|
||||
#include "nodes/nodes.h"
|
||||
#include "nodes/pathnodes.h"
|
||||
#include "nodes/pg_list.h"
|
||||
#include "storage/lockdefs.h"
|
||||
#include "utils/float.h"
|
||||
#include "utils/guc.h"
|
||||
#include "utils/relcache.h"
|
||||
@@ -184,64 +187,6 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(ivfflathandler);
|
||||
Datum
|
||||
ivfflathandler(PG_FUNCTION_ARGS)
|
||||
{
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
static const IndexAmRoutine amroutine = {
|
||||
.type = T_IndexAmRoutine,
|
||||
.amstrategies = 0,
|
||||
.amsupport = 5,
|
||||
.amoptsprocnum = 0,
|
||||
.amcanorder = false,
|
||||
.amcanorderbyop = true,
|
||||
.amcanhash = false,
|
||||
.amconsistentequality = false,
|
||||
.amconsistentordering = false,
|
||||
.amcanbackward = false,
|
||||
.amcanunique = false,
|
||||
.amcanmulticol = false,
|
||||
.amoptionalkey = true,
|
||||
.amsearcharray = false,
|
||||
.amsearchnulls = false,
|
||||
.amstorage = false,
|
||||
.amclusterable = false,
|
||||
.ampredlocks = false,
|
||||
.amcanparallel = false,
|
||||
.amcanbuildparallel = true,
|
||||
.amcaninclude = false,
|
||||
.amusemaintenanceworkmem = false,
|
||||
.amsummarizing = false,
|
||||
.amparallelvacuumoptions = VACUUM_OPTION_PARALLEL_BULKDEL,
|
||||
.amkeytype = InvalidOid,
|
||||
|
||||
.ambuild = ivfflatbuild,
|
||||
.ambuildempty = ivfflatbuildempty,
|
||||
.aminsert = ivfflatinsert,
|
||||
.aminsertcleanup = NULL,
|
||||
.ambulkdelete = ivfflatbulkdelete,
|
||||
.amvacuumcleanup = ivfflatvacuumcleanup,
|
||||
.amcanreturn = NULL,
|
||||
.amcostestimate = ivfflatcostestimate,
|
||||
.amgettreeheight = NULL,
|
||||
.amoptions = ivfflatoptions,
|
||||
.amproperty = NULL,
|
||||
.ambuildphasename = ivfflatbuildphasename,
|
||||
.amvalidate = ivfflatvalidate,
|
||||
.amadjustmembers = NULL,
|
||||
.ambeginscan = ivfflatbeginscan,
|
||||
.amrescan = ivfflatrescan,
|
||||
.amgettuple = ivfflatgettuple,
|
||||
.amgetbitmap = NULL,
|
||||
.amendscan = ivfflatendscan,
|
||||
.ammarkpos = NULL,
|
||||
.amrestrpos = NULL,
|
||||
.amestimateparallelscan = NULL,
|
||||
.aminitparallelscan = NULL,
|
||||
.amparallelrescan = NULL,
|
||||
.amtranslatestrategy = NULL,
|
||||
.amtranslatecmptype = NULL,
|
||||
};
|
||||
|
||||
PG_RETURN_POINTER(&amroutine);
|
||||
#else
|
||||
IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
|
||||
|
||||
amroutine->amstrategies = 0;
|
||||
@@ -315,5 +260,4 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
||||
#endif
|
||||
|
||||
PG_RETURN_POINTER(amroutine);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include "lib/pairingheap.h"
|
||||
#include "nodes/execnodes.h"
|
||||
#include "port.h" /* for random() */
|
||||
#include "storage/condition_variable.h"
|
||||
#include "utils/sampling.h"
|
||||
#include "utils/tuplesort.h"
|
||||
#include "vector.h"
|
||||
@@ -214,8 +213,7 @@ typedef struct IvfflatBuildState
|
||||
/* Sampling */
|
||||
BlockSamplerData bs;
|
||||
ReservoirStateData rstate;
|
||||
double samplerows;
|
||||
double rowstoskip;
|
||||
int rowstoskip;
|
||||
|
||||
/* Sorting */
|
||||
Tuplesortstate *sortstate;
|
||||
|
||||
@@ -5,13 +5,26 @@
|
||||
#include "access/genam.h"
|
||||
#include "access/generic_xlog.h"
|
||||
#include "access/itup.h"
|
||||
#include "common/relpath.h"
|
||||
#include "fmgr.h"
|
||||
#include "ivfflat.h"
|
||||
#include "nodes/execnodes.h"
|
||||
#include "storage/block.h"
|
||||
#include "storage/buf.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/itemid.h"
|
||||
#include "storage/itemptr.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "storage/lockdefs.h"
|
||||
#include "storage/off.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/rel.h"
|
||||
#include "utils/relcache.h"
|
||||
|
||||
#if PG_VERSION_NUM < 190000
|
||||
#include "storage/item.h"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Find the list that minimizes the distance function
|
||||
|
||||
@@ -2,20 +2,32 @@
|
||||
|
||||
#include <float.h>
|
||||
|
||||
#include "access/attnum.h"
|
||||
#include "access/genam.h"
|
||||
#include "access/itup.h"
|
||||
#include "access/relscan.h"
|
||||
#include "access/sdir.h"
|
||||
#include "access/skey.h"
|
||||
#include "access/tupdesc.h"
|
||||
#include "catalog/pg_operator_d.h"
|
||||
#include "catalog/pg_type_d.h"
|
||||
#include "common/relpath.h"
|
||||
#include "executor/tuptable.h"
|
||||
#include "fmgr.h"
|
||||
#include "lib/pairingheap.h"
|
||||
#include "ivfflat.h"
|
||||
#include "miscadmin.h"
|
||||
#include "pgstat.h"
|
||||
#include "storage/block.h"
|
||||
#include "storage/buf.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/itemid.h"
|
||||
#include "storage/itemptr.h"
|
||||
#include "storage/off.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/rel.h"
|
||||
#include "utils/relcache.h"
|
||||
#include "utils/snapmgr.h"
|
||||
#include "utils/tuplesort.h"
|
||||
|
||||
@@ -298,9 +310,6 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
||||
so->tupdesc = CreateTemplateTupleDesc(2);
|
||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
||||
#if PG_VERSION_NUM >= 190000
|
||||
TupleDescFinalize(so->tupdesc);
|
||||
#endif
|
||||
|
||||
/* Prep sort */
|
||||
so->sortstate = InitScanSortState(so->tupdesc);
|
||||
|
||||
@@ -2,11 +2,15 @@
|
||||
|
||||
#include "access/genam.h"
|
||||
#include "access/generic_xlog.h"
|
||||
#include "common/relpath.h"
|
||||
#include "fmgr.h"
|
||||
#include "halfutils.h"
|
||||
#include "halfvec.h"
|
||||
#include "ivfflat.h"
|
||||
#include "storage/block.h"
|
||||
#include "storage/buf.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/bufpage.h"
|
||||
#include "utils/relcache.h"
|
||||
#include "utils/varbit.h"
|
||||
#include "vector.h"
|
||||
|
||||
@@ -4,8 +4,14 @@
|
||||
#include "access/generic_xlog.h"
|
||||
#include "access/itup.h"
|
||||
#include "commands/vacuum.h"
|
||||
#include "common/relpath.h"
|
||||
#include "ivfflat.h"
|
||||
#include "storage/block.h"
|
||||
#include "storage/buf.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/bufpage.h"
|
||||
#include "storage/itemptr.h"
|
||||
#include "storage/off.h"
|
||||
#include "utils/relcache.h"
|
||||
|
||||
#if PG_VERSION_NUM >= 180000
|
||||
|
||||
@@ -22,10 +22,6 @@
|
||||
#include "varatt.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 170000
|
||||
#include "parser/scansup.h"
|
||||
#endif
|
||||
|
||||
typedef struct SparseInputElement
|
||||
{
|
||||
int32 index;
|
||||
@@ -159,9 +155,9 @@ InitSparseVector(int dim, int nnz)
|
||||
return result;
|
||||
}
|
||||
|
||||
#if PG_VERSION_NUM >= 170000
|
||||
#define sparsevec_isspace(ch) scanner_isspace(ch)
|
||||
#else
|
||||
/*
|
||||
* Check for whitespace, since array_isspace() is static
|
||||
*/
|
||||
static inline bool
|
||||
sparsevec_isspace(char ch)
|
||||
{
|
||||
@@ -174,7 +170,6 @@ sparsevec_isspace(char ch)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Compare indices
|
||||
@@ -182,10 +177,10 @@ sparsevec_isspace(char ch)
|
||||
static int
|
||||
CompareIndices(const void *a, const void *b)
|
||||
{
|
||||
if (((const SparseInputElement *) a)->index < ((const SparseInputElement *) b)->index)
|
||||
if (((SparseInputElement *) a)->index < ((SparseInputElement *) b)->index)
|
||||
return -1;
|
||||
|
||||
if (((const SparseInputElement *) a)->index > ((const SparseInputElement *) b)->index)
|
||||
if (((SparseInputElement *) a)->index > ((SparseInputElement *) b)->index)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
|
||||
13
src/vector.c
13
src/vector.c
@@ -26,10 +26,6 @@
|
||||
#include "varatt.h"
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 170000
|
||||
#include "parser/scansup.h"
|
||||
#endif
|
||||
|
||||
#define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1)
|
||||
#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1))
|
||||
|
||||
@@ -40,7 +36,7 @@
|
||||
#endif
|
||||
|
||||
#if PG_VERSION_NUM >= 180000
|
||||
PG_MODULE_MAGIC_EXT(.name = "vector", .version = "0.8.3");
|
||||
PG_MODULE_MAGIC_EXT(.name = "vector",.version = "0.8.1");
|
||||
#else
|
||||
PG_MODULE_MAGIC;
|
||||
#endif
|
||||
@@ -133,9 +129,9 @@ InitVector(int dim)
|
||||
return result;
|
||||
}
|
||||
|
||||
#if PG_VERSION_NUM >= 170000
|
||||
#define vector_isspace(ch) scanner_isspace(ch)
|
||||
#else
|
||||
/*
|
||||
* Check for whitespace, since array_isspace() is static
|
||||
*/
|
||||
static inline bool
|
||||
vector_isspace(char ch)
|
||||
{
|
||||
@@ -148,7 +144,6 @@ vector_isspace(char ch)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Check state array
|
||||
|
||||
@@ -16,19 +16,29 @@ $node->safe_psql("postgres",
|
||||
"INSERT INTO tst SELECT i, ARRAY[random(), random(), random()] FROM generate_series(1, 100000) i;"
|
||||
);
|
||||
|
||||
# Add index
|
||||
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v vector_l2_ops);");
|
||||
# Check each index type
|
||||
my @operators = ("<->", "<#>", "<=>");
|
||||
my @opclasses = ("vector_l2_ops", "vector_ip_ops", "vector_cosine_ops");
|
||||
|
||||
# Test 100% recall
|
||||
for (1 .. 20)
|
||||
for my $i (0 .. $#operators)
|
||||
{
|
||||
my $id = int(rand() * 100000);
|
||||
my $query = $node->safe_psql("postgres", "SELECT v FROM tst WHERE i = $id;");
|
||||
my $res = $node->safe_psql("postgres", qq(
|
||||
SET enable_seqscan = off;
|
||||
SELECT v FROM tst ORDER BY v <-> '$query' LIMIT 1;
|
||||
));
|
||||
is($res, $query);
|
||||
my $operator = $operators[$i];
|
||||
my $opclass = $opclasses[$i];
|
||||
|
||||
# Add index
|
||||
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v $opclass);");
|
||||
|
||||
# Test 100% recall
|
||||
for (1 .. 20)
|
||||
{
|
||||
my $id = int(rand() * 100000);
|
||||
my $query = $node->safe_psql("postgres", "SELECT v FROM tst WHERE i = $id;");
|
||||
my $res = $node->safe_psql("postgres", qq(
|
||||
SET enable_seqscan = off;
|
||||
SELECT v FROM tst ORDER BY v <-> '$query' LIMIT 1;
|
||||
));
|
||||
is($res, $query);
|
||||
}
|
||||
}
|
||||
|
||||
done_testing();
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
use PostgreSQL::Test::Cluster;
|
||||
use PostgreSQL::Test::Utils;
|
||||
use Test::More;
|
||||
|
||||
# Initialize node
|
||||
my $node = PostgreSQL::Test::Cluster->new('node');
|
||||
$node->init;
|
||||
$node->start;
|
||||
|
||||
# Create table
|
||||
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||
$node->safe_psql("postgres", "CREATE TABLE tst (v vector(3));");
|
||||
$node->safe_psql("postgres",
|
||||
"INSERT INTO tst SELECT ARRAY[random(), random(), random()] FROM generate_series(1, 1000) i;"
|
||||
);
|
||||
|
||||
my ($ret, $stdout, $stderr) = $node->psql("postgres", qq(
|
||||
SET client_min_messages = DEBUG;
|
||||
SET maintenance_work_mem = '3073kB';
|
||||
ALTER TABLE tst SET (parallel_workers = 1);
|
||||
CREATE INDEX ON tst USING hnsw (v vector_l2_ops);
|
||||
));
|
||||
is($ret, 0, $stderr);
|
||||
like($stderr, qr/using \d+ parallel workers/);
|
||||
like($stderr, qr/hnsw graph no longer fits into maintenance_work_mem after 0 tuples/);
|
||||
|
||||
done_testing();
|
||||
@@ -1,4 +1,4 @@
|
||||
comment = 'vector data type and ivfflat and hnsw access methods'
|
||||
default_version = '0.8.3'
|
||||
default_version = '0.8.1'
|
||||
module_pathname = '$libdir/vector'
|
||||
relocatable = true
|
||||
|
||||
Reference in New Issue
Block a user