Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 18CC72E2D7D for ; Thu, 15 Nov 2007 12:25:30 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 02118-04 for ; Thu, 15 Nov 2007 12:25:27 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id D67A12E2DBA for ; Thu, 15 Nov 2007 12:25:26 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id lAFGPOva003670; Thu, 15 Nov 2007 11:25:24 -0500 (EST) To: "Chris Hoover" cc: "pgsql-admin@postgresql.org Admin" Subject: Re: any way to make query use index? In-reply-to: <1d219a6f0711150714j6f192548xe4c134a25e5b57b2@mail.gmail.com> References: <1d219a6f0711150714j6f192548xe4c134a25e5b57b2@mail.gmail.com> Comments: In-reply-to "Chris Hoover" message dated "Thu, 15 Nov 2007 10:14:41 -0500" Date: Thu, 15 Nov 2007 11:25:24 -0500 Message-ID: <3669.1195143924@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/149 X-Sequence-Number: 27392 "Chris Hoover" writes: > SELECT rev_desc FROM rev_code_desc WHERE rev_code = lpad('0300', 4, '0') > Is there anyway to make postgres use an index on this query? > Here is the table definition: > CREATE TABLE rev_code_desc > ( > rev_code character(4) NOT NULL, Since rev_code is char(n), you need to cast the other thing to char(n) as well: SELECT rev_desc FROM rev_code_desc WHERE rev_code = lpad('0300', 4, '0')::char(4) lpad() is declared to return text, which means that your original query resolves as ... WHERE rev_code::text = lpad('0300', 4, '0'), which means that the index on rev_code is useless (since it has bpchar semantics not text semantics). If changing the queries seems impractical, you could add an index on rev_code::text, or reconsider the choice of column type. regards, tom lane