{{/*
Enable the PostgreSQL configuration automatically
*/}}
{{- define "postgresql-ha.configuration.postgresql.enabled" -}}
{{- if and (or .Values.postgresql.repmgrConfiguration .Values.postgresql.configuration .Values.postgresql.pgHbaConfiguration) (not .Values.postgresql.configurationCM) }}
    false
{{- else }}
    true
{{- end }}
{{- end }}

{{/*
Return Postgresql - memory
*/}}
{{- define "postgresql-ha.configuration.postgresql.memory" -}}
{{- $memory := required "postgresql.resources.limits.memory is required" .Values.postgresql.resources.limits.memory | trim -}}
{{- if hasSuffix "Gi" $memory -}}
    {{ mul (int (trimSuffix "Gi" $memory)) 1024 }}
{{- else if hasSuffix "GB" $memory  -}}
    {{ mul (int (trimSuffix "GB" $memory)) 1000 }}
{{- else if hasSuffix "Mi" $memory  -}}
    {{ int (trimSuffix "Mi" $memory) }}
{{- else if hasSuffix "MB" $memory -}}
    {{ int (trimSuffix "MB" $memory) }}
{{- else -}}
    {{ fail "`postgresql.resources.limits.memory` 값이 올바른 형식으로 설정되지 않았습니다. (예: '10Gi', '512Mi')" }}
{{- end -}}
{{- end }}

{{/*
Return Postgresql - shared memory.
*/}}
{{- define "postgresql-ha.configuration.postgresql.sharedMemory" -}}
{{- $memory := include "postgresql-ha.configuration.postgresql.memory" . | int -}}
{{- if gt $memory 256 -}}
    {{ div $memory 4 }}Mi
{{- end }}
{{- "" -}}
{{- end }}

{{/*
Return postgres.conf.
*/}}
{{- define "postgresql-ha.configuration.postgresConf" -}}
{{- $memory := include "postgresql-ha.configuration.postgresql.memory" . | int -}}
listen_addresses = '*'
port = {{ .Values.postgresql.containerPorts.postgresql | quote }}
password_encryption = md5              

wal_level = 'replica'
archive_mode = 'on'
archive_command = '/bin/true'
max_wal_senders = {{ add .Values.postgresql.replicaCount 2 }}
max_replication_slots = '10'
hot_standby = 'on'
logging_collector = 'on'
log_directory = '/opt/bitnami/postgresql/logs'
log_filename = 'postgresql.log'
log_connections = 'false'
log_disconnections = 'false'
log_hostname = 'true'
client_min_messages = 'error'
pg_stat_statements.max = 10000
pg_stat_statements.track = all
include_dir = 'conf.d'
pgaudit.log_catalog = 'off'

wal_buffers = -1
max_wal_size = 10GB
min_wal_size = 1GB

shared_preload_libraries = '{{ .Values.postgresql.sharedPreloadLibraries }}'

shared_buffers = {{ div $memory 4 }}MB

effective_cache_size = {{ mul (div $memory 4) 3 }}MB
random_page_cost = 2.0

{{- if ge $memory 16384 }}
maintenance_work_mem = 2GB
work_mem = {{ div (subf $memory (div $memory 4)  2048) .Values.postgresql.maxConnections }}MB
{{- else if ge $memory 8192 }}
maintenance_work_mem = 1GB
work_mem = {{ div (subf $memory (div $memory 4)  1024) .Values.postgresql.maxConnections }}MB
{{- else }}
maintenance_work_mem = 512MB
work_mem = {{ div (subf $memory (div $memory 4)  512) .Values.postgresql.maxConnections }}MB
{{- end }}

log_min_duration_statement = 3000

{{- $tzEnv := "" }}
{{- range .Values.postgresql.extraEnvVars -}}
  {{- if eq  "TZ" .name -}}
    {{- $tzEnv = .value -}}
  {{- end -}}
{{- end -}}
{{- if $tzEnv }}
timezone={{ $tzEnv }}
{{- end -}}
{{- end -}}