# PostgreSQL Configuration for High Concurrency (1000+ concurrent users)
# Optimized for Laravel applications with heavy read/write operations

# Connection Settings
max_connections = 200                    # Maximum concurrent connections
shared_buffers = 512MB                  # Shared memory for caching (25% of RAM typically)
effective_cache_size = 2GB              # System-wide cache estimate
work_mem = 4MB                          # Memory per operation (sort, hash, etc.)
maintenance_work_mem = 128MB            # Memory for maintenance operations

# Write Performance
wal_buffers = 16MB                      # WAL buffer size
checkpoint_completion_target = 0.9      # Checkpoint completion target
max_wal_size = 2GB                      # Maximum WAL size
min_wal_size = 512MB                    # Minimum WAL size

# Query Planning
random_page_cost = 1.1                  # SSD storage (lower is better)
effective_io_concurrency = 200          # Concurrent IO operations (SSD)

# Background Writer
bgwriter_delay = 200ms                  # Background writer delay
bgwriter_lru_maxpages = 100             # Max pages to flush per round
bgwriter_lru_multiplier = 2.0           # Multiplier for bgwriter

# Logging (minimal for performance, increase for debugging)
log_min_duration_statement = 1000       # Log slow queries (>1s)
log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '
log_checkpoints = on
log_connections = off
log_disconnections = off
log_duration = off
log_lock_waits = on

# Autovacuum (important for PostgreSQL performance)
autovacuum = on
autovacuum_max_workers = 3
autovacuum_naptime = 20s

# Statement Timeout (prevent runaway queries)
statement_timeout = 300000               # 5 minutes
idle_in_transaction_session_timeout = 60000  # 1 minute

# Locale (important for index performance)
lc_messages = 'C'
lc_monetary = 'C'
lc_numeric = 'C'
lc_time = 'C'

# Default text search configuration
default_text_search_config = 'pg_catalog.english'

# Synchronous Commit (can be relaxed for performance)
synchronous_commit = on                 # Set to 'off' for higher write performance (less safe)

# Max Worker Processes
max_worker_processes = 8
max_parallel_workers_per_gather = 4
max_parallel_workers = 8

# Shared Preload Libraries (for monitoring extensions)
# shared_preload_libraries = 'pg_stat_statements'
