Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hLsgK-00036O-GY for pgsql-general@arkaria.postgresql.org; Wed, 01 May 2019 17:06:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hLsgJ-0003bC-2b for pgsql-general@arkaria.postgresql.org; Wed, 01 May 2019 17:06:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hLsgI-0003W7-KJ for pgsql-general@lists.postgresql.org; Wed, 01 May 2019 17:06:06 +0000 Received: from mail-ot1-x32c.google.com ([2607:f8b0:4864:20::32c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hLsgC-0002lH-8D for pgsql-general@lists.postgresql.org; Wed, 01 May 2019 17:06:05 +0000 Received: by mail-ot1-x32c.google.com with SMTP id s11so10975735otp.0 for ; Wed, 01 May 2019 10:06:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=qN52kmjFrFFV6Wd672zqAdhEmmvo/sYR5EHp+XLQSww=; b=M3J8VLz1bCWhRsA58VbsRHYcPcXD/7pSGlcl2M/CkMzemF21y+aJp8/oMKSZqFt6B7 doiPYqn1YHVzyumW+/D2sXVHmJ3CUp0KARuDvHBOWmaA3xbroIzxl2sqNCHqLFL8gWCO qi+S0mWyp2u1TLoX0sZEti7PncyJMDd7gxvSf7j1aTBqx2UVZ+/X1VRf5frWmKxS9l27 DPLNNzQOYrIhneLUUNQVx7bMQIBNFyJX9nGbdkhf2t3IV8X0aFjQwnNmGcBhMUXoZcnK Bp7NutYoqN8wiEYqBZHx+/9fFO5az5k3CBuK8wefOre58mvIBVnP32d9wWjta60Nid3g Ywhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=qN52kmjFrFFV6Wd672zqAdhEmmvo/sYR5EHp+XLQSww=; b=fkrvtK2nX9nQIl47kX1jiaZ112klDwhIY2cVXgja64B+FDda9OPFjXs3snS6xSouxw gX+74cb4Wfy3yr2AbFxZm+AoGJQixHKueBhw8eM/hMAS8+unu2MxQf41gpkDOrfq3tg8 qIYGEzHdHLsFWR0oSUwdprUxKwfFCa7klDBv3N8SjuaAEtUBwLKleVOMcGUMn437kQYS OgPEjMDU6u6iT5WV6flovVQVvO7sHxuWeU08j1pzpTpQf6ojBnf9CLmptR53xggeAO9X dnbiwc6cQDVF5s4C+HAY2qn3CrH8YeGoP2l23SuHy68xf9KqwxcNG0ozSNL9HUj86Em0 7iYA== X-Gm-Message-State: APjAAAXkn4iZ+IZpCJaixh/UDTOkY7aqlgBfvfku3BaxBWQffLfV4acV 9yOAUN4n5kSFbz0SNScBIO3a9wMOVmwaCA== X-Google-Smtp-Source: APXvYqw8z+FFIE6i8bZQyDgvZjJ/pmZjQvv8U7C+iX03D5ulkVk4IAD+ZGpN9Sddj3mYoAT/efJEjg== X-Received: by 2002:a05:6830:11cc:: with SMTP id v12mr9846678otq.79.1556730359555; Wed, 01 May 2019 10:05:59 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id 96sm16699501otf.17.2019.05.01.10.05.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 May 2019 10:05:58 -0700 (PDT) Subject: Re: Query not producing expected result To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <105c087b-71a1-b26a-1453-8320e8318a03@gmail.com> Date: Wed, 1 May 2019 12:05:57 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------8DE12F56C48FEFBAC2F08245" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------8DE12F56C48FEFBAC2F08245 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 5/1/19 11:39 AM, Julien Rouhaud wrote: > On Wed, May 1, 2019 at 6:27 PM Chuck Martin wrote: >> I need help figuring out why a query is not returning the records I expect it to. I'm searching on a DateTime column (timestamp without time zone - not nullable). The query includes: >> >> AND event.Primaryresp_fkey = 5000011 AND event.DateTime <= 'May-1-2019' AND event.EventDone < 1 >> >> This does not return a record that has a DateTime value "May 1, 2019 9:52 AM". If I modify the query to: >> >> AND event.Primaryresp_fkey = 5000011 AND event.DateTime <= 'May-2-2019' AND event.EventDone < 1 >> >> it does return the record in question. I assume I am not doing the data comparison correctly, but don't know why. > That's because it's using a timestamp comparison, not a date > comparison. So 'May-1-2019' is casted to timestamp, which gives > 2019-05-01 00:00:00. > > If you want a comparison based on date instead of timestamp , you have > to ask to it explicitly. For instance: event.DateTime*::date* <= > 'May-1-2019' Wouldn't that stop the query planner from using any index on event.DateTime? -- Angular momentum makes the world go 'round. --------------8DE12F56C48FEFBAC2F08245 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit On 5/1/19 11:39 AM, Julien Rouhaud wrote:
On Wed, May 1, 2019 at 6:27 PM Chuck Martin <clmartin@theombudsman.com> wrote:
I need help figuring out why a query is not returning the records I expect it to. I'm searching on a DateTime column (timestamp without time zone - not nullable). The query includes:

 AND event.Primaryresp_fkey = 5000011 AND event.DateTime <= 'May-1-2019'  AND event.EventDone < 1

This does not return a record that has a DateTime value "May 1, 2019 9:52 AM". If I modify the query to:

 AND event.Primaryresp_fkey = 5000011 AND event.DateTime <= 'May-2-2019'  AND event.EventDone < 1

it does return the record in question. I assume I am not doing the data comparison correctly, but don't know why.
That's because it's using a timestamp comparison, not a date
comparison.  So 'May-1-2019' is casted to timestamp, which gives
2019-05-01 00:00:00.

If you want a comparison based on date instead of timestamp , you have
to ask to it explicitly.  For instance: event.DateTime::date <=
'May-1-2019'

Wouldn't that stop the query planner from using any index on event.DateTime?

--
Angular momentum makes the world go 'round.
--------------8DE12F56C48FEFBAC2F08245--