📜 ⬆️ ⬇️

Moving from PostgreSQL 9.0 to 9.2 under load

Good day to all!
As you know, PostgreSQL 9.2 was recently released with lots of interesting and useful things. Without thinking twice, we decided to upgrade our stream replication cluster from 9.0 to 9.2. All would be nothing if not for several circumstances:

Well, it's even more interesting ... How we did it and what came of it, read on.

Why all this?


Given:


Difficulties:

')
What we found a way out?
There is an exit in using Londiste from Skytools-3.0 package, once we moved with it from 8.4 to 9.0, so there is experience. Replication with Londiste is convenient because it allows replication of separate tables and databases in a cluster (for example, stream replication, replicates the entire cluster). Plus, with respect to the past move, we have stream replication. And this is also not a problem. Data replicated via Londiste will immediately be replicated to the freshly raised 9.2 slave using streaming replication. The scheme goes great: having joined 9.2, we transparently fill in the data of the slave 9.2. So the scheme and algorithm of the problem:

image

1. Admin part:

So from the technical side everything is ready. Now it remains to plan the progress of replication and the moment of switching. The day of the switch is selected Saturday. If something goes wrong, we have Sunday. We divided the activities into two stages, the preparatory stage and the switching stage. How will the switching be performed? For this, we introduced two new DNS names for the new bundle 9.2: db-master and db-slave. At the right moment, we will write these names into the backend configs and restart the applications.
Some of the activities of the preparatory plan have already been described above, but for the sake of completeness, I will nevertheless leave them in brief:

Until Friday:

Friday:

Saturday: this is the day of switching:

11.00-12.00 suspension of editing:

12.00-12.30 switching:

Everything. after that, replication via londiste becomes inconsistent, since the entire external record (the source of the record is the clients on the site) went to cluster 9.2;

After switching:

After moving

Rollback Plan in case something goes wrong:

Actually the whole algorithm. In the course of the event, of course, not everything went according to the master plan. Fortunately, there was no need to resort to a rollback plan.
If we talk about what went wrong, then there are only a couple of points,
The first point concerns the recently introduced service and the mechanism of manual transfer of schemes (which should be avoided). A few words about the service: a service based on the work of pgq collapsed, it was not quite clear how to replicate the pgq scheme (pgq was itself part of the replication mechanism). The manual transfer did not correct the situation either, so I had to reinitialize the circuit and restart the service (this is uncritically good, but still cant).
About the transfer of schemes ... practice has shown that the transfer of schemes does not always go as you like. Considering that the scheme of the entire database is created at an early stage of replication setup, in the future you have to transfer either the scheme over existing objects or individual data, during the transfer you can run into errors like:
ERROR: insert or update on table violates foreign key constraint
DETAIL: Key is not present in table.
From here a conclusion that transfer of schemes is better for doing so:
We rename the existing empty scheme in the destination database, then transfer the entire scheme from the source, delete the old renamed scheme from the destination database. Checking the uniformity of the schemes can be done through the bash structure. We run the command on both hosts, compare the output for matching (use diff)
# for i in schema_1 schema_2 schema_3; do psql -ltAF. -U postgres -c "\dt $i." db_name |cut -d. -f1,2 ; done |while read line ; do echo "$line" - $(psql -qAtX -U postgres -c "select count() from $line" db_name); done 

In the end, of course, I would like to note that you need to check all the places where you can write to the database several times and eliminate the possibility of recording when switching, when some services / backends have already switched to the new database, and the other part is not there yet. If you think further, then theoretically you can completely translate the volume to readonly, and perform a switch (mount / dmsetup / blockdev).

Well, a little bit of graphs.
1. NewRelic. Backend switching process
image

2. Zabbix. Daily server work with PG 9.0 (Monday September 10)
image

3. Zabbix. Daily server work with PG 9.0 (Monday September 10)
image

4. Zabbix. Daily server work with PG 9.2 + FlashCache (Monday September 17)
image

5. Zabbix. Daily server work with PG 9.2 + FlashCache (Monday September 17)
image

The biggest evil in Zabbix charts is the black line reflecting iowait. As you can see, the use of flashcache significantly reduced the load on hard drives.

Who are interested in technical details:
how stream replication is configured in PostgreSQL, see here .
how the table-based replication between PostgreSQL clusters is configured using Skytools-3, see here .

This is the story of one subbotnik. Thanks for attention!

Source: https://habr.com/ru/post/151849/


All Articles