Skip to content

Vector INSERT Examples - Quick Reference

Vector INSERT Examples - Quick Reference

All Working Syntax Forms

1. Explicit CAST (Your Original Query)

INSERT INTO docs VALUES (1, 'AI', '[0.9, 0.1, 0.0]'::VECTOR(3));

Status: WORKS NOW

INSERT INTO docs VALUES (1, 'AI', '[0.8, 0.2, 0.0]');

Status: WORKS - Automatically detects VECTOR column

3. With Explicit Column Names

INSERT INTO docs (id, content, embedding)
VALUES (1, 'AI', '[0.9, 0.1, 0.0]'::VECTOR(3));

Status: WORKS

4. Partial Column List

INSERT INTO docs (id, embedding)
VALUES (1, '[0.9, 0.1, 0.0]');

Status: WORKS - Auto-detects vector

5. Multiple Rows

INSERT INTO docs VALUES
(1, 'AI', '[0.9, 0.1, 0.0]'),
(2, 'ML', '[0.8, 0.2, 0.0]'),
(3, 'DL', '[0.7, 0.3, 0.0]');

Status: WORKS - All auto-detected

Vector Literal Formats

All these formats work for vector values:

-- With brackets
'[0.9, 0.1, 0.0]'
-- Explicit CAST
'[0.9, 0.1, 0.0]'::VECTOR(3)
-- Different precision
'[0.9234, 0.1567, 0.0123]'
-- Scientific notation
'[9e-1, 1e-1, 0.0]'

Full Example Session

-- Create table
CREATE TABLE docs (
id INT,
content TEXT,
embedding VECTOR(3)
);
-- Insert using CAST
INSERT INTO docs VALUES (1, 'AI', '[0.9, 0.1, 0.0]'::VECTOR(3));
-- Insert using auto-detection (easier!)
INSERT INTO docs VALUES (2, 'ML', '[0.8, 0.2, 0.0]');
-- Query
SELECT id, content FROM docs ORDER BY id;
-- Vector similarity search
SELECT id, content, embedding <-> '[1.0, 0.0, 0.0]' AS distance
FROM docs
ORDER BY distance
LIMIT 5;

Testing Your Setup

Run this to verify everything works:

Terminal window
cd /home/claude/HeliosDB
cargo test -p heliosdb-lite --test vector_insert_cast_test

Expected output:

test test_insert_vector_with_cast ... ok
test result: ok. 1 passed

Next Steps

  1. Create table with VECTOR column
  2. INSERT vectors (with or without CAST)
  3. Query your data
  4. Create HNSW index for fast similarity search
  5. Run vector similarity queries

Happy testing!