File size: 1,604 Bytes
95d6173 1502bf5 95d6173 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | ---
description:
globs:
alwaysApply: true
---
# Database Query Guidelines
This document outlines the guidelines for executing database queries and interacting with the database in the AI-powered interface.
## Query Execution
### SQL Query Generation
- Implemented in [postgre_mcp_server.py](mdc:postgre_mcp_server.py)
- Supports read-only SQL queries (SELECT, COUNT, GROUP BY, ORDER BY)
- Destructive operations (DELETE, UPDATE, INSERT, DROP) are not allowed
- SQL syntax validation before execution
- Query results are formatted for display
### Database Connection
- Asynchronous connection management using AsyncPG
- Connection pooling for efficient resource utilization
- Error handling for connection issues
- Secure database URL management
## Best Practices
### Query Design
- Use explicit column names instead of *
- Include LIMIT clauses to restrict result sets
- Add WHERE clauses to filter results
- Consider indexing for performance
- Format SQL with proper indentation and line breaks
### Performance Optimization
- Efficient use of connection pooling
- Asynchronous query execution
- Proper error handling and logging
- Resource cleanup after query execution
## Error Handling
### Common Issues
- Invalid SQL syntax
- Table or column not found
- Connection failures
- Query timeout
- Resource constraints
### Recovery Strategies
- Graceful error messages
- Fallback responses
- Automatic retry mechanisms
- User-friendly error explanations
## Monitoring
### Logging
- Query execution logs
- Performance metrics
- Error tracking
- Resource utilization
- Response time monitoring
|