News for hackers. Stuff that matters.
Re:Costs of a PostgreSQL connection by AznHisoka on Wednesday February 19, 2014 @10:14AM (#7263984)
For some reason, Rails doesn't behave well with pgbouncer or any other pg connection pool. So I really have no choice but to set my max connections to 1900, and pray that it's enough. Surprisingly, my 48 GB RAM, quad core CPU, non SSD server can handle that load, although the hardware will probably degrade after a certain time (oh well shrugs)
Re:Costs of a PostgreSQL connection by _hans_ on Wednesday February 19, 2014 @10:52AM (#7264171)
We enabled PGBouncer transaction mode and had to set 'prepared_statements: false' in our database.yml b/c the transaction mode doesn't support that. Works fine for us.
Re:Costs of a PostgreSQL connection by gkop on Wednesday February 19, 2014 @11:34AM (#7264415)
What are the implications of 'prepared_statements: false'? It sounds really bad.
Re:Costs of a PostgreSQL connection by justincormack on Wednesday February 19, 2014 @12:12PM (#7264633)
Prepared statements are not a massive performance gain in postgres so I wouldn't worry.
Re:Costs of a PostgreSQL connection by nirvdrum on Wednesday February 19, 2014 @02:32PM (#7265661)
They do provide a nice safety net against SQL injection though. A problem that Rails has from time to time.
Re:Costs of a PostgreSQL connection by zzzeek on Wednesday February 19, 2014 @03:04PM (#7265852)
bound parameters are the safety net against SQL injection. Prepared statements are not a prerequisite for that depending on database client API. (e.g. psycopg2, doesn't use prepared statements).
Re:Costs of a PostgreSQL connection by nirvdrum on Wednesday February 19, 2014 @04:23PM (#7266334)
I appreciate the comment, but since this was in regards to Rails, the two options were basically use prepared statements or don't. You don't get any safety without them and have to rely on Rails, which does an admirable job, but has faltered from time to time.
Re:Costs of a PostgreSQL connection by saurik on Wednesday February 19, 2014 @06:54PM (#7267576)
If Rails stops using the two-step parse/execute PostgreSQL commands (which are supported without issue by PGbouncer, and which is how you are supposed to get bound parameters) when prepared_statements is turned off (a setting which intuitively should only turn off using standard SQL named prepared statements), then that's pathetic behavior (even for Rails) and should really be fixed.
Re:Costs of a PostgreSQL connection by nousernamesleft on Wednesday February 19, 2014 @02:29PM (#7265641)
If done right they can be huge. The fact that they do nothing when you just do "prepare" followed immediately by "execute" in place of just running the query directly is not a problem with postgresql or prepared statements, it is a problem of doing something dumb. For simple selects the parsing and planning stage can be 90% of the time spent. The problem is that postgresql doesn't have stored procedures, so you have to make sure every connection is initialized by running all of your "prepare"s before it is made available to the pool.
Re:Costs of a PostgreSQL connection by dbenhur on Wednesday February 19, 2014 @05:25PM (#7266815)
You can get detailed timing data about parse, plan, execute timings from postgresql logging. Parsing is almost always trivial. Planning can get expensive for complex queries with many joins (large space of solutions to explore). For simple queries there's almost no benefit for prepared statements in postgres. Prepared statements are a maintenance headache and don't play well with session multiplexed connection pooling (like PgBouncer); generally best to avoid them unless you have measured a concrete and significant benefit.
Re:Costs of a PostgreSQL connection by nousernamesleft on Wednesday February 19, 2014 @06:57PM (#7267612)
>You can get detailed timing data about parse, plan, execute timings from postgresql logging. Parsing is almost always trivial.

And yet you can get >50% speedups for super common queries like "select * from foo where id = ?" if you prepare it once and then use execute instead of just running the select every time. Seems like maybe you're making assumptions you shouldn't.

Re:Costs of a PostgreSQL connection by justincormack on Wednesday February 19, 2014 @05:42PM (#7266996)
I think the statements I benchmarked were more complex and slower, so there wasnt much in it. It was a while ago though. For simple selects it could make much more difference.
Re:Costs of a PostgreSQL connection by stefantalpalaru on Wednesday February 19, 2014 @04:04PM (#7266208)
> The problem is that postgresql doesn't have stored procedures

Of course it has: http://www.postgresql.org/docs/9.3/static/sql-createfunction...

And you can choose between 4 languages: http://www.postgresql.org/docs/9.3/interactive/xplang.html

Re:Costs of a PostgreSQL connection by nousernamesleft on Wednesday February 19, 2014 @04:31PM (#7266373)
Those are functions, not stored procedures. Every time that function is executed, it is re-parsed and re-planned.
Re:Costs of a PostgreSQL connection by chanks on Wednesday February 19, 2014 @05:41PM (#7266984)
PL/pgSQL function plans are cached. Functions written in other languages may not be, I don't know, but in my experience PL/pgSQL is the most widely used.

http://www.postgresql.org/docs/9.3/static/plpgsql-implementa...

Re:Costs of a PostgreSQL connection by nousernamesleft on Wednesday February 19, 2014 @06:54PM (#7267574)
>PL/pgSQL function plans are cached

Read your link. They can be cached. You have no way to ensure that they are. And you have no way to have the planning done up front at all, you have to wait until the function is run, and even then every code path in the function has to be exercised to get it planned. And then half the time it doesn't think it should bother to cache the plan anyways. And it is per-session, not shared. So every single connection has to start over with an empty cache every time, and there's tons of duplicate plans in the cache wasting space. Not cool, and by far the biggest thing keeping a ton of "enterprise" customers on SQL server and oracle.

Re:Costs of a PostgreSQL connection by chanks on Wednesday February 19, 2014 @07:35PM (#7267850)
You have some valid criticisms of the implementation, though pgsql-hackers has their reasons (http://www.postgresql.org/message-id/CAHyXU0ybwZZUbuQQVFQMK3...) for the way things are. Regardless, my point is that the situation is not so bad as "Every time that function is executed, it is re-parsed and re-planned."
Re:Costs of a PostgreSQL connection by dbenhur on Wednesday February 19, 2014 @05:20PM (#7266751)
The default stored procedure language is PL/pgSQL http://www.postgresql.org/docs/9.3/static/plpgsql.html and has been available in Postgres almost forever.
Re:Costs of a PostgreSQL connection by nousernamesleft on Wednesday February 19, 2014 @06:54PM (#7267578)
Those are functions, not stored procedures.
Re:Costs of a PostgreSQL connection by _hans_ on Wednesday February 19, 2014 @12:24PM (#7264722)
There was no apparent problem with turning it off for us. But maybe there is for others.
Re:Costs of a PostgreSQL connection by tbarbugli on Wednesday February 19, 2014 @11:53AM (#7264525)
dont prepare statements?