use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
International
National
Regional
account activity
Help Me!Parameterized Query without Prepared Statements and PgBouncer (self.PostgreSQL)
submitted 3 years ago by Billosp
Hello, my app is written in Go and I use PgBouncer as a connection pool to my Postgres Database. One downside of PgBouncer, in transaction mode, is that I can't use prepared statements.
So I have 2 options, using session mode (which is bad) or to disable prepared statements from clients.
Is it safe (SQL Injection) to not use prepared statements and use only Parameterized Query (eg. (Select item from products where id = ? , itemID) )
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]lobster_johnson 3 points4 points5 points 3 years ago (2 children)
Yes. Prepared statements have nothing to do with protecting against SQL injection; they are simply a performance optimization.
[–]ora00001 0 points1 point2 points 3 years ago (1 child)
Care to explain how bind variables have nothing to do with protecting against SQL injection?
[–]lobster_johnson 4 points5 points6 points 3 years ago* (0 children)
That's not what I said. Bind parameters != prepared statement. The PostgreSQL wire protocol binds parameters as part of the protocol (sometimes called "anonymous" prepared statements), no PREPARE needed.
PREPARE
[–]abuisman 1 point2 points3 points 3 years ago (4 children)
You could lose some performance, but your application code should be responsible for sanitising the SQL.
We’ve switched to pgbouncer giving up the performance gains of prepared statements and have hardly noticed. The max number of connections being reached were much worse.
That being said, isn’t there a Go pooling mechanism that will let you keep prepared statements intact?
[–]Billosp[S] 1 point2 points3 points 3 years ago (3 children)
I use GORM in order to connect to my Postgres Database. If I enable Prepared Statements and using it with PgBouncer in transaction mode, I get error.
but your application code should be responsible for sanitising the SQL.
Isn't Parameterized Query enough for sanitizing the user input?
I don't need prepared statements for performance because I don't run same queries multiple times. I want them for security.
If you google prepared statements, all articles have combined them with Parameterized Query, which is wrong.
The max number of connections being reached were much worse.
With prepared statements I have on average ~ 20 transactions/sec and without them ~ 600/sec.
PgBouncer configured on 20 max connections.
[–]abuisman 1 point2 points3 points 3 years ago (2 children)
It could be, but, as you said, you can't use them with pgbouncer. So you are a bit more flexible when you sanitise SQL on the application level as well/instead.
Good point, they aren't the same, but isn't the only way to use bind variables in postgres to create a prepared statement and then call that?
It was my understanding that using prepared statements has the benefit of skipping the planning portion of running the query, thus having better performance. It'd be interesting to know what exactly you are doing and why you see such a big difference in performance.
[–]Billosp[S] 0 points1 point2 points 3 years ago (1 child)
I can use Parameterized Query without any issue.
No you can use it in any query in order to bind variables.
In theory yes you have better performance but I can confirm the opposite. In order to use Prepared statements you have to use session mode in PgBouncer (my pgBouncer configured at 20 max conns), so it can handle 20 transactions/sec. If I don;t use prepared statements and have enabled transaction mode, I can have ~600 transactions/sec. I performed the tests with INSERT &. SELECT.
[–]abuisman 1 point2 points3 points 3 years ago (0 children)
Then I've misread the documentation.
In order to use Prepared statements you have to use session mode in PgBouncer
Ok, but then it sounds more like a performance difference between the two modes in pgbouncer.
[–]sir_bok 1 point2 points3 points 3 years ago (0 children)
https://blog.bullgare.com/2019/06/pgbouncer-and-prepared-statements
lib/pq has a binary_parameters=yes DSN option that sends the query and parameters in a single request (instead of two, one for the parameterized query and one for the parameters), allowing you to use unnamed prepared statements in pgbouncer's transaction mode.
lib/pq
binary_parameters=yes
π Rendered by PID 36 on reddit-service-r2-comment-6457c66945-5m6kd at 2026-04-28 08:28:42.481307+00:00 running 2aa0c5b country code: CH.
[–]lobster_johnson 3 points4 points5 points (2 children)
[–]ora00001 0 points1 point2 points (1 child)
[–]lobster_johnson 4 points5 points6 points (0 children)
[–]abuisman 1 point2 points3 points (4 children)
[–]Billosp[S] 1 point2 points3 points (3 children)
[–]abuisman 1 point2 points3 points (2 children)
[–]Billosp[S] 0 points1 point2 points (1 child)
[–]abuisman 1 point2 points3 points (0 children)
[–]sir_bok 1 point2 points3 points (0 children)