Skip to content

Commit

Permalink
DB connections: Don't limit to 30 seconds lifetime to avoid timeout bugs
Browse files Browse the repository at this point in the history
Some time ago (at least 9+ years) we introduced a maximum connection
lifetime of 30 seconds via the SetConnMaxLifetime database/sql function.

This was likely intended as an extra safety measure in case the collector
didn't clean up its DB handle correctly. We've had a few minor issues
with incorrect cleanups over the years (though usually not with the DB
connection), but at this point our connection cleanup should be reliable.

The connection lifetime causes an issue because it causes Go to
reconnect to the database after 30 seconds have elapsed. This then
effectively removes any connection local settings, in particular
a statement_timeout.

Effectively this meant that queries may unintentionally not have been
subject to the statement_timeout, if the same connection previously took
30 seconds to do its work. To resolve, remove the max lifetime.
  • Loading branch information
lfittl committed Jan 6, 2025
1 parent 4ae7b1c commit 72d65e7
Showing 1 changed file with 0 additions and 2 deletions.
2 changes: 0 additions & 2 deletions input/postgres/establish_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"fmt"
"time"

"github.com/aws/aws-sdk-go/service/rds/rdsutils"
"github.com/pganalyze/collector/config"
Expand Down Expand Up @@ -78,7 +77,6 @@ func connectToDb(ctx context.Context, config config.ServerConfig, logger *util.L
}

db.SetMaxOpenConns(1)
db.SetConnMaxLifetime(30 * time.Second)

err = db.PingContext(ctx)
if err != nil {
Expand Down

0 comments on commit 72d65e7

Please sign in to comment.