void handle_alter_part_error(ALTER_PARTITION_PARAM_TYPE *lpt,
                             bool action_completed,
                             bool drop_partition,
                             bool frm_install,
                             bool close_table)
{
  partition_info *part_info= lpt->part_info;
  THD *thd= lpt->thd;
  TABLE *table= lpt->table;
  DBUG_ENTER("handle_alter_part_error");
  DBUG_ASSERT(table->needs_reopen());

  if (close_table)
  {
    /*
      All instances of this table needs to be closed.
      Better to do that here, than leave the cleaning up to others.
      Aquire EXCLUSIVE mdl lock if not already aquired.
    */
    if (!thd->mdl_context.is_lock_owner(MDL_key::TABLE, lpt->db.str,
                                        lpt->table_name.str,
                                        MDL_EXCLUSIVE))
    {
      if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN))
      {
        /* At least remove this instance on failure */
        goto err_exclusive_lock;
      }
    }
    /* Ensure the share is destroyed and reopened. */
    if (part_info)
      part_info= part_info->get_clone(thd);
    close_all_tables_for_name(thd, table->s, HA_EXTRA_NOT_USED, NULL);
  }
  else
  {
err_exclusive_lock:
    /*
      Temporarily remove it from the locked table list, so that it will get
      reopened.
    */
    thd->locked_tables_list.unlink_from_list(thd,
                                             table->pos_in_locked_tables,
                                             false);
    /*
      Make sure that the table is unlocked, closed and removed from
      the table cache.
    */
    mysql_lock_remove(thd, thd->lock, table);
    if (part_info)
      part_info= part_info->get_clone(thd);
    close_thread_table(thd, &thd->open_tables);
    lpt->table_list->table= NULL;
  }

  if (part_info->first_log_entry &&
      execute_ddl_log_entry(thd, part_info->first_log_entry->entry_pos))
  {
    /*
      We couldn't recover from error, most likely manual interaction
      is required.
    */
    write_log_completed(lpt, FALSE);
    release_log_entries(part_info);
    if (!action_completed)
    {
      if (drop_partition)
      {
        /* Table is still ok, but we left a shadow frm file behind. */
        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, 1,
                            "%s %s",
           "Operation was unsuccessful, table is still intact,",
           "but it is possible that a shadow frm file was left behind");
      }
      else
      {
        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, 1,
                            "%s %s %s %s",
           "Operation was unsuccessful, table is still intact,",
           "but it is possible that a shadow frm file was left behind.",
           "It is also possible that temporary partitions are left behind,",
           "these could be empty or more or less filled with records");
      }
    }
    else
    {
      if (frm_install)
      {
        /*
           Failed during install of shadow frm file, table isn't intact
           and dropped partitions are still there
        */
        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, 1,
                            "%s %s %s",
          "Failed during alter of partitions, table is no longer intact.",
          "The frm file is in an unknown state, and a backup",
          "is required.");
      }
      else if (drop_partition)
      {
        /*
          Table is ok, we have switched to new table but left dropped
          partitions still in their places. We remove the log records and
          ask the user to perform the action manually. We remove the log
          records and ask the user to perform the action manually.
        */
        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, 1,
                            "%s %s",
              "Failed during drop of partitions, table is intact.",
              "Manual drop of remaining partitions is required");
      }
      else
      {
        /*
          We failed during renaming of partitions. The table is most
          certainly in a very bad state so we give user warning and disable
          the table by writing an ancient frm version into it.
        */
        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, 1,
                            "%s %s %s",
           "Failed during renaming of partitions. We are now in a position",
           "where table is not reusable",
           "Table is disabled by writing ancient frm file version into it");
      }
    }
  }
  else
  {
    release_log_entries(part_info);
    if (!action_completed)
    {
      /*
        We hit an error before things were completed but managed
        to recover from the error. An error occurred and we have
        restored things to original so no need for further action.
      */
      ;
    }
    else
    {
      /*
        We hit an error after we had completed most of the operation
        and were successful in a second attempt so the operation
        actually is successful now. We need to issue a warning that
        even though we reported an error the operation was successfully
        completed.
      */
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, 1,"%s %s",
         "Operation was successfully completed by failure handling,",
         "after failure of normal operation");
    }
  }

  if (thd->locked_tables_mode)
  {
    Diagnostics_area *stmt_da= NULL;
    Diagnostics_area tmp_stmt_da(true);

    if (unlikely(thd->is_error()))
    {
      /* reopen might fail if we have a previous error, use a temporary da. */
      stmt_da= thd->get_stmt_da();
      thd->set_stmt_da(&tmp_stmt_da);
    }

    if (unlikely(thd->locked_tables_list.reopen_tables(thd, false)))
      sql_print_warning("We failed to reacquire LOCKs in ALTER TABLE");

    if (stmt_da)
      thd->set_stmt_da(stmt_da);
  }

  DBUG_VOID_RETURN;
}