Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a ULID type for easier use #4

Open
Trupal00p opened this issue Jan 3, 2021 · 3 comments
Open

Adding a ULID type for easier use #4

Trupal00p opened this issue Jan 3, 2021 · 3 comments

Comments

@Trupal00p
Copy link

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
Copy link
Member

@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
Copy link

mulcek commented Feb 13, 2022

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants