Skip to content

Commit 712704d

Browse files
committed
Remove redundant ftruncate() for POSIX DSM memory.
In early releases of the DSM infrastructure, it was possible to resize segments. That was removed in release 12 by commit 3c60d0f. Now the ftruncate() + posix_fallocate() sequence during DSM segment creation has a redundant step: we're always extending from zero to the desired size, so we might as well just call posix_fallocate(). Let's also include the remaining ftruncate() call (non-Linux POSIX systems) in the wait event reporting, for good measure. Discussion: https://postgr.es/m/CA%2BhUKGJSm-nq8s%2B_59zb7NbFQF-OS%3DxTnTAiGLrQpuSmU2y_1A%40mail.gmail.com
1 parent 4518c79 commit 712704d

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/backend/storage/ipc/dsm_impl.c

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -364,43 +364,39 @@ dsm_impl_posix_resize(int fd, off_t size)
364364
*/
365365
PG_SETMASK(&BlockSig);
366366

367-
/* Truncate (or extend) the file to the requested size. */
368-
do
369-
{
370-
rc = ftruncate(fd, size);
371-
} while (rc < 0 && errno == EINTR);
372-
367+
pgstat_report_wait_start(WAIT_EVENT_DSM_FILL_ZERO_WRITE);
368+
#if defined(HAVE_POSIX_FALLOCATE) && defined(__linux__)
373369
/*
374-
* On Linux, a shm_open fd is backed by a tmpfs file. After resizing with
375-
* ftruncate, the file may contain a hole. Accessing memory backed by a
370+
* On Linux, a shm_open fd is backed by a tmpfs file. If we were to use
371+
* ftruncate, the file would contain a hole. Accessing memory backed by a
376372
* hole causes tmpfs to allocate pages, which fails with SIGBUS if there
377373
* is no more tmpfs space available. So we ask tmpfs to allocate pages
378374
* here, so we can fail gracefully with ENOSPC now rather than risking
379375
* SIGBUS later.
376+
*
377+
* We still use a traditional EINTR retry loop to handle SIGCONT.
378+
* posix_fallocate() doesn't restart automatically, and we don't want
379+
* this to fail if you attach a debugger.
380380
*/
381-
#if defined(HAVE_POSIX_FALLOCATE) && defined(__linux__)
382-
if (rc == 0)
381+
do
383382
{
384-
/*
385-
* We still use a traditional EINTR retry loop to handle SIGCONT.
386-
* posix_fallocate() doesn't restart automatically, and we don't want
387-
* this to fail if you attach a debugger.
388-
*/
389-
pgstat_report_wait_start(WAIT_EVENT_DSM_FILL_ZERO_WRITE);
390-
do
391-
{
392-
rc = posix_fallocate(fd, 0, size);
393-
} while (rc == EINTR);
394-
pgstat_report_wait_end();
383+
rc = posix_fallocate(fd, 0, size);
384+
} while (rc == EINTR);
395385

396-
/*
397-
* The caller expects errno to be set, but posix_fallocate() doesn't
398-
* set it. Instead it returns error numbers directly. So set errno,
399-
* even though we'll also return rc to indicate success or failure.
400-
*/
401-
errno = rc;
402-
}
403-
#endif /* HAVE_POSIX_FALLOCATE && __linux__ */
386+
/*
387+
* The caller expects errno to be set, but posix_fallocate() doesn't
388+
* set it. Instead it returns error numbers directly. So set errno,
389+
* even though we'll also return rc to indicate success or failure.
390+
*/
391+
errno = rc;
392+
#else
393+
/* Extend the file to the requested size. */
394+
do
395+
{
396+
rc = ftruncate(fd, size);
397+
} while (rc < 0 && errno == EINTR);
398+
#endif
399+
pgstat_report_wait_end();
404400

405401
save_errno = errno;
406402
PG_SETMASK(&UnBlockSig);

0 commit comments

Comments
 (0)