Today we are going to discuss about character data types in PostgreSQL. On internet you can find misinformation about their performances, storage methods. But today we are going to correct all of them.
TL;DR: There is no performance difference between CHAR, VARCHAR and TEXT.
You can find PostgreSQL documentation about character datatypes from this link.
VARCHAR vs CHARACTER
Both CHARACTER(n) and VARCHAR(n) is designed to store n – length strings. When you use CHARACTER, the value will be padded with spaces to n.
When you use varchar, it only stores the data you put into column. Remaining space will not be padded with space.
VARCHAR vs TEXT
When you define a VARCHAR column you need to define it’s length, VARCHAR(n) means, your data will be limited with n characters (not bytes).
A text field is unlimited. Huh? Yes, uhmm, theorically.
Each column in PostgreSQL, is limited with 1 GB. Until hard limit of a column, you can store text into it.
VARCHAR data type is also limited with 10485760 characters:
postgres=# CREATE TABLE test (a varchar(1234567890));
ERROR: length for type varchar cannot exceed 10485760
LINE 1: CREATE TABLE test (a varchar(1234567890));
^
So you can use any of those data types without a hesitation. There is no performance difference between them. Also their ram and storage overheads are almost same.
Please leave a comment below if you have a question, or something to add. I’ll reach you as soon as possible.



Leave a Reply