Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop connections before drop database #14

Merged
merged 9 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
testing
  • Loading branch information
p4cket committed Dec 6, 2020
commit 723c56f9589971f4360a69f5dbbb91dca5b81702
26 changes: 14 additions & 12 deletions postgresql/resource_postgresql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,7 @@ func resourcePostgreSQLDatabaseDelete(d *schema.ResourceData, meta interface{})
return err
}

//block new connections to database
if c.featureSupported(featureDBAllowConnections) {
if err := setDBAllowConns(c, d); err != nil {
return fmt.Errorf("Error updating database AllowConnections during DROP DATABASE: %w", err)
}
}

//terminate all connections to database
// Terminate all active connections and block new one
if err := terminateBConnections(c, dbName); err != nil {
p4cket marked this conversation as resolved.
Show resolved Hide resolved
return err
}
Expand Down Expand Up @@ -562,10 +555,19 @@ func doSetDBIsTemplate(c *Client, dbName string, isTemplate bool) error {
}

func terminateBConnections(c *Client, dbName string) error {
sql := fmt.Sprintf("SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname '%s' AND pid <> pg_backend_pid();", pq.QuoteIdentifier(dbName))
if _, err := c.DB().Exec(sql); err != nil {
return fmt.Errorf("Error updating database IS_TEMPLATE: %w", err)
if c.featureSupported(featureDBAllowConnections) {
alterSql := fmt.Sprintf("ALTER DATABASE %s ALLOW_CONNECTIONS false", pq.QuoteIdentifier(dbName))

if _, err := c.DB().Exec(alterSql); err != nil {
return fmt.Errorf("Error blocking connections to database: %w", err)
}
}

terminateSql := fmt.Sprintf("SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '%s' AND pid <> pg_backend_pid()", dbName)

if _, err := c.DB().Exec(terminateSql); err != nil {
return fmt.Errorf("Error terminating database connections: %w", err)
}

return nil
}
}
6 changes: 3 additions & 3 deletions postgresql/resource_postgresql_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ JOIN pg_namespace ON pg_namespace.oid = pg_proc.pronamespace
LEFT JOIN (
select acls.*
from (
SELECT proname, prokind, pronamespace, (aclexplode(proacl)).* FROM pg_proc
SELECT proname, pronamespace, (aclexplode(proacl)).* FROM pg_proc
) acls
JOIN pg_roles on grantee = pg_roles.oid
WHERE rolname = $1
) privs
USING (proname, pronamespace, relkind)
WHERE nspname = $2 AND relkind = $3
USING (proname, pronamespace)
WHERE nspname = $2
GROUP BY pg_proc.proname
`
default:
Expand Down