https://github.com/ankane/pgvector Skip to content Sign up Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Project management - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] [search-key] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up Sign up {{ message }} ankane / pgvector * Notifications * Star 172 * Fork 0 Open-source vector similarity search for Postgres View license 172 stars 0 forks Star Notifications * Code * Issues 2 * Pull requests 0 * Actions * Security * Insights More * Code * Issues * Pull requests * Actions * Security * Insights master Switch branches/tags [ ] Branches Tags Nothing to show {{ refName }} default View all branches Nothing to show {{ refName }} default View all tags 1 branch 1 tag Go to file Code Clone HTTPS GitHub CLI [https://github.com/a] Use Git or checkout with SVN using the web URL. [gh repo clone ankane] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching Xcode If nothing happens, download Xcode and try again. Go back Launching Visual Studio If nothing happens, download the GitHub extension for Visual Studio and try again. Go back Latest commit @ankane ankane Moved libraries [skip ci] ... c4baa33 Apr 21, 2021 Moved libraries [skip ci] c4baa33 Git stats * 10 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows Run apt-get update on CI Apr 20, 2021 expected First commit Apr 20, 2021 sql First commit Apr 20, 2021 src Added src directory Apr 20, 2021 t Fixed TAP test for Postgres 9.6 Apr 20, 2021 .editorconfig First commit Apr 20, 2021 .gitignore First commit Apr 20, 2021 CHANGELOG.md Version bump to 0.1.0 [skip ci] Apr 20, 2021 LICENSE First commit Apr 20, 2021 Makefile Added src directory Apr 20, 2021 README.md Moved libraries [skip ci] Apr 22, 2021 vector--0.1.0.sql First commit Apr 20, 2021 vector.control First commit Apr 20, 2021 View code pgvector Installation Getting Started Indexing Index Options Query Options Reference Vector Type Vector Operators Vector Functions Libraries Thanks History Contributing README.md 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.0 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; 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; 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: * Neighbor (Ruby) Thanks Thanks to: * PASE: PostgreSQL Ultra-High-Dimensional Approximate Nearest Neighbor Search Extension * Faiss: A Library for Efficient Similarity Search and Clustering of Dense Vectors * Using the Triangle Inequality to Accelerate k-means * k-means++: The Advantage of Careful Seeding * Concept Decompositions for Large Sparse Text Data using Clustering History View the changelog Contributing Everyone is encouraged to help improve this project. Here are a few ways you can help: * Report bugs * Fix bugs and submit pull requests * Write, clarify, or fix documentation * Suggest or add new features 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=vector # regression test make prove_installcheck PROVE_TESTS=t/001_wal.pl # TAP test Directories * expected - expected output for regression tests * sql - regression tests * t - TAP tests Resources for contributors * Extension Building Infrastructure * Index Access Method Interface Definition * Generic WAL Records About Open-source vector similarity search for Postgres Topics nearest-neighbor-search approximate-nearest-neighbor-search Resources Readme License View license Releases 1 tags Packages 0 No packages published Languages * C 94.0% * Perl 3.9% * C++ 1.2% * Makefile 0.9% * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.