Load a scheduled Apify web scrape into Postgres
Run an Apify actor on a schedule, fetch its dataset, reshape the records, and bulk-load them into Postgres with cost caps and Slack alerts.
Detect invalid PostgreSQL indexes after failed concurrent builds, check duplicates, REINDEX CONCURRENTLY, drop _ccnew leftovers and re-audit.
CREATE INDEX CONCURRENTLY and REINDEX CONCURRENTLY do not roll back when they fail. A statement timeout, a cancelled migration or a duplicate key leaves the index in the catalog, marked INVALID. From then on, PostgreSQL updates it on every insert and update but never uses it for a query. The table is slower to write, and the index the migration was meant to add is missing. Nothing reports it: the migration tool logged one error, and the next deploy moved on.
This blueprint lists every invalid index. It alerts Slack, and can rebuild each one without blocking writes. A unique index is rebuilt only after a check that no duplicate key is left, since otherwise the rebuild would fail and leave yet another invalid index. A second audit verifies the result.
This blueprint was created by zkasuran.
| Kind | What it is | Fix |
|---|---|---|
INVALID |
An index left by a failed CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY |
REINDEX INDEX CONCURRENTLY |
INVALID on a unique index with duplicates |
The build failed on duplicate keys, which are still there | Skipped, with the number of duplicate keys to fix first |
LEFTOVER_CCNEW |
The <name>_ccnew copy left by a cancelled REINDEX CONCURRENTLY |
DROP INDEX CONCURRENTLY |
audit (io.kestra.plugin.jdbc.postgresql.Query, read-only) returns every index with indisvalid = false, outside system schemas. For each one it returns:regclass so names are quoted by PostgreSQL;summary logs every finding.route (io.kestra.plugin.core.flow.If):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) handles one index at a time. For a unique index, duplicates counts duplicate keys, and fix runs only when there are none.reaudit runs the audit again. verify_result fails if an index reported as fixed is still invalid, a new invalid index appeared, or an iteration did not report a result.skipped_notice posts the indexes that were skipped or failed.errors alerts Slack when the audit cannot run._ccnew copies that cancelled rebuilds leave behind.Finding invalid indexes is one query, but fixing them safely is a sequence: check duplicates first, rebuild one index at a time so a failure stays isolated, then check again. Kestra runs that sequence on a schedule, behind two gates. It reports skipped indexes to the owners, and keeps an execution history of what was rebuilt where.
On Kestra 2.0.5 OSS against PostgreSQL 16, with a local HTTP sink standing in for Slack:
orders (200,000 rows) has a unique index on ref that failed on 10 duplicate keys;events (600,000 rows) has an index whose concurrent build was cancelled by a statement timeout.| Run | Inputs | Result |
|---|---|---|
| 1 | defaults | 2 invalid indexes reported, alert sent |
| 2 | auto_remediate: true, dry_run: false |
events_payload_idx rebuilt and valid. orders_ref_uq skipped: 10 duplicate keys, Slack notice sent. Verified |
| 3 | duplicates deleted, and a REINDEX CONCURRENTLY cancelled by a timeout |
Reports orders_ref_uq and the leftover events_payload_idx_ccnew |
| 4 | same, dry run | Nothing changed: 2 invalid indexes still in pg_index |
| 5 | same, remediate | _ccnew dropped, orders_ref_uq rebuilt. psql shows both valid, 0 invalid left |
| 6 | defaults | Clean |
| 7 | unreachable database | errors alert sent |
REINDEX CONCURRENTLY.docker run -d --name pg -p 5432:5432 -e POSTGRES_PASSWORD=pg postgres:16;CREATE UNIQUE INDEX CONCURRENTLY on it.POSTGRES_USERNAME / POSTGRES_PASSWORD: credentials used by every query.SLACK_WEBHOOK_URL: used by alert, skipped_notice and the errors block.SECRET_.| Input | Default | Purpose |
|---|---|---|
jdbc_url |
jdbc:postgresql://localhost:5432/app |
Database |
auto_remediate |
false |
Allow rebuilds and drops |
dry_run |
true |
Must be false for a statement to run |
POSTGRES_USERNAME, POSTGRES_PASSWORD and SLACK_WEBHOOK_URL secrets.jdbc_url.auto_remediate: true and dry_run: false, outside peak hours: a concurrent rebuild reads the whole table.daily trigger, which always runs in safe mode.Pause for approval before rebuilding indexes on very large tables.