PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
The UUID
data type in PostgreSQL represents universally unique identifiers, which are 128-bit quantities that are usually represented by 32 hexadecimal digits displayed in five groups separated by hyphens. The standard format for a UUID is 8-4-4-4-12
(eight, four, four, four, then twelve hexadecimal digits). UUIDs are commonly used for various purposes due to their high likelihood of uniqueness across space and time.
Usage: The UUID type is commonly used for distributed databases because it ensures a high chance that the value is unique in the universe, making it especially suitable for primary keys.
Generation: PostgreSQL supports several functions to generate UUIDs, the most popular being from the pgcrypto
module.
Storage Size: A UUID value uses 16 bytes of storage.
To generate UUIDs in PostgreSQL, you can use the pgcrypto
extension.
First, you need to install the pgcrypto
extension:
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
After installing the pgcrypto
extension, you can use the gen_random_uuid()
function to generate a random UUID:
SELECT gen_random_uuid();
Here's how you can create a table with a UUID column and insert a UUID value into it:
CREATE TABLE my_table ( id UUID PRIMARY KEY, name TEXT ); INSERT INTO my_table (id, name) VALUES (gen_random_uuid(), 'John Doe');
Although UUIDs are unique, they are not usually sequential. This can have implications for the performance of database operations, especially for large datasets. The randomness of UUIDs can cause fragmentation in the table, making operations like INSERT
slower over time. Always test and ensure the performance meets your needs if you plan to use UUIDs as primary keys in large tables.
UUIDs are longer than traditional serial IDs, so they consume more space in the database and can make indexes and data storage slightly larger.
There are different versions of UUIDs (from version 1 to version 5), each having a specific use case. The pgcrypto
function generates a version 4 UUID, which is based on random numbers.
In conclusion, the UUID data type in PostgreSQL is valuable for ensuring global uniqueness, especially in distributed systems or scenarios where multiple independent parties might insert data into a database. However, they come with trade-offs, mainly concerning storage size and non-sequential nature, which can affect performance in some situations.
PostgreSQL UUID data type example: Create a table with a UUID column:
CREATE TABLE example_table ( id UUID PRIMARY KEY, name TEXT );
How to use UUID data type in PostgreSQL: Use the UUID data type in a table:
CREATE TABLE person ( id UUID PRIMARY KEY, name TEXT );
Generate UUID in PostgreSQL: Generate a UUID and insert it into a table:
INSERT INTO example_table (id, name) VALUES (UUID_GENERATE_V4(), 'John Doe');
PostgreSQL UUID vs SERIAL: Compare UUID and SERIAL as primary key types:
CREATE TABLE serial_example ( id SERIAL PRIMARY KEY, name TEXT ); CREATE TABLE uuid_example ( id UUID PRIMARY KEY, name TEXT );
Indexing UUID columns in PostgreSQL: Create an index on a UUID column for faster queries:
CREATE INDEX idx_uuid_column ON example_table (id);
PostgreSQL UUID default value: Set a default UUID value for a column:
CREATE TABLE default_uuid_example ( id UUID DEFAULT UUID_GENERATE_V4(), name TEXT );
Convert string to UUID in PostgreSQL: Convert a string to a UUID:
SELECT '550e8400-e29b-41d4-a716-446655440000'::UUID;
Working with UUID arrays in PostgreSQL: Use arrays of UUIDs in PostgreSQL:
CREATE TABLE uuid_array_example ( id UUID[], name TEXT );
PostgreSQL create table with UUID column: Create a table with a UUID column:
CREATE TABLE uuid_table ( id UUID PRIMARY KEY, description TEXT );