Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 9CC42633F00 for ; Tue, 3 Mar 2009 08:12:50 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 99551-04 for ; Tue, 3 Mar 2009 08:12:46 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from broadway.cambrient.com (broadway.cambrient.com [196.211.141.188]) by mail.postgresql.org (Postfix) with ESMTP id BB23C633EAB for ; Tue, 3 Mar 2009 08:12:45 -0400 (AST) Received: from 192.168.0.69 ([192.168.0.69]) by broadway.cambrient.com (JAMES SMTP Server 2.3.1) with SMTP ID 812 for ; Tue, 3 Mar 2009 14:18:27 +0200 (SAST) Message-ID: <49AD1EBD.1050105@cambrient.com> Date: Tue, 03 Mar 2009 14:12:45 +0200 From: Hans Liebenberg Reply-To: hans@cambrient.com User-Agent: Thunderbird 2.0.0.19 (Windows/20081209) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Substring search using "exists" with a space in the search term Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.673 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001, MIME_HTML_ONLY=1.672 X-Spam-Level: * X-Archive-Number: 200903/23 X-Sequence-Number: 32945 Hi,

I have come across a weird bug (i think) in postgres 8.1.11 (possibly others)

Without going into my table structure detail I will demonstrate the problem by showing the select statements:

The following statement:
SELECT count(*)
FROM object o, object_version v, object_type ot
where v.id = o.active_versionid and ot.id = o.object_typeid and o.is_active ='t' and (o.is_archived = 'f' or o.is_archived is null)
and o.is_published = 't' and ot.object_type_typeid <> 1

and exists (
select ova.object_versionid from attribute_value av, object_version_attribute ova where ova.attribute_valueid=av.id and object_versionid = v.id
and (upper(av.text_val) like '%KIWI%') )


runs fine and executes with success.
BUT now this is the strange bit, if I have a space in my search term then postgres hangs for an indefinite period: eg:

SELECT count(*)
FROM object o, object_version v, object_type ot
where v.id = o.active_versionid and ot.id = o.object_typeid and o.is_active ='t' and (o.is_archived = 'f' or o.is_archived is null)
and o.is_published = 't' and ot.object_type_typeid <> 1

and exists (
select ova.object_versionid from attribute_value av, object_version_attribute ova where ova.attribute_valueid=av.id and object_versionid = v.id
and (upper(av.text_val) like '%KIWI FRUIT%') )


Yet, if I modify the "exists" to an "in" all works well , as follows

SELECT count(*)
FROM object o, object_version v, object_type ot
where v.id = o.active_versionid and ot.id = o.object_typeid and o.is_active ='t' and (o.is_archived = 'f' or o.is_archived is null)
and o.is_published = 't' and ot.object_type_typeid <> 1

and v.id in (
select ova.object_versionid from attribute_value av, object_version_attribute ova where ova.attribute_valueid=av.id
and (upper(av.text_val) like '%KIWI FRUIT%') )


So my question is why would a space character cause postgres to hang when using the exists clause????

I have tested this on several different servers and mostly get the same result (v8.08 and v8.1.11) , when I check the execution plan for either query (space or no space) they are identical.

An upgrade to 8.3 fixes this, but I am still curious as to what could cause such bizarre behavior.

Thanks
Hans