Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Recently I started learning about pgbouncer, but there are some things I do not understand about the parameter server_reset_query.

Question 1: What is the main purpose of server_reset_query?

Question 2: There are three modes of the pool_mode of pgbouncer: session ,transaction and statement mode. When the pool_mode is session, we usually set the values of
server_reset_query to 'DISCARD ALL'. When the pool_mode is transaction we usually set the values of server_reset_query to '',

I do not know the details about this, so can anybody explain this?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

Since pgbouncer is a connection pooler, it will reuse one actual connection to the database server for potentially many client connections. It has to make sure that whatever session state the first client created is reset when the next client gets the connection. Otherwise the first client might do something like SET statement_timeout = '5min', and that would apply to all subsequent clients who happen to be assigned that connection. To avoid that, pgbouncer issues the "server reset query" before handing out the server connection to a new client. As the documentation says

A good choice for Postgres 8.2 and below is:

server_reset_query = RESET ALL; SET SESSION AUTHORIZATION DEFAULT;

for 8.3 and above its enough to do:

server_reset_query = DISCARD ALL;

The main reason this setting exists is that the DISCARD ALL statement, which was invented for this very purpose, was not available before PostgreSQL 8.3.

The setting applies independently of the pool mode. Note that the client could issue a SET statement or something like that in any pool mode.

The documentation also says

When transaction pooling is used, the server_reset_query should be empty, as clients should not use any session features.

I think this is a faith-based approach. If you think your code is clean and you need the extra performance, go for it. I've been bitten by this, though. For example, if you use check_postgres to monitor your pgbouncer connections, it will issue a SET statement_timeout like I showed above and mess up your connections.

share|improve this answer
    
thanks for your patience, According to your explanation , I unserstand a little。 –  francs Dec 9 '11 at 8:17
    
yes I also think pgbouncer docs should replace the "should, as" phrase with "can, if". this question keeps repeating on lists, irc etc. ... –  filiprem Dec 13 '11 at 17:28
    
Using check_postgres, it should connect directly to the database cluster, and not through the connection pooler. You should have a separate check for the connection pooler itself, something like check_pgbouncer. –  François Beausoleil Dec 6 '12 at 12:43
    
@FrançoisBeausoleil: I disagree. I want to check the connection as the client sees it, and the fact that it's pgbouncer should be transparent to the monitoring software. –  Peter Eisentraut Dec 6 '12 at 19:56

I have googled and yahooed around and came up with this brief definition:

server_reset_query

Query sent to server on connection release, before making it available to other clients. At that moment no transaction is in progress so it should not include ABORT or ROLLBACK.

Just from the name of the parameter alone, server_reset_query, it is a query that resets the server in some way. From the definitions and examples I have seen in google, yahoo and postgresql.org, it seems to be a directive that issues one or more queries for recycling connections and associated resources.

IMHO there does not seem to be a lot of explanation behind this directive except to see examples of its usage. Such examples include:

server_reset_query = DISCARD ALL;
server_reset_query = RESET ALL; SET SESSION AUTHORIZATION DEFAULT;

Look over as many examples of this setting as you can find. Take note of the queries attached to server_reset_query and find out what those individual queries do. Then, the purpose of server_reset_query will become more clear to you.

More example available here : http://pgbouncer.projects.postgresql.org/doc/faq.html

If there are any users of pgBouncer out there in the DBA.SE, please chime. I am a only nominal PostgreSQL DBA myself.

share|improve this answer
    
Thanks for your explanation, I read the docment about pgbouncer, But I still don't understand the parameter well. I think later I shoud do some tests about this。 –  francs Dec 9 '11 at 2:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.