CYBERTEC Logo
recovery.conf for doctors
© Laurenz Albe 2019

 

The biggest change in PostgreSQL v12 from the viewpoint of backward compatibility is that recovery.conf has been absorbed into postgresql.conf.

This article describes the changes and how you can adapt to them.

Getting rid of recovery.conf

Up to now, the presence of the file recovery.conf was the trigger for PostgreSQL to go into recovery mode upon server start. In addition, the file contained all parameters to configure recovery, for example

recovery.conf has been perceived as a wart for a long time, since it is unreasonable to have configuration parameters in more than one file.

In v12 PostgreSQL the move has been made, and the recovery.conf parameters are now part of postgresql.conf. These parameters are ignored until PostgreSQL is in recovery mode.

While this makes PostgreSQL work more naturally for newcomers, it has a few consequences that experienced users have to adapt to:

How to tell PostgreSQL to enter recovery mode?

Up to now, the existence of the file recovery.conf triggered recovery mode.

Since the file is gone, two new files have taken its place:

The files can be empty, only the fact that they exist is relevant. They are deleted as soon as recovery finishes.

The parameter “standby_mode” is not necessary anymore and has been removed.

What happens to the parameters after recovery is done?

Before, recovery.conf was renamed to recovery.done after recovery was finished. This effectively removed the recovery parameters.

From v12 on, the recovery.signal or standby.signal files will be deleted when recovery finishes, but the parameters in postgresql.conf remain. They will be ignored as long as PostgreSQL is not in recovery, but it is a good idea to disable them with a “#” comment. This will avoid nasty surprises later on, for example when you create a standby server.
If you set the recovery parameters using ALTER SYSTEM, reset them with ALTER SYSTEM RESET.

What happens to the “--write-recovery-conf” option of pg_basebackup?

The option is still there, and that is no mistake. It doesn't create recovery.conf any more, but it adds recovery configuration parameters.

Rather than adding the parameters to postgresql.conf, it adds them to postgresql.auto.conf, just like ALTER SYSTEM does. This is safer, since that file is made for automatic editing.

What happens if I recover PostgreSQL v12 with recovery.conf?

If you didn't notice the changes and try to recover PostgreSQL v12 with recovery.conf, the following will happen:

How to adapt my backup scripts?

Backup is not affected by the change. So your backup scripts need no modification.

Scripts that perform archive recovery or set up standby servers will have to be adapted. The main difficulty will be to add the necessary recovery parameters.
My recommendation is to add the parameters to postgresql.auto.conf, because as I said before, that file is made for automatic editing.

Here is some sample bash code to add recovery_target_time:

The command first removes all options starting with “recovery_target” and then adds the parameter.

Don't forget to do the following before starting the server:

I recommend that you remove the recovery parameters again once recovery is done.

I am using some third-party backup software – what shall I do?

Any dedicated PostgreSQL backup software that is worth its salt should have adapted to these changes by now. Upgrade to a current release.

I have checked PostgreSQL v12 support for the most widely-used programs:

Wrapping up

The changes in recovery will need some getting used to for experienced users, but it shouldn't be too hard to adapt.

Scripts that perform automatic recovery will have to be modified.

Take this as an opportunity to test your backup and recovery procedure!

For more information on backup in PostgreSQL, see my post on the deprecation of the "exclusive" backup method as of PostgreSQL v15.


In order to receive regular updates on important changes in PostgreSQL, subscribe to our newsletter, or follow us on Twitter, Facebook, or LinkedIn.

A lot has been written about configuring postgresql.conf, postgresql.auto.conf and so on. However, sometimes it requires to take a second look in order to understand, how PostgreSQL really handles configuration parameters. You will notice that PostgreSQL configuration offers more than meets the eye at first glance. So let us dive into PostgreSQL GUCs and configuration on a more theoretical level!

postgresql.conf: The classical method

Most people will directly change settings in postgresql.conf silently, assuming that this is the place to change PostgreSQL configuration parameters. However, this is not the only place you can use. The purpose of this blog is to show you which other options you have and how you can use these features to make your database configuration better.

For the sake of simplicity, I will use an easy configuration parameter to demonstrate how PostgreSQL operates:

The first thing you have to learn is how to figure out where configuration parameters actually come from. To do that, take a look at the pg_settings view:

postgresql.conf allows to include files. The idea is to give users the chance to break up postgresql.conf into smaller chunks.

postgresql.conf and included files

The rule here is simple: If your parameter is used inside a configuration file more than once, the LAST entry is going to be taken. In general, a parameter should only be in a config file once, but in case an error happens, you can be sure that the last entry is the one that counts.

Understanding ALTER SYSTEM

After the builtin settings, after taking what there is in postgresql.conf and after taking those include files into account, PostgreSQL will take a look at postgresql.auto.conf. The main question is: What is postgresql.auto.conf? It happens quite frequently, that administrators don’t have full access to the system (e.g. no SSH access). In this case superusers can take advantage of ALTER SYSTEM, which allows you to change PostgreSQL parameters using plain SQL. Here is how it works:

If you run ALTER SYSTEM, the database will made changes to postgresql.auto.conf:

These values will have precedence over postgresql.conf.

 

Builtin settings

As you can see, the parameter is now GMT. This is the default value set by the PostgreSQL binaries, in case there are no configuration parameters at all.

However, in many cases you don’t want to set a value permanently. For instance, you might only want to set it during maintenance mode. Maybe you want to start PostgreSQL on a different port to manually, while fixing a problem, to lock out users. In this case you can pass parameters via pg_ctl directly:

 

Using ALTER DATABASE SET …

In 80% of cases it is totally enough to either take the built-ins, postgresql.conf, or postgresql.auto.conf. Using -o is already quite rare. However, there is a lot more. Sometimes you want your configuration to be way finer grained. What if a parameter should only be used inside a specific database? Here is how it works:

 

 

After reconnecting to the database, you will see that the value is set correctly:

Not all changes can be made at the database level. Things such as “shared_buffers”, “port” can only be changed at the instance level and are not possible at the database level anymore, as shown in the next example:

ALTER USER … SET …

So far changes have been made to postgresql.conf, postgresql.auto.conf, on startup as well as on a per-database level. However, how about specific users? To do that, consider ALTER USER … SET …:

 

After a reconnect the value will be shown:

ALTER USER … IN DATABASE … test …

But what if this is still not fine-grained enough? What if you only want to set a value for a user inside a transaction? PostgreSQL can even do that:

After a reconnect the value will be shown:

Why is this kind of configuration useful? Suppose you are using a “datawarehouse” user to run some specific aggregations in of the databases. These specific operations might need special memory parameters, such as work_mem, to be efficient.

Changing PostgreSQL parameter at the session level

Sometimes hardwiring configuration settings is still not flexible enough. In PostgreSQL configuration, parameters can even be changed on a per session level. But be careful: This seemingly simple feature is highly sophisticated. The important thing to consider, is that in PostgreSQL everything is transaction. This includes PostgreSQL configuration parameters, as you can see in the next example:

What you can see, is that PostgreSQL even takes savepoints et cetera into account. If a transaction is not committed, the configuration parameters will be rolled back.

Assigning parameters to functions

After this introduction, there is a final feature I want to share: Parameters can be assigned to functions. Consider the following scenario:

The problem is that a “day” is not the same everywhere on the planet. So let us assume you want to calculate the turnover of every office per day. You can basically assign the timezone setting to each of those functions. Every function could run in a different timezone within the same SELECT statements.

CREATE FUNCTION shows how a setting can be passed to a function:

Finally ...

Configuring PostgreSQL parameters is really way more powerful than most users recognize. There are many ways to set parameters and it makes sense to explore these options to optimize your configuration. If you want to learn more about PostgreSQL configuration, you might want to check out my post about configuring parallel index creation.

SQL is query a language that is usually pretty easy to read. However, if people don't format their queries properly even SQL turns out to be a nightmare. That's why developers often turn to an SQL beautifier to turn an ugly query into a nicely formatted string. Various tools are available on the web to achieve exactly that.

 

PostgreSQL as a SQL Beautifier

 

Can the same thing be achieved using only PostgreSQL onboard tools? The answer is yes. This post will show you how to achieve that.

How PostgreSQL handles views

In PostgreSQL a view is not stored as plain text. Instead, it is stored inside the system table in binary, machine readable format:

The pg_node_tree data type contains all the magic here. This makes a lot of sense because data is more directly accessible during query execution. In addition, it allows PostgreSQL to easily handle changing column names and so on without breaking views. Internally, PostgreSQL is only using an object ID, and therefore names, and so on don't matter at all. Views will not be invalidated by renaming tables or a column.

However, if you use d+: How does PostgreSQL then provide the definition of a view in human readable format? The answer is: PostgreSQL reassembles the query again. This mechanism can be used to format an SQL string and turn it into something more beautiful.

Keep in mind: The mechanism was never intended to do that, but it is a nice illustration of what can be done.

SQL Beautifier: Turning a view into a proper query string

The pg_get_viewdef function returns the definition of a view as a string. We can make use of that. Let's take a look at the following function:

What it basically does is to take a string and turn it into a temporary view. This view is then turned into a string again and dropped. Here is the function in action:

As you can see the string is returned in a proper format. PostgreSQL will even resolve the “*” for you and turn it into a proper column list. What you see here is that psql has added a + in case a newline appears. We can easily fix that and tell psql to change its behavior:

PostgreSQL has created a beautiful SQL string for us.

Limitations of the SQL Beautifier

Of course, this approach comes with a couple of limitations. First of all, the function will only work if you provide a query that actually has a chance of being executed. If tables don't exist, an error will be thrown:

This can be seen as a feature or as a problem - it depends on what you are trying to achieve. In addition to that comments will be removed. There is no way to prevent that from happening.

Further reading

If you want to learn more about views in PostgreSQL, I highly encourage you to check out our post about views and row-level-security.

If you want to read more about views and dependency tracking check out our post about that.


In order to receive regular updates on important changes in PostgreSQL, subscribe to our newsletter, or follow us on Twitter, Facebook, or LinkedIn.

CYBERTEC Logo white
CYBERTEC PostgreSQL International GmbH
Römerstraße 19
2752 Wöllersdorf
Austria

+43 (0) 2622 93022-0
office@cybertec.at

Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2024
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram