title: Execution of a Postgresql query using JDBC taking much more time compared to execution of query in pgadmin
body: <p>I have a stored procedure, with an argument(Parameter)</p>
  
  <p>Let us call it <code>x</code>.</p>
  
  <p>When I run, </p>
  
  <pre><code>select * from x('myParameter')
  </code></pre>
  
  <p>It completes in 4 minutes.</p>
  
  <p>Now I try to execute the same stored prcedure in java as follows:-</p>
  
  <pre><code>PreparedStatement statement = connection.prepareStatement("Select * from x('myParameter')");
  org.postgresql.PGStatement pgstmt = (org.postgresql.PGStatement)statement;
  pgstmt.setPrepareThreshold(1);
  boolean usingServerPrepare = pgstmt.isUseServerPrepare();
  System.out.println(usingServerPrepare);
  statement.execute();
  </code></pre>
  
  <p>This takes approximately 26 hours to complete.</p>
  
  <p>I was initially trying without <code>pgstmt.setPrepareThreshold(1);</code>, 
  i.e. Default value for prepareThreshold.
  The result was the same.</p>
  
  <p>Then I tried the java program with "set enable_nestloop false" as the first line of the stored procedure and it completes in 4 minutes.</p>
  
  <p>I am not able to understand why the query changes the execution plan, when I am explicitly forcing server side prepared statement.</p>
  
  <p>I am using Postgres 9.4 and Java 8. I am also using PGBouncer for connection pooling.  </p>
  
  <p>My query here is - Why is JDBC influencing the execution plan if at all? Is there a way to force server side execution plan to my query triggered from java program?</p>