Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Added images

Ramiro Nuñez Dosio0ad4ab7669dc
diff --git a/apps/www/_blog/2022-09-07-choosing-a-postgres-primary-key.mdx b/apps/www/_blog/2022-09-07-choosing-a-postgres-primary-key.mdx
index 27e10462c78..02f01972116 100644
--- a/apps/www/_blog/2022-09-07-choosing-a-postgres-primary-key.mdx
+++ b/apps/www/_blog/2022-09-07-choosing-a-postgres-primary-key.mdx
@@ -2,8 +2,8 @@
title: Choosing a Postgres Primary Key
description: Turns out the question of which identifier to use as a Primary Key is complicated -- we're going to dive into some of the complexity and inherent trade-offs, and figure things out
author: victor
-image: seen-by/seen-by-postgresql-thumb.png
-thumb: seen-by/seen-by-postgresql-thumb.png
+image: primary-keys/choosing-a-postgres-primary-key-og.jpg
+thumb: primary-keys/choosing-a-postgres-primary-key-og.jpg
tags:
- postgres
date: '2022-09-07'
@@ -59,15 +59,7 @@ SELECT COUNT(*) + 1, '[email protected]', 'new_user' FROM users;
After running that query, we can double check our results:
-```sql
-SELECT * FROM users;
-
- id | email | name
-----+------------------+----------
- 1 | [email protected] | new_user
- 2 | [email protected] | new_user
-(2 rows)
-```
+![Result from the query](/images/blog/primary-keys/results-from-a-query.jpg)
Using `COUNT(*)` in our query is not the most efficient (or even easiest) solution though, and hopefully it's clear why -- **counting a sequence of numbers for primary keys is a feature built in to Postgres**!
@@ -94,17 +86,7 @@ CREATE TABLE users (
OK, now let's try inserting into it - we shouldn't have to specify `id`:
-```sql
-INSERT INTO users_serial (email, name) VALUES ('[email protected]', 'new user');
-
-SELECT * FROM users;
-
- id | email | name
-----+------------------+----------
- 1 | [email protected] | new user
-(1 row)
-INSERT 0 1
-```
+![Insert into query](/images/blog/primary-keys/query-and-result.jpg)
It works, as you might expect - now the application doesn't have to *somehow magically know* the right ID to use when inserting.
@@ -369,21 +351,7 @@ SELECT COUNT(idkit_ksuid_generate()) FROM generate_series(1, 1000000);
Running all of the ID generation mechanisms on a *single core* of my machine (which happens to be an [Oryx Pro](https://system76.com/laptops/oryx)), the lowest of 5 runs for each ID looks like this:
-```
-| ID type | Runtime (ms) | Notes (if present) |
-|-----------------------------------|--------------|----------------------------|
-| `serial` (baseline) | `511.925` | |
-| `xid` | `1017.814` | |
-| `ulid` | `2816.970` | |
-| `timeflake` | `2835.145` | |
-| `uuid_v6` | `3117.796` | |
-| `uuid_v7` | `3374.221` | |
-| `gen_random_uuid` (UUID baseline) | `3886.347` | |
-| `ksuid` | `4386.010` | CPU fans go whirr |
-| `cuid` | `8446.882` | CPU fans go whirr |
-| `nanoid` | `19718.094` | CPU fans go whirr |
-| `pushid` | `45757.820` | suboptimal implementation? |
-```
+!(Generation speed test)[/images/blog/primary-keys/generation-speed-chart.jpg]
To be fair, **generation speed shouldn’t be a deal breaker** as it’s unlikely to be the bottle neck for most applications. That said, it is nice to have some data on where each ID generation mechanism lands.
@@ -410,20 +378,7 @@ ORDER BY pg_total_relation_size(relid) DESC;
Here are the sizes in tabular form:
-```
-| Table name | Table size (mb) | Index size (mb) | Total size (mb) |
-|---------------------|-----------------|-----------------|-----------------|
-| baseline | `89` | `39` | `110` |
-| benchmark_xid | `104` | `39` | `143` |
-| benchmark_nanoid | `104` | `39` | `143` |
-| benchmark_pushid | `104` | `39` | `143` |
-| benchmark_ulid | `112` | `47` | `159` |
-| benchmark_cuid | `112` | `47` | `159` |
-| benchmark_uuidv7 | `120` | `56` | `177` |
-| benchmark_uuidv6 | `120` | `56` | `177` |
-| benchmark_timeflake | `120` | `56` | `177` |
-| benchmark_ksuid | `128` | `65` | `177` |
-```
+![Sizes in tabular form](/images/blog/primary-keys/table-and-index-size-chart.jpg)
These numbers are *mostly* a reflection of the *length* of the default settings of `pg_idkit` but probably worth having in front of you anyway.
diff --git a/apps/www/public/images/blog/primary-keys/choosing-a-postgres-primary-key-og.jpg b/apps/www/public/images/blog/primary-keys/choosing-a-postgres-primary-key-og.jpg
new file mode 100644
index 00000000000..12c46b4357b
Binary files /dev/null and b/apps/www/public/images/blog/primary-keys/choosing-a-postgres-primary-key-og.jpg differ
diff --git a/apps/www/public/images/blog/primary-keys/generation-speed-chart.jpg b/apps/www/public/images/blog/primary-keys/generation-speed-chart.jpg
new file mode 100644
index 00000000000..0fa0e9ca2be
Binary files /dev/null and b/apps/www/public/images/blog/primary-keys/generation-speed-chart.jpg differ
diff --git a/apps/www/public/images/blog/primary-keys/query-and-result.jpg b/apps/www/public/images/blog/primary-keys/query-and-result.jpg
new file mode 100644
index 00000000000..6805957e5c2
Binary files /dev/null and b/apps/www/public/images/blog/primary-keys/query-and-result.jpg differ
diff --git a/apps/www/public/images/blog/primary-keys/results-from-a-query.jpg b/apps/www/public/images/blog/primary-keys/results-from-a-query.jpg
new file mode 100644
index 00000000000..56e60ab6a6e
Binary files /dev/null and b/apps/www/public/images/blog/primary-keys/results-from-a-query.jpg differ
diff --git a/apps/www/public/images/blog/primary-keys/table-and-index-size-chart.jpg b/apps/www/public/images/blog/primary-keys/table-and-index-size-chart.jpg
new file mode 100644
index 00000000000..0dc45f0c330
Binary files /dev/null and b/apps/www/public/images/blog/primary-keys/table-and-index-size-chart.jpg differ