mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-03 07:57:20 +02:00
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
This commit is contained in:
parent
ed9c9b0335
commit
d5706ad7b7
@ -406,6 +406,41 @@ mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
|
|||||||
mdunlinkfork(rnode, forkNum, isRedo);
|
mdunlinkfork(rnode, forkNum, isRedo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Truncate a file to release disk space.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
do_truncate(const char *path)
|
||||||
|
{
|
||||||
|
int save_errno;
|
||||||
|
int ret;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
/* truncate(2) would be easier here, but Windows hasn't got it */
|
||||||
|
fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
|
||||||
|
if (fd >= 0)
|
||||||
|
{
|
||||||
|
ret = ftruncate(fd, 0);
|
||||||
|
save_errno = errno;
|
||||||
|
CloseTransientFile(fd);
|
||||||
|
errno = save_errno;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ret = -1;
|
||||||
|
|
||||||
|
/* Log a warning here to avoid repetition in callers. */
|
||||||
|
if (ret < 0 && errno != ENOENT)
|
||||||
|
{
|
||||||
|
save_errno = errno;
|
||||||
|
ereport(WARNING,
|
||||||
|
(errcode_for_file_access(),
|
||||||
|
errmsg("could not truncate file \"%s\": %m", path)));
|
||||||
|
errno = save_errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
|
mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
|
||||||
{
|
{
|
||||||
@ -419,33 +454,28 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
|
|||||||
*/
|
*/
|
||||||
if (isRedo || forkNum != MAIN_FORKNUM || RelFileNodeBackendIsTemp(rnode))
|
if (isRedo || forkNum != MAIN_FORKNUM || RelFileNodeBackendIsTemp(rnode))
|
||||||
{
|
{
|
||||||
ret = unlink(path);
|
if (!RelFileNodeBackendIsTemp(rnode))
|
||||||
if (ret < 0 && errno != ENOENT)
|
{
|
||||||
ereport(WARNING,
|
/* Prevent other backends' fds from holding on to the disk space */
|
||||||
(errcode_for_file_access(),
|
ret = do_truncate(path);
|
||||||
errmsg("could not remove file \"%s\": %m", path)));
|
}
|
||||||
|
else
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
/* Next unlink the file, unless it was already found to be missing */
|
||||||
|
if (ret == 0 || errno != ENOENT)
|
||||||
|
{
|
||||||
|
ret = unlink(path);
|
||||||
|
if (ret < 0 && errno != ENOENT)
|
||||||
|
ereport(WARNING,
|
||||||
|
(errcode_for_file_access(),
|
||||||
|
errmsg("could not remove file \"%s\": %m", path)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* truncate(2) would be easier here, but Windows hasn't got it */
|
/* Prevent other backends' fds from holding on to the disk space */
|
||||||
int fd;
|
ret = do_truncate(path);
|
||||||
|
|
||||||
fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
|
|
||||||
if (fd >= 0)
|
|
||||||
{
|
|
||||||
int save_errno;
|
|
||||||
|
|
||||||
ret = ftruncate(fd, 0);
|
|
||||||
save_errno = errno;
|
|
||||||
CloseTransientFile(fd);
|
|
||||||
errno = save_errno;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
ret = -1;
|
|
||||||
if (ret < 0 && errno != ENOENT)
|
|
||||||
ereport(WARNING,
|
|
||||||
(errcode_for_file_access(),
|
|
||||||
errmsg("could not truncate file \"%s\": %m", path)));
|
|
||||||
|
|
||||||
/* Register request to unlink first segment later */
|
/* Register request to unlink first segment later */
|
||||||
register_unlink(rnode);
|
register_unlink(rnode);
|
||||||
@ -466,6 +496,17 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
|
|||||||
for (segno = 1;; segno++)
|
for (segno = 1;; segno++)
|
||||||
{
|
{
|
||||||
sprintf(segpath, "%s.%u", path, segno);
|
sprintf(segpath, "%s.%u", path, segno);
|
||||||
|
|
||||||
|
if (!RelFileNodeBackendIsTemp(rnode))
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Prevent other backends' fds from holding on to the disk
|
||||||
|
* space.
|
||||||
|
*/
|
||||||
|
if (do_truncate(segpath) < 0 && errno == ENOENT)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (unlink(segpath) < 0)
|
if (unlink(segpath) < 0)
|
||||||
{
|
{
|
||||||
/* ENOENT is expected after the last segment... */
|
/* ENOENT is expected after the last segment... */
|
||||||
|
Loading…
Reference in New Issue
Block a user