GithubHelp home page GithubHelp logo

cxz / pgvector Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pgvector/pgvector

0.0 0.0 0.0 76 KB

Open-source vector similarity search for Postgres

License: Other

Makefile 1.94% C 93.66% Perl 3.77% Dockerfile 0.62%

pgvector's Introduction

pgvector

Open-source vector similarity search for Postgres

CREATE TABLE table (column vector(3));
CREATE INDEX ON table USING ivfflat (column);
SELECT * FROM table ORDER BY column <-> '[1,2,3]' LIMIT 5;

Supports L2 distance, inner product, and cosine distance

Build Status

Installation

Compile and install the extension (supports Postgres 9.6+)

git clone --branch v0.1.7 https://github.com/ankane/pgvector.git
cd pgvector
make
make install # may need sudo

Then load it in databases where you want to use it

CREATE EXTENSION vector;

You can also install it with Docker, Homebrew, or PGXN

Getting Started

Create a vector column with 3 dimensions (replace table and column with non-reserved names)

CREATE TABLE table (column vector(3));

Insert values

INSERT INTO table VALUES ('[1,2,3]'), ('[4,5,6]');

Get the nearest neighbor by L2 distance

SELECT * FROM table ORDER BY column <-> '[3,1,2]' LIMIT 1;

Also supports inner product (<#>) and cosine distance (<=>)

Note: <#> returns the negative inner product since Postgres only supports ASC order index scans on operators

Indexing

Speed up queries with an approximate index. Add an index for each distance function you want to use.

L2 distance

CREATE INDEX ON table USING ivfflat (column);

Inner product

CREATE INDEX ON table USING ivfflat (column vector_ip_ops);

Cosine distance

CREATE INDEX ON table USING ivfflat (column vector_cosine_ops);

Indexes should be created after the table has data for optimal clustering. Also, unlike typical indexes which only affect performance, you may see different results for queries after adding an approximate index.

Index Options

Specify the number of inverted lists (100 by default)

CREATE INDEX ON table USING ivfflat (column) WITH (lists = 100);

Query Options

Specify the number of probes (1 by default)

SET ivfflat.probes = 1;

A higher value improves recall at the cost of speed.

Use SET LOCAL inside a transaction to set it for a single query

BEGIN;
SET LOCAL ivfflat.probes = 1;
SELECT ...
COMMIT;

Partial Indexes

Consider partial indexes for queries with a WHERE clause

CREATE INDEX ON table USING ivfflat (column) WHERE (other_column = 123);

To index many different values of other_column, consider partitioning on other_column.

Reference

Vector Type

Each vector takes 4 * dimensions + 8 bytes of storage. Each element is a float, and all elements must be finite (no NaN, Infinity or -Infinity). Vectors can have up to 1024 dimensions.

Vector Operators

Operator Description
+ element-wise addition
- element-wise subtraction
<-> Euclidean distance
<#> negative inner product
<=> cosine distance

Vector Functions

Function Description
cosine_distance(vector, vector) cosine distance
inner_product(vector, vector) inner product
l2_distance(vector, vector) Euclidean distance
vector_dims(vector) number of dimensions
vector_norm(vector) Euclidean norm

Libraries

Libraries that use pgvector:

Additional Installation Methods

Docker

Get the Docker image with:

docker pull ankane/pgvector

This adds pgvector to the Postgres image.

You can also build the image manually

git clone --branch v0.1.7 https://github.com/ankane/pgvector.git
cd pgvector
docker build -t pgvector .

Homebrew

On Mac with Homebrew Postgres, you can use:

brew install ankane/brew/pgvector

PGXN

Install from the PostgreSQL Extension Network with:

pgxn install vector

Hosted Postgres

Some Postgres providers only support specific extensions. To request a new extension:

  • Amazon RDS - follow the instructions on this page
  • Google Cloud SQL - follow the instructions on this page
  • DigitalOcean Managed Databases - follow the instructions on this page
  • Azure Database for PostgreSQL - follow the instructions on this page

Upgrading

Install the latest version and run:

ALTER EXTENSION vector UPDATE;

Thanks

Thanks to:

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/pgvector.git
cd pgvector
make
make install

To run all tests:

make installcheck        # regression tests
make prove_installcheck  # TAP tests

To run single tests:

make installcheck REGRESS=functions                    # regression test
make prove_installcheck PROVE_TESTS=test/t/001_wal.pl  # TAP test

Resources for contributors

pgvector's People

Contributors

ankane avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.