Don't close relation files when the visibility map is extended

From: Bryan Green <dbryan(dot)green(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Don't close relation files when the visibility map is extended
Date: 2026-10-26 22:06:25
Message-ID: 62fd95fd-d81d-4566-998b-dc6e9422e9f1@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Greetings,

When vm_extend() grows the visibility map it sends an smgr
invalidation. Every backend that has the relation open then closes
all of its files, and reopens each segment the next time it touches
the table. VACUUM extends the map about once per 255MB of heap. On a
multi-TB table with a few hundred backends inserting, that is a lot of
opens, and as far as I can tell none of them are needed. The other
backends only have to learn that the map got bigger.

This assumes open() is cheap, and it isn't always. I ran into it on
17 with a ~4TB partition, on a system where open() goes through a
filesystem encryption layer. Each time VACUUM extended the map, every
inserting backend reopened about 4,000 segments, and inserts stalled
for seconds at a time. The same invalidation is in every branch from
14 to master, and on master on-access pruning can extend the map as
well, so it isn't only VACUUM.

The attached patch adds an invalidation message that just resets the
cached fork sizes, and vm_extend() sends that instead. Readers see
new map pages when they do now. I also tried dropping the
invalidation entirely, but then a backend that doesn't prune the
pages itself keeps a stale map size until VACUUM finishes.

On eCryptfs, with about 2,700 segments and 300 inserting clients,
VACUUM went from about 10 million opens to under 1 million, the extra
system CPU it caused mostly went away, and p99 insert latency during
VACUUM dropped from ~350ms to under 20ms. check-world passes.

Is there a reason the full smgr invalidation is needed here that I'm
missing?

--
Bryan Green
EDB: https://www.enterprisedb.com

Attachment Content-Type Size
v1-0001-visibility-extends-should-not-cause-file-opens.patch text/plain 9.8 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeff Davis 2026-10-26 22:15:50 Re: [PG19] ILIKE index scan misses rows with a libc UTF-8 collation on macOS
Previous Message Hannu Krosing 2026-10-26 21:49:22 Re: ZSTD TOAST compression, and an extensible compression method encoding