Re: Index usage with differing string types

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: henning(dot)garus(at)gmail(dot)com, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Index usage with differing string types
Date: 2025-02-04 16:29:06
Message-ID: 34ea369e-6814-427d-96fe-fe8e5fb41e0e@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michał Kłeczek 2025-02-04 16:31:13 Re: Lookup tables
Previous Message Henning Garus 2025-02-04 16:23:39 Index usage with differing string types