Skip to content

RELATIONAL-JDBC: Add support for cockroach DB - #3352

Merged
singhpk234 merged 16 commits into
apache:mainfrom
singhpk234:feature/cockroach
Mar 9, 2026
Merged

singhpk234 merged 16 commits into
apache:mainfrom
singhpk234:feature/cockroach

Conversation

@singhpk234

@singhpk234 singhpk234 commented Jan 5, 2026 •

Copy link
Copy Markdown
Contributor

About the change

Add support of cockroach DB in relational-jdbc persistence

There was pr long ago :

seems like the author got busy, all pending was to testing it E2E with cockroach with all the integ tests along with making it work with schema evolution logic. also marking them as co-author for their contribution.
Happy to close it if they wanna resume their original pr

co-author : @sathesuraj

Checklist

  • 🛡️ Don't disclose security issues! (contact security@apache.org)
  • 🔗 Clearly explained why the changes are needed, or linked related issues: Fixes #
  • 🧪 Added/updated tests with good coverage, or manually tested (and explained how)
  • 💡 Added comments for complex logic
  • 🧾 Updated CHANGELOG.md (if needed) -- will add later
  • 📚 Updated documentation in site/content/in-dev/unreleased (if needed) - will add later

@jbonofre
jbonofre self-requested a review January 5, 2026 14:06
@dimas-b

dimas-b commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

co-author : @sathesuraj

Please use the standard Co-Authored-By tag in commits :)

https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors

@dimas-b dimas-b left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to supporting CockroachDB and thanks for picking this work up, @singhpk234 !

Some comments below.

@github-actions

github-actions Bot commented Feb 9, 2026

Copy link
Copy Markdown

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions Bot added the stale label Feb 9, 2026
@singhpk234 singhpk234 removed the stale label Feb 9, 2026
@singhpk234 singhpk234 added this to the 1.5.0 milestone Feb 26, 2026
@flyrain

flyrain commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

@singhpk234, Thanks for adding more tests. There is a conflict and CI failures. Would you mind rebasing it?

@singhpk234

Copy link
Copy Markdown
Contributor Author

This is based on #3360 would be really to great this in first, if you all have time i added more tests there as well

sfc-gh-prsingh and others added 7 commits March 8, 2026 14:10
CockroachDB uses PostgreSQL wire protocol and is indistinguishable
from PostgreSQL at the JDBC driver level. Compatibility is ensured
through INT4 schema declarations and comprehensive test coverage.

All PostgreSQL tests run against both PostgreSQL and CockroachDB.
- Created 5 new integration test classes that mirror PostgreSQL tests
- CockroachApplicationIT, CockroachManagementServiceIT, CockroachPolicyServiceIT,
  CockroachRestCatalogIT, CockroachViewFileIT
- All tests extend the same base test classes as PostgreSQL tests
- Use CockroachRelationalJdbcProfile for CockroachDB container lifecycle
- Tests run with same logic as PostgreSQL to ensure compatibility
- Enabled parallel test execution (maxParallelForks = 2)
- PostgreSQL and CockroachDB tests can now run concurrently

This ensures comprehensive test coverage for CockroachDB backend.
CockroachDB Support:
- Added COCKROACHDB to DatabaseType enum (maps to "cockroachdb" directory)
- Created separate schema directory: cockroachdb/schema-v1.sql
- CockroachDB schema v1 based on PostgreSQL schema v3 (includes all tables)
- Uses INT4 explicitly for integer columns (required for CockroachDB JDBC driver)

Database Type Detection and Validation:
- Implemented DatabaseType.inferFromConnection() to detect database from JDBC metadata
- If database-type is configured: uses configured type and validates against connection
- If configured type mismatches connection: throws IllegalStateException with clear error
- If no configured type: auto-detects from connection (CockroachDB, PostgreSQL, H2)
- Validation ensures configuration matches actual database

Configuration:
- New property: polaris.persistence.relational.jdbc.database-type
- Supported values: "postgresql", "cockroachdb", "h2"
- If set, configuration is authoritative and validated against connection
- If not set, auto-detects from JDBC connection metadata

Bootstrap Support:
- Added DatabaseType.getLatestSchemaVersion() method
- PostgreSQL: latest version is 3
- CockroachDB: latest version is 1
- Updated JdbcBootstrapUtils to use database-specific latest version
- Bootstrap automatically selects correct schema for each database type

Test Configuration:
- CockroachDB test profiles explicitly set database-type=cockroachdb
- Ensures proper identification even with PostgreSQL JDBC driver
- Added 5 integration test classes for server module
- Enabled parallel test execution (PostgreSQL and CockroachDB tests run concurrently)

PostgreSQL Schema:
- Kept original INTEGER/INT types (no changes to existing schemas)
- Removed trailing newlines from schema files
singhpk234 and others added 8 commits March 8, 2026 14:10
CockroachDB schema was versioned as v1 but its contents matched
PostgreSQL v3 (including location_without_scheme column). This caused
ModelEntity.getAllColumnNames(1) to return the wrong column list,
silently skipping the location_without_scheme column.

Rename schema-v1.sql to schema-v3.sql and update getLatestSchemaVersion()
to return 3 for CockroachDB, keeping versions in sync with PostgreSQL.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add missing databaseType() to RelationalJdbcConfiguration implementations
  in MetricsReportPersistenceTest and RelationalJdbcIdempotencyStorePostgresIT
- Override testBootstrapFailsWhenAddingRealmWithDifferentSchemaVersion in
  CockroachJdbcBootstrapCommandTest to use v3 (CockroachDB only has schema-v3)
- Create cockroachdb/schema-v4.sql matching PostgreSQL v4 with
  idempotency_records, scan_metrics_report, and commit_metrics_report
  tables (using INT4 for CockroachDB JDBC compatibility)
- Remove cockroachdb/schema-v3.sql since CockroachDB is new and
  doesn't need older schema versions
- Update DatabaseType.getLatestSchemaVersion() to return 4 for all types
- Add missing database-type property to config docs
- Update CockroachJdbcBootstrapCommandTest to bootstrap with v4
- Use proper imports for java.sql.Connection and java.sql.SQLException
  instead of inline fully-qualified names (review nit from dimas-b)
- Update JdbcBootstrapUtilsTest expected versions from 3 to 4
- Add v4 test cases for parameterized schema version tests
Replace hardcoded Docker image in CockroachRelationalJdbcLifeCycleManagement
with ContainerSpecHelper pattern, matching PostgreSQL's approach. This enables
Renovate to manage the CockroachDB image version automatically.
@singhpk234
singhpk234 requested review from dimas-b and flyrain March 8, 2026 23:39

@dimas-b dimas-b left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably mention this in the CHANGELOG, WYDT?

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Mar 9, 2026

@flyrain flyrain left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @singhpk234 !

@singhpk234
singhpk234 merged commit 8f9ff62 into apache:main Mar 9, 2026
17 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in Basic Kanban Board Mar 9, 2026
@singhpk234

singhpk234 commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor Author

We should probably mention this in the CHANGELOG, WYDT?

Totally agree, creating a pr for it right away !

Thank you so much for review @dimas-b @flyrain

marked @sathesuraj as the primary author : 8f9ff62
thank you so much for your contribution

@dimas-b dimas-b mentioned this pull request Mar 16, 2026
3 of 6 tasks
@MonkeyCanCode MonkeyCanCode mentioned this pull request May 25, 2026
6 tasks
shyundev pushed a commit to shyundev/polaris that referenced this pull request Aug 5, 2026
…hDB schemas

The CockroachDB schema resources were forked from the PostgreSQL v4 schema in
apache#3352. One week later, apache#3939 added three performance indexes to fix the
realm-wide `grant_records` scan reported in apache#3685, but only to the H2 and
PostgreSQL v4 schemas. apache#5086 then derived the v5 schemas from their v4
ancestors, carrying the omission forward. CockroachDB has therefore never
declared them:

    idx_grants_realm_grantee   ON grant_records (realm_id, grantee_id)
    idx_grants_realm_securable ON grant_records (realm_id, securable_id)
    idx_entities_catalog_id_id ON entities (catalog_id, id)

This matters most for `idx_grants_realm_grantee`. The `grant_records` primary
key leads with the securable columns, so `loadAllGrantRecordsOnGrantee` -
issued on every resolver cache miss for a principal, principal role, or
catalog role - has `realm_id` as its only usable key prefix and scans every
grant record in the realm. On CockroachDB the primary key is also the physical
storage and range split key, so that scan fans out across nodes.

Diffing the two dialect files shows the change restores parity with the
PostgreSQL schema the CockroachDB one was forked from: apart from the header
and footer comments and the deliberate `INTEGER`/`INT` to `INT4` substitution,
these three indexes were the only difference. `idx_locations` is byte-identical
in both.

No schema version bump: the indexes affect query planning only, every statement
is `CREATE INDEX IF NOT EXISTS`, and no column, constraint, or query changes.
This mirrors how apache#3939 itself landed - its commit message records that a
proposed new schema version was moved into the existing file during review.

`SchemaIndexParityTest` guards against this class of drift recurring. It
compares only `(index name, table name)` pairs per schema version, across the
dialects that ship that version. Column lists are deliberately not compared,
because dialects legitimately differ - `idx_locations` is a partial index on
PostgreSQL but a plain index on H2. Without the schema change the test fails on
v4 parity, v5 parity, and the CockroachDB grant-record assertion, naming the
three missing indexes.

Because Polaris has no automated schema migrations, an existing CockroachDB
database only picks these up when a new realm is bootstrapped, since
bootstrapping is the only production path that runs the schema script. The
CHANGELOG and the Relational JDBC metastore documentation carry the one-time
`CREATE INDEX` statements for databases bootstrapped by an earlier release.

Related to apache#3685
shyundev added a commit to shyundev/polaris that referenced this pull request Aug 5, 2026
The CockroachDB schema resources were forked from the PostgreSQL v4 schema in
apache#3352. apache#3939 then added three performance indexes to fix the realm-wide
`grant_records` scan reported in apache#3685, but it was opened before the CockroachDB
schema landed on main, so it only covered the H2 and PostgreSQL v4 schemas. The
v5 schemas were later derived from their v4 ancestors, carrying the omission
forward. CockroachDB has therefore never declared them:

    idx_grants_realm_grantee   ON grant_records (realm_id, grantee_id)
    idx_grants_realm_securable ON grant_records (realm_id, securable_id)
    idx_entities_catalog_id_id ON entities (catalog_id, id)

`idx_grants_realm_grantee` is the one that fixes an access path. After
`realm_id`, the `grant_records` primary key continues with the securable
columns, so `loadAllGrantRecordsOnGrantee` - issued on every resolver cache miss
for a principal, principal role, or catalog role - has `realm_id` as its only
usable key prefix and scans every grant record in the realm. On CockroachDB the
primary key is also the physical storage and range split key, so that scan fans
out across nodes. The other two are included so the dialects agree, not because
a query needs them: `loadAllGrantRecordsOnSecurable` constrains a full primary
key prefix, and `idx_entities` already leads with `realm_id` for the only query
shaped like `idx_entities_catalog_id_id`.

Diffing the two dialect files shows the change restores parity with the
PostgreSQL schema the CockroachDB one was forked from: apart from comments,
whitespace and the deliberate `INTEGER`/`INT` to `INT4` substitution, these
three indexes were the only difference.

No schema version bump. Schema v4 and v5 already declare these indexes for
PostgreSQL and H2, so the CockroachDB files are brought in line with the version
they already claim rather than a new version being introduced; a v6 would
instead say the indexes are new, and leave CockroachDB v5 permanently short of
its own contract. The trade-off is that v4 and v5 have both shipped, so a
CockroachDB database bootstrapped by an earlier release reports the same version
without these indexes. Because Polaris has no automated schema migrations, such
a database only picks them up when a new realm is bootstrapped, since
bootstrapping is the only production path that runs the schema script. The
CHANGELOG and the Relational JDBC metastore documentation carry the one-time
`CREATE INDEX` statements for that case.

`SchemaIndexParityTest` guards against this class of drift recurring. It
compares only `(index name, table name)` pairs per schema version, across the
dialects that ship that version and declare standalone indexes. Column lists are
deliberately not compared, so it proves the dialects agree on which indexes
exist by name, not that they cover the same columns - today they do not, since
`idx_locations` is declared on different columns on H2 than on PostgreSQL and
CockroachDB.

Related to apache#3685
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants