Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 50f3385

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 b2603f1 commit 50f3385

File tree

1 file changed

+69
-31
lines changed
  • src/backend/storage/smgr

1 file changed

+69
-31
lines changed

src/backend/storage/smgr/md.c

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,41 @@ mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
273273
mdunlinkfork(rnode, forkNum, isRedo);
274274
}
275275

276+
/*
277+
* Truncate a file to release disk space.
278+
*/
279+
static int
280+
do_truncate(const char *path)
281+
{
282+
int save_errno;
283+
int ret;
284+
int fd;
285+
286+
/* truncate(2) would be easier here, but Windows hasn't got it */
287+
fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
288+
if (fd >= 0)
289+
{
290+
ret = ftruncate(fd, 0);
291+
save_errno = errno;
292+
CloseTransientFile(fd);
293+
errno = save_errno;
294+
}
295+
else
296+
ret = -1;
297+
298+
/* Log a warning here to avoid repetition in callers. */
299+
if (ret < 0 && errno != ENOENT)
300+
{
301+
save_errno = errno;
302+
ereport(WARNING,
303+
(errcode_for_file_access(),
304+
errmsg("could not truncate file \"%s\": %m", path)));
305+
errno = save_errno;
306+
}
307+
308+
return ret;
309+
}
310+
276311
static void
277312
mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
278313
{
@@ -286,38 +321,31 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
286321
*/
287322
if (isRedo || forkNum != MAIN_FORKNUM || RelFileNodeBackendIsTemp(rnode))
288323
{
289-
/* First, forget any pending sync requests for the first segment */
290324
if (!RelFileNodeBackendIsTemp(rnode))
325+
{
326+
/* Prevent other backends' fds from holding on to the disk space */
327+
ret = do_truncate(path);
328+
329+
/* Forget any pending sync requests for the first segment */
291330
register_forget_request(rnode, forkNum, 0 /* first seg */ );
331+
}
332+
else
333+
ret = 0;
292334

293-
/* Next unlink the file */
294-
ret = unlink(path);
295-
if (ret < 0 && errno != ENOENT)
296-
ereport(WARNING,
297-
(errcode_for_file_access(),
298-
errmsg("could not remove file \"%s\": %m", path)));
335+
/* Next unlink the file, unless it was already found to be missing */
336+
if (ret == 0 || errno != ENOENT)
337+
{
338+
ret = unlink(path);
339+
if (ret < 0 && errno != ENOENT)
340+
ereport(WARNING,
341+
(errcode_for_file_access(),
342+
errmsg("could not remove file \"%s\": %m", path)));
343+
}
299344
}
300345
else
301346
{
302-
/* truncate(2) would be easier here, but Windows hasn't got it */
303-
int fd;
304-
305-
fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
306-
if (fd >= 0)
307-
{
308-
int save_errno;
309-
310-
ret = ftruncate(fd, 0);
311-
save_errno = errno;
312-
CloseTransientFile(fd);
313-
errno = save_errno;
314-
}
315-
else
316-
ret = -1;
317-
if (ret < 0 && errno != ENOENT)
318-
ereport(WARNING,
319-
(errcode_for_file_access(),
320-
errmsg("could not truncate file \"%s\": %m", path)));
347+
/* Prevent other backends' fds from holding on to the disk space */
348+
ret = do_truncate(path);
321349

322350
/* Register request to unlink first segment later */
323351
register_unlink_segment(rnode, forkNum, 0 /* first seg */ );
@@ -337,14 +365,24 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
337365
*/
338366
for (segno = 1;; segno++)
339367
{
340-
/*
341-
* Forget any pending sync requests for this segment before we try
342-
* to unlink.
343-
*/
368+
sprintf(segpath, "%s.%u", path, segno);
369+
344370
if (!RelFileNodeBackendIsTemp(rnode))
371+
{
372+
/*
373+
* Prevent other backends' fds from holding on to the disk
374+
* space.
375+
*/
376+
if (do_truncate(segpath) < 0 && errno == ENOENT)
377+
break;
378+
379+
/*
380+
* Forget any pending sync requests for this segment before we
381+
* try to unlink.
382+
*/
345383
register_forget_request(rnode, forkNum, segno);
384+
}
346385

347-
sprintf(segpath, "%s.%u", path, segno);
348386
if (unlink(segpath) < 0)
349387
{
350388
/* ENOENT is expected after the last segment... */

0 commit comments

Comments
 (0)