• Stars
    star
    463
  • Rank 94,661 (Top 2 %)
  • Language PLpgSQL
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

YouTube-like Short IDs as Postgres Primary Keys

pg-shortkey

This installs a trigger and type which allow you to use YouTube-like short IDs (e.g. 1TNhBqYo-6Q) as Postgres Primary Keys. Just like the YouTube IDs, SHORTKEY IDs are fixed length and URL-safe.

Short comment on why this exists and how I use it in the thread over on Hacker News.

Install

Execute pg-shortkey.sql against your DB. It will do nothing if shortkey is already installed.

Use

Example usage:

CREATE TABLE test (id SHORTKEY PRIMARY KEY, name TEXT);

CREATE TRIGGER trigger_test_genid BEFORE INSERT ON test FOR EACH ROW EXECUTE PROCEDURE shortkey_generate();

-- generate
INSERT INTO test(name) VALUES ('bob'), ('joe');

-- user-supplied ID
INSERT INTO test(id, name) VALUES ('1TNhBqYo-6Q', 'lisa');

SELECT * FROM test;

-- id           name
-- 4E_z0mHJvrk  bob
-- wiz_j0HIBuQ  joe
-- 1TNhBqYo-6Q  lisa

SHORTKEY is compatible with text and should be handled as such by most sane ORMs. If not, create a type mapping to whatever your string type is in your app/ORM.

Caveats

SHORTKEYs are truly random, so they fragment the index space just like v4 UUIDs, decreasing performance slightly. But unlike UUID (which provides an instance-local pseudo-sequential type via UUID v1 MC), that behavior can't be changed. This is intentional. SHORTKEYs are supposed to be used for external facing IDs, like in APIs. There, they prevent enumeration and cardinality evaluation (e.g. how many things are there and what's the next/previous thing - just like YouTube). Use where appropriate.

More Repositories

1

js

turbo.js - perform massive parallel computations in your browser with GPGPU.
JavaScript
2,616
star
2

openftp4

A list of all FTP servers in IPv4 that allow anonymous logins.
653
star
3

KPTI-PoC-Collection

Meltdown/Spectre PoC src collection.
C++
498
star
4

c4

Open IP cameras in IPv4
131
star
5

justContext.js

Styleable context menu in pure JS (no jQuery).
JavaScript
80
star
6

zero2hero

C implementation of the file-less UAC exploit
C
72
star
7

pg-costop

Vector Arithmetic and Weighted, Variably Randomized Cosine Similarity Search in Postgres
PLpgSQL
40
star
8

nuTLS

Minimal, modern, dependency-free TLS 1.2 and 1.3 server/client library for x64 Linux.
C
38
star
9

zQuery

Pure JavaScript alternatives to jQuery things.
30
star
10

Kafka

Smallest possible C/++ GLSL framework (~550B) for demo-coding.
C++
24
star
11

treetop

Accurate memory and CPU usage stats for a linux process tree.
C#
22
star
12

medium125k

Dataset of 125,000 Medium Blog Post Titles and Subtitles (with Categories)
18
star
13

NanoCL

Zero-Overhead bare-metal GPGPU library for C++ on Windows.
C++
15
star
14

wasup

This experiment is no longer available.
10
star
15

ftprobe

Check if FTP server allows anonymous logins with no overhead. (buggy)
C
6
star
16

FormulaCompiler

Compile any code to basic mathematical formulas.
5
star
17

love2d-fxaa

FXAA Anti-Aliasing for Love2D
GLSL
4
star
18

gtfotfs

TFVC to Git Migration tool. Best for unattended migration using a unix system.
Shell
3
star
19

Recall

[asm.js experiment] Stack-based language that only knows about bit-ops.
3
star
20

Endering

Dictionary-assisted suffix-stripping morphographemic analyser
MoonScript
2
star
21

bee

(Archived, Incomplete) Text editor written in Bash 3
Shell
2
star
22

BrowserDisapproval

Subtly disapprove of browser choices.
2
star
23

mediatool

Dockerized ffmpeg, sox & imagemagick
Shell
1
star
24

mash

Summarity open-source monorepo
Nim
1
star
25

dlist

Naive doubly-linked list for Fennel
Fennel
1
star
26

ReduceRescan

Reduce a FTP banner grab JSON log and rescan with a custom payload. Used for openftp4.
Shell
1
star