Introducing pg_idkit: A Postgres extension for generating UUIDs

Categories
Postgres Logo

tl;dr - While guest writing a Supabase blog post on how identifiers work in Postgres, I was able to build pg_idkit which makes it easier to generate new UUIDs in Postgres

UUIDs are better supported in Postgres than in a lot of other databases. Postgres has the uuid datatype and you can easily generate UUIDs as primary keys with SQL like this:

CREATE TABLE example (
  uuid uuid PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY uuid_generate_v1mc(),
  col1 text NOT NULL,
  col2 integer NOT NULL,
  created_at timestamptz NOT NULL DEFAULT NOW()
);

Postgres has built-in support for UUIDv1, UUIDv4, and somet other versions, but it turns out there’s a raft of new UUID types out there. These formats are easy to get to from code, but not so much from inside your Postgres database… So I wrote something to do that.

I won’t go into all the different kinds of UUID types so much (for that, read the Supabase post!), but I do want to touch on what made it possible:

Made possible by Rust and pgx

Thanks to pgx, it’s easy to write Postgres extensions almost effortlessly in Rust, which means it’s easy to have a fast, performant, and safe codebase.

It’s hard to understate how transformative I think pgx will be for the Postgres ecosystem. The developer experience that has been carefully built in to pgx, along with the solidness of the solution is astounding. I was up and running in almost no time, and finally Postgres’s ability to sustain innovation via it’s extension ecosystem is going to thrive even more.