cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: Cannot add and then drop a column within the same transaction

Open JackHerring opened this issue 9 years ago • 9 comments

You cannot add a column and then drop it within the scope of the same transaction in cockroachdb sql. When I try this I get an error on the drop statement that the column I am trying to drop is in the middle of being added.

Obviously this is ostensibly an odd thing to need to do but the use case is actually pretty common - we have a database migrations project which loops through sql scripts running those which haven't yet been applied to the database. If you run the migrator somewhere where the add statement hasn't been applied yet ( build server/fresh deployment ) then the two statements will come together in a single transaction and the migration will fail.

JackHerring avatar Oct 12 '17 10:10 JackHerring

Really this ought to happen with any schema modifications, not just dropping columns.

JackHerring avatar Oct 16 '17 09:10 JackHerring

This should be easy to fix. It will involve the drop code looking at the cached schema for the transaction and dropping the schema element if it has been added by the transaction.

vivekmenezes avatar Oct 16 '17 14:10 vivekmenezes

@vivekmenezes I am thinking to have a look at this issue this evening, unless you are already working on it?

jrote1 avatar Oct 31 '17 13:10 jrote1

@jrote1 go for it! I'd first add a test to one of the logictests pkg/sql/logictest/testdata/logic_test/alter_table

and fix the problem by reading the table descriptor from the store, finding the mutation id of the column ADD and ensuring there is an outstanding schema change for that mutation id in schemaChangerCollection

Since you're doing this for columns, I'd also do this for indexes.

vivekmenezes avatar Oct 31 '17 18:10 vivekmenezes

@vivekmenezes Yeah I want to do it for Tables, Columns and Indexes, both adding then removing and removing then adding. And also some add some tests for adding and renaming, different combinations like that. Will probably begin work tomorrow was getting dev environment configured today.

jrote1 avatar Oct 31 '17 22:10 jrote1

Adding and removing in the same transaction is much easier than removing and then adding, because when you're removing a schema element you have to deal with the fact that the schema element could be cached somewhere. I'd stick with adding and then removing in the same transaction as a first PR. Thanks so much for your willingness to take this on!

vivekmenezes avatar Nov 01 '17 13:11 vivekmenezes

@vivekmenezes Am progressing, got cockroachdb building and tests running. Have created a failing test now to fix :)

jrote1 avatar Nov 01 '17 23:11 jrote1

Excellent!

On Wed, Nov 1, 2017 at 7:43 PM Jake Rote [email protected] wrote:

@vivekmenezes https://github.com/vivekmenezes Am progressing, got cockroachdb building and tests running. Have created a failing test now to fix :)

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/cockroachdb/cockroach/issues/19210#issuecomment-341275877, or mute the thread https://github.com/notifications/unsubscribe-auth/ALOpBMj7tkXmWCU-7dbL9Bo7W7xTHwbIks5syQIFgaJpZM4P2w7h .

vivekmenezes avatar Nov 02 '17 01:11 vivekmenezes

same as #24626

vivekmenezes avatar May 24 '18 19:05 vivekmenezes