Skip to content

Commit 126b60c

Browse files
committed
perf: skip caching of very large queries to prevent statement cache pollution
This serves as a basic protection from cache pollution. More sophisticated one will come in pgjdbc#344
1 parent 5ec7dea commit 126b60c

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

doc/pgjdbc.xml

+2
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ openssl pkcs8 -topk8 -in client.key -out client.pk8 -outform DER -v1 PBE-SHA1-3D
766766
the least recently used ones will be discarded.
767767
The main aim of this setting is to prevent <classname>OutOfMemoryError</classname>.
768768
The value of 0 disables the cache.
769+
If a query would consume more than a half of <varname>preparedStatementCacheSizeMiB</varname>,
770+
then it is discarded immediately.
769771
</para>
770772
</listitem>
771773
</varlistentry>

org/postgresql/util/LruCache.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@ public Value borrow(Key key) throws SQLException
103103
*/
104104
public void put(Key key, Value value)
105105
{
106-
if (maxSizeBytes == 0 || maxSizeEntries == 0) {
107-
// Just destroy the value if cache is disabled
106+
long valueSize = value.getSize();
107+
if (maxSizeBytes == 0 || maxSizeEntries == 0 || valueSize * 2 > maxSizeBytes) {
108+
// Just destroy the value if cache is disabled or if entry would consume more than a half of the cache
108109
evictValue(value);
109110
return;
110111
}
111-
currentSize += value.getSize();
112+
currentSize += valueSize;
112113
cache.put(key, value);
113114
}
114115
}

0 commit comments

Comments
 (0)