Skip to content

Persistent prepared statements - #1204

Closed
roji wants to merge 2 commits into
npgsql:devfrom
roji:persist-prepared
Closed

roji wants to merge 2 commits into
npgsql:devfrom
roji:persist-prepared

Conversation

@roji

@roji roji commented Jul 17, 2016

Copy link
Copy Markdown
Member

Implements #483

@MarkusSintonen

Copy link
Copy Markdown
Contributor

Hi @roji

There might be a resource leak at the PR. PersistentPreparedStatements dictionary at NpgsqlConnector does not seem to be cleaned properly. I think the PreparedStatementInfo should be removed from the dictionary when Unpersist()/ClosePreparedStatements() is used.

@roji
roji force-pushed the persist-prepared branch from 6ff849c to 88493de Compare July 18, 2016 12:25
@roji

roji commented Jul 18, 2016

Copy link
Copy Markdown
Member Author

@MarkusSintonen thanks for your useful review notes, have fixed. Like I said there's still a few things left I'd like to do, let me know if you run into any other problems.

@roji

roji commented Jul 18, 2016

Copy link
Copy Markdown
Member Author

And yes, there's now a test which catches the leak you found.

@MarkusSintonen

MarkusSintonen commented Jul 18, 2016 •

Copy link
Copy Markdown
Contributor

Good to hear that the comments were helpful. What you think what is the intended behaviour when user first calls Prepare(true) and after that Prepare(false) for the same command object. Should the already persisted command be freed? How about if user first calls Prepare(true) but afterwards for different command instance (with same SQL) Prepare(false), what is the intended behaviour here? Should behaviour for these cases also be covered by the unit tests?

Furthermore could it be allowed to unpersist commands without first Preparing them? For example user could free persisted command statements without first callind Prepare:

using (var cmd = new NpgsqlCommand("INSERT INTO data (first_name, last_name) VALUES ($1, $2)", conn))
{
    cmd.Prepare(true);
    // execute cmd etc.
}

// And somewhere else free the persistent command, without first calling Prepare
using (var cmd = new NpgsqlCommand("INSERT INTO data (first_name, last_name) VALUES ($1, $2)", conn))
{
    cmd.Unpersist();
}

These were the last few thoughts about the new API.

Prepared statements can now be made to persist across pooled connection
Close/Open. This can provide a significant performance boost in
applications with short-lived connections and complex queries that take
long to plan.

Based on significant work by @MarkusSintonen in npgsql#1084 (thanks!)

Fixes npgsql#483
@roji

roji commented Jul 18, 2016

Copy link
Copy Markdown
Member Author

@MarkusSintonen, those are very good questions.

What you think what is the intended behaviour when user first calls Prepare(true) and after that Prepare(false) for the same command object. Should the already persisted command be freed?

I wondered about that for a bit. I ended up deciding that persistent statements should only be explicitly unpersisted by calling Unpersist. This seems consistent with other behavior: disposing the command or changing the CommandText unprepares the command, but doesn't unpersist it. So for your scenario, Prepare(false) wouldn't unpersist the command, but it would create a new non-persistent prepared statement. Have added a test for this.

It is possible for the Prepare(false) to actually not create a new non-persistent prepared statement, but rather to "bind" to the first persistent statement. This could be the behavior both if Prepare(false) is called on the same command instance, or on a completely different instance (this is your second question). I ended up not doing this because it seems it would complicate the command state, is a bit confusing and mostly just isn't worth the effort... if you're preparing the same SQL in your app, you should know enough to simply use Prepare(true) both times.

Furthermore could it be allowed to unpersist commands without first Preparing them?

I've modified the code to allow this. Internally Unpersist() simply calls Prepare(true).

Thanks again for all the comments! I'll wait a bit more before merging in case you think of something else.

@roji
roji force-pushed the persist-prepared branch from 88493de to 118c0a6 Compare July 18, 2016 21:41
/// </summary>
public void Unpersist()
{
// We want to allow users to unpersist without having to persist first

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause unnecessary command preparing and immediate deallocation on PostgreSQL in a case where user calls Unpersist on a command SQL that has not yet been prepared and persisted. Probably in this case the Unpersist should not do anything. Of course fixing this would require some logic changes with Prepare and Unpersist. Easiest would be to document the intended behaviour but I could also suggest a fix.

@roji roji Jul 19, 2016 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's true, but it doesn't seem very important. Remember this is just Unpersist, which is supposed to be called once in the application lifecycle mostly. Also, it's within the programmer's ability (and responsibility) to avoid this scenario by making sure they call Unpersist() only for SQL that has been persisted before. Even if they don't, the consequences are minimal (prepare/unprepare)...

@MarkusSintonen

Copy link
Copy Markdown
Contributor

Looks good to me

@roji

roji commented Jul 30, 2016

Copy link
Copy Markdown
Member Author

Merged via adf1c0b

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants