In order to do a few clever things, Query Monitor loads earlier than you ever thought humanly possible (almost). It does this by symlinking a custom db.php in your WP_CONTENT_DIR. This file (when present) gets included before the database driver is loaded, meaning this portion of Query Monitor loads before WordPress even engages its brain.

In this file is Query Monitor’s extension to the wpdb class which:

  1. Allows us to log all database queries (including ones that happen before plugins are loaded)
  2. Logs the full stack trace for each query, which allows us to determine the component that’s responsible for the query
  3. Logs the query result, which allows us to display the affected rows or error message if applicable
  4. Logs various PHP configurations before anything has loaded, which allows us to display a message if these get altered at runtime by a plugin or theme

This is totally fucking clever and very well done considering that it’s not even using a profiler. Bravo, sir!