Postgres Auditing in 150 lines of SQL
A generic table-auditing system can be built inside PostgreSQL with about 150 lines of SQL: a single append-only audit.record_version table stores each row's old and new contents as JSONB, populated by a per-row trigger enabled via audit.enable_tracking(). Stable record IDs are derived by hashing the table OID plus primary-key values into a UUID v5, so a row's history can be queried by an indexed UUID. A BRIN index on the timestamp and a B-tree on table_oid keep time-range and per-table queries fast. Overhead is typically negligible under ~1000 writes/sec; higher-throughput tables should consider pgAudit, and the code is packaged as the supa_audit extension.
Related posts
- Supabase Storage: major performance, security, and reliability updates
Supabase mediumAdeptSupabase Storage replaces its trigger-maintained prefixes table with a hybrid skip-scan algorithm that derives folder structure at read time, and switches from OFFSET to cursor-based pagination, making deep pagination up to 14.8x faster on 60 million rows with no write penalty. The release also closes a file-backend path traversal bug and blocks direct SQL DELETEs on storage tables that orphaned S3 objects. It adds Postgres query cancellation on client disconnect, idempotent migrations verified by pg_dump comparison, a fix for a TUS S3 lock renewal race, and a move from prom-client to OpenTelemetry metrics. No code changes are required, though switching to the listV2 endpoint is recommended.
- Supabase is now on Postgres 13.3
Supabase easyNoviceNew Supabase projects now run Postgres 13.3 (up from 12.0), bringing native gen_random_uuid, parallel index vacuuming, incremental sorting and smaller B-tree indexes. Supabase Postgres versioning now tracks upstream major/minor versions, and Postgres is built from source so ARM Graviton 2 instances can use Large System Extensions (LSE) for faster atomics and locking, with pgbench comparisons on m6gd.8xlarge. The release also moves to Ubuntu 20.04, adds the pgRouting and wal2json extensions, configures fail2ban on ports 5432 and 6543 (10-minute ban after three failed attempts), and previews PgBouncer/PostgREST marketplace bundles.
- Supabase Vault
Supabase mediumAdeptSupabase Vault is a Postgres extension, built as a thin layer over pgsodium and libsodium, that stores secrets such as API keys and access tokens encrypted on disk and in backups. Unlike pgcrypto, which requires passing raw keys through SQL where they can leak into logs, replication streams and clients, Vault references keys by ID and derives them from a per-database root key held outside SQL by pgsodium's Server Key Management. Secrets go into the vault.secrets table and are decrypted on the fly through the vault.decrypted_secrets view. Encryption uses AEAD, so the associated-data column is authenticated alongside the secret, and custom keys can be created with pgsodium.create_key().
- Cracking PostgreSQL Interview Questions
Supabase mediumAdeptInterview preparation for the transactional side of PostgreSQL, organized around five areas: data modeling, indexes, views, roles and functions. It covers modeling 1-1, 1-M and M-M relationships with foreign keys and join tables, and using rich types like JSONB, enums and timestamptz. On performance and access control it explains B-tree and other index types, comparing plans with EXPLAIN ANALYZE, auto-updatable views, GRANTs to PUBLIC, and row level security policies. It also shows PL/pgSQL trigger functions for auto-updating timestamps, advises thinking in sets rather than loops, and introduces CTEs, all with short SQL examples.
- SQL or NoSQL? Why not use both (with PostgreSQL)?
Supabase mediumAdeptPostgreSQL's JSONB columns let an app store nested, variable-length data (here, a daily food, water and exercise log) as a single row instead of spreading it across several normalized tables, while keeping SQL available for analysis. A worked example builds a calendar table with JSONB log fields and inserts a whole day in one statement. It then uses jsonb_array_elements with the -> and ->> operators to flatten the arrays into rows. Because aggregates cannot wrap set-returning functions, the flattened query goes into a CTE before summing daily calories or filtering for specific items.