Piotr Czapla
Piotr Czapla

Reputation: 26512

Ways to implement data versioning in PostreSQL

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:

I'm considering the following approaches:

Upvotes: 16

Views: 9861

Answers (3)

vonPetrushev
vonPetrushev

Reputation: 5599

I'm versioning glossary data, and my approach was pretty successful for my needs. Basically, for records you need versioning, you divide the fieldset into persistent fields and version-dependent fields, thus creating two tables. Some of the first set should also be the unique key for the first table.

Address
id [pk]
fullname [uk]
birthday [uk]

Version
id [pk]
address_id [uk]
timestamp [uk]
address

In this fashion, you get an address subjects determined by fullname and birthday (should not change by versioning) and a versioned records containing addresses. address_id should be related to Address:id through foreign key. With each entry in Version table you'll get new version for subject Address:id=address_id with a specific timestamp, in which way you can have a history reference.

Upvotes: 2

knitti
knitti

Reputation: 7033

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.

Upvotes: 5

Luc M
Luc M

Reputation: 17304

You could have start_date and end_date.

When end_date is NULL, it`s the actual record.

Upvotes: 3

Related Questions