RUID - Readable and Usable Identifier

UUID is a fine data type that helps with various use-cases.

RUID is based on UUID and internally equal, meaning casts have no function call overhead. RUID is a different way representing UUID.

Reason

  • UUID representation in hex takes too much space.
  • Hex does not work as identifier depending on context.

Solution

  • Encode to base64 for fewer characters.
  • Pad UUID before encoding and cut off again afterwards to make use of the padding in a way it guarantees a range of start and end characters.

Encoding

Base64 is a relatively compact encoding in a range of readable characters, and with the two special characters additionally adjusted it becomes even more usable in various contexts, like file systems, copy-and-paste, URL, XML, CSS, and others.

Padding

Through the padding bytes the start and end characters of the resulting identifiers are in a range of 16 different alpha, respectively alpha-numeric characters:

  • Start: A B C D E F G H I J K L M N O P
  • End: 0 4 8 A E I M Q U Y c g k o s w

Example

Comparison of a single UUID vs. RUID:

5b0ba39c-6597-11e2-9d36-001b211595d1 (36)

vs.

FsLo5xllxHinTYAGyEVldE (22)

This accumulates very fast and leads to a lot of terminal space taken. For example the output of a table with just three columns with UUIDs compared to the same output with RUIDs:

Before

SELECT uuid_generate_v1(), uuid_generate_v1(), uuid_generate_v1();
           uuid_generate_v1           |           uuid_generate_v1           |           uuid_generate_v1           
--------------------------------------+--------------------------------------+--------------------------------------
 5b0ba39c-6597-11e2-9d36-001b211595d1 | 5b0ba5c2-6597-11e2-a017-001b211595d1 | 5b0ba766-6597-11e2-b5df-001b211595d1
(1 row)

After

SELECT uuid_generate_v1()::ruid, uuid_generate_v1()::ruid, uuid_generate_v1()::ruid;
    uuid_generate_v1    |    uuid_generate_v1    |    uuid_generate_v1    
------------------------+------------------------+------------------------
 HnDG6hllxHitpAAGyEVldE | HnDHeJllxHisUEAGyEVldE | HnDH5BllxHioSEAGyEVldE
(1 row)

Helper Functions

Since RUID is about usability too, it provides a few helper functions to make life with identifiers easier:

  • ruid_nil()
  • ruid_ns_dns()
  • ruid_ns_oid()
  • ruid_ns_url()
  • ruid_ns_x500()
  • ruid_v1()
  • ruid_v1mc()
  • ruid_v3(ruid, text)
  • ruid_v4()
  • ruid_v5(ruid, text)
  • ruid_dns(text): same as ruid_v5(ruid_ns_url(), trim(text))
  • ruid_oid(text): same as ruid_v5(ruid_ns_oid(), trim(text))
  • ruid_url(text): same as ruid_v5(ruid_ns_url(), trim(text))
  • ruid_x500(text): same as ruid_v5(ruid_ns_x500(), trim(text))
  • ruid_sum(text): same as ruid_v5(ruid_nil(), text)

Future Plans

  • Getting rid of uuid-ossp dependency.
    • Implement UUID version 1 using PostgreSQLs internal information.
    • UUID version 3 (MD5), 4 (random) and 5 (SHA1) are easy to implement.
  • Integrate into core using a type modifier for UUID or as separate type.
  • Benchmarks to get an idea of possible performance impact.
  • Measurement of size/bandwith savings.

Copyright and License

Copyright (c) 2010-2013 Simon Bertrang janus@errornet.de

Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.