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

race condition between ADD COLUMN and a subsequent UPDATE #97706

Closed
lopezator opened this issue Feb 27, 2023 · 4 comments
Closed

race condition between ADD COLUMN and a subsequent UPDATE #97706

lopezator opened this issue Feb 27, 2023 · 4 comments
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-blathers-triaged blathers was able to find an owner

Comments

@lopezator
Copy link
Contributor

lopezator commented Feb 27, 2023

Describe the problem

I v22.2.X if you execute a ADD COLUMN followed by an UPDATE over that column, the UPDATE stmt fails with column doesn't exist.

To Reproduce

22.1.15 works ✔️

Start container:

docker run --rm --name cockroach-v22.1.15 -p 22115:26257 cockroachdb/cockroach:v22.1.15 start-single-node --insecure

Execute:

cockroach sql --insecure --url="postgresql://root@localhost:22115/defaultdb?sslmode=disable" -e "CREATE TABLE foo(id CHAR(26) PRIMARY KEY);"
cockroach sql --insecure --url="postgresql://root@localhost:22115/defaultdb?sslmode=disable" -e "ALTER TABLE foo ADD COLUMN IF NOT EXISTS bar VARCHAR(200) NULL; UPDATE foo SET bar = '1';"

22.2.5 fails ❌

Start container:

docker run --rm --name cockroach-v22.2.5 -p 2225:26257 cockroachdb/cockroach:v22.2.5 start-single-node --insecure

Execute:

cockroach sql --insecure --url="postgresql://root@localhost:2225/defaultdb?sslmode=disable" -e "CREATE TABLE foo(id CHAR(26) PRIMARY KEY);"
cockroach sql --insecure --url="postgresql://root@localhost:2225/defaultdb?sslmode=disable" -e "ALTER TABLE foo ADD COLUMN IF NOT EXISTS bar VARCHAR(200) NULL; UPDATE foo SET bar = '1';"

Got error:

ALTER TABLE
ERROR: column "bar" does not exist
SQLSTATE: 42703
Failed running "sql"

Expected behavior

The stmt to work as it was working in 22.1.X

Environment:

  • CockroachDB version [e.g. 22.2.5]
  • Server OS: [e.g. Linux/Ubuntu]
  • Client app [e.g. cockroach sql]

Additional context

The development is block on our side, integration tests are failing because of this.

Jira issue: CRDB-24837

@lopezator lopezator added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Feb 27, 2023
@blathers-crl
Copy link

blathers-crl bot commented Feb 27, 2023

Hello, I am Blathers. I am here to help you get the issue triaged.

Hoot - a bug! Though bugs are the bane of my existence, rest assured the wretched thing will get the best of care here.

I have CC'd a few people who may be able to assist you:

  • @cockroachdb/sql-schema (found keywords: ALTER TABLE)

If we have not gotten back to your issue within a few business days, you can try the following:

  • Join our community slack channel and ask on #cockroachdb.
  • Try find someone from here if you know they worked closely on the area and CC them.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Feb 27, 2023
@ajwerner
Copy link
Contributor

The reason for the change in behavior here is #76834. That change is intended to improve compatibility in other directions. Namely, now, when you issue sql statements in a command batch, they are treated as part of the same transaction. This is what postgres does. So, before 22.2, the schema change and the update were in different transactions.

You can get the old behavior by setting the session variable: SET enable_implicit_transaction_for_batch_statements = false;

As for why cockroach doesn't support writing to a column in the same transaction that adds a column, that's a long-standing issue that has always applied to cockroach. The underlying reason is that it is difficult to both change the schema in a transaction and keep all schema changes online. We've been working towards that goal for years now, and are closer than ever, but the end state remains in the future. The design document here explains how we may achieve that goal.

@blathers-crl blathers-crl bot added the T-sql-schema-deprecated Use T-sql-foundations instead label Feb 27, 2023
@ajwerner
Copy link
Contributor

What's New in v22.2 -> Backward-incompatible changes

Changed the default value of the enable_implicit_transaction_for_batch_statements to true. This means that a batch of statements sent in one string separated by semicolons is treated as an implicit transaction. #76834

I'm inclined to close this issue as duplicating #26508.

@ajwerner
Copy link
Contributor

#31463 is another, perhaps better issue tracking the underlying limitation.

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-blathers-triaged blathers was able to find an owner
Projects
None yet
Development

No branches or pull requests

2 participants