Вы находитесь на странице: 1из 16

PostgreSQL:

(Sad Spirit)
borz_off@cs.msu.su

1
1.1 . . . . . . . . . . . . . .
1.2 . . . . . . . . . . . . . .
1.3 . . . . . . . . . . .

2
2
3
3

2
2.1 . . . . . . . . . . . . . . . . . . . . . . . .
2.1.1 : shared_buffers . . . . . . . . .
2.1.2 : sort_mem
2.1.3 VACUUM: vacuum_mem .
2.2 . . . . . . . . . . .
2.2.1 fsync . . . . . . . . . . . . . . .
2.2.2 . . . . . .
2.2.3 . . . . . . . . . . . . . . . . . . . . .
2.3 Free Space Map: VACUUM FULL . . . . . .
2.4 . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4.1 : effective_cache_size . . . .
2.4.2 . . . . . . . . . . . . . . . . . . . . . . .
2.5 . . . . . . . . . . . . . . . . . . . .
2.5.1 . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

3
4
4
4
5
5
5
6
6
6
7
7
7
8
8

3
3.1 . . . . . . . . . . . . . . . . . . .
3.1.1 VACUUM . . . . . . . . . . . . . . . . . . . . .
3.1.2 ANALYZE . . . . . . . . . . . . . . . . . . . . .
3.1.3 pg_autovacuum . . . . . . . . . . . . . . . . . . . . . . . .
3.1.4 REINDEX . . . . . . . . . . . . . . . . . . . . .
3.2 . . . . . . . . . . . . . . . . . . . . . .
3.2.1 EXPLAIN [ANALYZE] . . . . . . . . . . . . . .
3.2.2 . . . . . . . . . .
3.2.3 PostgreSQL . . . . . . . . . . .
3.3 . . . . . . . . . . . . . . . .
3.4 . . . . . . . . . . . . . . . .
3.4.1 SELECT max(...)/min(...) FROM < >
3.4.2 SELECT count(*) FROM < > . . . . .
3.4.3 SELECT ... WHERE ... IN (SELECT ...) . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

9
9
9
10
10
10
11
11
12
12
13
14
14
14
15

16

, , . , .
;
.
, .
, ,
?
, ? , .

PostgreSQL. , PostgreSQL , , [1] FAQ [2]. postgresqlperformance, .
, , .
: -
. , , [3], [4], [5]. ,
, [6], [7], [8] .

1.1

PostgreSQL ,

. .
:
, PostgreSQL, .
( )
, .
PostgreSQL, , . , , .. PostgreSQL :
;
, ;
;
, (,
).

1.2

PostgreSQL,
, .
.
7.1 ,
.
7.2 :
VACUUM, ;
ANALYZE,
,
;
.
7.4 (
IN/NOT IN).
,
7.2.

1.3

, , ,
. ,
, .
, ,
, ? , ,
.
, , .
, , :
.
(
).
(, , ).
.
(. 3.4).

,
. postgresql.conf .
3

2.1
2.1.1


: shared_buffers

PostgreSQL .
, , ,
.
,
. , ,
.
, ,
.

, ,
.
shared_buffers postgresql.conf.
8 . 641 , 512 .
, .
:
PostgreSQL , (.
2.4.1), . ,
,
, .
:
4 (512)
256512 : 1632 (2048
4096)
14 : 64256 (8192
32768)
.
ipcs . 1,22 , .
, ,
. , . PostgreSQL , :
http://developer.postgresql.org/docs/postgres/kernel-resources.html
2.1.2

: sort_mem

, . ,
(
).
,
. , .
1

7.4

sort_mem postgresql.conf.
1 . 1024.
24% .
.
, , ,
sort_mem .
2.1.3

VACUUM: vacuum_mem

, VACUUM.
, vacuum_mem .
vacuum_mem postgresql.conf.
1 . 8192.
.
VACUUM
/ VACUUM FULL.

2.2

PostgreSQL :
( )
, ,
.

:
. ,
: ( checkpoint_segments, 3)
, (
checkpoint_timeout, , 300).
, , .
2.2.1

fsync

No fsync.
, . :
, ,
!
, .
. . .
2.5.1 , ,
, .

2.2.2

,
2 . - .

(checkpoint_segments).
16 , .
2*checkpoint_segments+1
.
,
,
.
2.2.3

wal_sync_method , . . , ( , ).
wal_buffers ( 8 , 8 )
3 ,
. 256512 ,
.
commit_delay ( , 0 )
commit_siblings (5 ) .
commit_siblings
, commit_delay.
, , . ,
.

2.3

Free Space Map: VACUUM FULL

(
PostgreSQL) :
, , ,
, ( );
( UPDATE DELETE)
4 ( ).
, PostgreSQL
VACUUM ( 3.1.1).
2 .
checkpoint_warning ( ): , .
3
4 /

7.2 VACUUM .
7.2, VACUUM , SELECT, INSERT, UPDATE
DELETE .
VACUUM FULL.
,
, , , .
:
max_fsm_relations , .
max_fsm_pages , . ,
6 .
FSM,
VACUUM ,
VACUUM FULL, .
max_fsm_relations
.
max_fsm_pages , (UPDATE DELETE) VACUUM.

2.4
2.4.1


: effective_cache_size

PostgreSQL ,
.
effective_cache_size postgresql.conf.
8 . 1000.
1,5 , shared_buffers 32 , effective_cache_size 800 .
700 , PostgreSQL ,

merge joins. effective_cache_size 200 , ,
.
2550% 5
.
2.4.2

default_statistics_target , ANALYZE (. 3.1.2). , ,


. ALTER TABLE . . . SET STATISTICS.
5 ..

PostgreSQL ,
. ,
true/false:
stats_start_collector . , , .
stats_reset_on_server_start . .
stats_command_string
.
. ,
, ,
.
stats_row_level, stats_block_level . .
, , .
, : , (. 3.2.2).

2.5

,
. , , , .
PostgreSQL
, , . , ,
.

, , ,
noatime6 .
2.5.1


, .
7 ,
.
, ,
,
6


, , :

7

,
.
:
(!).
pg_xlog, ,
.
.
.
,
, ,
, , ,
.

:
1. , .
:
.
.
, .
2. .
3. .
4. .

3.1

, . ( cron)
.
3.1.1

VACUUM

. 7.2, :
VACUUM FULL (VACUUM 7.2) , , , .
.
VACUUM ( 7.2) ,
, (. 2.3). , , ,
, ,
. VACUUM .
9

VACUUM [FULL] ANALYZE,


, .
.
[7] [8], , (!) VACUUM ANALYZE
( ). 8
. :
, VACUUM
.
VACUUM FULL ,
.
.
3.1.2

ANALYZE

.

.
VACUUM ANALYZE. , ,
,
ANALYZE. .
3.1.3

pg_autovacuum

7.4, PostgreSQL
pg_autovacuum,
VACUUM / ANALYZE
.
VACUUM ANALYZE. ,
pg_autovacuum , .
pg_autovacuum (.
2.4.2) stats_row_level.
3.1.4

REINDEX

REINDEX .

;
.
. , ,
. PostgreSQL
9 , .
, .
8
9

, ..
7.4

10

- , REINDEX. :
REINDEX, VACUUM FULL, , , .

3.2

, . , , , . .
, , :
, , . , , .
. , .
, ,
, , ,
.
3.2.1

EXPLAIN [ANALYZE]

EXPLAIN [] , PostgreSQL . EXPLAIN ANALYZE []


10 , .
, .
:
(seq scan).

(nested loop).
EXPLAIN ANALYZE:
?
,
.
, . , , -
, - ,
,
. 80% ,
.
EXPLAIN ANALYZE ,
. ,
SET enable_seqscan=false;
10

EXPLAIN ANALYZE DELETE ...

11

, ,
, .
postgresql.conf! , !
3.2.2

(. 2.4.2) . :
pg_stat_user_tables
, ,
, , .
pg_stat_user_indexes
,
, , ( ,
, ).
pg_statio_user_tables
, , , (.
2.1.1), ,
, TOAST.
,
(
).
. , , , , PRIMARY KEY UNIQUE.
.
, , .
3.2.3

PostgreSQL

/ , , .
, , foo foo_name,
foo_name = , .

CREATE INDEX foo_name_first_idx
ON foo ((lower(substr(foo_name, 1, 1))));

12


SELECT * FROM foo
WHERE lower(substr(foo_name, 1, 1)) = ;
.
, 7.4,
, .
(partial indexes) WHERE. , ,
scheta uplocheno boolean. , uplocheno = false
, uplocheno = true, .
CREATE INDEX scheta_neuplocheno ON scheta (id)
WHERE NOT uplocheno;

SELECT * FROM scheta WHERE NOT uplocheno AND ...;
, , WHERE,
.

, , , . .
tsearch2 http://
www.sai.msu.su/~megera/postgres/gist/tsearch/V2/ PostgreSQL 7.4 contrib/tsearch2,
7.3 .
tsearch2
.

3.3

PostrgeSQL
, PostgreSQL ,
.
,
, 11 . , ,
;
.
, :
,
.
11 RULE PostgreSQL SQL, , ,

13

3.4

,
, . , ,
.
3.4.1

SELECT max(...)/min(...) FROM < >

PostgreSQL : , ,
.

, , min()
/ max() .
,
(), .

SELECT max(field) FROM foo;
field, . ,
.

SELECT field FROM foo ORDER BY field DESC LIMIT 1;
12 ,
field, .
3.4.2

SELECT count(*) FROM < >

count() PostgreSQL. ,
(
!) . , ,
.

SELECT count(*) FROM foo;
foo,
.
12

: ,

14

, , . :
1. , 13 , ,
ANALYZE:
SELECT reltuples FROM pg_class WHERE relname = foo;
2. , , ,
. ,
. ,
.
3. ,
(cron).
3.4.3

SELECT ... WHERE ... IN (SELECT ...)

, 7.4 IN / NOT IN , ( ) ,
EXISTS / NOT EXISTS. -
7.4, .

SELECT ...
FROM foo
WHERE foo_field IN (
SELECT bar_field
FROM bar
...
);
bar,
.
EXISTS:
SELECT ...
FROM foo
WHERE EXISTS (
SELECT bar_field
FROM bar
WHERE bar.bar_field = foo.foo_field
...
);
NOT IN,
NOT EXISTS.
13

10000 ,
50000 !

15

, PostgreSQL .
, .
.
PostgreSQL , LYX, , (PHPClub) ,
.
[ ].
email .


[1] PostgreSQL documentation, http://www.postgresql.org/docs/
[2] Momjian B., et. al. PostgreSQL FAQ, http://www.postgresql.org/docs/
faqs/FAQ.html
[3] Momjian B. PostgreSQL Hardware Performance Tuning, http://www.ca.
postgresql.org/docs/momjian/hw_performance/
[4] Berkus J., Daithankar S. Tuning PostgreSQL for performance, http://www.
varlena.com/varlena/GeneralBits/Tidbits/perf.html
[5] Berkus J. Annotated postgresql.conf and Global User Configuration
(GUC) Guide, http://www.varlena.com/varlena/GeneralBits/Tidbits/
annotated_conf_e.html
[6] Berkus J. Adventures in PostgreSQL Episode 2: The Joy of Index, http://
techdocs.postgresql.org/techdocs/pgsqladventuresep2.php
[7] Trout J. PostgreSQL: Features, Installation, Administration and Optimization,
http://postgres.jefftrout.com/
[8] Argudo J.-P. PostgreSQL Database Performance Tuning, http://www.argudo.
org/postgresql/soft-tuning.html

16

Оценить