title: Dynamic auditing of data with PostgreSQL trigger
body: <p>I'm interested in using the following audit mechanism in an existing PostgreSQL database.</p>
  
  <p><a href="http://wiki.postgresql.org/wiki/Audit_trigger" rel="nofollow">http://wiki.postgresql.org/wiki/Audit_trigger</a></p>
  
  <p>but, would like (if possible) to make one modification.  I would also like to log the primary_key's value where it could be queried later.  So, I would like to add a field named something like "record_id" to the "logged_actions" table.  The problem is that every table in the existing database has a different primary key fieldname.  The good news is that the database has a very consistent naming convention.  It's always, _id.  So, if a table was named "employee", the primary key is "employee_id".  </p>
  
  <p>Is there anyway to do this?  basically, I need something like OLD.FieldByName(x) or OLD[x] to get value out of the id field to put into the record_id field in the new audit record.</p>
  
  <p>I do understand that I could just create a separate, custom trigger for each table that I want to keep track of, but it would be nice to have it be generic.</p>
  
  <p>edit: I also understand that the key value does get logged in either the old/new data fields.  But, what I would like would be to make querying for the history easier and more efficient.  In other words, </p>
  
  <pre><code>select * from audit.logged_actions where table_name = 'xxxx' and record_id = 12345;
  </code></pre>
  
  <p>another edit: I'm using PostgreSQL 9.1</p>
  
  <p>Thanks!</p>