PostgreSQL and MySQL take different paths when performing physical reads. PostgreSQL loads data pages from storage into the OS page cache, while MySQL’s InnoDB bypasses it by default. This post compares the two read paths through experiments, explains the design trade-offs, and shows how those differences inform the sizing of PostgreSQL’s shared buffers and MySQL’s InnoDB buffer pool.

PostgreSQL: Always Reads from the Linux Page Cache

To see how the Linux page cache affects PostgreSQL query performance, we prepared a 1 GiB table and ran a full table scan before and after dropping the Linux page cache. PostgreSQL’s shared_buffers was set to its default of 128 MiB.

First, we started iostat -d -z -y sdb 2 in one terminal to monitor I/O on /dev/sdb, the device that holds the table data. Then, in a separate psql session, we ran the following commands:

1. Configure the psql session

Disable parallel query workers so that each run is easier to compare, and enable track_io_timing so PostgreSQL reports time spent on read I/O. Each SET response confirms that the corresponding session setting was accepted.

postgres=# SET max_parallel_workers_per_gather = 0;
SET
postgres=# SET track_io_timing = on;
SET

2. Run the query before dropping the Linux page cache

ANALYZE executes the query, and BUFFERS reports page accesses. TIMING OFF skips per-node execution timing but keeps the total execution time and the separate I/O timings enabled above.

postgres=# EXPLAIN (ANALYZE, BUFFERS, TIMING OFF) SELECT count(*) FROM yuan_1gb;

Output:

                                               QUERY PLAN
--------------------------------------------------------------------------------------------------------
 Aggregate  (cost=142540.80..142540.81 rows=1 width=8) (actual rows=1.00 loops=1)
   Buffers: shared hit=16114 read=114958
   I/O Timings: shared read=7.311
   ->  Seq Scan on yuan_1gb  (cost=0.00..140247.04 rows=917504 width=0) (actual rows=917504.00 loops=1)
         Buffers: shared hit=16114 read=114958
         I/O Timings: shared read=7.311
 Planning:
   Buffers: shared hit=15 read=4
   I/O Timings: shared read=1.237
 Planning Time: 1.412 ms
 Execution Time: 268.179 ms
(11 rows)

The Seq Scan on yuan_1gb reported 16,114 shared-buffer hits. At 8 KiB per page, that’s about 125.9 MiB of page accesses served from shared buffers—equivalent to 98.4% of the configured 128 MiB buffer pool.

For the other 114,958 pages (about 898.1 MiB, or 0.88 GiB), the plan reports read=114958 under Buffers: shared. PostgreSQL can’t tell from this count whether Linux served those pages from its page cache or fetched them from storage. Those reads took only 7.311 ms, and iostat in the other terminal showed little or no device I/O on /dev/sdbduring the run. Together, these observations suggest that Linux served the reads from its page cache.

3. Drop the Linux page cache and check memory

Writing 1 to drop_caches drops clean Linux page-cache pages without clearing PostgreSQL’s shared buffers. free -hthen shows Linux memory in human-readable units.

postgres=# \! echo 1 | sudo tee /proc/sys/vm/drop_caches >/dev/null
postgres=# \! free -h

Output:

               total        used        free      shared  buff/cache   available
Mem:            15Gi       2.8Gi        11Gi       467Mi       1.1Gi        12Gi
Swap:          4.0Gi          0B       4.0Gi

buff/cache shows 1.1 GiB after the drop. It’s a host-wide figure, not a measurement of which table pages are cached.

4. Run the same query with a cold Linux page cache

postgres=# EXPLAIN (ANALYZE, BUFFERS, TIMING OFF) SELECT count(*) FROM yuan_1gb;

Output:

                                               QUERY PLAN
--------------------------------------------------------------------------------------------------------
 Aggregate  (cost=142540.80..142540.81 rows=1 width=8) (actual rows=1.00 loops=1)
   Buffers: shared hit=16114 read=114958
   I/O Timings: shared read=36653.116
   ->  Seq Scan on yuan_1gb  (cost=0.00..140247.04 rows=917504 width=0) (actual rows=917504.00 loops=1)
         Buffers: shared hit=16114 read=114958
         I/O Timings: shared read=36653.116
 Planning Time: 0.057 ms
 Execution Time: 36933.700 ms
(8 rows)

The shared-buffer hit and read counts are identical to those of the first run, but read I/O time jumps to 36.65 s out of 36.93 s total. Meanwhile, iostat in the other terminal shows sustained read activity on /dev/sdb. Together, these observations suggest that Linux had to fetch most of the pages PostgreSQL missed in shared buffers from storage rather than serve them from its page cache.

5. Check memory again and rerun the query

postgres=# \! free -h

Output:

               total        used        free      shared  buff/cache   available
Mem:            15Gi       2.8Gi        10Gi       467Mi       2.0Gi        12Gi
Swap:          4.0Gi          0B       4.0Gi

After the cold run, buff/cache rose from 1.1 GiB to 2.0 GiB. That increase is consistent with Linux caching the table pages it just fetched: PostgreSQL recorded 114,958 reads, equivalent to about 898.1 MiB (0.88 GiB) at 8 KiB per page.

Then we ran the same query again without dropping the Linux page cache:

postgres=# EXPLAIN (ANALYZE, BUFFERS, TIMING OFF) SELECT count(*) FROM yuan_1gb;

Output:

                                               QUERY PLAN
--------------------------------------------------------------------------------------------------------
 Aggregate  (cost=142540.80..142540.81 rows=1 width=8) (actual rows=1.00 loops=1)
   Buffers: shared hit=16168 read=114904
   I/O Timings: shared read=5.250
   ->  Seq Scan on yuan_1gb  (cost=0.00..140247.04 rows=917504 width=0) (actual rows=917504.00 loops=1)
         Buffers: shared hit=16168 read=114904
         I/O Timings: shared read=5.250
 Planning Time: 0.059 ms
 Execution Time: 228.832 ms
(8 rows)



The rerun still reports 114,904 shared reads, but read I/O takes only 5.250 ms, and the query finishes in 229 ms. Meanwhile, iostat shows little device I/O on /dev/sdb. Together, these observations indicate that Linux served most of those reads from its page cache rather than from storage.

What the Experiment Shows About PostgreSQL Physical Reads

Query runShared-buffer hitsShared-buffer readsRead I/O timeExecution time
Before dropping the Linux page cache16,114114,9587.311 ms268.179 ms
After dropping the Linux page cache16,114114,95836,653.116 ms36,933.700 ms
Rerun with a warm Linux page cache16,168114,9045.250 ms228.832 ms

The experiment shows that when PostgreSQL reads a data page from storage, Linux stores a copy in its page cache. If a later request misses in PostgreSQL’s shared buffers, Linux can serve the page from memory, as long as it’s still cached, without another physical read.

MySQL: InnoDB Reads Bypass the Linux Page Cache by Default

In MySQL, innodb_flush_method controls how InnoDB performs file I/O. On Linux, the default is O_DIRECT where supported; on Windows, it is unbuffered. Both settings are strongly recommended best practices and bypass the operating system’s file cache for InnoDB data-file reads. For the Linux experiment, we prepared a 1 GiB InnoDB table, confirmed MySQL was using O_DIRECT, and compared scan times before and after enlarging the buffer pool.

1. Check the InnoDB buffer pool and I/O settings

mysql> SHOW VARIABLES LIKE 'innodb_buffer_pool_size';
+-------------------------+-----------+
| Variable_name | Value |
+-------------------------+-----------+
| innodb_buffer_pool_size | 134217728 |
+-------------------------+-----------+
1 row in set (0.010 sec)
mysql> SHOW VARIABLES LIKE 'innodb_flush_method';
+---------------------+----------+
| Variable_name | Value |
+---------------------+----------+
| innodb_flush_method | O_DIRECT |
+---------------------+----------+
1 row in set (0.002 sec)

The two SHOW VARIABLES commands confirm a 128 MiB buffer pool and O_DIRECT. The table is estimated at 1,172 MiB, so it cannot fit in the initial pool.

2. Drop the Linux page cache and run the query

mysql> \! echo 1 | sudo tee /proc/sys/vm/drop_caches >/dev/null
mysql> \! free -h
total used free shared buff/cache available
Mem: 15Gi 2.9Gi 11Gi 413Mi 1.1Gi 12Gi
Swap: 4.0Gi 0B 4.0Gi
mysql> EXPLAIN ANALYZE SELECT count(*) FROM yuan.yuan_1gb;
+-------------------------------------------------------------------------+
| EXPLAIN |
+-------------------------------------------------------------------------+
| -> Count rows in yuan_1gb (actual time=155656..155656 rows=1 loops=1)
|
+-------------------------------------------------------------------------+
1 row in set (2 min 35.656 sec)
mysql> \! free -h
total used free shared buff/cache available
Mem: 15Gi 2.9Gi 11Gi 413Mi 1.1Gi 12Gi
Swap: 4.0Gi 0B 4.0Gi

The query took 2 minutes and 36 seconds. During the run, iostat showed sustained read activity on /dev/sdb, while free -h reported about 1.1 GiB in buff/cache both before and after the query. These observations are consistent with O_DIRECT: InnoDB read data from storage without warming the Linux page cache. With the InnoDB buffer pool still at 128 MiB, too small to hold the table, we wouldn’t expect a rerun to get the same page-cache speedup we saw in PostgreSQL.

3. Resize the InnoDB buffer pool online and run the query again

mysql> SET GLOBAL innodb_buffer_pool_size = 2G;
ERROR 1232 (42000): Incorrect argument type to variable 'innodb_buffer_pool_size'
mysql> SET GLOBAL innodb_buffer_pool_size = 2 * 1024 * 1024 * 1024;
Query OK, 0 rows affected (0.000 sec)
mysql> EXPLAIN ANALYZE SELECT count(*) FROM yuan.yuan_1gb;
+-------------------------------------------------------------------------+
| EXPLAIN |
+-------------------------------------------------------------------------+
| -> Count rows in yuan_1gb (actual time=152495..152495 rows=1 loops=1)
|
+-------------------------------------------------------------------------+
1 row in set (2 min 32.495 sec)

MySQL rejects 2G in this SET GLOBAL statement, but accepts 2 1024 1024 * 1024 as a size in bytes. The first run after resizing still took 2 minutes and 32 seconds. Enlarging the buffer pool made room for the table, but it didn’t load the pages that weren’t already cached. The query still had to read those pages from storage.

4. Rerun the query with a warm InnoDB buffer pool

mysql> EXPLAIN ANALYZE SELECT count(*) FROM yuan.yuan_1gb;
+---------------------------------------------------------------------+
| EXPLAIN |
+---------------------------------------------------------------------+
| -> Count rows in yuan_1gb (actual time=64.6..64.6 rows=1 loops=1)
|
+---------------------------------------------------------------------+
1 row in set (0.065 sec)
mysql> \! free -h
total used free shared buff/cache available
Mem: 15Gi 4.1Gi 10Gi 413Mi 1.1Gi 11Gi
Swap: 4.0Gi 0B 4.0Gi
mysql>

This time the query finishes in 65 ms, while Linux buff/cache remains around 1.1 GiB. The result is consistent with the previous run warming the InnoDB buffer pool, not the Linux page cache.

What the Experiment Shows About InnoDB Physical Reads

Query runInnoDB buffer pool sizeElapsed time reported by the MySQL client
After dropping the Linux page cache128 MiB155,656 ms
First run after enlarging the buffer pool2 GiB152,495 ms
Rerun with a warm InnoDB buffer pool2 GiB65 ms

With O_DIRECT, InnoDB reads a data page that misses in its buffer pool without going through the Linux page cache. Unlike PostgreSQL’s buffered reads in this experiment, those reads do not leave a copy in the Linux page cache for the next request. A later request can avoid another physical read if the page is still in the InnoDB buffer pool.

Read Paths and Buffer Sizing: PostgreSQL vs. MySQL

The diagram below depicts how PostgreSQL and MySQL read a data page:

PostgreSQL with buffered I/O: shared buffers → Linux page cache → storage. A page evicted from shared buffers may still be in the Linux page cache, so PostgreSQL can get it back without another storage read. Buffered I/O also benefits from kernel optimizations such as read-ahead. The trade-off is that the same page can occupy RAM in both caches, and a Linux page-cache hit still requires copying the page into shared buffers.

MySQL InnoDB with O_DIRECT: buffer pool → storage. Data-file reads and writes bypass the Linux page cache. InnoDB manages which data pages stay cached and can do its own read-ahead. This avoids keeping two cached copies and reduces the CPU work of copying data through the Linux page cache, but an InnoDB buffer-pool miss has no Linux page-cache fallback.

PostgreSQL: The official documentation recommends 25% of system RAM as a reasonable starting point for shared_buffers on a dedicated database server with at least 1 GB of RAM. Because PostgreSQL also relies on the OS page cache with buffered I/O, allocating more than 40% of RAM to shared_buffers is unlikely to work better than a smaller allocation.

MySQL with O_DIRECT: On a dedicated MySQL server, you can set innodb_buffer_pool_size as large as possible without causing the system to swap under peak load.

Leave a Reply

I’m Yuan

Welcome to the blog of a database performance maven. Feel free to contact me to share and discuss database knowledge.

My Oracle Book

My 26 Oracle Certifications

A screenshot from certview.oracle.com

My MySQL Book

MySQL 8.0 Operations and Optimization

Let’s connect