Quick revision
The short answer
In PostgreSQL we can audit with the pgAudit extension (session and object auditing), the log_statement setting, or triggers that write changes to audit tables; in MySQL, with an audit log plugin (enterprise or third-party, depending on the edition) and the binary log for data changes. Database logs show which database role ran which statement, but usually not which end user, because applications share pooled accounts. Application audit trails record the business user, the action and the reason, for example with Hibernate Envers or explicit audit events, so we use both, correlate them with request ids, and protect the audit data from change.
What to remember
In this answer, we looked at the auditing options in both databases: log_statement, pgAudit session and object auditing, and triggers in PostgreSQL, and audit log plugins and the binary log in MySQL. We saw that database logs show the pooled database role and the statement but not the end user or reason, while application audit trails, through Hibernate Envers or explicit events, record the person, action and reason. We use both, link them with a request id, and keep the audit data in protected storage.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In PostgreSQL we can audit with the pgAudit extension (session and object auditing), the log_statement setting, or triggers that write changes to audit tables; in MySQL, with an audit log plugin (enterprise or third-party, depending on the edition) and the binary log for data changes. Database logs show which database role ran which statement, but usually not which end user, because applications share pooled accounts. Application audit trails record the business user, the action and the reason, for example with Hibernate Envers or explicit audit events, so we use both, correlate them with request ids, and protect the audit data from change.
Why this question is asked
Sooner or later someone asks a question like who looked at this customer's record last month, or who changed this price and why. If we cannot answer, the problem is no longer technical; it is a compliance failure, and sometimes a legal one. Interviewers ask this question to see whether we know the auditing tools of the databases we use and, more importantly, understand what each kind of log can and cannot tell us. In this answer, we will recap what auditing means, look at the options in PostgreSQL and MySQL, explain the end-user gap in database logs, cover application audit trails, and then join the two with request ids.
1A Quick Recap of Auditing
An audit log is a record of who did what, to which data, and when, kept so that it can be trusted later. It differs from ordinary application logs in two ways: it must be complete for the actions it covers, and it must be protected from being changed or deleted, including by the people it records. Auditing reads matters as much as auditing changes for sensitive data, because many privacy incidents are someone looking at records they had no reason to see.
2Auditing in PostgreSQL
PostgreSQL has three main options. The simplest is the log_statement setting, which writes statements to the server log: ddl logs schema changes, mod adds data changes, all logs everything. It is easy, but it logs whole statements with their text, it cannot be limited to sensitive tables, and with all it is very noisy.
The pgAudit extension is built for auditing. Session auditing logs classes of statements, such as READ, WRITE or DDL, for all sessions or for chosen roles. Object auditing logs only statements that touch specific tables, chosen by granting privileges on those tables to a dedicated auditor role. Each entry is structured, with the statement class, the object and the statement text.
The third option is triggers: a row-level trigger on a table writes the old and new values, the time and current_user into an audit table on every change. Triggers capture data changes precisely and keep them queryable in SQL, but they cannot see reads, they add cost to every write, and anyone who can disable the trigger can avoid it.
3Auditing in MySQL
MySQL's audit options depend on the edition. MySQL Enterprise Edition includes an audit log plugin with filters by user, event and table, and Percona Server and MariaDB ship their own audit plugins; the community edition of MySQL has no built-in one, so check your version and distribution. These plugins record connections and statements, including reads.
For data changes there is also the binary log, which MySQL writes for replication. In row-based format it records the actual before and after values of each changed row, and with binlog_rows_query_log_events it can include the original statement. Tools can read it to reconstruct changes. But it never records reads, and its retention is usually set for replication and recovery, a few days, not for audit, so it is evidence of last resort rather than an audit trail.
4What Database Logs Cannot Tell Us
Here is the gap that matters most. A Spring Boot service connects through a connection pool with one database account, say app_rw. When support agent Alice opens a customer record, the database sees app_rw run a SELECT. When Bob opens another one a second later, the database again sees app_rw. So database audit logs tell us which service accessed data and what statement it ran, but not which person was behind it, and nothing about why.
| Question | Database audit log | Application audit trail |
|---|---|---|
| Which person did it? | no, only the pooled role | yes, the authenticated user |
| Why? (ticket, reason) | no | yes, if the action asks for it |
| Exact statement and tables | yes | usually not |
| Access outside the application | yes, any client | no |
| Business meaning of the change | no, rows and columns | yes, e.g. bank details changed |
5Application Audit Trails
The application knows the authenticated user, the business action and often the reason, so it can write an audit trail with that meaning. Hibernate Envers does this at the entity level: annotate an entity with @Audited and Envers writes each version into a history table, with a revision entity where we can add the user name through a revision listener. That is good for answering what did this record look like on a date. Explicit audit events, written by our own code, are better for business-level records such as agent Alice viewed bank details of customer 42 for ticket 9134, including reads, which Envers does not capture.
6Using Both and Correlating Them
We get the full picture by linking the two with a request id, a unique id created when the request enters the system and passed through every call. The application writes it into its audit events, and we can put it into the database session, for example by setting application_name for the transaction or adding it as a SQL comment, so that it appears in pgAudit entries through log_line_prefix. An investigator can then start from Alice's event and find the exact statements, or start from a suspicious statement and find the person.
Finally, the audit data itself must be protected. Logs are shipped quickly to a separate store that database administrators and application roles cannot modify, with append-only or write-once settings, and access to the audit store is itself logged.
7Follow-up questions
Is log_statement = 'all' a good audit solution?
Not usually. It logs everything without structure, so the volume is huge and the sensitive events are hard to find, and it may write sensitive values from statement text into general server logs that many people can read. pgAudit's object auditing on the sensitive tables is far more targeted.
Why not give every employee their own database account so the database knows the person?
For direct human access that is exactly right, ideally through temporary SSO-based accounts. For application traffic, per-user database connections would destroy connection pooling and multiply accounts, so we keep pooled accounts and carry the user identity in application audit events and request ids instead.
How long should audit logs be kept?
As long as the regulation or contract requires for that data, which differs by industry and country, and never shorter than the time it typically takes to discover an incident. We define retention per data class and enforce it in the audit store, including legal holds.
8Conclusion
In this answer, we looked at the auditing options in both databases: log_statement, pgAudit session and object auditing, and triggers in PostgreSQL, and audit log plugins and the binary log in MySQL. We saw that database logs show the pooled database role and the statement but not the end user or reason, while application audit trails, through Hibernate Envers or explicit events, record the person, action and reason. We use both, link them with a request id, and keep the audit data in protected storage.
Open with
In PostgreSQL there are three main options. log_statement can log DDL, modifications or everything to the server log, but it is coarse and noisy. The pgAudit extension is built for this: session auditing logs classes like reads, writes, DDL and role changes, and object auditing logs only statements on chosen sensitive tables through an auditor role.
Then, if they want more
Triggers can also write old and new values into audit tables, but they miss reads and can be disabled. In MySQL, audit log plugins exist in Enterprise Edition and in distributions like Percona and MariaDB, and the binary log in row format shows data changes, but not reads and usually with short retention.
The key difference with application audit trails is identity. A Spring service uses one pooled account, so the database only knows that app_rw ran a statement, not which user or why.
The application knows the authenticated user, the business action and the reason, so it writes audit events, with Hibernate Envers for entity versions or explicit events for business actions, including reads of sensitive data.
I would use both, correlate them with a request id that is in the application event and in the database session, for example via application_name or a SQL comment, and ship audit data to protected append-only storage.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What options exist to audit who changed or read sensitive data in PostgreSQL and MySQL...": https://scrivio-interview-library.netlify.app/databases/database-security/f360a1b890/