A PostgreSQL application can experience slow queries even when its SQL statements and indexes look correct. Sometimes the actual problem is not the query itself but the time PostgreSQL spends waiting for storage operations.

For example, an application may retrieve customer records from a large table. Some pages may already be available in PostgreSQL's shared buffer cache, while others must be read from storage. If the storage system responds slowly, the query can take considerably longer even when PostgreSQL selects an efficient execution plan.

Finding these problems requires more than checking query execution time. Developers and database administrators need to understand how frequently PostgreSQL reads and writes data, how much time these operations consume, and which database processes are generating the I/O workload.

PostgreSQL provides several monitoring views for this purpose. The pg_stat_io view provides statistics about I/O operations performed by different backend types and for different objects and contexts. PostgreSQL 19 adds another useful capability: the IO option in EXPLAIN ANALYZE, which reports asynchronous I/O activity during query execution.

These features help developers investigate storage-related performance problems instead of making unnecessary changes to SQL queries or indexes.

This article explains how to use PostgreSQL 19 I/O statistics, identify slow disk operations, monitor asynchronous I/O, and investigate storage bottlenecks in a production database.

Understanding Database I/O Performance

PostgreSQL stores table and index data in database files. When an application executes a query, PostgreSQL first checks whether the required data is available in its shared buffer cache.

If the requested page is already available, PostgreSQL can use it without requesting another read from the storage system.

If the page is not available, PostgreSQL must obtain it through the operating system's storage interface. The time required for this operation depends on several factors:

  • Storage device performance.
  • Storage latency and throughput.
  • Operating system page cache.
  • PostgreSQL shared buffer configuration.
  • Number of concurrent database operations.
  • Query access patterns.
  • Background operations such as checkpoints and vacuuming.

Consider an application that executes the following query:

SQL
SELECT
    order_id,
    customer_id,
    order_date,
    total_amount
FROM orders
WHERE order_date >= CURRENT_DATE - INTERVAL '30 days';

If the query reads a large number of pages that are not cached, PostgreSQL may perform substantial read activity.

The query could become slower because the storage system cannot provide the required pages quickly enough.

The first task is to determine whether the database is experiencing a storage bottleneck or whether another resource, such as CPU, memory, or locking, is responsible.

Understanding pg_stat_io in PostgreSQL

PostgreSQL provides the pg_stat_io system view to collect I/O statistics across the database cluster.

It contains one row for each supported combination of backend type, target I/O object, and I/O context.

The view helps administrators understand which database processes are performing I/O and how those operations are distributed.

The main columns include:

ColumnDescription
backend_typeDatabase process performing the operation
objectTarget object, such as a relation or WAL
contextContext in which the I/O operation occurred
readsNumber of read operations
read_bytesTotal bytes read
read_timeTime spent waiting for reads
writesNumber of write operations
write_bytesTotal bytes written
write_timeTime spent waiting for writes
writebacksNumber of requested buffer writebacks
extendsNumber of relation extension operations
hitsNumber of shared-buffer hits
evictionsNumber of buffers evicted
fsyncsNumber of fsync operations
fsync_timeTime spent waiting for fsync

The view also contains statistics for different I/O contexts, including normal, bulkread, bulkwrite, vacuum, and init.

For example, bulkread statistics can help identify large read operations performed outside the normal shared-buffer path, while bulkwrite statistics can help investigate large write operations such as COPY.

The context is important because the same type of I/O operation can have different causes depending on which database process performs it.

Checking Overall Database I/O Activity

Start by examining the current I/O statistics.

SQL
SELECT
    backend_type,
    object,
    context,
    reads,
    pg_size_pretty(read_bytes::bigint) AS total_read,
    ROUND(read_time::numeric, 2) AS read_time_ms,
    writes,
    pg_size_pretty(write_bytes::bigint) AS total_written,
    ROUND(write_time::numeric, 2) AS write_time_ms
FROM pg_stat_io
ORDER BY read_time DESC NULLS LAST;

This query provides a useful overview of the database's accumulated read and write activity.

For example, a high read count for client backends may indicate that application queries are requesting many pages that are not already available in shared buffers.

A large amount of write activity from client backends may indicate that application processes are writing dirty buffers rather than relying primarily on background processes.

However, the statistics are cumulative. A large read count does not automatically indicate that the database is currently slow.

Compare the values over a known time interval and consider the number of operations, bytes transferred, and time spent waiting.

Also remember that read_time and write_time remain zero unless the relevant I/O timing configuration is enabled.

Enabling I/O Timing Statistics

PostgreSQL can collect timing information for database I/O operations.

The relevant settings are:

  • track_io_timing for relation I/O.
  • track_wal_io_timing for WAL I/O.

Check their current values:

SQL
SHOW track_io_timing;

SHOW track_wal_io_timing;

If the required timing information is disabled, an administrator can enable it:

SQL
ALTER SYSTEM
SET track_io_timing = on;

ALTER SYSTEM
SET track_wal_io_timing = on;

Reload the configuration:

SQL
SELECT pg_reload_conf();

Verify that the settings have taken effect:

SQL
SHOW track_io_timing;

SHOW track_wal_io_timing;

Timing collection introduces some overhead because PostgreSQL needs to measure elapsed time around I/O operations. Measure that overhead in your environment before enabling it across a heavily loaded production system.

Also, enabling timing does not populate historical timing information. Collect data over a representative workload period before drawing conclusions.

Finding Backend Processes With High I/O Activity

The pg_stat_io view separates statistics by backend type.

This makes it possible to understand whether I/O is primarily generated by application connections or background database processes.

For example, run:

SQL
SELECT
    backend_type,
    SUM(reads) AS total_reads,
    SUM(read_bytes) AS total_read_bytes,
    ROUND(SUM(read_time)::numeric, 2)
        AS total_read_time_ms,
    SUM(writes) AS total_writes,
    SUM(write_bytes) AS total_write_bytes,
    ROUND(SUM(write_time)::numeric, 2)
        AS total_write_time_ms
FROM pg_stat_io
GROUP BY backend_type
ORDER BY total_read_time_ms DESC NULLS LAST;

This report combines I/O activity for each backend type.

Some common backend types include:

  • client backend
  • checkpointer
  • background writer
  • autovacuum worker
  • WAL writer

Consider a database where client backends perform a large amount of reading while the checkpointer performs substantial writing.

This may indicate that application queries are generating significant read activity while PostgreSQL is also handling checkpoint-related storage operations.

The next step is to identify which tables, indexes, or queries contribute to the workload.

Do not assume that a particular backend is responsible for slow storage simply because its cumulative I/O count is high. Compare activity over the same time interval and investigate the corresponding workload.

Identifying Tables With High Read Activity

The pg_statio_user_tables view provides table-level statistics about heap, index, and TOAST access.

It is useful when investigating which application tables generate substantial read activity.

Run the following query:

SQL
SELECT
    schemaname,
    relname AS table_name,
    heap_blks_read,
    heap_blks_hit,
    idx_blks_read,
    idx_blks_hit,
    toast_blks_read,
    toast_blks_hit
FROM pg_statio_user_tables
ORDER BY heap_blks_read DESC
LIMIT 20;

The heap_blks_read column indicates the number of table blocks read into PostgreSQL's buffer cache.

The heap_blks_hit column indicates how many requested table blocks were already found in shared buffers.

Similarly, idx_blks_read and idx_blks_hit describe index block activity.

A table with a high number of heap block reads may deserve further investigation, especially when the application frequently accesses only a small subset of its records.

However, a large analytical table may legitimately require many reads. The important question is whether the access pattern matches the application's expected workload.

Finding Queries That Perform Expensive I/O

Table-level statistics identify where database activity occurs, but developers also need to identify the SQL statements responsible for it.

The pg_stat_statements extension provides aggregated query statistics, including execution time and buffer activity.

First, check whether the extension is available:

SQL
SELECT *
FROM pg_available_extensions
WHERE name = 'pg_stat_statements';

If it is not already configured, the database administrator must add it to shared_preload_libraries and restart PostgreSQL.

Then create the extension in the target database:

SQL
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

The following query identifies statements with significant shared-buffer read activity:

SQL
SELECT
    queryid,
    calls,
    ROUND(total_exec_time::numeric, 2)
        AS total_exec_ms,
    shared_blks_read,
    shared_blks_hit,
    temp_blks_read,
    temp_blks_written,
    query
FROM pg_stat_statements
ORDER BY shared_blks_read DESC
LIMIT 20;

This helps identify statements that repeatedly require blocks to be read into shared buffers.

The temp_blks_read and temp_blks_written columns are also useful because temporary file activity can indicate expensive sorting or hashing operations.

A query with many shared block reads is not necessarily slow. The operating system may already have the corresponding pages in its filesystem cache, and PostgreSQL's statistics do not distinguish that situation from a physical device read.

Use these results to select queries for deeper investigation.

Using EXPLAIN ANALYZE With the New IO Option

One of the important PostgreSQL 19 improvements is the IO option for EXPLAIN ANALYZE.

It provides additional information about asynchronous I/O activity during query execution.

This is particularly useful because PostgreSQL 18 introduced asynchronous I/O infrastructure, and PostgreSQL 19 builds on that capability with improved worker management and read-ahead scheduling.

Consider the following query:

SQL
EXPLAIN (
    ANALYZE,
    BUFFERS,
    IO,
    SETTINGS
)
SELECT
    order_id,
    customer_id,
    order_date,
    total_amount
FROM orders
WHERE order_date >= CURRENT_DATE - INTERVAL '30 days';

The options provide different information:

  • ANALYZE executes the query and reports actual execution details.
  • BUFFERS reports shared, local, and temporary buffer activity.
  • IO reports asynchronous I/O activity.
  • SETTINGS displays relevant non-default configuration settings.

The IO option can help developers understand how asynchronous reads are being issued and completed during query execution.

This is useful when a query reads large amounts of data and storage latency appears to be affecting execution time.

Remember that EXPLAIN ANALYZE executes the statement. For SELECT statements, this can still cause side effects if functions called by the query modify data or perform external operations. Use a safe environment when necessary.

The IO option provides additional visibility, but it does not replace operating system monitoring or storage-level diagnostics.

Monitoring Slow I/O Through Wait Events

PostgreSQL also provides pg_stat_activity to show what database sessions are currently doing.

The wait_event_type and wait_event columns help identify sessions waiting for I/O operations.

Run:

SQL
SELECT
    pid,
    usename,
    application_name,
    state,
    wait_event_type,
    wait_event,
    now() - query_start AS query_duration,
    query
FROM pg_stat_activity
WHERE state = 'active'
ORDER BY query_start;

Look for sessions where wait_event_type is IO.

PostgreSQL 19 provides several I/O-related wait events, including:

Wait eventMeaning
DataFileReadWaiting for a relation data file read
DataFileWriteWaiting for a relation data file write
DataFileSyncWaiting for relation data synchronization
DataFilePrefetchWaiting for asynchronous data prefetch
AioIoCompletionWaiting for another process to complete I/O
AioIoUringExecutionWaiting for I/O execution through io_uring
AioIoUringSubmitWaiting for I/O submission through io_uring

A session waiting on an I/O event is not automatically experiencing a storage failure. Some waiting is normal during database execution.

Investigate repeated or long waits together with query execution time, storage metrics, and database workload.

Understanding PostgreSQL 19 Asynchronous I/O Settings

PostgreSQL 19 continues improving the asynchronous I/O system introduced in PostgreSQL 18.

The io_method setting controls the asynchronous I/O implementation.

Check the current configuration:

SQL
SHOW io_method;

PostgreSQL 19 supports the worker method, which can automatically adjust the number of I/O workers using these settings:

SQL
SHOW io_min_workers;

SHOW io_max_workers;

SHOW io_worker_idle_timeout;

SHOW io_worker_launch_interval;

The worker method is the default in PostgreSQL 19.

The new worker settings provide more control over how many background workers handle asynchronous I/O activity.

However, increasing worker counts does not automatically improve performance. The storage system, CPU availability, query patterns, and concurrent workload all influence the results.

Make configuration changes only after identifying a specific bottleneck and testing the impact under representative load.

Best Practices for Production I/O Monitoring

I/O monitoring is most useful when statistics are collected consistently and compared over time.

The following practices help build a reliable monitoring process:

  1. Enable I/O timing after evaluating its overhead in your environment.
  2. Capture pg_stat_io statistics during normal and peak workloads.
  3. Compare read and write activity across backend types.
  4. Use pg_statio_user_tables to identify tables with high block access.
  5. Use pg_stat_statements to locate queries responsible for substantial buffer reads.
  6. Use EXPLAIN ANALYZE with BUFFERS and IO to investigate individual queries.
  7. Monitor pg_stat_activity for long-running I/O waits.
  8. Compare database statistics with operating system and storage monitoring.
  9. Review checkpoint, vacuum, and WAL activity when write latency increases.
  10. Keep historical measurements so that performance changes can be identified.

Do not reset database statistics simply to make a report easier to read. Instead, capture periodic snapshots and calculate differences between measurements. This provides a clearer view of I/O activity during a specific interval.

Summary

Slow disk operations can affect PostgreSQL application performance even when queries and indexes are correctly designed. Finding the actual cause requires visibility into reads, writes, buffer activity, asynchronous I/O, and database wait events.

PostgreSQL provides pg_stat_io for cluster-wide I/O statistics and pg_statio_user_tables for table-level access information. The pg_stat_statements extension helps identify SQL statements that generate substantial buffer activity.

PostgreSQL 19 adds the IO option to EXPLAIN ANALYZE, providing additional visibility into asynchronous I/O during query execution. Combined with pg_stat_activity and operating system monitoring, these tools help developers investigate storage-related performance problems more accurately.

The best approach is to collect statistics over representative workload periods, identify the processes and queries generating the most I/O, and investigate the underlying storage behavior before changing database configuration. This helps avoid unnecessary tuning and makes PostgreSQL performance troubleshooting more systematic.