Created
March 26, 2024 01:40
-
-
Save agaviria/84ce940564855fb2d053ec8d4c9cf542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- https://github.com/geckoboard/pgulid | |
-- pgulid is based on OK Log's Go implementation of the ULID spec | |
-- | |
-- https://github.com/oklog/ulid | |
-- https://github.com/ulid/spec | |
-- | |
-- Copyright 2016 The Oklog Authors | |
-- Licensed under the Apache License, Version 2.0 (the "License"); | |
-- you may not use this file except in compliance with the License. | |
-- You may obtain a copy of the License at | |
-- | |
-- http://www.apache.org/licenses/LICENSE-2.0 | |
-- | |
-- Unless required by applicable law or agreed to in writing, software | |
-- distributed under the License is distributed on an "AS IS" BASIS, | |
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
-- See the License for the specific language governing permissions and | |
-- limitations under the License. | |
create extension if not exists pgcrypto; | |
drop function if exists gen_random_ulid; | |
create function gen_random_ulid() returns text as $$ | |
declare | |
-- Crockford's Base32 | |
encoding BYTEA = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; | |
timestamp BYTEA = E'\\000\\000\\000\\000\\000\\000'; | |
output TEXT = ''; | |
unix_time bigint; | |
ulid bytea; | |
begin | |
-- 6 timestamp bytes | |
unix_time = (extract(epoch from now()) * 1000)::bigint; | |
timestamp = set_byte(timestamp, 0, (unix_time >> 40)::bit(8)::integer); | |
timestamp = set_byte(timestamp, 1, (unix_time >> 32)::bit(8)::integer); | |
timestamp = set_byte(timestamp, 2, (unix_time >> 24)::bit(8)::integer); | |
timestamp = set_byte(timestamp, 3, (unix_time >> 16)::bit(8)::integer); | |
timestamp = set_byte(timestamp, 4, (unix_time >> 8)::bit(8)::integer); | |
timestamp = set_byte(timestamp, 5, unix_time::bit(8)::integer); | |
-- 10 entropy bytes | |
ulid = timestamp || gen_random_bytes(10); | |
-- encode the timestamp | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 0) & 224) >> 5)); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 0) & 31))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 1) & 248) >> 3)); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 1) & 7) << 2) | ((get_byte(ulid, 2) & 192) >> 6))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 2) & 62) >> 1)); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 2) & 1) << 4) | ((get_byte(ulid, 3) & 240) >> 4))); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 3) & 15) << 1) | ((get_byte(ulid, 4) & 128) >> 7))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 4) & 124) >> 2)); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 4) & 3) << 3) | ((get_byte(ulid, 5) & 224) >> 5))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 5) & 31))); | |
-- encode the entropy | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 6) & 248) >> 3)); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 6) & 7) << 2) | ((get_byte(ulid, 7) & 192) >> 6))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 7) & 62) >> 1)); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 7) & 1) << 4) | ((get_byte(ulid, 8) & 240) >> 4))); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 8) & 15) << 1) | ((get_byte(ulid, 9) & 128) >> 7))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 9) & 124) >> 2)); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 9) & 3) << 3) | ((get_byte(ulid, 10) & 224) >> 5))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 10) & 31))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 11) & 248) >> 3)); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 11) & 7) << 2) | ((get_byte(ulid, 12) & 192) >> 6))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 12) & 62) >> 1)); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 12) & 1) << 4) | ((get_byte(ulid, 13) & 240) >> 4))); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 13) & 15) << 1) | ((get_byte(ulid, 14) & 128) >> 7))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 14) & 124) >> 2)); | |
output = output || chr(get_byte(encoding, ((get_byte(ulid, 14) & 3) << 3) | ((get_byte(ulid, 15) & 224) >> 5))); | |
output = output || chr(get_byte(encoding, (get_byte(ulid, 15) & 31))); | |
return output; | |
end | |
$$ language plpgsql volatile; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment