Skip to content

Commit d87d5f8

Browse files
committed
Don't try to read a multi-GB pg_stat_statements file in one call.
Windows fails on a request to read() more than INT_MAX bytes, and perhaps other platforms could have similar issues. Let's adjust this code to read at most 1GB per call. (One would not have thought the file could get that big, but now we have a field report of trouble, so it can. We likely ought to add some mechanism to limit the size of the query-texts file separately from the size of the hash table. That is not this patch, though.) Per bug #17254 from Yusuke Egashira. It's been like this for awhile, so back-patch to all supported branches. Discussion: https://postgr.es/m/17254-a926c89dc03375c2@postgresql.org
1 parent d0fe211 commit d87d5f8

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,7 @@ qtext_load_file(Size *buffer_size)
19331933
char *buf;
19341934
int fd;
19351935
struct stat stat;
1936+
Size nread;
19361937

19371938
fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDONLY | PG_BINARY, 0);
19381939
if (fd < 0)
@@ -1973,28 +1974,40 @@ qtext_load_file(Size *buffer_size)
19731974
}
19741975

19751976
/*
1976-
* OK, slurp in the file. If we get a short read and errno doesn't get
1977-
* set, the reason is probably that garbage collection truncated the file
1978-
* since we did the fstat(), so we don't log a complaint --- but we don't
1979-
* return the data, either, since it's most likely corrupt due to
1980-
* concurrent writes from garbage collection.
1977+
* OK, slurp in the file. Windows fails if we try to read more than
1978+
* INT_MAX bytes at once, and other platforms might not like that either,
1979+
* so read a very large file in 1GB segments.
19811980
*/
1982-
errno = 0;
1983-
if (read(fd, buf, stat.st_size) != stat.st_size)
1981+
nread = 0;
1982+
while (nread < stat.st_size)
19841983
{
1985-
if (errno)
1986-
ereport(LOG,
1987-
(errcode_for_file_access(),
1988-
errmsg("could not read pg_stat_statement file \"%s\": %m",
1989-
PGSS_TEXT_FILE)));
1990-
free(buf);
1991-
CloseTransientFile(fd);
1992-
return NULL;
1984+
int toread = Min(1024 * 1024 * 1024, stat.st_size - nread);
1985+
1986+
/*
1987+
* If we get a short read and errno doesn't get set, the reason is
1988+
* probably that garbage collection truncated the file since we did
1989+
* the fstat(), so we don't log a complaint --- but we don't return
1990+
* the data, either, since it's most likely corrupt due to concurrent
1991+
* writes from garbage collection.
1992+
*/
1993+
errno = 0;
1994+
if (read(fd, buf + nread, toread) != toread)
1995+
{
1996+
if (errno)
1997+
ereport(LOG,
1998+
(errcode_for_file_access(),
1999+
errmsg("could not read pg_stat_statement file \"%s\": %m",
2000+
PGSS_TEXT_FILE)));
2001+
free(buf);
2002+
CloseTransientFile(fd);
2003+
return NULL;
2004+
}
2005+
nread += toread;
19932006
}
19942007

19952008
CloseTransientFile(fd);
19962009

1997-
*buffer_size = stat.st_size;
2010+
*buffer_size = nread;
19982011
return buf;
19992012
}
20002013

0 commit comments

Comments
 (0)