Skip to content

Commit 3422764

Browse files
committed
Support FileWriteback for compressed files
1 parent 84270b7 commit 3422764

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

src/backend/storage/file/cfs.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
676676
int fd2 = -1;
677677
int md2 = -1;
678678
bool succeed = false;
679+
bool performed = false;
679680
int rc;
680681

681682
pg_atomic_fetch_add_u32(&cfs_state->n_active_gc, 1);
@@ -1055,17 +1056,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
10551056
pfree(inodes);
10561057
pfree(newMap);
10571058

1058-
if (cfs_gc_delay != 0 && background == CFS_BACKGROUND)
1059-
{
1060-
int rc = WaitLatch(MyLatch,
1061-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
1062-
cfs_gc_delay /* ms */ );
1063-
if (rc & WL_POSTMASTER_DEATH)
1064-
exit(1);
1065-
1066-
ResetLatch(MyLatch);
1067-
CHECK_FOR_INTERRUPTS();
1068-
}
1059+
performed = true;
10691060
}
10701061
else if (cfs_state->max_iterations == 1)
10711062
elog(LOG, "CFS GC worker %d: file %.*s: physical size %u, logical size %u, used %u, compression ratio %f",
@@ -1089,6 +1080,17 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
10891080
}
10901081
pg_atomic_fetch_sub_u32(&cfs_state->n_active_gc, 1);
10911082

1083+
if (cfs_gc_delay != 0 && performed && background == CFS_BACKGROUND)
1084+
{
1085+
int rc = WaitLatch(MyLatch,
1086+
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
1087+
cfs_gc_delay /* ms */ );
1088+
if (rc & WL_POSTMASTER_DEATH)
1089+
exit(1);
1090+
1091+
ResetLatch(MyLatch);
1092+
CHECK_FOR_INTERRUPTS();
1093+
}
10921094
return succeed;
10931095
}
10941096

src/backend/storage/file/fd.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,8 @@ FilePrefetch(File file, off_t offset, int amount)
16891689
#endif
16901690
}
16911691

1692+
static bool FileLock(File file);
1693+
16921694
void
16931695
FileWriteback(File file, off_t offset, off_t nbytes)
16941696
{
@@ -1711,7 +1713,55 @@ FileWriteback(File file, off_t offset, off_t nbytes)
17111713
if (returnCode < 0)
17121714
return;
17131715

1716+
if (VfdCache[file].fileFlags & PG_COMPRESSION)
1717+
{
1718+
FileMap *map = VfdCache[file].map;
1719+
inode_t inode;
1720+
uint32 i = (uint32)(offset / BLCKSZ);
1721+
uint32 end = (uint32)((offset + nbytes + (BLCKSZ-1)) / BLCKSZ);
1722+
uint32 max = 0;
1723+
uint32 min = UINT32_MAX;
1724+
uint32 virtSize;
1725+
uint32 physSize;
1726+
1727+
/* if GC is in progress, no need to flush this file */
1728+
if (!FileLock(file))
1729+
return;
1730+
virtSize = pg_atomic_read_u32(&map->hdr.virtSize);
1731+
/* in fact, we should not be here. Should it be Assert? */
1732+
if (virtSize / BLCKSZ < end)
1733+
end = virtSize / BLCKSZ;
1734+
for (; i <= end; i++)
1735+
{
1736+
uint32_t offs, size;
1737+
inode = map->inodes[i];
1738+
offs = CFS_INODE_SIZE(inode);
1739+
size = CFS_INODE_OFFS(inode);
1740+
if (offs < min)
1741+
min = offs;
1742+
if (offs + size > max)
1743+
max = offs + size;
1744+
}
1745+
physSize = pg_atomic_read_u32(&map->hdr.physSize);
1746+
if (min > physSize)
1747+
min = physSize;
1748+
if (max > physSize)
1749+
max = physSize;
1750+
if (max <= min)
1751+
{
1752+
cfs_unlock_file(map);
1753+
return;
1754+
}
1755+
offset = min;
1756+
nbytes = max - min;
1757+
/* if off_t == int32, we will fail in many other places,
1758+
* so don't check off_t overflow here */
1759+
}
17141760
pg_flush_data(VfdCache[file].fd, offset, nbytes);
1761+
if (VfdCache[file].fileFlags & PG_COMPRESSION)
1762+
{
1763+
cfs_unlock_file(map);
1764+
}
17151765
}
17161766

17171767
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy