title: Postgresql order by and limit
body: <p>I have a table R from which want to select the 3 predecessors or successor based on the z column. I have a btree index over the z column.</p>
  
  <pre><code>SELECT key, z, x, y FROM R
      WHERE z &lt;= some_value
      ORDER BY z DESC 
      LIMIT 3
  </code></pre>
  
  <p>retrieves the predecessors in 'no time' but</p>
  
  <pre><code>SELECT key, z, x, y FROM R
      WHERE z &gt;= some_value
      ORDER BY z ASC
      LIMIT 3
  </code></pre>
  
  <p>is useless for retrieving the next entries because is starts to scan through all the rows that have z >= some_value. Explain gives identical explanations on how the query is satisfied.
  Why is this and what could I do about it?</p>
  
  <p>EDIT:
  There are two indexes actually, one ascending and one descending.
  Explain for both queries above says:
  "Limit  (cost=0.00..31.91 rows=3 width=20)"
  "  ->  Index Scan Backward using zreverse on r  (cost=0.00..17727815.24 rows=1666667 width=20)"
  "        Filter: (z >= some_value")</p>