mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-23 04:20:56 +08:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9542b5548f |
8
.dockerignore
Normal file
8
.dockerignore
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/.git/
|
||||||
|
/dist/
|
||||||
|
/results/
|
||||||
|
/tmp_check/
|
||||||
|
/sql/vector--?.?.?.sql
|
||||||
|
regression.*
|
||||||
|
*.o
|
||||||
|
*.so
|
||||||
52
.github/workflows/build.yml
vendored
52
.github/workflows/build.yml
vendored
@@ -8,31 +8,27 @@ jobs:
|
|||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- postgres: 20
|
|
||||||
os: ubuntu-26.04
|
|
||||||
- postgres: 19
|
|
||||||
os: ubuntu-26.04
|
|
||||||
- postgres: 18
|
- postgres: 18
|
||||||
os: ubuntu-26.04-arm
|
os: ubuntu-24.04
|
||||||
- postgres: 17
|
- postgres: 17
|
||||||
os: ubuntu-24.04
|
os: ubuntu-24.04
|
||||||
- postgres: 16
|
- postgres: 16
|
||||||
os: ubuntu-24.04-arm
|
os: ubuntu-22.04
|
||||||
- postgres: 15
|
- postgres: 15
|
||||||
os: ubuntu-22.04
|
os: ubuntu-22.04
|
||||||
- postgres: 14
|
- postgres: 14
|
||||||
os: ubuntu-22.04-arm
|
os: ubuntu-20.04
|
||||||
- postgres: 13
|
- postgres: 13
|
||||||
os: ubuntu-22.04
|
os: ubuntu-20.04
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v4
|
||||||
- uses: ankane/setup-postgres@v1
|
- uses: ankane/setup-postgres@v1
|
||||||
with:
|
with:
|
||||||
postgres-version: ${{ matrix.postgres }}
|
postgres-version: ${{ matrix.postgres }}
|
||||||
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 ${{ matrix.postgres >= 18 && '-Wno-missing-field-initializers' || '' }}
|
PG_CFLAGS: -DUSE_ASSERT_CHECKING -Wall -Wextra -Werror -Wno-unused-parameter -Wno-sign-compare
|
||||||
- run: |
|
- run: |
|
||||||
export PG_CONFIG=`which pg_config`
|
export PG_CONFIG=`which pg_config`
|
||||||
sudo --preserve-env=PG_CONFIG make install
|
sudo --preserve-env=PG_CONFIG make install
|
||||||
@@ -50,18 +46,18 @@ jobs:
|
|||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- postgres: 18
|
- postgres: 17
|
||||||
os: macos-26
|
os: macos-15
|
||||||
- postgres: 14
|
- postgres: 14
|
||||||
os: macos-15-intel
|
os: macos-13
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v4
|
||||||
- uses: ankane/setup-postgres@v1
|
- uses: ankane/setup-postgres@v1
|
||||||
with:
|
with:
|
||||||
postgres-version: ${{ matrix.postgres }}
|
postgres-version: ${{ matrix.postgres }}
|
||||||
- run: make
|
- run: make
|
||||||
env:
|
env:
|
||||||
PG_CFLAGS: -DUSE_ASSERT_CHECKING -Wall -Wextra -Werror -Wno-unused-parameter -Wno-unknown-warning-option ${{ matrix.postgres >= 18 && '-Wno-missing-field-initializers' || '' }}
|
PG_CFLAGS: -DUSE_ASSERT_CHECKING -Wall -Wextra -Werror -Wno-unused-parameter
|
||||||
- run: make install
|
- run: make install
|
||||||
- run: make installcheck
|
- run: make installcheck
|
||||||
- if: ${{ failure() }}
|
- if: ${{ failure() }}
|
||||||
@@ -74,35 +70,27 @@ jobs:
|
|||||||
tar xf $TAG.tar.gz
|
tar xf $TAG.tar.gz
|
||||||
mv postgres-$TAG postgres
|
mv postgres-$TAG postgres
|
||||||
env:
|
env:
|
||||||
TAG: ${{ matrix.postgres == 18 && 'REL_18_2' || 'REL_14_21' }}
|
TAG: ${{ matrix.postgres == 17 && 'REL_17_2' || 'REL_14_15' }}
|
||||||
- run: make prove_installcheck PROVE_FLAGS="-I ./postgres/src/test/perl -I ./test/perl"
|
- run: make prove_installcheck PROVE_FLAGS="-I ./postgres/src/test/perl -I ./test/perl"
|
||||||
env:
|
env:
|
||||||
PERL5LIB: /Users/runner/perl5/lib/perl5
|
PERL5LIB: /Users/runner/perl5/lib/perl5
|
||||||
- run: make clean && $(brew --prefix llvm@$LLVM_VERSION)/bin/scan-build --status-bugs make
|
- run: make clean && $(brew --prefix llvm@$LLVM_VERSION)/bin/scan-build --status-bugs make
|
||||||
env:
|
env:
|
||||||
LLVM_VERSION: ${{ matrix.os == 'macos-26' && 20 || 18 }}
|
LLVM_VERSION: ${{ matrix.os == 'macos-15' && 18 || 15 }}
|
||||||
PG_CFLAGS: -DUSE_ASSERT_CHECKING
|
PG_CFLAGS: -DUSE_ASSERT_CHECKING
|
||||||
windows:
|
windows:
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: windows-latest
|
||||||
if: ${{ !startsWith(github.ref_name, 'mac') }}
|
if: ${{ !startsWith(github.ref_name, 'mac') }}
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- postgres: 17
|
|
||||||
os: windows-2025
|
|
||||||
- postgres: 14
|
|
||||||
os: windows-2022
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v4
|
||||||
- uses: ankane/setup-postgres@v1
|
- uses: ankane/setup-postgres@v1
|
||||||
with:
|
with:
|
||||||
postgres-version: ${{ matrix.postgres }}
|
postgres-version: 14
|
||||||
- run: |
|
- 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 && ^
|
||||||
nmake /NOLOGO /F Makefile.win install && ^
|
nmake /NOLOGO /F Makefile.win install && ^
|
||||||
nmake /NOLOGO /F Makefile.win installcheck ${{ matrix.postgres != 17 && 'PG_REGRESS=$(PGROOT)\bin\pg_regress' || '' }} && ^
|
nmake /NOLOGO /F Makefile.win installcheck && ^
|
||||||
nmake /NOLOGO /F Makefile.win clean && ^
|
nmake /NOLOGO /F Makefile.win clean && ^
|
||||||
nmake /NOLOGO /F Makefile.win uninstall
|
nmake /NOLOGO /F Makefile.win uninstall
|
||||||
shell: cmd
|
shell: cmd
|
||||||
@@ -135,10 +123,10 @@ jobs:
|
|||||||
if: ${{ !startsWith(github.ref_name, 'mac') && !startsWith(github.ref_name, 'windows') }}
|
if: ${{ !startsWith(github.ref_name, 'mac') && !startsWith(github.ref_name, 'windows') }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v4
|
||||||
- uses: ankane/setup-postgres-valgrind@v1
|
- uses: ankane/setup-postgres-valgrind@v1
|
||||||
with:
|
with:
|
||||||
postgres-version: 18
|
postgres-version: 17
|
||||||
check-ub: yes
|
check-ub: yes
|
||||||
- run: make OPTFLAGS=""
|
- run: make OPTFLAGS=""
|
||||||
- run: sudo --preserve-env=PG_CONFIG make install
|
- run: sudo --preserve-env=PG_CONFIG make install
|
||||||
|
|||||||
26
CHANGELOG.md
26
CHANGELOG.md
@@ -1,29 +1,3 @@
|
|||||||
## 0.8.5 (2026-07-08)
|
|
||||||
|
|
||||||
- Reduced memory usage for small tables for IVFFlat index builds
|
|
||||||
|
|
||||||
## 0.8.4 (2026-06-30)
|
|
||||||
|
|
||||||
- Fixed `hnsw graph not repaired` error with HNSW vacuuming
|
|
||||||
- Fixed possible error with inserts during HNSW vacuuming
|
|
||||||
- Fixed memory exceeding `maintenance_work_mem` with IVFFlat index builds
|
|
||||||
|
|
||||||
## 0.8.3 (2026-06-17)
|
|
||||||
|
|
||||||
- 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
|
|
||||||
|
|
||||||
## 0.8.1 (2025-09-04)
|
|
||||||
|
|
||||||
- Added support for Postgres 18 rc1
|
|
||||||
- Improved performance of `binary_quantize` function
|
|
||||||
|
|
||||||
## 0.8.0 (2024-10-30)
|
## 0.8.0 (2024-10-30)
|
||||||
|
|
||||||
- Added support for iterative index scans
|
- Added support for iterative index scans
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
# syntax=docker/dockerfile:1
|
|
||||||
|
|
||||||
ARG PG_MAJOR=17
|
ARG PG_MAJOR=17
|
||||||
ARG DEBIAN_CODENAME=bookworm
|
FROM postgres:$PG_MAJOR
|
||||||
FROM postgres:$PG_MAJOR-$DEBIAN_CODENAME
|
|
||||||
ARG PG_MAJOR
|
ARG PG_MAJOR
|
||||||
|
|
||||||
ADD https://github.com/pgvector/pgvector.git#v0.8.5 /tmp/pgvector
|
COPY . /tmp/pgvector
|
||||||
|
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
apt-mark hold locales && \
|
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
|
Portions Copyright (c) 1994, The Regents of the University of California
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,12 @@
|
|||||||
"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.8.5",
|
"version": "0.8.0",
|
||||||
"maintainer": [
|
"maintainer": [
|
||||||
"Andrew Kane <andrew@ankane.org>"
|
"Andrew Kane <andrew@ankane.org>"
|
||||||
],
|
],
|
||||||
"license": {
|
"license": {
|
||||||
"PostgreSQL": "https://www.postgresql.org/about/licence"
|
"PostgreSQL": "http://www.postgresql.org/about/licence"
|
||||||
},
|
},
|
||||||
"prereqs": {
|
"prereqs": {
|
||||||
"runtime": {
|
"runtime": {
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
"vector": {
|
"vector": {
|
||||||
"file": "sql/vector.sql",
|
"file": "sql/vector.sql",
|
||||||
"docfile": "README.md",
|
"docfile": "README.md",
|
||||||
"version": "0.8.5",
|
"version": "0.8.0",
|
||||||
"abstract": "Open-source vector similarity search for Postgres"
|
"abstract": "Open-source vector similarity search for Postgres"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -38,7 +38,7 @@
|
|||||||
"generated_by": "Andrew Kane",
|
"generated_by": "Andrew Kane",
|
||||||
"meta-spec": {
|
"meta-spec": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"url": "https://pgxn.org/meta/spec.txt"
|
"url": "http://pgxn.org/meta/spec.txt"
|
||||||
},
|
},
|
||||||
"tags": [
|
"tags": [
|
||||||
"vectors",
|
"vectors",
|
||||||
|
|||||||
14
Makefile
14
Makefile
@@ -1,5 +1,5 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.8.5
|
EXTVERSION = 0.8.0
|
||||||
|
|
||||||
MODULE_big = vector
|
MODULE_big = vector
|
||||||
DATA = $(wildcard sql/*--*--*.sql)
|
DATA = $(wildcard sql/*--*--*.sql)
|
||||||
@@ -27,11 +27,6 @@ ifneq ($(filter ppc64%, $(shell uname -m)), )
|
|||||||
OPTFLAGS =
|
OPTFLAGS =
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# RISC-V64 doesn't support -march=native
|
|
||||||
ifeq ($(shell uname -m), riscv64)
|
|
||||||
OPTFLAGS =
|
|
||||||
endif
|
|
||||||
|
|
||||||
# For auto-vectorization:
|
# For auto-vectorization:
|
||||||
# - GCC (needs -ftree-vectorize OR -O3) - https://gcc.gnu.org/projects/tree-ssa/vectorization.html
|
# - 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
|
# - Clang (could use pragma instead) - https://llvm.org/docs/Vectorizers.html
|
||||||
@@ -81,9 +76,4 @@ docker:
|
|||||||
.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) --build-arg DEBIAN_CODENAME=bookworm -t pgvector/pgvector:pg$(PG_MAJOR) -t pgvector/pgvector:pg$(PG_MAJOR)-bookworm -t pgvector/pgvector:$(EXTVERSION)-pg$(PG_MAJOR) -t pgvector/pgvector:$(EXTVERSION)-pg$(PG_MAJOR)-bookworm .
|
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) .
|
||||||
|
|
||||||
.PHONY: docker-release-trixie
|
|
||||||
|
|
||||||
docker-release-trixie:
|
|
||||||
docker buildx build --push --pull --no-cache --platform linux/amd64,linux/arm64 --build-arg PG_MAJOR=$(PG_MAJOR) --build-arg DEBIAN_CODENAME=trixie -t pgvector/pgvector:pg$(PG_MAJOR)-trixie -t pgvector/pgvector:$(EXTVERSION)-pg$(PG_MAJOR)-trixie .
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.8.5
|
EXTVERSION = 0.8.0
|
||||||
|
|
||||||
DATA_built = sql\$(EXTENSION)--$(EXTVERSION).sql
|
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
|
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
|
||||||
@@ -31,9 +31,6 @@ LIBDIR = $(PGROOT)\lib
|
|||||||
PKGLIBDIR = $(PGROOT)\lib
|
PKGLIBDIR = $(PGROOT)\lib
|
||||||
SHAREDIR = $(PGROOT)\share
|
SHAREDIR = $(PGROOT)\share
|
||||||
|
|
||||||
# Use $(PGROOT)\bin\pg_regress for Postgres < 17
|
|
||||||
PG_REGRESS = $(LIBDIR)\pgxs\src\test\regress\pg_regress
|
|
||||||
|
|
||||||
CFLAGS = /nologo /I"$(INCLUDEDIR_SERVER)\port\win32_msvc" /I"$(INCLUDEDIR_SERVER)\port\win32" /I"$(INCLUDEDIR_SERVER)" /I"$(INCLUDEDIR)"
|
CFLAGS = /nologo /I"$(INCLUDEDIR_SERVER)\port\win32_msvc" /I"$(INCLUDEDIR_SERVER)\port\win32" /I"$(INCLUDEDIR_SERVER)" /I"$(INCLUDEDIR)"
|
||||||
|
|
||||||
CFLAGS = $(CFLAGS) $(PG_CFLAGS)
|
CFLAGS = $(CFLAGS) $(PG_CFLAGS)
|
||||||
@@ -57,11 +54,11 @@ install: all
|
|||||||
copy $(SHLIB) "$(PKGLIBDIR)"
|
copy $(SHLIB) "$(PKGLIBDIR)"
|
||||||
copy $(EXTENSION).control "$(SHAREDIR)\extension"
|
copy $(EXTENSION).control "$(SHAREDIR)\extension"
|
||||||
copy sql\$(EXTENSION)--*.sql "$(SHAREDIR)\extension"
|
copy sql\$(EXTENSION)--*.sql "$(SHAREDIR)\extension"
|
||||||
if not exist "$(INCLUDEDIR_SERVER)\extension\$(EXTENSION)" mkdir "$(INCLUDEDIR_SERVER)\extension\$(EXTENSION)"
|
mkdir "$(INCLUDEDIR_SERVER)\extension\$(EXTENSION)"
|
||||||
for %f in ($(HEADERS)) do copy %f "$(INCLUDEDIR_SERVER)\extension\$(EXTENSION)"
|
for %f in ($(HEADERS)) do copy %f "$(INCLUDEDIR_SERVER)\extension\$(EXTENSION)"
|
||||||
|
|
||||||
installcheck:
|
installcheck:
|
||||||
"$(PG_REGRESS)" --bindir="$(BINDIR)" $(REGRESS_OPTS) $(REGRESS)
|
"$(BINDIR)\pg_regress" --bindir="$(BINDIR)" $(REGRESS_OPTS) $(REGRESS)
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
del /f "$(PKGLIBDIR)\$(SHLIB)"
|
del /f "$(PKGLIBDIR)\$(SHLIB)"
|
||||||
|
|||||||
175
README.md
175
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
|
Plus [ACID](https://en.wikipedia.org/wiki/ACID) compliance, point-in-time recovery, JOINs, and all of the other [great features](https://www.postgresql.org/about/) of Postgres
|
||||||
|
|
||||||
Have a lot of vectors? Use [quantization](#scaling) to scale
|
|
||||||
|
|
||||||
[](https://github.com/pgvector/pgvector/actions)
|
[](https://github.com/pgvector/pgvector/actions)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
@@ -23,7 +21,7 @@ Compile and install the extension (supports Postgres 13+)
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd /tmp
|
cd /tmp
|
||||||
git clone --branch v0.8.5 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.8.0 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
make
|
make
|
||||||
make install # may need sudo
|
make install # may need sudo
|
||||||
@@ -31,16 +29,24 @@ make install # may need sudo
|
|||||||
|
|
||||||
See the [installation notes](#installation-notes---linux-and-mac) if you run into issues
|
See the [installation notes](#installation-notes---linux-and-mac) if you run into issues
|
||||||
|
|
||||||
You can also install it with [Docker](#docker), [Homebrew](#homebrew), [PGXN](#pgxn), [APT](#apt), [Yum](#yum), [pkg](#pkg), [APK](#apk), or [conda-forge](#conda-forge), and it comes preinstalled with [Postgres.app](#postgresapp) and many [hosted providers](#hosted-postgres). There are also instructions for [GitHub Actions](https://github.com/pgvector/setup-pgvector).
|
You can also install it with [Docker](#docker), [Homebrew](#homebrew), [PGXN](#pgxn), [APT](#apt), [Yum](#yum), [pkg](#pkg), or [conda-forge](#conda-forge), and it comes preinstalled with [Postgres.app](#postgresapp) and many [hosted providers](#hosted-postgres). There are also instructions for [GitHub Actions](https://github.com/pgvector/setup-pgvector).
|
||||||
|
|
||||||
### Windows
|
### Windows
|
||||||
|
|
||||||
Ensure [C++ support in Visual Studio](https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170#download-and-install-the-tools) is installed and run `x64 Native Tools Command Prompt for VS [version]` as administrator. Then use `nmake` to build:
|
Ensure [C++ support in Visual Studio](https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170#download-and-install-the-tools) is installed, and run:
|
||||||
|
|
||||||
```cmd
|
```cmd
|
||||||
set "PGROOT=C:\Program Files\PostgreSQL\18"
|
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: The exact path will vary depending on your Visual Studio version and edition
|
||||||
|
|
||||||
|
Then use `nmake` to build:
|
||||||
|
|
||||||
|
```cmd
|
||||||
|
set "PGROOT=C:\Program Files\PostgreSQL\16"
|
||||||
cd %TEMP%
|
cd %TEMP%
|
||||||
git clone --branch v0.8.5 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.8.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
|
||||||
@@ -312,11 +318,7 @@ You can also speed up index creation by increasing the number of parallel worker
|
|||||||
SET max_parallel_maintenance_workers = 7; -- plus leader
|
SET max_parallel_maintenance_workers = 7; -- plus leader
|
||||||
```
|
```
|
||||||
|
|
||||||
For a large number of workers, you may need to increase `max_parallel_workers` (8 by default)
|
For a large number of workers, you may also need to increase `max_parallel_workers` (8 by default)
|
||||||
|
|
||||||
The [index options](#index-options) also have a significant impact on build time (use the defaults unless seeing low recall)
|
|
||||||
|
|
||||||
Use [binary quantization](#binary-quantization) for faster build times at scale
|
|
||||||
|
|
||||||
### Indexing Progress
|
### Indexing Progress
|
||||||
|
|
||||||
@@ -447,7 +449,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);
|
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
|
```sql
|
||||||
SET hnsw.iterative_scan = strict_order;
|
SET hnsw.iterative_scan = strict_order;
|
||||||
@@ -465,18 +473,10 @@ If filtering by many different values, consider [partitioning](https://www.postg
|
|||||||
CREATE TABLE items (embedding vector(3), category_id int) PARTITION BY LIST(category_id);
|
CREATE TABLE items (embedding vector(3), category_id int) PARTITION BY LIST(category_id);
|
||||||
```
|
```
|
||||||
|
|
||||||
## Multitenancy
|
|
||||||
|
|
||||||
For applications with multiple tenants, sharing an approximate index between tenants means vectors from one tenant can affect recall (and speed) for other tenants.
|
|
||||||
|
|
||||||
For tenant isolation, use [list partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html) or separate tables.
|
|
||||||
|
|
||||||
```sql
|
|
||||||
CREATE TABLE items (customer_id int, embedding vector(3)) PARTITION BY LIST(customer_id);
|
|
||||||
```
|
|
||||||
|
|
||||||
## Iterative Index Scans
|
## Iterative Index Scans
|
||||||
|
|
||||||
|
*Added in 0.8.0*
|
||||||
|
|
||||||
With approximate indexes, queries with filtering can return less results since filtering is applied *after* the index is scanned. Starting with 0.8.0, you can enable iterative index scans, which will automatically scan more of the index until enough results are found (or it reaches `hnsw.max_scan_tuples` or `ivfflat.max_probes`).
|
With approximate indexes, queries with filtering can return less results since filtering is applied *after* the index is scanned. Starting with 0.8.0, you can enable iterative index scans, which will automatically scan more of the index until enough results are found (or it reaches `hnsw.max_scan_tuples` or `ivfflat.max_probes`).
|
||||||
|
|
||||||
Iterative scans can use strict or relaxed ordering.
|
Iterative scans can use strict or relaxed ordering.
|
||||||
@@ -500,11 +500,9 @@ With relaxed ordering, you can use a [materialized CTE](https://www.postgresql.o
|
|||||||
```sql
|
```sql
|
||||||
WITH relaxed_results AS MATERIALIZED (
|
WITH relaxed_results AS MATERIALIZED (
|
||||||
SELECT id, embedding <-> '[1,2,3]' AS distance FROM items WHERE category_id = 123 ORDER BY distance LIMIT 5
|
SELECT id, embedding <-> '[1,2,3]' AS distance FROM items WHERE category_id = 123 ORDER BY distance LIMIT 5
|
||||||
) SELECT * FROM relaxed_results ORDER BY distance + 0;
|
) SELECT * FROM relaxed_results ORDER BY distance;
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: `+ 0` is needed for Postgres 17+
|
|
||||||
|
|
||||||
For queries that filter by distance, use a materialized CTE and place the distance filter outside of it for best performance (due to the [current behavior](https://www.postgresql.org/message-id/flat/CAOdR5yGUoMQ6j7M5hNUXrySzaqZVGf_Ne%2B8fwZMRKTFxU1nbJg%40mail.gmail.com) of the Postgres executor)
|
For queries that filter by distance, use a materialized CTE and place the distance filter outside of it for best performance (due to the [current behavior](https://www.postgresql.org/message-id/flat/CAOdR5yGUoMQ6j7M5hNUXrySzaqZVGf_Ne%2B8fwZMRKTFxU1nbJg%40mail.gmail.com) of the Postgres executor)
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
@@ -681,10 +679,6 @@ SHOW shared_buffers;
|
|||||||
|
|
||||||
Be sure to restart Postgres for changes to take effect.
|
Be sure to restart Postgres for changes to take effect.
|
||||||
|
|
||||||
### Storing
|
|
||||||
|
|
||||||
Use the `halfvec` type instead of `vector` for a smaller working set.
|
|
||||||
|
|
||||||
### Loading
|
### Loading
|
||||||
|
|
||||||
Use `COPY` for bulk loading data ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/loading/example.py)).
|
Use `COPY` for bulk loading data ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/loading/example.py)).
|
||||||
@@ -699,8 +693,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).
|
See index build time for [HNSW](#index-build-time) and [IVFFlat](#index-build-time-1).
|
||||||
|
|
||||||
Use [binary quantization](#binary-quantization) for smaller indexes and faster build times at scale.
|
|
||||||
|
|
||||||
In production environments, create indexes concurrently to avoid blocking writes.
|
In production environments, create indexes concurrently to avoid blocking writes.
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
@@ -709,10 +701,10 @@ CREATE INDEX CONCURRENTLY ...
|
|||||||
|
|
||||||
### Querying
|
### Querying
|
||||||
|
|
||||||
Use `EXPLAIN (ANALYZE, BUFFERS)` to debug performance.
|
Use `EXPLAIN ANALYZE` to debug performance.
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
|
EXPLAIN ANALYZE SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Exact Search
|
#### Exact Search
|
||||||
@@ -731,8 +723,6 @@ SELECT * FROM items ORDER BY embedding <#> '[3,1,2]' LIMIT 5;
|
|||||||
|
|
||||||
#### Approximate Search
|
#### Approximate Search
|
||||||
|
|
||||||
Use [binary quantization](#binary-quantization) with re-ranking to keep indexes in-memory at scale.
|
|
||||||
|
|
||||||
To speed up queries with an IVFFlat index, increase the number of inverted lists (at the expense of recall).
|
To speed up queries with an IVFFlat index, increase the number of inverted lists (at the expense of recall).
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
@@ -748,20 +738,23 @@ REINDEX INDEX CONCURRENTLY index_name;
|
|||||||
VACUUM table_name;
|
VACUUM table_name;
|
||||||
```
|
```
|
||||||
|
|
||||||
## Scaling
|
|
||||||
|
|
||||||
For a smaller working set:
|
|
||||||
|
|
||||||
1. Use the `halfvec` type instead of `vector` for tables
|
|
||||||
2. Use [binary quantization](#binary-quantization) for indexes (with re-ranking for search)
|
|
||||||
|
|
||||||
Scale vertically by increasing memory, CPU, and storage on a single instance. Use existing tools to [tune parameters](#tuning) and [monitor performance](#monitoring).
|
|
||||||
|
|
||||||
Scale horizontally with [replicas](https://www.postgresql.org/docs/current/hot-standby.html), or use [Citus](https://github.com/citusdata/citus), [PgDog](https://github.com/pgdogdev/pgdog), or another approach for sharding ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/citus/example.py)).
|
|
||||||
|
|
||||||
## Monitoring
|
## Monitoring
|
||||||
|
|
||||||
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;
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: Replace `total_plan_time + total_exec_time` with `total_time` for Postgres < 13
|
||||||
|
|
||||||
Monitor recall by comparing results from approximate search with exact search.
|
Monitor recall by comparing results from approximate search with exact search.
|
||||||
|
|
||||||
@@ -772,18 +765,23 @@ SELECT ...
|
|||||||
COMMIT;
|
COMMIT;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Scaling
|
||||||
|
|
||||||
|
Scale pgvector the same way you scale Postgres.
|
||||||
|
|
||||||
|
Scale vertically by increasing memory, CPU, and storage on a single instance. Use existing tools to [tune parameters](#tuning) and [monitor performance](#monitoring).
|
||||||
|
|
||||||
|
Scale horizontally with [replicas](https://www.postgresql.org/docs/current/hot-standby.html), or use [Citus](https://github.com/citusdata/citus) or another approach for sharding ([example](https://github.com/pgvector/pgvector-python/blob/master/examples/citus/example.py)).
|
||||||
|
|
||||||
## Languages
|
## Languages
|
||||||
|
|
||||||
Use pgvector from any language with a Postgres client. You can even generate and store vectors in one language and query them in another.
|
Use pgvector from any language with a Postgres client. You can even generate and store vectors in one language and query them in another.
|
||||||
|
|
||||||
Language | Libraries / Examples
|
Language | Libraries / Examples
|
||||||
--- | ---
|
--- | ---
|
||||||
Ada | [pgvector-ada](https://github.com/pgvector/pgvector-ada)
|
|
||||||
Algol | [pgvector-algol](https://github.com/pgvector/pgvector-algol)
|
|
||||||
C | [pgvector-c](https://github.com/pgvector/pgvector-c)
|
C | [pgvector-c](https://github.com/pgvector/pgvector-c)
|
||||||
C++ | [pgvector-cpp](https://github.com/pgvector/pgvector-cpp)
|
C++ | [pgvector-cpp](https://github.com/pgvector/pgvector-cpp)
|
||||||
C#, F#, Visual Basic | [pgvector-dotnet](https://github.com/pgvector/pgvector-dotnet)
|
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)
|
Crystal | [pgvector-crystal](https://github.com/pgvector/pgvector-crystal)
|
||||||
D | [pgvector-d](https://github.com/pgvector/pgvector-d)
|
D | [pgvector-d](https://github.com/pgvector/pgvector-d)
|
||||||
Dart | [pgvector-dart](https://github.com/pgvector/pgvector-dart)
|
Dart | [pgvector-dart](https://github.com/pgvector/pgvector-dart)
|
||||||
@@ -795,23 +793,19 @@ Go | [pgvector-go](https://github.com/pgvector/pgvector-go)
|
|||||||
Haskell | [pgvector-haskell](https://github.com/pgvector/pgvector-haskell)
|
Haskell | [pgvector-haskell](https://github.com/pgvector/pgvector-haskell)
|
||||||
Java, Kotlin, Groovy, Scala | [pgvector-java](https://github.com/pgvector/pgvector-java)
|
Java, Kotlin, Groovy, Scala | [pgvector-java](https://github.com/pgvector/pgvector-java)
|
||||||
JavaScript, TypeScript | [pgvector-node](https://github.com/pgvector/pgvector-node)
|
JavaScript, TypeScript | [pgvector-node](https://github.com/pgvector/pgvector-node)
|
||||||
Julia | [Pgvector.jl](https://github.com/pgvector/Pgvector.jl)
|
Julia | [pgvector-julia](https://github.com/pgvector/pgvector-julia)
|
||||||
Lisp | [pgvector-lisp](https://github.com/pgvector/pgvector-lisp)
|
Lisp | [pgvector-lisp](https://github.com/pgvector/pgvector-lisp)
|
||||||
Lua | [pgvector-lua](https://github.com/pgvector/pgvector-lua)
|
Lua | [pgvector-lua](https://github.com/pgvector/pgvector-lua)
|
||||||
Nim | [pgvector-nim](https://github.com/pgvector/pgvector-nim)
|
Nim | [pgvector-nim](https://github.com/pgvector/pgvector-nim)
|
||||||
OCaml | [pgvector-ocaml](https://github.com/pgvector/pgvector-ocaml)
|
OCaml | [pgvector-ocaml](https://github.com/pgvector/pgvector-ocaml)
|
||||||
Pascal | [pgvector-pascal](https://github.com/pgvector/pgvector-pascal)
|
|
||||||
Perl | [pgvector-perl](https://github.com/pgvector/pgvector-perl)
|
Perl | [pgvector-perl](https://github.com/pgvector/pgvector-perl)
|
||||||
PHP | [pgvector-php](https://github.com/pgvector/pgvector-php)
|
PHP | [pgvector-php](https://github.com/pgvector/pgvector-php)
|
||||||
Prolog | [pgvector-prolog](https://github.com/pgvector/pgvector-prolog)
|
|
||||||
Python | [pgvector-python](https://github.com/pgvector/pgvector-python)
|
Python | [pgvector-python](https://github.com/pgvector/pgvector-python)
|
||||||
R | [pgvector-r](https://github.com/pgvector/pgvector-r)
|
R | [pgvector-r](https://github.com/pgvector/pgvector-r)
|
||||||
Racket | [pgvector-racket](https://github.com/pgvector/pgvector-racket)
|
|
||||||
Raku | [pgvector-raku](https://github.com/pgvector/pgvector-raku)
|
Raku | [pgvector-raku](https://github.com/pgvector/pgvector-raku)
|
||||||
Ruby | [pgvector-ruby](https://github.com/pgvector/pgvector-ruby), [Neighbor](https://github.com/ankane/neighbor)
|
Ruby | [pgvector-ruby](https://github.com/pgvector/pgvector-ruby), [Neighbor](https://github.com/ankane/neighbor)
|
||||||
Rust | [pgvector-rust](https://github.com/pgvector/pgvector-rust)
|
Rust | [pgvector-rust](https://github.com/pgvector/pgvector-rust)
|
||||||
Swift | [pgvector-swift](https://github.com/pgvector/pgvector-swift)
|
Swift | [pgvector-swift](https://github.com/pgvector/pgvector-swift)
|
||||||
Tcl | [pgvector-tcl](https://github.com/pgvector/pgvector-tcl)
|
|
||||||
Zig | [pgvector-zig](https://github.com/pgvector/pgvector-zig)
|
Zig | [pgvector-zig](https://github.com/pgvector/pgvector-zig)
|
||||||
|
|
||||||
## Frequently Asked Questions
|
## Frequently Asked Questions
|
||||||
@@ -826,11 +820,11 @@ Yes, pgvector uses the write-ahead log (WAL), which allows for replication and p
|
|||||||
|
|
||||||
#### What if I want to index vectors with more than 2,000 dimensions?
|
#### What if I want to index vectors with more than 2,000 dimensions?
|
||||||
|
|
||||||
You can use [half-precision vectors](#half-precision-vectors) or [half-precision indexing](#half-precision-indexing) to index up to 4,000 dimensions or [binary quantization](#binary-quantization) to index up to 64,000 dimensions. Other options are [indexing subvectors](#indexing-subvectors) (for models that support it) or [dimensionality reduction](https://en.wikipedia.org/wiki/Dimensionality_reduction).
|
You can use [half-precision indexing](#half-precision-indexing) to index up to 4,000 dimensions or [binary quantization](#binary-quantization) to index up to 64,000 dimensions. Another option is [dimensionality reduction](https://en.wikipedia.org/wiki/Dimensionality_reduction).
|
||||||
|
|
||||||
#### Can I store vectors with different dimensions in the same column?
|
#### Can I store vectors with different dimensions in the same column?
|
||||||
|
|
||||||
You can use `vector` as the type (instead of `vector(n)`).
|
You can use `vector` as the type (instead of `vector(3)`).
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
CREATE TABLE embeddings (model_id bigint, item_id bigint, embedding vector, PRIMARY KEY (model_id, item_id));
|
CREATE TABLE embeddings (model_id bigint, item_id bigint, embedding vector, PRIMARY KEY (model_id, item_id));
|
||||||
@@ -885,8 +879,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'));
|
SELECT pg_size_pretty(pg_relation_size('index_name'));
|
||||||
```
|
```
|
||||||
|
|
||||||
Use [half-precision indexing](#half-precision-indexing) or [binary quantization](#binary-quantization) for smaller indexes.
|
|
||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
|
|
||||||
#### Why isn’t a query using an index?
|
#### Why isn’t a query using an index?
|
||||||
@@ -1080,7 +1072,7 @@ l2_normalize(sparsevec) → sparsevec | Normalize with Euclidean norm | 0.7.0
|
|||||||
If your machine has multiple Postgres installations, specify the path to [pg_config](https://www.postgresql.org/docs/current/app-pgconfig.html) with:
|
If your machine has multiple Postgres installations, specify the path to [pg_config](https://www.postgresql.org/docs/current/app-pgconfig.html) with:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
export PG_CONFIG=/Library/PostgreSQL/18/bin/pg_config
|
export PG_CONFIG=/Library/PostgreSQL/17/bin/pg_config
|
||||||
```
|
```
|
||||||
|
|
||||||
Then re-run the installation instructions (run `make clean` before `make` if needed). If `sudo` is needed for `make install`, use:
|
Then re-run the installation instructions (run `make clean` before `make` if needed). If `sudo` is needed for `make install`, use:
|
||||||
@@ -1091,11 +1083,11 @@ sudo --preserve-env=PG_CONFIG make install
|
|||||||
|
|
||||||
A few common paths on Mac are:
|
A few common paths on Mac are:
|
||||||
|
|
||||||
- EDB installer - `/Library/PostgreSQL/18/bin/pg_config`
|
- EDB installer - `/Library/PostgreSQL/17/bin/pg_config`
|
||||||
- Homebrew (arm64) - `/opt/homebrew/opt/postgresql@18/bin/pg_config`
|
- Homebrew (arm64) - `/opt/homebrew/opt/postgresql@17/bin/pg_config`
|
||||||
- Homebrew (x86-64) - `/usr/local/opt/postgresql@18/bin/pg_config`
|
- Homebrew (x86-64) - `/usr/local/opt/postgresql@17/bin/pg_config`
|
||||||
|
|
||||||
Note: Replace `18` with your Postgres server version
|
Note: Replace `17` with your Postgres server version
|
||||||
|
|
||||||
### Missing Header
|
### Missing Header
|
||||||
|
|
||||||
@@ -1104,10 +1096,10 @@ If compilation fails with `fatal error: postgres.h: No such file or directory`,
|
|||||||
For Ubuntu and Debian, use:
|
For Ubuntu and Debian, use:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
sudo apt install postgresql-server-dev-18
|
sudo apt install postgresql-server-dev-17
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: Replace `18` with your Postgres server version
|
Note: Replace `17` with your Postgres server version
|
||||||
|
|
||||||
### Missing SDK
|
### Missing SDK
|
||||||
|
|
||||||
@@ -1137,7 +1129,7 @@ If compilation fails with `Cannot open include file: 'postgres.h': No such file
|
|||||||
|
|
||||||
### Mismatched Architecture
|
### Mismatched Architecture
|
||||||
|
|
||||||
If compilation fails with `error C2196: case value '4' already used`, make sure you’re using the `x64 Native Tools Command Prompt`. Then run `nmake /F Makefile.win clean` and re-run the installation instructions.
|
If compilation fails with `error C2196: case value '4' already used`, make sure `vcvars64.bat` was called. Then run `nmake /F Makefile.win clean` and re-run the installation instructions.
|
||||||
|
|
||||||
### Missing Symbol
|
### Missing Symbol
|
||||||
|
|
||||||
@@ -1154,32 +1146,17 @@ If installation fails with `Access is denied`, re-run the installation instructi
|
|||||||
Get the [Docker image](https://hub.docker.com/r/pgvector/pgvector) with:
|
Get the [Docker image](https://hub.docker.com/r/pgvector/pgvector) with:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
docker pull pgvector/pgvector:pg18-trixie
|
docker pull pgvector/pgvector:pg17
|
||||||
```
|
```
|
||||||
|
|
||||||
This adds pgvector to the [Postgres image](https://hub.docker.com/_/postgres) (replace `18` with your Postgres server version, and run it the same way).
|
This adds pgvector to the [Postgres image](https://hub.docker.com/_/postgres) (replace `17` with your Postgres server version, and run it the same way).
|
||||||
|
|
||||||
Supported tags are:
|
|
||||||
|
|
||||||
- `pg18-trixie`, `0.8.5-pg18-trixie`
|
|
||||||
- `pg18-bookworm`, `0.8.5-pg18-bookworm`, `pg18`, `0.8.5-pg18`
|
|
||||||
- `pg17-trixie`, `0.8.5-pg17-trixie`
|
|
||||||
- `pg17-bookworm`, `0.8.5-pg17-bookworm`, `pg17`, `0.8.5-pg17`
|
|
||||||
- `pg16-trixie`, `0.8.5-pg16-trixie`
|
|
||||||
- `pg16-bookworm`, `0.8.5-pg16-bookworm`, `pg16`, `0.8.5-pg16`
|
|
||||||
- `pg15-trixie`, `0.8.5-pg15-trixie`
|
|
||||||
- `pg15-bookworm`, `0.8.5-pg15-bookworm`, `pg15`, `0.8.5-pg15`
|
|
||||||
- `pg14-trixie`, `0.8.5-pg14-trixie`
|
|
||||||
- `pg14-bookworm`, `0.8.5-pg14-bookworm`, `pg14`, `0.8.5-pg14`
|
|
||||||
- `pg13-trixie`, `0.8.5-pg13-trixie`
|
|
||||||
- `pg13-bookworm`, `0.8.5-pg13-bookworm`, `pg13`, `0.8.5-pg13`
|
|
||||||
|
|
||||||
You can also build the image manually:
|
You can also build the image manually:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone --branch v0.8.5 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.8.0 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
docker build --pull --build-arg PG_MAJOR=18 -t myuser/pgvector .
|
docker build --pull --build-arg PG_MAJOR=17 -t myuser/pgvector .
|
||||||
```
|
```
|
||||||
|
|
||||||
If you increase `maintenance_work_mem`, make sure `--shm-size` is at least that size to avoid an error with parallel HNSW index builds.
|
If you increase `maintenance_work_mem`, make sure `--shm-size` is at least that size to avoid an error with parallel HNSW index builds.
|
||||||
@@ -1196,7 +1173,7 @@ With Homebrew Postgres, you can use:
|
|||||||
brew install pgvector
|
brew install pgvector
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: This only adds it to the `postgresql@18` and `postgresql@17` formulas
|
Note: This only adds it to the `postgresql@17` and `postgresql@14` formulas
|
||||||
|
|
||||||
### PGXN
|
### PGXN
|
||||||
|
|
||||||
@@ -1211,29 +1188,29 @@ pgxn install vector
|
|||||||
Debian and Ubuntu packages are available from the [PostgreSQL APT Repository](https://wiki.postgresql.org/wiki/Apt). Follow the [setup instructions](https://wiki.postgresql.org/wiki/Apt#Quickstart) and run:
|
Debian and Ubuntu packages are available from the [PostgreSQL APT Repository](https://wiki.postgresql.org/wiki/Apt). Follow the [setup instructions](https://wiki.postgresql.org/wiki/Apt#Quickstart) and run:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
sudo apt install postgresql-18-pgvector
|
sudo apt install postgresql-17-pgvector
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: Replace `18` with your Postgres server version
|
Note: Replace `17` with your Postgres server version
|
||||||
|
|
||||||
### Yum
|
### Yum
|
||||||
|
|
||||||
RPM packages are available from the [PostgreSQL Yum Repository](https://yum.postgresql.org/). Follow the [setup instructions](https://www.postgresql.org/download/linux/redhat/) for your distribution and run:
|
RPM packages are available from the [PostgreSQL Yum Repository](https://yum.postgresql.org/). Follow the [setup instructions](https://www.postgresql.org/download/linux/redhat/) for your distribution and run:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
sudo yum install pgvector_18
|
sudo yum install pgvector_17
|
||||||
# or
|
# or
|
||||||
sudo dnf install pgvector_18
|
sudo dnf install pgvector_17
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: Replace `18` with your Postgres server version
|
Note: Replace `17` with your Postgres server version
|
||||||
|
|
||||||
### pkg
|
### pkg
|
||||||
|
|
||||||
Install the FreeBSD package with:
|
Install the FreeBSD package with:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pkg install postgresql18-pgvector
|
pkg install postgresql16-pgvector
|
||||||
```
|
```
|
||||||
|
|
||||||
or the port with:
|
or the port with:
|
||||||
@@ -1243,14 +1220,6 @@ cd /usr/ports/databases/pgvector
|
|||||||
make install
|
make install
|
||||||
```
|
```
|
||||||
|
|
||||||
### APK
|
|
||||||
|
|
||||||
Install the Alpine package with:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
apk add postgresql-pgvector
|
|
||||||
```
|
|
||||||
|
|
||||||
### conda-forge
|
### conda-forge
|
||||||
|
|
||||||
With Conda Postgres, install from [conda-forge](https://anaconda.org/conda-forge/pgvector) with:
|
With Conda Postgres, install from [conda-forge](https://anaconda.org/conda-forge/pgvector) with:
|
||||||
@@ -1339,7 +1308,7 @@ make clean && PG_CFLAGS="-DUSE_ASSERT_CHECKING" make && make install
|
|||||||
To enable benchmarking:
|
To enable benchmarking:
|
||||||
|
|
||||||
```sh
|
```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:
|
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.1'" 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.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
|
|
||||||
@@ -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.4'" 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.5'" to load this file. \quit
|
|
||||||
@@ -31,12 +31,10 @@
|
|||||||
#define BIT_TARGET_CLONES
|
#define BIT_TARGET_CLONES
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Use built-ins when possible for Postgres < 19 for inlining */
|
/* Use built-ins when possible for inlining */
|
||||||
#if PG_VERSION_NUM >= 190000
|
#if defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_INT_64)
|
||||||
#define popcount64(x) pg_popcount64(x)
|
|
||||||
#elif defined(HAVE__BUILTIN_POPCOUNT) && (defined(HAVE_LONG_INT_64) || SIZEOF_LONG == 8)
|
|
||||||
#define popcount64(x) __builtin_popcountl(x)
|
#define popcount64(x) __builtin_popcountl(x)
|
||||||
#elif defined(HAVE__BUILTIN_POPCOUNT) && (defined(HAVE_LONG_LONG_INT_64) || SIZEOF_LONG_LONG == 8)
|
#elif defined(HAVE__BUILTIN_POPCOUNT) && defined(HAVE_LONG_LONG_INT_64)
|
||||||
#define popcount64(x) __builtin_popcountll(x)
|
#define popcount64(x) __builtin_popcountll(x)
|
||||||
#elif !defined(_MSC_VER)
|
#elif !defined(_MSC_VER)
|
||||||
/* Fails to resolve with MSVC */
|
/* Fails to resolve with MSVC */
|
||||||
@@ -171,7 +169,7 @@ BitJaccardDistanceAvx512Popcount(uint32 bytes, unsigned char *ax, unsigned char
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
TARGET_XSAVE static bool
|
TARGET_XSAVE static bool
|
||||||
SupportsAvx512Popcount(void)
|
SupportsAvx512Popcount()
|
||||||
{
|
{
|
||||||
unsigned int exx[4] = {0, 0, 0, 0};
|
unsigned int exx[4] = {0, 0, 0, 0};
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,9 @@
|
|||||||
#error "Requires PostgreSQL 13+"
|
#error "Requires PostgreSQL 13+"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Check architecture in first header */
|
||||||
|
StaticAssertDecl(sizeof(Datum) == SIZEOF_DATUM, "Architecture mismatch");
|
||||||
|
|
||||||
extern uint64 (*BitHammingDistance) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 distance);
|
extern uint64 (*BitHammingDistance) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 distance);
|
||||||
extern double (*BitJaccardDistance) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 ab, uint64 aa, uint64 bb);
|
extern double (*BitJaccardDistance) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 ab, uint64 aa, uint64 bb);
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
#include "bitutils.h"
|
#include "bitutils.h"
|
||||||
#include "bitvec.h"
|
#include "bitvec.h"
|
||||||
#include "fmgr.h"
|
|
||||||
#include "utils/varbit.h"
|
#include "utils/varbit.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#include "halfutils.h"
|
#include "halfutils.h"
|
||||||
#include "halfvec.h"
|
#include "halfvec.h"
|
||||||
|
|
||||||
|
|||||||
@@ -13,20 +13,12 @@
|
|||||||
#include "port.h" /* for strtof() */
|
#include "port.h" /* for strtof() */
|
||||||
#include "sparsevec.h"
|
#include "sparsevec.h"
|
||||||
#include "utils/array.h"
|
#include "utils/array.h"
|
||||||
|
#include "utils/builtins.h"
|
||||||
#include "utils/float.h"
|
#include "utils/float.h"
|
||||||
#include "utils/fmgrprotos.h"
|
|
||||||
#include "utils/lsyscache.h"
|
#include "utils/lsyscache.h"
|
||||||
#include "utils/varbit.h"
|
#include "utils/numeric.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 170000
|
|
||||||
#include "parser/scansup.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1)
|
#define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1)
|
||||||
#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1))
|
#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1))
|
||||||
|
|
||||||
@@ -137,9 +129,9 @@ InitHalfVector(int dim)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 170000
|
/*
|
||||||
#define halfvec_isspace(ch) scanner_isspace(ch)
|
* Check for whitespace, since array_isspace() is static
|
||||||
#else
|
*/
|
||||||
static inline bool
|
static inline bool
|
||||||
halfvec_isspace(char ch)
|
halfvec_isspace(char ch)
|
||||||
{
|
{
|
||||||
@@ -152,7 +144,6 @@ halfvec_isspace(char ch)
|
|||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check state array
|
* Check state array
|
||||||
@@ -907,21 +898,8 @@ halfvec_binary_quantize(PG_FUNCTION_ARGS)
|
|||||||
half *ax = a->x;
|
half *ax = a->x;
|
||||||
VarBit *result = InitBitVector(a->dim);
|
VarBit *result = InitBitVector(a->dim);
|
||||||
unsigned char *rx = VARBITS(result);
|
unsigned char *rx = VARBITS(result);
|
||||||
int i = 0;
|
|
||||||
int count = (a->dim / 8) * 8;
|
|
||||||
|
|
||||||
/* Auto-vectorized on aarch64 */
|
for (int i = 0; i < a->dim; i++)
|
||||||
for (; i < count; i += 8)
|
|
||||||
{
|
|
||||||
unsigned char result_byte = 0;
|
|
||||||
|
|
||||||
for (int j = 0; j < 8; j++)
|
|
||||||
result_byte |= (HalfToFloat4(ax[i + j]) > 0) << (7 - j);
|
|
||||||
|
|
||||||
rx[i / 8] = result_byte;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; i < a->dim; i++)
|
|
||||||
rx[i / 8] |= (HalfToFloat4(ax[i]) > 0) << (7 - (i % 8));
|
rx[i / 8] |= (HalfToFloat4(ax[i]) > 0) << (7 - (i % 8));
|
||||||
|
|
||||||
PG_RETURN_VARBIT_P(result);
|
PG_RETURN_VARBIT_P(result);
|
||||||
|
|||||||
89
src/hnsw.c
89
src/hnsw.c
@@ -1,25 +1,18 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <limits.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "access/amapi.h"
|
#include "access/amapi.h"
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/reloptions.h"
|
#include "access/reloptions.h"
|
||||||
#include "commands/progress.h"
|
#include "commands/progress.h"
|
||||||
#include "commands/vacuum.h"
|
#include "commands/vacuum.h"
|
||||||
#include "fmgr.h"
|
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
#include "nodes/pg_list.h"
|
|
||||||
#include "storage/lwlock.h"
|
|
||||||
#include "utils/float.h"
|
#include "utils/float.h"
|
||||||
#include "utils/guc.h"
|
#include "utils/guc.h"
|
||||||
#include "utils/relcache.h"
|
|
||||||
#include "utils/selfuncs.h"
|
#include "utils/selfuncs.h"
|
||||||
#include "utils/spccache.h"
|
#include "utils/spccache.h"
|
||||||
#include "vector.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM < 150000
|
#if PG_VERSION_NUM < 150000
|
||||||
#define MarkGUCPrefixReserved(x) EmitWarningsOnPlaceholders(x)
|
#define MarkGUCPrefixReserved(x) EmitWarningsOnPlaceholders(x)
|
||||||
@@ -59,20 +52,12 @@ HnswInitLockTranche(void)
|
|||||||
sizeof(int) * 1,
|
sizeof(int) * 1,
|
||||||
&found);
|
&found);
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
|
||||||
#if PG_VERSION_NUM >= 190000
|
|
||||||
tranche_ids[0] = LWLockNewTrancheId("HnswBuild");
|
|
||||||
#else
|
|
||||||
tranche_ids[0] = LWLockNewTrancheId();
|
tranche_ids[0] = LWLockNewTrancheId();
|
||||||
#endif
|
|
||||||
}
|
|
||||||
hnsw_lock_tranche_id = tranche_ids[0];
|
hnsw_lock_tranche_id = tranche_ids[0];
|
||||||
LWLockRelease(AddinShmemInitLock);
|
LWLockRelease(AddinShmemInitLock);
|
||||||
|
|
||||||
#if PG_VERSION_NUM < 190000
|
|
||||||
/* Per-backend registration of the tranche ID */
|
/* Per-backend registration of the tranche ID */
|
||||||
LWLockRegisterTranche(hnsw_lock_tranche_id, "HnswBuild");
|
LWLockRegisterTranche(hnsw_lock_tranche_id, "HnswBuild");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -145,7 +130,7 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
|||||||
Relation index;
|
Relation index;
|
||||||
|
|
||||||
/* Never use index without order */
|
/* Never use index without order */
|
||||||
if (path->indexorderbys == NIL)
|
if (path->indexorderbys == NULL)
|
||||||
{
|
{
|
||||||
*indexStartupCost = get_float8_infinity();
|
*indexStartupCost = get_float8_infinity();
|
||||||
*indexTotalCost = get_float8_infinity();
|
*indexTotalCost = get_float8_infinity();
|
||||||
@@ -267,64 +252,6 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(hnswhandler);
|
|||||||
Datum
|
Datum
|
||||||
hnswhandler(PG_FUNCTION_ARGS)
|
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);
|
IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
|
||||||
|
|
||||||
amroutine->amstrategies = 0;
|
amroutine->amstrategies = 0;
|
||||||
@@ -332,11 +259,6 @@ hnswhandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->amoptsprocnum = 0;
|
amroutine->amoptsprocnum = 0;
|
||||||
amroutine->amcanorder = false;
|
amroutine->amcanorder = false;
|
||||||
amroutine->amcanorderbyop = true;
|
amroutine->amcanorderbyop = true;
|
||||||
#if PG_VERSION_NUM >= 180000
|
|
||||||
amroutine->amcanhash = false;
|
|
||||||
amroutine->amconsistentequality = false;
|
|
||||||
amroutine->amconsistentordering = false;
|
|
||||||
#endif
|
|
||||||
amroutine->amcanbackward = false; /* can change direction mid-scan */
|
amroutine->amcanbackward = false; /* can change direction mid-scan */
|
||||||
amroutine->amcanunique = false;
|
amroutine->amcanunique = false;
|
||||||
amroutine->amcanmulticol = false;
|
amroutine->amcanmulticol = false;
|
||||||
@@ -369,9 +291,6 @@ hnswhandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->amvacuumcleanup = hnswvacuumcleanup;
|
amroutine->amvacuumcleanup = hnswvacuumcleanup;
|
||||||
amroutine->amcanreturn = NULL;
|
amroutine->amcanreturn = NULL;
|
||||||
amroutine->amcostestimate = hnswcostestimate;
|
amroutine->amcostestimate = hnswcostestimate;
|
||||||
#if PG_VERSION_NUM >= 180000
|
|
||||||
amroutine->amgettreeheight = NULL;
|
|
||||||
#endif
|
|
||||||
amroutine->amoptions = hnswoptions;
|
amroutine->amoptions = hnswoptions;
|
||||||
amroutine->amproperty = NULL; /* TODO AMPROP_DISTANCE_ORDERABLE */
|
amroutine->amproperty = NULL; /* TODO AMPROP_DISTANCE_ORDERABLE */
|
||||||
amroutine->ambuildphasename = hnswbuildphasename;
|
amroutine->ambuildphasename = hnswbuildphasename;
|
||||||
@@ -392,11 +311,5 @@ hnswhandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->aminitparallelscan = NULL;
|
amroutine->aminitparallelscan = NULL;
|
||||||
amroutine->amparallelrescan = NULL;
|
amroutine->amparallelrescan = NULL;
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 180000
|
|
||||||
amroutine->amtranslatestrategy = NULL;
|
|
||||||
amroutine->amtranslatecmptype = NULL;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
PG_RETURN_POINTER(amroutine);
|
PG_RETURN_POINTER(amroutine);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|||||||
34
src/hnsw.h
34
src/hnsw.h
@@ -3,29 +3,15 @@
|
|||||||
|
|
||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#include "access/genam.h"
|
#include "access/genam.h"
|
||||||
#include "access/parallel.h"
|
#include "access/parallel.h"
|
||||||
#include "lib/pairingheap.h"
|
#include "lib/pairingheap.h"
|
||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "port.h" /* for random() */
|
#include "port.h" /* for random() */
|
||||||
#include "storage/bufpage.h"
|
|
||||||
#include "storage/condition_variable.h"
|
|
||||||
#include "storage/lwlock.h"
|
|
||||||
#include "storage/s_lock.h"
|
|
||||||
#include "utils/relptr.h"
|
#include "utils/relptr.h"
|
||||||
#include "utils/sampling.h"
|
#include "utils/sampling.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
#ifdef HNSW_BENCH
|
|
||||||
#include "portability/instr_time.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 190000
|
|
||||||
typedef Pointer Item;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define HNSW_MAX_DIM 2000
|
#define HNSW_MAX_DIM 2000
|
||||||
#define HNSW_MAX_NNZ 1000
|
#define HNSW_MAX_NNZ 1000
|
||||||
|
|
||||||
@@ -82,21 +68,6 @@ typedef Pointer Item;
|
|||||||
#define HnswPageGetOpaque(page) ((HnswPageOpaque) PageGetSpecialPointer(page))
|
#define HnswPageGetOpaque(page) ((HnswPageOpaque) PageGetSpecialPointer(page))
|
||||||
#define HnswPageGetMeta(page) ((HnswMetaPageData *) PageGetContents(page))
|
#define HnswPageGetMeta(page) ((HnswMetaPageData *) PageGetContents(page))
|
||||||
|
|
||||||
#ifdef HNSW_BENCH
|
|
||||||
#define HnswBench(name, code) \
|
|
||||||
do { \
|
|
||||||
instr_time start; \
|
|
||||||
instr_time duration; \
|
|
||||||
INSTR_TIME_SET_CURRENT(start); \
|
|
||||||
(code); \
|
|
||||||
INSTR_TIME_SET_CURRENT(duration); \
|
|
||||||
INSTR_TIME_SUBTRACT(duration, start); \
|
|
||||||
elog(INFO, "%s: %.3f ms", name, INSTR_TIME_GET_MILLISEC(duration)); \
|
|
||||||
} while (0)
|
|
||||||
#else
|
|
||||||
#define HnswBench(name, code) (code)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 150000
|
#if PG_VERSION_NUM >= 150000
|
||||||
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
|
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
|
||||||
#define SeedRandom(seed) pg_prng_seed(&pg_global_prng_state, seed)
|
#define SeedRandom(seed) pg_prng_seed(&pg_global_prng_state, seed)
|
||||||
@@ -427,11 +398,10 @@ typedef struct HnswVacuumState
|
|||||||
HnswSupport support;
|
HnswSupport support;
|
||||||
|
|
||||||
/* Variables */
|
/* Variables */
|
||||||
struct tidhash_hash *deleting;
|
struct tidhash_hash *deleted;
|
||||||
BufferAccessStrategy bas;
|
BufferAccessStrategy bas;
|
||||||
HnswNeighborTuple ntup;
|
HnswNeighborTuple ntup;
|
||||||
HnswElementData highestPoint;
|
HnswElementData highestPoint;
|
||||||
HnswElementData fallbackPoint;
|
|
||||||
|
|
||||||
/* Memory */
|
/* Memory */
|
||||||
MemoryContext tmpCtx;
|
MemoryContext tmpCtx;
|
||||||
@@ -454,7 +424,7 @@ void *HnswAlloc(HnswAllocator * allocator, Size size);
|
|||||||
HnswElement HnswInitElement(char *base, ItemPointer tid, int m, double ml, int maxLevel, HnswAllocator * alloc);
|
HnswElement HnswInitElement(char *base, ItemPointer tid, int m, double ml, int maxLevel, HnswAllocator * alloc);
|
||||||
HnswElement HnswInitElementFromBlock(BlockNumber blkno, OffsetNumber offno);
|
HnswElement HnswInitElementFromBlock(BlockNumber blkno, OffsetNumber offno);
|
||||||
void HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint, Relation index, HnswSupport * support, int m, int efConstruction, bool existing);
|
void HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint, Relation index, HnswSupport * support, int m, int efConstruction, bool existing);
|
||||||
HnswSearchCandidate *HnswEntryCandidate(char *base, HnswElement entryPoint, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec);
|
HnswSearchCandidate *HnswEntryCandidate(char *base, HnswElement em, HnswQuery * q, Relation rel, HnswSupport * support, bool loadVec);
|
||||||
void HnswUpdateMetaPage(Relation index, int updateEntry, HnswElement entryPoint, BlockNumber insertPage, ForkNumber forkNum, bool building);
|
void HnswUpdateMetaPage(Relation index, int updateEntry, HnswElement entryPoint, BlockNumber insertPage, ForkNumber forkNum, bool building);
|
||||||
void HnswSetNeighborTuple(char *base, HnswNeighborTuple ntup, HnswElement e, int m);
|
void HnswSetNeighborTuple(char *base, HnswNeighborTuple ntup, HnswElement e, int m);
|
||||||
void HnswAddHeapTid(HnswElement element, ItemPointer heaptid);
|
void HnswAddHeapTid(HnswElement element, ItemPointer heaptid);
|
||||||
|
|||||||
@@ -36,14 +36,11 @@
|
|||||||
*/
|
*/
|
||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include <limits.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/parallel.h"
|
#include "access/parallel.h"
|
||||||
#include "access/relscan.h"
|
|
||||||
#include "access/table.h"
|
#include "access/table.h"
|
||||||
#include "access/tableam.h"
|
#include "access/tableam.h"
|
||||||
#include "access/tupdesc.h"
|
|
||||||
#include "access/xact.h"
|
#include "access/xact.h"
|
||||||
#include "access/xloginsert.h"
|
#include "access/xloginsert.h"
|
||||||
#include "catalog/index.h"
|
#include "catalog/index.h"
|
||||||
@@ -51,19 +48,11 @@
|
|||||||
#include "commands/progress.h"
|
#include "commands/progress.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
#include "nodes/execnodes.h"
|
|
||||||
#include "optimizer/optimizer.h"
|
#include "optimizer/optimizer.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "storage/condition_variable.h"
|
|
||||||
#include "tcop/tcopprot.h"
|
#include "tcop/tcopprot.h"
|
||||||
#include "utils/datum.h"
|
#include "utils/datum.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
|
||||||
#include "utils/snapmgr.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 140000
|
#if PG_VERSION_NUM >= 140000
|
||||||
#include "utils/backend_progress.h"
|
#include "utils/backend_progress.h"
|
||||||
@@ -80,8 +69,6 @@
|
|||||||
#define PARALLEL_KEY_HNSW_AREA UINT64CONST(0xA000000000000002)
|
#define PARALLEL_KEY_HNSW_AREA UINT64CONST(0xA000000000000002)
|
||||||
#define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xA000000000000003)
|
#define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xA000000000000003)
|
||||||
|
|
||||||
#define HNSW_MAX_GRAPH_MEMORY (SIZE_MAX / 2)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create the metapage
|
* Create the metapage
|
||||||
*/
|
*/
|
||||||
@@ -411,7 +398,7 @@ UpdateNeighborsInMemory(char *base, HnswSupport * support, HnswElement e, int m)
|
|||||||
* Update graph in memory
|
* Update graph in memory
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
UpdateGraphInMemory(HnswSupport * support, HnswElement element, int m, HnswElement entryPoint, HnswBuildState * buildstate)
|
UpdateGraphInMemory(HnswSupport * support, HnswElement element, int m, int efConstruction, HnswElement entryPoint, HnswBuildState * buildstate)
|
||||||
{
|
{
|
||||||
HnswGraph *graph = buildstate->graph;
|
HnswGraph *graph = buildstate->graph;
|
||||||
char *base = buildstate->hnswarea;
|
char *base = buildstate->hnswarea;
|
||||||
@@ -473,7 +460,7 @@ InsertTupleInMemory(HnswBuildState * buildstate, HnswElement element)
|
|||||||
HnswFindElementNeighbors(base, element, entryPoint, NULL, support, m, efConstruction, false);
|
HnswFindElementNeighbors(base, element, entryPoint, NULL, support, m, efConstruction, false);
|
||||||
|
|
||||||
/* Update graph in memory */
|
/* Update graph in memory */
|
||||||
UpdateGraphInMemory(support, element, m, entryPoint, buildstate);
|
UpdateGraphInMemory(support, element, m, efConstruction, entryPoint, buildstate);
|
||||||
|
|
||||||
/* Release entry lock */
|
/* Release entry lock */
|
||||||
LWLockRelease(entryLock);
|
LWLockRelease(entryLock);
|
||||||
@@ -494,7 +481,6 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
|
|||||||
LWLock *flushLock = &graph->flushLock;
|
LWLock *flushLock = &graph->flushLock;
|
||||||
char *base = buildstate->hnswarea;
|
char *base = buildstate->hnswarea;
|
||||||
Datum value;
|
Datum value;
|
||||||
Size memoryMargin;
|
|
||||||
|
|
||||||
/* Form index value */
|
/* Form index value */
|
||||||
if (!HnswFormIndexValue(&value, values, isnull, buildstate->typeInfo, support))
|
if (!HnswFormIndexValue(&value, values, isnull, buildstate->typeInfo, support))
|
||||||
@@ -503,9 +489,6 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
|
|||||||
/* Get datum size */
|
/* Get datum size */
|
||||||
valueSize = VARSIZE_ANY(DatumGetPointer(value));
|
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 */
|
/* Ensure graph not flushed when inserting */
|
||||||
LWLockAcquire(flushLock, LW_SHARED);
|
LWLockAcquire(flushLock, LW_SHARED);
|
||||||
|
|
||||||
@@ -527,7 +510,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
|
|||||||
* Check that we have enough memory available for the new element now that
|
* Check that we have enough memory available for the new element now that
|
||||||
* we have the allocator lock, and flush pages if needed.
|
* we have the allocator lock, and flush pages if needed.
|
||||||
*/
|
*/
|
||||||
if (graph->memoryUsed + memoryMargin >= graph->memoryTotal)
|
if (graph->memoryUsed >= graph->memoryTotal)
|
||||||
{
|
{
|
||||||
LWLockRelease(&graph->allocatorLock);
|
LWLockRelease(&graph->allocatorLock);
|
||||||
|
|
||||||
@@ -562,7 +545,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
|
|||||||
|
|
||||||
/* Copy the datum */
|
/* Copy the datum */
|
||||||
memcpy(valuePtr, DatumGetPointer(value), valueSize);
|
memcpy(valuePtr, DatumGetPointer(value), valueSize);
|
||||||
HnswPtrStore(base, element->value, (char *) valuePtr);
|
HnswPtrStore(base, element->value, valuePtr);
|
||||||
|
|
||||||
/* Create a lock for the element */
|
/* Create a lock for the element */
|
||||||
LWLockInitialize(&element->lock, hnsw_lock_tranche_id);
|
LWLockInitialize(&element->lock, hnsw_lock_tranche_id);
|
||||||
@@ -620,7 +603,7 @@ InitGraph(HnswGraph * graph, char *base, Size memoryTotal)
|
|||||||
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;
|
||||||
graph->memoryTotal = Min(memoryTotal, HNSW_MAX_GRAPH_MEMORY);
|
graph->memoryTotal = memoryTotal;
|
||||||
graph->flushed = false;
|
graph->flushed = false;
|
||||||
graph->indtuples = 0;
|
graph->indtuples = 0;
|
||||||
SpinLockInit(&graph->lock);
|
SpinLockInit(&graph->lock);
|
||||||
@@ -661,17 +644,9 @@ static void *
|
|||||||
HnswSharedMemoryAlloc(Size size, void *state)
|
HnswSharedMemoryAlloc(Size size, void *state)
|
||||||
{
|
{
|
||||||
HnswBuildState *buildstate = (HnswBuildState *) state;
|
HnswBuildState *buildstate = (HnswBuildState *) state;
|
||||||
Size alignedSize = MAXALIGN(size);
|
void *chunk = buildstate->hnswarea + buildstate->graph->memoryUsed;
|
||||||
void *chunk;
|
|
||||||
|
|
||||||
if (alignedSize > 1024 * 1024)
|
buildstate->graph->memoryUsed += MAXALIGN(size);
|
||||||
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;
|
|
||||||
return chunk;
|
return chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -719,7 +694,7 @@ InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, Index
|
|||||||
/* Get support functions */
|
/* Get support functions */
|
||||||
HnswInitSupport(&buildstate->support, index);
|
HnswInitSupport(&buildstate->support, index);
|
||||||
|
|
||||||
InitGraph(&buildstate->graphData, NULL, maintenance_work_mem * (Size) 1024);
|
InitGraph(&buildstate->graphData, NULL, (Size) maintenance_work_mem * 1024L);
|
||||||
buildstate->graph = &buildstate->graphData;
|
buildstate->graph = &buildstate->graphData;
|
||||||
buildstate->ml = HnswGetMl(buildstate->m);
|
buildstate->ml = HnswGetMl(buildstate->m);
|
||||||
buildstate->maxLevel = HnswGetMaxLevel(buildstate->m);
|
buildstate->maxLevel = HnswGetMaxLevel(buildstate->m);
|
||||||
@@ -803,11 +778,7 @@ HnswParallelScanAndInsert(Relation heapRel, Relation indexRel, HnswShared * hnsw
|
|||||||
buildstate.hnswarea = hnswarea;
|
buildstate.hnswarea = hnswarea;
|
||||||
InitAllocator(&buildstate.allocator, &HnswSharedMemoryAlloc, &buildstate);
|
InitAllocator(&buildstate.allocator, &HnswSharedMemoryAlloc, &buildstate);
|
||||||
scan = table_beginscan_parallel(heapRel,
|
scan = table_beginscan_parallel(heapRel,
|
||||||
ParallelTableScanFromHnswShared(hnswshared)
|
ParallelTableScanFromHnswShared(hnswshared));
|
||||||
#if PG_VERSION_NUM >= 190000
|
|
||||||
,SO_NONE
|
|
||||||
#endif
|
|
||||||
);
|
|
||||||
reltuples = table_index_build_scan(heapRel, indexRel, indexInfo,
|
reltuples = table_index_build_scan(heapRel, indexRel, indexInfo,
|
||||||
true, progress, BuildCallback,
|
true, progress, BuildCallback,
|
||||||
(void *) &buildstate, scan);
|
(void *) &buildstate, scan);
|
||||||
@@ -956,13 +927,11 @@ HnswBeginParallel(HnswBuildState * buildstate, bool isconcurrent, int request)
|
|||||||
/* Leave space for other objects in shared memory */
|
/* Leave space for other objects in shared memory */
|
||||||
/* Docker has a default limit of 64 MB for shm_size */
|
/* Docker has a default limit of 64 MB for shm_size */
|
||||||
/* which happens to be the default value of maintenance_work_mem */
|
/* which happens to be the default value of maintenance_work_mem */
|
||||||
esthnswarea = maintenance_work_mem * (Size) 1024;
|
esthnswarea = maintenance_work_mem * 1024L;
|
||||||
estother = 3 * 1024 * 1024;
|
estother = 3 * 1024 * 1024;
|
||||||
if (esthnswarea > estother)
|
if (esthnswarea > estother)
|
||||||
esthnswarea -= estother;
|
esthnswarea -= estother;
|
||||||
|
|
||||||
esthnswarea = Min(esthnswarea, HNSW_MAX_GRAPH_MEMORY);
|
|
||||||
|
|
||||||
shm_toc_estimate_chunk(&pcxt->estimator, esthnswarea);
|
shm_toc_estimate_chunk(&pcxt->estimator, esthnswarea);
|
||||||
shm_toc_estimate_keys(&pcxt->estimator, 2);
|
shm_toc_estimate_keys(&pcxt->estimator, 2);
|
||||||
|
|
||||||
@@ -1005,7 +974,8 @@ HnswBeginParallel(HnswBuildState * buildstate, bool isconcurrent, int request)
|
|||||||
snapshot);
|
snapshot);
|
||||||
|
|
||||||
hnswarea = (char *) shm_toc_allocate(pcxt->toc, esthnswarea);
|
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
|
* Avoid base address for relptr for Postgres < 14.5
|
||||||
@@ -1084,7 +1054,7 @@ ComputeParallelWorkers(Relation heap, Relation index)
|
|||||||
* Build graph
|
* Build graph
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
BuildGraph(HnswBuildState * buildstate)
|
BuildGraph(HnswBuildState * buildstate, ForkNumber forkNum)
|
||||||
{
|
{
|
||||||
int parallel_workers = 0;
|
int parallel_workers = 0;
|
||||||
|
|
||||||
@@ -1132,7 +1102,7 @@ BuildIndex(Relation heap, Relation index, IndexInfo *indexInfo,
|
|||||||
|
|
||||||
InitBuildState(buildstate, heap, index, indexInfo, forkNum);
|
InitBuildState(buildstate, heap, index, indexInfo, forkNum);
|
||||||
|
|
||||||
BuildGraph(buildstate);
|
BuildGraph(buildstate, forkNum);
|
||||||
|
|
||||||
if (RelationNeedsWAL(index) || forkNum == INIT_FORKNUM)
|
if (RelationNeedsWAL(index) || forkNum == INIT_FORKNUM)
|
||||||
log_newpage_range(index, forkNum, 0, RelationGetNumberOfBlocksInFork(index, forkNum), true);
|
log_newpage_range(index, forkNum, 0, RelationGetNumberOfBlocksInFork(index, forkNum), true);
|
||||||
|
|||||||
@@ -1,19 +1,13 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include "access/genam.h"
|
#include <math.h>
|
||||||
|
|
||||||
#include "access/generic_xlog.h"
|
#include "access/generic_xlog.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "nodes/execnodes.h"
|
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "storage/lmgr.h"
|
#include "storage/lmgr.h"
|
||||||
#include "storage/lwlock.h"
|
|
||||||
#include "utils/datum.h"
|
#include "utils/datum.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get the insert page
|
* Get the insert page
|
||||||
@@ -666,7 +660,7 @@ FindDuplicateOnDisk(Relation index, HnswElement element, bool building)
|
|||||||
* Update graph on disk
|
* Update graph on disk
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
UpdateGraphOnDisk(Relation index, HnswSupport * support, HnswElement element, int m, HnswElement entryPoint, bool building)
|
UpdateGraphOnDisk(Relation index, HnswSupport * support, HnswElement element, int m, int efConstruction, HnswElement entryPoint, bool building)
|
||||||
{
|
{
|
||||||
BlockNumber newInsertPage = InvalidBlockNumber;
|
BlockNumber newInsertPage = InvalidBlockNumber;
|
||||||
|
|
||||||
@@ -714,7 +708,7 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, Datum value, ItemPo
|
|||||||
|
|
||||||
/* Create an element */
|
/* Create an element */
|
||||||
element = HnswInitElement(base, heaptid, m, HnswGetMl(m), HnswGetMaxLevel(m), NULL);
|
element = HnswInitElement(base, heaptid, m, HnswGetMl(m), HnswGetMaxLevel(m), NULL);
|
||||||
HnswPtrStore(base, element->value, (char *) DatumGetPointer(value));
|
HnswPtrStore(base, element->value, DatumGetPointer(value));
|
||||||
|
|
||||||
/* Prevent concurrent inserts when likely updating entry point */
|
/* Prevent concurrent inserts when likely updating entry point */
|
||||||
if (entryPoint == NULL || element->level > entryPoint->level)
|
if (entryPoint == NULL || element->level > entryPoint->level)
|
||||||
@@ -734,7 +728,7 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, Datum value, ItemPo
|
|||||||
HnswFindElementNeighbors(base, element, entryPoint, index, support, m, efConstruction, false);
|
HnswFindElementNeighbors(base, element, entryPoint, index, support, m, efConstruction, false);
|
||||||
|
|
||||||
/* Update graph on disk */
|
/* Update graph on disk */
|
||||||
UpdateGraphOnDisk(index, support, element, m, entryPoint, building);
|
UpdateGraphOnDisk(index, support, element, m, efConstruction, entryPoint, building);
|
||||||
|
|
||||||
/* Release lock */
|
/* Release lock */
|
||||||
UnlockPage(index, HNSW_UPDATE_LOCK, lockmode);
|
UnlockPage(index, HNSW_UPDATE_LOCK, lockmode);
|
||||||
|
|||||||
@@ -1,23 +1,12 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/relscan.h"
|
#include "access/relscan.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "lib/pairingheap.h"
|
|
||||||
#include "miscadmin.h"
|
|
||||||
#include "nodes/pg_list.h"
|
|
||||||
#include "pgstat.h"
|
#include "pgstat.h"
|
||||||
|
#include "storage/bufmgr.h"
|
||||||
#include "storage/lmgr.h"
|
#include "storage/lmgr.h"
|
||||||
#include "utils/float.h"
|
#include "utils/float.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/relcache.h"
|
|
||||||
#include "utils/snapmgr.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Algorithm 5 from paper
|
* Algorithm 5 from paper
|
||||||
@@ -153,7 +142,7 @@ hnswbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
/* Calculate max memory */
|
/* Calculate max memory */
|
||||||
/* Add 256 extra bytes to fill last block when close */
|
/* Add 256 extra bytes to fill last block when close */
|
||||||
maxMemory = (double) work_mem * hnsw_scan_mem_multiplier * 1024.0 + 256;
|
maxMemory = (double) work_mem * hnsw_scan_mem_multiplier * 1024.0 + 256;
|
||||||
so->maxMemory = Min(maxMemory, (double) (SIZE_MAX / 2));
|
so->maxMemory = Min(maxMemory, (double) SIZE_MAX);
|
||||||
|
|
||||||
scan->opaque = so;
|
scan->opaque = so;
|
||||||
|
|
||||||
@@ -204,10 +193,6 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
|
|
||||||
/* Count index scan for stats */
|
/* Count index scan for stats */
|
||||||
pgstat_count_index_scan(scan->indexRelation);
|
pgstat_count_index_scan(scan->indexRelation);
|
||||||
#if PG_VERSION_NUM >= 180000
|
|
||||||
if (scan->instrument)
|
|
||||||
scan->instrument->nsearches++;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Safety check */
|
/* Safety check */
|
||||||
if (scan->orderByData == NULL)
|
if (scan->orderByData == NULL)
|
||||||
|
|||||||
@@ -2,24 +2,18 @@
|
|||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/generic_xlog.h"
|
#include "access/generic_xlog.h"
|
||||||
|
#include "catalog/pg_type.h"
|
||||||
|
#include "catalog/pg_type_d.h"
|
||||||
#include "common/hashfn.h"
|
#include "common/hashfn.h"
|
||||||
#include "fmgr.h"
|
#include "fmgr.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "lib/pairingheap.h"
|
#include "lib/pairingheap.h"
|
||||||
#include "nodes/pg_list.h"
|
|
||||||
#include "port/atomics.h"
|
|
||||||
#include "sparsevec.h"
|
#include "sparsevec.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "utils/datum.h"
|
#include "utils/datum.h"
|
||||||
#include "utils/memdebug.h"
|
#include "utils/memdebug.h"
|
||||||
#include "utils/rel.h"
|
#include "utils/rel.h"
|
||||||
#include "vector.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM < 170000
|
#if PG_VERSION_NUM < 170000
|
||||||
static inline uint64
|
static inline uint64
|
||||||
@@ -262,7 +256,7 @@ HnswInitElement(char *base, ItemPointer heaptid, int m, double ml, int maxLevel,
|
|||||||
|
|
||||||
HnswInitNeighbors(base, element, m, allocator);
|
HnswInitNeighbors(base, element, m, allocator);
|
||||||
|
|
||||||
HnswPtrStore(base, element->value, (char *) NULL);
|
HnswPtrStore(base, element->value, (Pointer) NULL);
|
||||||
|
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
@@ -288,7 +282,7 @@ HnswInitElementFromBlock(BlockNumber blkno, OffsetNumber offno)
|
|||||||
element->blkno = blkno;
|
element->blkno = blkno;
|
||||||
element->offno = offno;
|
element->offno = offno;
|
||||||
HnswPtrStore(base, element->neighbors, (HnswNeighborArrayPtr *) NULL);
|
HnswPtrStore(base, element->neighbors, (HnswNeighborArrayPtr *) NULL);
|
||||||
HnswPtrStore(base, element->value, (char *) NULL);
|
HnswPtrStore(base, element->value, (Pointer) NULL);
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -514,7 +508,7 @@ HnswLoadElementFromTuple(HnswElement element, HnswElementTuple etup, bool loadHe
|
|||||||
char *base = NULL;
|
char *base = NULL;
|
||||||
Datum value = datumCopy(PointerGetDatum(&etup->data), false, -1);
|
Datum value = datumCopy(PointerGetDatum(&etup->data), false, -1);
|
||||||
|
|
||||||
HnswPtrStore(base, element->value, (char *) DatumGetPointer(value));
|
HnswPtrStore(base, element->value, DatumGetPointer(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,9 +540,6 @@ HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, Hns
|
|||||||
|
|
||||||
Assert(HnswIsElementTuple(etup));
|
Assert(HnswIsElementTuple(etup));
|
||||||
|
|
||||||
if (unlikely(etup->deleted))
|
|
||||||
elog(ERROR, "cannot load deleted element");
|
|
||||||
|
|
||||||
/* Calculate distance */
|
/* Calculate distance */
|
||||||
if (distance != NULL)
|
if (distance != NULL)
|
||||||
{
|
{
|
||||||
@@ -931,7 +922,7 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(eDistance < f->distance || alwaysAdd))
|
if (eElement == NULL || !(eDistance < f->distance || alwaysAdd))
|
||||||
{
|
{
|
||||||
if (discarded != NULL)
|
if (discarded != NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
218
src/hnswvacuum.c
218
src/hnswvacuum.c
@@ -1,30 +1,25 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include "access/genam.h"
|
#include <math.h>
|
||||||
|
|
||||||
#include "access/generic_xlog.h"
|
#include "access/generic_xlog.h"
|
||||||
#include "commands/vacuum.h"
|
#include "commands/vacuum.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "nodes/pg_list.h"
|
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "storage/lmgr.h"
|
#include "storage/lmgr.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 180000
|
#if PG_VERSION_NUM >= 180000
|
||||||
#define vacuum_delay_point() vacuum_delay_point(false)
|
#define vacuum_delay_point() vacuum_delay_point(false)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check if deletion list contains an element
|
* Check if deleted list contains an index TID
|
||||||
*/
|
*/
|
||||||
static bool
|
static bool
|
||||||
DeletingElement(tidhash_hash * deleting, ItemPointer indextid)
|
DeletedContains(tidhash_hash * deleted, ItemPointer indextid)
|
||||||
{
|
{
|
||||||
return tidhash_lookup(deleting, *indextid) != NULL;
|
return tidhash_lookup(deleted, *indextid) != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -37,20 +32,17 @@ RemoveHeapTids(HnswVacuumState * vacuumstate)
|
|||||||
{
|
{
|
||||||
BlockNumber blkno = HNSW_HEAD_BLKNO;
|
BlockNumber blkno = HNSW_HEAD_BLKNO;
|
||||||
HnswElement highestPoint = &vacuumstate->highestPoint;
|
HnswElement highestPoint = &vacuumstate->highestPoint;
|
||||||
HnswElement fallbackPoint = &vacuumstate->fallbackPoint;
|
|
||||||
Relation index = vacuumstate->index;
|
Relation index = vacuumstate->index;
|
||||||
BufferAccessStrategy bas = vacuumstate->bas;
|
BufferAccessStrategy bas = vacuumstate->bas;
|
||||||
|
HnswElement entryPoint = HnswGetEntryPoint(vacuumstate->index);
|
||||||
IndexBulkDeleteResult *stats = vacuumstate->stats;
|
IndexBulkDeleteResult *stats = vacuumstate->stats;
|
||||||
|
|
||||||
/* Store separately since HnswElement level is uint8 */
|
/* Store separately since highestPoint.level is uint8 */
|
||||||
int highestLevel = -1;
|
int highestLevel = -1;
|
||||||
int fallbackLevel = -1;
|
|
||||||
|
|
||||||
/* Initialize highest point and fallback point */
|
/* Initialize highest point */
|
||||||
highestPoint->blkno = InvalidBlockNumber;
|
highestPoint->blkno = InvalidBlockNumber;
|
||||||
highestPoint->offno = InvalidOffsetNumber;
|
highestPoint->offno = InvalidOffsetNumber;
|
||||||
fallbackPoint->blkno = InvalidBlockNumber;
|
|
||||||
fallbackPoint->offno = InvalidOffsetNumber;
|
|
||||||
|
|
||||||
while (BlockNumberIsValid(blkno))
|
while (BlockNumberIsValid(blkno))
|
||||||
{
|
{
|
||||||
@@ -80,14 +72,6 @@ RemoveHeapTids(HnswVacuumState * vacuumstate)
|
|||||||
if (!HnswIsElementTuple(etup))
|
if (!HnswIsElementTuple(etup))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/*
|
|
||||||
* Skip deleted tuples. It is important they are not added to the
|
|
||||||
* deletion list to avoid false positives in NeedsUpdated and
|
|
||||||
* ConfirmRepaired.
|
|
||||||
*/
|
|
||||||
if (etup->deleted)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (ItemPointerIsValid(&etup->heaptids[0]))
|
if (ItemPointerIsValid(&etup->heaptids[0]))
|
||||||
{
|
{
|
||||||
for (int i = 0; i < HNSW_HEAPTIDS; i++)
|
for (int i = 0; i < HNSW_HEAPTIDS; i++)
|
||||||
@@ -121,40 +105,23 @@ RemoveHeapTids(HnswVacuumState * vacuumstate)
|
|||||||
|
|
||||||
if (!ItemPointerIsValid(&etup->heaptids[0]))
|
if (!ItemPointerIsValid(&etup->heaptids[0]))
|
||||||
{
|
{
|
||||||
ItemPointerData indextid;
|
ItemPointerData ip;
|
||||||
bool found;
|
bool found;
|
||||||
|
|
||||||
/* Add to deletion list */
|
/* Add to deleted list */
|
||||||
ItemPointerSet(&indextid, blkno, offno);
|
ItemPointerSet(&ip, blkno, offno);
|
||||||
|
|
||||||
tidhash_insert(vacuumstate->deleting, indextid, &found);
|
tidhash_insert(vacuumstate->deleted, ip, &found);
|
||||||
Assert(!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))
|
/* Keep track of highest non-entry point */
|
||||||
{
|
|
||||||
/* Current highest point becomes fallback */
|
|
||||||
fallbackPoint->blkno = highestPoint->blkno;
|
|
||||||
fallbackPoint->offno = highestPoint->offno;
|
|
||||||
fallbackPoint->level = highestPoint->level;
|
|
||||||
fallbackLevel = highestLevel;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Keep track of highest point */
|
|
||||||
highestPoint->blkno = blkno;
|
highestPoint->blkno = blkno;
|
||||||
highestPoint->offno = offno;
|
highestPoint->offno = offno;
|
||||||
highestPoint->level = etup->level;
|
highestPoint->level = etup->level;
|
||||||
highestLevel = 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;
|
blkno = HnswPageGetOpaque(page)->nextblkno;
|
||||||
@@ -166,10 +133,6 @@ RemoveHeapTids(HnswVacuumState * vacuumstate)
|
|||||||
|
|
||||||
UnlockReleaseBuffer(buf);
|
UnlockReleaseBuffer(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HNSW_MEMORY
|
|
||||||
elog(INFO, "memory: %zu KB", MemoryContextMemAllocated(CurrentMemoryContext, true) / 1024);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -200,8 +163,8 @@ NeedsUpdated(HnswVacuumState * vacuumstate, HnswElement element)
|
|||||||
if (!ItemPointerIsValid(indextid))
|
if (!ItemPointerIsValid(indextid))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* Check if in deletion list */
|
/* Check if in deleted list */
|
||||||
if (DeletingElement(vacuumstate->deleting, indextid))
|
if (DeletedContains(vacuumstate->deleted, indextid))
|
||||||
{
|
{
|
||||||
needsUpdated = true;
|
needsUpdated = true;
|
||||||
break;
|
break;
|
||||||
@@ -210,8 +173,7 @@ NeedsUpdated(HnswVacuumState * vacuumstate, HnswElement element)
|
|||||||
|
|
||||||
/* Also update if layer 0 is not full */
|
/* Also update if layer 0 is not full */
|
||||||
/* This could indicate too many candidates being deleted during insert */
|
/* This could indicate too many candidates being deleted during insert */
|
||||||
/* There should always be more than zero indextids, but check for safety */
|
if (!needsUpdated)
|
||||||
if (!needsUpdated && ntup->count > 0)
|
|
||||||
needsUpdated = !ItemPointerIsValid(&ntup->indextids[ntup->count - 1]);
|
needsUpdated = !ItemPointerIsValid(&ntup->indextids[ntup->count - 1]);
|
||||||
|
|
||||||
UnlockReleaseBuffer(buf);
|
UnlockReleaseBuffer(buf);
|
||||||
@@ -297,27 +259,12 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate)
|
|||||||
/* Get a shared lock */
|
/* Get a shared lock */
|
||||||
LockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
LockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
||||||
|
|
||||||
/* Get latest entry point */
|
|
||||||
entryPoint = HnswGetEntryPoint(index);
|
|
||||||
|
|
||||||
/* 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 */
|
/* Load element */
|
||||||
HnswLoadElement(highestPoint, NULL, NULL, index, support, true, NULL);
|
HnswLoadElement(highestPoint, NULL, NULL, index, support, true, NULL);
|
||||||
|
|
||||||
/* Repair if needed */
|
/* Repair if needed */
|
||||||
if (NeedsUpdated(vacuumstate, highestPoint))
|
if (NeedsUpdated(vacuumstate, highestPoint))
|
||||||
RepairGraphElement(vacuumstate, highestPoint, entryPoint);
|
RepairGraphElement(vacuumstate, highestPoint, HnswGetEntryPoint(index));
|
||||||
}
|
|
||||||
|
|
||||||
/* Release lock */
|
/* Release lock */
|
||||||
UnlockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
UnlockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
||||||
@@ -335,7 +282,7 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate)
|
|||||||
|
|
||||||
ItemPointerSet(&epData, entryPoint->blkno, entryPoint->offno);
|
ItemPointerSet(&epData, entryPoint->blkno, entryPoint->offno);
|
||||||
|
|
||||||
if (DeletingElement(vacuumstate->deleting, &epData))
|
if (DeletedContains(vacuumstate->deleted, &epData))
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Replace the entry point with the highest point. If highest
|
* Replace the entry point with the highest point. If highest
|
||||||
@@ -421,10 +368,6 @@ RepairGraph(HnswVacuumState * vacuumstate)
|
|||||||
if (!HnswIsElementTuple(etup))
|
if (!HnswIsElementTuple(etup))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* Skip deleted tuples */
|
|
||||||
if (etup->deleted)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
/* Skip updating neighbors if being deleted */
|
/* Skip updating neighbors if being deleted */
|
||||||
if (!ItemPointerIsValid(&etup->heaptids[0]))
|
if (!ItemPointerIsValid(&etup->heaptids[0]))
|
||||||
continue;
|
continue;
|
||||||
@@ -488,103 +431,6 @@ RepairGraph(HnswVacuumState * vacuumstate)
|
|||||||
/* Reset memory context */
|
/* Reset memory context */
|
||||||
MemoryContextSwitchTo(oldCtx);
|
MemoryContextSwitchTo(oldCtx);
|
||||||
MemoryContextReset(vacuumstate->tmpCtx);
|
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 deleted tuples */
|
|
||||||
if (etup->deleted)
|
|
||||||
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 deletion list */
|
|
||||||
if (DeletingElement(vacuumstate->deleting, indextid))
|
|
||||||
elog(ERROR, "hnsw graph not repaired");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nbuf != buf)
|
|
||||||
UnlockReleaseBuffer(nbuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
blkno = HnswPageGetOpaque(page)->nextblkno;
|
|
||||||
|
|
||||||
UnlockReleaseBuffer(buf);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -600,15 +446,10 @@ MarkDeleted(HnswVacuumState * vacuumstate)
|
|||||||
BufferAccessStrategy bas = vacuumstate->bas;
|
BufferAccessStrategy bas = vacuumstate->bas;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Wait for inserts and index scans to complete. Inserts and scans before
|
* Wait for index scans to complete. Scans before this point may contain
|
||||||
* this point may visit tuples about to be deleted. Inserts and scans
|
* tuples about to be deleted. Scans after this point will not, since the
|
||||||
* after this point will not, since the graph has been repaired.
|
* graph has been repaired.
|
||||||
*/
|
*/
|
||||||
LockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock);
|
|
||||||
UnlockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock);
|
|
||||||
|
|
||||||
ConfirmRepaired(vacuumstate);
|
|
||||||
|
|
||||||
LockPage(index, HNSW_SCAN_LOCK, ExclusiveLock);
|
LockPage(index, HNSW_SCAN_LOCK, ExclusiveLock);
|
||||||
UnlockPage(index, HNSW_SCAN_LOCK, ExclusiveLock);
|
UnlockPage(index, HNSW_SCAN_LOCK, ExclusiveLock);
|
||||||
|
|
||||||
@@ -683,9 +524,8 @@ MarkDeleted(HnswVacuumState * vacuumstate)
|
|||||||
ntup = (HnswNeighborTuple) PageGetItem(npage, PageGetItemId(npage, neighborOffno));
|
ntup = (HnswNeighborTuple) PageGetItem(npage, PageGetItemId(npage, neighborOffno));
|
||||||
|
|
||||||
/* Overwrite element */
|
/* Overwrite element */
|
||||||
/* Use memset instead of MemSet to keep clang-tidy happy */
|
|
||||||
etup->deleted = 1;
|
etup->deleted = 1;
|
||||||
memset(&etup->data, 0, VARSIZE_ANY(&etup->data));
|
MemSet(&etup->data, 0, VARSIZE_ANY(&etup->data));
|
||||||
|
|
||||||
/* Overwrite neighbors */
|
/* Overwrite neighbors */
|
||||||
for (int i = 0; i < ntup->count; i++)
|
for (int i = 0; i < ntup->count; i++)
|
||||||
@@ -756,7 +596,7 @@ InitVacuumState(HnswVacuumState * vacuumstate, IndexVacuumInfo *info, IndexBulkD
|
|||||||
HnswGetMetaPageInfo(index, &vacuumstate->m, NULL);
|
HnswGetMetaPageInfo(index, &vacuumstate->m, NULL);
|
||||||
|
|
||||||
/* Create hash table */
|
/* Create hash table */
|
||||||
vacuumstate->deleting = tidhash_create(CurrentMemoryContext, 256, NULL);
|
vacuumstate->deleted = tidhash_create(CurrentMemoryContext, 256, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -765,7 +605,7 @@ InitVacuumState(HnswVacuumState * vacuumstate, IndexVacuumInfo *info, IndexBulkD
|
|||||||
static void
|
static void
|
||||||
FreeVacuumState(HnswVacuumState * vacuumstate)
|
FreeVacuumState(HnswVacuumState * vacuumstate)
|
||||||
{
|
{
|
||||||
tidhash_destroy(vacuumstate->deleting);
|
tidhash_destroy(vacuumstate->deleted);
|
||||||
FreeAccessStrategy(vacuumstate->bas);
|
FreeAccessStrategy(vacuumstate->bas);
|
||||||
pfree(vacuumstate->ntup);
|
pfree(vacuumstate->ntup);
|
||||||
MemoryContextDelete(vacuumstate->tmpCtx);
|
MemoryContextDelete(vacuumstate->tmpCtx);
|
||||||
@@ -783,13 +623,13 @@ hnswbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
|
|||||||
InitVacuumState(&vacuumstate, info, stats, callback, callback_state);
|
InitVacuumState(&vacuumstate, info, stats, callback, callback_state);
|
||||||
|
|
||||||
/* Pass 1: Remove heap TIDs */
|
/* Pass 1: Remove heap TIDs */
|
||||||
HnswBench("RemoveHeapTids", RemoveHeapTids(&vacuumstate));
|
RemoveHeapTids(&vacuumstate);
|
||||||
|
|
||||||
/* Pass 2: Repair graph */
|
/* Pass 2: Repair graph */
|
||||||
HnswBench("RepairGraph", RepairGraph(&vacuumstate));
|
RepairGraph(&vacuumstate);
|
||||||
|
|
||||||
/* Passes 3 and 4: Confirm repaired and mark as deleted */
|
/* Pass 3: Mark as deleted */
|
||||||
HnswBench("MarkDeleted", MarkDeleted(&vacuumstate));
|
MarkDeleted(&vacuumstate);
|
||||||
|
|
||||||
FreeVacuumState(&vacuumstate);
|
FreeVacuumState(&vacuumstate);
|
||||||
|
|
||||||
|
|||||||
@@ -2,37 +2,23 @@
|
|||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/generic_xlog.h"
|
|
||||||
#include "access/itup.h"
|
|
||||||
#include "access/relscan.h"
|
|
||||||
#include "access/table.h"
|
#include "access/table.h"
|
||||||
#include "access/tableam.h"
|
#include "access/tableam.h"
|
||||||
#include "access/tupdesc.h"
|
|
||||||
#include "access/parallel.h"
|
#include "access/parallel.h"
|
||||||
#include "access/xact.h"
|
#include "access/xact.h"
|
||||||
#include "access/xloginsert.h"
|
#include "bitvec.h"
|
||||||
#include "catalog/index.h"
|
#include "catalog/index.h"
|
||||||
#include "catalog/pg_operator_d.h"
|
#include "catalog/pg_operator_d.h"
|
||||||
#include "catalog/pg_type_d.h"
|
#include "catalog/pg_type_d.h"
|
||||||
#include "commands/progress.h"
|
#include "commands/progress.h"
|
||||||
#include "fmgr.h"
|
#include "halfvec.h"
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
#include "nodes/execnodes.h"
|
|
||||||
#include "optimizer/optimizer.h"
|
#include "optimizer/optimizer.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "storage/condition_variable.h"
|
|
||||||
#include "tcop/tcopprot.h"
|
#include "tcop/tcopprot.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
#include "vector.h"
|
||||||
#include "utils/sampling.h"
|
|
||||||
#include "utils/snapmgr.h"
|
|
||||||
#include "utils/tuplesort.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 140000
|
#if PG_VERSION_NUM >= 140000
|
||||||
#include "utils/backend_progress.h"
|
#include "utils/backend_progress.h"
|
||||||
@@ -63,13 +49,15 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check with KMEANS_NORM_PROC that the value can be normalized since
|
* Normalize with KMEANS_NORM_PROC since spherical distance function
|
||||||
* spherical distance function expects unit vectors
|
* expects unit vectors
|
||||||
*/
|
*/
|
||||||
if (buildstate->kmeansnormprocinfo != NULL)
|
if (buildstate->kmeansnormprocinfo != NULL)
|
||||||
{
|
{
|
||||||
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
value = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (samples->length < targsamples)
|
if (samples->length < targsamples)
|
||||||
@@ -80,7 +68,7 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (buildstate->rowstoskip < 0)
|
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)
|
if (buildstate->rowstoskip <= 0)
|
||||||
{
|
{
|
||||||
@@ -96,9 +84,6 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
|
|
||||||
buildstate->rowstoskip -= 1;
|
buildstate->rowstoskip -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Increment after reservoir_get_next_S */
|
|
||||||
buildstate->samplerows += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -135,7 +120,6 @@ SampleRows(IvfflatBuildState * buildstate)
|
|||||||
int targsamples = buildstate->samples->maxlen;
|
int targsamples = buildstate->samples->maxlen;
|
||||||
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
||||||
|
|
||||||
buildstate->samplerows = 0;
|
|
||||||
buildstate->rowstoskip = -1;
|
buildstate->rowstoskip = -1;
|
||||||
|
|
||||||
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt());
|
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt());
|
||||||
@@ -145,21 +129,16 @@ SampleRows(IvfflatBuildState * buildstate)
|
|||||||
{
|
{
|
||||||
BlockNumber targblock = BlockSampler_Next(&buildstate->bs);
|
BlockNumber targblock = BlockSampler_Next(&buildstate->bs);
|
||||||
|
|
||||||
/* Set anyvisible to false like table_index_build_scan */
|
|
||||||
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
||||||
false, false, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Normalize if needed */
|
|
||||||
if (buildstate->kmeansnormprocinfo != NULL)
|
|
||||||
IvfflatNormVectors(buildstate->typeInfo, buildstate->collation, buildstate->samples, buildstate->tmpCtx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Add tuple to sort
|
* Add tuple to sort
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
AddTupleToSort(ItemPointer tid, Datum *values, IvfflatBuildState * buildstate)
|
AddTupleToSort(Relation index, ItemPointer tid, Datum *values, IvfflatBuildState * buildstate)
|
||||||
{
|
{
|
||||||
double distance;
|
double distance;
|
||||||
double minDistance = DBL_MAX;
|
double minDistance = DBL_MAX;
|
||||||
@@ -236,7 +215,7 @@ BuildCallback(Relation index, ItemPointer tid, Datum *values,
|
|||||||
oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
|
oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
|
||||||
|
|
||||||
/* Add tuple to sort */
|
/* Add tuple to sort */
|
||||||
AddTupleToSort(tid, values, buildstate);
|
AddTupleToSort(index, tid, values, buildstate);
|
||||||
|
|
||||||
/* Reset memory context */
|
/* Reset memory context */
|
||||||
MemoryContextSwitchTo(oldCtx);
|
MemoryContextSwitchTo(oldCtx);
|
||||||
@@ -382,20 +361,10 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In
|
|||||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
||||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
||||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 3, "vector", TupleDescAttr(buildstate->tupdesc, 0)->atttypid, -1, 0);
|
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 3, "vector", TupleDescAttr(buildstate->tupdesc, 0)->atttypid, -1, 0);
|
||||||
#if PG_VERSION_NUM >= 190000
|
|
||||||
TupleDescFinalize(buildstate->sortdesc);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
buildstate->slot = MakeSingleTupleTableSlot(buildstate->sortdesc, &TTSOpsVirtual);
|
buildstate->slot = MakeSingleTupleTableSlot(buildstate->sortdesc, &TTSOpsVirtual);
|
||||||
|
|
||||||
buildstate->memoryUsed = 0;
|
buildstate->centers = VectorArrayInit(buildstate->lists, buildstate->dimensions, buildstate->typeInfo->itemSize(buildstate->dimensions));
|
||||||
buildstate->itemsize = buildstate->typeInfo->itemSize(buildstate->dimensions);
|
|
||||||
|
|
||||||
buildstate->memoryUsed += VECTOR_ARRAY_SIZE(buildstate->lists, buildstate->itemsize);
|
|
||||||
IvfflatCheckMemoryUsage(buildstate->memoryUsed);
|
|
||||||
buildstate->centers = VectorArrayInit(buildstate->lists, buildstate->dimensions, buildstate->itemsize);
|
|
||||||
|
|
||||||
/* TODO Move allocation to page creation */
|
|
||||||
buildstate->listInfo = palloc(sizeof(ListInfo) * buildstate->lists);
|
buildstate->listInfo = palloc(sizeof(ListInfo) * buildstate->lists);
|
||||||
|
|
||||||
buildstate->tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
|
buildstate->tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
|
||||||
@@ -438,30 +407,22 @@ ComputeCenters(IvfflatBuildState * buildstate)
|
|||||||
|
|
||||||
pgstat_progress_update_param(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_IVFFLAT_PHASE_KMEANS);
|
pgstat_progress_update_param(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_IVFFLAT_PHASE_KMEANS);
|
||||||
|
|
||||||
/* Skip samples for unlogged table */
|
|
||||||
if (buildstate->heap == NULL)
|
|
||||||
numSamples = 1;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int64 maxTuples = (int64) RelationGetNumberOfBlocks(buildstate->heap) * MaxHeapTuplesPerPage;
|
|
||||||
|
|
||||||
/* Target 50 samples per list, with at least 10000 samples */
|
/* Target 50 samples per list, with at least 10000 samples */
|
||||||
/* The number of samples has a large effect on index build time */
|
/* The number of samples has a large effect on index build time */
|
||||||
numSamples = buildstate->lists * 50;
|
numSamples = buildstate->lists * 50;
|
||||||
if (numSamples < 10000)
|
if (numSamples < 10000)
|
||||||
numSamples = 10000;
|
numSamples = 10000;
|
||||||
|
|
||||||
/* Save memory since will not have more than max tuples */
|
/* Skip samples for unlogged table */
|
||||||
numSamples = Max(Min(numSamples, maxTuples), 1);
|
if (buildstate->heap == NULL)
|
||||||
}
|
numSamples = 1;
|
||||||
|
|
||||||
/* Sample rows */
|
/* Sample rows */
|
||||||
buildstate->memoryUsed += VECTOR_ARRAY_SIZE(numSamples, buildstate->itemsize);
|
/* TODO Ensure within maintenance_work_mem */
|
||||||
IvfflatCheckMemoryUsage(buildstate->memoryUsed);
|
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions, buildstate->centers->itemsize);
|
||||||
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions, buildstate->itemsize);
|
|
||||||
if (buildstate->heap != NULL)
|
if (buildstate->heap != NULL)
|
||||||
{
|
{
|
||||||
IvfflatBench("sample rows", SampleRows(buildstate));
|
SampleRows(buildstate);
|
||||||
|
|
||||||
if (buildstate->samples->length < buildstate->lists)
|
if (buildstate->samples->length < buildstate->lists)
|
||||||
{
|
{
|
||||||
@@ -473,7 +434,7 @@ ComputeCenters(IvfflatBuildState * buildstate)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Calculate centers */
|
/* Calculate centers */
|
||||||
IvfflatBench("k-means", IvfflatKmeans(buildstate->index, buildstate->samples, buildstate->centers, buildstate->typeInfo, buildstate->memoryUsed));
|
IvfflatBench("k-means", IvfflatKmeans(buildstate->index, buildstate->samples, buildstate->centers, buildstate->typeInfo));
|
||||||
|
|
||||||
/* Free samples before we allocate more memory */
|
/* Free samples before we allocate more memory */
|
||||||
VectorArrayFree(buildstate->samples);
|
VectorArrayFree(buildstate->samples);
|
||||||
@@ -509,8 +470,8 @@ CreateMetaPage(Relation index, int dimensions, int lists, ForkNumber forkNum)
|
|||||||
* Create list pages
|
* Create list pages
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
CreateListPages(Relation index, VectorArray centers, int lists,
|
CreateListPages(Relation index, VectorArray centers, int dimensions,
|
||||||
ForkNumber forkNum, ListInfo * *listInfo)
|
int lists, ForkNumber forkNum, ListInfo * *listInfo)
|
||||||
{
|
{
|
||||||
Buffer buf;
|
Buffer buf;
|
||||||
Page page;
|
Page page;
|
||||||
@@ -676,11 +637,7 @@ IvfflatParallelScanAndSort(IvfflatSpool * ivfspool, IvfflatShared * ivfshared, S
|
|||||||
ivfspool->sortstate = InitBuildSortState(buildstate.sortdesc, sortmem, coordinate);
|
ivfspool->sortstate = InitBuildSortState(buildstate.sortdesc, sortmem, coordinate);
|
||||||
buildstate.sortstate = ivfspool->sortstate;
|
buildstate.sortstate = ivfspool->sortstate;
|
||||||
scan = table_beginscan_parallel(ivfspool->heap,
|
scan = table_beginscan_parallel(ivfspool->heap,
|
||||||
ParallelTableScanFromIvfflatShared(ivfshared)
|
ParallelTableScanFromIvfflatShared(ivfshared));
|
||||||
#if PG_VERSION_NUM >= 190000
|
|
||||||
,SO_NONE
|
|
||||||
#endif
|
|
||||||
);
|
|
||||||
reltuples = table_index_build_scan(ivfspool->heap, ivfspool->index, indexInfo,
|
reltuples = table_index_build_scan(ivfspool->heap, ivfspool->index, indexInfo,
|
||||||
true, progress, BuildCallback,
|
true, progress, BuildCallback,
|
||||||
(void *) &buildstate, scan);
|
(void *) &buildstate, scan);
|
||||||
@@ -1047,7 +1004,7 @@ BuildIndex(Relation heap, Relation index, IndexInfo *indexInfo,
|
|||||||
|
|
||||||
/* Create pages */
|
/* Create pages */
|
||||||
CreateMetaPage(index, buildstate->dimensions, buildstate->lists, forkNum);
|
CreateMetaPage(index, buildstate->dimensions, buildstate->lists, forkNum);
|
||||||
CreateListPages(index, buildstate->centers, buildstate->lists, forkNum, &buildstate->listInfo);
|
CreateListPages(index, buildstate->centers, buildstate->dimensions, buildstate->lists, forkNum, &buildstate->listInfo);
|
||||||
CreateEntryPages(buildstate, forkNum);
|
CreateEntryPages(buildstate, forkNum);
|
||||||
|
|
||||||
/* Write WAL for initialization fork since GenericXLog functions do not */
|
/* Write WAL for initialization fork since GenericXLog functions do not */
|
||||||
@@ -1066,10 +1023,6 @@ ivfflatbuild(Relation heap, Relation index, IndexInfo *indexInfo)
|
|||||||
IndexBuildResult *result;
|
IndexBuildResult *result;
|
||||||
IvfflatBuildState buildstate;
|
IvfflatBuildState buildstate;
|
||||||
|
|
||||||
#ifdef IVFFLAT_BENCH
|
|
||||||
SeedRandom(42);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
BuildIndex(heap, index, indexInfo, &buildstate, MAIN_FORKNUM);
|
BuildIndex(heap, index, indexInfo, &buildstate, MAIN_FORKNUM);
|
||||||
|
|
||||||
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
|
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
|
||||||
|
|||||||
@@ -3,19 +3,14 @@
|
|||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
|
||||||
#include "access/amapi.h"
|
#include "access/amapi.h"
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/reloptions.h"
|
#include "access/reloptions.h"
|
||||||
#include "commands/progress.h"
|
#include "commands/progress.h"
|
||||||
#include "commands/vacuum.h"
|
#include "commands/vacuum.h"
|
||||||
#include "fmgr.h"
|
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "nodes/pg_list.h"
|
|
||||||
#include "utils/float.h"
|
#include "utils/float.h"
|
||||||
#include "utils/guc.h"
|
#include "utils/guc.h"
|
||||||
#include "utils/relcache.h"
|
|
||||||
#include "utils/selfuncs.h"
|
#include "utils/selfuncs.h"
|
||||||
#include "utils/spccache.h"
|
#include "utils/spccache.h"
|
||||||
#include "vector.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM < 150000
|
#if PG_VERSION_NUM < 150000
|
||||||
#define MarkGUCPrefixReserved(x) EmitWarningsOnPlaceholders(x)
|
#define MarkGUCPrefixReserved(x) EmitWarningsOnPlaceholders(x)
|
||||||
@@ -97,7 +92,7 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
|||||||
Relation index;
|
Relation index;
|
||||||
|
|
||||||
/* Never use index without order */
|
/* Never use index without order */
|
||||||
if (path->indexorderbys == NIL)
|
if (path->indexorderbys == NULL)
|
||||||
{
|
{
|
||||||
*indexStartupCost = get_float8_infinity();
|
*indexStartupCost = get_float8_infinity();
|
||||||
*indexTotalCost = get_float8_infinity();
|
*indexTotalCost = get_float8_infinity();
|
||||||
@@ -184,64 +179,6 @@ FUNCTION_PREFIX PG_FUNCTION_INFO_V1(ivfflathandler);
|
|||||||
Datum
|
Datum
|
||||||
ivfflathandler(PG_FUNCTION_ARGS)
|
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);
|
IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
|
||||||
|
|
||||||
amroutine->amstrategies = 0;
|
amroutine->amstrategies = 0;
|
||||||
@@ -249,11 +186,6 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->amoptsprocnum = 0;
|
amroutine->amoptsprocnum = 0;
|
||||||
amroutine->amcanorder = false;
|
amroutine->amcanorder = false;
|
||||||
amroutine->amcanorderbyop = true;
|
amroutine->amcanorderbyop = true;
|
||||||
#if PG_VERSION_NUM >= 180000
|
|
||||||
amroutine->amcanhash = false;
|
|
||||||
amroutine->amconsistentequality = false;
|
|
||||||
amroutine->amconsistentordering = false;
|
|
||||||
#endif
|
|
||||||
amroutine->amcanbackward = false; /* can change direction mid-scan */
|
amroutine->amcanbackward = false; /* can change direction mid-scan */
|
||||||
amroutine->amcanunique = false;
|
amroutine->amcanunique = false;
|
||||||
amroutine->amcanmulticol = false;
|
amroutine->amcanmulticol = false;
|
||||||
@@ -286,9 +218,6 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->amvacuumcleanup = ivfflatvacuumcleanup;
|
amroutine->amvacuumcleanup = ivfflatvacuumcleanup;
|
||||||
amroutine->amcanreturn = NULL; /* tuple not included in heapsort */
|
amroutine->amcanreturn = NULL; /* tuple not included in heapsort */
|
||||||
amroutine->amcostestimate = ivfflatcostestimate;
|
amroutine->amcostestimate = ivfflatcostestimate;
|
||||||
#if PG_VERSION_NUM >= 180000
|
|
||||||
amroutine->amgettreeheight = NULL;
|
|
||||||
#endif
|
|
||||||
amroutine->amoptions = ivfflatoptions;
|
amroutine->amoptions = ivfflatoptions;
|
||||||
amroutine->amproperty = NULL; /* TODO AMPROP_DISTANCE_ORDERABLE */
|
amroutine->amproperty = NULL; /* TODO AMPROP_DISTANCE_ORDERABLE */
|
||||||
amroutine->ambuildphasename = ivfflatbuildphasename;
|
amroutine->ambuildphasename = ivfflatbuildphasename;
|
||||||
@@ -309,11 +238,5 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->aminitparallelscan = NULL;
|
amroutine->aminitparallelscan = NULL;
|
||||||
amroutine->amparallelrescan = NULL;
|
amroutine->amparallelrescan = NULL;
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 180000
|
|
||||||
amroutine->amtranslatestrategy = NULL;
|
|
||||||
amroutine->amtranslatecmptype = NULL;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
PG_RETURN_POINTER(amroutine);
|
PG_RETURN_POINTER(amroutine);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,10 @@
|
|||||||
#include "lib/pairingheap.h"
|
#include "lib/pairingheap.h"
|
||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "port.h" /* for random() */
|
#include "port.h" /* for random() */
|
||||||
#include "storage/condition_variable.h"
|
|
||||||
#include "utils/sampling.h"
|
#include "utils/sampling.h"
|
||||||
#include "utils/tuplesort.h"
|
#include "utils/tuplesort.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 150000
|
#if PG_VERSION_NUM >= 150000
|
||||||
#include "common/pg_prng.h"
|
#include "common/pg_prng.h"
|
||||||
#endif
|
#endif
|
||||||
@@ -26,10 +21,6 @@
|
|||||||
#include "portability/instr_time.h"
|
#include "portability/instr_time.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 190000
|
|
||||||
typedef Pointer Item;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define IVFFLAT_MAX_DIM 2000
|
#define IVFFLAT_MAX_DIM 2000
|
||||||
|
|
||||||
/* Support functions */
|
/* Support functions */
|
||||||
@@ -82,11 +73,9 @@ typedef Pointer Item;
|
|||||||
#if PG_VERSION_NUM >= 150000
|
#if PG_VERSION_NUM >= 150000
|
||||||
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
|
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
|
||||||
#define RandomInt() pg_prng_uint32(&pg_global_prng_state)
|
#define RandomInt() pg_prng_uint32(&pg_global_prng_state)
|
||||||
#define SeedRandom(seed) pg_prng_seed(&pg_global_prng_state, seed)
|
|
||||||
#else
|
#else
|
||||||
#define RandomDouble() (((double) random()) / MAX_RANDOM_VALUE)
|
#define RandomDouble() (((double) random()) / MAX_RANDOM_VALUE)
|
||||||
#define RandomInt() random()
|
#define RandomInt() random()
|
||||||
#define SeedRandom(seed) srandom(seed)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Variables */
|
/* Variables */
|
||||||
@@ -204,7 +193,6 @@ typedef struct IvfflatBuildState
|
|||||||
VectorArray samples;
|
VectorArray samples;
|
||||||
VectorArray centers;
|
VectorArray centers;
|
||||||
ListInfo *listInfo;
|
ListInfo *listInfo;
|
||||||
Size itemsize;
|
|
||||||
|
|
||||||
#ifdef IVFFLAT_KMEANS_DEBUG
|
#ifdef IVFFLAT_KMEANS_DEBUG
|
||||||
double inertia;
|
double inertia;
|
||||||
@@ -215,8 +203,7 @@ typedef struct IvfflatBuildState
|
|||||||
/* Sampling */
|
/* Sampling */
|
||||||
BlockSamplerData bs;
|
BlockSamplerData bs;
|
||||||
ReservoirStateData rstate;
|
ReservoirStateData rstate;
|
||||||
double samplerows;
|
int rowstoskip;
|
||||||
double rowstoskip;
|
|
||||||
|
|
||||||
/* Sorting */
|
/* Sorting */
|
||||||
Tuplesortstate *sortstate;
|
Tuplesortstate *sortstate;
|
||||||
@@ -224,7 +211,6 @@ typedef struct IvfflatBuildState
|
|||||||
TupleTableSlot *slot;
|
TupleTableSlot *slot;
|
||||||
|
|
||||||
/* Memory */
|
/* Memory */
|
||||||
Size memoryUsed;
|
|
||||||
MemoryContext tmpCtx;
|
MemoryContext tmpCtx;
|
||||||
|
|
||||||
/* Parallel builds */
|
/* Parallel builds */
|
||||||
@@ -305,32 +291,22 @@ typedef IvfflatScanOpaqueData * IvfflatScanOpaque;
|
|||||||
static inline Pointer
|
static inline Pointer
|
||||||
VectorArrayGet(VectorArray arr, int offset)
|
VectorArrayGet(VectorArray arr, int offset)
|
||||||
{
|
{
|
||||||
if (offset >= arr->maxlen)
|
|
||||||
elog(ERROR, "safety check failed");
|
|
||||||
|
|
||||||
return ((char *) arr->items) + (offset * arr->itemsize);
|
return ((char *) arr->items) + (offset * arr->itemsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
VectorArraySet(VectorArray arr, int offset, Pointer val)
|
VectorArraySet(VectorArray arr, int offset, Pointer val)
|
||||||
{
|
{
|
||||||
Size size = VARSIZE_ANY(val);
|
memcpy(VectorArrayGet(arr, offset), val, VARSIZE_ANY(val));
|
||||||
|
|
||||||
if (size > arr->itemsize)
|
|
||||||
elog(ERROR, "safety check failed");
|
|
||||||
|
|
||||||
memcpy(VectorArrayGet(arr, offset), val, size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Methods */
|
/* Methods */
|
||||||
VectorArray VectorArrayInit(int maxlen, int dimensions, Size itemsize);
|
VectorArray VectorArrayInit(int maxlen, int dimensions, Size itemsize);
|
||||||
void VectorArrayFree(VectorArray arr);
|
void VectorArrayFree(VectorArray arr);
|
||||||
void IvfflatKmeans(Relation index, VectorArray samples, VectorArray centers, const IvfflatTypeInfo * typeInfo, Size memoryUsed);
|
void IvfflatKmeans(Relation index, VectorArray samples, VectorArray centers, const IvfflatTypeInfo * typeInfo);
|
||||||
FmgrInfo *IvfflatOptionalProcInfo(Relation index, uint16 procnum);
|
FmgrInfo *IvfflatOptionalProcInfo(Relation index, uint16 procnum);
|
||||||
Datum IvfflatNormValue(const IvfflatTypeInfo * typeInfo, Oid collation, Datum value);
|
Datum IvfflatNormValue(const IvfflatTypeInfo * typeInfo, Oid collation, Datum value);
|
||||||
bool IvfflatCheckNorm(FmgrInfo *procinfo, Oid collation, Datum value);
|
bool IvfflatCheckNorm(FmgrInfo *procinfo, Oid collation, Datum value);
|
||||||
void IvfflatNormVectors(const IvfflatTypeInfo * typeInfo, Oid collation, VectorArray arr, MemoryContext tmpCtx);
|
|
||||||
void IvfflatCheckMemoryUsage(Size totalSize);
|
|
||||||
int IvfflatGetLists(Relation index);
|
int IvfflatGetLists(Relation index);
|
||||||
void IvfflatGetMetaPageInfo(Relation index, int *lists, int *dimensions);
|
void IvfflatGetMetaPageInfo(Relation index, int *lists, int *dimensions);
|
||||||
void IvfflatUpdateList(Relation index, ListInfo listInfo, BlockNumber insertPage, BlockNumber originalInsertPage, BlockNumber startPage, ForkNumber forkNum);
|
void IvfflatUpdateList(Relation index, ListInfo listInfo, BlockNumber insertPage, BlockNumber originalInsertPage, BlockNumber startPage, ForkNumber forkNum);
|
||||||
|
|||||||
@@ -2,16 +2,11 @@
|
|||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/generic_xlog.h"
|
#include "access/generic_xlog.h"
|
||||||
#include "access/itup.h"
|
|
||||||
#include "fmgr.h"
|
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "nodes/execnodes.h"
|
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "storage/lmgr.h"
|
#include "storage/lmgr.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find the list that minimizes the distance function
|
* Find the list that minimizes the distance function
|
||||||
@@ -70,7 +65,7 @@ FindInsertPage(Relation index, Datum *values, BlockNumber *insertPage, ListInfo
|
|||||||
* Insert a tuple into the index
|
* Insert a tuple into the index
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid)
|
InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid, Relation heapRel)
|
||||||
{
|
{
|
||||||
const IvfflatTypeInfo *typeInfo = IvfflatGetTypeInfo(index);
|
const IvfflatTypeInfo *typeInfo = IvfflatGetTypeInfo(index);
|
||||||
IndexTuple itup;
|
IndexTuple itup;
|
||||||
@@ -209,7 +204,7 @@ ivfflatinsert(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid,
|
|||||||
oldCtx = MemoryContextSwitchTo(insertCtx);
|
oldCtx = MemoryContextSwitchTo(insertCtx);
|
||||||
|
|
||||||
/* Insert tuple */
|
/* Insert tuple */
|
||||||
InsertTuple(index, values, isnull, heap_tid);
|
InsertTuple(index, values, isnull, heap_tid, heap);
|
||||||
|
|
||||||
/* Delete memory context */
|
/* Delete memory context */
|
||||||
MemoryContextSwitchTo(oldCtx);
|
MemoryContextSwitchTo(oldCtx);
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <limits.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
#include "bitvec.h"
|
||||||
#include "fmgr.h"
|
#include "halfutils.h"
|
||||||
|
#include "halfvec.h"
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
|
#include "utils/builtins.h"
|
||||||
|
#include "utils/datum.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/relcache.h"
|
#include "vector.h"
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize with kmeans++
|
* Initialize with kmeans++
|
||||||
@@ -99,8 +97,22 @@ NormCenters(const IvfflatTypeInfo * typeInfo, Oid collation, VectorArray centers
|
|||||||
MemoryContext normCtx = AllocSetContextCreate(CurrentMemoryContext,
|
MemoryContext normCtx = AllocSetContextCreate(CurrentMemoryContext,
|
||||||
"Ivfflat norm temporary context",
|
"Ivfflat norm temporary context",
|
||||||
ALLOCSET_DEFAULT_SIZES);
|
ALLOCSET_DEFAULT_SIZES);
|
||||||
|
MemoryContext oldCtx = MemoryContextSwitchTo(normCtx);
|
||||||
|
|
||||||
IvfflatNormVectors(typeInfo, collation, centers, normCtx);
|
for (int j = 0; j < centers->length; j++)
|
||||||
|
{
|
||||||
|
Datum center = PointerGetDatum(VectorArrayGet(centers, j));
|
||||||
|
Datum newCenter = IvfflatNormValue(typeInfo, collation, center);
|
||||||
|
Size size = VARSIZE_ANY(DatumGetPointer(newCenter));
|
||||||
|
|
||||||
|
if (size > centers->itemsize)
|
||||||
|
elog(ERROR, "safety check failed");
|
||||||
|
|
||||||
|
memcpy(DatumGetPointer(center), DatumGetPointer(newCenter), size);
|
||||||
|
MemoryContextReset(normCtx);
|
||||||
|
}
|
||||||
|
|
||||||
|
MemoryContextSwitchTo(oldCtx);
|
||||||
MemoryContextDelete(normCtx);
|
MemoryContextDelete(normCtx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,7 +256,7 @@ ComputeNewCenters(VectorArray samples, float *agg, VectorArray newCenters, int *
|
|||||||
* https://www.aaai.org/Papers/ICML/2003/ICML03-022.pdf
|
* https://www.aaai.org/Papers/ICML/2003/ICML03-022.pdf
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
ElkanKmeans(Relation index, VectorArray samples, VectorArray centers, const IvfflatTypeInfo * typeInfo, Size memoryUsed)
|
ElkanKmeans(Relation index, VectorArray samples, VectorArray centers, const IvfflatTypeInfo * typeInfo)
|
||||||
{
|
{
|
||||||
FmgrInfo *procinfo;
|
FmgrInfo *procinfo;
|
||||||
FmgrInfo *normprocinfo;
|
FmgrInfo *normprocinfo;
|
||||||
@@ -263,6 +275,8 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers, const Ivff
|
|||||||
float *newcdist;
|
float *newcdist;
|
||||||
|
|
||||||
/* Calculate allocation sizes */
|
/* Calculate allocation sizes */
|
||||||
|
Size samplesSize = VECTOR_ARRAY_SIZE(samples->maxlen, samples->itemsize);
|
||||||
|
Size centersSize = VECTOR_ARRAY_SIZE(centers->maxlen, centers->itemsize);
|
||||||
Size newCentersSize = VECTOR_ARRAY_SIZE(numCenters, centers->itemsize);
|
Size newCentersSize = VECTOR_ARRAY_SIZE(numCenters, centers->itemsize);
|
||||||
Size aggSize = sizeof(float) * (int64) numCenters * dimensions;
|
Size aggSize = sizeof(float) * (int64) numCenters * dimensions;
|
||||||
Size centerCountsSize = sizeof(int) * numCenters;
|
Size centerCountsSize = sizeof(int) * numCenters;
|
||||||
@@ -274,13 +288,18 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers, const Ivff
|
|||||||
Size newcdistSize = sizeof(float) * numCenters;
|
Size newcdistSize = sizeof(float) * numCenters;
|
||||||
|
|
||||||
/* Calculate total size */
|
/* Calculate total size */
|
||||||
Size totalSize = memoryUsed + newCentersSize + aggSize + centerCountsSize + closestCentersSize + lowerBoundSize + upperBoundSize + sSize + halfcdistSize + newcdistSize;
|
Size totalSize = samplesSize + centersSize + newCentersSize + aggSize + centerCountsSize + closestCentersSize + lowerBoundSize + upperBoundSize + sSize + halfcdistSize + newcdistSize;
|
||||||
|
|
||||||
/* Check memory requirements */
|
/* Check memory requirements */
|
||||||
IvfflatCheckMemoryUsage(totalSize);
|
/* Add one to error message to ceil */
|
||||||
|
if (totalSize > (Size) maintenance_work_mem * 1024L)
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
||||||
|
errmsg("memory required is %zu MB, maintenance_work_mem is %d MB",
|
||||||
|
totalSize / (1024 * 1024) + 1, maintenance_work_mem / 1024)));
|
||||||
|
|
||||||
/* Ensure indexing does not overflow */
|
/* Ensure indexing does not overflow */
|
||||||
if (numCenters > INT_MAX / numCenters)
|
if (numCenters * numCenters > INT_MAX)
|
||||||
elog(ERROR, "Indexing overflow detected. Please report a bug.");
|
elog(ERROR, "Indexing overflow detected. Please report a bug.");
|
||||||
|
|
||||||
/* Set support functions */
|
/* Set support functions */
|
||||||
@@ -541,7 +560,7 @@ CheckCenters(Relation index, VectorArray centers, const IvfflatTypeInfo * typeIn
|
|||||||
* We use spherical k-means for inner product and cosine
|
* We use spherical k-means for inner product and cosine
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
IvfflatKmeans(Relation index, VectorArray samples, VectorArray centers, const IvfflatTypeInfo * typeInfo, Size memoryUsed)
|
IvfflatKmeans(Relation index, VectorArray samples, VectorArray centers, const IvfflatTypeInfo * typeInfo)
|
||||||
{
|
{
|
||||||
MemoryContext kmeansCtx = AllocSetContextCreate(CurrentMemoryContext,
|
MemoryContext kmeansCtx = AllocSetContextCreate(CurrentMemoryContext,
|
||||||
"Ivfflat kmeans temporary context",
|
"Ivfflat kmeans temporary context",
|
||||||
@@ -551,7 +570,7 @@ IvfflatKmeans(Relation index, VectorArray samples, VectorArray centers, const Iv
|
|||||||
if (samples->length == 0)
|
if (samples->length == 0)
|
||||||
RandomCenters(index, centers, typeInfo);
|
RandomCenters(index, centers, typeInfo);
|
||||||
else
|
else
|
||||||
ElkanKmeans(index, samples, centers, typeInfo, memoryUsed);
|
ElkanKmeans(index, samples, centers, typeInfo);
|
||||||
|
|
||||||
CheckCenters(index, centers, typeInfo);
|
CheckCenters(index, centers, typeInfo);
|
||||||
|
|
||||||
|
|||||||
@@ -2,26 +2,15 @@
|
|||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/itup.h"
|
|
||||||
#include "access/relscan.h"
|
#include "access/relscan.h"
|
||||||
#include "access/tupdesc.h"
|
|
||||||
#include "catalog/pg_operator_d.h"
|
#include "catalog/pg_operator_d.h"
|
||||||
#include "catalog/pg_type_d.h"
|
#include "catalog/pg_type_d.h"
|
||||||
#include "fmgr.h"
|
|
||||||
#include "lib/pairingheap.h"
|
#include "lib/pairingheap.h"
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
#include "pgstat.h"
|
#include "pgstat.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
|
||||||
#include "utils/snapmgr.h"
|
|
||||||
#include "utils/tuplesort.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define GetScanList(ptr) pairingheap_container(IvfflatScanList, ph_node, ptr)
|
#define GetScanList(ptr) pairingheap_container(IvfflatScanList, ph_node, ptr)
|
||||||
#define GetScanListConst(ptr) pairingheap_const_container(IvfflatScanList, ph_node, ptr)
|
#define GetScanListConst(ptr) pairingheap_const_container(IvfflatScanList, ph_node, ptr)
|
||||||
@@ -298,9 +287,6 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
so->tupdesc = CreateTemplateTupleDesc(2);
|
so->tupdesc = CreateTemplateTupleDesc(2);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
||||||
#if PG_VERSION_NUM >= 190000
|
|
||||||
TupleDescFinalize(so->tupdesc);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Prep sort */
|
/* Prep sort */
|
||||||
so->sortstate = InitScanSortState(so->tupdesc);
|
so->sortstate = InitScanSortState(so->tupdesc);
|
||||||
@@ -369,10 +355,6 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
|
|
||||||
/* Count index scan for stats */
|
/* Count index scan for stats */
|
||||||
pgstat_count_index_scan(scan->indexRelation);
|
pgstat_count_index_scan(scan->indexRelation);
|
||||||
#if PG_VERSION_NUM >= 180000
|
|
||||||
if (scan->instrument)
|
|
||||||
scan->instrument->nsearches++;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Safety check */
|
/* Safety check */
|
||||||
if (scan->orderByData == NULL)
|
if (scan->orderByData == NULL)
|
||||||
|
|||||||
@@ -1,21 +1,13 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/generic_xlog.h"
|
#include "access/generic_xlog.h"
|
||||||
|
#include "bitvec.h"
|
||||||
|
#include "catalog/pg_type.h"
|
||||||
#include "fmgr.h"
|
#include "fmgr.h"
|
||||||
#include "halfutils.h"
|
#include "halfutils.h"
|
||||||
#include "halfvec.h"
|
#include "halfvec.h"
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "miscadmin.h"
|
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "utils/memutils.h"
|
|
||||||
#include "utils/relcache.h"
|
|
||||||
#include "utils/varbit.h"
|
|
||||||
#include "vector.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Allocate a vector array
|
* Allocate a vector array
|
||||||
@@ -23,15 +15,11 @@
|
|||||||
VectorArray
|
VectorArray
|
||||||
VectorArrayInit(int maxlen, int dimensions, Size itemsize)
|
VectorArrayInit(int maxlen, int dimensions, Size itemsize)
|
||||||
{
|
{
|
||||||
VectorArray res;
|
VectorArray res = palloc(sizeof(VectorArrayData));
|
||||||
|
|
||||||
if (maxlen < 1 || dimensions < 1 || itemsize == 0)
|
|
||||||
elog(ERROR, "cannot create vector array");
|
|
||||||
|
|
||||||
/* Ensure items are aligned to prevent UB */
|
/* Ensure items are aligned to prevent UB */
|
||||||
itemsize = MAXALIGN(itemsize);
|
itemsize = MAXALIGN(itemsize);
|
||||||
|
|
||||||
res = palloc(sizeof(VectorArrayData));
|
|
||||||
res->length = 0;
|
res->length = 0;
|
||||||
res->maxlen = maxlen;
|
res->maxlen = maxlen;
|
||||||
res->dim = dimensions;
|
res->dim = dimensions;
|
||||||
@@ -94,40 +82,6 @@ IvfflatCheckNorm(FmgrInfo *procinfo, Oid collation, Datum value)
|
|||||||
return DatumGetFloat8(FunctionCall1Coll(procinfo, collation, value)) > 0;
|
return DatumGetFloat8(FunctionCall1Coll(procinfo, collation, value)) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Normalize vectors
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
IvfflatNormVectors(const IvfflatTypeInfo * typeInfo, Oid collation, VectorArray arr, MemoryContext tmpCtx)
|
|
||||||
{
|
|
||||||
MemoryContext oldCtx = MemoryContextSwitchTo(tmpCtx);
|
|
||||||
|
|
||||||
for (int i = 0; i < arr->length; i++)
|
|
||||||
{
|
|
||||||
Datum value = PointerGetDatum(VectorArrayGet(arr, i));
|
|
||||||
Datum newValue = IvfflatNormValue(typeInfo, collation, value);
|
|
||||||
|
|
||||||
VectorArraySet(arr, i, DatumGetPointer(newValue));
|
|
||||||
MemoryContextReset(tmpCtx);
|
|
||||||
}
|
|
||||||
|
|
||||||
MemoryContextSwitchTo(oldCtx);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Check memory usage
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
IvfflatCheckMemoryUsage(Size totalSize)
|
|
||||||
{
|
|
||||||
/* Add one to error message to ceil */
|
|
||||||
if (totalSize > maintenance_work_mem * (Size) 1024)
|
|
||||||
ereport(ERROR,
|
|
||||||
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
||||||
errmsg("memory required is %zu MB, maintenance_work_mem is %d MB",
|
|
||||||
totalSize / (1024 * 1024) + 1, maintenance_work_mem / 1024)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* New buffer
|
* New buffer
|
||||||
*/
|
*/
|
||||||
@@ -305,8 +259,8 @@ VectorUpdateCenter(Pointer v, int dimensions, float *x)
|
|||||||
SET_VARSIZE(vec, VECTOR_SIZE(dimensions));
|
SET_VARSIZE(vec, VECTOR_SIZE(dimensions));
|
||||||
vec->dim = dimensions;
|
vec->dim = dimensions;
|
||||||
|
|
||||||
for (int i = 0; i < dimensions; i++)
|
for (int k = 0; k < dimensions; k++)
|
||||||
vec->x[i] = x[i];
|
vec->x[k] = x[k];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -317,8 +271,8 @@ HalfvecUpdateCenter(Pointer v, int dimensions, float *x)
|
|||||||
SET_VARSIZE(vec, HALFVEC_SIZE(dimensions));
|
SET_VARSIZE(vec, HALFVEC_SIZE(dimensions));
|
||||||
vec->dim = dimensions;
|
vec->dim = dimensions;
|
||||||
|
|
||||||
for (int i = 0; i < dimensions; i++)
|
for (int k = 0; k < dimensions; k++)
|
||||||
vec->x[i] = Float4ToHalfUnchecked(x[i]);
|
vec->x[k] = Float4ToHalfUnchecked(x[k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -330,33 +284,29 @@ BitUpdateCenter(Pointer v, int dimensions, float *x)
|
|||||||
SET_VARSIZE(vec, VARBITTOTALLEN(dimensions));
|
SET_VARSIZE(vec, VARBITTOTALLEN(dimensions));
|
||||||
VARBITLEN(vec) = dimensions;
|
VARBITLEN(vec) = dimensions;
|
||||||
|
|
||||||
for (uint32 i = 0; i < VARBITBYTES(vec); i++)
|
for (uint32 k = 0; k < VARBITBYTES(vec); k++)
|
||||||
nx[i] = 0;
|
nx[k] = 0;
|
||||||
|
|
||||||
for (int i = 0; i < dimensions; i++)
|
for (int k = 0; k < dimensions; k++)
|
||||||
nx[i / 8] |= (x[i] > 0.5 ? 1 : 0) << (7 - (i % 8));
|
nx[k / 8] |= (x[k] > 0.5 ? 1 : 0) << (7 - (k % 8));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
VectorSumCenter(Pointer v, float *x)
|
VectorSumCenter(Pointer v, float *x)
|
||||||
{
|
{
|
||||||
Vector *vec = (Vector *) v;
|
Vector *vec = (Vector *) v;
|
||||||
int dim = vec->dim;
|
|
||||||
|
|
||||||
/* Auto-vectorized */
|
for (int k = 0; k < vec->dim; k++)
|
||||||
for (int i = 0; i < dim; i++)
|
x[k] += vec->x[k];
|
||||||
x[i] += vec->x[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
HalfvecSumCenter(Pointer v, float *x)
|
HalfvecSumCenter(Pointer v, float *x)
|
||||||
{
|
{
|
||||||
HalfVector *vec = (HalfVector *) v;
|
HalfVector *vec = (HalfVector *) v;
|
||||||
int dim = vec->dim;
|
|
||||||
|
|
||||||
/* Auto-vectorized on aarch64 */
|
for (int k = 0; k < vec->dim; k++)
|
||||||
for (int i = 0; i < dim; i++)
|
x[k] += HalfToFloat4(vec->x[k]);
|
||||||
x[i] += HalfToFloat4(vec->x[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -364,8 +314,8 @@ BitSumCenter(Pointer v, float *x)
|
|||||||
{
|
{
|
||||||
VarBit *vec = (VarBit *) v;
|
VarBit *vec = (VarBit *) v;
|
||||||
|
|
||||||
for (int i = 0; i < VARBITLEN(vec); i++)
|
for (int k = 0; k < VARBITLEN(vec); k++)
|
||||||
x[i] += (float) (((VARBITS(vec)[i / 8]) >> (7 - (i % 8))) & 0x01);
|
x[k] += (float) (((VARBITS(vec)[k / 8]) >> (7 - (k % 8))) & 0x01);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include "access/genam.h"
|
|
||||||
#include "access/generic_xlog.h"
|
#include "access/generic_xlog.h"
|
||||||
#include "access/itup.h"
|
|
||||||
#include "commands/vacuum.h"
|
#include "commands/vacuum.h"
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "utils/relcache.h"
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 180000
|
#if PG_VERSION_NUM >= 180000
|
||||||
#define vacuum_delay_point() vacuum_delay_point(false)
|
#define vacuum_delay_point() vacuum_delay_point(false)
|
||||||
|
|||||||
@@ -5,27 +5,18 @@
|
|||||||
|
|
||||||
#include "catalog/pg_type.h"
|
#include "catalog/pg_type.h"
|
||||||
#include "common/shortest_dec.h"
|
#include "common/shortest_dec.h"
|
||||||
|
#include "common/string.h"
|
||||||
#include "fmgr.h"
|
#include "fmgr.h"
|
||||||
#include "halfutils.h"
|
#include "halfutils.h"
|
||||||
#include "halfvec.h"
|
#include "halfvec.h"
|
||||||
#include "lib/stringinfo.h"
|
|
||||||
#include "libpq/pqformat.h"
|
#include "libpq/pqformat.h"
|
||||||
#include "sparsevec.h"
|
#include "sparsevec.h"
|
||||||
#include "utils/array.h"
|
#include "utils/array.h"
|
||||||
#include "utils/builtins.h"
|
#include "utils/builtins.h"
|
||||||
#include "utils/float.h"
|
#include "utils/float.h"
|
||||||
#include "utils/fmgrprotos.h"
|
|
||||||
#include "utils/lsyscache.h"
|
#include "utils/lsyscache.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
|
||||||
#include "varatt.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 170000
|
|
||||||
#include "parser/scansup.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef struct SparseInputElement
|
typedef struct SparseInputElement
|
||||||
{
|
{
|
||||||
int32 index;
|
int32 index;
|
||||||
@@ -159,9 +150,9 @@ InitSparseVector(int dim, int nnz)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 170000
|
/*
|
||||||
#define sparsevec_isspace(ch) scanner_isspace(ch)
|
* Check for whitespace, since array_isspace() is static
|
||||||
#else
|
*/
|
||||||
static inline bool
|
static inline bool
|
||||||
sparsevec_isspace(char ch)
|
sparsevec_isspace(char ch)
|
||||||
{
|
{
|
||||||
@@ -174,7 +165,6 @@ sparsevec_isspace(char ch)
|
|||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compare indices
|
* Compare indices
|
||||||
@@ -182,10 +172,10 @@ sparsevec_isspace(char ch)
|
|||||||
static int
|
static int
|
||||||
CompareIndices(const void *a, const void *b)
|
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;
|
return -1;
|
||||||
|
|
||||||
if (((const SparseInputElement *) a)->index > ((const SparseInputElement *) b)->index)
|
if (((SparseInputElement *) a)->index > ((SparseInputElement *) b)->index)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
42
src/vector.c
42
src/vector.c
@@ -16,20 +16,16 @@
|
|||||||
#include "port.h" /* for strtof() */
|
#include "port.h" /* for strtof() */
|
||||||
#include "sparsevec.h"
|
#include "sparsevec.h"
|
||||||
#include "utils/array.h"
|
#include "utils/array.h"
|
||||||
|
#include "utils/builtins.h"
|
||||||
#include "utils/float.h"
|
#include "utils/float.h"
|
||||||
#include "utils/fmgrprotos.h"
|
|
||||||
#include "utils/lsyscache.h"
|
#include "utils/lsyscache.h"
|
||||||
#include "utils/varbit.h"
|
#include "utils/numeric.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 160000
|
#if PG_VERSION_NUM >= 160000
|
||||||
#include "varatt.h"
|
#include "varatt.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 170000
|
|
||||||
#include "parser/scansup.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1)
|
#define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1)
|
||||||
#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1))
|
#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1))
|
||||||
|
|
||||||
@@ -39,11 +35,7 @@
|
|||||||
#define VECTOR_TARGET_CLONES
|
#define VECTOR_TARGET_CLONES
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 180000
|
|
||||||
PG_MODULE_MAGIC_EXT(.name = "vector", .version = "0.8.5");
|
|
||||||
#else
|
|
||||||
PG_MODULE_MAGIC;
|
PG_MODULE_MAGIC;
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize index options and variables
|
* Initialize index options and variables
|
||||||
@@ -133,9 +125,9 @@ InitVector(int dim)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 170000
|
/*
|
||||||
#define vector_isspace(ch) scanner_isspace(ch)
|
* Check for whitespace, since array_isspace() is static
|
||||||
#else
|
*/
|
||||||
static inline bool
|
static inline bool
|
||||||
vector_isspace(char ch)
|
vector_isspace(char ch)
|
||||||
{
|
{
|
||||||
@@ -148,7 +140,6 @@ vector_isspace(char ch)
|
|||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check state array
|
* Check state array
|
||||||
@@ -929,13 +920,11 @@ vector_concat(PG_FUNCTION_ARGS)
|
|||||||
CheckDim(dim);
|
CheckDim(dim);
|
||||||
result = InitVector(dim);
|
result = InitVector(dim);
|
||||||
|
|
||||||
/* Auto-vectorized */
|
for (int i = 0; i < a->dim; i++)
|
||||||
for (int i = 0, imax = a->dim; i < imax; i++)
|
|
||||||
result->x[i] = a->x[i];
|
result->x[i] = a->x[i];
|
||||||
|
|
||||||
/* Auto-vectorized */
|
for (int i = 0; i < b->dim; i++)
|
||||||
for (int i = 0, imax = b->dim, start = a->dim; i < imax; i++)
|
result->x[i + a->dim] = b->x[i];
|
||||||
result->x[i + start] = b->x[i];
|
|
||||||
|
|
||||||
PG_RETURN_POINTER(result);
|
PG_RETURN_POINTER(result);
|
||||||
}
|
}
|
||||||
@@ -951,21 +940,8 @@ binary_quantize(PG_FUNCTION_ARGS)
|
|||||||
float *ax = a->x;
|
float *ax = a->x;
|
||||||
VarBit *result = InitBitVector(a->dim);
|
VarBit *result = InitBitVector(a->dim);
|
||||||
unsigned char *rx = VARBITS(result);
|
unsigned char *rx = VARBITS(result);
|
||||||
int i = 0;
|
|
||||||
int count = (a->dim / 8) * 8;
|
|
||||||
|
|
||||||
/* Auto-vectorized */
|
for (int i = 0; i < a->dim; i++)
|
||||||
for (; i < count; i += 8)
|
|
||||||
{
|
|
||||||
unsigned char result_byte = 0;
|
|
||||||
|
|
||||||
for (int j = 0; j < 8; j++)
|
|
||||||
result_byte |= (ax[i + j] > 0) << (7 - j);
|
|
||||||
|
|
||||||
rx[i / 8] = result_byte;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; i < a->dim; i++)
|
|
||||||
rx[i / 8] |= (ax[i] > 0) << (7 - (i % 8));
|
rx[i / 8] |= (ax[i] > 0) << (7 - (i % 8));
|
||||||
|
|
||||||
PG_RETURN_VARBIT_P(result);
|
PG_RETURN_VARBIT_P(result);
|
||||||
|
|||||||
@@ -540,12 +540,6 @@ SELECT binary_quantize('[0,0.1,-0.2,-0.3,0.4,0.5,0.6,-0.7,0.8,-0.9,1]'::halfvec)
|
|||||||
01001110101
|
01001110101
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT binary_quantize('[1,2,3,-4,5,6,-7,8,1,-2,-3,4,5,-6,7,8,-1,2,3]'::halfvec);
|
|
||||||
binary_quantize
|
|
||||||
---------------------
|
|
||||||
1110110110011011011
|
|
||||||
(1 row)
|
|
||||||
|
|
||||||
SELECT subvector('[1,2,3,4,5]'::halfvec, 1, 3);
|
SELECT subvector('[1,2,3,4,5]'::halfvec, 1, 3);
|
||||||
subvector
|
subvector
|
||||||
-----------
|
-----------
|
||||||
|
|||||||
@@ -49,11 +49,3 @@ CREATE INDEX ON t USING hnsw ((val::bit(3)) bit_hamming_ops);
|
|||||||
CREATE INDEX ON t USING hnsw ((val::bit(64001)) bit_hamming_ops);
|
CREATE INDEX ON t USING hnsw ((val::bit(64001)) bit_hamming_ops);
|
||||||
ERROR: column cannot have more than 64000 dimensions for hnsw index
|
ERROR: column cannot have more than 64000 dimensions for hnsw index
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
-- dimensions
|
|
||||||
CREATE TABLE t (val bit(64000));
|
|
||||||
CREATE INDEX ON t USING hnsw (val bit_hamming_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
CREATE TABLE t (val bit(64001));
|
|
||||||
CREATE INDEX ON t USING hnsw (val bit_hamming_ops);
|
|
||||||
ERROR: column cannot have more than 64000 dimensions for hnsw index
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|||||||
@@ -100,11 +100,3 @@ SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <+> (SELECT NULL::halfvec)) t
|
|||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
-- dimensions
|
|
||||||
CREATE TABLE t (val halfvec(4000));
|
|
||||||
CREATE INDEX ON t USING hnsw (val halfvec_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
CREATE TABLE t (val halfvec(4001));
|
|
||||||
CREATE INDEX ON t USING hnsw (val halfvec_l2_ops);
|
|
||||||
ERROR: column cannot have more than 4000 dimensions for hnsw index
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|||||||
@@ -161,7 +161,6 @@ ERROR: value 1001 out of bounds for option "ef_construction"
|
|||||||
DETAIL: Valid values are between "4" and "1000".
|
DETAIL: Valid values are between "4" and "1000".
|
||||||
CREATE INDEX ON t USING hnsw (val vector_l2_ops) WITH (m = 16, ef_construction = 31);
|
CREATE INDEX ON t USING hnsw (val vector_l2_ops) WITH (m = 16, ef_construction = 31);
|
||||||
ERROR: ef_construction must be greater than or equal to 2 * m
|
ERROR: ef_construction must be greater than or equal to 2 * m
|
||||||
DROP TABLE t;
|
|
||||||
SHOW hnsw.ef_search;
|
SHOW hnsw.ef_search;
|
||||||
hnsw.ef_search
|
hnsw.ef_search
|
||||||
----------------
|
----------------
|
||||||
@@ -199,11 +198,4 @@ SET hnsw.scan_mem_multiplier = 0;
|
|||||||
ERROR: 0 is outside the valid range for parameter "hnsw.scan_mem_multiplier" (1 .. 1000)
|
ERROR: 0 is outside the valid range for parameter "hnsw.scan_mem_multiplier" (1 .. 1000)
|
||||||
SET hnsw.scan_mem_multiplier = 1001;
|
SET hnsw.scan_mem_multiplier = 1001;
|
||||||
ERROR: 1001 is outside the valid range for parameter "hnsw.scan_mem_multiplier" (1 .. 1000)
|
ERROR: 1001 is outside the valid range for parameter "hnsw.scan_mem_multiplier" (1 .. 1000)
|
||||||
-- dimensions
|
|
||||||
CREATE TABLE t (val vector(2000));
|
|
||||||
CREATE INDEX ON t USING hnsw (val vector_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
CREATE TABLE t (val vector(2001));
|
|
||||||
CREATE INDEX ON t USING hnsw (val vector_l2_ops);
|
|
||||||
ERROR: column cannot have more than 2000 dimensions for hnsw index
|
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
|
|||||||
@@ -35,32 +35,3 @@ NOTICE: ivfflat index created with little data
|
|||||||
DETAIL: This will cause low recall.
|
DETAIL: This will cause low recall.
|
||||||
HINT: Drop the index until the table has more data.
|
HINT: Drop the index until the table has more data.
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
-- dimensions
|
|
||||||
CREATE TABLE t (val bit(64000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val bit_hamming_ops);
|
|
||||||
NOTICE: ivfflat index created with little data
|
|
||||||
DETAIL: This will cause low recall.
|
|
||||||
HINT: Drop the index until the table has more data.
|
|
||||||
DROP TABLE t;
|
|
||||||
CREATE TABLE t (val bit(64001));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val bit_hamming_ops);
|
|
||||||
ERROR: column cannot have more than 64000 dimensions for ivfflat index
|
|
||||||
DROP TABLE t;
|
|
||||||
-- memory
|
|
||||||
SET maintenance_work_mem = '1MB';
|
|
||||||
CREATE TABLE t (val bit(64000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val bit_hamming_ops);
|
|
||||||
NOTICE: ivfflat index created with little data
|
|
||||||
DETAIL: This will cause low recall.
|
|
||||||
HINT: Drop the index until the table has more data.
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
SET maintenance_work_mem = '29MB';
|
|
||||||
CREATE TABLE t (val bit(64000));
|
|
||||||
INSERT INTO t (val) VALUES (B'0'::bit(64000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val bit_hamming_ops);
|
|
||||||
NOTICE: ivfflat index created with little data
|
|
||||||
DETAIL: This will cause low recall.
|
|
||||||
HINT: Drop the index until the table has more data.
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
|
|||||||
@@ -82,32 +82,3 @@ SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <=> (SELECT NULL::halfvec)) t
|
|||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
-- dimensions
|
|
||||||
CREATE TABLE t (val halfvec(4000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val halfvec_l2_ops);
|
|
||||||
NOTICE: ivfflat index created with little data
|
|
||||||
DETAIL: This will cause low recall.
|
|
||||||
HINT: Drop the index until the table has more data.
|
|
||||||
DROP TABLE t;
|
|
||||||
CREATE TABLE t (val halfvec(4001));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val halfvec_l2_ops);
|
|
||||||
ERROR: column cannot have more than 4000 dimensions for ivfflat index
|
|
||||||
DROP TABLE t;
|
|
||||||
-- memory
|
|
||||||
SET maintenance_work_mem = '1MB';
|
|
||||||
CREATE TABLE t (val halfvec(4000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val halfvec_l2_ops);
|
|
||||||
NOTICE: ivfflat index created with little data
|
|
||||||
DETAIL: This will cause low recall.
|
|
||||||
HINT: Drop the index until the table has more data.
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
SET maintenance_work_mem = '6MB';
|
|
||||||
CREATE TABLE t (val halfvec(4000));
|
|
||||||
INSERT INTO t (val) VALUES (array_fill(0, ARRAY[4000]));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val halfvec_l2_ops);
|
|
||||||
NOTICE: ivfflat index created with little data
|
|
||||||
DETAIL: This will cause low recall.
|
|
||||||
HINT: Drop the index until the table has more data.
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
|
|||||||
@@ -143,7 +143,6 @@ DETAIL: Valid values are between "1" and "32768".
|
|||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 32769);
|
CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 32769);
|
||||||
ERROR: value 32769 out of bounds for option "lists"
|
ERROR: value 32769 out of bounds for option "lists"
|
||||||
DETAIL: Valid values are between "1" and "32768".
|
DETAIL: Valid values are between "1" and "32768".
|
||||||
DROP TABLE t;
|
|
||||||
SHOW ivfflat.probes;
|
SHOW ivfflat.probes;
|
||||||
ivfflat.probes
|
ivfflat.probes
|
||||||
----------------
|
----------------
|
||||||
@@ -173,32 +172,4 @@ SET ivfflat.max_probes = 0;
|
|||||||
ERROR: 0 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32768)
|
ERROR: 0 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32768)
|
||||||
SET ivfflat.max_probes = 32769;
|
SET ivfflat.max_probes = 32769;
|
||||||
ERROR: 32769 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32768)
|
ERROR: 32769 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32768)
|
||||||
-- dimensions
|
|
||||||
CREATE TABLE t (val vector(2000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops);
|
|
||||||
NOTICE: ivfflat index created with little data
|
|
||||||
DETAIL: This will cause low recall.
|
|
||||||
HINT: Drop the index until the table has more data.
|
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
CREATE TABLE t (val vector(2001));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops);
|
|
||||||
ERROR: column cannot have more than 2000 dimensions for ivfflat index
|
|
||||||
DROP TABLE t;
|
|
||||||
-- memory
|
|
||||||
SET maintenance_work_mem = '1MB';
|
|
||||||
CREATE TABLE t (val vector(2000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops);
|
|
||||||
NOTICE: ivfflat index created with little data
|
|
||||||
DETAIL: This will cause low recall.
|
|
||||||
HINT: Drop the index until the table has more data.
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
SET maintenance_work_mem = '5MB';
|
|
||||||
CREATE TABLE t (val vector(2000));
|
|
||||||
INSERT INTO t (val) VALUES (array_fill(0, ARRAY[2000]));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops);
|
|
||||||
NOTICE: ivfflat index created with little data
|
|
||||||
DETAIL: This will cause low recall.
|
|
||||||
HINT: Drop the index until the table has more data.
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
|
|||||||
@@ -576,12 +576,6 @@ SELECT binary_quantize('[0,0.1,-0.2,-0.3,0.4,0.5,0.6,-0.7,0.8,-0.9,1]'::vector);
|
|||||||
01001110101
|
01001110101
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT binary_quantize('[1,2,3,-4,5,6,-7,8,1,-2,-3,4,5,-6,7,8,-1,2,3]'::vector);
|
|
||||||
binary_quantize
|
|
||||||
---------------------
|
|
||||||
1110110110011011011
|
|
||||||
(1 row)
|
|
||||||
|
|
||||||
SELECT subvector('[1,2,3,4,5]'::vector, 1, 3);
|
SELECT subvector('[1,2,3,4,5]'::vector, 1, 3);
|
||||||
subvector
|
subvector
|
||||||
-----------
|
-----------
|
||||||
|
|||||||
@@ -121,7 +121,6 @@ SELECT l2_normalize('[65504]'::halfvec);
|
|||||||
|
|
||||||
SELECT binary_quantize('[1,0,-1]'::halfvec);
|
SELECT binary_quantize('[1,0,-1]'::halfvec);
|
||||||
SELECT binary_quantize('[0,0.1,-0.2,-0.3,0.4,0.5,0.6,-0.7,0.8,-0.9,1]'::halfvec);
|
SELECT binary_quantize('[0,0.1,-0.2,-0.3,0.4,0.5,0.6,-0.7,0.8,-0.9,1]'::halfvec);
|
||||||
SELECT binary_quantize('[1,2,3,-4,5,6,-7,8,1,-2,-3,4,5,-6,7,8,-1,2,3]'::halfvec);
|
|
||||||
|
|
||||||
SELECT subvector('[1,2,3,4,5]'::halfvec, 1, 3);
|
SELECT subvector('[1,2,3,4,5]'::halfvec, 1, 3);
|
||||||
SELECT subvector('[1,2,3,4,5]'::halfvec, 3, 2);
|
SELECT subvector('[1,2,3,4,5]'::halfvec, 3, 2);
|
||||||
|
|||||||
@@ -33,13 +33,3 @@ CREATE INDEX ON t USING hnsw (val bit_hamming_ops);
|
|||||||
CREATE INDEX ON t USING hnsw ((val::bit(3)) bit_hamming_ops);
|
CREATE INDEX ON t USING hnsw ((val::bit(3)) bit_hamming_ops);
|
||||||
CREATE INDEX ON t USING hnsw ((val::bit(64001)) bit_hamming_ops);
|
CREATE INDEX ON t USING hnsw ((val::bit(64001)) bit_hamming_ops);
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
|
|
||||||
-- dimensions
|
|
||||||
|
|
||||||
CREATE TABLE t (val bit(64000));
|
|
||||||
CREATE INDEX ON t USING hnsw (val bit_hamming_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
CREATE TABLE t (val bit(64001));
|
|
||||||
CREATE INDEX ON t USING hnsw (val bit_hamming_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|||||||
@@ -56,13 +56,3 @@ SELECT * FROM t ORDER BY val <+> '[3,3,3]';
|
|||||||
SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <+> (SELECT NULL::halfvec)) t2;
|
SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <+> (SELECT NULL::halfvec)) t2;
|
||||||
|
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
|
|
||||||
-- dimensions
|
|
||||||
|
|
||||||
CREATE TABLE t (val halfvec(4000));
|
|
||||||
CREATE INDEX ON t USING hnsw (val halfvec_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
CREATE TABLE t (val halfvec(4001));
|
|
||||||
CREATE INDEX ON t USING hnsw (val halfvec_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|||||||
@@ -95,28 +95,23 @@ CREATE INDEX ON t USING hnsw (val vector_l2_ops) WITH (m = 101);
|
|||||||
CREATE INDEX ON t USING hnsw (val vector_l2_ops) WITH (ef_construction = 3);
|
CREATE INDEX ON t USING hnsw (val vector_l2_ops) WITH (ef_construction = 3);
|
||||||
CREATE INDEX ON t USING hnsw (val vector_l2_ops) WITH (ef_construction = 1001);
|
CREATE INDEX ON t USING hnsw (val vector_l2_ops) WITH (ef_construction = 1001);
|
||||||
CREATE INDEX ON t USING hnsw (val vector_l2_ops) WITH (m = 16, ef_construction = 31);
|
CREATE INDEX ON t USING hnsw (val vector_l2_ops) WITH (m = 16, ef_construction = 31);
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
SHOW hnsw.ef_search;
|
SHOW hnsw.ef_search;
|
||||||
|
|
||||||
SET hnsw.ef_search = 0;
|
SET hnsw.ef_search = 0;
|
||||||
SET hnsw.ef_search = 1001;
|
SET hnsw.ef_search = 1001;
|
||||||
|
|
||||||
SHOW hnsw.iterative_scan;
|
SHOW hnsw.iterative_scan;
|
||||||
|
|
||||||
SET hnsw.iterative_scan = on;
|
SET hnsw.iterative_scan = on;
|
||||||
|
|
||||||
SHOW hnsw.max_scan_tuples;
|
SHOW hnsw.max_scan_tuples;
|
||||||
|
|
||||||
SET hnsw.max_scan_tuples = 0;
|
SET hnsw.max_scan_tuples = 0;
|
||||||
|
|
||||||
SHOW hnsw.scan_mem_multiplier;
|
SHOW hnsw.scan_mem_multiplier;
|
||||||
|
|
||||||
SET hnsw.scan_mem_multiplier = 0;
|
SET hnsw.scan_mem_multiplier = 0;
|
||||||
SET hnsw.scan_mem_multiplier = 1001;
|
SET hnsw.scan_mem_multiplier = 1001;
|
||||||
|
|
||||||
-- dimensions
|
|
||||||
|
|
||||||
CREATE TABLE t (val vector(2000));
|
|
||||||
CREATE INDEX ON t USING hnsw (val vector_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
CREATE TABLE t (val vector(2001));
|
|
||||||
CREATE INDEX ON t USING hnsw (val vector_l2_ops);
|
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
|
|||||||
@@ -21,28 +21,3 @@ CREATE INDEX ON t USING ivfflat ((val::bit(3)) bit_hamming_ops) WITH (lists = 1)
|
|||||||
CREATE INDEX ON t USING ivfflat ((val::bit(64001)) bit_hamming_ops) WITH (lists = 1);
|
CREATE INDEX ON t USING ivfflat ((val::bit(64001)) bit_hamming_ops) WITH (lists = 1);
|
||||||
CREATE INDEX ON t USING ivfflat ((val::bit(2)) bit_hamming_ops) WITH (lists = 5);
|
CREATE INDEX ON t USING ivfflat ((val::bit(2)) bit_hamming_ops) WITH (lists = 5);
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
|
|
||||||
-- dimensions
|
|
||||||
|
|
||||||
CREATE TABLE t (val bit(64000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val bit_hamming_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
CREATE TABLE t (val bit(64001));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val bit_hamming_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
-- memory
|
|
||||||
|
|
||||||
SET maintenance_work_mem = '1MB';
|
|
||||||
CREATE TABLE t (val bit(64000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val bit_hamming_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
|
|
||||||
SET maintenance_work_mem = '29MB';
|
|
||||||
CREATE TABLE t (val bit(64000));
|
|
||||||
INSERT INTO t (val) VALUES (B'0'::bit(64000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val bit_hamming_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
|
|||||||
@@ -43,28 +43,3 @@ SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <=> '[0,0,0]') t2;
|
|||||||
SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <=> (SELECT NULL::halfvec)) t2;
|
SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <=> (SELECT NULL::halfvec)) t2;
|
||||||
|
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
|
|
||||||
-- dimensions
|
|
||||||
|
|
||||||
CREATE TABLE t (val halfvec(4000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val halfvec_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
CREATE TABLE t (val halfvec(4001));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val halfvec_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
-- memory
|
|
||||||
|
|
||||||
SET maintenance_work_mem = '1MB';
|
|
||||||
CREATE TABLE t (val halfvec(4000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val halfvec_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
|
|
||||||
SET maintenance_work_mem = '6MB';
|
|
||||||
CREATE TABLE t (val halfvec(4000));
|
|
||||||
INSERT INTO t (val) VALUES (array_fill(0, ARRAY[4000]));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val halfvec_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
|
|||||||
@@ -81,40 +81,19 @@ DROP TABLE t;
|
|||||||
CREATE TABLE t (val vector(3));
|
CREATE TABLE t (val vector(3));
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 0);
|
CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 0);
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 32769);
|
CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 32769);
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
SHOW ivfflat.probes;
|
SHOW ivfflat.probes;
|
||||||
|
|
||||||
SET ivfflat.probes = 0;
|
SET ivfflat.probes = 0;
|
||||||
SET ivfflat.probes = 32769;
|
SET ivfflat.probes = 32769;
|
||||||
|
|
||||||
SHOW ivfflat.iterative_scan;
|
SHOW ivfflat.iterative_scan;
|
||||||
|
|
||||||
SET ivfflat.iterative_scan = on;
|
SET ivfflat.iterative_scan = on;
|
||||||
|
|
||||||
SHOW ivfflat.max_probes;
|
SHOW ivfflat.max_probes;
|
||||||
|
|
||||||
SET ivfflat.max_probes = 0;
|
SET ivfflat.max_probes = 0;
|
||||||
SET ivfflat.max_probes = 32769;
|
SET ivfflat.max_probes = 32769;
|
||||||
|
|
||||||
-- dimensions
|
|
||||||
|
|
||||||
CREATE TABLE t (val vector(2000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops);
|
|
||||||
DROP TABLE t;
|
DROP TABLE t;
|
||||||
|
|
||||||
CREATE TABLE t (val vector(2001));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
|
|
||||||
-- memory
|
|
||||||
|
|
||||||
SET maintenance_work_mem = '1MB';
|
|
||||||
CREATE TABLE t (val vector(2000));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
|
|
||||||
SET maintenance_work_mem = '5MB';
|
|
||||||
CREATE TABLE t (val vector(2000));
|
|
||||||
INSERT INTO t (val) VALUES (array_fill(0, ARRAY[2000]));
|
|
||||||
CREATE INDEX ON t USING ivfflat (val vector_l2_ops);
|
|
||||||
DROP TABLE t;
|
|
||||||
RESET maintenance_work_mem;
|
|
||||||
|
|||||||
@@ -128,7 +128,6 @@ SELECT l2_normalize('[3e38]'::vector);
|
|||||||
|
|
||||||
SELECT binary_quantize('[1,0,-1]'::vector);
|
SELECT binary_quantize('[1,0,-1]'::vector);
|
||||||
SELECT binary_quantize('[0,0.1,-0.2,-0.3,0.4,0.5,0.6,-0.7,0.8,-0.9,1]'::vector);
|
SELECT binary_quantize('[0,0.1,-0.2,-0.3,0.4,0.5,0.6,-0.7,0.8,-0.9,1]'::vector);
|
||||||
SELECT binary_quantize('[1,2,3,-4,5,6,-7,8,1,-2,-3,4,5,-6,7,8,-1,2,3]'::vector);
|
|
||||||
|
|
||||||
SELECT subvector('[1,2,3,4,5]'::vector, 1, 3);
|
SELECT subvector('[1,2,3,4,5]'::vector, 1, 3);
|
||||||
SELECT subvector('[1,2,3,4,5]'::vector, 3, 2);
|
SELECT subvector('[1,2,3,4,5]'::vector, 3, 2);
|
||||||
|
|||||||
@@ -16,8 +16,17 @@ $node->safe_psql("postgres",
|
|||||||
"INSERT INTO tst SELECT i, ARRAY[random(), random(), random()] FROM generate_series(1, 100000) i;"
|
"INSERT INTO tst SELECT i, ARRAY[random(), random(), random()] FROM generate_series(1, 100000) i;"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
# Check each index type
|
||||||
|
my @operators = ("<->", "<#>", "<=>");
|
||||||
|
my @opclasses = ("vector_l2_ops", "vector_ip_ops", "vector_cosine_ops");
|
||||||
|
|
||||||
|
for my $i (0 .. $#operators)
|
||||||
|
{
|
||||||
|
my $operator = $operators[$i];
|
||||||
|
my $opclass = $opclasses[$i];
|
||||||
|
|
||||||
# Add index
|
# Add index
|
||||||
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v vector_l2_ops);");
|
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v $opclass);");
|
||||||
|
|
||||||
# Test 100% recall
|
# Test 100% recall
|
||||||
for (1 .. 20)
|
for (1 .. 20)
|
||||||
@@ -30,5 +39,6 @@ for (1 .. 20)
|
|||||||
));
|
));
|
||||||
is($res, $query);
|
is($res, $query);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
done_testing();
|
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,38 +0,0 @@
|
|||||||
use strict;
|
|
||||||
use warnings FATAL => 'all';
|
|
||||||
use PostgreSQL::Test::Cluster;
|
|
||||||
use PostgreSQL::Test::Utils;
|
|
||||||
use Test::More;
|
|
||||||
|
|
||||||
my $dim = 3;
|
|
||||||
my $array_sql = join(",", ('random()') x $dim);
|
|
||||||
|
|
||||||
# Initialize node
|
|
||||||
my $node = PostgreSQL::Test::Cluster->new('node');
|
|
||||||
$node->init;
|
|
||||||
$node->start;
|
|
||||||
|
|
||||||
# Create table and index
|
|
||||||
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
|
||||||
$node->safe_psql("postgres", "CREATE TABLE tst (i serial, v vector($dim));");
|
|
||||||
$node->safe_psql("postgres", "ALTER TABLE tst SET (autovacuum_enabled = false);");
|
|
||||||
$node->safe_psql("postgres",
|
|
||||||
"INSERT INTO tst (v) SELECT ARRAY[$array_sql] FROM generate_series(1, 1000) i;"
|
|
||||||
);
|
|
||||||
$node->safe_psql("postgres", "CREATE INDEX ON tst USING hnsw (v vector_l2_ops);");
|
|
||||||
$node->safe_psql("postgres", "DELETE FROM tst");
|
|
||||||
|
|
||||||
# Test HNSW_SCAN_LOCK at the beginning of MarkDeleted is effective
|
|
||||||
$node->pgbench(
|
|
||||||
"--no-vacuum --client=5 --transactions=1000",
|
|
||||||
0,
|
|
||||||
[qr{actually processed}],
|
|
||||||
[qr{^$}],
|
|
||||||
"concurrent SELECTs and VACUUM",
|
|
||||||
{
|
|
||||||
"046_hnsw_vacuum_scan_select\@1000" => "SELECT i FROM tst ORDER BY v <-> '[0,0,0]' LIMIT 10;",
|
|
||||||
"046_hnsw_vacuum_scan_vacuum\@1" => "VACUUM tst;"
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
done_testing();
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
use strict;
|
|
||||||
use warnings FATAL => 'all';
|
|
||||||
use PostgreSQL::Test::Cluster;
|
|
||||||
use PostgreSQL::Test::Utils;
|
|
||||||
use Test::More;
|
|
||||||
|
|
||||||
my $dim = 3;
|
|
||||||
my $array_sql = join(",", ('random()') x $dim);
|
|
||||||
|
|
||||||
# Initialize node
|
|
||||||
my $node = PostgreSQL::Test::Cluster->new('node');
|
|
||||||
$node->init;
|
|
||||||
$node->start;
|
|
||||||
|
|
||||||
# Create table and index
|
|
||||||
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
|
||||||
$node->safe_psql("postgres", "CREATE TABLE tst (i serial, v vector($dim));");
|
|
||||||
$node->safe_psql("postgres", "ALTER TABLE tst SET (autovacuum_enabled = false);");
|
|
||||||
$node->safe_psql("postgres",
|
|
||||||
"INSERT INTO tst (v) SELECT ARRAY[$array_sql] FROM generate_series(1, 1000) i;"
|
|
||||||
);
|
|
||||||
$node->safe_psql("postgres", "CREATE INDEX ON tst USING hnsw (v vector_l2_ops);");
|
|
||||||
|
|
||||||
# Test no "hnsw graph not repaired" errors
|
|
||||||
$node->pgbench(
|
|
||||||
"--no-vacuum --client=5 --transactions=1000",
|
|
||||||
0,
|
|
||||||
[qr{actually processed}],
|
|
||||||
[qr{^$}],
|
|
||||||
"concurrent INSERTs, DELETEs, SELECTs, and VACUUM",
|
|
||||||
{
|
|
||||||
"047_hnsw_vacuum_insert_insert\@500" => "INSERT INTO tst (v) VALUES (ARRAY[$array_sql]);",
|
|
||||||
"047_hnsw_vacuum_insert_delete\@500" => "DELETE FROM tst WHERE i = (SELECT i FROM tst LIMIT 1);",
|
|
||||||
"047_hnsw_vacuum_insert_select\@20" => "SELECT i FROM tst ORDER BY v <-> (SELECT ARRAY[$array_sql]::vector) LIMIT 10;",
|
|
||||||
"047_hnsw_vacuum_insert_vacuum\@1" => "VACUUM tst;"
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
done_testing();
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
use strict;
|
|
||||||
use warnings FATAL => 'all';
|
|
||||||
use PostgreSQL::Test::Cluster;
|
|
||||||
use PostgreSQL::Test::Utils;
|
|
||||||
use Test::More;
|
|
||||||
|
|
||||||
my $dim = 3;
|
|
||||||
my $array_sql = join(",", ('random()') x $dim);
|
|
||||||
|
|
||||||
# Initialize node
|
|
||||||
my $node = PostgreSQL::Test::Cluster->new('node');
|
|
||||||
$node->init;
|
|
||||||
$node->start;
|
|
||||||
|
|
||||||
# Create table and index
|
|
||||||
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
|
||||||
$node->safe_psql("postgres", "CREATE TABLE tst (i serial, v vector($dim));");
|
|
||||||
$node->safe_psql("postgres", "ALTER TABLE tst SET (autovacuum_enabled = false);");
|
|
||||||
$node->safe_psql("postgres",
|
|
||||||
"INSERT INTO tst (v) SELECT ARRAY[$array_sql] FROM generate_series(1, 1000) i;"
|
|
||||||
);
|
|
||||||
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v vector_l2_ops) WITH (lists = 10);");
|
|
||||||
|
|
||||||
# Test no errors
|
|
||||||
$node->pgbench(
|
|
||||||
"--no-vacuum --client=5 --transactions=1500",
|
|
||||||
0,
|
|
||||||
[qr{actually processed}],
|
|
||||||
[qr{^$}],
|
|
||||||
"concurrent INSERTs, DELETEs, SELECTs, and VACUUM",
|
|
||||||
{
|
|
||||||
"048_ivfflat_vacuum_insert_insert\@500" => "INSERT INTO tst (v) VALUES (ARRAY[$array_sql]);",
|
|
||||||
"048_ivfflat_vacuum_insert_delete\@500" => "DELETE FROM tst WHERE i = (SELECT i FROM tst LIMIT 1);",
|
|
||||||
"048_ivfflat_vacuum_insert_select\@500" => "SELECT i FROM tst ORDER BY v <-> (SELECT ARRAY[$array_sql]::vector) LIMIT 10;",
|
|
||||||
"048_ivfflat_vacuum_insert_vacuum\@1" => "VACUUM tst;"
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
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.8.5'
|
default_version = '0.8.0'
|
||||||
module_pathname = '$libdir/vector'
|
module_pathname = '$libdir/vector'
|
||||||
relocatable = true
|
relocatable = true
|
||||||
|
|||||||
Reference in New Issue
Block a user