Skip to content

Setting CommandTimeout on NpgsqlCommand with pgbouncer causes dropped connection #630

Closed

Description

@oldsaltycrab

Hello, I'm on the release-3.0.0 branch (c5f2caa).

As noted in #616, setting the CommandTimeout value in the connection string while connecting via pgbouncer does not work due to an issue with pgbouncer. I've been experimenting amongst the different ways to work around that and came across an issue.

With pgbouncer set to transaction mode, if I keep CommandTimeout in the connection string set to 0, but set the CommandTimeout property on the NpgsqlCommand itself to a value, the server drops my connection.

If I don't set the CommandTimeout property and instead prepend my statement with SET statement_timeout=10000, this works properly (which seems to be close to what npgsql does internally with the CommandTimeout property anyway?)

This also works with pgbouncer set to session mode.

The postgres logs will show this:

2015-06-01 22:22:37.141 29876 WARNING S-0x2349a98: TestDB/[email protected]:5432 got packet '1' from server when not linked   <------------------------- ERROR
2015-06-01 22:22:37.357 29876 DEBUG loopcnt full
2015-06-01 22:22:37.357 29876 LOG S-0x2349a98: TestDB/[email protected]:5432 closing because: SV_IDLE server got dirty (age=76) <-------------------------- connection terminated

Hope this isn't a duplicate of #616, figured this was different given that this way of doing it doesn't use the startup packet.

Thanks

Activity

  1. roji commented on Jun 3, 2015

    @roji
    Member

    @oldsaltycrab, thanks for this info. That's strange behavior on pgbouncer's part. First, about the difference between your method (prepending set statement_timeout) vs. Npgsql's internal one (triggered by the CommandTimeout property)...

    When you send a command with multiple queries (A; B), Npgsql splits it on the semicolon, and sends the queries using the PostgreSQL extended protocol. The protocol messages sent for this case are: Parse1/Describe1/Bind1/Parse2/Describe2/Bind2/Execute1/Close1/Execute2/Close2/Sync, see the docs for an explanation of what each means.

    When you set the timeout via CommandTimeout, Npgsql prepends a simple protocol messages to the actual query: Query (timeout)/Parse/Describe/Bind/Execute/Sync. The reason we do this, rather than use the extended protocol as above, is efficiency: rather than the P/D/B/E chain which is designed for complicated things (e.g. parameters), we send a single light message to set the timeout.

    So it seems pgbouncer reacts differently based on whether the SET statement_timeout is in a simple message (it hangs), or whether it's in an extended message (it lets it through without an error).

    Now a much more important point... In transaction mode, pgbouncer uses a different physical connection every time you send a statement (except for transactions). This is why you're not allowed to set parameters: doing this changes the server state for a connection, which may then be used for an entirely different operation. In other words, even if setting statement_timeout seemed to work for you (when using the extended protocol), it would create total chaos in later queries: every time you execute some statement, the timeout would be determined "randomly", based on what statement_timeout was set to the last time this connection was used.

    To summarize, pgbouncer transaction mode is totally incompatible with any sort of parameter setting (as is documented). There's some weird behavior there regarding what happens when you do set it - sometimes the connection is dropped, sometime it seems OK - but it shouldn't ever be done.

    Let me know if this is clear and if I'm missing anything...

  2. roji commented on Jun 3, 2015

    @roji
    Member

    One more note on session mode... I have no idea why pgbouncer doesn't allow setting the statement_timeout parameter in the startup packet (at connection time) - this really shouldn't be a problem. However, in session mode it shouldn't be problematic to use the standard CommandTimeout property to set the time, and this would be faster thanks to the simple protocol used.

  3. oldsaltycrab commented on Jun 3, 2015

    @oldsaltycrab
    Author

    Thanks for the explanations @roji, makes sense!

  4. roji commented on Jun 3, 2015

    @roji
    Member

    OK great :) It's too bad pgbouncer has this quirky behavior... You're welcome to follow and vote pgbouncer/pgbouncer#52 for the connection dropping issue. I haven't opened an issue regarding pgbouncer not accepting statement_timeout in the startup packet, I suspect there's some reason there but you're welcome to open an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions