···11+Copyright (c) 2025 Tsiry Sandratraina <tsiry.sndr@rocksky.app>
22+33+Permission is hereby granted, free of charge, to any person obtaining a copy
44+of this software and associated documentation files (the "Software"), to deal
55+in the Software without restriction, including without limitation the rights
66+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
77+copies of the Software, and to permit persons to whom the Software is
88+furnished to do so, subject to the following conditions:
99+1010+The above copyright notice and this permission notice shall be included in all
1111+copies or substantial portions of the Software.
1212+1313+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1414+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1515+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1616+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1717+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919+SOFTWARE.
+100
README.md
···11+# xata_id PostgreSQL Extension
22+[](https://github.com/tsirysndr/xata_id_extension/actions/workflows/ci.yml)
33+44+A 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`).
55+66+## Features
77+- Generates 24-character unique IDs with a rec_ prefix and 20 random characters (a-z0-9).
88+- Safe integration with PostgreSQL via pgrx for robust memory management.
99+- Comprehensive unit and integration tests to verify ID format, length, and uniqueness.
1010+- Lightweight and performant, suitable for use as a primary key default value.
1111+1212+## Requirements
1313+- PostgreSQL 13–18
1414+- Rust and cargo-pgrx (version 0.15)
1515+- A compatible operating system (Linux, macOS, or Windows with WSL)
1616+1717+## Installation
1818+**1. Install Dependencies**
1919+2020+Install Rust and pgrx:
2121+2222+```bash
2323+curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
2424+cargo install cargo-pgrx
2525+cargo pgrx init
2626+```
2727+2828+**2. Clone the repository:**
2929+3030+```bash
3131+git clone https://github.com/tsirysndr/xata_id_extension
3232+cd xata_id_extension
3333+```
3434+3535+**3. Build the Extension**
3636+3737+Compile the extension for your PostgreSQL version (e.g., PostgreSQL 16):
3838+3939+```bash
4040+cargo pgrx package
4141+```
4242+4343+**4. Install the Extension**
4444+4545+Install the extension to your PostgreSQL instance:
4646+4747+```bash
4848+cargo pgrx install
4949+```
5050+5151+**5. Enable the Extension in PostgreSQL**
5252+5353+Connect to your PostgreSQL database and run:
5454+5555+```bash
5656+psql -d your_database_name
5757+```
5858+5959+Then, enable the extension:
6060+6161+```sql
6262+CREATE EXTENSION xata_id_extension;
6363+```
6464+6565+## Usage
6666+6767+The extension provides a `xata_id()` function that generates a unique ID. Use it as the default value for a primary key in a table:
6868+6969+**Create a Table**
7070+7171+```sql
7272+CREATE TABLE users (
7373+ id TEXT PRIMARY KEY DEFAULT xata_id(),
7474+ username VARCHAR(50) NOT NULL,
7575+ email VARCHAR(255) UNIQUE,
7676+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
7777+);
7878+```
7979+**Insert Data**
8080+8181+Insert rows, and the id column will automatically use xata_id():
8282+8383+```sql
8484+INSERT INTO users (username, email) VALUES ('alice', 'alice@example.com');
8585+INSERT INTO users (username, email) VALUES ('bob', 'bob@example.com');
8686+SELECT * FROM users;
8787+```
8888+8989+**Example Output**
9090+9191+```plaintext
9292+ id | username | email | created_at
9393+--------------------------+----------+-------------------+------------------------
9494+ rec_cug4h6ibhfbm7uq5dte0 | alice | alice@example.com | 2025-07-25 12:00:00
9595+ rec_4h6ibhfbm7uq5dte0cug | bob | bob@example.com | 2025-07-25 12:01:00
9696+```
9797+9898+## License
9999+100100+This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.