[IMP] base: reduce new DB size by ordering columns

Postgres is aligning columns to 4 or 8 bytes, depending on their type.
So, consecutive fixed-length columns of differing size will be padded
with empty bytes due to the alignment requirements.

Before this patch, columns where created in their definition order. Now,
they are ordered based on their size, in order to minimize the padding.

As an example, before each row uses 36 bytes (+24b header):

       attname   |  typname  | typlen
    -------------+-----------+--------
     id          | int4      |      4
     create_uid  | int4      |      4
     create_date | timestamp |      8
     write_uid   | int4      |      4    -> 4 bytes padding
     write_date  | timestamp |      8
     active      | bool      |      1    -> 3 bytes padding

After each row uses 32 bytes (4 bytes saved per row):

       attname   |  typname  | typlen
    -------------+-----------+--------
     id          | int4      |      4
     create_uid  | int4      |      4
     write_uid   | int4      |      4
     active      | bool      |      1    -> 3 bytes padding
     create_date | timestamp |      8
     write_date  | timestamp |      8

This saving scheme applies to all rows in all tables. We save between 4
and 8 bytes per row just on the usual create_uid, create_date,
write_uid, write_date.

closes odoo/odoo#87896

Signed-off-by: Raphael Collet <rco@odoo.com>
This commit is contained in:
Fabien Pinckaers
2022-04-05 17:17:27 +02:00
committed by Raphael Collet
parent abd50a5ccb
commit e045e76e35
4 changed files with 27 additions and 10 deletions
+5 -5
View File
@@ -14,14 +14,14 @@ CREATE TABLE ir_act_client (primary key(id)) INHERITS (ir_actions);
CREATE TABLE res_users (
id serial NOT NULL,
active boolean default True,
login varchar(64) NOT NULL UNIQUE,
password varchar default null,
-- No FK references below, will be added later by ORM
-- (when the destination rows exist)
company_id integer, -- references res_company,
partner_id integer, -- references res_partner,
active boolean default True,
create_date timestamp without time zone,
login varchar(64) NOT NULL UNIQUE,
password varchar default null,
primary key(id)
);
@@ -86,11 +86,11 @@ CREATE TABLE ir_model_data (
create_date timestamp without time zone DEFAULT (now() at time zone 'UTC'),
write_date timestamp without time zone DEFAULT (now() at time zone 'UTC'),
write_uid integer,
res_id integer,
noupdate boolean DEFAULT False,
name varchar NOT NULL,
module varchar NOT NULL,
model varchar NOT NULL,
res_id integer,
primary key(id)
);
@@ -113,9 +113,9 @@ CREATE TABLE res_company (
CREATE TABLE res_partner (
id serial,
name varchar,
company_id integer,
create_date timestamp without time zone,
name varchar,
primary key(id)
);