talk2data / .cursor /rules /database-query-guidelines.mdc
cevheri's picture
docs: add cursor rules
95d6173
raw
history blame
1.6 kB
---
description:
globs:
alwaysApply: false
---
# 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