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 1tduPl-00AZjq-8K for pgsql-novice@arkaria.postgresql.org; Fri, 31 Jan 2025 17:02:46 +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 1tduPk-001ndx-3S for pgsql-novice@arkaria.postgresql.org; Fri, 31 Jan 2025 17:02:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tduPj-001ndm-3K for pgsql-novice@lists.postgresql.org; Fri, 31 Jan 2025 17:02:43 +0000 Received: from fhigh-b6-smtp.messagingengine.com ([202.12.124.157]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tduPe-002ZK6-2d for pgsql-novice@postgresql.org; Fri, 31 Jan 2025 17:02:41 +0000 Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfhigh.stl.internal (Postfix) with ESMTP id 6DA232540105 for ; Fri, 31 Jan 2025 12:02:36 -0500 (EST) Received: from phl-imap-07 ([10.202.2.97]) by phl-compute-02.internal (MEProxy); Fri, 31 Jan 2025 12:02:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=de.me.tz; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:message-id:mime-version:reply-to:subject :subject:to:to; s=fm1; t=1738342956; x=1738429356; bh=AA942b4AzB uFq1FoDB82Zz4kOaZp2ubsh9Iobq+d2pI=; b=P1kTU/pLQIMt58iwI2LwA/L+/N vPI3xADWCBlIoQvLlo08axsPq8ieFS42TDZgyWeeBhvFpUxdfG+wxS3x6K7h/vXF gs5szLyvXcgt1sLDN6mgRBOSgfXOEAB9iahRNqz7kDUXQELV/+WimuINGonEiQNo 2wpJIX0UJsK1EPpGG10QAbj8bGLYQoU6RpDMJOQjtCwqxodsp3N7oSly8teELxzy NTyHPClgJppHixztR434SuGKK/0dHJe4BiNBYIUSmQLm9nb6xFpwtI8PIYg6DfNr vyXuc9E4CSoi7tlQTr1rKXTvfRve2BJYrIPx3K6ufpLR/uSR5Obq7Uy6Mu1w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1738342956; x=1738429356; bh=AA942b4AzBuFq1FoDB82Zz4kOaZp2ubsh9I obq+d2pI=; b=xvUTkJmLQPNx5+5118pf/8HTs9r3T8II9JPBnuGPwOY9HpCooJt 4Z660hRzfh4sC0jGuTu7xFXQQChXSNZnAPpZhntYiv28Bq/+3h+TqUemrw+bq1yP vJ/hbfkQEKjIVSXWA1hR3hXmxkKBn+zRg7NwxtcE6U4/N6ZI0lTDRxDaZyZPOOKh jayLmd/88ce33V4tbGnmozzwM5v6qnRp3kCrmLzmXbogDlIF6X37lbbKWvq0imLb IdXYpgRkJY97Jvr8dYb3nJshV97R6Mjeiakg3kvKgnT12ABQQu9DsuzD/roxIIX5 axv5lLc8U7Gh16ItfTQIqfZs2PSQjWfUsTA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdelfedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefoggffhf fvkffutgfgsehtjeertdertddtnecuhfhrohhmpedfsfhuvghnthhinhcuuggvucfovght iidfuceoqhhuvghnthhinhesuggvrdhmvgdrthiiqeenucggtffrrghtthgvrhhnpedtff fgvddviefgjeekleetieehveeiffeuueegjeelvdduhfdvgefhgedvhffgteenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehquhgvnhhtihhnse guvgdrmhgvrdhtiidpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhr tghpthhtohepphhgshhqlhdqnhhovhhitggvsehpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i7a6842ed:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 51023BA006F; Fri, 31 Jan 2025 12:02:35 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 Date: Fri, 31 Jan 2025 18:02:13 +0100 From: "Quentin de Metz" To: pgsql-novice@postgresql.org Message-Id: <58782480-ab75-4416-a177-ccf91be288a9@app.fastmail.com> Subject: btree_gin, bigint and number literals Content-Type: text/plain Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi all, On a multi-column GIN index over a bigint column and a text column, the query planner does not filter the index on the bigint column when a condition on this column is specified with a number literal. Please find below a simple reproduction script: CREATE TABLE cars (owner_id BIGINT, license_plate TEXT); INSERT INTO cars (owner_id, license_plate) SELECT i % 100, md5(random()::text) FROM generate_series(1, 10000) AS t(i); CREATE EXTENSION btree_gin; CREATE EXTENSION pg_trgm; CREATE INDEX testidx ON cars USING GIN (owner_id, license_plate gin_trgm_ops); -- below, we see that the Index Cond does not take the owner_id condition into account EXPLAIN SELECT * FROM cars WHERE owner_id = 12 AND license_plate ILIKE '%abc%'; QUERY PLAN ------------------------------------------------------------------------- Bitmap Heap Scan on cars (cost=13.32..112.93 rows=1 width=41) Recheck Cond: (license_plate ~~* '%abc%'::text) Filter: (owner_id = 12) -> Bitmap Index Scan on testidx (cost=0.00..13.32 rows=101 width=0) Index Cond: (license_plate ~~* '%abc%'::text) -- when explicitly casting the number literal to bigint, the Index Cond takes it into account EXPLAIN SELECT * FROM cars WHERE owner_id = 12::bigint AND license_plate ILIKE '%abc%'; QUERY PLAN --------------------------------------------------------------------------------------- Bitmap Heap Scan on cars (cost=21.52..25.54 rows=1 width=41) Recheck Cond: ((owner_id = '12'::bigint) AND (license_plate ~~* '%abc%'::text)) -> Bitmap Index Scan on testidx (cost=0.00..21.52 rows=1 width=0) Index Cond: ((owner_id = '12'::bigint) AND (license_plate ~~* '%abc%'::text)) -- the more permanent solution seems to be altering the operator type ALTER OPERATOR FAMILY int8_ops USING gin ADD OPERATOR 1 < (int8, int4) , OPERATOR 2 <= (int8, int4) , OPERATOR 3 = (int8, int4) , OPERATOR 4 >= (int8, int4) , OPERATOR 5 > (int8, int4) , FUNCTION 1 (int8, int4) btint84cmp(int8, int4); QUERY PLAN ----------------------------------------------------------------------------- Bitmap Heap Scan on cars (cost=21.52..25.54 rows=1 width=41) Recheck Cond: ((owner_id = 12) AND (license_plate ~~* '%abc%'::text)) -> Bitmap Index Scan on testidx (cost=0.00..21.52 rows=1 width=0) Index Cond: ((owner_id = 12) AND (license_plate ~~* '%abc%'::text)) Would you be open to considering a patch to include the ALTER OPERATOR snippet in the btree_gin install script, so that this works out of the box? Quentin de Metz