PostgreSQL Extension to generate various type of Universally Unique IDS
Rust
76
30 commits
updated Jun 9, 2024
postgres=# CREATE EXTENSION uids;
CREATE EXTENSION
postgres=# SELECT generate_typeid('user');
generate_typeid
--------------------------------------
user_01h2xcejqtf2nbrexx3vqjhp41
Using the uids-postgres extension for generating unique IDs in PostgreSQL offers several advantages:
The extension supports multiple methodologies for generating unique IDs, including UUID v6, UUID v7, NanoId, Ksuid, Ulid, Timeflake, PushId, and Cuid2. This flexibility allows you to choose the most suitable ID generation strategy for your specific use case, whether you need time-based IDs, lexicographically sortable IDs, or IDs with custom prefixes.
Using unique identifiers like UUIDs ensures data integrity and uniqueness across distributed systems. This is particularly important for applications that require federated data or need to avoid collisions in a multi-node environment.
Generating unique IDs within the database can be more efficient than handling this in the application layer. This reduces the overhead of network communication and ensures that ID generation is consistent and centralized.
The extension provides simple SQL functions to generate and validate various types of unique IDs. This makes it easy to integrate into existing SQL workflows without requiring significant changes to your application code.
For applications that require compliance with data security standards, using IDs like UUIDs can help meet these requirements. The extension ensures that IDs are generated in a secure manner, reducing the risk of predictable or duplicate IDs.
PostgreSQL has a rich ecosystem of extensions that enhance its capabilities. By using uids-postgres, you leverage the power of PostgreSQL's extensibility, allowing you to tailor your database to meet specific needs without altering its core architecture.
Unique IDs like UUIDs and Ksuid are designed to be scalable and can handle large volumes of data efficiently. This is crucial for applications that need to maintain good performance as they grow to handle billions of rows.
Many modern applications, especially those involving microservices, distributed systems, and IoT, benefit from using unique identifiers. The uids-postgres extension supports various ID types that are well-suited for these environments, ensuring compatibility and ease of integration.
By using the uids-postgres extension, you can enhance your PostgreSQL database with robust, flexible, and efficient unique ID generation capabilities, making it a valuable tool for a wide range of applications.
Choosing ULID or TypeID/ etc over UUID can offer several advantages depending on your specific use case. Here are some key reasons:
Lexicographical Sortability
Faster Generation
Compactness and URL-Safety
Timestamp Encoding
Custom Prefixes
Readability and Context
Cryptographic Security
Standardization
Choosing between these options depends on the specific requirements of your application, such as the need for sorting, speed of generation, security, and readability.
| Methodology | Function | Crate | Description |
|---|---|---|---|
| UUID v6 | generate_uuidv6() | uuid | UUID v6 ([RFC 4122][rfc-4122-update]) |
generate_uuidv6_text() | UUID v6 as text | ||
generate_uuidv6_uuid() | UUID v6 as Postgres UUID object | ||
| UUID v7 | generate_uuidv7() | uuid | UUID v7 ([RFC 4122][rfc-4122-update]) |
generate_uuidv7_bytes() | UUID v7 as bytes | ||
generate_uuidv7_from_string(TEXT) | Generate UUID v7 from a string | ||
parse_uuidv7(TEXT) | Parse UUID v7 | ||
| NanoId | generate_nanoid() | nanoid | NanoID, developed by [Andrey Sitnik][github-ai] |
generate_nanoid_length(INT) | NanoID with a custom length | ||
generate_nanoid_c(TEXT) | NanoID with custom alphabets | ||
generate_nanoid_length_c(INT, TEXT) | NanoID with custom length and alphabets | ||
| Ksuid | generate_ksuid() | svix-ksuid | Created by [Segment][segment] |
generate_ksuid_bytes() | KSUID as bytes | ||
| Ulid | generate_ulid() | ulid | Unique, lexicographically sortable identifiers |
generate_ulid_bytes() | ULID as bytes | ||
generate_ulid_from_string(TEXT) | Generate ULID from a string | ||
| Timeflake | generate_timeflake() | timeflake-rs | Twitter's Snowflake + Instagram's ID + Firebase's PushID |
generate_timeflake_bytes() | Timeflake as bytes | ||
generate_timeflake_uuid() | Timeflake as UUID | ||
| PushId | generate_pushid() | pushid | Google Firebase's PushID |
generate_pushid_text() | PushID as text | ||
| Cuid2 | generate_cuid2() | cuid2 | CUID2 |
check_cuid2(TEXT) | Check if a string is a valid CUID2 | ||
| TypeId | generate_typeid(TEXT) | typeid | Generate TypeId with a specific prefix |
check_typeid(TEXT, TEXT) | Check if a TypeId matches a specific prefix |
This Postgres extension is made possible thanks to pgrx.
Use pgrx. You can clone this repo and install this extension locally by following this guide.
You can also download relevant files from releases page.
Refer to the included Dockerfile for the installation template.
CREATE EXTENSION IF NOT EXISTS uids;
Generate a new KSUID:
SELECT generate_ksuid();
Example output:
28KKKI8lpDkK2lHbAdWdgJYoLWF
Generate KSUID bytes:
SELECT generate_ksuid_bytes();
Example output:
\x0ef557bc9b5b8027f222e2b32ed65e91b6bb8eb6
Generate a new NanoId (default size 21):
SELECT generate_nanoid();
Example output:
FfuwjZHjS5j5rATHVyl8M
Generate a NanoId with a custom size:
SELECT generate_nanoid_length(10);
Example output:
V2D2D7-dnw
Generate a NanoId with custom alphabets (length 21):
SELECT generate_nanoid_c('1234567890abcdef');
Example output:
6df80ad84587f4a20838c
Generate a NanoId with custom alphabets and custom length:
SELECT generate_nanoid_length_c(10, '1234567890abcdef');
Example output:
050487bff0
Generate a new Ulid:
SELECT generate_ulid();
Example output:
01G1JE4GXWC1A9PXHG0SXQDE1J
Generate Ulid bytes:
SELECT generate_ulid_bytes();
Example output:
\x018064e2bff9e6bb876aa8948e50d9c6
Generate Ulid from a custom string:
SELECT generate_ulid_from_string('01CAT3X5Y5G9A62F1rFA6Tnice');
Example output:
01CAT3X5Y5G9A62F1RFA6TN1CE
Generate a TypeId with a specific prefix:
SELECT generate_typeid('user');
Example output:
user_01h2xcejqtf2nbrexx3vqjhp41
Check if a TypeId matches a specific prefix:
SELECT check_typeid('user', 'user_01h2xcejqtf2nbrexx3vqjhp41');
Example output:
true
Generate a new UUIDv7:
SELECT generate_uuidv7();
Example output:
01809424-3e59-7c05-9219-566f82fff672
Generate UUIDv7 bytes:
SELECT generate_uuidv7_bytes();
Example output:
\x018094243e597c059219566f82fff672
Generate UUIDv7 from a string:
SELECT generate_uuidv7_from_string('67e55044-10b1-426f-9247-bb680e5fe0c8');
Example output:
67e55044-10b1-426f-9247-bb680e5fe0c8
Parse UUIDv7:
SELECT parse_uuidv7('67e55044-10b1-426f-9247-bb680e5fe0c8');
Example output:
67e55044-10b1-426f-9247-bb680e5fe0c8
Generate a new Cuid2:
SELECT generate_cuid2();
Example output:
cl8f8y8f80000000000000000
Check if a string is a valid Cuid2:
SELECT check_cuid2('cl8f8y8f80000000000000000');
Example output:
true
Generate a new PushId:
SELECT generate_pushid();
Example output:
-MZ1e2f3g4h5i6j7k8l9
Generate a PushId as text:
SELECT generate_pushid_text();
Example output:
-MZ1e2f3g4h5i6j7k8l9
Generate a new Timeflake:
SELECT generate_timeflake();
Example output:
01F8MECHJ8KZ9Q9J8KZ9Q9J8KZ
Generate Timeflake bytes:
SELECT generate_timeflake_bytes();
Example output:
\x018064e2bff9e6bb876aa8948e50d9c6
Generate Timeflake UUID:
SELECT generate_timeflake_uuid();
Example output:
018064e2-bff9-e6bb-876a-a8948e50d9c6
Generate a new UUIDv6:
SELECT generate_uuidv6();
Example output:
1e4eaa4e-7c4b-6e5d-8a4e-7c4b6e5d8a4e
Generate a UUIDv6 as text:
SELECT generate_uuidv6_text();
Example output:
1e4eaa4e-7c4b-6e5d-8a4e-7c4b6e5d8a4e
Generate a UUIDv6 as a Postgres UUID object:
SELECT generate_uuidv6_uuid();
Example output:
1e4eaa4e-7c4b-6e5d-8a4e-7c4b6e5d8a4e
This setup provides a comprehensive set of functions to generate and validate various types of unique identifiers within a PostgreSQL extension using pgx.
In recent additions to this project, the new ids, and the dockerfile were taken from pgx_ulid, alongside some help from pg_idkit.
79 followers · starred Aug 2024
Rust
87.2%
Dockerfile
12.8%
PostgreSQL Extension to generate various type of Universally Unique IDS
Rust
76
30 commits
updated Jun 9, 2024
postgres=# CREATE EXTENSION uids;
CREATE EXTENSION
postgres=# SELECT generate_typeid('user');
generate_typeid
--------------------------------------
user_01h2xcejqtf2nbrexx3vqjhp41
Using the uids-postgres extension for generating unique IDs in PostgreSQL offers several advantages:
The extension supports multiple methodologies for generating unique IDs, including UUID v6, UUID v7, NanoId, Ksuid, Ulid, Timeflake, PushId, and Cuid2. This flexibility allows you to choose the most suitable ID generation strategy for your specific use case, whether you need time-based IDs, lexicographically sortable IDs, or IDs with custom prefixes.
Using unique identifiers like UUIDs ensures data integrity and uniqueness across distributed systems. This is particularly important for applications that require federated data or need to avoid collisions in a multi-node environment.
Generating unique IDs within the database can be more efficient than handling this in the application layer. This reduces the overhead of network communication and ensures that ID generation is consistent and centralized.
The extension provides simple SQL functions to generate and validate various types of unique IDs. This makes it easy to integrate into existing SQL workflows without requiring significant changes to your application code.
For applications that require compliance with data security standards, using IDs like UUIDs can help meet these requirements. The extension ensures that IDs are generated in a secure manner, reducing the risk of predictable or duplicate IDs.
PostgreSQL has a rich ecosystem of extensions that enhance its capabilities. By using uids-postgres, you leverage the power of PostgreSQL's extensibility, allowing you to tailor your database to meet specific needs without altering its core architecture.
Unique IDs like UUIDs and Ksuid are designed to be scalable and can handle large volumes of data efficiently. This is crucial for applications that need to maintain good performance as they grow to handle billions of rows.
Many modern applications, especially those involving microservices, distributed systems, and IoT, benefit from using unique identifiers. The uids-postgres extension supports various ID types that are well-suited for these environments, ensuring compatibility and ease of integration.
By using the uids-postgres extension, you can enhance your PostgreSQL database with robust, flexible, and efficient unique ID generation capabilities, making it a valuable tool for a wide range of applications.
Choosing ULID or TypeID/ etc over UUID can offer several advantages depending on your specific use case. Here are some key reasons:
Lexicographical Sortability
Faster Generation
Compactness and URL-Safety
Timestamp Encoding
Custom Prefixes
Readability and Context
Cryptographic Security
Standardization
Choosing between these options depends on the specific requirements of your application, such as the need for sorting, speed of generation, security, and readability.
| Methodology | Function | Crate | Description |
|---|---|---|---|
| UUID v6 | generate_uuidv6() | uuid | UUID v6 ([RFC 4122][rfc-4122-update]) |
generate_uuidv6_text() | UUID v6 as text | ||
generate_uuidv6_uuid() | UUID v6 as Postgres UUID object | ||
| UUID v7 | generate_uuidv7() | uuid | UUID v7 ([RFC 4122][rfc-4122-update]) |
generate_uuidv7_bytes() | UUID v7 as bytes | ||
generate_uuidv7_from_string(TEXT) | Generate UUID v7 from a string | ||
parse_uuidv7(TEXT) | Parse UUID v7 | ||
| NanoId | generate_nanoid() | nanoid | NanoID, developed by [Andrey Sitnik][github-ai] |
generate_nanoid_length(INT) | NanoID with a custom length | ||
generate_nanoid_c(TEXT) | NanoID with custom alphabets | ||
generate_nanoid_length_c(INT, TEXT) | NanoID with custom length and alphabets | ||
| Ksuid | generate_ksuid() | svix-ksuid | Created by [Segment][segment] |
generate_ksuid_bytes() | KSUID as bytes | ||
| Ulid | generate_ulid() | ulid | Unique, lexicographically sortable identifiers |
generate_ulid_bytes() | ULID as bytes | ||
generate_ulid_from_string(TEXT) | Generate ULID from a string | ||
| Timeflake | generate_timeflake() | timeflake-rs | Twitter's Snowflake + Instagram's ID + Firebase's PushID |
generate_timeflake_bytes() | Timeflake as bytes | ||
generate_timeflake_uuid() | Timeflake as UUID | ||
| PushId | generate_pushid() | pushid | Google Firebase's PushID |
generate_pushid_text() | PushID as text | ||
| Cuid2 | generate_cuid2() | cuid2 | CUID2 |
check_cuid2(TEXT) | Check if a string is a valid CUID2 | ||
| TypeId | generate_typeid(TEXT) | typeid | Generate TypeId with a specific prefix |
check_typeid(TEXT, TEXT) | Check if a TypeId matches a specific prefix |
This Postgres extension is made possible thanks to pgrx.
Use pgrx. You can clone this repo and install this extension locally by following this guide.
You can also download relevant files from releases page.
Refer to the included Dockerfile for the installation template.
CREATE EXTENSION IF NOT EXISTS uids;
Generate a new KSUID:
SELECT generate_ksuid();
Example output:
28KKKI8lpDkK2lHbAdWdgJYoLWF
Generate KSUID bytes:
SELECT generate_ksuid_bytes();
Example output:
\x0ef557bc9b5b8027f222e2b32ed65e91b6bb8eb6
Generate a new NanoId (default size 21):
SELECT generate_nanoid();
Example output:
FfuwjZHjS5j5rATHVyl8M
Generate a NanoId with a custom size:
SELECT generate_nanoid_length(10);
Example output:
V2D2D7-dnw
Generate a NanoId with custom alphabets (length 21):
SELECT generate_nanoid_c('1234567890abcdef');
Example output:
6df80ad84587f4a20838c
Generate a NanoId with custom alphabets and custom length:
SELECT generate_nanoid_length_c(10, '1234567890abcdef');
Example output:
050487bff0
Generate a new Ulid:
SELECT generate_ulid();
Example output:
01G1JE4GXWC1A9PXHG0SXQDE1J
Generate Ulid bytes:
SELECT generate_ulid_bytes();
Example output:
\x018064e2bff9e6bb876aa8948e50d9c6
Generate Ulid from a custom string:
SELECT generate_ulid_from_string('01CAT3X5Y5G9A62F1rFA6Tnice');
Example output:
01CAT3X5Y5G9A62F1RFA6TN1CE
Generate a TypeId with a specific prefix:
SELECT generate_typeid('user');
Example output:
user_01h2xcejqtf2nbrexx3vqjhp41
Check if a TypeId matches a specific prefix:
SELECT check_typeid('user', 'user_01h2xcejqtf2nbrexx3vqjhp41');
Example output:
true
Generate a new UUIDv7:
SELECT generate_uuidv7();
Example output:
01809424-3e59-7c05-9219-566f82fff672
Generate UUIDv7 bytes:
SELECT generate_uuidv7_bytes();
Example output:
\x018094243e597c059219566f82fff672
Generate UUIDv7 from a string:
SELECT generate_uuidv7_from_string('67e55044-10b1-426f-9247-bb680e5fe0c8');
Example output:
67e55044-10b1-426f-9247-bb680e5fe0c8
Parse UUIDv7:
SELECT parse_uuidv7('67e55044-10b1-426f-9247-bb680e5fe0c8');
Example output:
67e55044-10b1-426f-9247-bb680e5fe0c8
Generate a new Cuid2:
SELECT generate_cuid2();
Example output:
cl8f8y8f80000000000000000
Check if a string is a valid Cuid2:
SELECT check_cuid2('cl8f8y8f80000000000000000');
Example output:
true
Generate a new PushId:
SELECT generate_pushid();
Example output:
-MZ1e2f3g4h5i6j7k8l9
Generate a PushId as text:
SELECT generate_pushid_text();
Example output:
-MZ1e2f3g4h5i6j7k8l9
Generate a new Timeflake:
SELECT generate_timeflake();
Example output:
01F8MECHJ8KZ9Q9J8KZ9Q9J8KZ
Generate Timeflake bytes:
SELECT generate_timeflake_bytes();
Example output:
\x018064e2bff9e6bb876aa8948e50d9c6
Generate Timeflake UUID:
SELECT generate_timeflake_uuid();
Example output:
018064e2-bff9-e6bb-876a-a8948e50d9c6
Generate a new UUIDv6:
SELECT generate_uuidv6();
Example output:
1e4eaa4e-7c4b-6e5d-8a4e-7c4b6e5d8a4e
Generate a UUIDv6 as text:
SELECT generate_uuidv6_text();
Example output:
1e4eaa4e-7c4b-6e5d-8a4e-7c4b6e5d8a4e
Generate a UUIDv6 as a Postgres UUID object:
SELECT generate_uuidv6_uuid();
Example output:
1e4eaa4e-7c4b-6e5d-8a4e-7c4b6e5d8a4e
This setup provides a comprehensive set of functions to generate and validate various types of unique identifiers within a PostgreSQL extension using pgx.
In recent additions to this project, the new ids, and the dockerfile were taken from pgx_ulid, alongside some help from pg_idkit.
79 followers · starred Aug 2024
Rust
87.2%
Dockerfile
12.8%