mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-22 20:15:46 +08:00
Compare commits
80 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9852351746 | ||
|
|
ecbf46938f | ||
|
|
4ca264ba02 | ||
|
|
50349ed4f5 | ||
|
|
f5458414b8 | ||
|
|
e64ed39acb | ||
|
|
2ee510aa67 | ||
|
|
cad655b77f | ||
|
|
0d025be9d3 | ||
|
|
fed60dce78 | ||
|
|
a37f5eea4a | ||
|
|
4bdb27e85a | ||
|
|
38f869e0bd | ||
|
|
ad8acc00d4 | ||
|
|
01926a418e | ||
|
|
9658d3c1ad | ||
|
|
6b9c6516f4 | ||
|
|
88be03a3fa | ||
|
|
bf5b2c8d7e | ||
|
|
ac65ec2856 | ||
|
|
7bba0e2a01 | ||
|
|
c35e9f3b84 | ||
|
|
9549d93260 | ||
|
|
310809d0e5 | ||
|
|
ce72ca8620 | ||
|
|
4f2c937a1f | ||
|
|
21ca5d3845 | ||
|
|
ff400ce5f1 | ||
|
|
8374498e6c | ||
|
|
c1d6b9b41b | ||
|
|
a77340d40b | ||
|
|
81b68fbf5b | ||
|
|
8ee6d0e596 | ||
|
|
41d11c62d6 | ||
|
|
fa5e90585d | ||
|
|
71d2908be1 | ||
|
|
d0a1c1d0ed | ||
|
|
8063201cdf | ||
|
|
2f52127bee | ||
|
|
4778039ffa | ||
|
|
e3c9d70acc | ||
|
|
5971e28f8c | ||
|
|
2da27e2f01 | ||
|
|
7dd934913d | ||
|
|
9b46987e73 | ||
|
|
868e1f99a4 | ||
|
|
354271178f | ||
|
|
fe9b218903 | ||
|
|
3cdd1c16fb | ||
|
|
4f7a5c92da | ||
|
|
9399575e02 | ||
|
|
e7539fe548 | ||
|
|
61860ee6d9 | ||
|
|
eb99730d87 | ||
|
|
e5da5cc532 | ||
|
|
f59b1184fc | ||
|
|
cab103a2e7 | ||
|
|
0094b70a36 | ||
|
|
46ed56d80f | ||
|
|
d390cc9fa6 | ||
|
|
02ae98791c | ||
|
|
b64c7d6228 | ||
|
|
76f117c81c | ||
|
|
5414b2bc9e | ||
|
|
6ba583b587 | ||
|
|
da88217fd8 | ||
|
|
d5da22f24b | ||
|
|
062e220936 | ||
|
|
3d2b867a58 | ||
|
|
5aeabc3648 | ||
|
|
687ec6d8d7 | ||
|
|
51dc18d5b7 | ||
|
|
cb108ebfd2 | ||
|
|
12bc4eb036 | ||
|
|
fd0e472316 | ||
|
|
9b11b05cd7 | ||
|
|
ab079c1911 | ||
|
|
f4eaa3de1f | ||
|
|
e47675d6ae | ||
|
|
20167ddc33 |
@@ -1,6 +1,6 @@
|
|||||||
root = true
|
root = true
|
||||||
|
|
||||||
[*.{c,h}]
|
[*.{c,h,pl}]
|
||||||
indent_style = tab
|
indent_style = tab
|
||||||
indent_size = tab
|
indent_size = tab
|
||||||
tab_width = 4
|
tab_width = 4
|
||||||
|
|||||||
18
CHANGELOG.md
18
CHANGELOG.md
@@ -1,6 +1,22 @@
|
|||||||
|
## 0.2.6 (unreleased)
|
||||||
|
|
||||||
|
- Switched to mini-batch k-means
|
||||||
|
- Improved performance of index creation for Postgres < 12
|
||||||
|
|
||||||
|
## 0.2.5 (2022-02-11)
|
||||||
|
|
||||||
|
- Reduced memory usage during index creation
|
||||||
|
- Fixed index creation exceeding `maintenance_work_mem`
|
||||||
|
- Fixed error with index creation when lists > 1600
|
||||||
|
|
||||||
|
## 0.2.4 (2022-02-06)
|
||||||
|
|
||||||
|
- Added support for parallel vacuum
|
||||||
|
- Fixed issue with index not reusing space
|
||||||
|
|
||||||
## 0.2.3 (2022-01-30)
|
## 0.2.3 (2022-01-30)
|
||||||
|
|
||||||
- Added build progress for Postgres 12+
|
- Added indexing progress for Postgres 12+
|
||||||
- Improved interrupt handling during index creation
|
- Improved interrupt handling during index creation
|
||||||
|
|
||||||
## 0.2.2 (2022-01-15)
|
## 0.2.2 (2022-01-15)
|
||||||
|
|||||||
@@ -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.2.3",
|
"version": "0.2.5",
|
||||||
"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.2.3",
|
"version": "0.2.5",
|
||||||
"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.2.3
|
EXTVERSION = 0.2.5
|
||||||
|
|
||||||
MODULE_big = vector
|
MODULE_big = vector
|
||||||
DATA = $(wildcard sql/*--*.sql)
|
DATA = $(wildcard sql/*--*.sql)
|
||||||
@@ -21,7 +21,7 @@ endif
|
|||||||
# For auto-vectorization:
|
# For auto-vectorization:
|
||||||
# - GCC (needs -ftree-vectorize OR -O3) - https://gcc.gnu.org/projects/tree-ssa/vectorization.html
|
# - GCC (needs -ftree-vectorize OR -O3) - https://gcc.gnu.org/projects/tree-ssa/vectorization.html
|
||||||
# - Clang (could use pragma instead) - https://llvm.org/docs/Vectorizers.html
|
# - Clang (could use pragma instead) - https://llvm.org/docs/Vectorizers.html
|
||||||
PG_CFLAGS = $(OPTFLAGS) -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math
|
PG_CFLAGS += $(OPTFLAGS) -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math
|
||||||
|
|
||||||
# Debug GCC auto-vectorization
|
# Debug GCC auto-vectorization
|
||||||
# PG_CFLAGS += -fopt-info-vec
|
# PG_CFLAGS += -fopt-info-vec
|
||||||
|
|||||||
36
README.md
36
README.md
@@ -17,7 +17,7 @@ Supports L2 distance, inner product, and cosine distance
|
|||||||
Compile and install the extension (supports Postgres 9.6+)
|
Compile and install the extension (supports Postgres 9.6+)
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone --branch v0.2.3 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.2.5 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
make
|
make
|
||||||
make install # may need sudo
|
make install # may need sudo
|
||||||
@@ -77,19 +77,7 @@ Cosine distance
|
|||||||
CREATE INDEX ON table USING ivfflat (column vector_cosine_ops);
|
CREATE INDEX ON table USING ivfflat (column vector_cosine_ops);
|
||||||
```
|
```
|
||||||
|
|
||||||
Indexes should be created after the table has data for optimal clustering. If the distribution of data changes significantly, you can reindex without downtime:
|
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.
|
||||||
|
|
||||||
```sql
|
|
||||||
-- Postgres 12+
|
|
||||||
REINDEX INDEX CONCURRENTLY index_name;
|
|
||||||
|
|
||||||
-- Postgres < 12
|
|
||||||
CREATE INDEX CONCURRENTLY temp_name ON table USING ivfflat (column opclass);
|
|
||||||
DROP INDEX CONCURRENTLY index_name;
|
|
||||||
ALTER INDEX temp_name RENAME TO index_name;
|
|
||||||
```
|
|
||||||
|
|
||||||
Also, unlike typical indexes which only affect performance, you may see different results for queries after adding an approximate index.
|
|
||||||
|
|
||||||
### Index Options
|
### Index Options
|
||||||
|
|
||||||
@@ -120,7 +108,7 @@ SELECT ...
|
|||||||
COMMIT;
|
COMMIT;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Indexing Progress [unreleased]
|
### Indexing Progress
|
||||||
|
|
||||||
Check [indexing progress](https://www.postgresql.org/docs/current/progress-reporting.html#CREATE-INDEX-PROGRESS-REPORTING) with Postgres 12+
|
Check [indexing progress](https://www.postgresql.org/docs/current/progress-reporting.html#CREATE-INDEX-PROGRESS-REPORTING) with Postgres 12+
|
||||||
|
|
||||||
@@ -131,10 +119,9 @@ SELECT phase, tuples_done, tuples_total FROM pg_stat_progress_create_index;
|
|||||||
The phases are:
|
The phases are:
|
||||||
|
|
||||||
1. `initializing`
|
1. `initializing`
|
||||||
2. `sampling table`
|
2. `performing k-means`
|
||||||
3. `performing k-means`
|
3. `sorting tuples`
|
||||||
4. `sorting tuples`
|
4. `loading tuples`
|
||||||
5. `loading tuples`
|
|
||||||
|
|
||||||
Note: `tuples_done` and `tuples_total` are only populated during the `loading tuples` phase
|
Note: `tuples_done` and `tuples_total` are only populated during the `loading tuples` phase
|
||||||
|
|
||||||
@@ -194,6 +181,7 @@ Libraries that use pgvector:
|
|||||||
|
|
||||||
- [pgvector-python](https://github.com/pgvector/pgvector-python) (Python)
|
- [pgvector-python](https://github.com/pgvector/pgvector-python) (Python)
|
||||||
- [Neighbor](https://github.com/ankane/neighbor) (Ruby)
|
- [Neighbor](https://github.com/ankane/neighbor) (Ruby)
|
||||||
|
- [pgvector-ruby](https://github.com/pgvector/pgvector-ruby) (Ruby)
|
||||||
- [pgvector-node](https://github.com/pgvector/pgvector-node) (Node.js)
|
- [pgvector-node](https://github.com/pgvector/pgvector-node) (Node.js)
|
||||||
- [pgvector-go](https://github.com/pgvector/pgvector-go) (Go)
|
- [pgvector-go](https://github.com/pgvector/pgvector-go) (Go)
|
||||||
- [pgvector-rust](https://github.com/pgvector/pgvector-rust) (Rust)
|
- [pgvector-rust](https://github.com/pgvector/pgvector-rust) (Rust)
|
||||||
@@ -231,7 +219,7 @@ This adds pgvector to the [Postgres image](https://hub.docker.com/_/postgres).
|
|||||||
You can also build the image manually
|
You can also build the image manually
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone --branch v0.2.3 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.2.5 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
docker build -t pgvector .
|
docker build -t pgvector .
|
||||||
```
|
```
|
||||||
@@ -275,7 +263,7 @@ Thanks to:
|
|||||||
|
|
||||||
- [PASE: PostgreSQL Ultra-High-Dimensional Approximate Nearest Neighbor Search Extension](https://dl.acm.org/doi/pdf/10.1145/3318464.3386131)
|
- [PASE: PostgreSQL Ultra-High-Dimensional Approximate Nearest Neighbor Search Extension](https://dl.acm.org/doi/pdf/10.1145/3318464.3386131)
|
||||||
- [Faiss: A Library for Efficient Similarity Search and Clustering of Dense Vectors](https://github.com/facebookresearch/faiss)
|
- [Faiss: A Library for Efficient Similarity Search and Clustering of Dense Vectors](https://github.com/facebookresearch/faiss)
|
||||||
- [Using the Triangle Inequality to Accelerate k-means](https://www.aaai.org/Papers/ICML/2003/ICML03-022.pdf)
|
- [Web-Scale k-means Clustering](https://www.eecs.tufts.edu/~dsculley/papers/fastkmeans.pdf)
|
||||||
- [k-means++: The Advantage of Careful Seeding](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf)
|
- [k-means++: The Advantage of Careful Seeding](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf)
|
||||||
- [Concept Decompositions for Large Sparse Text Data using Clustering](https://www.cs.utexas.edu/users/inderjit/public_papers/concept_mlj.pdf)
|
- [Concept Decompositions for Large Sparse Text Data using Clustering](https://www.cs.utexas.edu/users/inderjit/public_papers/concept_mlj.pdf)
|
||||||
|
|
||||||
@@ -315,6 +303,12 @@ make installcheck REGRESS=functions # regression test
|
|||||||
make prove_installcheck PROVE_TESTS=test/t/001_wal.pl # TAP test
|
make prove_installcheck PROVE_TESTS=test/t/001_wal.pl # TAP test
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To enable benchmarking:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
make clean && PG_CFLAGS=-DIVFFLAT_BENCH make && make install
|
||||||
|
```
|
||||||
|
|
||||||
Resources for contributors
|
Resources for contributors
|
||||||
|
|
||||||
- [Extension Building Infrastructure](https://www.postgresql.org/docs/current/extend-pgxs.html)
|
- [Extension Building Infrastructure](https://www.postgresql.org/docs/current/extend-pgxs.html)
|
||||||
|
|||||||
2
sql/vector--0.2.3--0.2.4.sql
Normal file
2
sql/vector--0.2.3--0.2.4.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.2.4'" to load this file. \quit
|
||||||
2
sql/vector--0.2.4--0.2.5.sql
Normal file
2
sql/vector--0.2.4--0.2.5.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.2.5'" to load this file. \quit
|
||||||
218
src/ivfbuild.c
218
src/ivfbuild.c
@@ -36,97 +36,11 @@
|
|||||||
#define CALLBACK_ITEM_POINTER HeapTuple hup
|
#define CALLBACK_ITEM_POINTER HeapTuple hup
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
* Update build phase progress
|
|
||||||
*/
|
|
||||||
static inline void
|
|
||||||
UpdateProgress(int index, int64 val)
|
|
||||||
{
|
|
||||||
#if PG_VERSION_NUM >= 120000
|
#if PG_VERSION_NUM >= 120000
|
||||||
pgstat_progress_update_param(index, val);
|
#define UpdateProgress(index, val) pgstat_progress_update_param(index, val)
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Callback for sampling
|
|
||||||
*/
|
|
||||||
static void
|
|
||||||
SampleCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values,
|
|
||||||
bool *isnull, bool tupleIsAlive, void *state)
|
|
||||||
{
|
|
||||||
IvfflatBuildState *buildstate = (IvfflatBuildState *) state;
|
|
||||||
VectorArray samples = buildstate->samples;
|
|
||||||
int targsamples = samples->maxlen;
|
|
||||||
Datum value = values[0];
|
|
||||||
|
|
||||||
/* Skip nulls */
|
|
||||||
if (isnull[0])
|
|
||||||
return;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Normalize with KMEANS_NORM_PROC since spherical distance function
|
|
||||||
* expects unit vectors
|
|
||||||
*/
|
|
||||||
if (buildstate->kmeansnormprocinfo != NULL)
|
|
||||||
{
|
|
||||||
if (!IvfflatNormValue(buildstate->kmeansnormprocinfo, buildstate->collation, &value, buildstate->normvec))
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (samples->length < targsamples)
|
|
||||||
{
|
|
||||||
VectorArraySet(samples, samples->length, DatumGetVector(value));
|
|
||||||
samples->length++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (buildstate->rowstoskip < 0)
|
|
||||||
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, samples->length, targsamples);
|
|
||||||
|
|
||||||
if (buildstate->rowstoskip <= 0)
|
|
||||||
{
|
|
||||||
int k = (int) (targsamples * sampler_random_fract(buildstate->rstate.randstate));
|
|
||||||
|
|
||||||
Assert(k >= 0 && k < targsamples);
|
|
||||||
VectorArraySet(samples, k, DatumGetVector(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
buildstate->rowstoskip -= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Sample rows with same logic as ANALYZE
|
|
||||||
*/
|
|
||||||
static void
|
|
||||||
SampleRows(IvfflatBuildState * buildstate)
|
|
||||||
{
|
|
||||||
int targsamples = buildstate->samples->maxlen;
|
|
||||||
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
|
||||||
|
|
||||||
UpdateProgress(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_IVFFLAT_PHASE_SAMPLE);
|
|
||||||
|
|
||||||
buildstate->rowstoskip = -1;
|
|
||||||
|
|
||||||
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, random());
|
|
||||||
|
|
||||||
reservoir_init_selection_state(&buildstate->rstate, targsamples);
|
|
||||||
while (BlockSampler_HasMore(&buildstate->bs))
|
|
||||||
{
|
|
||||||
BlockNumber targblock = BlockSampler_Next(&buildstate->bs);
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 120000
|
|
||||||
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
|
||||||
false, true, true, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
|
||||||
#elif PG_VERSION_NUM >= 110000
|
|
||||||
IndexBuildHeapRangeScan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
|
||||||
true, true, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
|
||||||
#else
|
#else
|
||||||
IndexBuildHeapRangeScan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
#define UpdateProgress(index, val) ((void)val)
|
||||||
true, true, targblock, 1, SampleCallback, (void *) buildstate);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Callback for table_index_build_scan
|
* Callback for table_index_build_scan
|
||||||
@@ -170,16 +84,20 @@ BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef IVFFLAT_KMEANS_DEBUG
|
||||||
|
buildstate->inertia += minDistance;
|
||||||
|
buildstate->listSums[closestCenter] += minDistance;
|
||||||
|
buildstate->listCounts[closestCenter]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Create a virtual tuple */
|
/* Create a virtual tuple */
|
||||||
ExecClearTuple(slot);
|
ExecClearTuple(slot);
|
||||||
slot->tts_values[0] = Int32GetDatum(closestCenter);
|
slot->tts_values[0] = Int32GetDatum(closestCenter);
|
||||||
slot->tts_isnull[0] = false;
|
slot->tts_isnull[0] = false;
|
||||||
slot->tts_values[1] = Int32GetDatum(ItemPointerGetBlockNumberNoCheck(tid));
|
slot->tts_values[1] = PointerGetDatum(tid);
|
||||||
slot->tts_isnull[1] = false;
|
slot->tts_isnull[1] = false;
|
||||||
slot->tts_values[2] = Int32GetDatum(ItemPointerGetOffsetNumberNoCheck(tid));
|
slot->tts_values[2] = value;
|
||||||
slot->tts_isnull[2] = false;
|
slot->tts_isnull[2] = false;
|
||||||
slot->tts_values[3] = value;
|
|
||||||
slot->tts_isnull[3] = false;
|
|
||||||
ExecStoreVirtualTuple(slot);
|
ExecStoreVirtualTuple(slot);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -201,8 +119,6 @@ GetNextTuple(Tuplesortstate *sortstate, TupleDesc tupdesc, TupleTableSlot *slot,
|
|||||||
{
|
{
|
||||||
Datum value;
|
Datum value;
|
||||||
bool isnull;
|
bool isnull;
|
||||||
int tupblk;
|
|
||||||
int tupoff;
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 100000
|
#if PG_VERSION_NUM >= 100000
|
||||||
if (tuplesort_gettupleslot(sortstate, true, false, slot, NULL))
|
if (tuplesort_gettupleslot(sortstate, true, false, slot, NULL))
|
||||||
@@ -211,13 +127,11 @@ GetNextTuple(Tuplesortstate *sortstate, TupleDesc tupdesc, TupleTableSlot *slot,
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
*list = DatumGetInt32(slot_getattr(slot, 1, &isnull));
|
*list = DatumGetInt32(slot_getattr(slot, 1, &isnull));
|
||||||
tupblk = DatumGetInt32(slot_getattr(slot, 2, &isnull));
|
value = slot_getattr(slot, 3, &isnull);
|
||||||
tupoff = DatumGetInt32(slot_getattr(slot, 3, &isnull));
|
|
||||||
value = slot_getattr(slot, 4, &isnull);
|
|
||||||
|
|
||||||
/* Form the index tuple */
|
/* Form the index tuple */
|
||||||
*itup = index_form_tuple(tupdesc, &value, &isnull);
|
*itup = index_form_tuple(tupdesc, &value, &isnull);
|
||||||
ItemPointerSet(&(*itup)->t_tid, tupblk, tupoff);
|
(*itup)->t_tid = *((ItemPointer) DatumGetPointer(slot_getattr(slot, 2, &isnull)));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
*list = -1;
|
*list = -1;
|
||||||
@@ -288,7 +202,7 @@ InsertTuples(Relation index, IvfflatBuildState * buildstate, ForkNumber forkNum)
|
|||||||
IvfflatCommitBuffer(buf, state);
|
IvfflatCommitBuffer(buf, state);
|
||||||
|
|
||||||
/* Set the start and insert pages */
|
/* Set the start and insert pages */
|
||||||
IvfflatUpdateList(index, state, buildstate->listInfo[i], insertPage, startPage, forkNum);
|
IvfflatUpdateList(index, state, buildstate->listInfo[i], insertPage, InvalidBlockNumber, startPage, forkNum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,17 +240,16 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In
|
|||||||
|
|
||||||
/* Create tuple description for sorting */
|
/* Create tuple description for sorting */
|
||||||
#if PG_VERSION_NUM >= 120000
|
#if PG_VERSION_NUM >= 120000
|
||||||
buildstate->tupdesc = CreateTemplateTupleDesc(4);
|
buildstate->tupdesc = CreateTemplateTupleDesc(3);
|
||||||
#else
|
#else
|
||||||
buildstate->tupdesc = CreateTemplateTupleDesc(4, false);
|
buildstate->tupdesc = CreateTemplateTupleDesc(3, false);
|
||||||
#endif
|
#endif
|
||||||
TupleDescInitEntry(buildstate->tupdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
TupleDescInitEntry(buildstate->tupdesc, (AttrNumber) 1, "list", INT4OID, -1, 0);
|
||||||
TupleDescInitEntry(buildstate->tupdesc, (AttrNumber) 2, "blkno", INT4OID, -1, 0);
|
TupleDescInitEntry(buildstate->tupdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
||||||
TupleDescInitEntry(buildstate->tupdesc, (AttrNumber) 3, "offset", INT4OID, -1, 0);
|
|
||||||
#if PG_VERSION_NUM >= 110000
|
#if PG_VERSION_NUM >= 110000
|
||||||
TupleDescInitEntry(buildstate->tupdesc, (AttrNumber) 4, "vector", RelationGetDescr(index)->attrs[0].atttypid, -1, 0);
|
TupleDescInitEntry(buildstate->tupdesc, (AttrNumber) 3, "vector", RelationGetDescr(index)->attrs[0].atttypid, -1, 0);
|
||||||
#else
|
#else
|
||||||
TupleDescInitEntry(buildstate->tupdesc, (AttrNumber) 4, "vector", RelationGetDescr(index)->attrs[0]->atttypid, -1, 0);
|
TupleDescInitEntry(buildstate->tupdesc, (AttrNumber) 3, "vector", RelationGetDescr(index)->attrs[0]->atttypid, -1, 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 120000
|
#if PG_VERSION_NUM >= 120000
|
||||||
@@ -350,6 +263,12 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In
|
|||||||
|
|
||||||
/* Reuse for each tuple */
|
/* Reuse for each tuple */
|
||||||
buildstate->normvec = InitVector(buildstate->dimensions);
|
buildstate->normvec = InitVector(buildstate->dimensions);
|
||||||
|
|
||||||
|
#ifdef IVFFLAT_KMEANS_DEBUG
|
||||||
|
buildstate->inertia = 0;
|
||||||
|
buildstate->listSums = palloc0(sizeof(double) * buildstate->lists);
|
||||||
|
buildstate->listCounts = palloc0(sizeof(int) * buildstate->lists);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -361,33 +280,11 @@ FreeBuildState(IvfflatBuildState * buildstate)
|
|||||||
pfree(buildstate->centers);
|
pfree(buildstate->centers);
|
||||||
pfree(buildstate->listInfo);
|
pfree(buildstate->listInfo);
|
||||||
pfree(buildstate->normvec);
|
pfree(buildstate->normvec);
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
#ifdef IVFFLAT_KMEANS_DEBUG
|
||||||
* Compute centers
|
pfree(buildstate->listSums);
|
||||||
*/
|
pfree(buildstate->listCounts);
|
||||||
static void
|
#endif
|
||||||
ComputeCenters(IvfflatBuildState * buildstate)
|
|
||||||
{
|
|
||||||
int numSamples;
|
|
||||||
|
|
||||||
/* Target 50 samples per list, with at least 10000 samples */
|
|
||||||
/* The number of samples has a large effect on index build time */
|
|
||||||
numSamples = buildstate->lists * 50;
|
|
||||||
if (numSamples < 10000)
|
|
||||||
numSamples = 10000;
|
|
||||||
|
|
||||||
/* Sample samples */
|
|
||||||
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions);
|
|
||||||
if (buildstate->heap != NULL)
|
|
||||||
SampleRows(buildstate);
|
|
||||||
|
|
||||||
/* Calculate centers */
|
|
||||||
UpdateProgress(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_IVFFLAT_PHASE_KMEANS);
|
|
||||||
IvfflatKmeans(buildstate->index, buildstate->samples, buildstate->centers);
|
|
||||||
|
|
||||||
/* Free samples before we allocate more memory */
|
|
||||||
pfree(buildstate->samples);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -463,6 +360,51 @@ CreateListPages(Relation index, VectorArray centers, int dimensions,
|
|||||||
pfree(list);
|
pfree(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print k-means metrics
|
||||||
|
*/
|
||||||
|
#ifdef IVFFLAT_KMEANS_DEBUG
|
||||||
|
static void
|
||||||
|
PrintKmeansMetrics(IvfflatBuildState * buildstate)
|
||||||
|
{
|
||||||
|
elog(INFO, "inertia: %.3e", buildstate->inertia);
|
||||||
|
|
||||||
|
/* Calculate Davies-Bouldin index */
|
||||||
|
if (buildstate->lists > 1)
|
||||||
|
{
|
||||||
|
double db = 0.0;
|
||||||
|
|
||||||
|
/* Calculate average distance */
|
||||||
|
for (int i = 0; i < buildstate->lists; i++)
|
||||||
|
{
|
||||||
|
if (buildstate->listCounts[i] > 0)
|
||||||
|
buildstate->listSums[i] /= buildstate->listCounts[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < buildstate->lists; i++)
|
||||||
|
{
|
||||||
|
double max = 0.0;
|
||||||
|
double distance;
|
||||||
|
|
||||||
|
for (int j = 0; j < buildstate->lists; j++)
|
||||||
|
{
|
||||||
|
if (j == i)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
distance = DatumGetFloat8(FunctionCall2Coll(buildstate->procinfo, buildstate->collation, PointerGetDatum(VectorArrayGet(buildstate->centers, i)), PointerGetDatum(VectorArrayGet(buildstate->centers, j))));
|
||||||
|
distance = (buildstate->listSums[i] + buildstate->listSums[j]) / distance;
|
||||||
|
|
||||||
|
if (distance > max)
|
||||||
|
max = distance;
|
||||||
|
}
|
||||||
|
db += max;
|
||||||
|
}
|
||||||
|
db /= buildstate->lists;
|
||||||
|
elog(INFO, "davies-bouldin: %.3f", db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create entry pages
|
* Create entry pages
|
||||||
*/
|
*/
|
||||||
@@ -497,8 +439,14 @@ CreateEntryPages(IvfflatBuildState * buildstate, ForkNumber forkNum)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sort and insert */
|
/* Sort */
|
||||||
tuplesort_performsort(buildstate->sortstate);
|
tuplesort_performsort(buildstate->sortstate);
|
||||||
|
|
||||||
|
#ifdef IVFFLAT_KMEANS_DEBUG
|
||||||
|
PrintKmeansMetrics(buildstate);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Insert */
|
||||||
InsertTuples(buildstate->index, buildstate, forkNum);
|
InsertTuples(buildstate->index, buildstate, forkNum);
|
||||||
tuplesort_end(buildstate->sortstate);
|
tuplesort_end(buildstate->sortstate);
|
||||||
}
|
}
|
||||||
@@ -512,12 +460,14 @@ BuildIndex(Relation heap, Relation index, IndexInfo *indexInfo,
|
|||||||
{
|
{
|
||||||
InitBuildState(buildstate, heap, index, indexInfo);
|
InitBuildState(buildstate, heap, index, indexInfo);
|
||||||
|
|
||||||
ComputeCenters(buildstate);
|
/* Perform k-means clustering */
|
||||||
|
UpdateProgress(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_IVFFLAT_PHASE_KMEANS);
|
||||||
|
IvfflatBench("k-means", IvfflatKmeans(buildstate));
|
||||||
|
|
||||||
/* Create pages */
|
/* Create pages */
|
||||||
CreateMetaPage(index, buildstate->dimensions, buildstate->lists, forkNum);
|
CreateMetaPage(index, buildstate->dimensions, buildstate->lists, forkNum);
|
||||||
CreateListPages(index, buildstate->centers, buildstate->dimensions, buildstate->lists, forkNum, &buildstate->listInfo);
|
CreateListPages(index, buildstate->centers, buildstate->dimensions, buildstate->lists, forkNum, &buildstate->listInfo);
|
||||||
CreateEntryPages(buildstate, forkNum);
|
IvfflatBench("CreateEntryPages", CreateEntryPages(buildstate, forkNum));
|
||||||
|
|
||||||
FreeBuildState(buildstate);
|
FreeBuildState(buildstate);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,8 +45,6 @@ ivfflatbuildphasename(int64 phasenum)
|
|||||||
{
|
{
|
||||||
case PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE:
|
case PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE:
|
||||||
return "initializing";
|
return "initializing";
|
||||||
case PROGRESS_IVFFLAT_PHASE_SAMPLE:
|
|
||||||
return "sampling table";
|
|
||||||
case PROGRESS_IVFFLAT_PHASE_KMEANS:
|
case PROGRESS_IVFFLAT_PHASE_KMEANS:
|
||||||
return "performing k-means";
|
return "performing k-means";
|
||||||
case PROGRESS_IVFFLAT_PHASE_SORT:
|
case PROGRESS_IVFFLAT_PHASE_SORT:
|
||||||
@@ -159,6 +157,11 @@ ivfflatvalidate(Oid opclassoid)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define index handler
|
||||||
|
*
|
||||||
|
* See https://www.postgresql.org/docs/current/index-api.html
|
||||||
|
*/
|
||||||
PG_FUNCTION_INFO_V1(ivfflathandler);
|
PG_FUNCTION_INFO_V1(ivfflathandler);
|
||||||
Datum
|
Datum
|
||||||
ivfflathandler(PG_FUNCTION_ARGS)
|
ivfflathandler(PG_FUNCTION_ARGS)
|
||||||
@@ -189,16 +192,17 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
|||||||
#endif
|
#endif
|
||||||
#if PG_VERSION_NUM >= 130000
|
#if PG_VERSION_NUM >= 130000
|
||||||
amroutine->amusemaintenanceworkmem = false; /* not used during VACUUM */
|
amroutine->amusemaintenanceworkmem = false; /* not used during VACUUM */
|
||||||
amroutine->amparallelvacuumoptions = VACUUM_OPTION_NO_PARALLEL; /* TODO support parallel */
|
amroutine->amparallelvacuumoptions = VACUUM_OPTION_PARALLEL_BULKDEL;
|
||||||
#endif
|
#endif
|
||||||
amroutine->amkeytype = InvalidOid;
|
amroutine->amkeytype = InvalidOid;
|
||||||
|
|
||||||
|
/* Interface functions */
|
||||||
amroutine->ambuild = ivfflatbuild;
|
amroutine->ambuild = ivfflatbuild;
|
||||||
amroutine->ambuildempty = ivfflatbuildempty;
|
amroutine->ambuildempty = ivfflatbuildempty;
|
||||||
amroutine->aminsert = ivfflatinsert;
|
amroutine->aminsert = ivfflatinsert;
|
||||||
amroutine->ambulkdelete = ivfflatbulkdelete;
|
amroutine->ambulkdelete = ivfflatbulkdelete;
|
||||||
amroutine->amvacuumcleanup = ivfflatvacuumcleanup;
|
amroutine->amvacuumcleanup = ivfflatvacuumcleanup;
|
||||||
amroutine->amcanreturn = NULL;
|
amroutine->amcanreturn = NULL; /* tuple not included in heapsort */
|
||||||
amroutine->amcostestimate = ivfflatcostestimate;
|
amroutine->amcostestimate = ivfflatcostestimate;
|
||||||
amroutine->amoptions = ivfflatoptions;
|
amroutine->amoptions = ivfflatoptions;
|
||||||
amroutine->amproperty = NULL; /* TODO AMPROP_DISTANCE_ORDERABLE */
|
amroutine->amproperty = NULL; /* TODO AMPROP_DISTANCE_ORDERABLE */
|
||||||
@@ -206,6 +210,9 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->ambuildphasename = ivfflatbuildphasename;
|
amroutine->ambuildphasename = ivfflatbuildphasename;
|
||||||
#endif
|
#endif
|
||||||
amroutine->amvalidate = ivfflatvalidate;
|
amroutine->amvalidate = ivfflatvalidate;
|
||||||
|
#if PG_VERSION_NUM >= 140000
|
||||||
|
amroutine->amadjustmembers = NULL;
|
||||||
|
#endif
|
||||||
amroutine->ambeginscan = ivfflatbeginscan;
|
amroutine->ambeginscan = ivfflatbeginscan;
|
||||||
amroutine->amrescan = ivfflatrescan;
|
amroutine->amrescan = ivfflatrescan;
|
||||||
amroutine->amgettuple = ivfflatgettuple;
|
amroutine->amgettuple = ivfflatgettuple;
|
||||||
@@ -213,6 +220,8 @@ ivfflathandler(PG_FUNCTION_ARGS)
|
|||||||
amroutine->amendscan = ivfflatendscan;
|
amroutine->amendscan = ivfflatendscan;
|
||||||
amroutine->ammarkpos = NULL;
|
amroutine->ammarkpos = NULL;
|
||||||
amroutine->amrestrpos = NULL;
|
amroutine->amrestrpos = NULL;
|
||||||
|
|
||||||
|
/* Interface functions to support parallel index scans */
|
||||||
#if PG_VERSION_NUM >= 100000
|
#if PG_VERSION_NUM >= 100000
|
||||||
amroutine->amestimateparallelscan = NULL;
|
amroutine->amestimateparallelscan = NULL;
|
||||||
amroutine->aminitparallelscan = NULL;
|
amroutine->aminitparallelscan = NULL;
|
||||||
|
|||||||
@@ -10,6 +10,14 @@
|
|||||||
#include "utils/tuplesort.h"
|
#include "utils/tuplesort.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
|
#ifdef IVFFLAT_BENCH
|
||||||
|
#include "portability/instr_time.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM < 90600
|
||||||
|
#error "Requires PostgreSQL 9.6+"
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Support functions */
|
/* Support functions */
|
||||||
#define IVFFLAT_DISTANCE_PROC 1
|
#define IVFFLAT_DISTANCE_PROC 1
|
||||||
#define IVFFLAT_NORM_PROC 2
|
#define IVFFLAT_NORM_PROC 2
|
||||||
@@ -29,19 +37,28 @@
|
|||||||
|
|
||||||
/* Build phases */
|
/* Build phases */
|
||||||
/* PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE is 1 */
|
/* PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE is 1 */
|
||||||
#define PROGRESS_IVFFLAT_PHASE_SAMPLE 2
|
#define PROGRESS_IVFFLAT_PHASE_KMEANS 2
|
||||||
#define PROGRESS_IVFFLAT_PHASE_KMEANS 3
|
#define PROGRESS_IVFFLAT_PHASE_SORT 3
|
||||||
#define PROGRESS_IVFFLAT_PHASE_SORT 4
|
#define PROGRESS_IVFFLAT_PHASE_LOAD 4
|
||||||
#define PROGRESS_IVFFLAT_PHASE_LOAD 5
|
|
||||||
|
|
||||||
#define IVFFLAT_LIST_SIZE(_dim) (offsetof(IvfflatListData, center) + VECTOR_SIZE(_dim))
|
#define IVFFLAT_LIST_SIZE(_dim) (offsetof(IvfflatListData, center) + VECTOR_SIZE(_dim))
|
||||||
|
|
||||||
#define IvfflatPageGetOpaque(page) ((IvfflatPageOpaque) PageGetSpecialPointer(page))
|
#define IvfflatPageGetOpaque(page) ((IvfflatPageOpaque) PageGetSpecialPointer(page))
|
||||||
#define IvfflatPageGetMeta(page) ((IvfflatMetaPageData *) PageGetContents(page))
|
#define IvfflatPageGetMeta(page) ((IvfflatMetaPageData *) PageGetContents(page))
|
||||||
|
|
||||||
#if PG_VERSION_NUM < 100000
|
#ifdef IVFFLAT_BENCH
|
||||||
#define ItemPointerGetBlockNumberNoCheck ItemPointerGetBlockNumber
|
#define IvfflatBench(name, code) \
|
||||||
#define ItemPointerGetOffsetNumberNoCheck ItemPointerGetOffsetNumber
|
do { \
|
||||||
|
instr_time start; \
|
||||||
|
instr_time duration; \
|
||||||
|
INSTR_TIME_SET_CURRENT(start); \
|
||||||
|
(code); \
|
||||||
|
INSTR_TIME_SET_CURRENT(duration); \
|
||||||
|
INSTR_TIME_SUBTRACT(duration, start); \
|
||||||
|
elog(INFO, "%s: %.3f ms", name, INSTR_TIME_GET_MILLISEC(duration)); \
|
||||||
|
} while (0)
|
||||||
|
#else
|
||||||
|
#define IvfflatBench(name, code) (code)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Variables */
|
/* Variables */
|
||||||
@@ -97,6 +114,12 @@ typedef struct IvfflatBuildState
|
|||||||
ListInfo *listInfo;
|
ListInfo *listInfo;
|
||||||
Vector *normvec;
|
Vector *normvec;
|
||||||
|
|
||||||
|
#ifdef IVFFLAT_KMEANS_DEBUG
|
||||||
|
double inertia;
|
||||||
|
double *listSums;
|
||||||
|
int *listCounts;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Sampling */
|
/* Sampling */
|
||||||
BlockSamplerData bs;
|
BlockSamplerData bs;
|
||||||
ReservoirStateData rstate;
|
ReservoirStateData rstate;
|
||||||
@@ -138,6 +161,7 @@ typedef IvfflatListData * IvfflatList;
|
|||||||
|
|
||||||
typedef struct IvfflatScanList
|
typedef struct IvfflatScanList
|
||||||
{
|
{
|
||||||
|
pairingheap_node ph_node;
|
||||||
BlockNumber startPage;
|
BlockNumber startPage;
|
||||||
double distance;
|
double distance;
|
||||||
} IvfflatScanList;
|
} IvfflatScanList;
|
||||||
@@ -159,6 +183,8 @@ typedef struct IvfflatScanOpaqueData
|
|||||||
FmgrInfo *normprocinfo;
|
FmgrInfo *normprocinfo;
|
||||||
Oid collation;
|
Oid collation;
|
||||||
|
|
||||||
|
/* Lists */
|
||||||
|
pairingheap *listQueue;
|
||||||
IvfflatScanList lists[FLEXIBLE_ARRAY_MEMBER]; /* must come last */
|
IvfflatScanList lists[FLEXIBLE_ARRAY_MEMBER]; /* must come last */
|
||||||
} IvfflatScanOpaqueData;
|
} IvfflatScanOpaqueData;
|
||||||
|
|
||||||
@@ -173,11 +199,11 @@ typedef IvfflatScanOpaqueData * IvfflatScanOpaque;
|
|||||||
void _PG_init(void);
|
void _PG_init(void);
|
||||||
VectorArray VectorArrayInit(int maxlen, int dimensions);
|
VectorArray VectorArrayInit(int maxlen, int dimensions);
|
||||||
void PrintVectorArray(char *msg, VectorArray arr);
|
void PrintVectorArray(char *msg, VectorArray arr);
|
||||||
void IvfflatKmeans(Relation index, VectorArray samples, VectorArray centers);
|
void IvfflatKmeans(IvfflatBuildState * buildstate);
|
||||||
FmgrInfo *IvfflatOptionalProcInfo(Relation rel, uint16 procnum);
|
FmgrInfo *IvfflatOptionalProcInfo(Relation rel, uint16 procnum);
|
||||||
bool IvfflatNormValue(FmgrInfo *procinfo, Oid collation, Datum *value, Vector * result);
|
bool IvfflatNormValue(FmgrInfo *procinfo, Oid collation, Datum *value, Vector * result);
|
||||||
int IvfflatGetLists(Relation index);
|
int IvfflatGetLists(Relation index);
|
||||||
void IvfflatUpdateList(Relation index, GenericXLogState *state, ListInfo listInfo, BlockNumber insertPage, BlockNumber startPage, ForkNumber forkNum);
|
void IvfflatUpdateList(Relation index, GenericXLogState *state, ListInfo listInfo, BlockNumber insertPage, BlockNumber originalInsertPage, BlockNumber startPage, ForkNumber forkNum);
|
||||||
void IvfflatCommitBuffer(Buffer buf, GenericXLogState *state);
|
void IvfflatCommitBuffer(Buffer buf, GenericXLogState *state);
|
||||||
void IvfflatAppendPage(Relation index, Buffer *buf, Page *page, GenericXLogState **state, ForkNumber forkNum);
|
void IvfflatAppendPage(Relation index, Buffer *buf, Page *page, GenericXLogState **state, ForkNumber forkNum);
|
||||||
Buffer IvfflatNewBuffer(Relation index, ForkNumber forkNum);
|
Buffer IvfflatNewBuffer(Relation index, ForkNumber forkNum);
|
||||||
|
|||||||
@@ -77,11 +77,12 @@ InsertTuple(Relation rel, IndexTuple itup, Relation heapRel, Datum *values)
|
|||||||
Size itemsz;
|
Size itemsz;
|
||||||
BlockNumber insertPage = InvalidBlockNumber;
|
BlockNumber insertPage = InvalidBlockNumber;
|
||||||
ListInfo listInfo;
|
ListInfo listInfo;
|
||||||
bool newPage = false;
|
BlockNumber originalInsertPage;
|
||||||
|
|
||||||
/* Find the insert page - sets the page and list info */
|
/* Find the insert page - sets the page and list info */
|
||||||
FindInsertPage(rel, values, &insertPage, &listInfo);
|
FindInsertPage(rel, values, &insertPage, &listInfo);
|
||||||
Assert(BlockNumberIsValid(insertPage));
|
Assert(BlockNumberIsValid(insertPage));
|
||||||
|
originalInsertPage = insertPage;
|
||||||
|
|
||||||
itemsz = MAXALIGN(IndexTupleSize(itup));
|
itemsz = MAXALIGN(IndexTupleSize(itup));
|
||||||
Assert(itemsz <= BLCKSZ - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(IvfflatPageOpaqueData)));
|
Assert(itemsz <= BLCKSZ - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(IvfflatPageOpaqueData)));
|
||||||
@@ -107,7 +108,6 @@ InsertTuple(Relation rel, IndexTuple itup, Relation heapRel, Datum *values)
|
|||||||
IvfflatAppendPage(rel, &buf, &page, &state, MAIN_FORKNUM);
|
IvfflatAppendPage(rel, &buf, &page, &state, MAIN_FORKNUM);
|
||||||
|
|
||||||
insertPage = BufferGetBlockNumber(buf);
|
insertPage = BufferGetBlockNumber(buf);
|
||||||
newPage = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,8 +118,8 @@ InsertTuple(Relation rel, IndexTuple itup, Relation heapRel, Datum *values)
|
|||||||
IvfflatCommitBuffer(buf, state);
|
IvfflatCommitBuffer(buf, state);
|
||||||
|
|
||||||
/* Update the insert page */
|
/* Update the insert page */
|
||||||
if (newPage)
|
if (insertPage != originalInsertPage)
|
||||||
IvfflatUpdateList(rel, state, listInfo, insertPage, InvalidBlockNumber, MAIN_FORKNUM);
|
IvfflatUpdateList(rel, state, listInfo, insertPage, originalInsertPage, InvalidBlockNumber, MAIN_FORKNUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
439
src/ivfkmeans.c
439
src/ivfkmeans.c
@@ -2,8 +2,20 @@
|
|||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
|
||||||
|
#include "catalog/index.h"
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
|
#include "storage/bufmgr.h"
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 120000
|
||||||
|
#include "access/tableam.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 130000
|
||||||
|
#define CALLBACK_ITEM_POINTER ItemPointer tid
|
||||||
|
#else
|
||||||
|
#define CALLBACK_ITEM_POINTER HeapTuple hup
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize with kmeans++
|
* Initialize with kmeans++
|
||||||
@@ -11,7 +23,7 @@
|
|||||||
* https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf
|
* https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
InitCenters(Relation index, VectorArray samples, VectorArray centers, double *lowerBound)
|
InitCenters(Relation index, VectorArray samples, VectorArray centers)
|
||||||
{
|
{
|
||||||
FmgrInfo *procinfo;
|
FmgrInfo *procinfo;
|
||||||
Oid collation;
|
Oid collation;
|
||||||
@@ -21,7 +33,7 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, double *lo
|
|||||||
double sum;
|
double sum;
|
||||||
double choice;
|
double choice;
|
||||||
Vector *vec;
|
Vector *vec;
|
||||||
double *weight = palloc(samples->length * sizeof(double));
|
float *weight = palloc(samples->length * sizeof(float));
|
||||||
int numCenters = centers->maxlen;
|
int numCenters = centers->maxlen;
|
||||||
int numSamples = samples->length;
|
int numSamples = samples->length;
|
||||||
|
|
||||||
@@ -35,7 +47,7 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, double *lo
|
|||||||
for (j = 0; j < numSamples; j++)
|
for (j = 0; j < numSamples; j++)
|
||||||
weight[j] = DBL_MAX;
|
weight[j] = DBL_MAX;
|
||||||
|
|
||||||
for (i = 0; i < numCenters; i++)
|
for (i = 0; i < numCenters - 1; i++)
|
||||||
{
|
{
|
||||||
CHECK_FOR_INTERRUPTS();
|
CHECK_FOR_INTERRUPTS();
|
||||||
|
|
||||||
@@ -49,9 +61,6 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, double *lo
|
|||||||
/* TODO Use triangle inequality to reduce distance calculations */
|
/* TODO Use triangle inequality to reduce distance calculations */
|
||||||
distance = DatumGetFloat8(FunctionCall2Coll(procinfo, collation, PointerGetDatum(vec), PointerGetDatum(VectorArrayGet(centers, i))));
|
distance = DatumGetFloat8(FunctionCall2Coll(procinfo, collation, PointerGetDatum(vec), PointerGetDatum(VectorArrayGet(centers, i))));
|
||||||
|
|
||||||
/* Set lower bound */
|
|
||||||
lowerBound[j * numCenters + i] = distance;
|
|
||||||
|
|
||||||
/* Use distance squared for weighted probability distribution */
|
/* Use distance squared for weighted probability distribution */
|
||||||
distance *= distance;
|
distance *= distance;
|
||||||
|
|
||||||
@@ -61,10 +70,6 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, double *lo
|
|||||||
sum += weight[j];
|
sum += weight[j];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Only compute lower bound on last iteration */
|
|
||||||
if (i + 1 == numCenters)
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* Choose new center using weighted probability distribution. */
|
/* Choose new center using weighted probability distribution. */
|
||||||
choice = sum * (((double) random()) / MAX_RANDOM_VALUE);
|
choice = sum * (((double) random()) / MAX_RANDOM_VALUE);
|
||||||
for (j = 0; j < numSamples - 1; j++)
|
for (j = 0; j < numSamples - 1; j++)
|
||||||
@@ -121,6 +126,8 @@ QuickCenters(Relation index, VectorArray samples, VectorArray centers)
|
|||||||
FmgrInfo *normprocinfo = IvfflatOptionalProcInfo(index, IVFFLAT_KMEANS_NORM_PROC);
|
FmgrInfo *normprocinfo = IvfflatOptionalProcInfo(index, IVFFLAT_KMEANS_NORM_PROC);
|
||||||
|
|
||||||
/* Copy existing vectors while avoiding duplicates */
|
/* Copy existing vectors while avoiding duplicates */
|
||||||
|
if (samples->length > 0)
|
||||||
|
{
|
||||||
qsort(samples->items, samples->length, VECTOR_SIZE(samples->dim), CompareVectors);
|
qsort(samples->items, samples->length, VECTOR_SIZE(samples->dim), CompareVectors);
|
||||||
for (i = 0; i < samples->length; i++)
|
for (i = 0; i < samples->length; i++)
|
||||||
{
|
{
|
||||||
@@ -132,6 +139,7 @@ QuickCenters(Relation index, VectorArray samples, VectorArray centers)
|
|||||||
centers->length++;
|
centers->length++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Fill remaining with random data */
|
/* Fill remaining with random data */
|
||||||
while (centers->length < centers->maxlen)
|
while (centers->length < centers->maxlen)
|
||||||
@@ -153,82 +161,147 @@ QuickCenters(Relation index, VectorArray samples, VectorArray centers)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Use Elkan for performance. This requires distance function to satisfy triangle inequality.
|
* Callback for sampling
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
SampleCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values,
|
||||||
|
bool *isnull, bool tupleIsAlive, void *state)
|
||||||
|
{
|
||||||
|
IvfflatBuildState *buildstate = (IvfflatBuildState *) state;
|
||||||
|
VectorArray samples = buildstate->samples;
|
||||||
|
int targsamples = samples->maxlen;
|
||||||
|
Datum value = values[0];
|
||||||
|
|
||||||
|
/* Skip nulls */
|
||||||
|
if (isnull[0])
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Normalize with KMEANS_NORM_PROC since spherical distance function
|
||||||
|
* expects unit vectors
|
||||||
|
*/
|
||||||
|
if (buildstate->kmeansnormprocinfo != NULL)
|
||||||
|
{
|
||||||
|
if (!IvfflatNormValue(buildstate->kmeansnormprocinfo, buildstate->collation, &value, buildstate->normvec))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (samples->length < targsamples)
|
||||||
|
{
|
||||||
|
VectorArraySet(samples, samples->length, DatumGetVector(value));
|
||||||
|
samples->length++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (buildstate->rowstoskip < 0)
|
||||||
|
buildstate->rowstoskip = reservoir_get_next_S(&buildstate->rstate, samples->length, targsamples);
|
||||||
|
|
||||||
|
if (buildstate->rowstoskip <= 0)
|
||||||
|
{
|
||||||
|
int k = (int) (targsamples * sampler_random_fract(buildstate->rstate.randstate));
|
||||||
|
|
||||||
|
Assert(k >= 0 && k < targsamples);
|
||||||
|
VectorArraySet(samples, k, DatumGetVector(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
buildstate->rowstoskip -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sample rows with same logic as ANALYZE
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
SampleRows(IvfflatBuildState * buildstate)
|
||||||
|
{
|
||||||
|
int targsamples = buildstate->samples->maxlen;
|
||||||
|
BlockNumber totalblocks = RelationGetNumberOfBlocks(buildstate->heap);
|
||||||
|
|
||||||
|
buildstate->rowstoskip = -1;
|
||||||
|
buildstate->samples->length = 0;
|
||||||
|
|
||||||
|
BlockSampler_Init(&buildstate->bs, totalblocks, targsamples, random());
|
||||||
|
|
||||||
|
reservoir_init_selection_state(&buildstate->rstate, targsamples);
|
||||||
|
while (BlockSampler_HasMore(&buildstate->bs))
|
||||||
|
{
|
||||||
|
BlockNumber targblock = BlockSampler_Next(&buildstate->bs);
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 120000
|
||||||
|
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
||||||
|
false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
||||||
|
#elif PG_VERSION_NUM >= 110000
|
||||||
|
IndexBuildHeapRangeScan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
||||||
|
false, true, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
||||||
|
#else
|
||||||
|
IndexBuildHeapRangeScan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
||||||
|
false, true, targblock, 1, SampleCallback, (void *) buildstate);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Use mini-batch k-means
|
||||||
*
|
*
|
||||||
* We use L2 distance for L2 (not L2 squared like index scan)
|
* We use L2 distance for L2 (not L2 squared like index scan)
|
||||||
* and angular distance for inner product and cosine distance
|
* and angular distance for inner product and cosine distance
|
||||||
*
|
*
|
||||||
* https://www.aaai.org/Papers/ICML/2003/ICML03-022.pdf
|
* https://www.eecs.tufts.edu/~dsculley/papers/fastkmeans.pdf
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
ElkanKmeans(Relation index, VectorArray samples, VectorArray centers)
|
MiniBatchKmeans(IvfflatBuildState * buildstate)
|
||||||
{
|
{
|
||||||
FmgrInfo *procinfo;
|
VectorArray centers = buildstate->centers;
|
||||||
FmgrInfo *normprocinfo;
|
VectorArray m = buildstate->samples;
|
||||||
Oid collation;
|
int b = m->maxlen;
|
||||||
Vector *vec;
|
int t = 20;
|
||||||
Vector *newCenter;
|
double distance;
|
||||||
int iteration;
|
|
||||||
int j;
|
|
||||||
int k;
|
|
||||||
int dimensions = centers->dim;
|
|
||||||
int numCenters = centers->maxlen;
|
|
||||||
int numSamples = samples->length;
|
|
||||||
VectorArray newCenters;
|
|
||||||
int *centerCounts;
|
|
||||||
int *closestCenters;
|
|
||||||
double *lowerBound;
|
|
||||||
double *upperBound;
|
|
||||||
double *s;
|
|
||||||
double *halfcdist;
|
|
||||||
double *newcdist;
|
|
||||||
int changes;
|
|
||||||
double minDistance;
|
double minDistance;
|
||||||
int closestCenter;
|
int closestCenter;
|
||||||
double distance;
|
int i;
|
||||||
bool rj;
|
int j;
|
||||||
bool rjreset;
|
int k;
|
||||||
double dxcx;
|
Vector *c;
|
||||||
double dxc;
|
Vector *x;
|
||||||
|
int *v;
|
||||||
|
int *d;
|
||||||
|
double eta;
|
||||||
|
|
||||||
/* Set support functions */
|
/* Set support functions */
|
||||||
procinfo = index_getprocinfo(index, 1, IVFFLAT_KMEANS_DISTANCE_PROC);
|
FmgrInfo *procinfo = index_getprocinfo(buildstate->index, 1, IVFFLAT_KMEANS_DISTANCE_PROC);
|
||||||
normprocinfo = IvfflatOptionalProcInfo(index, IVFFLAT_KMEANS_NORM_PROC);
|
FmgrInfo *normprocinfo = buildstate->kmeansnormprocinfo;
|
||||||
collation = index->rd_indcollation[0];
|
Oid collation = buildstate->index->rd_indcollation[0];
|
||||||
|
|
||||||
/* Allocate space */
|
|
||||||
centerCounts = palloc(sizeof(int) * numCenters);
|
|
||||||
closestCenters = palloc(sizeof(int) * numSamples);
|
|
||||||
lowerBound = palloc(sizeof(double) * numSamples * numCenters);
|
|
||||||
upperBound = palloc(sizeof(double) * numSamples);
|
|
||||||
s = palloc(sizeof(double) * numCenters);
|
|
||||||
halfcdist = palloc(sizeof(double) * numCenters * numCenters);
|
|
||||||
newcdist = palloc(sizeof(double) * numCenters);
|
|
||||||
|
|
||||||
newCenters = VectorArrayInit(numCenters, dimensions);
|
|
||||||
for (j = 0; j < numCenters; j++)
|
|
||||||
{
|
|
||||||
vec = VectorArrayGet(newCenters, j);
|
|
||||||
SET_VARSIZE(vec, VECTOR_SIZE(dimensions));
|
|
||||||
vec->dim = dimensions;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Pick initial centers */
|
/* Pick initial centers */
|
||||||
InitCenters(index, samples, centers, lowerBound);
|
InitCenters(buildstate->index, buildstate->samples, buildstate->centers);
|
||||||
|
|
||||||
/* Assign each x to its closest initial center c(x) = argmin d(x,c) */
|
v = palloc(sizeof(int) * centers->maxlen);
|
||||||
for (j = 0; j < numSamples; j++)
|
d = palloc(sizeof(int) * b);
|
||||||
|
|
||||||
|
for (int i = 0; i < centers->length; i++)
|
||||||
|
v[i] = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < t; i++)
|
||||||
{
|
{
|
||||||
|
/* Can take a while, so ensure we can interrupt */
|
||||||
|
CHECK_FOR_INTERRUPTS();
|
||||||
|
|
||||||
|
/* Get b examples picked randomly from X */
|
||||||
|
SampleRows(buildstate);
|
||||||
|
|
||||||
|
/* Cache nearest center to x */
|
||||||
|
for (j = 0; j < m->length; j++)
|
||||||
|
{
|
||||||
|
/* compute closest */
|
||||||
minDistance = DBL_MAX;
|
minDistance = DBL_MAX;
|
||||||
closestCenter = -1;
|
closestCenter = -1;
|
||||||
|
|
||||||
vec = VectorArrayGet(samples, j);
|
x = VectorArrayGet(m, j);
|
||||||
|
|
||||||
/* Find closest center */
|
/* Find closest center */
|
||||||
for (k = 0; k < numCenters; k++)
|
for (k = 0; k < centers->length; k++)
|
||||||
{
|
{
|
||||||
/* TODO Use Lemma 1 in k-means++ initialization */
|
distance = DatumGetFloat8(FunctionCall2Coll(procinfo, collation, PointerGetDatum(x), PointerGetDatum(VectorArrayGet(centers, k))));
|
||||||
distance = lowerBound[j * numCenters + k];
|
|
||||||
|
|
||||||
if (distance < minDistance)
|
if (distance < minDistance)
|
||||||
{
|
{
|
||||||
@@ -237,191 +310,53 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
upperBound[j] = minDistance;
|
d[j] = closestCenter;
|
||||||
closestCenters[j] = closestCenter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Give 500 iterations to converge */
|
for (j = 0; j < m->length; j++)
|
||||||
for (iteration = 0; iteration < 500; iteration++)
|
|
||||||
{
|
{
|
||||||
/* Can take a while, so ensure we can interrupt */
|
x = VectorArrayGet(m, j);
|
||||||
CHECK_FOR_INTERRUPTS();
|
|
||||||
|
|
||||||
changes = 0;
|
/* Get cached center for this x */
|
||||||
|
c = VectorArrayGet(centers, d[j]);
|
||||||
|
|
||||||
/* Step 1: For all centers, compute distance */
|
/* Update per-center counts */
|
||||||
for (j = 0; j < numCenters; j++)
|
v[d[j]]++;
|
||||||
{
|
|
||||||
vec = VectorArrayGet(centers, j);
|
|
||||||
|
|
||||||
for (k = j + 1; k < numCenters; k++)
|
/* Get per-center learning rate */
|
||||||
{
|
eta = 1.0 / v[d[j]];
|
||||||
distance = 0.5 * DatumGetFloat8(FunctionCall2Coll(procinfo, collation, PointerGetDatum(vec), PointerGetDatum(VectorArrayGet(centers, k))));
|
|
||||||
halfcdist[j * numCenters + k] = distance;
|
/* Take gradient step */
|
||||||
halfcdist[k * numCenters + j] = distance;
|
for (k = 0; k < c->dim; k++)
|
||||||
}
|
c->x[k] = (1 - eta) * c->x[k] + eta * x->x[k];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* For all centers c, compute s(c) */
|
/* Check for empty centers (likely duplicates) */
|
||||||
for (j = 0; j < numCenters; j++)
|
if (i == 0)
|
||||||
{
|
{
|
||||||
minDistance = DBL_MAX;
|
for (j = 0; j < centers->length; j++)
|
||||||
|
|
||||||
for (k = 0; k < numCenters; k++)
|
|
||||||
{
|
{
|
||||||
if (j == k)
|
if (v[j] == 0)
|
||||||
continue;
|
|
||||||
|
|
||||||
distance = halfcdist[j * numCenters + k];
|
|
||||||
if (distance < minDistance)
|
|
||||||
minDistance = distance;
|
|
||||||
}
|
|
||||||
|
|
||||||
s[j] = minDistance;
|
|
||||||
}
|
|
||||||
|
|
||||||
rjreset = iteration != 0;
|
|
||||||
|
|
||||||
for (j = 0; j < numSamples; j++)
|
|
||||||
{
|
{
|
||||||
/* Step 2: Identify all points x such that u(x) <= s(c(x)) */
|
c = VectorArrayGet(centers, j);
|
||||||
if (upperBound[j] <= s[closestCenters[j]])
|
|
||||||
continue;
|
|
||||||
|
|
||||||
rj = rjreset;
|
|
||||||
|
|
||||||
for (k = 0; k < numCenters; k++)
|
|
||||||
{
|
|
||||||
/* Step 3: For all remaining points x and centers c */
|
|
||||||
if (k == closestCenters[j])
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (upperBound[j] <= lowerBound[j * numCenters + k])
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (upperBound[j] <= halfcdist[closestCenters[j] * numCenters + k])
|
|
||||||
continue;
|
|
||||||
|
|
||||||
vec = VectorArrayGet(samples, j);
|
|
||||||
|
|
||||||
/* Step 3a */
|
|
||||||
if (rj)
|
|
||||||
{
|
|
||||||
dxcx = DatumGetFloat8(FunctionCall2Coll(procinfo, collation, PointerGetDatum(vec), PointerGetDatum(VectorArrayGet(centers, closestCenters[j]))));
|
|
||||||
|
|
||||||
/* d(x,c(x)) computed, which is a form of d(x,c) */
|
|
||||||
lowerBound[j * numCenters + closestCenters[j]] = dxcx;
|
|
||||||
upperBound[j] = dxcx;
|
|
||||||
|
|
||||||
rj = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
dxcx = upperBound[j];
|
|
||||||
|
|
||||||
/* Step 3b */
|
|
||||||
if (dxcx > lowerBound[j * numCenters + k] || dxcx > halfcdist[closestCenters[j] * numCenters + k])
|
|
||||||
{
|
|
||||||
dxc = DatumGetFloat8(FunctionCall2Coll(procinfo, collation, PointerGetDatum(vec), PointerGetDatum(VectorArrayGet(centers, k))));
|
|
||||||
|
|
||||||
/* d(x,c) calculated */
|
|
||||||
lowerBound[j * numCenters + k] = dxc;
|
|
||||||
|
|
||||||
if (dxc < dxcx)
|
|
||||||
{
|
|
||||||
closestCenters[j] = k;
|
|
||||||
|
|
||||||
/* c(x) changed */
|
|
||||||
upperBound[j] = dxc;
|
|
||||||
|
|
||||||
changes++;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Step 4: For each center c, let m(c) be mean of all points assigned */
|
|
||||||
for (j = 0; j < numCenters; j++)
|
|
||||||
{
|
|
||||||
vec = VectorArrayGet(newCenters, j);
|
|
||||||
for (k = 0; k < dimensions; k++)
|
|
||||||
vec->x[k] = 0.0;
|
|
||||||
|
|
||||||
centerCounts[j] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (j = 0; j < numSamples; j++)
|
|
||||||
{
|
|
||||||
vec = VectorArrayGet(samples, j);
|
|
||||||
closestCenter = closestCenters[j];
|
|
||||||
|
|
||||||
/* Increment sum and count of closest center */
|
|
||||||
newCenter = VectorArrayGet(newCenters, closestCenter);
|
|
||||||
for (k = 0; k < dimensions; k++)
|
|
||||||
newCenter->x[k] += vec->x[k];
|
|
||||||
|
|
||||||
centerCounts[closestCenter] += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (j = 0; j < numCenters; j++)
|
|
||||||
{
|
|
||||||
vec = VectorArrayGet(newCenters, j);
|
|
||||||
|
|
||||||
if (centerCounts[j] > 0)
|
|
||||||
{
|
|
||||||
for (k = 0; k < dimensions; k++)
|
|
||||||
vec->x[k] /= centerCounts[j];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* TODO Handle empty centers properly */
|
/* TODO Handle empty centers properly */
|
||||||
for (k = 0; k < dimensions; k++)
|
for (k = 0; k < c->dim; k++)
|
||||||
vec->x[k] = ((double) random()) / MAX_RANDOM_VALUE;
|
c->x[k] = ((double) random()) / MAX_RANDOM_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Normalize if needed */
|
/* Normalize if needed */
|
||||||
if (normprocinfo != NULL)
|
if (normprocinfo != NULL)
|
||||||
ApplyNorm(normprocinfo, collation, vec);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Step 5 */
|
|
||||||
for (j = 0; j < numCenters; j++)
|
|
||||||
newcdist[j] = DatumGetFloat8(FunctionCall2Coll(procinfo, collation, PointerGetDatum(VectorArrayGet(centers, j)), PointerGetDatum(VectorArrayGet(newCenters, j))));
|
|
||||||
|
|
||||||
for (j = 0; j < numSamples; j++)
|
|
||||||
{
|
{
|
||||||
for (k = 0; k < numCenters; k++)
|
for (j = 0; j < centers->length; j++)
|
||||||
{
|
ApplyNorm(normprocinfo, collation, VectorArrayGet(centers, j));
|
||||||
distance = lowerBound[j * numCenters + k] - newcdist[k];
|
|
||||||
|
|
||||||
if (distance < 0)
|
|
||||||
distance = 0;
|
|
||||||
|
|
||||||
lowerBound[j * numCenters + k] = distance;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Step 6 */
|
pfree(v);
|
||||||
/* We reset r(x) before Step 3 in the next iteration */
|
pfree(d);
|
||||||
for (j = 0; j < numSamples; j++)
|
|
||||||
upperBound[j] += newcdist[closestCenters[j]];
|
|
||||||
|
|
||||||
/* Step 7 */
|
|
||||||
for (j = 0; j < numCenters; j++)
|
|
||||||
memcpy(VectorArrayGet(centers, j), VectorArrayGet(newCenters, j), VECTOR_SIZE(dimensions));
|
|
||||||
|
|
||||||
if (changes == 0 && iteration != 0)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
pfree(newCenters);
|
|
||||||
pfree(centerCounts);
|
|
||||||
pfree(closestCenters);
|
|
||||||
pfree(lowerBound);
|
|
||||||
pfree(upperBound);
|
|
||||||
pfree(s);
|
|
||||||
pfree(halfcdist);
|
|
||||||
pfree(newcdist);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -464,16 +399,48 @@ CheckCenters(Relation index, VectorArray centers)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Perform naive k-means centering
|
* Perform k-means clustering
|
||||||
* We use spherical k-means for inner product and cosine
|
* We use spherical k-means for inner product and cosine
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
IvfflatKmeans(Relation index, VectorArray samples, VectorArray centers)
|
IvfflatKmeans(IvfflatBuildState * buildstate)
|
||||||
{
|
{
|
||||||
if (samples->length <= centers->maxlen)
|
int numSamples;
|
||||||
QuickCenters(index, samples, centers);
|
Size totalSize;
|
||||||
else
|
|
||||||
ElkanKmeans(index, samples, centers);
|
|
||||||
|
|
||||||
CheckCenters(index, centers);
|
/* Target 10 samples per list, with at least 10000 samples */
|
||||||
|
/* The number of samples has a large effect on index build time */
|
||||||
|
numSamples = buildstate->lists * 10;
|
||||||
|
if (numSamples < 10000)
|
||||||
|
numSamples = 10000;
|
||||||
|
|
||||||
|
/* Skip samples for unlogged table */
|
||||||
|
if (buildstate->heap == NULL)
|
||||||
|
numSamples = 1;
|
||||||
|
|
||||||
|
/* Calculate total size */
|
||||||
|
totalSize = VECTOR_ARRAY_SIZE(numSamples, buildstate->dimensions);
|
||||||
|
|
||||||
|
/* Check memory requirements */
|
||||||
|
/* Add one to error message to ceil */
|
||||||
|
if (totalSize / 1024 > maintenance_work_mem)
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
||||||
|
errmsg("memory required is %zu MB, maintenance_work_mem is %d MB",
|
||||||
|
totalSize / (1024 * 1024) + 1, maintenance_work_mem / 1024)));
|
||||||
|
|
||||||
|
/* Sample rows */
|
||||||
|
buildstate->samples = VectorArrayInit(numSamples, buildstate->dimensions);
|
||||||
|
if (buildstate->heap != NULL)
|
||||||
|
SampleRows(buildstate);
|
||||||
|
|
||||||
|
if (buildstate->samples->length <= buildstate->centers->maxlen)
|
||||||
|
QuickCenters(buildstate->index, buildstate->samples, buildstate->centers);
|
||||||
|
else
|
||||||
|
MiniBatchKmeans(buildstate);
|
||||||
|
|
||||||
|
CheckCenters(buildstate->index, buildstate->centers);
|
||||||
|
|
||||||
|
/* Free samples before we allocate more memory */
|
||||||
|
pfree(buildstate->samples);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
|
#include <float.h>
|
||||||
|
|
||||||
#include "access/relscan.h"
|
#include "access/relscan.h"
|
||||||
#include "ivfflat.h"
|
#include "ivfflat.h"
|
||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
@@ -17,14 +19,12 @@
|
|||||||
* Compare list distances
|
* Compare list distances
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
CompareLists(const void *a, const void *b)
|
CompareLists(const pairingheap_node *a, const pairingheap_node *b, void *arg)
|
||||||
{
|
{
|
||||||
double diff = (((IvfflatScanList *) a)->distance - ((IvfflatScanList *) b)->distance);
|
if (((const IvfflatScanList *) a)->distance > ((const IvfflatScanList *) b)->distance)
|
||||||
|
|
||||||
if (diff > 0)
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (diff < 0)
|
if (((const IvfflatScanList *) a)->distance < ((const IvfflatScanList *) b)->distance)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -45,6 +45,8 @@ GetScanLists(IndexScanDesc scan, Datum value)
|
|||||||
int listCount = 0;
|
int listCount = 0;
|
||||||
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
|
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
|
||||||
double distance;
|
double distance;
|
||||||
|
IvfflatScanList *scanlist;
|
||||||
|
double maxDistance = DBL_MAX;
|
||||||
|
|
||||||
/* Search all list pages */
|
/* Search all list pages */
|
||||||
while (BlockNumberIsValid(nextblkno))
|
while (BlockNumberIsValid(nextblkno))
|
||||||
@@ -62,21 +64,39 @@ GetScanLists(IndexScanDesc scan, Datum value)
|
|||||||
/* Use procinfo from the index instead of scan key for performance */
|
/* Use procinfo from the index instead of scan key for performance */
|
||||||
distance = DatumGetFloat8(FunctionCall2Coll(so->procinfo, so->collation, PointerGetDatum(&list->center), value));
|
distance = DatumGetFloat8(FunctionCall2Coll(so->procinfo, so->collation, PointerGetDatum(&list->center), value));
|
||||||
|
|
||||||
so->lists[listCount].startPage = list->startPage;
|
if (listCount < so->probes)
|
||||||
so->lists[listCount].distance = distance;
|
{
|
||||||
|
scanlist = &so->lists[listCount];
|
||||||
|
scanlist->startPage = list->startPage;
|
||||||
|
scanlist->distance = distance;
|
||||||
listCount++;
|
listCount++;
|
||||||
|
|
||||||
|
/* Add to heap */
|
||||||
|
pairingheap_add(so->listQueue, &scanlist->ph_node);
|
||||||
|
|
||||||
|
/* Calculate max distance */
|
||||||
|
if (listCount == so->probes)
|
||||||
|
maxDistance = ((IvfflatScanList *) pairingheap_first(so->listQueue))->distance;
|
||||||
|
}
|
||||||
|
else if (distance < maxDistance)
|
||||||
|
{
|
||||||
|
/* Remove */
|
||||||
|
scanlist = (IvfflatScanList *) pairingheap_remove_first(so->listQueue);
|
||||||
|
|
||||||
|
/* Reuse */
|
||||||
|
scanlist->startPage = list->startPage;
|
||||||
|
scanlist->distance = distance;
|
||||||
|
pairingheap_add(so->listQueue, &scanlist->ph_node);
|
||||||
|
|
||||||
|
/* Update max distance */
|
||||||
|
maxDistance = ((IvfflatScanList *) pairingheap_first(so->listQueue))->distance;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nextblkno = IvfflatPageGetOpaque(cpage)->nextblkno;
|
nextblkno = IvfflatPageGetOpaque(cpage)->nextblkno;
|
||||||
|
|
||||||
UnlockReleaseBuffer(cbuf);
|
UnlockReleaseBuffer(cbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sort by distance */
|
|
||||||
qsort(so->lists, listCount, sizeof(IvfflatScanList), CompareLists);
|
|
||||||
|
|
||||||
if (so->probes > listCount)
|
|
||||||
so->probes = listCount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -94,7 +114,6 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
OffsetNumber maxoffno;
|
OffsetNumber maxoffno;
|
||||||
Datum datum;
|
Datum datum;
|
||||||
bool isnull;
|
bool isnull;
|
||||||
int i;
|
|
||||||
TupleDesc tupdesc = RelationGetDescr(scan->indexRelation);
|
TupleDesc tupdesc = RelationGetDescr(scan->indexRelation);
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 120000
|
#if PG_VERSION_NUM >= 120000
|
||||||
@@ -111,9 +130,9 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
BufferAccessStrategy bas = GetAccessStrategy(BAS_BULKREAD);
|
BufferAccessStrategy bas = GetAccessStrategy(BAS_BULKREAD);
|
||||||
|
|
||||||
/* Search closest probes lists */
|
/* Search closest probes lists */
|
||||||
for (i = 0; i < so->probes; i++)
|
while (!pairingheap_is_empty(so->listQueue))
|
||||||
{
|
{
|
||||||
searchPage = so->lists[i].startPage;
|
searchPage = ((IvfflatScanList *) pairingheap_remove_first(so->listQueue))->startPage;
|
||||||
|
|
||||||
/* Search all entry pages for list */
|
/* Search all entry pages for list */
|
||||||
while (BlockNumberIsValid(searchPage))
|
while (BlockNumberIsValid(searchPage))
|
||||||
@@ -137,12 +156,10 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
ExecClearTuple(slot);
|
ExecClearTuple(slot);
|
||||||
slot->tts_values[0] = FunctionCall2Coll(so->procinfo, so->collation, datum, value);
|
slot->tts_values[0] = FunctionCall2Coll(so->procinfo, so->collation, datum, value);
|
||||||
slot->tts_isnull[0] = false;
|
slot->tts_isnull[0] = false;
|
||||||
slot->tts_values[1] = Int32GetDatum((int) ItemPointerGetBlockNumberNoCheck(&itup->t_tid));
|
slot->tts_values[1] = PointerGetDatum(&itup->t_tid);
|
||||||
slot->tts_isnull[1] = false;
|
slot->tts_isnull[1] = false;
|
||||||
slot->tts_values[2] = Int32GetDatum((int) ItemPointerGetOffsetNumberNoCheck(&itup->t_tid));
|
slot->tts_values[2] = Int32GetDatum((int) searchPage);
|
||||||
slot->tts_isnull[2] = false;
|
slot->tts_isnull[2] = false;
|
||||||
slot->tts_values[3] = Int32GetDatum((int) searchPage);
|
|
||||||
slot->tts_isnull[3] = false;
|
|
||||||
ExecStoreVirtualTuple(slot);
|
ExecStoreVirtualTuple(slot);
|
||||||
|
|
||||||
tuplesort_puttupleslot(so->sortstate, slot);
|
tuplesort_puttupleslot(so->sortstate, slot);
|
||||||
@@ -153,6 +170,8 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
UnlockReleaseBuffer(buf);
|
UnlockReleaseBuffer(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tuplesort_performsort(so->sortstate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -168,13 +187,18 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
Oid sortOperators[] = {Float8LessOperator};
|
Oid sortOperators[] = {Float8LessOperator};
|
||||||
Oid sortCollations[] = {InvalidOid};
|
Oid sortCollations[] = {InvalidOid};
|
||||||
bool nullsFirstFlags[] = {false};
|
bool nullsFirstFlags[] = {false};
|
||||||
|
int probes = ivfflat_probes;
|
||||||
|
|
||||||
scan = RelationGetIndexScan(index, nkeys, norderbys);
|
scan = RelationGetIndexScan(index, nkeys, norderbys);
|
||||||
lists = IvfflatGetLists(scan->indexRelation);
|
lists = IvfflatGetLists(scan->indexRelation);
|
||||||
|
|
||||||
so = (IvfflatScanOpaque) palloc(offsetof(IvfflatScanOpaqueData, lists) + lists * sizeof(IvfflatScanList));
|
if (probes > lists)
|
||||||
|
probes = lists;
|
||||||
|
|
||||||
|
so = (IvfflatScanOpaque) palloc(offsetof(IvfflatScanOpaqueData, lists) + probes * sizeof(IvfflatScanList));
|
||||||
so->buf = InvalidBuffer;
|
so->buf = InvalidBuffer;
|
||||||
so->first = true;
|
so->first = true;
|
||||||
|
so->probes = probes;
|
||||||
|
|
||||||
/* Set support functions */
|
/* Set support functions */
|
||||||
so->procinfo = index_getprocinfo(index, 1, IVFFLAT_DISTANCE_PROC);
|
so->procinfo = index_getprocinfo(index, 1, IVFFLAT_DISTANCE_PROC);
|
||||||
@@ -183,14 +207,13 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
|
|
||||||
/* Create tuple description for sorting */
|
/* Create tuple description for sorting */
|
||||||
#if PG_VERSION_NUM >= 120000
|
#if PG_VERSION_NUM >= 120000
|
||||||
so->tupdesc = CreateTemplateTupleDesc(4);
|
so->tupdesc = CreateTemplateTupleDesc(3);
|
||||||
#else
|
#else
|
||||||
so->tupdesc = CreateTemplateTupleDesc(4, false);
|
so->tupdesc = CreateTemplateTupleDesc(3, false);
|
||||||
#endif
|
#endif
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "blkno", INT4OID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "tid", TIDOID, -1, 0);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 3, "offset", INT4OID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 3, "indexblkno", INT4OID, -1, 0);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 4, "indexblkno", INT4OID, -1, 0);
|
|
||||||
|
|
||||||
/* Prep sort */
|
/* Prep sort */
|
||||||
#if PG_VERSION_NUM >= 110000
|
#if PG_VERSION_NUM >= 110000
|
||||||
@@ -205,6 +228,8 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
so->slot = MakeSingleTupleTableSlot(so->tupdesc);
|
so->slot = MakeSingleTupleTableSlot(so->tupdesc);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
so->listQueue = pairingheap_allocate(CompareLists, scan);
|
||||||
|
|
||||||
scan->opaque = so;
|
scan->opaque = so;
|
||||||
|
|
||||||
return scan;
|
return scan;
|
||||||
@@ -224,7 +249,7 @@ ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
so->first = true;
|
so->first = true;
|
||||||
so->probes = ivfflat_probes;
|
pairingheap_reset(so->listQueue);
|
||||||
|
|
||||||
if (keys && scan->numberOfKeys > 0)
|
if (keys && scan->numberOfKeys > 0)
|
||||||
memmove(scan->keyData, keys, scan->numberOfKeys * sizeof(ScanKeyData));
|
memmove(scan->keyData, keys, scan->numberOfKeys * sizeof(ScanKeyData));
|
||||||
@@ -268,9 +293,8 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetScanLists(scan, value);
|
IvfflatBench("GetScanLists", GetScanLists(scan, value));
|
||||||
GetScanItems(scan, value);
|
IvfflatBench("GetScanItems", GetScanItems(scan, value));
|
||||||
tuplesort_performsort(so->sortstate);
|
|
||||||
so->first = false;
|
so->first = false;
|
||||||
|
|
||||||
/* Clean up if we allocated a new value */
|
/* Clean up if we allocated a new value */
|
||||||
@@ -284,14 +308,13 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
if (tuplesort_gettupleslot(so->sortstate, true, so->slot, NULL))
|
if (tuplesort_gettupleslot(so->sortstate, true, so->slot, NULL))
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
BlockNumber blkno = DatumGetInt32(slot_getattr(so->slot, 2, &so->isnull));
|
ItemPointer tid = (ItemPointer) DatumGetPointer(slot_getattr(so->slot, 2, &so->isnull));
|
||||||
OffsetNumber offset = DatumGetInt32(slot_getattr(so->slot, 3, &so->isnull));
|
BlockNumber indexblkno = DatumGetInt32(slot_getattr(so->slot, 3, &so->isnull));
|
||||||
BlockNumber indexblkno = DatumGetInt32(slot_getattr(so->slot, 4, &so->isnull));
|
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 120000
|
#if PG_VERSION_NUM >= 120000
|
||||||
ItemPointerSet(&scan->xs_heaptid, blkno, offset);
|
scan->xs_heaptid = *tid;
|
||||||
#else
|
#else
|
||||||
ItemPointerSet(&scan->xs_ctup.t_self, blkno, offset);
|
scan->xs_ctup.t_self = *tid;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (BufferIsValid(so->buf))
|
if (BufferIsValid(so->buf))
|
||||||
@@ -324,6 +347,7 @@ ivfflatendscan(IndexScanDesc scan)
|
|||||||
if (BufferIsValid(so->buf))
|
if (BufferIsValid(so->buf))
|
||||||
ReleaseBuffer(so->buf);
|
ReleaseBuffer(so->buf);
|
||||||
|
|
||||||
|
pairingheap_free(so->listQueue);
|
||||||
tuplesort_end(so->sortstate);
|
tuplesort_end(so->sortstate);
|
||||||
|
|
||||||
pfree(so);
|
pfree(so);
|
||||||
|
|||||||
@@ -153,11 +153,13 @@ IvfflatAppendPage(Relation index, Buffer *buf, Page *page, GenericXLogState **st
|
|||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
IvfflatUpdateList(Relation index, GenericXLogState *state, ListInfo listInfo,
|
IvfflatUpdateList(Relation index, GenericXLogState *state, ListInfo listInfo,
|
||||||
BlockNumber insertPage, BlockNumber startPage, ForkNumber forkNum)
|
BlockNumber insertPage, BlockNumber originalInsertPage,
|
||||||
|
BlockNumber startPage, ForkNumber forkNum)
|
||||||
{
|
{
|
||||||
Buffer buf;
|
Buffer buf;
|
||||||
Page page;
|
Page page;
|
||||||
IvfflatList list;
|
IvfflatList list;
|
||||||
|
bool changed = false;
|
||||||
|
|
||||||
buf = ReadBufferExtended(index, forkNum, listInfo.blkno, RBM_NORMAL, NULL);
|
buf = ReadBufferExtended(index, forkNum, listInfo.blkno, RBM_NORMAL, NULL);
|
||||||
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
|
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
|
||||||
@@ -165,12 +167,29 @@ IvfflatUpdateList(Relation index, GenericXLogState *state, ListInfo listInfo,
|
|||||||
page = GenericXLogRegisterBuffer(state, buf, 0);
|
page = GenericXLogRegisterBuffer(state, buf, 0);
|
||||||
list = (IvfflatList) PageGetItem(page, PageGetItemId(page, listInfo.offno));
|
list = (IvfflatList) PageGetItem(page, PageGetItemId(page, listInfo.offno));
|
||||||
|
|
||||||
if (BlockNumberIsValid(insertPage))
|
if (BlockNumberIsValid(insertPage) && insertPage != list->insertPage)
|
||||||
|
{
|
||||||
|
/* Skip update if insert page is lower than original insert page */
|
||||||
|
/* This is needed to prevent insert from overwriting vacuum */
|
||||||
|
if (!BlockNumberIsValid(originalInsertPage) || insertPage >= originalInsertPage)
|
||||||
|
{
|
||||||
list->insertPage = insertPage;
|
list->insertPage = insertPage;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (BlockNumberIsValid(startPage))
|
if (BlockNumberIsValid(startPage) && startPage != list->startPage)
|
||||||
|
{
|
||||||
list->startPage = startPage;
|
list->startPage = startPage;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
/* Could only commit if changed, but extra complexity isn't needed */
|
/* Only commit if changed */
|
||||||
|
if (changed)
|
||||||
IvfflatCommitBuffer(buf, state);
|
IvfflatCommitBuffer(buf, state);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
GenericXLogAbort(state);
|
||||||
|
UnlockReleaseBuffer(buf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
|
|||||||
ItemPointer htup;
|
ItemPointer htup;
|
||||||
OffsetNumber deletable[MaxOffsetNumber];
|
OffsetNumber deletable[MaxOffsetNumber];
|
||||||
int ndeletable;
|
int ndeletable;
|
||||||
OffsetNumber startPages[MaxOffsetNumber];
|
BlockNumber startPages[MaxOffsetNumber];
|
||||||
BlockNumber nextblkno = IVFFLAT_HEAD_BLKNO;
|
BlockNumber nextblkno = IVFFLAT_HEAD_BLKNO;
|
||||||
BlockNumber searchPage;
|
BlockNumber searchPage;
|
||||||
BlockNumber insertPage;
|
BlockNumber insertPage;
|
||||||
@@ -98,6 +98,11 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
|
|||||||
stats->num_index_tuples++;
|
stats->num_index_tuples++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Set to first free page */
|
||||||
|
/* Must be set before searchPage is updated */
|
||||||
|
if (!BlockNumberIsValid(insertPage) && ndeletable > 0)
|
||||||
|
insertPage = searchPage;
|
||||||
|
|
||||||
searchPage = IvfflatPageGetOpaque(page)->nextblkno;
|
searchPage = IvfflatPageGetOpaque(page)->nextblkno;
|
||||||
|
|
||||||
if (ndeletable > 0)
|
if (ndeletable > 0)
|
||||||
@@ -106,10 +111,6 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
|
|||||||
PageIndexMultiDelete(page, deletable, ndeletable);
|
PageIndexMultiDelete(page, deletable, ndeletable);
|
||||||
MarkBufferDirty(buf);
|
MarkBufferDirty(buf);
|
||||||
GenericXLogFinish(state);
|
GenericXLogFinish(state);
|
||||||
|
|
||||||
/* Set to first free page */
|
|
||||||
if (!BlockNumberIsValid(insertPage))
|
|
||||||
insertPage = searchPage;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
GenericXLogAbort(state);
|
GenericXLogAbort(state);
|
||||||
@@ -123,10 +124,10 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
|
|||||||
* We don't add or delete items from lists pages, so offset won't
|
* We don't add or delete items from lists pages, so offset won't
|
||||||
* change.
|
* change.
|
||||||
*/
|
*/
|
||||||
if (!BlockNumberIsValid(insertPage))
|
if (BlockNumberIsValid(insertPage))
|
||||||
{
|
{
|
||||||
listInfo.offno = coffno;
|
listInfo.offno = coffno;
|
||||||
IvfflatUpdateList(index, state, listInfo, insertPage, InvalidBlockNumber, MAIN_FORKNUM);
|
IvfflatUpdateList(index, state, listInfo, insertPage, InvalidBlockNumber, InvalidBlockNumber, MAIN_FORKNUM);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,3 +53,12 @@ LINE 1: SELECT '[1,]'::vector;
|
|||||||
^
|
^
|
||||||
SELECT '[1,2,3]'::vector(2);
|
SELECT '[1,2,3]'::vector(2);
|
||||||
ERROR: expected 2 dimensions, not 3
|
ERROR: expected 2 dimensions, not 3
|
||||||
|
SELECT unnest('{"[1,2,3]", "[4,5,6]"}'::vector[]);
|
||||||
|
unnest
|
||||||
|
---------
|
||||||
|
[1,2,3]
|
||||||
|
[4,5,6]
|
||||||
|
(2 rows)
|
||||||
|
|
||||||
|
SELECT '{"[1,2,3]"}'::vector(2)[];
|
||||||
|
ERROR: expected 2 dimensions, not 3
|
||||||
|
|||||||
@@ -13,3 +13,6 @@ SELECT '1,2,3'::vector;
|
|||||||
SELECT '[]'::vector;
|
SELECT '[]'::vector;
|
||||||
SELECT '[1,]'::vector;
|
SELECT '[1,]'::vector;
|
||||||
SELECT '[1,2,3]'::vector(2);
|
SELECT '[1,2,3]'::vector(2);
|
||||||
|
|
||||||
|
SELECT unnest('{"[1,2,3]", "[4,5,6]"}'::vector[]);
|
||||||
|
SELECT '{"[1,2,3]"}'::vector(2)[];
|
||||||
|
|||||||
@@ -34,9 +34,10 @@ sub test_index_replay
|
|||||||
my $r2 = rand();
|
my $r2 = rand();
|
||||||
my $r3 = rand();
|
my $r3 = rand();
|
||||||
|
|
||||||
my $queries = qq(SET enable_seqscan=off;
|
my $queries = qq(
|
||||||
SELECT * FROM tst ORDER BY v <-> '[$r1,$r2,$r3]' LIMIT 10;
|
SET enable_seqscan = off;
|
||||||
);
|
SELECT * FROM tst ORDER BY v <-> '[$r1,$r2,$r3]' LIMIT 10;
|
||||||
|
);
|
||||||
|
|
||||||
# Run test queries and compare their result
|
# Run test queries and compare their result
|
||||||
my $primary_result = $node_primary->safe_psql("postgres", $queries);
|
my $primary_result = $node_primary->safe_psql("postgres", $queries);
|
||||||
@@ -65,10 +66,9 @@ $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(3));");
|
$node_primary->safe_psql("postgres", "CREATE TABLE tst (i int4, v vector(3));");
|
||||||
$node_primary->safe_psql("postgres",
|
$node_primary->safe_psql("postgres",
|
||||||
"INSERT INTO tst SELECT i%10, ARRAY[random(), random(), random()] FROM generate_series(1,100000) i;"
|
"INSERT INTO tst SELECT i % 10, ARRAY[random(), random(), random()] FROM generate_series(1, 100000) i;"
|
||||||
);
|
);
|
||||||
$node_primary->safe_psql("postgres",
|
$node_primary->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v);");
|
||||||
"CREATE INDEX ON tst USING ivfflat (v);");
|
|
||||||
|
|
||||||
# Test that queries give same result
|
# Test that queries give same result
|
||||||
test_index_replay('initial');
|
test_index_replay('initial');
|
||||||
@@ -82,7 +82,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, ARRAY[random(), random(), random()] FROM generate_series($start,$end) i;"
|
"INSERT INTO tst SELECT i % 10, ARRAY[random(), random(), random()] FROM generate_series($start, $end) i;"
|
||||||
);
|
);
|
||||||
test_index_replay("insert $i");
|
test_index_replay("insert $i");
|
||||||
}
|
}
|
||||||
|
|||||||
32
test/t/002_vacuum.pl
Normal file
32
test/t/002_vacuum.pl
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use PostgresNode;
|
||||||
|
use TestLib;
|
||||||
|
use Test::More tests => 1;
|
||||||
|
|
||||||
|
# Initialize node
|
||||||
|
my $node = get_new_node('node');
|
||||||
|
$node->init;
|
||||||
|
$node->start;
|
||||||
|
|
||||||
|
# Create table and index
|
||||||
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
|
$node->safe_psql("postgres", "CREATE TABLE tst (i int4, v vector(3));");
|
||||||
|
$node->safe_psql("postgres",
|
||||||
|
"INSERT INTO tst SELECT i % 10, ARRAY[i % 1000, i % 333, i % 55] FROM generate_series(1, 100000) i;"
|
||||||
|
);
|
||||||
|
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v);");
|
||||||
|
|
||||||
|
# Get size
|
||||||
|
my $size = $node->safe_psql("postgres", "SELECT pg_total_relation_size('tst_v_idx');");
|
||||||
|
|
||||||
|
# Delete all, vacuum, and insert same data
|
||||||
|
$node->safe_psql("postgres", "DELETE FROM tst;");
|
||||||
|
$node->safe_psql("postgres", "VACUUM tst;");
|
||||||
|
$node->safe_psql("postgres",
|
||||||
|
"INSERT INTO tst SELECT i % 10, ARRAY[i % 1000, i % 333, i % 55] FROM generate_series(1, 100000) i;"
|
||||||
|
);
|
||||||
|
|
||||||
|
# Check size
|
||||||
|
my $new_size = $node->safe_psql("postgres", "SELECT pg_total_relation_size('tst_v_idx');");
|
||||||
|
is($size, $new_size, "size does not change");
|
||||||
88
test/t/003_recall.pl
Normal file
88
test/t/003_recall.pl
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use PostgresNode;
|
||||||
|
use TestLib;
|
||||||
|
use Test::More tests => 9;
|
||||||
|
|
||||||
|
my $node;
|
||||||
|
my @queries = ();
|
||||||
|
my @expected;
|
||||||
|
my $limit = 20;
|
||||||
|
|
||||||
|
sub test_recall
|
||||||
|
{
|
||||||
|
my ($probes, $min, $operator) = @_;
|
||||||
|
my $correct = 0;
|
||||||
|
my $total = 0;
|
||||||
|
|
||||||
|
for my $i (0 .. $#queries) {
|
||||||
|
my $actual = $node->safe_psql("postgres", qq(
|
||||||
|
SET enable_seqscan = off;
|
||||||
|
SET ivfflat.probes = $probes;
|
||||||
|
SELECT i FROM tst ORDER BY v $operator '$queries[$i]' LIMIT $limit;
|
||||||
|
));
|
||||||
|
my @actual_ids = split("\n", $actual);
|
||||||
|
my %actual_set = map { $_ => 1 } @actual_ids;
|
||||||
|
|
||||||
|
my @expected_ids = split("\n", $expected[$i]);
|
||||||
|
|
||||||
|
foreach (@expected_ids) {
|
||||||
|
if (exists($actual_set{$_})) {
|
||||||
|
$correct++;
|
||||||
|
}
|
||||||
|
$total++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp_ok($correct / $total, ">=", $min, $operator);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Initialize node
|
||||||
|
$node = get_new_node('node');
|
||||||
|
$node->init;
|
||||||
|
$node->start;
|
||||||
|
|
||||||
|
# Create table
|
||||||
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
|
$node->safe_psql("postgres", "CREATE TABLE tst (i int4, v vector(3));");
|
||||||
|
$node->safe_psql("postgres",
|
||||||
|
"INSERT INTO tst SELECT i, ARRAY[random(), random(), random()] FROM generate_series(1, 100000) i;"
|
||||||
|
);
|
||||||
|
|
||||||
|
# Generate queries
|
||||||
|
for (1..20) {
|
||||||
|
my $r1 = rand();
|
||||||
|
my $r2 = rand();
|
||||||
|
my $r3 = rand();
|
||||||
|
push(@queries, "[$r1,$r2,$r3]");
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check each index type
|
||||||
|
my @operators = ("<->", "<#>", "<=>");
|
||||||
|
|
||||||
|
foreach (@operators) {
|
||||||
|
my $operator = $_;
|
||||||
|
|
||||||
|
# Get exact results
|
||||||
|
@expected = ();
|
||||||
|
foreach (@queries) {
|
||||||
|
my $res = $node->safe_psql("postgres", "SELECT i FROM tst ORDER BY v $operator '$_' LIMIT $limit;");
|
||||||
|
push(@expected, $res);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add index
|
||||||
|
my $opclass;
|
||||||
|
if ($operator == "<->") {
|
||||||
|
$opclass = "vector_l2_ops";
|
||||||
|
} elsif ($operator == "<#>") {
|
||||||
|
$opclass = "vector_ip_ops";
|
||||||
|
} else {
|
||||||
|
$opclass = "vector_cosine_ops";
|
||||||
|
}
|
||||||
|
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v $opclass);");
|
||||||
|
|
||||||
|
# Test approximate results
|
||||||
|
test_recall(1, 0.75, $operator);
|
||||||
|
test_recall(10, 0.95, $operator);
|
||||||
|
test_recall(100, 1.0, $operator);
|
||||||
|
}
|
||||||
36
test/t/004_centers.pl
Normal file
36
test/t/004_centers.pl
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use PostgresNode;
|
||||||
|
use TestLib;
|
||||||
|
use Test::More tests => 3;
|
||||||
|
|
||||||
|
# Initialize node
|
||||||
|
my $node = get_new_node('node');
|
||||||
|
$node->init;
|
||||||
|
$node->start;
|
||||||
|
|
||||||
|
# Create table
|
||||||
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
|
$node->safe_psql("postgres", "CREATE TABLE tst (i int4, v vector(3));");
|
||||||
|
$node->safe_psql("postgres",
|
||||||
|
"INSERT INTO tst SELECT i, '[1,2,3]' FROM generate_series(1, 10) i;"
|
||||||
|
);
|
||||||
|
|
||||||
|
sub test_centers
|
||||||
|
{
|
||||||
|
my ($lists, $min) = @_;
|
||||||
|
|
||||||
|
my ($ret, $stdout, $stderr) = $node->psql("postgres", "CREATE INDEX ON tst USING ivfflat (v) WITH (lists = $lists);");
|
||||||
|
is($ret, 0, $stderr);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Test no error for duplicate centers
|
||||||
|
test_centers(5);
|
||||||
|
test_centers(10);
|
||||||
|
|
||||||
|
$node->safe_psql("postgres",
|
||||||
|
"INSERT INTO tst SELECT i, '[4,5,6]' FROM generate_series(1, 10) i;"
|
||||||
|
);
|
||||||
|
|
||||||
|
# Test no error for duplicate centers
|
||||||
|
test_centers(10);
|
||||||
45
test/t/005_query_recall.pl
Normal file
45
test/t/005_query_recall.pl
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use PostgresNode;
|
||||||
|
use TestLib;
|
||||||
|
use Test::More tests => 60;
|
||||||
|
|
||||||
|
# Initialize node
|
||||||
|
my $node = get_new_node('node');
|
||||||
|
$node->init;
|
||||||
|
$node->start;
|
||||||
|
|
||||||
|
# Create table
|
||||||
|
$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",
|
||||||
|
"INSERT INTO tst SELECT i, ARRAY[random(), random(), random()] FROM generate_series(1, 100000) i;"
|
||||||
|
);
|
||||||
|
|
||||||
|
# Check each index type
|
||||||
|
my @operators = ("<->", "<#>", "<=>");
|
||||||
|
foreach (@operators) {
|
||||||
|
my $operator = $_;
|
||||||
|
|
||||||
|
# Add index
|
||||||
|
my $opclass;
|
||||||
|
if ($operator == "<->") {
|
||||||
|
$opclass = "vector_l2_ops";
|
||||||
|
} elsif ($operator == "<#>") {
|
||||||
|
$opclass = "vector_ip_ops";
|
||||||
|
} else {
|
||||||
|
$opclass = "vector_cosine_ops";
|
||||||
|
}
|
||||||
|
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v $opclass);");
|
||||||
|
|
||||||
|
# Test 100% recall
|
||||||
|
for (1..20) {
|
||||||
|
my $i = int(rand() * 100000);
|
||||||
|
my $query = $node->safe_psql("postgres", "SELECT v FROM tst WHERE i = $i;");
|
||||||
|
my $res = $node->safe_psql("postgres", qq(
|
||||||
|
SET enable_seqscan = off;
|
||||||
|
SELECT v FROM tst ORDER BY v <-> '$query' LIMIT 1;
|
||||||
|
));
|
||||||
|
is($res, $query);
|
||||||
|
}
|
||||||
|
}
|
||||||
31
test/t/006_lists.pl
Normal file
31
test/t/006_lists.pl
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use PostgresNode;
|
||||||
|
use TestLib;
|
||||||
|
use Test::More tests => 3;
|
||||||
|
|
||||||
|
# Initialize node
|
||||||
|
my $node = get_new_node('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, 100000) i;"
|
||||||
|
);
|
||||||
|
|
||||||
|
$node->safe_psql("postgres", "CREATE INDEX lists50 ON tst USING ivfflat (v) WITH (lists = 50);");
|
||||||
|
$node->safe_psql("postgres", "CREATE INDEX lists100 ON tst USING ivfflat (v) WITH (lists = 100);");
|
||||||
|
|
||||||
|
# Test prefers more lists
|
||||||
|
my $res = $node->safe_psql("postgres", "EXPLAIN SELECT v FROM tst ORDER BY v <-> '[0.5,0.5,0.5]' LIMIT 10;");
|
||||||
|
like($res, qr/lists100/);
|
||||||
|
unlike($res, qr/lists50/);
|
||||||
|
|
||||||
|
# Test errors with too much memory
|
||||||
|
my ($ret, $stdout, $stderr) = $node->psql("postgres",
|
||||||
|
"CREATE INDEX lists10000 ON tst USING ivfflat (v) WITH (lists = 10000);"
|
||||||
|
);
|
||||||
|
like($stderr, qr/memory required is/);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
comment = 'vector data type and ivfflat access method'
|
comment = 'vector data type and ivfflat access method'
|
||||||
default_version = '0.2.3'
|
default_version = '0.2.5'
|
||||||
module_pathname = '$libdir/vector'
|
module_pathname = '$libdir/vector'
|
||||||
relocatable = true
|
relocatable = true
|
||||||
|
|||||||
Reference in New Issue
Block a user