You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -33,7 +33,7 @@ SELECT id FROM users; -- user_2accvpp5guht4dts56je5a
33
33
SELECT id FROM users WHERE id ='user_2accvpp5guht4dts56je5a';
34
34
```
35
35
36
-
Plays nice with your server code too, no extra work needed:
36
+
Plays nice with your server code, no extra work needed:
37
37
```python
38
38
with psycopg.connect("postgresql://...") as conn:
39
39
res = conn.execute("SELECT id FROM users").fetchone()
@@ -79,7 +79,7 @@ Key changes relative to ULID:
79
79
```
80
80
81
81
### Collision
82
-
Relative to ULID, the time precision is reduced from 48 to 40 bits (keeping the most significant bits, so oveflow still won't occur until 10889 AD), and the randomness reduced from 80 to 64 bits.
82
+
Relative to ULID, the time precision is reduced from 48 to 40 bits (keeping the most significant bits, so overflow still won't occur until 10889 AD), and the randomness reduced from 80 to 64 bits.
83
83
84
84
The timestamp precision at 40 bits is around 250 milliseconds. In order to have a 50% probability of collision with 64 bits of randomness, you would need to generate around **4 billion items per 250 millisecond window**.
Code and tests are in the [py/](./py/) directory. Using [Rye](https://rye.astral.sh/) for development (installation instructions at the link).
140
+
Code and tests are in the [py/](./py/) directory.
141
+
Using [Rye](https://rye.astral.sh/) for development (installation instructions at the link).
109
142
110
143
```bash
111
144
# can be run from the repo root
@@ -118,6 +151,8 @@ If you just want to have a look around, pip should also work:
118
151
pip install -e .
119
152
```
120
153
154
+
Please open a PR if you spot a bug or improvement!
155
+
121
156
## Rust implementation
122
157
The current Rust implementation is based on [dylanhart/ulid-rs](https://github.com/dylanhart/ulid-rs), but using the same lookup base32 lookup method as the Python implementation.
Code and tests are in the [upid_rs/](./upid_rs/) directory.
137
197
@@ -140,48 +200,80 @@ cd upid_rs
140
200
cargo check # or fmt/clippy/build/test/run
141
201
```
142
202
203
+
Please open a PR if you spot a bug or improvement!
204
+
143
205
## Postgres extension
144
206
There is also a Postgres extension built on the Rust implementation, using [pgrx](https://github.com/pgcentralfoundation/pgrx) and based on the very similar extension [pksunkara/pgx_ulid](https://github.com/pksunkara/pgx_ulid).
145
207
146
208
#### Installation
147
-
You can try out the Docker image [carderne/postgres-upid:16](https://hub.docker.com/r/carderne/postgres-upid):
209
+
The easiest would be to try out the Docker image [carderne/postgres-upid:16](https://hub.docker.com/r/carderne/postgres-upid), currently built for arm64 and amd64 but only for Postgres 16:
148
210
```bash
149
211
docker run -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 carderne/postgres-upid:16
150
212
```
151
213
152
-
If you want to install it into another Postgres, you'll install pgrx and follow its [installation instructions](https://github.com/pgcentralfoundation/pgrx/blob/develop/cargo-pgrx/README.md).
153
-
Something like this:
154
-
```bash
155
-
cargo install --locked cargo-pgrx
156
-
pgrx init
157
-
cd upid_pg
158
-
pgrx install
159
-
```
214
+
You can also grab a Linux `.deb` from the [Releases](https://github.com/carderne/upid/releases) page. This is built for Postgres 16 and amd64 only.
160
215
161
-
Installable binaries will come soon.
216
+
More architectures and versions will follow once it is out of alpha.
162
217
163
218
#### Usage
164
219
```sql
165
-
CREATE EXTENSION ulid;
166
-
220
+
CREATE EXTENSION upid_pg;
167
221
168
222
CREATETABLEusers (
169
223
id upid NOT NULL DEFAULT gen_upid('user') PRIMARY KEY,
170
224
name textNOT NULL
171
225
);
226
+
172
227
INSERT INTO users (name) VALUES('Bob');
228
+
173
229
SELECT*FROM users;
230
+
-- id | name
231
+
-- -----------------------------+------
232
+
-- user_2accvpp5guht4dts56je5a | Bob
174
233
```
175
234
176
-
#### Development
177
-
Code and tests are in the [upid_pg/](./upid_pg/) directory.
235
+
You can get the raw `bytea` data, or the prefix or timestamp:
236
+
```sql
237
+
SELECT upid_to_bytea(id) FROM users;
238
+
-- \x019...
178
239
240
+
SELECT upid_to_prefix(id) FROM users;
241
+
-- 'user'
242
+
243
+
SELECT upid_to_timestamp(id) FROM users;
244
+
-- 2024-07-07 ...
245
+
```
246
+
247
+
You can convert a `UPID` to a regular Postgres `UUID`:
248
+
```sql
249
+
SELECT upid_to_uuid(gen_upid('user'));
250
+
```
251
+
252
+
Or the reverse (although the prefix and timestamp will no longer make sense):
253
+
```sql
254
+
select upid_from_uuid(gen_random_uuid());
255
+
```
256
+
257
+
#### Development
258
+
If you want to install it into another Postgres, you'll install pgrx and follow its [installation instructions](https://github.com/pgcentralfoundation/pgrx/blob/develop/cargo-pgrx/README.md).
259
+
Something like this:
179
260
```bash
180
261
cd upid_pg
262
+
cargo install --locked cargo-pgrx
263
+
cargo pgrx init
264
+
cargo pgrx install
265
+
```
266
+
267
+
Some `cargo` commands work as normal:
268
+
```bash
181
269
cargo check # or fmt/clippy
270
+
```
271
+
272
+
But building, testing and running must be done via pgrx.
273
+
This will compile it into a Postgres installation, and allow an interactive session and tests there.
182
274
183
-
# must test/run/install with pgrx
184
-
# this will compile it into a Postgres installation
185
-
#and run the tests there, or drop you into a psql prompt
0 commit comments