Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL pg_current_logfile: How to Use and Configure

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The pg_current_logfile() function returns the name of the currently active log file for the current PostgreSQL session. This is primarily used by database administrators to quickly identify which physical file on the server disk is receiving the current stream of logs, which is essential for troubleshooting real-time errors or monitoring system performance without manually searching through the entire log directory.

What You'll Need

  • A PostgreSQL instance with logging enabled.
  • A database client (such as psql, pgAdmin, or DBeaver) connected to the server.
  • Superuser privileges or the pg_monitor role (depending on the version) to execute the function.
  • The logging_collector parameter set to on in the postgresql.conf file.

Step-by-Step

  1. Connect to your database: Open your preferred SQL interface and connect to the PostgreSQL server where you need to find the log file.
  2. Verify logging is active: Before running the function, ensure that the logging collector is enabled. You can check this by running:
    SHOW logging_collector;
    If the result is off, the pg_current_logfile() function will return NULL because the server is not managing log files through the collector.
  3. Execute the function: Run the following SQL command to retrieve the current log filename:
    SELECT pg_current_logfile();
  4. Interpret the result: The function will return a text string representing the filename (e.g., postgresql-2026-12-05_000000.log). Note that this is the filename only, not the full absolute path to the file on the server's file system.
  5. Locate the full path: To find where this file is actually stored on the disk, run the following command to find the log directory:
    SHOW log_directory;
    Combine the result of log_directory with the result of pg_current_logfile() to get the complete path to the active log.

Common Mistakes

  • Expecting a full file path: A common error is assuming pg_current_logfile() provides the absolute path (e.g., /var/lib/pgsql/data/log/postgresql.log). It only provides the name of the file. You must check the log_directory setting separately.
  • Running it with logging disabled: If logging_collector is set to off, the function returns NULL. This does not mean the server isn't logging; it may be logging to stderr or syslog, which are handled by the operating system rather than the PostgreSQL collector.
  • Insufficient Permissions: Attempting to run this function as a low-privileged user may result in a permission error. Ensure the user has the necessary administrative roles.

Tips

  • Combine for Efficiency: You can retrieve both the directory and the filename in a single query to get the full path immediately:
    SELECT current_setting('log_directory') || '/' || pg_current_logfile();
  • Use for Automation: This function is highly useful in monitoring scripts. Instead of using ls -t on the server to find the newest file, the script can query the database directly to identify the correct log to tail.
  • Check Rotation: If you notice the filename changes frequently, check your log_rotation_age and log_rotation_size settings in postgresql.conf.

Troubleshooting

If the function is not working as expected, follow these steps in order:

  • Check for NULL results: If the function returns NULL, verify that logging_collector = on is set in your configuration. If you change this setting, you must restart the PostgreSQL service for it to take effect.
  • Verify Permissions: If you receive an "access denied" or "permission denied" error, grant the pg_monitor role to your user:
    GRANT pg_monitor TO your_username;
  • Confirm Log Destination: Run SHOW log_destination;. If it is set to syslog or eventlog, pg_current_logfile() will not work because the logs are being sent to an external system manager rather than a local file managed by PostgreSQL.
  • Check Disk Space: If logs are not appearing in the file returned by the function, check the server's disk space. If the disk is full, the collector may stop writing.

Frequently Asked Questions

Does pg_current_logfile work on all operating systems?

Yes, it works on Linux, Windows, and macOS, provided that the logging_collector is enabled and the logs are being written to files.

Can I use this function to read the contents of the log?

No. This function only tells you the name of the file. To read the logs, you must have access to the server's file system or use a tool like pg_read_file() (which requires superuser privileges).

Why is my log filename different every day?

This is due to the log_filename configuration setting. By default, PostgreSQL often includes the date in the filename to organize logs by day.

What happens to the result after a log rotation?

As soon as the server rotates the log to a new file, pg_current_logfile() will immediately begin returning the name of the new active file.

Is there a performance hit to calling this function?

No. The function retrieves a value already known to the server process; it does not perform a heavy disk scan or complex calculation.