Fork: This is a fork of supa_audit with the following changes:
- Simple migration instead of a postgres extension for easier "installation"
- Include the transaction id (
xact_id) in the audit record for correlation of changes - Use single-column UUID PKs directly as
record_idinstead of deriving a v5 UUID from table oid + PK values. - Strict auth: If
auth.uid()exists at migration time, the addedauth_uidcolumn isnot nulland thus requiresauth.uid()to return a uuid. - Initial
SNAPSHOT:enable_trackinghas an optional second argumenttake_snapshot(defaultfalse) which, if set totrue, will insert the current contents of the table intoaudit.record_versionasSNAPSHOTrows. - Test setup with docker-compose and scripts instead of nix
The supi_audit PostgreSQL code is a generic solution for tracking changes to tables' data over time.
The audit table, audit.record_version, leverages each records primary key values to produce a stable record_id::uuid, enabling efficient (linear time) history queries.
Apply the content of supi_audit.sql to your database.
create table public.account(
id int primary key,
name text not null
);
-- Enable auditing
select audit.enable_tracking('public.account'::regclass); -- no initial snapshot
-- Insert a record
insert into public.account(id, name)
values (1, 'Foo Barsworth');
-- Update a record
update public.account
set name = 'Foo Barsworht III'
where id = 1;
-- Delete a record
delete from public.account
where id = 1;
-- Truncate the table
truncate table public.account;
-- Review the history
select
*
from
audit.record_version;
/*
id | record_id | old_record_id | op | ts | table_oid | table_schema | table_name | xact_id | record | old_record
----+--------------------------------------+--------------------------------------+----------+-------------------------------+-----------+--------------+------------+---------+----------------------------------------+----------------------------------------
9 | 36c93923-0c5c-5fc2-879d-e4aa264b8f6e | | INSERT | 2025-09-23 21:44:19.437243+00 | 19544 | public | account | 1387 | {"id": 1, "name": "Foo Barsworth"} |
10 | 36c93923-0c5c-5fc2-879d-e4aa264b8f6e | 36c93923-0c5c-5fc2-879d-e4aa264b8f6e | UPDATE | 2025-09-23 21:44:19.44223+00 | 19544 | public | account | 1388 | {"id": 1, "name": "Foo Barsworht III"} | {"id": 1, "name": "Foo Barsworth"}
11 | | 36c93923-0c5c-5fc2-879d-e4aa264b8f6e | DELETE | 2025-09-23 21:44:19.44503+00 | 19544 | public | account | 1389 | | {"id": 1, "name": "Foo Barsworht III"}
12 | | | TRUNCATE | 2025-09-23 21:44:19.44763+00 | 19544 | public | account | 1390 | |
(4 rows)
*/
-- Disable auditing
select audit.disable_tracking('public.account'::regclass);If a function auth.uid() and auth.role() exists at the time of running the migration, the audit.record_version table will have the columns auth_uid and auth_role which will be populated with the result of calling these functions at the time of the data change.
On supabase, these functions already exist. Outside of supabase, you can create them like demonstrated in auth.sql. auth.uid() contains a custom error message if the user id is not set.
See the test auth.sql.
When calling audit.enable_tracking(table, true) the current contents of the table are inserted into audit.record_version with the operation SNAPSHOT. These rows act as a baseline so that a table that already contains data has a starting point in the audit history.
Snapshots are optional and disabled by default (take_snapshot defaults to false) to keep enable_tracking fast for large tables. You can always capture a snapshot manually later by temporarily disabling and re‑enabling tracking with take_snapshot => true.
-- Enable auditing and capture a snapshot of existing rows
select audit.enable_tracking('public.account'::regclass, true);
-- Enable auditing without snapshot (default)
select audit.enable_tracking('public.account'::regclass);./scripts/run-sql-tests.shTests are located in test/sql/ and the expected output is in test/expected/
The output of the most recent test run is stored in results/.
When the output for a test in results/ is correct, copy it to test/expected/ and the test will pass.
Auditing tables reduces throughput of inserts, updates, and deletes.
It is not recommended to enable tracking on tables with a peak write throughput over 3k ops/second.
When querying a table's history, filter on the indexed table_oid rather than the table_name and schema_name columns.
select
*
from
audit.record_version
where
table_oid = 'public.account'::regclass::oid;