SlideShare a Scribd company logo
www.postgrespro.ru
In-core compression:
how to shrink your database
size in several times
Aleksander Alekseev
Anastasia Lubennikova
Agenda
●
What does Postgres store?
• A couple of words about storage internals
●
Check list for your schema
• A set of tricks to optimize database size
●
In-core block level compression
• Out-of-box feature of Postgres Pro EE
●
ZSON
• Extension for transparent JSONB compression
What this talk doesn’t cover
●
MVCC bloat
• Tune autovacuum properly
• Drop unused indexes
• Use pg_repack
• Try pg_squeeze
●
Catalog bloat
• Create less temporary tables
●
WAL-log size
• Enable wal_compression
●
FS level compression
• ZFS, btrfs, etc
Data layout
Empty tables are not that empty
●
Imagine we have no data
create table tbl();
insert into tbl select from generate_series(0,1e07);
select pg_size_pretty(pg_relation_size('tbl'));
pg_size_pretty
---------------
???
Empty tables are not that empty
●
Imagine we have no data
create table tbl();
insert into tbl select from generate_series(0,1e07);
select pg_size_pretty(pg_relation_size('tbl'));
pg_size_pretty
---------------
268 MB
Meta information
db=# select * from heap_page_items(get_raw_page('tbl',0));
-[ RECORD 1 ]-------------------
lp | 1
lp_off | 8160
lp_flags | 1
lp_len | 32
t_xmin | 720
t_xmax | 0
t_field3 | 0
t_ctid | (0,1)
t_infomask2 | 2
t_infomask | 2048
t_hoff | 24
t_bits |
t_oid |
t_data |
Order matters
●
Attributes must be aligned inside the row
Safe up to 20% of space.
create table bad (i1 int, b1 bigint, i1 int);
create table good (i1 int, i1 int, b1 bigint);
NULLs for free*
●
Tuple header size: 23 bytes
●
With alignment: 24 bytes
●
Null mask is placed right after a header
●
Result: up to 8 nullable columns cost nothing
●
Also: buy one NULL, get 7 NULLs for free! (plus
alignment)
* not actually free
Alignment and B-tree
All index entries are 8 bytes aligned
create table good (i1 int, i1 int, b1 bigint);
create index idx on good (i1);
create index idx_multi on good (i1, i1);
create index idx_big on good (b1);
Alignment and B-tree
●
It cannot be smaller, but it can keep more data
●
Covering indexes* may come in handy here
• CREATE INDEX tbl_pkey (i1) INCLUDE (i2)
●
+ It enables index-only scan for READ queries
●
– It disables HOT updates for WRITE queries
*Already in PostgresPro, hopefully will be in PostgreSQL 10
Use proper data types
CREATE TABLE b AS
SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::bytea;
select lp_len, t_data from heap_page_items(get_raw_page('b',0));
lp_len | t_data
-------+---------------------------------------------------------
61 |
x4b61306565626339392d396330622d346566382d626236642d3662623962643
33830613131
CREATE TABLE u AS
SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid;
select lp_len, t_data from heap_page_items(get_raw_page('u',0));
lp_len | t_data
-------+------------------------------------
40 | xa0eebc999c0b4ef8bb6d6bb9bd380a11
Timetz vs timestamptz
●
timetz: int64 (timestamp) + int32 (timezone)
●
timestamptz: always an int64 in UTC
●
Result: time takes more space then date + time
TOAST
●
Splitting of oversized attributes with an optional
compression
• PGLZ: more or less same (speed, ratio) as ZLIB
• Heuristic: if beginning of the attribute is compressed
well then compress it
• Works out of the box for large string-like attributes
Know your data and your database
●
Use proper data types
●
Reorder columns to avoid padding
●
Pack data into bigger chunks to trigger TOAST
Know your data and your database
●
Use proper data types
●
Reorder columns to avoid padding
●
Pack data into bigger chunks to trigger TOAST
CFS
●
CFS — «compressed file system»
• Out of box (PostgresPro Enterprise Edition)
decompress
compress
Layout changes
●
Postgres layout ●
CFS layout
CFS usage
CREATE TABLESPACE cfs LOCATION
'/home/tblspc/cfs' with (compression=true);
SET default_tablespace=cfs;
CREATE TABLE tbl (x int);
INSERT INTO tbl VALUES (generate_series(1, 1000000));
UPDATE tbl set x=x+1;
SELECT cfs_start_gc(4); /* 4 — number of workers */
Pgbench performance
●
pgbench -s 1000 -i
• 2 times slower
• 98 sec → 214 sec
●
database size
• 18 times smaller
• 15334 MB → 827 MB
●
pgbench -c 10 -j 10 -t 10000
• 5% better
• 3904 TPS → 4126 TPS
Always doubt benchmarks
db=# select * from pgbench_accounts;
-[ RECORD1 ]--------------------------------------
aid | 1
bid | 1
abalance | 0
filler |
db=# d pgbench_accounts
Table "public.pgbench_accounts"
Column | Type | Collation | Nullable | Default
----------+---------------+-----------+----------+---------
aid | integer | | not null |
bid | integer | | |
abalance | integer | | |
filler | character(84) | | |
db=# select pg_column_size(filler) from pgbench_accounts;
pg_column_size
----------------
85
Comparison of
compression algoritms
Configuration Size (Gb) Time (sec)
no compression 15.31 92
snappy 5.18 99
lz4 4.12 91
postgres internal lz 3.89 214
lzfse 2.80 1099
zlib (best speed) 2.43 191
zlib (default level) 2.37 284
zstd 1.69 125
pgbench -i -s 1000
CFS: I/O usage
CPU usage
CFS pros
●
Good compression rate:
• All information on the page is compressed including
headers
●
Better locality:
• CFS always writes new pages sequentially
●
Minimal changes in Postgres core:
• CFS works at the lowest level
●
Flexibility:
• Easy to use various compression algorithms
CFS cons
●
Shared buffers utilization:
• Buffer cache keeps pages uncompressed
●
Inefficient WAL and replication:
• Replica has to perform compression and GC itself
●
Fragmentation
• CFS needs its own garbage collector
ZSON
●
An extension for transparent JSONB compression
●
A dictionary of common strings is created based
on your data (re-learning is also supported)
●
This dictionary is used to replace strings to 16 bit
codes
●
Data is compressed in memory and on the disk
●
In some cases it gives 10% more TPS
●
●
https://github.com/postgrespro/zson
How JSONB looks like
JSONB Problems
●
Redundancy
●
Disk space
●
Memory
●
=> IO & TPS
The Idea
●
Step 1 — replace common strings to 16 bit codes
●
Step 2 — compress using PGLZ as usual
zson_learn
zson_learn(
tables_and_columns text[][],
max_examples int default 10000,
min_length int default 2,
max_length int default 128,
min_count int default 2
)
Example:
select zson_learn('{{"table1", "col1"}, {"table2", "col2"}}');
zson_extract_strings
Other ZSON internals
Encoding
// VARHDRSZ
// zson_version [uint8]
// dict_version [uint32]
// decoded_size [uint32]
// hint [uint8 x PGLZ_HINT_SIZE]
// {
//skip_bytes [uint8]
//... skip_bytes bytes ...
//string_code [uint16], 0 = no_string
// } *
Thank you for your attention!
Any questions?
●
https://postgrespro.com/
●
a.lubennikova@postgrespro.ru
●
a.alekseev@postgrespro.ru
Bonus
Slides!
CFS parameters
●
cfs_gc_workers = 1
• Number of background workers performing CFS
garbage collection
●
cfs_gc_threashold = 50%
• Percent of garbage in the file after which
defragmentation begins
●
cfs_gc_period = 5 seconds
• Interval between CFS garbage collection iterations
●
cfs_gc_delay = 0 milliseconds
• Delay between files defragmentation
CFS: Compression ratio
In-memory dictionary
Ad

More Related Content

What's hot (20)

PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 TaipeiPostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
Satoshi Nagayasu
 
PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs
PGConf APAC
 
PostgreSQL Replication Tutorial
PostgreSQL Replication TutorialPostgreSQL Replication Tutorial
PostgreSQL Replication Tutorial
Hans-Jürgen Schönig
 
Case Studies on PostgreSQL
Case Studies on PostgreSQLCase Studies on PostgreSQL
Case Studies on PostgreSQL
InMobi Technology
 
100500 способов кэширования в Oracle Database или как достичь максимальной ск...
100500 способов кэширования в Oracle Database или как достичь максимальной ск...100500 способов кэширования в Oracle Database или как достичь максимальной ск...
100500 способов кэширования в Oracle Database или как достичь максимальной ск...
Ontico
 
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
Ontico
 
Hypertable Berlin Buzzwords
Hypertable Berlin BuzzwordsHypertable Berlin Buzzwords
Hypertable Berlin Buzzwords
hypertable
 
TokuDB internals / Лесин Владислав (Percona)
TokuDB internals / Лесин Владислав (Percona)TokuDB internals / Лесин Владислав (Percona)
TokuDB internals / Лесин Владислав (Percona)
Ontico
 
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Altinity Ltd
 
Pgcenter overview
Pgcenter overviewPgcenter overview
Pgcenter overview
Alexey Lesovsky
 
XtraDB 5.7: key performance algorithms
XtraDB 5.7: key performance algorithmsXtraDB 5.7: key performance algorithms
XtraDB 5.7: key performance algorithms
Laurynas Biveinis
 
Out of the box replication in postgres 9.4
Out of the box replication in postgres 9.4Out of the box replication in postgres 9.4
Out of the box replication in postgres 9.4
Denish Patel
 
Page compression. PGCON_2016
Page compression. PGCON_2016Page compression. PGCON_2016
Page compression. PGCON_2016
Anastasia Lubennikova
 
PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12
Andrew Dunstan
 
Apache tajo configuration
Apache tajo configurationApache tajo configuration
Apache tajo configuration
Jihoon Son
 
Pgbr 2013 postgres on aws
Pgbr 2013   postgres on awsPgbr 2013   postgres on aws
Pgbr 2013 postgres on aws
Emanuel Calvo
 
PostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication CheatsheetPostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication Cheatsheet
Alexey Lesovsky
 
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).
Alexey Lesovsky
 
Building Spark as Service in Cloud
Building Spark as Service in CloudBuilding Spark as Service in Cloud
Building Spark as Service in Cloud
InMobi Technology
 
XtraDB 5.6 and 5.7: Key Performance Algorithms
XtraDB 5.6 and 5.7: Key Performance AlgorithmsXtraDB 5.6 and 5.7: Key Performance Algorithms
XtraDB 5.6 and 5.7: Key Performance Algorithms
Laurynas Biveinis
 
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 TaipeiPostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
Satoshi Nagayasu
 
PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs
PGConf APAC
 
100500 способов кэширования в Oracle Database или как достичь максимальной ск...
100500 способов кэширования в Oracle Database или как достичь максимальной ск...100500 способов кэширования в Oracle Database или как достичь максимальной ск...
100500 способов кэширования в Oracle Database или как достичь максимальной ск...
Ontico
 
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
Ontico
 
Hypertable Berlin Buzzwords
Hypertable Berlin BuzzwordsHypertable Berlin Buzzwords
Hypertable Berlin Buzzwords
hypertable
 
TokuDB internals / Лесин Владислав (Percona)
TokuDB internals / Лесин Владислав (Percona)TokuDB internals / Лесин Владислав (Percona)
TokuDB internals / Лесин Владислав (Percona)
Ontico
 
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Analytics at Speed: Introduction to ClickHouse and Common Use Cases. By Mikha...
Altinity Ltd
 
XtraDB 5.7: key performance algorithms
XtraDB 5.7: key performance algorithmsXtraDB 5.7: key performance algorithms
XtraDB 5.7: key performance algorithms
Laurynas Biveinis
 
Out of the box replication in postgres 9.4
Out of the box replication in postgres 9.4Out of the box replication in postgres 9.4
Out of the box replication in postgres 9.4
Denish Patel
 
PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12
Andrew Dunstan
 
Apache tajo configuration
Apache tajo configurationApache tajo configuration
Apache tajo configuration
Jihoon Son
 
Pgbr 2013 postgres on aws
Pgbr 2013   postgres on awsPgbr 2013   postgres on aws
Pgbr 2013 postgres on aws
Emanuel Calvo
 
PostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication CheatsheetPostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication Cheatsheet
Alexey Lesovsky
 
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).
Alexey Lesovsky
 
Building Spark as Service in Cloud
Building Spark as Service in CloudBuilding Spark as Service in Cloud
Building Spark as Service in Cloud
InMobi Technology
 
XtraDB 5.6 and 5.7: Key Performance Algorithms
XtraDB 5.6 and 5.7: Key Performance AlgorithmsXtraDB 5.6 and 5.7: Key Performance Algorithms
XtraDB 5.6 and 5.7: Key Performance Algorithms
Laurynas Biveinis
 

Similar to In-core compression: how to shrink your database size in several times (20)

PgconfSV compression
PgconfSV compressionPgconfSV compression
PgconfSV compression
Anastasia Lubennikova
 
10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL
Satoshi Nagayasu
 
Etl confessions pg conf us 2017
Etl confessions   pg conf us 2017Etl confessions   pg conf us 2017
Etl confessions pg conf us 2017
Corey Huinker
 
Loadays MySQL
Loadays MySQLLoadays MySQL
Loadays MySQL
lefredbe
 
PostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major FeaturesPostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major Features
InMobi Technology
 
SQLDAY 2023 Chodkowski Adrian Databricks Performance Tuning
SQLDAY 2023 Chodkowski Adrian Databricks Performance TuningSQLDAY 2023 Chodkowski Adrian Databricks Performance Tuning
SQLDAY 2023 Chodkowski Adrian Databricks Performance Tuning
SeeQuality.net
 
Db2 performance tuning for dummies
Db2 performance tuning for dummiesDb2 performance tuning for dummies
Db2 performance tuning for dummies
Angel Dueñas Neyra
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQL
Morgan Tocker
 
Percona FT / TokuDB
Percona FT / TokuDBPercona FT / TokuDB
Percona FT / TokuDB
Vadim Tkachenko
 
The Accidental DBA
The Accidental DBAThe Accidental DBA
The Accidental DBA
PostgreSQL Experts, Inc.
 
Caching in
Caching inCaching in
Caching in
RichardWarburton
 
MySQL 5.7 in a Nutshell
MySQL 5.7 in a NutshellMySQL 5.7 in a Nutshell
MySQL 5.7 in a Nutshell
Emily Ikuta
 
12 in 12 – A closer look at twelve or so new things in Postgres 12
12 in 12 – A closer look at twelve or so new things in Postgres 1212 in 12 – A closer look at twelve or so new things in Postgres 12
12 in 12 – A closer look at twelve or so new things in Postgres 12
BasilBourque1
 
Grokking TechTalk #20: PostgreSQL Internals 101
Grokking TechTalk #20: PostgreSQL Internals 101Grokking TechTalk #20: PostgreSQL Internals 101
Grokking TechTalk #20: PostgreSQL Internals 101
Grokking VN
 
Big data should be simple
Big data should be simpleBig data should be simple
Big data should be simple
Dori Waldman
 
21st Athens Big Data Meetup - 1st Talk - Fast and simple data exploration wit...
21st Athens Big Data Meetup - 1st Talk - Fast and simple data exploration wit...21st Athens Big Data Meetup - 1st Talk - Fast and simple data exploration wit...
21st Athens Big Data Meetup - 1st Talk - Fast and simple data exploration wit...
Athens Big Data
 
Oracle Database 12.1.0.2 New Features
Oracle Database 12.1.0.2 New FeaturesOracle Database 12.1.0.2 New Features
Oracle Database 12.1.0.2 New Features
Alex Zaballa
 
VLDB Administration Strategies
VLDB Administration StrategiesVLDB Administration Strategies
VLDB Administration Strategies
Murilo Miranda
 
Postgresql Database Administration Basic - Day2
Postgresql  Database Administration Basic  - Day2Postgresql  Database Administration Basic  - Day2
Postgresql Database Administration Basic - Day2
PoguttuezhiniVP
 
Changing your huge table's data types in production
Changing your huge table's data types in productionChanging your huge table's data types in production
Changing your huge table's data types in production
Jimmy Angelakos
 
10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL
Satoshi Nagayasu
 
Etl confessions pg conf us 2017
Etl confessions   pg conf us 2017Etl confessions   pg conf us 2017
Etl confessions pg conf us 2017
Corey Huinker
 
Loadays MySQL
Loadays MySQLLoadays MySQL
Loadays MySQL
lefredbe
 
PostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major FeaturesPostgreSQL 9.5 - Major Features
PostgreSQL 9.5 - Major Features
InMobi Technology
 
SQLDAY 2023 Chodkowski Adrian Databricks Performance Tuning
SQLDAY 2023 Chodkowski Adrian Databricks Performance TuningSQLDAY 2023 Chodkowski Adrian Databricks Performance Tuning
SQLDAY 2023 Chodkowski Adrian Databricks Performance Tuning
SeeQuality.net
 
Db2 performance tuning for dummies
Db2 performance tuning for dummiesDb2 performance tuning for dummies
Db2 performance tuning for dummies
Angel Dueñas Neyra
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQL
Morgan Tocker
 
MySQL 5.7 in a Nutshell
MySQL 5.7 in a NutshellMySQL 5.7 in a Nutshell
MySQL 5.7 in a Nutshell
Emily Ikuta
 
12 in 12 – A closer look at twelve or so new things in Postgres 12
12 in 12 – A closer look at twelve or so new things in Postgres 1212 in 12 – A closer look at twelve or so new things in Postgres 12
12 in 12 – A closer look at twelve or so new things in Postgres 12
BasilBourque1
 
Grokking TechTalk #20: PostgreSQL Internals 101
Grokking TechTalk #20: PostgreSQL Internals 101Grokking TechTalk #20: PostgreSQL Internals 101
Grokking TechTalk #20: PostgreSQL Internals 101
Grokking VN
 
Big data should be simple
Big data should be simpleBig data should be simple
Big data should be simple
Dori Waldman
 
21st Athens Big Data Meetup - 1st Talk - Fast and simple data exploration wit...
21st Athens Big Data Meetup - 1st Talk - Fast and simple data exploration wit...21st Athens Big Data Meetup - 1st Talk - Fast and simple data exploration wit...
21st Athens Big Data Meetup - 1st Talk - Fast and simple data exploration wit...
Athens Big Data
 
Oracle Database 12.1.0.2 New Features
Oracle Database 12.1.0.2 New FeaturesOracle Database 12.1.0.2 New Features
Oracle Database 12.1.0.2 New Features
Alex Zaballa
 
VLDB Administration Strategies
VLDB Administration StrategiesVLDB Administration Strategies
VLDB Administration Strategies
Murilo Miranda
 
Postgresql Database Administration Basic - Day2
Postgresql  Database Administration Basic  - Day2Postgresql  Database Administration Basic  - Day2
Postgresql Database Administration Basic - Day2
PoguttuezhiniVP
 
Changing your huge table's data types in production
Changing your huge table's data types in productionChanging your huge table's data types in production
Changing your huge table's data types in production
Jimmy Angelakos
 
Ad

More from Aleksander Alekseev (13)

Growing up new PostgreSQL developers (pgcon.org 2018)
Growing up new PostgreSQL developers (pgcon.org 2018)Growing up new PostgreSQL developers (pgcon.org 2018)
Growing up new PostgreSQL developers (pgcon.org 2018)
Aleksander Alekseev
 
PostgreSQL and Compressed Documents (pgconf.ru 2018)
PostgreSQL and Compressed Documents (pgconf.ru 2018)PostgreSQL and Compressed Documents (pgconf.ru 2018)
PostgreSQL and Compressed Documents (pgconf.ru 2018)
Aleksander Alekseev
 
PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)
PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)
PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)
Aleksander Alekseev
 
Data recovery using pg_filedump
Data recovery using pg_filedumpData recovery using pg_filedump
Data recovery using pg_filedump
Aleksander Alekseev
 
Full Text Search in PostgreSQL
Full Text Search in PostgreSQLFull Text Search in PostgreSQL
Full Text Search in PostgreSQL
Aleksander Alekseev
 
pg_filedump
pg_filedumppg_filedump
pg_filedump
Aleksander Alekseev
 
Quality Assurance in PostgreSQL
Quality Assurance in PostgreSQLQuality Assurance in PostgreSQL
Quality Assurance in PostgreSQL
Aleksander Alekseev
 
ZSON, или прозрачное сжатие JSON
ZSON, или прозрачное сжатие JSONZSON, или прозрачное сжатие JSON
ZSON, или прозрачное сжатие JSON
Aleksander Alekseev
 
Профилирование кода на C/C++ в *nix системах
Профилирование кода на C/C++ в *nix системахПрофилирование кода на C/C++ в *nix системах
Профилирование кода на C/C++ в *nix системах
Aleksander Alekseev
 
Новые технологии репликации данных в PostgreSQL - Александр Алексеев
Новые технологии репликации данных в PostgreSQL - Александр АлексеевНовые технологии репликации данных в PostgreSQL - Александр Алексеев
Новые технологии репликации данных в PostgreSQL - Александр Алексеев
Aleksander Alekseev
 
Haskell - это просто - Александр Алексеев
Haskell - это просто - Александр АлексеевHaskell - это просто - Александр Алексеев
Haskell - это просто - Александр Алексеев
Aleksander Alekseev
 
Работа с Akka Cluster - Александр Алексеев
Работа с Akka Cluster - Александр АлексеевРабота с Akka Cluster - Александр Алексеев
Работа с Akka Cluster - Александр Алексеев
Aleksander Alekseev
 
Функциональное программирование - Александр Алексеев
Функциональное программирование - Александр АлексеевФункциональное программирование - Александр Алексеев
Функциональное программирование - Александр Алексеев
Aleksander Alekseev
 
Growing up new PostgreSQL developers (pgcon.org 2018)
Growing up new PostgreSQL developers (pgcon.org 2018)Growing up new PostgreSQL developers (pgcon.org 2018)
Growing up new PostgreSQL developers (pgcon.org 2018)
Aleksander Alekseev
 
PostgreSQL and Compressed Documents (pgconf.ru 2018)
PostgreSQL and Compressed Documents (pgconf.ru 2018)PostgreSQL and Compressed Documents (pgconf.ru 2018)
PostgreSQL and Compressed Documents (pgconf.ru 2018)
Aleksander Alekseev
 
PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)
PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)
PostgreSQL Sharding and HA: Theory and Practice (PGConf.ASIA 2017)
Aleksander Alekseev
 
ZSON, или прозрачное сжатие JSON
ZSON, или прозрачное сжатие JSONZSON, или прозрачное сжатие JSON
ZSON, или прозрачное сжатие JSON
Aleksander Alekseev
 
Профилирование кода на C/C++ в *nix системах
Профилирование кода на C/C++ в *nix системахПрофилирование кода на C/C++ в *nix системах
Профилирование кода на C/C++ в *nix системах
Aleksander Alekseev
 
Новые технологии репликации данных в PostgreSQL - Александр Алексеев
Новые технологии репликации данных в PostgreSQL - Александр АлексеевНовые технологии репликации данных в PostgreSQL - Александр Алексеев
Новые технологии репликации данных в PostgreSQL - Александр Алексеев
Aleksander Alekseev
 
Haskell - это просто - Александр Алексеев
Haskell - это просто - Александр АлексеевHaskell - это просто - Александр Алексеев
Haskell - это просто - Александр Алексеев
Aleksander Alekseev
 
Работа с Akka Cluster - Александр Алексеев
Работа с Akka Cluster - Александр АлексеевРабота с Akka Cluster - Александр Алексеев
Работа с Akka Cluster - Александр Алексеев
Aleksander Alekseev
 
Функциональное программирование - Александр Алексеев
Функциональное программирование - Александр АлексеевФункциональное программирование - Александр Алексеев
Функциональное программирование - Александр Алексеев
Aleksander Alekseev
 
Ad

Recently uploaded (20)

Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 

In-core compression: how to shrink your database size in several times

  • 1. www.postgrespro.ru In-core compression: how to shrink your database size in several times Aleksander Alekseev Anastasia Lubennikova
  • 2. Agenda ● What does Postgres store? • A couple of words about storage internals ● Check list for your schema • A set of tricks to optimize database size ● In-core block level compression • Out-of-box feature of Postgres Pro EE ● ZSON • Extension for transparent JSONB compression
  • 3. What this talk doesn’t cover ● MVCC bloat • Tune autovacuum properly • Drop unused indexes • Use pg_repack • Try pg_squeeze ● Catalog bloat • Create less temporary tables ● WAL-log size • Enable wal_compression ● FS level compression • ZFS, btrfs, etc
  • 5. Empty tables are not that empty ● Imagine we have no data create table tbl(); insert into tbl select from generate_series(0,1e07); select pg_size_pretty(pg_relation_size('tbl')); pg_size_pretty --------------- ???
  • 6. Empty tables are not that empty ● Imagine we have no data create table tbl(); insert into tbl select from generate_series(0,1e07); select pg_size_pretty(pg_relation_size('tbl')); pg_size_pretty --------------- 268 MB
  • 7. Meta information db=# select * from heap_page_items(get_raw_page('tbl',0)); -[ RECORD 1 ]------------------- lp | 1 lp_off | 8160 lp_flags | 1 lp_len | 32 t_xmin | 720 t_xmax | 0 t_field3 | 0 t_ctid | (0,1) t_infomask2 | 2 t_infomask | 2048 t_hoff | 24 t_bits | t_oid | t_data |
  • 8. Order matters ● Attributes must be aligned inside the row Safe up to 20% of space. create table bad (i1 int, b1 bigint, i1 int); create table good (i1 int, i1 int, b1 bigint);
  • 9. NULLs for free* ● Tuple header size: 23 bytes ● With alignment: 24 bytes ● Null mask is placed right after a header ● Result: up to 8 nullable columns cost nothing ● Also: buy one NULL, get 7 NULLs for free! (plus alignment) * not actually free
  • 10. Alignment and B-tree All index entries are 8 bytes aligned create table good (i1 int, i1 int, b1 bigint); create index idx on good (i1); create index idx_multi on good (i1, i1); create index idx_big on good (b1);
  • 11. Alignment and B-tree ● It cannot be smaller, but it can keep more data ● Covering indexes* may come in handy here • CREATE INDEX tbl_pkey (i1) INCLUDE (i2) ● + It enables index-only scan for READ queries ● – It disables HOT updates for WRITE queries *Already in PostgresPro, hopefully will be in PostgreSQL 10
  • 12. Use proper data types CREATE TABLE b AS SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::bytea; select lp_len, t_data from heap_page_items(get_raw_page('b',0)); lp_len | t_data -------+--------------------------------------------------------- 61 | x4b61306565626339392d396330622d346566382d626236642d3662623962643 33830613131 CREATE TABLE u AS SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid; select lp_len, t_data from heap_page_items(get_raw_page('u',0)); lp_len | t_data -------+------------------------------------ 40 | xa0eebc999c0b4ef8bb6d6bb9bd380a11
  • 13. Timetz vs timestamptz ● timetz: int64 (timestamp) + int32 (timezone) ● timestamptz: always an int64 in UTC ● Result: time takes more space then date + time
  • 14. TOAST ● Splitting of oversized attributes with an optional compression • PGLZ: more or less same (speed, ratio) as ZLIB • Heuristic: if beginning of the attribute is compressed well then compress it • Works out of the box for large string-like attributes
  • 15. Know your data and your database ● Use proper data types ● Reorder columns to avoid padding ● Pack data into bigger chunks to trigger TOAST
  • 16. Know your data and your database ● Use proper data types ● Reorder columns to avoid padding ● Pack data into bigger chunks to trigger TOAST
  • 17. CFS ● CFS — «compressed file system» • Out of box (PostgresPro Enterprise Edition) decompress compress
  • 19. CFS usage CREATE TABLESPACE cfs LOCATION '/home/tblspc/cfs' with (compression=true); SET default_tablespace=cfs; CREATE TABLE tbl (x int); INSERT INTO tbl VALUES (generate_series(1, 1000000)); UPDATE tbl set x=x+1; SELECT cfs_start_gc(4); /* 4 — number of workers */
  • 20. Pgbench performance ● pgbench -s 1000 -i • 2 times slower • 98 sec → 214 sec ● database size • 18 times smaller • 15334 MB → 827 MB ● pgbench -c 10 -j 10 -t 10000 • 5% better • 3904 TPS → 4126 TPS
  • 21. Always doubt benchmarks db=# select * from pgbench_accounts; -[ RECORD1 ]-------------------------------------- aid | 1 bid | 1 abalance | 0 filler | db=# d pgbench_accounts Table "public.pgbench_accounts" Column | Type | Collation | Nullable | Default ----------+---------------+-----------+----------+--------- aid | integer | | not null | bid | integer | | | abalance | integer | | | filler | character(84) | | | db=# select pg_column_size(filler) from pgbench_accounts; pg_column_size ---------------- 85
  • 22. Comparison of compression algoritms Configuration Size (Gb) Time (sec) no compression 15.31 92 snappy 5.18 99 lz4 4.12 91 postgres internal lz 3.89 214 lzfse 2.80 1099 zlib (best speed) 2.43 191 zlib (default level) 2.37 284 zstd 1.69 125 pgbench -i -s 1000
  • 25. CFS pros ● Good compression rate: • All information on the page is compressed including headers ● Better locality: • CFS always writes new pages sequentially ● Minimal changes in Postgres core: • CFS works at the lowest level ● Flexibility: • Easy to use various compression algorithms
  • 26. CFS cons ● Shared buffers utilization: • Buffer cache keeps pages uncompressed ● Inefficient WAL and replication: • Replica has to perform compression and GC itself ● Fragmentation • CFS needs its own garbage collector
  • 27. ZSON ● An extension for transparent JSONB compression ● A dictionary of common strings is created based on your data (re-learning is also supported) ● This dictionary is used to replace strings to 16 bit codes ● Data is compressed in memory and on the disk ● In some cases it gives 10% more TPS ● ● https://github.com/postgrespro/zson
  • 30. The Idea ● Step 1 — replace common strings to 16 bit codes ● Step 2 — compress using PGLZ as usual
  • 31. zson_learn zson_learn( tables_and_columns text[][], max_examples int default 10000, min_length int default 2, max_length int default 128, min_count int default 2 ) Example: select zson_learn('{{"table1", "col1"}, {"table2", "col2"}}');
  • 34. Encoding // VARHDRSZ // zson_version [uint8] // dict_version [uint32] // decoded_size [uint32] // hint [uint8 x PGLZ_HINT_SIZE] // { //skip_bytes [uint8] //... skip_bytes bytes ... //string_code [uint16], 0 = no_string // } *
  • 35. Thank you for your attention! Any questions? ● https://postgrespro.com/ ● [email protected][email protected]
  • 37. CFS parameters ● cfs_gc_workers = 1 • Number of background workers performing CFS garbage collection ● cfs_gc_threashold = 50% • Percent of garbage in the file after which defragmentation begins ● cfs_gc_period = 5 seconds • Interval between CFS garbage collection iterations ● cfs_gc_delay = 0 milliseconds • Delay between files defragmentation