Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f9b50b7

Browse files
committed
Fix removal of files in pgstats directories
Instead of deleting all files in stats_temp_directory and the permanent directory on a crash, only remove those files that match the pattern of files we actually write in them, to avoid possibly clobbering existing unrelated contents of the temporary directory. Per complaint from Jeff Janes, and subsequent discussion, starting at message CAMkU=1z9+7RsDODnT4=cDFBRBp8wYQbd_qsLcMtKEf-oFwuOdQ@mail.gmail.com Also, fix a bug in the same routine to avoid removing files from the permanent directory twice (instead of once from that directory and then from the temporary directory), also per report from Jeff Janes, in message CAMkU=1wbk947=-pAosDMX5VC+sQw9W4ttq6RM9rXu=MjNeEQKA@mail.gmail.com
1 parent 3619a20 commit f9b50b7

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,37 @@ pgstat_reset_remove_files(const char *directory)
563563
struct dirent *entry;
564564
char fname[MAXPGPATH];
565565

566-
dir = AllocateDir(pgstat_stat_directory);
567-
while ((entry = ReadDir(dir, pgstat_stat_directory)) != NULL)
566+
dir = AllocateDir(directory);
567+
while ((entry = ReadDir(dir, directory)) != NULL)
568568
{
569-
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
569+
int nitems;
570+
Oid tmp_oid;
571+
char tmp_type[8];
572+
char tmp_rest[2];
573+
574+
if (strncmp(entry->d_name, ".", 2) == 0 ||
575+
strncmp(entry->d_name, "..", 3) == 0)
570576
continue;
571577

572-
/* XXX should we try to ignore files other than the ones we write? */
578+
/*
579+
* Skip directory entries that don't match the file names we write.
580+
* See get_dbstat_filename for the database-specific pattern.
581+
*/
582+
nitems = sscanf(entry->d_name, "db_%u.%5s%1s",
583+
&tmp_oid, tmp_type, tmp_rest);
584+
if (nitems != 2)
585+
{
586+
nitems = sscanf(entry->d_name, "global.%5s%1s",
587+
tmp_type, tmp_rest);
588+
if (nitems != 1)
589+
continue;
590+
}
591+
592+
if (strncmp(tmp_type, "tmp", 4) != 0 &&
593+
strncmp(tmp_type, "stat", 5) != 0)
594+
continue;
573595

574-
snprintf(fname, MAXPGPATH, "%s/%s", pgstat_stat_directory,
596+
snprintf(fname, MAXPGPATH, "%s/%s", directory,
575597
entry->d_name);
576598
unlink(fname);
577599
}
@@ -3631,6 +3653,7 @@ get_dbstat_filename(bool permanent, bool tempname, Oid databaseid,
36313653
{
36323654
int printed;
36333655

3656+
/* NB -- pgstat_reset_remove_files knows about the pattern this uses */
36343657
printed = snprintf(filename, len, "%s/db_%u.%s",
36353658
permanent ? PGSTAT_STAT_PERMANENT_DIRECTORY :
36363659
pgstat_stat_directory,

0 commit comments

Comments
 (0)