Defining and getting database cluster settings

The PostgreSQL configuration settings control several aspects of the PostgreSQL cluster. From an administration perspective, one can define the statement timeout, memory settings, connections, logging, vacuum, and planner settings. From a development point of view, these settings can help a developer optimize queries. One can use the current_settings function or the show statement for convenience:

SELECT current_setting('work_mem');
current_setting
-----------------
4MB
(1 row)

show work_mem;
work_mem
----------
4MB
(1 row)

In order to change a certain configuration value, the function set_config(setting_name, new_value, is_local) can be utilized. If is_local is true, the new value will only apply to the current transaction, otherwise to the current session:

SELECT set_config('work_mem', '8 MB', false);

The set_config function can raise an error if the setting cannot be set to the session context such as the number of allowed connections and shared buffers, as follows:

SELECT set_config('shared_buffers', '1 GB', false);
ERROR: parameter "shared_buffers" cannot be changed without restarting the server

In addition to set_config, PostgreSQL provides the ALTER SYSTEM command used to change PostgreSQL configuration parameters. The synopsis for the ALTER SYSTEM is:

ALTER SYSTEM SET configuration_parameter { TO | = } { value | 'value' | DEFAULT }
ALTER SYSTEM RESET configuration_parameter
ALTER SYSTEM RESET ALL

In case the setting value requires a system reload or restart, the setting will take effect after the system reload or restart, respectively. The ALTER SYSTEM command requires superuser privileges. The ALTER SYSTEM command amends a file called postgresql.auto.conf. The ALTER SYSTEM name indicates that it has a global effect, and one needs to reload the server configuration as follows:

postgres=# ALTER SYSTEM SET work_mem TO '8MB';
ALTER SYSTEM
postgres=# SHOW work_mem;
work_mem
----------
4MB
(1 row)

postgres=# SELECT pg_reload_conf();
pg_reload_conf
----------------
t
(1 row)

postgres=# SHOW work_mem;
work_mem
----------
8MB
(1 row)

The following command shows the content of postgresql.auto.conf:

$cat postgresql.auto.conf 
# Do not edit this file manually!
# It will be overwritten by ALTER SYSTEM command.
work_mem = '8MB'

Finally, browsing the postgresql.conf file is a bit tricky due to the big number of PostgreSQL settings. Also, most settings have the default boot values. Therefore, getting the server configuration settings that are not assigned the default values in postgresql.conf can be easily done, as follows:

SELECT name, current_setting(name), source FROM pg_settings WHERE source IN ('configuration file');
name | current_setting | source
----------------------------+-----------------------------------------+--------------------
cluster_name | 10/main | configuration file
DateStyle | ISO, DMY | configuration file
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset