Overview
The Product Distribution Dashboard uses PostgreSQL 16 as its database with Flyway for schema migrations. The application supports different database configurations for development and production environments.PostgreSQL Setup
Development Environment
For local development, PostgreSQL runs in Docker via docker-compose:Connection Settings
Development Profile
Production Profile
- SSL encryption enabled (
sslmode=require) - Batch insert optimization (
reWriteBatchedInserts=true)
HikariCP Connection Pooling
The production profile uses HikariCP for connection pooling with the following configuration:Connection Pool Settings
| Setting | Value | Description |
|---|---|---|
maximum-pool-size | 10 | Maximum number of connections in the pool |
minimum-idle | 5 | Minimum number of idle connections maintained |
connection-timeout | 30000ms | Maximum time to wait for a connection (30s) |
idle-timeout | 600000ms | Maximum idle time before connection is retired (10m) |
max-lifetime | 1800000ms | Maximum lifetime of a connection (30m) |
Performance Optimization
The production profile includes Hibernate batch processing configuration:- Batch Size: 100 operations per batch
- Ordered Operations: Inserts and updates are ordered for better batching efficiency
- Autocommit: Not disabled to ensure transactional consistency
Flyway Migrations
The application uses Flyway for database schema versioning and migrations. JPA is configured to validate the schema rather than auto-generate it:Migration Files
Migrations are located insrc/main/resources/db/migration/:
V1__Create_products_table.sql- Products and sizesV2__Create_stores_table.sql- Store locations and capacityV3__Create_warehouses_table.sql- Warehouse locationsV4__Create_product_items_table.sql- Inventory itemsV5__Create_stock_assignments_table.sql- Distribution assignmentsV6__Create_unfulfilled_demands_table.sql- Unfulfilled demand tracking
Database Schema
Products
Stores
Warehouses
Product Items
Stock Assignments
Unfulfilled Demands
Database Indexing Strategy
The schema includes strategic indexes to optimize common queries:- Single-column indexes: Fast lookups by store, warehouse, or product ID
- Composite indexes: Optimize multi-column WHERE clauses and JOIN operations
- Distance indexes: Enable efficient distance-based sorting and filtering
PgAdmin
The Docker Compose setup includes PgAdmin for database administration:Backup and Maintenance
Backup Recommendations
-
Automated Backups: Schedule regular PostgreSQL dumps
- Point-in-Time Recovery: Enable WAL archiving for production
- Backup Retention: Keep at least 7 days of daily backups
Maintenance Tasks
-
Vacuum: Run VACUUM ANALYZE periodically to optimize query performance
-
Index Maintenance: Monitor index usage and rebuild fragmented indexes
- Connection Monitoring: Monitor active connections and pool utilization
- Log Analysis: Review PostgreSQL logs for slow queries and errors
Health Checks
The Docker Compose configuration includes a health check for PostgreSQL:Troubleshooting
Connection Refused
Problem: Application cannot connect to database Solutions:- Verify PostgreSQL is running:
docker ps - Check environment variables are set correctly
- Ensure database host and port are reachable
- Check firewall rules for port 5432
Migration Failures
Problem: Flyway migration fails on startup Solutions:- Check Flyway migration history:
SELECT * FROM flyway_schema_history; - Verify migration file checksums match
- Ensure database user has DDL permissions
- Review application logs for specific error messages
Connection Pool Exhaustion
Problem: “Connection pool exhausted” errors Solutions:- Increase
maximum-pool-sizein production configuration - Check for connection leaks (unclosed connections)
- Monitor slow queries that hold connections
- Review application logs for long-running transactions
Performance Issues
Problem: Slow query performance Solutions:- Run
EXPLAIN ANALYZEon slow queries - Verify indexes are being used
- Check for missing indexes on frequently queried columns
- Consider increasing
batch_sizefor bulk operations - Monitor connection pool metrics