Database Performance Debugging

VerifiedSafe

Debug and troubleshoot database performance in ettametta. Use when investigating slow queries, connection pool issues, migration conflicts, N+1 patterns, or schema drift.

Sby Skills Guide Bot
DevelopmentIntermediate
7/26/2026
Claude Code
#database#performance#postgresql#debugging#query-optimization

Recommended for

Automated summary

This skill provides commands and guidance to diagnose and fix database performance issues in the ettametta project.

Strengths

  • Concrete commands for inspecting connections, active queries, and table sizes
  • Covers connection configuration and session patterns (async, sync, Celery)
  • Identifies N+1 risks and missing indexes

Limitations

  • Specific to the ettametta project and its database schema
  • May require adaptation for other environments
  • Does not cover advanced optimizations (complex queries, GIN/GIST indexes)
When to use it

When investigating slow queries, connection pool exhaustion, migration conflicts, or N+1 patterns.

When not to use it

For non-database performance issues, or for general PostgreSQL tuning without application context.

Security analysis

Safe
Quality score90/100

The skill provides read-only diagnostic SQL commands and environment inspection via docker compose, with no destructive operations or data exfiltration.

No concerns found

How this analysis is made

Examples

Diagnose slow queries
I'm experiencing slow performance in the application. Please run the database debugging commands from the db-performance skill to check active queries, connection count, and table sizes.
Check for N+1 problems
Analyze the query patterns in the routes/nexus.py file for potential N+1 issues using the db-performance skill.
Identify missing indexes
Review the database schema and suggest missing indexes based on the common issues listed in the db-performance skill.

name: db-performance description: Debug and troubleshoot database performance in ettametta. Use when investigating slow queries, connection pool issues, migration conflicts, N+1 patterns, or schema drift.

Database Performance Debugging

Quick Diagnostics

# Connection count
docker compose exec db psql -U postgres -c "SELECT count(*) FROM pg_stat_activity;"

# Active queries
docker compose exec db psql -U postgres -c "SELECT pid, now()-query_start AS duration, query FROM pg_stat_activity WHERE state='active' ORDER BY duration DESC;"

# Table sizes
docker compose exec db psql -U postgres -c "SELECT relname, n_live_tup, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_stat_user_tables ORDER BY n_live_tup DESC;"

# Migration state
alembic heads
alembic current
alembic check

Connection Configuration

Async engine (FastAPI): pool_pre_ping=True, defaults pool_size=5, max_overflow=10

Sync engine (Celery): Default QueuePool, pool_size=5, max_overflow=10

Celery task-scoped: pool_size=2, max_overflow=0 (tight isolation)

NullPool for Celery workers: Fresh engine per call, disposed after use. Expensive but avoids event-loop conflicts.

Default DATABASE_URL: sqlite:///./data/db/ettametta.db. Production uses PostgreSQL.

Session Management

  • Pattern A (routes): get_db() dependency with rollback on exception
  • Pattern B (services): async_session_factory() inline
  • Pattern C (Celery): get_async_session() with NullPool

All use expire_on_commit=False, autocommit=False, autoflush=False.

Models

35 ORM models. Zero relationship() declarations — all cross-table access via explicit joins.

Key models: UserDB (7 unique indexes), ContentCandidateDB (external_id unique, niche, region), VideoJobDB, PublishedContentDB, ScheduledPostDB, SocialAccount.

Missing indexes (potential)

  • video_jobs.user_id
  • audit_logs.user_id
  • scheduled_posts.user_id
  • nexus_jobs.user_id

No composite indexes declared.

Migrations

20 migration files. Current HEAD: d410fb0d40a9. 2 merge migrations exist.

CI/CD bypasses Alembic: Deployment uses create_all() not alembic upgrade head.

Query Patterns

N+1 risks

  • Nexus stats: 4 separate COUNT(*) queries (lines 482-506 in routes/nexus.py). Fix: single GROUP BY.
  • Settings: 2 separate queries for system + user settings. Fix: join.

In-memory pagination

Discovery routes use paginate_list() — loads all then slices. Use SQL .offset().limit().

DateTime Monkeypatch

database.py lines 12-24: strips timezone info from DateTime. Workaround for SQLite/PG compat.

Common Issues

Connection pool exhaustion

docker compose exec db psql -U postgres -c "SELECT count(*) FROM pg_stat_activity;"

Stale connections

Async engine has pool_pre_ping=True. Sync engine does not.

NullPool performance

get_async_session() creates/disposes full engine per call. Expensive for high-frequency ops.

Missing composite indexes

Queries on multiple columns do sequential scans. Add Index() declarations.

expire_on_commit=False

Correct for async but means stale data if objects reused after commit without refresh.

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/database-performance-debugging-ms1aw6gv/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/database-performance-debugging-ms1aw6gv' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/database-performance-debugging-ms1aw6gv/download' --output '.skill-downloads/database-performance-debugging-ms1aw6gv/SKILL.md.part' && mv '.skill-downloads/database-performance-debugging-ms1aw6gv/SKILL.md.part' '.skill-downloads/database-performance-debugging-ms1aw6gv/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/database-performance-debugging-ms1aw6gv/SKILL.md' && test -s '.skill-downloads/database-performance-debugging-ms1aw6gv/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/db-performance' && cp -R '.skill-downloads/database-performance-debugging-ms1aw6gv' '.claude/skills/db-performance'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/database-performance-debugging-ms1aw6gv'

openskills read 'database-performance-debugging-ms1aw6gv'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/database-performance-debugging-ms1aw6gv/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills