Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions coderd/database/dbtestutil/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand Down
Loading