Skip to content

Commit 3a5b313

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 df238ae commit 3a5b313

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
@@ -2114,6 +2114,7 @@ qtext_load_file(Size *buffer_size)
21142114
char *buf;
21152115
int fd;
21162116
struct stat stat;
2117+
Size nread;
21172118

21182119
fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDONLY | PG_BINARY);
21192120
if (fd < 0)
@@ -2154,31 +2155,43 @@ qtext_load_file(Size *buffer_size)
21542155
}
21552156

21562157
/*
2157-
* OK, slurp in the file. If we get a short read and errno doesn't get
2158-
* set, the reason is probably that garbage collection truncated the file
2159-
* since we did the fstat(), so we don't log a complaint --- but we don't
2160-
* return the data, either, since it's most likely corrupt due to
2161-
* concurrent writes from garbage collection.
2158+
* OK, slurp in the file. Windows fails if we try to read more than
2159+
* INT_MAX bytes at once, and other platforms might not like that either,
2160+
* so read a very large file in 1GB segments.
21622161
*/
2163-
errno = 0;
2164-
if (read(fd, buf, stat.st_size) != stat.st_size)
2162+
nread = 0;
2163+
while (nread < stat.st_size)
21652164
{
2166-
if (errno)
2167-
ereport(LOG,
2168-
(errcode_for_file_access(),
2169-
errmsg("could not read file \"%s\": %m",
2170-
PGSS_TEXT_FILE)));
2171-
free(buf);
2172-
CloseTransientFile(fd);
2173-
return NULL;
2165+
int toread = Min(1024 * 1024 * 1024, stat.st_size - nread);
2166+
2167+
/*
2168+
* If we get a short read and errno doesn't get set, the reason is
2169+
* probably that garbage collection truncated the file since we did
2170+
* the fstat(), so we don't log a complaint --- but we don't return
2171+
* the data, either, since it's most likely corrupt due to concurrent
2172+
* writes from garbage collection.
2173+
*/
2174+
errno = 0;
2175+
if (read(fd, buf + nread, toread) != toread)
2176+
{
2177+
if (errno)
2178+
ereport(LOG,
2179+
(errcode_for_file_access(),
2180+
errmsg("could not read file \"%s\": %m",
2181+
PGSS_TEXT_FILE)));
2182+
free(buf);
2183+
CloseTransientFile(fd);
2184+
return NULL;
2185+
}
2186+
nread += toread;
21742187
}
21752188

21762189
if (CloseTransientFile(fd) != 0)
21772190
ereport(LOG,
21782191
(errcode_for_file_access(),
21792192
errmsg("could not close file \"%s\": %m", PGSS_TEXT_FILE)));
21802193

2181-
*buffer_size = stat.st_size;
2194+
*buffer_size = nread;
21822195
return buf;
21832196
}
21842197

0 commit comments

Comments
 (0)