entelix-memory-pgvector 0.5.4

entelix concrete VectorStore — sqlx + pgvector extension backend
Documentation
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! `PgVectorStore` — concrete `VectorStore` over Postgres + pgvector.
//!
//! Single-table design: `(namespace_key, doc_id)` composite primary
//! key + `embedding VECTOR(N)` + `metadata JSONB`. The composite PK
//! doubles as the namespace anchor index, so every read / write /
//! count / list rides a B-tree probe before the vector / GIN index
//! ever sees a row. Cross-tenant data leak is structurally
//! impossible — the namespace anchor is mandatory in every query.

use std::sync::Arc;

use async_trait::async_trait;
use pgvector::Vector;
use serde_json::Value;
use sqlx::{PgPool, Postgres, QueryBuilder, Row};
use uuid::Uuid;

use entelix_core::context::ExecutionContext;
use entelix_core::error::{Error, Result};
use entelix_memory::{Document, Namespace, VectorFilter, VectorStore};

use crate::error::{PgVectorStoreError, PgVectorStoreResult};
use crate::filter::append_where;
use crate::migration;
use crate::tenant::set_tenant_session;

/// Distance metric used for vector similarity. Mirrors pgvector's
/// own taxonomy 1:1 — operators familiar with `<=>` / `<->` /
/// `<#>` pick the metric they would have picked there.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
#[non_exhaustive]
pub enum DistanceMetric {
    /// Cosine similarity (`<=>` operator). The right default for
    /// normalized embeddings (`text-embedding-3-*`, etc.).
    #[default]
    Cosine,
    /// Euclidean / L2 distance (`<->` operator).
    L2,
    /// Inner product (`<#>` operator). Note: pgvector's `<#>`
    /// returns the *negative* inner product so smaller is "more
    /// similar"; the store inverts it on read so caller-facing
    /// scores stay "higher = better".
    InnerProduct,
}

/// ANN index kind. HNSW is the production default; IVFFlat is
/// selected when build time matters more than query latency.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
#[non_exhaustive]
pub enum IndexKind {
    /// Hierarchical Navigable Small World — pgvector's HNSW. Best
    /// recall / throughput trade-off for ≤10M vectors per
    /// namespace.
    #[default]
    Hnsw,
    /// IVF-Flat — fast build, lower memory at the cost of recall.
    /// Operators must `SET ivfflat.probes = N` per session for
    /// query-time recall tuning.
    IvfFlat,
}

/// Concrete [`VectorStore`] backed by Postgres + pgvector.
///
/// Cloning is cheap — the pool is `Arc`-shared internally.
#[derive(Clone)]
pub struct PgVectorStore {
    pool: PgPool,
    table: Arc<str>,
    dimension: usize,
    distance: DistanceMetric,
}

impl std::fmt::Debug for PgVectorStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PgVectorStore")
            .field("table", &self.table)
            .field("dimension", &self.dimension)
            .field("distance", &self.distance)
            .finish_non_exhaustive()
    }
}

impl PgVectorStore {
    /// Begin building a [`PgVectorStore`].
    pub fn builder(dimension: usize) -> PgVectorStoreBuilder {
        PgVectorStoreBuilder::new(dimension)
    }

    fn distance_op(&self) -> &'static str {
        match self.distance {
            DistanceMetric::Cosine => "<=>",
            DistanceMetric::L2 => "<->",
            DistanceMetric::InnerProduct => "<#>",
        }
    }

    /// Convert pgvector's distance into a "higher = better"
    /// similarity score in `[0.0, 1.0]` for cosine / L2 (best
    /// effort) and the negated inner product for ip metric.
    /// Comparable only within a single query result set.
    fn distance_to_score(&self, distance: f64) -> f32 {
        let s = match self.distance {
            DistanceMetric::Cosine => 1.0 - distance,
            DistanceMetric::L2 => 1.0 / (1.0 + distance),
            // pgvector's `<#>` returns negative inner product;
            // `-distance` recovers the operator-facing similarity.
            DistanceMetric::InnerProduct => -distance,
        };
        s as f32
    }
}

/// Builder for [`PgVectorStore`].
#[must_use]
pub struct PgVectorStoreBuilder {
    table: String,
    dimension: usize,
    distance: DistanceMetric,
    index_kind: IndexKind,
    auto_migrate: bool,
    connection_string: Option<String>,
    pool: Option<PgPool>,
    max_connections: u32,
}

impl PgVectorStoreBuilder {
    fn new(dimension: usize) -> Self {
        Self {
            table: "entelix_vectors".into(),
            dimension,
            distance: DistanceMetric::default(),
            index_kind: IndexKind::default(),
            auto_migrate: true,
            connection_string: None,
            pool: None,
            max_connections: 10,
        }
    }

    /// Override the table name. Defaults to `entelix_vectors`.
    /// Must satisfy SQL-identifier rules
    /// (`[a-zA-Z_][a-zA-Z0-9_]{0,62}`).
    pub fn with_table(mut self, table: impl Into<String>) -> Self {
        self.table = table.into();
        self
    }

    /// Override the distance metric. Defaults to
    /// [`DistanceMetric::Cosine`].
    pub const fn with_distance(mut self, distance: DistanceMetric) -> Self {
        self.distance = distance;
        self
    }

    /// Override the ANN index kind. Defaults to
    /// [`IndexKind::Hnsw`].
    pub const fn with_index_kind(mut self, kind: IndexKind) -> Self {
        self.index_kind = kind;
        self
    }

    /// Disable the automatic schema bootstrap.
    ///
    /// Pass `false` when the table + extension + indexes are
    /// provisioned externally (DBA-managed, IaC, migration
    /// pipeline) and the store should consume an existing schema.
    /// Defaults to `true`.
    pub const fn with_auto_migrate(mut self, auto: bool) -> Self {
        self.auto_migrate = auto;
        self
    }

    /// Connect with a libpq-style connection string. Mutually
    /// exclusive with [`Self::with_pool`].
    pub fn with_connection_string(mut self, url: impl Into<String>) -> Self {
        self.connection_string = Some(url.into());
        self
    }

    /// Reuse an existing `PgPool`. Mutually exclusive with
    /// [`Self::with_connection_string`].
    pub fn with_pool(mut self, pool: PgPool) -> Self {
        self.pool = Some(pool);
        self
    }

    /// Override the pool's `max_connections` (when the builder
    /// constructs the pool). Ignored when [`Self::with_pool`]
    /// supplies a pre-built pool.
    pub const fn with_max_connections(mut self, max: u32) -> Self {
        self.max_connections = max;
        self
    }

    /// Finalize the builder. Connects (or adopts the supplied
    /// pool) and runs the schema bootstrap when
    /// `auto_migrate=true`.
    pub async fn build(self) -> PgVectorStoreResult<PgVectorStore> {
        let pool = match (self.pool, self.connection_string) {
            (Some(p), None) => p,
            (None, Some(url)) => {
                sqlx::postgres::PgPoolOptions::new()
                    .max_connections(self.max_connections)
                    .connect(&url)
                    .await?
            }
            (None, None) => {
                return Err(PgVectorStoreError::Config(
                    "either with_pool or with_connection_string is required".into(),
                ));
            }
            (Some(_), Some(_)) => {
                return Err(PgVectorStoreError::Config(
                    "with_pool and with_connection_string are mutually exclusive".into(),
                ));
            }
        };

        if self.auto_migrate {
            migration::bootstrap(
                &pool,
                &self.table,
                self.dimension,
                self.distance,
                self.index_kind,
            )
            .await?;
        }

        Ok(PgVectorStore {
            pool,
            table: self.table.into(),
            dimension: self.dimension,
            distance: self.distance,
        })
    }
}

#[async_trait]
impl VectorStore for PgVectorStore {
    fn dimension(&self) -> usize {
        self.dimension
    }

    async fn add(
        &self,
        ctx: &ExecutionContext,
        ns: &Namespace,
        document: Document,
        vector: Vec<f32>,
    ) -> Result<()> {
        if ctx.is_cancelled() {
            return Err(Error::Cancelled);
        }
        if vector.len() != self.dimension {
            return Err(Error::invalid_request(format!(
                "PgVectorStore: vector dimension {} does not match \
                 index dimension {}",
                vector.len(),
                self.dimension
            )));
        }
        let ns_key = ns.render();
        let doc_id = document
            .doc_id
            .clone()
            .unwrap_or_else(|| Uuid::new_v4().to_string());
        let metadata = if document.metadata.is_null() {
            Value::Object(serde_json::Map::new())
        } else {
            document.metadata
        };
        let stmt = format!(
            "INSERT INTO {table} (tenant_id, namespace_key, doc_id, content, metadata, embedding) \
             VALUES ($1, $2, $3, $4, $5, $6) \
             ON CONFLICT (namespace_key, doc_id) DO UPDATE SET \
                 content = EXCLUDED.content, \
                 metadata = EXCLUDED.metadata, \
                 embedding = EXCLUDED.embedding",
            table = self.table
        );
        let mut tx = self
            .pool
            .begin()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        set_tenant_session(&mut *tx, ns.tenant_id()).await?;
        sqlx::query(&stmt)
            .bind(ns.tenant_id().as_str())
            .bind(ns_key)
            .bind(doc_id)
            .bind(document.content)
            .bind(sqlx::types::Json(metadata))
            .bind(Vector::from(vector))
            .execute(&mut *tx)
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        tx.commit()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        Ok(())
    }

    async fn add_batch(
        &self,
        ctx: &ExecutionContext,
        ns: &Namespace,
        items: Vec<(Document, Vec<f32>)>,
    ) -> Result<()> {
        if ctx.is_cancelled() {
            return Err(Error::Cancelled);
        }
        if items.is_empty() {
            return Ok(());
        }
        let ns_key = ns.render();
        for (_, vector) in &items {
            if vector.len() != self.dimension {
                return Err(Error::invalid_request(format!(
                    "PgVectorStore: vector dimension {} does not match \
                     index dimension {}",
                    vector.len(),
                    self.dimension
                )));
            }
        }
        // Bulk insert via QueryBuilder::push_values — single round-trip.
        let tenant_id = ns.tenant_id().as_str().to_owned();
        let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new(format!(
            "INSERT INTO {table} \
             (tenant_id, namespace_key, doc_id, content, metadata, embedding) ",
            table = self.table
        ));
        qb.push_values(items, |mut b, (mut document, vector)| {
            let doc_id = document
                .doc_id
                .take()
                .unwrap_or_else(|| Uuid::new_v4().to_string());
            let metadata = if document.metadata.is_null() {
                Value::Object(serde_json::Map::new())
            } else {
                document.metadata
            };
            b.push_bind(tenant_id.clone())
                .push_bind(ns_key.clone())
                .push_bind(doc_id)
                .push_bind(document.content)
                .push_bind(sqlx::types::Json(metadata))
                .push_bind(Vector::from(vector));
        });
        qb.push(
            " ON CONFLICT (namespace_key, doc_id) DO UPDATE SET \
                 content = EXCLUDED.content, \
                 metadata = EXCLUDED.metadata, \
                 embedding = EXCLUDED.embedding",
        );
        let mut tx = self
            .pool
            .begin()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        set_tenant_session(&mut *tx, ns.tenant_id()).await?;
        qb.build()
            .execute(&mut *tx)
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        tx.commit()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        Ok(())
    }

    async fn search(
        &self,
        ctx: &ExecutionContext,
        ns: &Namespace,
        query_vector: &[f32],
        top_k: usize,
    ) -> Result<Vec<Document>> {
        self.search_filtered(ctx, ns, query_vector, top_k, &VectorFilter::All)
            .await
    }

    async fn search_filtered(
        &self,
        ctx: &ExecutionContext,
        ns: &Namespace,
        query_vector: &[f32],
        top_k: usize,
        filter: &VectorFilter,
    ) -> Result<Vec<Document>> {
        if ctx.is_cancelled() {
            return Err(Error::Cancelled);
        }
        if query_vector.len() != self.dimension {
            return Err(Error::invalid_request(format!(
                "PgVectorStore: query dimension {} does not match \
                 index dimension {}",
                query_vector.len(),
                self.dimension
            )));
        }
        let ns_key = ns.render();

        // Postgres lets `ORDER BY <alias>` reference the SELECT
        // alias directly, so the query vector binds exactly once
        // — emitted into the SELECT distance expression and
        // reused by the ORDER BY through the `distance` alias.
        let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new(format!(
            "SELECT doc_id, content, metadata, embedding {op} ",
            op = self.distance_op(),
        ));
        qb.push_bind(Vector::from(query_vector.to_vec()));
        qb.push(format!(" AS distance FROM {table}", table = self.table));
        append_where(&mut qb, &ns_key, Some(filter)).map_err(Error::from)?;
        qb.push(" ORDER BY distance LIMIT ");
        qb.push_bind(top_k as i64);

        let mut tx = self
            .pool
            .begin()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        set_tenant_session(&mut *tx, ns.tenant_id()).await?;
        let rows = qb
            .build()
            .fetch_all(&mut *tx)
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        tx.commit()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        rows.into_iter()
            .map(|row| self.row_to_document(&row, true))
            .collect()
    }

    async fn delete(&self, ctx: &ExecutionContext, ns: &Namespace, doc_id: &str) -> Result<()> {
        if ctx.is_cancelled() {
            return Err(Error::Cancelled);
        }
        let stmt = format!(
            "DELETE FROM {table} WHERE namespace_key = $1 AND doc_id = $2",
            table = self.table
        );
        let mut tx = self
            .pool
            .begin()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        set_tenant_session(&mut *tx, ns.tenant_id()).await?;
        sqlx::query(&stmt)
            .bind(ns.render())
            .bind(doc_id.to_owned())
            .execute(&mut *tx)
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        tx.commit()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        Ok(())
    }

    async fn update(
        &self,
        ctx: &ExecutionContext,
        ns: &Namespace,
        doc_id: &str,
        document: Document,
        vector: Vec<f32>,
    ) -> Result<()> {
        // `INSERT … ON CONFLICT … DO UPDATE` is atomic per-row, so
        // we override the trait's non-atomic delete-then-add
        // default via the same code path as `add`.
        let stored = Document {
            doc_id: Some(doc_id.to_owned()),
            ..document
        };
        self.add(ctx, ns, stored, vector).await
    }

    async fn count(
        &self,
        ctx: &ExecutionContext,
        ns: &Namespace,
        filter: Option<&VectorFilter>,
    ) -> Result<usize> {
        if ctx.is_cancelled() {
            return Err(Error::Cancelled);
        }
        let ns_key = ns.render();
        let mut qb: QueryBuilder<'_, Postgres> =
            QueryBuilder::new(format!("SELECT COUNT(*) FROM {table}", table = self.table));
        append_where(&mut qb, &ns_key, filter).map_err(Error::from)?;
        let mut tx = self
            .pool
            .begin()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        set_tenant_session(&mut *tx, ns.tenant_id()).await?;
        let row = qb
            .build()
            .fetch_one(&mut *tx)
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        tx.commit()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        let count: i64 = row.try_get::<i64, _>(0).map_err(|e| {
            Error::from(PgVectorStoreError::Malformed(format!(
                "COUNT(*) row missing expected column: {e}"
            )))
        })?;
        Ok(count.max(0) as usize)
    }

    async fn list(
        &self,
        ctx: &ExecutionContext,
        ns: &Namespace,
        filter: Option<&VectorFilter>,
        limit: usize,
        offset: usize,
    ) -> Result<Vec<Document>> {
        if ctx.is_cancelled() {
            return Err(Error::Cancelled);
        }
        let ns_key = ns.render();
        let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new(format!(
            "SELECT doc_id, content, metadata FROM {table}",
            table = self.table
        ));
        append_where(&mut qb, &ns_key, filter).map_err(Error::from)?;
        // Stable iteration order — `(namespace_key, doc_id)` is
        // the PK so the ordering is deterministic across calls.
        qb.push(" ORDER BY doc_id");
        qb.push(" LIMIT ");
        qb.push_bind(limit as i64);
        qb.push(" OFFSET ");
        qb.push_bind(offset as i64);
        let mut tx = self
            .pool
            .begin()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        set_tenant_session(&mut *tx, ns.tenant_id()).await?;
        let rows = qb
            .build()
            .fetch_all(&mut *tx)
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        tx.commit()
            .await
            .map_err(|e| Error::from(PgVectorStoreError::from(e)))?;
        rows.into_iter()
            .map(|row| self.row_to_document(&row, false))
            .collect()
    }
}

impl PgVectorStore {
    fn row_to_document(
        &self,
        row: &sqlx::postgres::PgRow,
        with_distance: bool,
    ) -> Result<Document> {
        let doc_id: String = row.try_get("doc_id").map_err(|e| {
            Error::from(PgVectorStoreError::Malformed(format!(
                "row missing doc_id: {e}"
            )))
        })?;
        let content: String = row.try_get("content").map_err(|e| {
            Error::from(PgVectorStoreError::Malformed(format!(
                "row missing content: {e}"
            )))
        })?;
        let metadata: sqlx::types::Json<Value> = row.try_get("metadata").map_err(|e| {
            Error::from(PgVectorStoreError::Malformed(format!(
                "row missing metadata: {e}"
            )))
        })?;
        let score = if with_distance {
            let distance: f64 = row.try_get("distance").map_err(|e| {
                Error::from(PgVectorStoreError::Malformed(format!(
                    "row missing distance: {e}"
                )))
            })?;
            Some(self.distance_to_score(distance))
        } else {
            None
        };
        Ok(Document {
            doc_id: Some(doc_id),
            content,
            metadata: metadata.0,
            score,
        })
    }
}