8. pg_hba.conf
• どこからの接続を許可するなどを記述
• type, database, user, address, method
• methodはtrust, reject, md5, clearなど
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
host test test 192.168.1.0/24 md5
unix domain
socket
TCP
29. デッドロック検出
• postgresql.confのdeadlock_timeoutの間隔でデッドロックを検出
• ロックグラフをtopological sort
• 重いので10秒に設定してある(default 1秒)
test=# begin;
BEGIN
test=# lock table t2;
LOCK TABLE
test=# lock table t1;
LOCK TABLE
test=#
test=# begin;
BEGIN
test=# lock table t1;
LOCK TABLE
test=# lock table t2;
ERROR: deadlock detected
DETAIL: Process 4405 waits for AccessExclusiveLock on
relation 370301 of database 96179; blocked by process 4397.
Process 4397 waits for AccessExclusiveLock on relation
370298 of database 96179; blocked by process 4405.
HINT: See server log for query details.
30. ロック周りのトラブル
• サーバが綺麗にダウンしなかった場合
• バッチサーバがOOMで落ちてしまった
• → keepaliveの設定を短くする
#tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds;
# 0 selects the system default
#tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;
# 0 selects the system default
#tcp_keepalives_count = 0 # TCP_KEEPCNT;
# 0 selects the system default
41. autovacuum
• デーモンプロセスで、更新頻度によって自動的に
VACUUMを動かす
• thresholdはテーブル単位で設定可能
#autovacuum = on # Enable autovacuum subprocess? 'on'
# requires track_counts to also be on.
#log_autovacuum_min_duration = -1 # -1 disables, 0 logs all actions and
# their durations, > 0 logs only
# actions running at least this number
# of milliseconds.
#autovacuum_max_workers = 3 # max number of autovacuum
subprocesses
# (change requires restart)
#autovacuum_naptime = 1min # time between autovacuum runs
#autovacuum_vacuum_threshold = 50 # min number of row updates before
# vacuum
#autovacuum_analyze_threshold = 50 # min number of row updates before
# analyze
#autovacuum_vacuum_scale_factor = 0.2 # fraction of table size before
vacuum
#autovacuum_analyze_scale_factor = 0.1 # fraction of table size before analyze
#autovacuum_freeze_max_age = 200000000 # maximum XID age before
forced vacuum
# (change requires restart)
#autovacuum_vacuum_cost_delay = 20ms # default vacuum cost delay for
# autovacuum, in milliseconds;
# -1 means use vacuum_cost_delay
#autovacuum_vacuum_cost_limit = -1 # default vacuum cost limit for
# autovacuum, -1 means use
# vacuum_cost_limit
45. 例
test=# explain select * from t1 where a < 100;
QUERY PLAN
----------------------------------------------------------------------
Index Only Scan using t1_a on t1 (cost=0.42..10.16 rows=99 width=4)
Index Cond: (a < 100)
(2 rows)
test=# explain analyze select * from t1 where a < 100;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Index Only Scan using t1_a on t1 (cost=0.42..10.16 rows=99 width=4) (actual time=0.017..0.513
rows=99 loops=1)
Index Cond: (a < 100)
Heap Fetches: 99
Total runtime: 1.502 ms
(4 rows)
46. table scan
• seq scan
• テーブルを上から下まで全てスキャンする
• index scan
• インデックス経由でデータをフェッチ
• インデックスはbtreeやGINなど色々ある
• bitmapscanというインデックスを組み合わせる
プランもある
• index only scan
• インデックスだけを見てデータをフェッチ
テーブルデータ
インデックスデータ テーブルデータ
インデックスデータ
66. test=# select pg_backend_pid();
pg_backend_pid
----------------
9574
(1 row)
% gdb -p 9574
…
(gdb) bt
#0 0x00007fe6c4d1121d in __libc_recv (fd=10, buf=0xcc09e0 <PqRecvBuffer>, n=8192, flags=-1)
at ../sysdeps/unix/sysv/linux/x86_64/recv.c:29
#1 0x000000000064a848 in secure_read (port=0x16dbf10, ptr=0xcc09e0 <PqRecvBuffer>,
len=8192) at be-secure.c:317
#2 0x00000000006561a5 in pq_recvbuf () at pqcomm.c:854
#3 0x000000000065623f in pq_getbyte () at pqcomm.c:895
#4 0x000000000075c002 in SocketBackend (inBuf=0x7fffe4adbdb0) at postgres.c:335
#5 0x000000000075c3dc in ReadCommand (inBuf=0x7fffe4adbdb0) at postgres.c:483
#6 0x0000000000760d93 in PostgresMain (argc=1, argv=0x16bc438, dbname=0x16bc2e8
"postgres", username=0x16bc2d0 "y-asaba") at postgres.c:3965
#7 0x00000000006f313a in BackendRun (port=0x16dbf10) at postmaster.c:4117
#8 0x00000000006f283f in BackendStartup (port=0x16dbf10) at postmaster.c:3791
#9 0x00000000006ef259 in ServerLoop () at postmaster.c:1570
#10 0x00000000006ee90f in PostmasterMain (argc=3, argv=0x16bb530) at postmaster.c:1223
#11 0x000000000065849c in main (argc=3, argv=0x16bb530) at main.c:225