Dynamic Configuration Settings
Dynamic configuration settings stored in DCS and applied cluster-wide.
Source: https://patroni.readthedocs.io/en/latest/patroni_configuration.html
There are 3 types of Patroni configuration:
Global dynamic configuration.
These options are stored in the DCS (Distributed Configuration Store) and applied on all cluster nodes. Dynamic configuration can be set at any time using patronictl_edit_config tool or Patroni REST API. If the options changed are not part of the startup configuration, they are applied asynchronously (upon the next wake up cycle) to every node, which gets subsequently reloaded. If the node requires a restart to apply the configuration (for PostgreSQL parameters with context postmaster, if their values have changed), a special flag pending_restart indicating this is set in the members.data JSON. Additionally, the node status indicates this by showing "restart_pending": true.
Local configuration file (patroni.yml).
These options are defined in the configuration file and take precedence over dynamic configuration. patroni.yml can be changed and reloaded at runtime (without restart of Patroni) by sending SIGHUP to the Patroni process, performing POST /reload REST-API request or executing patronictl_reload. Local configuration can be either a single YAML file or a directory. When it is a directory, all YAML files in that directory are loaded one by one in sorted order. In case a key is defined in multiple files, the occurrence in the last file takes precedence.
Environment configuration.
It is possible to set/override some of the “Local” configuration parameters with environment variables. Environment configuration is very useful when you are running in a dynamic environment and you don’t know some of the parameters in advance (for example it’s not possible to know your external IP address when you are running inside docker).
Some of the PostgreSQL parameters must hold the same values on the primary and the replicas. For those, values set either in the local patroni configuration files or via the environment variables take no effect. To alter or set their values one must change the shared configuration in the DCS. Below is the actual list of such parameters together with the default and minimal values:
For the parameters below, PostgreSQL does not require equal values among the primary and all the replicas. However, considering the possibility of a replica to become the primary at any time, it doesn’t really make sense to set them differently; therefore, Patroni restricts setting their values to the dynamic configuration.
These parameters are validated to ensure they are sane, or meet a minimum value.
There are some other Postgres parameters controlled by Patroni:
postgresql.listen or from PATRONI_POSTGRESQL_LISTEN environment variablepostgresql.listen or from PATRONI_POSTGRESQL_LISTEN environment variablescope or from PATRONI_SCOPE environment variableTo be on the safe side parameters from the above lists are written into postgresql.conf, and passed as a list of arguments to the postgres which gives them the highest precedence (except wal_keep_segments and wal_keep_size), even above ALTER SYSTEM
There also are some parameters like postgresql.listen, postgresql.data_dir that can be set only locally, i.e. in the Patroni config file or via configuration variable. In most cases the local configuration will override the dynamic configuration.
When applying the local or dynamic configuration options, the following actions are taken:
postgresql.base.conf file or if the custom_conf parameter is set.custom_conf parameter is set, the file it specifies is used as the base configuration, ignoring postgresql.base.conf and postgresql.conf.custom_conf parameter is not set and postgresql.base.conf exists, it contains the renamed “original” configuration and is used as the base configuration.custom_conf nor postgresql.base.conf, the original postgresql.conf is renamed to postgresql.base.conf and used as the base configuration.postgresql.conf and an include is set in postgresql.conf to the base configuration (either postgresql.base.conf or the file at custom_conf). Therefore, we would be able to apply new options without re-reading the configuration file to check if the include is present or not.The parameters would be applied in the following order (run-time are given the highest priority):
postgresql.base.conf (or from a custom_conf file, if set)postgresql.confpostgresql.auto.conf-o --name=valueThis allows configuration for all the nodes (2), configuration for a specific node using ALTER SYSTEM (3) and ensures that parameters essential to the running of Patroni are enforced (4), as well as leaves room for configuration tools that manage postgresql.conf directly without involving Patroni (1).
PostgreSQL has some parameters that determine the size of the shared memory used by them:
Changing these parameters require a PostgreSQL restart to take effect, and their shared memory structures cannot be smaller on the standby nodes than on the primary node.
As explained before, Patroni restrict changing their values through dynamic configuration, which usually consists of:
/config endpoint)/restart endpoint)Note: please keep in mind that you should perform a restart of the PostgreSQL nodes through patronictl_restart command, or via REST API /restart endpoint. An attempt to restart PostgreSQL by restarting the Patroni daemon, e.g. by executing systemctl restart patroni, can cause a failover to occur in the cluster, if you are restarting the primary node.
However, as those settings manage shared memory, some extra care should be taken when restarting the nodes:
If you want to increase the value of any of those settings:
- Restart all standbys first
- Restart the primary after that
If you want to decrease the value of any of those settings:
- Restart the primary first
- Restart all standbys after that
Note: if you attempt to restart all nodes in one go after decreasing the value of any of those settings, Patroni will ignore the change and restart the standby with the original setting value, thus requiring that you restart the standbys again later. Patroni does that to prevent the standby to enter in an infinite crash loop, because PostgreSQL quits with a FATAL message if you attempt to set any of those parameters to a value lower than what is visible in pg_controldata on the Standby node. In other words, we can only decrease the setting on the standby once its pg_controldata is up-to-date with the primary in regards to these changes on the primary.
More information about that can be found at PostgreSQL Administrator’s Overview.
Also the following Patroni configuration options can be changed only dynamically:
Upon changing these options, Patroni will read the relevant section of the configuration stored in DCS and change its run-time values.
Patroni nodes are dumping the state of the DCS options to disk upon for every change of the configuration into the file patroni.dynamic.json located in the Postgres data directory. Only the leader is allowed to restore these options from the on-disk dump if these are completely absent from the DCS or if they are invalid.
Patroni provides command-line interfaces for a Patroni local configuration generation and validation. Using the patroni executable you can:
patroni --generate-sample-config [configfile]
Generate a sample Patroni configuration file in yaml format. Parameter values are defined using the Environment configuration, otherwise, if not set, the defaults used in Patroni or the #FIXME string for the values that should be later defined by the user.
Some default values are defined based on the local setup:
- postgresql.listen: the IP address returned by
gethostnamecall for the current machine’s hostname and the standard5432port.- postgresql.connect_address: the IP address returned by
gethostnamecall for the current machine’s hostname and the standard5432port.- postgresql.authentication.rewind: is only defined if the PostgreSQL version can be defined from the binary and the version is 11 or later.
- restapi.listen: IP address returned by
gethostnamecall for the current machine’s hostname and the standard8008port.- restapi.connect_address: IP address returned by
gethostnamecall for the current machine’s hostname and the standard8008port.
configfile - full path to the configuration file used to store the result. If not provided, the result is sent to stdout.
patroni --generate-config [--dsn DSN] [configfile]
Generate a Patroni configuration in yaml format for the locally running PostgreSQL instance. Either the provided DSN (takes precedence) or PostgreSQL environment variables will be used for the PostgreSQL connection. If the password is not provided, it should be entered via prompt.
All the non-internal GUCs defined in the source Postgres instance, independently if they were set through a configuration file, through the postmaster command-line, or through environment variables, will be used as the source for the following Patroni configuration parameters:
- scope:
cluster_nameGUC value;- postgresql.listen:
listen_addressesandportGUC values;- postgresql.datadir:
data_directoryGUC value;- postgresql.parameters:
archive_command,restore_command,archive_cleanup_command,recovery_end_command,ssl_passphrase_command,hba_file,ident_file,config_fileGUC values;- bootstrap.dcs: all other gathered PostgreSQL GUCs.
If scope, postgresql.listen or postgresql.datadir is not set from the Postgres GUCs, the respective Environment configuration value is used.
Other rules applied for the values definition:
- name:
PATRONI_NAMEenvironment variable value if set, otherwise the current machine’s hostname.- postgresql.bin_dir: path to the Postgres binaries gathered from the running instance.
- postgresql.connect_address: the IP address returned by
gethostnamecall for the current machine’s hostname and the port used for the instance connection or theportGUC value.- postgresql.authentication.superuser: the configuration used for the instance connection;
- postgresql.pg_hba: the lines gathered from the source instance’s
hba_file.- postgresql.pg_ident: the lines gathered from the source instance’s
ident_file.- restapi.listen: IP address returned by
gethostnamecall for the current machine’s hostname and the standard8008port.- restapi.connect_address: IP address returned by
gethostnamecall for the current machine’s hostname and the standard8008port.
Other parameters defined using Environment configuration are also included into the configuration.
configfile
Full path to the configuration file used to store the result. If not provided, result is sent to stdout.
dsn
Optional DSN string for the local PostgreSQL instance to get GUC values from.
patroni --validate-config [configfile] [--ignore-listen-port | -i]
Validate the given Patroni configuration and print the information about the failed checks.
configfile
Full path to the configuration file to check. If not given or file does not exist, will try to read from the PATRONI_CONFIG_VARIABLE environment variable or, if not set, from the Patroni environment variables.
--ignore-listen-port | -i
Optional flag to ignore bind failures for listen ports that are already in use when validating the configfile.
--print | -p
Optional flag to print out local configuration (including environment configuration overrides) after it has been successfully validated.
Dynamic configuration settings stored in DCS and applied cluster-wide.
Complete reference for Patroni YAML configuration options and sections.
Environment variables for overriding Patroni configuration parameters.
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.