Skip to content

Commit 37484ce

Browse files
committed
Merge pgstat_activity changes for CFS
1 parent 8ed3154 commit 37484ce

File tree

6 files changed

+52
-26
lines changed

6 files changed

+52
-26
lines changed

src/backend/storage/file/cfs.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
855855
char* file_path = (char*)palloc(suf+1);
856856
char* map_bck_path = (char*)palloc(suf+10);
857857
char* file_bck_path = (char*)palloc(suf+5);
858+
char* state;
858859
int rc;
859860

860861
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)
876877
{
877878
pg_atomic_fetch_sub_u32(&cfs_state->n_active_gc, 1);
878879

880+
pgstat_report_activity(STATE_DISABLED, "GC is disabled");
879881
rc = WaitLatch(MyLatch,
880882
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
881883
CFS_DISABLE_TIMEOUT /* ms */);
@@ -914,6 +916,10 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
914916
strcat(strcpy(map_bck_path, map_path), ".bck");
915917
strcat(strcpy(file_bck_path, file_path), ".bck");
916918

919+
state = psprintf("Check file %s", file_path);
920+
pgstat_report_activity(STATE_RUNNING, state);
921+
pfree(state);
922+
917923
/* mostly same as for cfs_lock_file */
918924
if (pg_atomic_read_u32(&map->gc_active)) /* Check if GC was not normally completed at previous Postgres run */
919925
{
@@ -958,6 +964,10 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
958964

959965
lock = cfs_get_lock(file_path);
960966

967+
state = psprintf("Process file %s", file_path);
968+
pgstat_report_activity(STATE_RUNNING, state);
969+
pfree(state);
970+
961971
fd2 = open(file_bck_path, O_CREAT|O_RDWR|PG_BINARY|O_TRUNC, 0600);
962972
if (fd2 < 0)
963973
{
@@ -1346,10 +1356,12 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
13461356

13471357
if (background == CFS_BACKGROUND)
13481358
{
1349-
int rc = WaitLatch(MyLatch,
1350-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
1351-
performed ? cfs_gc_delay : 0 /* ms */ );
1352-
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))
13531365
exit(1);
13541366

13551367
ResetLatch(MyLatch);
@@ -1440,12 +1452,19 @@ static void cfs_gc_bgworker_main(Datum arg)
14401452
{
14411453
MemoryContext MemCxt;
14421454
int worker_id = DatumGetInt32(arg);
1455+
char* appname;
14431456

14441457
pqsignal(SIGINT, cfs_gc_cancel);
14451458
pqsignal(SIGQUIT, cfs_gc_cancel);
14461459
pqsignal(SIGTERM, cfs_gc_cancel);
14471460
pqsignal(SIGHUP, cfs_sighup);
14481461

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+
14491468
/* We're now ready to receive signals */
14501469
BackgroundWorkerUnblockSignals();
14511470

@@ -1468,6 +1487,7 @@ static void cfs_gc_bgworker_main(Datum arg)
14681487
{
14691488
break;
14701489
}
1490+
pgstat_report_activity(STATE_IDLE, "Pause between GC iterations");
14711491
rc = WaitLatch(MyLatch,
14721492
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
14731493
timeout /* ms */ );
@@ -1888,3 +1908,8 @@ Datum cfs_gc_activity_scanned_files(PG_FUNCTION_ARGS)
18881908
{
18891909
PG_RETURN_INT64(cfs_state->gc_stat.scannedFiles);
18901910
}
1911+
1912+
void cfs_on_exit_callback(int code, Datum arg)
1913+
{
1914+
cfs_control_gc_unlock();
1915+
}

src/backend/storage/file/copydir.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,27 @@ copydir(char *fromdir, char *todir, bool recurse)
5757

5858
cfs_control_gc_lock(); /* disable GC during copy */
5959

60-
PG_ENSURE_ERROR_CLEANUP(cfs_control_gc_unlock, (Datum)NULL);
60+
PG_ENSURE_ERROR_CLEANUP(cfs_on_exit_callback, (Datum)NULL);
6161
{
6262
while ((xlde = ReadDir(xldir, fromdir)) != NULL)
6363
{
6464
struct stat fst;
65-
65+
6666
/* If we got a cancel signal during the copy of the directory, quit */
6767
CHECK_FOR_INTERRUPTS();
68-
68+
6969
if (strcmp(xlde->d_name, ".") == 0 ||
7070
strcmp(xlde->d_name, "..") == 0)
7171
continue;
72-
72+
7373
snprintf(fromfile, MAXPGPATH, "%s/%s", fromdir, xlde->d_name);
7474
snprintf(tofile, MAXPGPATH, "%s/%s", todir, xlde->d_name);
75-
75+
7676
if (lstat(fromfile, &fst) < 0)
7777
ereport(ERROR,
7878
(errcode_for_file_access(),
7979
errmsg("could not stat file \"%s\": %m", fromfile)));
80-
80+
8181
if (S_ISDIR(fst.st_mode))
8282
{
8383
/* recurse to handle subdirectories */
@@ -89,9 +89,9 @@ copydir(char *fromdir, char *todir, bool recurse)
8989
}
9090
FreeDir(xldir);
9191
}
92-
PG_END_ENSURE_ERROR_CLEANUP(cfs_control_gc_unlock, (Datum)NULL);
92+
PG_END_ENSURE_ERROR_CLEANUP(cfs_on_exit_callback, (Datum)NULL);
9393
cfs_control_gc_unlock();
94-
94+
9595
/*
9696
* Be paranoid here and fsync all files to ensure the copy is really done.
9797
* But if fsync is disabled, we're done.
@@ -163,35 +163,35 @@ copyzipdir(char *fromdir, bool from_compressed,
163163

164164
cfs_control_gc_lock(); /* disable GC during copy */
165165

166-
PG_ENSURE_ERROR_CLEANUP(cfs_control_gc_unlock, (Datum)NULL);
166+
PG_ENSURE_ERROR_CLEANUP(cfs_on_exit_callback, (Datum)NULL);
167167
{
168168
while ((xlde = ReadDir(xldir, fromdir)) != NULL)
169169
{
170170
struct stat fst;
171-
171+
172172
/* If we got a cancel signal during the copy of the directory, quit */
173173
CHECK_FOR_INTERRUPTS();
174-
174+
175175
if (strcmp(xlde->d_name, ".") == 0
176176
|| strcmp(xlde->d_name, "..") == 0
177177
|| (strlen(xlde->d_name) > 4
178178
&& strcmp(xlde->d_name + strlen(xlde->d_name) - 4, ".cfm") == 0))
179179
continue;
180-
180+
181181
snprintf(fromfile, MAXPGPATH, "%s/%s", fromdir, xlde->d_name);
182182
snprintf(tofile, MAXPGPATH, "%s/%s", todir, xlde->d_name);
183-
183+
184184
if (lstat(fromfile, &fst) < 0)
185185
ereport(ERROR,
186186
(errcode_for_file_access(),
187187
errmsg("could not stat file \"%s\": %m", fromfile)));
188-
188+
189189
if (S_ISREG(fst.st_mode))
190190
copy_zip_file(fromfile, from_compressed, tofile, to_compressed);
191191
}
192192
FreeDir(xldir);
193193
}
194-
PG_END_ENSURE_ERROR_CLEANUP(cfs_control_gc_unlock, (Datum)NULL);
194+
PG_END_ENSURE_ERROR_CLEANUP(cfs_on_exit_callback, (Datum)NULL);
195195
cfs_control_gc_unlock();
196196

197197
/*

src/backend/storage/smgr/md.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,11 @@ mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
480480
else
481481
{
482482
cfs_control_gc_lock();
483-
PG_ENSURE_ERROR_CLEANUP(cfs_control_gc_unlock, BoolGetDatum(false));
483+
PG_ENSURE_ERROR_CLEANUP(cfs_on_exit_callback, BoolGetDatum(false));
484484
{
485485
do_mdunlink(rnode, forkNum, isRedo);
486486
}
487-
PG_END_ENSURE_ERROR_CLEANUP(cfs_control_gc_unlock, BoolGetDatum(false));
487+
PG_END_ENSURE_ERROR_CLEANUP(cfs_on_exit_callback, BoolGetDatum(false));
488488
cfs_control_gc_unlock();
489489
}
490490
}

src/include/storage/cfs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,13 @@ void cfs_unlock_file(FileMap* map, char const* path);
130130
uint32 cfs_alloc_page(FileMap* map, uint32 oldSize, uint32 newSize);
131131
void cfs_extend(FileMap* map, uint32 pos);
132132
void cfs_control_gc_lock(void);
133-
void cfs_control_gc_unlock(); /* argument could be given by PG_ENSURE_ERROR_CLEANUP */
133+
void cfs_control_gc_unlock(void); /* argument could be given by PG_ENSURE_ERROR_CLEANUP */
134134
int cfs_msync(FileMap* map);
135135
FileMap* cfs_mmap(int md);
136136
int cfs_munmap(FileMap* map);
137137
void cfs_initialize(void);
138138
size_t cfs_shmem_size(void);
139+
void cfs_on_exit_callback(int code, Datum arg);
139140

140141
void cfs_encrypt(const char* fname, void* block, uint32 offs, uint32 size);
141142
void cfs_decrypt(const char* fname, void* block, uint32 offs, uint32 size);

src/test/isolation/specs/atx-deadlock-1.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ teardown
1111
}
1212

1313
session "s1"
14-
setup { begin; }
14+
setup { begin; SET deadlock_timeout = '10s'; }
1515
step "s1la" { lock table a in access exclusive mode; }
1616
step "s1atxb" { begin autonomous; }
1717
step "s1lb" { lock table b in access exclusive mode; }
1818
step "s1atxc" { commit; }
1919
step "s1c" { commit; }
2020

2121
session "s2"
22-
setup { begin; }
22+
setup { begin; SET deadlock_timeout = '10s'; }
2323
step "s2lb" { lock table b in access exclusive mode; }
2424
step "s2la" { lock table a in access exclusive mode; }
2525
step "s2c" { commit; }

src/test/isolation/specs/atx-deadlock-2.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ teardown
1010
}
1111

1212
session "s1"
13-
setup { begin; }
13+
setup { begin; SET deadlock_timeout = '10s'; }
1414
step "s1u1" { update t set b = 11 where a = 1; }
1515
step "s1atxb" { begin autonomous; }
1616
step "s1u2" { update t set b = 21 where a = 2; }
1717
step "s1atxc" { commit; }
1818
step "s1c" { commit; }
1919

2020
session "s2"
21-
setup { begin; }
21+
setup { begin; SET deadlock_timeout = '10s'; }
2222
step "s2u2" { update t set b = 22 where a = 2; }
2323
step "s2u1" { update t set b = 11 where a = 1; }
2424
step "s2c" { commit; }

0 commit comments

Comments
 (0)