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

Oracle Unusable Index Rebuild Guard

Find Oracle indexes and index partitions left UNUSABLE after a MOVE or direct-path load, rebuild them behind a dry-run gate, then re-check the dictionary.

Categories
DataInfrastructure

An Oracle index can be marked UNUSABLE without any error at the time. Move a table or a partition. Split a partition. Run a direct-path load with SKIP_INDEX_MAINTENANCE. Any of these leaves the affected index or index partition STATUS='UNUSABLE'. The optimizer then ignores it, queries fall back to full scans and a unique index stops enforcing its constraint, all silently until someone reads the data dictionary.

This blueprint finds those segments before a query planner does. It reads dba_indexes and dba_ind_partitions for the schemas you name, rebuilds each unusable index with ALTER INDEX REBUILD and each unusable partition with REBUILD PARTITION, then reads the dictionary again and fails the run if anything is still UNUSABLE.

This blueprint was created by zkasuran.

Findings

Where the status lives Column read Rebuild used
Whole index (non-partitioned or global) dba_indexes.status = 'UNUSABLE' ALTER INDEX "owner"."name" REBUILD
One index partition (local or global partitioned) dba_ind_partitions.status = 'UNUSABLE' ALTER INDEX "owner"."name" REBUILD PARTITION "p"

A partitioned index carries dba_indexes.status = 'N/A' and keeps the real state per partition, so the index level and the partition level are read separately and the two result sets are unioned. ONLINE is appended to every rebuild by default so base-table DML is not blocked while the index is rebuilt.

How it works

  1. audit (io.kestra.plugin.jdbc.oracle.Query, read-only) reads dba_indexes for whole indexes whose status is UNUSABLE and dba_ind_partitions for index partitions whose status is UNUSABLE, limited to the owners in index_owners. It returns the exact ALTER INDEX REBUILD for each.
  2. fixes (io.kestra.plugin.core.output.OutputValues) deduplicates those statements. summary (io.kestra.plugin.core.log.Log) logs the unusable segments.
  3. route (io.kestra.plugin.core.flow.If):
    • clean: nothing is UNUSABLE, so it logs and stops.
    • findings: alert posts the list to Slack.
      • remediate_gate runs only when auto_remediate is true and dry_run is false.
      • rebuild (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) runs each ALTER INDEX REBUILD in turn, appending ONLINE when rebuild_online is true.
      • reaudit reads the dictionary again. verify_result fails the run through rebuild_failed if any segment is still UNUSABLE, otherwise rebuilt logs the before and after counts.
  4. errors posts to Slack if the flow fails for any reason, including an index that could not be rebuilt.

What you get

  • A scheduled check that catches an unusable index after a MOVE or a load instead of a query planner silently dropping it.
  • A rebuild that is safe to re-run. Rebuilding an already usable index is not attempted, because the audit only returns the ones still marked UNUSABLE.
  • A real verification step. The run passes only when the dictionary reports every segment usable again.
  • Slack alerts on the findings and on any failure.

Who it's for

  • DBAs who run partition maintenance or direct-path loads and want the indexes confirmed usable before traffic hits them.
  • Platform teams that move or reorganize tables during a release.
  • Teams evaluating the Oracle plugin who want an example that reads the data dictionary then changes state behind a gate.

Why orchestrate this with Kestra

The usual fix is a one-off ALTER INDEX a DBA runs by hand after noticing a slow query. Kestra runs the check on a schedule, scopes it to the schemas you own, posts the unusable indexes to Slack, applies the rebuild only behind auto_remediate and dry_run, one segment at a time, then proves the result by reading the dictionary again. Every run keeps its logs, so you can see when an index went unusable and what rebuilt it.

Tested end to end

On Kestra 2.0.5 OSS against Oracle Database Free 23ai (gvenzl/oracle-free, service FREEPDB1), with a local HTTP sink standing in for Slack. The test schema APP held a non-partitioned table T1 with a secondary index IDX_T1_C1 plus its primary-key index. A range-partitioned table TP carried a local index IDX_TP_C1. ALTER TABLE APP.T1 MOVE and ALTER TABLE APP.TP MOVE PARTITION P1 left two whole indexes and one index partition STATUS='UNUSABLE', read back from dba_indexes and dba_ind_partitions with sqlplus.

Run Inputs Result
Clean defaults, nothing broken 0 unusable segments, clean branch, SUCCESS
Detect defaults (dry_run: true) 3 unusable segments found (IDX_T1_C1, IDX_TP_C1 PARTITION P1, the PK index), Slack alert posted, no change. The dictionary still showed 3
Gate hold auto_remediate: true, dry_run: true Same report, Slack said no change applied, the dictionary still showed 3. The dry-run gate held
Remediate auto_remediate: true, dry_run: false Rebuilt all 3 with ONLINE, the re-audit returned 0, SUCCESS. The dictionary went from 3 unusable to 0, every segment VALID or USABLE
Offline auto_remediate: true, dry_run: false, rebuild_online: false Same 3 rebuilt without ONLINE, re-audit 0, SUCCESS
Failure unreachable instance audit failed with ORA-17868, the errors block posted to Slack, HTTP 200

The clean and detect runs are the proof the check can fail: the same audit returned 0 against the healthy schema and 3 against the broken one. The remediate run is the before and after, 3 down to 0, read from the dictionary by sqlplus not only from the Kestra log. The partitioned index is why the index level and the partition level are read separately: dba_indexes reported IDX_TP_C1 as N/A while dba_ind_partitions carried the real UNUSABLE on partition P1.

Prerequisites

  • An Oracle Database reachable over the thin JDBC driver (jdbc:oracle:thin:@//host:1521/service).
  • A database user that can read dba_indexes and dba_ind_partitions (SELECT_CATALOG_ROLE or SELECT ANY DICTIONARY) and rebuild the indexes it finds. Rebuilding another schema's index needs ALTER ANY INDEX, which a DBA role carries. A user rebuilding only its own indexes needs none of those.

Secrets

  • ORACLE_PASSWORD: the database password. It is the default of the password input, so it is never written in the flow.
  • SLACK_WEBHOOK_URL: used by alert and the errors block.
  • In Kestra OSS, provide each secret as a base64-encoded environment variable prefixed SECRET_.

Inputs

Input Default Purpose
jdbc_url jdbc:oracle:thin:@//oracle-host:1521/FREEPDB1 The Oracle instance to check
username SYSTEM The database user
password {{ secret('ORACLE_PASSWORD') }} The database password
index_owners [APP] Schema owners whose indexes are checked
rebuild_online true Append ONLINE to each REBUILD
auto_remediate false Allow ALTER INDEX REBUILD
dry_run true Must be false for a rebuild to run

Quick start

  1. Add the ORACLE_PASSWORD and SLACK_WEBHOOK_URL secrets.
  2. Import the flow, set jdbc_url and username, then list the schemas to check in index_owners.
  3. Run it once with the defaults and read the unusable segments per schema.
  4. When you want it to fix them, run with auto_remediate: true and dry_run: false.
  5. Enable the daily trigger, which always runs in safe mode.

How to extend

  • Add dba_ind_subpartitions in a third union branch to cover composite-partitioned indexes, whose status lives one level deeper.
  • Fail the run as soon as the first audit finds anything, for a read-only drift check with no rebuild.
  • Set rebuild_online: false where an offline rebuild is acceptable and faster during a maintenance window.
  • Loop over several instances to check every database from one flow.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.