Skip to content

Commit a667b06

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 b214155 commit a667b06

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
@@ -2125,6 +2125,7 @@ qtext_load_file(Size *buffer_size)
21252125
char *buf;
21262126
int fd;
21272127
struct stat stat;
2128+
Size nread;
21282129

21292130
fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDONLY | PG_BINARY);
21302131
if (fd < 0)
@@ -2165,31 +2166,43 @@ qtext_load_file(Size *buffer_size)
21652166
}
21662167

21672168
/*
2168-
* OK, slurp in the file. If we get a short read and errno doesn't get
2169-
* set, the reason is probably that garbage collection truncated the file
2170-
* since we did the fstat(), so we don't log a complaint --- but we don't
2171-
* return the data, either, since it's most likely corrupt due to
2172-
* concurrent writes from garbage collection.
2169+
* OK, slurp in the file. Windows fails if we try to read more than
2170+
* INT_MAX bytes at once, and other platforms might not like that either,
2171+
* so read a very large file in 1GB segments.
21732172
*/
2174-
errno = 0;
2175-
if (read(fd, buf, stat.st_size) != stat.st_size)
2173+
nread = 0;
2174+
while (nread < stat.st_size)
21762175
{
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;
2176+
int toread = Min(1024 * 1024 * 1024, stat.st_size - nread);
2177+
2178+
/*
2179+
* If we get a short read and errno doesn't get set, the reason is
2180+
* probably that garbage collection truncated the file since we did
2181+
* the fstat(), so we don't log a complaint --- but we don't return
2182+
* the data, either, since it's most likely corrupt due to concurrent
2183+
* writes from garbage collection.
2184+
*/
2185+
errno = 0;
2186+
if (read(fd, buf + nread, toread) != toread)
2187+
{
2188+
if (errno)
2189+
ereport(LOG,
2190+
(errcode_for_file_access(),
2191+
errmsg("could not read file \"%s\": %m",
2192+
PGSS_TEXT_FILE)));
2193+
free(buf);
2194+
CloseTransientFile(fd);
2195+
return NULL;
2196+
}
2197+
nread += toread;
21852198
}
21862199

21872200
if (CloseTransientFile(fd) != 0)
21882201
ereport(LOG,
21892202
(errcode_for_file_access(),
21902203
errmsg("could not close file \"%s\": %m", PGSS_TEXT_FILE)));
21912204

2192-
*buffer_size = stat.st_size;
2205+
*buffer_size = nread;
21932206
return buf;
21942207
}
21952208

0 commit comments

Comments
 (0)