Skip to content

Do PREPARED-Statements make any sense with the current implementation of NpgsqlConnection? #434

Closed

Description

@dekr

As you can read on the PostgreSQL website

http://www.postgresql.org/docs/9.3/static/sql-prepare.html

PREPARED statements are lost after executing DEALLOCATE. If I understand correctly, DEALLOCATE is executed when connection is closed/disposed or moved to the Npgsql connection pool.

However, in the most cases Npgsql is used for websites and connections are very short-living (1-3 commands).

So I suggest that supporting PREPARED statements does not provide any benefits and just makes the Npgsql-code more complicated.

Activity

  1. Emill commented on Dec 19, 2014

    @Emill
    Contributor

    Well, that might be true. But it is the same for all ADO.NET providers. We can't "leak" state between connections.
    I think the PostgreSQL backend itself should have a better system for reusing queries between connections. All other PostgreSQL providers for different languages probably have the same problem.
    The thing you can do is have connections that are not created and destroyed for every web request, if the performance is very important.

    MS SQL "solves" this by having a better query plan cache that lasts between connections.

    Note that there are still non-websites that uses long-lasting connections that benefit from prepared statements.

  2. roji commented on Dec 20, 2014

    @roji
    Member

    I agree with @Emill - and at the end of the day not everyone is a website with very short connection times - in many cases connections are long-lived and prepared statements there are crucial.

    We could in theory think about a special mechanism for not deallocating prepared statements when they are returned to the pool. One way to do this, is to track which prepared statements are open on which connection, and if a user tries to prepare query text which has already been prepared - reuse that. It doesn't seem very hard to implement, what do you think @Emill?

  3. franciscojunior commented on Dec 24, 2014

    @franciscojunior
    Member

    I looked for that in postgresql and found this thread from 2008:
    http://postgresql.nabble.com/Cached-Query-Plans-was-global-prepared-statements-td1981260.html

    It seems people from php would like to have something like that with support from the server itself. But from the discussion this feature wasn't implemented :(

    One of the server side implementation ideas is exactly what @roji said: hash the query (without the parameters) and check if it is already prepared.

  4. roji commented on Feb 12, 2015

    @roji
    Member

    Opened #483 for keeping prepared statements across connections.

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