Apparently PostgreSQL stores a couple of values in the header of each database row.
If I don’t use NULL values in that table – is the null bitmap still there?
Does defining the columns with NOT NULL make any difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s actually more complex than that.
The null bitmap needs one bit per column in the row, rounded up to full bytes. It is only there if the actual row includes at least one NULL value and is fully allocated in that case.
NOT NULLconstraints do not directly affect that. (Of course, if all fields of your table areNOT NULL, there can never be a null bitmap.)The “heap tuple header” (per row) is 23 bytes long. Actual data starts at a multiple of
MAXALIGN(Maximum data alignment) after that, which is typically 8 bytes on 64-bit OS (4 bytes on 32-bit OS). Run the following command from your PostgreSQL binary dir as root to get a definitive answer:On a typical Debian-based installation of Postgres 12 that would be:
Either way, there is one free byte between the header and the aligned start of the data, which the null bitmap can utilize. As long as your table has 8 columns or less, NULL storage is effectively absolutely free (as far as disk space is concerned).
After that, another
MAXALIGN(typically 8 bytes) is allocated for the null bitmap to cover another (typically) 64 fields. Etc.This is valid for at least versions 8.4 – 12 and most likely won’t change.