title: Prevent usage of index for a particular query in Postgres
body: <p>I have a slow query in a Postgres DB. Using <code>explain analyze</code>, I can see that Postgres makes bitmap index scan on two different indexes followed by bitmap AND on the two resulting sets.</p>
  
  <p>Deleting one of the indexes makes the evaluation ten times faster (bitmap index scan is still used on the first index). However, that deleted index is useful in other queries.</p>
  
  <p>Query:</p>
  
  <pre><code>select
    booking_id
  from
    booking
  where
    substitute_confirmation_token is null
    and date_trunc('day', from_time) &gt;= cast('01/25/2016 14:23:00.004' as date)
    and from_time &gt;= '01/25/2016 14:23:00.004'
    and type = 'LESSON_SUBSTITUTE'
    and valid
  order by
    booking_id;
  </code></pre>
  
  <p>Indexes:</p>
  
  <pre><code>"idx_booking_lesson_substitute_day" btree (date_trunc('day'::text, from_time)) WHERE valid AND type::text = 'LESSON_SUBSTITUTE'::text
  "booking_substitute_confirmation_token_key" UNIQUE CONSTRAINT, btree (substitute_confirmation_token)
  </code></pre>
  
  <p>Query plan:</p>
  
  <pre><code>Sort  (cost=287.26..287.26 rows=1 width=8) (actual time=711.371..711.377 rows=44 loops=1)
    Sort Key: booking_id
    Sort Method: quicksort  Memory: 27kB
    Buffers: shared hit=8 read=7437 written=1
    -&gt;  Bitmap Heap Scan on booking  (cost=275.25..287.25 rows=1 width=8) (actual time=711.255..711.294 rows=44 loops=1)
          Recheck Cond: ((date_trunc('day'::text, from_time) &gt;= '2016-01-25'::date) AND valid AND ((type)::text = 'LESSON_SUBSTITUTE'::text) AND (substitute_confirmation_token IS NULL))
          Filter: (from_time &gt;= '2016-01-25 14:23:00.004'::timestamp without time zone)
          Buffers: shared hit=5 read=7437 written=1
          -&gt;  BitmapAnd  (cost=275.25..275.25 rows=3 width=0) (actual time=711.224..711.224 rows=0 loops=1)
                Buffers: shared hit=5 read=7433 written=1
                -&gt;  Bitmap Index Scan on idx_booking_lesson_substitute_day  (cost=0.00..20.50 rows=594 width=0) (actual time=0.080..0.080 rows=72 loops=1)
                      Index Cond: (date_trunc('day'::text, from_time) &gt;= '2016-01-25'::date)
                      Buffers: shared hit=5 read=1
                -&gt;  Bitmap Index Scan on booking_substitute_confirmation_token_key  (cost=0.00..254.50 rows=13594 width=0) (actual time=711.102..711.102 rows=2718734 loops=1)
                      Index Cond: (substitute_confirmation_token IS NULL)
                      Buffers: shared read=7432 written=1
  Total runtime: 711.436 ms
  </code></pre>
  
  <p>Can I prevent using a particular index for a particular query in Postgres?</p>