Re: text and varchar are not equivalent - Mailing list pgsql-docs

From Tom Lane
Subject Re: text and varchar are not equivalent
Date
Msg-id 1694198.1707501111@sss.pgh.pa.us
Whole thread Raw
In response to Re: text and varchar are not equivalent  ("David G. Johnston" <david.g.johnston@gmail.com>)
List pgsql-docs
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Fri, Feb 9, 2024, 10:12 PG Doc comments form <noreply@postgresql.org>
> wrote:
>> The documentation implies that the data types text and varchar are
>> equivalent, but this is not the case with this test in Postgresql version
>> 16.

> Fair point.  But I'd rather further emphasize that char should just be
> avoided so this and other unexpected outcomes simply do not manifest in a
> real database scenario.  Rather than try and document how odd it's behavior
> is when dealing with intra-textual type conversions.

Yeah, this is less about varchar acting oddly and more about char
acting oddly.  The short answer though is that text is a preferred
type, varchar is not, and that makes a difference when resolving
whether to apply text's or char's equality operator.  You can
detect how it's being handled with EXPLAIN:

regression=# explain verbose SELECT vc = ch AS vc_ch FROM test;
                          QUERY PLAN                           
---------------------------------------------------------------
 Seq Scan on pg_temp.test  (cost=0.00..17.88 rows=630 width=1)
   Output: ((vc)::bpchar = ch)
(2 rows)

regression=# explain verbose SELECT txt = ch AS txt_ch FROM test;
                          QUERY PLAN                           
---------------------------------------------------------------
 Seq Scan on pg_temp.test  (cost=0.00..19.45 rows=630 width=1)
   Output: (txt = (ch)::text)
(2 rows)

            regards, tom lane



pgsql-docs by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: text and varchar are not equivalent
Next
From: PG Doc comments form
Date:
Subject: bgwriter_delay