Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eCpof-0003Hq-Bv for pgsql-general@arkaria.postgresql.org; Thu, 09 Nov 2017 16:36:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1eCpoe-0005AG-Bd for pgsql-general@arkaria.postgresql.org; Thu, 09 Nov 2017 16:36:32 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eCpmE-0000kV-0J for pgsql-general@postgresql.org; Thu, 09 Nov 2017 16:34:02 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eCpmB-0000oO-G3 for pgsql-general@postgresql.org; Thu, 09 Nov 2017 16:34:00 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id vA9GXsb4024937; Thu, 9 Nov 2017 11:33:54 -0500 From: Tom Lane To: Dingyuan Wang cc: "pgsql-general@postgresql.org" Subject: Re: Index not used when using expression In-reply-to: <42dbce76-9986-f9a0-e29d-12013e1b8b8e@aosc.io> References: <42dbce76-9986-f9a0-e29d-12013e1b8b8e@aosc.io> Comments: In-reply-to Dingyuan Wang message dated "Fri, 10 Nov 2017 00:27:59 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <24935.1510245234.1@sss.pgh.pa.us> Date: Thu, 09 Nov 2017 11:33:54 -0500 Message-ID: <24936.1510245234@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org Dingyuan Wang writes: > I have a table named "gps", with an indexed column "packettime", which > has unix timestamps. > The following query: > select * from gps where packettime < extract(epoch from '2017-05-01 > 08:00+08'::timestamp with time zone) > explains to: > Seq Scan on gps (cost=0.00..43411860.64 rows=384325803 width=120) > Filter: ((packettime)::double precision < date_part('epoch'::text, > '2017-05-01 08:00:00+08'::timestamp with time zone)) The reason that's not working for you is that the query is not testing packettime, it's testing packettime::float8, because date_part() returns float8. You could cast the result of date_part() to bigint, or whatever type the packettime column actually is, so that the comparison is to the unadorned variable. regards, tom lane -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general