pg_slug_gen

pg_slug_gen

pg_slug_gen : Generate cryptographically secure timestamp-based slugs

Overview

ID Extension Package Version Category License Language
4550
pg_slug_gen
pg_slug_gen
1.0.0
FUNC
MIT
C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--s-d-r
No
Yes
No
Yes
yes
no
Relationships
See Also
pg_hashids
sequential_uuids
uuid-ossp
pg_uuidv7

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT
PIGSTY
1.0.0
18
17
16
15
14
pg_slug_gen -
RPM
PIGSTY
1.0.0
18
17
16
15
14
pg_slug_gen_$v -
DEB
PIGSTY
1.0.0
18
17
16
15
14
postgresql-$v-pg-slug-gen -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
el8.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
el9.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
el9.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
el10.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
el10.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
d12.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
d12.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
d13.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
d13.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
u22.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
u22.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
u24.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
u24.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
MISS
Package Version OS ORG SIZE File URL
pg_slug_gen_18 1.0.0 el8.x86_64 pigsty 14.7 KiB pg_slug_gen_18-1.0.0-1PIGSTY.el8.x86_64.rpm
pg_slug_gen_18 1.0.0 el8.aarch64 pigsty 14.9 KiB pg_slug_gen_18-1.0.0-1PIGSTY.el8.aarch64.rpm
pg_slug_gen_18 1.0.0 el9.x86_64 pigsty 14.6 KiB pg_slug_gen_18-1.0.0-1PIGSTY.el9.x86_64.rpm
pg_slug_gen_18 1.0.0 el9.aarch64 pigsty 14.8 KiB pg_slug_gen_18-1.0.0-1PIGSTY.el9.aarch64.rpm
pg_slug_gen_18 1.0.0 el10.x86_64 pigsty 14.5 KiB pg_slug_gen_18-1.0.0-1PIGSTY.el10.x86_64.rpm
pg_slug_gen_18 1.0.0 el10.aarch64 pigsty 14.8 KiB pg_slug_gen_18-1.0.0-1PIGSTY.el10.aarch64.rpm
postgresql-18-pg-slug-gen 1.0.0 d12.x86_64 pigsty 12.1 KiB postgresql-18-pg-slug-gen_1.0.0-1PIGSTY~bookworm_amd64.deb
postgresql-18-pg-slug-gen 1.0.0 d12.aarch64 pigsty 12.0 KiB postgresql-18-pg-slug-gen_1.0.0-1PIGSTY~bookworm_arm64.deb
postgresql-18-pg-slug-gen 1.0.0 d13.x86_64 pigsty 12.0 KiB postgresql-18-pg-slug-gen_1.0.0-1PIGSTY~trixie_amd64.deb
postgresql-18-pg-slug-gen 1.0.0 d13.aarch64 pigsty 12.0 KiB postgresql-18-pg-slug-gen_1.0.0-1PIGSTY~trixie_arm64.deb
postgresql-18-pg-slug-gen 1.0.0 u22.x86_64 pigsty 12.0 KiB postgresql-18-pg-slug-gen_1.0.0-1PIGSTY~jammy_amd64.deb
postgresql-18-pg-slug-gen 1.0.0 u22.aarch64 pigsty 12.0 KiB postgresql-18-pg-slug-gen_1.0.0-1PIGSTY~jammy_arm64.deb
postgresql-18-pg-slug-gen 1.0.0 u24.x86_64 pigsty 12.3 KiB postgresql-18-pg-slug-gen_1.0.0-1PIGSTY~noble_amd64.deb
postgresql-18-pg-slug-gen 1.0.0 u24.aarch64 pigsty 12.1 KiB postgresql-18-pg-slug-gen_1.0.0-1PIGSTY~noble_arm64.deb
Package Version OS ORG SIZE File URL
pg_slug_gen_17 1.0.0 el8.x86_64 pigsty 14.7 KiB pg_slug_gen_17-1.0.0-1PIGSTY.el8.x86_64.rpm
pg_slug_gen_17 1.0.0 el8.aarch64 pigsty 14.9 KiB pg_slug_gen_17-1.0.0-1PIGSTY.el8.aarch64.rpm
pg_slug_gen_17 1.0.0 el9.x86_64 pigsty 14.6 KiB pg_slug_gen_17-1.0.0-1PIGSTY.el9.x86_64.rpm
pg_slug_gen_17 1.0.0 el9.aarch64 pigsty 14.8 KiB pg_slug_gen_17-1.0.0-1PIGSTY.el9.aarch64.rpm
pg_slug_gen_17 1.0.0 el10.x86_64 pigsty 14.6 KiB pg_slug_gen_17-1.0.0-1PIGSTY.el10.x86_64.rpm
pg_slug_gen_17 1.0.0 el10.aarch64 pigsty 14.8 KiB pg_slug_gen_17-1.0.0-1PIGSTY.el10.aarch64.rpm
postgresql-17-pg-slug-gen 1.0.0 d12.x86_64 pigsty 12.0 KiB postgresql-17-pg-slug-gen_1.0.0-1PIGSTY~bookworm_amd64.deb
postgresql-17-pg-slug-gen 1.0.0 d12.aarch64 pigsty 12.0 KiB postgresql-17-pg-slug-gen_1.0.0-1PIGSTY~bookworm_arm64.deb
postgresql-17-pg-slug-gen 1.0.0 d13.x86_64 pigsty 12.0 KiB postgresql-17-pg-slug-gen_1.0.0-1PIGSTY~trixie_amd64.deb
postgresql-17-pg-slug-gen 1.0.0 d13.aarch64 pigsty 12.0 KiB postgresql-17-pg-slug-gen_1.0.0-1PIGSTY~trixie_arm64.deb
postgresql-17-pg-slug-gen 1.0.0 u22.x86_64 pigsty 12.3 KiB postgresql-17-pg-slug-gen_1.0.0-1PIGSTY~jammy_amd64.deb
postgresql-17-pg-slug-gen 1.0.0 u22.aarch64 pigsty 12.3 KiB postgresql-17-pg-slug-gen_1.0.0-1PIGSTY~jammy_arm64.deb
postgresql-17-pg-slug-gen 1.0.0 u24.x86_64 pigsty 12.3 KiB postgresql-17-pg-slug-gen_1.0.0-1PIGSTY~noble_amd64.deb
postgresql-17-pg-slug-gen 1.0.0 u24.aarch64 pigsty 12.1 KiB postgresql-17-pg-slug-gen_1.0.0-1PIGSTY~noble_arm64.deb
Package Version OS ORG SIZE File URL
pg_slug_gen_16 1.0.0 el8.x86_64 pigsty 14.7 KiB pg_slug_gen_16-1.0.0-1PIGSTY.el8.x86_64.rpm
pg_slug_gen_16 1.0.0 el8.aarch64 pigsty 14.9 KiB pg_slug_gen_16-1.0.0-1PIGSTY.el8.aarch64.rpm
pg_slug_gen_16 1.0.0 el9.x86_64 pigsty 14.6 KiB pg_slug_gen_16-1.0.0-1PIGSTY.el9.x86_64.rpm
pg_slug_gen_16 1.0.0 el9.aarch64 pigsty 14.8 KiB pg_slug_gen_16-1.0.0-1PIGSTY.el9.aarch64.rpm
pg_slug_gen_16 1.0.0 el10.x86_64 pigsty 14.6 KiB pg_slug_gen_16-1.0.0-1PIGSTY.el10.x86_64.rpm
pg_slug_gen_16 1.0.0 el10.aarch64 pigsty 14.8 KiB pg_slug_gen_16-1.0.0-1PIGSTY.el10.aarch64.rpm
postgresql-16-pg-slug-gen 1.0.0 d12.x86_64 pigsty 12.0 KiB postgresql-16-pg-slug-gen_1.0.0-1PIGSTY~bookworm_amd64.deb
postgresql-16-pg-slug-gen 1.0.0 d12.aarch64 pigsty 12.0 KiB postgresql-16-pg-slug-gen_1.0.0-1PIGSTY~bookworm_arm64.deb
postgresql-16-pg-slug-gen 1.0.0 d13.x86_64 pigsty 12.0 KiB postgresql-16-pg-slug-gen_1.0.0-1PIGSTY~trixie_amd64.deb
postgresql-16-pg-slug-gen 1.0.0 d13.aarch64 pigsty 12.0 KiB postgresql-16-pg-slug-gen_1.0.0-1PIGSTY~trixie_arm64.deb
postgresql-16-pg-slug-gen 1.0.0 u22.x86_64 pigsty 12.3 KiB postgresql-16-pg-slug-gen_1.0.0-1PIGSTY~jammy_amd64.deb
postgresql-16-pg-slug-gen 1.0.0 u22.aarch64 pigsty 12.3 KiB postgresql-16-pg-slug-gen_1.0.0-1PIGSTY~jammy_arm64.deb
postgresql-16-pg-slug-gen 1.0.0 u24.x86_64 pigsty 12.3 KiB postgresql-16-pg-slug-gen_1.0.0-1PIGSTY~noble_amd64.deb
postgresql-16-pg-slug-gen 1.0.0 u24.aarch64 pigsty 12.1 KiB postgresql-16-pg-slug-gen_1.0.0-1PIGSTY~noble_arm64.deb
Package Version OS ORG SIZE File URL
pg_slug_gen_15 1.0.0 el8.x86_64 pigsty 14.7 KiB pg_slug_gen_15-1.0.0-1PIGSTY.el8.x86_64.rpm
pg_slug_gen_15 1.0.0 el8.aarch64 pigsty 14.9 KiB pg_slug_gen_15-1.0.0-1PIGSTY.el8.aarch64.rpm
pg_slug_gen_15 1.0.0 el9.x86_64 pigsty 14.6 KiB pg_slug_gen_15-1.0.0-1PIGSTY.el9.x86_64.rpm
pg_slug_gen_15 1.0.0 el9.aarch64 pigsty 14.8 KiB pg_slug_gen_15-1.0.0-1PIGSTY.el9.aarch64.rpm
pg_slug_gen_15 1.0.0 el10.x86_64 pigsty 14.6 KiB pg_slug_gen_15-1.0.0-1PIGSTY.el10.x86_64.rpm
pg_slug_gen_15 1.0.0 el10.aarch64 pigsty 14.8 KiB pg_slug_gen_15-1.0.0-1PIGSTY.el10.aarch64.rpm
postgresql-15-pg-slug-gen 1.0.0 d12.x86_64 pigsty 12.0 KiB postgresql-15-pg-slug-gen_1.0.0-1PIGSTY~bookworm_amd64.deb
postgresql-15-pg-slug-gen 1.0.0 d12.aarch64 pigsty 12.0 KiB postgresql-15-pg-slug-gen_1.0.0-1PIGSTY~bookworm_arm64.deb
postgresql-15-pg-slug-gen 1.0.0 d13.x86_64 pigsty 12.0 KiB postgresql-15-pg-slug-gen_1.0.0-1PIGSTY~trixie_amd64.deb
postgresql-15-pg-slug-gen 1.0.0 d13.aarch64 pigsty 11.9 KiB postgresql-15-pg-slug-gen_1.0.0-1PIGSTY~trixie_arm64.deb
postgresql-15-pg-slug-gen 1.0.0 u22.x86_64 pigsty 12.3 KiB postgresql-15-pg-slug-gen_1.0.0-1PIGSTY~jammy_amd64.deb
postgresql-15-pg-slug-gen 1.0.0 u22.aarch64 pigsty 12.3 KiB postgresql-15-pg-slug-gen_1.0.0-1PIGSTY~jammy_arm64.deb
postgresql-15-pg-slug-gen 1.0.0 u24.x86_64 pigsty 12.3 KiB postgresql-15-pg-slug-gen_1.0.0-1PIGSTY~noble_amd64.deb
postgresql-15-pg-slug-gen 1.0.0 u24.aarch64 pigsty 12.1 KiB postgresql-15-pg-slug-gen_1.0.0-1PIGSTY~noble_arm64.deb

Source

pig build pkg pg_slug_gen;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with pig:

pig install pg_slug_gen;		# install via package name, for the active PG version

pig install pg_slug_gen -v 18;   # install for PG 18
pig install pg_slug_gen -v 17;   # install for PG 17
pig install pg_slug_gen -v 16;   # install for PG 16
pig install pg_slug_gen -v 15;   # install for PG 15

Create this extension with:

CREATE EXTENSION pg_slug_gen;

Usage

Syntax:

CREATE EXTENSION pg_slug_gen;
SELECT gen_random_slug();
SELECT gen_random_slug(13);

Source: PGXN release README

pg_slug_gen generates timestamp-based slugs using cryptographic randomness. The PGXN release README describes it as a PostgreSQL extension that maps timestamp digits into letter buckets and inserts a hyphen in the middle, producing URL-friendly slugs.

Function

The documented SQL function is:

gen_random_slug(slug_length int DEFAULT 16) RETURNS text

The README shows these interfaces:

gen_random_slug()      -- default: 16 (microseconds)
gen_random_slug(10)    -- seconds
gen_random_slug(13)    -- milliseconds
gen_random_slug(16)    -- microseconds
gen_random_slug(19)    -- nanoseconds

Precision and Length

The release README maps precision to timestamp digits and maximum collision-free throughput:

  • 10 digits for seconds, up to 1 insert per second
  • 13 digits for milliseconds, up to 1,000 inserts per second
  • 16 digits for microseconds, up to 1,000,000 inserts per second
  • 19 digits for nanoseconds, up to 1 billion inserts per second

The slug includes a midpoint hyphen:

  • seconds: 5-5 pattern, 11 characters total
  • milliseconds: 6-7 pattern, 14 characters
  • microseconds: 8-8 pattern, 17 characters
  • nanoseconds: 9-10 pattern, 20 characters

Examples

SELECT gen_random_slug();
SELECT gen_random_slug(10);
SELECT gen_random_slug(13);
SELECT gen_random_slug(16);
SELECT gen_random_slug(19);

As a table default:

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

How It Works

The release README describes the algorithm as:

  1. take the current timestamp at the chosen precision
  2. map each digit to a QWERTY-based character bucket
  3. choose one random character from that bucket using pg_strong_random()
  4. insert a hyphen at the midpoint

The README also notes that same-timestamp collisions remain possible, but with microsecond precision the probability is stated as roughly 1 in 10 million.

Last updated on