Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 24 additions & 8 deletions postgresql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -239,9 +240,16 @@ func (c *Config) connParams() []string {
params["sslrootcert"] = c.SSLRootCertPath
}

// Sort keys to ensure consistent DSN generation across multiple calls
keys := make([]string, 0, len(params))
for key := range params {
keys = append(keys, key)
}
sort.Strings(keys)

paramsArray := []string{}
for key, value := range params {
paramsArray = append(paramsArray, fmt.Sprintf("%s=%s", key, url.QueryEscape(value)))
for _, key := range keys {
paramsArray = append(paramsArray, fmt.Sprintf("%s=%s", key, url.QueryEscape(params[key])))
}

return paramsArray
Expand Down Expand Up @@ -285,6 +293,7 @@ func (c *Client) Connect() (*DBConnection, error) {

dsn := c.config.connStr(c.databaseName)
conn, found := dbRegistry[dsn]

if !found {

var db *sql.DB
Expand All @@ -305,12 +314,6 @@ func (c *Client) Connect() (*DBConnection, error) {
return nil, fmt.Errorf("error connecting to PostgreSQL server %s (scheme: %s): %s", c.config.Host, c.config.Scheme, errString)
}

// We don't want to retain connection
// So when we connect on a specific database which might be managed by terraform,
// we don't keep opened connection in case of the db has to be dropped in the plan.
db.SetMaxIdleConns(0)
db.SetMaxOpenConns(c.config.MaxConns)

defaultVersion, _ := semver.Parse(defaultExpectedPostgreSQLVersion)
version := &c.config.ExpectedVersion
if defaultVersion.Equals(c.config.ExpectedVersion) {
Expand All @@ -327,6 +330,19 @@ func (c *Client) Connect() (*DBConnection, error) {
c,
*version,
}

if conn.featureSupported(featureForceDropDatabase) {
// Connections can be forcefully closed when dropping a database.
db.SetMaxIdleConns(c.config.MaxConns)
} else {
// We don't want to retain connection
// So when we connect on a specific database which might be managed by terraform,
// we don't keep opened connection in case of the db has to be dropped in the plan.
db.SetMaxIdleConns(0)
}

db.SetMaxOpenConns(c.config.MaxConns)

dbRegistry[dsn] = conn
}

Expand Down
3 changes: 2 additions & 1 deletion postgresql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package postgresql
import (
"context"
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/sts"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
Expand Down