PostgreSQL extension for generating Xata-style xata_id unique identifiers (rec_<20_chars>) using Rust and pgrx.
1# xata_id PostgreSQL Extension 2[![ci](https://github.com/tsirysndr/xata_id_extension/actions/workflows/ci.yml/badge.svg)](https://github.com/tsirysndr/xata_id_extension/actions/workflows/ci.yml) 3 4A PostgreSQL extension written in Rust using pgrx to generate Xata-style unique identifiers (xata_id) in the format rec_<20_random_chars>. The IDs consist of a rec_ prefix followed by a 20-character random string using the alphabet a-z0-9, mimicking Xata's ID format (e.g., ` rec_cug4h6ibhfbm7uq5dte0`). 5 6## Features 7- Generates 24-character unique IDs with a rec_ prefix and 20 random characters (a-z0-9). 8- Safe integration with PostgreSQL via pgrx for robust memory management. 9- Comprehensive unit and integration tests to verify ID format, length, and uniqueness. 10- Lightweight and performant, suitable for use as a primary key default value. 11 12## Requirements 13- PostgreSQL 13–18 14- Rust and cargo-pgrx (version 0.15) 15- A compatible operating system (Linux, macOS, or Windows with WSL) 16 17## Installation 18**1. Install Dependencies** 19 20Install Rust and pgrx: 21 22```bash 23curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh 24cargo install cargo-pgrx 25cargo pgrx init 26``` 27 28**2. Clone the repository:** 29 30```bash 31git clone https://github.com/tsirysndr/xata_id_extension 32cd xata_id_extension 33``` 34 35**3. Build the Extension** 36 37Compile the extension for your PostgreSQL version (e.g., PostgreSQL 16): 38 39```bash 40cargo pgrx package 41``` 42 43**4. Install the Extension** 44 45Install the extension to your PostgreSQL instance: 46 47```bash 48cargo pgrx install 49``` 50 51**5. Enable the Extension in PostgreSQL** 52 53Connect to your PostgreSQL database and run: 54 55```bash 56psql -d your_database_name 57``` 58 59Then, enable the extension: 60 61```sql 62CREATE EXTENSION xata_id_extension; 63``` 64 65## Usage 66 67The extension provides a `xata_id()` function that generates a unique ID. Use it as the default value for a primary key in a table: 68 69**Create a Table** 70 71```sql 72CREATE TABLE users ( 73 id TEXT PRIMARY KEY DEFAULT xata_id(), 74 username VARCHAR(50) NOT NULL, 75 email VARCHAR(255) UNIQUE, 76 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP 77); 78``` 79**Insert Data** 80 81Insert rows, and the id column will automatically use xata_id(): 82 83```sql 84INSERT INTO users (username, email) VALUES ('alice', 'alice@example.com'); 85INSERT INTO users (username, email) VALUES ('bob', 'bob@example.com'); 86SELECT * FROM users; 87``` 88 89**Example Output** 90 91```plaintext 92 id | username | email | created_at 93--------------------------+----------+-------------------+------------------------ 94 rec_cug4h6ibhfbm7uq5dte0 | alice | alice@example.com | 2025-07-25 12:00:00 95 rec_4h6ibhfbm7uq5dte0cug | bob | bob@example.com | 2025-07-25 12:01:00 96``` 97 98## License 99 100This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.