SlideShare a Scribd company logo
v9.5の新機能
Custom Scan/Join Interface
KaiGai Kohei <kaigai@kaigai.gr.jp>
PostgreSQL - The most advanced
Open Source Relational Database
v9.5 New Feature
PostgreSQL Unconference/Tokyo May-20152
Custom-Scan
Interface
Custom-Join
Interface
PostgreSQL - The most advanced
Open Source Relational Database
Custom Scan/Join Interfaceが実現する事
▌クエリ実行計画の一部を、拡張モジュール実装の “何か” で置き換える
▌何を置き換える事ができるのか?
 Scan ... set_rel_pathlist_hook で候補パス追加
 Join ... set_join_pathlist_hook で候補パス追加
 Others ... planner_hookを使ってインジェクションすれば何でもアリ。
 局所最適にならないよう、注意は必要だが…。
▌応用例
 GPUアクセラレーションによる全体的なRDBMS性能の底上げ。
 PG-Strom (オタワで発表するんでヨロ)
 特定のテーブル同士のJoinをMaterialized Viewスキャンに透過的に置換え。
 特定のテーブルのうち、参照頻度の高い列だけを列指向形式でキャッシュ
 Scan/Joinの中間結果から統計情報を採取。値によるヒストグラム作成。
 ......など
PostgreSQL Unconference/Tokyo May-20153
PostgreSQL - The most advanced
Open Source Relational Database
クエリ実行計画
postgres=# EXPLAIN SELECT cat, AVG(x)
FROM t0 NATURAL JOIN t1 RIGHT JOIN t2 ON t0.aid=t2.bid OR t0.bid=t2.bid
WHERE atext like '%abc%' GROUP BY cat;
QUERY PLAN
------------------------------------------------------------------------------
HashAggregate (cost=979149.25..979149.58 rows=26 width=12)
Group Key: t0.cat
-> Nested Loop (cost=834.05..979139.33 rows=1985 width=12)
Join Filter: ((t0.aid = t2.bid) OR (t0.bid = t2.bid))
-> Seq Scan on t2 (cost=0.00..734.00 rows=40000 width=4)
-> Materialize (cost=834.05..281207.82 rows=996 width=20)
-> Hash Join (cost=834.05..281202.84 rows=996 width=20)
Hash Cond: (t0.aid = t1.aid)
-> Seq Scan on t0 (cost=0.00..242858.60 rows=10000060 width=20)
-> Hash (cost=834.00..834.00 rows=4 width=4)
-> Seq Scan on t1 (cost=0.00..834.00 rows=4 width=4)
Filter: (atext ~~ '%abc%'::text)
(12 rows)
PostgreSQL Unconference/Tokyo May-20154
PostgreSQL - The most advanced
Open Source Relational Database
PostgreSQL クエリ処理の流れ (1/2)
▌リレーションに対する候補パスを列挙
 Base Relation
 Join Relation
▌プラナーが最も実行コストの小さな
パスを選択。
全てのテーブルを単一のJoin-Treeに
収めるまで結合順を試行錯誤
▌実行パスを元に、実行計画を作成
PostgreSQL Unconference/Tokyo May-20155
SQL
クエリ・パーサ (構文解析)
クエリ・オプティマイザ (最適化)
クエリ・エグゼキュータ(実行)
結果セット
PostgreSQL - The most advanced
Open Source Relational Database
PostgreSQL クエリ処理の流れ (2/2)
▌ExecInitNode
 実行ノードの初期化
▌ExecProcNode
 一行を生成して TupleTableSlot を返す
▌ExecEndNode
 実行ノードの終了処理
▌その他
 ExecReScan
 ExecMarkPos (optional)
 ExecRestrPos (optional)
PostgreSQL Unconference/Tokyo May-20156
SQL
クエリ・パーサ (構文解析)
クエリ・オプティマイザ (最適化)
クエリ・エグゼキュータ(実行)
結果セット
PostgreSQL - The most advanced
Open Source Relational Database
Custom Scan/Joinを使うには (1/3)
▌候補パスを追加する
typedef void (*set_rel_pathlist_hook_type) (PlannerInfo *root,
RelOptInfo *rel,
Index rti,
RangeTblEntry *rte);
extern PGDLLIMPORT set_rel_pathlist_hook_type set_rel_pathlist_hook;
typedef void (*set_join_pathlist_hook_type) (PlannerInfo *root,
RelOptInfo *joinrel,
RelOptInfo *outerrel,
RelOptInfo *innerrel,
JoinType jointype,
JoinPathExtraData *extra);
extern PGDLLIMPORT set_join_pathlist_hook_type set_join_pathlist_hook;
▌CustomPath 構造体
typedef struct CustomPath
{
Path path; // Path 共通部分
uint32 flags; // CUSTOMPATH_* flags
List *custom_private; // プライベート領域
const CustomPathMethods *methods; // コールバック関数
} CustomPath;
PostgreSQL Unconference/Tokyo May-20157
PostgreSQL - The most advanced
Open Source Relational Database
Custom Scan/Joinを使うには (2/3)
▌CustomPath  CustomScan
 プラナーがCustomPathを選択
CustomScanノードを生成する
必要がある
▌どういう事か?
 CustomPathから生成されるのは、
CustomScanだけではない。(将来的に)
CustomSort、CustomAgg、...
 プラン木はcopyObject()可能である事
GpuScanの例
/* Path information of GpuScan */
typedef struct {
CustomPath cpath;
/* RestrictInfo run on host */
List *host_quals;
/* RestrictInfo run on devices */
List *dev_quals;
} GpuScanPath;
▌CustomScan  CustomScanState
 エグゼキュータの開始時に、
CustomScanノードの内容に基づいて
CustomScanStateを初期化。
GpuScanの例
typedef struct {
GpuTaskState gts;
BlockNumber curr_blknum;
BlockNumber last_blknum;
HeapTupleData scan_tuple;
List *dev_quals;
cl_uint num_rechecked;
} GpuScanState;
PostgreSQL Unconference/Tokyo May-20158
CustomScanStateを継承
PostgreSQL - The most advanced
Open Source Relational Database
Custom Scan/Joinを使うには (3/3)
▌Executorコールバックを各自実装する
typedef struct CustomExecMethods
{
const char *CustomName;
/* Executor methods: mark/restore are optional, the rest are required */
void (*BeginCustomScan) (struct CustomScanState *node,
EState *estate,
int eflags);
TupleTableSlot *(*ExecCustomScan) (struct CustomScanState *node);
void (*EndCustomScan) (struct CustomScanState *node);
void (*ReScanCustomScan) (struct CustomScanState *node);
void (*MarkPosCustomScan) (struct CustomScanState *node);
void (*RestrPosCustomScan) (struct CustomScanState *node);
/* Optional: print additional information in EXPLAIN */
void (*ExplainCustomScan) (struct CustomScanState *node,
List *ancestors,
struct ExplainState *es);
} CustomExecMethods;
PostgreSQL Unconference/Tokyo May-20159
PostgreSQL - The most advanced
Open Source Relational Database
CustomJoinはどう見えるか?
Table.X Table.Y
Path ①
NestLoop
Path ②
HashJoin
Path ③
MergeJoin
Path ④
CustomScan
(GpuJoin)
Built-in
logics
clause
x.id = y.id
X Y X Y
NestLoop HashJoin
X Y
MergeJoin
X Y
GpuJoin Result set
of tables join
Join “後” の結果セットを
スキャンしているように見え
る。
▌CustomScanによる Join の置換え
PostgreSQL Unconference/Tokyo May-201510
PostgreSQL - The most advanced
Open Source Relational Database
結果セットのレコード型
PostgreSQL Unconference/Tokyo May-201511
table
(t0)
table
(t1)
table
(t2)
単純なスキャン 複数のテーブルから成るジョイン
t0.a t0.b t0.c t0.d
結果セットのレコード型は、
常にテーブル定義と一致
t1.x t2.i t1.y t1.z t2.j t2.k
結果セットのレコード型は、
時と場合によって異なる
PostgreSQL - The most advanced
Open Source Relational Database
scanrelid == 0 と custom_scan_tlist
▌scanrelid == 0
 CustomScanが特定の実テーブルスキャンでない事を示す Magic Number
 custom_scan_tlist で指定されたレコード型をスキャンすると見なす。
 EXPLAINで、元々の列名を
表示する際の名前解決にも
使用される。
PostgreSQL Unconference/Tokyo May-201512
table
(t1)
table
(t2)
t1.x t2.i t1.y t1.z t2.j t2.k
CustomScan
scanrelid == 0
custom_scan_tlist
PostgreSQL - The most advanced
Open Source Relational Database
CustomScanを含むクエリ実行計画
postgres=# EXPLAIN SELECT cat, AVG(x)
FROM t0 NATURAL JOIN t1 RIGHT JOIN t2 ON t0.aid=t2.bid OR t0.bid=t2.bid
WHERE atext like '%abc%' GROUP BY cat;
QUERY PLAN
---------------------------------------------------------------------------
HashAggregate (cost=348362.37..348362.70 rows=26 width=12)
Group Key: t0.cat
-> Custom Scan (GpuPreAgg) (cost=2134.00..348362.24 rows=26 width=52)
Bulkload: On
Reduction: Local + Global
-> Custom Scan (GpuJoin) (cost=1134.00..347362.19 rows=1985 width=12)
Bulkload: On
Depth 1: Logic: GpuHashJoin, HashKeys: (aid), JoinQual: (aid = aid)
Depth 2: Logic: GpuNestLoop, JoinQual: ((aid = bid) OR (bid = bid))
-> Custom Scan (GpuScan) on t0 (cost=1000.00..143858.00 rows=10000060 ...
-> Custom Scan (MultiRels) (cost=834.05..343937.39 rows=4 width=4)
Hash keys: aid
nBatches: 1, Buckets: 1024, Buffer Usage: 2.33%
-> Seq Scan on t1 (cost=0.00..834.00 rows=4 width=4)
Filter: (atext ~~ '%abc%'::text)
-> Custom Scan (MultiRels) (cost=1134.00..347362.19 rows=40000 width=4)
nBatches: 1, Buffer Usage: 97.64%
-> Seq Scan on t2 (cost=0.00..734.00 rows=40000 width=4)
PostgreSQL Unconference/Tokyo May-201513
PostgreSQL - The most advanced
Open Source Relational Database
CustomにJoinを実装してみた結果
▌SELECT cat, AVG(x) FROM t0 NATURAL JOIN t1 [, ...] GROUP BY cat; を
テーブル数を変えながら実行してクエリ応答時間を測定。
▌t0:1億行、t1~t10:それぞれ10万行を含む。全データはバッファにロード済み。
▌PostgreSQL v9.5devel + PG-Strom 5/26開発版、on CUDA7 (x86_64)
▌CPU: Xeon E5-2640, RAM: 256GB, GPU: NVIDIA GTX980, OS: RedHat EL7
PostgreSQL Unconference/Tokyo May-201514
81.71
122.96
165.05
214.64
261.51
307.18
356.20
406.59
468.59
520.45
8.38 9.02 8.84 10.33 11.47 13.21
14.48 17.15 19.37 21.72
0
100
200
300
400
500
600
1 2 3 4 5 6 7 8 9 10
QueryExecutionTime[sec]
number of tables joined
PostgreSQL PG-Strom
PostgreSQL - The most advanced
Open Source Relational Database
v9.5に向けての Open Issue
PostgreSQL Unconference/Tokyo May-201515
拡張モジュールが
Child-Pathをプラン木に
変換できなくなってしまった!?
PostgreSQL - The most advanced
Open Source Relational Database
もふっ、もふもふ。
16

More Related Content

What's hot (20)

PPTX
SQLチューニング入門 入門編
Miki Shimogai
 
PDF
Das 2015
Takefumi MIYOSHI
 
PDF
並列クエリを実行するPostgreSQLのアーキテクチャ
Kohei KaiGai
 
PDF
Pythonによる並列プログラミング -GPGPUも-
Yusaku Watanabe
 
PDF
CPUから見たG1GC
Kenji Kazumura
 
PDF
Hls friends 20161122.key
Takefumi MIYOSHI
 
PDF
Synthesijer fpgax 20150201
Takefumi MIYOSHI
 
PPTX
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
YujiSoftware
 
PPTX
OSC2015nagoya
Miki Shimogai
 
KEY
PyOpenCLによるGPGPU入門
Yosuke Onoue
 
PPT
HandlerSocket plugin for MySQL
akirahiguchi
 
PPTX
OSC2015kyoto
Miki Shimogai
 
PDF
20170329_BigData基盤研究会#7
Kohei KaiGai
 
PDF
About GStreamer 1.0 application development for beginners
Shota TAMURA
 
PDF
[TL06] 日本の第一人者が C# の現状と今後を徹底解説! 「この素晴らしい C# に祝福を!」
de:code 2017
 
PDF
[DI05] Azure Event Hubs と Azure Stream Analytics で、”今を処理”する
de:code 2017
 
PDF
Kibanaでsysstatを可視化する
Kensuke Maeda
 
PDF
【関東GPGPU勉強会#4】GTX 1080でComputer Vision アルゴリズムを色々動かしてみる
Yasuhiro Yoshimura
 
PDF
Do postgres-dream-of-graph-database
Toshi Harada
 
PDF
20170310_InDatabaseAnalytics_#1
Kohei KaiGai
 
SQLチューニング入門 入門編
Miki Shimogai
 
並列クエリを実行するPostgreSQLのアーキテクチャ
Kohei KaiGai
 
Pythonによる並列プログラミング -GPGPUも-
Yusaku Watanabe
 
CPUから見たG1GC
Kenji Kazumura
 
Hls friends 20161122.key
Takefumi MIYOSHI
 
Synthesijer fpgax 20150201
Takefumi MIYOSHI
 
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
YujiSoftware
 
OSC2015nagoya
Miki Shimogai
 
PyOpenCLによるGPGPU入門
Yosuke Onoue
 
HandlerSocket plugin for MySQL
akirahiguchi
 
OSC2015kyoto
Miki Shimogai
 
20170329_BigData基盤研究会#7
Kohei KaiGai
 
About GStreamer 1.0 application development for beginners
Shota TAMURA
 
[TL06] 日本の第一人者が C# の現状と今後を徹底解説! 「この素晴らしい C# に祝福を!」
de:code 2017
 
[DI05] Azure Event Hubs と Azure Stream Analytics で、”今を処理”する
de:code 2017
 
Kibanaでsysstatを可視化する
Kensuke Maeda
 
【関東GPGPU勉強会#4】GTX 1080でComputer Vision アルゴリズムを色々動かしてみる
Yasuhiro Yoshimura
 
Do postgres-dream-of-graph-database
Toshi Harada
 
20170310_InDatabaseAnalytics_#1
Kohei KaiGai
 

Similar to PostgreSQL v9.5の新機能~CustomScan/Join Interface (20)

PDF
20170127 JAWS HPC-UG#8
Kohei KaiGai
 
PDF
PL/CUDA - Fusion of HPC Grade Power with In-Database Analytics
Kohei KaiGai
 
PDF
20221111_JPUG_CustomScan_API
Kohei KaiGai
 
PDF
PL/CUDA - GPU Accelerated In-Database Analytics
Kohei KaiGai
 
PDF
20181212 - PGconf.ASIA - LT
Kohei KaiGai
 
PPTX
CloudNativePGを動かしてみた! ~PostgreSQL on Kubernetes~(第34回PostgreSQLアンカンファレンス@オンライ...
NTT DATA Technology & Innovation
 
PDF
PostgreSQL 12の話
Masahiko Sawada
 
PDF
pg_bigmを用いた全文検索のしくみ(前編)
NTT DATA OSS Professional Services
 
PDF
20171220_hbstudy80_pgstrom
Kohei KaiGai
 
PDF
PostgreSQL最新動向 ~カラムナストアから生成AI連携まで~ (Open Source Conference 2025 Tokyo/Spring ...
NTT DATA Technology & Innovation
 
PDF
SSDとGPUがPostgreSQLを加速する【OSC.Enterprise】
Kohei KaiGai
 
PDF
20180920_DBTS_PGStrom_JP
Kohei KaiGai
 
PDF
PostgreSQLレプリケーション(pgcon17j_t4)
Kosuke Kida
 
PDF
Introduction new features in Spark 3.0
Kazuaki Ishizaki
 
PPTX
GNS3上の仮想アプライアンス+GitLabRunner+BDDによるテスト自動化
Shigeru Tsubota
 
PDF
SDN Lab環境でのRobotFramework実践活用
Toshiki Tsuboi
 
PDF
オープンソースのIoT向けスケールアウト型データベース GridDB 〜性能ベンチマーク結果とOSSを利用したビッグデータ分析環境〜
griddb
 
PDF
20190119 aws-study-pg-extension
Toshi Harada
 
PDF
Maxwell と Java CUDAプログラミング
NVIDIA Japan
 
PPTX
gRPCurlDotNet.pptx
Takao Tetsuro
 
20170127 JAWS HPC-UG#8
Kohei KaiGai
 
PL/CUDA - Fusion of HPC Grade Power with In-Database Analytics
Kohei KaiGai
 
20221111_JPUG_CustomScan_API
Kohei KaiGai
 
PL/CUDA - GPU Accelerated In-Database Analytics
Kohei KaiGai
 
20181212 - PGconf.ASIA - LT
Kohei KaiGai
 
CloudNativePGを動かしてみた! ~PostgreSQL on Kubernetes~(第34回PostgreSQLアンカンファレンス@オンライ...
NTT DATA Technology & Innovation
 
PostgreSQL 12の話
Masahiko Sawada
 
pg_bigmを用いた全文検索のしくみ(前編)
NTT DATA OSS Professional Services
 
20171220_hbstudy80_pgstrom
Kohei KaiGai
 
PostgreSQL最新動向 ~カラムナストアから生成AI連携まで~ (Open Source Conference 2025 Tokyo/Spring ...
NTT DATA Technology & Innovation
 
SSDとGPUがPostgreSQLを加速する【OSC.Enterprise】
Kohei KaiGai
 
20180920_DBTS_PGStrom_JP
Kohei KaiGai
 
PostgreSQLレプリケーション(pgcon17j_t4)
Kosuke Kida
 
Introduction new features in Spark 3.0
Kazuaki Ishizaki
 
GNS3上の仮想アプライアンス+GitLabRunner+BDDによるテスト自動化
Shigeru Tsubota
 
SDN Lab環境でのRobotFramework実践活用
Toshiki Tsuboi
 
オープンソースのIoT向けスケールアウト型データベース GridDB 〜性能ベンチマーク結果とOSSを利用したビッグデータ分析環境〜
griddb
 
20190119 aws-study-pg-extension
Toshi Harada
 
Maxwell と Java CUDAプログラミング
NVIDIA Japan
 
gRPCurlDotNet.pptx
Takao Tetsuro
 
Ad

More from Kohei KaiGai (20)

PDF
20221116_DBTS_PGStrom_History
Kohei KaiGai
 
PDF
20211112_jpugcon_gpu_and_arrow
Kohei KaiGai
 
PDF
20210928_pgunconf_hll_count
Kohei KaiGai
 
PDF
20210731_OSC_Kyoto_PGStrom3.0
Kohei KaiGai
 
PDF
20210301_PGconf_Online_GPU_PostGIS_GiST_Index
Kohei KaiGai
 
PDF
20201128_OSC_Fukuoka_Online_GPUPostGIS
Kohei KaiGai
 
PDF
20201113_PGconf_Japan_GPU_PostGIS
Kohei KaiGai
 
PDF
20201006_PGconf_Online_Large_Data_Processing
Kohei KaiGai
 
PDF
20200828_OSCKyoto_Online
Kohei KaiGai
 
PDF
20200806_PGStrom_PostGIS_GstoreFdw
Kohei KaiGai
 
PDF
20200424_Writable_Arrow_Fdw
Kohei KaiGai
 
PDF
20191211_Apache_Arrow_Meetup_Tokyo
Kohei KaiGai
 
PDF
20191115-PGconf.Japan
Kohei KaiGai
 
PDF
20190926_Try_RHEL8_NVMEoF_Beta
Kohei KaiGai
 
PDF
20190925_DBTS_PGStrom
Kohei KaiGai
 
PDF
20190909_PGconf.ASIA_KaiGai
Kohei KaiGai
 
PDF
20190516_DLC10_PGStrom
Kohei KaiGai
 
PDF
20190418_PGStrom_on_ArrowFdw
Kohei KaiGai
 
PDF
20190314 PGStrom Arrow_Fdw
Kohei KaiGai
 
PDF
20181212 - PGconfASIA - LT - English
Kohei KaiGai
 
20221116_DBTS_PGStrom_History
Kohei KaiGai
 
20211112_jpugcon_gpu_and_arrow
Kohei KaiGai
 
20210928_pgunconf_hll_count
Kohei KaiGai
 
20210731_OSC_Kyoto_PGStrom3.0
Kohei KaiGai
 
20210301_PGconf_Online_GPU_PostGIS_GiST_Index
Kohei KaiGai
 
20201128_OSC_Fukuoka_Online_GPUPostGIS
Kohei KaiGai
 
20201113_PGconf_Japan_GPU_PostGIS
Kohei KaiGai
 
20201006_PGconf_Online_Large_Data_Processing
Kohei KaiGai
 
20200828_OSCKyoto_Online
Kohei KaiGai
 
20200806_PGStrom_PostGIS_GstoreFdw
Kohei KaiGai
 
20200424_Writable_Arrow_Fdw
Kohei KaiGai
 
20191211_Apache_Arrow_Meetup_Tokyo
Kohei KaiGai
 
20191115-PGconf.Japan
Kohei KaiGai
 
20190926_Try_RHEL8_NVMEoF_Beta
Kohei KaiGai
 
20190925_DBTS_PGStrom
Kohei KaiGai
 
20190909_PGconf.ASIA_KaiGai
Kohei KaiGai
 
20190516_DLC10_PGStrom
Kohei KaiGai
 
20190418_PGStrom_on_ArrowFdw
Kohei KaiGai
 
20190314 PGStrom Arrow_Fdw
Kohei KaiGai
 
20181212 - PGconfASIA - LT - English
Kohei KaiGai
 
Ad

Recently uploaded (10)

PDF
人気ブロックチェーン基盤「Hyperledger Fabric」最新版を動かしてみた!
LFDT Tokyo Meetup
 
PDF
プライバシ保護のためのインターネットアーキテクチャの進化 (2025-07-11)
Jun Kurihara
 
PDF
Hyperledger Fabric公式サンプル fabric-samples徹底解説
LFDT Tokyo Meetup
 
PDF
SIG-AUDIO 2025 Vol.02 オンラインセミナー SIG-Audioプレゼン資料_オーディオプラグイン開発_塩澤達矢.pdf
IGDA Japan SIG-Audio
 
PDF
Hyperledger Fabric最新v3.x系での機能強化、変更点にキャッチアップ!
LFDT Tokyo Meetup
 
PDF
[Hardening Designers Confernece 2025]ランサムウェアでの見えざるログ・見えるログ
kataware
 
PDF
20250630_aws_reinforce_2025_aws_sheild_network_security_director
uedayuki
 
PDF
生成AIパネルトーク(Interop25Tokyo APPS JAPAN M1-07,M2-07 嶋ポジショントーク)
嶋 是一 (Yoshikazu SHIMA)
 
PDF
ABC2025S LT講演「世界の窓から Androidこんにちは2025」アプリ自動生成の将来?ロボティクスの夢再び?
嶋 是一 (Yoshikazu SHIMA)
 
PDF
20250710_Devinで切り拓くDB革命_〜価値創出に集中せよ〜.pdf
Masaki Yamakawa
 
人気ブロックチェーン基盤「Hyperledger Fabric」最新版を動かしてみた!
LFDT Tokyo Meetup
 
プライバシ保護のためのインターネットアーキテクチャの進化 (2025-07-11)
Jun Kurihara
 
Hyperledger Fabric公式サンプル fabric-samples徹底解説
LFDT Tokyo Meetup
 
SIG-AUDIO 2025 Vol.02 オンラインセミナー SIG-Audioプレゼン資料_オーディオプラグイン開発_塩澤達矢.pdf
IGDA Japan SIG-Audio
 
Hyperledger Fabric最新v3.x系での機能強化、変更点にキャッチアップ!
LFDT Tokyo Meetup
 
[Hardening Designers Confernece 2025]ランサムウェアでの見えざるログ・見えるログ
kataware
 
20250630_aws_reinforce_2025_aws_sheild_network_security_director
uedayuki
 
生成AIパネルトーク(Interop25Tokyo APPS JAPAN M1-07,M2-07 嶋ポジショントーク)
嶋 是一 (Yoshikazu SHIMA)
 
ABC2025S LT講演「世界の窓から Androidこんにちは2025」アプリ自動生成の将来?ロボティクスの夢再び?
嶋 是一 (Yoshikazu SHIMA)
 
20250710_Devinで切り拓くDB革命_〜価値創出に集中せよ〜.pdf
Masaki Yamakawa
 

PostgreSQL v9.5の新機能~CustomScan/Join Interface

  • 2. PostgreSQL - The most advanced Open Source Relational Database v9.5 New Feature PostgreSQL Unconference/Tokyo May-20152 Custom-Scan Interface Custom-Join Interface
  • 3. PostgreSQL - The most advanced Open Source Relational Database Custom Scan/Join Interfaceが実現する事 ▌クエリ実行計画の一部を、拡張モジュール実装の “何か” で置き換える ▌何を置き換える事ができるのか?  Scan ... set_rel_pathlist_hook で候補パス追加  Join ... set_join_pathlist_hook で候補パス追加  Others ... planner_hookを使ってインジェクションすれば何でもアリ。  局所最適にならないよう、注意は必要だが…。 ▌応用例  GPUアクセラレーションによる全体的なRDBMS性能の底上げ。  PG-Strom (オタワで発表するんでヨロ)  特定のテーブル同士のJoinをMaterialized Viewスキャンに透過的に置換え。  特定のテーブルのうち、参照頻度の高い列だけを列指向形式でキャッシュ  Scan/Joinの中間結果から統計情報を採取。値によるヒストグラム作成。  ......など PostgreSQL Unconference/Tokyo May-20153
  • 4. PostgreSQL - The most advanced Open Source Relational Database クエリ実行計画 postgres=# EXPLAIN SELECT cat, AVG(x) FROM t0 NATURAL JOIN t1 RIGHT JOIN t2 ON t0.aid=t2.bid OR t0.bid=t2.bid WHERE atext like '%abc%' GROUP BY cat; QUERY PLAN ------------------------------------------------------------------------------ HashAggregate (cost=979149.25..979149.58 rows=26 width=12) Group Key: t0.cat -> Nested Loop (cost=834.05..979139.33 rows=1985 width=12) Join Filter: ((t0.aid = t2.bid) OR (t0.bid = t2.bid)) -> Seq Scan on t2 (cost=0.00..734.00 rows=40000 width=4) -> Materialize (cost=834.05..281207.82 rows=996 width=20) -> Hash Join (cost=834.05..281202.84 rows=996 width=20) Hash Cond: (t0.aid = t1.aid) -> Seq Scan on t0 (cost=0.00..242858.60 rows=10000060 width=20) -> Hash (cost=834.00..834.00 rows=4 width=4) -> Seq Scan on t1 (cost=0.00..834.00 rows=4 width=4) Filter: (atext ~~ '%abc%'::text) (12 rows) PostgreSQL Unconference/Tokyo May-20154
  • 5. PostgreSQL - The most advanced Open Source Relational Database PostgreSQL クエリ処理の流れ (1/2) ▌リレーションに対する候補パスを列挙  Base Relation  Join Relation ▌プラナーが最も実行コストの小さな パスを選択。 全てのテーブルを単一のJoin-Treeに 収めるまで結合順を試行錯誤 ▌実行パスを元に、実行計画を作成 PostgreSQL Unconference/Tokyo May-20155 SQL クエリ・パーサ (構文解析) クエリ・オプティマイザ (最適化) クエリ・エグゼキュータ(実行) 結果セット
  • 6. PostgreSQL - The most advanced Open Source Relational Database PostgreSQL クエリ処理の流れ (2/2) ▌ExecInitNode  実行ノードの初期化 ▌ExecProcNode  一行を生成して TupleTableSlot を返す ▌ExecEndNode  実行ノードの終了処理 ▌その他  ExecReScan  ExecMarkPos (optional)  ExecRestrPos (optional) PostgreSQL Unconference/Tokyo May-20156 SQL クエリ・パーサ (構文解析) クエリ・オプティマイザ (最適化) クエリ・エグゼキュータ(実行) 結果セット
  • 7. PostgreSQL - The most advanced Open Source Relational Database Custom Scan/Joinを使うには (1/3) ▌候補パスを追加する typedef void (*set_rel_pathlist_hook_type) (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte); extern PGDLLIMPORT set_rel_pathlist_hook_type set_rel_pathlist_hook; typedef void (*set_join_pathlist_hook_type) (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra); extern PGDLLIMPORT set_join_pathlist_hook_type set_join_pathlist_hook; ▌CustomPath 構造体 typedef struct CustomPath { Path path; // Path 共通部分 uint32 flags; // CUSTOMPATH_* flags List *custom_private; // プライベート領域 const CustomPathMethods *methods; // コールバック関数 } CustomPath; PostgreSQL Unconference/Tokyo May-20157
  • 8. PostgreSQL - The most advanced Open Source Relational Database Custom Scan/Joinを使うには (2/3) ▌CustomPath  CustomScan  プラナーがCustomPathを選択 CustomScanノードを生成する 必要がある ▌どういう事か?  CustomPathから生成されるのは、 CustomScanだけではない。(将来的に) CustomSort、CustomAgg、...  プラン木はcopyObject()可能である事 GpuScanの例 /* Path information of GpuScan */ typedef struct { CustomPath cpath; /* RestrictInfo run on host */ List *host_quals; /* RestrictInfo run on devices */ List *dev_quals; } GpuScanPath; ▌CustomScan  CustomScanState  エグゼキュータの開始時に、 CustomScanノードの内容に基づいて CustomScanStateを初期化。 GpuScanの例 typedef struct { GpuTaskState gts; BlockNumber curr_blknum; BlockNumber last_blknum; HeapTupleData scan_tuple; List *dev_quals; cl_uint num_rechecked; } GpuScanState; PostgreSQL Unconference/Tokyo May-20158 CustomScanStateを継承
  • 9. PostgreSQL - The most advanced Open Source Relational Database Custom Scan/Joinを使うには (3/3) ▌Executorコールバックを各自実装する typedef struct CustomExecMethods { const char *CustomName; /* Executor methods: mark/restore are optional, the rest are required */ void (*BeginCustomScan) (struct CustomScanState *node, EState *estate, int eflags); TupleTableSlot *(*ExecCustomScan) (struct CustomScanState *node); void (*EndCustomScan) (struct CustomScanState *node); void (*ReScanCustomScan) (struct CustomScanState *node); void (*MarkPosCustomScan) (struct CustomScanState *node); void (*RestrPosCustomScan) (struct CustomScanState *node); /* Optional: print additional information in EXPLAIN */ void (*ExplainCustomScan) (struct CustomScanState *node, List *ancestors, struct ExplainState *es); } CustomExecMethods; PostgreSQL Unconference/Tokyo May-20159
  • 10. PostgreSQL - The most advanced Open Source Relational Database CustomJoinはどう見えるか? Table.X Table.Y Path ① NestLoop Path ② HashJoin Path ③ MergeJoin Path ④ CustomScan (GpuJoin) Built-in logics clause x.id = y.id X Y X Y NestLoop HashJoin X Y MergeJoin X Y GpuJoin Result set of tables join Join “後” の結果セットを スキャンしているように見え る。 ▌CustomScanによる Join の置換え PostgreSQL Unconference/Tokyo May-201510
  • 11. PostgreSQL - The most advanced Open Source Relational Database 結果セットのレコード型 PostgreSQL Unconference/Tokyo May-201511 table (t0) table (t1) table (t2) 単純なスキャン 複数のテーブルから成るジョイン t0.a t0.b t0.c t0.d 結果セットのレコード型は、 常にテーブル定義と一致 t1.x t2.i t1.y t1.z t2.j t2.k 結果セットのレコード型は、 時と場合によって異なる
  • 12. PostgreSQL - The most advanced Open Source Relational Database scanrelid == 0 と custom_scan_tlist ▌scanrelid == 0  CustomScanが特定の実テーブルスキャンでない事を示す Magic Number  custom_scan_tlist で指定されたレコード型をスキャンすると見なす。  EXPLAINで、元々の列名を 表示する際の名前解決にも 使用される。 PostgreSQL Unconference/Tokyo May-201512 table (t1) table (t2) t1.x t2.i t1.y t1.z t2.j t2.k CustomScan scanrelid == 0 custom_scan_tlist
  • 13. PostgreSQL - The most advanced Open Source Relational Database CustomScanを含むクエリ実行計画 postgres=# EXPLAIN SELECT cat, AVG(x) FROM t0 NATURAL JOIN t1 RIGHT JOIN t2 ON t0.aid=t2.bid OR t0.bid=t2.bid WHERE atext like '%abc%' GROUP BY cat; QUERY PLAN --------------------------------------------------------------------------- HashAggregate (cost=348362.37..348362.70 rows=26 width=12) Group Key: t0.cat -> Custom Scan (GpuPreAgg) (cost=2134.00..348362.24 rows=26 width=52) Bulkload: On Reduction: Local + Global -> Custom Scan (GpuJoin) (cost=1134.00..347362.19 rows=1985 width=12) Bulkload: On Depth 1: Logic: GpuHashJoin, HashKeys: (aid), JoinQual: (aid = aid) Depth 2: Logic: GpuNestLoop, JoinQual: ((aid = bid) OR (bid = bid)) -> Custom Scan (GpuScan) on t0 (cost=1000.00..143858.00 rows=10000060 ... -> Custom Scan (MultiRels) (cost=834.05..343937.39 rows=4 width=4) Hash keys: aid nBatches: 1, Buckets: 1024, Buffer Usage: 2.33% -> Seq Scan on t1 (cost=0.00..834.00 rows=4 width=4) Filter: (atext ~~ '%abc%'::text) -> Custom Scan (MultiRels) (cost=1134.00..347362.19 rows=40000 width=4) nBatches: 1, Buffer Usage: 97.64% -> Seq Scan on t2 (cost=0.00..734.00 rows=40000 width=4) PostgreSQL Unconference/Tokyo May-201513
  • 14. PostgreSQL - The most advanced Open Source Relational Database CustomにJoinを実装してみた結果 ▌SELECT cat, AVG(x) FROM t0 NATURAL JOIN t1 [, ...] GROUP BY cat; を テーブル数を変えながら実行してクエリ応答時間を測定。 ▌t0:1億行、t1~t10:それぞれ10万行を含む。全データはバッファにロード済み。 ▌PostgreSQL v9.5devel + PG-Strom 5/26開発版、on CUDA7 (x86_64) ▌CPU: Xeon E5-2640, RAM: 256GB, GPU: NVIDIA GTX980, OS: RedHat EL7 PostgreSQL Unconference/Tokyo May-201514 81.71 122.96 165.05 214.64 261.51 307.18 356.20 406.59 468.59 520.45 8.38 9.02 8.84 10.33 11.47 13.21 14.48 17.15 19.37 21.72 0 100 200 300 400 500 600 1 2 3 4 5 6 7 8 9 10 QueryExecutionTime[sec] number of tables joined PostgreSQL PG-Strom
  • 15. PostgreSQL - The most advanced Open Source Relational Database v9.5に向けての Open Issue PostgreSQL Unconference/Tokyo May-201515 拡張モジュールが Child-Pathをプラン木に 変換できなくなってしまった!?
  • 16. PostgreSQL - The most advanced Open Source Relational Database もふっ、もふもふ。 16