Convert UUID to IDs in PostgreSQL – Universal Method

performancepostgresqluuid

what are the best practices in Postgres for globally converting UUIDs into IDs. I'm currently using both UUIDs and IDs in my schema. The intent is for UUIDs to be exposed to the public (for URLs et.) and IDs for internal systems.

I've looked at the following methods but am unsure of the best practices.

  1. a custom function that does a UUID/ID lookup on the target table
  2. using a BEFORE trigger that does the conversion and keeps all postgres functions clean

Can someone provide some wisdom/suggestions here?

Thanks!

Best Answer

For exposing it to public, we kept ID field. Its user friendly to see an ID vs UUID in URL. We replaced ID field from primary key and introduced UUID primary key. ID and UUID both exists for different reasons. For searchabilty, showing in URL, we still continue showing ID field.

This suggests something similar: https://stackoverflow.com/questions/52414414/best-practices-on-primary-key-auto-increment-and-uuid-in-rdbms-and-sql-databas

Some additional reading: https://tomharrisonjr.com/uuid-or-guid-as-primary-keys-be-careful-7b2aa3dcb439