CockroachDB Internals ] CockroachDB is PostgreSQL compliant, how and why

Arpit Bhayani

Arpit Bhayani

Apr 11, 2023 • 2 min read


[CockroachDB Internals #2] CockroachDB is PostgreSQL compliant, how and why? ⚡

For any client to talk to a database, they need to speak the common language and this is called the “wire protocol”. Most databases have their own implementations.

MySQL has its own, PostgreSQL has its own, and even Redis has its own protocol spec called RESP. So long as the client can send the message in the same format, the database will be able to understand and process it.

⚡ How

CockroachDB chose to not implement its own format, instead, it reuses PostgreSQL’s wire protocol and that is how it became PostgreSQL compliant.

In order to understand how this is done, you can

  1. go through the libpq library on GitHub
  2. go through the protocol spec on PostgreSQL documentation

If you are planning to re-implement, I would recommend finding an existing project and reusing the modules.

⚡ Benefits

Given that CockroachDB is a “distributed SQL” database, it has to support SQL that the database parses and understands. But by piggybacking on PostgreSQL’s ecosystem they get the following benefits

No changes to code

Being PostgreSQL compliant allows applications and tools that are built for PostgreSQL to work seamlessly with CockroachDB, without requiring any modifications or changes to the code.

Reuse existing tools

We can use existing tools like DBeaver, Grafana, etc to connect to the cluster and do the needed. This also means that the clients can continue using the same drivers and ORMs to talk to the database.

This massively increases the chances of adoption, and you could see this trend in every single upcoming database. They are compliant with some or the other existing famous databases.

This is the true power of abstraction 🔥

Note, the protocol is the same, but the magic happens after the query is sent and parsed. This is precisely where CockroachDB does its magic and executes the SQL query over its distributed KV store with all the interesting guarantees.

… something we will discuss and understand in the future 🙃

Recently, I started diving deep into CockroachDB internals and will share my learning in public. So, if you find it amusing, follow along.

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses