gh-ost icon indicating copy to clipboard operation
gh-ost copied to clipboard

CHECK constraints being renamed to MySQL defaults

Open zball opened this issue 3 years ago • 2 comments

gh-ost version: 1.1.6

We noticed when migrating tables with named CHECK constraints, they were renamed after the migration to the MySQL default names.

eg:
A CHECK constraint on a table foo named my_check_constraint is renamed to foo_check_1 after a gh-ost migration of table foo.

I suspect this is because it looks like gh-ost is using a CREATE TABLE ... LIKE stmt referenced here. CREATE TABLE ... LIKE will copy the CHECK constraints but generate their names. [src]

For CREATE TABLE ... LIKE, the destination table preserves CHECK constraints from the original table, except that all the constraint names are generated.

We'd like to keep these CHECK constraint names consistent if possible. Is this a feature that can be added in a future release or a welcomed PR?

Thanks for all of the great work on gh-ost!

zball avatar Nov 30 '23 19:11 zball

Yes we'd welcome a PR

meiji163 avatar Nov 05 '24 16:11 meiji163

This came up for a team I was working with recently, so I looked into the feasibility of a fix. Unfortunately, I don't think it can be implemented without violating one of gh-ost's core promises.

There are fundamental limitations of MySQL that we run into:

  • CREATE TABLE ... LIKE regenerates CHECK constraint names
  • MySQL has no operation like RENAME CONSTRAINT
  • CHECK constraint names must be unique per schema
  • ADD CHECK on a non-empty table always performs a full validation scan

Here's what a potential fix would have to do:

  1. During row copy, the ghost table holds CHECKs under temporary names (required because the original names are still owned by the source table and MySQL enforces schema-global uniqueness).
  2. After cut-over, drop the CHECKs on the renamed-away old table (instant) to free the names.
  3. On the now-live new table, drop the temp-named CHECK and add it back under the original name.

The final step is a full-table scan, not feasible on large/busy production tables. Unlike the operations that gh-ost manages, this scan:

  • Can't be throttled — MySQL doesn't have any kind of DDL throttle/IO limit/replication-lag awareness
  • Can't be paused/resumed — only option is KILL which rolls the work back, so retry starts from zero
  • Competes with live traffic for IO and buffer pool
  • Replicates as a single ALTER, so replicas re-do the scan and can fall behind

This kind of impact on large production tables is exactly what gh-ost is designed to avoid. I'd suggest documenting unstable CHECK names as a known limitation rather than attempting a fix.

ggilder avatar May 15 '26 17:05 ggilder