Search code examples
postgresqljdbcprepared-statementpg-jdbc

What are the bennefits of prepareThreshold = 5 in pgjdbc?


The prepareThreshold in pgjdbc have the following definition:

Determine the number of PreparedStatement executions required before switching over to use server side prepared statements. The default is five, meaning start using server side prepared statements on the fifth execution of the same PreparedStatement object. More information on server side prepared statements is available in the section called “Server Prepared Statements”.

I wonder what benefit this actually brings us? Most webservers aren't restarted for months, so all database queries will be sent more than 5 times eventually, so give it a week or so and all prepared statements will be stored at the server, won't them? Is this only for benefiting desktop applications? Or am I missing something, like a "5 threshold over a period of time"?


Solution

  • The reason the driver waits until the client has used the PreparedStatement 5 times by default is because using a named prepared statement on the server implies the overhead of an extra round trip to describe the statement. Typically it's not worth the extra round trip. The driver provides the connection parameter to allow the client to adjust that threshold as you see fit.