I have these two identical tables:
Table "public.region"
Column | Type | Collation | Nullable | Default
-------------+----------+-----------+----------+---------
r_regionkey | integer | | not null |
r_name | char(25) | | |
r_comment | char(152) | | |
Indexes:
"region_pkey" PRIMARY KEY, btree (r_regionkey)
and
Table "public.region2"
Column | Type | Collation | Nullable | Default
-------------+----------+-----------+----------+---------
r_regionkey | smallint | | not null |
r_name | text | | |
r_comment | text | | |
Indexes:
"region_pkey" PRIMARY KEY, btree (r_regionkey)
I am using smallint
and text
in order to save space, but weirdly this is the result:
select pg_size_pretty(pg_table_size('region'))
returns 8192 bytes
while
select pg_size_pretty(pg_table_size('region2'))
returns 48 kB
.
Why is region2
taking more space, even though I am using smallint
instead of integer
and text
instead of char(n)
?
VACUUM (FULL)
on both tables and compare again. Avoidingchar
is good, but always usebigint
for artificial primary keys.SMALLINT
is superior toBIGINT
as a PK?bigint
... So,integer
typically makes sense. (smallint
much less commonly.)