diff --git a/packages/functions-runtime/src/database.js b/packages/functions-runtime/src/database.js index ff76ecdc5..3f9f06b74 100644 --- a/packages/functions-runtime/src/database.js +++ b/packages/functions-runtime/src/database.js @@ -145,6 +145,19 @@ function getDialect() { return new PostgresDialect({ pool: new InstrumentedPool({ Client: InstrumentedClient, + // Increased idle time before closing a connection in the local pool (from 10s default). + // Establising a new connection on (almost) every functions query can be expensive, so this + // will reduce having to open connections as regularly. https://node-postgres.com/apis/pool + // + // NOTE: We should consider setting this to 0 (i.e. never pool locally) and open and close + // connections with each invocation. This is because the freeze/thaw nature of lambdas can cause problems + // with long-lived connections - see https://github.com/brianc/node-postgres/issues/2718 + // Once we're "fully regional" this should not be a performance problem anymore. + // + // Although I doubt we will run into these freeze/thaw issues if idleTimeoutMillis is always shorter than the + // time is takes for a lambda to freeze (which is not a constant, but could be as short as several minutes, + // https://www.pluralsight.com/resources/blog/cloud/how-long-does-aws-lambda-keep-your-idle-functions-around-before-a-cold-start) + idleTimeoutMillis: 120000, connectionString: mustEnv("KEEL_DB_CONN"), }), }); diff --git a/runtime/actions/create.go b/runtime/actions/create.go index 110bd51fb..d8904035a 100644 --- a/runtime/actions/create.go +++ b/runtime/actions/create.go @@ -38,13 +38,6 @@ func Create(scope *Scope, input map[string]any) (res map[string]any, err error) case !canResolveEarly: err = database.Transaction(scope.Context, func(ctx context.Context) error { scope := scope.WithContext(ctx) - query := NewQuery(scope.Model) - - // Generate the SQL statement - statement, err := GenerateCreateStatement(query, scope, input) - if err != nil { - return err - } // Execute database request, expecting a single result res, err = statement.ExecuteToSingle(scope.Context) diff --git a/runtime/actions/get.go b/runtime/actions/get.go index 0b0bb90ce..332e1b8ea 100644 --- a/runtime/actions/get.go +++ b/runtime/actions/get.go @@ -17,9 +17,8 @@ func Get(scope *Scope, input map[string]any) (map[string]any, error) { return nil, common.NewPermissionError() } - query := NewQuery(scope.Model) - // Generate the SQL statement + query := NewQuery(scope.Model) statement, err := GenerateGetStatement(query, scope, input) if err != nil { return nil, err diff --git a/runtime/actions/list.go b/runtime/actions/list.go index 4a93d5484..f0a9cd27f 100644 --- a/runtime/actions/list.go +++ b/runtime/actions/list.go @@ -121,9 +121,8 @@ func List(scope *Scope, input map[string]any) (map[string]any, error) { return nil, common.NewPermissionError() } - query := NewQuery(scope.Model) - // Generate the SQL statement. + query := NewQuery(scope.Model) statement, page, err := GenerateListStatement(query, scope, input) if err != nil { return nil, err