Skip to content

pgvector/pgvector-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector-c

pgvector examples for C

Supports libpq

Build Status

Getting Started

Follow the instructions for your database library:

libpq

Enable the extension

PGresult *res = PQexec(conn, "CREATE EXTENSION IF NOT EXISTS vector");

Create a table

PGresult *res = PQexec(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");

Insert vectors

const char *paramValues[2] = {"[1,2,3]", "[4,5,6]"};
PGresult *res = PQexecParams(conn, "INSERT INTO items (embedding) VALUES ($1), ($2)", 2, NULL, paramValues, NULL, NULL, 0);

Get the nearest neighbors

const char *paramValues[1] = {"[3,1,2]"};
PGresult *res = PQexecParams(conn, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", 1, NULL, paramValues, NULL, NULL, 0);

Add an approximate index

PGresult *res = PQexec(conn, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)");
// or
PGresult *res = PQexec(conn, "CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)");

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

See a full example

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/pgvector/pgvector-c.git
cd pgvector-c
createdb pgvector_c_test
gcc -Wall -Wextra -Werror -o test/pq test/pq_test.c -lpq
test/pq

Releases

No releases published

Packages

No packages published

Languages