Inspection 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
Inspection
1 functionExtract the embedded generation timestamp from a TSID string or (DuckDB 2.0 build) UUID. It need not equal insertion or transaction commit time.
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
tsid
|
Type
VARCHAR | UUID
2 concrete typesUUIDVARCHAR
|
Mode Positional | Description |
Description
Extract the embedded timestamp from a TSID. Returns TIMESTAMP (microsecond precision) — the time the ID was generated, or the time passed to tsid(ts). Accepts the 32-hex-digit form or the dashed 8-4-4-4-12 form, in either case. NULL input returns NULL; any other string raises a Conversion Error (Invalid TSID format) — wrap it in TRY() to get NULL instead.
DuckDB 2.0 build: also accepts a native UUID (from tsid_uuid() or a cast). Parsing is strict: a 36-character string must have its dashes at the 8-4-4-4-12 positions. Earlier builds could crash the process on some malformed 36-character strings and decoded an all-dash string as 1970-01-01.
SELECT id, tsid_to_timestamp(id) AS created_at FROM events;
SELECT tsid_to_timestamp('65d4a2b6000000004000800000000000') AS a,
tsid_to_timestamp('65d4a2b6-0000-0000-4000-800000000000') AS b;
SELECT tsid_to_timestamp('65d4a2b6-0000-0000-4000-800000000000'::UUID) AS created_at;
SELECT TRY(tsid_to_timestamp('not-a-tsid')) AS created_at;