PostgreSQL: Generating UUID primary keys
A universally unique identifier (UUID) is a 128-bit number that is generated in a way that makes it very unlikely that the same identifier will be generated by anyone else in the known universe (globally unique).
In PostgreSQL, you can generate UUIDs using the uuid_generate_v4
function from the uuid-ossp
extension.
What is UUID?
UUID stands for Universally Unique Identifier. It is a 128-bit identifier used to uniquely identify information in computer systems and applications. UUIDs are often represented as a string of 36 characters, typically in a format such as "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where each "x" represents a hexadecimal digit.
The purpose of UUIDs is to provide a way to generate identifiers that are highly unlikely to collide with other identifiers, even if they are created on different systems or at different times. This makes UUIDs particularly useful in scenarios where there is a need to uniquely identify objects, entities, or resources in distributed systems, databases, or other applications.
When to use UUIDs?
You can use UUIDs when you need to generate a globally unique indentifier without using an id generation service, for example, OpenTelemetry uses 16-bytes identifiers as a trace id.
Usually, UUIDs are generated by taking 16 random bytes and the uniqueness is based on the sheer quantity, not the generation algorithm. Such identifiers are proven to be unique, but are larger and slightly slower than 64-bit sequential numbers.
TIP
UUIDs are slightly slower than 64-bit sequential identifiers. Use them only when you don't have an easy way to generate smaller sequential identifiers.
UUID in PostgreSQL
PostgreSQL requires an extension to support UUID column type. The extenstion comes with postgresql-contrib-*
package:
sudo apt install postgresql-contrib-14
Then you need to install the extension in each database where you are going to use it:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
UUID in Go
For working with UUIDs in Go you need to install google/uuid package.
go get github.com/google/uuid
satori/go.uuid works too, but it does not look maintained any more.
Generating UUIDs
In PostgreSQL:
SELECT uuid_generate_v4();
In Go:
import "github.com/google/uuid"
fmt.Println(uuid.New())
Using UUIDs in models
You can use uuid.UUID
type in Bun models like this:
import "github.com/google/uuid"
type Story struct {
ID uuid.UUID `bun:"type:uuid,default:uuid_generate_v4()"`
Title string
AuthorID uuid.UUID `bun:"type:uuid"`
}
UUID
field name also works well:
type Story struct {
UUID uuid.UUID `bun:",pk,type:uuid,default:uuid_generate_v4()"`
Title string
AuthorUUID uuid.UUID `bun:"type:uuid"`
}
Monitoring performance
To monitor PostgreSQL, you can use OpenTelemetry PostgreSQL receiver that comes with OpenTelemetry Collector.
OpenTelemetry Collector is a valuable component for monitoring applications and infrastructure in distributed environments. It enables efficient data collection, processing, and export to improve observability, troubleshooting, and performance of software systems.
Uptrace is an open source APM for OpenTelemetry that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.
Uptrace comes with an intuitive query builder, rich dashboards, alerting rules, notifications, and integrations for most languages and frameworks.
Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.
In just a few minutes, you can try Uptrace by visiting the cloud demo (no login required) or running it locally with Docker. The source code is available on GitHub.