Received: from sss2.sss.pgh.pa.us (sss.pgh.pa.us [209.114.166.2]) by hub.org (8.9.3/8.9.3) with ESMTP id PAA60061 for ; Tue, 16 May 2000 15:12:05 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss2.sss.pgh.pa.us (8.9.3/8.9.3) with ESMTP id PAA14058; Tue, 16 May 2000 15:11:56 -0400 (EDT) To: Kyle Bateman cc: pgsql-sql@postgresql.org, merrill@actarg.com Subject: Re: Index not used in functions in 7.0? In-reply-to: <3921689A.B70CFAC8@actarg.com> References: <3921689A.B70CFAC8@actarg.com> Comments: In-reply-to Kyle Bateman message dated "Tue, 16 May 2000 09:26:18 -0600" Date: Tue, 16 May 2000 15:11:56 -0400 Message-ID: <14055.958504316@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200005/20 Kyle Bateman writes: > create function summtr_ocpt2(text, int4) returns int4 as ' > select sum(tquant) from mtr_reg where to_proj = $2 and pnum = $1 and > (status = \'open\' or status = \'clsd\' or status = \'prip\'); > ' language 'sql'; Ah, I've sussed it. The difference between the environment in your function and the environment in a hand-entered query is that you've declared $1 to be type 'text', whereas in the hand-entered query the parser resolves the unknown-type string literal into type 'varchar' to match the type of what it's being compared to. What you effectively have inside the function is pnum::text = $1::text, and the planner is not able to figure out that it can use a varchar index for that. If you had written the hand-entered query as "... pnum = '1051'::text ..." it wouldn't have been done with an indexscan either. Best short-term solution is to declare the function as taking varchar in the first place. This behavior is a regression from 6.5, which handled implicit coercions differently and could recognize the clause as indexable despite the inserted coercion. I'll look into whether it's patchable for 7.0.1. A proper fix might be too complex to risk patching into 7.0.1 though :-( ... might have to wait for 7.1. regards, tom lane