Postgresql – Ways to implement data versioning in PostreSQL

database-versioningpostgresql

Can you share your thoughts how would you implement data versioning in PostgreSQL. (I've asked similar question regarding Cassandra and MongoDB. If you have any thoughts which db is better for that please share)

Suppose that I need to version records in a simple address book. Address book records are stored in one table without relations for simplicity. I expect that the history:

  • will be used infrequently
  • will be used all at once to present it in a "time machine" fashion
  • there won't be more versions than few hundred to a single record.
  • history won't expire.

I'm considering the following approaches:

  • Create a new object table to store history of records with a copy of schema of addressbook table and add timestamp and foreign key to address book table.

  • Create a kind of schema less table to store changes to address book records. Such table would consist of: AddressBookId, TimeStamp, FieldName, Value. This way I would store only changes to the records and I wouldn't have to keep history table and address book table in sync.

  • Create a table to store seralized (JSON) address book records or changes to address book records. Such table would looks as follows: AddressBookId, TimeStamp, Object (varchar).
    Again this is schema less so I wouldn't have to keep the history table with address book table in sync.
    (This is modelled after Simple Document Versioning with CouchDB)

Best Answer

I do something like your second approach: have the table with the actual working set and a history with changes (timestamp, record_id, property_id, property_value). This includes the creation of records. A third table describes the properties (id, property_name, property_type), which helps in data conversion higher up in the application. So you can also track very easily changes of single properties.

Instead of a timestamp you could also have an int-like, wich you increment for every change per record_id, so you have an actual version.

Related Topic