1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
;;; vecdb-psql.el --- An interface to postgres with vector extension -*- lexical-binding: t; -*-
;; Copyright (c) 2025 Free Software Foundation, Inc.
;; Author: Andrew Hyatt <ahyatt@gmail.com>
;; Homepage: https://github.com/ahyatt/vecdb
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides an implementation of vecdb for using with postgres.
;;; Code:
(require 'vecdb)
(require 'pg)
(require 'cl-lib)
(require 'map)
(require 'seq)
(cl-defstruct (vecdb-psql-provider (:include vecdb-provider
(name "postgres")))
"Provider for the vector database.
DBNAME is the database name, which must have been created by the user."
dbname
username
(password "")
(host "localhost")
(port 5432))
(defconst vecdb-psql-connection-cache
(make-hash-table :test 'equal)
"Cache for database connections by db name.")
(defun vecdb-psql-get-connection (provider)
"Get a connection to the database specified by PROVIDER."
(let* ((key (vecdb-psql-provider-dbname provider))
(connection (gethash key vecdb-psql-connection-cache)))
(unless connection
(setq connection
(pg-connect-plist
(vecdb-psql-provider-dbname provider)
(vecdb-psql-provider-username provider)
:password (vecdb-psql-provider-password provider)
:host (vecdb-psql-provider-host provider)
:port (vecdb-psql-provider-port provider)))
(puthash key connection vecdb-psql-connection-cache))
connection))
(defun vecdb-psql-table-name (collection-name)
"Turn COLLECTION-NAME into a safe table name."
(replace-regexp-in-string "[^a-zA-Z0-9_]" "_" (downcase collection-name)))
(defun vecdb-psql-type (collection-type)
"Convert COLLECTION-TYPE to a PostgreSQL type string."
(pcase collection-type
('string "TEXT")
('integer "INT8")
('float "FLOAT")
(_ (error "Unsupported field type: %s" collection-type))))
(defun vecdb-psql-oid (collection-type)
"Convert COLLECTION-TYPE to a psql OID."
(pcase collection-type
('string "TEXT")
('integer "INT8")
('float "FLOAT8")
(_ (error "Unsupported field type: %s" collection-type))))
(cl-defmethod vecdb-create ((provider vecdb-psql-provider)
(collection vecdb-collection))
"Create COLLECTION in database PROVIDER."
(pg-vector-setup (vecdb-psql-get-connection provider))
(pg-exec (vecdb-psql-get-connection provider)
(format "CREATE EXTENSION IF NOT EXISTS vector;"))
(pg-exec (vecdb-psql-get-connection provider)
(format "CREATE TABLE IF NOT EXISTS %s (
id INT8 PRIMARY KEY,
vector VECTOR(%d) NOT NULL%s
%s
);"
(vecdb-psql-table-name (vecdb-collection-name collection))
(vecdb-collection-vector-size collection)
(if (vecdb-collection-payload-fields collection) "," "")
(mapconcat
(lambda (field)
(format "\"%s\" %s NULL"
(car field)
(vecdb-psql-type (cdr field))))
(vecdb-collection-payload-fields collection)
", ")))
(pg-exec (vecdb-psql-get-connection provider)
(format "CREATE INDEX IF NOT EXISTS %s_embedding_hnsw_idx ON %s USING hnsw (vector vector_cosine_ops)"
(vecdb-psql-table-name (vecdb-collection-name collection))
(vecdb-psql-table-name (vecdb-collection-name collection))))
(mapc (lambda (field)
(pg-exec (vecdb-psql-get-connection provider)
(format "CREATE INDEX IF NOT EXISTS \"%s_%s_idx\" ON %s (\"%s\")"
(vecdb-psql-table-name (vecdb-collection-name collection))
(car field)
(vecdb-psql-table-name (vecdb-collection-name collection))
(car field))))
(vecdb-collection-payload-fields collection)))
(cl-defmethod vecdb-delete ((provider vecdb-psql-provider)
(collection vecdb-collection))
"Delete COLLECTION from database PROVIDER."
(pg-exec (vecdb-psql-get-connection provider)
(format "DROP TABLE IF EXISTS %s;"
(vecdb-psql-table-name (vecdb-collection-name collection)))))
(cl-defmethod vecdb-exists ((provider vecdb-psql-provider)
(collection vecdb-collection))
"Check if the COLLECTION exists in the database specified by PROVIDER."
(let ((result
(pg-exec (vecdb-psql-get-connection provider)
(format "SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = '%s'
);"
(vecdb-psql-table-name (vecdb-collection-name collection))))))
(and result
(equal (caar (pg-result result :tuples)) t))))
(defun vecdb-psql--plist-keys (plist)
"Return a list of keys from PLIST, as strings with the colon removed."
(cl-loop for (k _v) on plist by #'cddr
collect (substring (symbol-name k) 1)))
(cl-defmethod vecdb-upsert-items ((provider vecdb-psql-provider)
(collection vecdb-collection)
data-list &optional _)
"Upsert items into the COLLECTION in the database PROVIDER.
All items in DATA-LIST must have the same payloads."
(pg-vector-setup (vecdb-psql-get-connection provider))
(let ((arg-count 0))
(funcall #'pg-exec-prepared
(vecdb-psql-get-connection provider)
(format "INSERT INTO %s (id, vector%s%s) VALUES %s
ON CONFLICT (id) DO UPDATE SET vector = EXCLUDED.vector%s%s;"
(vecdb-psql-table-name (vecdb-collection-name collection))
(if (vecdb-collection-payload-fields collection) ", " "")
;; We assume every vecdb-item has the same payload structure
(mapconcat (lambda (field) (format "\"%s\"" field))
(vecdb-psql--plist-keys
(vecdb-item-payload (car data-list)))
", ")
(mapconcat (lambda (item)
(format "(%s)"
(string-join (cl-loop for i from 1 below (+ 2 (length (vecdb-item-payload item)))
do (cl-incf arg-count)
collect (format "$%d" arg-count))
", ")))
data-list
", ")
(if (vecdb-collection-payload-fields collection) ", " "")
(mapconcat
(lambda (field)
(format "\"%s\" = EXCLUDED.\"%s\"" (car field) (car field)))
(vecdb-collection-payload-fields collection)
", "))
(mapcan (lambda (item)
(append
(list
(cons (vecdb-item-id item) "int8")
(cons (vecdb-item-vector item) "vector"))
(mapcar (lambda (payload-key)
(cons (plist-get (vecdb-item-payload item) payload-key)
(vecdb-psql-oid (assoc-default
(intern (substring (symbol-name payload-key) 1))
(vecdb-collection-payload-fields collection)))))
(map-keys (vecdb-item-payload (car data-list))))))
data-list))))
(defun vecdb-psql--full-row-to-item (row collection)
"Convert a full database row ROW into a vecdb-item for COLLECTION."
(make-vecdb-item
:id (nth 0 row)
:vector (nth 1 row)
:payload
(flatten-list (cl-loop for field in (vecdb-collection-payload-fields collection)
collect
(list (intern (format ":%s" (car field)))
(nth (+ 2 (cl-position field
(vecdb-collection-payload-fields collection)
:test #'equal))
row))))))
(cl-defmethod vecdb-get-item ((provider vecdb-psql-provider)
(collection vecdb-collection)
id)
"Get an item from COLLECTION by ID.
PROVIDER specifies the database that the collection is in."
(let ((result
(pg-result
(pg-exec-prepared (vecdb-psql-get-connection provider)
(format "SELECT id, vector::vector%s %s FROM %s WHERE id = $1;"
(if (vecdb-collection-payload-fields collection) ", " "")
(mapconcat
(lambda (field)
(format "\"%s\"" (car field)))
(vecdb-collection-payload-fields collection)
", ")
(vecdb-psql-table-name (vecdb-collection-name collection)))
(list (cons id "int8")))
:tuples)))
(when result
(vecdb-psql--full-row-to-item (car result) collection))))
(cl-defmethod vecdb-delete-items ((provider vecdb-psql-provider)
(collection vecdb-collection)
ids &optional _)
"Delete items from COLLECTION by IDs.
PROVIDER is the database that the collection is in."
(when ids
;; TODO: This should ideally be a prepared statement, but I dont know how to do
;; this with psql.
(pg-exec (vecdb-psql-get-connection provider)
(format "DELETE FROM %s WHERE id IN (%s);"
(vecdb-psql-table-name (vecdb-collection-name collection))
(mapconcat #'number-to-string ids ", ")))))
(cl-defmethod vecdb-search-by-vector ((provider vecdb-psql-provider)
(collection vecdb-collection)
vector
&optional limit)
"Search for items in COLLECTION by VECTOR.
PROVIDER is the database that the collection is in."
(let ((limit-clause (if limit
(format "LIMIT %d" limit)
"")))
(mapcar (lambda (row)
(vecdb-psql--full-row-to-item row collection))
(pg-result
(pg-exec-prepared (vecdb-psql-get-connection provider)
(format "SELECT id, vector::vector%s %s FROM %s
ORDER BY vector <-> $1 %s;"
(if (vecdb-collection-payload-fields collection) ", " "")
(mapconcat
(lambda (field)
(format "\"%s\"" (car field)))
(vecdb-collection-payload-fields collection)
", ")
(vecdb-psql-table-name (vecdb-collection-name collection))
limit-clause)
(list (cons vector "vector")))
:tuples))))
(provide 'vecdb-psql)
;;; vecdb-psql.el ends here
|