

Some more details: The problem here is that PostgreSQL doesn't give any exceptions when creating indexes for text type or varchar(n) where n is greater than 2712. If you use text type without a constraint and have indexes on these columns, it is very possible that you hit this limit for some of your columns and get error when you try to insert data but with using varchar(n), you can prevent it. But, it should be pointed out that indexes in PostgreSQL has its size limit of 2712 bytes per row. Yes, they all use the same underlying type and all that. In my opinion, varchar(n) has it's own advantages.

Prepare specific test (examples) DROP TABLE IF EXISTS test ĬREATE TABLE test ( f text CHECK(char_length(f)5 AND char_length(x)<=20 AND x LIKE 'Hello%') (this answer is a Wiki, you can edit - please correct and improve!) UPDATING BENCHMARKS FOR 2016 (pg9.5+)Īnd using "pure SQL" benchmarks (without any external script)ĬREATE FUNCTION string_generator(int DEFAULT 20,int DEFAULT 10) RETURNS text AS $f$ Function based constraints or domains provide the advantage of instant increase of the length constraint, and on the basis that decreasing a string length constraint is rare, depesz concludes that one of them is usually the best choice for a length limit. It also takes a detailed look at alternate ways on constraining the length when needed. The article does detailed testing to show that the performance of inserts and selects for all 4 data types are similar.

varchar(n) – it's problematic to change the limit in live environment (requires exclusive lock while altering table).
POSTGRESQL VS ORACLE DATA TYPES PLUS
Spaces, plus it is problematic to change the limit char(n) – takes too much space when dealing with values shorter than n (pads them to n), and can lead to subtle errors because of adding trailing.There is no difference, under the hood it's all varlena ( variable length array).
