Postgresql – timelines and history files PostgreSQL 9.1

postgresql

What is the purpose of timelines, history files, and the setting 'recovery_target_timeline' in recovery.conf?

The vague understanding I have from the PostgreSQL 9.1 documentation is when the slave completes recovery it switches to a new timeline to prevent overwriting of the WALs of a previous timeline.

I am not clear how this is used in a recovery scenario, and the purpose of the .history file and the 'recovery_target_timeline' setting to 'latest'.

Best Answer

When the slave is promoted to a new master it creates a new timeline to avoid WAL names overlapping. The .history file contains information about database timeline branches. Recovery process uses this information to determine the timeline it is working with. By default it uses the same timeline that was when base backup was made. So, for example, if you promote one of your slaves to a new master other slaves will not be able to continue recovery from the new master as they use previous timeline.

To make them able to use current timeline, in easy words to switch them to the new master, you need them to know about the new history. Just delete everything in the pg_xlog directory on slaves and copy the history file from the new master. And then set recovery_target_timeline to 'latest' to force the database using the latest found timeline.

Note that the slave pretending to be a new master must be the most catch up one. Otherwise it might lead to data corruption on other slaves after you switch them to the new master.