#!/usr/bin/env node /** * Script zum Prüfen und Bereinigen von PostgreSQL-Verbindungen */ import './config/loadEnv.js'; import { sequelize } from './utils/sequelize.js'; async function main() { try { console.log('🔍 Prüfe PostgreSQL-Verbindungen...\n'); // Prüfe aktive Verbindungen const [connections] = await sequelize.query(` SELECT count(*) as total, count(*) FILTER (WHERE state = 'active') as active, count(*) FILTER (WHERE state = 'idle') as idle, count(*) FILTER (WHERE state = 'idle in transaction') as idle_in_transaction, count(*) FILTER (WHERE usename = current_user) as my_connections FROM pg_stat_activity WHERE datname = current_database(); `); console.log('📊 Verbindungsstatistik:'); console.log(` Gesamt: ${connections[0].total}`); console.log(` Aktiv: ${connections[0].active}`); console.log(` Idle: ${connections[0].idle}`); console.log(` Idle in Transaction: ${connections[0].idle_in_transaction}`); console.log(` Meine Verbindungen: ${connections[0].my_connections}\n`); // Prüfe max_connections Limit const [maxConn] = await sequelize.query(` SELECT setting::int as max_connections FROM pg_settings WHERE name = 'max_connections'; `); console.log(`📈 Max Connections Limit: ${maxConn[0].max_connections}`); console.log(`📉 Verfügbare Connections: ${maxConn[0].max_connections - connections[0].total}\n`); // Zeige alte idle Verbindungen const [oldConnections] = await sequelize.query(` SELECT pid, usename, application_name, state, state_change, now() - state_change as idle_duration, query FROM pg_stat_activity WHERE datname = current_database() AND state = 'idle' AND state_change < now() - interval '1 minute' ORDER BY state_change ASC LIMIT 10; `); if (oldConnections.length > 0) { console.log(`⚠️ Gefunden ${oldConnections.length} alte idle Verbindungen (> 1 Minute):`); oldConnections.forEach(conn => { console.log(` PID: ${conn.pid}, User: ${conn.usename}, Idle seit: ${conn.idle_duration}`); }); console.log('\n💡 Tipp: Du kannst alte Verbindungen beenden mit:'); console.log(' SELECT pg_terminate_backend(pid) FROM pg_stat_activity'); console.log(' WHERE datname = current_database() AND state = \'idle\' AND state_change < now() - interval \'5 minutes\';\n'); } // Prüfe ob wir nahe am Limit sind const usagePercent = (connections[0].total / maxConn[0].max_connections) * 100; if (usagePercent > 80) { console.log(`⚠️ WARNUNG: ${usagePercent.toFixed(1)}% der verfügbaren Verbindungen werden verwendet!`); console.log(' Es könnte sein, dass nicht genug Verbindungen verfügbar sind.\n'); } await sequelize.close(); process.exit(0); } catch (error) { console.error('❌ Fehler:', error.message); process.exit(1); } } main();