版本問題

11 之前default value會做insert
11 之後不會insert 只有用到 查詢到才會做
(從 PG 11 之後就不需要 rewrite 整個 table,而是先將 default value 存在 system catalog 上)

pk

背景:
有RDBMS課程學生問他跑benchmarking on postgresql,他發現用auto-inc比uuidV4更快,他想知道原因。
(謎之聲:原來這傢伙真的有售後服務的)
------------------------------------
首先:
我跟他談過了,他的測試的方向是沒什麼大問題的,而且是開128 threads來跑insert的。
所以,uuidv4真的在benchmarking時會比auto-increment慢。
我直接先跳到結論:
benchmarking和現實世界不完全相同的。
benchmarking和現實世界不完全相同的。
benchmarking和現實世界不完全相同的。
例子:
graphic card A在benchmarking 時打LOL有100 average FPS,然後graphic card B就只有80 average FPS。
表面上應該買card A吧。
但是呢,card A在大部份時間都有100FPS,但半小時遊戲內有可能有觸發過熱保護。
讓你跟艾希啊,易大師啊你追我逐時沒了畫面半秒……然後你就趴在地上GG了……
這樣子的頂級效能graphic card你買不買???
(謎之聲:這傢伙整天說電腦太爛才會打輸LOL的……)
------------------------------------
先說auto-inc為何benchmarking會比較快:
1)
auto-inc insert在PK的B+ tree index中,只會動到最右邊的那個leaf node。
然後uuidV4 insert,是會隨機的改動到PK B+tree的任一leaf node。
所以,如果有大量insert時,auto-inc只有最右邊新追加的數個PK leaf node需要進REDO log(和後續寫進storage的IO)
然後uuidV4是有大量被改動過的PK leaf node需要寫進REDO log的。
別忘記一點:即使你的8KB leaf node只改動其中一byte也好,也是需要整個8KB node都抄到REDO log的。
(不然突然斷電了有可能在storage寫到一半的data會救不回來)
所以,uuid在PK index上,是用得遠比auto-inc更多的IO的。
2)
uuidv4是16 byte,而postgresql bigint是8 byte,這樣子pk index size會差了一倍的。
在benchmarking會有差別的。(雖然現實世界今天硬體這是可以無視的)
1+2的後果:就是auto-inc在benchmarking會跑得比較快是合理的。
------------------------------------
那麼,為何明明auto-inc跑起來比較快吃比較少的IO,現實上pg人大都喜歡用natural key / UUID而不喜歡auto-inc???
現實上……
1)
越近期被加入database的資料,就越有可能被拿出來改動。
用auto-inc下,所有近期的資料全在PK-index的右邊,所以右邊忙死然後左邊空閒。
這讓data contention更有可能發生,限制了「同一時間下,能有多少個平行工作一起來跑」的能力。
2)
很多transaction不會只改動single record。
作為backend人我們最最最害怕的,是locking的「滾雪球」。
當Thread A拿不到需要的東西而被卡住時(blocking),很可能Thread A他現在已經拿了的東西其他人也需要用上,觸發Thread B的blocking。
然後就到Thread C, Thread D......
到最後就是:整個database明明有大量的CPU thread可以用,但就看起來是single thread在跑一樣。
------------------------------------
postgresql的長期設計哲學:
寧可single thread表現慢一點,寧可resource用得多一點。
(對的,auto-inc用的IO和storage size都比uuidv4少)
但是,postgresql是希望「同一時間能做盡量多的事務」(a.k.a. 迴避data contention)
所以就跟開始時的graphic card一樣:
你是寧可80FPS穩穩跑?
還是寧可100FPS爽爽跑,但有時會黑畫面???
別忘記一點:
用多了io嘛,就拿dxxxxxg數據跟老闆申請加大機器硬體就好。
data contention嘛,你是除了被老闆罵,其實你是沒什麼東西能真正做的。
錢是公司的,命是自己的~~~
------------------------------------
就最後一句:
為何20年前大家都不喜歡pg而喜歡mysql?
而今天越來越多人轉到pg???
(真正答案:因為萬惡的Oracle)
另一個答案是:
20年前硬體超級超級貴。
如果有二片graphic card,一片30 FPS但有可能畫面會黑半秒,一片15 FPS但畫面不會黑。
很多人都會賭「不是剛剛好打會戰時黑畫面」而要前者的。
今天硬體便宜。
100FPS和80FPS開始沒感覺,畫面會不會突然黑掉就當然是問題囉。

優化教學 instagram

https://instagram-engineering.com/handling-growth-with-postgres-5-tips-from-instagram-d5d7e7ffdfcb

explain

image

image
https://www.youtube.com/watch?v=PS-ozd3Jdxo

message defuplication

可以用lsn的message id當作key

https://medium.com/dcardlab/postgresql-技術筆記-跟疾管署沒有關係的cdc-218e27eb363d

PostgreSQL 優化 看io等

https://www.youtube.com/watch?v=PS-ozd3Jdxo

聚合

欄位依賴性
https://blog.csdn.net/danpu0978/article/details/106775634

db詳解 必看 必看

https://ithelp.ithome.com.tw/articles/10320923

schema change 問題

https://blog.kennycoder.io/2023/12/31/Postgres-常見的-schema-change-問題/

Copy Operation

https://blog.kennycoder.io/2021/09/19/Postgres-Copy-Operation-細節探討/#more

少為人知 PostgreSQL 的功能

https://blog.kennycoder.io/2023/12/30/少為人知-PostgreSQL-的功能/

postgres vs mysql

https://blog.kennycoder.io/2023/11/18/談談-Postgres-與-MySQL-的差異/

image
image

upgrade

https://blog.kennycoder.io/2022/11/27/透過-pg-upgrade-快速升級-Postgres/

分頁

https://blog.kennycoder.io/2022/08/07/Postgres-各種分頁的方式介紹/

copy 大量insert , output優化

https://blog.kennycoder.io/2021/09/19/Postgres-Copy-Operation-細節探討/#more

index

說明用跟mysql差異
https://www.bilibili.com/video/BV1hr4y1d7hj/?spm_id_from=333.788&vd_source=f726dd30598fe01d9fbc9c5a988d6408

詳細
https://blog.kennycoder.io/2021/08/08/Postgres-深入探討index-engine-的行為/#more

看了才知道會有不同index還會用到memory

image

特別的 計算
image

image

最左

image

建立index要小心

image

觸發有問題

function

WAL

https://ithelp.ithome.com.tw/articles/10325322

physical replication

https://blog.kennycoder.io/2023/05/01/Postgres-一文看懂-physical-replication-及-logical-replication-概念/

https://blog.kennycoder.io/2022/12/11/Postgres-設定-physical-replication-教學/

https://blog.kennycoder.io/2022/11/27/透過-pg-upgrade-快速升級-Postgres/

https://blog.kennycoder.io/2023/02/12/介紹-postgres-logical-decoding-的概念與示範/

https://www.modb.pro/db/52463
image

太多了 建議他blog想相關的看一遍

migrate

image
https://blog.kennycoder.io/2021/07/24/Postgres-zero-downtime-migration-該注意的細節/

index在migrate

image

image
image
我猜測是因為mvcc原因

PG Cluster 進行主版本的升級

https://blog.kennycoder.io/2023/08/19/How-We-Achieved-Minimal-Downtime-During-Our-PostgreSQL-Database-Upgrade-中文版/

image

Copy Operation

https://blog.kennycoder.io/2021/09/19/Postgres-Copy-Operation-細節探討/#more

pgpool-ll 主從 proxy

https://blog.kennycoder.io/2023/01/08/pgpool-ll-stream-replication-架設教學/

架構

https://www.bilibili.com/video/BV1C84y1f7n1/?spm_id_from=333.788&vd_source=f726dd30598fe01d9fbc9c5a988d6408

full text

一定要docker , mac有問題
https://stackoverflow.com/questions/38249935/pg-trgm-behaves-differently-on-ubuntu-and-mac-os-x

建立function
https://juejin.cn/post/6844903718572261389

目前踩過的坑
第一 pg_tram支援中文 9版後
另外1. 2字不支援 所以要跑自定義function
建立好function再利用gin 去建立index
另外在選取index的時候要where這邊的 不然時間會很長
另外function名稱不支援大小寫 所以要用_ 在給全縣的時候才不會有問題

注意事項

時間加上時區轉換

https://stackoverflow.com/questions/11126037/postgresql-date-with-timezone

-> ->>

https://stackoverflow.com/questions/38777535/what-is-the-difference-between-and-in-postgres-sql

時間

自動欄位


filter

https://medium.com/little-programming-joys/the-filter-clause-in-postgres-9-4-3dd327d3c852

分庫

https://medium.com/nerd-for-tech/implementing-table-partitioning-partitioned-views-in-sql-server-dc1528d1dd6e
如果是sql server要自己聯合

https://docs.postgresql.tw/the-sql-language/ddl/table-partitioning
但如果是postgre都會幫你自己聯合

大量刪除

https://byron0920.pixnet.net/blog/post/85759990

優勢

auto-increment

https://stackoverflow.com/questions/48446399/sql-auto-increment-pgadmin-4

注意5之後用法不一樣

PostgreSQL 如何估算 HashAggregate 的 Return Rows ,以及低估的後果

https://medium.com/pgsql-tw/postgresql-如何估算-hashaggregate-的-return-rows-以及低估的後果-cc06719641da

RETURNING

https://docs.postgresql.tw/the-sql-language/data-manipulation/returning-data-from-modified-rows

varchar

character varying and varchar is correct.
https://stackoverflow.com/questions/56977456/varchar-is-missing-on-pgadmin4-ui

cli

登入 psql -U (userName) (databse name)

\dt 列出全部table

function

coalesce()
會判斷第一個是不是null 是的話就用第二個參數 預設欄位

between 特別用法

Materialized View

https://harryuan-65.medium.com/提升服務效能-減輕db負擔-2-materialized-view-d6addb870c51

func

https://www.sjkjc.com/postgresql-ref/array_append/

運算符號

&& 運算符用於檢查兩個數組是否有重疊的元素

ip

enum

https://ithelp.ithome.com.tw/articles/10224450
自己認為一般就const存
如果像是coloer這種可能會有順序的在用

index

https://blog.kennycoder.io/2021/08/08/Postgres-深入探討index-engine-的行為/#more

race Condition

https://blog.kennycoder.io/2020/03/06/RDBMS-不同的Isolation-Level-Race-Condition示範/

tags: MySql