Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3a5b313

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'sadjust this code to read at most 1GB per call.(One would not have thought the file could get that big, but nowwe have a field report of trouble, so it can. We likely ought toadd some mechanism to limit the size of the query-texts fileseparately from the size of the hash table. That is not thispatch, though.)Per bug #17254 from Yusuke Egashira. It's been like this forawhile, so back-patch to all supported branches.Discussion:https://postgr.es/m/17254-a926c89dc03375c2@postgresql.org
1 parentdf238ae commit3a5b313

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
intfd;
21162116
structstatstat;
2117+
Sizenread;
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-
returnNULL;
2165+
inttoread=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+
returnNULL;
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
returnbuf;
21832196
}
21842197

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp