pg_slug_gen

Generate cryptographically secure timestamp-based slugs

Overview

PackageVersionCategoryLicenseLanguage
pg_slug_gen1.0.0FUNCMITC
IDExtensionBinLibLoadCreateTrustRelocSchema
4550pg_slug_genNoYesNoYesNoYes-
Relatedpg_hashids sequential_uuids uuid-ossp pg_uuidv7

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.0.01817161514pg_slug_gen-
RPMPIGSTY1.0.01817161514pg_slug_gen_$v-
DEBPIGSTY1.0.01817161514postgresql-$v-pg-slug-gen-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64PIGSTY MISS
el8.aarch64PIGSTY MISS
el9.x86_64PIGSTY MISS
el9.aarch64PIGSTY MISS
el10.x86_64PIGSTY MISS
el10.aarch64PIGSTY MISS
d12.x86_64PIGSTY MISS
d12.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY MISS
d13.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY MISS
d13.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY MISS
u22.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY MISS
u22.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY MISS
u24.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY MISS
u24.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY MISS

Build

You can build the RPM / DEB packages for pg_slug_gen using pig build:

pig build pkg pg_slug_gen         # build RPM / DEB packages

Install

You can install pg_slug_gen directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_slug_gen;          # Install for current active PG version
pig ext install -y pg_slug_gen -v 18  # PG 18
pig ext install -y pg_slug_gen -v 17  # PG 17
pig ext install -y pg_slug_gen -v 16  # PG 16
pig ext install -y pg_slug_gen -v 15  # PG 15
dnf install -y pg_slug_gen_18       # PG 18
dnf install -y pg_slug_gen_17       # PG 17
dnf install -y pg_slug_gen_16       # PG 16
dnf install -y pg_slug_gen_15       # PG 15
apt install -y postgresql-18-pg-slug-gen   # PG 18
apt install -y postgresql-17-pg-slug-gen   # PG 17
apt install -y postgresql-16-pg-slug-gen   # PG 16
apt install -y postgresql-15-pg-slug-gen   # PG 15

Create Extension:

CREATE EXTENSION pg_slug_gen;

Usage

Sources: official PGXN release page, official release README, official release SQL, official release metadata

pg_slug_gen generates timestamp-based slugs with cryptographic randomness. The official 1.0.0 release describes it as a secure, URL-friendly short ID generator where the requested length selects the timestamp precision.

CREATE EXTENSION pg_slug_gen;

SELECT gen_random_slug();
SELECT gen_random_slug(13);

Function

  • gen_random_slug(slug_length int DEFAULT 16) returns text

The release SQL comment and README document these supported values:

  • 10: seconds
  • 13: milliseconds
  • 16: microseconds, also the default
  • 19: nanoseconds

Precision And Format

Each precision maps to a timestamp width and a fixed slug shape:

  • 10 digits: 5-5 format, 11 characters total
  • 13 digits: 6-7 format, 14 characters total
  • 16 digits: 8-8 format, 17 characters total
  • 19 digits: 9-10 format, 20 characters total

The README states the collision-free window is bounded by timestamp precision: at most 1 insert per second, millisecond, microsecond, or nanosecond respectively.

Examples

SELECT gen_random_slug();
SELECT gen_random_slug(10);
SELECT gen_random_slug(16);

CREATE TABLE products (
  id serial PRIMARY KEY,
  name text NOT NULL,
  slug text DEFAULT gen_random_slug() UNIQUE
);

How It Works

The official README describes this algorithm:

  • take the current timestamp at the chosen precision
  • map each digit to a QWERTY-based character bucket
  • choose one random character from that bucket with pg_strong_random()
  • insert a hyphen at the midpoint

Caveats

  • This is a secure short-ID generator, not a text transliteration or title-to-URL slugifier.
  • Same-timestamp collisions are still possible; upstream only claims uniqueness when inserts do not exceed one per chosen time unit.
  • The official release metadata still points to https://github.com/fernandoolle/pg_slug_gen, but that repo URL currently returns 404.

Last Modified 2026-04-19: update extension stub docs (9f178c3)