title: PostgreSQL timestamp index not used when cast to date
body: <p>This is my query:</p>
  
  <pre><code>SELECT
      i::date AS day,
      (SELECT COUNT(*) FROM genericevent WHERE event = 'chat_message' AND eventDate::date = i::date AND extra1 = 'public') AS message_public_total,
      (SELECT COUNT(*) FROM genericevent WHERE event = 'chat_message' AND eventDate::date = i::date AND extra1 = 'public' AND extra2 = 'clean') AS message_public_clean
  FROM generate_series('2013-08-01', '2013-08-27', INTERVAL '1 day') i
  </code></pre>
  
  <p>I have an index which I as a human consider fully usable for this query (in fact it should result in an index-only scan):</p>
  
  <pre><code>CREATE INDEX idx__genericevent__event__extra1__date
    ON genericevent
    USING btree
    (event COLLATE pg_catalog."default", extra1 COLLATE pg_catalog."default", eventDate);
  </code></pre>
  
  <p>However, as <code>EXPLAIN</code>ed, PostgreSQL doesn't deem it so. It uses <code>event</code> and <code>extra1</code> from this index, but not <code>eventDate</code> (see the <code>Index Cond</code> lines):</p>
  
  <pre><code>"Function Scan on generate_series i  (cost=0.00..145219698.17 rows=1000 width=8)"
  "  SubPlan 1"
  "    -&gt;  Aggregate  (cost=72274.87..72274.88 rows=1 width=0)"
  "          -&gt;  Bitmap Heap Scan on genericevent  (cost=11367.74..72271.51 rows=1345 width=0)"
  "                Recheck Cond: (((event)::text = 'chat_message'::text) AND ((extra1)::text = 'public'::text))"
  "                Filter: ((eventDate)::date = (i.i)::date)"
  "                -&gt;  Bitmap Index Scan on idx__genericevent__event__extra1__date  (cost=0.00..11367.40 rows=269012 width=0)"
  "                      Index Cond: (((event)::text = 'chat_message'::text) AND ((extra1)::text = 'public'::text))"
  "  SubPlan 2"
  "    -&gt;  Aggregate  (cost=72944.79..72944.80 rows=1 width=0)"
  "          -&gt;  Bitmap Heap Scan on genericevent  (cost=11367.50..72943.80 rows=396 width=0)"
  "                Recheck Cond: (((event)::text = 'chat_message'::text) AND ((extra1)::text = 'public'::text))"
  "                Filter: (((extra2)::text = 'clean'::text) AND ((eventDate)::date = (i.i)::date))"
  "                -&gt;  Bitmap Index Scan on idx__genericevent__event__extra1__date  (cost=0.00..11367.40 rows=269012 width=0)"
  "                      Index Cond: (((event)::text = 'chat_message'::text) AND ((extra1)::text = 'public'::text))"
  </code></pre>
  
  <p>I think it may have to do something by the <code>eventDate::date</code> cast. How can I change the query or the index to improve performance?</p>
  
  <p>For completeness, here's the table:</p>
  
  <pre><code>CREATE TABLE genericevent
  (
    id bigint NOT NULL,
    eventDate timestamp with time zone NOT NULL,
    event character varying(50) NOT NULL,
    extra1 character varying(100),
    extra2 character varying(100),
    CONSTRAINT genericevent_pkey PRIMARY KEY (id)
  )
  </code></pre>