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

Commit7104e0b

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 parent8424dfc commit7104e0b

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
intfd;
21272127
structstatstat;
2128+
Sizenread;
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-
returnNULL;
2176+
inttoread=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+
returnNULL;
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
returnbuf;
21942207
}
21952208

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp