DocumentationNeuronDB Documentation
Documentation Branch: You are viewing documentation for the main branch (3.0.0-devel). Select a branch to view its documentation:

SQL API Reference

SQL API Reference

Complete reference for all NeuronDB SQL functions, operators, types, and configuration parameters.

Table of Contents

Vector Types

vector(n)

A fixed-dimension vector of floating-point numbers.

-- Create table with vector column
CREATE TABLE embeddings (
  id SERIAL PRIMARY KEY,
  embedding vector(1536)  -- 1536-dimensional vector
);

-- Insert vectors
INSERT INTO embeddings (embedding) VALUES ('[1, 2, 3]');
INSERT INTO embeddings (embedding) VALUES (ARRAY[1.0, 2.0, 3.0]::vector);

-- Cast from array
SELECT ARRAY[1.0, 2.0, 3.0]::vector(3);

Distance Operators

OperatorDescriptionReturn TypeStabilityExample
<->L2 (Euclidean) distancefloatStableembedding <-> '[1,2,3]'
<=>Cosine distancefloatStableembedding <=> '[1,2,3]'
<#>Inner product (negative dot product)floatStableembedding <#> '[1,2,3]'

Embedding Functions

neurondb_embed(text, model) {#stability-stable}

Generate embeddings using configured LLM providers.

Stability: Stable
Parameters: text (TEXT), model (TEXT)
Returns: vector

-- Configure OpenAI provider
SET neurondb.llm_provider = 'openai';
SET neurondb.llm_api_key = 'sk-...';

-- Generate embedding
SELECT neurondb_embed('Hello world', 'text-embedding-ada-002');

-- Batch embeddings
INSERT INTO documents (content, embedding)
SELECT content, neurondb_embed(content, 'text-embedding-ada-002')
FROM source_documents;

neurondb_embed_batch(texts, model) {#stability-stable}

Generate embeddings for multiple texts in a single API call (more efficient).

Stability: Stable
Parameters: texts (TEXT[]), model (TEXT)
Returns: vector[]

-- Batch embed multiple texts
SELECT neurondb_embed_batch(
  ARRAY['text1', 'text2', 'text3'],
  'text-embedding-ada-002'
);

GPU Distance Functions

GPU-accelerated distance computation functions. Require neurondb.compute_mode = true.

vector_l2_distance_gpu(a, b) {#stability-experimental}

Compute L2 distance on GPU.

Stability: Experimental
Parameters: a (vector), b (vector)
Returns: float8

SET neurondb.compute_mode = true;
SELECT vector_l2_distance_gpu(embedding, '[1,2,3]'::vector) FROM documents;

vector_cosine_distance_gpu(a, b) {#stability-experimental}

Compute cosine distance on GPU.

Stability: Experimental

vector_inner_product_gpu(a, b) {#stability-experimental}

Compute inner product on GPU.

Stability: Experimental

vector_to_int8_gpu(v), vector_to_fp16_gpu(v), vector_to_binary_gpu(v) {#stability-experimental}

Quantize vectors on GPU.

Stability: Experimental

ML Analytics Functions

cluster_kmeans(data, k, max_iter, tol) {#stability-stable}

K-Means clustering aggregate function.

Stability: Stable
Parameters: data (vector), k (int), max_iter (int, default 100), tol (float8, default 0.0001)
Returns: TABLE(cluster_id int, centroid vector, size bigint)

SELECT * FROM cluster_kmeans(
  (SELECT embedding FROM documents),
  5,  -- 5 clusters
  100,  -- max iterations
  0.0001  -- tolerance
);

cluster_minibatch_kmeans(data, k, batch_size, max_iter) {#stability-stable}

Mini-batch K-Means for large datasets.

Stability: Stable
Parameters: data (vector), k (int), batch_size (int, default 100), max_iter (int, default 100)
Returns: TABLE(cluster_id int, centroid vector, size bigint)

cluster_gmm(data, k, max_iter, tol) {#stability-experimental}

Gaussian Mixture Model clustering with soft assignments.

Stability: Experimental
Parameters: data (vector), k (int), max_iter (int, default 100), tol (float8, default 0.0001)
Returns: TABLE(cluster_id int, mean vector, covariance vector, weight float8)

-- Convert soft assignments to hard clusters with helper function
SELECT gmm_to_clusters(
  embedding,
  ARRAY(SELECT mean FROM cluster_gmm(...)),
  ARRAY(SELECT covariance FROM cluster_gmm(...)),
  ARRAY(SELECT weight FROM cluster_gmm(...))
) AS cluster_id
FROM documents;

detect_outliers_zscore(data, threshold) {#stability-stable}

Detect outliers using Z-score method.

Stability: Stable
Parameters: data (vector), threshold (float8, default 3.0)
Returns: TABLE(vector_data vector, is_outlier boolean, z_score float8)

-- Detect outliers with Z-score > 3
SELECT 
  id,
  (stats).is_outlier,
  (stats).z_score
FROM (
  SELECT 
    id,
    detect_outliers_zscore(embedding, 3.0) AS stats
  FROM documents
) sub
WHERE (stats).is_outlier;

ML Project Management

neurondb_create_ml_project(name, description) {#stability-experimental}

Create a new ML project for model management.

Stability: Experimental

neurondb_train_kmeans_project(project_id, data, k, max_iter) {#stability-experimental}

Train a K-Means model within a project.

Stability: Experimental

neurondb_list_project_models(project_id) {#stability-experimental}

List all models in a project.

Stability: Experimental

neurondb_deploy_model(model_id, deployment_name) {#stability-experimental}

Deploy a model for inference.

Stability: Experimental

neurondb_get_deployed_model(deployment_name) {#stability-experimental}

Retrieve deployed model metadata.

Stability: Experimental

neurondb_get_project_info(project_id) {#stability-experimental}

Get project information and statistics.

Stability: Experimental

Configuration Parameters (GUCs)

LLM Provider Settings

ParameterTypeDefaultDescription
neurondb.llm_providerstring'openai'LLM provider (openai, cohere, huggingface)
neurondb.llm_api_keystringNULLAPI key for LLM provider
neurondb.llm_endpointstringNULLCustom API endpoint URL
neurondb.llm_timeout_msint30000API call timeout (milliseconds)
neurondb.llm_max_retriesint3Max retry attempts for failed API calls

GPU Settings

ParameterTypeDefaultDescription
neurondb.compute_modeboolfalseEnable GPU acceleration
neurondb.gpu_deviceint0GPU device ID to use
neurondb.gpu_batch_sizeint1000Batch size for GPU operations
neurondb.gpu_streamsint4Number of CUDA streams
neurondb.gpu_memory_pool_mbint512GPU memory pool size (MB)
neurondb.gpu_fail_openbooltrueFallback to CPU on GPU error
neurondb.gpu_kernelsstring'auto'GPU kernel selection (auto, cuda, rocm)
neurondb.gpu_backendstring'cuda'GPU backend (cuda, rocm)
neurondb.gpu_timeout_msint5000GPU operation timeout (ms)

Background Worker Settings

ParameterTypeDefaultDescription
neurondb.enable_background_workersboolfalseEnable background embedding workers
neurondb.worker_batch_sizeint100Batch size for worker tasks
neurondb.worker_interval_secint60Worker polling interval (seconds)
neurondb.worker_max_concurrentint4Max concurrent worker tasks

Monitoring and Metrics

ParameterTypeDefaultDescription
neurondb.enable_metricsbooltrueEnable performance metrics collection
neurondb.metrics_retention_daysint7Metric retention period
neurondb.log_levelstring'info'Logging level (debug, info, warning, error)

Index Types and Operator Classes

Index Types

  • hnsw - Hierarchical Navigable Small World graph index
  • ivfflat - Inverted File with Flat quantization

Operator Classes

  • vector_l2_ops - L2 (Euclidean) distance operations
  • vector_cosine_ops - Cosine distance operations
  • vector_ip_ops - Inner product operations

API Contract and Policies

NeuronDB follows strict API contract policies to ensure stability and predictability:

All functions in this reference are classified according to their stability level. Functions marked as stable are safe for production use and maintain backward compatibility.

Next Steps