Repository navigation
Expand file tree
/
Copy pathnanoid.sql
More file actions
154 lines (141 loc) · 7.37 KB
/
Copy pathnanoid.sql
File metadata and controls
154 lines (141 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright 2026 Viascom Ltd liab. Co
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-- The whole install/upgrade runs in one transaction: if any statement fails (for example a
-- DROP blocked by objects that depend on an old function signature), everything rolls back
-- and the database keeps its previous, fully working installation.
BEGIN;
CREATE EXTENSION IF NOT EXISTS pgcrypto;
-- The `nanoid()` function generates a compact, URL-friendly unique identifier.
-- Based on the given size and alphabet, it creates a randomized string that's ideal for
-- use-cases requiring small, unpredictable IDs (e.g., URL shorteners, generated file names, etc.).
-- While it comes with a default configuration, the function is designed to be flexible,
-- allowing for customization to meet specific needs.
-- Old signatures are dropped so positional calls stay unambiguous after an upgrade: the
-- 2022-era nanoid(int, text) and the pre-prefix nanoid(int, text, float), which cannot be
-- upgraded in place because the signature gains the prefix parameter. If objects such as
-- column defaults depend on the old function, the drop is refused and the whole script
-- rolls back; see the Upgrading section of the README for the recovery steps.
DROP FUNCTION IF EXISTS nanoid(int, text);
DROP FUNCTION IF EXISTS nanoid(int, text, float);
CREATE OR REPLACE FUNCTION nanoid(
size int DEFAULT 21, -- The number of symbols in the NanoId String. Must be greater than 0.
alphabet text DEFAULT '_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', -- The symbols used in the NanoId String. Must contain between 1 and 256 symbols.
additionalBytesFactor float DEFAULT 1.6, -- The additional bytes factor used for calculating the step size. Acts as a safety margin for rejected bytes. Must be equal to or greater than 1.
prefix text DEFAULT '' -- An optional prefix prepended to the NanoId String (e.g. 'usr_'). Does not count towards size; NULL behaves like ''.
)
RETURNS text -- A randomly generated NanoId String
LANGUAGE plpgsql
VOLATILE
-- PL/pgSQL advances the command counter and takes new snapshots when evaluating volatile expressions,
-- which is forbidden in parallel mode ("cannot start commands during a parallel operation", see issue #16).
PARALLEL UNSAFE
-- Uncomment the following line if you have superuser privileges
-- LEAKPROOF
AS
$$
DECLARE
alphabetLength int;
cutoff int;
step int;
BEGIN
IF size IS NULL OR size < 1 THEN
RAISE EXCEPTION 'The size must be defined and greater than 0!';
END IF;
IF alphabet IS NULL OR length(alphabet) = 0 OR length(alphabet) > 256 THEN
RAISE EXCEPTION 'The alphabet can''t be undefined, zero or bigger than 256 symbols!';
END IF;
IF additionalBytesFactor IS NULL OR additionalBytesFactor < 1 THEN
RAISE EXCEPTION 'The additional bytes factor can''t be less than 1!';
END IF;
alphabetLength := length(alphabet);
-- Random bytes are 0-255. `byte % alphabetLength` would make some symbols more likely
-- when 256 is not a multiple of the alphabet length. Bytes greater than or equal to
-- `cutoff` are rejected instead, so every symbol keeps an equal chance.
cutoff := 256 - (256 % alphabetLength);
-- On average `256 / cutoff` random bytes are needed per symbol; the additional bytes
-- factor adds a safety margin to cover unlucky streaks of rejected bytes.
-- gen_random_bytes() accepts at most 1024 bytes per call; capping before the int cast
-- also keeps absurd sizes from overflowing the cast.
step := cast(least(1024, ceil(additionalBytesFactor * 256 * size / cutoff)) AS int);
RETURN coalesce(prefix, '') || nanoid_optimized(size, alphabet, cutoff, step);
END
$$;
-- Generates an optimized random string of a specified size using the given alphabet, cutoff, and step.
-- This optimized version is designed for higher performance and lower memory overhead.
-- Beyond the termination guards below, no checks are performed (the cutoff is not validated against
-- the alphabet)! Use it only if you really know what you are doing.
-- The drop is required because the third parameter was renamed (mask -> cutoff) and
-- CREATE OR REPLACE cannot rename parameters. If dependent objects block the drop, the
-- transaction rolls back; see the Upgrading section of the README.
DROP FUNCTION IF EXISTS nanoid_optimized(int, text, int, int);
CREATE OR REPLACE FUNCTION nanoid_optimized(
size int, -- The desired length of the generated string.
alphabet text, -- The set of characters to choose from for generating the string.
cutoff int, -- The exclusive upper bound for accepted random bytes. Should be `256 - (256 % alphabetLength)`; bytes greater than or equal to it are rejected to avoid modulo bias.
step int -- The number of random bytes to generate in each iteration. A larger value may speed up the function but increase memory usage.
)
RETURNS text -- A randomly generated NanoId String
LANGUAGE plpgsql
VOLATILE
-- PL/pgSQL advances the command counter and takes new snapshots when evaluating volatile expressions,
-- which is forbidden in parallel mode ("cannot start commands during a parallel operation", see issue #16).
PARALLEL UNSAFE
-- Uncomment the following line if you have superuser privileges
-- LEAKPROOF
AS
$$
DECLARE
idBuilder text := '';
counter int := 0;
bytes bytea;
randomByte int;
alphabetArray text[];
alphabetLength int;
BEGIN
-- Termination guards: without them these inputs would spin the generation loop forever,
-- since the only exit is reached after a character has been appended.
IF size IS NULL OR size < 1 THEN
RAISE EXCEPTION 'The size must be defined and greater than 0!';
END IF;
IF alphabet IS NULL OR length(alphabet) = 0 THEN
RAISE EXCEPTION 'The alphabet can''t be undefined or zero!';
END IF;
IF cutoff IS NULL OR cutoff < 1 OR step IS NULL OR step < 1 THEN
RAISE EXCEPTION 'The cutoff and step must be defined and greater than 0!';
END IF;
alphabetArray := regexp_split_to_array(alphabet, '');
alphabetLength := array_length(alphabetArray, 1);
LOOP
bytes := gen_random_bytes(step);
FOR counter IN 0..step - 1
LOOP
randomByte := get_byte(bytes, counter);
IF randomByte < cutoff THEN
idBuilder := idBuilder || alphabetArray[(randomByte % alphabetLength) + 1];
IF length(idBuilder) = size THEN
RETURN idBuilder;
END IF;
END IF;
END LOOP;
END LOOP;
END
$$;
COMMIT;