Skip to content

DuckDB Extension

TSID

Generate time-sorted identifiers in DuckDB SQL. Create IDs for rows and recover their embedded timestamps with the TSID extension.

58,908
extension loads Β· last 90 days
On this page
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';

Generate identifiers with an embedded timestamp

TSID creates identifiers whose leading portion represents their generation time. This gives IDs a chronological ordering and lets you recover a timestamp from the identifier later.

Use it for new rows when a timestamp-bearing ID is useful to your consumers. Keep an explicit event-time column when generation time and event time differ. If you need an exact insertion order across concurrent writers, record a separate sequence.

Example

Generate IDs for three rows

Each call generates a new identifier. The query sorts the output explicitly.

Tested with DuckDB 1.5.6. Browser examples use Haybarn-Wasm 2.0.0-rc12.

SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
  WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL tsid FROM query_farm;
LOAD tsid FROM query_farm;

SELECT tsid() AS id
FROM range(3)
ORDER BY id;

Your IDs will differ on each run. Sorting IDs does not guarantee insertion order across threads or machines; keep a separate sequence when that order matters.

Common tasks

Deep Dive

Technical Details

Reference

Extension Contents

Quick reference to all available functions and settings organized by category.

Generation
tsid() Object type: Scalar function Generate a new Time-Sorted Unique Identifier as a 32-character lowercase hex string.
tsid_uuid() Object type: Scalar function 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.
Inspection
tsid_to_timestamp() Object type: Scalar function Extract the embedded timestamp from a TSID.

API Reference

Function Reference

Practical Examples

Cookbook

Real-world recipes and patterns for common use cases.

Platform Support

Compatibility

Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.

Quick Facts

Release status Stable
Software License MIT
Pricing Free
Written In C++
Source Available Yes
View on GitHub
Usage
58,908
loads Β· last 90 days

Platforms

DuckDB 2.0.0

  • Linux x86_64aarch64
  • Linux (musl) Not available
  • macOS Apple Silicon
  • Windows x86_64x86_64 (MinGW)
  • WASM Not available
Compiled binary sizes
Platform Architecture Size
Linux x86_64 3.95 MB
Linux aarch64 3.32 MB
macOS Apple Silicon 2.80 MB
Windows x86_64 10.19 MB
Windows x86_64 (MinGW) 17.80 MB

Compressed download sizes for the release shown above.

DuckDB & Haybarn

Release calendar
  • DuckDB v2.0.0 Query.Farm repository Supported
  • Haybarn v1.5.6 Built-in community repository Supported

Browser examples use Haybarn-Wasm 2.0.0-rc12.

Checked .

Learn and explore

Tutorials and articles

Writing about TSID: how it works, what changed, and why.