Re: Different query plan used for the same query depending on how parameters are passed

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David Chapman <david(dot)chapman(at)mavensecurities(dot)com>
Cc: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Different query plan used for the same query depending on how parameters are passed
Date: 2017-05-16 15:51:58
Message-ID: 20300.1494949918@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

David Chapman <david(dot)chapman(at)mavensecurities(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Peter J. Holzer 2017-05-16 16:14:40 Re: storing large files in database - performance
Previous Message Martin Goodson 2017-05-16 15:39:48 Re: EnterpriseDB installed PostgreSQL 9.6 vs. REPMGR. Round 4 - compilation issues on RHEL 7.2