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 1iaEll-0003Vm-BS for pgsql-general@arkaria.postgresql.org; Thu, 28 Nov 2019 08:03:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iaElk-00020R-2R for pgsql-general@arkaria.postgresql.org; Thu, 28 Nov 2019 08:03:20 +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 1iaElj-00020K-N8 for pgsql-general@lists.postgresql.org; Thu, 28 Nov 2019 08:03:19 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iaEle-0005RK-8u for pgsql-general@lists.postgresql.org; Thu, 28 Nov 2019 08:03:19 +0000 Received: by mail-wr1-x443.google.com with SMTP id b18so29889180wrj.8 for ; Thu, 28 Nov 2019 00:03:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=vanDDHvlNG56yG7zXR7WlSgo4qAUhBEhWZK9c1iz5Vs=; b=fhvvsXTG1chzj3BpLR6tm5pAnsvG8mBNftbwWF7WhRn4NDVr4S0apBBkJk5p0EvihM zSAHdz1oDujimPd8mJwEjhJ3WVkegqyl3uu0hcFLmBeGmCOeE8J6dbe3MJirZTqqfgyL 2vVyXLEqPkiBp41kSPbNn/FbmksGVyCSgFYmTxLn/+YhdVhlX0vj/urEB5YaLudTTI+6 QzbZhrECh2XcQPwuxHQ4Srl/GH/Hiy8rCOxIMbs8y8GBs/rwb2x6Brc7tRQ3YoxoDlIL 3+zyP/Bb9PONZ058hHWfTS8f7eGxMlAcV4rMmcuIKCB3kItU0vTMVXYP/VpMQIPsnKMo 1dEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=vanDDHvlNG56yG7zXR7WlSgo4qAUhBEhWZK9c1iz5Vs=; b=ZGOzkhHzf+mv9/a+jVeSbtYFg7ljDrNSuV6aZ9oGfTVYX4eWaWaickS0m8au3ZGn5y g9mNTx6gD0o0sy3ag6uBnYGaPvsQOGqBFO2yk4VWPAdBj2/O2dvzROsBkIoNzwgr5B3U GstKP8FFbaAz+GPeRBzoKuiJyybWnCz0xO0gkvsgjsL9QdMYYrw2aXKQP+Wzx4yK9wvz xVAzIKGfvdAIxLQbk0t3OIC9Og3FPVu4mMsUrVW7k0n2grLA5ZI5+Gux/2lqYaRddUMM ztd+WNTYTpMtPmdOQDcDCWMjleFD18nkRQ7bRAHAjuotwLRwFASpysv63MvwhvDh9Z1F STfg== X-Gm-Message-State: APjAAAVfvATcvh/lOS2WIoCnn4RxCl1qlquDNF+SS/8Quxu2tdsi3l4U 5FTwNPRpPdLneOh+yY+2fNg= X-Google-Smtp-Source: APXvYqyznaajAHyv2JPmt/v4fMg3j/Fb/l6CaYFuSHWbDJUjXU4ZF0GyioKGRhuRUToKmOKa0/uw7Q== X-Received: by 2002:adf:dc11:: with SMTP id t17mr29373929wri.294.1574928193033; Thu, 28 Nov 2019 00:03:13 -0800 (PST) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id d7sm22677612wrx.11.2019.11.28.00.03.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Nov 2019 00:03:11 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: Range contains element filter not using index of the element column From: Alban Hertroys In-Reply-To: Date: Thu, 28 Nov 2019 09:03:10 +0100 Cc: pgsql-general@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Lauri Kajan X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 27 Nov 2019, at 10:32, Lauri Kajan wrote: >=20 > Hi all, > I'm wondering if there are anything to do to utilize a index when = doing a range contains element query. I have tested this with 9.6 and = 12.0. >=20 > 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. >=20 > 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 <=3D $2); > I like the <@ syntax more. Is there something I can do differently? = Maybe a different type of index instead? Does it help to use timestamps -infinity and infinity instead of nulls = in your case? =3D> select t, t < current_timestamp, current_timestamp <=3D t from = (values ('-infinity'::timestamp), ('infinity'::timestamp)) x(t); = =20 t | ?column? | ?column?=20 -----------+----------+---------- -infinity | t | f infinity | f | t (2 rows) Regards, Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.