One embodiment of the present invention provides a system that selectively
audits accesses to a relational database. The system receives a query at
the relational database and modifies the query so that processing the
query causes an audit record to be created and recorded for rows in
relational tables that are accessed by the query and that satisfy an
auditing condition. Next, the system processes the modified query to
produce a query result. This processing causes an audit record to be
created for rows in relational tables that are accessed by the query and
that satisfy the auditing condition. The system records the audit record
in an audit record store, and then returns the query result. In one
embodiment of the present invention, if the query includes a select
statement, the system inserts a case statement into the select statement.
This case statement calls a function that causes the audit record to be
created and recorded if the auditing condition is satisfied. In a
variation on this embodiment, the case statement is evaluated near the
end of the query processing so that the case statement is evaluated only
after other conditions of the query are satisfied. In this way, an audit
record is created only for rows that are actually accessed by the query.