short_ids
short_ids · PL/pgSQL generator for short random text IDs with collision checks and transaction advisory locks.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 101161 | short_ids | short_ids | 0.1.0 | FUNC | PostgreSQL | SQL |
PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18
| Relationships | |
|---|---|
| Requires | plpgsql |
Usage
Sources:
short_ids generates short random text identifiers for use as a column default. It checks the target table for collisions and uses transaction-scoped advisory locks so concurrent callers of the same function do not return the same candidate; a unique or primary-key constraint must still be the authoritative guard.
Core Workflow
For a custom alphabet, pass the optional fifth argument:
On collision or advisory-lock contention, generate_random_id() increases the candidate length by one and tries again.
Function Index
generate_random_id(table_schema text, table_name text, column_name text, string_length integer, possible_chars text DEFAULT '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')returns a currently unused candidate.get_random_string(string_length integer, possible_chars text DEFAULT '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')generates a string without checking a table.
Caveats
Version 0.1.0 uses PostgreSQL random(), not a cryptographically secure generator. Identifiers are guessable and must not serve as passwords, bearer tokens, or access-control secrets. Safety depends on all writers respecting the database uniqueness constraint; direct concurrent inserts can race with the pre-check, and callers must retry a uniqueness violation. Validate positive lengths and a nonempty alphabet, schema-qualify the function if installed outside public, monitor collision-driven growth, and note that the project is old and cataloged as abandoned.