Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do NULLs take up space within postgresql indexes?

While it is readily documented that NULLs take up only 1 bit in the postgresql tuple header for the row, how much space does NULLs take up in the INDEX of a nullable column (not the tuple, but the index)? Is it the same 1 bit within the index as in the tuple, or is it the full column data type size (EG: integer = 4 bytes)?

The context for the question is that I have a postgresql table that has 3 referencing columns (EG: foo_id, bar_id and baz_id) and for any row, only one of those columns would have a value (the other 2 columns would be NULL). However, I need all 3 columns to be indexed. Assuming that each column is an integer (4 bytes in postgresql), each row should occupy 4 bytes (for the non-null column) plus 2 bits (for the 2 null columns). However, if I were to add indexes for all 3 columns, the storage for the 3 indexes would either be 12 bytes (if the index takes up the full 4 bytes for a null value) or the same 4 bytes + 2 bits as in the tuple itself.

like image 339
archmeta Avatar asked Nov 15 '22 03:11

archmeta


1 Answers

The btree access routines use PageGetItem() to get the actual key from the index. So I believe that btree index pages use the same storage scheme as regular heap pages (including the nullability mask), they just have additional navigation information in the "special" region of the page to hold all the btree pointers.

like image 190
araqnid Avatar answered Dec 06 '22 19:12

araqnid