Skip to content

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

fix: set max open conns for metaDB (#3752)

p0ny13c2b589c36a
diff --git a/store/pg_engine.go b/store/pg_engine.go
index cf1886e563..17c6429256 100644
--- a/store/pg_engine.go
+++ b/store/pg_engine.go
@@ -153,6 +153,22 @@ func (db *DB) Open(ctx context.Context) (err error) {
return errors.Wrapf(err, "failed to connect to database %q", db.connCfg.Username)
}
+ // Set the max open connections so that we won't exceed the connection limit of metaDB.
+ // The limit is the max connections minus connections reserved for superuser.
+ var maxConns, reservedConns int
+ if err := db.db.QueryRowContext(ctx, `SHOW max_connections`).Scan(&maxConns); err != nil {
+ return errors.Wrap(err, "failed to get max_connections from metaDB")
+ }
+ if err := db.db.QueryRowContext(ctx, `SHOW superuser_reserved_connections`).Scan(&reservedConns); err != nil {
+ return errors.Wrap(err, "failed to get superuser_reserved_connections from metaDB")
+ }
+ maxOpenConns := maxConns - reservedConns
+ if maxOpenConns > 50 {
+ // capped to 50
+ maxOpenConns = 50
+ }
+ db.db.SetMaxOpenConns(maxOpenConns)
+
if err := db.setupDemoData(); err != nil {
return errors.Wrapf(err, "failed to setup demo data."+
" It could be Bytebase is running against an old Bytebase schema. If you are developing Bytebase, you can remove pgdata"+