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.
The shortcut
Before changing anything, look up the setting's context. It tells you in one word whether the change is instant, needs a reload, or needs a planned restart.
Set it
ALTER SYSTEM SET log_min_duration_statement = '500ms';
The trap is editing postgresql.conf for a setting that was earlier changed with ALTER SYSTEM. The value in postgresql.auto.conf silently wins. pg_settings.sourcefile shows which file supplied the value.
Drills
Answer fast — trust the rule.
pg_settings shows context = postmaster for a setting. What does changing it require?
Where does ALTER SYSTEM SET write its value?
Which view shows every current connection and what it is running?
Why does enabling pg_stat_statements need a restart?
The analyst role should never run queries longer than 60 seconds, but other roles should be unaffected. What fits best?
Remember
- Set it, check its context, reload or restart, then confirm.
postgresql.auto.confbeatspostgresql.conf;pg_settingsshows the truth.- Slow-query logging and the pg_stat views are your first diagnostic tools.
