mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-26 13:52:42 +08:00
Compare commits
20 Commits
ci-alpine
...
hnsw-read-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90aaf2102b | ||
|
|
a7551a61ca | ||
|
|
5c9a97af3b | ||
|
|
8af675cd25 | ||
|
|
97e0ed4464 | ||
|
|
533ad160e0 | ||
|
|
dfbd724a1f | ||
|
|
5f270c9663 | ||
|
|
352ec5be29 | ||
|
|
17916cad00 | ||
|
|
284f2f0fec | ||
|
|
edd49863ba | ||
|
|
411291189d | ||
|
|
cab9da72c0 | ||
|
|
d702ee31f2 | ||
|
|
9ed39fb94b | ||
|
|
b7e680d41a | ||
|
|
f5841f07fd | ||
|
|
35ab919bf5 | ||
|
|
5c08be0291 |
246
.github/workflows/build.yml
vendored
246
.github/workflows/build.yml
vendored
@@ -1,120 +1,120 @@
|
|||||||
name: build
|
name: build
|
||||||
on: [push, pull_request]
|
on: [push, pull_request]
|
||||||
jobs:
|
jobs:
|
||||||
# ubuntu:
|
ubuntu:
|
||||||
# runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
# if: ${{ !startsWith(github.ref_name, 'mac') && !startsWith(github.ref_name, 'windows') }}
|
if: ${{ !startsWith(github.ref_name, 'mac') && !startsWith(github.ref_name, 'windows') }}
|
||||||
# strategy:
|
strategy:
|
||||||
# fail-fast: false
|
fail-fast: false
|
||||||
# matrix:
|
matrix:
|
||||||
# include:
|
include:
|
||||||
# - postgres: 19
|
- postgres: 19
|
||||||
# os: ubuntu-24.04
|
os: ubuntu-24.04
|
||||||
# - postgres: 18
|
- postgres: 18
|
||||||
# os: ubuntu-24.04
|
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-24.04-arm
|
||||||
# - postgres: 15
|
- postgres: 15
|
||||||
# os: ubuntu-22.04
|
os: ubuntu-22.04
|
||||||
# - postgres: 14
|
- postgres: 14
|
||||||
# os: ubuntu-22.04-arm
|
os: ubuntu-22.04-arm
|
||||||
# - postgres: 13
|
- postgres: 13
|
||||||
# os: ubuntu-22.04
|
os: ubuntu-22.04
|
||||||
# steps:
|
steps:
|
||||||
# - uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
# - 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 ${{ matrix.postgres >= 18 && '-Wno-missing-field-initializers' || '' }}
|
||||||
# - 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
|
||||||
# - run: make installcheck
|
- run: make installcheck
|
||||||
# - if: ${{ failure() }}
|
- if: ${{ failure() }}
|
||||||
# run: cat regression.diffs
|
run: cat regression.diffs
|
||||||
# - run: |
|
- run: |
|
||||||
# sudo apt-get update
|
sudo apt-get update
|
||||||
# sudo apt-get install libipc-run-perl
|
sudo apt-get install libipc-run-perl
|
||||||
# - run: make prove_installcheck
|
- run: make prove_installcheck
|
||||||
# mac:
|
mac:
|
||||||
# runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
# if: ${{ !startsWith(github.ref_name, 'windows') }}
|
if: ${{ !startsWith(github.ref_name, 'windows') }}
|
||||||
# strategy:
|
strategy:
|
||||||
# fail-fast: false
|
fail-fast: false
|
||||||
# matrix:
|
matrix:
|
||||||
# include:
|
include:
|
||||||
# - postgres: 18
|
- postgres: 18
|
||||||
# os: macos-26
|
os: macos-26
|
||||||
# - postgres: 14
|
- postgres: 14
|
||||||
# os: macos-15-intel
|
os: macos-15-intel
|
||||||
# steps:
|
steps:
|
||||||
# - uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
# - 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 -Wno-unknown-warning-option ${{ matrix.postgres >= 18 && '-Wno-missing-field-initializers' || '' }}
|
||||||
# - run: make install
|
- run: make install
|
||||||
# - run: make installcheck
|
- run: make installcheck
|
||||||
# - if: ${{ failure() }}
|
- if: ${{ failure() }}
|
||||||
# run: cat regression.diffs
|
run: cat regression.diffs
|
||||||
# # Homebrew Postgres does not enable TAP tests, so need to download
|
# Homebrew Postgres does not enable TAP tests, so need to download
|
||||||
# - run: |
|
- run: |
|
||||||
# brew install cpanm
|
brew install cpanm
|
||||||
# cpanm --notest IPC::Run
|
cpanm --notest IPC::Run
|
||||||
# wget -q https://github.com/postgres/postgres/archive/refs/tags/$TAG.tar.gz
|
wget -q https://github.com/postgres/postgres/archive/refs/tags/$TAG.tar.gz
|
||||||
# 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_0' || 'REL_14_19' }}
|
TAG: ${{ matrix.postgres == 18 && 'REL_18_2' || 'REL_14_21' }}
|
||||||
# - 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-26' && 20 || 18 }}
|
||||||
# PG_CFLAGS: -DUSE_ASSERT_CHECKING
|
PG_CFLAGS: -DUSE_ASSERT_CHECKING
|
||||||
# windows:
|
windows:
|
||||||
# runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
# if: ${{ !startsWith(github.ref_name, 'mac') }}
|
if: ${{ !startsWith(github.ref_name, 'mac') }}
|
||||||
# strategy:
|
strategy:
|
||||||
# fail-fast: false
|
fail-fast: false
|
||||||
# matrix:
|
matrix:
|
||||||
# include:
|
include:
|
||||||
# - postgres: 17
|
- postgres: 17
|
||||||
# os: windows-2025
|
os: windows-2025
|
||||||
# - postgres: 14
|
- postgres: 14
|
||||||
# os: windows-2022
|
os: windows-2022
|
||||||
# steps:
|
steps:
|
||||||
# - uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
# - uses: ankane/setup-postgres@v1
|
- uses: ankane/setup-postgres@v1
|
||||||
# with:
|
with:
|
||||||
# postgres-version: ${{ matrix.postgres }}
|
postgres-version: ${{ matrix.postgres }}
|
||||||
# - run: |
|
- run: |
|
||||||
# call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && ^
|
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && ^
|
||||||
# nmake /NOLOGO /F Makefile.win && ^
|
nmake /NOLOGO /F Makefile.win && ^
|
||||||
# nmake /NOLOGO /F Makefile.win install && ^
|
nmake /NOLOGO /F Makefile.win install && ^
|
||||||
# nmake /NOLOGO /F Makefile.win installcheck ${{ matrix.postgres != 17 && 'PG_REGRESS=$(PGROOT)\bin\pg_regress' || '' }} && ^
|
nmake /NOLOGO /F Makefile.win installcheck ${{ matrix.postgres != 17 && 'PG_REGRESS=$(PGROOT)\bin\pg_regress' || '' }} && ^
|
||||||
# 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
|
||||||
# - if: ${{ failure() }}
|
- if: ${{ failure() }}
|
||||||
# run: cat regression.diffs
|
run: cat regression.diffs
|
||||||
i386:
|
i386:
|
||||||
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
|
||||||
container:
|
container:
|
||||||
image: alpine:3.23
|
image: debian:12
|
||||||
options: --platform linux/386
|
options: --platform linux/386
|
||||||
steps:
|
steps:
|
||||||
- run: apk add --update build-base git openrc perl perl-ipc-run perl-test-harness-utils postgresql18 postgresql18-dev
|
- run: apt-get update && apt-get install -y build-essential git libipc-run-perl postgresql-15 postgresql-server-dev-15 sudo
|
||||||
- run: mkdir /run/openrc && touch /run/openrc/softlevel && rc-update add postgresql && rc-service postgresql start
|
- run: service postgresql start
|
||||||
- run: |
|
- run: |
|
||||||
git clone https://github.com/${{ github.repository }}.git pgvector
|
git clone https://github.com/${{ github.repository }}.git pgvector
|
||||||
cd pgvector
|
cd pgvector
|
||||||
@@ -123,21 +123,21 @@ jobs:
|
|||||||
make
|
make
|
||||||
make install
|
make install
|
||||||
chown -R postgres .
|
chown -R postgres .
|
||||||
su postgres -c "make installcheck"
|
sudo -u postgres make installcheck
|
||||||
su postgres -c "make prove_installcheck"
|
sudo -u postgres make prove_installcheck
|
||||||
env:
|
env:
|
||||||
PG_CFLAGS: -DUSE_ASSERT_CHECKING -Wall -Wextra -Werror -Wno-unused-parameter -Wno-sign-compare -Wno-missing-field-initializers
|
PG_CFLAGS: -DUSE_ASSERT_CHECKING -Wall -Wextra -Werror -Wno-unused-parameter -Wno-sign-compare
|
||||||
- if: ${{ failure() }}
|
- if: ${{ failure() }}
|
||||||
run: cat pgvector/regression.diffs
|
run: cat pgvector/regression.diffs
|
||||||
# valgrind:
|
valgrind:
|
||||||
# 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@v5
|
- uses: actions/checkout@v6
|
||||||
# - uses: ankane/setup-postgres-valgrind@v1
|
- uses: ankane/setup-postgres-valgrind@v1
|
||||||
# with:
|
with:
|
||||||
# postgres-version: 18
|
postgres-version: 18
|
||||||
# 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
|
||||||
# - run: make installcheck
|
- run: make installcheck
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
## 0.8.2 (unreleased)
|
## 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
|
- Improved `install` target on Windows
|
||||||
- Fixed `Index Searches` in `EXPLAIN` output for Postgres 18
|
- Fixed `Index Searches` in `EXPLAIN` output for Postgres 18
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ ARG DEBIAN_CODENAME=bookworm
|
|||||||
FROM postgres:$PG_MAJOR-$DEBIAN_CODENAME
|
FROM postgres:$PG_MAJOR-$DEBIAN_CODENAME
|
||||||
ARG PG_MAJOR
|
ARG PG_MAJOR
|
||||||
|
|
||||||
ADD https://github.com/pgvector/pgvector.git#v0.8.1 /tmp/pgvector
|
ADD https://github.com/pgvector/pgvector.git#v0.8.2 /tmp/pgvector
|
||||||
|
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
apt-mark hold locales && \
|
apt-mark hold locales && \
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "vector",
|
"name": "vector",
|
||||||
"abstract": "Open-source vector similarity search for Postgres",
|
"abstract": "Open-source vector similarity search for Postgres",
|
||||||
"description": "Supports L2 distance, inner product, and cosine distance",
|
"description": "Supports L2 distance, inner product, and cosine distance",
|
||||||
"version": "0.8.1",
|
"version": "0.8.2",
|
||||||
"maintainer": [
|
"maintainer": [
|
||||||
"Andrew Kane <andrew@ankane.org>"
|
"Andrew Kane <andrew@ankane.org>"
|
||||||
],
|
],
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
"vector": {
|
"vector": {
|
||||||
"file": "sql/vector.sql",
|
"file": "sql/vector.sql",
|
||||||
"docfile": "README.md",
|
"docfile": "README.md",
|
||||||
"version": "0.8.1",
|
"version": "0.8.2",
|
||||||
"abstract": "Open-source vector similarity search for Postgres"
|
"abstract": "Open-source vector similarity search for Postgres"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -1,5 +1,5 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.8.1
|
EXTVERSION = 0.8.2
|
||||||
|
|
||||||
MODULE_big = vector
|
MODULE_big = vector
|
||||||
DATA = $(wildcard sql/*--*--*.sql)
|
DATA = $(wildcard sql/*--*--*.sql)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.8.1
|
EXTVERSION = 0.8.2
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
30
README.md
30
README.md
@@ -21,7 +21,7 @@ Compile and install the extension (supports Postgres 13+)
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd /tmp
|
cd /tmp
|
||||||
git clone --branch v0.8.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.8.2 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
make
|
make
|
||||||
make install # may need sudo
|
make install # may need sudo
|
||||||
@@ -38,7 +38,7 @@ Ensure [C++ support in Visual Studio](https://learn.microsoft.com/en-us/cpp/buil
|
|||||||
```cmd
|
```cmd
|
||||||
set "PGROOT=C:\Program Files\PostgreSQL\18"
|
set "PGROOT=C:\Program Files\PostgreSQL\18"
|
||||||
cd %TEMP%
|
cd %TEMP%
|
||||||
git clone --branch v0.8.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.8.2 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
nmake /F Makefile.win
|
nmake /F Makefile.win
|
||||||
nmake /F Makefile.win install
|
nmake /F Makefile.win install
|
||||||
@@ -1152,23 +1152,23 @@ This adds pgvector to the [Postgres image](https://hub.docker.com/_/postgres) (r
|
|||||||
|
|
||||||
Supported tags are:
|
Supported tags are:
|
||||||
|
|
||||||
- `pg18-trixie`, `0.8.1-pg18-trixie`
|
- `pg18-trixie`, `0.8.2-pg18-trixie`
|
||||||
- `pg18-bookworm`, `0.8.1-pg18-bookworm`, `pg18`, `0.8.1-pg18`
|
- `pg18-bookworm`, `0.8.2-pg18-bookworm`, `pg18`, `0.8.2-pg18`
|
||||||
- `pg17-trixie`, `0.8.1-pg17-trixie`
|
- `pg17-trixie`, `0.8.2-pg17-trixie`
|
||||||
- `pg17-bookworm`, `0.8.1-pg17-bookworm`, `pg17`, `0.8.1-pg17`
|
- `pg17-bookworm`, `0.8.2-pg17-bookworm`, `pg17`, `0.8.2-pg17`
|
||||||
- `pg16-trixie`, `0.8.1-pg16-trixie`
|
- `pg16-trixie`, `0.8.2-pg16-trixie`
|
||||||
- `pg16-bookworm`, `0.8.1-pg16-bookworm`, `pg16`, `0.8.1-pg16`
|
- `pg16-bookworm`, `0.8.2-pg16-bookworm`, `pg16`, `0.8.2-pg16`
|
||||||
- `pg15-trixie`, `0.8.1-pg15-trixie`
|
- `pg15-trixie`, `0.8.2-pg15-trixie`
|
||||||
- `pg15-bookworm`, `0.8.1-pg15-bookworm`, `pg15`, `0.8.1-pg15`
|
- `pg15-bookworm`, `0.8.2-pg15-bookworm`, `pg15`, `0.8.2-pg15`
|
||||||
- `pg14-trixie`, `0.8.1-pg14-trixie`
|
- `pg14-trixie`, `0.8.2-pg14-trixie`
|
||||||
- `pg14-bookworm`, `0.8.1-pg14-bookworm`, `pg14`, `0.8.1-pg14`
|
- `pg14-bookworm`, `0.8.2-pg14-bookworm`, `pg14`, `0.8.2-pg14`
|
||||||
- `pg13-trixie`, `0.8.1-pg13-trixie`
|
- `pg13-trixie`, `0.8.2-pg13-trixie`
|
||||||
- `pg13-bookworm`, `0.8.1-pg13-bookworm`, `pg13`, `0.8.1-pg13`
|
- `pg13-bookworm`, `0.8.2-pg13-bookworm`, `pg13`, `0.8.2-pg13`
|
||||||
|
|
||||||
You can also build the image manually:
|
You can also build the image manually:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone --branch v0.8.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.8.2 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=18 -t myuser/pgvector .
|
||||||
```
|
```
|
||||||
|
|||||||
2
sql/vector--0.8.1--0.8.2.sql
Normal file
2
sql/vector--0.8.1--0.8.2.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||||
|
\echo Use "ALTER EXTENSION vector UPDATE TO '0.8.2'" to load this file. \quit
|
||||||
@@ -169,7 +169,7 @@ BitJaccardDistanceAvx512Popcount(uint32 bytes, unsigned char *ax, unsigned char
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
TARGET_XSAVE static bool
|
TARGET_XSAVE static bool
|
||||||
SupportsAvx512Popcount()
|
SupportsAvx512Popcount(void)
|
||||||
{
|
{
|
||||||
unsigned int exx[4] = {0, 0, 0, 0};
|
unsigned int exx[4] = {0, 0, 0, 0};
|
||||||
|
|
||||||
|
|||||||
60
src/hnsw.c
60
src/hnsw.c
@@ -13,6 +13,7 @@
|
|||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
#include "nodes/pg_list.h"
|
#include "nodes/pg_list.h"
|
||||||
|
#include "storage/lwlock.h"
|
||||||
#include "utils/float.h"
|
#include "utils/float.h"
|
||||||
#include "utils/guc.h"
|
#include "utils/guc.h"
|
||||||
#include "utils/relcache.h"
|
#include "utils/relcache.h"
|
||||||
@@ -266,6 +267,64 @@ 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;
|
||||||
@@ -339,4 +398,5 @@ hnswhandler(PG_FUNCTION_ARGS)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
PG_RETURN_POINTER(amroutine);
|
PG_RETURN_POINTER(amroutine);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/hnsw.h
15
src/hnsw.h
@@ -10,6 +10,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/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"
|
||||||
@@ -368,6 +372,13 @@ typedef union
|
|||||||
ItemPointerData indextid;
|
ItemPointerData indextid;
|
||||||
} HnswUnvisited;
|
} HnswUnvisited;
|
||||||
|
|
||||||
|
typedef struct HnswReadStreamData
|
||||||
|
{
|
||||||
|
HnswUnvisited *unvisited;
|
||||||
|
int unvisitedLength;
|
||||||
|
int visited;
|
||||||
|
} HnswReadStreamData;
|
||||||
|
|
||||||
typedef struct HnswScanOpaqueData
|
typedef struct HnswScanOpaqueData
|
||||||
{
|
{
|
||||||
const HnswTypeInfo *typeInfo;
|
const HnswTypeInfo *typeInfo;
|
||||||
@@ -423,13 +434,13 @@ bool HnswCheckNorm(HnswSupport * support, Datum value);
|
|||||||
Buffer HnswNewBuffer(Relation index, ForkNumber forkNum);
|
Buffer HnswNewBuffer(Relation index, ForkNumber forkNum);
|
||||||
void HnswInitPage(Buffer buf, Page page);
|
void HnswInitPage(Buffer buf, Page page);
|
||||||
void HnswInit(void);
|
void HnswInit(void);
|
||||||
List *HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation index, HnswSupport * support, int m, bool inserting, HnswElement skipElement, visited_hash * v, pairingheap **discarded, bool initVisited, int64 *tuples);
|
List *HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation index, HnswSupport * support, int m, bool inserting, HnswElement skipElement, visited_hash * v, pairingheap **discarded, bool initVisited, int64 *tuples, bool maintenance);
|
||||||
HnswElement HnswGetEntryPoint(Relation index);
|
HnswElement HnswGetEntryPoint(Relation index);
|
||||||
void HnswGetMetaPageInfo(Relation index, int *m, HnswElement * entryPoint);
|
void HnswGetMetaPageInfo(Relation index, int *m, HnswElement * entryPoint);
|
||||||
void *HnswAlloc(HnswAllocator * allocator, Size size);
|
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, bool maintenance);
|
||||||
HnswSearchCandidate *HnswEntryCandidate(char *base, HnswElement entryPoint, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec);
|
HnswSearchCandidate *HnswEntryCandidate(char *base, HnswElement entryPoint, HnswQuery * q, Relation index, 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);
|
||||||
|
|||||||
@@ -36,6 +36,8 @@
|
|||||||
*/
|
*/
|
||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
#include "access/genam.h"
|
||||||
#include "access/parallel.h"
|
#include "access/parallel.h"
|
||||||
#include "access/relscan.h"
|
#include "access/relscan.h"
|
||||||
@@ -52,6 +54,7 @@
|
|||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "optimizer/optimizer.h"
|
#include "optimizer/optimizer.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
|
#include "storage/condition_variable.h"
|
||||||
#include "tcop/tcopprot.h"
|
#include "tcop/tcopprot.h"
|
||||||
#include "utils/datum.h"
|
#include "utils/datum.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
@@ -77,6 +80,8 @@
|
|||||||
#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
|
||||||
*/
|
*/
|
||||||
@@ -465,7 +470,7 @@ InsertTupleInMemory(HnswBuildState * buildstate, HnswElement element)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Find neighbors for element */
|
/* Find neighbors for element */
|
||||||
HnswFindElementNeighbors(base, element, entryPoint, NULL, support, m, efConstruction, false);
|
HnswFindElementNeighbors(base, element, entryPoint, NULL, support, m, efConstruction, false, true);
|
||||||
|
|
||||||
/* Update graph in memory */
|
/* Update graph in memory */
|
||||||
UpdateGraphInMemory(support, element, m, entryPoint, buildstate);
|
UpdateGraphInMemory(support, element, m, entryPoint, buildstate);
|
||||||
@@ -489,6 +494,7 @@ 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))
|
||||||
@@ -497,6 +503,9 @@ 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);
|
||||||
|
|
||||||
@@ -518,7 +527,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 >= graph->memoryTotal)
|
if (graph->memoryUsed + memoryMargin >= graph->memoryTotal)
|
||||||
{
|
{
|
||||||
LWLockRelease(&graph->allocatorLock);
|
LWLockRelease(&graph->allocatorLock);
|
||||||
|
|
||||||
@@ -611,7 +620,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 = memoryTotal;
|
graph->memoryTotal = Min(memoryTotal, HNSW_MAX_GRAPH_MEMORY);
|
||||||
graph->flushed = false;
|
graph->flushed = false;
|
||||||
graph->indtuples = 0;
|
graph->indtuples = 0;
|
||||||
SpinLockInit(&graph->lock);
|
SpinLockInit(&graph->lock);
|
||||||
@@ -652,9 +661,17 @@ static void *
|
|||||||
HnswSharedMemoryAlloc(Size size, void *state)
|
HnswSharedMemoryAlloc(Size size, void *state)
|
||||||
{
|
{
|
||||||
HnswBuildState *buildstate = (HnswBuildState *) state;
|
HnswBuildState *buildstate = (HnswBuildState *) state;
|
||||||
void *chunk = buildstate->hnswarea + buildstate->graph->memoryUsed;
|
Size alignedSize = MAXALIGN(size);
|
||||||
|
void *chunk;
|
||||||
|
|
||||||
buildstate->graph->memoryUsed += MAXALIGN(size);
|
if (alignedSize > 1024 * 1024)
|
||||||
|
elog(ERROR, "hnsw allocation too large");
|
||||||
|
|
||||||
|
if (buildstate->graph->memoryUsed + alignedSize > buildstate->graph->memoryTotal)
|
||||||
|
elog(ERROR, "hnsw allocator out of memory");
|
||||||
|
|
||||||
|
chunk = buildstate->hnswarea + buildstate->graph->memoryUsed;
|
||||||
|
buildstate->graph->memoryUsed += alignedSize;
|
||||||
return chunk;
|
return chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -786,7 +803,11 @@ HnswParallelScanAndInsert(Relation heapRel, Relation indexRel, HnswShared * hnsw
|
|||||||
buildstate.hnswarea = hnswarea;
|
buildstate.hnswarea = hnswarea;
|
||||||
InitAllocator(&buildstate.allocator, &HnswSharedMemoryAlloc, &buildstate);
|
InitAllocator(&buildstate.allocator, &HnswSharedMemoryAlloc, &buildstate);
|
||||||
scan = table_beginscan_parallel(heapRel,
|
scan = table_beginscan_parallel(heapRel,
|
||||||
ParallelTableScanFromHnswShared(hnswshared));
|
ParallelTableScanFromHnswShared(hnswshared)
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
,SO_NONE
|
||||||
|
#endif
|
||||||
|
);
|
||||||
reltuples = table_index_build_scan(heapRel, indexRel, indexInfo,
|
reltuples = table_index_build_scan(heapRel, indexRel, indexInfo,
|
||||||
true, progress, BuildCallback,
|
true, progress, BuildCallback,
|
||||||
(void *) &buildstate, scan);
|
(void *) &buildstate, scan);
|
||||||
@@ -940,6 +961,8 @@ HnswBeginParallel(HnswBuildState * buildstate, bool isconcurrent, int request)
|
|||||||
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);
|
||||||
|
|
||||||
@@ -982,8 +1005,7 @@ 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);
|
||||||
/* Report less than allocated so never fails */
|
InitGraph(&hnswshared->graphData, hnswarea, esthnswarea);
|
||||||
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
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "storage/lmgr.h"
|
#include "storage/lmgr.h"
|
||||||
|
#include "storage/lwlock.h"
|
||||||
#include "utils/datum.h"
|
#include "utils/datum.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
#include "utils/rel.h"
|
||||||
@@ -730,7 +731,7 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, Datum value, ItemPo
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Find neighbors for element */
|
/* Find neighbors for element */
|
||||||
HnswFindElementNeighbors(base, element, entryPoint, index, support, m, efConstruction, false);
|
HnswFindElementNeighbors(base, element, entryPoint, index, support, m, efConstruction, false, building);
|
||||||
|
|
||||||
/* Update graph on disk */
|
/* Update graph on disk */
|
||||||
UpdateGraphOnDisk(index, support, element, m, entryPoint, building);
|
UpdateGraphOnDisk(index, support, element, m, entryPoint, building);
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "access/genam.h"
|
#include "access/genam.h"
|
||||||
#include "access/relscan.h"
|
#include "access/relscan.h"
|
||||||
#include "hnsw.h"
|
#include "hnsw.h"
|
||||||
@@ -46,11 +48,11 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
|
|
||||||
for (int lc = entryPoint->level; lc >= 1; lc--)
|
for (int lc = entryPoint->level; lc >= 1; lc--)
|
||||||
{
|
{
|
||||||
w = HnswSearchLayer(base, q, ep, 1, lc, index, support, m, false, NULL, NULL, NULL, true, NULL);
|
w = HnswSearchLayer(base, q, ep, 1, lc, index, support, m, false, NULL, NULL, NULL, true, NULL, false);
|
||||||
ep = w;
|
ep = w;
|
||||||
}
|
}
|
||||||
|
|
||||||
return HnswSearchLayer(base, q, ep, hnsw_ef_search, 0, index, support, m, false, NULL, &so->v, hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF ? &so->discarded : NULL, true, &so->tuples);
|
return HnswSearchLayer(base, q, ep, hnsw_ef_search, 0, index, support, m, false, NULL, &so->v, hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF ? &so->discarded : NULL, true, &so->tuples, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -81,7 +83,7 @@ ResumeScanItems(IndexScanDesc scan)
|
|||||||
ep = lappend(ep, sc);
|
ep = lappend(ep, sc);
|
||||||
}
|
}
|
||||||
|
|
||||||
return HnswSearchLayer(base, &so->q, ep, batch_size, 0, index, &so->support, so->m, false, NULL, &so->v, &so->discarded, false, &so->tuples);
|
return HnswSearchLayer(base, &so->q, ep, batch_size, 0, index, &so->support, so->m, false, NULL, &so->v, &so->discarded, false, &so->tuples, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -151,7 +153,7 @@ hnswbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
/* Calculate max memory */
|
/* Calculate max memory */
|
||||||
/* Add 256 extra bytes to fill last block when close */
|
/* Add 256 extra bytes to fill last block when close */
|
||||||
maxMemory = (double) work_mem * hnsw_scan_mem_multiplier * 1024.0 + 256;
|
maxMemory = (double) work_mem * hnsw_scan_mem_multiplier * 1024.0 + 256;
|
||||||
so->maxMemory = Min(maxMemory, (double) SIZE_MAX);
|
so->maxMemory = Min(maxMemory, (double) (SIZE_MAX / 2));
|
||||||
|
|
||||||
scan->opaque = so;
|
scan->opaque = so;
|
||||||
|
|
||||||
|
|||||||
104
src/hnswutils.c
104
src/hnswutils.c
@@ -21,6 +21,10 @@
|
|||||||
#include "varatt.h"
|
#include "varatt.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
#include "storage/read_stream.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#if PG_VERSION_NUM < 170000
|
#if PG_VERSION_NUM < 170000
|
||||||
static inline uint64
|
static inline uint64
|
||||||
murmurhash64(uint64 data)
|
murmurhash64(uint64 data)
|
||||||
@@ -531,14 +535,12 @@ HnswGetDistance(Datum a, Datum b, HnswSupport * support)
|
|||||||
* Load an element and optionally get its distance from q
|
* Load an element and optionally get its distance from q
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec, double *maxDistance, HnswElement * element)
|
HnswLoadElementImpl(Buffer buf, OffsetNumber offno, double *distance, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec, double *maxDistance, HnswElement * element)
|
||||||
{
|
{
|
||||||
Buffer buf;
|
|
||||||
Page page;
|
Page page;
|
||||||
HnswElementTuple etup;
|
HnswElementTuple etup;
|
||||||
|
|
||||||
/* Read vector */
|
/* Read vector */
|
||||||
buf = ReadBuffer(index, blkno);
|
|
||||||
LockBuffer(buf, BUFFER_LOCK_SHARE);
|
LockBuffer(buf, BUFFER_LOCK_SHARE);
|
||||||
page = BufferGetPage(buf);
|
page = BufferGetPage(buf);
|
||||||
|
|
||||||
@@ -559,7 +561,7 @@ HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, Hns
|
|||||||
if (distance == NULL || maxDistance == NULL || *distance < *maxDistance)
|
if (distance == NULL || maxDistance == NULL || *distance < *maxDistance)
|
||||||
{
|
{
|
||||||
if (*element == NULL)
|
if (*element == NULL)
|
||||||
*element = HnswInitElementFromBlock(blkno, offno);
|
*element = HnswInitElementFromBlock(BufferGetBlockNumber(buf), offno);
|
||||||
|
|
||||||
HnswLoadElementFromTuple(*element, etup, true, loadVec);
|
HnswLoadElementFromTuple(*element, etup, true, loadVec);
|
||||||
}
|
}
|
||||||
@@ -573,7 +575,9 @@ HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, Hns
|
|||||||
void
|
void
|
||||||
HnswLoadElement(HnswElement element, double *distance, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec, double *maxDistance)
|
HnswLoadElement(HnswElement element, double *distance, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec, double *maxDistance)
|
||||||
{
|
{
|
||||||
HnswLoadElementImpl(element->blkno, element->offno, distance, q, index, support, loadVec, maxDistance, &element);
|
Buffer buf = ReadBuffer(index, element->blkno);
|
||||||
|
|
||||||
|
HnswLoadElementImpl(buf, element->offno, distance, q, index, support, loadVec, maxDistance, &element);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -813,11 +817,31 @@ HnswLoadUnvisitedFromDisk(HnswElement element, HnswUnvisited * unvisited, int *u
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
/*
|
||||||
|
* Get next block number for read stream
|
||||||
|
*/
|
||||||
|
static BlockNumber
|
||||||
|
HnswReadStreamNextBlock(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
|
||||||
|
{
|
||||||
|
HnswReadStreamData *streamData = callback_private_data;
|
||||||
|
OffsetNumber *offno = per_buffer_data;
|
||||||
|
HnswUnvisited *uv;
|
||||||
|
|
||||||
|
if (streamData->visited == streamData->unvisitedLength)
|
||||||
|
return InvalidBlockNumber;
|
||||||
|
|
||||||
|
uv = &streamData->unvisited[streamData->visited++];
|
||||||
|
*offno = ItemPointerGetOffsetNumber(&uv->indextid);
|
||||||
|
return ItemPointerGetBlockNumber(&uv->indextid);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Algorithm 2 from paper
|
* Algorithm 2 from paper
|
||||||
*/
|
*/
|
||||||
List *
|
List *
|
||||||
HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation index, HnswSupport * support, int m, bool inserting, HnswElement skipElement, visited_hash * v, pairingheap **discarded, bool initVisited, int64 *tuples)
|
HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation index, HnswSupport * support, int m, bool inserting, HnswElement skipElement, visited_hash * v, pairingheap **discarded, bool initVisited, int64 *tuples, bool maintenance)
|
||||||
{
|
{
|
||||||
List *w = NIL;
|
List *w = NIL;
|
||||||
pairingheap *C = pairingheap_allocate(CompareNearestCandidates, NULL);
|
pairingheap *C = pairingheap_allocate(CompareNearestCandidates, NULL);
|
||||||
@@ -832,6 +856,21 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
|
|||||||
int unvisitedLength;
|
int unvisitedLength;
|
||||||
bool inMemory = index == NULL;
|
bool inMemory = index == NULL;
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
HnswReadStreamData streamData;
|
||||||
|
ReadStream *stream = NULL;
|
||||||
|
|
||||||
|
if (!inMemory)
|
||||||
|
{
|
||||||
|
int flags = READ_STREAM_USE_BATCHING;
|
||||||
|
|
||||||
|
if (maintenance)
|
||||||
|
flags |= READ_STREAM_MAINTENANCE;
|
||||||
|
|
||||||
|
stream = read_stream_begin_relation(flags, NULL, index, MAIN_FORKNUM, HnswReadStreamNextBlock, &streamData, sizeof(OffsetNumber));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (v == NULL)
|
if (v == NULL)
|
||||||
{
|
{
|
||||||
v = &vh;
|
v = &vh;
|
||||||
@@ -894,13 +933,23 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
|
|||||||
if (inMemory)
|
if (inMemory)
|
||||||
HnswLoadUnvisitedFromMemory(base, cElement, unvisited, &unvisitedLength, v, lc, localNeighborhood, neighborhoodSize);
|
HnswLoadUnvisitedFromMemory(base, cElement, unvisited, &unvisitedLength, v, lc, localNeighborhood, neighborhoodSize);
|
||||||
else
|
else
|
||||||
|
{
|
||||||
HnswLoadUnvisitedFromDisk(cElement, unvisited, &unvisitedLength, v, index, m, lm, lc);
|
HnswLoadUnvisitedFromDisk(cElement, unvisited, &unvisitedLength, v, index, m, lm, lc);
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
read_stream_resume(stream);
|
||||||
|
|
||||||
|
streamData.unvisited = unvisited;
|
||||||
|
streamData.unvisitedLength = unvisitedLength;
|
||||||
|
streamData.visited = 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/* OK to count elements instead of tuples */
|
/* OK to count elements instead of tuples */
|
||||||
if (tuples != NULL)
|
if (tuples != NULL)
|
||||||
(*tuples) += unvisitedLength;
|
(*tuples) += unvisitedLength;
|
||||||
|
|
||||||
for (int i = 0; i < unvisitedLength; i++)
|
for (int i = 0;; i++)
|
||||||
{
|
{
|
||||||
HnswElement eElement;
|
HnswElement eElement;
|
||||||
HnswSearchCandidate *e;
|
HnswSearchCandidate *e;
|
||||||
@@ -911,18 +960,40 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
|
|||||||
|
|
||||||
if (inMemory)
|
if (inMemory)
|
||||||
{
|
{
|
||||||
|
if (i == unvisitedLength)
|
||||||
|
break;
|
||||||
|
|
||||||
eElement = unvisited[i].element;
|
eElement = unvisited[i].element;
|
||||||
eDistance = GetElementDistance(base, eElement, q, support);
|
eDistance = GetElementDistance(base, eElement, q, support);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ItemPointer indextid = &unvisited[i].indextid;
|
Buffer buf;
|
||||||
BlockNumber blkno = ItemPointerGetBlockNumber(indextid);
|
OffsetNumber offno;
|
||||||
OffsetNumber offno = ItemPointerGetOffsetNumber(indextid);
|
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
void *offnoPtr;
|
||||||
|
|
||||||
|
buf = read_stream_next_buffer(stream, &offnoPtr);
|
||||||
|
|
||||||
|
if (!BufferIsValid(buf))
|
||||||
|
break;
|
||||||
|
|
||||||
|
offno = *((OffsetNumber *) offnoPtr);
|
||||||
|
#else
|
||||||
|
ItemPointer indextid;
|
||||||
|
|
||||||
|
if (i == unvisitedLength)
|
||||||
|
break;
|
||||||
|
|
||||||
|
indextid = &unvisited[i].indextid;
|
||||||
|
buf = ReadBuffer(index, ItemPointerGetBlockNumber(indextid));
|
||||||
|
offno = ItemPointerGetOffsetNumber(indextid);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Avoid any allocations if not adding */
|
/* Avoid any allocations if not adding */
|
||||||
eElement = NULL;
|
eElement = NULL;
|
||||||
HnswLoadElementImpl(blkno, offno, &eDistance, q, index, support, inserting, alwaysAdd || discarded != NULL ? NULL : &f->distance, &eElement);
|
HnswLoadElementImpl(buf, offno, &eDistance, q, index, support, inserting, alwaysAdd || discarded != NULL ? NULL : &f->distance, &eElement);
|
||||||
|
|
||||||
if (eElement == NULL)
|
if (eElement == NULL)
|
||||||
continue;
|
continue;
|
||||||
@@ -978,6 +1049,11 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
|
|||||||
w = lappend(w, sc);
|
w = lappend(w, sc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
if (!inMemory)
|
||||||
|
read_stream_end(stream);
|
||||||
|
#endif
|
||||||
|
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1273,7 +1349,7 @@ PrecomputeHash(char *base, HnswElement element)
|
|||||||
* Algorithm 1 from paper
|
* Algorithm 1 from paper
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint, Relation index, HnswSupport * support, int m, int efConstruction, bool existing)
|
HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint, Relation index, HnswSupport * support, int m, int efConstruction, bool existing, bool maintenance)
|
||||||
{
|
{
|
||||||
List *ep;
|
List *ep;
|
||||||
List *w;
|
List *w;
|
||||||
@@ -1300,7 +1376,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
|
|||||||
/* 1st phase: greedy search to insert level */
|
/* 1st phase: greedy search to insert level */
|
||||||
for (int lc = entryLevel; lc >= level + 1; lc--)
|
for (int lc = entryLevel; lc >= level + 1; lc--)
|
||||||
{
|
{
|
||||||
w = HnswSearchLayer(base, &q, ep, 1, lc, index, support, m, true, skipElement, NULL, NULL, true, NULL);
|
w = HnswSearchLayer(base, &q, ep, 1, lc, index, support, m, true, skipElement, NULL, NULL, true, NULL, maintenance);
|
||||||
ep = w;
|
ep = w;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1319,7 +1395,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
|
|||||||
List *lw = NIL;
|
List *lw = NIL;
|
||||||
ListCell *lc2;
|
ListCell *lc2;
|
||||||
|
|
||||||
w = HnswSearchLayer(base, &q, ep, efConstruction, lc, index, support, m, true, skipElement, NULL, NULL, true, NULL);
|
w = HnswSearchLayer(base, &q, ep, efConstruction, lc, index, support, m, true, skipElement, NULL, NULL, true, NULL, maintenance);
|
||||||
|
|
||||||
/* Convert search candidates to candidates */
|
/* Convert search candidates to candidates */
|
||||||
foreach(lc2, w)
|
foreach(lc2, w)
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ RepairGraphElement(HnswVacuumState * vacuumstate, HnswElement element, HnswEleme
|
|||||||
element->heaptidsLength = 0;
|
element->heaptidsLength = 0;
|
||||||
|
|
||||||
/* Find neighbors for element, skipping itself */
|
/* Find neighbors for element, skipping itself */
|
||||||
HnswFindElementNeighbors(base, element, entryPoint, index, support, m, efConstruction, true);
|
HnswFindElementNeighbors(base, element, entryPoint, index, support, m, efConstruction, true, true);
|
||||||
|
|
||||||
/* Zero memory for each element */
|
/* Zero memory for each element */
|
||||||
MemSet(ntup, 0, HNSW_TUPLE_ALLOC_SIZE);
|
MemSet(ntup, 0, HNSW_TUPLE_ALLOC_SIZE);
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "optimizer/optimizer.h"
|
#include "optimizer/optimizer.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
|
#include "storage/condition_variable.h"
|
||||||
#include "tcop/tcopprot.h"
|
#include "tcop/tcopprot.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
#include "utils/rel.h"
|
||||||
@@ -62,15 +63,13 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Normalize with KMEANS_NORM_PROC since spherical distance function
|
* Check with KMEANS_NORM_PROC that the value can be normalized since
|
||||||
* expects unit vectors
|
* spherical distance function expects unit vectors
|
||||||
*/
|
*/
|
||||||
if (buildstate->kmeansnormprocinfo != NULL)
|
if (buildstate->kmeansnormprocinfo != NULL)
|
||||||
{
|
{
|
||||||
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
value = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (samples->length < targsamples)
|
if (samples->length < targsamples)
|
||||||
@@ -81,7 +80,7 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (buildstate->rowstoskip < 0)
|
if (buildstate->rowstoskip < 0)
|
||||||
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, samples->length, targsamples);
|
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, buildstate->samplerows, targsamples);
|
||||||
|
|
||||||
if (buildstate->rowstoskip <= 0)
|
if (buildstate->rowstoskip <= 0)
|
||||||
{
|
{
|
||||||
@@ -97,6 +96,9 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
|
|
||||||
buildstate->rowstoskip -= 1;
|
buildstate->rowstoskip -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Increment after reservoir_get_next_S */
|
||||||
|
buildstate->samplerows += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -133,6 +135,7 @@ SampleRows(IvfflatBuildState * buildstate)
|
|||||||
int targsamples = buildstate->samples->maxlen;
|
int targsamples = buildstate->samples->maxlen;
|
||||||
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
||||||
|
|
||||||
|
buildstate->samplerows = 0;
|
||||||
buildstate->rowstoskip = -1;
|
buildstate->rowstoskip = -1;
|
||||||
|
|
||||||
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt());
|
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, RandomInt());
|
||||||
@@ -145,6 +148,21 @@ SampleRows(IvfflatBuildState * buildstate)
|
|||||||
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
||||||
false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Normalize if needed */
|
||||||
|
if (buildstate->kmeansnormprocinfo != NULL)
|
||||||
|
{
|
||||||
|
VectorArray samples = buildstate->samples;
|
||||||
|
|
||||||
|
for (int i = 0; i < samples->length; i++)
|
||||||
|
{
|
||||||
|
Datum value = PointerGetDatum(VectorArrayGet(samples, i));
|
||||||
|
Datum normValue = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
||||||
|
|
||||||
|
VectorArraySet(samples, i, DatumGetPointer(normValue));
|
||||||
|
pfree(DatumGetPointer(normValue));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -374,6 +392,9 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In
|
|||||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
||||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
||||||
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 3, "vector", TupleDescAttr(buildstate->tupdesc, 0)->atttypid, -1, 0);
|
TupleDescInitEntry(buildstate->sortdesc, (AttrNumber) 3, "vector", TupleDescAttr(buildstate->tupdesc, 0)->atttypid, -1, 0);
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
TupleDescFinalize(buildstate->sortdesc);
|
||||||
|
#endif
|
||||||
|
|
||||||
buildstate->slot = MakeSingleTupleTableSlot(buildstate->sortdesc, &TTSOpsVirtual);
|
buildstate->slot = MakeSingleTupleTableSlot(buildstate->sortdesc, &TTSOpsVirtual);
|
||||||
|
|
||||||
@@ -435,7 +456,7 @@ ComputeCenters(IvfflatBuildState * buildstate)
|
|||||||
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions, buildstate->centers->itemsize);
|
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions, buildstate->centers->itemsize);
|
||||||
if (buildstate->heap != NULL)
|
if (buildstate->heap != NULL)
|
||||||
{
|
{
|
||||||
SampleRows(buildstate);
|
IvfflatBench("sample rows", SampleRows(buildstate));
|
||||||
|
|
||||||
if (buildstate->samples->length < buildstate->lists)
|
if (buildstate->samples->length < buildstate->lists)
|
||||||
{
|
{
|
||||||
@@ -650,7 +671,11 @@ IvfflatParallelScanAndSort(IvfflatSpool * ivfspool, IvfflatShared * ivfshared, S
|
|||||||
ivfspool->sortstate = InitBuildSortState(buildstate.sortdesc, sortmem, coordinate);
|
ivfspool->sortstate = InitBuildSortState(buildstate.sortdesc, sortmem, coordinate);
|
||||||
buildstate.sortstate = ivfspool->sortstate;
|
buildstate.sortstate = ivfspool->sortstate;
|
||||||
scan = table_beginscan_parallel(ivfspool->heap,
|
scan = table_beginscan_parallel(ivfspool->heap,
|
||||||
ParallelTableScanFromIvfflatShared(ivfshared));
|
ParallelTableScanFromIvfflatShared(ivfshared)
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
,SO_NONE
|
||||||
|
#endif
|
||||||
|
);
|
||||||
reltuples = table_index_build_scan(ivfspool->heap, ivfspool->index, indexInfo,
|
reltuples = table_index_build_scan(ivfspool->heap, ivfspool->index, indexInfo,
|
||||||
true, progress, BuildCallback,
|
true, progress, BuildCallback,
|
||||||
(void *) &buildstate, scan);
|
(void *) &buildstate, scan);
|
||||||
|
|||||||
@@ -184,6 +184,64 @@ 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;
|
||||||
@@ -257,4 +315,5 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
PG_RETURN_POINTER(amroutine);
|
PG_RETURN_POINTER(amroutine);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "lib/pairingheap.h"
|
#include "lib/pairingheap.h"
|
||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
#include "port.h" /* for random() */
|
#include "port.h" /* for random() */
|
||||||
|
#include "storage/condition_variable.h"
|
||||||
#include "utils/sampling.h"
|
#include "utils/sampling.h"
|
||||||
#include "utils/tuplesort.h"
|
#include "utils/tuplesort.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
@@ -213,7 +214,8 @@ typedef struct IvfflatBuildState
|
|||||||
/* Sampling */
|
/* Sampling */
|
||||||
BlockSamplerData bs;
|
BlockSamplerData bs;
|
||||||
ReservoirStateData rstate;
|
ReservoirStateData rstate;
|
||||||
int rowstoskip;
|
double samplerows;
|
||||||
|
double rowstoskip;
|
||||||
|
|
||||||
/* Sorting */
|
/* Sorting */
|
||||||
Tuplesortstate *sortstate;
|
Tuplesortstate *sortstate;
|
||||||
|
|||||||
@@ -298,6 +298,9 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
so->tupdesc = CreateTemplateTupleDesc(2);
|
so->tupdesc = CreateTemplateTupleDesc(2);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
||||||
|
#if PG_VERSION_NUM >= 190000
|
||||||
|
TupleDescFinalize(so->tupdesc);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Prep sort */
|
/* Prep sort */
|
||||||
so->sortstate = InitScanSortState(so->tupdesc);
|
so->sortstate = InitScanSortState(so->tupdesc);
|
||||||
|
|||||||
@@ -40,7 +40,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 180000
|
#if PG_VERSION_NUM >= 180000
|
||||||
PG_MODULE_MAGIC_EXT(.name = "vector",.version = "0.8.1");
|
PG_MODULE_MAGIC_EXT(.name = "vector",.version = "0.8.2");
|
||||||
#else
|
#else
|
||||||
PG_MODULE_MAGIC;
|
PG_MODULE_MAGIC;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
29
test/t/045_hnsw_low_memory_build.pl
Normal file
29
test/t/045_hnsw_low_memory_build.pl
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
use strict;
|
||||||
|
use warnings FATAL => 'all';
|
||||||
|
use PostgreSQL::Test::Cluster;
|
||||||
|
use PostgreSQL::Test::Utils;
|
||||||
|
use Test::More;
|
||||||
|
|
||||||
|
# Initialize node
|
||||||
|
my $node = PostgreSQL::Test::Cluster->new('node');
|
||||||
|
$node->init;
|
||||||
|
$node->start;
|
||||||
|
|
||||||
|
# Create table
|
||||||
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
|
$node->safe_psql("postgres", "CREATE TABLE tst (v vector(3));");
|
||||||
|
$node->safe_psql("postgres",
|
||||||
|
"INSERT INTO tst SELECT ARRAY[random(), random(), random()] FROM generate_series(1, 1000) i;"
|
||||||
|
);
|
||||||
|
|
||||||
|
my ($ret, $stdout, $stderr) = $node->psql("postgres", qq(
|
||||||
|
SET client_min_messages = DEBUG;
|
||||||
|
SET maintenance_work_mem = '3073kB';
|
||||||
|
ALTER TABLE tst SET (parallel_workers = 1);
|
||||||
|
CREATE INDEX ON tst USING hnsw (v vector_l2_ops);
|
||||||
|
));
|
||||||
|
is($ret, 0, $stderr);
|
||||||
|
like($stderr, qr/using \d+ parallel workers/);
|
||||||
|
like($stderr, qr/hnsw graph no longer fits into maintenance_work_mem after 0 tuples/);
|
||||||
|
|
||||||
|
done_testing();
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
comment = 'vector data type and ivfflat and hnsw access methods'
|
comment = 'vector data type and ivfflat and hnsw access methods'
|
||||||
default_version = '0.8.1'
|
default_version = '0.8.2'
|
||||||
module_pathname = '$libdir/vector'
|
module_pathname = '$libdir/vector'
|
||||||
relocatable = true
|
relocatable = true
|
||||||
|
|||||||
Reference in New Issue
Block a user