# {{ ansible_managed }}
# /!\ Any change here require a postgresql restart to be taken into account

# File Locations
data_directory = '{{ pg_data_directory }}'
hba_file = '{{ pg_data_directory }}/pg_hba.conf'
ident_file = '{{ pg_data_directory }}/pg_ident.conf'
external_pid_file = '/var/run/postgresql/{{ pg_version }}-{{ pg_cluster_name }}.pid'

# Connection Settings
listen_addresses = '{{ pg_listen_addresses }}'
port = {{ pg_port }}
max_connections = {{ pg_max_connections }}
unix_socket_directories = '/var/run/postgresql'

# Security and Authentication
{% if pg_ssl %}
ssl = true
ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem'
ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'
{% endif %}

# Resource Consumption
shared_buffers = '{{ pg_shared_buffers }}'
huge_pages = '{{ pg_huge_pages }}'
{% if pg_version is version('9.6', '>=') and pg_max_worker_processes is defined %}
max_worker_processes = {{ pg_max_worker_processes }}
{% endif %}

# Write Ahead Log
wal_buffers = '16MB'
wal_level = '{{ pg_wal_level | default('replica') }}'

# Archiving
{% if pg_archive_command is defined %}
archive_mode = 'on'
{% endif %}

# Replication
{% if pg_version is version('9.6', '<=') %}
hot_standby = 'on'
{% endif %}
{% if pg_replication %}
max_wal_senders = '5'
max_replication_slots = '5'
{% elif pg_wal_level is defined and pg_wal_level == 'minimal' %}
max_wal_senders = '0'
max_replication_slots = '0'
{% endif %}

# Error Reporting and Logging
{% if pg_logging %}
logging_collector = 'on'
{% endif %}

# Process Title
cluster_name = '{{ pg_version }}-{{ pg_cluster_name }}'

# Run-time Statistics
track_activity_query_size = '10240'

# Automatic Vacuuming
{% if pg_autovacuum_max_workers is defined %}
autovacuum_max_workers = '{{ pg_autovacuum_max_workers }}'
{% endif %}
{% if pg_autovacuum_freeze_max_age is defined %}
autovacuum_freeze_max_age = '{{ pg_autovacuum_freeze_max_age }}'
{% endif %}
{% if pg_recovery %}

# Recovery
{% if pg_recovery_target is defined %}
recovery_target = '{{ pg_recovery_target }}'
{% endif %}
{% if pg_recovery_target_action is defined %}
recovery_target_action = '{{ pg_recovery_target_action }}'
{% endif %}
{% if pg_recovery_target_time is defined %}
recovery_target_time = '{{ pg_recovery_target_time }}'
{% endif %}
{% endif %}
{% if pg_shared_preload_libraries is defined %}

# shared library
shared_preload_libraries = '{{ pg_shared_preload_libraries | join(',') }}'
{% endif %}
