# Pt-online-schema-change not handling rename column properly

**URL:** <https://forums.percona.com/t/pt-online-schema-change-not-handling-rename-column-properly/33317>\
**Category:** Percona Toolkit\
**Created:** [September 24, 2024, 12:45pm UTC](https://forums.percona.com/t/pt-online-schema-change-not-handling-rename-column-properly/33317 "2024-09-24T12:45:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gilg](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/gilg/32/1301_2.png) [@gilg](https://forums.percona.com/u/gilg)\
**Post date:** [September 24, 2024, 12:45pm UTC](https://forums.percona.com/t/pt-online-schema-change-not-handling-rename-column-properly/33317/1 "2024-09-24T12:45:25Z")

</div>

Hey  
I have a table with columns x and x\_new

i’m running pt-online-schema-change with the following alter

DROP COLUMN x, RENAME COLUMN x\_new TO x

the tool finishes successfully, now table has only column x, but the values in it are the values that were before in x, not in x\_new

version 3.5.7

I see I can solve it by making the alter

drop column x, CHANGE x\_new x int

but in this case I get an error that I must specify --no-check-alter, and that I might lose data.

What is the best way to do such a change?

---

<div class="post-metadata">

**Author:** ![matthewb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/matthewb/32/34_2.png) [@matthewb](https://forums.percona.com/u/matthewb)\
**Post date:** [September 25, 2024, 12:27am UTC](https://forums.percona.com/t/pt-online-schema-change-not-handling-rename-column-properly/33317/2 "2024-09-25T00:27:19Z")

</div>

> [@gilg](#):
>
> but the values in it are the values that were before in x, not in x\_new

The way pt-osc works is as follows:

- create new table foo\_new like foo
- alter table foo\_new \<insert user’s --alter statement\>
- copy rows from foo to foo\_new
- drop foo, rename foo\_new to foo

Because you have drop column, rename column in 1 command, the foo\_new table only has `x` column in it because you dropped it, then renamed the \_new one. Then when ptosc copies rows from original table, it’s copying rows from `x`.

The ALTER TABLE is not a two-step process. It is not doing “drop X, copy data, then rename x\_new to x” It performs the entire alter _first_, then copies the data. This behavior you are experiencing is expected.

> [@gilg](#):
>
> I see I can solve it by making the alter

This won’t solve it. you’ll have the same result because of what I described above.

If x\_new already has the data you want, then run ptosc to handle the DROP only. Once done, run a regular alter to rename the column. Renaming a column does not require rebuilding the table (mysql 8.0).

---

<div class="post-metadata">

**Author:** ![gilg](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/gilg/32/1301_2.png) [@gilg](https://forums.percona.com/u/gilg)\
**Post date:** [September 25, 2024, 3:50am UTC](https://forums.percona.com/t/pt-online-schema-change-not-handling-rename-column-properly/33317/3 "2024-09-25T03:50:59Z")

</div>

Thanks for the response, notice when said changing the alter would work, it is because I tested it, it worked as intended with the “change” version

---

<div class="post-metadata">

**Author:** ![matthewb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/matthewb/32/34_2.png) [@matthewb](https://forums.percona.com/u/matthewb)\
**Post date:** [September 25, 2024, 6:34pm UTC](https://forums.percona.com/t/pt-online-schema-change-not-handling-rename-column-properly/33317/4 "2024-09-25T18:34:43Z")

</div>

Could you provide the PTDEBUG=1 of your test? I would be interested in seeing what is actually happening when you use CHANGE vs RENAME COLUMN in this case because the resulting column name is the same before the data copy happens.

---

<div class="post-metadata">

**Author:** ![gilg](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/gilg/32/1301_2.png) [@gilg](https://forums.percona.com/u/gilg)\
**Post date:** [September 26, 2024, 10:58am UTC](https://forums.percona.com/t/pt-online-schema-change-not-handling-rename-column-properly/33317/5 "2024-09-26T10:58:17Z")

</div>

No problem

I see now that the working version I got was two changes, not drop and change, not sure if that is what you didn’t see how it worked, but anyway :

create table test123(id int(10) not null, v1 int(10), v2 int(10), v3 int(10),v3\_new int(10), primary key(id));

(Attachment pt\_result is missing)

---

<div class="post-metadata">

**Author:** ![gilg](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/gilg/32/1301_2.png) [@gilg](https://forums.percona.com/u/gilg)\
**Post date:** [September 26, 2024, 11:00am UTC](https://forums.percona.com/t/pt-online-schema-change-not-handling-rename-column-properly/33317/6 "2024-09-26T11:00:19Z")

</div>

[pt\_result.txt](https://forums.percona.com/uploads/short-url/igMKPgk5BLKdTpANETXfmjmk6vj.txt) (57.6 KB)

---

<div class="post-metadata">

**Author:** ![matthewb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/matthewb/32/34_2.png) [@matthewb](https://forums.percona.com/u/matthewb)\
**Post date:** [September 26, 2024, 5:18pm UTC](https://forums.percona.com/t/pt-online-schema-change-not-handling-rename-column-properly/33317/7 "2024-09-26T17:18:15Z")

</div>

Looking at the source code for pt-osc:

```auto
   my $alter_change_col_re = qr/\bCHANGE \s+ (?:COLUMN \s+)?
                                ($table_ident) \s+ ($table_ident)/ix;

```

pt-osc has a specific check for CHANGE which tracks the old column name and the new name. That’s why it works! This is interesting and something I didn’t know pt-osc did.

Thanks for the debug info.
