Skip to content

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

chore: allow no enabled database for Postgres slow queries (#7561)

rebeliced21e0d87d3d0
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.go
index 74f1b5e825..26c9333be5 100644
--- a/backend/api/v1/instance_service.go
+++ b/backend/api/v1/instance_service.go
@@ -269,11 +269,18 @@ func (s *InstanceService) syncSlowQueriesImpl(ctx context.Context, project *stor
}
defer driver.Close(ctx)
if err := driver.CheckSlowQueryLogEnabled(ctx); err != nil {
- return status.Errorf(codes.FailedPrecondition, "slow query log is not enabled: %s", err.Error())
+ log.Warn("slow query log is not enabled", zap.String("instance", instance.ResourceID), zap.Error(err))
+ return nil
}
// Sync slow queries for instance.
- s.stateCfg.InstanceSlowQuerySyncChan <- instance.ResourceID
+ message := &state.InstanceSlowQuerySyncMessage{
+ InstanceID: instance.ResourceID,
+ }
+ if project != nil {
+ message.ProjectID = project.ResourceID
+ }
+ s.stateCfg.InstanceSlowQuerySyncChan <- message
case db.Postgres:
findDatabase := &store.FindDatabaseMessage{
InstanceID: &instance.ResourceID,
@@ -310,14 +317,17 @@ func (s *InstanceService) syncSlowQueriesImpl(ctx context.Context, project *stor
}
if len(enabledDatabases) == 0 {
- if project == nil {
- return status.Errorf(codes.FailedPrecondition, "no database enabled pg_stat_statements")
- }
return nil
}
// Sync slow queries for instance.
- s.stateCfg.InstanceSlowQuerySyncChan <- instance.ResourceID
+ message := &state.InstanceSlowQuerySyncMessage{
+ InstanceID: instance.ResourceID,
+ }
+ if project != nil {
+ message.ProjectID = project.ResourceID
+ }
+ s.stateCfg.InstanceSlowQuerySyncChan <- message
default:
return status.Errorf(codes.InvalidArgument, "unsupported engine %q", instance.Engine)
}
diff --git a/backend/component/state/state.go b/backend/component/state/state.go
index 3335d5930b..62a1ce59d5 100644
--- a/backend/component/state/state.go
+++ b/backend/component/state/state.go
@@ -15,7 +15,7 @@ type State struct {
// InstanceDatabaseSyncChan is the channel for synchronizing schemas for instances.
InstanceDatabaseSyncChan chan *store.InstanceMessage
// InstanceSlowQuerySyncChan is the channel for synchronizing slow query logs for instances.
- InstanceSlowQuerySyncChan chan string
+ InstanceSlowQuerySyncChan chan *InstanceSlowQuerySyncMessage
// RollbackGenerate is the set of tasks for generating rollback statements.
RollbackGenerate sync.Map // map[task.ID]*store.TaskMessage
@@ -53,3 +53,13 @@ type State struct {
sync.Mutex
}
+
+// InstanceSlowQuerySyncMessage is the message for synchronizing slow query logs for instances.
+type InstanceSlowQuerySyncMessage struct {
+ InstanceID string
+
+ // ProjectID is used to filter the database list.
+ // If ProjectID is empty, then all databases will be synced.
+ // If ProjectID is not empty, then only databases belong to the project will be synced.
+ ProjectID string
+}
diff --git a/backend/runner/slowquerysync/syncer.go b/backend/runner/slowquerysync/syncer.go
index a470ec6255..90f942becf 100644
--- a/backend/runner/slowquerysync/syncer.go
+++ b/backend/runner/slowquerysync/syncer.go
@@ -58,9 +58,9 @@ func (s *Syncer) Run(ctx context.Context, wg *sync.WaitGroup) {
case <-ctx.Done():
log.Debug("Slow query syncer received context cancellation")
return
- case instanceResourceID := <-s.stateCfg.InstanceSlowQuerySyncChan:
- log.Debug("Slow query syncer received instance slow query sync request", zap.String("instance", instanceResourceID))
- s.syncSlowQuery(ctx, &instanceResourceID)
+ case message := <-s.stateCfg.InstanceSlowQuerySyncChan:
+ log.Debug("Slow query syncer received instance slow query sync request", zap.String("instance", message.InstanceID), zap.String("project", message.ProjectID))
+ s.syncSlowQuery(ctx, message)
case <-ticker.C:
log.Debug("Slow query syncer received tick")
s.syncSlowQuery(ctx, nil)
@@ -68,7 +68,7 @@ func (s *Syncer) Run(ctx context.Context, wg *sync.WaitGroup) {
}
}
-func (s *Syncer) syncSlowQuery(ctx context.Context, instanceResourceID *string) {
+func (s *Syncer) syncSlowQuery(ctx context.Context, message *state.InstanceSlowQuerySyncMessage) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
@@ -80,8 +80,10 @@ func (s *Syncer) syncSlowQuery(ctx context.Context, instanceResourceID *string)
}()
find := &store.FindInstanceMessage{}
- if instanceResourceID != nil {
- find.ResourceID = instanceResourceID
+ project := ""
+ if message != nil {
+ find.ResourceID = &message.InstanceID
+ project = message.ProjectID
}
instances, err := s.store.ListInstancesV2(ctx, find)
if err != nil {
@@ -97,7 +99,7 @@ func (s *Syncer) syncSlowQuery(ctx context.Context, instanceResourceID *string)
instanceWG.Add(1)
go func(instance *store.InstanceMessage) {
defer instanceWG.Done()
- if err := s.syncInstanceSlowQuery(ctx, instance); err != nil {
+ if err := s.syncInstanceSlowQuery(ctx, instance, project); err != nil {
log.Debug("Failed to sync instance slow query",
zap.String("instance", instance.ResourceID),
zap.Error(err))
@@ -107,7 +109,7 @@ func (s *Syncer) syncSlowQuery(ctx context.Context, instanceResourceID *string)
instanceWG.Wait()
}
-func (s *Syncer) syncInstanceSlowQuery(ctx context.Context, instance *store.InstanceMessage) error {
+func (s *Syncer) syncInstanceSlowQuery(ctx context.Context, instance *store.InstanceMessage, project string) error {
slowQueryPolicy, err := s.store.GetSlowQueryPolicy(ctx, api.PolicyResourceTypeInstance, instance.UID)
if err != nil {
return err
@@ -120,13 +122,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)
+ return s.syncPostgreSQLSlowQuery(ctx, instance, project)
default:
return errors.Errorf("unsupported database engine: %s", instance.Engine)
}
}
-func (s *Syncer) syncPostgreSQLSlowQuery(ctx context.Context, instance *store.InstanceMessage) error {
+func (s *Syncer) syncPostgreSQLSlowQuery(ctx context.Context, instance *store.InstanceMessage, project string) error {
today := time.Now().UTC().Truncate(24 * time.Hour)
earliestDate := today.AddDate(0, 0, -retentionCycle)
@@ -135,9 +137,13 @@ func (s *Syncer) syncPostgreSQLSlowQuery(ctx context.Context, instance *store.In
return err
}
- databases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{
+ findDatabases := &store.FindDatabaseMessage{
InstanceID: &instance.ResourceID,
- })
+ }
+ if project != "" {
+ findDatabases.ProjectID = &project
+ }
+ databases, err := s.store.ListDatabases(ctx, findDatabases)
if err != nil {
return err
}
diff --git a/backend/server/server.go b/backend/server/server.go
index 6247549612..1842ddd43d 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -338,7 +338,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
s.stateCfg = &state.State{
InstanceDatabaseSyncChan: make(chan *store.InstanceMessage, 100),
- InstanceSlowQuerySyncChan: make(chan string, 100),
+ InstanceSlowQuerySyncChan: make(chan *state.InstanceSlowQuerySyncMessage, 100),
InstanceOutstandingConnections: make(map[int]int),
IssueExternalApprovalRelayCancelChan: make(chan int, 1),
}