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 1tfLnU-001hmZ-UG for pgsql-general@arkaria.postgresql.org; Tue, 04 Feb 2025 16:29:13 +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 1tfLnT-005G49-7Y for pgsql-general@arkaria.postgresql.org; Tue, 04 Feb 2025 16:29:11 +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 1tfLnS-005G41-Sf for pgsql-general@lists.postgresql.org; Tue, 04 Feb 2025 16:29:10 +0000 Received: from fhigh-a1-smtp.messagingengine.com ([103.168.172.152]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tfLnQ-0039Y2-2a for pgsql-general@lists.postgresql.org; Tue, 04 Feb 2025 16:29:09 +0000 Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfhigh.phl.internal (Postfix) with ESMTP id 306D11140196; Tue, 4 Feb 2025 11:29:08 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Tue, 04 Feb 2025 11:29:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1738686548; x=1738772948; bh=naT2G5D02dHhtV5RBj2RSqkPohiweSkZosJV3C0ztoU=; b= Up5BMrTMTJJH1xfbRzX5XKa5uN6Y/xFx6DCdrMpgu5AP4QEnmVr3L4FcnnwatGI7 Osm1+a2LPQRPxC173+eq3cnCnn21XAqcbFdwM1vp8EOJJaNSWjZSwPSJ55oQm7le 8VTWiSbDZKqtfAdRN4SOI7jyadeTaD/lw6wPgaFGqv8vpZOo/ruHqNa+HvVifhuB VM+onkROKHl2DKEtdMlBLxkpE2CI2iIGBZOoMRM5rOXrkflNtd4rBnz/8L1De2ZG ya/XmGHAuDEOwmcodTdItDEd+T/AorWseAdSSKBQiVT77XGQ/sa30mAdPnOfgbqJ EzJmYFZ9EgzTlEy/EiSVLw== 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:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1738686548; x=1738772948; bh=n aT2G5D02dHhtV5RBj2RSqkPohiweSkZosJV3C0ztoU=; b=RNrG+8329M6bKLYBb 8h2PVhuHk5pywZCz1wTmF4OiRBxoGoCjlAIyn85Ddb6KgTJgv1lCLplzgklVyHiZ Azc9tKkZSGjb722OyF32w9kAC3CscVGHEZhTZCk4n7xCASwYFxBi3D1j9hFVKwna SSL82cU5mIiHCIim24jNlqP5ydy0PYUVQOM4d3q7M4BBjymz1jPzsD/Ad4YmBURn ugXLEad9olHs9zkeTXkpHFb6kPrB8KLZIJc/xsslALmSjqm5Rwvn8Lx2sr3vRENU w4AXLMr6uT6TKS3Dp9an+Vp7dcaxQ4xI1dl9BNnHv4Hl2VcoP618ZgXtuFGJnk4L QIx7w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvuddtudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgg gfuffvfhfhjggtgfesthekredttddvjeenucfhrhhomheptegurhhirghnucfmlhgrvhgv rhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrg htthgvrhhnpeffleegieefgfevudehtdfhkeeutdffjeevgeffgeejvedthefgudeiteef heejheenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe grughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggprhgtphhtthho pedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehhvghnnhhinhhgrdhgrghruh hssehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlheslhhi shhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 4 Feb 2025 11:29:07 -0500 (EST) Message-ID: <34ea369e-6814-427d-96fe-fe8e5fb41e0e@aklaver.com> Date: Tue, 4 Feb 2025 08:29:06 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Index usage with differing string types To: henning.garus@gmail.com, pgsql-general@lists.postgresql.org References: Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/4/25 08:23, Henning Garus wrote: > Hi, > > I stumbled upon this behaviour when digging into the performance of some > merge statements generated by hibernate. > > Looking at different String types (varchar, text and bpchar) in some > cases an index is used when the index type differs from the type in the > query, in some cases it isn't used. > > Given a table with an index on a bpchar column: > > create table test (id bpchar(8) primary key); > > Both of the following queries use the index: > > explain select * from test where id = 'foo'::bpachar(3); > explain select * from test where id = 'foo'::varchar; > > However when the String is cast to text the index isn't used: > > explain select * from test where id = 'foo'::text; The output from EXPLAIN ANALYZE on each of the queries would be useful. > > This behavior seems to be consistent across postgres 12, 16 and 17. > > I find it surprising that the cast to varchar behaves differently than > the cast to text, is this intended behaviour? > > Cheers >   Henning > -- Adrian Klaver adrian.klaver@aklaver.com