-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: pg_catalog.pg_constraint broken after swapping PK and altering column type #71709
Comments
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:
If we have not gotten back to your issue within a few business days, you can try the following:
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan. |
Well, for some sort of good news, we "fixed" this on master. Let me track down why we allowed it in 21.1 and earlier.
|
This error you're seeing is the direct consequence of |
What about the following? CREATE TABLE a(id int8 primary key, legacy_id int8 not null);
CREATE TABLE b(a_id int8 references a (id));
BEGIN;
ALTER TABLE a DROP CONSTRAINT "primary";
ALTER TABLE a ADD CONSTRAINT "primary" PRIMARY KEY (legacy_id);
COMMIT;
ALTER TABLE a DROP COLUMN id;
SELECT * FROM pg_catalog.pg_constraint; Here we are dropping the column instead of altering the column data type. This also seems to trigger the same behaviour. Shouldn't it be impossible to drop the I don't know if this is fixed on |
I'm not able to repro the error with the above I'm sorry to say the reason you're seeing this error is that the fix for #71089 hasn't actually been merged to the 21.1 release branch yet: #71166. This is because we usually wait a couple of weeks for a change to "bake" in the master branch before actually backporting it. |
That being said, there's something else at play here. I just checked out the branch for my backport and it also allows the |
We just grabbed build (CCL v22.1.0-alpha.00000000-481-g0e2e6454a3-dirty @ 2021/10/19 16:14:21 (go1.16.6)) from TeamCity and the following appears to produce the error: CREATE TABLE a(id int8 primary key, legacy_id int8 not null );
CREATE TABLE b(a_id int8 references a (id));
BEGIN;
ALTER TABLE a DROP CONSTRAINT "a_pkey";
ALTER TABLE a ADD CONSTRAINT "a_pkey" PRIMARY KEY (legacy_id);
COMMIT;
ALTER TABLE a DROP COLUMN id;
SELECT * FROM pg_catalog.pg_constraint; |
Previously, ALTER TABLE ... ALTER COLUMN ... TYPE ... statements would execute regardless if the column is also the target of a foreign key constraint on another table. This commit fixes this bug. Fixes cockroachdb#71709. Release note (bug fix): fixes a bug in which an ALTER TABLE ... ALTER COLUMN ... TYPE ... statement could execute despite the column being the target of a foreign key constraint on another table.
Thanks, I got it to repro. This proved very useful. The real issue here is that its Note that this is correctly enforced with unique constraints:
In the code this means we should be calling something like |
@fqazi do you know what this is about? |
Only recently picked this up from Marius because it was in a similar area to #75155. It's on my backlog for this week, only did a very superficial look in terms of what needs to be done. |
Fixes: cockroachdb#71709 Previously, when swapping primary keys it was possible to cause foreign key references to break by removing the primary indexes enforcing uniqueness. To address this, this patch will detect if any foreign key references will lose their uniqueness with the removal of the primary index. Release note (bug fix): Swapping primary keys could lead to scenarios where foreign key references could lose their uniqueness.
Fixes: cockroachdb#71709 Previously, when swapping primary keys it was possible to cause foreign key references to break by removing the primary indexes enforcing uniqueness. To address this, this patch will detect if any foreign key references will lose their uniqueness with the removal of the primary index. Release note (bug fix): Swapping primary keys could lead to scenarios where foreign key references could lose their uniqueness.
75820: sql: block swapping primary keys if dependent fk references exist r=ajwerner a=fqazi Fixes: #71709 Previously, when swapping primary keys it was possible to cause foreign key references to break by removing the primary indexes enforcing uniqueness. To address this, this patch will detect if any foreign key references will lose their uniqueness with the removal of the primary index. Release note (bug fix): Swapping primary keys could lead to scenarios where foreign key references could lose their uniqueness. Co-authored-by: Faizan Qazi <[email protected]>
We're currently working on a rather large database migration where we're migrating ids to uuids in all of our old tables. During this migration process we first encountered #71089, and now we're back with yet another exciting bug for you guys 😂
This bug is actually two different parts:
When creating two simple tables, where one references the other via a FK, and swapping the primary keys around on the references table in order to alter the column type of the
id
column (first bug: this should be impossible due to the FK) we encounter a bug where selecting frompg_catalog.pg_constraint;
completely breaks with the following error:ERROR: column-id "1" does not exist
To Reproduce
Expected behavior
For
SELECT * FROM pg_catalog.pg_constraint;
to not return an error, and for changing the data type on a column that is referenced by a foreign key to be impossible. Could this possibly be related toenable_experimental_alter_column_type_general
?Environment:
21.1.11
cockroachdb/cockroach:v21.1.11
cockroach sql
Additional context
Introspection in tools like DataGrip is now fully broken, it's impossible to select from
pg_catalog.pg_constraint
and we've also noticed that it is impossible in some circumstances to check if a table has another colum viapg_catalog.pg_tables
so there might be more to it.The text was updated successfully, but these errors were encountered: