Skip to content

About

Generic postgres table auditing

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

78 Commits

Folders and files

Repository files navigation

supi_audit

PostgreSQL version Tests


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_id instead of deriving a v5 UUID from table oid + PK values.
  • Strict auth: If auth.uid() exists at migration time, the added auth_uid column is not null and thus requires auth.uid() to return a uuid.
  • Initial SNAPSHOT: enable_tracking has an optional second argument take_snapshot (default false) which, if set to true, will insert the current contents of the table into audit.record_version as SNAPSHOT rows.
  • 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.

Usage

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);

Auth

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.

Snapshot rows

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);

Test

Run the Tests

./scripts/run-sql-tests.sh

Adding Tests

Tests 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.

Performance

Write Throughput

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.

Querying

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;

About

Generic postgres table auditing

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Contributors

Languages