Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PostgreSQLクエリプロトコルの実証 / Demonstration of Postgr...
Search
kabaome
April 24, 2020
Technology
1.5k
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PostgreSQLクエリプロトコルの実証 / Demonstration of PostgreSQL Query Protocol
kabaome
April 24, 2020
More Decks by kabaome
See All by kabaome
PostgreSQL:行数推定を読み解く/row-estimation
kyabatalian
4
4.9k
Other Decks in Technology
See All in Technology
壊して学ぶAWS CDK: そのcdk deployで消えるもの、残るもの
k_adachi_01
1
420
AI時代の開発生産性は、個人技からチーム設計へ
moongift
PRO
4
2.4k
「AIに依存している」と 「AIを使いこなしている」の違い
k8yasuma
0
120
Gen3R: 3D Scene Generation Meets Feed-Forward Reconstruction
spatial_ai_network
0
130
プロダクト開発組織の現在地(Ver.2026/07) / product-organization
kaonavi
0
110
脱金融のフューチャー・デザイン / Future Design Beyond Finance
ks91
PRO
0
160
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
15
110k
タスクの複雑さでモデルを選ぶ ── Thompson Samplingで動かす“トークン/コスト最適化
satohy0323
0
570
シンガポールで登壇してきます
yama3133
0
250
SRE本の知られざる名シーン / The Hidden Gems of Google SRE Book
nari_ex
1
420
kaonavi Tech Night#1
kaonavi
0
110
発表と総括 / Presentations and Summary
ks91
PRO
0
140
Featured
See All Featured
Information Architects: The Missing Link in Design Systems
soysaucechin
0
1k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
A Tale of Four Properties
chriscoyier
163
24k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
390
The World Runs on Bad Software
bkeepers
PRO
72
12k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
420
Code Review Best Practice
trishagee
74
20k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.6k
WCS-LA-2024
lcolladotor
0
720
Transcript
PostgreSQL クエリプロトコルの実証 @kabaome 第12回 PostgreSQLアンカンファレンス@オンライン
概要 https://github.com/kbth/PostgresMessageSerializer • プロトコルわかりたい • シリアライザをつくった • プロトコルを話してみた
デモ
背景
きっかけ: 事象 PostgreSQL PostgreSQL Pgpool-Ⅱ Application Client Library クライアントライブラリを バージョンアップ
アプリケーションがハング。。
きっかけ: 原因 PostgreSQL PostgreSQL Pgpool-Ⅱ Application Client Library https://www.pgpool.net/docs/latest/en/html/release-3-6-13.html 利用するクエリプロトコルが
変更されていた 拡張クエリ特有のバグがあった 簡易クエリ → 拡張クエリ
プロトコル わかりたい
公式ドキュメントを読む ? https://www.postgresql.jp/document/11/html/protocol-overview.html 整理されている◎ 文字でイメージしづらい△
サーバ or クライアントのコードを読む ? 実践で使われている◎ 高度に最適化されている△ Name Language Comments Website
DBD::Pg Perl Perl DBI driver https://metacpan.org/release/DBD-Pg JDBC Java Type 4 JDBC driver https://jdbc.postgresql.org/ libpqxx C++ C++ interface http://pqxx.org/ node-postgres JavaScript Node.js driver https://node-postgres.com/ Npgsql .NET .NET data provider http://www.npgsql.org/ pgtcl Tcl https://github.com/flightaware/Pgtcl pgtclng Tcl https://sourceforge.net/projects/pgtclng/ pq Go Pure Go driver for Go's database/sql https://github.com/lib/pq psqlODBC ODBC ODBC driver https://odbc.postgresql.org/ psycopg Python DB API 2.0-compliant http://initd.org/psycopg/ https://www.postgresql.org/docs/12/external-interfaces.html
通信パケットを読む ? 具体的な通信がみえる◎ 読みづらい△ https://www.manniwood.com/2016_12_29/tcpdump_pg.html
シリアライザの実装
実証する対象 • プロトコルバージョン3.0(PostgreSQL 7.4〜) • クエリ用のサブプロトコル PostgreSQL Protocol Start-up Query
Copy Termination Function Simple Query Extended Query
フォーマット Server Client Message A Message B Message C メッセージ種別
(byte) メッセージ長 (int32) ペイロード
シリアライズ メッセージ型のインスタンスから バイト列を生成したい ペイロードのフォーマットは メッセージごとに定義されている メッセージの種類を識別する
シリアライズ メッセージ種別 (byte) メッセージ長 (int32) ペイロード
デシリアライズ バイト列からメッセージ型の インスタンスを生成したい プロトコルに則って読み書きをする 例: 文字列は ’0’ で区切られる
デシリアライズ メッセージ種別 (byte) メッセージ長 (int32) ペイロード
シリアライザの実装:まとめ • クエリプロトコルの実証を目的とした • コードの品質・実用性は無視した
プロトコルの実証
準備 Server Client 2. テーブルを作る 1. ローカルPCで PostgreSQLを起動する
簡易クエリ (データなし) Server Client Query CommandComplete ReadyForQuery コマンドが完了したことを通知する サーバサイドがメッセージを受け入れる 状態になったことを通知する
SQLを送信する
簡易クエリ (データなし)
簡易クエリ (データなし): 出力の形式 サーバ(PostgreSQL)への送信 サーバからの返信 メッセージ名 メッセージのペイロードを 解釈したプロパティ
簡易クエリ (データなし) I (73) => トランザクションブロック外(Idle) T (84) => トランザクションブロック内(Tran)
E (69) => トランザクションブロック内(Error) 挿入先テーブルのOID(ない場合は「0」)と、 挿入した行数
簡易クエリ (データあり) Server Client Query RowDescription DataRow DataRow CommandComplete ReadyForQuery
行の応答が返ることを示し、 行のデータを解釈する情報を含む 行ごとのメッセージとして データを取得する
簡易クエリ (データあり)
簡易クエリ (データあり) フィールド (≒列・属性)の数 フィールド名 テーブルのOID 型のサイズ(int = 4バイト) フィールドの型のOID
簡易クエリ (データあり) 列の数 値のバイト列の長さ FormatCode = 0 : 文字列 (ASCII)
FieldTypeOid = 23 : int4 から、「1」と解釈できる
簡易クエリの内部 Server Client Query RowDescription DataRow DataRow CommandComplete ReadyForQuery parse
bind execute SQLを構文解析し、 プリペアド文を生成する プリペアド文からポータルを生成し、 実行計画を生成する ポータルを実行し、 リレーションを取得する
拡張クエリ Server Client Parse ParseComplete Bind BindComplete Describe Execute Sync
ReadyForQuery DataRow DataRow CommandComplete RowDescription SQLを構文解析し、 プリペアド文を生成する プリペアド文からポータルを生成し、 実行計画を生成する ポータルを実行し、 リレーションを取得する サーバ・クライアントの状態を 同期する
拡張クエリ
拡張クエリ 生成するステートメント名(使い回せる) パラメータの型指定(今回はなし)
拡張クエリ ParameterXxxはパラメータの設定(今回はなし) 生成するポータル名
拡張クエリ S (ステートメント) もしくは P (ポータル) を指定する
拡張クエリ 取得するデータに対してLimitをかける (≒カーソル) 実行するポータル名
簡易クエリと拡張クエリ 簡易クエリ 拡張クエリ 実行の流れ 簡易 詳細 パラメータバインド 利用できない 利用できる メリット
実装しやすい 性能・セキュリティ 利用シーン psqlやPgAdmin等のツール クライアントライブラリ
プロトコルの実証:まとめ • 簡易クエリと拡張クエリを試した • その他はブログを参照ください • トランザクション • パイプライン •
パラメータ • エラー • etc… https://kyabatalian.hatenablog.com/entry/2020/03/15/194923
まとめ
まとめ • クエリプロトコルに対してシリアライザを実装した • 実用だけでなく実証もモチベーションになりうる • 「抽象化のすきま」 が問題解決につながる