cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: support ALTER TABLE ... ALTER COLUMN ... SET DATA TYPE ...

Open knz opened this issue 10 years ago • 23 comments

Requested by @jayesh_nambiar_twitter on gitter, suggested by @dt:

you can add the new col, use an UPDATE tbl SET newcol = f(oldcol) to move the data over, then drop the old col (and then rename newcol to oldcol) f there could be as simple as a ::newtype cast, or some other builtin function that computes the new values from the old ones

Edit by Richard (March, 19, 2020) Proposal for alter column type: (Rohan's suggestion)

Suppose a user wants to alter column c from type t to t'

The overall procedure will be similar to primary key changes. In the first txn, we add:

  • A new computed column c' computed using c::t'
  • For all indexes i that contain c, create a new i' that indexes all columns in i, but replaces c for c'
  • Enqueue an "index swap" mutation containing all indexes that need to be rewritten.

When processing the index swap mutation, we'll need to

  • change c' to no longer be a computed column, and to be renamed to c.
  • Add a column drop mutation for the old column c

It seems like we'll need to handle this case of child jobs / async job cleanup better since there are now multiple things using it.

An annoying case seems to be if the column is indexed in the primary key. In that case, we'll want to just add the new column, and then use the existing alter primary key logcic, with a column drop.

Another thing that will need to be done is make sure everything that references the column is updated, like foreign keys

Summary:

  1. Create a new computed column c' using c::t'
  2. For all indexes i that contain c, create a new i' that indexes all columns in c, but replaces c for c'
  3. Enqueue an "index swap" mutation containing all indexes that need to be rewritten.

The index swap mutation:

  1. Change c' to no longer be a computed column, rename to c
  2. Add a column drop mutation for the old column c

After Swap: Change foreign key references

Questions: Can sequence be altered to another sequence?

  • Can altered column type still be a sequence?

Jira issue: CRDB-6147

knz avatar Oct 10 '16 18:10 knz

See also #5950, which asks for a simpler subset of type changes (changing the length limits of string types, needed by the django ORM).

bdarnell avatar Oct 11 '16 07:10 bdarnell

@knz Can I have a try?

yznming avatar Nov 24 '16 15:11 yznming

Hi @yznming yes you can try this, but this is not my area of expertise so I am not sure I can help you.

knz avatar Nov 24 '16 20:11 knz

@knz you have gave me lots of help, thank you so much :)

yznming avatar Nov 25 '16 03:11 yznming

Based on an informal survey of popular open source Ruby on Rails apps, it seems like this is a very common operation - developers often add migrations that change column types as their apps evolved.

We should make an effort to support this in the not-too-distant future.

jordanlewis avatar Jun 21 '17 14:06 jordanlewis

Yeah, this is a big deal in almost every framework I've ever used... I'm currently trying to implement a workaround for the Buffalo framework for go, but have run into a weird snag if anyone could help that would be at least one platform that could support CockroachDB out of the box with all migrations supported.

mclark4386 avatar Jan 01 '18 20:01 mclark4386

cc @awoods187 this feature is coming up more and more for compatibility with other tools, perhaps it is time to re-prioritize.

knz avatar Feb 02 '18 12:02 knz

Drafting an RFC here https://github.com/cockroachdb/cockroach/pull/24703

bobvawter avatar Apr 11 '18 19:04 bobvawter

Zendesk ticket #2497 has been linked to this issue.

tim-o avatar Jul 11 '18 21:07 tim-o

top requested feature based on telemetry data

awoods187 avatar Oct 30 '18 14:10 awoods187

Comment made from Zendesk by Ricardo Rocha on 2019-07-08 at 14:23:

Closing older ZenDesk forum incidents as per Support meeting on 7/2

tim-o avatar Jul 08 '19 14:07 tim-o

Why this was closed?

markelog avatar Jul 15 '19 06:07 markelog

Good question. It shouldn't have been.

jordanlewis avatar Jul 15 '19 12:07 jordanlewis

That was our error, apologies.

tim-o avatar Jul 15 '19 13:07 tim-o

Comment made from Zendesk by Ricardo Rocha on 2019-07-15 at 17:26:

Reopening since bulk update closed incorrectly.

tim-o avatar Jul 15 '19 17:07 tim-o

Cool, thanks :), excited about the possibility of this feature btw

markelog avatar Jul 16 '19 00:07 markelog

@RichardJCai In which commit was this fixed? (just curious, I can't find it)

sandstrom avatar Jun 22 '20 07:06 sandstrom

Apparently, not yet fully in 20.2.0-alpha.1:

ERROR:  internal error: unknown conversion for VARCHAR(20) -> VARCHAR(10)

Luit avatar Jun 22 '20 08:06 Luit

cc @jordanlewis for re-triage

knz avatar Jun 22 '20 09:06 knz

Apparently, not yet fully in 20.2.0-alpha.1:

ERROR:  internal error: unknown conversion for VARCHAR(20) -> VARCHAR(10)

This should be fixed in 20.2.0 alpha 2.

RichardJCai avatar Jun 22 '20 14:06 RichardJCai