SlideShare una empresa de Scribd logo
1 de 61
Descargar para leer sin conexión
Ruian Huang @Golang Taipei 2020/06/23
Golang 的熱⾨ 

PostgreSQL Library 比較
透過觀察封包比較實作上的差異
Dcard ⼯程師
• 交⼤資⼯畢業

• 偶爾寫點⽂章

https://medium.com/@ruian



包含 PG Wire Protocol 與
Query Optimizer 相關⽂章
Ruian Huang
回顧去年同樣於 Golang Taipei 分享的主題
回顧去年同樣於 Golang Taipei 分享的主題
這次分享的內容主要來⾃ Medium 上的⽂章,

改⽤ Wireshark 展⽰,並補充 Connection Pool 細節
這次比較的 lib
• lib/pq v1.7.0

• Gorm (同 lib/pq)

• go-pg/v10

• pgx/v4
我們希望這些 lib 能幫我們做什麼?怎麼做的?
• 傳送 SQL 讀取結果 -> ⽤什麼形式?

• 預防 SQL Injection -> 怎麼預防的?
使⽤範例: lib/pq
使⽤範例: gorm
使⽤範例: go-pg
使⽤範例: pgx
我們希望這些 lib 能幫我們做什麼?怎麼做的?
• 傳送 SQL 讀取結果 -> ⽤什麼形式?

• Printable SQL Representation?

• 預防 SQL Injection -> 怎麼預防的?

• Parameter Placeholder?
範例 Users 表
先插入⼀個有 200 bytes 的 secret 的 foo user 然後⽤不同 lib 讀出來看看
go-pg
go-pg 預設即便使⽤ Parameter Placeholder 寫法,

依然使⽤ Simple Query 與 PG 溝通。

SQL Injection 的防範仰賴 go-pg 做正確的參數跳脫
並且在 Simple Query 下,回傳 bytea 以 Hex 表⽰

體積變成原本的兩倍
lib/pq
lib/pq
lib/pq
lib/pq
lib/pq 則使⽤ Extended Query Protocol

避免 SQL Injection,並且指定 bytea 使⽤

Binary Format 回傳
lib/pq
bytea 使⽤ Binary Format 回傳

沒有浪費頻寬
關於 PG 的 Protocol 細節可以於⼿冊 52 章節找到
Extended Query Protocol 的好處
• 避免 SQL Injection

• 重複利⽤ Parse 結果?

• Binary Format 節省頻寬?
PostgreSQL Binary Format vs Text Format
Binary Format Size Text Format Size Text Example
UUID 16 bytes 36 bytes
d4d1d263-4f5c-49bb
-ae36-1e26d4adf44a
Timestampz 8 bytes ~ 30 bytes
2020-06-22
17:16:28.87282+00
Bigint 8 bytes 1 ~ 19 bytes 9223372036854775807
Bytea Variable
Hex

2x size
x1feb6644e0
Extended Query Protocol 的⽀援程度
⾃動使⽤ EQP
Binary
Parameter
Binary Data
Result
重複使⽤ Parse
lib/pq V
gorm V
go-pg X
pgx V
lib/pq
lib/pq 在使⽤ Prepare 時,預設使⽤ Text 傳參數

回傳的 Bytea 使⽤ Binary Format,

回傳的 Timestampz 使⽤ Text Format
lib/pq + binary_parameters=yes
Prepare 在開啟 binary_parameter 參數後,

Bytea 參數也會使⽤ Binary Format 傳遞給 PG
Extended Query Protocol 的⽀援程度
⾃動使⽤ EQP
Binary
Parameter
Binary Data
Result
重複使⽤ Parse
lib/pq + Parepare - X Bytea Only
lib/pq + Parepare
binary_parameters=yes - Bytea Only Bytea Only
gorm(lib/pq) V
gorm(lib/pq) +
binary_parameters=yes V
go-pg X
pgx V
gorm (與 lib/pq 不使⽤顯性 Prepare 呼叫相同)
gorm + binary_parameters=yes
gorm 與 lib/pq 比較特別的是

當不使⽤ Prepare 顯興呼叫,並且開啟

binary_parameters 後,P/B/D/E 這幾個

訊息會⼀起送給 PG,取得結果只有 1 RTT



但 Bytea 的回傳它會改回使⽤ Hex Format
Extended Query Protocol 的⽀援程度
⾃動使⽤
EQP
Binary
Parameter
Binary

Data Result
RTT
重複使⽤
Parse
lib/pq + Parepare - X Bytea Only 2
lib/pq + Parepare
binary_parameters=
yes
- Bytea Only Bytea Only 2
gorm(lib/pq) V X Bytea Only 2
gorm(lib/pq) +
binary_parameters=
yes
V Bytea Only X 1
go-pg X
pgx V
go-pg
go-pg 完全不⽀援 Binary Format
Extended Query Protocol 的⽀援程度
⾃動使⽤
EQP
Binary
Parameter
Binary

Data Result
RTT
重複使⽤
Parse
lib/pq + Parepare - X Bytea Only 2
lib/pq + Parepare
binary_parameters=
yes
- Bytea Only Bytea Only 2
gorm(lib/pq) V X Bytea Only 2
gorm(lib/pq) +
binary_parameters=
yes
V Bytea Only X 1
go-pg + Parepare - X X 2
pgx V
pgx
pgx 搭配 pgtype 即可⽀援多達 70 多種的 Binary Format
Extended Query Protocol 的⽀援程度
⾃動使⽤
EQP
Binary
Parameter
Binary

Data Result
RTT
重複使⽤
Parse
lib/pq + Parepare - X Bytea Only 2
lib/pq + Parepare
binary_parameters=
yes
- Bytea Only Bytea Only 2
gorm(lib/pq) V X Bytea Only 2
gorm(lib/pq) +
binary_parameters=
yes
V Bytea Only X 1
go-pg + Parepare - X X 2
pgx V 70 types 70 types 2
重複使⽤ Parse 

減少 RTT 與解析 SQL 次數?
預期中的實作應該像右邊流程圖

go-pg ⽬前卻不是這樣
lib/pq
將 Connection Pool ⼤⼩設定成 2

並送 6 個慢 query 觀察 lib/pq 如何使⽤

Extended Query Protocol
不顯性呼叫 Prepare 時

lib/pq 並不會重⽤ Parse

因此這邊可以看到 6 次 Parse
lib/pg + Prepare
lib/pg + Prepare
顯性呼叫 Prepare 時

lib/pq 重⽤ Parse

因此這邊只看到 2 次 Parse

並且後續的查詢只需要 1 RTT
Extended Query Protocol 的⽀援程度
⾃動使⽤
EQP
Binary
Parameter
Binary

Data Result
RTT(重複使⽤)
重複使⽤
Parse
lib/pq + Parepare - X Bytea 2(1) V
lib/pq + Parepare
binary_parameters=
yes
- Bytea Bytea 2(1) V
gorm(lib/pq) V X Bytea 2 X
gorm(lib/pq) +
binary_parameters=
yes
V Bytea X 1 X
go-pg + Parepare - X X 2
pgx V 70 types 70 types 2
go-pg
53
go-pg 在相同的測試中只看到 1 次 Parse

與預期中看到 2 次 Parse 不符。
這是因為 go-pg 在 prepare stmt 創建的的當下

會立刻綁定⼀個 connection 給它。



這個 stmt 雖然可以 concurrently 呼叫,但它的

concurrency 被限制在只有 1,容易有效能問題。
Extended Query Protocol 的⽀援程度
⾃動使⽤
EQP
Binary
Parameter
Binary

Data Result
RTT(重複使⽤)
重複使⽤
Parse
lib/pq + Parepare - X Bytea 2(1) V
lib/pq + Parepare
binary_parameters=
yes
- Bytea Bytea 2(1) V
gorm(lib/pq) V X Bytea 2 X
gorm(lib/pq) +
binary_parameters=
yes
V Bytea X 1 X
go-pg + Parepare - X X 2(1) concurrency=1
pgx V 70 types 70 types 2
pgx
Extended Query Protocol 的⽀援程度
⾃動使⽤
EQP
Binary
Parameter
Binary

Data Result
RTT(重複使⽤)
重複使⽤
Parse
lib/pq + Parepare - X Bytea 2(1) V
lib/pq + Parepare
binary_parameters=
yes
- Bytea Bytea 2(1) V
gorm(lib/pq) V X Bytea 2 X
gorm(lib/pq) +
binary_parameters=
yes
V Bytea X 1 X
go-pg + Parepare - X X 2(1) concurrency=1
pgx V 70 types 70 types 2(1) V
Q&A
夥伴招募中,歡迎投履歷~

Más contenido relacionado

Similar a Golang PostgreSQL Libraries Comparasion With Wireshark

談 Uber 從 PostgreSQL 轉用 MySQL 的技術爭議
談 Uber 從 PostgreSQL 轉用 MySQL 的技術爭議談 Uber 從 PostgreSQL 轉用 MySQL 的技術爭議
談 Uber 從 PostgreSQL 轉用 MySQL 的技術爭議Yi-Feng Tzeng
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析wavefly
 
Python meetup 1
Python meetup 1Python meetup 1
Python meetup 1Vic Yang
 
微博Lamp性能优化之路(2014)
微博Lamp性能优化之路(2014)微博Lamp性能优化之路(2014)
微博Lamp性能优化之路(2014)Xinchen Hui
 
⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨Wen-Tien Chang
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeAngel Boy
 
[3]投影片 futurewad樹莓派研習會 141204
[3]投影片 futurewad樹莓派研習會 141204[3]投影片 futurewad樹莓派研習會 141204
[3]投影片 futurewad樹莓派研習會 141204CAVEDU Education
 
Adorable python
Adorable pythonAdorable python
Adorable pythonRhythm Sun
 

Similar a Golang PostgreSQL Libraries Comparasion With Wireshark (9)

談 Uber 從 PostgreSQL 轉用 MySQL 的技術爭議
談 Uber 從 PostgreSQL 轉用 MySQL 的技術爭議談 Uber 從 PostgreSQL 轉用 MySQL 的技術爭議
談 Uber 從 PostgreSQL 轉用 MySQL 的技術爭議
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析
 
Python meetup 1
Python meetup 1Python meetup 1
Python meetup 1
 
微博Lamp性能优化之路(2014)
微博Lamp性能优化之路(2014)微博Lamp性能优化之路(2014)
微博Lamp性能优化之路(2014)
 
⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨
 
rebar erlang
rebar erlangrebar erlang
rebar erlang
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledge
 
[3]投影片 futurewad樹莓派研習會 141204
[3]投影片 futurewad樹莓派研習會 141204[3]投影片 futurewad樹莓派研習會 141204
[3]投影片 futurewad樹莓派研習會 141204
 
Adorable python
Adorable pythonAdorable python
Adorable python
 

Más de Jui An Huang (黃瑞安)

Más de Jui An Huang (黃瑞安) (7)

Www 2017第二次新生訓練:系統設計
Www 2017第二次新生訓練:系統設計Www 2017第二次新生訓練:系統設計
Www 2017第二次新生訓練:系統設計
 
Www 2017第一次新生訓練:系統分析
Www 2017第一次新生訓練:系統分析Www 2017第一次新生訓練:系統分析
Www 2017第一次新生訓練:系統分析
 
Www 2017第三次新生訓練:transaction, migration, worker
Www 2017第三次新生訓練:transaction, migration, workerWww 2017第三次新生訓練:transaction, migration, worker
Www 2017第三次新生訓練:transaction, migration, worker
 
Elasitcsearch + Logstash + Kibana 日誌監控
Elasitcsearch + Logstash + Kibana 日誌監控Elasitcsearch + Logstash + Kibana 日誌監控
Elasitcsearch + Logstash + Kibana 日誌監控
 
Elasticsearch 簡介
Elasticsearch 簡介Elasticsearch 簡介
Elasticsearch 簡介
 
Docker應用
Docker應用Docker應用
Docker應用
 
Git 版本控制 (使用教學)
Git 版本控制 (使用教學)Git 版本控制 (使用教學)
Git 版本控制 (使用教學)
 

Golang PostgreSQL Libraries Comparasion With Wireshark