Skip to content

Commit 0e86701

Browse files
committed
Fix cleanup of unlogged relations
1 parent a6be7fe commit 0e86701

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

src/backend/storage/file/reinit.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ parse_filename_for_nontemp_relation(const char *name, int *oidchars,
430430
{
431431
int segchar;
432432

433+
/* .cfm is used by compression */
434+
if (strcmp(name+pos+1, "cfm") == 0) {
435+
return true;
436+
}
433437
for (segchar = 1; isdigit((unsigned char) name[pos + segchar]); ++segchar)
434438
;
435439
if (segchar <= 1)

src/backend/storage/smgr/md.c

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ typedef struct
209209
bool compressed;
210210
} TablespaceStatus;
211211

212-
static bool md_use_compression(SMgrRelation reln, ForkNumber forknum)
212+
static bool md_use_compression(RelFileNodeBackend rnode, ForkNumber forknum)
213213
{
214214
static HTAB* tblspaceMap;
215215
char* compressionFilePath;
@@ -219,9 +219,9 @@ static bool md_use_compression(SMgrRelation reln, ForkNumber forknum)
219219

220220
/* Do not compress system (catalog) relations created during bootstrap */
221221
if (forknum != MAIN_FORKNUM
222-
|| reln->smgr_rnode.node.spcNode == DEFAULTTABLESPACE_OID
223-
|| reln->smgr_rnode.node.spcNode == GLOBALTABLESPACE_OID
224-
|| reln->smgr_rnode.node.relNode < FirstNormalObjectId)
222+
|| rnode.node.spcNode == DEFAULTTABLESPACE_OID
223+
|| rnode.node.spcNode == GLOBALTABLESPACE_OID
224+
|| rnode.node.relNode < FirstNormalObjectId)
225225
{
226226
return false;
227227
}
@@ -231,10 +231,10 @@ static bool md_use_compression(SMgrRelation reln, ForkNumber forknum)
231231
ctl.entrysize = sizeof(TablespaceStatus);
232232
tblspaceMap = hash_create("tablespace_map", 256, &ctl, HASH_ELEM);
233233
}
234-
ts = hash_search(tblspaceMap, &reln->smgr_rnode.node.spcNode, HASH_ENTER, &found);
234+
ts = hash_search(tblspaceMap, &rnode.node.spcNode, HASH_ENTER, &found);
235235
if (!found) {
236236
compressionFilePath = psprintf("pg_tblspc/%u/%s/pg_compression",
237-
reln->smgr_rnode.node.spcNode,
237+
rnode.node.spcNode,
238238
TABLESPACE_VERSION_DIRECTORY);
239239
compressionFile = fopen(compressionFilePath, "r");
240240
if (compressionFile != NULL) {
@@ -357,7 +357,7 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
357357
if (isRedo && reln->md_fd[forkNum] != NULL)
358358
return; /* created and opened already... */
359359

360-
if (md_use_compression(reln, forkNum))
360+
if (md_use_compression(reln->smgr_rnode, forkNum))
361361
{
362362
flags |= PG_COMPRESSION;
363363
}
@@ -473,9 +473,11 @@ static void
473473
mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
474474
{
475475
char *path;
476+
char *segpath;
476477
int ret;
477478

478479
path = relpath(rnode, forkNum);
480+
segpath = (char *) palloc(strlen(path) + 16);
479481

480482
/*
481483
* Delete or truncate the first segment.
@@ -484,32 +486,54 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
484486
{
485487
ret = unlink(path);
486488
if (ret < 0 && errno != ENOENT)
489+
{
487490
ereport(WARNING,
488491
(errcode_for_file_access(),
489492
errmsg("could not remove file \"%s\": %m", path)));
493+
}
494+
else if (forkNum == MAIN_FORKNUM)
495+
{
496+
sprintf(segpath, "%s.cfm", path);
497+
unlink(segpath);
498+
}
490499
}
491500
else
492501
{
493-
/* truncate(2) would be easier here, but Windows hasn't got it */
494-
int fd;
495-
496-
fd = OpenTransientFile(path, O_RDWR | PG_BINARY, 0);
497-
if (fd >= 0)
502+
if (md_use_compression(rnode, forkNum))
498503
{
499-
int save_errno;
500-
501-
ret = ftruncate(fd, 0);
502-
save_errno = errno;
503-
CloseTransientFile(fd);
504-
errno = save_errno;
504+
File file = PathNameOpenFile(path, O_RDWR | PG_BINARY | PG_COMPRESSION, 0);
505+
if (file >= 0) {
506+
elog(LOG, "Truncate file %s", path);
507+
if (FileTruncate(file, 0) < 0) {
508+
ereport(WARNING,
509+
(errcode_for_file_access(),
510+
errmsg("could not truncate file \"%s\": %m", path)));
511+
}
512+
}
513+
FileClose(file);
514+
}
515+
else
516+
{
517+
/* truncate(2) would be easier here, but Windows hasn't got it */
518+
int fd;
519+
520+
fd = OpenTransientFile(path, O_RDWR | PG_BINARY, 0);
521+
if (fd >= 0)
522+
{
523+
int save_errno;
524+
525+
ret = ftruncate(fd, 0);
526+
save_errno = errno;
527+
CloseTransientFile(fd);
528+
errno = save_errno;
529+
}
530+
else
531+
ret = -1;
532+
if (ret < 0 && errno != ENOENT)
533+
ereport(WARNING,
534+
(errcode_for_file_access(),
535+
errmsg("could not truncate file \"%s\": %m", path)));
505536
}
506-
else
507-
ret = -1;
508-
if (ret < 0 && errno != ENOENT)
509-
ereport(WARNING,
510-
(errcode_for_file_access(),
511-
errmsg("could not truncate file \"%s\": %m", path)));
512-
513537
/* Register request to unlink first segment later */
514538
register_unlink(rnode);
515539
}
@@ -519,7 +543,6 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
519543
*/
520544
if (ret >= 0)
521545
{
522-
char *segpath = (char *) palloc(strlen(path) + 16);
523546
BlockNumber segno;
524547

525548
/*
@@ -550,8 +573,8 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
550573
sprintf(segpath, "%s.cfm", path);
551574
unlink(segpath);
552575
}
553-
pfree(segpath);
554576
}
577+
pfree(segpath);
555578
pfree(path);
556579
}
557580

@@ -657,7 +680,7 @@ mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
657680

658681
path = relpath(reln->smgr_rnode, forknum);
659682

660-
if (md_use_compression(reln, forknum))
683+
if (md_use_compression(reln->smgr_rnode, forknum))
661684
{
662685
flags |= PG_COMPRESSION;
663686
}
@@ -1824,7 +1847,7 @@ _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
18241847
int fd;
18251848
char *fullpath;
18261849

1827-
if (md_use_compression(reln, forknum))
1850+
if (md_use_compression(reln->smgr_rnode, forknum))
18281851
{
18291852
oflags |= PG_COMPRESSION;
18301853
}

0 commit comments

Comments
 (0)