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: Can't replace a table with a view in a transaction (SQL docs) #22868

Closed
mbertschler opened this issue Feb 21, 2018 · 7 comments
Closed

sql: Can't replace a table with a view in a transaction (SQL docs) #22868

mbertschler opened this issue Feb 21, 2018 · 7 comments
Labels
A-schema-changes A-schema-transactional C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@mbertschler
Copy link
Contributor

mbertschler commented Feb 21, 2018

I followed the docs at this document and tried running this SQL queries:
https://www.cockroachlabs.com/docs/stable/views.html#simplify-supporting-legacy-code

I expected that the query would run, but it complained that the relation still exists:

root@:26257/> BEGIN;
Now adding input for a multi-line SQL transaction client-side.
Press Enter two times to send the SQL text collected so far to the server, or Ctrl+C to cancel.
You can also use \show to display the statements entered so far.
               -> ALTER TABLE bank.user_accounts RENAME TO bank.client_accounts;
               -> CREATE VIEW bank.user_accounts
               ->   AS SELECT type, email
               ->   FROM bank.client_accounts;
               -> COMMIT;
pq: relation "user_accounts" already exists

Outside of a transaction these statements work.

Full SQL to reproduce:

-- setup
CREATE DATABASE bank;
CREATE TABLE bank.user_accounts (email STRING PRIMARY KEY, type STRING);

-- transaction with error
BEGIN;
ALTER TABLE bank.user_accounts RENAME TO bank.client_accounts;
CREATE VIEW bank.user_accounts
  AS SELECT type, email
  FROM bank.client_accounts;
COMMIT;

Version:

[config] binary: CockroachDB CCL v1.1.5 (darwin amd64, built 2018/02/05 18:22:21, go1.9.3)
[config] arguments: [cockroach start --insecure --host=localhost]

Jira issue: CRDB-5837

@vivekmenezes
Copy link
Contributor

similar to #12123

@knz
Copy link
Contributor

knz commented Feb 21, 2018

@vivekmenezes what do you think about allowing the CREATE to proceed but using some internally defined table name that is guaranteed to be unique. Then perform the rename of the new table to is final name when the txn commits, during the schema change?

@knz
Copy link
Contributor

knz commented Feb 21, 2018

This would also solve #12123.

@vivekmenezes
Copy link
Contributor

This is discussed in https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/sqlbase/structured.proto
in the comment above NameInfo (see worry 3).

The particular problem with your proposal is that it fails when the CREATE statement is followed by an INSERT statement in the same transaction.

@knz
Copy link
Contributor

knz commented Feb 21, 2018

Within the same transaction the correct name would be known to the TableCollection (uncommittedTables). The INSERT would see the correct name from there. I think it would work?

@vivekmenezes
Copy link
Contributor

Yeah, it's a lot more complex though because we have to choose what gets done with transaction with timestamp T and what gets done with transaction with timestamp S. There are two transactions at work here. And once that choice is made it requires some documentation work so users know what to expect. Unfortunately this is not looking like it can be done "transactionally" as in it support serializability

@knz knz added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-bulkio-schema-changes labels May 14, 2018
storjBuildBot pushed a commit to storj/storj that referenced this issue Mar 20, 2020
…add index on node_id

The goal of this change is to improve the storagenode_storage_tallies table by removing the unneeded id column that is not being used but only taking up space, and also to add an index on a different column that needs it. Removing and adding a column seems simple, but ended up being more complicated because of some cockroachdb limitations.

The cockroachdb limitation when trying to remove a column from a table and create a new primary key are:
1. only allows primary key creation at table creation time (docs: https://www.cockroachlabs.com/docs/stable/primary-key.html)
2. table drop or rename is performed async and cannot be done in a transaction (issue: cockroachdb/cockroach#12123, cockroachdb/cockroach#22868)

To address these differences between cockroachdb  and Postgres, this PR performs different migrations for the two database. The Postgres migration is straight forward and what you would expect, but the cockroach migration has two main changes:
1. To change a primary key, use the recommended process from the cockroachdb docs to create a new table with the new primary key you want and then migrate the data.
2. In order to do 1, we needed to do the new table renaming in a separate transaction from the data migration.

Ref: SM-65

Change-Id: Idc9aee3ab57aa4d5570e3d2980afea853cd966bf
VinozzZ pushed a commit to storj/storj that referenced this issue Mar 29, 2020
…add index on node_id

The goal of this change is to improve the storagenode_storage_tallies table by removing the unneeded id column that is not being used but only taking up space, and also to add an index on a different column that needs it. Removing and adding a column seems simple, but ended up being more complicated because of some cockroachdb limitations.

The cockroachdb limitation when trying to remove a column from a table and create a new primary key are:
1. only allows primary key creation at table creation time (docs: https://www.cockroachlabs.com/docs/stable/primary-key.html)
2. table drop or rename is performed async and cannot be done in a transaction (issue: cockroachdb/cockroach#12123, cockroachdb/cockroach#22868)

To address these differences between cockroachdb  and Postgres, this PR performs different migrations for the two database. The Postgres migration is straight forward and what you would expect, but the cockroach migration has two main changes:
1. To change a primary key, use the recommended process from the cockroachdb docs to create a new table with the new primary key you want and then migrate the data.
2. In order to do 1, we needed to do the new table renaming in a separate transaction from the data migration.

Ref: SM-65

Change-Id: Idc9aee3ab57aa4d5570e3d2980afea853cd966bf
@jlinder jlinder added the T-sql-schema-deprecated Use T-sql-foundations instead label Jun 16, 2021
@jordanlewis
Copy link
Member

This is solved. 🎉

@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
A-schema-changes A-schema-transactional C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
Development

No branches or pull requests

5 participants