Skip to content

Commit 614c7e4

Browse files
Ivan VakhrushevIvan Vakhrushev
Ivan Vakhrushev
authored and
Ivan Vakhrushev
committed
Added demo for new article
1 parent ee5a2b5 commit 614c7e4

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.idea/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
create table if not exists test
2+
(
3+
id bigserial primary key,
4+
fld varchar(255),
5+
mark varchar(255),
6+
nil varchar(255)
7+
);
8+
9+
insert into test
10+
select data.id, case when data.id % 2 = 0 then now()::text else null end, case when data.id % 2 = 0 then 'test_string'::text else null end, null
11+
from generate_series(1, 100000) as data(id);
12+
13+
create index if not exists i_test_fld_with_nulls on test (fld);
14+
create index if not exists i_test_fld_without_nulls on test (fld) where fld is not null;
15+
create index if not exists i_test_mark_with_nulls on test (mark);
16+
create index if not exists i_test_mark_without_nulls on test (mark) where mark is not null;
17+
create index if not exists i_test_nil_with_nulls on test (nil);
18+
create index if not exists i_test_nil_without_nulls on test (nil) where nil is not null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# De-duplication of B-tree indexes in PostgreSQL 13
2+
3+
## Run PostgreSQL 13.2 in Docker
4+
```
5+
docker run --name postgres-13 -e POSTGRES_USER=testuser -e POSTGRES_PASSWORD=testpwd -e POSTGRES_DB=testdb -d -p 5432:5432 -v /absolute/path/to/initdb.sql:/docker-entrypoint-initdb.d/initdb.sql postgres:13.2
6+
```
7+
8+
## Run PostgreSQL 12.6 in Docker
9+
```
10+
docker run --name postgres-12 -e POSTGRES_USER=testuser -e POSTGRES_PASSWORD=testpwd -e POSTGRES_DB=testdb -d -p 6432:5432 -v /absolute/path/to/initdb.sql:/docker-entrypoint-initdb.d/initdb.sql postgres:12.6
11+
```
12+
13+
## Run psql from Docker CLI
14+
```
15+
psql -U testuser -d testdb
16+
```
17+
18+
## Get indexes size
19+
```sql
20+
select
21+
x.indrelid::regclass as table_name,
22+
x.indexrelid::regclass as index_name,
23+
pg_size_pretty(pg_relation_size(x.indexrelid)) as index_size
24+
from pg_index x
25+
join pg_stat_all_indexes psai on x.indexrelid = psai.indexrelid and psai.schemaname = 'public'
26+
order by 1,2;
27+
```
28+
29+
### PostgreSQL 12 results
30+
|table_name | index_name | index_size | percentage
31+
|-----------|---------------------------|------------|-----------
32+
|test | test_pkey | 2208 kB | -
33+
|test | i_test_fld_with_nulls | 3552 kB | -
34+
|test | i_test_fld_without_nulls | 2456 kB | 30% less
35+
|test | i_test_mark_with_nulls | 2664 kB | -
36+
|test | i_test_mark_without_nulls | 1568 kB | 41% less
37+
|test | i_test_nil_with_nulls | 2224 kB | -
38+
|test | i_test_nil_without_nulls | 8192 bytes | -
39+
40+
### PostgreSQL 13 results
41+
|table_name | index_name | index_size | percentage
42+
|-----------|---------------------------|------------|-----------
43+
|test | test_pkey | 2208 kB | -
44+
|test | i_test_fld_with_nulls | 704 kB | -
45+
|test | i_test_fld_without_nulls | 368 kB | 47% less
46+
|test | i_test_mark_with_nulls | 696 kB | -
47+
|test | i_test_mark_without_nulls | 360 kB | 48% less
48+
|test | i_test_nil_with_nulls | 696 kB | -
49+
|test | i_test_nil_without_nulls | 8192 bytes | -
Loading

0 commit comments

Comments
 (0)