<p>What are some strategies that people have had success with for maintaining a change history for data in a fairly complex database. One of the applications that I frequently use and develop for could really benefit from a more comprehensive way of tracking how records have changed over time. For instance, right now records can have a number of timestamp and modified user fields, but we currently don't have a scheme for logging multiple change, for instance if an operation is rolled back. In a perfect world, it would be possible to reconstruct the record as it was after each save, etc.</p>

<p>Some info on the DB:</p>

<ul>
<li>Needs to have the capacity to grow by thousands of records per week</li>
<li>50-60 Tables</li>
<li>Main revisioned tables may have several million records each</li>
<li>Reasonable amount of foreign keys and indexes set</li>
<li>Using PostgreSQL 8.x</li>
</ul>

[{'answer_id': 23777, 'author': 'mk.', 'author_id': 1797, 'author_profile': 'https://Stackoverflow.com/users/1797', 'pm_score': 5, 'selected': True, 'text': '<p>In the past I have used triggers to construct db update/insert/delete logging. </p>\n\n<p>You could insert a record each time one of the above actions is done on a specific table into a logging table that keeps track of the action, what db user did it, timestamp, table it was performed on, and previous value. </p>\n\n<p>There is probably a better answer though as this would require you to cache the value before the actual delete or update was performed I think. But you could use this to do rollbacks. </p>\n'}, {'answer_id': 23780, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 5, 'selected': False, 'text': '<p>One strategy you could use is MVCC, Multi-Value Concurrency Control.  In this scheme, you never do updates to any of your tables, you just do inserts, maintaining version numbers for each record.  This has the advantage of providing an exact snapshot from any point in time, and it also completely sidesteps the update lock problems that plague many databases.</p>\n\n<p>But it makes for a huge database, and selects all require an extra clause to select the current version of a record.</p>\n'}, {'answer_id': 23825, 'author': 'Martin Klinke', 'author_id': 1793, 'author_profile': 'https://Stackoverflow.com/users/1793', 'pm_score': 4, 'selected': False, 'text': '<p>If you are using Hibernate, take a look at <a href="http://www.jboss.org/envers/" rel="nofollow noreferrer">JBoss Envers</a>. From the project homepage:</p>\n\n<blockquote>\n  <p>The Envers project aims to enable easy versioning of persistent JPA classes. All that you have to do is annotate your persistent class or some of its properties, that you want to version, with @Versioned. For each versioned entity, a table will be created, which will hold the history of changes made to the entity. You can then retrieve and query historical data without much effort. </p>\n</blockquote>\n\n<p>This is somewhat similar to <a href="https://stackoverflow.com/questions/23770/good-strategy-for-leaving-an-audit-trailchange-history-for-db-applications#23780">Eric\'s approach</a>, but probably much less effort. Don\'t know, what language/technology you use to access the database, though.</p>\n'}, {'answer_id': 65259, 'author': 'amitm', 'author_id': 9149, 'author_profile': 'https://Stackoverflow.com/users/9149', 'pm_score': 2, 'selected': False, 'text': '<p>The only problem with using Triggers is that it adds to performance overhead of any insert/update/delete. For higher scalability and performance, you would like to keep the database transaction to a minimum. Auditing via triggers increase the time required to do the transaction and depending on the volume may cause performance issues. </p>\n\n<p>another way is to explore if the database provides any way of mining the "Redo" logs as is the case in Oracle. Redo logs is what the database uses to recreate the data in case it fails and has to recover. </p>\n'}, {'answer_id': 65393, 'author': 'Jeff Davis', 'author_id': 9249, 'author_profile': 'https://Stackoverflow.com/users/9249', 'pm_score': 2, 'selected': False, 'text': '<p>Similar to a trigger (or even with) you can have every transaction fire a logging event asynchronously and have another process (or just thread) actually handle the logging. There would be many ways to implement this depending upon your application. I suggest having the application fire the event so that it does not cause unnecessary load on your first transaction (which sometimes leads to locks from cascading audit logs). </p>\n\n<p>In addition, you may be able to improve performance to the primary database by keeping the audit database in a separate location.</p>\n'}, {'answer_id': 71915, 'author': 'Chris Burgess', 'author_id': 6624, 'author_profile': 'https://Stackoverflow.com/users/6624', 'pm_score': 2, 'selected': False, 'text': '<p>I use SQL Server, not PostgreSQL, so I\'m not sure if this will work for you or not, but Pop Rivett had a great article on creating an audit trail here:\n<a href="http://www.simple-talk.com/sql/database-administration/pop-rivetts-sql-server-faq-no.5-pop-on-the-audit-trail/" rel="nofollow noreferrer">Pop rivett\'s SQL Server FAQ No.5: Pop on the Audit Trail</a></p>\n\n<p>Build an audit table, then create a trigger for each table you want to audit. </p>\n\n<p>Hint: use <a href="http://www.codesmithtools.com/" rel="nofollow noreferrer">Codesmith</a> to build your triggers.</p>\n'}]
['https://Stackoverflow.com/questions/23770', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2567/']