Overview
Ironclad uses PostgreSQL as its primary database, leveraging SQLx for compile-time verified queries and async connection pooling. The framework is built on the robust foundation ofsqlx version 0.7 with PostgreSQL-specific features enabled.
Dependencies
FromCargo.toml:
runtime-tokio-rustls: Async runtime with Rustls for TLSruntime-tokio-native-tls: Async runtime with native TLSpostgres: PostgreSQL driver supportchrono: DateTime type support
Connection Setup
The PostgreSQL connection pool is initialized insrc/db/postgres.rs:
Configuration
PostgreSQL configuration is managed through environment variables:Configuration Struct
Query Patterns
Basic Query
Query with Parameters
Insert with Returning
Update
Delete
Transaction
PostgreSQL-Specific Features
Array Types
JSONB Support
Full-Text Search
LISTEN/NOTIFY
Type Mappings
Common Rust-to-PostgreSQL type mappings:| Rust Type | PostgreSQL Type | Notes |
|---|---|---|
String | VARCHAR, TEXT | For string data |
i32 | INTEGER | 32-bit integer |
i64 | BIGINT | 64-bit integer |
f64 | DOUBLE PRECISION | Floating point |
bool | BOOLEAN | True/false |
chrono::DateTime<Utc> | TIMESTAMP WITH TIME ZONE | Timestamps |
chrono::NaiveDateTime | TIMESTAMP | Without timezone |
uuid::Uuid | UUID | UUID type |
serde_json::Value | JSON, JSONB | JSON data |
Vec<T> | ARRAY | PostgreSQL arrays |
Option<T> | NULL | Nullable columns |
Connection Pooling
Pool Configuration Best Practices
Monitoring Pool Health
Error Handling
Performance Tips
1. Use Indexes
2. Batch Operations
3. Use Connection from Pool Efficiently
4. Use Prepared Statements
SQLx automatically uses prepared statements for all queries, which improves performance for repeated queries.Troubleshooting
Connection Pool Exhaustion
Symptom:timed out while waiting for an open connection
Solutions:
- Increase
max_connections - Reduce
acquire_timeoutto fail fast - Check for connection leaks (unclosed transactions)
- Monitor slow queries
Deadlocks
Symptom:deadlock detected
Solutions:
- Always acquire locks in the same order
- Keep transactions short
- Use
SELECT ... FOR UPDATE NOWAITto fail fast
Query Performance
Symptom: Slow queries Solutions:- Use
EXPLAIN ANALYZEto understand query plans - Add appropriate indexes
- Avoid
SELECT *, fetch only needed columns - Use connection pooling
- Consider query result caching
Next Steps
Database Migrations
Learn how to manage schema changes with SQLx migrations
Configuration
Fine-tune your database connection settings