Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: create slow_query table for prod (#5463)

rebelice40cfd6d6b0b4
diff --git a/backend/migrator/migration/dev/20230404000000##slow_query_table.sql b/backend/migrator/migration/prod/1.16/0000##slow_query_table.sql
similarity index 100%
rename from backend/migrator/migration/dev/20230404000000##slow_query_table.sql
rename to backend/migrator/migration/prod/1.16/0000##slow_query_table.sql
diff --git a/backend/migrator/migration/prod/LATEST.sql b/backend/migrator/migration/prod/LATEST.sql
index 541cf2e39b..94b102ac74 100644
--- a/backend/migrator/migration/prod/LATEST.sql
+++ b/backend/migrator/migration/prod/LATEST.sql
@@ -1126,3 +1126,35 @@ BEFORE
UPDATE
ON risk FOR EACH ROW
EXECUTE FUNCTION trigger_update_updated_ts();
+
+-- slow_query stores slow query statistics for each database.
+CREATE TABLE slow_query (
+ id SERIAL PRIMARY KEY,
+ row_status row_status NOT NULL DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
+ created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
+ -- updated_ts is used to identify the latest timestamp for syncing slow query logs.
+ updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ -- In MySQL, users can query without specifying a database. In this case, instance_id is used to identify the instance.
+ instance_id INTEGER NOT NULL REFERENCES instance (id),
+ -- In MySQL, users can query without specifying a database. In this case, database_id is NULL.
+ database_id INTEGER NULL REFERENCES db (id),
+ -- It's hard to store all slow query logs, so the slow query is aggregated by day and database.
+ log_date_ts INTEGER NOT NULL,
+ -- It's hard to store all slow query logs, we sample the slow query log and store the part of them as details.
+ slow_query_statistics JSONB NOT NULL DEFAULT '{}'
+);
+
+-- The slow query log is aggregated by day and database and we usually query the slow query log by day and database.
+CREATE UNIQUE INDEX uk_slow_query_database_id_log_date_ts ON slow_query (database_id, log_date_ts);
+
+CREATE INDEX idx_slow_query_instance_id_log_date_ts ON slow_query (instance_id, log_date_ts);
+
+ALTER SEQUENCE slow_query_id_seq RESTART WITH 101;
+
+CREATE TRIGGER update_slow_query_updated_ts
+BEFORE
+UPDATE
+ ON slow_query FOR EACH ROW
+EXECUTE FUNCTION trigger_update_updated_ts();
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index 3978439c35..eeae7d9ff9 100644
--- a/backend/migrator/migrator_test.go
+++ b/backend/migrator/migrator_test.go
@@ -241,5 +241,5 @@ func TestMigrationCompatibility(t *testing.T) {
func TestGetCutoffVersion(t *testing.T) {
releaseVersion, err := getProdCutoffVersion()
require.NoError(t, err)
- require.Equal(t, semver.MustParse("1.15.5"), releaseVersion)
+ require.Equal(t, semver.MustParse("1.16.0"), releaseVersion)
}