Add smgrzeroextend(), FileZero(), FileFallocate()
authorAndres Freund <[email protected]>
Wed, 5 Apr 2023 17:06:39 +0000 (10:06 -0700)
committerAndres Freund <[email protected]>
Wed, 5 Apr 2023 17:06:39 +0000 (10:06 -0700)
commit4d330a61bb1969df31f2cebfe1ba9d1d004346d8
treebe72b30736467697a10d744672b282afb6d13eb7
parent4766eef3174b0b1bd8d2428381a7a33813386a6a
Add smgrzeroextend(), FileZero(), FileFallocate()

smgrzeroextend() uses FileFallocate() to efficiently extend files by multiple
blocks. When extending by a small number of blocks, use FileZero() instead, as
using posix_fallocate() for small numbers of blocks is inefficient for some
file systems / operating systems. FileZero() is also used as the fallback for
FileFallocate() on platforms / filesystems that don't support fallocate.

A big advantage of using posix_fallocate() is that it typically won't cause
dirty buffers in the kernel pagecache. So far the most common pattern in our
code is that we smgrextend() a page full of zeroes and put the corresponding
page into shared buffers, from where we later write out the actual contents of
the page. If the kernel, e.g. due to memory pressure or elapsed time, already
wrote back the all-zeroes page, this can lead to doubling the amount of writes
reaching storage.

There are no users of smgrzeroextend() as of this commit. That will follow in
future commits.

Reviewed-by: Melanie Plageman <[email protected]>
Reviewed-by: Heikki Linnakangas <[email protected]>
Reviewed-by: Kyotaro Horiguchi <[email protected]>
Reviewed-by: David Rowley <[email protected]>
Reviewed-by: John Naylor <[email protected]>
Discussion: https://postgr.es/m/20221029025420[email protected]
src/backend/storage/file/fd.c
src/backend/storage/smgr/md.c
src/backend/storage/smgr/smgr.c
src/include/storage/fd.h
src/include/storage/md.h
src/include/storage/smgr.h