1

This is a quick question. I was curious as to whether setting the default value for a column in Rails+PostgreSQL costs additional space (versus leaving the default as nil).

I am also curious if that naturally leads to concluding whether creating an empty record using rails (full of nils) takes the same space as a row full of data would.

I would suppose this highly depends on the DB implementation, but I am only interested in PostgreSQL databases (9.1+). But I got lost when I was searching for implementation details specific to that.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
yara
  • 95
  • 1
  • 8
  • possible duplicate of [Do nullable columns occupy additional space in PostgreSQL?](http://stackoverflow.com/questions/12145772/do-nullable-columns-occupy-additional-space-in-postgresql) – Erwin Brandstetter Mar 05 '14 at 05:51

1 Answers1

0

Setting the default is just an attribute in the the catalog table pg_attributes.

But actual values in table rows generally cost additional disk space as compared to NULL. NULL values are stored in a special structure called "NULL bitmap", and require a single bit per column. Therefore, a row with NULL values typically takes less space on disk than a row with (dummy) values.

It's actually more complicated. Find details and links in these related answers:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228