ruvector-postgres 2.0.5

High-performance PostgreSQL vector database extension v2 - pgvector drop-in replacement with 230+ SQL functions, SIMD acceleration, Flash Attention, GNN layers, hybrid search, multi-tenancy, self-healing, and self-learning capabilities
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# RuVector-Postgres Installation Guide

## Overview

This guide covers installation of RuVector-Postgres on various platforms including standard PostgreSQL, Neon, Supabase, and containerized environments.

## Prerequisites

### System Requirements

| Component | Minimum | Recommended |
|-----------|---------|-------------|
| PostgreSQL | 14+ | 16+ |
| RAM | 4 GB | 16+ GB |
| CPU | x86_64 or ARM64 | x86_64 with AVX2+ |
| Disk | 10 GB | SSD recommended |

### PostgreSQL Version Requirements

RuVector-Postgres supports PostgreSQL 14-18:

| PostgreSQL Version | Status | Notes |
|-------------------|--------|-------|
| 18 | ✓ Full support | Latest features |
| 17 | ✓ Full support | Recommended |
| 16 | ✓ Full support | Stable |
| 15 | ✓ Full support | Stable |
| 14 | ✓ Full support | Minimum version |
| 13 and below | ✗ Not supported | Use pgvector |

### Build Requirements

| Tool | Version | Purpose |
|------|---------|---------|
| Rust | 1.75+ | Compilation |
| Cargo | 1.75+ | Build system |
| pgrx | 0.12.9+ | PostgreSQL extension framework |
| PostgreSQL Dev | 14-18 | Headers and libraries |
| clang | 14+ | LLVM backend for pgrx |
| pkg-config | any | Dependency management |
| git | 2.0+ | Source checkout |

#### pgrx Version Requirements

**Critical:** RuVector-Postgres requires pgrx **0.12.9 or higher**.

```bash
# Install specific pgrx version
cargo install --locked cargo-pgrx@0.12.9

# Verify version
cargo pgrx --version
# Should output: cargo-pgrx 0.12.9 or higher
```

**Known Issues with Earlier Versions:**

- pgrx 0.11.x: Missing varlena APIs, incompatible type system
- pgrx 0.12.0-0.12.8: Potential memory alignment issues

## Installation Methods

### Method 1: Build from Source (Recommended)

#### Step 1: Install Rust

```bash
# Install Rust via rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env

# Verify installation
rustc --version  # Should be 1.75.0 or higher
cargo --version
```

#### Step 2: Install System Dependencies

**Ubuntu/Debian:**

```bash
# PostgreSQL and development headers
sudo apt-get update
sudo apt-get install -y \
    postgresql-16 \
    postgresql-server-dev-16 \
    build-essential \
    pkg-config \
    libssl-dev \
    libclang-dev \
    clang \
    git

# Verify pg_config
pg_config --version
```

**RHEL/CentOS/Fedora:**

```bash
# PostgreSQL and development headers
sudo dnf install -y \
    postgresql16-server \
    postgresql16-devel \
    gcc \
    gcc-c++ \
    pkg-config \
    openssl-devel \
    clang-devel \
    git

# Verify pg_config
/usr/pgsql-16/bin/pg_config --version
```

**macOS:**

```bash
# Install PostgreSQL via Homebrew
brew install postgresql@16

# Install build dependencies
brew install llvm pkg-config

# Add pg_config to PATH
export PATH="/opt/homebrew/opt/postgresql@16/bin:$PATH"

# Verify
pg_config --version
```

#### Step 3: Install pgrx

```bash
# Install pgrx CLI (locked version)
cargo install --locked cargo-pgrx@0.12.9

# Initialize pgrx for your PostgreSQL version
cargo pgrx init --pg16 $(which pg_config)

# Or for multiple versions:
cargo pgrx init \
    --pg14 /usr/lib/postgresql/14/bin/pg_config \
    --pg15 /usr/lib/postgresql/15/bin/pg_config \
    --pg16 /usr/lib/postgresql/16/bin/pg_config

# Verify initialization
ls ~/.pgrx/
# Should show: 16.x, data-16, etc.
```

#### Step 4: Build the Extension

```bash
# Clone the repository
git clone https://github.com/ruvnet/ruvector.git
cd ruvector/crates/ruvector-postgres

# Build for your PostgreSQL version
cargo pgrx package --pg-config $(which pg_config)

# The built extension will be in:
# target/release/ruvector-pg16/usr/share/postgresql/16/extension/
# target/release/ruvector-pg16/usr/lib/postgresql/16/lib/
```

**Build Options:**

```bash
# Debug build (for development)
cargo pgrx package --pg-config $(which pg_config) --debug

# Release build with optimizations (default)
cargo pgrx package --pg-config $(which pg_config) --release

# Test before installing
cargo pgrx test pg16
```

#### Step 5: Install the Extension

```bash
# Copy files to PostgreSQL directories
sudo cp target/release/ruvector-pg16/usr/share/postgresql/16/extension/* \
    /usr/share/postgresql/16/extension/

sudo cp target/release/ruvector-pg16/usr/lib/postgresql/16/lib/* \
    /usr/lib/postgresql/16/lib/

# Set proper permissions
sudo chmod 644 /usr/share/postgresql/16/extension/ruvector*
sudo chmod 755 /usr/lib/postgresql/16/lib/ruvector.so

# Restart PostgreSQL
sudo systemctl restart postgresql

# Or on macOS:
brew services restart postgresql@16
```

#### Step 6: Enable in Database

```sql
-- Connect to your database
psql -U postgres -d your_database

-- Create the extension
CREATE EXTENSION ruvector;

-- Verify installation
SELECT ruvector_version();
-- Expected output: 0.1.19 (or current version)

-- Check SIMD capabilities
SELECT ruvector_simd_info();
-- Expected: AVX512, AVX2, NEON, or Scalar
```

### Method 2: Docker Deployment

#### Quick Start with Docker

```bash
# Pull the pre-built image (when available)
docker pull ruvector/postgres:16

# Run container
docker run -d \
    --name ruvector-postgres \
    -e POSTGRES_PASSWORD=mysecretpassword \
    -e POSTGRES_DB=vectordb \
    -p 5432:5432 \
    -v ruvector-data:/var/lib/postgresql/data \
    ruvector/postgres:16

# Connect and enable extension
docker exec -it ruvector-postgres psql -U postgres -d vectordb
```

#### Building Custom Docker Image

Create a `Dockerfile`:

```dockerfile
# Dockerfile for RuVector-Postgres
FROM postgres:16

# Install build dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    pkg-config \
    libssl-dev \
    libclang-dev \
    clang \
    curl \
    git \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Install Rust
ENV RUSTUP_HOME=/usr/local/rustup \
    CARGO_HOME=/usr/local/cargo \
    PATH=/usr/local/cargo/bin:$PATH
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | \
    sh -s -- -y --default-toolchain 1.75.0

# Install pgrx
RUN cargo install --locked cargo-pgrx@0.12.9
RUN cargo pgrx init --pg16 /usr/lib/postgresql/16/bin/pg_config

# Copy and build extension
COPY . /app/ruvector
WORKDIR /app/ruvector/crates/ruvector-postgres
RUN cargo pgrx install --release --pg-config /usr/lib/postgresql/16/bin/pg_config

# Clean up build dependencies to reduce image size
RUN apt-get remove -y build-essential git curl && \
    apt-get autoremove -y && \
    rm -rf /usr/local/cargo/registry /app/ruvector

# Auto-enable extension on database creation
RUN echo "CREATE EXTENSION IF NOT EXISTS ruvector;" > /docker-entrypoint-initdb.d/init-ruvector.sql

EXPOSE 5432
```

Build and run:

```bash
# Build image
docker build -t ruvector-postgres:custom .

# Run container
docker run -d \
    --name ruvector-db \
    -e POSTGRES_PASSWORD=secret \
    -e POSTGRES_DB=vectordb \
    -p 5432:5432 \
    -v $(pwd)/data:/var/lib/postgresql/data \
    ruvector-postgres:custom

# Verify installation
docker exec -it ruvector-db psql -U postgres -d vectordb -c "SELECT ruvector_version();"
```

#### Docker Compose

Create `docker-compose.yml`:

```yaml
version: '3.8'

services:
  postgres:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: ruvector-postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-secret}
      POSTGRES_DB: vectordb
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

volumes:
  postgres-data:
    driver: local
```

Deploy:

```bash
# Start services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

# Stop and remove volumes
docker-compose down -v
```

### Method 3: Cloud Platforms

#### Neon (Serverless PostgreSQL)

See [NEON_COMPATIBILITY.md](./NEON_COMPATIBILITY.md) for detailed instructions.

**Requirements:**
- Neon Scale plan or higher
- Support ticket for custom extension

**Process:**

1. **Request Installation** (Scale Plan customers):
   ```
   Navigate to: console.neon.tech → Support
   Subject: Custom Extension Request - RuVector-Postgres
   Details:
   - PostgreSQL version: 16 (or your version)
   - Extension: ruvector-postgres v0.1.19
   - Use case: Vector similarity search
   ```

2. **Provide Artifacts**:
   - Pre-built `.so` files
   - Control file (`ruvector.control`)
   - SQL scripts (`ruvector--0.1.0.sql`)

3. **Enable After Approval**:
   ```sql
   CREATE EXTENSION ruvector;
   SELECT ruvector_version();
   ```

#### Supabase

```sql
-- Contact Supabase support for custom extension installation
-- support@supabase.io or via dashboard

-- Once installed:
CREATE EXTENSION ruvector;

-- Verify
SELECT ruvector_version();
```

#### AWS RDS

**Note:** RDS does not support custom extensions. Use EC2 with self-managed PostgreSQL.

**Alternative: RDS with pgvector, migrate later:**

```sql
-- On RDS: Use pgvector
CREATE EXTENSION vector;

-- Migrate to EC2 with RuVector when needed
-- Follow Method 1 (Build from Source)
```

## Configuration

### PostgreSQL Configuration

Add to `postgresql.conf`:

```ini
# RuVector settings
shared_preload_libraries = 'ruvector'  # Optional, for background workers

# Memory settings for vector operations
maintenance_work_mem = '2GB'           # For index builds
work_mem = '256MB'                     # For queries
shared_buffers = '4GB'                 # For caching

# Parallel query settings
max_parallel_workers_per_gather = 4
max_parallel_maintenance_workers = 8
max_worker_processes = 16

# Logging (optional)
log_min_messages = INFO
log_min_duration_statement = 1000      # Log slow queries (1s+)
```

Restart PostgreSQL:

```bash
sudo systemctl restart postgresql
```

### Extension Settings (GUCs)

```sql
-- Search quality (higher = better recall, slower)
SET ruvector.ef_search = 100;          -- Default: 40, Range: 1-1000

-- IVFFlat probes (higher = better recall, slower)
SET ruvector.probes = 10;              -- Default: 1, Range: 1-10000

-- Set globally in postgresql.conf:
ALTER SYSTEM SET ruvector.ef_search = 100;
ALTER SYSTEM SET ruvector.probes = 10;
SELECT pg_reload_conf();
```

### Per-Session Settings

```sql
-- For high-recall queries
BEGIN;
SET LOCAL ruvector.ef_search = 200;
SET LOCAL ruvector.probes = 20;
SELECT * FROM items ORDER BY embedding <-> query LIMIT 10;
COMMIT;

-- For low-latency queries
BEGIN;
SET LOCAL ruvector.ef_search = 20;
SET LOCAL ruvector.probes = 1;
SELECT * FROM items ORDER BY embedding <-> query LIMIT 10;
COMMIT;
```

## Verification

### Check Installation

```sql
-- Verify extension is installed
SELECT * FROM pg_extension WHERE extname = 'ruvector';
-- Expected: extname=ruvector, extversion=0.1.19

-- Check version
SELECT ruvector_version();
-- Expected: 0.1.19

-- Check SIMD capabilities
SELECT ruvector_simd_info();
-- Expected: AVX512, AVX2, NEON, or Scalar
```

### Basic Functionality Test

```sql
-- Create test table
CREATE TABLE test_vectors (
    id SERIAL PRIMARY KEY,
    embedding ruvector(3)
);

-- Insert vectors
INSERT INTO test_vectors (embedding) VALUES
    ('[1, 2, 3]'),
    ('[4, 5, 6]'),
    ('[7, 8, 9]');

-- Test distance calculation
SELECT id, embedding <-> '[1, 1, 1]'::ruvector AS distance
FROM test_vectors
ORDER BY distance
LIMIT 3;

-- Expected output:
-- id | distance
-- ---+-----------
--  1 | 2.449...
--  2 | 6.782...
--  3 | 11.224...

-- Clean up
DROP TABLE test_vectors;
```

### Index Creation Test

```sql
-- Create table with embeddings
CREATE TABLE items (
    id SERIAL PRIMARY KEY,
    embedding ruvector(128)
);

-- Insert sample data (10,000 vectors)
INSERT INTO items (embedding)
SELECT ('[' || array_to_string(array_agg(random()), ',') || ']')::ruvector
FROM generate_series(1, 128) d
CROSS JOIN generate_series(1, 10000) i
GROUP BY i;

-- Create HNSW index
CREATE INDEX items_embedding_idx ON items
USING ruhnsw (embedding ruvector_l2_ops)
WITH (m = 16, ef_construction = 100);

-- Test search with index
EXPLAIN ANALYZE
SELECT * FROM items
ORDER BY embedding <-> (SELECT embedding FROM items LIMIT 1)
LIMIT 10;

-- Verify index usage in plan
-- Should show: "Index Scan using items_embedding_idx"

-- Clean up
DROP TABLE items;
```

## Troubleshooting

### Common Installation Issues

#### 1. Extension Won't Load

```bash
# Check library path
pg_config --pkglibdir
ls -la $(pg_config --pkglibdir)/ruvector*

# Expected output:
# -rwxr-xr-x ... ruvector.so

# Check extension path
pg_config --sharedir
ls -la $(pg_config --sharedir)/extension/ruvector*

# Expected output:
# -rw-r--r-- ... ruvector.control
# -rw-r--r-- ... ruvector--0.1.0.sql

# Check PostgreSQL logs
sudo tail -100 /var/log/postgresql/postgresql-16-main.log
```

**Fix:** Reinstall with correct permissions:

```bash
sudo chmod 755 $(pg_config --pkglibdir)/ruvector.so
sudo chmod 644 $(pg_config --sharedir)/extension/ruvector*
sudo systemctl restart postgresql
```

#### 2. pgrx Version Mismatch

**Error:** `error: failed to load manifest at .../Cargo.toml`

**Cause:** pgrx version < 0.12.9

**Fix:**

```bash
# Uninstall old version
cargo uninstall cargo-pgrx

# Install correct version
cargo install --locked cargo-pgrx@0.12.9

# Re-initialize
cargo pgrx init --pg16 $(which pg_config)

# Rebuild
cargo pgrx package --pg-config $(which pg_config)
```

#### 3. SIMD Not Detected

```sql
-- Check detected SIMD
SELECT ruvector_simd_info();
-- Output: Scalar (unexpected on modern CPUs)
```

**Diagnose:**

```bash
# Linux: Check CPU capabilities
cat /proc/cpuinfo | grep -E 'avx2|avx512'

# macOS: Check CPU features
sysctl -a | grep machdep.cpu.features
```

**Possible Causes:**

- Running in VM without AVX passthrough
- Old CPU without AVX2 support
- Scalar build (missing `target-cpu=native`)

**Fix:** Rebuild with native optimizations:

```bash
# Set Rust flags
export RUSTFLAGS="-C target-cpu=native"

# Rebuild
cargo pgrx package --pg-config $(which pg_config)
sudo systemctl restart postgresql
```

#### 4. Index Build Slow or OOM

**Symptoms:** Index creation times out or crashes

**Solutions:**

```sql
-- Increase maintenance memory
SET maintenance_work_mem = '8GB';

-- Increase parallelism
SET max_parallel_maintenance_workers = 16;

-- Use CONCURRENTLY for non-blocking builds
CREATE INDEX CONCURRENTLY items_embedding_idx ON items
USING ruhnsw (embedding ruvector_l2_ops);

-- Monitor progress
SELECT * FROM pg_stat_progress_create_index;
```

#### 5. Connection Issues

```bash
# Check PostgreSQL is running
sudo systemctl status postgresql

# Check listen addresses
grep listen_addresses /etc/postgresql/16/main/postgresql.conf
# Should be: listen_addresses = '*' or '0.0.0.0'

# Check pg_hba.conf for authentication
sudo cat /etc/postgresql/16/main/pg_hba.conf
# Add: host all all 0.0.0.0/0 md5

# Restart
sudo systemctl restart postgresql
```

## Upgrading

### Minor Version Upgrade (0.1.19 → 0.1.20)

```sql
-- Check current version
SELECT ruvector_version();

-- Upgrade extension
ALTER EXTENSION ruvector UPDATE TO '0.1.20';

-- Verify
SELECT ruvector_version();
```

### Major Version Upgrade

```bash
# Stop PostgreSQL
sudo systemctl stop postgresql

# Install new version
cd ruvector/crates/ruvector-postgres
git pull
cargo pgrx package --pg-config $(which pg_config)
sudo cp target/release/ruvector-pg16/usr/lib/postgresql/16/lib/* \
    $(pg_config --pkglibdir)/

# Start PostgreSQL
sudo systemctl start postgresql

# Upgrade in database
psql -U postgres -d your_database -c "ALTER EXTENSION ruvector UPDATE;"
```

## Uninstallation

```sql
-- Drop all dependent objects first
DROP INDEX IF EXISTS items_embedding_idx;

-- Drop extension
DROP EXTENSION ruvector CASCADE;
```

```bash
# Remove library files
sudo rm $(pg_config --pkglibdir)/ruvector.so
sudo rm $(pg_config --sharedir)/extension/ruvector*

# Restart PostgreSQL
sudo systemctl restart postgresql
```

## Support

- **Documentation**: https://github.com/ruvnet/ruvector/tree/main/crates/ruvector-postgres/docs
- **Issues**: https://github.com/ruvnet/ruvector/issues
- **Discussions**: https://github.com/ruvnet/ruvector/discussions