Schedule icon
Query icon
OutputValues icon
Log icon
If icon
SlackIncomingWebhook icon
Pause icon
Loop icon
Fail icon

PostgreSQL Disabled Event Trigger Recovery Guard

Detect PostgreSQL event triggers left disabled after a migration, enable them behind an approval gate, then re-read evtenabled to confirm.

Categories
DataInfrastructure

An event trigger is how a team enforces DDL policy inside the database. It audits every schema change, refuses an unsafe DROP, records a migration. Someone runs ALTER EVENT TRIGGER ... DISABLE to push a migration through, the migration lands, the re-enable is forgotten. From then on the policy is off and nothing reports it, because a disabled event trigger looks exactly like one that was never created from the application's point of view: no error, no warning, no row in the audit table.

This guard reads pg_event_trigger, treats evtenabled = 'D' as the fault, enables the targeted triggers again behind a dry run plus an approval gate, then re-reads evtenabled and fails the run if any target did not reach the requested state.

This blueprint was created by zkasuran.

Findings

  • Re-enabling an event trigger does NOT audit the DDL that ran while it was off. Those commands are gone. That is the whole reason the gap matters, so the Slack alert fires on every detection, including a run that fixed the state. The success log says the finding stays open.
  • PostgreSQL records no timestamp for the disable, so the catalog cannot tell you how long the guard was off. Treat every detection as an unbounded audit gap until a human checks the migration history.
  • evtenabled has four states, not two. O fires in origin and local sessions, A always including replica sessions, R in replica sessions only, D never. Only D is the fault here. An R trigger is inert on a primary by design, which is a deliberate choice on a replica rather than a break, so the flow reports it and never changes it.

Safety

  • Read-only by default. dry_run is true and auto_remediate is false, so a plain run reports and never enables anything.
  • A change happens only when auto_remediate is true and dry_run is false. It then waits for a human to resume the Pause with approved set true. An unanswered gate reads as not approved.
  • Enabling is deliberately gated because it has an immediate effect. An event trigger that blocks an unsafe DROP or rewrites DDL starts firing on the next statement, so a migration running at that moment can start failing. Read the dry-run list first.
  • desired_state decides what the trigger is restored to, O or A, instead of the flow guessing. A trigger that was ENABLE ALWAYS before someone disabled it comes back as ALWAYS only when you ask for it.
  • desired_triggers narrows the pass to a named allowlist, so a trigger that is meant to stay disabled is never touched.
  • The re-read of evtenabled is the real gate. ALTER EVENT TRIGGER needs the trigger owner or a superuser, so a connecting role without that right produces a failed run rather than a false success.

How it works

  1. detect (io.kestra.plugin.jdbc.postgresql.Query) reads pg_event_trigger joined to pg_proc, keeps the rows at evtenabled = 'D' that match the allowlist, then returns the handler function, the command tags the trigger filters on and the exact ALTER EVENT TRIGGER statement that restores the requested state.
  2. roster reads every event trigger with its state decoded, plus a count per state, so the log shows the O, A and R triggers next to the D ones.
  3. summarize (io.kestra.plugin.core.output.OutputValues) builds the target name list and the JSON the remediation loop iterates.
  4. log_state (io.kestra.plugin.core.log.Log) logs the count, the targets, the full roster and the mode.
  5. guard (io.kestra.plugin.core.flow.If) branches on the count. With none found, clean logs the roster and the run ends. Otherwise findings_alert posts the gap to Slack on every detection.
  6. mode (io.kestra.plugin.core.flow.If) enters the remediation branch only when auto_remediate is true and dry_run is false. Otherwise report_only lists what would be enabled.
  7. approval_gate (io.kestra.plugin.core.flow.Pause) holds the run for a human. enable_triggers (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) then runs one ALTER EVENT TRIGGER per target.
  8. reverify re-reads evtenabled for every target and returns only the ones that did not reach the requested state, counting a dropped trigger as absent. verify_result fails the run through io.kestra.plugin.core.execution.Fail when that list is not empty.

What you get

  • A check for a silent failure mode that nothing else reports. A disabled event trigger raises no error and writes no row, so the only evidence is the catalog.
  • Honest handling of all four evtenabled states, so a replica-only trigger is reported rather than quietly switched on a primary.
  • The ALTER statement built inside PostgreSQL with format('%I'), so the trigger name is quoted by the database.
  • An operator-chosen target state, O or A, instead of an assumption about what the trigger held before.
  • A real verification step. The run passes only when the second read of evtenabled shows every target in the requested state.
  • Slack alerts on the findings and on any failure, with the alert naming the part a fix cannot undo.

Who it's for

  • DBAs and platform engineers who use event triggers to audit DDL or block unsafe commands, then need to know the guard is still armed.
  • Teams whose migration runbook disables an event trigger on purpose, where the re-enable depends on somebody remembering.
  • Security and compliance teams who need evidence that the in-database DDL audit was running, not just installed.

Why orchestrate this with Kestra

PostgreSQL will not tell you that one of its own policy triggers is off. There is no scheduler for the check, no approval step before flipping something that changes how DDL behaves, no proof that an ALTER took effect. Kestra adds each piece. It schedules the sweep, keeps it read-only until an operator asks for a change, holds the change behind a human approval, builds the ALTER inside the database so names are quoted correctly, re-reads evtenabled to turn a hopeful fix into a verified one, then posts to Slack both when a gap is found and when a step fails.

Tested end to end

Run on Kestra 2.0.5 against a PostgreSQL 16 server, with a local HTTP sink standing in for Slack. The database was seeded with three event triggers on ddl_command_end, each with its own handler writing a labelled row to a ddl_audit table: one audit trigger, one policy trigger that stays enabled, one deliberately set to ENABLE REPLICA.

  • The audit trigger was proven to work first. A CREATE TABLE produced a ddl_audit row from both enabled triggers. After ALTER EVENT TRIGGER audit_ddl_end DISABLE, the same CREATE TABLE produced a row from the still enabled trigger and nothing from the disabled one. That is the gap this guard closes.
  • Detector can fail: against the healthy state (O, O, R) it returned 0. After one trigger was disabled it returned exactly 1, never the enabled trigger nor the replica-only one, confirmed with psql against pg_event_trigger.
  • Dry run reported the finding, alerted and left evtenabled at D.
  • Remediate with approval ran ALTER EVENT TRIGGER ... ENABLE and the re-read reported 0 targets off the requested state. A CREATE TABLE after the run wrote the audit row again. The enabled trigger and the replica-only trigger were untouched.
  • A role that could read the catalog but did not own the trigger had its ALTER rejected, the re-read still found the target at D and the run failed and alerted.
  • Declining the gate left the trigger disabled.
  • An unreachable host failed the first query and fired the Slack alert.

Prerequisites

A PostgreSQL server reachable over JDBC. The connecting user must be able to read pg_event_trigger, which any role can. It must also own the triggers it enables, which usually means a superuser since creating an event trigger is a superuser action.

Secrets

  • POSTGRES_USERNAME: PostgreSQL user, read when use_secret_auth is true.
  • POSTGRES_PASSWORD: password for that user.
  • SLACK_WEBHOOK_URL: used by findings_alert and the errors block.
  • In Kestra OSS, provide each secret as a base64-encoded environment variable prefixed SECRET_.

Inputs

Name Type Default Description
postgres_url STRING jdbc:postgresql://localhost:5432/postgres JDBC URL for the target database.
use_secret_auth BOOL true Read credentials from secrets. Set false only for inline test credentials.
desired_triggers STRING "" Comma separated allowlist of trigger names. Empty targets every disabled event trigger.
desired_state SELECT origin origin restores evtenabled O, always restores A.
dry_run BOOL true Report without enabling anything.
auto_remediate BOOL false Enable behind the approval gate when dry_run is false.

Quick start

  1. Set POSTGRES_USERNAME, POSTGRES_PASSWORD and SLACK_WEBHOOK_URL as namespace secrets.
  2. Import this flow.
  3. Run it with postgres_url pointed at a database that has event triggers. The default dry run reports any that sit at evtenabled = 'D'.
  4. To enable them, run again with dry_run false and auto_remediate true, then resume the paused execution with approved true.
  5. Read the dry-run list before approving. An event trigger that blocks DDL starts refusing commands again straight away.

How to extend

  • Set desired_triggers to the names your runbook disables during a migration, so the sweep is scoped to exactly the guards that must come back.
  • Switch desired_state to always for a trigger that must fire in replica sessions too.
  • Fail the run as soon as detection finds anything, for a read-only policy check with no remediation at all.
  • Add a second pass that reads the ddl_command_end audit table and compares its newest entry with the migration log, to narrow the window of DDL that was never audited.
  • Point the same pattern at pg_trigger.tgenabled to cover per-table triggers, which use ALTER TABLE ... ENABLE TRIGGER and are a different object.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.