Compare commits

...

63 Commits

Author SHA1 Message Date
Andrew Kane
581ec6bfed Use maxItems 10 [skip ci] 2023-04-29 12:20:04 -07:00
Andrew Kane
a3c1eab27a Use pairing heap [skip ci] 2023-04-29 12:02:54 -07:00
Andrew Kane
a445355a48 Added Heroku Postgres link [skip ci] 2023-04-26 12:48:55 -07:00
Andrew Kane
d5b17a3624 Fixed installation error with Postgres 12.0-12.2 - fixes #101 2023-04-25 09:36:21 -07:00
Andrew Kane
6383078029 Updated readme [skip ci] 2023-04-22 17:07:10 -07:00
Andrew Kane
5146c7cc57 Added guidance for probes to readme [skip ci] 2023-04-20 12:32:49 -07:00
Andrew Kane
ac63f9858b Updated readme [skip ci] 2023-04-14 12:35:59 -07:00
Andrew Kane
76a4166857 Added link to pgvector-crystal [skip ci] 2023-04-13 21:10:18 -07:00
Andrew Kane
31fb6963a3 Now available on Render [skip ci] 2023-04-13 13:20:06 -07:00
Andrew Kane
18c06cb9b1 Added link to pgvector-swift [skip ci] 2023-04-11 21:11:46 -07:00
Andrew Kane
f858796c64 Added link to pgvector-haskell [skip ci] 2023-04-11 12:53:00 -07:00
Andrew Kane
f32f695844 Improved notice [skip ci] 2023-04-10 21:31:33 -07:00
Andrew Kane
1b013a94f7 Added notice when index created with little data [skip ci] 2023-04-10 21:28:24 -07:00
Andrew Kane
00148dfa1f Improved indexing docs [skip ci] 2023-04-10 21:12:25 -07:00
Andrew Kane
67fc791d95 Improved indexing docs [skip ci] 2023-04-10 21:04:46 -07:00
Andrew Kane
8bf360ed84 Updated header [skip ci] 2023-04-02 11:24:46 -07:00
Andrew Kane
f79d28347b Updated installation notes [skip ci] 2023-04-02 11:21:57 -07:00
Andrew Kane
20cf63de0a Updated readme [skip ci] 2023-04-02 10:56:37 -07:00
Andrew Kane
587cbcf15b Updated readme [skip ci] 2023-04-02 10:46:44 -07:00
Andrew Kane
c09edb5b8f Moved instructions [skip ci] 2023-04-02 10:39:41 -07:00
Will Laurance
c63501cca4 Update readme to show example usage of PG_CONFIG (#79) 2023-04-02 10:34:02 -07:00
Andrew Kane
58f0c922d2 Updated readme [skip ci] 2023-04-02 10:24:21 -07:00
Andrew Kane
36e73d2818 Updated readme [skip ci] 2023-04-01 20:06:18 -07:00
Andrew Kane
dd92d0ece3 Updated readme [skip ci] 2023-04-01 20:04:47 -07:00
Andrew Kane
6ede7681a5 Updated readme [skip ci] 2023-04-01 19:47:13 -07:00
Andrew Kane
8733729149 Updated readme [skip ci] 2023-04-01 19:45:13 -07:00
Andrew Kane
03a5789132 Updated readme [skip ci] 2023-04-01 19:44:23 -07:00
Andrew Kane
e5b612a856 Updated readme [skip ci] 2023-04-01 19:34:21 -07:00
Andrew Kane
9a7d3532f5 Updated readme [skip ci] 2023-04-01 19:32:26 -07:00
Andrew Kane
91315dfeff Added additional instructions for Ubuntu, Debian, and Windows [skip ci] 2023-04-01 17:42:44 -07:00
Andrew Kane
6e3101d527 Updated Dockerfile [skip ci] 2023-04-01 13:31:52 -07:00
Andrew Kane
96ae1a6a72 Added PG_MAJOR arg to Dockerfile [skip ci] 2023-04-01 13:15:00 -07:00
Andrew Kane
55aeba8bd6 Added lists to example [skip ci] 2023-03-31 22:12:35 -07:00
Andrew Kane
aebe1bae02 Updated readme [skip ci] 2023-03-31 21:55:24 -07:00
Andrew Kane
14355b9312 Updated readme [skip ci] 2023-03-31 21:46:38 -07:00
Andrew Kane
b5c66d0416 Updated readme [skip ci] 2023-03-31 21:36:25 -07:00
Andrew Kane
f534d9878a Updated readme [skip ci] 2023-03-31 21:31:38 -07:00
Andrew Kane
f3df137db6 Improved indexing instructions [skip ci] 2023-03-31 21:28:45 -07:00
Andrew Kane
489cdb5068 Added delete example [skip ci] 2023-03-31 20:02:22 -07:00
Andrew Kane
161f48793e Updated readme [skip ci] 2023-03-31 19:59:53 -07:00
Andrew Kane
f0f7ffca41 Updated readme [skip ci] 2023-03-31 19:34:39 -07:00
Andrew Kane
138d9be616 Added storage examples [skip ci] 2023-03-31 19:17:26 -07:00
Andrew Kane
fb98e73255 Updated readme [skip ci] 2023-03-31 18:55:51 -07:00
Andrew Kane
4754cac40c Updated readme [skip ci] 2023-03-31 18:47:15 -07:00
Andrew Kane
8432efb7d8 Added cosine similarity example [skip ci] 2023-03-31 18:34:36 -07:00
Andrew Kane
c38410259c Updated header [skip ci] 2023-03-31 16:59:21 -07:00
Andrew Kane
609d9fbf0a Updated header [skip ci] 2023-03-31 16:54:14 -07:00
Andrew Kane
7946424639 Updated header [skip ci] 2023-03-31 16:49:40 -07:00
Andrew Kane
d51310dfa0 Updated readme [skip ci] 2023-03-31 16:38:09 -07:00
Andrew Kane
30f2893aeb Updated readme [skip ci] 2023-03-31 16:30:42 -07:00
Andrew Kane
5d0f88529e Updated readme [skip ci] 2023-03-31 16:28:27 -07:00
Andrew Kane
1d020abdd1 Added auto-vectorized comments [skip ci] 2023-03-31 16:20:10 -07:00
Andrew Kane
d0fdd42652 Added comment [skip ci] 2023-03-31 13:37:33 -07:00
Andrew Kane
8473468925 Added instructions for Yum - #76 2023-03-30 15:39:27 -07:00
Andrew Kane
9c01524466 Updated readme [skip ci] 2023-03-26 23:39:09 -07:00
Andrew Kane
121baa411e Added debug message for index scan 2023-03-26 12:45:44 -07:00
Andrew Kane
50005d7326 Updated CI [skip ci] 2023-03-26 09:46:21 -07:00
Andrew Kane
ec12d79cbc Added link to pgvector-perl [skip ci] 2023-03-23 21:42:43 -07:00
Andrew Kane
d3eb56df07 Improved tests 2023-03-22 16:35:56 -07:00
Andrew Kane
13f7aa50c3 Added Render link [skip ci] 2023-03-22 16:29:06 -07:00
Andrew Kane
81e9e72fbc Updated guidance on lists [skip ci] 2023-03-22 14:00:47 -07:00
Andrew Kane
8d95510302 Updated readme [skip ci] 2023-03-22 13:32:25 -07:00
Andrew Kane
53bb2ed0cd Updated Homebrew instructions [skip ci] 2023-03-21 13:20:57 -07:00
11 changed files with 348 additions and 126 deletions

View File

@@ -17,7 +17,7 @@ jobs:
- postgres: 12
os: ubuntu-20.04
- postgres: 11
os: ubuntu-18.04
os: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: ankane/setup-postgres@v1

View File

@@ -1,3 +1,8 @@
## 0.4.2 (unreleased)
- Added notice when index created with little data
- Fixed installation error with Postgres 12.0-12.2
## 0.4.1 (2023-03-21)
- Improved performance of cosine distance

View File

@@ -1,9 +1,11 @@
FROM postgres:15
ARG PG_MAJOR=15
FROM postgres:$PG_MAJOR
ARG PG_MAJOR
COPY . /tmp/pgvector
RUN apt-get update && \
apt-get install -y --no-install-recommends build-essential postgresql-server-dev-15 && \
apt-get install -y --no-install-recommends build-essential postgresql-server-dev-$PG_MAJOR && \
cd /tmp/pgvector && \
make clean && \
make OPTFLAGS="" && \
@@ -11,6 +13,6 @@ RUN apt-get update && \
mkdir /usr/share/doc/pgvector && \
cp LICENSE README.md /usr/share/doc/pgvector && \
rm -r /tmp/pgvector && \
apt-get remove -y build-essential postgresql-server-dev-15 && \
apt-get remove -y build-essential postgresql-server-dev-$PG_MAJOR && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/*

View File

@@ -14,6 +14,7 @@ OPTFLAGS = -march=native
# Mac ARM doesn't support -march=native
ifeq ($(shell uname -s), Darwin)
ifeq ($(shell uname -p), arm)
# no difference with -march=armv8.5-a
OPTFLAGS =
endif
endif

243
README.md
View File

@@ -2,13 +2,11 @@
Open-source vector similarity search for Postgres
```sql
CREATE TABLE items (embedding vector(3));
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops);
SELECT * FROM items ORDER BY embedding <-> '[1,2,3]' LIMIT 5;
```
Supports
Supports L2 distance, inner product, and cosine distance
- exact and approximate nearest neighbor search
- L2 distance, inner product, and cosine distance
- any [language](#languages) with a Postgres client
[![Build Status](https://github.com/pgvector/pgvector/workflows/build/badge.svg?branch=master)](https://github.com/pgvector/pgvector/actions)
@@ -17,6 +15,7 @@ Supports L2 distance, inner product, and cosine distance
Compile and install the extension (supports Postgres 11+)
```sh
cd /tmp
git clone --branch v0.4.1 https://github.com/pgvector/pgvector.git
cd pgvector
make
@@ -29,41 +28,88 @@ Then load it in databases where you want to use it
CREATE EXTENSION vector;
```
You can also install it with [Docker](#docker), [Homebrew](#homebrew), [PGXN](#pgxn), or [conda-forge](#conda-forge)
See the [installation notes](#installation-notes) if you run into issues
You can also install it with [Docker](#docker), [Homebrew](#homebrew), [PGXN](#pgxn), [Yum](#yum), or [conda-forge](#conda-forge)
## Getting Started
Create a vector column with 3 dimensions
```sql
CREATE TABLE items (embedding vector(3));
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
```
Insert values
Insert vectors
```sql
INSERT INTO items VALUES ('[1,2,3]'), ('[4,5,6]');
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
```
Get the nearest neighbor by L2 distance
Get the nearest neighbors by L2 distance
```sql
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 1;
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
```
Also supports inner product (`<#>`) and cosine distance (`<=>`)
Note: `<#>` returns the negative inner product since Postgres only supports `ASC` order index scans on operators
## Querying
## Storing
Use a `SELECT` clause to get the distance
Create a new table with a vector column
```sql
SELECT embedding <-> '[3,1,2]' AS distance FROM items;
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
```
Use a `WHERE` clause to get rows within a certain distance
Or add a vector column to an existing table
```sql
ALTER TABLE items ADD COLUMN embedding vector(3);
```
Insert vectors
```sql
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
```
Upsert vectors
```sql
INSERT INTO items (id, embedding) VALUES (1, '[1,2,3]'), (2, '[4,5,6]')
ON CONFLICT (id) DO UPDATE SET embedding = EXCLUDED.embedding;
```
Update vectors
```sql
UPDATE items SET embedding = '[1,2,3]' WHERE id = 1;
```
Delete vectors
```sql
DELETE FROM items WHERE id = 1;
```
## Querying
Get the nearest neighbors to a vector
```sql
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
```
Get the nearest neighbors to a row
```sql
SELECT * FROM items WHERE id != 1 ORDER BY embedding <-> (SELECT embedding FROM items WHERE id = 1) LIMIT 5;
```
Get rows within a certain distance
```sql
SELECT * FROM items WHERE embedding <-> '[3,1,2]' < 5;
@@ -71,61 +117,89 @@ 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
#### Distances
Get the distance
```sql
SELECT embedding <-> '[3,1,2]' AS distance FROM items;
```
For inner product, multiply by -1 (since `<#>` returns the negative inner product)
```sql
SELECT (embedding <#> '[3,1,2]') * -1 AS inner_product FROM items;
```
For cosine similarity, use 1 - cosine distance
```sql
SELECT 1 - (embedding <=> '[3,1,2]') AS cosine_similarity FROM items;
```
#### Aggregates
Average vectors
```sql
SELECT AVG(embedding) FROM items;
```
Average groups of vectors
```sql
SELECT category_id, AVG(embedding) FROM items GROUP BY category_id;
```
## Indexing
Speed up queries with an approximate index. Add an index for each distance function you want to use.
By default, pgvector performs exact nearest neighbor search, which provides perfect recall.
You can add an index to use approximate nearest neighbor search, which trades some recall for performance. Unlike typical indexes, you will see different results for queries after adding an approximate index.
Three keys to achieving good recall are:
1. Create the index *after* the table has some data
2. Choose an appropriate number of lists - a good place to start is `rows / 1000` for up to 1M rows and `sqrt(rows)` for over 1M rows
3. When querying, specify an appropriate number of [probes](#query-options) (higher is better for recall, lower is better for speed) - a good place to start is `lists / 10` for up to 1M rows and `sqrt(lists)` for over 1M rows
Add an index for each distance function you want to use.
L2 distance
```sql
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops);
```
Inner product
```sql
CREATE INDEX ON items USING ivfflat (embedding vector_ip_ops);
```
Cosine distance
```sql
CREATE INDEX ON items USING ivfflat (embedding vector_cosine_ops);
```
Indexes should be created after the table has some data for optimal clustering. Also, unlike typical indexes which only affect performance, you may see different results for queries after adding an approximate index. Vectors with up to 2,000 dimensions can be indexed.
### Index Options
Specify the number of inverted lists (100 by default)
```sql
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)`
Inner product
```sql
CREATE INDEX ON items USING ivfflat (embedding vector_ip_ops) WITH (lists = 100);
```
Cosine distance
```sql
CREATE INDEX ON items USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
```
Vectors with up to 2,000 dimensions can be indexed.
### Query Options
Specify the number of probes (1 by default)
```sql
SET ivfflat.probes = 1;
SET ivfflat.probes = 10;
```
A higher value improves 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 wont use the index)
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 wont use the index)
Use `SET LOCAL` inside a transaction to set it for a single query
```sql
BEGIN;
SET LOCAL ivfflat.probes = 1;
SET LOCAL ivfflat.probes = 10;
SELECT ...
COMMIT;
```
@@ -158,7 +232,7 @@ SELECT * FROM items WHERE category_id = 123 ORDER BY embedding <-> '[3,1,2]' LIM
can be indexed with:
```sql
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WHERE (category_id = 123);
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100) WHERE (category_id = 123);
```
To index many different values of `category_id`, consider [partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html) on `category_id`.
@@ -169,24 +243,34 @@ CREATE TABLE items (embedding vector(3), category_id int) PARTITION BY LIST(cate
## Performance
Use `EXPLAIN ANALYZE` to debug performance.
```sql
EXPLAIN ANALYZE SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
```
### Exact Search
To speed up queries without an index, increase `max_parallel_workers_per_gather`.
```sql
SET max_parallel_workers_per_gather = 4;
```
If vectors are normalized to length 1 (like [OpenAI embeddings](https://platform.openai.com/docs/guides/embeddings/which-distance-function-should-i-use)), use inner product for best performance.
```sql
SELECT * FROM items ORDER BY embedding <#> '[3,1,2]' LIMIT 5;
```
### Approximate Search
To speed up queries with an index, increase the number of inverted lists (at the expense of recall).
```sql
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.
@@ -195,17 +279,21 @@ Language | Libraries / Examples
--- | ---
C++ | [pgvector-cpp](https://github.com/pgvector/pgvector-cpp)
C# | [pgvector-dotnet](https://github.com/pgvector/pgvector-dotnet)
Crystal | [pgvector-crystal](https://github.com/pgvector/pgvector-crystal)
Elixir | [pgvector-elixir](https://github.com/pgvector/pgvector-elixir)
Go | [pgvector-go](https://github.com/pgvector/pgvector-go)
Haskell | [pgvector-haskell](https://github.com/pgvector/pgvector-haskell)
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)
Swift | [pgvector-swift](https://github.com/pgvector/pgvector-swift)
## Frequently Asked Questions
@@ -256,6 +344,42 @@ Function | Description
--- | ---
avg(vector) → vector | arithmetic mean
## Installation Notes
### Postgres Location
If your machine has multiple Postgres installations, specify the path to [pg_config](https://www.postgresql.org/docs/current/app-pgconfig.html) with:
```sh
export PG_CONFIG=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config
```
Then re-run the installation instructions (run `make clean` before `make` if needed)
### Missing Header
If compilation fails with `fatal error: postgres.h: No such file or directory`, make sure Postgres development files are installed on the server.
For Ubuntu and Debian, use:
```sh
sudo apt-get install postgresql-server-dev-15
```
Note: Replace `15` with your Postgres server version
### Windows
Support for Windows is currently experimental. Use `nmake` to build:
```cmd
set "PGROOT=C:\Program Files\PostgreSQL\15"
git clone --branch v0.4.1 https://github.com/pgvector/pgvector.git
cd pgvector
nmake /F Makefile.win
nmake /F Makefile.win install
```
## Additional Installation Methods
### Docker
@@ -281,7 +405,7 @@ docker build -t pgvector .
With Homebrew Postgres, you can use:
```sh
brew install pgvector/brew/pgvector
brew install pgvector
```
### PGXN
@@ -292,6 +416,18 @@ Install from the [PostgreSQL Extension Network](https://pgxn.org/dist/vector) wi
pgxn install vector
```
### Yum
RPM packages are available from the [PostgreSQL Yum Repository](https://yum.postgresql.org/). Follow the [setup instructions](https://www.postgresql.org/download/linux/redhat/) for your distribution and run:
```sh
sudo yum install pgvector_15
# or
sudo dnf install pgvector_15
```
Note: Replace `15` with your Postgres server version
### conda-forge
With Conda Postgres, install from [conda-forge](https://anaconda.org/conda-forge/pgvector) with:
@@ -310,8 +446,9 @@ To request a new extension on other providers:
- 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)
- DigitalOcean Managed Databases - vote or comment on [this page](https://ideas.digitalocean.com/app-framework-services/p/pgvector-extension-for-postgresql)
- 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)
- Heroku Postgres - vote or comment on [this page](https://github.com/heroku/roadmap/issues/156)
## Upgrading

View File

@@ -431,8 +431,18 @@ ComputeCenters(IvfflatBuildState * buildstate)
/* TODO Ensure within maintenance_work_mem */
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions);
if (buildstate->heap != NULL)
{
SampleRows(buildstate);
if (buildstate->samples->length < buildstate->lists)
{
ereport(NOTICE,
(errmsg("ivfflat index created with little data"),
errdetail("this will cause poor recall"),
errhint("drop the index until the table has more data")));
}
}
/* Calculate centers */
IvfflatBench("k-means", IvfflatKmeans(buildstate->index, buildstate->samples, buildstate->centers));

View File

@@ -187,6 +187,14 @@ typedef struct IvfflatScanList
double distance;
} IvfflatScanList;
typedef struct IvfflatScanItem
{
pairingheap_node ph_node;
BlockNumber searchPage;
double distance;
ItemPointerData tid;
} IvfflatScanItem;
typedef struct IvfflatScanOpaqueData
{
int probes;
@@ -204,6 +212,13 @@ typedef struct IvfflatScanOpaqueData
FmgrInfo *normprocinfo;
Oid collation;
/* Items */
int maxItems;
int itemCount;
pairingheap *itemQueue;
IvfflatScanItem *items;
IvfflatScanItem **sortedItems;
/* Lists */
pairingheap *listQueue;
IvfflatScanList lists[FLEXIBLE_ARRAY_MEMBER]; /* must come last */

View File

@@ -26,6 +26,21 @@ CompareLists(const pairingheap_node *a, const pairingheap_node *b, void *arg)
return 0;
}
/*
* Compare item distances
*/
static int
CompareItems(const pairingheap_node *a, const pairingheap_node *b, void *arg)
{
if (((const IvfflatScanItem *) a)->distance > ((const IvfflatScanItem *) b)->distance)
return 1;
if (((const IvfflatScanItem *) a)->distance < ((const IvfflatScanItem *) b)->distance)
return -1;
return 0;
}
/*
* Get lists and sort by distance
*/
@@ -111,12 +126,10 @@ GetScanItems(IndexScanDesc scan, Datum value)
Datum datum;
bool isnull;
TupleDesc tupdesc = RelationGetDescr(scan->indexRelation);
#if PG_VERSION_NUM >= 120000
TupleTableSlot *slot = MakeSingleTupleTableSlot(so->tupdesc, &TTSOpsVirtual);
#else
TupleTableSlot *slot = MakeSingleTupleTableSlot(so->tupdesc);
#endif
int i;
double distance;
IvfflatScanItem *scanitem;
double maxDistance = DBL_MAX;
/*
* Reuse same set of shared buffers for scan
@@ -142,23 +155,40 @@ GetScanItems(IndexScanDesc scan, Datum value)
{
itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offno));
datum = index_getattr(itup, 1, tupdesc, &isnull);
distance = DatumGetFloat8(FunctionCall2Coll(so->procinfo, so->collation, datum, value));
/*
* Add virtual tuple
*
* Use procinfo from the index instead of scan key for
* performance
*/
ExecClearTuple(slot);
slot->tts_values[0] = FunctionCall2Coll(so->procinfo, so->collation, datum, value);
slot->tts_isnull[0] = false;
slot->tts_values[1] = PointerGetDatum(&itup->t_tid);
slot->tts_isnull[1] = false;
slot->tts_values[2] = Int32GetDatum((int) searchPage);
slot->tts_isnull[2] = false;
ExecStoreVirtualTuple(slot);
if (so->itemCount < so->maxItems)
{
scanitem = &so->items[so->itemCount];
scanitem->searchPage = searchPage;
scanitem->tid = itup->t_tid;
scanitem->distance = distance;
so->itemCount++;
tuplesort_puttupleslot(so->sortstate, slot);
/* Add to heap */
pairingheap_add(so->itemQueue, &scanitem->ph_node);
/* Calculate max distance */
if (so->itemCount == so->maxItems)
{
maxDistance = ((IvfflatScanItem *) pairingheap_first(so->itemQueue))->distance;
scanitem = &so->items[so->itemCount];
}
}
else if (distance < maxDistance)
{
/* Reuse */
scanitem->searchPage = searchPage;
scanitem->tid = itup->t_tid;
scanitem->distance = distance;
pairingheap_add(so->itemQueue, &scanitem->ph_node);
/* Remove */
scanitem = (IvfflatScanItem *) pairingheap_remove_first(so->itemQueue);
/* Update max distance */
maxDistance = ((IvfflatScanItem *) pairingheap_first(so->itemQueue))->distance;
}
}
searchPage = IvfflatPageGetOpaque(page)->nextblkno;
@@ -167,7 +197,10 @@ GetScanItems(IndexScanDesc scan, Datum value)
}
}
tuplesort_performsort(so->sortstate);
for (i = 0; i < so->itemCount; i++)
so->sortedItems[i] = (IvfflatScanItem *) pairingheap_remove_first(so->itemQueue);
Assert(pairingheap_is_empty(so->itemQueue));
}
/*
@@ -179,10 +212,6 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
IndexScanDesc scan;
IvfflatScanOpaque so;
int lists;
AttrNumber attNums[] = {1};
Oid sortOperators[] = {Float8LessOperator};
Oid sortCollations[] = {InvalidOid};
bool nullsFirstFlags[] = {false};
int probes = ivfflat_probes;
scan = RelationGetIndexScan(index, nkeys, norderbys);
@@ -201,27 +230,14 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
so->normprocinfo = IvfflatOptionalProcInfo(index, IVFFLAT_NORM_PROC);
so->collation = index->rd_indcollation[0];
/* Create tuple description for sorting */
#if PG_VERSION_NUM >= 120000
so->tupdesc = CreateTemplateTupleDesc(3);
#else
so->tupdesc = CreateTemplateTupleDesc(3, false);
#endif
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
TupleDescInitEntry(so->tupdesc, (AttrNumber) 3, "indexblkno", INT4OID, -1, 0);
/* Prep sort */
so->sortstate = tuplesort_begin_heap(so->tupdesc, 1, attNums, sortOperators, sortCollations, nullsFirstFlags, work_mem, NULL, false);
#if PG_VERSION_NUM >= 120000
so->slot = MakeSingleTupleTableSlot(so->tupdesc, &TTSOpsMinimalTuple);
#else
so->slot = MakeSingleTupleTableSlot(so->tupdesc);
#endif
so->listQueue = pairingheap_allocate(CompareLists, scan);
so->maxItems = 10;
so->itemCount = 0;
so->itemQueue = pairingheap_allocate(CompareItems, scan);
so->items = palloc(sizeof(IvfflatScanItem) * (so->maxItems + 1));
so->sortedItems = palloc(sizeof(IvfflatScanItem *) * so->maxItems);
scan->opaque = so;
return scan;
@@ -235,13 +251,10 @@ ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int
{
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
#if PG_VERSION_NUM >= 130000
if (!so->first)
tuplesort_reset(so->sortstate);
#endif
so->first = true;
pairingheap_reset(so->listQueue);
pairingheap_reset(so->itemQueue);
so->itemCount = 0;
if (keys && scan->numberOfKeys > 0)
memmove(scan->keyData, keys, scan->numberOfKeys * sizeof(ScanKeyData));
@@ -301,15 +314,18 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
pfree(DatumGetPointer(value));
}
if (tuplesort_gettupleslot(so->sortstate, true, false, so->slot, NULL))
if (so->itemCount > 0)
{
ItemPointer tid = (ItemPointer) DatumGetPointer(slot_getattr(so->slot, 2, &so->isnull));
BlockNumber indexblkno = DatumGetInt32(slot_getattr(so->slot, 3, &so->isnull));
IvfflatScanItem *scanitem;
so->itemCount--;
scanitem = so->sortedItems[so->itemCount];
#if PG_VERSION_NUM >= 120000
scan->xs_heaptid = *tid;
scan->xs_heaptid = scanitem->tid;
#else
scan->xs_ctup.t_self = *tid;
scan->xs_ctup.t_self = scanitem->tid;
#endif
if (BufferIsValid(so->buf))
@@ -321,7 +337,7 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
*
* https://www.postgresql.org/docs/current/index-locking.html
*/
so->buf = ReadBuffer(scan->indexRelation, indexblkno);
so->buf = ReadBuffer(scan->indexRelation, scanitem->searchPage);
scan->xs_recheckorderby = false;
return true;
@@ -343,7 +359,10 @@ ivfflatendscan(IndexScanDesc scan)
ReleaseBuffer(so->buf);
pairingheap_free(so->listQueue);
tuplesort_end(so->sortstate);
pairingheap_free(so->itemQueue);
pfree(so->items);
pfree(so->sortedItems);
pfree(so);
scan->opaque = NULL;

View File

@@ -100,7 +100,7 @@ CheckStateArray(ArrayType *statearray, const char *caller)
return (float8 *) ARR_DATA_PTR(statearray);
}
#if PG_VERSION_NUM < 120000
#if PG_VERSION_NUM < 120003
static pg_noinline void
float_overflow_error(void)
{
@@ -467,6 +467,7 @@ l2_distance(PG_FUNCTION_ARGS)
CheckDims(a, b);
/* Auto-vectorized */
for (int i = 0; i < a->dim; i++)
{
diff = ax[i] - bx[i];
@@ -493,6 +494,7 @@ vector_l2_squared_distance(PG_FUNCTION_ARGS)
CheckDims(a, b);
/* Auto-vectorized */
for (int i = 0; i < a->dim; i++)
{
diff = ax[i] - bx[i];
@@ -517,6 +519,7 @@ inner_product(PG_FUNCTION_ARGS)
CheckDims(a, b);
/* Auto-vectorized */
for (int i = 0; i < a->dim; i++)
distance += ax[i] * bx[i];
@@ -538,6 +541,7 @@ vector_negative_inner_product(PG_FUNCTION_ARGS)
CheckDims(a, b);
/* Auto-vectorized */
for (int i = 0; i < a->dim; i++)
distance += ax[i] * bx[i];
@@ -561,6 +565,7 @@ cosine_distance(PG_FUNCTION_ARGS)
CheckDims(a, b);
/* Auto-vectorized */
for (int i = 0; i < a->dim; i++)
{
distance += ax[i] * bx[i];
@@ -587,6 +592,7 @@ vector_spherical_distance(PG_FUNCTION_ARGS)
CheckDims(a, b);
/* Auto-vectorized */
for (int i = 0; i < a->dim; i++)
distance += a->x[i] * b->x[i];
@@ -622,6 +628,7 @@ vector_norm(PG_FUNCTION_ARGS)
float *ax = a->x;
double norm = 0.0;
/* Auto-vectorized */
for (int i = 0; i < a->dim; i++)
norm += ax[i] * ax[i];
@@ -646,6 +653,8 @@ vector_add(PG_FUNCTION_ARGS)
result = InitVector(a->dim);
rx = result->x;
/* Auto-vectorized */
for (int i = 0, imax = a->dim; i < imax; i++)
rx[i] = ax[i] + bx[i];
@@ -670,6 +679,8 @@ vector_sub(PG_FUNCTION_ARGS)
result = InitVector(a->dim);
rx = result->x;
/* Auto-vectorized */
for (int i = 0, imax = a->dim; i < imax; i++)
rx[i] = ax[i] - bx[i];

View File

@@ -22,10 +22,28 @@ SELECT round(vector_norm('[1,1]')::numeric, 5);
1.41421
(1 row)
SELECT round(l2_distance('[1,2]', '[0,0]')::numeric, 5);
round
---------
2.23607
SELECT vector_norm('[3,4]');
vector_norm
-------------
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)
SELECT l2_distance('[1,2]', '[3]');
@@ -38,10 +56,10 @@ SELECT inner_product('[1,2]', '[3,4]');
SELECT inner_product('[1,2]', '[3]');
ERROR: different vector dimensions 2 and 1
SELECT round(cosine_distance('[1,2]', '[2,4]')::numeric, 5);
round
---------
0.00000
SELECT cosine_distance('[1,2]', '[2,4]');
cosine_distance
-----------------
0
(1 row)
SELECT cosine_distance('[1,2]', '[0,0]');

View File

@@ -2,15 +2,19 @@ SELECT '[1,2,3]'::vector + '[4,5,6]';
SELECT '[1,2,3]'::vector - '[4,5,6]';
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 inner_product('[1,2]', '[3,4]');
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,1]', '[1,1]');
SELECT cosine_distance('[1,1]', '[-1,-1]');