I am trying to understand how the column order minimize the table size in PostgreSQL.
Example:
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)
The tuple size:
23 byte overhead of tuple header + 1 byte for NULL bitmap, so:
24+4+4+8+8+5+7+5+5+8+1+1=80 but the actual tuple size is 82.
Is there any extra overhead of 2 byte?
I understood the example given on below link:
Calculating and saving space in PostgreSQL
If we remove the column_8 numeric(5,2)
then also the tuple size remains the same, i.e.: 82.
I have reorder the table to minimize the tuple size and gives 80.
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
Is there any suggestion for column order in PostgreSQL?