How can I get a plugin to hook ‘dbdelta_queries’ — a filter used during version update?

Must-use (mu) plugins are loaded during the version update process, unlike ordinary plugins. Explanation here. So it’s possible to hook dbdelta_queries and the related filters from within a mu plugin.

(Thanks to Sergey Biryukov for the answer.)

But, some plugins use the same dbDelta() function as core update, so it’s possible to get queries for other tables to hook in the filter.

To avoid filtering anything except during core update, code like this will work.

add_filter( 'dbdelta_queries', 'my_query_filter', 10, 1 );
 
/** Filters the dbDelta SQL queries.
 *
 * @param string[] $queries An array of dbDelta SQL queries.
 * @since 3.3.0
 *
 */
function my_query_filter( $queries ) {
 
  // Look for the core update lock
  $lock_option = 'core_updater.lock';
  $lock_result = get_option( $lock_option );
  /* No lock found? We'e not doing a core update, so bail */
  if ( ! $lock_result ) {
    return $queries;
  }
 
  // Check to see if the lock is still valid. If it is, bail.
  if ( $lock_result > ( time() - ( 15 * MINUTE_IN_SECONDS ) ) ) {
    return $queries;
  }
 
  // Filter the queries as needed
 
  return $queries;
}

See the source code for WP_Upgrader::create_lock() to understand the 'core_updater.lock' option stuff.

And see here for a more complete writeup about how to do this.