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 1iZz19-0001t7-Pr for pgsql-general@arkaria.postgresql.org; Wed, 27 Nov 2019 15:14:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iZz18-0004VW-6i for pgsql-general@arkaria.postgresql.org; Wed, 27 Nov 2019 15:14:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZz17-0004VP-TP for pgsql-general@lists.postgresql.org; Wed, 27 Nov 2019 15:14:09 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZz12-0004h7-Hw for pgsql-general@lists.postgresql.org; Wed, 27 Nov 2019 15:14:09 +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 xARFDxBL006966; Wed, 27 Nov 2019 10:13:59 -0500 From: Tom Lane To: Lauri Kajan cc: pgsql-general@lists.postgresql.org Subject: Re: Range contains element filter not using index of the element column In-reply-to: References: Comments: In-reply-to Lauri Kajan message dated "Wed, 27 Nov 2019 11:32:10 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <6964.1574867639.1@sss.pgh.pa.us> Date: Wed, 27 Nov 2019 10:13:59 -0500 Message-ID: <6965.1574867639@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Lauri Kajan writes: > I have a table with a timestamp column that has a btree index. > I would like to do a query: > SELECT * FROM table WHERE ts <@ tsrange($1, $2, '(]'); > The index is not used and a seq scan is done instead. > To use the index correctly I have to do the query like this: > SELECT * FROM table WHERE ($1 IS null OR $1 < ts) AND ($2 IS null OR ts <= > $2); > I like the <@ syntax more. Is there something I can do differently? Maybe a > different type of index instead? As others mentioned, a gist index on a tsrange expression could be used for this, but another idea is to build some syntactic sugar using a custom operator. Light testing suggests that this works: create function expand_range_contain(anyelement, anyrange) returns bool language sql parallel safe as $$ select case when lower_inf($2) then true when lower_inc($2) then $1 >= lower($2) else $1 > lower($2) end and case when upper_inf($2) then true when upper_inc($2) then $1 <= upper($2) else $1 < upper($2) end $$; create operator <<@ ( function = expand_range_contain, leftarg = anyelement, rightarg = anyrange ); select * from table where ts <<@ tsrange($1, $2, '(]'); An important caveat though is that the range operand *must* reduce to a constant. If the planner fails to const-simplify those CASE expressions, you'll not only not get an indexscan, but you'll be worse off than with the native <@ operator. So this isn't an all-purpose fix --- but it might cover your needs and be nicer than maintaining a second index on the column. regards, tom lane