3 comments

[ 3.1 ms ] story [ 20.3 ms ] thread
Nice, I've done this before - even using a prefix byte to indicate the key type. uuids are ugly and the '-' makes them hard to cut and paste imho.

I never did the deduplication query thing though - using 11 base58 digits seemed good enough for that app.

TBF there's only a 50% chance that the deduplication loop will run (once) after 5.1 x 10^9 keys. It's still better than erroring in that case IMO.

If that's not needed, the loop and subquery and be removed. This then enables the usage of shortkey_generate() as a DEFAULT expression instead of a trigger. Maybe I should provide that as an alternative, something like shortkey_generate_unsafe().

Alternatively, perhaps one could handle the duplicate key error on an insert into the table using one of these keys as the PK.