Postgresql rails analyzer

From el-feo/ai-context · View interactive version

Postgresql rails analyzer is an AI agent skill from the GitHub repository el-feo/ai-context. Comprehensive PostgreSQL configuration and usage analysis for Rails applications. Use when Claude Code needs to analyze a Rails codebase for database performance issues, optimization opportunities, or best practice violations. Detects N+1 queries, missing indexes, suboptimal database configurations,

How to use this skill

Skill file

PostgreSQL Rails Analyzer

Analyze Rails applications for PostgreSQL performance issues and provide actionable optimization recommendations based on "High Performance PostgreSQL for Rails" best practices.

Analysis Scripts

Run from the Rails application root directory:

1. N+1 Query Analysis

ruby scripts/analyze_n_plus_one.rb

Detects potential N+1 query issues by analyzing:

2. Index Analysis

ruby scripts/analyze_indexes.rb

Identifies indexing opportunities:

3. Configuration Analysis

ruby scripts/analyze_config.rb

Reviews database.yml for:

Workflow

Step 1: Understand the Request

Clarify what the user wants to analyze: full performance audit, specific issue (slow queries, N+1 problems), configuration review, or schema optimization.

Step 2: Run Appropriate Analysis Scripts

# For comprehensive analysis, run all three
ruby scripts/analyze_n_plus_one.rb
ruby scripts/analyze_indexes.rb
ruby scripts/analyze_config.rb

Step 3: Review Results

Script output categorizes issues by severity:

Step 4: Provide Recommendations

Create a prioritized list of actionable recommendations:

  1. Critical Issues (fix immediately) — FK indexes, N+1 in hot paths, missing timeouts
  2. Performance Optimizations (high impact) — partial indexes, counter caches, eager loading
  3. Best Practices (preventative) — configuration tuning, pool optimization, monitoring

Step 5: Generate Migration Code

For index and schema recommendations, provide ready-to-use migration code. Use algorithm: :concurrently for production migrations to avoid locking tables.

class AddPerformanceIndexes < ActiveRecord::Migration[7.0]
  disable_ddl_transaction!

  def change
    add_index :posts, :user_id, algorithm: :concurrently
    add_index :users, :active, where: "active = false", algorithm: :concurrently
    add_index :orders, [:status, :created_at], algorithm: :concurrently
  end
end

Step 6: Reference Additional Documentation

Load these references when users need deeper understanding:

Advanced Analysis

For deeper analysis beyond the scripts, manually review:

Limitations

These analysis scripts use static analysis. They may produce false positives (flagging non-issues) or false negatives (missing runtime-only issues). Always recommend testing fixes in staging and using EXPLAIN ANALYZE to verify.

Complementary Tools

Suggest for ongoing monitoring: PgHero (dashboard), pg_stat_statements (query stats), Bullet gem (runtime N+1 detection), Rails query logging (development visibility).

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.