DB queries do get pre compiled and cached if you use prepared statements. This is why you should always use prepared statements if you can.
[1]: https://pkg.go.dev/github.com/jackc/pgx/v5#hdr-Prepared_Stat...
[2]: https://www.psycopg.org/psycopg3/docs/advanced/prepare.html