---
title: "Postgres Auditing in 150 lines of SQL"
description: "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()."
url: "https://swe.quest/articles/postgres-auditing-in-150-lines-of-sql-856d18202de453da"
canonical: "https://supabase.com/blog/postgres-audit"
source: "Supabase"
published: "2022-03-08"
difficulty: "medium"
kind: "How-to"
reading_minutes: 7
tags: ["postgres","sql","databases"]
---

# Postgres Auditing in 150 lines of SQL

A post on [Supabase](https://swe.quest/sources/supabase.md), published 2022-03-08, 7 min read, How-to. Original: [https://supabase.com/blog/postgres-audit](https://supabase.com/blog/postgres-audit)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- PostgreSQL triggers ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- PL/pgSQL
- JSONB
- Postgres indexes ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

PostgreSQL, JSONB, BRIN index, UUID v5, supa\_audit, pgAudit, Supabase

## Related posts

- [Supabase Storage: major performance, security, and reliability updates](https://swe.quest/articles/supabase-storage-major-performance-security-and-reliability-updates-2ffa30c3ec95bba1.md): Supabase · 2026-03-05 · Medium · 4 min read · Storage systems, PostgreSQL, Performance engineering, Security, Observability
  Supabase 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.
- [Supabase is now on Postgres 13.3](https://swe.quest/articles/supabase-is-now-on-postgres-13-3-2931acd8ef3ece75.md): Supabase · 2021-07-26 · Easy · 3 min read · PostgreSQL, Performance engineering
  New 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 Vault](https://swe.quest/articles/supabase-vault-3db5f31fada1a63f.md): Supabase · 2022-08-19 · Medium · 5 min read · PostgreSQL, Cryptography, Security
  Supabase 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.
- [Cracking PostgreSQL Interview Questions](https://swe.quest/articles/cracking-postgresql-interview-questions-e9efeda89524a92a.md): Supabase · 2021-02-27 · Medium · 6 min read · PostgreSQL, SQL, Databases, Identity & auth, Engineering culture
  Interview preparation for the transactional side of PostgreSQL, organized around five areas: data modeling, indexes, views, roles and functions.
- [SQL or NoSQL? Why not use both (with PostgreSQL)?](https://swe.quest/articles/sql-or-nosql-why-not-use-both-with-postgresql-98c239e2ff7e761c.md): Supabase · 2022-11-24 · Medium · 12 min read · PostgreSQL, SQL, Databases
  PostgreSQL'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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
