Skip to content

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

chore: update pg check slow query enabled (#5684)

rebelice805cc515f1cc
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.go
index 399ec26eb4..4c5e3760bf 100644
--- a/backend/api/v1/instance_service.go
+++ b/backend/api/v1/instance_service.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
+ "go.uber.org/multierr"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
@@ -17,6 +18,7 @@ import (
api "github.com/bytebase/bytebase/backend/legacyapi"
metricAPI "github.com/bytebase/bytebase/backend/metric"
"github.com/bytebase/bytebase/backend/plugin/db"
+ "github.com/bytebase/bytebase/backend/plugin/db/pg"
"github.com/bytebase/bytebase/backend/plugin/metric"
"github.com/bytebase/bytebase/backend/runner/metricreport"
"github.com/bytebase/bytebase/backend/store"
@@ -218,17 +220,13 @@ func (s *InstanceService) SyncSlowQueries(ctx context.Context, request *v1pb.Syn
return nil, status.Errorf(codes.Internal, "failed to list databases: %s", err.Error())
}
- databaseMap := make(map[string]bool)
- for _, database := range slowQueryPolicy.DatabaseList {
- databaseMap[database] = true
- }
-
var firstDatabase string
+ var errs error
for _, database := range databases {
if database.SyncState != api.OK {
continue
}
- if _, exists := databaseMap[database.DatabaseName]; !exists {
+ if _, exists := pg.ExcludedDatabaseList[database.DatabaseName]; exists {
continue
}
if err := func() error {
@@ -239,7 +237,7 @@ func (s *InstanceService) SyncSlowQueries(ctx context.Context, request *v1pb.Syn
defer driver.Close(ctx)
return driver.CheckSlowQueryLogEnabled(ctx)
}(); err != nil {
- return nil, status.Errorf(codes.FailedPrecondition, "slow query log for database %q is not enabled: %s", database.DatabaseName, err.Error())
+ errs = multierr.Append(errs, err)
}
if firstDatabase == "" {
@@ -247,6 +245,10 @@ func (s *InstanceService) SyncSlowQueries(ctx context.Context, request *v1pb.Syn
}
}
+ if errs != nil {
+ return nil, status.Errorf(codes.FailedPrecondition, "slow query log is not enabled: %s", errs.Error())
+ }
+
if firstDatabase == "" {
return nil, status.Errorf(codes.FailedPrecondition, "no database enabled pg_stat_statements")
}
diff --git a/backend/legacyapi/policy.go b/backend/legacyapi/policy.go
index 1c7811f7e9..07035bfc7a 100644
--- a/backend/legacyapi/policy.go
+++ b/backend/legacyapi/policy.go
@@ -292,8 +292,7 @@ func (p *AccessControlPolicy) String() (string, error) {
// SlowQueryPolicy is the policy configuration for slow query.
type SlowQueryPolicy struct {
- Active bool `json:"active"`
- DatabaseList []string `json:"databaseList"`
+ Active bool `json:"active"`
}
// UnmarshalSlowQueryPolicy will unmarshal payload to slow query policy.
diff --git a/backend/plugin/db/pg/dump.go b/backend/plugin/db/pg/dump.go
index a0180a6dde..dd25d3346f 100644
--- a/backend/plugin/db/pg/dump.go
+++ b/backend/plugin/db/pg/dump.go
@@ -40,7 +40,7 @@ func (driver *Driver) Dump(ctx context.Context, out io.Writer, schemaOnly bool)
dumpableDbNames = []string{driver.databaseName}
} else {
for _, n := range databases {
- if excludedDatabaseList[n.Name] {
+ if ExcludedDatabaseList[n.Name] {
continue
}
dumpableDbNames = append(dumpableDbNames, n.Name)
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index 167e1aba54..80c5d04d6d 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -24,7 +24,8 @@ import (
)
var (
- excludedDatabaseList = map[string]bool{
+ // ExcludedDatabaseList is the list of system or internal databases.
+ ExcludedDatabaseList = map[string]bool{
// Skip our internal "bytebase" database
"bytebase": true,
// Skip internal databases from cloud service providers
diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.go
index a4abcdcda1..11fc32703d 100644
--- a/backend/plugin/db/pg/sync.go
+++ b/backend/plugin/db/pg/sync.go
@@ -43,7 +43,7 @@ func (driver *Driver) SyncInstance(ctx context.Context) (*db.InstanceMetadata, e
var filteredDatabases []*storepb.DatabaseMetadata
for _, database := range databases {
// Skip all system databases
- if _, ok := excludedDatabaseList[database.Name]; ok {
+ if _, ok := ExcludedDatabaseList[database.Name]; ok {
continue
}
filteredDatabases = append(filteredDatabases, database)
diff --git a/backend/runner/slowquerysync/syncer.go b/backend/runner/slowquerysync/syncer.go
index adb7a5c615..73c99589e9 100644
--- a/backend/runner/slowquerysync/syncer.go
+++ b/backend/runner/slowquerysync/syncer.go
@@ -17,6 +17,7 @@ import (
"github.com/bytebase/bytebase/backend/component/state"
api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/plugin/db"
+ "github.com/bytebase/bytebase/backend/plugin/db/pg"
"github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
@@ -119,13 +120,13 @@ func (s *Syncer) syncInstanceSlowQuery(ctx context.Context, instance *store.Inst
case db.MySQL:
return s.syncMySQLSlowQuery(ctx, instance)
case db.Postgres:
- return s.syncPostgreSQLSlowQuery(ctx, instance, slowQueryPolicy)
+ return s.syncPostgreSQLSlowQuery(ctx, instance)
default:
return errors.Errorf("unsupported database engine: %s", instance.Engine)
}
}
-func (s *Syncer) syncPostgreSQLSlowQuery(ctx context.Context, instance *store.InstanceMessage, policy *api.SlowQueryPolicy) error {
+func (s *Syncer) syncPostgreSQLSlowQuery(ctx context.Context, instance *store.InstanceMessage) error {
databases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{
InstanceID: &instance.ResourceID,
})
@@ -133,17 +134,12 @@ func (s *Syncer) syncPostgreSQLSlowQuery(ctx context.Context, instance *store.In
return err
}
- databaseMap := make(map[string]bool)
- for _, database := range policy.DatabaseList {
- databaseMap[database] = true
- }
-
var firstDatabase string
for _, database := range databases {
if database.SyncState != api.OK {
continue
}
- if _, exists := databaseMap[database.DatabaseName]; !exists {
+ if _, exists := pg.ExcludedDatabaseList[database.DatabaseName]; exists {
continue
}
if err := func() error {