db-performance-watchlist
Defines database performance monitoring strategy with slow query detection, resource usage alerts, query execution thresholds, and automated alerting. Use for "database monitoring", "performance alerts", "slow queries", or "DB metrics".
What this skill does
# DB Performance Watchlist
Monitor database performance and prevent regressions.
## Key Performance Metrics
```typescript
// performance-metrics.ts
export interface DBMetrics {
// Query Performance
slowQueries: {
threshold: number; // ms
count: number;
queries: SlowQuery[];
};
// Connection Pool
connections: {
active: number;
idle: number;
total: number;
maxConnections: number;
utilizationPercent: number;
};
// Resource Usage
resources: {
cpuPercent: number;
memoryPercent: number;
diskUsagePercent: number;
iops: number;
};
// Query Statistics
queryStats: {
selectsPerSecond: number;
insertsPerSecond: number;
updatesPerSecond: number;
deletesPerSecond: number;
};
// Cache Performance
cache: {
hitRate: number; // %
size: number; // MB
evictions: number;
};
// Index Usage
indexes: {
unusedIndexes: string[];
missingIndexes: string[];
};
}
interface SlowQuery {
query: string;
duration: number;
calls: number;
avgDuration: number;
table: string;
}
```
## Slow Query Detection
```typescript
// scripts/detect-slow-queries.ts
async function detectSlowQueries(thresholdMs: number = 100) {
// Enable slow query logging (PostgreSQL)
await prisma.$executeRaw`
ALTER DATABASE mydb
SET log_min_duration_statement = ${thresholdMs};
`;
// Query pg_stat_statements for slow queries
const slowQueries = await prisma.$queryRaw<SlowQuery[]>`
SELECT
query,
calls,
total_exec_time / 1000 as total_time_ms,
mean_exec_time / 1000 as avg_time_ms,
max_exec_time / 1000 as max_time_ms,
(total_exec_time / sum(total_exec_time) OVER()) * 100 as percent_of_total
FROM pg_stat_statements
WHERE mean_exec_time > ${thresholdMs}
ORDER BY mean_exec_time DESC
LIMIT 20
`;
console.log("๐ Slow Queries Detected:\n");
slowQueries.forEach((q, i) => {
console.log(`${i + 1}. ${q.query.substring(0, 80)}...`);
console.log(` Calls: ${q.calls}`);
console.log(` Avg: ${q.avg_time_ms.toFixed(2)}ms`);
console.log(` Max: ${q.max_time_ms.toFixed(2)}ms`);
console.log(` % of total time: ${q.percent_of_total.toFixed(1)}%\n`);
});
return slowQueries;
}
```
## Connection Pool Monitoring
```typescript
async function monitorConnectionPool() {
const stats = await prisma.$queryRaw<any[]>`
SELECT
sum(numbackends) as total_connections,
sum(CASE WHEN state = 'active' THEN 1 ELSE 0 END) as active,
sum(CASE WHEN state = 'idle' THEN 1 ELSE 0 END) as idle,
max_connections
FROM pg_stat_database
CROSS JOIN (SELECT setting::int as max_connections FROM pg_settings WHERE name = 'max_connections')
WHERE datname = current_database()
GROUP BY max_connections
`;
const { total_connections, active, idle, max_connections } = stats[0];
const utilization = (total_connections / max_connections) * 100;
console.log("๐ Connection Pool Status:");
console.log(
` Total: ${total_connections}/${max_connections} (${utilization.toFixed(
1
)}%)`
);
console.log(` Active: ${active}`);
console.log(` Idle: ${idle}`);
// Alert if > 80% utilization
if (utilization > 80) {
console.warn("โ ๏ธ Connection pool >80% utilized!");
await sendAlert({
title: "High connection pool usage",
message: `${utilization.toFixed(1)}% of connections in use`,
});
}
}
```
## Resource Monitoring
```typescript
async function monitorResources() {
// CPU Usage
const cpuStats = await prisma.$queryRaw<any[]>`
SELECT
(sum(total_exec_time) / (extract(epoch from (now() - stats_reset)) * 1000 * 100)) as cpu_percent
FROM pg_stat_statements, pg_stat_database
WHERE datname = current_database()
`;
// Memory Usage
const memStats = await prisma.$queryRaw<any[]>`
SELECT
pg_size_pretty(pg_database_size(current_database())) as db_size,
pg_size_pretty(sum(pg_relation_size(schemaname||'.'||tablename))) as tables_size
FROM pg_tables
WHERE schemaname = 'public'
`;
// Cache Hit Rate
const cacheStats = await prisma.$queryRaw<any[]>`
SELECT
sum(heap_blks_hit) / (sum(heap_blks_hit) + sum(heap_blks_read)) * 100 as cache_hit_rate
FROM pg_statio_user_tables
`;
console.log("๐ Resource Usage:");
console.log(` CPU: ${cpuStats[0].cpu_percent.toFixed(1)}%`);
console.log(` Database Size: ${memStats[0].db_size}`);
console.log(` Cache Hit Rate: ${cacheStats[0].cache_hit_rate.toFixed(1)}%`);
// Alert if cache hit rate < 90%
if (cacheStats[0].cache_hit_rate < 90) {
console.warn("โ ๏ธ Cache hit rate below 90%!");
await sendAlert({
title: "Low cache hit rate",
message: `Cache hit rate: ${cacheStats[0].cache_hit_rate.toFixed(1)}%`,
});
}
}
```
## Index Usage Analysis
```typescript
async function analyzeIndexUsage() {
// Find unused indexes
const unusedIndexes = await prisma.$queryRaw<any[]>`
SELECT
schemaname,
tablename,
indexname,
idx_scan
FROM pg_stat_user_indexes
WHERE idx_scan = 0
AND indexname NOT LIKE '%_pkey'
ORDER BY pg_relation_size(indexrelid) DESC
`;
console.log("๐๏ธ Unused Indexes:\n");
unusedIndexes.forEach((idx) => {
console.log(` ${idx.tablename}.${idx.indexname} (0 scans)`);
});
// Find missing indexes (sequential scans on large tables)
const missingIndexes = await prisma.$queryRaw<any[]>`
SELECT
schemaname,
tablename,
seq_scan,
seq_tup_read,
idx_scan,
n_live_tup
FROM pg_stat_user_tables
WHERE seq_scan > 1000
AND n_live_tup > 10000
ORDER BY seq_scan * n_live_tup DESC
LIMIT 10
`;
console.log("\n๐ Tables with High Sequential Scans:\n");
missingIndexes.forEach((table) => {
console.log(` ${table.tablename}:`);
console.log(` Sequential scans: ${table.seq_scan}`);
console.log(` Rows: ${table.n_live_tup}`);
console.log(` Index scans: ${table.idx_scan}`);
});
}
```
## Alert Thresholds
```typescript
const ALERT_THRESHOLDS = {
slowQuery: {
avgDuration: 500, // ms
maxDuration: 2000, // ms
callsPerMinute: 100,
},
connections: {
utilizationWarning: 70, // %
utilizationCritical: 85, // %
},
resources: {
cpuWarning: 70, // %
cpuCritical: 85, // %
memoryWarning: 80, // %
memoryCritical: 90, // %
diskWarning: 75, // %
diskCritical: 85, // %
},
cache: {
hitRateWarning: 90, // %
hitRateCritical: 80, // %
},
queryRate: {
maxSelectsPerSecond: 10000,
maxWritesPerSecond: 1000,
},
};
async function checkThresholds() {
const metrics = await gatherMetrics();
// Check slow queries
if (metrics.slowQueries.count > 10) {
await sendAlert({
level: "warning",
title: "Slow queries detected",
message: `${metrics.slowQueries.count} queries exceeding ${ALERT_THRESHOLDS.slowQuery.avgDuration}ms`,
});
}
// Check connection pool
if (
metrics.connections.utilizationPercent >
ALERT_THRESHOLDS.connections.utilizationCritical
) {
await sendAlert({
level: "critical",
title: "Connection pool critical",
message: `${metrics.connections.utilizationPercent.toFixed(
1
)}% utilization`,
});
}
// Check cache hit rate
if (metrics.cache.hitRate < ALERT_THRESHOLDS.cache.hitRateCritical) {
await sendAlert({
level: "critical",
title: "Cache hit rate critical",
message: `${metrics.cache.hitRate.toFixed(1)}% hit rate`,
});
}
}
```
## Monitoring Dashboard
```typescript
// Generate monitoring report
async function generatePerformanceReport() {
console.log("๐ Database Performance Report\n");
console.log("=".repeat(50) + "\n");
// Slow queries
const slowQueries = await detectSlowQueries(100);
console.log(`Slow Queries (>100ms): ${slowQueries.length}\n`);
// Connection pool
await monitorConnectionPool();
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.