Generation functions in the TSID DuckDB extension
Sample data for the examples
Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.
-- Fixtures for tsid cookbook / functions / technical-details examples.
-- Idempotent: uses CREATE OR REPLACE so re-running is safe.
--
-- tsid() is non-deterministic, so we seed the events table with literal
-- 32-char hex strings (the TSID rendering) rather than calling tsid().
-- The values are time-ordered by ordinary string compare, matching the
-- property the cookbook examples demonstrate.
CREATE OR REPLACE TABLE events (
id VARCHAR PRIMARY KEY,
payload JSON
);
INSERT INTO events(id, payload) VALUES
('675716e86985495e9cf575f0b9c4a8db', '{"type":"login"}'),
('675716e96985495e9cf575f0b9c4a8dc', '{"type":"click"}'),
('675716ea6985495e9cf575f0b9c4a8dd', '{"type":"logout"}');
-- Bind variable referenced as :last_id_from_page_1 in the pagination recipe.
SET VARIABLE last_id_from_page_1 = '675716ea6985495e9cf575f0b9c4a8dd';
Function category
Generation
2 functionsGenerate a timestamp-prefixed TSID with tsid() (VARCHAR) or, in the DuckDB 2.0 build, tsid_uuid() (native UUID); both accept an optional TIMESTAMP. Use ORDER BY for sorted results; generation does not guarantee strict insertion or commit order.
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
ts
|
Type
TIMESTAMP
|
Mode Positional | Description |
Description
Generate a new Time-Sorted Unique Identifier as a 32-character lowercase hex string. The first 64 bits hold the generation time (seconds and nanoseconds since the Unix epoch), so ordinary string comparison sorts by generation time; the remaining 62 bits are random and the layout carries UUID version 4 and variant bits. Concurrent calls and clock changes do not establish strict insertion order.
DuckDB 2.0 build: tsid(ts) takes a TIMESTAMP (1970-01-01 to 2106-02-07) and embeds that time instead of the current one — useful for backfilling or for building ID range bounds; out-of-range or infinite timestamps raise an error and NULL returns NULL. The random tail comes from a per-thread generator seeded from the operating system (std::random_device) instead of one clock-seeded generator shared by all threads.
SELECT tsid();
SELECT tsid() AS id FROM range(100);
SELECT tsid_to_timestamp(tsid(TIMESTAMP '2024-06-01 12:00:00.123456')) AS embedded;
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
ts
|
Type
TIMESTAMP
|
Mode Positional | Description |
Description
DuckDB 2.0 build only. Generate a TSID as a native UUID value: the same bits, layout and time ordering as tsid(), stored in 16 bytes instead of a 32-character string. UUID comparison follows the byte order, so ORDER BY sorts by generation time. tsid_uuid(ts) embeds a given TIMESTAMP (1970-01-01 to 2106-02-07). Decode with tsid_to_timestamp. The value is version-4-shaped, so DuckDB's uuid_extract_timestamp (which reads UUIDv7) does not apply.
CREATE OR REPLACE TABLE orders (
id UUID DEFAULT tsid_uuid() PRIMARY KEY,
amount DECIMAL(10, 2)
);
INSERT INTO orders(amount) VALUES (9.99), (24.50);
SELECT count(*) AS orders FROM orders;
SELECT tsid_to_timestamp(tsid_uuid(TIMESTAMP '2030-05-05 05:05:05')) AS embedded;