mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-22 12:07:34 +08:00
Compare commits
51 Commits
random_vec
...
parallel-i
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54870b9bdc | ||
|
|
0950968c11 | ||
|
|
ec12d79cbc | ||
|
|
d3eb56df07 | ||
|
|
13f7aa50c3 | ||
|
|
81e9e72fbc | ||
|
|
8d95510302 | ||
|
|
53bb2ed0cd | ||
|
|
fac8b9c8d9 | ||
|
|
4e68f4f800 | ||
|
|
b69ac51ad7 | ||
|
|
29c7af5d18 | ||
|
|
bc9e2a37ec | ||
|
|
18eb04e278 | ||
|
|
42da2b334b | ||
|
|
dbfc6a35d9 | ||
|
|
bbae64b784 | ||
|
|
a6e8f36415 | ||
|
|
44985abbc6 | ||
|
|
9897ba7f64 | ||
|
|
bb75ce2cf2 | ||
|
|
385b2437a9 | ||
|
|
6c1536ee78 | ||
|
|
4d8eae1c3e | ||
|
|
68378066a8 | ||
|
|
4f8620fe9e | ||
|
|
a1ab0a453c | ||
|
|
f1983ce672 | ||
|
|
0a3615b82f | ||
|
|
e8a67dcf1c | ||
|
|
e83121eee4 | ||
|
|
77154f8cbb | ||
|
|
051c42a05a | ||
|
|
9077d85407 | ||
|
|
d74c82ff97 | ||
|
|
cb6b7a3893 | ||
|
|
cb7d787d7c | ||
|
|
fc09ee200d | ||
|
|
eedcd64e08 | ||
|
|
c1671a4982 | ||
|
|
010055b288 | ||
|
|
8b759b695b | ||
|
|
e1e53860d4 | ||
|
|
a85250c7bc | ||
|
|
4984411906 | ||
|
|
e4b0d41d30 | ||
|
|
1b16a28906 | ||
|
|
0b3dc0887f | ||
|
|
bb71b22e2e | ||
|
|
3b9df1c145 | ||
|
|
11f66e6010 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
/dist/
|
/dist/
|
||||||
|
/log/
|
||||||
/results/
|
/results/
|
||||||
/tmp_check/
|
/tmp_check/
|
||||||
/sql/vector--?.?.?.sql
|
/sql/vector--?.?.?.sql
|
||||||
@@ -7,6 +8,7 @@ regression.*
|
|||||||
*.so
|
*.so
|
||||||
*.bc
|
*.bc
|
||||||
*.dll
|
*.dll
|
||||||
|
*.dylib
|
||||||
*.obj
|
*.obj
|
||||||
*.lib
|
*.lib
|
||||||
*.exp
|
*.exp
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
## 0.4.1 (unreleased)
|
## 0.4.1 (2023-03-21)
|
||||||
|
|
||||||
- Added `random_vector` function
|
- Improved performance of cosine distance
|
||||||
|
- Fixed index scan count
|
||||||
|
|
||||||
## 0.4.0 (2023-01-11)
|
## 0.4.0 (2023-01-11)
|
||||||
|
|
||||||
|
|||||||
@@ -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.4.0",
|
"version": "0.4.1",
|
||||||
"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.4.0",
|
"version": "0.4.1",
|
||||||
"abstract": "Open-source vector similarity search for Postgres"
|
"abstract": "Open-source vector similarity search for Postgres"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
4
Makefile
4
Makefile
@@ -1,5 +1,5 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.4.0
|
EXTVERSION = 0.4.1
|
||||||
|
|
||||||
MODULE_big = vector
|
MODULE_big = vector
|
||||||
DATA = $(wildcard sql/*--*.sql)
|
DATA = $(wildcard sql/*--*.sql)
|
||||||
@@ -61,4 +61,4 @@ dist:
|
|||||||
.PHONY: docker
|
.PHONY: docker
|
||||||
|
|
||||||
docker:
|
docker:
|
||||||
docker build --pull --no-cache -t ankane/pgvector:latest .
|
docker build --pull --no-cache --platform linux/amd64 -t ankane/pgvector:latest .
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.4.0
|
EXTVERSION = 0.4.1
|
||||||
|
|
||||||
OBJS = src\ivfbuild.obj src\ivfflat.obj src\ivfinsert.obj src\ivfkmeans.obj src\ivfscan.obj src\ivfutils.obj src\ivfvacuum.obj src\vector.obj
|
OBJS = src\ivfbuild.obj src\ivfflat.obj src\ivfinsert.obj src\ivfkmeans.obj src\ivfscan.obj src\ivfutils.obj src\ivfvacuum.obj src\vector.obj
|
||||||
|
|
||||||
|
|||||||
123
README.md
123
README.md
@@ -17,7 +17,7 @@ Supports L2 distance, inner product, and cosine distance
|
|||||||
Compile and install the extension (supports Postgres 11+)
|
Compile and install the extension (supports Postgres 11+)
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone --branch v0.4.0 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.4.1 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
make
|
make
|
||||||
make install # may need sudo
|
make install # may need sudo
|
||||||
@@ -55,6 +55,28 @@ Also supports inner product (`<#>`) and cosine distance (`<=>`)
|
|||||||
|
|
||||||
Note: `<#>` returns the negative inner product since Postgres only supports `ASC` order index scans on operators
|
Note: `<#>` returns the negative inner product since Postgres only supports `ASC` order index scans on operators
|
||||||
|
|
||||||
|
## Querying
|
||||||
|
|
||||||
|
Use a `SELECT` clause to get the distance
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT embedding <-> '[3,1,2]' AS distance FROM items;
|
||||||
|
```
|
||||||
|
|
||||||
|
Use a `WHERE` clause to get rows within a certain distance
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT * FROM items WHERE embedding <-> '[3,1,2]' < 5;
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: Combine with `ORDER BY` and `LIMIT` to use an index
|
||||||
|
|
||||||
|
Get the average of vectors
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT AVG(embedding) FROM items;
|
||||||
|
```
|
||||||
|
|
||||||
## Indexing
|
## Indexing
|
||||||
|
|
||||||
Speed up queries with an approximate index. Add an index for each distance function you want to use.
|
Speed up queries with an approximate index. Add an index for each distance function you want to use.
|
||||||
@@ -87,7 +109,10 @@ Specify the number of inverted lists (100 by default)
|
|||||||
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100);
|
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100);
|
||||||
```
|
```
|
||||||
|
|
||||||
A [good place to start](https://github.com/facebookresearch/faiss/issues/112) is `4 * sqrt(rows)`
|
A lower value provides better recall at the cost of speed. A good place to start is:
|
||||||
|
|
||||||
|
- `rows / 1000` for up to 1M rows
|
||||||
|
- `sqrt(rows)` for over 1M rows
|
||||||
|
|
||||||
### Query Options
|
### Query Options
|
||||||
|
|
||||||
@@ -97,7 +122,7 @@ Specify the number of probes (1 by default)
|
|||||||
SET ivfflat.probes = 1;
|
SET ivfflat.probes = 1;
|
||||||
```
|
```
|
||||||
|
|
||||||
A higher value improves recall at the cost of speed.
|
A higher value provides better recall at the cost of speed, and it can be set to the number of lists for exact nearest neighbor search (at which point the planner won’t use the index)
|
||||||
|
|
||||||
Use `SET LOCAL` inside a transaction to set it for a single query
|
Use `SET LOCAL` inside a transaction to set it for a single query
|
||||||
|
|
||||||
@@ -159,6 +184,50 @@ To speed up queries with an index, increase the number of inverted lists (at the
|
|||||||
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 1000);
|
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 1000);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Use `EXPLAIN ANALYZE` to debug performance.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
EXPLAIN ANALYZE SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 1;
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
|
Language | Libraries / Examples
|
||||||
|
--- | ---
|
||||||
|
C++ | [pgvector-cpp](https://github.com/pgvector/pgvector-cpp)
|
||||||
|
C# | [pgvector-dotnet](https://github.com/pgvector/pgvector-dotnet)
|
||||||
|
Elixir | [pgvector-elixir](https://github.com/pgvector/pgvector-elixir)
|
||||||
|
Go | [pgvector-go](https://github.com/pgvector/pgvector-go)
|
||||||
|
Java, Scala | [pgvector-java](https://github.com/pgvector/pgvector-java)
|
||||||
|
Julia | [pgvector-julia](https://github.com/pgvector/pgvector-julia)
|
||||||
|
Lua | [pgvector-lua](https://github.com/pgvector/pgvector-lua)
|
||||||
|
Node.js | [pgvector-node](https://github.com/pgvector/pgvector-node)
|
||||||
|
Perl | [pgvector-perl](https://github.com/pgvector/pgvector-perl)
|
||||||
|
PHP | [pgvector-php](https://github.com/pgvector/pgvector-php)
|
||||||
|
Python | [pgvector-python](https://github.com/pgvector/pgvector-python)
|
||||||
|
R | [pgvector-r](https://github.com/pgvector/pgvector-r)
|
||||||
|
Ruby | [pgvector-ruby](https://github.com/pgvector/pgvector-ruby), [Neighbor](https://github.com/ankane/neighbor)
|
||||||
|
Rust | [pgvector-rust](https://github.com/pgvector/pgvector-rust)
|
||||||
|
|
||||||
|
## Frequently Asked Questions
|
||||||
|
|
||||||
|
#### How many vectors can be stored in a single table?
|
||||||
|
|
||||||
|
A non-partitioned table has a limit of 32 TB by default in Postgres. A partitioned table can have thousands of partitions of that size.
|
||||||
|
|
||||||
|
#### Is replication supported?
|
||||||
|
|
||||||
|
Yes, pgvector uses the write-ahead log (WAL), which allows for replication and point-in-time recovery.
|
||||||
|
|
||||||
|
#### What if I want to index vectors with more than 2,000 dimensions?
|
||||||
|
|
||||||
|
Two things you can try are:
|
||||||
|
|
||||||
|
1. use dimensionality reduction
|
||||||
|
2. compile Postgres with a larger block size (`./configure --with-blocksize=32`) and edit the limit in `src/ivfflat.h`
|
||||||
|
|
||||||
## Reference
|
## Reference
|
||||||
|
|
||||||
### Vector Type
|
### Vector Type
|
||||||
@@ -184,7 +253,6 @@ inner_product(vector, vector) → double precision | inner product
|
|||||||
l2_distance(vector, vector) → double precision | Euclidean distance
|
l2_distance(vector, vector) → double precision | Euclidean distance
|
||||||
vector_dims(vector) → integer | number of dimensions
|
vector_dims(vector) → integer | number of dimensions
|
||||||
vector_norm(vector) → double precision | Euclidean norm
|
vector_norm(vector) → double precision | Euclidean norm
|
||||||
random_vector(integer) → vector | random vector [unreleased]
|
|
||||||
|
|
||||||
### Aggregate Functions
|
### Aggregate Functions
|
||||||
|
|
||||||
@@ -192,36 +260,6 @@ Function | Description
|
|||||||
--- | ---
|
--- | ---
|
||||||
avg(vector) → vector | arithmetic mean
|
avg(vector) → vector | arithmetic mean
|
||||||
|
|
||||||
## Libraries
|
|
||||||
|
|
||||||
Language | Libraries
|
|
||||||
--- | ---
|
|
||||||
Python | [pgvector-python](https://github.com/pgvector/pgvector-python)
|
|
||||||
Ruby | [Neighbor](https://github.com/ankane/neighbor), [pgvector-ruby](https://github.com/pgvector/pgvector-ruby)
|
|
||||||
Node | [pgvector-node](https://github.com/pgvector/pgvector-node)
|
|
||||||
Go | [pgvector-go](https://github.com/pgvector/pgvector-go)
|
|
||||||
PHP | [pgvector-php](https://github.com/pgvector/pgvector-php)
|
|
||||||
Rust | [pgvector-rust](https://github.com/pgvector/pgvector-rust)
|
|
||||||
C++ | [pgvector-cpp](https://github.com/pgvector/pgvector-cpp)
|
|
||||||
Elixir | [pgvector-elixir](https://github.com/pgvector/pgvector-elixir)
|
|
||||||
|
|
||||||
## Frequently Asked Questions
|
|
||||||
|
|
||||||
#### How many vectors can be stored in a single table?
|
|
||||||
|
|
||||||
A non-partitioned table has a limit of 32 TB by default in Postgres. A partitioned table can have thousands of partitions of that size.
|
|
||||||
|
|
||||||
#### Is replication supported?
|
|
||||||
|
|
||||||
Yes, pgvector uses the write-ahead log (WAL), which allows for replication and point-in-time recovery.
|
|
||||||
|
|
||||||
#### What if I want to index vectors with more than 2,000 dimensions?
|
|
||||||
|
|
||||||
Two things you can try are:
|
|
||||||
|
|
||||||
1. use dimensionality reduction
|
|
||||||
2. compile Postgres with a larger block size (`./configure --with-blocksize=32`) and edit the limit in `src/ivfflat.h`
|
|
||||||
|
|
||||||
## Additional Installation Methods
|
## Additional Installation Methods
|
||||||
|
|
||||||
### Docker
|
### Docker
|
||||||
@@ -232,12 +270,12 @@ Get the [Docker image](https://hub.docker.com/r/ankane/pgvector) with:
|
|||||||
docker pull ankane/pgvector
|
docker pull ankane/pgvector
|
||||||
```
|
```
|
||||||
|
|
||||||
This adds pgvector to the [Postgres image](https://hub.docker.com/_/postgres).
|
This adds pgvector to the [Postgres image](https://hub.docker.com/_/postgres) (run it the same way).
|
||||||
|
|
||||||
You can also build the image manually
|
You can also build the image manually:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone --branch v0.4.0 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.4.1 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
docker build -t pgvector .
|
docker build -t pgvector .
|
||||||
```
|
```
|
||||||
@@ -247,7 +285,7 @@ docker build -t pgvector .
|
|||||||
With Homebrew Postgres, you can use:
|
With Homebrew Postgres, you can use:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
brew install pgvector/brew/pgvector
|
brew install pgvector
|
||||||
```
|
```
|
||||||
|
|
||||||
### PGXN
|
### PGXN
|
||||||
@@ -260,7 +298,7 @@ pgxn install vector
|
|||||||
|
|
||||||
### conda-forge
|
### conda-forge
|
||||||
|
|
||||||
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:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
conda install -c conda-forge pgvector
|
conda install -c conda-forge pgvector
|
||||||
@@ -270,12 +308,15 @@ This method is [community-maintained](https://github.com/conda-forge/pgvector-fe
|
|||||||
|
|
||||||
## Hosted Postgres
|
## Hosted Postgres
|
||||||
|
|
||||||
Some Postgres providers only support specific extensions. To request a new extension:
|
pgvector is available on [these providers](https://github.com/pgvector/pgvector/issues/54).
|
||||||
|
|
||||||
|
To request a new extension on other providers:
|
||||||
|
|
||||||
- Amazon RDS - follow the instructions on [this page](https://aws.amazon.com/rds/postgresql/faqs/)
|
- Amazon RDS - follow the instructions on [this page](https://aws.amazon.com/rds/postgresql/faqs/)
|
||||||
- Google Cloud SQL - vote or comment on [this page](https://issuetracker.google.com/issues/265172065)
|
- Google Cloud SQL - vote or comment on [this page](https://issuetracker.google.com/issues/265172065)
|
||||||
|
- Azure Database - vote or comment on [this page](https://feedback.azure.com/d365community/idea/7b423322-6189-ed11-a81b-000d3ae49307)
|
||||||
- DigitalOcean Managed Databases - vote or comment on [this page](https://ideas.digitalocean.com/app-framework-services/p/pgvector-extension-for-postgresql)
|
- DigitalOcean Managed Databases - vote or comment on [this page](https://ideas.digitalocean.com/app-framework-services/p/pgvector-extension-for-postgresql)
|
||||||
- Azure Database for PostgreSQL - vote or comment on [this page](https://feedback.azure.com/d365community/idea/7b423322-6189-ed11-a81b-000d3ae49307)
|
- Render - vote or comment on [this page](https://feedback.render.com/features/p/add-pgvector-extension-to-postgresql)
|
||||||
|
|
||||||
## Upgrading
|
## Upgrading
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,2 @@
|
|||||||
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||||
\echo Use "ALTER EXTENSION vector UPDATE TO '0.4.1'" to load this file. \quit
|
\echo Use "ALTER EXTENSION vector UPDATE TO '0.4.1'" to load this file. \quit
|
||||||
|
|
||||||
CREATE FUNCTION random_vector(integer) RETURNS vector
|
|
||||||
AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT PARALLEL SAFE;
|
|
||||||
|
|||||||
@@ -52,9 +52,6 @@ CREATE FUNCTION vector_add(vector, vector) RETURNS vector
|
|||||||
CREATE FUNCTION vector_sub(vector, vector) RETURNS vector
|
CREATE FUNCTION vector_sub(vector, vector) RETURNS vector
|
||||||
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
CREATE FUNCTION random_vector(integer) RETURNS vector
|
|
||||||
AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT PARALLEL SAFE;
|
|
||||||
|
|
||||||
-- private functions
|
-- private functions
|
||||||
|
|
||||||
CREATE FUNCTION vector_lt(vector, vector) RETURNS bool
|
CREATE FUNCTION vector_lt(vector, vector) RETURNS bool
|
||||||
|
|||||||
@@ -103,7 +103,13 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
|||||||
if (ratio > 1)
|
if (ratio > 1)
|
||||||
ratio = 1;
|
ratio = 1;
|
||||||
|
|
||||||
|
// cost estimates for parallel workers applied outside of amcostestimate
|
||||||
|
elog(INFO, "parallel_workers = %d, parallel aware = %d", path->path.parallel_workers, path->path.parallel_aware);
|
||||||
|
|
||||||
costs.indexTotalCost *= ratio;
|
costs.indexTotalCost *= ratio;
|
||||||
|
costs.numIndexPages *= ratio;
|
||||||
|
|
||||||
|
elog(INFO, "ivfflatcostestimate = %f", costs.indexTotalCost);
|
||||||
|
|
||||||
/* Startup cost and total cost are same */
|
/* Startup cost and total cost are same */
|
||||||
*indexStartupCost = costs.indexTotalCost;
|
*indexStartupCost = costs.indexTotalCost;
|
||||||
@@ -151,6 +157,25 @@ ivfflatvalidate(Oid opclassoid)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Size
|
||||||
|
ivfflatestimateparallelscan()
|
||||||
|
{
|
||||||
|
elog(INFO, "ivfflatestimateparallelscan");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
ivfflatinitparallelscan(void *target)
|
||||||
|
{
|
||||||
|
elog(INFO, "ivfflatinitparallelscan");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
ivfflatparallelrescan(IndexScanDesc scan)
|
||||||
|
{
|
||||||
|
elog(INFO, "ivfflatparallelrescan");
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Define index handler
|
* Define index handler
|
||||||
*
|
*
|
||||||
@@ -178,7 +203,7 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->amstorage = false;
|
amroutine->amstorage = false;
|
||||||
amroutine->amclusterable = false;
|
amroutine->amclusterable = false;
|
||||||
amroutine->ampredlocks = false;
|
amroutine->ampredlocks = false;
|
||||||
amroutine->amcanparallel = false;
|
amroutine->amcanparallel = true;
|
||||||
amroutine->amcaninclude = false;
|
amroutine->amcaninclude = false;
|
||||||
#if PG_VERSION_NUM >= 130000
|
#if PG_VERSION_NUM >= 130000
|
||||||
amroutine->amusemaintenanceworkmem = false; /* not used during VACUUM */
|
amroutine->amusemaintenanceworkmem = false; /* not used during VACUUM */
|
||||||
@@ -212,9 +237,9 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->amrestrpos = NULL;
|
amroutine->amrestrpos = NULL;
|
||||||
|
|
||||||
/* Interface functions to support parallel index scans */
|
/* Interface functions to support parallel index scans */
|
||||||
amroutine->amestimateparallelscan = NULL;
|
amroutine->amestimateparallelscan = ivfflatestimateparallelscan;
|
||||||
amroutine->aminitparallelscan = NULL;
|
amroutine->aminitparallelscan = ivfflatinitparallelscan;
|
||||||
amroutine->amparallelrescan = NULL;
|
amroutine->amparallelrescan = ivfflatparallelrescan;
|
||||||
|
|
||||||
PG_RETURN_POINTER(amroutine);
|
PG_RETURN_POINTER(amroutine);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,15 @@
|
|||||||
#include "access/generic_xlog.h"
|
#include "access/generic_xlog.h"
|
||||||
#include "access/reloptions.h"
|
#include "access/reloptions.h"
|
||||||
#include "nodes/execnodes.h"
|
#include "nodes/execnodes.h"
|
||||||
|
#include "port.h" /* for strtof() and random() */
|
||||||
#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 >= 150000
|
||||||
|
#include "common/pg_prng.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef IVFFLAT_BENCH
|
#ifdef IVFFLAT_BENCH
|
||||||
#include "portability/instr_time.h"
|
#include "portability/instr_time.h"
|
||||||
#endif
|
#endif
|
||||||
@@ -63,6 +68,14 @@
|
|||||||
#define IvfflatBench(name, code) (code)
|
#define IvfflatBench(name, code) (code)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 150000
|
||||||
|
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
|
||||||
|
#define RandomInt() pg_prng_uint32(&pg_global_prng_state)
|
||||||
|
#else
|
||||||
|
#define RandomDouble() (((double) random()) / MAX_RANDOM_VALUE)
|
||||||
|
#define RandomInt() random()
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Variables */
|
/* Variables */
|
||||||
extern int ivfflat_probes;
|
extern int ivfflat_probes;
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "access/relscan.h"
|
#include "access/relscan.h"
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
|
#include "pgstat.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
|
|
||||||
#include "catalog/pg_operator_d.h"
|
#include "catalog/pg_operator_d.h"
|
||||||
@@ -267,6 +268,9 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
{
|
{
|
||||||
Datum value;
|
Datum value;
|
||||||
|
|
||||||
|
/* Count index scan for stats */
|
||||||
|
pgstat_count_index_scan(scan->indexRelation);
|
||||||
|
|
||||||
/* Safety check */
|
/* Safety check */
|
||||||
if (scan->orderByData == NULL)
|
if (scan->orderByData == NULL)
|
||||||
elog(ERROR, "cannot scan ivfflat index without order");
|
elog(ERROR, "cannot scan ivfflat index without order");
|
||||||
|
|||||||
@@ -143,6 +143,11 @@ ivfflatvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
|
|||||||
{
|
{
|
||||||
Relation rel = info->index;
|
Relation rel = info->index;
|
||||||
|
|
||||||
|
if (info->analyze_only)
|
||||||
|
return stats;
|
||||||
|
|
||||||
|
/* stats is NULL if ambulkdelete not called */
|
||||||
|
/* OK to return NULL if index not changed */
|
||||||
if (stats == NULL)
|
if (stats == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
|||||||
26
src/vector.c
26
src/vector.c
@@ -416,7 +416,7 @@ array_to_vector(PG_FUNCTION_ARGS)
|
|||||||
else if (ARR_ELEMTYPE(array) == FLOAT4OID)
|
else if (ARR_ELEMTYPE(array) == FLOAT4OID)
|
||||||
result->x[i] = DatumGetFloat4(elemsp[i]);
|
result->x[i] = DatumGetFloat4(elemsp[i]);
|
||||||
else if (ARR_ELEMTYPE(array) == NUMERICOID)
|
else if (ARR_ELEMTYPE(array) == NUMERICOID)
|
||||||
result->x[i] = DatumGetFloat4(DirectFunctionCall1(numeric_float4, NumericGetDatum(elemsp[i])));
|
result->x[i] = DatumGetFloat4(DirectFunctionCall1(numeric_float4, elemsp[i]));
|
||||||
else
|
else
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_DATA_EXCEPTION),
|
(errcode(ERRCODE_DATA_EXCEPTION),
|
||||||
@@ -568,7 +568,8 @@ cosine_distance(PG_FUNCTION_ARGS)
|
|||||||
normb += bx[i] * bx[i];
|
normb += bx[i] * bx[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
PG_RETURN_FLOAT8(1 - (distance / (sqrt(norma) * sqrt(normb))));
|
/* Use sqrt(a * b) over sqrt(a) * sqrt(b) */
|
||||||
|
PG_RETURN_FLOAT8(1 - (distance / sqrt(norma * normb)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -822,7 +823,7 @@ vector_accum(PG_FUNCTION_ARGS)
|
|||||||
if (newarr)
|
if (newarr)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < dim; i++)
|
for (int i = 0; i < dim; i++)
|
||||||
statedatums[i + 1] = Float8GetDatumFast(x[i]);
|
statedatums[i + 1] = Float8GetDatumFast((double) x[i]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -950,22 +951,3 @@ vector_avg(PG_FUNCTION_ARGS)
|
|||||||
|
|
||||||
PG_RETURN_POINTER(result);
|
PG_RETURN_POINTER(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Generate a random vector
|
|
||||||
*/
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(random_vector);
|
|
||||||
Datum
|
|
||||||
random_vector(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
int32 dim = PG_GETARG_INT32(0);
|
|
||||||
Vector *result;
|
|
||||||
|
|
||||||
CheckDim(dim);
|
|
||||||
|
|
||||||
result = InitVector(dim);
|
|
||||||
for (int i = 0; i < dim; i++)
|
|
||||||
result->x[i] = RandomDouble();
|
|
||||||
|
|
||||||
PG_RETURN_POINTER(result);
|
|
||||||
}
|
|
||||||
|
|||||||
14
src/vector.h
14
src/vector.h
@@ -3,10 +3,8 @@
|
|||||||
|
|
||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include "port.h" /* for strtof() and random() */
|
#if PG_VERSION_NUM >= 160000
|
||||||
|
#include "varatt.h"
|
||||||
#if PG_VERSION_NUM >= 150000
|
|
||||||
#include "common/pg_prng.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define VECTOR_MAX_DIM 16000
|
#define VECTOR_MAX_DIM 16000
|
||||||
@@ -16,14 +14,6 @@
|
|||||||
#define PG_GETARG_VECTOR_P(x) DatumGetVector(PG_GETARG_DATUM(x))
|
#define PG_GETARG_VECTOR_P(x) DatumGetVector(PG_GETARG_DATUM(x))
|
||||||
#define PG_RETURN_VECTOR_P(x) PG_RETURN_POINTER(x)
|
#define PG_RETURN_VECTOR_P(x) PG_RETURN_POINTER(x)
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 150000
|
|
||||||
#define RandomDouble() pg_prng_double(&pg_global_prng_state)
|
|
||||||
#define RandomInt() pg_prng_uint32(&pg_global_prng_state)
|
|
||||||
#else
|
|
||||||
#define RandomDouble() (((double) random()) / MAX_RANDOM_VALUE)
|
|
||||||
#define RandomInt() random()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef struct Vector
|
typedef struct Vector
|
||||||
{
|
{
|
||||||
int32 vl_len_; /* varlena header (do not touch directly!) */
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
||||||
|
|||||||
@@ -22,6 +22,12 @@ SELECT ARRAY[1,2,3]::float8[]::vector;
|
|||||||
[1,2,3]
|
[1,2,3]
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
|
SELECT ARRAY[1,2,3]::numeric[]::vector;
|
||||||
|
array
|
||||||
|
---------
|
||||||
|
[1,2,3]
|
||||||
|
(1 row)
|
||||||
|
|
||||||
SELECT '{NULL}'::real[]::vector;
|
SELECT '{NULL}'::real[]::vector;
|
||||||
ERROR: array must not containing NULLs
|
ERROR: array must not containing NULLs
|
||||||
SELECT '{NaN}'::real[]::vector;
|
SELECT '{NaN}'::real[]::vector;
|
||||||
|
|||||||
@@ -22,10 +22,28 @@ SELECT round(vector_norm('[1,1]')::numeric, 5);
|
|||||||
1.41421
|
1.41421
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT round(l2_distance('[1,2]', '[0,0]')::numeric, 5);
|
SELECT vector_norm('[3,4]');
|
||||||
round
|
vector_norm
|
||||||
---------
|
-------------
|
||||||
2.23607
|
5
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT vector_norm('[0,1]');
|
||||||
|
vector_norm
|
||||||
|
-------------
|
||||||
|
1
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT l2_distance('[0,0]', '[3,4]');
|
||||||
|
l2_distance
|
||||||
|
-------------
|
||||||
|
5
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT l2_distance('[0,0]', '[0,1]');
|
||||||
|
l2_distance
|
||||||
|
-------------
|
||||||
|
1
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT l2_distance('[1,2]', '[3]');
|
SELECT l2_distance('[1,2]', '[3]');
|
||||||
@@ -38,10 +56,10 @@ SELECT inner_product('[1,2]', '[3,4]');
|
|||||||
|
|
||||||
SELECT inner_product('[1,2]', '[3]');
|
SELECT inner_product('[1,2]', '[3]');
|
||||||
ERROR: different vector dimensions 2 and 1
|
ERROR: different vector dimensions 2 and 1
|
||||||
SELECT round(cosine_distance('[1,2]', '[2,4]')::numeric, 5);
|
SELECT cosine_distance('[1,2]', '[2,4]');
|
||||||
round
|
cosine_distance
|
||||||
---------
|
-----------------
|
||||||
0.00000
|
0
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[1,2]', '[0,0]');
|
SELECT cosine_distance('[1,2]', '[0,0]');
|
||||||
@@ -50,6 +68,18 @@ SELECT cosine_distance('[1,2]', '[0,0]');
|
|||||||
NaN
|
NaN
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
|
SELECT cosine_distance('[1,1]', '[1,1]');
|
||||||
|
cosine_distance
|
||||||
|
-----------------
|
||||||
|
0
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT cosine_distance('[1,1]', '[-1,-1]');
|
||||||
|
cosine_distance
|
||||||
|
-----------------
|
||||||
|
2
|
||||||
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[1,2]', '[3]');
|
SELECT cosine_distance('[1,2]', '[3]');
|
||||||
ERROR: different vector dimensions 2 and 1
|
ERROR: different vector dimensions 2 and 1
|
||||||
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
|
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ SELECT ARRAY[1,2,3]::vector;
|
|||||||
SELECT ARRAY[1.0,2.0,3.0]::vector;
|
SELECT ARRAY[1.0,2.0,3.0]::vector;
|
||||||
SELECT ARRAY[1,2,3]::float4[]::vector;
|
SELECT ARRAY[1,2,3]::float4[]::vector;
|
||||||
SELECT ARRAY[1,2,3]::float8[]::vector;
|
SELECT ARRAY[1,2,3]::float8[]::vector;
|
||||||
|
SELECT ARRAY[1,2,3]::numeric[]::vector;
|
||||||
SELECT '{NULL}'::real[]::vector;
|
SELECT '{NULL}'::real[]::vector;
|
||||||
SELECT '{NaN}'::real[]::vector;
|
SELECT '{NaN}'::real[]::vector;
|
||||||
SELECT '{Infinity}'::real[]::vector;
|
SELECT '{Infinity}'::real[]::vector;
|
||||||
|
|||||||
@@ -2,16 +2,22 @@ SELECT '[1,2,3]'::vector + '[4,5,6]';
|
|||||||
SELECT '[1,2,3]'::vector - '[4,5,6]';
|
SELECT '[1,2,3]'::vector - '[4,5,6]';
|
||||||
|
|
||||||
SELECT vector_dims('[1,2,3]');
|
SELECT vector_dims('[1,2,3]');
|
||||||
SELECT round(vector_norm('[1,1]')::numeric, 5);
|
|
||||||
|
|
||||||
SELECT round(l2_distance('[1,2]', '[0,0]')::numeric, 5);
|
SELECT round(vector_norm('[1,1]')::numeric, 5);
|
||||||
|
SELECT vector_norm('[3,4]');
|
||||||
|
SELECT vector_norm('[0,1]');
|
||||||
|
|
||||||
|
SELECT l2_distance('[0,0]', '[3,4]');
|
||||||
|
SELECT l2_distance('[0,0]', '[0,1]');
|
||||||
SELECT l2_distance('[1,2]', '[3]');
|
SELECT l2_distance('[1,2]', '[3]');
|
||||||
|
|
||||||
SELECT inner_product('[1,2]', '[3,4]');
|
SELECT inner_product('[1,2]', '[3,4]');
|
||||||
SELECT inner_product('[1,2]', '[3]');
|
SELECT inner_product('[1,2]', '[3]');
|
||||||
|
|
||||||
SELECT round(cosine_distance('[1,2]', '[2,4]')::numeric, 5);
|
SELECT cosine_distance('[1,2]', '[2,4]');
|
||||||
SELECT cosine_distance('[1,2]', '[0,0]');
|
SELECT cosine_distance('[1,2]', '[0,0]');
|
||||||
|
SELECT cosine_distance('[1,1]', '[1,1]');
|
||||||
|
SELECT cosine_distance('[1,1]', '[-1,-1]');
|
||||||
SELECT cosine_distance('[1,2]', '[3]');
|
SELECT cosine_distance('[1,2]', '[3]');
|
||||||
|
|
||||||
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
|
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
|
||||||
|
|||||||
15
test/sql/ivfflat_parallel.sql
Normal file
15
test/sql/ivfflat_parallel.sql
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
-- SET force_parallel_mode = on;
|
||||||
|
SET parallel_setup_cost = 10;
|
||||||
|
SET parallel_tuple_cost = 0.001;
|
||||||
|
SET min_parallel_table_scan_size = 1;
|
||||||
|
SET min_parallel_index_scan_size = 1;
|
||||||
|
|
||||||
|
CREATE TABLE t (id integer, val vector(3));
|
||||||
|
INSERT INTO t (id, val) SELECT n, ARRAY[random(), random(), random()] FROM generate_series(1,1000000) n;
|
||||||
|
CREATE INDEX ON t USING ivfflat (val) WITH (lists = 10);
|
||||||
|
SET ivfflat.probes = 2;
|
||||||
|
|
||||||
|
EXPLAIN SELECT * FROM t ORDER BY val <-> '[0.5,0.5,0.5]' LIMIT 5;
|
||||||
|
SELECT * FROM t ORDER BY val <-> '[0.5,0.5,0.5]' LIMIT 5;
|
||||||
|
|
||||||
|
DROP TABLE t;
|
||||||
@@ -44,6 +44,11 @@ sub test_index_replay
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Use ARRAY[random(), random(), random(), ...] over
|
||||||
|
# SELECT array_agg(random()) FROM generate_series(1, $dim)
|
||||||
|
# to generate different values for each row
|
||||||
|
my $array_sql = join(",", ('random()') x $dim);
|
||||||
|
|
||||||
# Initialize primary node
|
# Initialize primary node
|
||||||
$node_primary = get_new_node('primary');
|
$node_primary = get_new_node('primary');
|
||||||
$node_primary->init(allows_streaming => 1);
|
$node_primary->init(allows_streaming => 1);
|
||||||
@@ -70,7 +75,7 @@ $node_replica->start;
|
|||||||
$node_primary->safe_psql("postgres", "CREATE EXTENSION vector;");
|
$node_primary->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
$node_primary->safe_psql("postgres", "CREATE TABLE tst (i int4, v vector($dim));");
|
$node_primary->safe_psql("postgres", "CREATE TABLE tst (i int4, v vector($dim));");
|
||||||
$node_primary->safe_psql("postgres",
|
$node_primary->safe_psql("postgres",
|
||||||
"INSERT INTO tst SELECT i % 10, random_vector($dim) FROM generate_series(1, 100000) i;"
|
"INSERT INTO tst SELECT i % 10, ARRAY[$array_sql] FROM generate_series(1, 100000) i;"
|
||||||
);
|
);
|
||||||
$node_primary->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v);");
|
$node_primary->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v);");
|
||||||
|
|
||||||
@@ -86,7 +91,7 @@ for my $i (1 .. 10)
|
|||||||
test_index_replay("vacuum $i");
|
test_index_replay("vacuum $i");
|
||||||
my ($start, $end) = (100001 + ($i - 1) * 10000, 100000 + $i * 10000);
|
my ($start, $end) = (100001 + ($i - 1) * 10000, 100000 + $i * 10000);
|
||||||
$node_primary->safe_psql("postgres",
|
$node_primary->safe_psql("postgres",
|
||||||
"INSERT INTO tst SELECT i % 10, random_vector($dim) FROM generate_series($start, $end) i;"
|
"INSERT INTO tst SELECT i % 10, ARRAY[$array_sql] FROM generate_series($start, $end) i;"
|
||||||
);
|
);
|
||||||
test_index_replay("insert $i");
|
test_index_replay("insert $i");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ $node->start;
|
|||||||
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
$node->safe_psql("postgres", "CREATE TABLE tst (i int4, v vector(3));");
|
$node->safe_psql("postgres", "CREATE TABLE tst (i int4, v vector(3));");
|
||||||
$node->safe_psql("postgres",
|
$node->safe_psql("postgres",
|
||||||
"INSERT INTO tst SELECT i, random_vector(3) FROM generate_series(1, 100000) i;"
|
"INSERT INTO tst SELECT i, ARRAY[random(), random(), random()] FROM generate_series(1, 100000) i;"
|
||||||
);
|
);
|
||||||
|
|
||||||
# Generate queries
|
# Generate queries
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ $node->start;
|
|||||||
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
$node->safe_psql("postgres", "CREATE TABLE tst (i int4 primary key, v vector(3));");
|
$node->safe_psql("postgres", "CREATE TABLE tst (i int4 primary key, v vector(3));");
|
||||||
$node->safe_psql("postgres",
|
$node->safe_psql("postgres",
|
||||||
"INSERT INTO tst SELECT i, random_vector(3) 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
|
# Check each index type
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ $node->start;
|
|||||||
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
$node->safe_psql("postgres", "CREATE TABLE tst (v vector(3));");
|
$node->safe_psql("postgres", "CREATE TABLE tst (v vector(3));");
|
||||||
$node->safe_psql("postgres",
|
$node->safe_psql("postgres",
|
||||||
"INSERT INTO tst SELECT random_vector(3) FROM generate_series(1, 100000) i;"
|
"INSERT INTO tst SELECT ARRAY[random(), random(), random()] FROM generate_series(1, 100000) i;"
|
||||||
);
|
);
|
||||||
|
|
||||||
$node->safe_psql("postgres", "CREATE INDEX lists50 ON tst USING ivfflat (v) WITH (lists = 50);");
|
$node->safe_psql("postgres", "CREATE INDEX lists50 ON tst USING ivfflat (v) WITH (lists = 50);");
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ use strict;
|
|||||||
use warnings;
|
use warnings;
|
||||||
use PostgresNode;
|
use PostgresNode;
|
||||||
use TestLib;
|
use TestLib;
|
||||||
use Test::More tests => 5;
|
use Test::More tests => 7;
|
||||||
|
|
||||||
my $dim = 768;
|
my $dim = 768;
|
||||||
|
|
||||||
|
my $array_sql = join(",", ('random()') x $dim);
|
||||||
|
|
||||||
# Initialize node
|
# Initialize node
|
||||||
my $node = get_new_node('node');
|
my $node = get_new_node('node');
|
||||||
$node->init;
|
$node->init;
|
||||||
@@ -15,7 +17,7 @@ $node->start;
|
|||||||
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
$node->safe_psql("postgres", "CREATE TABLE tst (v vector($dim));");
|
$node->safe_psql("postgres", "CREATE TABLE tst (v vector($dim));");
|
||||||
$node->safe_psql("postgres",
|
$node->safe_psql("postgres",
|
||||||
"INSERT INTO tst SELECT random_vector($dim) FROM generate_series(1, 10000) i;"
|
"INSERT INTO tst SELECT ARRAY[$array_sql] FROM generate_series(1, 10000) i;"
|
||||||
);
|
);
|
||||||
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v);");
|
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v);");
|
||||||
|
|
||||||
@@ -26,14 +28,23 @@ $node->pgbench(
|
|||||||
[qr{^$}],
|
[qr{^$}],
|
||||||
"concurrent INSERTs",
|
"concurrent INSERTs",
|
||||||
{
|
{
|
||||||
"007_inserts" => "INSERT INTO tst SELECT random_vector($dim) FROM generate_series(1, 10) i;"
|
"007_inserts" => "INSERT INTO tst SELECT ARRAY[$array_sql] FROM generate_series(1, 10) i;"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
sub idx_scan
|
||||||
|
{
|
||||||
|
# Stats do not update instantaneously
|
||||||
|
# https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-STATS-VIEWS
|
||||||
|
sleep(1);
|
||||||
|
$node->safe_psql("postgres", "SELECT idx_scan FROM pg_stat_user_indexes WHERE indexrelid = 'tst_v_idx'::regclass;");
|
||||||
|
}
|
||||||
|
|
||||||
my $expected = 10000 + 5 * 100 * 10;
|
my $expected = 10000 + 5 * 100 * 10;
|
||||||
|
|
||||||
my $count = $node->safe_psql("postgres", "SELECT COUNT(*) FROM tst;");
|
my $count = $node->safe_psql("postgres", "SELECT COUNT(*) FROM tst;");
|
||||||
is($count, $expected);
|
is($count, $expected);
|
||||||
|
is(idx_scan(), 0);
|
||||||
|
|
||||||
$count = $node->safe_psql("postgres", qq(
|
$count = $node->safe_psql("postgres", qq(
|
||||||
SET enable_seqscan = off;
|
SET enable_seqscan = off;
|
||||||
@@ -41,3 +52,4 @@ $count = $node->safe_psql("postgres", qq(
|
|||||||
SELECT COUNT(*) FROM (SELECT v FROM tst ORDER BY v <-> (SELECT v FROM tst LIMIT 1)) t;
|
SELECT COUNT(*) FROM (SELECT v FROM tst ORDER BY v <-> (SELECT v FROM tst LIMIT 1)) t;
|
||||||
));
|
));
|
||||||
is($count, $expected);
|
is($count, $expected);
|
||||||
|
is(idx_scan(), 1);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ $node->safe_psql("postgres", "CREATE TABLE tst (v1 vector(1024), v2 vector(1024)
|
|||||||
|
|
||||||
# Test insert succeeds
|
# Test insert succeeds
|
||||||
$node->safe_psql("postgres",
|
$node->safe_psql("postgres",
|
||||||
"INSERT INTO tst SELECT random_vector($dim), random_vector($dim), random_vector($dim)"
|
"INSERT INTO tst SELECT array_agg(n), array_agg(n), array_agg(n) FROM generate_series(1, $dim) n"
|
||||||
);
|
);
|
||||||
|
|
||||||
# Change storage to PLAIN
|
# Change storage to PLAIN
|
||||||
@@ -27,6 +27,6 @@ $node->safe_psql("postgres", "ALTER TABLE tst ALTER COLUMN v3 SET STORAGE PLAIN"
|
|||||||
|
|
||||||
# Test insert fails
|
# Test insert fails
|
||||||
my ($ret, $stdout, $stderr) = $node->psql("postgres",
|
my ($ret, $stdout, $stderr) = $node->psql("postgres",
|
||||||
"INSERT INTO tst SELECT random_vector($dim), random_vector($dim), random_vector($dim)"
|
"INSERT INTO tst SELECT array_agg(n), array_agg(n), array_agg(n) FROM generate_series(1, $dim) n"
|
||||||
);
|
);
|
||||||
like($stderr, qr/row is too big/);
|
like($stderr, qr/row is too big/);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
comment = 'vector data type and ivfflat access method'
|
comment = 'vector data type and ivfflat access method'
|
||||||
default_version = '0.4.0'
|
default_version = '0.4.1'
|
||||||
module_pathname = '$libdir/vector'
|
module_pathname = '$libdir/vector'
|
||||||
relocatable = true
|
relocatable = true
|
||||||
|
|||||||
Reference in New Issue
Block a user