PostgreSQL’s jsonb type is surprisingly more flexible than CockroachDB’s jsonb type, particularly when it comes to indexing deeply nested fields.

Let’s see how CockroachDB handles jsonb data. Imagine we have a table users with a profile column of type jsonb:

CREATE TABLE users (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    profile JSONB
);

INSERT INTO users (profile) VALUES
('{"name": "Alice", "address": {"street": "123 Main St", "city": "Anytown"}}'),
('{"name": "Bob", "address": {"street": "456 Oak Ave", "city": "Otherville"}}');

Now, let’s query for users in "Anytown":

SELECT id, profile
FROM users
WHERE profile -> 'address' ->> 'city' = 'Anytown';

This query works as expected, returning Alice’s record. CockroachDB’s JSON operators (-> for JSON object field access, ->> for JSON object field access returning text) allow you to traverse the JSON structure.

CockroachDB aims for high compatibility with PostgreSQL, especially for core SQL features and data types. For jsonb, this means many common operations and functions behave identically. Both support standard JSON operators like ->, ->>, #>, and #>>. Functions like jsonb_array_elements, jsonb_object_keys, and jsonb_typeof are also available in both.

However, the devil is in the details, particularly with indexing and performance for complex JSON structures.

CockroachDB’s JSONB Indexing:

CockroachDB allows creating secondary indexes on JSONB columns, but the syntax and capabilities differ from PostgreSQL’s GIN indexes.

In CockroachDB, you can create an index on a specific JSONB path:

CREATE INDEX idx_users_city ON users USING HASH ((profile -> 'address' ->> 'city'));

This HASH index is efficient for exact matches on the extracted text value. For range queries or more complex pattern matching within JSONB, you might consider a GIN index in PostgreSQL. CockroachDB’s equivalent for full-text search or more complex indexing on JSONB is less direct and often involves creating separate indexed columns or using specific functions within the index definition, which can lead to different performance characteristics.

Migration Considerations:

When migrating from PostgreSQL to CockroachDB, most of your existing jsonb queries will likely work without modification. The primary areas to scrutinize are:

  1. Performance of Complex JSON Queries: If you heavily rely on PostgreSQL’s GIN indexes for efficient searching within deeply nested or array-based JSONB data, you’ll need to re-evaluate your indexing strategy in CockroachDB. CockroachDB’s HASH indexes on extracted values are excellent for equality checks, but for broader search capabilities on JSONB, you might need to flatten parts of your JSON into separate indexed columns or explore CockroachDB’s full-text search capabilities if applicable.

  2. PostgreSQL-Specific JSON Functions: While CockroachDB supports many common JSON functions, there might be obscure or advanced PostgreSQL-specific JSON functions that do not have a direct equivalent. Always test your critical queries.

  3. Data Modeling: Consider if your current JSONB-heavy data model is truly optimal for distributed systems. Sometimes, denormalizing or restructuring JSON data into more traditional relational columns can yield better performance and scalability in a distributed database like CockroachDB.

The most surprising difference is often not in the syntax of the operators, but in how you achieve performant querying on complex JSON structures. PostgreSQL’s GIN indexes offer a powerful, built-in mechanism for indexing arbitrary paths and values within jsonb, allowing for very flexible and performant queries on nested data. CockroachDB’s approach, while compatible for basic operations, often requires a more explicit strategy for indexing JSON content, typically by extracting specific fields into indexed columns or leveraging HASH indexes on those extracted values. This means that a query that flies in PostgreSQL with a GIN index might require manual optimization in CockroachDB to achieve similar performance, often by pre-processing or restructuring the data.

The next hurdle you’ll likely encounter is understanding CockroachDB’s distributed query execution and how it impacts jsonb performance across multiple nodes.

Want structured learning?

Take the full Cockroachdb course →