Apache Pulsar multi-tenant backlog quarantine and priority event router
Drain a multi-tenant Pulsar topic and route each batch by tenant priority tier, logging ENTERPRISE traffic and quarantining the rest, with a Slack report per run.
Detect CockroachDB indexes no query reads, guard against reset usage counters, drop them behind an approval and re-read the index list.
A secondary index that no query reads still costs you on every write. Each INSERT, UPDATE and DELETE on the table maintains it, it takes storage, it is replicated across the cluster, it is backed up with the table. Indexes pile up from abandoned features, from experiments nobody cleaned up and from someone adding one just in case. Nothing in a database tells you an index is dead weight. The write amplification shows up as a slightly slower cluster, with no statement to blame.
CockroachDB does keep the answer. crdb_internal.index_usage_statistics holds a total_reads counter per index. A count of zero means the optimizer has not chosen that index once since the counters started. This blueprint reads it for every index on every base table in the connected database, classifies each one, drops the unused ones behind a dry run and a human approval, then reads the catalog again and fails the run if a target is still there.
The counters are the hard part, so most of the flow is about not trusting them.
This blueprint was created by zkasuran.
total_reads is zero for every index in the cluster, which reads exactly like "nothing uses any of these". Acting on that would drop the whole secondary index set of a healthy database. So the first task reads crdb_internal.gossip_nodes and takes the newest started_at across the cluster, because the youngest node is the age of the evidence. Below min_observation_hours the run reports and refuses, then says which node start it is reading from.DROP INDEX orders@uq_orders_ref on a table holding one row per order_ref, a plain INSERT of a duplicate order_ref was accepted. No error, no warning, no CASCADE needed. The pg_constraint row disappeared along with the index. An index built with ALTER TABLE ... ADD CONSTRAINT ... UNIQUE behaves the same way. The is_unique filter in this flow is not a nicety on top of an engine guard. It is the only thing standing in front of silent data corruption.DROP INDEX does refuse the primary index, with cannot drop the primary index of a table using DROP INDEX. That is the one case where the engine protects you, which makes it the one case where this flow's own filter is belt and braces rather than the belt.pg_constraint.conindid is not usable for this on CockroachDB. Two different constraints on two different tables came back with the same conindid value, so joining an index to its constraint through that column is wrong. Uniqueness is read from crdb_internal.table_indexes.is_unique, which is the index's own property. Foreign key coverage is derived from conkey and confkey against the index key columns in crdb_internal.index_columns.NOT VISIBLE tells the optimizer to ignore the index, so of course nothing reads it. Making an index invisible is the right rehearsal before dropping it, but the counter from that period is evidence of the setting, not of the workload. These are reported as INVISIBLE_UNPROVEN and never dropped. Make it visible again, wait out the window, then let the guard decide.crdb_internal.tables is cluster wide but lists views with no type column, while information_schema.tables has the type for the connected database only. The scope query joins the two and keeps BASE TABLE, which is also why one run audits one database, the one named in the JDBC URL.index_type = 'primary', is_unique, any index whose key columns cover a foreign key's columns on either side of the reference, plus any index with is_visible = false.min_index_age_hours keeps a new index out of the drop set. An index created yesterday has no reads because nothing has had the chance to use it yet.auto_remediate is true, dry_run is false and the approval Pause is answered true. An unanswered Pause fails the run after two hours.max_indexes_dropped bounds one pass, oldest index first, so a database with forty dead indexes is worked through over successive runs and each pass is small enough to reason about.concurrencyLimit: 1, so two schema changes never collide on the same table.DROP INDEX <table>@<index> against names read from the catalog. No CASCADE, no table rewrite, no setting change, no user data touched.uptime (io.kestra.plugin.jdbc.postgresql.Query, read-only) reads crdb_internal.gossip_nodes and returns the newest node start, the counter window in hours and whether that window clears min_observation_hours.
audit reads one row per index on every base table in scope, joining crdb_internal.table_indexes, crdb_internal.index_columns, crdb_internal.index_usage_statistics and pg_catalog.pg_constraint, then gives each index a verdict.
| Verdict | Meaning |
|---|---|
UNUSED |
Secondary, not unique, no foreign key coverage, visible, old enough, zero reads. The only droppable class |
IN_USE |
total_reads above zero, so the optimizer has chosen it |
PRIMARY_PROTECTED |
The table's primary index |
UNIQUE_PROTECTED |
Enforces uniqueness, which is a correctness guarantee |
FOREIGN_KEY_PROTECTED |
Covers a foreign key's columns, so the constraint check depends on it |
INVISIBLE_UNPROVEN |
NOT VISIBLE, so its zero read count describes the setting rather than the workload |
TOO_NEW |
Created inside min_index_age_hours |
summary logs every index with its read count, its last read, its age, its key columns and its verdict, so a run is readable without opening the database.
route (io.kestra.plugin.core.flow.If) splits on whether anything is unused or the counters are untrusted.
alert posts the unused list, the protected list and the counter age to Slack.window_gate is the guard that matters. With the counters younger than min_observation_hours, refuse_window logs the refusal with the node start it read, then the run ends without touching anything.
remediate_gate runs only when auto_remediate is true and dry_run is false. Otherwise no_change prints the exact statements it would have issued plus the indexes it would have refused.
approval_gate (io.kestra.plugin.core.flow.Pause) holds the run for a human. remediate_drop (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) then issues one DROP INDEX <table>@<index> per target, up to the cap.
verify reads the index catalog again and verify_result fails the run through io.kestra.plugin.core.execution.Fail when a target survived or a protected index went missing.
errors alerts Slack when the audit itself cannot run.
The signal here is only valid if it has been accumulating, which makes this a schedule rather than an alert. Kestra runs the audit weekly, keeps each run with its numbers so you can see an index stay at zero reads across passes, posts the findings to Slack and holds the drop behind auto_remediate, dry_run and a Pause that a human answers. The re-read after the drop makes the run report success only once the catalog agrees. The per run cap lets a schedule work through a large backlog a few indexes at a time.
On Kestra 2.0.5 OSS against a CockroachDB v24.1 single node cluster reached with the PostgreSQL plugin, with a local HTTP sink standing in for Slack. The seed carried a table with a primary key, one secondary index nothing ever queried, one secondary index queried on purpose so its counter climbed, a unique index and an index covering a foreign key. Every result was read back in the cockroach client, from SHOW INDEXES FROM plus crdb_internal.table_indexes joined to crdb_internal.index_usage_statistics, never from the Kestra log.
| Run | Cluster state | Result |
|---|---|---|
| 1 | 9 indexes, counters 9 minutes old against a 168h window | REFUSED. The counter age plus the node start it read are named, nothing dropped |
| 2 | same cluster in full write mode (auto_remediate: true, dry_run: false), 2 targets found, window still 168h |
REFUSED again. The approval Pause was never reached |
| 3 | same, min_observation_hours: 0 |
2 UNUSED, 2 IN_USE at 2 reads plus 1 read, 3 PRIMARY_PROTECTED, 1 UNIQUE_PROTECTED, 1 FOREIGN_KEY_PROTECTED. Alert sent, no change |
| 4 | same, auto_remediate: true, dry_run: true |
Both statements printed, all 9 indexes still in the SQL client |
| 5 | same, dry_run: false, Pause answered false |
Nothing dropped, catalog unchanged |
| 6 | same, Pause answered true, max_indexes_dropped: 1 |
idx_orders_region dropped, the second target reported for the next pass, re-read clean, 8 indexes left |
| 7 | the next pass at cap 3 | idx_items_sku dropped, 7 left. The unique index, the foreign key index, the read index plus all 3 primaries still present in the client |
| 8 | 7 indexes, every one read or protected | Clean. 0 findings |
| 9 | an index added NOT VISIBLE |
INVISIBLE_UNPROVEN, reported, never offered for dropping |
| 10 | 2 unused again, run as a role holding SELECT but no privilege to drop | Both DROP INDEX rejected, the re-read found both survivors, the run FAILED. errors alert sent |
| 11 | the same 10 indexes with one read given to each unused index | 0 findings on the schema that reported 2 the run before |
| 12 | unreachable host | errors alert sent |
jdbc_url.crdb_internal.index_usage_statistics, crdb_internal.table_indexes, crdb_internal.index_columns and crdb_internal.gossip_nodes. The node list is admin scoped, so grant the VIEWCLUSTERMETADATA system privilege or use a member of admin. Dropping an index needs CREATE on the table.min_observation_hours longer than your slowest reporting cycle. A monthly report that is the only reader of an index will make it look dead for 29 days of every month.COCKROACH_PASSWORD: the SQL password, read by the password input's default. An insecure local node ignores it.SLACK_WEBHOOK_URL: used by alert and the errors block.SECRET_.| Input | Default | Purpose |
|---|---|---|
jdbc_url |
jdbc:postgresql://localhost:26257/defaultdb?sslmode=require |
Any node plus the database to audit |
db_user |
root |
The role the queries connect as |
password |
{{ secret('COCKROACH_PASSWORD') }} |
SQL password |
include_schemas |
empty | Comma separated schemas, empty means every user schema |
min_observation_hours |
168 |
How long the usage counters must have been accumulating before a drop is allowed |
min_index_age_hours |
168 |
An index younger than this is reported, never dropped |
max_indexes_dropped |
3 |
Per run ceiling on DROP INDEX |
auto_remediate |
false |
Allow the drop |
dry_run |
true |
Must be false before anything is dropped |
COCKROACH_PASSWORD and SLACK_WEBHOOK_URL secrets.jdbc_url to a node, with the database you want audited in the path.UNUSED list and the protected list beside it. Check the protected list looks right for your schema before trusting the other one.auto_remediate: true and dry_run: false, then answer the Pause. Start with max_indexes_dropped: 1.weekly trigger, which always runs in safe mode.NOT VISIBLE first instead of dropping it, wait a cycle, then drop it. The flow already refuses to drop an invisible index, so an invisible index that nobody has complained about is a cleaner handover between the two passes.create_statement from crdb_internal.table_indexes into the Slack message or a file before the drop, so rebuilding an index someone turns out to need is a copy and paste.crdb_internal.ranges so the Slack message leads with the indexes whose removal frees the most.