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

Commit fd3a75d

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 a095e04 commit fd3a75d

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
@@ -286,6 +286,41 @@ mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
286286
mdunlinkfork(rnode, forkNum, isRedo);
287287
}
288288

289+
/*
290+
* Truncate a file to release disk space.
291+
*/
292+
static int
293+
do_truncate(const char *path)
294+
{
295+
int save_errno;
296+
int ret;
297+
int fd;
298+
299+
/* truncate(2) would be easier here, but Windows hasn't got it */
300+
fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
301+
if (fd >= 0)
302+
{
303+
ret = ftruncate(fd, 0);
304+
save_errno = errno;
305+
CloseTransientFile(fd);
306+
errno = save_errno;
307+
}
308+
else
309+
ret = -1;
310+
311+
/* Log a warning here to avoid repetition in callers. */
312+
if (ret < 0 && errno != ENOENT)
313+
{
314+
save_errno = errno;
315+
ereport(WARNING,
316+
(errcode_for_file_access(),
317+
errmsg("could not truncate file \"%s\": %m", path)));
318+
errno = save_errno;
319+
}
320+
321+
return ret;
322+
}
323+
289324
static void
290325
mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
291326
{
@@ -299,38 +334,31 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
299334
*/
300335
if (isRedo || forkNum != MAIN_FORKNUM || RelFileNodeBackendIsTemp(rnode))
301336
{
302-
/* First, forget any pending sync requests for the first segment */
303337
if (!RelFileNodeBackendIsTemp(rnode))
338+
{
339+
/* Prevent other backends' fds from holding on to the disk space */
340+
ret = do_truncate(path);
341+
342+
/* Forget any pending sync requests for the first segment */
304343
register_forget_request(rnode, forkNum, 0 /* first seg */ );
344+
}
345+
else
346+
ret = 0;
305347

306-
/* Next unlink the file */
307-
ret = unlink(path);
308-
if (ret < 0 && errno != ENOENT)
309-
ereport(WARNING,
310-
(errcode_for_file_access(),
311-
errmsg("could not remove file \"%s\": %m", path)));
348+
/* Next unlink the file, unless it was already found to be missing */
349+
if (ret == 0 || errno != ENOENT)
350+
{
351+
ret = unlink(path);
352+
if (ret < 0 && errno != ENOENT)
353+
ereport(WARNING,
354+
(errcode_for_file_access(),
355+
errmsg("could not remove file \"%s\": %m", path)));
356+
}
312357
}
313358
else
314359
{
315-
/* truncate(2) would be easier here, but Windows hasn't got it */
316-
int fd;
317-
318-
fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
319-
if (fd >= 0)
320-
{
321-
int save_errno;
322-
323-
ret = ftruncate(fd, 0);
324-
save_errno = errno;
325-
CloseTransientFile(fd);
326-
errno = save_errno;
327-
}
328-
else
329-
ret = -1;
330-
if (ret < 0 && errno != ENOENT)
331-
ereport(WARNING,
332-
(errcode_for_file_access(),
333-
errmsg("could not truncate file \"%s\": %m", path)));
360+
/* Prevent other backends' fds from holding on to the disk space */
361+
ret = do_truncate(path);
334362

335363
/* Register request to unlink first segment later */
336364
register_unlink_segment(rnode, forkNum, 0 /* first seg */ );
@@ -350,14 +378,24 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
350378
*/
351379
for (segno = 1;; segno++)
352380
{
353-
/*
354-
* Forget any pending sync requests for this segment before we try
355-
* to unlink.
356-
*/
381+
sprintf(segpath, "%s.%u", path, segno);
382+
357383
if (!RelFileNodeBackendIsTemp(rnode))
384+
{
385+
/*
386+
* Prevent other backends' fds from holding on to the disk
387+
* space.
388+
*/
389+
if (do_truncate(segpath) < 0 && errno == ENOENT)
390+
break;
391+
392+
/*
393+
* Forget any pending sync requests for this segment before we
394+
* try to unlink.
395+
*/
358396
register_forget_request(rnode, forkNum, segno);
397+
}
359398

360-
sprintf(segpath, "%s.%u", path, segno);
361399
if (unlink(segpath) < 0)
362400
{
363401
/* ENOENT is expected after the last segment... */

0 commit comments

Comments
 (0)