Skip to content

Commit fdb60ca

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 22a498b commit fdb60ca

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
@@ -1876,6 +1876,7 @@ qtext_load_file(Size *buffer_size)
18761876
char *buf;
18771877
int fd;
18781878
struct stat stat;
1879+
Size nread;
18791880

18801881
fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDONLY | PG_BINARY, 0);
18811882
if (fd < 0)
@@ -1916,28 +1917,40 @@ qtext_load_file(Size *buffer_size)
19161917
}
19171918

19181919
/*
1919-
* OK, slurp in the file. If we get a short read and errno doesn't get
1920-
* set, the reason is probably that garbage collection truncated the file
1921-
* since we did the fstat(), so we don't log a complaint --- but we don't
1922-
* return the data, either, since it's most likely corrupt due to
1923-
* concurrent writes from garbage collection.
1920+
* OK, slurp in the file. Windows fails if we try to read more than
1921+
* INT_MAX bytes at once, and other platforms might not like that either,
1922+
* so read a very large file in 1GB segments.
19241923
*/
1925-
errno = 0;
1926-
if (read(fd, buf, stat.st_size) != stat.st_size)
1924+
nread = 0;
1925+
while (nread < stat.st_size)
19271926
{
1928-
if (errno)
1929-
ereport(LOG,
1930-
(errcode_for_file_access(),
1931-
errmsg("could not read pg_stat_statement file \"%s\": %m",
1932-
PGSS_TEXT_FILE)));
1933-
free(buf);
1934-
CloseTransientFile(fd);
1935-
return NULL;
1927+
int toread = Min(1024 * 1024 * 1024, stat.st_size - nread);
1928+
1929+
/*
1930+
* If we get a short read and errno doesn't get set, the reason is
1931+
* probably that garbage collection truncated the file since we did
1932+
* the fstat(), so we don't log a complaint --- but we don't return
1933+
* the data, either, since it's most likely corrupt due to concurrent
1934+
* writes from garbage collection.
1935+
*/
1936+
errno = 0;
1937+
if (read(fd, buf + nread, toread) != toread)
1938+
{
1939+
if (errno)
1940+
ereport(LOG,
1941+
(errcode_for_file_access(),
1942+
errmsg("could not read pg_stat_statement file \"%s\": %m",
1943+
PGSS_TEXT_FILE)));
1944+
free(buf);
1945+
CloseTransientFile(fd);
1946+
return NULL;
1947+
}
1948+
nread += toread;
19361949
}
19371950

19381951
CloseTransientFile(fd);
19391952

1940-
*buffer_size = stat.st_size;
1953+
*buffer_size = nread;
19411954
return buf;
19421955
}
19431956

0 commit comments

Comments
 (0)