title: Postgres Query Plan hugely differs for query with increased timetamp range
body: <p>Hey there postgres specialists.
  We are facing a strange behavior with one of our tables. While increasing the queried timestamp range there is the tipping point where the query time suddenly increases by about 5 times.</p>
  <p>The table we are looking at:</p>
  <pre><code>                   Table &quot;measurements.hourly_measurement_recordings&quot;
          Column         |              Type              | Collation | Nullable | Default 
  -----------------------+--------------------------------+-----------+----------+---------
   dimension_identifier  | integer                        |           | not null | 
   occasion_identifier   | integer                        |           | not null | 
   items_identifier      | bit(64)                        |           | not null | 
   beginning_of_timeslot | timestamp(0) without time zone |           | not null | 
   dumped_weight         | integer                        |           | not null | 
  Indexes:
      &quot;hourly_measurement_recordings_kind_identifier_type_identifier_p&quot; UNIQUE, btree (occasion_identifier, dimension_identifier, items_identifier, beginning_of_timeslot)
      &quot;hourly_measurement_recordings_kind_identifier_type_identifier_b&quot; btree (occasion_identifier, dimension_identifier, beginning_of_timeslot)
  </code></pre>
  <p>First query + plan:
  <a href="https://explain.depesz.com/s/LyzL" rel="nofollow noreferrer">https://explain.depesz.com/s/LyzL</a></p>
  <p>Second query + plan:
  <a href="https://explain.depesz.com/s/ZCvZ" rel="nofollow noreferrer">https://explain.depesz.com/s/ZCvZ</a></p>
  <p>The only difference between those 2 queries is the increased timestamp range:</p>
  <p><code>'2021-03-15 23:00:00Z', '2021-04-19 21:00:00Z'</code></p>
  <p>vs</p>
  <p><code>'2021-03-14 23:00:00Z', '2021-04-19 21:00:00Z'</code></p>
  <p>As you can see there is a shift from a <code>Parallel Bitmap Heap Scan</code> to a <code>Parallel Seq Scan</code> which increases the query time.</p>
  <p>Here I some specs of the used database:</p>
  <ul>
  <li>PostgreSQL v13.1</li>
  <li>Running on AWS db.t3.medium with 2 vCPUs, 4GB of RAM, General Purpose SSD (40GB)</li>
  </ul>
  <p>I am not really experienced with database configurations so I don't really know which parameters might be useful for you - just let me know if there is any more information relevant to share.</p>
  <p>Thank you,
  Nick</p>