geckoboard/pgulid

Adding a ULID type for easier use

Opened this issue · 3 comments

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.

@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.

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.

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.