Configuration, Logging and Statistics
Riders complain that ending a ride sometimes takes five seconds. The query is fine in testing. You need the server to tell you which statements are slow, which means changing a setting, reloading it and reading the log. This is the everyday loop of a DBA.
The rule
Set it, check its context, reload or restart, then confirm.
Settings live in postgresql.conf. ALTER SYSTEM writes to a second file, postgresql.auto.conf, which is read last and so wins:
ALTER SYSTEM SET log_min_duration_statement = '500ms';
SELECT pg_reload_conf();
SELECT name, setting, unit, context, pending_restart
FROM pg_settings
WHERE name IN ('log_min_duration_statement', 'shared_buffers');The context column says how a setting can change:
postmaster: only at server start.shared_buffers,max_connections,listen_addresses,shared_preload_libraries. Needs a restart;pending_restartshowstuntil then.sighup: re-read on reload.superuser/user: can also be set per session withSET, and per database or role withALTER DATABASE pedal SET …orALTER ROLE analyst SET ….
Logging settings every server should consider:
logging_collector = onto capture logs into files underlog_directory(needs a restart).log_min_duration_statementto log any statement slower than a threshold.log_line_prefixto stamp each line with time, process id, user and database.log_connections,log_lock_waitsandlog_autovacuum_min_durationto see connections, lock waits and vacuum work.
Statistics views are updated continuously by the server:
pg_stat_activity: one row per connection, with its state and current query.pg_stat_user_tables: scans, inserts, dead tuples and last vacuum per table.pg_stat_database: commits, rollbacks and cache hits per database.
For per-query totals, add the pg_stat_statements extension. Extensions are optional features you add with CREATE EXTENSION; this one must also be loaded at startup, so it goes in shared_preload_libraries and needs one restart. pg_available_extensions lists what is installed and ready to add.