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
Caches
CPU
Registers
CPU Cache
Kernel Cache
Disk Drive
PostgreSQL Performance Tuning 3
Cache Sizes
Storage Area Measured in
CPU registers bytes
CPU cache kilobytes
RAM megabytes
disk drives gigabytes
PostgreSQL Performance Tuning 4
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
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
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
Internals
PostgreSQL Performance Tuning 8
SQL Query
SELECT firstname
FROM friend
WHERE age = 33;
PostgreSQL Performance Tuning 9
Query in Psql
test=> SELECT firstname
test−> FROM friend
test−> WHERE age = 33;
firstname
−−−−−−−−−−−−−−−−−
Sandy
(1 row)
PostgreSQL Performance Tuning 10
Query Processing
test=> SELECT firstname
test−> FROM friend
test−> WHERE age = 33;
[ query is processed ]
firstname
−−−−−−−−−−−−−−−−−
Sandy
(1 row)
PostgreSQL Performance Tuning 11
Query in Libpq
test=> SELECT firstname
test−> FROM friend
test−> WHERE age = 33;
Breakpoint 1, PQexec (conn=0x807a000,
query=0x8081200 "SELECT firstnamenFROM friendnWHERE age = 33;")
at fe−exec.c:1195
PostgreSQL Performance Tuning 12
Libpq
User
Terminal
PostgreSQL
Application Database
Code
Server
LIBPQ
Queries
Results
PostgreSQL Performance Tuning 13
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
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
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
Join Methods
Nested Loop with Sequential Scan
Nested Loop with Index Scan
Merge Join
Hash Join
PostgreSQL Performance Tuning 26
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
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
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
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
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
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
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
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
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
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
Range partitioning is also possible.
PostgreSQL Performance Tuning 56
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
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
Caches
System Cache
Relation Information Cache
File Descriptor Cache
PostgreSQL Performance Tuning 59
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
0 comments
Post a comment