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: Drop and add a primary key constraint trick doesn't work with primary key named as sql keyword #90836

Closed
chengxiong-ruan opened this issue Oct 28, 2022 · 8 comments · Fixed by #90865
Labels
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

@chengxiong-ruan
Copy link
Contributor

chengxiong-ruan commented Oct 28, 2022

Describe the problem
If a table has a primary key whose name is a sql key world, then the Drop and add a primary key constraint trick won't work. The workaround is to rename the primary key to something else >_>.

The reason is that to use sql keyword as name, we need to double quote the string, and this line of code doesn't work because name.String() return a name with the double quote in the string :(

To Reproduce

demo@127.0.0.1:26257/movr> create table stats ( id string, new_id string, CONSTRAINT "primary" PRIMARY KEY (id) );
demo@127.0.0.1:26257/movr> ALTER TABLE stats ALTER COLUMN new_id SET NOT NULL;
demo@127.0.0.1:26257/movr> BEGIN; ALTER TABLE stats DROP CONSTRAINT "primary"; ALTER TABLE stats ADD CONSTRAINT "primary" PRIMARY KEY (new_id); COMMIT;

BEGIN
ALTER TABLE
ERROR: constraint with name "primary" already exists
SQLSTATE: 42710

If you rename the primary key to something else, then it works....

demo@127.0.0.1:26257/movr> ALTER TABLE stats RENAME CONSTRAINT "primary" TO p_key;
demo@127.0.0.1:26257/movr> BEGIN; ALTER TABLE stats DROP CONSTRAINT p_key; ALTER TABLE stats ADD CONSTRAINT p_key PRIMARY KEY (new_id); COMMIT;

Note: timings for multiple statements on a single line are not supported. See https://go.crdb.dev/issue-v/48180/v22.2.

demo@127.0.0.1:26257/movr> show create table stats;
  table_name |               create_statement
-------------+------------------------------------------------
  stats      | CREATE TABLE public.stats (
             |     id STRING NOT NULL,
             |     new_id STRING NOT NULL,
             |     CONSTRAINT p_key PRIMARY KEY (new_id ASC)
             | )

Expected behavior
The trick should work with any primary key names?

Environment:
This can be repro on v22.1, v22.2 and master

Jira issue: CRDB-20965

@chengxiong-ruan chengxiong-ruan added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-schema-deprecated Use T-sql-foundations instead labels Oct 28, 2022
@ajwerner
Copy link
Contributor

Can you explain what's special about the name primary in the code?

@chengxiong-ruan chengxiong-ruan changed the title sql: Drop and add a primary key constraint trick doesn't work with primary key with name primary sql: Drop and add a primary key constraint trick doesn't work with primary key named as sql keyword Oct 28, 2022
@chengxiong-ruan
Copy link
Contributor Author

chengxiong-ruan commented Oct 28, 2022

Can you explain what's special about the name primary in the code?

Sorry, my goland went crazy today...just had a chance to debug. It turned out that it's not about the name primary, it's actually about all sql keywords (double quoted). I updated the issue title and description.

@chengxiong-ruan
Copy link
Contributor Author

Looks like an easy fix... just use string(name) instead of name.String() which escapes keywords at this line.

craig bot pushed a commit that referenced this issue Oct 31, 2022
90176: cli/start: unify code between `cockroach start` and `cockroach mt start-sql` r=andreimatei a=knz

Fixes #89974.
Fixes #90831.
Fixes #90524.

This PR merges the server initialization code between `cockroach start` and `cockroach mt start-sql`.

In doing so, it brings `cockroach mt start-sql` closer to what we expect from proper CockroachDB server processes:

- it fixes support for the (test-only)
  `COCKROACH_EXPERIMENTAL_LINEARIZABLE` env var. (from #4754)

- it adds a tracing span for the startup code. (from #8712!!)

- it properly supports `--listening-url-file`. (from #15468)

- it properly does sanitization of `--external-io-dir`. (from #19725)

- it sets the proper log severity level for gRPC. (from #20308)

- it reports the command-line and env config to logs. (from #21344)

- it prevents startup if there is a `_CRITICAL_ALERT.txt` file
  in the store directory. (from #42401)

- sets the umask for newly created file to remove "other" permission
  bits. This was a security team request originally. (from #44043)

- it recovers support for `DISABLE_STARTING_BACKGROUND_JOBS`. (from #44786)

- it sets `GOMAXPROCS` from current cgroup limits. (from #57390)

- it stops the server early if the storage disk is full. (from #66893)

- it fixes support for the `COCKROACH_DISABLE_SPAN_CONFIGS` config env
  var. (from #73876)

See the individual commit for details.



90660: sql: add contention_events to cluster_execution_insights r=j82w a=j82w

The original contention column will remain
to make query operations faster. The events
are being put into a json column because it's
possible there could be multiple blocking events
for a single statement. The json column avoids the complexity of adding another table and keeping it
in sync with the insights table.

The table can be joined with index_columns and tables to get the database name, table name, and index name that contention occurred on. This does not contain the blocking statement information, and the blocking fingerprint id.

closes: #88561

Release note (sql change): Adds contention_events
to cluster_execution_insights. This is used
to see which transaction is blocking the specific
statement.

90719: opgen: added a bool field in struct opgen.transition r=Xiang-Gu a=Xiang-Gu

This PR adds a bool field in struct opgen.transition that indicates whether it results from a `equiv(xx)` transition spec in the opgen file. It will be useful for a test where we need to find the inital status on a adding/dropping path. Without such a change, it can be problematic if we have a `equiv(xx)` spec as the first transition. E.g.
```
  ToAbsent(
    PUBLIC,
    equiv(VALIDATED),
    to(WRITE_ONLY),
    to(ABSENT),
  )
```
Without this change, the inital status will confusingly be `VALIDATED`, and the next status will be `PUBLIC`.
With this change, the initial status will be `PUBLIC`, and the next status will be `WRITE_ONLY`.

We also added some comments when we make transitions from the specs.

Epic: None
Release note: None

90865: sql: use bare name string of new pk  to compare with pk name when altering primary key r=chengxiong-ruan a=chengxiong-ruan

Fixes #90836

Release note (sql change): previously, the `DROP CONSTRAINT, ADD CONSTRAINT` in one trick to have a new primary key without moving old primary key to be a secondary index didn't work if the primary key name is a reserved SQL keyword. A `constraint already exists` error was returned. This patch fixed the bug, the trick now also works with primary key named as reserved keywords.

Co-authored-by: Raphael 'kena' Poss <[email protected]>
Co-authored-by: j82w <[email protected]>
Co-authored-by: Xiang Gu <[email protected]>
Co-authored-by: Chengxiong Ruan <[email protected]>
@craig craig bot closed this as completed in 746f8f9 Oct 31, 2022
blathers-crl bot pushed a commit that referenced this issue Oct 31, 2022
Fixes #90836

Release note (sql change): previously, the `DROP CONSTRAINT, ADD CONSTRAINT`
in one trick to have a new primary key without moving old
primary key to be a secondary index didn't work if the primary
key name is a reserved SQL keyword. A `constraint already exists`
error was returned. This patch fixed the bug, the trick now
also works with primary key named as reserved keywords.
@theodore-hyman
Copy link
Contributor

Hello lovely engineering folks! Customer is looking for a timeframe on the fix, if anyone can give any insight into what dot release the fix will land on, please let me know.

If you all need more time to investigate and work on it and you don't know right now, that's ok too, just checking in. Thank you

@ajwerner
Copy link
Contributor

@chengxiong-ruan want to backport to 22.1?

@ajwerner
Copy link
Contributor

It has been fixed on master and will be in 22.2.1 (note not 22.2.0). We'll get it into the next 22.1 point release

@chengxiong-ruan
Copy link
Contributor Author

Oh, oops, I'll backport to 22.1.

@chengxiong-ruan
Copy link
Contributor Author

@theodore-hyman I'll manage to merge the backport to 22.1 tomorrow, and the next patch release 22.1.11 will be published on Nov 14th given the release timeline.

chengxiong-ruan added a commit to chengxiong-ruan/cockroach that referenced this issue Nov 1, 2022
Fixes cockroachdb#90836

Release note (sql change): previously, the `DROP CONSTRAINT, ADD CONSTRAINT`
in one trick to have a new primary key without moving old
primary key to be a secondary index didn't work if the primary
key name is a reserved SQL keyword. A `constraint already exists`
error was returned. This patch fixed the bug, the trick now
also works with primary key named as reserved keywords.
chengxiong-ruan added a commit to chengxiong-ruan/cockroach that referenced this issue Nov 1, 2022
Fixes cockroachdb#90836

Release note (sql change): previously, the `DROP CONSTRAINT, ADD CONSTRAINT`
in one trick to have a new primary key without moving old
primary key to be a secondary index didn't work if the primary
key name is a reserved SQL keyword. A `constraint already exists`
error was returned. This patch fixed the bug, the trick now
also works with primary key named as reserved keywords.
@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. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
3 participants