@@ -855,6 +855,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
855
855
char * file_path = (char * )palloc (suf + 1 );
856
856
char * map_bck_path = (char * )palloc (suf + 10 );
857
857
char * file_bck_path = (char * )palloc (suf + 5 );
858
+ char * state ;
858
859
int rc ;
859
860
860
861
pg_atomic_fetch_add_u32 (& cfs_state -> n_active_gc , 1 );
@@ -876,6 +877,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
876
877
{
877
878
pg_atomic_fetch_sub_u32 (& cfs_state -> n_active_gc , 1 );
878
879
880
+ pgstat_report_activity (STATE_DISABLED , "GC is disabled" );
879
881
rc = WaitLatch (MyLatch ,
880
882
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH ,
881
883
CFS_DISABLE_TIMEOUT /* ms */ );
@@ -914,6 +916,10 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
914
916
strcat (strcpy (map_bck_path , map_path ), ".bck" );
915
917
strcat (strcpy (file_bck_path , file_path ), ".bck" );
916
918
919
+ state = psprintf ("Check file %s" , file_path );
920
+ pgstat_report_activity (STATE_RUNNING , state );
921
+ pfree (state );
922
+
917
923
/* mostly same as for cfs_lock_file */
918
924
if (pg_atomic_read_u32 (& map -> gc_active )) /* Check if GC was not normally completed at previous Postgres run */
919
925
{
@@ -943,6 +949,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
943
949
uint32 second_pass_bytes = 0 ;
944
950
inode_t * * inodes = (inode_t * * )palloc (RELSEG_SIZE * sizeof (inode_t * ));
945
951
bool remove_backups = true;
952
+ bool got_lock = false;
946
953
int second_pass_whole = 0 ;
947
954
int n_pages , n_pages1 ;
948
955
TimestampTz startTime , secondTime , endTime ;
@@ -957,6 +964,10 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
957
964
958
965
lock = cfs_get_lock (file_path );
959
966
967
+ state = psprintf ("Process file %s" , file_path );
968
+ pgstat_report_activity (STATE_RUNNING , state );
969
+ pfree (state );
970
+
960
971
fd2 = open (file_bck_path , O_CREAT |O_RDWR |PG_BINARY |O_TRUNC , 0600 );
961
972
if (fd2 < 0 )
962
973
{
@@ -998,6 +1009,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
998
1009
retry :
999
1010
/* temporary lock file for fetching map snapshot */
1000
1011
cfs_gc_lock (lock );
1012
+ got_lock = true;
1001
1013
1002
1014
/* Reread variables after locking file */
1003
1015
physSize = pg_atomic_read_u32 (& map -> hdr .physSize );
@@ -1013,6 +1025,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
1013
1025
}
1014
1026
/* may unlock until second phase */
1015
1027
cfs_gc_unlock (lock );
1028
+ got_lock = false;
1016
1029
1017
1030
if (!cfs_copy_inodes (inodes , n_pages , fd , fd2 , & writeback , & newSize ,
1018
1031
file_path , file_bck_path ))
@@ -1034,6 +1047,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
1034
1047
secondTime = GetCurrentTimestamp ();
1035
1048
1036
1049
cfs_gc_lock (lock );
1050
+ got_lock = true;
1037
1051
1038
1052
/* Reread variables after locking file */
1039
1053
n_pages1 = n_pages ;
@@ -1087,6 +1101,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
1087
1101
if (second_pass_whole == 1 && physSize < CFS_RETRY_GC_THRESHOLD )
1088
1102
{
1089
1103
cfs_gc_unlock (lock );
1104
+ got_lock = false;
1090
1105
/* sleep, cause there is possibly checkpoint is on a way */
1091
1106
pg_usleep (CFS_LOCK_MAX_TIMEOUT );
1092
1107
second_pass = 0 ;
@@ -1285,7 +1300,8 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
1285
1300
else
1286
1301
remove_backups = true; /* we don't need backups anymore */
1287
1302
1288
- cfs_gc_unlock (lock );
1303
+ if (got_lock )
1304
+ cfs_gc_unlock (lock );
1289
1305
1290
1306
/* remove map backup file */
1291
1307
if (remove_backups && unlink (map_bck_path ))
@@ -1340,10 +1356,12 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
1340
1356
1341
1357
if (background == CFS_BACKGROUND )
1342
1358
{
1343
- int rc = WaitLatch (MyLatch ,
1344
- WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH ,
1345
- performed ? cfs_gc_delay : 0 /* ms */ );
1346
- if (rc & WL_POSTMASTER_DEATH )
1359
+ int rc ;
1360
+ pgstat_report_activity (STATE_IDLE , "Processing pause" );
1361
+ rc = WaitLatch (MyLatch ,
1362
+ WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH ,
1363
+ performed ? cfs_gc_delay : 0 /* ms */ );
1364
+ if (cfs_gc_stop || (rc & WL_POSTMASTER_DEATH ))
1347
1365
exit (1 );
1348
1366
1349
1367
ResetLatch (MyLatch );
@@ -1408,6 +1426,7 @@ static bool cfs_gc_directory(int worker_id, char const* path, int depth)
1408
1426
static void cfs_gc_cancel (int sig )
1409
1427
{
1410
1428
cfs_gc_stop = true;
1429
+ SetLatch (MyLatch );
1411
1430
}
1412
1431
1413
1432
static void cfs_sighup (SIGNAL_ARGS )
@@ -1433,12 +1452,19 @@ static void cfs_gc_bgworker_main(Datum arg)
1433
1452
{
1434
1453
MemoryContext MemCxt ;
1435
1454
int worker_id = DatumGetInt32 (arg );
1455
+ char * appname ;
1436
1456
1437
1457
pqsignal (SIGINT , cfs_gc_cancel );
1438
1458
pqsignal (SIGQUIT , cfs_gc_cancel );
1439
1459
pqsignal (SIGTERM , cfs_gc_cancel );
1440
1460
pqsignal (SIGHUP , cfs_sighup );
1441
1461
1462
+ InitPostgres (NULL , InvalidOid , NULL , InvalidOid , NULL );
1463
+
1464
+ appname = psprintf ("CFS GC worker %d" , worker_id );
1465
+ pgstat_report_appname (appname );
1466
+ pfree (appname );
1467
+
1442
1468
/* We're now ready to receive signals */
1443
1469
BackgroundWorkerUnblockSignals ();
1444
1470
@@ -1461,10 +1487,11 @@ static void cfs_gc_bgworker_main(Datum arg)
1461
1487
{
1462
1488
break ;
1463
1489
}
1490
+ pgstat_report_activity (STATE_IDLE , "Pause between GC iterations" );
1464
1491
rc = WaitLatch (MyLatch ,
1465
1492
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH ,
1466
1493
timeout /* ms */ );
1467
- if (rc & WL_POSTMASTER_DEATH )
1494
+ if (( rc & WL_POSTMASTER_DEATH ) || cfs_gc_stop )
1468
1495
exit (1 );
1469
1496
1470
1497
ResetLatch (MyLatch );
0 commit comments