title: Calculate tuple size
body: <p>I am trying to understand how the column order minimize the table size in PostgreSQL.</p>
  
  <p>Example:</p>
  
  <pre><code>CREATE TABLE test (
   column_1 int
  ,column_2 int
  ,column_3 bigint
  ,column_4 bigint
  ,column_5 text
  ,column_6 text
  ,column_7 numeric(5,2)
  ,column_8 numeric(5,2)
  ,column_9 timestamp
  ,column_10 boolean
  ,column_11 boolean
  );
  
  INSERT INTO test
    VALUES(1,1,1,1,'test','test_1',12,12,current_timestamp,true,false);
  
  SELECT pg_column_size(test.*) FROM test;
  
   pg_column_size 
  ----------------
         82
      (1 row)
  </code></pre>
  
  <p>The tuple size:</p>
  
  <p>23 byte overhead of tuple header + 1 byte for NULL bitmap, so:</p>
  
  <p>24+4+4+8+8+5+7+5+5+8+1+1=80 but the actual tuple size is 82. </p>
  
  <p>Is there any extra overhead of 2 byte?</p>
  
  <p>I understood the example given on below link:<br>
  <a href="https://stackoverflow.com/questions/2966524/calculating-and-saving-space-in-postgresql/7431468#7431468">Calculating and saving space in PostgreSQL</a></p>
  
  <p>If we remove the <code>column_8 numeric(5,2)</code> then also the tuple size remains the same, i.e.: 82.</p>
  
  <p>I have reorder the table to minimize the tuple size and gives 80.</p>
  
  <pre><code>CREATE TABLE test (
   column_3 bigint
  ,column_4 bigint
  ,column_9 timestamp
  ,column_1 int
  ,column_2 int
  ,column_10 boolean
  ,column_11 boolean
  ,column_7 numeric(5,2)
  ,column_8 numeric(5,2)
  ,column_5 text
  ,column_6 text);
  
  INSERT INTO test
    VALUES(1,1,current_timestamp,1,1,true,false,12,12,'test','test_1');
  
  SELECT pg_column_size(test) FROM test;
  
   pg_column_size 
  ----------------
        80
  </code></pre>
  
  <p>Is there any suggestion for column order in PostgreSQL?</p>