Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gEt8l-00042O-P6 for pgsql-general@arkaria.postgresql.org; Tue, 23 Oct 2018 09:38:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gEt8j-0006Qv-51 for pgsql-general@arkaria.postgresql.org; Tue, 23 Oct 2018 09:38:17 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gEt8h-0006Qn-UP for pgsql-general@lists.postgresql.org; Tue, 23 Oct 2018 09:38:16 +0000 Received: from mout01.posteo.de ([185.67.36.65]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gEt8Z-0003Av-Un for pgsql-general@postgresql.org; Tue, 23 Oct 2018 09:38:14 +0000 Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 39A2B20984 for ; Tue, 23 Oct 2018 11:38:03 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1540287484; bh=ycnPcdyI0nw9vRaAaM44q5KxiS2RZeAZJD9hLDzudFc=; h=From:Date:To:Subject:From; b=ZW/FrUFPkfDYUzsjz5SSmvRnfIkWmHpC+6mT+e186B2ob1MPM+QLUtzvqeZIdFRvT g+lvgRvR4Sz9aCTfQBNs9baWg3WlOZ0yj4ENSPB36+fyI1W3Zwxc5+kG+R1tuyshFH qIuG26F/0Baa+ZuzGzmGSHG3RNEjGVQPF0MFp7eGX4sX+DVTLz5Wg7TjfRA5XbMTlU uIscgnUH7JdQkUvhC0fudyycuEzCVLr3YDAurSiIrwwO+9ycAhyXBV7MczNSO0IR8W XX2fQtVGoSWXwXJCHmGBlyLUGR4ih6C7HIwRgB1vLTFo1pGLcIY7JRzNlNbWYXe6eo cIyFH/q0gUQqQ== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 42fSx63G7Nz6tmH for ; Tue, 23 Oct 2018 11:38:02 +0200 (CEST) Received: by posteo.de (sSMTP sendmail emulation); Tue, 23 Oct 2018 11:38:01 +0200 From: "Lutz Horn" Date: Tue, 23 Oct 2018 11:38:01 +0200 To: pgsql-general@postgresql.org Subject: Select "todays" timestamps in an index friendly way Message-ID: <20181023093801.GA21523@lutz-pc.ecm4u.intra> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, I am looking for a way to select all timestamps that are "today" in an index friendly way. This select should not depend on the concrete value of "today". Given a table create temporary table t ( id SERIAL primary key, ts timestamp not null default now() ); with some data insert into t (ts) select ts from generate_series( '2018-01-01T00:00:01'::timestamp, '2018-12-31T23:59:59'::timestamp, '2 minutes') as ts; and an index create index on t (ts, id); I can of course make an explicit select for `ts` values that are "today": select ts, id from t where ts >= '2018-10-23T00:00:00'::timestamp and ts <= '2018-10-23T23:59:59'::timestamp; This uses an Bitmap Index Scan on `t_ts_id_idx`. Good. But the where conditions depends on concrete values of "today" which will not return the intended result if I execute it tomorrow. I will have to change the where condition. Not good. I am looking for a way to make the where condition independed of the date of execution. I can create a function create function is_today(timestamp) returns boolean as $$ select to_char(now(), 'YYYY-MM-DD') = to_char($1, 'YYYY-MM-DD'); $$ language sql; that converts the timestamps to text. But using this function select * from t where is_today(ts); will not benefit from the existing index. A Seq Scan on `t` will be used. Not good. Is there a way to have both: be independed of the concrete value of "today" *and* use the index on the timestamp column? Lutz