title: PostgreSQL JDBC query slow, direct on database fast
body: <p>I'm having an interesting issue with Spring JDBC. I have a simple SELECT query which just selects a single row from the database by its identifier. When I run this query through PgAdmin raw on the database it returns in approximately 500ms. When I run it through my Java stack it takes a full 20 <em>seconds</em>, and it does not get faster on subsequent runs.</p>
  
  <p>This is the last layer of my code that hangs:</p>
  
  <pre><code>return jdbcTemplate.queryForObject(QUERY_SINGLE_COMPONENT, queryArguments, new ComponentRowMapper());
  </code></pre>
  
  <p>(my jdbcTemplate is a NamedParameterJdbcTemplate). I have run VisualVM on my code during this execution:</p>
  
  <p><a href="https://i.stack.imgur.com/iGjrh.png" rel="noreferrer"><img src="https://i.stack.imgur.com/iGjrh.png" alt="jvisualvm profile"></a></p>
  
  <p>the PostgreSQL driver readMore is taking essentially all of the time. I'm using the PostgreSQL driver 42.0.0 but I tried updating to the latest version which did not help.</p>
  
  <p>Other IDs in this same query return just fine, other queries I'm making through this same codebase work just fine. Its been driving me crazy for days and would appreciate any insight!</p>
  
  <p>EDIT: I've managed to isolate the issue to our schema search path. We use a test schema to stage database changes. When test is in the schema search path it is slow, when test is removed it is fast. In both cases it is still slower than when I run it in PgAdmin by an order of magnitude.</p>