Skip to content

sql: legacy ADD/DROP COLUMN unnecessarily version bumps of all UDTs in table #144293

Closed
@spilchen

Description

@spilchen

In the legacy schema changer, most ALTER operations unconditionally create a TYPEDESC SCHEMA CHANGE job for all user-defined types (UDTs) in the table, in order to maintain back references. This has the side effect of bumping the version of each UDT—even when nothing about the type has actually changed.

While this behavior isn't inherently problematic, it becomes an issue in multi-region databases where every table includes a crdb_region column backed by the crdb_internal_region enum. As a result, any ALTER on such tables will repeatedly bump the version of that enum.

This frequent versioning has exposed a caching-related bug in the query engine (see #132105). While this change doesn’t fix the underlying bug, it may help reduce how often it's triggered.

To Reproduce

  1. Start the demo:
$ cockroach demo --nodes=6 --demo-locality=region=us-east1,zone=us-east1-a:region=us-east1,zone=us-east1-b:region=us-central1,zone=us-central1-a:region=us-central1,zone=us-central1-b:region=us-west1,zone=us-west1-a:region=us-west1,zone=us-west1-b --no-example-database
  1. Execute the following the cockroach sql shell:
> CREATE DATABASE bank PRIMARY REGION "us-east1" REGIONS "us-east1", "us-central1", "us-west1" SURVIVE REGION FAILURE;
CREATE DATABASE
> use bank;
SET
> select crdb_internal.pb_to_json('descriptor', descriptor) -> 'type' -> 'version' from system.descriptor where id = 'crdb_internal_region'::REGTYPE::INT - 100000;
  ?column?
------------
  "1"
(1 row)
> create table t1 (c1 int) locality regional by row;
CREATE TABLE
> select crdb_internal.pb_to_json('descriptor', descriptor) -> 'type' -> 'version' from system.descriptor where id = 'crdb_internal_region'::REGTYPE::INT - 100000;
  ?column?
------------
  "2"
(1 row)
-- add column via declarative schema changer
> alter table t1 add column c2 int;
ALTER TABLE
> select crdb_internal.pb_to_json('descriptor', descriptor) -> 'type' -> 'version' from system.descriptor where id = 'crdb_internal_region'::REGTYPE::INT - 100000;
  ?column?
------------
  "2"
(1 row)
-- add column via legacy schema changer
> begin;
BEGIN
> set local autocommit_before_ddl = false;
SET
> alter table t1 add column c3 int;
ALTER TABLE
> commit;
COMMIT
> select crdb_internal.pb_to_json('descriptor', descriptor) -> 'type' -> 'version' from system.descriptor where id = 'crdb_internal_region'::REGTYPE::INT - 100000;
  ?column?
------------
  "3"
(1 row)

Jira issue: CRDB-49320

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions