diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index fc89352b661..5c9c6d2a7db 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5296,6 +5296,8 @@ ATRewriteCatalogs(List **wqueue, LOCKMODE lockmode,
                                  AlterTableUtilityContext *context)
 {
        ListCell   *ltab;
+       bool        needAlterTypeCleanup = false;
+       AlteredTableInfo *tabToCleanup = NULL;

        /*
         * We process all the tables "in parallel", one pass at a time.  This is
@@ -5313,6 +5315,13 @@ ATRewriteCatalogs(List **wqueue, LOCKMODE lockmode,
                        List       *subcmds = tab->subcmds[pass];
                        ListCell   *lcmd;

+                       if (pass == AT_PASS_OLD_INDEX && needAlterTypeCleanup)
+                       {
+                               ATPostAlterTypeCleanup(wqueue, tabToCleanup, lockmode);
+                               needAlterTypeCleanup = false;
+                               tabToCleanup = NULL;
+                       }
+
                        if (subcmds == NIL)
                                continue;

@@ -5334,7 +5343,10 @@ ATRewriteCatalogs(List **wqueue, LOCKMODE lockmode,
                         * done only once if multiple columns of a table are altered).
                         */
                        if (pass == AT_PASS_ALTER_TYPE || pass == AT_PASS_SET_EXPRESSION)
-                               ATPostAlterTypeCleanup(wqueue, tab, lockmode);
+                       {
+                               needAlterTypeCleanup = true;
+                               tabToCleanup = tab;
+                       }

                        if (tab->rel)
                        {
