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

PostgreSQL Publication Replica Identity Guard

Find published PostgreSQL tables with no replica identity, which reject UPDATE and DELETE, alert Slack, set USING INDEX or FULL and verify.

Categories
DataInfrastructure

Adding a table to a logical replication publication, for CDC with Debezium, a read replica or a migration, has a sharp edge. If the publication publishes updates or deletes and the table has no primary key, every UPDATE and DELETE on it starts failing. PostgreSQL raises cannot update table because it does not have a replica identity and publishes updates. It is the application that breaks, not the replication, and usually right after someone ran CREATE PUBLICATION ... FOR ALL TABLES.

This blueprint finds the published tables that are in that state, or about to be. It alerts Slack, and can set the right replica identity:

  • USING INDEX on a unique index whose columns are all NOT NULL, when one exists;
  • FULL otherwise.

A second audit verifies the result.

This blueprint was created by zkasuran.

Findings

Finding Severity Rule Fix
NO_REPLICA_IDENTITY High In a publication that publishes UPDATE or DELETE, with no primary key and identity DEFAULT, or identity NOTHING REPLICA IDENTITY USING INDEX on the best unique not-null index, else FULL (if allow_full)
FULL_WITH_KEY Medium Identity FULL, although a unique not-null index exists Report only: USING INDEX would write much less WAL

Tables in insert-only publications are never flagged: they are not affected.

How it works

  1. audit (io.kestra.plugin.jdbc.postgresql.Query, read-only) joins pg_publication, pg_publication_tables and pg_class, and looks for a candidate index:

    • unique, valid, immediate, not partial, no expressions;
    • all its columns NOT NULL.

    It returns each finding with its fix, built with regclass and format('%I').

  2. summary logs every finding with its publications, current identity and row count.

  3. route (io.kestra.plugin.core.flow.If):

    • clean: logs it.
    • findings: alert posts to Slack.
      • remediate_gate runs only when auto_remediate is true and dry_run is false.
      • remediate (io.kestra.plugin.core.flow.Loop) runs each ALTER TABLE ... REPLICA IDENTITY in its own task.
      • reaudit and verify_result fail if a fixed table still has no replica identity.
  4. errors alerts Slack when the audit cannot run.

What you get

  • Early warning, before or right after a publication change, of every table whose writes will fail.
  • The lightest fix available per table: an existing unique index first, FULL only as a fallback.
  • A second audit as proof, and a report of tables paying for FULL when they do not need to.

Who it's for

  • Teams running Debezium or other CDC tools on PostgreSQL.
  • Teams using logical replication for read replicas, zero-downtime upgrades or migrations.
  • Teams evaluating the PostgreSQL plugin who want an example of catalog queries driving ALTER TABLE.

Why orchestrate this with Kestra

The failure shows up as application errors, not as a replication alert. Kestra checks the publications every hour or after each schema change, and turns findings into a Slack alert naming the tables and publications. It applies the fix only behind auto_remediate and dry_run, verifies it, and keeps a history of every identity change.

Tested end to end

On Kestra 2.0.5 OSS against PostgreSQL 16, with a local HTTP sink standing in for Slack. The publication cdc_pub holds:

  • cdc_events (50,000 rows, no key at all);
  • cdc_tags (20,000 rows, no primary key but a unique index on two NOT NULL columns);
  • cdc_users (primary key);
  • cdc_ledger (FULL, with a unique not-null index).

An insert-only publication holds cdc_audit, with no key.

Before the fix, UPDATE cdc_events and DELETE FROM cdc_tags failed with the replica identity error.

Run Inputs Result
1 defaults 2 high findings: cdc_events with fix FULL, cdc_tags with fix USING INDEX cdc_tags_uq. cdc_ledger medium. cdc_users and cdc_audit not flagged. Alert sent
2 auto_remediate: true, dry_run: true Same report, nothing changed
3 auto_remediate: true, dry_run: false Both fixes applied and verified. pg_class.relreplident shows f and i, and the same UPDATE and DELETE now succeed
4 defaults Only the FULL_WITH_KEY report left
5 cdc_ledger switched to USING INDEX by hand Clean
6 unreachable database errors alert sent

Prerequisites

  • For the audit, any role can read the catalog. For remediation, the owner of the tables. ALTER TABLE ... REPLICA IDENTITY takes a brief lock.
  • Local testing:
    • docker run -d --name pg -p 5432:5432 -e POSTGRES_PASSWORD=pg postgres:16;
    • create a table with no primary key, then CREATE PUBLICATION p FOR TABLE t and try an UPDATE.

Secrets

  • POSTGRES_USERNAME / POSTGRES_PASSWORD: credentials used by every query.
  • SLACK_WEBHOOK_URL: used by alert and the errors block.
  • In Kestra OSS, provide each one as a base64-encoded environment variable prefixed SECRET_.

Inputs

Input Default Purpose
jdbc_url jdbc:postgresql://localhost:5432/app Database
allow_full true Use FULL when no index fits
auto_remediate false Allow ALTER TABLE
dry_run true Must be false for a statement to run

Quick start

  1. Add the POSTGRES_USERNAME, POSTGRES_PASSWORD and SLACK_WEBHOOK_URL secrets.
  2. Import the flow and set jdbc_url.
  3. Run it once and read the findings.
  4. Run it with auto_remediate: true and dry_run: false.
  5. Enable the hourly trigger, which always runs in safe mode, or call the flow after each migration.

How to extend

  • Run the flow before ALTER PUBLICATION ... ADD TABLE in your migration pipeline, and stop the pipeline on a finding.
  • Add a primary key instead of FULL when a column is unique in practice, after a duplicate check.
  • Loop over a list of JDBC URLs to check every publisher.
  • Report the WAL volume of FULL tables from pg_stat_replication_slots.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.