Search code examples
postgresqldatabase-designvarchar

Postgres CHAR vs VARCHAR crossover variation


In Postgres (or pretty much any SQL database, but I'm asking specifically about Postgres), if you know the content of the field will always be exactly 20 characters, it's obviously more efficient to make it CHAR than VARCHAR.

Suppose it varies somewhat. Not enormously, or it would obviously be better to use VARCHAR. Say the maximum is 20, but the average is about 10. In that case, is CHAR still more efficient?

In general, how much variation does there need to be, before it starts being more efficient to use VARCHAR?


Solution

  • Even if the column has always exactly 20 characters, varchar(20) won't be any less efficient than char(20) in PostgreSQL. Never use char(n), because it has strange semantics, and you won't gain anything. See the "tip" in the documentation.