Sling Academy
Home/PostgreSQL/How to generate and use UUIDs in PostgreSQL

How to generate and use UUIDs in PostgreSQL

Last updated: January 04, 2024

Introduction

A universally unique identifier (UUID) is a 128-bit number used to uniquely identify information in computer systems. UUIDs are particularly useful in databases like PostgreSQL for ensuring that each row can be uniquely identified, without domain-specific constraints. This tutorial will guide you through the steps to generate and use UUIDs in your PostgreSQL databases.

Understanding UUIDs

Before diving into implementation, it’s crucial to understand what UUIDs are and why they are beneficial. UUIDs can be generated independently from one another and are almost guaranteed to be unique. They are helpful in distributed systems where generating unique IDs across different machines without communication is required. PostgreSQL has built-in support for UUIDs, making it relatively straightforward to implement them.

Installing the UUID Extension

In PostgreSQL, UUIDs are not enabled by default. To use them, you must enable the uuid-ossp extension. Here is how to install it:

-- Enable the extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

Ensure that you have the proper privileges to create extensions before running the command. If created successfully, you now have access to several functions that can generate UUIDs.

Generating UUIDs

To generate a UUID in PostgreSQL, you can use one of the functions provided by the uuid-ossp extension. The most common functions are uuid_generate_v1() and uuid_generate_v4(). Version 1 UUIDs are time-based and version 4 UUIDs are randomly generated. Here are examples of how to use them:

-- Generate a version 1 UUID
SELECT uuid_generate_v1();

-- Generate a version 4 UUID
SELECT uuid_generate_v4();

When you call these functions, PostgreSQL will return a new UUID each time.

Creating a Table with UUID Primary Key

Integrating UUIDs into your tables is straightforward. Here is an example of how to create a table with a UUID primary key:

-- Create a table with a UUID primary key using the 'uuid' type
CREATE TABLE my_table (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    data TEXT
);

The id column is set to the uuid data type, and by default, each new row will be assigned a version 4 (random) UUID if no specific value is provided.

Indexing UUIDs

Working with UUIDs also means considering how to index them. UUIDs can be indexed as other data types. Here’s how to create an index on the UUID column:

-- Create an index on the UUID column
CREATE INDEX idx_my_table_id ON my_table (id);

An index will improve the lookup performance on UUID columns, especially as the table grows in size.

Querying with UUIDs

To query data using UUIDs, you can treat them like any other data type in your SELECT statements:

-- Select a row by its UUID
SELECT * FROM my_table WHERE id = '123e4567-e89b-12d3-a456-426655440000';

Remember to use the correct UUID string or the query will not return the expected results.

Best Practices for UUIDs

Despite their benefits, there are some best practices to consider before implementing UUIDs:

  • UUID generation, especially time-based versions, can be slower than traditional sequential IDs.
  • UUIDs are larger (16 bytes) than integer-based keys (typically 4 bytes), which can result in increased storage requirements.
  • Use uniformly random UUIDs (version 4) to avoid the bloat and fragmentation that can occur with time-based versions in high-insert scenarios.
  • Because UUIDs are effectively random, indexes on UUID fields can become less efficient than those on sequentially incremented keys.

Advanced Usage: Custom Functions and Generation Modes

For advanced scenarios, you might want to create a custom function for UUID generation which involves different generation modes or integrating with an Application Level UUID generation strategy. This offers a higher level of control and can cater to specific application needs. Moreover, you can use `uuid_generate_v5(namespace UUID, name TEXT)` for name-based UUIDs that are generated from a namespace and a name.

-- Create a name-based UUID for 'my_name' in the given namespace
SELECT uuid_generate_v5(uuid_ns_url(), 'my_name');

Understanding the implications of each generation strategy and aligning them with your application’s needs is fundamental in an advanced usage scenario.

Conclusion

In summary, UUIDs provide a great way to ensure uniqueness across tables and databases. PostgreSQL’s support for UUIDs through the uuid-ossp extension adds a powerful tool to the PostgreSQL developer’s toolbox. As with any technology decision, it’s important to weigh the pros and cons of UUIDs relative to your application’s requirements. By following the practices outlined in this tutorial, you should feel equipped to implement UUIDs in your own PostgreSQL environment.

Next Article: Array data type in PostgreSQL: Store multiple values in a single column

Previous Article: TIMESTAMP and TIMESTAMPTZ data types in PostgreSQL (7 examples)

Series: PostgreSQL Tutorials: From Basic to Advanced

PostgreSQL

You May Also Like

  • PostgreSQL with TimescaleDB: Querying Time-Series Data with SQL
  • PostgreSQL Full-Text Search with Boolean Operators
  • Filtering Stop Words in PostgreSQL Full-Text Search
  • PostgreSQL command-line cheat sheet
  • How to Perform Efficient Rolling Aggregations with TimescaleDB
  • PostgreSQL with TimescaleDB: Migrating from Traditional Relational Models
  • Best Practices for Maintaining PostgreSQL and TimescaleDB Databases
  • PostgreSQL with TimescaleDB: Building a High-Performance Analytics Engine
  • Integrating PostgreSQL and TimescaleDB with Machine Learning Models
  • PostgreSQL with TimescaleDB: Implementing Temporal Data Analysis
  • Combining PostgreSQL, TimescaleDB, and Airflow for Data Workflows
  • PostgreSQL with TimescaleDB: Visualizing Real-Time Data with Superset
  • Using PostgreSQL with TimescaleDB for Energy Consumption Analysis
  • PostgreSQL with TimescaleDB: How to Query Massive Datasets Efficiently
  • Best Practices for Writing Time-Series Queries in PostgreSQL with TimescaleDB
  • PostgreSQL with TimescaleDB: Implementing Batch Data Processing
  • Using PostgreSQL with TimescaleDB for Network Traffic Analysis
  • PostgreSQL with TimescaleDB: Troubleshooting Common Performance Issues
  • Building an IoT Data Pipeline with PostgreSQL and TimescaleDB