Merge remote-tracking branch 'origin/v3' into rewrite-fixes

This commit is contained in:
Matthew Bucci
2024-02-26 23:55:09 -08:00
23 changed files with 454 additions and 78 deletions

View File

@@ -24,6 +24,9 @@ class SelectSQLRewriter extends AbstractSQLRewriter
// Remove the LIMIT clause if it exists
$sql = preg_replace('/\s+LIMIT\s+\d+(\s*,\s*\d+)?/i', '', $sql);
// Remove the ORDER BY containing case / end clause if it exists
$sql = preg_replace('/\s+ORDER\s+BY\s+.+END\),[^)]+/is', '', $sql);
// Remove the ORDER BY clause if it exists
$sql = preg_replace('/\s+ORDER\s+BY\s+[^)]+/i', '', $sql);
@@ -31,6 +34,16 @@ class SelectSQLRewriter extends AbstractSQLRewriter
$sql = preg_replace('/SELECT\s+.*?\s+FROM\s+/is', 'SELECT COUNT(*) FROM ', $sql, 1);
}
if(false !== strpos($sql, 'information_schema')) {
// WP Site Health rewrites
if (false !== strpos($sql, "SELECT TABLE_NAME AS 'table', TABLE_ROWS AS 'rows', SUM(data_length + index_length)")) {
$sql = $this->postgresTableSizeRewrite();
return $sql;
}
throw new Exception("Unsupported call to information_schema, this probably won't work correctly and needs to be specifically handled, open a github issue with the SQL");
}
$sql = $this->ensureOrderByInSelect($sql);
// Convert CONVERT to CAST
@@ -346,4 +359,29 @@ class SelectSQLRewriter extends AbstractSQLRewriter
return $sql;
}
// This method is specifically to handle should_suggest_persistent_object_cache in wp site health
protected function postgresTableSizeRewrite($schema = 'public')
{
$sql = <<<SQL
SELECT
C.relname AS "table",
S.n_live_tup AS "rows",
pg_total_relation_size(C.oid) AS "bytes"
FROM
pg_class C
LEFT JOIN
pg_namespace N ON (N.oid = C.relnamespace)
INNER JOIN
pg_stat_user_tables S ON (S.relid = C.oid)
WHERE
N.nspname = '$schema' AND
C.relname IN ('wp_comments','wp_options','wp_posts','wp_terms','wp_users')
GROUP BY
C.relname, pg_total_relation_size(C.oid), S.n_live_tup;
SQL;
return $sql;
}
}