title: Can prepared SQL statement be rewritten by the database?
body: <p>I have this fairly innocent looking JDBC code:</p>
  
  <pre><code>String sql = "UPDATE table_name SET column2 = column1";
  try (PreparedStatement statement = dbConnection.prepareStatement(sql)) {
    statement.executeUpdate();
  }
  dbConnection.commit();
  </code></pre>
  
  <p>When running this on PostgreSQL I have noticed that the <em>actual running</em> query (which is visible from within PostgreSQL) is as follows:</p>
  
  <pre><code>UPDATE table_name SET column2 = i.column1 FROM table_name i
  </code></pre>
  
  <p>The problem is that the rewritten query is <strong>much</strong> more expensive:</p>
  
  <pre><code># explain update table_name set column2 = i.column1 from table_name i;
  
                           QUERY PLAN                                      
  
  -------------------------------------------------------------------------------  
  Update on table_name  (cost=0.00..3586127424.55 rows=206294914809 width=166)          
     -&gt;  Nested Loop  (cost=0.00..3586127424.55 rows=206294914809 width=166)
           -&gt;  Seq Scan on table_name (cost=0.00..15453.97 rows=454197 width=156)
           -&gt;  Materialize  (cost=0.00..19942.96 rows=454197 width=10)
                 -&gt;  Seq Scan on table_name i  (cost=0.00..15453.97 rows=454197 width=10)
  
  (5 rows)
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code># explain update table_name set column2 = column1;
  
                             QUERY PLAN                                
  
  ------------------------------------------------------------------------
  Update on table_name  (cost=0.00..15453.97 rows=454197 width=156)
   -&gt;  Seq Scan on table_name  (cost=0.00..15453.97 rows=454197 width=156)
  (2 rows)
  </code></pre>
  
  <p>The rewritten query takes virtually infinite amount of time to run whereas non-rewritten finishes in few minutes if not seconds.</p>
  
  <p>Questions:</p>
  
  <ul>
  <li>Is it common for database (I presume) to rewrite queries?</li>
  <li>If yes, how come PostgreSQL is stupid enough to shoot itself in the foot? Is this a known bug?</li>
  <li>How can query rewrite be avoided - both at database but preferably JDBC level?</li>
  </ul>