id: postgres-forced-checkpoint-max-wal-tuning-guard
namespace: company.team
inputs:
- id: postgres_url
type: STRING
defaults: jdbc:postgresql://localhost:5432/postgres
description: >-
PostgreSQL JDBC URL for the instance whose checkpoint behaviour is
measured. The setting is instance wide, so any database on the cluster
works. Example jdbc:postgresql://postgres:5432/app.
- id: use_secret_auth
type: BOOL
defaults: true
description: >-
Leave true in production so the connection reads POSTGRES_USERNAME and
POSTGRES_PASSWORD from secrets. Set false only to pass credentials inline
in a disposable test environment.
- id: postgres_username
type: STRING
defaults: postgres
description: Username used only when use_secret_auth is false. Ignored in production.
- id: postgres_password
type: STRING
defaults: ""
description: Password used only when use_secret_auth is false. Ignored in production.
- id: min_window_checkpoints
type: INT
defaults: 20
description: >-
Minimum number of checkpoints inside the measured window before the ratio
is judged. A window with two checkpoints says nothing, so below this count
the run reports and stops.
- id: max_requested_share
type: FLOAT
defaults: 0.5
description: >-
Share of the checkpoints in the window that may be requested by WAL volume
rather than fired by the timer. Above this the instance is treated as
checkpointing under WAL pressure. 0.5 means more than half were volume
forced.
- id: headroom_factor
type: FLOAT
defaults: 2.0
description: >-
Multiple of the WAL volume actually measured over one checkpoint_timeout
period to propose as the new max_wal_size. 1.0 proposes exactly the
measured volume, which leaves no room for a busier hour.
- id: max_wal_ceiling_mb
type: INT
defaults: 4096
description: >-
Hard cap in MB on the proposed max_wal_size, whatever the measurement
derives. Keeps a short burst of heavy writes from proposing an absurd
value.
- id: wal_volume_free_mb
type: INT
defaults: 0
description: >-
Free space in MB on the filesystem holding pg_wal, from your own
monitoring. PostgreSQL exposes no free space function, so this number has
to come from outside. Left at 0 the flow refuses to raise anything,
because it cannot prove the volume has room.
- id: wal_disk_fraction
type: FLOAT
defaults: 0.25
description: >-
Largest share of the WAL volume (free space plus what pg_wal already
holds) that max_wal_size may claim. The proposal is clamped to this. A
ceiling at or below the current setting is refused.
- id: dry_run
type: BOOL
defaults: true
description: When true, report the measured checkpoint pressure plus the
max_wal_size the measurement derives, without changing anything.
- id: auto_remediate
type: BOOL
defaults: false
description: >-
When true and dry_run is false, run ALTER SYSTEM SET max_wal_size behind
an approval gate, reload the configuration, then re-read the live setting.
variables:
baseline_key: "ckpt_window_{{ inputs.postgres_url | slugify }}"
empty_baseline: '{"timed":-1,"req":-1,"lsn":"","at":"","reset":""}'
detect_sql: |
WITH b AS (
SELECT
{{ outputs.read_baseline.values.b_timed }}::bigint AS b_timed,
{{ outputs.read_baseline.values.b_req }}::bigint AS b_req,
nullif('{{ outputs.read_baseline.values.b_lsn }}', '')::pg_lsn AS b_lsn,
nullif('{{ outputs.read_baseline.values.b_at }}', '')::timestamptz AS b_at,
nullif('{{ outputs.read_baseline.values.b_reset }}', '') AS b_reset
),
cfg AS (
SELECT
(SELECT setting::bigint FROM pg_settings WHERE name = 'max_wal_size') AS current_max_wal_mb,
(SELECT setting::bigint FROM pg_settings WHERE name = 'checkpoint_timeout') AS checkpoint_timeout_s,
(SELECT context FROM pg_settings WHERE name = 'max_wal_size') AS max_wal_context,
(SELECT source FROM pg_settings WHERE name = 'max_wal_size') AS max_wal_source,
(SELECT pending_restart FROM pg_settings WHERE name = 'max_wal_size') AS max_wal_pending_restart,
(SELECT coalesce(sum(size), 0) FROM pg_ls_waldir()) AS wal_dir_bytes
),
live AS (
SELECT
checkpoints_timed AS now_timed,
checkpoints_req AS now_req,
stats_reset::text AS now_reset,
pg_current_wal_lsn()::text AS now_lsn,
now() AS now_at
FROM pg_stat_bgwriter
),
win AS (
SELECT
b.*, cfg.*, n.*,
(b.b_timed >= 0 AND b.b_at IS NOT NULL) AS baseline_known,
(b.b_timed >= 0 AND (n.now_timed < b.b_timed OR n.now_req < b.b_req
OR (b.b_reset IS NOT NULL AND n.now_reset <> b.b_reset))) AS counters_rolled_back,
GREATEST(EXTRACT(epoch FROM (n.now_at - b.b_at)), 1) AS window_seconds,
CASE WHEN b.b_timed >= 0 THEN GREATEST(n.now_timed - b.b_timed, 0) ELSE 0 END AS timed_delta,
CASE WHEN b.b_req >= 0 THEN GREATEST(n.now_req - b.b_req, 0) ELSE 0 END AS req_delta,
CASE WHEN b.b_lsn IS NOT NULL
THEN GREATEST(pg_wal_lsn_diff(n.now_lsn::pg_lsn, b.b_lsn), 0) ELSE 0 END AS wal_bytes_in_window
FROM b, cfg, live n
),
calc AS (
SELECT
w.*,
w.timed_delta + w.req_delta AS total_delta,
CASE WHEN w.timed_delta + w.req_delta > 0
THEN round(w.req_delta::numeric / (w.timed_delta + w.req_delta), 4) ELSE 0 END AS requested_share,
CASE WHEN w.now_timed + w.now_req > 0
THEN round(w.now_req::numeric / (w.now_timed + w.now_req), 4) ELSE 0 END AS lifetime_requested_share,
round(w.wal_bytes_in_window / 1048576.0 / w.window_seconds, 3) AS wal_mb_per_second,
round(w.wal_dir_bytes / 1048576.0) AS wal_dir_mb,
GREATEST(ceil(w.wal_bytes_in_window / 1048576.0 / w.window_seconds
* w.checkpoint_timeout_s * {{ inputs.headroom_factor }}), 0)::bigint AS wal_per_timeout_mb,
floor({{ inputs.wal_disk_fraction }}
* ({{ inputs.wal_volume_free_mb }} + w.wal_dir_bytes / 1048576.0))::bigint AS disk_ceiling_mb
FROM win w
),
plan AS (
SELECT
c.*,
GREATEST(
LEAST(
GREATEST(c.wal_per_timeout_mb, c.current_max_wal_mb),
{{ inputs.max_wal_ceiling_mb }}::bigint,
c.disk_ceiling_mb
),
c.current_max_wal_mb
) AS proposed_mb
FROM calc c
)
SELECT
current_max_wal_mb,
checkpoint_timeout_s,
max_wal_context,
max_wal_source,
max_wal_pending_restart,
baseline_known,
counters_rolled_back,
round(window_seconds)::bigint AS window_seconds,
timed_delta,
req_delta,
total_delta,
requested_share,
lifetime_requested_share,
now_timed,
now_req,
now_reset,
now_lsn,
now_at::text AS now_at,
wal_bytes_in_window,
wal_mb_per_second,
wal_dir_mb,
wal_per_timeout_mb,
disk_ceiling_mb,
proposed_mb,
(baseline_known AND NOT counters_rolled_back
AND total_delta >= {{ inputs.min_window_checkpoints }}
AND requested_share > {{ inputs.max_requested_share }}) AS pressured,
(proposed_mb > current_max_wal_mb AND max_wal_source <> 'command line'
AND {{ inputs.wal_volume_free_mb }} > 0) AS raise_possible,
CASE
WHEN max_wal_source = 'command line'
THEN format('max_wal_size is pinned on the postmaster command line (pg_settings.source is %L), which outranks postgresql.auto.conf. ALTER SYSTEM would write the new value and the reload would keep serving %s MB. Change the container flag or the service unit instead.', max_wal_source, current_max_wal_mb)
WHEN {{ inputs.wal_volume_free_mb }} <= 0
THEN format('wal_volume_free_mb is 0, so free space on the WAL volume is unknown. Raising max_wal_size is refused rather than guessed. Current setting %s MB.', current_max_wal_mb)
WHEN disk_ceiling_mb <= current_max_wal_mb
THEN format('Disk ceiling %s MB is not above the current %s MB, so there is no headroom to raise into. %s percent of (free %s MB plus pg_wal %s MB) is all max_wal_size may claim.', disk_ceiling_mb, current_max_wal_mb, round(100 * {{ inputs.wal_disk_fraction }}), {{ inputs.wal_volume_free_mb }}, wal_dir_mb)
WHEN wal_per_timeout_mb <= current_max_wal_mb
THEN format('The measured WAL rate produces %s MB per checkpoint_timeout period, which already fits inside the current %s MB, so no raise is derived.', wal_per_timeout_mb, current_max_wal_mb)
ELSE ''
END AS refusal_reason,
format('ALTER SYSTEM SET max_wal_size = %L', proposed_mb::text || 'MB') AS fix_sql
FROM plan
reverify_sql: |
SELECT
current_setting('max_wal_size') AS effective_max_wal_size,
(SELECT setting::bigint FROM pg_settings WHERE name = 'max_wal_size') AS effective_max_wal_mb,
(SELECT pending_restart FROM pg_settings WHERE name = 'max_wal_size') AS pending_restart,
(SELECT context FROM pg_settings WHERE name = 'max_wal_size') AS setting_context,
(
(SELECT setting::bigint FROM pg_settings WHERE name = 'max_wal_size')
< {{ outputs.detect.row.proposed_mb }}::bigint
OR (SELECT pending_restart FROM pg_settings WHERE name = 'max_wal_size')
) AS verify_failed
close_sql: |
SELECT
checkpoints_timed AS now_timed,
checkpoints_req AS now_req,
stats_reset::text AS now_reset,
pg_current_wal_lsn()::text AS now_lsn,
now()::text AS now_at
FROM pg_stat_bgwriter
triggers:
- id: hourly_checkpoint_pressure_window
type: io.kestra.plugin.core.trigger.Schedule
disabled: true
cron: "7 * * * *"
description: >-
Hourly sample of the checkpoint counters. Each run measures the window
since the previous run, so the ratio describes the last hour rather than
the life of the server. Disabled by default and pinned to safe mode so it
reports without changing anything until an operator enables it.
inputs:
dry_run: true
auto_remediate: false
tasks:
- id: read_baseline
type: io.kestra.plugin.core.output.OutputValues
description: >-
Read the counters this flow stored at the end of its previous run from the
namespace KV store. They are the start of the measured window. A missing
key means this is the first run, so the flow records a baseline and judges
nothing.
values:
b_timed: "{{ (kv(render(vars.baseline_key), errorOnMissing=false) ??
vars.empty_baseline) | jq('.timed') | first }}"
b_req: "{{ (kv(render(vars.baseline_key), errorOnMissing=false) ??
vars.empty_baseline) | jq('.req') | first }}"
b_lsn: "{{ (kv(render(vars.baseline_key), errorOnMissing=false) ??
vars.empty_baseline) | jq('.lsn') | first }}"
b_at: "{{ (kv(render(vars.baseline_key), errorOnMissing=false) ??
vars.empty_baseline) | jq('.at') | first }}"
b_reset: "{{ (kv(render(vars.baseline_key), errorOnMissing=false) ??
vars.empty_baseline) | jq('.reset') | first }}"
- id: detect
type: io.kestra.plugin.jdbc.postgresql.Query
description: >-
Read pg_stat_bgwriter, the WAL position, the live settings plus the size
of pg_wal, then derive the window. checkpoints_req counts the checkpoints
WAL volume forced, checkpoints_timed counts the ones checkpoint_timeout
fired. The deltas against the stored baseline give the ratio for this
window only. The WAL bytes written in the same window give the rate the
new max_wal_size is derived from.
url: "{{ inputs.postgres_url }}"
username: "{{ inputs.use_secret_auth ? secret('POSTGRES_USERNAME') :
inputs.postgres_username }}"
password: "{{ inputs.use_secret_auth ? secret('POSTGRES_PASSWORD') :
inputs.postgres_password }}"
fetchType: FETCH_ONE
sql: "{{ render(vars.detect_sql) }}"
- id: log_detect
type: io.kestra.plugin.core.log.Log
message: |
Checkpoint window: {{ outputs.detect.row.window_seconds }}s, {{ outputs.detect.row.req_delta }} requested and {{ outputs.detect.row.timed_delta }} timed checkpoints, requested share {{ outputs.detect.row.requested_share }} against a threshold of {{ inputs.max_requested_share }}.
Lifetime share since stats_reset {{ outputs.detect.row.now_reset }} is {{ outputs.detect.row.lifetime_requested_share }} over {{ outputs.detect.row.now_req }} requested and {{ outputs.detect.row.now_timed }} timed. That number is reported for context and is never judged, because it cannot improve once the history is in it.
WAL written in the window: {{ outputs.detect.row.wal_bytes_in_window }} bytes, {{ outputs.detect.row.wal_mb_per_second }} MB per second.
max_wal_size is {{ outputs.detect.row.current_max_wal_mb }} MB (context {{ outputs.detect.row.max_wal_context }}, source {{ outputs.detect.row.max_wal_source }}, pending_restart {{ outputs.detect.row.max_wal_pending_restart }}). checkpoint_timeout is {{ outputs.detect.row.checkpoint_timeout_s }}s.
Derived need {{ outputs.detect.row.wal_per_timeout_mb }} MB, disk ceiling {{ outputs.detect.row.disk_ceiling_mb }} MB, proposal {{ outputs.detect.row.proposed_mb }} MB.
baseline_known={{ outputs.detect.row.baseline_known }} counters_rolled_back={{ outputs.detect.row.counters_rolled_back }} pressured={{ outputs.detect.row.pressured }} raise_possible={{ outputs.detect.row.raise_possible }}
Mode: dry_run={{ inputs.dry_run }} auto_remediate={{ inputs.auto_remediate }}
- id: guard
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.detect.row.pressured }}"
then:
- id: findings_alert
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: >-
Alert on every detection, in dry run as well as in remediate mode, so
a human sees the checkpoint pressure even when nothing is changed.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "PostgreSQL forced checkpoint guard: {{ outputs.detect.row.req_delta }} of {{ outputs.detect.row.total_delta }} checkpoints in the last {{ outputs.detect.row.window_seconds }}s were forced by WAL volume (share {{ outputs.detect.row.requested_share }}). max_wal_size is {{ outputs.detect.row.current_max_wal_mb }} MB, the measured WAL rate of {{ outputs.detect.row.wal_mb_per_second }} MB/s derives {{ outputs.detect.row.wal_per_timeout_mb }} MB per checkpoint_timeout period, proposal {{ outputs.detect.row.proposed_mb }} MB. dry_run={{ inputs.dry_run }} auto_remediate={{ inputs.auto_remediate }}. {{ outputs.detect.row.refusal_reason }}"}
- id: mode
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.auto_remediate and not inputs.dry_run }}"
then:
- id: headroom
type: io.kestra.plugin.core.flow.If
description: >-
Disk is the real limit on this change. A larger max_wal_size lets
pg_wal grow further before a checkpoint, so the proposal is only
taken forward when it stays inside the operator supplied share of
the WAL volume.
condition: "{{ outputs.detect.row.raise_possible }}"
then:
- id: approval_gate
type: io.kestra.plugin.core.flow.Pause
description: >-
Hold the run until a human approves the new value. The change
is instance wide, it is written to postgresql.auto.conf and it
trades disk for fewer checkpoints. Read the proposal against
the free space on the WAL volume before approving.
pauseDuration: PT2H
behavior: FAIL
onResume:
- id: approved
type: BOOL
defaults: false
description: >-
Set true to run ALTER SYSTEM SET max_wal_size at the
proposed value and reload the configuration. Anything else
leaves the setting alone.
- id: apply
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.approval_gate.onResume.approved ?? false }}"
then:
- id: apply_setting
type: io.kestra.plugin.jdbc.postgresql.Query
description: >-
Write the new value. ALTER SYSTEM appends it to
postgresql.auto.conf, which PostgreSQL reads after
postgresql.conf, so nothing in the main file has to be
edited. allowFailure keeps a rejected statement from
hiding the outcome, because the re-read below is the real
gate.
allowFailure: true
url: "{{ inputs.postgres_url }}"
username: "{{ inputs.use_secret_auth ? secret('POSTGRES_USERNAME') :
inputs.postgres_username }}"
password: "{{ inputs.use_secret_auth ? secret('POSTGRES_PASSWORD') :
inputs.postgres_password }}"
fetchType: NONE
sql: "{{ outputs.detect.row.fix_sql }}"
- id: reload_conf
type: io.kestra.plugin.jdbc.postgresql.Query
description: >-
Signal the postmaster to re-read its configuration files.
max_wal_size has context sighup, so the reload is enough
for it to take effect and no restart is scheduled by this
flow.
allowFailure: true
url: "{{ inputs.postgres_url }}"
username: "{{ inputs.use_secret_auth ? secret('POSTGRES_USERNAME') :
inputs.postgres_username }}"
password: "{{ inputs.use_secret_auth ? secret('POSTGRES_PASSWORD') :
inputs.postgres_password }}"
fetchType: FETCH_ONE
sql: SELECT pg_reload_conf() AS reloaded
- id: settle
type: io.kestra.plugin.core.flow.Sleep
description: >-
pg_reload_conf() returns before the postmaster has
processed the signal, so a read fired in the same instant
can still see the old value. Two seconds removes that race
from the verification.
duration: PT2S
- id: reverify
type: io.kestra.plugin.jdbc.postgresql.Query
description: >-
Read the live value back with current_setting, plus
pending_restart to prove the reload was enough. This step
proves the setting changed. It does not prove the pressure
is gone, which is what the next window measures.
url: "{{ inputs.postgres_url }}"
username: "{{ inputs.use_secret_auth ? secret('POSTGRES_USERNAME') :
inputs.postgres_username }}"
password: "{{ inputs.use_secret_auth ? secret('POSTGRES_PASSWORD') :
inputs.postgres_password }}"
fetchType: FETCH_ONE
sql: "{{ render(vars.reverify_sql) }}"
- id: verify_result
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.reverify.row.verify_failed }}"
then:
- id: remediation_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: >-
max_wal_size did not land. Wanted at least {{
outputs.detect.row.proposed_mb }} MB, read back {{
outputs.reverify.row.effective_max_wal_mb }} MB
(current_setting reports {{
outputs.reverify.row.effective_max_wal_size }},
pending_restart {{
outputs.reverify.row.pending_restart }}, source at
detection time {{ outputs.detect.row.max_wal_source
}}). ALTER SYSTEM needs a superuser or a role granted
SET on that parameter, pg_reload_conf needs the same,
so a role without either leaves the value untouched
while the statements fail. A value pinned on the
postmaster command line also outranks
postgresql.auto.conf, so the write lands in the file
while the live value never moves.
else:
- id: remediated
type: io.kestra.plugin.core.log.Log
message: |
max_wal_size moved from {{ outputs.detect.row.current_max_wal_mb }} MB to {{ outputs.reverify.row.effective_max_wal_mb }} MB. current_setting reports {{ outputs.reverify.row.effective_max_wal_size }}, setting context is {{ outputs.reverify.row.setting_context }} and pending_restart is {{ outputs.reverify.row.pending_restart }}, so the reload was enough and no restart is needed.
What this proves: the value changed. What it does not prove: that the pressure is gone. The pg_stat_bgwriter counters are cumulative, so the ratio only improves in the windows measured from here. The baseline recorded at the end of this run is the start of the first window that measures the fix.
The trade is explicit. Fewer checkpoints, each flushing more, more WAL kept between them and a longer crash recovery.
else:
- id: not_approved
type: io.kestra.plugin.core.log.Log
message: "Approval not granted, so max_wal_size stays at {{
outputs.detect.row.current_max_wal_mb }} MB and WAL volume
keeps forcing checkpoints."
else:
- id: headroom_refused
type: io.kestra.plugin.core.log.Log
message: |
Refusing to raise max_wal_size. {{ outputs.detect.row.refusal_reason }}
Nothing was changed. Fix the cause named above, then run this again. Shortening checkpoint_timeout is not the way out, because that fires more checkpoints rather than fewer.
else:
- id: report_only
type: io.kestra.plugin.core.log.Log
message: "Report only (dry_run={{ inputs.dry_run }}, auto_remediate={{
inputs.auto_remediate }}). max_wal_size would move from {{
outputs.detect.row.current_max_wal_mb }} MB to {{
outputs.detect.row.proposed_mb }} MB. No change made. {{
outputs.detect.row.refusal_reason }}"
else:
- id: clean
type: io.kestra.plugin.core.log.Log
message: |
No checkpoint pressure to act on. Window {{ outputs.detect.row.window_seconds }}s with {{ outputs.detect.row.req_delta }} requested of {{ outputs.detect.row.total_delta }} checkpoints, requested share {{ outputs.detect.row.requested_share }}.
baseline_known={{ outputs.detect.row.baseline_known }} counters_rolled_back={{ outputs.detect.row.counters_rolled_back }}. A first run or a stats reset records a fresh baseline and judges nothing. A window under {{ inputs.min_window_checkpoints }} checkpoints is too small to read.
- id: close_window
type: io.kestra.plugin.jdbc.postgresql.Query
description: >-
Read the counters again at the end of the run. These become the start of
the next window, so after a remediation the next run measures only post
change behaviour.
url: "{{ inputs.postgres_url }}"
username: "{{ inputs.use_secret_auth ? secret('POSTGRES_USERNAME') :
inputs.postgres_username }}"
password: "{{ inputs.use_secret_auth ? secret('POSTGRES_PASSWORD') :
inputs.postgres_password }}"
fetchType: FETCH_ONE
sql: "{{ render(vars.close_sql) }}"
- id: record_baseline
type: io.kestra.plugin.core.kv.Set
description: >-
Store the closing counters, the WAL position plus the stats_reset stamp
under a key derived from the target URL. No TTL, because an expiring
cursor would silently restart the window.
key: "{{ render(vars.baseline_key) }}"
value: "{{ {'timed': outputs.close_window.row.now_timed, 'req':
outputs.close_window.row.now_req, 'lsn': outputs.close_window.row.now_lsn,
'at': outputs.close_window.row.now_at, 'reset':
outputs.close_window.row.now_reset} | toJson }}"
errors:
- id: alert_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: >-
Post to Slack on any failure, including a connection error, a rejected
ALTER SYSTEM or a max_wal_size that did not change after the reload.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "PostgreSQL forced checkpoint max_wal_size guard FAILED (execution {{ execution.id }}). A connection error, a rejected ALTER SYSTEM or a setting that did not move after pg_reload_conf. Check the run."}