title: Postgres - ERROR: prepared statement "S_1" already exists
body: <p>When executing batch queries via JDBC to pgbouncer, I get the following error:</p>
  
  <pre><code>org.postgresql.util.PSQLException: ERROR: prepared statement "S_1" already exists
  </code></pre>
  
  <p>I've found bug reports around the web, but they all seem to deal with Postgres 8.3 or below, whereas we're working with Postgres 9.</p>
  
  <p>Here's the code that triggers the error:</p>
  
  <pre><code>this.getJdbcTemplate().update("delete from xx where username = ?", username);
  
  this.getJdbcTemplate().batchUpdate( "INSERT INTO xx(a, b, c, d, e) " + 
                  "VALUES (?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() {
      @Override
      public void setValues(PreparedStatement ps, int i) throws SQLException {
          ps.setString(1, value1);
          ps.setString(2, value2);
          ps.setString(3, value3);
          ps.setString(4, value4);
          ps.setBoolean(5, value5);
      }
      @Override
      public int getBatchSize() {
          return something();
      }
  });
  </code></pre>
  
  <p>Anyone seen this before?</p>
  
  <p><strong><em>Edit 1:</em></strong></p>
  
  <p>This turned out to be a pgBouncer issue that occurs when using anything other than <strong>session pooling</strong>. We were using <strong>transaction pooling</strong>, which apparently can't support prepared statements. By switching to <strong>session pooling</strong>, we got around the issue.</p>
  
  <p>Unfortunately, this isn't a good fix for our use case. We have two separate uses for pgBouncer: one part of our system does bulk updates which are most efficient as prepared statements, and another part needs many connections in very rapid succession. Since pgBouncer doesn't allow switching back and forth between <strong>session pooling</strong> and <strong>transaction pooling</strong>, we're forced to run two separate instances on different ports just to support our needs.</p>
  
  <p><strong><em>Edit 2:</em></strong></p>
  
  <p>I ran across <a href="http://pgfoundry.org/pipermail/pgbouncer-general/2010-February/000507.html" rel="noreferrer">this link</a>, where the poster has rolled a patch of his own. We're currently looking at implementing it for our own uses if it proves to be safe and effective.</p>