# {{ ansible_managed }}
# PostgreSQL unified configuration file

# include the parameters that require restart if changed
include '{{ pg_configuration_file_restart_required }}'

{% if pg_version is version('15', '<') %}
stats_temp_directory = '/var/run/postgresql/{{ pg_version }}-{{ pg_cluster_name }}.pg_stat_tmp'
{% endif %}
{% if pg_timezone is defined %}
timezone = '{{ pg_timezone }}'
{% endif %}
statement_timeout = '{{ pg_statement_timeout }}'

# fixed parameters, those settings should never be changed
checkpoint_completion_target = '0.9'
datestyle = 'iso, dmy'
default_text_search_config = 'pg_catalog.english'
dynamic_shared_memory_type = 'posix'
lc_messages = '{{ pg_default_encoding | default("en_US.UTF-8") }}'
lc_monetary = '{{ pg_default_encoding | default("en_US.UTF-8") }}'
lc_numeric = '{{ pg_default_encoding | default("en_US.UTF-8") }}'
lc_time = '{{ pg_default_encoding | default("en_US.UTF-8") }}'

# memory settings
{% if pg_checkpoint_timeout is defined %}
checkpoint_timeout = '{{ pg_checkpoint_timeout }}'
{% endif %}
{% if pg_effective_cache_size is defined %}
effective_cache_size = '{{ pg_effective_cache_size }}'
{% endif %}
{% if pg_maintenance_work_mem is defined %}
maintenance_work_mem = '{{ pg_maintenance_work_mem }}'
{% endif %}
{% if pg_work_mem is defined %}
work_mem = '{{ pg_work_mem }}'
{% endif %}

# wal
{% if pg_max_wal_size is defined %}
max_wal_size = '{{ pg_max_wal_size }}'
{% endif %}
{% if pg_min_wal_size is defined %}
min_wal_size = '{{ pg_min_wal_size }}'
{% endif %}

# autovacuum
{% if pg_autovacuum_vacuum_scale_factor is defined %}
autovacuum_vacuum_scale_factor = '{{ pg_autovacuum_vacuum_scale_factor }}'
{% endif %}
{% if pg_autovacuum_work_mem is defined %}
autovacuum_work_mem = '{{ pg_autovacuum_work_mem }}'
{% endif %}
{% if pg_autovacuum_vacuum_cost_delay is defined %}
autovacuum_vacuum_cost_delay = '{{ pg_autovacuum_vacuum_cost_delay }}'
{% endif %}
{% if pg_autovacuum_vacuum_cost_limit is defined %}
autovacuum_vacuum_cost_limit = '{{ pg_autovacuum_vacuum_cost_limit }}'
{% endif %}

# ssd scale
{% if pg_ssd %}
seq_page_cost = 1.0
random_page_cost = {{ pg_random_page_cost }}
effective_io_concurrency = '{{ pg_effective_io_concurrency }}'
{% if pg_version is version('13', '>=') %}
maintenance_io_concurrency = '{{ pg_maintenance_io_concurrency }}'
{% endif %}
{% endif %}

# logging
{% if pg_logging %}
log_autovacuum_min_duration = '0'
log_checkpoints = 'on'
log_connections = 'on'
log_destination = 'csvlog'
log_directory = '{{ pg_log_directory }}'
log_disconnections = 'on'
log_file_mode = '0644'
log_filename = '{{ pg_version }}-{{ pg_cluster_name }}-{{ pg_log_filename_suffix }}.log'
log_line_prefix = '%t,%u,%d,%p,%h,%x,%l,%i,%s,%e,%q,%v,%a'
log_lock_waits = 'on'
log_min_duration_statement = '{{ pg_log_min_duration_statement }}'
log_rotation_age = '1h'
log_rotation_size = '0'
log_temp_files = '0'
{% if pg_timezone is defined %}
log_timezone = '{{ pg_timezone }}'
{% endif %}
log_truncate_on_rotation = 'on'
{% endif %}

# replication
{% if pg_archive_command is defined %}
archive_command = '{{ pg_archive_command }}'
archive_timeout = '{{ pg_archive_timeout }}'
{% endif %}
{% if pg_replication %}
hot_standby_feedback = 'on'
hot_standby = 'on'
max_standby_archive_delay = '{{ pg_max_standby_archive_delay }}'
max_standby_streaming_delay = '{{ pg_max_standby_streaming_delay }}'
{% endif %}

# specific to pg version >= 9.6
{% if pg_version is version('9.6', '>=') %}
{% if pg_idle_in_transaction_session_timeout is defined %}
idle_in_transaction_session_timeout = '{{ pg_idle_in_transaction_session_timeout }}'
{% endif %}
{% if pg_max_parallel_workers_per_gather is defined %}
max_parallel_workers_per_gather = {{ pg_max_parallel_workers_per_gather }}
{% endif %}
{% endif %}

# performance
synchronous_commit = '{{ pg_synchronous_commit }}'
{% if not pg_fsync %}
# WARNING: fsync is essential to prevent database corruption
# never set fsync = off except for initial loading of a new database cluster from a backup file
# don't forget to re-enable fsync as soon as you care about the data
fsync = off
{% endif %}
{% if file_system.stdout == "zfs" %}

# zfs
full_page_writes = off
wal_compression = off
{% if pg_version is version('12', '>=') %}
wal_recycle = off
wal_init_zero = off
{% endif %}
{% endif %}
{% if (pg_version is version('12', '>=') and pg_replication_role == "replica") or pg_restore_command is defined %}

# replica
{% if postgres_primary_slot_name is defined %}
primary_conninfo = 'host={{ postgres_primary_conninfo_host }} port={{ postgres_primary_conninfo_port }} user={{ postgres_primary_conninfo_user }} password={{ postgres_primary_conninfo_password }} sslmode={{ postgres_primary_conninfo_sslmode }}'
primary_slot_name = '{{ postgres_primary_slot_name }}'
{% endif %}
{% if pg_restore_command is defined %}
restore_command = '{{ pg_restore_command }}'
{% endif %}
{% endif %}
{% if (pg_version is version('17', '>=')) %}

# compatibility
allow_alter_system = off
{% endif %}
