SlideShare una empresa de Scribd logo
1 de 86
Descargar para leer sin conexión
A Tour of PostgreSQL
Masahiko Sawada
PostgreSQL Conference Japan 2020
1
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.2
Agenda
• モチベーション
• OSから⾒見見たPostgreSQL
• 「データ」を中⼼心にPostgreSQL内部構造を紐解く
• PostgreSQLの構成
• SQL処理理
• テーブル/インデックス
• トランザクション
• まとめ
モチベーション
内部構造を知ることはなにに役に立つのか?
3
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.4
ユーザだけど…内部構造を知ることは必要?
Feel the breath of PostgreSQL
予想している聴講者層
• これからPostgreSQLを使いたい/勉強したい
• 実際に運用している
このような方々にも役に立つ!
• 他のRDBMSとの違いを知りたい
• PostgreSQLをいじってみたい
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.5
用語紹介と概要
データベースクラスタ
データベース
スキーマ
a.users b.users …
OSから見た
PostgreSQL
6
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.7
PostgreSQLのプロセス群
OSから見れば一つのプログラム
$ ll -h bin/postgres
-rwxr-xr-x 1 masahiko masahiko 25M Oct 28 18:10 bin/postgres
$ ps x | grep postgres
72572 - Ss 0:00.89 /usr/home/masahiko/pgsql/master/bin/postgres -D data -p 5432
72574 - Ss 0:00.39 postgres: checkpointer (postgres)
72575 - Ss 0:01.82 postgres: background writer (postgres)
72576 - Ss 0:01.95 postgres: walwriter (postgres)
72577 - Is 0:00.34 postgres: autovacuum launcher (postgres)
72578 - Ss 0:01.15 postgres: stats collector (postgres)
72579 - Is 0:00.03 postgres: logical replication launcher (postgres)
75547 - Ss 0:00.00 postgres: masahiko postgres [local] idle (postgres)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.8
PostgreSQLのプロセス群
OSから見れば一つのプログラム
Backend
Postmaster
Backend
Backend
Checkpointer
Wal writer
Bg writer
LoggerArchiver
Autovacuum

launcher/worker
Wal

sender/receiver
Stats collector
Logical repl.

launcher/worker
Clients
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.9
PostgreSQLのディレクトリ構造
データベースクラスタの実体は一つのディレクトリ
$ ls ${PGDATA}
PG_VERSION pg_dynshmem pg_notify pg_stat_tmp pg_xact
base pg_hba.conf pg_replslot pg_subtrans postgresql.auto.conf
global pg_ident.conf pg_serial pg_tblspc postgresql.conf
pg_commit_ts pg_logical pg_snapshots pg_twophase postmaster.opts
pg_multixact pg_stat pg_wal postmaster.pid
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.10
少しずつ見えてきた!
• クライアントが一つのデータベースに接続すると、一つのプロセスがSQLを処理する
• PostgreSQLは様々な役割を持ったプロセスの集合
• データベースクラスタはOSからみると1つのディレクトリ
「データ」を中心に
PostgreSQL内部を
見てみる
11
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.12
本発表でのアプローチ
• 1つのSQLを例に、PostgreSQLがどのよ
うに処理し、データを取得するのかを順番
に解説
• 必要に応じて少し脱線し、関連する機能や
仕組みを紹介
• 最終的には大まかな全体像が掴める
• PostgreSQL13を元に解説します
https://jp.depositphotos.com/v/3oi2kp.html
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.13
SELECT * FROM users WHERE id = 100;
Table "public.users"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | not null |
name | text | | |
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.14
SQLを理解する
SELECT * FROM users WHERE id = 100;
• パーサが受け取ったSQLの構文を解析する
• SELECT文?UPDATE文?文法は正しい?
• ERROR : syntax error at or near xxxx"
• 文法的に正しければOK
• テーブルや列が本当にあるかどうかは気にしない
Parser
SelectStmt

• targetList

• fromClause

• whereClause

A_STAR
=
100id
users
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.15
パースツリーを解析する
• アナライザがテーブルや列の存在チェックをする
• ERROR: relation "badtable" does not exist at character 15
• システムカタログを使う
• (リライタ(Rewriter)がクエリの書き換えを行う)
Parser
Analyzer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.16
システムカタログ
PostgreSQLのメタデータが詰まった内部テーブル
• 実体は通常のテーブルと同じ
• PostgreSQL13には62個ある
• テーブル、テーブルにある列、インデックス、データ型、演算子、トリガー、関数、ユーザ、型変
換など、内部的な情報はすべてシステムカタログにある
• テーブルとしてpg_catalogスキーマに存在するので、SQLでアクセスできる
• 例)テーブルを検索する時:
• SELECT relname FROM pg_class WHERE relname = user and relkind = r ;
Parser
Analyzer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.17
例)テーブルを作った時
CREATE TABLE users (id INT PRIMARY KEY, name TEXT);
• テーブル、インデックス(主キー) → pg_class
• インデックス(主キー) → pg_index
• id列、name列 → pg_attribute
• 主キー制約 → pg_constraint
• 依存関係(テーブルを削除したら主キーも削除する) → pg_depend
Parser
Analyzer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.18
[少し脱線] 共有する/しないシステムカタログ
• データベース、ユーザなどはデータベースクラスタ全体で共有するので対応するカタログ
(pg_database, pg_authidなど)は共有するシステムカタログ(pg_class.isshared = t )
• その他の定義情報(テーブル、トリガなど)は、データベース毎に存在する
Parser
Analyzer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.19
どのようにデータを取得するか?
• テーブルの状態、検索条件によって最適なアクセス方法は異なる
• テーブルにはたった10行しか入っていないかもしれない
• 1億行の内から一つの行を取得するかもしれない
• プランナが最適(だと思われれる)な実行プランを計画する
• 実行プラン:どのように目的のデータを取得するのかをまとめた計画
Parser
Analyzer
Planner
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.20
代表的なアクセス方法
• Seq Scan
• テーブルの(物理的な)先頭から順番にスキャンする
• テーブル内の多くの割合のデータを取得する時に高速
• Index Scan
• インデックスをスキャンしてから、テーブルをスキャンする
• テーブル内の一部のデータを取得する時に高速
Parser
Analyzer
Planner
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.21
PostgreSQLがサポートする実行ノード
Aggregate ForeignScan HashJoin MergeAppend Result SubQueryScan WorkTableScan
BitmapAnd FunctionScan IncrementalSort MergeJoin SampleScan TableFuncScan
BitmapHeapSc
an
Gather IndexOnlyScan NamedTupleSt
oreScan
SeqScan TidScan
BitmapIndexSc
an
GatherMerge Limit NestLoop SetOp Unique
BitmapOr Group LockRows ProjectSet Sort ValueScan
CteScan Hash Material RecursiveUnion SubPlan WindowAgg
Parser
Analyzer
Planner
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.22
どうやってプランを決めるのか?
まずは選択度
• テーブルの統計情報を利用して最適なアクセス方法を決める
• 列の固有値 : (例)一意の列の固有値は-1。都道府県を格納する列の固有値は=47
• 列の最頻値 : 最も多いデータとその割合のペアのリスト
• most_common_vals : {user0,user1,user2,user3,user4,user5,user6,user7,user8,user9}
• most_common_freqs : {0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1}
• 列のヒストグラム : 度数を均一にしたときのBinの境界値
• histogram_bounds : {1,100,200,300,400,500,600,700,800,900,1000}
• pg_statsビュー
• 統計情報はANALYZEで更新
Parser
Analyzer
Planner
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.23
行数の見積もり
選択度(Selectivity)の決定
等価条件(WHERE id = 100)の場合
• 列が主キー(固有値が-1)なら簡単
• 必ず0行か1行
• 最頻値から探す
• あればその割合が選択度
• なければ「それ以外の割合」から選択度を求める
Parser
Analyzer
Planner
範囲条件(WHERE id < 100)の場合
• ヒストグラムから100を含むBinの位置を求める
• 「そのBin」+「それより前のBin」が占める割合を選択
度とする
80 120 200 500
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.24
コストの見積もり
(推定)取得行数から処理コストを計算する
• Seq Scanの場合
• 取得行数によらず一定
• 必要なデータ=テーブルサイズ
• Index Scanの場合
• 取得行数によってコストが異なる
• 必要なデータ=インデックス内を探す時に必要なデータ+

         テーブル内の検索対象が入っているデータ
Parser
Analyzer
Planner
Index Scan
Seq Scan
Cost
# of tuples
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.25
[少し脱線] 結合の場合はどうなる?
• プランはボトムアップにプラン候補を集め
ながら決めていく
• テーブルのスキャン方法→結合方法
→…
Parser
Analyzer
Planner
https://www.pgcon.org/2016/schedule/attachments/433_PGCON2016_beyond_explain.pdf
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.26
EXPLAIN
実行プランの確認
cost : プランナが見積もったコスト
rows : プランナが見積もった取得行数
width : プランナが見積もった行のサイズ
=# EXPLAIN SELECT * FROM users WHERE id = 100;
QUERY PLAN
-------------------------------------------------------------------------
Index Scan using users_pkey on users (cost=0.28..8.29 rows=1 width=10)
Index Cond: (id = 100)
(2 rows)
Parser
Analyzer
Planner
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.27
計画したプランを実行する
• エグゼキュータが受け取ったプランを実行する
• PostgreSQLのエグゼキュータはPull方式
• 再帰的にノードを実行していく
• 全ての実行ノードは1行返す
• EXPLAIN ANALYZEでプランと一緒に実行時間を確認できる
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.28
プランはどのように実行されるのか?
QUERY PLAN
-------------------------------------------------------------------------
Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1)
Index Cond: (id = 100)
(2 rows)
Executor
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.29
プランはどのように実行されるのか?
1⾏行行ちょうだい
QUERY PLAN
-------------------------------------------------------------------------
Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1)
Index Cond: (id = 100)
(2 rows)
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.30
プランはどのように実行されるのか?
1⾏行行ちょうだい
QUERY PLAN
-------------------------------------------------------------------------
Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1)
Index Cond: (id = 100)
(2 rows)
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.31
プランはどのように実行されるのか?
QUERY PLAN
-------------------------------------------------------------------------
Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1)
Index Cond: (id = 100)
(2 rows)
どうぞ
最初の⾏行行を
取得するまでに
かかった時間(ms)
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.32
プランはどのように実行されるのか?
1⾏行行ちょうだい
QUERY PLAN
-------------------------------------------------------------------------
Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1)
Index Cond: (id = 100)
(2 rows)
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.33
プランはどのように実行されるのか?
1⾏行行ちょうだい
QUERY PLAN
-------------------------------------------------------------------------
Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1)
Index Cond: (id = 100)
(2 rows)
もうないよ
処理理が完了了
した時間
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.34
[少し脱線] ソートの場合はどうなる?
SELECT * FROM users WHERE id < 100 ORDER BY id;
QUERY PLAN
---------------------------------------------------------------------------------------
Sort (actual time=1.646..1.656 rows=99 loops=1)
Sort Key: name
Sort Method: quicksort Memory: 30kB
-> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1)
Index Cond: (id < 100)
ソートした結果を返したい
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.35
[少し脱線] ソートの場合はどうなる?
SELECT * FROM users WHERE id < 100 ORDER BY id;
QUERY PLAN
---------------------------------------------------------------------------------------
Sort (actual time=1.646..1.656 rows=99 loops=1)
Sort Key: name
Sort Method: quicksort Memory: 30kB
-> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1)
Index Cond: (id < 100)
1⾏行行取得!
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.36
[少し脱線] ソートの場合はどうなる?
SELECT * FROM users WHERE id < 100 ORDER BY id;
QUERY PLAN
---------------------------------------------------------------------------------------
Sort (actual time=1.646..1.656 rows=99 loops=1)
Sort Key: name
Sort Method: quicksort Memory: 30kB
-> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1)
Index Cond: (id < 100)
1⾏行行取得!!
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.37
[少し脱線] ソートの場合はどうなる?
SELECT * FROM users WHERE id < 100 ORDER BY id;
QUERY PLAN
---------------------------------------------------------------------------------------
Sort (actual time=1.646..1.656 rows=99 loops=1)
Sort Key: name
Sort Method: quicksort Memory: 30kB
-> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1)
Index Cond: (id < 100)
1⾏行行取得!!!
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.38
[少し脱線] ソートの場合はどうなる?
SELECT * FROM users WHERE id < 100 ORDER BY id;
QUERY PLAN
---------------------------------------------------------------------------------------
Sort (actual time=1.646..1.656 rows=99 loops=1)
Sort Key: name
Sort Method: quicksort Memory: 30kB
-> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1)
Index Cond: (id < 100)
1⾏行行取得!!!!
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.39
[少し脱線] ソートの場合はどうなる?
SELECT * FROM users WHERE id < 100 ORDER BY id;
QUERY PLAN
---------------------------------------------------------------------------------------
Sort (actual time=1.646..1.656 rows=99 loops=1)
Sort Key: name
Sort Method: quicksort Memory: 30kB
-> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1)
Index Cond: (id < 100)
1⾏行行取得!!!!!× 99
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.40
[少し脱線] ソートの場合はどうなる?
SELECT * FROM users WHERE id < 100 ORDER BY id;
QUERY PLAN
---------------------------------------------------------------------------------------
Sort (actual time=1.646..1.656 rows=99 loops=1)
Sort Key: name
Sort Method: quicksort Memory: 30kB
-> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1)
Index Cond: (id < 100)
全部そろったからソートするよ
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.41
[少し脱線] ソートの場合はどうなる?
SELECT * FROM users WHERE id < 100 ORDER BY id;
QUERY PLAN
---------------------------------------------------------------------------------------
Sort (actual time=1.646..1.656 rows=99 loops=1)
Sort Key: name
Sort Method: quicksort Memory: 30kB
-> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1)
Index Cond: (id < 100)
やっと1⾏行行返せる
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.42
[少し脱線] 結合の場合はどうなる?
ユーザ(name = user_100 )の給料を検索する
=# EXPLAIN SELECT * FROM users, salary WHERE
users.id = salary.id and users.name = ‘user_100';
QUERY PLAN
--------------------------------------------------------------------------------
Nested Loop (cost=0.29..26.80 rows=1 width=18)
-> Seq Scan on users (cost=0.00..18.50 rows=1 width=10)
Filter: (name = 'user_100'::text)
-> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8)
Index Cond: (id = users.id)
(5 rows)
users salary
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.43
[少し脱線] 結合の場合はどうなる?
ユーザ(name = user_100 )の給料を検索する
QUERY PLAN
--------------------------------------------------------------------------------
Nested Loop (cost=0.29..26.80 rows=1 width=18)
-> Seq Scan on users (cost=0.00..18.50 rows=1 width=10)
Filter: (name = 'user_100'::text)
-> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8)
Index Cond: (id = users.id)
(5 rows)
まずは内部表(駆動表)
から1⾏行行とるよ
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.44
[少し脱線] 結合の場合はどうなる?
ユーザ(name = user_100 )の給料を検索する
QUERY PLAN
--------------------------------------------------------------------------------
Nested Loop (cost=0.29..26.80 rows=1 width=18)
-> Seq Scan on users (cost=0.00..18.50 rows=1 width=10)
Filter: (name = 'user_100'::text)
-> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8)
Index Cond: (id = users.id)
(5 rows)
まずは内部表(駆動表)
から1⾏行行とるよ
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.45
[少し脱線] 結合の場合はどうなる?
ユーザ(name = user_100 )の給料を検索する
QUERY PLAN
--------------------------------------------------------------------------------
Nested Loop (cost=0.29..26.80 rows=1 width=18)
-> Seq Scan on users (cost=0.00..18.50 rows=1 width=10)
Filter: (name = 'user_100'::text)
-> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8)
Index Cond: (id = users.id)
(5 rows)
先程取った⾏行行のID列列の
値を使って次は内部表
から1⾏行行とるよ
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.46
[少し脱線] 結合の場合はどうなる?
ユーザ(name = user_100 )の給料を検索する
QUERY PLAN
--------------------------------------------------------------------------------
Nested Loop (cost=0.29..26.80 rows=1 width=18)
-> Seq Scan on users (cost=0.00..18.50 rows=1 width=10)
Filter: (name = 'user_100'::text)
-> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8)
Index Cond: (id = users.id)
(5 rows)
1⾏行行結合できたので、
エグゼキュータに返すよ
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.47
ここまでのまとめ
パーサがSQLを構文解析し、
アナライザがシステムカタログを使ってテーブルや列の有無を確認し、
プランナが統計情報を使って実行プランを計画し、
エグゼキュータが実行プランを実行する。
Parser
Analyzer
Planner
Executor
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.48
実際にデータを読む
PostgreSQLはデータをどのように管理するのか?
• テーブルやインデックスの実体は1つのファイル
• 8kB(ブロック)単位で読み書きする
• 共有バッファに読んだデータをキャッシュする
• OSのファイルキャッシュも利用する
• shared_buffersで調整
• 満杯になったら入れ替える(Clock Sweep)
• CheckpointerやBgwriterが変更したデータをディス
クに書き込む
File Cache

(OS)
Disk
Executor

(PostgreSQL)
SharedBuffer

(PostgreSQL)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.49
PostgreSQLのメモリ
共有するメモリとしないメモリ
• プロセス間で共有するデータは共有メモリに確保
• 共有バッファ、ロック、管理情報など
• 例)shared_buffersは共有メモリとして一つ確保
• プロセス内だけで使うメモリはプロセス毎に確保
• プランニング時に使うメモリ、ソート等のSQL実行時に使うメモリ、システムカタログの
キャッシュなど
• 例)work_memはプロセス毎に確保する
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.50
ここまでのまとめ
• PostgreSQLのプロセス構造が分かってきた
• postmasterやbackendプロセス
• SQLがどのように処理されるのかが分かってきた
• パーサ、アナライザ、プランナ、エグゼキュータ
• EXPLAINの読み方、どのように実行されるのかも分かってきた
• PostgreSQLはデータを共有バッファ、OSキャッシュ、ディスクに持っている
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.51
PostgreSQLを完全に理解した?
ワタシポストグレスチョットワカル
• これでは「SQLでアクセスできるデータストア」の構造を理解しただけ
• RDBMSの機能の一部分にすぎない
• 実際には複数クライアントが同時にアクセスするし、途中でクラッシュするかもしれない
• データの不整合が起きない
• データが失われない
トランザクション
52
BEGIN;

SELECT …;

INSERT …;

SELECT …;

COMMIT;
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.53
SELECTとUPDATEが同時に起きたら?
• UPDATE途中のデータを見たくない
• 1回目のSELECTと2回目でSELECTで同じ結果が見たい
• ACIDのI (Isolation)
• UPDATE中はSELECTできない、というのは性能的にNG
• PostgreSQLは変更前、変更後の両方を持つことで実現している
• マルチバージョンを使った同時実行制御(MVCC)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.54
MVCC (Multi-Version Concurrency Control)
• 1つのデータに対し複数のバージョンを持てば、一方が変更している間にもう一方が読むことがで
きる
• PostgreSQLではUPDATEを「DELETE+INSERT」で実現
• UPDATEでもテーブルの物理的なサイズは大きくなっていく(テーブルの肥大化)
• 追記型アーキテクチャ
• ROLLBACKしたデータ(誰からも参照されないデータ)もテーブルに残る
• うまくフィルタリングしながらデータを読む
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.55
追記型とVacuum
• Vacuumで不要になった領域を回収し再利用できるようにする
• 物理的なサイズは小さくならない
Appel
Ball
Cat
Appel
Ball
Cat
Apple
Ball
Cat
Apple
Dog
Ball
Cat
Apple
UPDATE VACUUM INSERT
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.56
Vacuumのせいで遅い?
Q. Vacuum中は読み書きできる?
A. Yes
Q. Vacuumは手動、もしくはcronとかで実行する必要がある?
A. No、自動Vacuum(とHOT)があるので基本的には放っておいて大丈夫です。
Q. ゴミを確認するためにテーブルをすべてスキャンする?
A. No、ゴミがないと分かっている箇所はスキップします。
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.57
自動VacuumはどうやってVacuumするテーブルを見つける?
• 稼働統計情報を使ってVacuumするべきかどうかを判断する
• pg_stat_all_tables/indexes/functionsビュー
• テーブル内の生きているタプル数、死んでいる(Vacuumできる)タプル数、最後に
Vacuumされた時間、Vacuumされた回数など
• テーブルがSeqScanされた回数、IndexScanされた回数など
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.58
[少し脱線] UNDOログ
OracleやMySQLとの違い
• OracleやMySQLもMVCCを使っているが古いデータをUNDOログに持つ
• 要らなくなったらUNDOログをまとめて削除すれば良い
• テーブル内にはゴミは溜まらない
• テーブルをチェックする必要はない
• テーブルの肥大化は起きにくい方式
• UNDOログの方が優れている、とも言えない
• 例)ROLLBACKに時間がかかる(ゴミだと思っていたけどやっぱり違った)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.59
トランザクションのステータス
COMMIT、ABORTはどうやって判断する?
• ACIDのA(Atomicity)
• PostgreSQLは各トランザクションにID(単調増加の非負整数)を割り当てる(XID)
• コミットログと呼ばれるファイルに、各トランザクションがCommit/Abortした情報を格納する(2bits/txn)
• たくさんテーブルを更新してもたった2bitの更新でCommit/Abortを記録する
• 各タプルには「INSERTされたトランザクションのXID(=xmin)」と「DELETEされたトランザクションの
XID(=xmax)」の両方を持つ
• タプルから取得したXIDからステータスを確認して、「COMMITされたINSERT」なのか「ABORTされた
INSERT」なのかを確認する
90 Apple120
Commit log
00100 … 0110
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.60
タプルの可視性
ゴミも混ざっているテーブル内のタプルをどのように正しく読むか?
• モチベーションの例)トランザクション内では、一度読んだデータをもう一度読んでも同じデータが返ってきて
ほしい!
• REPEATABLE READに相当する
• これまで紹介した仕組みを組み合わせる(マルチバージョン、XID、コミットログ)
• 基本的な考え方
• 自分のXIDより小さいXIDで追加されたデータは読める(過去に追加された)
• 自分のXIDより小さいXIDで削除されたデータは読めない(過去に削除された)
• ただし例外も・・・
• 現在進行中のトランザクションによって行われた(たとえXIDが小さくても)変更は見てはいけない
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.61
データベースのスナップショットを見る
データベースある時点を切り取ったようなデータ
100 Apple
XID = 100: INSERT ‘Apple’;
xmin

(insert)
xmax

(delete)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.62
データベースのスナップショットを見る
データベースある時点を切り取ったようなデータ
100 Apple
100 Ball
XID = 100: INSERT ‘Apple’;

INSERT ‘Ball’;
xmin

(insert)
xmax

(delete)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.63
データベースのスナップショットを見る
データベースある時点を切り取ったようなデータ
100 200 Apple
100 Ball
200 Cat
XID = 100: INSERT ‘Apple’;

INSERT ‘Ball’;

XID = 200: UPDATE ‘Apple’ -> ‘Cat’;
xmin

(insert)
xmax

(delete)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.64
データベースのスナップショットを見る
データベースある時点を切り取ったようなデータ
100 200 Apple
100 Ball
200 Cat
XID = 100: INSERT ‘Apple’;

INSERT ‘Ball’;

XID = 200: UPDATE ‘Apple’ -> ‘Cat’;
xmin

(insert)
xmax

(delete)
XID = 150
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.65
データベースのスナップショットを見る
データベースある時点を切り取ったようなデータ
100 200 Apple
100 Ball
200 Cat
XID = 100: INSERT ‘Apple’;

INSERT ‘Ball’;

XID = 200: UPDATE ‘Apple’ -> ‘Cat’;
xmin

(insert)
xmax

(delete)
XID = 150 xmin = 100 → 過去にINSERTされた
xmax = 200 → 未来でDELETEされる

見える!!
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.66
データベースのスナップショットを見る
データベースある時点を切り取ったようなデータ
100 200 Apple
100 Ball
200 Cat
XID = 100: INSERT ‘Apple’;

INSERT ‘Ball’;

XID = 200: UPDATE ‘Apple’ -> ‘Cat’;
xmin

(insert)
xmax

(delete)
XID = 150 xmin = 100 → 過去にINSERTされた
まだ誰からも削除されていない

見える!!
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.67
データベースのスナップショットを見る
データベースある時点を切り取ったようなデータ
100 200 Apple
100 Ball
200 Cat
XID = 100: INSERT ‘Apple’;

INSERT ‘Ball’;

XID = 200: UPDATE ‘Apple’ -> ‘Cat’;
xmin

(insert)
xmax

(delete)
XID = 150 xmin = 200 → 未来にINSERTされる
まだ誰からも削除されていない

見えない!!
WAL
68
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.69
すべてはログに書いてある
• コミット済みのデータを失わない!
• ACIDのAとD(Durability)
• WAL (Write Ahead Logging)
• データ(テーブルやインデックス)の変更をディスクに反映する前に、必ず対応するロ
グをディスクに反映する
• COMMIT後にクラッシュしてもログを再生すれば元通り(ロールフォワード)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.70
Shared Buffer Disk WAL(Disk)
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.71
Disk
Apple
INSERT ‘Apple’;

WAL(Disk)
INS: Apple
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.72
Disk
Apple
Ball
INSERT ‘Apple’;

INSERT ‘Ball’;

WAL(Disk)
INS: Apple
INS: Ball
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.73
Disk
Apple
Ball
INSERT ‘Apple’;

INSERT ‘Ball’;

WAL(Disk)
INS: Apple
INS: Ball
Apple
Ball
Sync
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.74
Disk WAL(Disk)
Apple
Ball
Alice
INSERT ‘Apple’;

INSERT ‘Ball’;

UPDATE ‘Apple’ -> ‘Alice’;

INS: Apple
INS: Ball
UPD: 1 -> ‘Alice’
Apple
Ball
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.75
Disk WAL(Disk)
Apple
Ball
Alice
Cat
INSERT ‘Apple’;

INSERT ‘Ball’;

UPDATE ‘Apple’ -> ‘Alice’;

INSERT ‘Cat’;

INS: ‘Apple’ at 1
INS: ‘Ball’ at 2
UPD: 1 -> ‘Alice’ at 3
INS: ‘Cat’ at 4
Apple
Ball
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.76
Disk WAL(Disk)
Apple
Ball
Alice
Cat
INSERT ‘Apple’;

INSERT ‘Ball’;

UPDATE ‘Apple’ -> ‘Alice’;

INSERT ‘Cat’;

INS: ‘Apple’ at 1
INS: ‘Ball’ at 2
UPD: 1 -> ‘Alice’ at 3
INS: ‘Cat’ at 4
Apple
Ball
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.77
Disk WAL(Disk)
INSERT ‘Apple’;

INSERT ‘Ball’;

UPDATE ‘Apple’ -> ‘Alice’;

INSERT ‘Cat’;

INS: ‘Apple’ at 1
INS: ‘Ball’ at 2
UPD: 1 -> ‘Alice’ at 3
INS: ‘Cat’ at 4
Apple
Ball
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.78
Disk WAL(Disk)
INSERT ‘Apple’;

INSERT ‘Ball’;

UPDATE ‘Apple’ -> ‘Alice’;

INSERT ‘Cat’;

INS: ‘Apple’ at 1
INS: ‘Ball’ at 2
UPD: 1 -> ‘Alice’ at 3
INS: ‘Cat’ at 4
Apple
Ball
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.79
Disk WAL(Disk)
INSERT ‘Apple’;

INSERT ‘Ball’;

UPDATE ‘Apple’ -> ‘Alice’;

INSERT ‘Cat’;

INS: ‘Apple’ at 1
INS: ‘Ball’ at 2
UPD: 1 -> ‘Alice’ at 3
INS: ‘Cat’ at 4
Apple
Ball
Apple
Ball
Alice
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.80
Disk WAL(Disk)
INSERT ‘Apple’;

INSERT ‘Ball’;

UPDATE ‘Apple’ -> ‘Alice’;

INSERT ‘Cat’;

INS: ‘Apple’ at 1
INS: ‘Ball’ at 2
UPD: 1 -> ‘Alice’ at 3
INS: ‘Cat’ at 4
Recovery !!

(Roll Forward)
Apple
Ball
Alice
Cat
Apple
Ball
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.81
Disk WAL(Disk)
INSERT ‘Apple’;

INSERT ‘Ball’;

UPDATE ‘Apple’ -> ‘Alice’;

INSERT ‘Cat’;

INS: ‘Apple’ at 1
INS: ‘Ball’ at 2
UPD: 1 -> ‘Alice’ at 3
INS: ‘Cat’ at 4
Recovery !!

(Roll Forward)
Apple
Ball
Alice
Cat
Apple
Ball
Alice
Cat
Sync
Shared Buffer
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.82
すべてはログに書いてある
• クラッシュしてもCOMMIT済みのデータは失われない!
• WALはSequential Writeになる
• WALはpg_wal配下に格納される。絶対消さないで!
$ ls ${PGDATA}/pg_wal/
00000001000000000000002B 000000010000000000000032 archive_status
00000001000000000000002C 000000010000000000000033
00000001000000000000002D 000000010000000000000034
00000001000000000000002E 000000010000000000000035
00000001000000000000002F 000000010000000000000036
000000010000000000000030 000000010000000000000037
000000010000000000000031 000000010000000000000038
まとめ
83
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.84
今回触れられなかった部分
• ロック
• トランザクション分離レベル
• 補助プロセス群(Wal writer、Checkpointerなど)
• その他高度な機能(レプリケーション、パラレルクエリ、パーティショニング、など)
• きっと次の講演で聞けるはず!
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.85
まとめ
Feel the breath of PostgreSQL
• PostgreSQLがどのようにSQLを処理し、データを取得しているのか
• どのように同時実行制御しているのか
• COMMIT済みのデータが決して失われない仕組み
• PostgreSQLはもちろんRDBMSの勉強としても役立つ
• この後の講演の理解度もぜんぜん違う(はず)!
© Copyright EnterpriseDB Corporation, 2020. All rights reserved.86
Thank you
Masahiko Sawada
masahiko.sawada@enterprisedb.com

Más contenido relacionado

La actualidad más candente

OLAP options on Hadoop
OLAP options on HadoopOLAP options on Hadoop
OLAP options on HadoopYuta Imai
 
sparksql-hive-bench-by-nec-hwx-at-hcj16
sparksql-hive-bench-by-nec-hwx-at-hcj16sparksql-hive-bench-by-nec-hwx-at-hcj16
sparksql-hive-bench-by-nec-hwx-at-hcj16Yifeng Jiang
 
Yahoo! JAPANのOracle構成-2017年版
Yahoo! JAPANのOracle構成-2017年版Yahoo! JAPANのOracle構成-2017年版
Yahoo! JAPANのOracle構成-2017年版Makoto Sato
 
[D36] Michael Stonebrakerが生み出した列指向データベースは何が凄いのか? ~Verticaを例に列指向データベースのアーキテクチャ...
[D36] Michael Stonebrakerが生み出した列指向データベースは何が凄いのか? ~Verticaを例に列指向データベースのアーキテクチャ...[D36] Michael Stonebrakerが生み出した列指向データベースは何が凄いのか? ~Verticaを例に列指向データベースのアーキテクチャ...
[D36] Michael Stonebrakerが生み出した列指向データベースは何が凄いのか? ~Verticaを例に列指向データベースのアーキテクチャ...Insight Technology, Inc.
 
Treasure Dataを支える技術 - MessagePack編
Treasure Dataを支える技術 - MessagePack編Treasure Dataを支える技術 - MessagePack編
Treasure Dataを支える技術 - MessagePack編Taro L. Saito
 
SIGMOD’12勉強会 -Session 7-
SIGMOD’12勉強会 -Session 7-SIGMOD’12勉強会 -Session 7-
SIGMOD’12勉強会 -Session 7-Takeshi Yamamuro
 
Prestoで実現するインタラクティブクエリ - dbtech showcase 2014 Tokyo
Prestoで実現するインタラクティブクエリ - dbtech showcase 2014 TokyoPrestoで実現するインタラクティブクエリ - dbtech showcase 2014 Tokyo
Prestoで実現するインタラクティブクエリ - dbtech showcase 2014 TokyoTreasure Data, Inc.
 
BigtopでHadoopをビルドする(Open Source Conference 2021 Online/Spring 発表資料)
BigtopでHadoopをビルドする(Open Source Conference 2021 Online/Spring 発表資料)BigtopでHadoopをビルドする(Open Source Conference 2021 Online/Spring 発表資料)
BigtopでHadoopをビルドする(Open Source Conference 2021 Online/Spring 発表資料)NTT DATA Technology & Innovation
 
Introduction to Hadoop and Spark (before joining the other talk) and An Overv...
Introduction to Hadoop and Spark (before joining the other talk) and An Overv...Introduction to Hadoop and Spark (before joining the other talk) and An Overv...
Introduction to Hadoop and Spark (before joining the other talk) and An Overv...DataWorks Summit/Hadoop Summit
 
20160121 データサイエンティスト協会 木曜セミナー #5
20160121 データサイエンティスト協会 木曜セミナー #520160121 データサイエンティスト協会 木曜セミナー #5
20160121 データサイエンティスト協会 木曜セミナー #5Koichiro Sasaki
 
ビッグデータ処理データベースの全体像と使い分け - 2017年 Version -
ビッグデータ処理データベースの全体像と使い分け - 2017年 Version - ビッグデータ処理データベースの全体像と使い分け - 2017年 Version -
ビッグデータ処理データベースの全体像と使い分け - 2017年 Version - Tetsutaro Watanabe
 
簡単!AWRをEXCELピボットグラフで分析しよう♪
簡単!AWRをEXCELピボットグラフで分析しよう♪簡単!AWRをEXCELピボットグラフで分析しよう♪
簡単!AWRをEXCELピボットグラフで分析しよう♪Yohei Azekatsu
 
SQL Server 2016 R Services + Microsoft R Server 技術資料
SQL Server 2016 R Services + Microsoft R Server 技術資料SQL Server 2016 R Services + Microsoft R Server 技術資料
SQL Server 2016 R Services + Microsoft R Server 技術資料Koichiro Sasaki
 
20160220 MSのビッグデータ分析基盤 - データマイニング+WEB@東京
20160220 MSのビッグデータ分析基盤 - データマイニング+WEB@東京20160220 MSのビッグデータ分析基盤 - データマイニング+WEB@東京
20160220 MSのビッグデータ分析基盤 - データマイニング+WEB@東京Koichiro Sasaki
 
Hadoop Conference Japan_2016 セッション「顧客事例から学んだ、 エンタープライズでの "マジな"Hadoop導入の勘所」
Hadoop Conference Japan_2016 セッション「顧客事例から学んだ、 エンタープライズでの "マジな"Hadoop導入の勘所」Hadoop Conference Japan_2016 セッション「顧客事例から学んだ、 エンタープライズでの "マジな"Hadoop導入の勘所」
Hadoop Conference Japan_2016 セッション「顧客事例から学んだ、 エンタープライズでの "マジな"Hadoop導入の勘所」オラクルエンジニア通信
 
20190314 PGStrom Arrow_Fdw
20190314 PGStrom Arrow_Fdw20190314 PGStrom Arrow_Fdw
20190314 PGStrom Arrow_FdwKohei KaiGai
 
Kuduを調べてみた #dogenzakalt
Kuduを調べてみた #dogenzakaltKuduを調べてみた #dogenzakalt
Kuduを調べてみた #dogenzakaltToshihiro Suzuki
 
HAWQをCDHで動かしてみた
HAWQをCDHで動かしてみたHAWQをCDHで動かしてみた
HAWQをCDHで動かしてみたadachij2002
 
[db tech showcase Tokyo 2017] A32: Attunity Replicate + Kafka + Hadoop マルチデータ...
[db tech showcase Tokyo 2017] A32: Attunity Replicate + Kafka + Hadoop マルチデータ...[db tech showcase Tokyo 2017] A32: Attunity Replicate + Kafka + Hadoop マルチデータ...
[db tech showcase Tokyo 2017] A32: Attunity Replicate + Kafka + Hadoop マルチデータ...Insight Technology, Inc.
 

La actualidad más candente (20)

OLAP options on Hadoop
OLAP options on HadoopOLAP options on Hadoop
OLAP options on Hadoop
 
sparksql-hive-bench-by-nec-hwx-at-hcj16
sparksql-hive-bench-by-nec-hwx-at-hcj16sparksql-hive-bench-by-nec-hwx-at-hcj16
sparksql-hive-bench-by-nec-hwx-at-hcj16
 
Yahoo! JAPANのOracle構成-2017年版
Yahoo! JAPANのOracle構成-2017年版Yahoo! JAPANのOracle構成-2017年版
Yahoo! JAPANのOracle構成-2017年版
 
[D36] Michael Stonebrakerが生み出した列指向データベースは何が凄いのか? ~Verticaを例に列指向データベースのアーキテクチャ...
[D36] Michael Stonebrakerが生み出した列指向データベースは何が凄いのか? ~Verticaを例に列指向データベースのアーキテクチャ...[D36] Michael Stonebrakerが生み出した列指向データベースは何が凄いのか? ~Verticaを例に列指向データベースのアーキテクチャ...
[D36] Michael Stonebrakerが生み出した列指向データベースは何が凄いのか? ~Verticaを例に列指向データベースのアーキテクチャ...
 
Treasure Dataを支える技術 - MessagePack編
Treasure Dataを支える技術 - MessagePack編Treasure Dataを支える技術 - MessagePack編
Treasure Dataを支える技術 - MessagePack編
 
SIGMOD’12勉強会 -Session 7-
SIGMOD’12勉強会 -Session 7-SIGMOD’12勉強会 -Session 7-
SIGMOD’12勉強会 -Session 7-
 
Prestoで実現するインタラクティブクエリ - dbtech showcase 2014 Tokyo
Prestoで実現するインタラクティブクエリ - dbtech showcase 2014 TokyoPrestoで実現するインタラクティブクエリ - dbtech showcase 2014 Tokyo
Prestoで実現するインタラクティブクエリ - dbtech showcase 2014 Tokyo
 
BigtopでHadoopをビルドする(Open Source Conference 2021 Online/Spring 発表資料)
BigtopでHadoopをビルドする(Open Source Conference 2021 Online/Spring 発表資料)BigtopでHadoopをビルドする(Open Source Conference 2021 Online/Spring 発表資料)
BigtopでHadoopをビルドする(Open Source Conference 2021 Online/Spring 発表資料)
 
Introduction to Hadoop and Spark (before joining the other talk) and An Overv...
Introduction to Hadoop and Spark (before joining the other talk) and An Overv...Introduction to Hadoop and Spark (before joining the other talk) and An Overv...
Introduction to Hadoop and Spark (before joining the other talk) and An Overv...
 
PostgreSQL 12の話
PostgreSQL 12の話PostgreSQL 12の話
PostgreSQL 12の話
 
20160121 データサイエンティスト協会 木曜セミナー #5
20160121 データサイエンティスト協会 木曜セミナー #520160121 データサイエンティスト協会 木曜セミナー #5
20160121 データサイエンティスト協会 木曜セミナー #5
 
ビッグデータ処理データベースの全体像と使い分け - 2017年 Version -
ビッグデータ処理データベースの全体像と使い分け - 2017年 Version - ビッグデータ処理データベースの全体像と使い分け - 2017年 Version -
ビッグデータ処理データベースの全体像と使い分け - 2017年 Version -
 
簡単!AWRをEXCELピボットグラフで分析しよう♪
簡単!AWRをEXCELピボットグラフで分析しよう♪簡単!AWRをEXCELピボットグラフで分析しよう♪
簡単!AWRをEXCELピボットグラフで分析しよう♪
 
SQL Server 2016 R Services + Microsoft R Server 技術資料
SQL Server 2016 R Services + Microsoft R Server 技術資料SQL Server 2016 R Services + Microsoft R Server 技術資料
SQL Server 2016 R Services + Microsoft R Server 技術資料
 
20160220 MSのビッグデータ分析基盤 - データマイニング+WEB@東京
20160220 MSのビッグデータ分析基盤 - データマイニング+WEB@東京20160220 MSのビッグデータ分析基盤 - データマイニング+WEB@東京
20160220 MSのビッグデータ分析基盤 - データマイニング+WEB@東京
 
Hadoop Conference Japan_2016 セッション「顧客事例から学んだ、 エンタープライズでの "マジな"Hadoop導入の勘所」
Hadoop Conference Japan_2016 セッション「顧客事例から学んだ、 エンタープライズでの "マジな"Hadoop導入の勘所」Hadoop Conference Japan_2016 セッション「顧客事例から学んだ、 エンタープライズでの "マジな"Hadoop導入の勘所」
Hadoop Conference Japan_2016 セッション「顧客事例から学んだ、 エンタープライズでの "マジな"Hadoop導入の勘所」
 
20190314 PGStrom Arrow_Fdw
20190314 PGStrom Arrow_Fdw20190314 PGStrom Arrow_Fdw
20190314 PGStrom Arrow_Fdw
 
Kuduを調べてみた #dogenzakalt
Kuduを調べてみた #dogenzakaltKuduを調べてみた #dogenzakalt
Kuduを調べてみた #dogenzakalt
 
HAWQをCDHで動かしてみた
HAWQをCDHで動かしてみたHAWQをCDHで動かしてみた
HAWQをCDHで動かしてみた
 
[db tech showcase Tokyo 2017] A32: Attunity Replicate + Kafka + Hadoop マルチデータ...
[db tech showcase Tokyo 2017] A32: Attunity Replicate + Kafka + Hadoop マルチデータ...[db tech showcase Tokyo 2017] A32: Attunity Replicate + Kafka + Hadoop マルチデータ...
[db tech showcase Tokyo 2017] A32: Attunity Replicate + Kafka + Hadoop マルチデータ...
 

Similar a A Tour of PostgreSQL

SQLチューニング入門 入門編
SQLチューニング入門 入門編SQLチューニング入門 入門編
SQLチューニング入門 入門編Miki Shimogai
 
Jubatusでマルウェア分類
Jubatusでマルウェア分類Jubatusでマルウェア分類
Jubatusでマルウェア分類Shuzo Kashihara
 
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~Miki Shimogai
 
Databricksを初めて使う人に向けて.pptx
Databricksを初めて使う人に向けて.pptxDatabricksを初めて使う人に向けて.pptx
Databricksを初めて使う人に向けて.pptxotato
 
Let's scale-out PostgreSQL using Citus (Japanese)
Let's scale-out PostgreSQL using Citus (Japanese)Let's scale-out PostgreSQL using Citus (Japanese)
Let's scale-out PostgreSQL using Citus (Japanese)Noriyoshi Shinoda
 
外部データラッパによる PostgreSQL の拡張
外部データラッパによる PostgreSQL の拡張外部データラッパによる PostgreSQL の拡張
外部データラッパによる PostgreSQL の拡張Shigeru Hanada
 
PostgreSQL10徹底解説
PostgreSQL10徹底解説PostgreSQL10徹底解説
PostgreSQL10徹底解説Masahiko Sawada
 
OSS-DB Gold技術解説セミナー@db tech showcase 東京 2014
OSS-DB Gold技術解説セミナー@db tech showcase 東京 2014OSS-DB Gold技術解説セミナー@db tech showcase 東京 2014
OSS-DB Gold技術解説セミナー@db tech showcase 東京 2014Shigeru Hanada
 
2019年度 若手技術者向け講座 DBMSの機能
2019年度 若手技術者向け講座 DBMSの機能2019年度 若手技術者向け講座 DBMSの機能
2019年度 若手技術者向け講座 DBMSの機能keki3
 
PostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイント
PostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイントPostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイント
PostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイントNTT DATA OSS Professional Services
 
Qlik Replicateでのタスクの定義と管理
Qlik Replicateでのタスクの定義と管理Qlik Replicateでのタスクの定義と管理
Qlik Replicateでのタスクの定義と管理QlikPresalesJapan
 
2015 03-12 道玄坂LT祭り第2回 Spark DataFrame Introduction
2015 03-12 道玄坂LT祭り第2回 Spark DataFrame Introduction2015 03-12 道玄坂LT祭り第2回 Spark DataFrame Introduction
2015 03-12 道玄坂LT祭り第2回 Spark DataFrame IntroductionYu Ishikawa
 
20190119 aws-study-pg-extension
20190119 aws-study-pg-extension20190119 aws-study-pg-extension
20190119 aws-study-pg-extensionToshi Harada
 
アプリ開発者、DB 管理者視点での Cloud Spanner 活用方法 | 第 10 回 Google Cloud INSIDE Games & App...
アプリ開発者、DB 管理者視点での Cloud Spanner 活用方法 | 第 10 回 Google Cloud INSIDE Games & App...アプリ開発者、DB 管理者視点での Cloud Spanner 活用方法 | 第 10 回 Google Cloud INSIDE Games & App...
アプリ開発者、DB 管理者視点での Cloud Spanner 活用方法 | 第 10 回 Google Cloud INSIDE Games & App...Google Cloud Platform - Japan
 
XPages 開発 Tips 百連発
XPages 開発 Tips 百連発XPages 開発 Tips 百連発
XPages 開発 Tips 百連発Mitsuru Katoh
 
R超入門機械学習をはじめよう
R超入門機械学習をはじめようR超入門機械学習をはじめよう
R超入門機械学習をはじめよう幹雄 小川
 
SQL Beginners Day #1 - SQL Server および Azure SQL のインストールと管理
SQL Beginners Day #1 - SQL Server および Azure SQL のインストールと管理SQL Beginners Day #1 - SQL Server および Azure SQL のインストールと管理
SQL Beginners Day #1 - SQL Server および Azure SQL のインストールと管理Oshitari_kochi
 
PostgreSQL 9.2 新機能 - OSC 2012 Kansai@Kyoto
PostgreSQL 9.2 新機能 - OSC 2012 Kansai@KyotoPostgreSQL 9.2 新機能 - OSC 2012 Kansai@Kyoto
PostgreSQL 9.2 新機能 - OSC 2012 Kansai@KyotoShigeru Hanada
 
開発者なのに運用で手がいっぱい? そんなあなたに贈る、 クラウド時代に最適な OSS の RDBMS ! Azure Database for MySQL...
開発者なのに運用で手がいっぱい? そんなあなたに贈る、 クラウド時代に最適な OSS の RDBMS ! Azure Database for MySQL...開発者なのに運用で手がいっぱい? そんなあなたに贈る、 クラウド時代に最適な OSS の RDBMS ! Azure Database for MySQL...
開発者なのに運用で手がいっぱい? そんなあなたに贈る、 クラウド時代に最適な OSS の RDBMS ! Azure Database for MySQL...Suguru Ito
 

Similar a A Tour of PostgreSQL (20)

SQLチューニング入門 入門編
SQLチューニング入門 入門編SQLチューニング入門 入門編
SQLチューニング入門 入門編
 
Jubatusでマルウェア分類
Jubatusでマルウェア分類Jubatusでマルウェア分類
Jubatusでマルウェア分類
 
Chugokudb18_2
Chugokudb18_2Chugokudb18_2
Chugokudb18_2
 
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
 
Databricksを初めて使う人に向けて.pptx
Databricksを初めて使う人に向けて.pptxDatabricksを初めて使う人に向けて.pptx
Databricksを初めて使う人に向けて.pptx
 
Let's scale-out PostgreSQL using Citus (Japanese)
Let's scale-out PostgreSQL using Citus (Japanese)Let's scale-out PostgreSQL using Citus (Japanese)
Let's scale-out PostgreSQL using Citus (Japanese)
 
外部データラッパによる PostgreSQL の拡張
外部データラッパによる PostgreSQL の拡張外部データラッパによる PostgreSQL の拡張
外部データラッパによる PostgreSQL の拡張
 
PostgreSQL10徹底解説
PostgreSQL10徹底解説PostgreSQL10徹底解説
PostgreSQL10徹底解説
 
OSS-DB Gold技術解説セミナー@db tech showcase 東京 2014
OSS-DB Gold技術解説セミナー@db tech showcase 東京 2014OSS-DB Gold技術解説セミナー@db tech showcase 東京 2014
OSS-DB Gold技術解説セミナー@db tech showcase 東京 2014
 
2019年度 若手技術者向け講座 DBMSの機能
2019年度 若手技術者向け講座 DBMSの機能2019年度 若手技術者向け講座 DBMSの機能
2019年度 若手技術者向け講座 DBMSの機能
 
PostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイント
PostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイントPostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイント
PostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイント
 
Qlik Replicateでのタスクの定義と管理
Qlik Replicateでのタスクの定義と管理Qlik Replicateでのタスクの定義と管理
Qlik Replicateでのタスクの定義と管理
 
2015 03-12 道玄坂LT祭り第2回 Spark DataFrame Introduction
2015 03-12 道玄坂LT祭り第2回 Spark DataFrame Introduction2015 03-12 道玄坂LT祭り第2回 Spark DataFrame Introduction
2015 03-12 道玄坂LT祭り第2回 Spark DataFrame Introduction
 
20190119 aws-study-pg-extension
20190119 aws-study-pg-extension20190119 aws-study-pg-extension
20190119 aws-study-pg-extension
 
アプリ開発者、DB 管理者視点での Cloud Spanner 活用方法 | 第 10 回 Google Cloud INSIDE Games & App...
アプリ開発者、DB 管理者視点での Cloud Spanner 活用方法 | 第 10 回 Google Cloud INSIDE Games & App...アプリ開発者、DB 管理者視点での Cloud Spanner 活用方法 | 第 10 回 Google Cloud INSIDE Games & App...
アプリ開発者、DB 管理者視点での Cloud Spanner 活用方法 | 第 10 回 Google Cloud INSIDE Games & App...
 
XPages 開発 Tips 百連発
XPages 開発 Tips 百連発XPages 開発 Tips 百連発
XPages 開発 Tips 百連発
 
R超入門機械学習をはじめよう
R超入門機械学習をはじめようR超入門機械学習をはじめよう
R超入門機械学習をはじめよう
 
SQL Beginners Day #1 - SQL Server および Azure SQL のインストールと管理
SQL Beginners Day #1 - SQL Server および Azure SQL のインストールと管理SQL Beginners Day #1 - SQL Server および Azure SQL のインストールと管理
SQL Beginners Day #1 - SQL Server および Azure SQL のインストールと管理
 
PostgreSQL 9.2 新機能 - OSC 2012 Kansai@Kyoto
PostgreSQL 9.2 新機能 - OSC 2012 Kansai@KyotoPostgreSQL 9.2 新機能 - OSC 2012 Kansai@Kyoto
PostgreSQL 9.2 新機能 - OSC 2012 Kansai@Kyoto
 
開発者なのに運用で手がいっぱい? そんなあなたに贈る、 クラウド時代に最適な OSS の RDBMS ! Azure Database for MySQL...
開発者なのに運用で手がいっぱい? そんなあなたに贈る、 クラウド時代に最適な OSS の RDBMS ! Azure Database for MySQL...開発者なのに運用で手がいっぱい? そんなあなたに贈る、 クラウド時代に最適な OSS の RDBMS ! Azure Database for MySQL...
開発者なのに運用で手がいっぱい? そんなあなたに贈る、 クラウド時代に最適な OSS の RDBMS ! Azure Database for MySQL...
 

Más de EDB

Cloud Migration Paths: Kubernetes, IaaS, or DBaaS
Cloud Migration Paths: Kubernetes, IaaS, or DBaaSCloud Migration Paths: Kubernetes, IaaS, or DBaaS
Cloud Migration Paths: Kubernetes, IaaS, or DBaaSEDB
 
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr UnternehmenDie 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr UnternehmenEDB
 
Migre sus bases de datos Oracle a la nube
Migre sus bases de datos Oracle a la nube Migre sus bases de datos Oracle a la nube
Migre sus bases de datos Oracle a la nube EDB
 
EFM Office Hours - APJ - July 29, 2021
EFM Office Hours - APJ - July 29, 2021EFM Office Hours - APJ - July 29, 2021
EFM Office Hours - APJ - July 29, 2021EDB
 
Benchmarking Cloud Native PostgreSQL
Benchmarking Cloud Native PostgreSQLBenchmarking Cloud Native PostgreSQL
Benchmarking Cloud Native PostgreSQLEDB
 
Las Variaciones de la Replicación de PostgreSQL
Las Variaciones de la Replicación de PostgreSQLLas Variaciones de la Replicación de PostgreSQL
Las Variaciones de la Replicación de PostgreSQLEDB
 
NoSQL and Spatial Database Capabilities using PostgreSQL
NoSQL and Spatial Database Capabilities using PostgreSQLNoSQL and Spatial Database Capabilities using PostgreSQL
NoSQL and Spatial Database Capabilities using PostgreSQLEDB
 
Is There Anything PgBouncer Can’t Do?
Is There Anything PgBouncer Can’t Do?Is There Anything PgBouncer Can’t Do?
Is There Anything PgBouncer Can’t Do?EDB
 
Data Analysis with TensorFlow in PostgreSQL
Data Analysis with TensorFlow in PostgreSQLData Analysis with TensorFlow in PostgreSQL
Data Analysis with TensorFlow in PostgreSQLEDB
 
Practical Partitioning in Production with Postgres
Practical Partitioning in Production with PostgresPractical Partitioning in Production with Postgres
Practical Partitioning in Production with PostgresEDB
 
A Deeper Dive into EXPLAIN
A Deeper Dive into EXPLAINA Deeper Dive into EXPLAIN
A Deeper Dive into EXPLAINEDB
 
IOT with PostgreSQL
IOT with PostgreSQLIOT with PostgreSQL
IOT with PostgreSQLEDB
 
A Journey from Oracle to PostgreSQL
A Journey from Oracle to PostgreSQLA Journey from Oracle to PostgreSQL
A Journey from Oracle to PostgreSQLEDB
 
Psql is awesome!
Psql is awesome!Psql is awesome!
Psql is awesome!EDB
 
EDB 13 - New Enhancements for Security and Usability - APJ
EDB 13 - New Enhancements for Security and Usability - APJEDB 13 - New Enhancements for Security and Usability - APJ
EDB 13 - New Enhancements for Security and Usability - APJEDB
 
Comment sauvegarder correctement vos données
Comment sauvegarder correctement vos donnéesComment sauvegarder correctement vos données
Comment sauvegarder correctement vos donnéesEDB
 
Cloud Native PostgreSQL - Italiano
Cloud Native PostgreSQL - ItalianoCloud Native PostgreSQL - Italiano
Cloud Native PostgreSQL - ItalianoEDB
 
New enhancements for security and usability in EDB 13
New enhancements for security and usability in EDB 13New enhancements for security and usability in EDB 13
New enhancements for security and usability in EDB 13EDB
 
Best Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQLBest Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQLEDB
 
Cloud Native PostgreSQL - APJ
Cloud Native PostgreSQL - APJCloud Native PostgreSQL - APJ
Cloud Native PostgreSQL - APJEDB
 

Más de EDB (20)

Cloud Migration Paths: Kubernetes, IaaS, or DBaaS
Cloud Migration Paths: Kubernetes, IaaS, or DBaaSCloud Migration Paths: Kubernetes, IaaS, or DBaaS
Cloud Migration Paths: Kubernetes, IaaS, or DBaaS
 
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr UnternehmenDie 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
 
Migre sus bases de datos Oracle a la nube
Migre sus bases de datos Oracle a la nube Migre sus bases de datos Oracle a la nube
Migre sus bases de datos Oracle a la nube
 
EFM Office Hours - APJ - July 29, 2021
EFM Office Hours - APJ - July 29, 2021EFM Office Hours - APJ - July 29, 2021
EFM Office Hours - APJ - July 29, 2021
 
Benchmarking Cloud Native PostgreSQL
Benchmarking Cloud Native PostgreSQLBenchmarking Cloud Native PostgreSQL
Benchmarking Cloud Native PostgreSQL
 
Las Variaciones de la Replicación de PostgreSQL
Las Variaciones de la Replicación de PostgreSQLLas Variaciones de la Replicación de PostgreSQL
Las Variaciones de la Replicación de PostgreSQL
 
NoSQL and Spatial Database Capabilities using PostgreSQL
NoSQL and Spatial Database Capabilities using PostgreSQLNoSQL and Spatial Database Capabilities using PostgreSQL
NoSQL and Spatial Database Capabilities using PostgreSQL
 
Is There Anything PgBouncer Can’t Do?
Is There Anything PgBouncer Can’t Do?Is There Anything PgBouncer Can’t Do?
Is There Anything PgBouncer Can’t Do?
 
Data Analysis with TensorFlow in PostgreSQL
Data Analysis with TensorFlow in PostgreSQLData Analysis with TensorFlow in PostgreSQL
Data Analysis with TensorFlow in PostgreSQL
 
Practical Partitioning in Production with Postgres
Practical Partitioning in Production with PostgresPractical Partitioning in Production with Postgres
Practical Partitioning in Production with Postgres
 
A Deeper Dive into EXPLAIN
A Deeper Dive into EXPLAINA Deeper Dive into EXPLAIN
A Deeper Dive into EXPLAIN
 
IOT with PostgreSQL
IOT with PostgreSQLIOT with PostgreSQL
IOT with PostgreSQL
 
A Journey from Oracle to PostgreSQL
A Journey from Oracle to PostgreSQLA Journey from Oracle to PostgreSQL
A Journey from Oracle to PostgreSQL
 
Psql is awesome!
Psql is awesome!Psql is awesome!
Psql is awesome!
 
EDB 13 - New Enhancements for Security and Usability - APJ
EDB 13 - New Enhancements for Security and Usability - APJEDB 13 - New Enhancements for Security and Usability - APJ
EDB 13 - New Enhancements for Security and Usability - APJ
 
Comment sauvegarder correctement vos données
Comment sauvegarder correctement vos donnéesComment sauvegarder correctement vos données
Comment sauvegarder correctement vos données
 
Cloud Native PostgreSQL - Italiano
Cloud Native PostgreSQL - ItalianoCloud Native PostgreSQL - Italiano
Cloud Native PostgreSQL - Italiano
 
New enhancements for security and usability in EDB 13
New enhancements for security and usability in EDB 13New enhancements for security and usability in EDB 13
New enhancements for security and usability in EDB 13
 
Best Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQLBest Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQL
 
Cloud Native PostgreSQL - APJ
Cloud Native PostgreSQL - APJCloud Native PostgreSQL - APJ
Cloud Native PostgreSQL - APJ
 

Último

AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfFumieNakayama
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)UEHARA, Tetsutaro
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)Hiroshi Tomioka
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?akihisamiyanaga1
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...博三 太田
 
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)NTT DATA Technology & Innovation
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfFumieNakayama
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineerYuki Kikuchi
 

Último (8)

AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
 
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
 

A Tour of PostgreSQL

  • 1. A Tour of PostgreSQL Masahiko Sawada PostgreSQL Conference Japan 2020 1
  • 2. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.2 Agenda • モチベーション • OSから⾒見見たPostgreSQL • 「データ」を中⼼心にPostgreSQL内部構造を紐解く • PostgreSQLの構成 • SQL処理理 • テーブル/インデックス • トランザクション • まとめ
  • 4. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.4 ユーザだけど…内部構造を知ることは必要? Feel the breath of PostgreSQL 予想している聴講者層 • これからPostgreSQLを使いたい/勉強したい • 実際に運用している このような方々にも役に立つ! • 他のRDBMSとの違いを知りたい • PostgreSQLをいじってみたい
  • 5. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.5 用語紹介と概要 データベースクラスタ データベース スキーマ a.users b.users …
  • 7. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.7 PostgreSQLのプロセス群 OSから見れば一つのプログラム $ ll -h bin/postgres -rwxr-xr-x 1 masahiko masahiko 25M Oct 28 18:10 bin/postgres $ ps x | grep postgres 72572 - Ss 0:00.89 /usr/home/masahiko/pgsql/master/bin/postgres -D data -p 5432 72574 - Ss 0:00.39 postgres: checkpointer (postgres) 72575 - Ss 0:01.82 postgres: background writer (postgres) 72576 - Ss 0:01.95 postgres: walwriter (postgres) 72577 - Is 0:00.34 postgres: autovacuum launcher (postgres) 72578 - Ss 0:01.15 postgres: stats collector (postgres) 72579 - Is 0:00.03 postgres: logical replication launcher (postgres) 75547 - Ss 0:00.00 postgres: masahiko postgres [local] idle (postgres)
  • 8. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.8 PostgreSQLのプロセス群 OSから見れば一つのプログラム Backend Postmaster Backend Backend Checkpointer Wal writer Bg writer LoggerArchiver Autovacuum launcher/worker Wal sender/receiver Stats collector Logical repl. launcher/worker Clients
  • 9. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.9 PostgreSQLのディレクトリ構造 データベースクラスタの実体は一つのディレクトリ $ ls ${PGDATA} PG_VERSION pg_dynshmem pg_notify pg_stat_tmp pg_xact base pg_hba.conf pg_replslot pg_subtrans postgresql.auto.conf global pg_ident.conf pg_serial pg_tblspc postgresql.conf pg_commit_ts pg_logical pg_snapshots pg_twophase postmaster.opts pg_multixact pg_stat pg_wal postmaster.pid
  • 10. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.10 少しずつ見えてきた! • クライアントが一つのデータベースに接続すると、一つのプロセスがSQLを処理する • PostgreSQLは様々な役割を持ったプロセスの集合 • データベースクラスタはOSからみると1つのディレクトリ
  • 12. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.12 本発表でのアプローチ • 1つのSQLを例に、PostgreSQLがどのよ うに処理し、データを取得するのかを順番 に解説 • 必要に応じて少し脱線し、関連する機能や 仕組みを紹介 • 最終的には大まかな全体像が掴める • PostgreSQL13を元に解説します https://jp.depositphotos.com/v/3oi2kp.html
  • 13. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.13 SELECT * FROM users WHERE id = 100; Table "public.users" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+--------- id | integer | | not null | name | text | | | Indexes: "users_pkey" PRIMARY KEY, btree (id)
  • 14. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.14 SQLを理解する SELECT * FROM users WHERE id = 100; • パーサが受け取ったSQLの構文を解析する • SELECT文?UPDATE文?文法は正しい? • ERROR : syntax error at or near xxxx" • 文法的に正しければOK • テーブルや列が本当にあるかどうかは気にしない Parser SelectStmt • targetList • fromClause
 • whereClause A_STAR = 100id users
  • 15. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.15 パースツリーを解析する • アナライザがテーブルや列の存在チェックをする • ERROR: relation "badtable" does not exist at character 15 • システムカタログを使う • (リライタ(Rewriter)がクエリの書き換えを行う) Parser Analyzer
  • 16. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.16 システムカタログ PostgreSQLのメタデータが詰まった内部テーブル • 実体は通常のテーブルと同じ • PostgreSQL13には62個ある • テーブル、テーブルにある列、インデックス、データ型、演算子、トリガー、関数、ユーザ、型変 換など、内部的な情報はすべてシステムカタログにある • テーブルとしてpg_catalogスキーマに存在するので、SQLでアクセスできる • 例)テーブルを検索する時: • SELECT relname FROM pg_class WHERE relname = user and relkind = r ; Parser Analyzer
  • 17. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.17 例)テーブルを作った時 CREATE TABLE users (id INT PRIMARY KEY, name TEXT); • テーブル、インデックス(主キー) → pg_class • インデックス(主キー) → pg_index • id列、name列 → pg_attribute • 主キー制約 → pg_constraint • 依存関係(テーブルを削除したら主キーも削除する) → pg_depend Parser Analyzer
  • 18. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.18 [少し脱線] 共有する/しないシステムカタログ • データベース、ユーザなどはデータベースクラスタ全体で共有するので対応するカタログ (pg_database, pg_authidなど)は共有するシステムカタログ(pg_class.isshared = t ) • その他の定義情報(テーブル、トリガなど)は、データベース毎に存在する Parser Analyzer
  • 19. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.19 どのようにデータを取得するか? • テーブルの状態、検索条件によって最適なアクセス方法は異なる • テーブルにはたった10行しか入っていないかもしれない • 1億行の内から一つの行を取得するかもしれない • プランナが最適(だと思われれる)な実行プランを計画する • 実行プラン:どのように目的のデータを取得するのかをまとめた計画 Parser Analyzer Planner
  • 20. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.20 代表的なアクセス方法 • Seq Scan • テーブルの(物理的な)先頭から順番にスキャンする • テーブル内の多くの割合のデータを取得する時に高速 • Index Scan • インデックスをスキャンしてから、テーブルをスキャンする • テーブル内の一部のデータを取得する時に高速 Parser Analyzer Planner
  • 21. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.21 PostgreSQLがサポートする実行ノード Aggregate ForeignScan HashJoin MergeAppend Result SubQueryScan WorkTableScan BitmapAnd FunctionScan IncrementalSort MergeJoin SampleScan TableFuncScan BitmapHeapSc an Gather IndexOnlyScan NamedTupleSt oreScan SeqScan TidScan BitmapIndexSc an GatherMerge Limit NestLoop SetOp Unique BitmapOr Group LockRows ProjectSet Sort ValueScan CteScan Hash Material RecursiveUnion SubPlan WindowAgg Parser Analyzer Planner
  • 22. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.22 どうやってプランを決めるのか? まずは選択度 • テーブルの統計情報を利用して最適なアクセス方法を決める • 列の固有値 : (例)一意の列の固有値は-1。都道府県を格納する列の固有値は=47 • 列の最頻値 : 最も多いデータとその割合のペアのリスト • most_common_vals : {user0,user1,user2,user3,user4,user5,user6,user7,user8,user9} • most_common_freqs : {0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1} • 列のヒストグラム : 度数を均一にしたときのBinの境界値 • histogram_bounds : {1,100,200,300,400,500,600,700,800,900,1000} • pg_statsビュー • 統計情報はANALYZEで更新 Parser Analyzer Planner
  • 23. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.23 行数の見積もり 選択度(Selectivity)の決定 等価条件(WHERE id = 100)の場合 • 列が主キー(固有値が-1)なら簡単 • 必ず0行か1行 • 最頻値から探す • あればその割合が選択度 • なければ「それ以外の割合」から選択度を求める Parser Analyzer Planner 範囲条件(WHERE id < 100)の場合 • ヒストグラムから100を含むBinの位置を求める • 「そのBin」+「それより前のBin」が占める割合を選択 度とする 80 120 200 500
  • 24. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.24 コストの見積もり (推定)取得行数から処理コストを計算する • Seq Scanの場合 • 取得行数によらず一定 • 必要なデータ=テーブルサイズ • Index Scanの場合 • 取得行数によってコストが異なる • 必要なデータ=インデックス内を探す時に必要なデータ+
          テーブル内の検索対象が入っているデータ Parser Analyzer Planner Index Scan Seq Scan Cost # of tuples
  • 25. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.25 [少し脱線] 結合の場合はどうなる? • プランはボトムアップにプラン候補を集め ながら決めていく • テーブルのスキャン方法→結合方法 →… Parser Analyzer Planner https://www.pgcon.org/2016/schedule/attachments/433_PGCON2016_beyond_explain.pdf
  • 26. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.26 EXPLAIN 実行プランの確認 cost : プランナが見積もったコスト rows : プランナが見積もった取得行数 width : プランナが見積もった行のサイズ =# EXPLAIN SELECT * FROM users WHERE id = 100; QUERY PLAN ------------------------------------------------------------------------- Index Scan using users_pkey on users (cost=0.28..8.29 rows=1 width=10) Index Cond: (id = 100) (2 rows) Parser Analyzer Planner
  • 27. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.27 計画したプランを実行する • エグゼキュータが受け取ったプランを実行する • PostgreSQLのエグゼキュータはPull方式 • 再帰的にノードを実行していく • 全ての実行ノードは1行返す • EXPLAIN ANALYZEでプランと一緒に実行時間を確認できる Parser Analyzer Planner Executor
  • 28. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.28 プランはどのように実行されるのか? QUERY PLAN ------------------------------------------------------------------------- Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1) Index Cond: (id = 100) (2 rows) Executor Parser Analyzer Planner Executor
  • 29. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.29 プランはどのように実行されるのか? 1⾏行行ちょうだい QUERY PLAN ------------------------------------------------------------------------- Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1) Index Cond: (id = 100) (2 rows) Parser Analyzer Planner Executor
  • 30. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.30 プランはどのように実行されるのか? 1⾏行行ちょうだい QUERY PLAN ------------------------------------------------------------------------- Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1) Index Cond: (id = 100) (2 rows) Parser Analyzer Planner Executor
  • 31. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.31 プランはどのように実行されるのか? QUERY PLAN ------------------------------------------------------------------------- Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1) Index Cond: (id = 100) (2 rows) どうぞ 最初の⾏行行を 取得するまでに かかった時間(ms) Parser Analyzer Planner Executor
  • 32. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.32 プランはどのように実行されるのか? 1⾏行行ちょうだい QUERY PLAN ------------------------------------------------------------------------- Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1) Index Cond: (id = 100) (2 rows) Parser Analyzer Planner Executor
  • 33. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.33 プランはどのように実行されるのか? 1⾏行行ちょうだい QUERY PLAN ------------------------------------------------------------------------- Index Scan using users_pkey on users (actual time=0.020..0.021 rows=1 loops=1) Index Cond: (id = 100) (2 rows) もうないよ 処理理が完了了 した時間 Parser Analyzer Planner Executor
  • 34. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.34 [少し脱線] ソートの場合はどうなる? SELECT * FROM users WHERE id < 100 ORDER BY id; QUERY PLAN --------------------------------------------------------------------------------------- Sort (actual time=1.646..1.656 rows=99 loops=1) Sort Key: name Sort Method: quicksort Memory: 30kB -> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1) Index Cond: (id < 100) ソートした結果を返したい Parser Analyzer Planner Executor
  • 35. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.35 [少し脱線] ソートの場合はどうなる? SELECT * FROM users WHERE id < 100 ORDER BY id; QUERY PLAN --------------------------------------------------------------------------------------- Sort (actual time=1.646..1.656 rows=99 loops=1) Sort Key: name Sort Method: quicksort Memory: 30kB -> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1) Index Cond: (id < 100) 1⾏行行取得! Parser Analyzer Planner Executor
  • 36. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.36 [少し脱線] ソートの場合はどうなる? SELECT * FROM users WHERE id < 100 ORDER BY id; QUERY PLAN --------------------------------------------------------------------------------------- Sort (actual time=1.646..1.656 rows=99 loops=1) Sort Key: name Sort Method: quicksort Memory: 30kB -> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1) Index Cond: (id < 100) 1⾏行行取得!! Parser Analyzer Planner Executor
  • 37. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.37 [少し脱線] ソートの場合はどうなる? SELECT * FROM users WHERE id < 100 ORDER BY id; QUERY PLAN --------------------------------------------------------------------------------------- Sort (actual time=1.646..1.656 rows=99 loops=1) Sort Key: name Sort Method: quicksort Memory: 30kB -> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1) Index Cond: (id < 100) 1⾏行行取得!!! Parser Analyzer Planner Executor
  • 38. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.38 [少し脱線] ソートの場合はどうなる? SELECT * FROM users WHERE id < 100 ORDER BY id; QUERY PLAN --------------------------------------------------------------------------------------- Sort (actual time=1.646..1.656 rows=99 loops=1) Sort Key: name Sort Method: quicksort Memory: 30kB -> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1) Index Cond: (id < 100) 1⾏行行取得!!!! Parser Analyzer Planner Executor
  • 39. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.39 [少し脱線] ソートの場合はどうなる? SELECT * FROM users WHERE id < 100 ORDER BY id; QUERY PLAN --------------------------------------------------------------------------------------- Sort (actual time=1.646..1.656 rows=99 loops=1) Sort Key: name Sort Method: quicksort Memory: 30kB -> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1) Index Cond: (id < 100) 1⾏行行取得!!!!!× 99 Parser Analyzer Planner Executor
  • 40. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.40 [少し脱線] ソートの場合はどうなる? SELECT * FROM users WHERE id < 100 ORDER BY id; QUERY PLAN --------------------------------------------------------------------------------------- Sort (actual time=1.646..1.656 rows=99 loops=1) Sort Key: name Sort Method: quicksort Memory: 30kB -> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1) Index Cond: (id < 100) 全部そろったからソートするよ Parser Analyzer Planner Executor
  • 41. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.41 [少し脱線] ソートの場合はどうなる? SELECT * FROM users WHERE id < 100 ORDER BY id; QUERY PLAN --------------------------------------------------------------------------------------- Sort (actual time=1.646..1.656 rows=99 loops=1) Sort Key: name Sort Method: quicksort Memory: 30kB -> Index Scan using users_pkey on users (actual time=0.025..0.067 rows=99 loops=1) Index Cond: (id < 100) やっと1⾏行行返せる Parser Analyzer Planner Executor
  • 42. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.42 [少し脱線] 結合の場合はどうなる? ユーザ(name = user_100 )の給料を検索する =# EXPLAIN SELECT * FROM users, salary WHERE users.id = salary.id and users.name = ‘user_100'; QUERY PLAN -------------------------------------------------------------------------------- Nested Loop (cost=0.29..26.80 rows=1 width=18) -> Seq Scan on users (cost=0.00..18.50 rows=1 width=10) Filter: (name = 'user_100'::text) -> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8) Index Cond: (id = users.id) (5 rows) users salary
  • 43. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.43 [少し脱線] 結合の場合はどうなる? ユーザ(name = user_100 )の給料を検索する QUERY PLAN -------------------------------------------------------------------------------- Nested Loop (cost=0.29..26.80 rows=1 width=18) -> Seq Scan on users (cost=0.00..18.50 rows=1 width=10) Filter: (name = 'user_100'::text) -> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8) Index Cond: (id = users.id) (5 rows) まずは内部表(駆動表) から1⾏行行とるよ Parser Analyzer Planner Executor
  • 44. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.44 [少し脱線] 結合の場合はどうなる? ユーザ(name = user_100 )の給料を検索する QUERY PLAN -------------------------------------------------------------------------------- Nested Loop (cost=0.29..26.80 rows=1 width=18) -> Seq Scan on users (cost=0.00..18.50 rows=1 width=10) Filter: (name = 'user_100'::text) -> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8) Index Cond: (id = users.id) (5 rows) まずは内部表(駆動表) から1⾏行行とるよ Parser Analyzer Planner Executor
  • 45. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.45 [少し脱線] 結合の場合はどうなる? ユーザ(name = user_100 )の給料を検索する QUERY PLAN -------------------------------------------------------------------------------- Nested Loop (cost=0.29..26.80 rows=1 width=18) -> Seq Scan on users (cost=0.00..18.50 rows=1 width=10) Filter: (name = 'user_100'::text) -> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8) Index Cond: (id = users.id) (5 rows) 先程取った⾏行行のID列列の 値を使って次は内部表 から1⾏行行とるよ Parser Analyzer Planner Executor
  • 46. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.46 [少し脱線] 結合の場合はどうなる? ユーザ(name = user_100 )の給料を検索する QUERY PLAN -------------------------------------------------------------------------------- Nested Loop (cost=0.29..26.80 rows=1 width=18) -> Seq Scan on users (cost=0.00..18.50 rows=1 width=10) Filter: (name = 'user_100'::text) -> Index Scan using salary_pkey on salary (cost=0.29..8.30 rows=1 width=8) Index Cond: (id = users.id) (5 rows) 1⾏行行結合できたので、 エグゼキュータに返すよ Parser Analyzer Planner Executor
  • 47. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.47 ここまでのまとめ パーサがSQLを構文解析し、 アナライザがシステムカタログを使ってテーブルや列の有無を確認し、 プランナが統計情報を使って実行プランを計画し、 エグゼキュータが実行プランを実行する。 Parser Analyzer Planner Executor
  • 48. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.48 実際にデータを読む PostgreSQLはデータをどのように管理するのか? • テーブルやインデックスの実体は1つのファイル • 8kB(ブロック)単位で読み書きする • 共有バッファに読んだデータをキャッシュする • OSのファイルキャッシュも利用する • shared_buffersで調整 • 満杯になったら入れ替える(Clock Sweep) • CheckpointerやBgwriterが変更したデータをディス クに書き込む File Cache (OS) Disk Executor (PostgreSQL) SharedBuffer (PostgreSQL)
  • 49. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.49 PostgreSQLのメモリ 共有するメモリとしないメモリ • プロセス間で共有するデータは共有メモリに確保 • 共有バッファ、ロック、管理情報など • 例)shared_buffersは共有メモリとして一つ確保 • プロセス内だけで使うメモリはプロセス毎に確保 • プランニング時に使うメモリ、ソート等のSQL実行時に使うメモリ、システムカタログの キャッシュなど • 例)work_memはプロセス毎に確保する
  • 50. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.50 ここまでのまとめ • PostgreSQLのプロセス構造が分かってきた • postmasterやbackendプロセス • SQLがどのように処理されるのかが分かってきた • パーサ、アナライザ、プランナ、エグゼキュータ • EXPLAINの読み方、どのように実行されるのかも分かってきた • PostgreSQLはデータを共有バッファ、OSキャッシュ、ディスクに持っている
  • 51. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.51 PostgreSQLを完全に理解した? ワタシポストグレスチョットワカル • これでは「SQLでアクセスできるデータストア」の構造を理解しただけ • RDBMSの機能の一部分にすぎない • 実際には複数クライアントが同時にアクセスするし、途中でクラッシュするかもしれない • データの不整合が起きない • データが失われない
  • 53. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.53 SELECTとUPDATEが同時に起きたら? • UPDATE途中のデータを見たくない • 1回目のSELECTと2回目でSELECTで同じ結果が見たい • ACIDのI (Isolation) • UPDATE中はSELECTできない、というのは性能的にNG • PostgreSQLは変更前、変更後の両方を持つことで実現している • マルチバージョンを使った同時実行制御(MVCC)
  • 54. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.54 MVCC (Multi-Version Concurrency Control) • 1つのデータに対し複数のバージョンを持てば、一方が変更している間にもう一方が読むことがで きる • PostgreSQLではUPDATEを「DELETE+INSERT」で実現 • UPDATEでもテーブルの物理的なサイズは大きくなっていく(テーブルの肥大化) • 追記型アーキテクチャ • ROLLBACKしたデータ(誰からも参照されないデータ)もテーブルに残る • うまくフィルタリングしながらデータを読む
  • 55. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.55 追記型とVacuum • Vacuumで不要になった領域を回収し再利用できるようにする • 物理的なサイズは小さくならない Appel Ball Cat Appel Ball Cat Apple Ball Cat Apple Dog Ball Cat Apple UPDATE VACUUM INSERT
  • 56. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.56 Vacuumのせいで遅い? Q. Vacuum中は読み書きできる? A. Yes Q. Vacuumは手動、もしくはcronとかで実行する必要がある? A. No、自動Vacuum(とHOT)があるので基本的には放っておいて大丈夫です。 Q. ゴミを確認するためにテーブルをすべてスキャンする? A. No、ゴミがないと分かっている箇所はスキップします。
  • 57. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.57 自動VacuumはどうやってVacuumするテーブルを見つける? • 稼働統計情報を使ってVacuumするべきかどうかを判断する • pg_stat_all_tables/indexes/functionsビュー • テーブル内の生きているタプル数、死んでいる(Vacuumできる)タプル数、最後に Vacuumされた時間、Vacuumされた回数など • テーブルがSeqScanされた回数、IndexScanされた回数など
  • 58. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.58 [少し脱線] UNDOログ OracleやMySQLとの違い • OracleやMySQLもMVCCを使っているが古いデータをUNDOログに持つ • 要らなくなったらUNDOログをまとめて削除すれば良い • テーブル内にはゴミは溜まらない • テーブルをチェックする必要はない • テーブルの肥大化は起きにくい方式 • UNDOログの方が優れている、とも言えない • 例)ROLLBACKに時間がかかる(ゴミだと思っていたけどやっぱり違った)
  • 59. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.59 トランザクションのステータス COMMIT、ABORTはどうやって判断する? • ACIDのA(Atomicity) • PostgreSQLは各トランザクションにID(単調増加の非負整数)を割り当てる(XID) • コミットログと呼ばれるファイルに、各トランザクションがCommit/Abortした情報を格納する(2bits/txn) • たくさんテーブルを更新してもたった2bitの更新でCommit/Abortを記録する • 各タプルには「INSERTされたトランザクションのXID(=xmin)」と「DELETEされたトランザクションの XID(=xmax)」の両方を持つ • タプルから取得したXIDからステータスを確認して、「COMMITされたINSERT」なのか「ABORTされた INSERT」なのかを確認する 90 Apple120 Commit log 00100 … 0110
  • 60. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.60 タプルの可視性 ゴミも混ざっているテーブル内のタプルをどのように正しく読むか? • モチベーションの例)トランザクション内では、一度読んだデータをもう一度読んでも同じデータが返ってきて ほしい! • REPEATABLE READに相当する • これまで紹介した仕組みを組み合わせる(マルチバージョン、XID、コミットログ) • 基本的な考え方 • 自分のXIDより小さいXIDで追加されたデータは読める(過去に追加された) • 自分のXIDより小さいXIDで削除されたデータは読めない(過去に削除された) • ただし例外も・・・ • 現在進行中のトランザクションによって行われた(たとえXIDが小さくても)変更は見てはいけない
  • 61. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.61 データベースのスナップショットを見る データベースある時点を切り取ったようなデータ 100 Apple XID = 100: INSERT ‘Apple’; xmin (insert) xmax (delete)
  • 62. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.62 データベースのスナップショットを見る データベースある時点を切り取ったようなデータ 100 Apple 100 Ball XID = 100: INSERT ‘Apple’; INSERT ‘Ball’; xmin (insert) xmax (delete)
  • 63. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.63 データベースのスナップショットを見る データベースある時点を切り取ったようなデータ 100 200 Apple 100 Ball 200 Cat XID = 100: INSERT ‘Apple’; INSERT ‘Ball’; XID = 200: UPDATE ‘Apple’ -> ‘Cat’; xmin (insert) xmax (delete)
  • 64. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.64 データベースのスナップショットを見る データベースある時点を切り取ったようなデータ 100 200 Apple 100 Ball 200 Cat XID = 100: INSERT ‘Apple’; INSERT ‘Ball’; XID = 200: UPDATE ‘Apple’ -> ‘Cat’; xmin (insert) xmax (delete) XID = 150
  • 65. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.65 データベースのスナップショットを見る データベースある時点を切り取ったようなデータ 100 200 Apple 100 Ball 200 Cat XID = 100: INSERT ‘Apple’; INSERT ‘Ball’; XID = 200: UPDATE ‘Apple’ -> ‘Cat’; xmin (insert) xmax (delete) XID = 150 xmin = 100 → 過去にINSERTされた xmax = 200 → 未来でDELETEされる
 見える!!
  • 66. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.66 データベースのスナップショットを見る データベースある時点を切り取ったようなデータ 100 200 Apple 100 Ball 200 Cat XID = 100: INSERT ‘Apple’; INSERT ‘Ball’; XID = 200: UPDATE ‘Apple’ -> ‘Cat’; xmin (insert) xmax (delete) XID = 150 xmin = 100 → 過去にINSERTされた まだ誰からも削除されていない
 見える!!
  • 67. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.67 データベースのスナップショットを見る データベースある時点を切り取ったようなデータ 100 200 Apple 100 Ball 200 Cat XID = 100: INSERT ‘Apple’; INSERT ‘Ball’; XID = 200: UPDATE ‘Apple’ -> ‘Cat’; xmin (insert) xmax (delete) XID = 150 xmin = 200 → 未来にINSERTされる まだ誰からも削除されていない
 見えない!!
  • 69. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.69 すべてはログに書いてある • コミット済みのデータを失わない! • ACIDのAとD(Durability) • WAL (Write Ahead Logging) • データ(テーブルやインデックス)の変更をディスクに反映する前に、必ず対応するロ グをディスクに反映する • COMMIT後にクラッシュしてもログを再生すれば元通り(ロールフォワード)
  • 70. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.70 Shared Buffer Disk WAL(Disk)
  • 71. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.71 Disk Apple INSERT ‘Apple’; WAL(Disk) INS: Apple Shared Buffer
  • 72. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.72 Disk Apple Ball INSERT ‘Apple’;
 INSERT ‘Ball’; WAL(Disk) INS: Apple INS: Ball Shared Buffer
  • 73. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.73 Disk Apple Ball INSERT ‘Apple’;
 INSERT ‘Ball’; WAL(Disk) INS: Apple INS: Ball Apple Ball Sync Shared Buffer
  • 74. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.74 Disk WAL(Disk) Apple Ball Alice INSERT ‘Apple’;
 INSERT ‘Ball’; UPDATE ‘Apple’ -> ‘Alice’; INS: Apple INS: Ball UPD: 1 -> ‘Alice’ Apple Ball Shared Buffer
  • 75. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.75 Disk WAL(Disk) Apple Ball Alice Cat INSERT ‘Apple’;
 INSERT ‘Ball’; UPDATE ‘Apple’ -> ‘Alice’; INSERT ‘Cat’; INS: ‘Apple’ at 1 INS: ‘Ball’ at 2 UPD: 1 -> ‘Alice’ at 3 INS: ‘Cat’ at 4 Apple Ball Shared Buffer
  • 76. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.76 Disk WAL(Disk) Apple Ball Alice Cat INSERT ‘Apple’;
 INSERT ‘Ball’; UPDATE ‘Apple’ -> ‘Alice’; INSERT ‘Cat’; INS: ‘Apple’ at 1 INS: ‘Ball’ at 2 UPD: 1 -> ‘Alice’ at 3 INS: ‘Cat’ at 4 Apple Ball Shared Buffer
  • 77. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.77 Disk WAL(Disk) INSERT ‘Apple’;
 INSERT ‘Ball’; UPDATE ‘Apple’ -> ‘Alice’; INSERT ‘Cat’; INS: ‘Apple’ at 1 INS: ‘Ball’ at 2 UPD: 1 -> ‘Alice’ at 3 INS: ‘Cat’ at 4 Apple Ball Shared Buffer
  • 78. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.78 Disk WAL(Disk) INSERT ‘Apple’;
 INSERT ‘Ball’; UPDATE ‘Apple’ -> ‘Alice’; INSERT ‘Cat’; INS: ‘Apple’ at 1 INS: ‘Ball’ at 2 UPD: 1 -> ‘Alice’ at 3 INS: ‘Cat’ at 4 Apple Ball Shared Buffer
  • 79. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.79 Disk WAL(Disk) INSERT ‘Apple’;
 INSERT ‘Ball’; UPDATE ‘Apple’ -> ‘Alice’; INSERT ‘Cat’; INS: ‘Apple’ at 1 INS: ‘Ball’ at 2 UPD: 1 -> ‘Alice’ at 3 INS: ‘Cat’ at 4 Apple Ball Apple Ball Alice Shared Buffer
  • 80. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.80 Disk WAL(Disk) INSERT ‘Apple’;
 INSERT ‘Ball’; UPDATE ‘Apple’ -> ‘Alice’; INSERT ‘Cat’; INS: ‘Apple’ at 1 INS: ‘Ball’ at 2 UPD: 1 -> ‘Alice’ at 3 INS: ‘Cat’ at 4 Recovery !! (Roll Forward) Apple Ball Alice Cat Apple Ball Shared Buffer
  • 81. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.81 Disk WAL(Disk) INSERT ‘Apple’;
 INSERT ‘Ball’; UPDATE ‘Apple’ -> ‘Alice’; INSERT ‘Cat’; INS: ‘Apple’ at 1 INS: ‘Ball’ at 2 UPD: 1 -> ‘Alice’ at 3 INS: ‘Cat’ at 4 Recovery !! (Roll Forward) Apple Ball Alice Cat Apple Ball Alice Cat Sync Shared Buffer
  • 82. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.82 すべてはログに書いてある • クラッシュしてもCOMMIT済みのデータは失われない! • WALはSequential Writeになる • WALはpg_wal配下に格納される。絶対消さないで! $ ls ${PGDATA}/pg_wal/ 00000001000000000000002B 000000010000000000000032 archive_status 00000001000000000000002C 000000010000000000000033 00000001000000000000002D 000000010000000000000034 00000001000000000000002E 000000010000000000000035 00000001000000000000002F 000000010000000000000036 000000010000000000000030 000000010000000000000037 000000010000000000000031 000000010000000000000038
  • 84. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.84 今回触れられなかった部分 • ロック • トランザクション分離レベル • 補助プロセス群(Wal writer、Checkpointerなど) • その他高度な機能(レプリケーション、パラレルクエリ、パーティショニング、など) • きっと次の講演で聞けるはず!
  • 85. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.85 まとめ Feel the breath of PostgreSQL • PostgreSQLがどのようにSQLを処理し、データを取得しているのか • どのように同時実行制御しているのか • COMMIT済みのデータが決して失われない仕組み • PostgreSQLはもちろんRDBMSの勉強としても役立つ • この後の講演の理解度もぜんぜん違う(はず)!
  • 86. © Copyright EnterpriseDB Corporation, 2020. All rights reserved.86 Thank you Masahiko Sawada masahiko.sawada@enterprisedb.com