SlideShare a Scribd company logo
Custom Scan API

KaiGai Kohei <kaigai@kaigai.gr.jp>
(Tw: @kkaigai)
自己紹介

 海外 浩平 (かいがい こうへい)
最近、国内に戻ってきました。
 SE-PostgreSQLやPG-Stromを作っています。
2

PostgreSQL Unconference #3
Custom Scan APIとは
Extensionがエグゼキュータ処理を乗っ取るAPI
俺様的方法で Scan を実装できる

俺様的方法で Join を実装できる
(俺様的方法で Xxxx を実装できる)
PostgreSQL v9.4 の標準機能化に向けて議論の途中

3

PostgreSQL Unconference #3
ExecutorXXX_hook ではダメなのか?(1/3)
/* Hook for plugins to get control in ExecutorStart() */
typedef void (*ExecutorStart_hook_type) (QueryDesc *queryDesc,
int eflags);
extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook;
/* Hook for plugins to get control in ExecutorRun() */
typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc,
ScanDirection direction,
long count);
extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook;

/* Hook for plugins to get control in ExecutorFinish() */
typedef void (*ExecutorFinish_hook_type) (QueryDesc *queryDesc);
extern PGDLLIMPORT ExecutorFinish_hook_type ExecutorFinish_hook;
/* Hook for plugins to get control in ExecutorEnd() */
typedef void (*ExecutorEnd_hook_type) (QueryDesc *queryDesc);
extern PGDLLIMPORT ExecutorEnd_hook_type ExecutorEnd_hook;
4

PostgreSQL Unconference #3
ExecutorXXX_hook ではダメなのか?(2/3)
postgres=# explain(costs off) select y from l_tbl join r_tbl
on a = x group by y,b order by b;
QUERY PLAN
------------------------------------------------独自のソート実装を
Group
エクステンションとし
Group Key: l_tbl.b, r_tbl.y
て実装できるか?
-> Sort
Sort Key: l_tbl.b, r_tbl.y
-> Merge Join
Merge Cond: (l_tbl.a = r_tbl.x)
-> Sort
Sort Key: l_tbl.a
-> Seq Scan on l_tbl
-> Materialize
-> Sort
Sort Key: r_tbl.x
-> Seq Scan on r_tbl
(13 rows)
5

PostgreSQL Unconference #3
ExecutorXXX_hook ではダメなのか?(3/3)
TupleTableSlot *ExecProcNode(PlanState *node)
{
:
switch (nodeTag(node))
{
:
case T_SeqScanState:
result = ExecSeqScan((SeqScanState *) node);
break;
:
default:
elog(ERROR, "unrecognized node type: %d",
(int) nodeTag(node));
result = NULL;
break;
}
何か 『任意の処理を行う』 ノードを
return result;
PostgreSQL本体が認識できる
}
必要がある。
6

PostgreSQL Unconference #3
Custom Scan API (1/3)
 オプティマイザへ介入するためのフック
/* Hook for plugins to add custom scan path */
typedef void (*add_scan_path_hook_type)(PlannerInfo *root,
RelOptInfo *baserel,
RangeTblEntry *rte);
extern PGDLLIMPORT add_scan_path_hook_type add_scan_path_hook;

/* Hook for plugins to add custom join path */
typedef void (*add_join_path_hook_type)(PlannerInfo *root,
RelOptInfo *joinrel,
RelOptInfo *outerrel,
RelOptInfo *innerrel,
JoinType jointype,
SpecialJoinInfo *sjinfo,
List *restrictlist,
List *mergeclause_list,
SemiAntiJoinFactors *semafac,
Relids param_source_rels,
Relids extra_lateral_rels);
extern PGDLLIMPORT add_join_path_hook_type add_join_path_hook;
7

PostgreSQL Unconference #3
Custom Scan API (2/3)
エグゼキュータに介入するためのコールバック#1
typedef void (*InitCustomScanPlan_function)
(PlannerInfo *root,
CustomScan *cscan_plan,
CustomPath *cscan_path,
List *tlist,
List *scan_clauses);
typedef void (*SetPlanRefCustomScan_function)
(PlannerInfo *root,
CustomScan *cscan_plan,
int rtoffset);
typedef void (*BeginCustomScan_function)
(CustomScanState *csstate, int eflags);
typedef TupleTableSlot *
(*ExecCustomScan_function)(CustomScanState *csstate);

8

PostgreSQL Unconference #3
Custom Scan API (3/3)
エグゼキュータに介入するためのコールバック#2
typedef Node *(*MultiExecCustomScan_function)
(CustomScanState *csstate);
typedef void (*EndCustomScan_function)
(CustomScanState *csstate);
typedef void (*ReScanCustomScan_function)
(CustomScanState *csstate);
typedef void (*MarkPosCustomScan_function)
(CustomScanState *csstate);
typedef void (*RestorePosCustom_function)
(CustomScanState *csstate);
typedef void (*ExplainCustomScan_function)
(CustomScanState *csstate,
ExplainState *es);
9

PostgreSQL Unconference #3
PostgreSQLクエリ処理の流れ (1/2)
SQL
IndexScan

クエリ・パーサ (構文解析)

クエリ・オプティマイザ (最適化)

Path
Cost=100

IndexPath
Cost=5

クエリ・エグゼキュータ(実行)

SeqScan

IndexScan

結果セット
10

PostgreSQL Unconference #3

TidScan
PostgreSQLクエリ処理の流れ (2/3)
SQL
CustomScan

クエリ・パーサ (構文解析)

クエリ・オプティマイザ (最適化)

Path
Cost=100

IndexPath
Cost=5

クエリ・エグゼキュータ(実行)

SeqScan

IndexScan

Custom
ScanPath
Cost=1

Extension
モジュール

結果セット
11

TidScan

PostgreSQL Unconference #3

Custom
Scan
PostgreSQLクエリ処理の流れ (3/3)
SQL

クエリ・パーサ (構文解析)
実行開始

クエリ・オプティマイザ (最適化)

一行を取り出し

実行終了
クエリ・エグゼキュータ(実行)
再帰的な
呼び出しも…。

結果セット
12

Custom Scan API
Extension
モジュール

PostgreSQL Unconference #3
FDWとの違い
FDW / Foreign Table
 参照や更新の対象として使用できる。
 “一行を返す”時に、データ型は常にテーブル定義と
一致していなければならない。
 オブジェクトを実装する役割

Custom Scan API
 参照や更新の対象として使用できない
 “一行を返す” 時に、データ型を任意に定める事ができる。
(上位ノードの期待通りである事はモジュールの責任)
 メソッドを実装する役割

13

PostgreSQL Unconference #3
データ型が変動するとは?
SELECT * FROM t1 JOIN t2 ON t1.a = t2.x;
HeapTuple

JOIN
Scan t1

HeapTuple

事前にデータ型は
明らかであるのか?

Custom Scan
Scan t1

Scan t2

Scan t2

 テーブルスキャンを実装する場合、返却されるタプルの型は事前に明らか
CREATE TABLEで指定したデータ型
 JOINを実装する場合、返却されるタプルの型は毎回変わる
JOINされるテーブルの定義による
14

PostgreSQL Unconference #3
JoinをCustomScan APIで置き換えた例
Postgres_fdw への機能拡張
 Foreign table 同士のJOINで、
 同一のForeign serverにホストされており、
 結合条件がリモート実行可能なものである場合
postgres=# explain (costs off, verbose)
select * from ft1 where b like '%aaa%';
QUERY PLAN
---------------------------------------------------------Foreign Scan on public.ft1
Output: a, b
Remote SQL: SELECT a, b FROM public.t1
WHERE ((b ~~ '%aaa%'::text))
(3 rows)

15

PostgreSQL Unconference #3
JoinをCustomScan APIで置き換えた例
Postgres_fdw への機能拡張
 Foreign table 同士のJOINで、
 同一のForeign serverにホストされており、
 結合条件がリモート実行可能なものである場合
postgres=# explain (costs off, verbose)
select * from ft1 join ft2 on a = x
where b like '%aaa%';
QUERY PLAN
---------------------------------------------------------Custom Scan (postgres-fdw)
Output: a, b, x, y
Remote SQL: SELECT r1.a, r1.b, r2.x, r2.y FROM
(public.t1 r1 JOIN public.t2 r2 ON ((r1.a = r2.x)))
WHERE ((r1.b ~~ '%aaa%'::text))
(3 rows)
16

PostgreSQL Unconference #3
その他の利用例 (1/3) – Cache-only Scan
postgres=# EXPLAIN(costs off)
SELECT a,b FROM t1 WHERE a > b;
QUERY PLAN
--------------------------------------Custom Scan (cache scan) on t1
Filter: ((a)::double precision > b)
(2 rows)

エグゼキュータ

列A、Bのみ
キャッシュ
キャッシュヒット

Custom Scan
(cache_scan)
ミスヒット

17

heap

PostgreSQL Unconference #3
その他の利用例 (2/3) – Cache-only Scan
postgres=# EXPLAIN(costs off)
SELECT a,b FROM t1 WHERE a > b;
QUERY PLAN
--------------------------------------Custom Scan (cache scan) on t1
Filter: ((a)::double precision > b)
(2 rows)

エグゼキュータ

列A、Bのみ
キャッシュ
キャッシュヒット

Custom Scan
(cache_scan)
ミスヒット

18

heap

PostgreSQL Unconference #3

キャッシュに載ったデータを
GPGPUで”超”並列処理。
その他の利用例 (3/3) – Cheat Join
!!Just An Idea!!

(3,2)

(2,4)

PostgreSQL Unconference #3

(3,1)

(4,3)

:

19

(8,2)

(2,3)

Right
Relation

(7,3)

(2,2)

Left
Relation

(5,1)

(2,1)

Nest Loop

(4,2)

(1,5)

Custom Scan
(cheat join)

(7,3)

(1,4)

Cheat Join

Right
Tid

(1,3)

エグゼキュータ

Left
Tid
(1,2)

② カンペを参照
しつつ TidScan

:

① カンペを作成
Call for your feedback!

20

PostgreSQL Unconference #3

More Related Content

What's hot (20)

C#次世代非同期処理概観 - Task vs Reactive Extensions
C#次世代非同期処理概観 - Task vs Reactive ExtensionsC#次世代非同期処理概観 - Task vs Reactive Extensions
C#次世代非同期処理概観 - Task vs Reactive Extensions
Yoshifumi Kawai
 
GPUをJavaで使う話(Java Casual Talks #1)
GPUをJavaで使う話(Java Casual Talks #1)GPUをJavaで使う話(Java Casual Talks #1)
GPUをJavaで使う話(Java Casual Talks #1)
なおき きしだ
 
About GStreamer 1.0 application development for beginners
About GStreamer 1.0 application development for beginnersAbout GStreamer 1.0 application development for beginners
About GStreamer 1.0 application development for beginners
Shota TAMURA
 
OSC2015kyoto
OSC2015kyotoOSC2015kyoto
OSC2015kyoto
Miki Shimogai
 
Rx java x retrofit
Rx java x retrofitRx java x retrofit
Rx java x retrofit
Shun Nakahara
 
async/await のしくみ
async/await のしくみasync/await のしくみ
async/await のしくみ
信之 岩永
 
Qt Creator を拡張する
Qt Creator を拡張するQt Creator を拡張する
Qt Creator を拡張する
Takumi Asaki
 
片手間MySQLチューニング戦略
片手間MySQLチューニング戦略片手間MySQLチューニング戦略
片手間MySQLチューニング戦略
yoku0825
 
HandlerSocket plugin for MySQL
HandlerSocket plugin for MySQLHandlerSocket plugin for MySQL
HandlerSocket plugin for MySQL
akirahiguchi
 
PostgreSQLの関数属性を知ろう
PostgreSQLの関数属性を知ろうPostgreSQLの関数属性を知ろう
PostgreSQLの関数属性を知ろう
kasaharatt
 
Deep Dive async/await in Unity with UniTask(UniRx.Async)
Deep Dive async/await in Unity with UniTask(UniRx.Async)Deep Dive async/await in Unity with UniTask(UniRx.Async)
Deep Dive async/await in Unity with UniTask(UniRx.Async)
Yoshifumi Kawai
 
Lisp Tutorial for Pythonista : Day 4
Lisp Tutorial for Pythonista : Day 4Lisp Tutorial for Pythonista : Day 4
Lisp Tutorial for Pythonista : Day 4
Ransui Iso
 
History & Practices for UniRx UniRxの歴史、或いは開発(中)タイトルの用例と落とし穴の回避法
History & Practices for UniRx UniRxの歴史、或いは開発(中)タイトルの用例と落とし穴の回避法History & Practices for UniRx UniRxの歴史、或いは開発(中)タイトルの用例と落とし穴の回避法
History & Practices for UniRx UniRxの歴史、或いは開発(中)タイトルの用例と落とし穴の回避法
Yoshifumi Kawai
 
CEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭する
CEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭するCEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭する
CEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭する
Yoshifumi Kawai
 
linq.js - Linq to Objects for JavaScript
linq.js - Linq to Objects for JavaScriptlinq.js - Linq to Objects for JavaScript
linq.js - Linq to Objects for JavaScript
Yoshifumi Kawai
 
GoBGP活用によるSD-WANプラクティス
GoBGP活用によるSD-WANプラクティスGoBGP活用によるSD-WANプラクティス
GoBGP活用によるSD-WANプラクティス
Toshiki Tsuboi
 
「Grails-1.1を斬る!〜Grails-1.1からのチーム開発〜」
「Grails-1.1を斬る!〜Grails-1.1からのチーム開発〜」「Grails-1.1を斬る!〜Grails-1.1からのチーム開発〜」
「Grails-1.1を斬る!〜Grails-1.1からのチーム開発〜」
Tsuyoshi Yamamoto
 
C#次世代非同期処理概観 - Task vs Reactive Extensions
C#次世代非同期処理概観 - Task vs Reactive ExtensionsC#次世代非同期処理概観 - Task vs Reactive Extensions
C#次世代非同期処理概観 - Task vs Reactive Extensions
Yoshifumi Kawai
 
GPUをJavaで使う話(Java Casual Talks #1)
GPUをJavaで使う話(Java Casual Talks #1)GPUをJavaで使う話(Java Casual Talks #1)
GPUをJavaで使う話(Java Casual Talks #1)
なおき きしだ
 
About GStreamer 1.0 application development for beginners
About GStreamer 1.0 application development for beginnersAbout GStreamer 1.0 application development for beginners
About GStreamer 1.0 application development for beginners
Shota TAMURA
 
async/await のしくみ
async/await のしくみasync/await のしくみ
async/await のしくみ
信之 岩永
 
Qt Creator を拡張する
Qt Creator を拡張するQt Creator を拡張する
Qt Creator を拡張する
Takumi Asaki
 
片手間MySQLチューニング戦略
片手間MySQLチューニング戦略片手間MySQLチューニング戦略
片手間MySQLチューニング戦略
yoku0825
 
HandlerSocket plugin for MySQL
HandlerSocket plugin for MySQLHandlerSocket plugin for MySQL
HandlerSocket plugin for MySQL
akirahiguchi
 
PostgreSQLの関数属性を知ろう
PostgreSQLの関数属性を知ろうPostgreSQLの関数属性を知ろう
PostgreSQLの関数属性を知ろう
kasaharatt
 
Deep Dive async/await in Unity with UniTask(UniRx.Async)
Deep Dive async/await in Unity with UniTask(UniRx.Async)Deep Dive async/await in Unity with UniTask(UniRx.Async)
Deep Dive async/await in Unity with UniTask(UniRx.Async)
Yoshifumi Kawai
 
Lisp Tutorial for Pythonista : Day 4
Lisp Tutorial for Pythonista : Day 4Lisp Tutorial for Pythonista : Day 4
Lisp Tutorial for Pythonista : Day 4
Ransui Iso
 
History & Practices for UniRx UniRxの歴史、或いは開発(中)タイトルの用例と落とし穴の回避法
History & Practices for UniRx UniRxの歴史、或いは開発(中)タイトルの用例と落とし穴の回避法History & Practices for UniRx UniRxの歴史、或いは開発(中)タイトルの用例と落とし穴の回避法
History & Practices for UniRx UniRxの歴史、或いは開発(中)タイトルの用例と落とし穴の回避法
Yoshifumi Kawai
 
CEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭する
CEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭するCEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭する
CEDEC 2018 最速のC#の書き方 - C#大統一理論へ向けて性能的課題を払拭する
Yoshifumi Kawai
 
linq.js - Linq to Objects for JavaScript
linq.js - Linq to Objects for JavaScriptlinq.js - Linq to Objects for JavaScript
linq.js - Linq to Objects for JavaScript
Yoshifumi Kawai
 
GoBGP活用によるSD-WANプラクティス
GoBGP活用によるSD-WANプラクティスGoBGP活用によるSD-WANプラクティス
GoBGP活用によるSD-WANプラクティス
Toshiki Tsuboi
 
「Grails-1.1を斬る!〜Grails-1.1からのチーム開発〜」
「Grails-1.1を斬る!〜Grails-1.1からのチーム開発〜」「Grails-1.1を斬る!〜Grails-1.1からのチーム開発〜」
「Grails-1.1を斬る!〜Grails-1.1からのチーム開発〜」
Tsuyoshi Yamamoto
 

Similar to Custom Scan API - PostgreSQL Unconference #3 (18-Jan-2014) (20)

Azure で Serverless 初心者向けタッチ&トライ
Azure で Serverless 初心者向けタッチ&トライAzure で Serverless 初心者向けタッチ&トライ
Azure で Serverless 初心者向けタッチ&トライ
Masanobu Sato
 
20170127 JAWS HPC-UG#8
20170127 JAWS HPC-UG#820170127 JAWS HPC-UG#8
20170127 JAWS HPC-UG#8
Kohei KaiGai
 
20221111_JPUG_CustomScan_API
20221111_JPUG_CustomScan_API20221111_JPUG_CustomScan_API
20221111_JPUG_CustomScan_API
Kohei KaiGai
 
基礎から見直す ASP.NET MVC の単体テスト自動化方法 ~ Windows Azure 関連もあるかも~
基礎から見直す ASP.NET MVC の単体テスト自動化方法 ~ Windows Azure 関連もあるかも~基礎から見直す ASP.NET MVC の単体テスト自動化方法 ~ Windows Azure 関連もあるかも~
基礎から見直す ASP.NET MVC の単体テスト自動化方法 ~ Windows Azure 関連もあるかも~
normalian
 
T sql の parse と generator
T sql の parse と generatorT sql の parse と generator
T sql の parse と generator
Oda Shinsuke
 
20190119 aws-study-pg-extension
20190119 aws-study-pg-extension20190119 aws-study-pg-extension
20190119 aws-study-pg-extension
Toshi Harada
 
20181110 fok2018-pg-extension
20181110 fok2018-pg-extension20181110 fok2018-pg-extension
20181110 fok2018-pg-extension
Toshi Harada
 
TypeScript 言語処理系ことはじめ
TypeScript 言語処理系ことはじめTypeScript 言語処理系ことはじめ
TypeScript 言語処理系ことはじめ
Yu Nobuoka
 
Next2Dで始めるゲーム開発 - Game Development Starting with Next2D
Next2Dで始めるゲーム開発  - Game Development Starting with Next2DNext2Dで始めるゲーム開発  - Game Development Starting with Next2D
Next2Dで始めるゲーム開発 - Game Development Starting with Next2D
Toshiyuki Ienaga
 
ソーシャルアプリ勉強会(第一回資料)配布用
ソーシャルアプリ勉強会(第一回資料)配布用ソーシャルアプリ勉強会(第一回資料)配布用
ソーシャルアプリ勉強会(第一回資料)配布用
Yatabe Terumasa
 
DTraceによるMySQL解析ことはじめ
DTraceによるMySQL解析ことはじめDTraceによるMySQL解析ことはじめ
DTraceによるMySQL解析ことはじめ
Mikiya Okuno
 
Elasticsearchプラグインの作り方
Elasticsearchプラグインの作り方Elasticsearchプラグインの作り方
Elasticsearchプラグインの作り方
Shinsuke Sugaya
 
Apache Torqueについて
Apache TorqueについてApache Torqueについて
Apache Torqueについて
tako pons
 
PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方
Satoshi Nagayasu
 
20141017 introduce razor
20141017 introduce razor20141017 introduce razor
20141017 introduce razor
do_aki
 
gRPCurlDotNet.pptx
gRPCurlDotNet.pptxgRPCurlDotNet.pptx
gRPCurlDotNet.pptx
Takao Tetsuro
 
Infrastructure as code for azure
Infrastructure as code for azureInfrastructure as code for azure
Infrastructure as code for azure
Keiji Kamebuchi
 
20090107 Postgre Sqlチューニング(Sql編)
20090107 Postgre Sqlチューニング(Sql編)20090107 Postgre Sqlチューニング(Sql編)
20090107 Postgre Sqlチューニング(Sql編)
Hiromu Shioya
 
Inside mobage platform
Inside mobage platformInside mobage platform
Inside mobage platform
Toru Yamaguchi
 
Azure で Serverless 初心者向けタッチ&トライ
Azure で Serverless 初心者向けタッチ&トライAzure で Serverless 初心者向けタッチ&トライ
Azure で Serverless 初心者向けタッチ&トライ
Masanobu Sato
 
20170127 JAWS HPC-UG#8
20170127 JAWS HPC-UG#820170127 JAWS HPC-UG#8
20170127 JAWS HPC-UG#8
Kohei KaiGai
 
20221111_JPUG_CustomScan_API
20221111_JPUG_CustomScan_API20221111_JPUG_CustomScan_API
20221111_JPUG_CustomScan_API
Kohei KaiGai
 
基礎から見直す ASP.NET MVC の単体テスト自動化方法 ~ Windows Azure 関連もあるかも~
基礎から見直す ASP.NET MVC の単体テスト自動化方法 ~ Windows Azure 関連もあるかも~基礎から見直す ASP.NET MVC の単体テスト自動化方法 ~ Windows Azure 関連もあるかも~
基礎から見直す ASP.NET MVC の単体テスト自動化方法 ~ Windows Azure 関連もあるかも~
normalian
 
T sql の parse と generator
T sql の parse と generatorT sql の parse と generator
T sql の parse と generator
Oda Shinsuke
 
20190119 aws-study-pg-extension
20190119 aws-study-pg-extension20190119 aws-study-pg-extension
20190119 aws-study-pg-extension
Toshi Harada
 
20181110 fok2018-pg-extension
20181110 fok2018-pg-extension20181110 fok2018-pg-extension
20181110 fok2018-pg-extension
Toshi Harada
 
TypeScript 言語処理系ことはじめ
TypeScript 言語処理系ことはじめTypeScript 言語処理系ことはじめ
TypeScript 言語処理系ことはじめ
Yu Nobuoka
 
Next2Dで始めるゲーム開発 - Game Development Starting with Next2D
Next2Dで始めるゲーム開発  - Game Development Starting with Next2DNext2Dで始めるゲーム開発  - Game Development Starting with Next2D
Next2Dで始めるゲーム開発 - Game Development Starting with Next2D
Toshiyuki Ienaga
 
ソーシャルアプリ勉強会(第一回資料)配布用
ソーシャルアプリ勉強会(第一回資料)配布用ソーシャルアプリ勉強会(第一回資料)配布用
ソーシャルアプリ勉強会(第一回資料)配布用
Yatabe Terumasa
 
DTraceによるMySQL解析ことはじめ
DTraceによるMySQL解析ことはじめDTraceによるMySQL解析ことはじめ
DTraceによるMySQL解析ことはじめ
Mikiya Okuno
 
Elasticsearchプラグインの作り方
Elasticsearchプラグインの作り方Elasticsearchプラグインの作り方
Elasticsearchプラグインの作り方
Shinsuke Sugaya
 
Apache Torqueについて
Apache TorqueについてApache Torqueについて
Apache Torqueについて
tako pons
 
PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方
Satoshi Nagayasu
 
20141017 introduce razor
20141017 introduce razor20141017 introduce razor
20141017 introduce razor
do_aki
 
Infrastructure as code for azure
Infrastructure as code for azureInfrastructure as code for azure
Infrastructure as code for azure
Keiji Kamebuchi
 
20090107 Postgre Sqlチューニング(Sql編)
20090107 Postgre Sqlチューニング(Sql編)20090107 Postgre Sqlチューニング(Sql編)
20090107 Postgre Sqlチューニング(Sql編)
Hiromu Shioya
 
Inside mobage platform
Inside mobage platformInside mobage platform
Inside mobage platform
Toru Yamaguchi
 
Ad

More from Kohei KaiGai (20)

20221116_DBTS_PGStrom_History
20221116_DBTS_PGStrom_History20221116_DBTS_PGStrom_History
20221116_DBTS_PGStrom_History
Kohei KaiGai
 
20211112_jpugcon_gpu_and_arrow
20211112_jpugcon_gpu_and_arrow20211112_jpugcon_gpu_and_arrow
20211112_jpugcon_gpu_and_arrow
Kohei KaiGai
 
20210928_pgunconf_hll_count
20210928_pgunconf_hll_count20210928_pgunconf_hll_count
20210928_pgunconf_hll_count
Kohei KaiGai
 
20210731_OSC_Kyoto_PGStrom3.0
20210731_OSC_Kyoto_PGStrom3.020210731_OSC_Kyoto_PGStrom3.0
20210731_OSC_Kyoto_PGStrom3.0
Kohei KaiGai
 
20210511_PGStrom_GpuCache
20210511_PGStrom_GpuCache20210511_PGStrom_GpuCache
20210511_PGStrom_GpuCache
Kohei KaiGai
 
20210301_PGconf_Online_GPU_PostGIS_GiST_Index
20210301_PGconf_Online_GPU_PostGIS_GiST_Index20210301_PGconf_Online_GPU_PostGIS_GiST_Index
20210301_PGconf_Online_GPU_PostGIS_GiST_Index
Kohei KaiGai
 
20201128_OSC_Fukuoka_Online_GPUPostGIS
20201128_OSC_Fukuoka_Online_GPUPostGIS20201128_OSC_Fukuoka_Online_GPUPostGIS
20201128_OSC_Fukuoka_Online_GPUPostGIS
Kohei KaiGai
 
20201113_PGconf_Japan_GPU_PostGIS
20201113_PGconf_Japan_GPU_PostGIS20201113_PGconf_Japan_GPU_PostGIS
20201113_PGconf_Japan_GPU_PostGIS
Kohei KaiGai
 
20201006_PGconf_Online_Large_Data_Processing
20201006_PGconf_Online_Large_Data_Processing20201006_PGconf_Online_Large_Data_Processing
20201006_PGconf_Online_Large_Data_Processing
Kohei KaiGai
 
20200828_OSCKyoto_Online
20200828_OSCKyoto_Online20200828_OSCKyoto_Online
20200828_OSCKyoto_Online
Kohei KaiGai
 
20200806_PGStrom_PostGIS_GstoreFdw
20200806_PGStrom_PostGIS_GstoreFdw20200806_PGStrom_PostGIS_GstoreFdw
20200806_PGStrom_PostGIS_GstoreFdw
Kohei KaiGai
 
20200424_Writable_Arrow_Fdw
20200424_Writable_Arrow_Fdw20200424_Writable_Arrow_Fdw
20200424_Writable_Arrow_Fdw
Kohei KaiGai
 
20191211_Apache_Arrow_Meetup_Tokyo
20191211_Apache_Arrow_Meetup_Tokyo20191211_Apache_Arrow_Meetup_Tokyo
20191211_Apache_Arrow_Meetup_Tokyo
Kohei KaiGai
 
20191115-PGconf.Japan
20191115-PGconf.Japan20191115-PGconf.Japan
20191115-PGconf.Japan
Kohei KaiGai
 
20190926_Try_RHEL8_NVMEoF_Beta
20190926_Try_RHEL8_NVMEoF_Beta20190926_Try_RHEL8_NVMEoF_Beta
20190926_Try_RHEL8_NVMEoF_Beta
Kohei KaiGai
 
20190925_DBTS_PGStrom
20190925_DBTS_PGStrom20190925_DBTS_PGStrom
20190925_DBTS_PGStrom
Kohei KaiGai
 
20190909_PGconf.ASIA_KaiGai
20190909_PGconf.ASIA_KaiGai20190909_PGconf.ASIA_KaiGai
20190909_PGconf.ASIA_KaiGai
Kohei KaiGai
 
20190516_DLC10_PGStrom
20190516_DLC10_PGStrom20190516_DLC10_PGStrom
20190516_DLC10_PGStrom
Kohei KaiGai
 
20190418_PGStrom_on_ArrowFdw
20190418_PGStrom_on_ArrowFdw20190418_PGStrom_on_ArrowFdw
20190418_PGStrom_on_ArrowFdw
Kohei KaiGai
 
20190314 PGStrom Arrow_Fdw
20190314 PGStrom Arrow_Fdw20190314 PGStrom Arrow_Fdw
20190314 PGStrom Arrow_Fdw
Kohei KaiGai
 
20221116_DBTS_PGStrom_History
20221116_DBTS_PGStrom_History20221116_DBTS_PGStrom_History
20221116_DBTS_PGStrom_History
Kohei KaiGai
 
20211112_jpugcon_gpu_and_arrow
20211112_jpugcon_gpu_and_arrow20211112_jpugcon_gpu_and_arrow
20211112_jpugcon_gpu_and_arrow
Kohei KaiGai
 
20210928_pgunconf_hll_count
20210928_pgunconf_hll_count20210928_pgunconf_hll_count
20210928_pgunconf_hll_count
Kohei KaiGai
 
20210731_OSC_Kyoto_PGStrom3.0
20210731_OSC_Kyoto_PGStrom3.020210731_OSC_Kyoto_PGStrom3.0
20210731_OSC_Kyoto_PGStrom3.0
Kohei KaiGai
 
20210511_PGStrom_GpuCache
20210511_PGStrom_GpuCache20210511_PGStrom_GpuCache
20210511_PGStrom_GpuCache
Kohei KaiGai
 
20210301_PGconf_Online_GPU_PostGIS_GiST_Index
20210301_PGconf_Online_GPU_PostGIS_GiST_Index20210301_PGconf_Online_GPU_PostGIS_GiST_Index
20210301_PGconf_Online_GPU_PostGIS_GiST_Index
Kohei KaiGai
 
20201128_OSC_Fukuoka_Online_GPUPostGIS
20201128_OSC_Fukuoka_Online_GPUPostGIS20201128_OSC_Fukuoka_Online_GPUPostGIS
20201128_OSC_Fukuoka_Online_GPUPostGIS
Kohei KaiGai
 
20201113_PGconf_Japan_GPU_PostGIS
20201113_PGconf_Japan_GPU_PostGIS20201113_PGconf_Japan_GPU_PostGIS
20201113_PGconf_Japan_GPU_PostGIS
Kohei KaiGai
 
20201006_PGconf_Online_Large_Data_Processing
20201006_PGconf_Online_Large_Data_Processing20201006_PGconf_Online_Large_Data_Processing
20201006_PGconf_Online_Large_Data_Processing
Kohei KaiGai
 
20200828_OSCKyoto_Online
20200828_OSCKyoto_Online20200828_OSCKyoto_Online
20200828_OSCKyoto_Online
Kohei KaiGai
 
20200806_PGStrom_PostGIS_GstoreFdw
20200806_PGStrom_PostGIS_GstoreFdw20200806_PGStrom_PostGIS_GstoreFdw
20200806_PGStrom_PostGIS_GstoreFdw
Kohei KaiGai
 
20200424_Writable_Arrow_Fdw
20200424_Writable_Arrow_Fdw20200424_Writable_Arrow_Fdw
20200424_Writable_Arrow_Fdw
Kohei KaiGai
 
20191211_Apache_Arrow_Meetup_Tokyo
20191211_Apache_Arrow_Meetup_Tokyo20191211_Apache_Arrow_Meetup_Tokyo
20191211_Apache_Arrow_Meetup_Tokyo
Kohei KaiGai
 
20191115-PGconf.Japan
20191115-PGconf.Japan20191115-PGconf.Japan
20191115-PGconf.Japan
Kohei KaiGai
 
20190926_Try_RHEL8_NVMEoF_Beta
20190926_Try_RHEL8_NVMEoF_Beta20190926_Try_RHEL8_NVMEoF_Beta
20190926_Try_RHEL8_NVMEoF_Beta
Kohei KaiGai
 
20190925_DBTS_PGStrom
20190925_DBTS_PGStrom20190925_DBTS_PGStrom
20190925_DBTS_PGStrom
Kohei KaiGai
 
20190909_PGconf.ASIA_KaiGai
20190909_PGconf.ASIA_KaiGai20190909_PGconf.ASIA_KaiGai
20190909_PGconf.ASIA_KaiGai
Kohei KaiGai
 
20190516_DLC10_PGStrom
20190516_DLC10_PGStrom20190516_DLC10_PGStrom
20190516_DLC10_PGStrom
Kohei KaiGai
 
20190418_PGStrom_on_ArrowFdw
20190418_PGStrom_on_ArrowFdw20190418_PGStrom_on_ArrowFdw
20190418_PGStrom_on_ArrowFdw
Kohei KaiGai
 
20190314 PGStrom Arrow_Fdw
20190314 PGStrom Arrow_Fdw20190314 PGStrom Arrow_Fdw
20190314 PGStrom Arrow_Fdw
Kohei KaiGai
 
Ad

Custom Scan API - PostgreSQL Unconference #3 (18-Jan-2014)

  • 2. 自己紹介  海外 浩平 (かいがい こうへい) 最近、国内に戻ってきました。  SE-PostgreSQLやPG-Stromを作っています。 2 PostgreSQL Unconference #3
  • 3. Custom Scan APIとは Extensionがエグゼキュータ処理を乗っ取るAPI 俺様的方法で Scan を実装できる 俺様的方法で Join を実装できる (俺様的方法で Xxxx を実装できる) PostgreSQL v9.4 の標準機能化に向けて議論の途中 3 PostgreSQL Unconference #3
  • 4. ExecutorXXX_hook ではダメなのか?(1/3) /* Hook for plugins to get control in ExecutorStart() */ typedef void (*ExecutorStart_hook_type) (QueryDesc *queryDesc, int eflags); extern PGDLLIMPORT ExecutorStart_hook_type ExecutorStart_hook; /* Hook for plugins to get control in ExecutorRun() */ typedef void (*ExecutorRun_hook_type) (QueryDesc *queryDesc, ScanDirection direction, long count); extern PGDLLIMPORT ExecutorRun_hook_type ExecutorRun_hook; /* Hook for plugins to get control in ExecutorFinish() */ typedef void (*ExecutorFinish_hook_type) (QueryDesc *queryDesc); extern PGDLLIMPORT ExecutorFinish_hook_type ExecutorFinish_hook; /* Hook for plugins to get control in ExecutorEnd() */ typedef void (*ExecutorEnd_hook_type) (QueryDesc *queryDesc); extern PGDLLIMPORT ExecutorEnd_hook_type ExecutorEnd_hook; 4 PostgreSQL Unconference #3
  • 5. ExecutorXXX_hook ではダメなのか?(2/3) postgres=# explain(costs off) select y from l_tbl join r_tbl on a = x group by y,b order by b; QUERY PLAN ------------------------------------------------独自のソート実装を Group エクステンションとし Group Key: l_tbl.b, r_tbl.y て実装できるか? -> Sort Sort Key: l_tbl.b, r_tbl.y -> Merge Join Merge Cond: (l_tbl.a = r_tbl.x) -> Sort Sort Key: l_tbl.a -> Seq Scan on l_tbl -> Materialize -> Sort Sort Key: r_tbl.x -> Seq Scan on r_tbl (13 rows) 5 PostgreSQL Unconference #3
  • 6. ExecutorXXX_hook ではダメなのか?(3/3) TupleTableSlot *ExecProcNode(PlanState *node) { : switch (nodeTag(node)) { : case T_SeqScanState: result = ExecSeqScan((SeqScanState *) node); break; : default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node)); result = NULL; break; } 何か 『任意の処理を行う』 ノードを return result; PostgreSQL本体が認識できる } 必要がある。 6 PostgreSQL Unconference #3
  • 7. Custom Scan API (1/3)  オプティマイザへ介入するためのフック /* Hook for plugins to add custom scan path */ typedef void (*add_scan_path_hook_type)(PlannerInfo *root, RelOptInfo *baserel, RangeTblEntry *rte); extern PGDLLIMPORT add_scan_path_hook_type add_scan_path_hook; /* Hook for plugins to add custom join path */ typedef void (*add_join_path_hook_type)(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist, List *mergeclause_list, SemiAntiJoinFactors *semafac, Relids param_source_rels, Relids extra_lateral_rels); extern PGDLLIMPORT add_join_path_hook_type add_join_path_hook; 7 PostgreSQL Unconference #3
  • 8. Custom Scan API (2/3) エグゼキュータに介入するためのコールバック#1 typedef void (*InitCustomScanPlan_function) (PlannerInfo *root, CustomScan *cscan_plan, CustomPath *cscan_path, List *tlist, List *scan_clauses); typedef void (*SetPlanRefCustomScan_function) (PlannerInfo *root, CustomScan *cscan_plan, int rtoffset); typedef void (*BeginCustomScan_function) (CustomScanState *csstate, int eflags); typedef TupleTableSlot * (*ExecCustomScan_function)(CustomScanState *csstate); 8 PostgreSQL Unconference #3
  • 9. Custom Scan API (3/3) エグゼキュータに介入するためのコールバック#2 typedef Node *(*MultiExecCustomScan_function) (CustomScanState *csstate); typedef void (*EndCustomScan_function) (CustomScanState *csstate); typedef void (*ReScanCustomScan_function) (CustomScanState *csstate); typedef void (*MarkPosCustomScan_function) (CustomScanState *csstate); typedef void (*RestorePosCustom_function) (CustomScanState *csstate); typedef void (*ExplainCustomScan_function) (CustomScanState *csstate, ExplainState *es); 9 PostgreSQL Unconference #3
  • 10. PostgreSQLクエリ処理の流れ (1/2) SQL IndexScan クエリ・パーサ (構文解析) クエリ・オプティマイザ (最適化) Path Cost=100 IndexPath Cost=5 クエリ・エグゼキュータ(実行) SeqScan IndexScan 結果セット 10 PostgreSQL Unconference #3 TidScan
  • 11. PostgreSQLクエリ処理の流れ (2/3) SQL CustomScan クエリ・パーサ (構文解析) クエリ・オプティマイザ (最適化) Path Cost=100 IndexPath Cost=5 クエリ・エグゼキュータ(実行) SeqScan IndexScan Custom ScanPath Cost=1 Extension モジュール 結果セット 11 TidScan PostgreSQL Unconference #3 Custom Scan
  • 12. PostgreSQLクエリ処理の流れ (3/3) SQL クエリ・パーサ (構文解析) 実行開始 クエリ・オプティマイザ (最適化) 一行を取り出し 実行終了 クエリ・エグゼキュータ(実行) 再帰的な 呼び出しも…。 結果セット 12 Custom Scan API Extension モジュール PostgreSQL Unconference #3
  • 13. FDWとの違い FDW / Foreign Table  参照や更新の対象として使用できる。  “一行を返す”時に、データ型は常にテーブル定義と 一致していなければならない。  オブジェクトを実装する役割 Custom Scan API  参照や更新の対象として使用できない  “一行を返す” 時に、データ型を任意に定める事ができる。 (上位ノードの期待通りである事はモジュールの責任)  メソッドを実装する役割 13 PostgreSQL Unconference #3
  • 14. データ型が変動するとは? SELECT * FROM t1 JOIN t2 ON t1.a = t2.x; HeapTuple JOIN Scan t1 HeapTuple 事前にデータ型は 明らかであるのか? Custom Scan Scan t1 Scan t2 Scan t2  テーブルスキャンを実装する場合、返却されるタプルの型は事前に明らか CREATE TABLEで指定したデータ型  JOINを実装する場合、返却されるタプルの型は毎回変わる JOINされるテーブルの定義による 14 PostgreSQL Unconference #3
  • 15. JoinをCustomScan APIで置き換えた例 Postgres_fdw への機能拡張  Foreign table 同士のJOINで、  同一のForeign serverにホストされており、  結合条件がリモート実行可能なものである場合 postgres=# explain (costs off, verbose) select * from ft1 where b like '%aaa%'; QUERY PLAN ---------------------------------------------------------Foreign Scan on public.ft1 Output: a, b Remote SQL: SELECT a, b FROM public.t1 WHERE ((b ~~ '%aaa%'::text)) (3 rows) 15 PostgreSQL Unconference #3
  • 16. JoinをCustomScan APIで置き換えた例 Postgres_fdw への機能拡張  Foreign table 同士のJOINで、  同一のForeign serverにホストされており、  結合条件がリモート実行可能なものである場合 postgres=# explain (costs off, verbose) select * from ft1 join ft2 on a = x where b like '%aaa%'; QUERY PLAN ---------------------------------------------------------Custom Scan (postgres-fdw) Output: a, b, x, y Remote SQL: SELECT r1.a, r1.b, r2.x, r2.y FROM (public.t1 r1 JOIN public.t2 r2 ON ((r1.a = r2.x))) WHERE ((r1.b ~~ '%aaa%'::text)) (3 rows) 16 PostgreSQL Unconference #3
  • 17. その他の利用例 (1/3) – Cache-only Scan postgres=# EXPLAIN(costs off) SELECT a,b FROM t1 WHERE a > b; QUERY PLAN --------------------------------------Custom Scan (cache scan) on t1 Filter: ((a)::double precision > b) (2 rows) エグゼキュータ 列A、Bのみ キャッシュ キャッシュヒット Custom Scan (cache_scan) ミスヒット 17 heap PostgreSQL Unconference #3
  • 18. その他の利用例 (2/3) – Cache-only Scan postgres=# EXPLAIN(costs off) SELECT a,b FROM t1 WHERE a > b; QUERY PLAN --------------------------------------Custom Scan (cache scan) on t1 Filter: ((a)::double precision > b) (2 rows) エグゼキュータ 列A、Bのみ キャッシュ キャッシュヒット Custom Scan (cache_scan) ミスヒット 18 heap PostgreSQL Unconference #3 キャッシュに載ったデータを GPGPUで”超”並列処理。
  • 19. その他の利用例 (3/3) – Cheat Join !!Just An Idea!! (3,2) (2,4) PostgreSQL Unconference #3 (3,1) (4,3) : 19 (8,2) (2,3) Right Relation (7,3) (2,2) Left Relation (5,1) (2,1) Nest Loop (4,2) (1,5) Custom Scan (cheat join) (7,3) (1,4) Cheat Join Right Tid (1,3) エグゼキュータ Left Tid (1,2) ② カンペを参照 しつつ TidScan : ① カンペを作成
  • 20. Call for your feedback! 20 PostgreSQL Unconference #3