Skip to main content

Extensions

Turso provides several built-in extensions that add specialized SQL functions and virtual tables. Extensions can be loaded at runtime using the load_extension() function.

Loading Extensions

SELECT load_extension('extension_name');
Turso supports loading Turso-native extensions. SQLite .so/.dll loadable extensions are not supported.

UUID Extension

The UUID extension provides functions for generating and working with UUIDs. UUIDs are stored as 16-byte BLOBs by default.

Functions

FunctionParametersReturn TypeDescription
uuid4()noneBLOBGenerate a random UUID v4
uuid4_str()noneTEXTGenerate a random UUID v4 as string. Alias: gen_random_uuid()
uuid7()noneBLOBGenerate a time-ordered UUID v7
uuid7(seconds)INTEGERBLOBGenerate a UUID v7 with specified seconds since epoch
uuid7_timestamp_ms(uuid)BLOBINTEGERExtract milliseconds since epoch from a UUID v7
uuid_str(uuid)BLOBTEXTConvert a UUID blob to string representation
uuid_blob(uuid)TEXTBLOBConvert a UUID string to blob representation

Examples

-- Generate UUIDs
SELECT uuid4_str();
-- '550e8400-e29b-41d4-a716-446655440000'

SELECT uuid_str(uuid4());
-- 'f47ac10b-58cc-4372-a567-0e02b2c3d479'

-- UUID v7 is time-ordered (good for primary keys)
SELECT uuid_str(uuid7());
-- '0190a5c0-1234-7abc-8def-0123456789ab'

-- Extract timestamp from UUID v7
SELECT uuid7_timestamp_ms(uuid7());
-- 1720000000000

-- Use in a table
CREATE TABLE documents (
    id BLOB PRIMARY KEY DEFAULT (uuid7()),
    title TEXT
);
INSERT INTO documents (title) VALUES ('My Document');
SELECT uuid_str(id), title FROM documents;

Regexp Extension

The regexp extension provides regular expression functions compatible with sqlean-regexp.

Functions

FunctionParametersReturn TypeDescription
regexp(pattern, source)TEXT, TEXTINTEGERReturns 1 if source matches pattern
regexp_like(source, pattern)TEXT, TEXTINTEGERReturns 1 if source matches pattern (argument order reversed)
regexp_substr(source, pattern)TEXT, TEXTTEXTReturns the first substring matching pattern, or NULL
regexp_capture(source, pattern)TEXT, TEXTTEXTReturns the first capture group match
regexp_capture(source, pattern, n)TEXT, TEXT, INTEGERTEXTReturns the n-th capture group match
regexp_replace(source, pattern, replacement)TEXT, TEXT, TEXTTEXTReplace matches with replacement string

Examples

-- Pattern matching
SELECT regexp('[0-9]+', 'abc123def');
-- 1

-- The REGEXP operator uses this extension
SELECT 'hello123' REGEXP '[a-z]+[0-9]+';
-- 1

-- Extract matching substring
SELECT regexp_substr('Price: $42.50', '[0-9]+\.[0-9]+');
-- '42.50'

-- Capture groups
SELECT regexp_capture('2025-03-15', '(\d{4})-(\d{2})-(\d{2})', 1);
-- '2025'

-- Replace
SELECT regexp_replace('Hello World', '[aeiou]', '*');
-- 'H*ll* W*rld'

Vector Extension

Turso Extension: Native vector search for similarity search and semantic search applications.
The vector extension provides functions for creating, storing, and searching vector embeddings. Vectors are stored as BLOBs. For the complete function reference, see Vector Functions.

Quick Reference

FunctionDescription
vector32(json)Create a 32-bit float vector
vector64(json)Create a 64-bit float vector
vector_distance_cos(v1, v2)Cosine distance
vector_distance_l2(v1, v2)Euclidean distance
vector_extract(v)Convert vector to JSON
vector_concat(v1, v2)Concatenate vectors
vector_slice(v, start, end)Extract vector slice

Example

CREATE TABLE docs (
    id INTEGER PRIMARY KEY,
    content TEXT,
    embedding BLOB
);

INSERT INTO docs VALUES (1, 'database systems', vector32('[0.1, 0.2, 0.3]'));
INSERT INTO docs VALUES (2, 'machine learning', vector32('[0.4, 0.5, 0.6]'));

-- Find similar documents
SELECT content, vector_distance_cos(embedding, vector32('[0.1, 0.25, 0.35]')) AS dist
FROM docs
ORDER BY dist
LIMIT 5;

Time Extension

The time extension is compatible with sqlean-time and provides advanced time manipulation functions.

Key Functions

FunctionParametersReturn TypeDescription
time_now()noneINTEGERCurrent time as Unix nanoseconds
time_date(y,m,d,...)INTEGER…INTEGERCreate time from components
time_unix(sec)INTEGERINTEGERCreate time from Unix seconds
time_milli(ms)INTEGERINTEGERCreate time from milliseconds
time_micro(us)INTEGERINTEGERCreate time from microseconds
time_nano(ns)INTEGERINTEGERCreate time from nanoseconds
time_to_unix(t)INTEGERINTEGERConvert to Unix seconds
time_to_milli(t)INTEGERINTEGERConvert to milliseconds
time_to_micro(t)INTEGERINTEGERConvert to microseconds
time_to_nano(t)INTEGERINTEGERConvert to nanoseconds
time_get_year(t)INTEGERINTEGERExtract year
time_get_month(t)INTEGERINTEGERExtract month (1-12)
time_get_day(t)INTEGERINTEGERExtract day (1-31)
time_get_hour(t)INTEGERINTEGERExtract hour (0-23)
time_get_minute(t)INTEGERINTEGERExtract minute (0-59)
time_get_second(t)INTEGERINTEGERExtract second (0-59)
time_get_weekday(t)INTEGERINTEGERDay of week (0=Sunday)
time_get(t, field)INTEGER, TEXTINTEGERExtract named field
time_add(t, d)INTEGER, INTEGERINTEGERAdd duration
time_add_date(t, y, m, d)INTEGER…INTEGERAdd years/months/days
time_sub(t, u)INTEGER, INTEGERINTEGERDifference between times
time_since(t)INTEGERINTEGERDuration since time
time_until(t)INTEGERINTEGERDuration until time
time_trunc(t, field)INTEGER, TEXTINTEGERTruncate to field
time_after(t, u)INTEGER, INTEGERINTEGER1 if t is after u
time_before(t, u)INTEGER, INTEGERINTEGER1 if t is before u
time_compare(t, u)INTEGER, INTEGERINTEGER-1, 0, or 1
time_equal(t, u)INTEGER, INTEGERINTEGER1 if equal
time_fmt_iso(t)INTEGERTEXTFormat as ISO 8601
time_fmt_datetime(t)INTEGERTEXTFormat as datetime
time_fmt_date(t)INTEGERTEXTFormat as date
time_fmt_time(t)INTEGERTEXTFormat as time
time_parse(s)TEXTINTEGERParse time string

Duration Constants

FunctionValue
dur_ns()1 nanosecond
dur_us()1 microsecond
dur_ms()1 millisecond
dur_s()1 second
dur_m()1 minute
dur_h()1 hour

Example

-- Current time formatted
SELECT time_fmt_iso(time_now());
-- '2025-03-15T10:30:00Z'

-- Date arithmetic
SELECT time_fmt_date(time_add_date(time_now(), 0, 1, 0));
-- add 1 month to today

-- Duration since a timestamp
SELECT time_since(time_date(2025, 1, 1, 0, 0, 0)) / dur_h();
-- hours since Jan 1, 2025

Full-Text Search (FTS)

Turso Extension: Full-text search powered by Tantivy, replacing SQLite’s FTS3/FTS4/FTS5.
Turso provides full-text search through the FTS index method. For the complete reference including query syntax, tokenizers, and scoring, see FTS Functions.

Quick Reference

-- Create an FTS index
CREATE INDEX idx ON articles USING fts (title, body);

-- Search
SELECT title, fts_score('idx') AS score
FROM articles
WHERE fts_match('idx', 'database search')
ORDER BY score;

-- Highlighted results
SELECT fts_highlight('idx', 0, '<b>', '</b>') AS title
FROM articles
WHERE fts_match('idx', 'database');

CSV Extension

The CSV extension provides a virtual table for reading CSV files.

Usage

CREATE VIRTUAL TABLE temp.csv_data USING csv(
    filename='/path/to/data.csv',
    header=yes
);

SELECT * FROM csv_data;
ParameterDescription
filenamePath to the CSV file
headeryes if the first row contains column names

Percentile Extension

Statistical aggregate functions for computing percentiles.

Functions

FunctionParametersReturn TypeDescription
median(X)columnREALMedian value (50th percentile)
percentile(Y, P)column, REALREALP-th percentile of Y (P between 0 and 100)
percentile_cont(Y, P)column, REALREALContinuous percentile (interpolated)
percentile_disc(Y, P)column, REALREALDiscrete percentile (nearest value)

Examples

CREATE TABLE scores (value REAL);
INSERT INTO scores VALUES (10), (20), (30), (40), (50);

SELECT median(value) FROM scores;
-- 30.0

SELECT percentile(value, 75) FROM scores;
-- 40.0

SELECT percentile_cont(value, 0.25) FROM scores;
-- 20.0

generate_series

The generate_series table-valued function generates a sequence of integers.
SELECT value FROM generate_series(start, stop);
SELECT value FROM generate_series(start, stop, step);
ParameterTypeDescription
startINTEGERStarting value (inclusive)
stopINTEGEREnding value (inclusive)
stepINTEGERStep increment (default: 1)
SELECT value FROM generate_series(1, 5);
-- 1, 2, 3, 4, 5

SELECT value FROM generate_series(0, 10, 2);
-- 0, 2, 4, 6, 8, 10

SELECT value FROM generate_series(10, 1, -3);
-- 10, 7, 4, 1

See Also