David Chapman <david.chapman@mavensecurities.com> writes:
> Here is the output of EXPLAIN ANALYZE on the two queries.
> Index Scan using test_index_t1_t2 on test (cost=0.43..684.11 rows=71
> width=245) (actual time=0.022..1.147 rows=99 loops=1)
> Index Cond: ((t1 = 'X'::bpchar) AND (t2 = ANY ('{2286575,2139022,2139030,
^^^^^^^^^^^^^^^^^^
> Seq Scan on test (cost=0.00..403725.30 rows=1 width=245) (actual
> time=47.543..5362.518 rows=99 loops=1)
> Filter: (((t1)::text = 'X'::text) AND (t2 = ANY
^^^^^^^^^^^^^^^^^^^^^^^^
There's your problem. t1 is evidently of char(n) type, and when you
write "t1 = 'X'" the literal also becomes char(n) and so you have
a condition that can match the index. But the parameter is evidently
being assigned type text, which wins the type conflict so then you have
"t1::text text-eq text-constant", and that operator doesn't match the index.
Probably, casting the parameter to char(n) explicitly would fix this.
regards, tom lane