diff --git a/internal/collector/collector_common.go b/internal/collector/collector_common.go index 8176d33..9e81998 100644 --- a/internal/collector/collector_common.go +++ b/internal/collector/collector_common.go @@ -267,11 +267,9 @@ func updateFromMultipleDatabases(config Config, descSets []typedDescSet, ch chan err = updateSingleDescSet(conn, s, ch, true) if err != nil { log.Errorf("collect failed: %s; skip", err) - conn.Close() - continue } - // Close connection. + // Close connection and go next database. conn.Close() } } diff --git a/internal/collector/config.go b/internal/collector/config.go index 60ef779..95a8121 100644 --- a/internal/collector/config.go +++ b/internal/collector/config.go @@ -198,6 +198,7 @@ func discoverPgStatStatements(connStr string) (bool, string, string, error) { var setting string err = conn.Conn().QueryRow(context.Background(), "SELECT setting FROM pg_settings WHERE name = 'shared_preload_libraries'").Scan(&setting) if err != nil { + conn.Close() return false, "", "", err } @@ -238,10 +239,11 @@ func discoverPgStatStatements(connStr string) (bool, string, string, error) { // If pg_stat_statements found, update source and return connection. if schema := extensionInstalledSchema(conn, "pg_stat_statements"); schema != "" { + conn.Close() return true, conn.Conn().Config().Database, schema, nil } - // Otherwise close connection and go to next database in the list. + // Otherwise, close connection and go to next database in the list. conn.Close() } diff --git a/internal/collector/postgres_statements.go b/internal/collector/postgres_statements.go index a2d95fc..5323394 100644 --- a/internal/collector/postgres_statements.go +++ b/internal/collector/postgres_statements.go @@ -196,14 +196,14 @@ func (c *postgresStatementsCollector) Update(config Config, ch chan<- prometheus return err } + defer conn.Close() + // get pg_stat_statements stats res, err := conn.Query(selectStatementsQuery(config.serverVersionNum, config.pgStatStatementsSchema)) if err != nil { return err } - conn.Close() - // parse pg_stat_statements stats stats := parsePostgresStatementsStats(res, []string{"user", "database", "queryid", "query"})