-- Memory / performance tuning for an ~8 GB budget. Applied once, on a fresh data
-- volume, via ALTER SYSTEM (persists to postgresql.auto.conf); the container's
-- post-init restart brings the restart-only settings (shared_buffers, …) into
-- effect. To re-apply on an existing volume, run these by hand and restart:
--   docker compose exec db psql -U thermograph -d thermograph -f /docker-entrypoint-initdb.d/20-tuning.sql
--   docker compose restart db

-- Caching: the shared page cache, and the planner's view of total cache (PG + OS).
ALTER SYSTEM SET shared_buffers = '2GB';
ALTER SYSTEM SET effective_cache_size = '6GB';

-- Processing: per-operation sort/hash memory, and maintenance (index builds, VACUUM).
ALTER SYSTEM SET work_mem = '64MB';
ALTER SYSTEM SET maintenance_work_mem = '512MB';

-- Write throughput: fewer, larger checkpoints.
ALTER SYSTEM SET wal_buffers = '16MB';
ALTER SYSTEM SET min_wal_size = '1GB';
ALTER SYSTEM SET max_wal_size = '4GB';
ALTER SYSTEM SET checkpoint_completion_target = 0.9;

-- SSD-friendly planner + IO concurrency.
ALTER SYSTEM SET random_page_cost = 1.1;
ALTER SYSTEM SET effective_io_concurrency = 200;

-- Some room for parallel scans/aggregates on the bigger analytic queries.
ALTER SYSTEM SET max_parallel_workers_per_gather = 2;

-- DuckDB (pg_duckdb) memory ceiling for parquet processing. pg_duckdb is
-- preloaded, so this GUC exists at ALTER SYSTEM time.
ALTER SYSTEM SET duckdb.max_memory = '4GB';
