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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
70
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
140
LessCSS
myles
0
52
jrnl
myles
0
51
Apache CouchDB
myles
0
45
Other Decks in Technology
See All in Technology
CSに"SLO"は要らない、経営層に"99.9%"は伝わらない - SREを全社に"翻訳"する3原則
cscengineer
PRO
0
3k
Road to SRE NEXTの今までとこれから
hiroyaonoe
0
180
Oracle Exadata Database Service on Cloud@Customer X11M (ExaDB-C@C) サービス概要
oracle4engineer
PRO
2
8.3k
デジタル・デザイン構想 by Sayaka Ishizuka
y150saya
0
200
人を動かすのは時間ではなく、納得感 〜新任EMが入社3ヶ月、組織を2回変えた話〜
kakehashi
PRO
2
130
初めてのDatabricks勉強会
taka_aki
2
240
『AIに負けない』より『AIと遊ぶ』」〜ワクワクが最強のテスト・QA学習戦略_公開用
odan611
1
480
認証認可だけじゃない! ID管理の構成要素と ライフサイクルを意識しよう
ritou
1
520
インフラ寄りSREでも 開発に踏み出せる〜境界を越えてユーザー体験に向き合いたい〜
sansantech
PRO
0
2k
記録をかんたんに、提案をパーソナルに ── AIであすけんが目指すもの
oprstchn
0
170
SRE歴2ヶ月でも開発6年の知見を活かして、チームで止まっていた環境改善を前に進めた話
a_ono
1
210
ZOZOTOWNの進化と信頼性を両立する負荷試験
zozotech
PRO
0
140
Featured
See All Featured
Thoughts on Productivity
jonyablonski
76
5.2k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.2k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
230
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.8k
Odyssey Design
rkendrick25
PRO
2
730
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
600
Become a Pro
speakerdeck
PRO
31
6k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
320
Designing Experiences People Love
moore
143
24k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
55k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
410
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
180
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