geckoboard / pgulid

Universally Unique Lexicographically Sortable Identifier (ULID) for PostgreSQL
339 stars 20 forks source link

Adding a ULID type for easier use #4

Open Trupal00p opened 3 years ago

Trupal00p commented 3 years ago

Howdy thanks for creating this really useful function! To use this more easily, I ended up adding a new domain type for the text result of this function so it can be used more like the uuid type that its replacing. The one extra SQL statement I added:

create domain ulid as text 
default generate_ulid() 
not null 
constraint ulid_length_check check(char_length(value) = 26)
constraint ulid_upper_bound check(value<='7ZZZZZZZZZZZZZZZZZZZZZZZZZ');

this add a few standard checks for string length and sets the max value (per the ULID spec) to prevent invalid IDs from sneaking in. It also allows you to use the new ulid type in table creation statements in a clean way:

CREATE TABLE example_table
(
  id                        ulid primary key,
  ...other columns
);

Was gonna open a PR but since it is such a small change thought this might be easier to add to the discussion here.

leocassarani commented 3 years ago

@Trupal00p Thanks, that's helpful! Maybe you could make a pull request to add a note about this to the README? I don't think we should require every user of the generate_ulid() function to also import this new type.

Your ulid_upper_bound check may also run into problems if a user chooses to pass a custom prefix to the generate function.

mulcek commented 2 years ago

I would add as others pointed out, casting to text (32-bytes) basically doubles the size. Better to cast to uuid (16-bytes). Affects size of indexes.

pksunkara commented 1 year ago

I was using this extension before, but I really needed a proper ulid type (not a domain) and thus built my own at pgx_ulid.