Back to Graphql Engine

Auditing Actions on Tables in Postgres

docs/docs/schema/postgres/postgres-guides/auditing-tables.mdx

2.48.16812 B
Original Source

Auditing Actions on Tables in Postgres

Typically audit logging is added to some tables to comply with various certifications. You may want to capture the user information (role and the session variables) for every change in Postgres that is done through the GraphQL engine.

For every mutation, Hasura roughly executes the following transaction:

sql
BEGIN;
SET local "hasura.user" = '{"x-hasura-role": "role", ... various session variables}'
SQL related to the mutation
COMMIT;

This information can therefore be captured in any trigger on the underlying tables by using the current_setting function as follows:

sql
current_setting('hasura.user');

We've set up some utility functions that'll let you quickly get started with auditing in this repo.