ruvector/crates/ruvector-postgres/docs/GNN_INDEX.md
rUv eb1227047d feat(postgres): Add 7 advanced AI modules to ruvector-postgres
Comprehensive implementation of advanced AI capabilities:

## New Modules (23,541 lines of code)

### 1. Self-Learning / ReasoningBank (`src/learning/`)
- Trajectory tracking for query optimization
- Pattern extraction using K-means clustering
- ReasoningBank for pattern storage and matching
- Adaptive search parameter optimization

### 2. Attention Mechanisms (`src/attention/`)
- Scaled dot-product attention (core)
- Multi-head attention with parallel heads
- Flash Attention v2 (memory-efficient)
- 10 attention types with PostgresEnum support

### 3. GNN Layers (`src/gnn/`)
- Message passing framework
- GCN (Graph Convolutional Network)
- GraphSAGE with mean/max aggregation
- Configurable aggregation methods

### 4. Hyperbolic Embeddings (`src/hyperbolic/`)
- Poincaré ball model
- Lorentz hyperboloid model
- Hyperbolic distance metrics
- Möbius operations

### 5. Sparse Vectors (`src/sparse/`)
- COO format sparse vector type
- Efficient sparse-sparse distance functions
- BM25/SPLADE compatible
- Top-k pruning operations

### 6. Graph Operations & Cypher (`src/graph/`)
- Property graph storage (nodes/edges)
- BFS, DFS, Dijkstra traversal
- Cypher query parser (AST-based)
- Query executor with pattern matching

### 7. Tiny Dancer Routing (`src/routing/`)
- FastGRNN neural network
- Agent registry with capabilities
- Multi-objective routing optimization
- Cost/latency/quality balancing

## Docker Infrastructure
- Dockerfile with pgrx 0.12.6 and PostgreSQL 16
- docker-compose.yml with test runner
- Initialization SQL with test tables
- Shell scripts for dev/test/benchmark

## Feature Flags
- `learning`, `attention`, `gnn`, `hyperbolic`
- `sparse`, `graph`, `routing`
- `ai-complete` and `graph-complete` bundles

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 20:12:48 +00:00

5.6 KiB

GNN Module Index

Overview

Complete Graph Neural Network (GNN) implementation for ruvector-postgres PostgreSQL extension.

Total Lines of Code: 1,301
Total Documentation: 1,156 lines
Implementation Status: Complete

Source Files

Core Implementation (src/gnn/)

File Lines Description
mod.rs 30 Module exports and organization
message_passing.rs 233 Message passing framework, adjacency lists, propagation
aggregators.rs 197 Sum/mean/max aggregation functions
gcn.rs 227 Graph Convolutional Network layer
graphsage.rs 300 GraphSAGE with neighbor sampling
operators.rs 314 PostgreSQL operator functions
Total 1,301 Complete GNN implementation

Documentation Files

User Documentation (docs/)

File Lines Purpose
GNN_IMPLEMENTATION_SUMMARY.md 280 Architecture overview and design decisions
GNN_QUICK_REFERENCE.md 368 SQL function reference and common patterns
GNN_USAGE_EXAMPLES.md 508 Real-world examples and applications
Total 1,156 Comprehensive documentation

Key Features

Implemented Components

Message Passing Framework

  • Generic MessagePassing trait
  • build_adjacency_list() for graph structure
  • propagate() for message passing
  • propagate_weighted() for edge weights
  • Parallel node processing with Rayon

Aggregation Functions

  • Sum aggregation
  • Mean aggregation
  • Max aggregation (element-wise)
  • Weighted aggregation
  • Generic aggregate() function

GCN Layer

  • Xavier/Glorot weight initialization
  • Degree normalization
  • Linear transformation
  • ReLU activation
  • Optional bias terms
  • Edge weight support

GraphSAGE Layer

  • Uniform neighbor sampling
  • Multiple aggregator types (Mean, MaxPool, LSTM)
  • Separate neighbor/self weight matrices
  • L2 normalization
  • Inductive learning support

PostgreSQL Operators

  • ruvector_gcn_forward()
  • ruvector_gnn_aggregate()
  • ruvector_message_pass()
  • ruvector_graphsage_forward()
  • ruvector_gnn_batch_forward()

Testing Coverage

Unit Tests

  • Message passing correctness
  • All aggregation methods
  • GCN layer forward pass
  • GraphSAGE sampling
  • Edge cases (disconnected nodes, empty graphs)

PostgreSQL Tests (#[pg_test])

  • SQL function correctness
  • Empty input handling
  • Weighted edges
  • Batch processing
  • Different aggregation methods

SQL Functions Reference

1. GCN Forward Pass

ruvector_gcn_forward(embeddings, src, dst, weights, out_dim) -> FLOAT[][]

2. GNN Aggregation

ruvector_gnn_aggregate(messages, method) -> FLOAT[]

3. GraphSAGE Forward Pass

ruvector_graphsage_forward(embeddings, src, dst, out_dim, num_samples) -> FLOAT[][]

4. Multi-Hop Message Passing

ruvector_message_pass(node_table, edge_table, embedding_col, hops, layer_type) -> TEXT

5. Batch Processing

ruvector_gnn_batch_forward(embeddings_batch, edge_indices, graph_sizes, layer_type, out_dim) -> FLOAT[][]

Usage Examples

Basic GCN

SELECT ruvector_gcn_forward(
    ARRAY[ARRAY[1.0, 2.0], ARRAY[3.0, 4.0]],
    ARRAY[0], ARRAY[1], NULL, 8
);

Aggregation

SELECT ruvector_gnn_aggregate(
    ARRAY[ARRAY[1.0, 2.0], ARRAY[3.0, 4.0]],
    'mean'
);

GraphSAGE with Sampling

SELECT ruvector_graphsage_forward(
    node_embeddings, edge_src, edge_dst, 64, 10
);

Performance Characteristics

  • Parallel Processing: All nodes processed concurrently via Rayon
  • Memory Efficient: HashMap-based adjacency lists for sparse graphs
  • Scalable Sampling: GraphSAGE samples k neighbors instead of processing all
  • Batch Support: Process multiple graphs simultaneously
  • Zero-Copy: Minimal data copying during operations

Integration

The GNN module is integrated into the main extension via:

// src/lib.rs
pub mod gnn;

All functions are automatically registered with PostgreSQL via pgrx macros.

Dependencies

  • pgrx - PostgreSQL extension framework
  • rayon - Parallel processing
  • rand - Random neighbor sampling
  • serde_json - JSON serialization

Documentation Structure

docs/
├── GNN_INDEX.md                    # This file - index of all GNN files
├── GNN_IMPLEMENTATION_SUMMARY.md   # Architecture and design
├── GNN_QUICK_REFERENCE.md          # SQL function reference
└── GNN_USAGE_EXAMPLES.md           # Real-world examples

Source Code Structure

src/gnn/
├── mod.rs                # Module exports
├── message_passing.rs    # Core framework
├── aggregators.rs        # Aggregation functions
├── gcn.rs               # GCN layer
├── graphsage.rs         # GraphSAGE layer
└── operators.rs         # PostgreSQL functions

Next Steps

To use the GNN module:

  1. Install Extension:

    CREATE EXTENSION ruvector;
    
  2. Check Functions:

    \df ruvector_gnn_*
    \df ruvector_gcn_*
    \df ruvector_graphsage_*
    
  3. Run Examples: See GNN_USAGE_EXAMPLES.md

References


Status: Implementation Complete
Last Updated: 2025-12-02
Version: 1.0.0