title: PostgreSQL keeps connections made by Go open
body: <p>Using the Go cron package I run the following code every second:</p>
  
  <pre><code>func dbPGConnectionCount(db *sql.DB) (int64, error) {
      var connectionCount int64
      rows, err := db.Query(`SELECT count(1) from pg_stat_activity;`)
      if err != nil {
          return 0, err
      }
      defer rows.Close()
      for rows.Next() {
          err = rows.Scan(&amp;connectionCount)
          if err != nil {
              return 0, err
          }
      }
      return connectionCount, err
  }
  </code></pre>
  
  <p>where <code>db</code> is a connection pool for connections to PostgreSQL.</p>
  
  <p>Thanks to the <code>defer</code> statement I expected that PostgreSQL wouldn't keep any connections open. However, soon after starting my program, when I check with <code>select * from pg_stat_activity</code> I see new idle queries being added every second.</p>
  
  <p>What am I doing wrong?</p>