Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PostgresDoc: Using Postgres as a Document Orien...
Search
Myles Braithwaite
September 13, 2016
Technology
220
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PostgresDoc: Using Postgres as a Document Oriented Database
Myles Braithwaite
September 13, 2016
More Decks by Myles Braithwaite
See All by Myles Braithwaite
pgloader
myles
0
92
IndieWeb - The person-focused alternative to the corporate web
myles
0
210
Let's Encrypt All the Things
myles
1
140
Chat Bots
myles
1
170
Ansible: Orchestrate Your Infustrature Like Gustav Mahler
myles
0
230
Take a Stroll in the Bazaar
myles
0
150
LessCSS
myles
0
55
jrnl
myles
0
54
Apache CouchDB
myles
0
51
Other Decks in Technology
See All in Technology
データ品質を壊しながらSnowflakeのAIに分析させてみた
kawanago
0
370
メルカリにおけるAI時代の高速プロトタイピング基盤「Arca」
ryotarai
18
14k
AWS FinOps Agent 結局何が得意なの?
siromi
0
230
行動するAIのためのオントロジー | DevRev — Encraft #26.pdf
dvrv_tknrszk
2
670
[2026-09-30]ロックンロールは鳴り止まないっ - 信頼性かまってちゃん - 「データ駆動を投げ捨ててまで。」追いかける信頼性改善に向けた取り組みの話
tosite
0
180
HacobuにおけるFDEとは/登壇資料(戸井田 裕貴)
hacobu
PRO
1
700
OpenClawでAzure DevOpsのWiki更新を自動化する - クラウドAIだけでは届かない場所へ
yutakaosada
0
140
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
16
120k
猫でもわかるKiro Web
kentapapa
1
160
AgentCoreで実践するハーネスエンジニアリング
yakumo
1
190
OSC2026on_the-world-is-waiting-for-your-voice.pdf
naruoga
0
220
覗いてみよう 関数型ビジュアル言語×2Dグラフィックスの世界
yohyamasaki
0
160
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
28
2.8k
The Cost Of JavaScript in 2023
addyosmani
55
10k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
540
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
520
The Art of Programming - Codeland 2020
erikaheidi
57
14k
For a Future-Friendly Web
brad_frost
183
10k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Prompt Engineering for Job Search
mfonobong
0
470
30 Presentation Tips
portentint
PRO
1
410
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
A better future with KSS
kneath
240
18k
Transcript
PostgresDoc Using Postgres as a Document Database Myles Braithwaite |
mylesb.ca |
[email protected]
| @mylesb 1
How I Learned to Stop Worrying and Love the SQL
Again Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 2
Relational Database Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb
3
Relational Database Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb
4
Document Oriented Database { "_id": "b04b9746-77bc-11e6-a75a-34363bd0c9bc", "name": "Lot 9 Pilsner",
"brewer": "Creemore Springs", "rating": 4.2 } Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 5
Document Oriented Database { "_id": "b04b9746-77bc-11e6-a75a-34363bd0c9bc", "date": "2016-09-11T16:35:33.773325-0400" "customer": {
"name": "Myles Braithwaite", "telephone": "+1 (416) 555-1234", "address": "123 Street Ave." }, "order": [{ "name": "beer", "price_per_unit": 12, "quantity": 2, "total": 24 }], "total": 24, "payment": { "type": "VISA", "number": "12345", "expiry": "2001-04" } } Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 6
Document Oriented Database Myles Braithwaite | mylesb.ca |
[email protected]
|
@mylesb 7
Why not both? Myles Braithwaite | mylesb.ca |
[email protected]
|
@mylesb 8
Why not both? • Avoid complicated JOINs. • Ability to
store complicated JSON API responses in the database. • Avoid transforming data before returning it via a JSON API. Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 9
Postgres JSON/JSONB Type Myles Braithwaite | mylesb.ca |
[email protected]
|
@mylesb 10
CREATE TABLE Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb
11
CREATE TABLE notes ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
notebook_id UUID REFERENCES notebooks (id), note JSONB ); Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 12
INSERT Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 13
INSERT INTO notes VALUES ( '675afef8-7863-11e6-8914-34363bd0c9bc', '8921ad42-300e-447a-8417-ec92bb18e2df', '{ "name": "PostgresDoc",
"tags": ["Postgres", "Document Oriented Database"], "body": "Hello, World" }' ) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 14
SELECT Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 15
SELECT note->>'name' AS name FROM notes; name ---------------------------------------- PostgresDoc World
Domination Plans Superman < Batman > Supergirl = Batgirl (3 rows) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 16
SELECT jsonb_array_elements_text(note->'tags') AS tag FROM notes WHERE id = '675afef8-7863-11e6-8914-34363bd0c9bc';
tag --------------------------- Postgres Document Oriented Database (2 rows) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 17
SELECT id, note FROM notes WHERE note->'tags' ? 'Postgres'; id
| note | -------------------------------------+------------------------ 675afef8-7863-11e6-8914-34363bd0c9bc | {"name": "PostgresDoc"} (1 row) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 18
SELECT count(*) FROM notes WHERE note ? 'checklist'; count -------
1 (1 row) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 19
CREATE INDEX idx_checklist ON notes ((note->>'tags')) Myles Braithwaite | mylesb.ca
|
[email protected]
| @mylesb 20
ORMs Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 21
SQLAlchemy note_table = Table('notes', metadata, Column('id', Integer, primary_key=True), Column('note', JSONB)
) with engine.connect() as conn: conn.execute( note_table.insert(), note = {"name": "Postgres", "tags": ["Postgres"]} ) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 22
Rails create_table :notes do |t| t.json 'note' end class Note
< ApplicationRecord end Note.create(note: { name: "Postgres", tags: ["Postgres"]}) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 23
https://github.com/myles/2016-09-13-postgresdoc Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 24