Skip to content

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 functions

tsid

Scalar function Generation
Signature
2 overloaded forms · click to inspect
Description
1 One-off ID
SELECT tsid();
2 Bulk generation
SELECT tsid() AS id FROM range(100);
3 DuckDB 2.0 build: an ID for a given time — the embedded timestamp round-trips
SELECT tsid_to_timestamp(tsid(TIMESTAMP '2024-06-01 12:00:00.123456')) AS embedded;

tsid_uuid

Scalar function Generation
Signature
2 overloaded forms · click to inspect
Description
1 Native UUID primary key
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;
2 ID for a given time
SELECT tsid_to_timestamp(tsid_uuid(TIMESTAMP '2030-05-05 05:05:05')) AS embedded;