Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rR40g-00531u-Aa for pgsql-general@arkaria.postgresql.org; Sat, 20 Jan 2024 05:35:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rR40f-00CihL-Cg for pgsql-general@arkaria.postgresql.org; Sat, 20 Jan 2024 05:35:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rR40f-00CihD-1n for pgsql-general@lists.postgresql.org; Sat, 20 Jan 2024 05:35:13 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rR40c-002Qsc-7a for pgsql-general@lists.postgresql.org; Sat, 20 Jan 2024 05:35:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 40K5Z7HY2922216; Sat, 20 Jan 2024 00:35:07 -0500 From: Tom Lane To: Adrian Klaver cc: Andreas Joseph Krogh , pgsql-general@lists.postgresql.org Subject: Re: Daterange question In-reply-to: <253fb378-5d21-4605-a444-9338990253a2@aklaver.com> References: <253fb378-5d21-4605-a444-9338990253a2@aklaver.com> Comments: In-reply-to Adrian Klaver message dated "Fri, 19 Jan 2024 21:13:20 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2922214.1705728907.1@sss.pgh.pa.us> Date: Sat, 20 Jan 2024 00:35:07 -0500 Message-ID: <2922215.1705728907@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Adrian Klaver writes: > On 1/19/24 20:08, Andreas Joseph Krogh wrote: >> This seems to do what I want: >> |NOT (drange << daterange(CURRENT_DATE, NULL, '[)'))| >> But this doesn't use the index. >> Any idea how to write a query so it uses the index on |drange|? > Without the full query and the EXPLAIN output I don't see that there is > much that can be offered in way of an answer. Well, we can definitively state that the NOT makes this unindexable. You need a WHERE clause that looks like indexed-column indexable-operator pseudo-constant which this isn't, nor does << have a negator operator that could allow the NOT to be simplified out. Wouldn't drange && daterange(CURRENT_DATE, NULL, '[)') serve the purpose? That should be indexable. regards, tom lane