The character
occupies slightly more, but is correct. CPF is descriptive information that happens to be composed only of digits, one day may not even be like this. You don’t have to do calculations with it, it doesn’t quantify anything, so using a numerical type doesn’t make any sense.
See more.
This can be easily tested in the database:
SELECT pg_column_size(CHAR(11) '999999999'), pg_column_size(VARCHAR(11) '999999999'), pg_column_size(NUMERIC(9,0) '999999999');
Behold working in the SQL Fiddle. Also put on the Github for future reference.
Note that the CHAR
is what occupies most (what surprises me, this seems something bad from Postgresql), but it is the most correct semantically. Think about it, if you know that this information has 11 characters, why would you create a type that size is variable? In normal databases this should be the most economical since it does not need any control metadata, who knows why Postgresql does it. In another database can give a different result. It would not be better to use a general solution?
Interestingly use a VARCHAR
is a premature optimization.
Reading the documentation and see how much each type occupies.
– Maniero
Hello, Maniero! Thank you for your reply. His reply and that of Lacobus helped me a lot to understand, however, the latter helped me with a subtle detail, indicating the type of data
character varying
.– user86792
Oh yeah! I didn’t even think to read the documentation that’s why I posted in ptSO. /s
– user86792
@Tonymontana I set how to measure.
– Maniero