SlideShare a Scribd company logo
@cockroachdb
PostgreSQL meetup, November 2015
CockroachDB
presented by Peter Mattis / Co-Founder
@cockroachdb
1.Overview of CockroachDB
2.SQL Data Model
3.Logical Data Storage
4.Online/Concurrent Schema Change
Agenda
@cockroachdb
What is CockroachDB?
■Scale out SQL
■Distributed
■Survivable
■Consistent
■Open source
@cockroachdb
CockroachDB: Architecture
■Layered abstractions
■SQL is starting point
■Distributes at map
■Replicates at physical layer
SQL
Transactional KV
Monolithic Map
Raft
@cockroachdb
CockroachDB: Architecture
■Layered abstractions
■SQL is starting point
■Distributes at map
■Replicates at physical layer
Transactional KV
Monolithic Map
Raft
GraphSQL
@cockroachdb
CockroachDB: Architecture
■Layered abstractions
■SQL is starting point
■Distributes at map
■Replicates at physical layer
Transactional KV
Monolithic Map
Raft
SQL Graph Doc
@cockroachdb
CockroachDB: Architecture
■Layered abstractions
■SQL is starting point
■Distributes at map
■Replicates at physical layer
Transactional KV
Monolithic Map
Raft
SQL
@cockroachdb
CockroachDB: Architecture
■Layered abstractions
■SQL is starting point
■Distributes at map
■Replicates at physical layer
Transactional KV
Monolithic Map
Raft
SQL
Physical
@cockroachdb
SQL Data Model
@cockroachdb
■Tables
SQL Data Model
@cockroachdb
■Tables
SQL Data Model
Inventory
@cockroachdb
■Tables
■Rows
SQL Data Model
Inventory
@cockroachdb
■Tables
■Rows
■Columns
SQL Data Model
Inventory
ID Name Price
1 Glove 1.11
2 Ball 2.22
3 Shirt 3.33
4 Shorts 4.44
5 Bat 5.55
6 Shoes 6.66
@cockroachdb
■Tables
■Rows
■Columns
■Indexes
SQL Data Model
Inventory
ID Name Price
1 Glove 1.11
2 Ball 2.22
3 Shirt 3.33
4 Shorts 4.44
5 Bat 5.55
6 Shoes 6.66
Name
Ball
Bat
Glove
Shirt
Shoes
Shorts
Name_Idx
@cockroachdb
PostgreSQL: Logical Data Storage
@cockroachdb
■Rows are stored in an unordered heap
■Indexes are btrees
■Primary key is a unique index
PostgreSQL: Data Storage
@cockroachdb
CREATE TABLE test (
id INTEGER PRIMARY KEY,
name VARCHAR,
price FLOAT,
);
PostgreSQL: Example Table
@cockroachdb
INSERT INTO test VALUES (1, “ball”, 3.33);
PostgreSQL: Logical Data Storage
@cockroachdb
INSERT INTO test VALUES (1, “ball”, 3.33);
PostgreSQL: Logical Data Storage
Tuple ID (Page# / Item#) Row
(0, 1) (1, “ball”, 3.33)
test (heap)
@cockroachdb
INSERT INTO test VALUES (1, “ball”, 3.33);
PostgreSQL: Logical Data Storage
Tuple ID (Page# / Item#) Row
(0, 1) (1, “ball”, 3.33)
Index Key Tuple ID
1 (0, 1)
test (heap)test_pkey (btree)
@cockroachdb
INSERT INTO test VALUES (1, “ball”, 3.33);
INSERT INTO test VALUES (2, “glove”, 4.44);
PostgreSQL: Logical Data Storage
Tuple ID (Page# / Item#) Row
(0, 1) (1, “ball”, 3.33)
(0, 2) (2, “glove”, 4.44)
Index Key Tuple ID
1 (0, 1)
2 (0, 2)
test (heap)test_pkey (btree)
@cockroachdb
CockroachDB: Logical Data Storage
@cockroachdb
■Keys and values are strings
■Monolithic, sorted map
CockroachDB: KV
@cockroachdb
Get(key)
Put(key, value)
ConditionalPut(key, value, expValue)
Scan(startKey, endKey)
CockroachDB: KV Primitives
@cockroachdb
Get(key)
Put(key, value)
ConditionalPut(key, value, expValue)
Scan(startKey, endKey)
Del(key)
CockroachDB: KV Primitives
@cockroachdb
■All tables have a primary key
■One key/value pair per column
CockroachDB: Row Storage
@cockroachdb
■All tables have a primary key
■One key/value pair per column
■Key anatomy:
/<table>/<index>/<pkey>/<column>
CockroachDB: Row Storage
@cockroachdb
CREATE TABLE test (
id INTEGER PRIMARY KEY,
name VARCHAR,
price FLOAT,
);
CockroachDB: Example Table
@cockroachdb
INSERT INTO test VALUES (1, “ball”, 2.22);
CockroachDB: Key Anatomy
Key: /<table>/<index>/<key>/<column> Value
/test/primary/1/name “ball”
/test/primary/1/price 2.22
@cockroachdb
INSERT INTO test VALUES (1, “ball”, 2.22);
INSERT INTO test VALUES (2, “glove”, 3.33);
CockroachDB: Key Anatomy
Key: /<table>/<index>/<key>/<column> Value
/test/primary/1/name “ball”
/test/primary/1/price 2.22
/test/primary/2/name “glove”
/test/primary/2/price 3.33
@cockroachdb
INSERT INTO test VALUES (1, “ball”, 2.22);
INSERT INTO test VALUES (2, “glove”, 3.33);
CockroachDB: Key Anatomy
Key: /<table>/<index>/<key>/<column> Value
/test/primary/1/name “ball”
.../price 2.22
.../2/name “glove”
.../price 3.33
@cockroachdb
INSERT INTO test VALUES (1, “ball”, 2.22);
INSERT INTO test VALUES (2, “glove”, 3.33);
CockroachDB: Key Anatomy
Key: /<table>/<index>/<key>/<column> Value
/1000/1/1/1 “ball”
.../2 2.22
.../2/1 “glove”
.../2 3.33
@cockroachdb
■Key encoding
■NULL column values
■Unique indexes
■Non-unique indexes
CockroachDB: The Details
@cockroachdb
■Keys and values are strings
■Columns are typed data
■???
CockroachDB: Key Encoding
@cockroachdb
■NULL indicates value does not exist
■NULL is weird: NULL != NULL
CockroachDB: NULL Column Values
@cockroachdb
■NULL indicates value does not exist
■NULL is weird: NULL != NULL
■CockroachDB: NULL values are not explicitly stored
CockroachDB: NULL Column Values
@cockroachdb
INSERT INTO test VALUES (1, “ball”, NULL);
CockroachDB: NULL Column Values
Key: /<table>/<index>/<key>/<column> Value
/test/primary/1/name “ball”
@cockroachdb
INSERT INTO test VALUES (1, “ball”, NULL);
INSERT INTO test VALUES (2, NULL, NULL);
CockroachDB: NULL Column Values
Key: /<table>/<index>/<key>/<column> Value
/test/primary/1/name “ball”
??? ???
@cockroachdb
INSERT INTO test VALUES (1, “ball”, NULL);
INSERT INTO test VALUES (2, NULL, NULL);
CockroachDB: NULL Column Values
Key: /<table>/<index>/<key>[/<column>] Value
/test/primary/1 Ø
/test/primary/1/name “ball”
/test/primary/2 Ø
@cockroachdb
CREATE UNIQUE INDEX bar ON test (name);
■Multiple table rows with equal indexed values are
not allowed
CockroachDB: Unique Indexes
@cockroachdb
CREATE UNIQUE INDEX bar ON test (name);
INSERT INTO test VALUES (1, “ball”, 2.22);
CockroachDB: Unique Indexes
Key: /<table>/<index>/<key> Value
/test/bar/”ball” 1
@cockroachdb
CREATE UNIQUE INDEX bar ON test (name);
INSERT INTO test VALUES (1, “ball”, 2.22);
INSERT INTO test VALUES (2, “glove”, 3.33);
CockroachDB: Unique Indexes
Key: /<table>/<index>/<key> Value
/test/bar/”ball” 1
/test/bar/”glove” 2
@cockroachdb
CREATE UNIQUE INDEX bar ON test (name);
INSERT INTO test VALUES (1, “ball”, 2.22);
INSERT INTO test VALUES (2, “glove”, 3.33);
INSERT INTO test VALUES (3, “glove”, 4.44);
CockroachDB: Unique Indexes
Key: /<table>/<index>/<key> Value
/test/bar/”ball” 1
/test/bar/”glove” 2
/test/bar/”glove” 3
@cockroachdb
CREATE UNIQUE INDEX bar ON test (name);
■NULL is weird: NULL != NULL
CockroachDB: Unique Indexes (NULL Values)
@cockroachdb
CREATE UNIQUE INDEX bar ON test (name);
INSERT INTO test VALUES (3, NULL, NULL);
CockroachDB: Unique Indexes (NULL Values)
Key: /<table>/<index>/<key> Value
/test/bar/NULL 3
@cockroachdb
CREATE UNIQUE INDEX bar ON test (name);
INSERT INTO test VALUES (3, NULL, NULL);
INSERT INTO test VALUES (4, NULL, NULL);
CockroachDB: Unique Indexes (NULL Values)
Key: /<table>/<index>/<key> Value
/test/bar/NULL 3
/test/bar/NULL 4
@cockroachdb
CREATE UNIQUE INDEX bar ON test (name);
INSERT INTO test VALUES (3, NULL, NULL);
CockroachDB: Unique Indexes (NULL Values)
Key: /<table>/<index>/<key>[/<pkey>] Value
/test/bar/NULL/3 Ø
@cockroachdb
CREATE UNIQUE INDEX bar ON test (name);
INSERT INTO test VALUES (3, NULL, NULL);
INSERT INTO test VALUES (4, NULL, NULL);
CockroachDB: Unique Indexes (NULL Values)
Key: /<table>/<index>/<key>[/<pkey>] Value
/test/bar/NULL/3 Ø
/test/bar/NULL/4 Ø
@cockroachdb
CREATE INDEX foo ON test (name);
■Multiple table rows with equal indexed values are
allowed
CockroachDB: Non-Unique Indexes
@cockroachdb
CREATE INDEX foo ON test (name);
■Multiple table rows with equal indexed values are
allowed
■Primary key is a unique index
CockroachDB: Non-Unique Indexes
@cockroachdb
CREATE INDEX foo ON test (name);
INSERT INTO test VALUES (1, “ball”, 2.22);
CockroachDB: Non-Unique Indexes
Key: /<table>/<index>/<key>/<pkey> Value
/test/foo/”ball”/1 Ø
@cockroachdb
CREATE INDEX foo ON test (name);
INSERT INTO test VALUES (1, “ball”, 2.22);
INSERT INTO test VALUES (2, “glove”, 3.33);
CockroachDB: Non-Unique Indexes
Key: /<table>/<index>/<key>/<pkey> Value
/test/foo/”ball”/1 Ø
/test/foo/”glove”/2 Ø
@cockroachdb
CREATE INDEX foo ON test (name);
INSERT INTO test VALUES (1, “ball”, 2.22);
INSERT INTO test VALUES (2, “glove”, 3.33);
INSERT INTO test VALUES (3, “glove”, 4.44);
CockroachDB: Non-Unique Indexes
Key: /<table>/<index>/<key>/<pkey> Value
/test/foo/”ball”/1 Ø
/test/foo/”glove”/2 Ø
/test/foo/”glove”/3 Ø
@cockroachdb
■Keys and values are strings
■NULL column values
■Unique indexes
■Non-unique indexes
CockroachDB: Logical Data Storage
@cockroachdb
Logical Data Storage
PostgreSQL CockroachDB
Keys are composite structures Keys are strings
Heap storage for rows Required primary key
Per-table heap/indexes Monolithic map
@cockroachdb
Online Schema Change
@cockroachdb
Schema Change Operations
CREATE INDEX foo ON test (col1, col2, …);
ALTER TABLE test DROP col1;
ALTER TABLE test ADD col3 INTEGER;
...
@cockroachdb
Schema Change (the easy way)
1. Lock table
2. Adjust table data (add column, populate index, etc.)
3. Unlock table
@cockroachdb
Schema Change (the easy way)
1. Apologize for down time
2. Lock table
3. Adjust table data (add column, populate index, etc.)
4. Unlock table
@cockroachdb
Schema Change (the MySQL way)
1. Create new table with altered schema
2. Capture changes from source to the new table
3. Copy rows from the source to the new table
4. Synchronize source and new table
5. Swap/rename source and new table
@cockroachdb
Schema Change (the PostgreSQL way)
1. CREATE INDEX CONCURRENTLY
@cockroachdb
CockroachDB: Schema Change
■TableDescriptor contains table schema
■TableDescriptor replicated on every node
■Distributed atomic updates are difficult
■Distributed locking is difficult
■The easy way isn’t feasible
@cockroachdb
CockroachDB: CREATE INDEX
CREATE INDEX foo ON TEST
1. Backfill index entries
2. Add index to TableDescriptor
@cockroachdb
CockroachDB: CREATE INDEX
T1 T2
CREATE INDEX foo ON test… INSERT INTO test…
@cockroachdb
CockroachDB: CREATE INDEX
CREATE INDEX foo ON TEST
1. Add index to TableDescriptor as write-only
2. Backfill index entries
3. Mark index as read-write
@cockroachdb
CockroachDB: CREATE INDEX
T1 T2
CREATE INDEX foo ON test… INSERT INTO test…
or
UPDATE test…
or
DELETE FROM test…
@cockroachdb
CockroachDB: CREATE INDEX
CREATE INDEX foo ON TEST
1. Add index to TableDescriptor as delete-only
2. Wait for descriptor propagation
3. Mark index as write-only
4. Wait for descriptor propagation
5. Backfill index entries
6. Mark index as read-write
@cockroachdb
Online Schema Change
Online schema change is difficult
The database should do the heavy lifting
@cockroachdb
The End
SQL databases are KV stores on steroids
@cockroachdb
github.com/cockroachdb/cockroach
CockroachLabs.com
@cockroachdb
Thank You

More Related Content

PPTX
CockroachDB
PDF
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
PDF
Ceph and RocksDB
PDF
BlueStore, A New Storage Backend for Ceph, One Year In
PDF
CockroachDB: Architecture of a Geo-Distributed SQL Database
PDF
Fundamentals of Apache Kafka
PPTX
Introduction to Kafka Cruise Control
PPTX
RocksDB detail
CockroachDB
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
Ceph and RocksDB
BlueStore, A New Storage Backend for Ceph, One Year In
CockroachDB: Architecture of a Geo-Distributed SQL Database
Fundamentals of Apache Kafka
Introduction to Kafka Cruise Control
RocksDB detail

What's hot (20)

PPTX
RocksDB compaction
PDF
Introduction to Cassandra
PPTX
Hive: Loading Data
PPTX
Apache Flink and what it is used for
PDF
Log Structured Merge Tree
PDF
[234]멀티테넌트 하둡 클러스터 운영 경험기
PDF
An Introduction to Apache Kafka
PDF
Producer Performance Tuning for Apache Kafka
PDF
Apache Iceberg - A Table Format for Hige Analytic Datasets
PPTX
Introduction to Storm
PPTX
Tuning kafka pipelines
PDF
Spark + Parquet In Depth: Spark Summit East Talk by Emily Curtin and Robbie S...
PDF
AF Ceph: Ceph Performance Analysis and Improvement on Flash
PDF
BlueStore: a new, faster storage backend for Ceph
PPTX
Ceph Performance and Sizing Guide
PPTX
Squirreling Away $640 Billion: How Stripe Leverages Flink for Change Data Cap...
PDF
A Thorough Comparison of Delta Lake, Iceberg and Hudi
PPTX
Exactly-Once Financial Data Processing at Scale with Flink and Pinot
PPTX
Apache Spark Architecture
PDF
Batch Processing at Scale with Flink & Iceberg
RocksDB compaction
Introduction to Cassandra
Hive: Loading Data
Apache Flink and what it is used for
Log Structured Merge Tree
[234]멀티테넌트 하둡 클러스터 운영 경험기
An Introduction to Apache Kafka
Producer Performance Tuning for Apache Kafka
Apache Iceberg - A Table Format for Hige Analytic Datasets
Introduction to Storm
Tuning kafka pipelines
Spark + Parquet In Depth: Spark Summit East Talk by Emily Curtin and Robbie S...
AF Ceph: Ceph Performance Analysis and Improvement on Flash
BlueStore: a new, faster storage backend for Ceph
Ceph Performance and Sizing Guide
Squirreling Away $640 Billion: How Stripe Leverages Flink for Change Data Cap...
A Thorough Comparison of Delta Lake, Iceberg and Hudi
Exactly-Once Financial Data Processing at Scale with Flink and Pinot
Apache Spark Architecture
Batch Processing at Scale with Flink & Iceberg
Ad

Viewers also liked (14)

PDF
RocksDB storage engine for MySQL and MongoDB
PDF
Why go ?
PPTX
Storage Engine Wars at Parse
PPTX
Business Track: How Criteo Scaled and Supported Massive Growth with MongoDB
PDF
MongoDB Wins
PPTX
Criteo Couchbase live 2015
PDF
1 Million Writes per second on 60 nodes with Cassandra and EBS
PPTX
How companies use NoSQL and Couchbase
PDF
Couchbase live 2016
KEY
Node.js - As a networking tool
PDF
MyRocks Deep Dive
PDF
Spark SQL Deep Dive @ Melbourne Spark Meetup
PDF
Cassandra and Docker Lessons Learned
PDF
Road to Analytics
RocksDB storage engine for MySQL and MongoDB
Why go ?
Storage Engine Wars at Parse
Business Track: How Criteo Scaled and Supported Massive Growth with MongoDB
MongoDB Wins
Criteo Couchbase live 2015
1 Million Writes per second on 60 nodes with Cassandra and EBS
How companies use NoSQL and Couchbase
Couchbase live 2016
Node.js - As a networking tool
MyRocks Deep Dive
Spark SQL Deep Dive @ Melbourne Spark Meetup
Cassandra and Docker Lessons Learned
Road to Analytics
Ad

Similar to PostgreSQL and CockroachDB SQL (20)

DOCX
PetToyPetOwnerPetPetFoodToyIdTag10, 10-n, .docx
PPTX
NoSQL Data Modeling 101
PDF
Postgres index types
PDF
Breadth or Depth: What's in a column-store?
PDF
Real data models of silicon valley
PDF
Cassandra Summit 2014: Real Data Models of Silicon Valley
PDF
Wide Column Store NoSQL vs SQL Data Modeling
PDF
Data Management for Quantitative Biology - Database Systems (continued) LIMS ...
PDF
Kyotoproducts
TXT
sanya's Bug database
KEY
Taming NoSQL with Spring Data
PDF
The State of NoSQL
PDF
Apache Cassandra - Data modelling
PDF
Become a super modeler
PDF
Large Scale Accumulo Clusters
PDF
Accumulo Summit 2014: Four Orders of Magnitude: Running Large Scale Accumulo ...
PDF
Rob Sullivan at Heroku's Waza 2013: Your Database -- A Story of Indifference
PDF
Cassandra Explained
PDF
Data storage systems
PDF
Cassandra Data Modeling
PetToyPetOwnerPetPetFoodToyIdTag10, 10-n, .docx
NoSQL Data Modeling 101
Postgres index types
Breadth or Depth: What's in a column-store?
Real data models of silicon valley
Cassandra Summit 2014: Real Data Models of Silicon Valley
Wide Column Store NoSQL vs SQL Data Modeling
Data Management for Quantitative Biology - Database Systems (continued) LIMS ...
Kyotoproducts
sanya's Bug database
Taming NoSQL with Spring Data
The State of NoSQL
Apache Cassandra - Data modelling
Become a super modeler
Large Scale Accumulo Clusters
Accumulo Summit 2014: Four Orders of Magnitude: Running Large Scale Accumulo ...
Rob Sullivan at Heroku's Waza 2013: Your Database -- A Story of Indifference
Cassandra Explained
Data storage systems
Cassandra Data Modeling

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Encapsulation theory and applications.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
KodekX | Application Modernization Development
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Understanding_Digital_Forensics_Presentation.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
Encapsulation theory and applications.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Dropbox Q2 2025 Financial Results & Investor Presentation
20250228 LYD VKU AI Blended-Learning.pptx
Modernizing your data center with Dell and AMD
NewMind AI Monthly Chronicles - July 2025
KodekX | Application Modernization Development
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Reach Out and Touch Someone: Haptics and Empathic Computing
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

PostgreSQL and CockroachDB SQL

Editor's Notes

  • #4: This is a PostgreSQL meetup, why should I care about CockroachDB? The CockroachDB SQL grammar is based on the Postgres grammar. Postgres is a SQL database. CockroachDB is a distributed SQL database.
  • #5: Layered abstractions make it possible to deal with complexity Higher levels can treat lower levels as functional black boxes
  • #6: Top two layers are logical Don’t stop at SQL!
  • #8: Monolithic sorted map is distributed layer
  • #9: RocksDB at physical layer
  • #16: This is a brief overview of logical data storage in PostgreSQL. I’m using the term “logical” to refer to how SQL data is mapped down into PostgreSQL structures and distinguishing it from “physical” data storage which is exactly how those structures are implemented.
  • #17: The heap structure is unindexed storage of row tuples. Think of it as a hash table where rows are given a unique id at insertion time, except that it is unfortunately not that simple. Tuples (rows) are located by a tuple ID which is composed of a page# and item# within the page. A Btree stores values sorted by key. Btree index key is a tuple of the columns in the index. Value is the row’s tuple ID.
  • #18: An example will make this clearer.
  • #22: Tuple IDs just happen to be ordered in this example. They are not in general. And tuple IDs are an internal detail of tables and not stable for external usage.
  • #38: Row sentinel!
  • #39: Row sentinel!
  • #40: Row sentinel!
  • #41: Value contains any primary key column that is not stored in index key.
  • #42: Value contains any primary key column that is not stored in index key.
  • #43: Value contains any primary key column that is not stored in index key.
  • #44: Value contains any primary key column that is not stored in index key.
  • #45: Value contains any primary key column that is not stored in index key.
  • #57: Poll audience about experience with production schema change.