Renewable Farm Cluster Index Defragmentation Script
Develop a zero-downtime maintenance and index defragmentation script for solar and wind generation telemetry databases.
Use this template when setting up automated scheduled index maintenance on 24/7 generation asset databases. It creates safe, lock-free index rebuild scripts with replication lag awareness for operational energy databases.
Role: Lead Database Administrator specializing in mission-critical renewable energy telemetry clusters.
Context
- Asset management entity: {{asset_operator}}
- Database engine and version: {{db_engine_version}}
- Primary telemetry table: {{primary_telemetry_table}}
- Maintenance window duration: {{maintenance_window_duration}} minutes
- Replication lag threshold: {{max_replication_lag_seconds}} seconds
- Lock wait timeout: {{lock_timeout_ms}} ms
Task
Write an automated maintenance shell and SQL script for {{asset_operator}} that safely assesses index fragmentation on {{primary_telemetry_table}}, rebuilds bloated indexes concurrently, and prevents replication lag spikes on {{db_engine_version}}.
Method
- Query database catalog metadata to evaluate index bloat and fragmentation levels on {{primary_telemetry_table}}.
- Filter for indexes exhibiting fragmentation greater than 30% or physical bloat above 500MB.
- Set session-level lock timeouts to {{lock_timeout_ms}} ms to eliminate table lock starvation.
- Check streaming replication lag across read replicas, pausing operations if lag exceeds {{max_replication_lag_seconds}} seconds.
- Execute online concurrent index rebuild commands (
REINDEX CONCURRENTLYor equivalent). - Track execution progress against the {{maintenance_window_duration}} minute time limit, terminating cleanly before window expiry.
- Log fragmented vs post-rebuild sizes and send an operational summary to standard output.
Constraints
- MUST utilize concurrent/online index rebuilding commands that do not acquire exclusive write locks.
- MUST abort the maintenance step immediately if replication lag exceeds {{max_replication_lag_seconds}} seconds.
- Code MUST run idempotently without leaving orphan temporary indexes.
- Shell script must adhere to strict mode (
set -euo pipefail).
Output format
Provide the deliverable in the following order:
- Block 1: Pre-flight catalog query to identify candidate indexes
- Block 2: Replication health-check helper script
- Block 3: Core online defragmentation Bash/SQL wrapper script
- Block 4: Logging and verification query Total output length must not exceed 130 lines of code.
Self-review
- Verify that lock timeout {{lock_timeout_ms}} ms is applied before any DDL.
- Ensure the script pauses or exits if elapsed time nears {{maintenance_window_duration}} minutes.
- Validate syntax specifically against {{db_engine_version}} requirements.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.