I'm seeing higher than expected latency when I query postgres from my Go application.
Below is a working example, using a publicly accessible DB.
When I run the below code, I get a response in 800ms. When I run the same query directly in my sql editor, the response is 120 ms. I'm not sure what is causing such a large difference.
I'm fairly new to GO, so I'm guessing that I'm missing something fundamental, but I've tried quite a few things and can't figure out the cause of the latency.
I tried to follow the PGX 'todo' example for my queries. https://github.com/jackc/pgx/blob/master/examples/todo/main.go
Anyone have ideas on what I should be doing differently?
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/jackc/pgx/v4"
)
func main() {
//these are public credentials: https://rnacentral.org/help/public-database
urlString := "postgres://reader:NWDMCE5xdipIjRrp@hh-pgsql-
public.ebi.ac.uk:5432/pfmegrnargs"
conn, err := pgx.Connect(context.Background(), urlString)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var upi string
err = conn.QueryRow(context.Background(), "SELECT upi FROM xref LIMIT 1").Scan(&upi)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(upi)
}