Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying ASCII columns in a UTF-8 PostgreSQL database

I have a PostgreSQL database with UTF8 encoding and LC_* en_US.UTF8. The database stores text columns in many different languages.

On some columns however, I am 100% sure there will never be any special characters, i.e. ISO country & currency codes.

I've tried doing something like:

"countryCode" char(3) CHARACTER SET "C" NOT NULL

and

 "countryCode" char(3) CHARACTER SET "SQL_ASCII" NOT NULL

but this comes back with the error

ERROR: type "pg_catalog.bpchar_C" does not exist
ERROR: type "pg_catalog.bpchar_SQL_ASCII" does not exist

What am I doing wrong?

More importantly, should I even bother with this? I'm coming from a MySQL background where doing this was a performance and space enhancement, is this also the case with PostgreSQL?

TIA

like image 556
ianaré Avatar asked Apr 08 '26 10:04

ianaré


1 Answers

Honestly, I do not see the purpose of such settings, as:

  • as @JoachimSauer mentions, ASCII subset in the UTF-8 encoding will occupy exactly the same number of bytes, as that was the main point of inventing UTF-8: keep ASCII unchanged. Therefore I see no size benefits;
  • all software that is capable of processing strings in different encoding will use a common internal encoding, which is UTF-8 by default for PostgreSQL nowadays. When some textual data comes in to the processing stage, database will convert it into the internal encoding if encodings do not match. Therefore, if you specify some columns as being non-UTF8, this will lead to the extra processing of the data, thus you will loose some cycles (don't think it will be notable performance hit though).

Given there's no space benefits and there's a potential performance hit, I think it is better to leave things as they are, i.e. keep all columns in the database's default encoding.

I think for the same arguments PostgreSQL do not allow to specify encodings for individual objects within the database. Character Set and Locale are set on the per-database level.

like image 88
vyegorov Avatar answered Apr 10 '26 01:04

vyegorov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!