LP #1697915: pt-online-schema-change remove the old and new table
Description
Environment
AFFECTED CS IDs
Smart Checklist
Activity
Sveta Smirnova September 15, 2020 at 12:32 PM
This is duplicate of https://perconadev.atlassian.net/browse/PT-169#icft=PT-169
lpjirasync January 24, 2018 at 5:02 PM
**Comment from Launchpad by: Hamoon Mohammadian Pour on: 29-06-2017 05:53:20
Thank you for testing.
I think the document should be updated and warned to prevent this happen.
lpjirasync January 24, 2018 at 5:02 PM
**Comment from Launchpad by: Carlos Salguero on: 26-06-2017 17:45:55
To try to re-reproduce this error, I wrote this small Go program to load some sample data: https://gist.github.com/percona-csalguero/22457e02e46e10219ad0d8df892ce8e1
On my first try, I was unable to reproduce the problem, even when the program fails, no data is deleted:
Determining the method to update foreign keys...
2017-06-26T14:34:48 `test`.`user_comments`: 96850 rows; can use rebuild_constraints
2017-06-26T14:34:48 Analyzing new table...
2017-06-26T14:34:48 Swapping tables...
2017-06-26T14:34:48 Swapped original and new tables OK.
2017-06-26T14:34:48 Rebuilding foreign key constraints...
2017-06-26T14:34:48 Dropping triggers...
2017-06-26T14:34:48 Dropped triggers OK.
Altered `test`.`users` but there were errors or warnings.
Error updating foreign key constraints: 2017-06-26T14:34:48 DBD::mysql::db do failed: Failed to add the foreign key constaint. Missing index for constraint '_fk1' in the referenced table 'users' [for Statement "ALTER TABLE `test`.`user_comments` DROP FOREIGN KEY `fk1`, ADD CONSTRAINT `_fk1` FOREIGN KEY (`user_id`) REFERENCES `test`.`users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION"] at bin/pt-online-schema-change line 10674.
As you can see in the log, osc chose rebuild_constraints method for FKs:
2017-06-26T14:34:48 `test`.`user_comments`: 96850 rows; can use rebuild_constraints
So I've updated my program to load 200K rows instead of 100K ( max_number = 1E5) and ran osc again:
Determining the method to update foreign keys...
2017-06-26T14:37:39 `test`.`user_comments`: too many rows: 184955; must use drop_swap
2017-06-26T14:37:39 Drop-swapping tables...
2017-06-26T14:37:39 Analyzing new table...
2017-06-26T14:37:39 Dropping triggers...
2017-06-26T14:37:39 Dropped triggers OK.
2017-06-26T14:37:39 Dropping new table...
2017-06-26T14:37:39 Dropped new table OK.
Altered `test`.`users` but there were errors or warnings.
Error updating foreign key constraints: 2017-06-26T14:37:39 DBD::mysql::db do failed: Error on rename of './test/_users_new' to './test/users' (errno: 150 "Foreign key constraint is incorrectly formed") [for Statement "RENAME TABLE `test`.`_users_new` TO `test`.`users`"] at bin/pt-online-schema-change line 10742.
This time the program failed and since there are too many rows, osc chose drop_swap to rebuild constraints, so to be able to reproduce this issue, --alter-foreign-keys-method should be drop_swap.
lpjirasync January 24, 2018 at 5:02 PM
**Comment from Launchpad by: Carlos Salguero on: 26-06-2017 15:23:36
I've verified it fails but if you don't force set-vars=foreign_key_checks=0, all data is preserved.
lpjirasync January 24, 2018 at 5:02 PM
**Comment from Launchpad by: Hamoon Mohammadian Pour on: 23-06-2017 12:11:01
Hi Carlos,
Look at post number 0 and number 2.
I explained
Details
Assignee
UnassignedUnassignedReporter
lpjirasynclpjirasync(Deactivated)Priority
High
Details
Details
Assignee
Reporter
Priority
Smart Checklist
Open Smart Checklist
Smart Checklist
Open Smart Checklist
Smart Checklist

**Reported in Launchpad by Hamoon Mohammadian Pour last update 29-06-2017 05:53:20
Hello.
Today I altered one of my tables (users) with pt-online-schema-change
One table(user_comments) of my database had a foreign key (refer) to users table.
I changed id column from int to bigint
But at the end when it wanted to rename new table to original table, this message was displayed:
Error updating foreign key constraints: 2017-06-14T10:17:41 DBD::mysql::db do failed: Error on rename of './myblog/_users_new' to './myblog/users' (errno: 150 "Foreign key constraint is incorrectly formed") [for Statement "RENAME TABLE `myblog`.`_users_new` TO `myblog`.`users`"] at /usr/bin/pt-online-schema-change line 10700.
I know what is this, and I know firstly I must alter user_comments table or removeForeign key before alter users table, but I think pt-online-schema-change has bug because it should be kept up new table.
It was full command:
pt-online-schema-change -uxxxx -pxxxx --max-load "Threads_running=400" --set-vars=foreign_key_checks=0 --alter-foreign-keys-method=auto --no-check-alter --critical-load="Threads_running=200" --alter="CHANGE COLUMN id id BIGINT UNSIGNED NOT NULL FIRST" D=myblog,t=users --execute
MariaDB Version: mariadb-galera-10.0.30
pt-online-schema-change Version: 3.0.3