PostgreSQL 9.4 for administrators (part two)

December 04, 2014

Written by Francesco Canovai

In the previous instalment, we introduced the logical replication feature which has been added to PostgreSQL 9.4. Let’s go on exploring the multitude of new features that version 9.4 brings to the Operation field, easing the management of PostgreSQL databases for system and database administrators.


pg_prewarm

pg_prewarm is a new extension to solve the problem of slow servers after a restart. Buffers are emptied during a restart, therefore Postgres won’t be able to find in RAM the data it needs, forcing disk reads. With pg_prewarm it is possible to load in memory an important table immediately after a reboot with the simple query:

SELECT pg_prewarm('my_table');

This way, we won’t have to wait for the database to load data in cache through its routine operations.


Tablespace management

Two handy features have been introduced to simplify the usage of tablespaces. The first is the command ALTER TABLESPACE … MOVE, which allows moving tables, indexes and materialised views from one tablespace to another one. The second is the syntax CREATE TABLESPACE … WITH … options, with which it is now possible to set tablespace options while creating tablespaces, saving a second ALTER TABLESPACE. Two parameters are available right now, `seq_page_cost` and `random_page_cost`, which the planner can use to understand which are the faster disks.

CREATE TABLESPACE new_tblspc LOCATION 'my_dir' WITH random_page_cost = 1;
ALTER TABLESPACE old_tblspc MOVE TABLES TO new_tblspc;

WAL archive monitoring

While working on Barman here in 2ndQuadrant, we had to estimate many times the number of WAL segments produced by a server. This is the reason Gabriele Bartolini produced this patch. PostgreSQL 9.4 has a stat table about the archiver activity, showing the number of WAL segments archived since the last reset (and the number of time the archiver failed, in case failures happened).

SELECT * FROM pg_stat_archiver;
-[ RECORD 1 ]------+------------------------------
 archived_count     | 4
 last_archived_wal  | 00000001000000000000000B
 last_archived_time | 2014-10-07 08:58:02.258657+00
 failed_count       | 0
 last_failed_wal    |
 last_failed_time   |
 stats_reset        | 2014-10-07 08:51:29.852523+00

Time delayed standbys

While a standby server is very useful if the master server crashes, its utility is zero in the instance of a human error. A mistakenly run `DROP TABLE` would be replicated immediately on the standby, making a *point in time recovery* a necessity. The possibility of having a replication server which applies the changes with a certain delay gives the admin some time to stop the replication, thus avoiding the propagation of the error and saving the standby server.

To configure a standby server with a delay of at least one hour, it is sufficient to set in the `recovery.conf`

min_recovery_apply_delay = 1h

Modify configuration on the fly

`ALTER SYSTEM` is another new command introduced in version 9.4 to ease working with Postgres. It is now possible to change the `postgresql.conf` file from within a SQL connection. With the sole exception of the `PGDATA` and the parameters that need to be set during compile time, all the other parameters can be changed as in the following example:

ALTER SYSTEM SET wal_level = hot_standby;

The new values will be written in the `postgresql.auto.conf` file. A reload or a restart will still be necessary if the altered parameter requires it.


WAL performance

Write-ahead logging performance has improved. Lock contention has been reduced for insert operations into the WAL, and the WAL record size has been reduced for UPDATEs. Thus the same UPDATE operation would now generate less I/O.


Conclusions

For us Linux sysadmins and DBAs, PostgreSQL 9.4 improves further the management of databases in business continuity. It also lays the groundwork for new horizons in system architectures, with the multi-master replication in sight. Finally, thanks to the logical replication, we will surely see the trigger-based replication tools (Londiste, Slony, Bucardo) add support for event decodification from logical replication slots, making replication management lighter. Keep following our blog for the new article from Giuseppe on the new PostgreSQL features for developers. See you soon!

Share this

More Blogs