title: PostgreSQL slow JOIN with CASE statement
body: <p>In my database I have a table that contains ~3500 records and as a part of more complicated query I've tried to perform inner join on itself using "CASE" condition just as you can see below.</p>
  
  <pre><code>SELECT *
  FROM some_table AS t1
  JOIN some_table AS t2 ON t1.type = t2.type
      AND CASE
         WHEN t1.type = 'ab' THEN t1.first = t2.first
         WHEN t1.type = 'cd' THEN t1.second = t2.second
         -- Column type contains only one of 2 possible varchar values
      END;
  </code></pre>
  
  <p>The problem is this query is performed for 3.2 - 4.5 seconds while next request is performed in 40 - 50 milliseconds.</p>
  
  <pre><code>SELECT *
  FROM some_table AS t1
  JOIN some_table AS t2 ON t1.type = t2.type
      AND (t1.first = t2.first OR t1.second = t2.second)
  </code></pre>
  
  <p>Also according to the execution plan in first case database processes ~5.8 millions of records while table contains only ~3500. There are next indexes on this table: (id), (type), (type, first), (type, second). </p>
  
  <p>We are using next version:
  PostgreSQL 9.4.5 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120
  313 (Red Hat 4.4.7-16), 64-bit</p>
  
  <p>Any ideas why PostgreSQL works so weird in this case?</p>