Skip to content

Commit c3cb2b6

Browse files
committed
Fix idle clone check when user <> 'postgres'
If user is not `postgres` and we do not specify `dbname`, connection will be established to dbname=user. Which may fail if there is no such database. We can rely on the fact that DB `postgres` is present: > The postgres database is also created when a database cluster is initialized. -- from the official docs https://www.postgresql.org/docs/current/manage-ag-templatedbs.html.
1 parent d188491 commit c3cb2b6

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

pkg/services/cloning/mode_base.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,15 +561,17 @@ func hasNotQueryActivity(session *provision.Session) (bool, error) {
561561

562562
// TODO(akartasov): Move the function to the provision service.
563563
func getSocketConnStr(session *provision.Session) string {
564-
return fmt.Sprintf("host=%s user=%s", session.SocketHost, session.User)
564+
return fmt.Sprintf("host=%s user=%s dbname=postgres", session.SocketHost, session.User)
565565
}
566566

567567
// checkActiveQueryNotExists runs query to check a user activity.
568568
func checkActiveQueryNotExists(db *sql.DB) (bool, error) {
569569
var isRunningQueryNotExists bool
570570

571-
query := `SELECT NOT EXISTS(
572-
SELECT * FROM pg_stat_activity WHERE state NOT ILIKE 'idle%' AND query NOT LIKE 'autovacuum: %' AND pid <> pg_backend_pid())`
571+
query := `select not exists (
572+
select * from pg_stat_activity
573+
where state <> 'idle' and query not like 'autovacuum: %' and pid <> pg_backend_pid()
574+
)`
573575
err := db.QueryRow(query).Scan(&isRunningQueryNotExists)
574576

575577
return isRunningQueryNotExists, err

0 commit comments

Comments
 (0)