From 353879f5d12b2fe674d31bd0f329c3d2a73d2eff Mon Sep 17 00:00:00 2001 From: Michael Suchacz <203725896+ibetitsmike@users.noreply.github.com> Date: Thu, 17 Sep 2026 07:24:15 +0000 Subject: [PATCH] test(coderd/database/dbtestutil): disable Postgres JIT for test databases On near-empty test databases the planner misestimates some queries so badly that they exceed jit_above_cost, and Postgres LLVM-compiles them on every call. GetUserStatusCounts alone spent about 275ms of its 290ms execution in JIT. JIT is an execution optimization with no semantic effect, so disable it per test database next to the existing timezone setting. --- coderd/database/dbtestutil/db.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) 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,