Skip to Content

How to Update PostgreSQL Server Configurations Without Downtime

Introduction

Updating PostgreSQL server configurations is a critical part of database administration. While the simplest method to update configurations is by modifying the postgresql.conf file and restarting the server, this approach can cause downtime, which is especially problematic in high-availability setups or production environments with only a single server running.

Fortunately, not all configuration parameters in PostgreSQL require a full server restart. By using the pg_settings view, you can identify which parameters can be modified without restarting the server. This guide will explain how to manage PostgreSQL configuration changes effectively, ensuring minimal disruption.

Identifying Parameters that Require a Restart

PostgreSQL offers the pg_settings view, which provides detailed information about the server’s runtime configuration parameters.

One important column in this view is the context column, which specifies how a parameter can be changed.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

The following query retrieves the context for the min_wal_size, max_wal_size, and wal_level parameters:

SELECT
name,
context
FROM
pg_settings
WHERE
name IN ('min_wal_size', 'max_wal_size', 'wal_level');

The query results might look like this:

name         | context
--------------+------------
max_wal_size  | sighup
min_wal_size  | sighup
wal_level     | postmaster
(3 rows)

In this example:

  • sighup context: Parameters such as max_wal_size and min_wal_size can be changed in the postgresql.conf file without requiring a restart. The changes are applied by sending a SIGHUP signal to the postmaster process.
  • postmaster context: The wal_level parameter, however, requires a server restart for changes to take effect.

Reloading Configuration Without Restart

If a parameter can be reloaded, you don’t need to restart the entire server. Instead, you can send a SIGHUP signal to the postmaster process, which causes PostgreSQL to re-read its configuration file. This can be done with the pg_reload_conf function.

This function is restricted to superusers. To reload the configuration, run the following command from a psql session:

SELECT pg_reload_conf();

This will apply any changes made to parameters with the sighup context without requiring a restart of the server process.

Making Temporary Changes with ALTER SYSTEM

For quick configuration changes, you can use the ALTER SYSTEM command. This command allows you to update “reloadable” configuration parameters directly from a psql session without editing the postgresql.conf file.

However, there are some important caveats:

  • Ephemeral Changes: Changes made with ALTER SYSTEM are temporary. If the server is restarted, the changes will be reverted.
  • Convenience: ALTER SYSTEM is useful during incidents or testing scenarios where quick changes are needed.

Here’s an example of how to use the ALTER SYSTEM command:

ALTER SYSTEM SET work_mem = '64MB';

While ALTER SYSTEM is convenient, it’s generally a good idea to make permanent changes in the postgresql.conf file to avoid confusion and ensure that changes are persistent across server restarts.

Understanding Implicit Units in Configuration Parameters

Some PostgreSQL configuration parameters, especially those related to memory and time, use implicit units. When setting values for these parameters, the value specified acts as a multiplier for an implicit unit.

For example, if max_wal_size is set to 144, and the unit for this parameter is 16MB, the actual value is calculated as:

144 * 16MB = 2304MB

When querying the pg_settings view, ensure you consider both the setting and unit columns to interpret the full value. For example:

SELECT name, setting, unit
FROM pg_settings
WHERE name = 'max_wal_size';

By considering both columns, you can accurately understand the actual value of configuration parameters.

Best Practices for Updating PostgreSQL Configurations

When updating PostgreSQL configurations, follow these best practices:

  • Check the context: Use the pg_settings view to identify which parameters require a restart and which can be reloaded.
  • Minimize downtime: If a restart is required, plan for a maintenance window to minimize disruption.
  • Use ALTER SYSTEM sparingly: The ALTER SYSTEM command is useful for quick fixes but should not be relied upon for permanent changes.
  • Document changes: Always keep track of changes made to configuration parameters, especially when modifying the postgresql.conf file.

Conclusion

Managing PostgreSQL configurations effectively is essential for database performance and reliability. By using tools like pg_settings, pg_reload_conf, and ALTER SYSTEM, you can make updates with minimal downtime and avoid unnecessary server restarts. Always ensure that you understand the implications of configuration changes and consider implicit units when interpreting parameter values.

For comprehensive information on PostgreSQL configuration, refer to the official PostgreSQL documentation: PostgreSQL Configuration Documentation.