The document provides an overview of PostgreSQL performance tuning. It discusses caching, query processing internals, and optimization of storage and memory usage. Specific topics covered include the PostgreSQL configuration parameters for tuning shared buffers, work memory, and free space map settings.
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
PostgreSQL Performance Tuning
1. PostgreSQL
Performance Tuning
BRUCE MOMJIAN,
ENTERPRISEDB
September, 2007
Abstract
POSTGRESQL is an open-source, full-featured relational database.
This presentation gives an overview of POSTGRESQL performance
tuning.
http://momjian.us/presentations
4. Caches
CPU
Registers
CPU Cache
Kernel Cache
Disk Drive
PostgreSQL Performance Tuning 3
5. Cache Sizes
Storage Area Measured in
CPU registers bytes
CPU cache kilobytes
RAM megabytes
disk drives gigabytes
PostgreSQL Performance Tuning 4
6. Buffer / Disk Interaction
Postgres Postgres Postgres
Backend Backend Backend
PostgreSQL Shared Buffer Cache Write−Ahead Log
fsync
Kernel Disk Buffer Cache
fsync
Disk Blocks
PostgreSQL Performance Tuning 5
7. Memory Usage
Postgres Backend
Postgres Backend
Postgres Backend
R
PostgreSQL Shared Buffer Cache
A
Kernel Disk Buffer Cache Page Out
M
Swap
Free
Page In
Kernel
PostgreSQL Performance Tuning 6
8. Postgresql.conf Cache Parameters
# - Memory -
#shared_buffers = 1000 # min 16, at least max_connections*2, 8KB each
#work_mem = 1024 # min 64, size in KB
#maintenance_work_mem = 16384 # min 1024, size in KB
# - Free Space Map -
#max_fsm_pages = 20000 # min max_fsm_relations*16, 6 bytes each
#max_fsm_relations = 1000 # min 100, ~50 bytes each
PostgreSQL Performance Tuning 7
24. Sequential Scan
Heap
D D D D D D D D D D D D
A A A A A A A A A A A A
T T T T T T T T T T T T
A A A A A A A A A A A A
8K
PostgreSQL Performance Tuning 23
25. Btree Index Scan
Index < Key = >
< Key = >
< Key = >
Heap
D D D D D D D D D D D D
A A A A A A A A A A A A
T T T T T T T T T T T T
A A A A A A A A A A A A
PostgreSQL Performance Tuning 24
26. Bitmap Scan
Index 1 Index 2 Combined Table
col1 = ’A’ col2 = ’NS’ Index
0 0 0 ’A’ AND ’NS’
1 1 1
& =
0 1 0
1 0 0
PostgreSQL Performance Tuning 25
27. Join Methods
Nested Loop with Sequential Scan
Nested Loop with Index Scan
Merge Join
Hash Join
PostgreSQL Performance Tuning 26
28. Nested Loop Join with
Sequential Scan
Table 1 Table 2
aag aai
aay aag
aar aas
aai aar
aay
aaa
aag
No Setup Required
Used For Small Tables
PostgreSQL Performance Tuning 27
29. Nested Loop Join with
Index Scan
Table 1 Table 2
aag aai
aay aag
aar aas
aai aar
aay
aaa
Index Lookup
aag
No Setup Required
Index Must Already Exist
PostgreSQL Performance Tuning 28
30. Merge Join
Table 1 Table 2
aaa aaa
aab aab
Sorted
aac aab
Sorted
aad aac
aae
aaf
aaf
Ideal for Large Tables
An Index Can Be Used to Eliminate the Sort
PostgreSQL Performance Tuning 29
31. Hash Join
Table 1 Table 2
aay aak aas
aag
aak aam aay aar
aar
Hashed aao aaw
Must fit in Main Memory
PostgreSQL Performance Tuning 30
32. Three-Table Join Query
SELECT part.price
FROM customer, salesorder, part
WHERE customer.customer_id = salesorder.customer_id AND
salesorder.part = part.part_id
PostgreSQL Performance Tuning 31
37. Result Returned
test=> SELECT firstname
test−> FROM friend
test−> WHERE age = 33;
1: firstname (typeid = 1042, len = −1, typmod = 19, byval = f)
−−−−
1: firstname = "Sandy" (typeid = 1042, len = −1, typmod = 19, byval = f)
−−−−
firstname
−−−−−−−−−−−−−−−−−
Sandy
(1 row)
PostgreSQL Performance Tuning 36
38. VACUUM ANALYZE
test=> VACUUM ANALYZE VERBOSE customer;
INFO: vacuuming "pg_catalog.pg_depend"
INFO: index "pg_depend_depender_-
index" now contains 3616 row versions in 19 pages
DETAIL: 0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: index "pg_depend_reference_-
index" now contains 3616 row versions in 23 pages
DETAIL: 0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: "pg_depend": found 0 removable, 3616 nonremovable row ver-
sions in 25 pages
DETAIL: 0 dead row versions cannot be removed yet.
There were 9 unused item pointers.
0 pages are entirely empty.
CPU 0.00s/-1.99u sec elapsed 0.00 sec.
PostgreSQL Performance Tuning 37
51. Index Page Structure
Page Header Item Item Item
Internal
>= N
<F <N Special
Page Header Item Item Item Page Header Item Item Item
Leaf E L
A C Special G K Special
Heap M C I A G E P K W L
PostgreSQL Performance Tuning 50
52. CLUSTER
Page Header Item Item Item
Internal
>= N
<F <N Special
Page Header Item Item Item Page Header Item Item Item
Leaf E L
A C Special G K Special
Heap A C D D D E G K K L
PostgreSQL Performance Tuning 51
53. CLUSTER
test=> CREATE TABLE customer (id SERIAL, name TEXT);
NOTICE: CREATE TABLE will create implicit sequence ’customer_id_-
seq’ for SERIAL column ’customer.id’
test=> CREATE INDEX customer_id_index ON customer (id);
CREATE INDEX
test=> CLUSTER customer_id_index ON customer;
CLUSTER
PostgreSQL Performance Tuning 52
58. Manual and Automatic Vacuum
Free Space Map
Table Block # Block # Block #
DB oid
Relfilenode Table Block # Block #
Table Block # Block # Block #
Hashed
Shared Memory
PostgreSQL Performance Tuning 57
59. Vacuum Full
A A E A A A E A A A A A
Original Heap C C X C C C X C C C C C
T T P T T T P T T T T T
With Expired I I I I I I I I I I I I
V V R V V V R V V V V V
Rows Identified E E E E E E E E E E E E
A A A A A A A A A A
Move Trailing C C C C C C C C C C
T T T T T T T T T T
Rows Into Expired I I I I I I I I I I
V V V V V V V V V V
Slots E E E E E E E E E E
A A A A A A A A A A
C C C C C C C C C C
T T T T T T T T T T
Truncate File I I I I I I I I I I
V V V V V V V V V V
E E E E E E E E E E
PostgreSQL Performance Tuning 58
60. Caches
System Cache
Relation Information Cache
File Descriptor Cache
PostgreSQL Performance Tuning 59
62. Query Tips
COPY vs. INSERT
LIMIT vs. CURSOR
DELETE vs. TRUNCATE
Functional Indexes
Partial Indexes
Prepared Queries
INTERSECT vs. AND (selfjoin)
UNION vs. OR
PostgreSQL Performance Tuning 61