Skip to content

Commit d5706ad

Browse files
committed
Free disk space for dropped relations on commit.
When committing a transaction that dropped a relation, we previously truncated only the first segment file to free up disk space (the one that won't be unlinked until the next checkpoint). Truncate higher numbered segments too, even though we unlink them on commit. This frees the disk space immediately, even if other backends have open file descriptors and might take a long time to get around to handling shared invalidation events and closing them. Also extend the same behavior to the first segment, in recovery. Back-patch to all supported releases. Bug: #16663 Reported-by: Denis Patron <denis.patron@previnet.it> Reviewed-by: Pavel Borisov <pashkin.elfe@gmail.com> Reviewed-by: Neil Chen <carpenter.nail.cz@gmail.com> Reviewed-by: David Zhang <david.zhang@highgo.ca> Discussion: https://postgr.es/m/16663-fe97ccf9932fc800%40postgresql.org
1 parent ed9c9b0 commit d5706ad

File tree

1 file changed

+65
-24
lines changed
  • src/backend/storage/smgr

1 file changed

+65
-24
lines changed

src/backend/storage/smgr/md.c

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,41 @@ mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
406406
mdunlinkfork(rnode, forkNum, isRedo);
407407
}
408408

409+
/*
410+
* Truncate a file to release disk space.
411+
*/
412+
static int
413+
do_truncate(const char *path)
414+
{
415+
int save_errno;
416+
int ret;
417+
int fd;
418+
419+
/* truncate(2) would be easier here, but Windows hasn't got it */
420+
fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
421+
if (fd >= 0)
422+
{
423+
ret = ftruncate(fd, 0);
424+
save_errno = errno;
425+
CloseTransientFile(fd);
426+
errno = save_errno;
427+
}
428+
else
429+
ret = -1;
430+
431+
/* Log a warning here to avoid repetition in callers. */
432+
if (ret < 0 && errno != ENOENT)
433+
{
434+
save_errno = errno;
435+
ereport(WARNING,
436+
(errcode_for_file_access(),
437+
errmsg("could not truncate file \"%s\": %m", path)));
438+
errno = save_errno;
439+
}
440+
441+
return ret;
442+
}
443+
409444
static void
410445
mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
411446
{
@@ -419,33 +454,28 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
419454
*/
420455
if (isRedo || forkNum != MAIN_FORKNUM || RelFileNodeBackendIsTemp(rnode))
421456
{
422-
ret = unlink(path);
423-
if (ret < 0 && errno != ENOENT)
424-
ereport(WARNING,
425-
(errcode_for_file_access(),
426-
errmsg("could not remove file \"%s\": %m", path)));
427-
}
428-
else
429-
{
430-
/* truncate(2) would be easier here, but Windows hasn't got it */
431-
int fd;
432-
433-
fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
434-
if (fd >= 0)
457+
if (!RelFileNodeBackendIsTemp(rnode))
435458
{
436-
int save_errno;
437-
438-
ret = ftruncate(fd, 0);
439-
save_errno = errno;
440-
CloseTransientFile(fd);
441-
errno = save_errno;
459+
/* Prevent other backends' fds from holding on to the disk space */
460+
ret = do_truncate(path);
442461
}
443462
else
444-
ret = -1;
445-
if (ret < 0 && errno != ENOENT)
446-
ereport(WARNING,
447-
(errcode_for_file_access(),
448-
errmsg("could not truncate file \"%s\": %m", path)));
463+
ret = 0;
464+
465+
/* Next unlink the file, unless it was already found to be missing */
466+
if (ret == 0 || errno != ENOENT)
467+
{
468+
ret = unlink(path);
469+
if (ret < 0 && errno != ENOENT)
470+
ereport(WARNING,
471+
(errcode_for_file_access(),
472+
errmsg("could not remove file \"%s\": %m", path)));
473+
}
474+
}
475+
else
476+
{
477+
/* Prevent other backends' fds from holding on to the disk space */
478+
ret = do_truncate(path);
449479

450480
/* Register request to unlink first segment later */
451481
register_unlink(rnode);
@@ -466,6 +496,17 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
466496
for (segno = 1;; segno++)
467497
{
468498
sprintf(segpath, "%s.%u", path, segno);
499+
500+
if (!RelFileNodeBackendIsTemp(rnode))
501+
{
502+
/*
503+
* Prevent other backends' fds from holding on to the disk
504+
* space.
505+
*/
506+
if (do_truncate(segpath) < 0 && errno == ENOENT)
507+
break;
508+
}
509+
469510
if (unlink(segpath) < 0)
470511
{
471512
/* ENOENT is expected after the last segment... */

0 commit comments

Comments
 (0)