Files
ess-helm/charts/matrix-stack/templates/postgres/_helpers.tpl
Ben Banfield-Zanin 630ad34160 Ensure Postgres is fully setup before marking as available or live.
When the Postgres container initialises itself it does so without binding any TCP ports, only allowing
connections on the Unix socket. Only after it restarts after running the entrypoint scripts/SQL does it
bind TCP ports. Make the liveness/readiness/startup probes only use the TCP port so that we don't mark
Postgres as being ready and available too early.

```
...
2025-11-25 16:56:15.588 UTC [32] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2025-11-25 16:56:15.597 UTC [35] LOG:  database system was shut down at 2025-11-25 16:56:15 UTC
2025-11-25 16:56:15.606 UTC [32] LOG:  database system is ready to accept connections
 done
server started

/usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/init-ess-dbs.sh
Password:
Password:
CREATE ROLE
Password:
Password:
Password:
CREATE ROLE
Password:

2025-11-25 16:56:15.934 UTC [32] LOG:  received fast shutdown request
waiting for server to shut down....2025-11-25 16:56:15.938 UTC [32] LOG:  aborting any active transactions
2025-11-25 16:56:15.946 UTC [32] LOG:  background worker "logical replication launcher" (PID 38) exited with exit code 1
2025-11-25 16:56:15.954 UTC [33] LOG:  shutting down
2025-11-25 16:56:15.958 UTC [33] LOG:  checkpoint starting: shutdown immediate
2025-11-25 16:56:16.042 UTC [33] LOG:  checkpoint complete: wrote 1853 buffers (1.4%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.071 s, sync=0.008 s, total=0.088 s; sync files=604, longest=0.003 s, average=0.001 s; distance=8515 kB, estimate=8515 kB; lsn=0/1D3DDC0, redo lsn=0/1D3DDC0
2025-11-25 16:56:16.070 UTC [32] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2025-11-25 16:56:16.189 UTC [1] LOG:  starting PostgreSQL 17.7 on x86_64-pc-linux-musl, compiled by gcc (Alpine 14.2.0) 14.2.0, 64-bit
2025-11-25 16:56:16.189 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2025-11-25 16:56:16.189 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2025-11-25 16:56:16.194 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2025-11-25 16:56:16.202 UTC [87] LOG:  database system was shut down at 2025-11-25 16:56:16 UTC
2025-11-25 16:56:16.257 UTC [1] LOG:  database system is ready to accept connections
/usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/init-ess-dbs.sh
Password:
Password:
CREATE ROLE
Password:
Password:
Password:
CREATE ROLE
Password:

2025-11-25 16:56:15.934 UTC [32] LOG:  received fast shutdown request
...
2025-11-25 16:56:30.082 UTC [129] ERROR:  deadlock detected
2025-11-25 16:56:30.082 UTC [129] DETAIL:  Process 129 waits for ShareLock on virtual transaction 22/5; blocked by process 130.
	Process 130 waits for ExclusiveLock on advisory lock [16385,438033630,3765090638,1]; blocked by process 129.
	Process 129: -- no-transaction
	-- Copyright 2025 New Vector Ltd.
	--
	-- SPDX-License-Identifier: AGPL-3.0-only
	-- Please see LICENSE in the repository root for full details.

	CREATE INDEX CONCURRENTLY
	  compat_access_tokens_session_fk
	  ON compat_access_tokens (compat_session_id);

	Process 130: SELECT pg_advisory_lock($1)
...
```
2025-11-26 10:41:21 +00:00

189 lines
6.6 KiB
Smarty

{{- /*
Copyright 2025 New Vector Ltd
Copyright 2025 Element Creations Ltd
SPDX-License-Identifier: AGPL-3.0-only
*/ -}}
{{- define "element-io.postgres.validations" }}
{{- $root := .root -}}
{{- with required "element-io.postgres.validations missing context" .context -}}
{{- $messages := list -}}
{{- $postgresUrls := dict -}}
{{- range $component, $compValues := $root.Values -}}
{{- if and (eq (kindOf $compValues) "map") (hasKey $compValues "postgres") ($compValues.enabled) ($compValues.postgres) -}}
{{- $postgresValues := (index $root.Values $component).postgres -}}
{{- $_ := set $postgresUrls $component (printf "%s:%s/%s" $postgresValues.host $postgresValues.port $postgresValues.database) -}}
{{- end -}}
{{- end -}}
{{- if ne (len ($postgresUrls | values)) (len (($postgresUrls | values) | uniq | sortAlpha)) -}}
{{- range $compA, $pgUrlA := $postgresUrls -}}
{{- range $compB, $pgUrlB := $postgresUrls -}}
{{- if and (ne $compA $compB) (eq $pgUrlA $pgUrlB) -}}
{{ $messages = append $messages (printf "%s.postgres is using the same database as %s.postgres" $compA $compB) }}
{{- end }}
{{- end }}
{{- end }}
{{- end -}}
{{ $messages | toJson }}
{{- end }}
{{- end }}
{{- define "element-io.postgres.labels" -}}
{{- $root := .root -}}
{{- with required "element-io.postgres.labels missing context" .context -}}
{{ include "element-io.ess-library.labels.common" (dict "root" $root "context" (dict "labels" .labels "withChartVersion" .withChartVersion)) }}
app.kubernetes.io/component: matrix-stack-db
app.kubernetes.io/name: postgres
app.kubernetes.io/instance: {{ $root.Release.Name }}-postgres
app.kubernetes.io/version: {{ include "element-io.ess-library.labels.makeSafe" .image.tag }}
{{- end }}
{{- end }}
{{- define "element-io.postgres.enabled" }}
{{- $root := .root -}}
{{- if and $root.Values.postgres.enabled (or
(and $root.Values.matrixAuthenticationService.enabled
(not $root.Values.matrixAuthenticationService.postgres))
(and $root.Values.synapse.enabled
(not $root.Values.synapse.postgres))
) -}}
true
{{- end }}
{{- end }}
{{- define "element-io.postgres.anyEssPasswordHasValue" }}
{{- $root := .root -}}
{{- with required "element-io.postgres.anyEssPasswordHasValue missing context" .context -}}
{{- range .essPasswords -}}
{{- if .value -}}
true
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- define "element-io.postgres.configSecrets" -}}
{{- $root := .root -}}
{{- with required "element-io.postgres.configSecrets missing context" .context -}}
{{- $configSecrets := list }}
{{- if or .adminPassword.value (include "element-io.postgres.anyEssPasswordHasValue" (dict "root" $root "context" .)) }}
{{- $configSecrets = append $configSecrets (printf "%s-postgres" $root.Release.Name) }}
{{- end }}
{{- with .adminPassword.secret -}}
{{ $configSecrets = append $configSecrets (tpl . $root) }}
{{- end -}}
{{- range $key := (.essPasswords | keys | uniq | sortAlpha) -}}
{{- $prop := index $root.Values.postgres.essPasswords $key }}
{{- with $prop.secret -}}
{{ $configSecrets = append $configSecrets (tpl . $root) }}
{{- end }}
{{- end }}
{{ $configSecrets | uniq | toJson }}
{{- end }}
{{- end }}
{{- define "element-io.postgres.memoryLimitsMB" -}}
{{- $root := .root -}}
{{- with required "element-io.postgres.memoryLimitsMB missing context" .context -}}
{{- $value := .resources.limits.memory }}
{{- if $value | hasSuffix "Mi" }}
{{- printf "%d" (trimSuffix "Mi" $value) | int64 -}}
{{- else if $value | hasSuffix "Gi" }}
{{- printf "%d" (mul (int64 (trimSuffix "Gi" $value)) 1024) | int64 -}}
{{- else if $value | hasSuffix "Ti" }}
{{- printf "%d" (mul (mul (int64 (trimSuffix "Ti" $value)) 1024) 1024) | int64 -}}
{{- else -}}
{{- fail (printf "Could not compute Postgres memory limits from %s" $value) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- define "element-io.postgres.args" -}}
{{- $root := .root -}}
{{- with required "element-io.postgres.args missing context" .context -}}
{{- $memoryLimitsMB := include "element-io.postgres.memoryLimitsMB" (dict "root" $root "context" .) }}
- "-c"
- "max_connections={{ printf "%d" (div $memoryLimitsMB 16) }}"
- "-c"
- "shared_buffers={{ printf "%s" (printf "%dMB" (div $memoryLimitsMB 4)) }}"
- "-c"
- "effective_cache_size={{ printf "%s" (printf "%dMB" (sub $memoryLimitsMB 256)) }}"
{{- end -}}
{{- end -}}
{{- define "element-io.postgres-password-updater.overrideEnv" }}
{{- $root := .root -}}
{{- with required "element-io.postgres.password-change-env missing context" .context -}}
env:
- name: "POSTGRES_PASSWORD_FILE"
value: {{ printf "/secrets/%s" (
include "element-io.ess-library.init-secret-path" (dict
"root" $root
"context" (dict
"secretPath" "postgres.adminPassword"
"initSecretKey" "POSTGRES_ADMIN_PASSWORD"
"defaultSecretName" (include "element-io.postgres.secret-name" (dict "root" $root "context" (dict "isHook" false)))
"defaultSecretKey" "ADMIN_PASSWORD"
)
)
) }}
- name: "PGHOST"
value: "localhost"
- name: "PGDATA"
value: "/var/lib/postgres/data/pgdata"
{{- end -}}
{{- end -}}
{{- define "element-io.postgres.overrideEnv" }}
{{- $root := .root -}}
{{- with required "element-io.postgres.overrideEnv missing context" .context -}}
env:
- name: "POSTGRES_PASSWORD_FILE"
value: {{ printf "/secrets/%s" (
include "element-io.ess-library.init-secret-path" (dict
"root" $root
"context" (dict
"secretPath" "postgres.adminPassword"
"initSecretKey" "POSTGRES_ADMIN_PASSWORD"
"defaultSecretName" (include "element-io.postgres.secret-name" (dict "root" $root "context" (dict "isHook" false)))
"defaultSecretKey" "ADMIN_PASSWORD"
)
)
) }}
- name: "PGDATA"
value: "/var/lib/postgres/data/pgdata"
- name: "POSTGRES_INITDB_ARGS"
value: "-E UTF8"
- name: "LC_COLLATE"
value: "C"
- name: "LC_CTYPE"
value: "C"
{{- end -}}
{{- end -}}
{{- define "element-io.postgres-exporter.overrideEnv" }}
{{- $root := .root -}}
{{- with required "element-io.postgres-exporter.overrideEnv missing context" .context -}}
env:
- name: "DATA_SOURCE_URI"
value: "localhost?sslmode=disable"
- name: "DATA_SOURCE_USER"
value: "postgres"
{{- end -}}
{{- end -}}
{{- define "element-io.postgres.configmap-data" -}}
{{- $root := .root -}}
{{- with required "element-io.postgres.configmap-data" .context -}}
configure-dbs.sh: |
{{- (tpl ($root.Files.Get "configs/postgres/configure-dbs.sh.tpl") (dict "root" $root "context" .)) | nindent 2 }}
{{- end }}
{{- end }}