title: How to pass parameters to a query written in PL/pgSQL?
body: <p>I wonder if it's possible to pass parameters to a query written in PL/pgSQL?</p>
  
  <p>I tried this, but it failed with <code>pq: got 1 parameters but the statement requires 0</code></p>
  
  <pre><code>package main
  
  import (
      "database/sql"
      "fmt"
      "log"
  
      _ "github.com/lib/pq"
  )
  
  func main() {
      db, err := sql.Open("postgres", "host=localhost dbname=db user=user sslmode=disable password=pw")
      if err != nil {
          log.Fatal(err)
      }
      row := db.QueryRow(`
  DO $$
  BEGIN
      IF true THEN
          SELECT $1;
      END IF;
  END$$
  `, 1)
      var num int
      err = row.Scan(&amp;num)
      if err != nil {
          log.Fatal(err)
      }
      fmt.Println(num)
  }
  </code></pre>
  
  <p>Another related question is that I want to use transactions, but the APIs provided by the <code>sql</code> package seems to connect to db every time a query is executed in a tx. I'd like everything to be executed in one go if that's possible. For example, with go you are supposed to use transactions like this</p>
  
  <pre><code>tx, err := db.Begin()
  rows, err := tx.Query(sql1)
  result, err := tx.Exec(sql2)
  tx.Commit()
  </code></pre>
  
  <p>The problem is that calling <code>tx.Query</code> and <code>tx.Exec</code> makes two trips to PostgreSQL server if I'm not wrong. What I want to achieve is to merge <code>sql1</code> and <code>sql2</code>, wrap them inside <code>BEGIN</code> and <code>END</code> and execute them in one trip. And my question is that:</p>
  
  <ol>
  <li>Do you think it's necessary? I imagine that with enough traffic, the performance difference might be obvious, but I'm not sure.</li>
  <li>If so, what's the best way to execute this merged transaction? Create a function and run the transaction inside PL/pgSQL (since I might need to use conditional statements, etc)?</li>
  </ol>