Querying CockroachDB

CockroachDB speaks the PostgreSQL wire protocol, so Querydsl SQL talks to it through the regular PostgreSQL JDBC driver. CockroachDBTemplates extends PostgreSQLTemplates and adjusts the SQL where CockroachDB differs.

Maven Integration

<dependency>
  <groupId>io.github.openfeign.querydsl</groupId>
  <artifactId>querydsl-sql</artifactId>
  <version>7.1</version>
</dependency>

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.7.15</version>
</dependency>

Connect with a regular PostgreSQL URL on the CockroachDB port, for example jdbc:postgresql://localhost:26257/defaultdb?sslmode=disable.

Templates

SQLTemplates templates = CockroachDBTemplates.builder().build();
Configuration configuration = new Configuration(templates);

SQLTemplatesRegistry detects CockroachDB automatically. The driver reports PostgreSQL, so the registry spends one extra round-trip on select version() and picks CockroachDBTemplates when the result mentions CockroachDB. It falls back to PostgreSQLTemplates when the probe fails or the connection is not in auto-commit mode, because the probe never runs inside your transaction. On a non-autocommit connection, pass CockroachDBTemplates explicitly.

Code Generation

Code generation works like it does for PostgreSQL, with one limitation for now: querydsl:export needs pgjdbc 42.7.14 or older. Starting with 42.7.15, the driver filters its metadata queries with OPERATOR(pg_catalog.~~), which CockroachDB cannot parse. Set the schema pattern to public (or your schema) so that the crdb_internal, pg_catalog and information_schema schemas are not exported:

<configuration>
  <jdbcDriver>org.postgresql.Driver</jdbcDriver>
  <jdbcUrl>jdbc:postgresql://localhost:26257/defaultdb?sslmode=disable</jdbcUrl>
  <jdbcUser>root</jdbcUser>
  <schemaPattern>public</schemaPattern>
  <packageName>com.myproject.domain</packageName>
  <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
</configuration>
<dependencies>
  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.7.14</version>
  </dependency>
</dependencies>

For a table without a primary key, CockroachDB adds a hidden rowid column, and the export currently includes it.

Queries

CockroachDBQueryFactory creates CockroachDBQuery instances. They support everything PostgreSQLQuery does (distinctOn, forShare, noWait, of) and add AS OF SYSTEM TIME for historical and follower reads:

CockroachDBQueryFactory queryFactory = new CockroachDBQueryFactory(configuration, dataSource::getConnection);

List<String> names = queryFactory
    .select(customer.name)
    .from(customer)
    .asOfSystemTime("-10s")
    .fetch();

List<String> atInstant = queryFactory
    .select(customer.name)
    .from(customer)
    .asOfSystemTime(Instant.parse("2026-01-01T00:00:00Z"))
    .fetch();

List<String> fromFollowers = queryFactory
    .select(customer.name)
    .from(customer)
    .asOfFollowerReadTimestamp()
    .fetch();

CockroachDB does not accept bind parameters in AS OF SYSTEM TIME, so the value is written into the SQL as a quoted literal.

Insert, Update, Delete and Upsert

insert, update, delete and their batch variants work as usual. Keys generated by unique_rowid() can be read back with executeWithKey / executeWithKeys.

CockroachDB has no SQL MERGE statement. merge(...) therefore falls back to the same select-then-update-or-insert strategy as PostgreSQL. For an atomic write keyed on the primary key, use upsert:

queryFactory.upsert(customer)
    .set(customer.id, 1L)
    .set(customer.name, "Bob")
    .execute();

Differences from PostgreSQL

  • INT means INT8; declare INT4 columns, or set the default_int_size session variable to 4, if your query types use Integer.
  • / on two integers returns a decimal; use // in templates when you need integer division.
  • SERIAL and unique_rowid() produce unique but not consecutive values.
  • CockroachDB does not mix numeric types implicitly, so float8 * decimal fails. Keep the Java type of a path in line with its column type.
  • There is no XML type.
  • Transactions run at SERIALIZABLE isolation and can fail with SQLSTATE 40001. Retry them.