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
PostgresDoc: Using Postgres as a Document Orien...
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Myles Braithwaite
September 13, 2016
Technology
210
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
78
IndieWeb - The person-focused alternative to the corporate web
myles
0
190
Let's Encrypt All the Things
myles
1
130
Chat Bots
myles
1
160
Ansible: Orchestrate Your Infustrature Like Gustav Mahler
myles
0
220
Take a Stroll in the Bazaar
myles
0
150
LessCSS
myles
0
52
jrnl
myles
0
51
Apache CouchDB
myles
0
47
Other Decks in Technology
See All in Technology
Bits Agent Builder の⼊⾨と活⽤事例
nulabinc
PRO
0
160
【CEDEC2026】Creative Approaches to Localizing the Dialects and Unique Speech of Umamusume: Pretty Derby Characters in English
cygames
PRO
3
24k
SO-101×VLAによる3色キューブのピック&プレース
abeja
0
210
攻撃と防御で学ぶAI時代のプロダクトセキュリティ演習
recruitengineers
PRO
9
2.9k
認知負荷をGemini で溶かす — GKE 基盤「Orbit」における AI エージェントの実践
sansantech
PRO
1
310
20260804_Q4AzureUpdateBite_FabricDataAgentの精度を高める設計.pdf
matayuuu
1
140
Flutterをカメラで動かしたかった話
sony
1
140
グローバル基準のSREは、運用現場でどう機能したか:成熟度アセスメントの実践 / SRE NEXT 2026
sorawatanabe
0
270
侵入は突然に 〜 IoTマルウェアと悪用される家庭の機器 ~ / When Intrusion Strikes: IoT Malware and the Abuse of Home Devices
nttcom
0
5.9k
【CEDEC2026】『ウマ娘 プリティーダービー』 英語版のキャラクターの方言や口調をローカライズするための創造的アプローチ
cygames
PRO
2
1k
Genie Codeハンズオン応用編
taka_aki
0
110
Agent 時代の Kaggle 展望 / kaggle-in-the-agentic-era
upura
1
770
Featured
See All Featured
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
370
Utilizing Notion as your number one productivity tool
mfonobong
4
550
Building Adaptive Systems
keathley
44
3.2k
Ruling the World: When Life Gets Gamed
codingconduct
0
300
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
240
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
370
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Building the Perfect Custom Keyboard
takai
2
840
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.8k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
Thoughts on Productivity
jonyablonski
76
5.3k
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