Lesson 7.7: Deployment and Maintenance
Initial Setup Script
BEGIN;
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
CREATE EXTENSION IF NOT EXISTS pg_partman;
SELECT create_parent('public.model_predictions', 'created_at', 'native', 'monthly');
UPDATE part_config
SET premake = 3,
retention_keep_table = false,
retention_keep_index = false,
retention = '6 months'
WHERE parent_table = 'public.model_predictions';
COMMIT;
Maintenance Tasks
DO $$
BEGIN
REFRESH MATERIALIZED VIEW CONCURRENTLY feature_stats;
REFRESH MATERIALIZED VIEW CONCURRENTLY model_performance_daily;
PERFORM refresh_user_features();
VACUUM ANALYZE;
END $$;
Production Checklist
Key Takeaways
- Extensions (vector, pg_stat_statements, pg_partman) enable advanced features
- pg_partman automates partition creation and retention
- Premake parameter creates future partitions in advance
- Retention policies automatically drop old partitions
- Daily maintenance refreshes materialized views and updates statistics
- VACUUM ANALYZE keeps statistics fresh for query planner
- Connection pooling (PgBouncer) manages connection overhead
- Production checklist ensures system readiness before launch
- Read replicas offload analytics queries from primary database