diff --git a/coderd/database/dbtestutil/db.go b/coderd/database/dbtestutil/db.go index d25f2508e40..711c76bb600 100644 --- a/coderd/database/dbtestutil/db.go +++ b/coderd/database/dbtestutil/db.go @@ -120,7 +120,7 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) { o.fixedTimezone = DefaultTimezone } dbName := dbNameFromConnectionURL(t, connectionURL) - setDBTimezone(t, connectionURL, dbName, o.fixedTimezone) + setDBSettings(t, connectionURL, dbName, o.fixedTimezone) sqlDB, err := sql.Open("postgres", connectionURL) require.NoError(t, err) @@ -145,10 +145,11 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) { return db, ps } -// setRandDBTimezone sets the timezone of the database to the given timezone. -// Note that the updated timezone only comes into effect on reconnect, so we -// create our own connection for this and close the DB after we're done. -func setDBTimezone(t testing.TB, dbURL, dbname, tz string) { +// setDBSettings sets the timezone of the database to the given timezone and +// disables JIT. Note that the updated settings only come into effect on +// reconnect, so we create our own connection for this and close the DB after +// we're done. +func setDBSettings(t testing.TB, dbURL, dbname, tz string) { t.Helper() sqlDB, err := sql.Open("postgres", dbURL) @@ -160,6 +161,14 @@ func setDBTimezone(t testing.TB, dbURL, dbname, tz string) { // nolint: gosec // This unfortunately does not work with placeholders. _, err = sqlDB.Exec(fmt.Sprintf("ALTER DATABASE %s SET TIMEZONE TO %q", dbname, tz)) require.NoError(t, err, "failed to set timezone for database") + + // The planner misestimates some queries so badly on near-empty test + // databases that they exceed jit_above_cost, and Postgres then spends + // hundreds of milliseconds LLVM-compiling them on every call. JIT is an + // execution optimization with no semantic effect, so turn it off. + // nolint: gosec // This unfortunately does not work with placeholders. + _, err = sqlDB.Exec(fmt.Sprintf("ALTER DATABASE %s SET jit = off", dbname)) + require.NoError(t, err, "failed to disable jit for database") } // dbNameFromConnectionURL returns the database name from the given connection URL,