8000 Fix removal of files in pgstats directories · sqlparser/postgres@db5b49c · GitHub
[go: up one dir, main page]

Skip to content

Commit db5b49c

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 38c6923 commit db5b49c

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
@@ -562,15 +562,37 @@ pgstat_reset_remove_files(const char *directory)
562562
struct dirent *entry;
563563
char fname[MAXPGPATH];
564564

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

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

573-
snprintf(fname, MAXPGPATH, "%s/%s", pgstat_stat_directory,
595+
snprintf(fname, MAXPGPATH, "%s/%s", directory,
574596
entry->d_name);
575597
unlink(fname);
576598
}
@@ -3627,6 +3649,7 @@ get_dbstat_filename(bool permanent, bool tempname, Oid databaseid,
36273649
{
36283650
int printed;
36293651

3652+
/* NB -- pgstat_reset_remove_files knows about the pattern this uses */
36303653
printed = snprintf(filename, len, "%s/db_%u.%s",
36313654
permanent ? PGSTAT_STAT_PERMANENT_DIRECTORY :
36323655
pgstat_stat_directory,

0 commit comments

Comments
 (0)
0