Skip to content

Commit f5f45c9

Browse files
committed
Fix disabling GC
1 parent 9c321d6 commit f5f45c9

File tree

4 files changed

+53
-51
lines changed

4 files changed

+53
-51
lines changed

src/backend/storage/file/cfs.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,8 @@ void cfs_initialize()
404404
pg_atomic_init_flag(&cfs_state->gc_started);
405405
pg_atomic_init_u32(&cfs_state->n_active_gc, 0);
406406
cfs_state->n_workers = 0;
407-
cfs_state->gc_enabled = cfs_gc_enabled;
407+
cfs_state->background_gc_enabled = cfs_gc_enabled;
408+
cfs_state->gc_enabled = true;
408409
cfs_state->max_iterations = 0;
409410

410411
if (cfs_encryption)
@@ -662,24 +663,23 @@ static bool cfs_gc_file(char* map_path, bool background)
662663

663664
pg_atomic_fetch_add_u32(&cfs_state->n_active_gc, 1);
664665

665-
if (background)
666+
while (!(background ? (cfs_state->gc_enabled & cfs_state->background_gc_enabled) : cfs_state->gc_enabled))
666667
{
667-
while (!cfs_state->gc_enabled)
668-
{
669-
pg_atomic_fetch_sub_u32(&cfs_state->n_active_gc, 1);
670-
671-
rc = WaitLatch(MyLatch,
672-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
673-
CFS_DISABLE_TIMEOUT /* ms */);
674-
if (cfs_gc_stop || (rc & WL_POSTMASTER_DEATH))
675-
exit(1);
668+
pg_atomic_fetch_sub_u32(&cfs_state->n_active_gc, 1);
676669

677-
CHECK_FOR_INTERRUPTS();
678-
ResetLatch(MyLatch);
670+
rc = WaitLatch(MyLatch,
671+
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
672+
CFS_DISABLE_TIMEOUT /* ms */);
673+
if (cfs_gc_stop || (rc & WL_POSTMASTER_DEATH))
674+
exit(1);
679675

680-
pg_atomic_fetch_add_u32(&cfs_state->n_active_gc, 1);
681-
}
676+
ResetLatch(MyLatch);
677+
CHECK_FOR_INTERRUPTS();
682678

679+
pg_atomic_fetch_add_u32(&cfs_state->n_active_gc, 1);
680+
}
681+
if (background)
682+
{
683683
LWLockAcquire(CfsGcLock, LW_SHARED); /* avoid race condition with cfs_file_lock */
684684
}
685685

@@ -1022,8 +1022,8 @@ static bool cfs_gc_file(char* map_path, bool background)
10221022
if (rc & WL_POSTMASTER_DEATH)
10231023
exit(1);
10241024

1025-
CHECK_FOR_INTERRUPTS();
10261025
ResetLatch(MyLatch);
1026+
CHECK_FOR_INTERRUPTS();
10271027
}
10281028
}
10291029
else if (cfs_state->max_iterations == 1)
@@ -1145,8 +1145,8 @@ static void cfs_gc_bgworker_main(Datum arg)
11451145
if (rc & WL_POSTMASTER_DEATH)
11461146
exit(1);
11471147

1148-
CHECK_FOR_INTERRUPTS();
11491148
ResetLatch(MyLatch);
1149+
CHECK_FOR_INTERRUPTS();
11501150
}
11511151
}
11521152

@@ -1190,8 +1190,8 @@ bool cfs_control_gc(bool enabled)
11901190
if (rc & WL_POSTMASTER_DEATH)
11911191
exit(1);
11921192

1193-
CHECK_FOR_INTERRUPTS();
11941193
ResetLatch(MyLatch);
1194+
CHECK_FOR_INTERRUPTS();
11951195
}
11961196
}
11971197
return was_enabled;

src/backend/storage/file/fd.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ LruDelete(File file)
985985
{
986986
if (cfs_munmap(vfdP->map))
987987
elog(ERROR, "could not unmap file \"%s.cfm\": %m", vfdP->fileName);
988-
988+
989989
if (close(vfdP->md))
990990
elog(ERROR, "could not close map file \"%s.cfm\": %m", vfdP->fileName);
991991

@@ -1068,10 +1068,10 @@ LruInsert(File file)
10681068
* overall system file table being full. So, be prepared to release
10691069
* another FD if necessary...
10701070
*/
1071-
if (vfdP->fileFlags & PG_COMPRESSION)
1071+
if (vfdP->fileFlags & PG_COMPRESSION)
10721072
{
10731073
char* mapFileName = psprintf("%s.cfm", vfdP->fileName);
1074-
vfdP->md = open(mapFileName, vfdP->fileFlags & ~PG_COMPRESSION, vfdP->fileMode);
1074+
vfdP->md = BasicOpenFile(mapFileName, vfdP->fileFlags & ~PG_COMPRESSION, vfdP->fileMode);
10751075
pfree(mapFileName);
10761076
if (vfdP->md < 0)
10771077
{
@@ -1598,7 +1598,7 @@ FileClose(File file)
15981598
if (unlink(vfdP->fileName))
15991599
elog(LOG, "could not unlink file \"%s\": %m", vfdP->fileName);
16001600

1601-
if (vfdP->fileFlags & PG_COMPRESSION) {
1601+
if (vfdP->fileFlags & PG_COMPRESSION) {
16021602
char* mapFileName = psprintf("%s.cfm", vfdP->fileName);
16031603
if (unlink(mapFileName))
16041604
elog(LOG, "could not unlink file \"%s\": %m", mapFileName);
@@ -1716,7 +1716,7 @@ FileLock(File file)
17161716

17171717
map_generation = vfdP->map->generation;
17181718
pg_read_barrier();
1719-
1719+
17201720
/* Reopen file, because it was rewritten by gc */
17211721
if (vfdP->generation != map_generation)
17221722
{
@@ -1759,7 +1759,7 @@ FileRead(File file, char *buffer, int amount)
17591759
if (VfdCache[file].seekPos / BLCKSZ >= RELSEG_SIZE)
17601760
return 0;
17611761

1762-
if (!FileLock(file))
1762+
if (!FileLock(file))
17631763
return -1;
17641764

17651765
inode = map->inodes[VfdCache[file].seekPos / BLCKSZ];
@@ -1777,7 +1777,7 @@ FileRead(File file, char *buffer, int amount)
17771777
return amount;
17781778
}
17791779

1780-
seekPos = lseek(VfdCache[file].fd, CFS_INODE_OFFS(inode), SEEK_SET);
1780+
seekPos = lseek(VfdCache[file].fd, CFS_INODE_OFFS(inode), SEEK_SET);
17811781
Assert(seekPos == (off_t)CFS_INODE_OFFS(inode));
17821782

17831783
if (amount < BLCKSZ)
@@ -1798,10 +1798,10 @@ FileRead(File file, char *buffer, int amount)
17981798
if (errno != EINTR)
17991799
{
18001800
if (returnCode == 0)
1801-
elog(LOG, "Block %u position %u size %u is beyond end of compressed file %s",
1801+
elog(LOG, "Block %u position %u size %u is beyond end of compressed file %s",
18021802
(uint32)(VfdCache[file].seekPos / BLCKSZ), (uint32)seekPos, size, VfdCache[file].fileName);
18031803
else
1804-
elog(LOG, "Failed to read block %u position %u size %u from compressed file %s: %m",
1804+
elog(LOG, "Failed to read block %u position %u size %u from compressed file %s: %m",
18051805
(uint32)(VfdCache[file].seekPos / BLCKSZ), (uint32)seekPos, size, VfdCache[file].fileName);
18061806
cfs_unlock_file(map);
18071807
return returnCode;
@@ -1818,7 +1818,7 @@ FileRead(File file, char *buffer, int amount)
18181818
INIT_TRADITIONAL_CRC32(crc);
18191819
COMP_TRADITIONAL_CRC32(crc, compressedBuffer, amount);
18201820
FIN_TRADITIONAL_CRC32(crc);
1821-
elog(LOG, "CFS: decompress error: %d for file %s block %u position %u compressed size %u crc %x",
1821+
elog(LOG, "CFS: decompress error: %d for file %s block %u position %u compressed size %u crc %x",
18221822
returnCode, VfdCache[file].fileName, (uint32)(VfdCache[file].seekPos / BLCKSZ), (uint32)seekPos, amount, crc);
18231823
VfdCache[file].seekPos = FileUnknownPos;
18241824
returnCode = -1;
@@ -1835,7 +1835,7 @@ FileRead(File file, char *buffer, int amount)
18351835
returnCode = read(VfdCache[file].fd, buffer, amount);
18361836
if (returnCode >= 0)
18371837
{
1838-
if (VfdCache[file].fileFlags & PG_COMPRESSION)
1838+
if (VfdCache[file].fileFlags & PG_COMPRESSION)
18391839
{
18401840
cfs_decrypt(VfdCache[file].fileName, buffer, VfdCache[file].seekPos, amount);
18411841
}
@@ -1874,7 +1874,7 @@ FileRead(File file, char *buffer, int amount)
18741874
VfdCache[file].seekPos = FileUnknownPos;
18751875
}
18761876

1877-
if (VfdCache[file].fileFlags & PG_COMPRESSION)
1877+
if (VfdCache[file].fileFlags & PG_COMPRESSION)
18781878
{
18791879
cfs_unlock_file(VfdCache[file].map);
18801880
}
@@ -1884,7 +1884,7 @@ FileRead(File file, char *buffer, int amount)
18841884
int
18851885
FileWrite(File file, char *buffer, int amount)
18861886
{
1887-
int returnCode;
1887+
int returnCode;
18881888
char compressedBuffer[CFS_MAX_COMPRESSED_SIZE(BLCKSZ)];
18891889
inode_t inode = 0;
18901890
/*inode_t prev_inode;*/
@@ -1943,7 +1943,7 @@ FileWrite(File file, char *buffer, int amount)
19431943
}
19441944
}
19451945

1946-
if (VfdCache[file].fileFlags & PG_COMPRESSION)
1946+
if (VfdCache[file].fileFlags & PG_COMPRESSION)
19471947
{
19481948
FileMap* map = VfdCache[file].map;
19491949
uint32 compressedSize;
@@ -2019,7 +2019,7 @@ FileWrite(File file, char *buffer, int amount)
20192019
if (VfdCache[file].fileFlags & PG_COMPRESSION)
20202020
{
20212021
if (returnCode == amount)
2022-
{
2022+
{
20232023
VfdCache[file].map->inodes[VfdCache[file].seekPos / BLCKSZ] = inode;
20242024
VfdCache[file].seekPos += BLCKSZ;
20252025
cfs_extend(VfdCache[file].map, VfdCache[file].seekPos);
@@ -2083,17 +2083,17 @@ FileWrite(File file, char *buffer, int amount)
20832083
vfdP->seekPos = FileUnknownPos;
20842084
}
20852085

2086-
if (VfdCache[file].fileFlags & PG_COMPRESSION)
2086+
if (VfdCache[file].fileFlags & PG_COMPRESSION)
20872087
{
20882088
cfs_unlock_file(VfdCache[file].map);
2089-
/*
2090-
* If GC is disabled for a long time, then faile can unlimited grow.
2089+
/*
2090+
* If GC is disabled for a long time, then file can unlimited grow.
20912091
* To avoid wrap aound of 32-bit offsets we force GC on this file when destination position
2092-
* cross 2Gb boundary.
2092+
* cross 2Gb boundary.
20932093
*/
2094-
if ((int32)pos >= 0 && (int32)(pos + amount) < 0)
2095-
{
2096-
elog(LOG, "CFS: backend %d forced to performe GC on file %s block %u because it's size exceed %u bytes",
2094+
if ((int32)pos >= 0 && (int32)(pos + amount) < 0)
2095+
{
2096+
elog(LOG, "CFS: backend %d forced to perform GC on file %s block %u because it's size exceed %u bytes",
20972097
MyProcPid, VfdCache[file].fileName, (uint32)(VfdCache[file].seekPos / BLCKSZ), pos);
20982098
cfs_gc_segment(VfdCache[file].fileName);
20992099
}
@@ -2261,8 +2261,8 @@ FileTruncate(File file, off_t offset)
22612261

22622262
pg_atomic_write_u32(&map->virtSize, offset);
22632263
pg_atomic_fetch_sub_u32(&map->usedSize, released);
2264-
2265-
if (offset == 0)
2264+
2265+
if (offset == 0)
22662266
{
22672267
/* We can truncate compressed file only with zero offset */
22682268
pg_atomic_write_u32(&map->physSize, 0);

src/backend/utils/misc/guc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10858,15 +10858,15 @@ show_log_file_mode(void)
1085810858
static void set_cfs_gc_enabled(bool newval, void* extra)
1085910859
{
1086010860
cfs_gc_enabled = newval;
10861-
if (cfs_state && MyProcPid == PostmasterPid)
10861+
if (cfs_state && MyProcPid == PostmasterPid)
1086210862
{
10863-
cfs_state->gc_enabled = newval;
10863+
cfs_state->background_gc_enabled = newval;
1086410864
}
1086510865
}
1086610866

1086710867
static char const* show_cfs_gc_enabled(void)
1086810868
{
10869-
return (cfs_state ? cfs_state->gc_enabled : cfs_gc_enabled) ? "on" : "off";
10869+
return (cfs_state ? cfs_state->background_gc_enabled : cfs_gc_enabled) ? "on" : "off";
1087010870
}
1087110871

1087210872

src/include/storage/cfs.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define CFS_MAX_COMPRESSED_SIZE(size) ((size)*2)
2323

2424
/* Minimal compression ratio when compression is expected to be reasonable.
25-
* Right now it is hardcoded and equal to 2/3 of original size. If compressed image is larger than 2/3 of original image,
25+
* Right now it is hardcoded and equal to 2/3 of original size. If compressed image is larger than 2/3 of original image,
2626
* then uncompressed version of the page is stored.
2727
*/
2828
#define CFS_MIN_COMPRESSED_SIZE(size) ((size)*2/3)
@@ -39,7 +39,7 @@
3939
* Set CFS_COMPRESSOR to one of the names above
4040
* to compile postgres with chosen compression algorithm
4141
*/
42-
#ifndef CFS_COMPRESSOR
42+
#ifndef CFS_COMPRESSOR
4343
#define CFS_COMPRESSOR ZLIB_COMPRESSOR
4444
#endif
4545

@@ -79,18 +79,20 @@ typedef struct
7979
/* Max number of garbage collection background workers.
8080
* Duplicates 'cfs_gc_workers' global variable. */
8181
int n_workers;
82-
/* Maximal number of iterations with GC should perform. Automatically started GC performs infinite number of iterations.
82+
/* Maximal number of iterations with GC should perform. Automatically started GC performs infinite number of iterations.
8383
* Manually started GC performs just one iteration. */
8484
int max_iterations;
85-
/* Flag for temporary didsabling GC */
85+
/* Flag for temporary disabling GC */
8686
bool gc_enabled;
87+
/* Flag for controlling background GC */
88+
bool background_gc_enabled;
8789
/* CFS GC statatistic */
8890
CfsStatistic gc_stat;
8991
rijndael_ctx aes_context;
9092
} CfsState;
9193

9294

93-
/* Map file format (mmap in memory and shared by all backends) */
95+
/* Map file format (mmap in memory and shared by all backends) */
9496
typedef struct
9597
{
9698
/* Physical size of the file (size of the file on disk) */
@@ -101,7 +103,7 @@ typedef struct
101103
pg_atomic_uint32 usedSize;
102104
/* Lock used to synchronize access to the file */
103105
pg_atomic_uint32 lock;
104-
/* PID (process identifier) of postmaster. We check it at open time to revoke lock in case when postgres is restarted.
106+
/* PID (process identifier) of postmaster. We check it at open time to revoke lock in case when postgres is restarted.
105107
* TODO: not so reliable because it can happen that occasionally postmaster will be given the same PID */
106108
pid_t postmasterPid;
107109
/* Each pass of GC updates generation of the map */

0 commit comments

Comments
 (0)