Skip to content

Instantly share code, notes, and snippets.

@mkstayalive
mkstayalive / fn_ksuid.sql
Last active October 26, 2022 10:01 — forked from fabiolimace/ksuid.sql
Function for generating Segment's KSUIDs on PostgreSQL
/**
* Returns a Segment's KSUID.
*
* Reference implementation: https://github.com/segmentio/ksuid
* Also read: https://segment.com/blog/a-brief-history-of-the-uuid/
* Taken suggestions from: https://stackoverflow.com/a/71137273/518493
*/
CREATE EXTENSION pgcrypto;
CREATE OR REPLACE FUNCTION fn_ksuid(prefix text DEFAULT '') RETURNS text AS $$