Skip to content

Commit 16b15b8

Browse files
committed
Added pg.zig example
1 parent d95508d commit 16b15b8

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ jobs:
1717
make
1818
sudo make install
1919
- run: zig build
20+
- run: zig-out/bin/pg
2021
- run: zig-out/bin/libpq

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,56 @@
22

33
[pgvector](https://github.com/pgvector/pgvector) examples for Zig
44

5-
Supports [libpq](https://www.postgresql.org/docs/current/libpq.html)
5+
Supports [pg.zig](https://github.com/karlseguin/pg.zig) and [libpq](https://www.postgresql.org/docs/current/libpq.html)
66

77
[![Build Status](https://github.com/pgvector/pgvector-zig/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-zig/actions)
88

99
## Getting Started
1010

1111
Follow the instructions for your database library:
1212

13+
- [pg.zig](#pg-zig)
1314
- [libpq](#libpq)
1415

16+
## pg.zig
17+
18+
Enable the extension
19+
20+
```zig
21+
_ = try conn.exec("CREATE EXTENSION IF NOT EXISTS vector", .{});
22+
```
23+
24+
Create a table
25+
26+
```zig
27+
_ = try conn.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))", .{});
28+
```
29+
30+
Insert vectors
31+
32+
```zig
33+
const params = .{ "[1,1,1]", "[2,2,2]", "[1,1,2]" };
34+
_ = try conn.exec("INSERT INTO items (embedding) VALUES ($1), ($2), ($3)", params);
35+
```
36+
37+
Get the nearest neighbors
38+
39+
```zig
40+
var result = try conn.query("SELECT id FROM items ORDER BY embedding <-> $1 LIMIT 5", .{"[1,1,1]"});
41+
```
42+
43+
Add an approximate index
44+
45+
```zig
46+
_ = try conn.exec("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)", .{});
47+
// or
48+
_ = try conn.exec("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)", .{});
49+
```
50+
51+
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
52+
53+
See a [full example](examples/pg.zig)
54+
1555
## libpq
1656

1757
Import libpq
@@ -76,6 +116,7 @@ git clone https://github.com/pgvector/pgvector-zig.git
76116
cd pgvector-zig
77117
createdb pgvector_zig_test
78118
zig build
119+
zig-out/bin/pg
79120
zig-out/bin/libpq
80121
```
81122

build.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
const std = @import("std");
22

33
pub fn build(b: *std.Build) void {
4+
const pgExe = b.addExecutable(.{
5+
.name = "pg",
6+
.root_source_file = b.path("examples/pg.zig"),
7+
.target = b.graph.host,
8+
});
9+
pgExe.linkSystemLibrary("pq");
10+
pgExe.linkLibC();
11+
const pg = b.dependency("pg", .{});
12+
pgExe.root_module.addImport("pg", pg.module("pg"));
13+
b.installArtifact(pgExe);
14+
415
const libpqExe = b.addExecutable(.{
516
.name = "libpq",
617
.root_source_file = b.path("examples/libpq.zig"),

build.zig.zon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.{
2+
.name = .pgvector,
3+
.version = "0.0.0",
4+
.fingerprint = 0x2dcfd56a3910ab6c,
5+
.minimum_zig_version = "0.14.0",
6+
.dependencies = .{
7+
.pg = .{
8+
.url = "git+https://github.com/karlseguin/pg.zig?ref=master#740b6bfe31bc3ebc81b7bc97fe883871da2604c7",
9+
.hash = "pg-0.0.0-Wp_7gQr-BQAMQ8q9R0TzXrVgsyLVubJpg7mg_CSSIYqI",
10+
},
11+
},
12+
.paths = .{
13+
"build.zig",
14+
"build.zig.zon",
15+
"LICENSE.txt",
16+
"README.md",
17+
},
18+
}

examples/pg.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const pg = @import("pg");
2+
const std = @import("std");
3+
const print = std.debug.print;
4+
5+
pub fn main() !void {
6+
const allocator = std.heap.c_allocator;
7+
var pool = try pg.Pool.init(allocator, .{ .auth = .{
8+
.username = std.mem.sliceTo(std.c.getenv("USER").?, 0),
9+
.database = "pgvector_zig_test",
10+
} });
11+
defer pool.deinit();
12+
13+
const conn = try pool.acquire();
14+
defer pool.release(conn);
15+
16+
_ = try conn.exec("CREATE EXTENSION IF NOT EXISTS vector", .{});
17+
18+
_ = try conn.exec("DROP TABLE IF EXISTS pg_items", .{});
19+
20+
_ = try conn.exec("CREATE TABLE pg_items (id bigserial PRIMARY KEY, embedding vector(3))", .{});
21+
22+
const params = .{ "[1,1,1]", "[2,2,2]", "[1,1,2]" };
23+
_ = try conn.exec("INSERT INTO pg_items (embedding) VALUES ($1), ($2), ($3)", params);
24+
25+
var result = try conn.query("SELECT id FROM pg_items ORDER BY embedding <-> $1 LIMIT 5", .{"[1,1,1]"});
26+
defer result.deinit();
27+
while (try result.next()) |row| {
28+
const id = row.get(i64, 0);
29+
print("{d}\n", .{id});
30+
}
31+
}

0 commit comments

Comments
 (0)