> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getbifrost.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# pgvector

> PostgreSQL pgvector integration for vector storage in Bifrost.

## pgvector

[pgvector](https://github.com/pgvector/pgvector) adds vector similarity search to PostgreSQL. With it, Bifrost keeps its vectors in a PostgreSQL database, so a deployment that already runs PostgreSQL needs no separate vector database.

### Key Features

* **Runs on PostgreSQL**: the same database as the config or logs store, or a separate one
* **HNSW indexes**: approximate nearest-neighbour search, with filters on stored properties
* **Shared across replicas**: every Bifrost replica reads and writes the same tables
* **Own connection pool**: pgvector connects with its own settings and pool, like the other vector stores

### Requirements

* PostgreSQL with the pgvector extension, **version 0.8.0 or later**. 0.8.0 is the first release with iterative index scans, which a filtered search needs to return a full set of results.
* At startup Bifrost runs `CREATE EXTENSION IF NOT EXISTS vector` when its database user is allowed to. If it is not, Bifrost refuses to start and prints the command a database administrator has to run.
* Bifrost does not upgrade an installed extension. An older version makes it refuse to start with the `ALTER EXTENSION vector UPDATE;` command to run.
* The extension's schema must be on the database user's `search_path`. If it is not, the startup error names the schema and gives an `ALTER ROLE ... SET search_path` command that keeps the current entries.

### Setup & Installation

**Local PostgreSQL:**

```bash theme={null}
# PostgreSQL 16 with the pgvector extension available
docker run -d \
  --name pgvector \
  -p 5432:5432 \
  -e POSTGRES_USER=bifrost \
  -e POSTGRES_PASSWORD=bifrost_password \
  -e POSTGRES_DB=bifrost \
  pgvector/pgvector:0.8.0-pg16
```

**Managed PostgreSQL** (Amazon RDS, Cloud SQL, Azure Database for PostgreSQL, Supabase, and others): the extension is available but has to be created in the database, and older engine versions offer a pgvector older than 0.8.0. Check what the database offers before enabling it:

```sql theme={null}
SELECT default_version, installed_version FROM pg_available_extensions WHERE name = 'vector';
```

`default_version` must be 0.8.0 or later; if it is not, upgrade the engine's minor version. If Bifrost's user may not create extensions, have an administrator run `CREATE EXTENSION vector;` in the database first.

### Configuration Options

> [tab] Go SDK
>
> ```go theme={null}
> import (
>     "github.com/maximhq/bifrost/core/schemas"
>     "github.com/maximhq/bifrost/framework/postgresconn"
>     "github.com/maximhq/bifrost/framework/vectorstore"
> )
>
> vectorConfig := &vectorstore.Config{
>     Enabled: true,
>     Type:    vectorstore.VectorStoreTypePgVector,
>     Config: vectorstore.PgVectorConfig{
>         Config: postgresconn.Config{
>             Host:     schemas.NewSecretVar("localhost"),
>             Port:     schemas.NewSecretVar("5432"),
>             User:     schemas.NewSecretVar("bifrost"),
>             Password: schemas.NewSecretVar("env.PGVECTOR_PASSWORD"),
>             DBName:   schemas.NewSecretVar("bifrost"),
>             SSLMode:  schemas.NewSecretVar("disable"),
>         },
>     },
> }
>
> store, err := vectorstore.NewVectorStore(context.Background(), vectorConfig, logger)
> ```
>
> The store owns its connection pool; `store.Close(ctx, "")` closes it.

> [tab] config.json
>
> **Same database as the logs store:**
>
> ```json theme={null}
> {
>   "logs_store": {
>     "enabled": true,
>     "type": "postgres",
>     "config": {
>       "host": "env.PG_HOST",
>       "port": "5432",
>       "user": "bifrost",
>       "password": "env.PG_PASSWORD",
>       "db_name": "bifrost",
>       "ssl_mode": "require"
>     }
>   },
>   "vector_store": {
>     "enabled": true,
>     "type": "pgvector",
>     "config": {
>       "host": "env.PG_HOST",
>       "port": "5432",
>       "user": "bifrost",
>       "password": "env.PG_PASSWORD",
>       "db_name": "bifrost",
>       "ssl_mode": "require"
>     }
>   }
> }
> ```
>
> **Separate database, with a smaller pool:**
>
> ```json theme={null}
> {
>   "vector_store": {
>     "enabled": true,
>     "type": "pgvector",
>     "config": {
>       "host": "vectors.example.com",
>       "port": "5432",
>       "user": "bifrost_vectors",
>       "password": "env.PGVECTOR_PASSWORD",
>       "db_name": "vectors",
>       "ssl_mode": "verify-full",
>       "max_open_conns": 20
>     }
>   }
> }
> ```
>
> **Dynamic credentials** (for example Amazon RDS IAM authentication):
>
> ```json theme={null}
> {
>   "vector_store": {
>     "enabled": true,
>     "type": "pgvector",
>     "config": {
>       "host": "mydb.abc123.us-east-1.rds.amazonaws.com",
>       "port": "5432",
>       "user": "bifrost",
>       "password_command": {
>         "command": "aws",
>         "args": ["rds", "generate-db-auth-token", "--hostname", "mydb.abc123.us-east-1.rds.amazonaws.com", "--port", "5432", "--username", "bifrost"],
>         "cache_ttl": "10m"
>       },
>       "db_name": "bifrost",
>       "ssl_mode": "require"
>     }
>   }
> }
> ```

### Configuration Reference

The `config` block takes the same connection settings as a PostgreSQL config or logs store, without the logs store's materialized-view and migration settings.

| Field | Type | Default | Description |
| - | - | - | - |
| `host` | string | — | Database host. Supports `env.VAR_NAME` syntax. **Required.** |
| `port` | string | — | Database port. **Required.** |
| `user` | string | — | Database user. **Required.** |
| `password` | string | — | Database password. Supports `env.VAR_NAME` syntax. Set this or `password_command`. |
| `password_command` | object | — | Command run without a shell whose stdout is the password: `command` (required), `args`, `timeout` (default `10s`), and `cache_ttl` (default `60s`). Set this or `password`. |
| `db_name` | string | — | Database name. **Required.** |
| `ssl_mode` | string | — | `disable`, `allow`, `prefer`, `require`, `verify-ca`, or `verify-full`. **Required.** |
| `max_idle_conns` | integer | `5` | Idle connections kept in pgvector's pool. `0` also means the default. |
| `max_open_conns` | integer | `50` | Open connections in pgvector's pool. |
| `conn_max_lifetime` | string | — | Maximum lifetime of a connection (Go duration, e.g. `1h`). |
| `conn_max_idle_time` | string | `5m` | How long an idle connection is kept. |
| `statement_timeout` | string | `120s` | `statement_timeout` set on every connection. `"0"` leaves the server default. |
| `idle_in_transaction_session_timeout` | string | `120s` | `idle_in_transaction_session_timeout` set on every connection. `"0"` leaves the server default. |

### How Vectors Are Stored

* **One table per namespace.** pgvector indexes only fixed-dimension columns, so each namespace gets its own table, named from a hash of the namespace and its dimension. The `vector_store_namespaces` table maps namespaces to their tables.
* **Indexing by dimension.** Up to 2,000 dimensions a namespace gets an HNSW index on the `vector` column. From 2,001 to 4,000 it gets an HNSW index over a `halfvec` expression, and results are re-ranked at full precision. Above 4,000, up to the 16,000 maximum, searches are exact.
* **Properties** are one `jsonb` column with a GIN index, which serves equality and contains filters. Integer properties declared when the namespace is created also get a btree index, which serves range filters.
* **Entries without a vector** are allowed, so caches that store metadata-only entries work as they do on Redis.

### Production Considerations

* **Connections.** pgvector opens its own pool, in addition to the config and logs stores' pools. A pod's connection ceiling is the sum of their `max_open_conns`, which matters on a small managed instance.
* **Shared load.** On the same database as the logs store, vector searches and writes share its CPU, memory, and I/O with request logging.
* **Multiple replicas.** Replicas coordinate namespace creation and deletion through PostgreSQL advisory locks, so they can start together against an empty database.
* **Session settings.** pgvector sets the settings each search needs, such as `hnsw.iterative_scan`, on the transaction, and pins `READ COMMITTED` where it depends on it, so it does not rely on the database user's defaults.

> [!NOTE]
>
> For Kubernetes deployments, see [Helm storage](https://docs.getbifrost.ai/deployment-guides/helm/storage.md#pgvector). The chart's bundled PostgreSQL image does not include pgvector by default.

For the VectorStore interface API and usage examples, see [Vector Store Architecture](https://docs.getbifrost.ai/architecture/framework/vector-store.md). For semantic caching setup, see [Semantic Caching](https://docs.getbifrost.ai/features/semantic-caching.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.