Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q7CWz-0007uI-Ci for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jun 2023 10:06:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q7CWy-0007yg-99 for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jun 2023 10:06:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q7CWx-0007yX-VQ for pgsql-hackers@lists.postgresql.org; Thu, 08 Jun 2023 10:06:11 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q7CWs-000sBc-FC for pgsql-hackers@lists.postgresql.org; Thu, 08 Jun 2023 10:06:10 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id E50A85C021E for ; Thu, 8 Jun 2023 06:06:05 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute2.internal (MEProxy); Thu, 08 Jun 2023 06:06:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:sender:subject:subject:to:to; s=fm1; t=1686218765; x=1686305165; bh=eTD5pm5TYa0/89jBT19tWfd+S 0k63mgb+FcNbX+26Mg=; b=Yar6e9k5Oa8veWVz9UpU7EI7mWmYPnrlTF+XbDzgm 2c0hRIsElQOsLdnfRzf9D65G9SQHgy4WN+Uf/xPGYebu6MdBBYAOxBbike1L7Cy8 cHKyLop5keqN7Wg1vRcjLaU+uq+njOcBwCX4mwoqgGyUXBOW64S2QNJL6KlOZyQQ w0X78Nmane9dm164ZR1vGkFVLxmXAl8aIB5BKstAvcTaoBE1elFgz5wSlhiBQG2D vjJXQEtkVDBtCrRxe+fGg2a9MHNVuGtnbSOfwLS/n6kNk6p2JZpYxlQXvbs9lx8Z 8CLqEw+rDNsryt57bXC94fBnL0Ste64/ZfQGhGMlH5BIw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1686218765; x=1686305165; bh=eTD5pm5TYa0/89jBT19tWfd+S0k63mgb+Fc NbX+26Mg=; b=uOOg9TWKrUQ2aYPWZqv2FUT2GoSh07cLSMXp8iWKJ3U1/lndfvI X2xSsk4AHWcR7+waSDkCkqBu2IOxVhhtvrO4s70DduAom0Wl5gp897px16KaZ37t +P13z2A+pZmeRovrniA7Uz/iPDRH23iRILr0/aQycbklBmRSu+FD/T1hgttwlKYA 5rqDu8wvJdD88sJ31cwoylJ/ek/zjeQFsmriHpIXVNvLkqjkta6jwX7Oo2CPD25C 5Rlo4M2DwmGtSUvgFYfKFblqvjreNQhF7WspGjp3bteaZrTd+caHXn3vBztMQxA+ D3ZzjAhfkeCzPuXCQx9hbGDWoVeoHbQrNJQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgedtiedgvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsegrtderre erredtnecuhfhrohhmpedflfhovghlucflrggtohgsshhonhdfuceojhhovghlsegtohhm phhilhgvrhdrohhrgheqnecuggftrfgrthhtvghrnhepgefgvdfhgfefjedtleegteejje fhieegjedvhfehueetfeektdeiieefheffkeelnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomhepjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id A10B331A0063; Thu, 8 Jun 2023 06:06:05 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-447-ge2460e13b3-fm-20230525.001-ge2460e13 Mime-Version: 1.0 Message-Id: Date: Thu, 08 Jun 2023 12:05:44 +0200 From: "Joel Jacobson" To: pgsql-hackers@lists.postgresql.org Subject: [btree-indexed column] <@ [range | multirange] Content-Type: multipart/alternative; boundary=47e336bf2eeb4fe88d79be40d4099441 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --47e336bf2eeb4fe88d79be40d4099441 Content-Type: text/plain I've noticed the planner is not yet smart enough to do an index scan when the left operand of a contains operator (<@) is a btree-indexed column and the right operand is a range or multirange type of the same type as the column. For instance, given a users table with an id int primary key column, these two queries are functionally equivalent, but only the second one makes use of the btree index: SELECT COUNT(*) FROM users WHERE id <@ int4range(10,20); SELECT COUNT(*) FROM users WHERE id >= 10 AND id < 20; Multirange example: SELECT COUNT(*) FROM users WHERE id <@ int4multirange('{[10,20),[30,40)}'); SELECT COUNT(*) FROM users WHERE id >= 10 AND id < 20 OR id >= 30 AND id < 40; I think support for this would open up for some interesting new use-cases, when range/multirange could be used to store aggregated intermediate IDs which would then be filtered on using a btree-indexed column. /Joel --47e336bf2eeb4fe88d79be40d4099441 Content-Type: text/html Content-Transfer-Encoding: quoted-printable
I've noticed th= e planner is not yet smart enough to do an index scan
when= the left operand of a contains operator (<@) is a btree-indexed colu= mn
and the right operand is a range or multirange type of = the same type
as the column.

= For instance, given a users table with an id int primary key column,
=
these two queries are functionally equivalent, but only the s= econd
one makes use of the btree index:

=
SELECT COUNT(*) FROM users WHERE id <@ int4range(10,20);
SELECT COUNT(*) FROM users WHERE id >=3D 10 AND id < = 20;

Multirange example:

SELECT COUNT(*) FROM users WHERE id <@ int4multirange('{[10,20)= ,[30,40)}');
SELECT COUNT(*) FROM users WHERE id >=3D 1= 0 AND id < 20 OR id >=3D 30 AND id < 40;

I think support for this would open up for some interesting new = use-cases,
when range/multirange could be used to store ag= gregated intermediate IDs
which would then be filtered on = using a btree-indexed column.

/Joel



--47e336bf2eeb4fe88d79be40d4099441--