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

CockroachDB Unused Index Drop Guard

Detect CockroachDB indexes no query reads, guard against reset usage counters, drop them behind an approval and re-read the index list.

Categories
DataInfrastructure

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.

Findings

  • The counters reset when a node restarts. Nothing in the view says so. They live in each node's memory. After a rolling restart, a version upgrade or a pod reschedule, 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.
  • CockroachDB v24.1 will drop an index backing a UNIQUE constraint without any complaint. The uniqueness goes with it. Measured: after 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.
  • An index covering a foreign key is dropped just as quietly. The constraint stays correct while becoming unbounded work. After the index on the referencing column went away, the foreign key was still enforced, so an insert of a missing parent key was still rejected. The lookup behind that check just stopped being a point read. Every write to the child table, plus any cascade on the parent, now scans. That is why the flow refuses these too.
  • 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.
  • An invisible index cannot earn a zero read verdict. 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.

Safety

  • Four classes of index are never dropped. Each is a predicate in the SQL rather than a line in this document: 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.
  • The foreign key test is deliberately wider than "the index the planner would pick". An index is refused when the foreign key's column set is contained in the index's own key columns, so a composite index that merely happens to cover a reference is kept. Over-refusing wastes an index. Under-refusing turns every constrained write into a scan.
  • 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.
  • Nothing is dropped unless 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.
  • The drop loop runs with concurrencyLimit: 1, so two schema changes never collide on the same table.
  • The only statement the flow ever writes is DROP INDEX <table>@<index> against names read from the catalog. No CASCADE, no table rewrite, no setting change, no user data touched.
  • The re-read checks both directions. Every target must be gone and every protected index must still be present. A protected index that vanished during the run fails it.

How it works

  1. 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.

  2. 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
  3. 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.

  4. route (io.kestra.plugin.core.flow.If) splits on whether anything is unused or the counters are untrusted.

    • clean: logs that every index has been read or is protected.
    • findings: alert posts the unused list, the protected list and the counter age to Slack.
  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. errors alerts Slack when the audit itself cannot run.

What you get

  • The indexes in your cluster that no query has read, with the read count, the last read time and the creation time beside each one, so the verdict is auditable rather than asserted.
  • A refusal you can trust after a restart. The run tells you the counters are too young and names the node start it read, instead of handing you a list of every index in the database.
  • The indexes it will not touch, with the reason: primary, unique, foreign key, invisible, too new. That list is the proof the detector is precise, not just loud.
  • A drop that is safe to leave scheduled. It reports until an operator turns on remediation and answers the approval for that run.
  • A before and after read of the catalog, so a green run means the indexes are actually gone and the protected ones are actually still there.

Who it's for

  • Teams on CockroachDB whose write latency has crept up with no deploy to point at, which is what a pile of unread indexes looks like from the outside.
  • Anyone inheriting a schema with indexes nobody can explain, where the honest question is which ones are load-bearing.
  • Platform engineers who want index cleanup to be a reviewed, reversible, scheduled step rather than a one-off afternoon with a SQL client.

Why orchestrate this with Kestra

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.

Tested end to end

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

Prerequisites

  • A CockroachDB cluster reachable over the Postgres wire protocol, which is what the PostgreSQL plugin speaks to it. There is no separate CockroachDB plugin.
  • The audited database is the one named in jdbc_url.
  • The role needs to read 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.
  • Leave 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.
  • A cluster that restarts often, for example one rescheduled by an orchestrator, may never clear the window. That is the correct answer rather than a limitation: these counters cannot support a drop decision on a cluster whose nodes do not stay up.

Secrets

  • 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.
  • In Kestra OSS, provide each one as a base64-encoded environment variable prefixed SECRET_.

Inputs

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

Quick start

  1. Add the COCKROACH_PASSWORD and SLACK_WEBHOOK_URL secrets.
  2. Import the flow and set jdbc_url to a node, with the database you want audited in the path.
  3. Run it once as it ships. Read the counter age line first. If it refuses, that is the flow working.
  4. Once the window clears, read the UNUSED list and the protected list beside it. Check the protected list looks right for your schema before trusting the other one.
  5. Drop with auto_remediate: true and dry_run: false, then answer the Pause. Start with max_indexes_dropped: 1.
  6. Enable the weekly trigger, which always runs in safe mode.

How to extend

  • Make each target 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.
  • Record each index's read count in a KV key per run and require N consecutive zero-read passes before offering it, which survives a restart that the uptime guard would otherwise have to wait out.
  • Capture 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.
  • Loop over several databases or clusters by passing a list of JDBC URLs, since one run audits the database in its own URL.
  • Add a branch that compares the index's storage size from crdb_internal.ranges so the Slack message leads with the indexes whose removal frees the most.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.