Understanding PostgreSQL pg_settings and pg_file_settings with Practical Examples

Understanding PostgreSQL pg_settings and pg_file_settings with Practical Examples

PostgreSQL offers powerful catalog views for examining configuration values that shape the performance and behavior of your database. Among these, pg_settings and pg_file_settings are essential tools for administrators who want to ensure optimal performance and configuration consistency.

This article breaks down a useful snippet of PostgreSQL code that leverages these views and provides step-by-step insights for database administrators (DBAs).

The Code

 1--\pset title 'pg_settings'
 2\echo pg_settings:
 3SELECT
 4    name,
 5    setting,
 6    unit,
 7    context
 8FROM
 9    pg_settings;
10\echo
11--\pset title 'pg_file_settings'
12\echo pg_file_settings:
13SELECT
14    *
15FROM
16    pg_file_settings;

Purpose of the Code

The main objective of this script is to inspect PostgreSQL database configuration parameters in detail. By using both pg_settings and pg_file_settings, DBAs can cross-check active values and those defined in configuration files.

  • pg_settings → Shows current values actively used by PostgreSQL.
  • pg_file_settings → Displays values specifically as they are defined in PostgreSQL configuration files.

Breakdown of the PostgreSQL Code

pg_settings

1SELECT
2    name,
3    setting,
4    unit,
5    context
6FROM
7    pg_settings;
  • name → The configuration parameter name (e.g., shared_buffers, work_mem).
  • setting → The current value applied to that parameter.
  • unit → Units for the configuration value (kB, ms, etc.).
  • context → Where the setting can be changed (e.g., postmaster, user, sighup).

➡️ This query helps you understand the real-time configuration without checking the config file.

pg_file_settings

1SELECT
2    *
3FROM
4    pg_file_settings;
  • Displays every parameter defined in postgresql.conf or related files.
  • Fields include source file, applied value, and whether the configuration is currently active or pending reload.

➡️Useful for detecting mismatches between config file definitions and actual running values in memory.

Key Points & Insights

  1. Cross-validation of settings Using both views allows administrators to confirm that PostgreSQL is using the correct settings and to troubleshoot cases where changes in postgresql.conf are not being applied.
  2. Performance tuning Reviewing pg_settings helps you adjust resource-heavy configurations (like work_mem, shared_buffers, maintenance_work_mem) based on workload.
  3. Change context awareness The context column tells you whether a change requires a PostgreSQL restart (postmaster), a reload (sighup), or can be altered at session/user level.
  4. Error detection in configs pg_file_settings highlights if invalid or misconfigured parameters exist in configuration files, helping avoid runtime issues.
  5. Best practice for DBAs Always verify both active and file-based configurations after making changes to maintain a well-tuned environment.

Practical Insights for DBAs

  • Use pg_settings when you need live insights on current settings.
  • Use pg_file_settings when auditing configuration drift between what is written in configuration files and what PostgreSQL is running.
  • Combine these queries for troubleshooting performance issues, ensuring optimized use of system resources.

References

Posts in this series