Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: alter column type in transaction not supported #49351

Open
Tracked by #49329
RichardJCai opened this issue May 21, 2020 · 17 comments
Open
Tracked by #49329

sql: alter column type in transaction not supported #49351

RichardJCai opened this issue May 21, 2020 · 17 comments
Labels
A-schema-changes C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-anchored-telemetry The issue number is anchored by telemetry references.

Comments

@RichardJCai
Copy link
Contributor

RichardJCai commented May 21, 2020

With the current schema changer infrastructure, it's difficult to support alter column type in transactions. This is because the computed column swap doesn't actually occur until the transaction is committed. Thus any references to the column being altered will be done on the original column and not the column after the swap.

Here is an example with adding a constraint to an altered column

root@127.0.0.1:61263/movr> ALTER TABLE t ADD CONSTRAINT x_unique UNIQUE (x);
root@127.0.0.1:61263/movr> BEGIN;
ALTER TABLE t ALTER COLUMN x TYPE STRING;
ALTER TABLE t ADD COLUMN y INT;
ALTER TABLE t ADD COLUMN z INT;
ALTER TABLE t ADD CONSTRAINT x_unique UNIQUE (x);
BEGIN

Time: 101µs

NOTICE: ALTER COLUMN TYPE changes are finalized asynchronously; further schema changes on this table may be restricted until the job completes; some writes to the altered column may be rejected until the schema change is finalized
ALTER TABLE

Time: 5.336ms

ALTER TABLE

Time: 9.576ms

ALTER TABLE

Time: 4.358ms

ALTER TABLE

Time: 3.898ms

root@127.0.0.1:61263/movr  OPEN> commit;
ERROR: transaction committed but schema change aborted with error: (XXUUU): verify-idx-count: column-id "1" does not exist
SQLSTATE: XXA00

Allowing alter column type in transactions will likely depend on supporting transactional schema changes in general.

Similarly, ALTER COLUMN TYPE cannot be used in combination with other ALTER TABLE statements since this runs the statements inside a transaction.

Example:
ALTER TABLE t ALTER COLUMN x TYPE STRING, ALTER COLUMN x SET NOT NULL;

Jira issue: CRDB-4235
Epic CRDB-25314

@RichardJCai RichardJCai added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-schema-changes labels May 21, 2020
@RichardJCai RichardJCai changed the title sql: alter column type in explicit transaction not supported sql: alter column type in transaction not supported May 26, 2020
@rafiss rafiss added the X-anchored-telemetry The issue number is anchored by telemetry references. label Oct 21, 2020
@Anticom
Copy link

Anticom commented Dec 29, 2020

Any updates on this?

@vy-ton
Copy link
Contributor

vy-ton commented Jan 19, 2021

@Anticom This won't be fixed in the upcoming 21.1 release but our team is working on the underlying changes needed to support hopefully later in 2021.

@jlinder jlinder added the T-sql-schema-deprecated Use T-sql-foundations instead label Jun 16, 2021
@nickelnickel
Copy link
Contributor

Checking in on this one, it's come up again in an attempted migration from 20.1 to 21.1

@gnat
Copy link

gnat commented Sep 1, 2022

ALTER transactions are used by the Piccolo ORM automatic migration tool.

Would be a great lightweight tool for free, open source migrations that aren't SQLAlchemy. 😉

@cjoelrun
Copy link

Also creeps up with Django migrations with AlterField.

from django.db import migrations, models

class Migration(migrations.Migration):
        migrations.CreateModel(
            name='model1',
            fields=[
                ('field1', models.DateField()),
            ]

This causes psycopg2.errors.FeatureNotSupported: unimplemented: ALTER COLUMN TYPE cannot be used in combination with other ALTER TABLE commands

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0001_auto_20220903_1024'),
    ]

    operations = [
        migrations.AlterField(
            model_name='model1',
            name='field1',
            field=models.DateTimeField(blank=True, null=True),
        ),
    ]

@ewassef
Copy link

ewassef commented Apr 30, 2023

Where are we on this? running v22.2 and still getting this error. is there a cluster setting we can turn on?

@exalate-issue-sync exalate-issue-sync bot added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) and removed T-sql-schema-deprecated Use T-sql-foundations instead labels May 10, 2023
@janpio
Copy link

janpio commented Aug 4, 2023

Same problem for Prisma: prisma/prisma#20557

Is there a workaround we could implement to somehow get these migrations done anyway?
Do we have to skip the transaction completely for this type of column change?

@gnat
Copy link

gnat commented Aug 4, 2023

The short term fix is don't do the migration in a transaction.

For better ORM compatibility, I think there's an argument to be made for CRDB to just ignore the transaction part for schema changes- as I don't see a real fix any time soon, but it's a pressing issue for a lot of ORM's.

@Lord-Y
Copy link

Lord-Y commented Aug 9, 2023

Hello guys, were are we on this issue? ALTER COLUMN TYPE is something pretty common. Can we have a priorisation on this issue?

@HaraldTR
Copy link

+1 for prioritization on this

@mari-arondeus
Copy link

I know my org is still waiting for this before we dump our old postgres ha cluster. Aaaaany day now ;)

@ValentinKardzhaliev
Copy link

Is there any progress on this issue?

@Kolawole99
Copy link

+1 for prioritizing this issue.

Would need this to migrate from Postgres to Cockroach.

@SwastikGorai
Copy link

Is this issue resolved yet??

@arantesxyz
Copy link

+1

@brandan-schmitz
Copy link

Is there any hope of getting this done? It has been several years now after it has been said it was being worked on and was supposed to be done later in 2021. There are many applications out there that use this in their migrations, and when CockroachDB claims to be PostgreSQL compatible but then does not work for things like this, it makes it really hard to use for things!

@adityadeshmukh369
Copy link

any updates on the issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-schema-changes C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-anchored-telemetry The issue number is anchored by telemetry references.
Projects
None yet
Development

No branches or pull requests