Re: CHAR vs NVARCHAR vs TEXT performance - Mailing list pgsql-hackers

From Tom Lane
Subject Re: CHAR vs NVARCHAR vs TEXT performance
Date
Msg-id 30249.1556559837@sss.pgh.pa.us
Whole thread Raw
In response to CHAR vs NVARCHAR vs TEXT performance  (Rob <postgresql@mintsoft.net>)
List pgsql-hackers
Rob <postgresql@mintsoft.net> writes:
> Basically, if a table exists with a PK which is CHAR(n) and a query is
> sent with VARCHAR or CHAR then it uses an Index Scan. If the query is
> sent with TEXT as the type then postgresql casts the column to TEXT
> (rather than the value to CHAR) and it does a Seq Scan.

Yeah, this is an artifact of the fact that text is considered a
"preferred type" so it wins out in the parser's choice of which
type to promote to.  See

https://www.postgresql.org/docs/current/typeconv-oper.html

> I guess the root question is: is TEXT supposed to be identical to 
> VARCHAR in all scenarios?

It's not for this purpose, because varchar isn't a preferred type.

FWIW, my recommendation for this sort of thing is almost always
to not use CHAR(n).  The use-case for that datatype pretty much
disappeared with the last IBM Model 029 card punch.

            regards, tom lane



pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Race conditions with checkpointer and shutdown
Next
From: Ashwin Agrawal
Date:
Subject: Re: Race conditions with checkpointer and shutdown