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?


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:


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.

  • 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. 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. Dec 6 '12 at 19:56
  • 1
    One thing that's worth noting is that since pgbouncer version 1.6.1 there's a new setting server_reset_query_always. From the changelog: " When set, disables server_reset_query use on non-session pools." As of 1.7 this defaults to off, so transaction pools will no longer send the reset query by default. This just caught me out on upgrading pgbouncer. Feb 5 '16 at 11:59

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


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;

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.

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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