With the pgAudit extension on PostgreSQL, how do session and object audit logging differ, and why is log_statement = 'all' not enough for an audit?
answer
- what was requested vs what happened
- classes for the session
- grants to an audit role
- AUDIT: lines, CSV-shaped
basics
~20 slog_statement records only the text a client sent, so dynamic SQL and substatements are lost. pgAudit session logging records classes of statements (pgaudit.log = 'read, write, ddl'). Object logging records SELECT and DML on whatever the pgaudit.role role has been granted.
solid answer
~40 s`log_statement = 'all'` logs what the client *requested*. A `DO` block that builds `CREATE TABLE` with `EXECUTE` appears only as the `DO` text, and you cannot search the log by table. pgAudit writes `AUDIT:` lines with type, statement and substatement IDs, class, command, object type, fully qualified object name and statement. **Session** logging is set with `pgaudit.log` to classes such as `READ`, `WRITE`, `FUNCTION`, `ROLE`, `DDL`, `MISC` or `ALL`, and you can subtract one with `-`. It audits everything in those classes. **Object** logging uses the permission system: you name an audit role in `pgaudit.role` and grant it, say, `SELECT (passport_number)` on `passengers`. Only `SELECT`, `INSERT`, `UPDATE` and `DELETE` that touch those grants are logged, `TRUNCATE` excluded. pgAudit cannot reliably audit superusers.
code
sql · 7 linesCREATE EXTENSION pgaudit;
ALTER SYSTEM SET pgaudit.log = 'ddl, role';
CREATE ROLE booking_auditor NOLOGIN;
ALTER SYSTEM SET pgaudit.role = 'booking_auditor';
SELECT pg_reload_conf();
GRANT SELECT (passport_number) ON passengers TO booking_auditor;
GRANT UPDATE, DELETE ON bookings TO booking_auditor;go deeper
Know that pgAudit is a third-party extension that writes structured AUDIT lines to the normal server log, and that log_statement alone is not considered an audit trail.
Explain session classes in pgaudit.log versus object logging through grants to the pgaudit.role role, and which statement types object logging covers.
Set it up in the right order, explain why settings do not follow role membership, combine ddl/role session logging with column-level object grants, and state the superuser and volume limits.
Decide what the audit must prove and to whom, then choose classes and grants to keep volume affordable, with superuser access restricted rather than assumed to be audited.
## Why plain statement logging falls short `log_statement = 'all'` writes the text of every statement the client sends. That is fine for debugging, but an auditor asks other questions: *who read the passport numbers?* *was the `bookings` table altered outside the maintenance window?* The standard log answers with what the client **requested**, not with what the server **did**: - A `DO` block, or a function that builds SQL with `EXECUTE`, is logged only as the outer text. The `CREATE TABLE` or `DELETE` it ran inside never appears. - A line names no object in a searchable field. Finding every access to one table means parsing SQL text, and access through views and functions defeats that. - There is no classification, so you cannot ask for "all role changes" or "all reads of these tables" without logging everything. The pgAudit extension uses the same logging facility but writes structured lines. Each line is CSV-shaped once the `log_line_prefix` part is stripped: | field | example | |---|---| | AUDIT_TYPE | `SESSION` or `OBJECT` | | STATEMENT_ID, SUBSTATEMENT_ID | `33`, `2` | | CLASS | `DDL` | | COMMAND | `CREATE TABLE` | | OBJECT_TYPE, OBJECT_NAME | `TABLE`, `public.fare_rules` | | STATEMENT | the statement or substatement text | | PARAMETER | values when `pgaudit.log_parameter = on`, else `<not logged>` | The `DO` block that ran a dynamic `CREATE TABLE` now produces two entries: substatement 1 is the `DO`, and substatement 2 is the `CREATE TABLE` with its fully qualified table name. ## Session audit logging Session logging is set with **`pgaudit.log`**, a comma-separated list of classes (default `none`): - `READ`: `SELECT`, and `COPY` from a relation or query - `WRITE`: `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, and `COPY` into a relation - `FUNCTION`: function calls and `DO` blocks - `ROLE`: `GRANT`, `REVOKE` and `CREATE/ALTER/DROP ROLE` - `DDL`: all DDL not in `ROLE` - `MISC` and `MISC_SET`: commands such as `VACUUM`, `CHECKPOINT` and `SET` - `ALL`: everything; prefix a class with `-` to subtract it, as in `'all, -misc'` Useful companions: `pgaudit.log_relation = on` writes one entry per relation a `SELECT` or DML statement touches; `pgaudit.log_catalog` (default `on`) can be turned off to drop noise from tools that query `pg_catalog`; `pgaudit.log_parameter` and `pgaudit.log_rows` add bind values and row counts. ## Object audit logging Object logging is the finer-grained option, meant to replace `pgaudit.log = 'read, write'` for chosen relations. It is driven by the **permission system**. `pgaudit.role` (no default) names an audit role. Any `SELECT`, `INSERT`, `UPDATE` or `DELETE` is logged when that role holds, or inherits, the privilege the statement uses on the relation or column. Column grants make it precise: grant `SELECT (passport_number)` on `passengers`, and only queries that read that column are logged. `TRUNCATE` is not covered. New tables are not covered until someone grants them to the audit role. | | session (`pgaudit.log`) | object (`pgaudit.role`) | |---|---|---| | selects by | statement class | grants to the audit role | | covers | every class, DDL and roles included | `SELECT`, `INSERT`, `UPDATE`, `DELETE` only | | granularity | all relations | named tables or columns | | volume | high with `READ`/`WRITE` | proportional to what is granted | A common combination is session logging for `ddl, role` plus object logging on the sensitive tables. ## Setting it up correctly 1. The library must be preloaded (the extensions topic covers that), and `CREATE EXTENSION pgaudit` must run **before** `pgaudit.log` is set. The extension installs event triggers, and without them DDL lines lack object type and name. 2. Only superusers can change the settings. They can be set globally, per database or per role, but they are **not inherited through role membership**, and `SET ROLE` does not change them. 3. Put user, database, time and PID into `log_line_prefix`, because the `AUDIT:` payload does not carry them. ## Limits to state in an interview - Superusers cannot be audited reliably, because they can change the settings themselves. Restrict superuser use instead. - Autovacuum and autoanalyze are not logged. Statements run after a transaction has entered the aborted state are not audit-logged either. - Volume is real: auditing `WRITE` on a high-insert table can produce more log than data. Keeping the trail tamper-proof and deciding retention are audit-design questions owned by the general auditing topic.
- You add a new payments table next month. Does object audit logging cover it automatically?No. Object logging follows the audit role's grants, so a new relation is audited only after someone grants the relevant privileges to the role in `pgaudit.role`. Teams usually make that grant part of the migration that creates the table, or use session logging for the classes that must never be missed.
- Why should CREATE EXTENSION pgaudit run before pgaudit.log is set?The extension installs event triggers that add object type and name to DDL entries. pgAudit still logs without them, but DDL lines then lack the fields that make them searchable by object. Also, if the extension is dropped and recreated, `pgaudit.log` must be unset first, or an error is raised.
- log_statement = 'all' records every table touched by functions and DO blocks.
- Object audit logging also captures TRUNCATE on the granted tables.
- pgaudit.log set on a group role applies to all its members.
- pgAudit gives a reliable trail of what superusers do.
- Object logging needs no grants: list the tables in pgaudit.role.