Friday, June 28, 2013

Npgsql 2.0.13 beta1 released!

Today we released the first beta of Npgsql 2.0.13! 

This new beta release had a lot of bugs fixed and initial support for Entity Framework 6! More information about how to use Entity framework 6 with Npgsql can be found in this post.

Checkout the release notes for more information about the bugs fixed in this release.

Download it from our downloads section.

Important notice about this release

Unfortunately, I made a mistake when updating the assembly version for this release and it was created with a wrong value. I'm very sorry for that.

This release should have been and not

Next beta release will have the version value fixed.

What are the implications? 

The biggest problem is that this beta version will have a version number higher than the final 2.0.13 version while this beta version has As it is a beta and it is not supposed to be deployed in production systems, we think this won't give problems to our users. We are very sorry for any problem this may have caused.

How is Npgsql assembly versioned?

In the beginning, we used to use the same assembly version througout all the beta release cycle until the final release.
With this approach, Npgsql 2.0.10 beta1 would have the assembly version of and beta2 would also have the assembly version of This was creating confusion between users because they couldn't differentiate which version they were working with.

Starting with 2.0.11, we changed this in order to identify each version. We started to use the last two parts of the assembly version in order to identify beta releases. 

This way, a beta release was identified in the following form:

  • Npgsql 2.0.11 beta1 would be identified as:;
  • Npgsql 2.0.11 beta2 would be identified as and so on. 
With this schema, the beta versions will be incremented towards the final version 

Regarding this 2.0.13 beta1 release, its assembly version should have been and not The former value indicates the assembly is approaching the 2.0.13 while the latter indicates the assembly is approaching 2.0.14 which is not the case.

We hope this won't happen again. :(

Thursday, June 27, 2013

Performance improvements when creating NpgsqlConnection objects

Recently, I applied a patch from Kevin Pullin which will improve the performance of programs using Npgsql. This patch reduces significantly the time to create new NpgsqlConnection objects. This particularly applies in scenarios where you are creating and disposing a lot of NpgsqlConnection objects, like when you are using connection pool, ( you are using it, right? :) ).

Comparison test

I made an artificial test to show the impact of this patch. This test consists of a simple loop where I create 10k NpgsqlConnection objects. 

    class Program
        static void Main(string[] args)
            var connString = "server=;userid=npgsql_tests;database=npgsql_tests;";
            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0i < 10000i++)
                var conn = new NpgsqlConnection(connString);

These are the results 

Code without patch:

  • 1st run: 1316 ms
  • 2nd run: 1333 ms
  • 3rd run: 1310 ms
  • Average time: 1319 ms

Code with patch:

  • 1st run: 33 ms
  • 2nd run: 39 ms
  • 3rd run: 33 ms
  • Average time: 35 ms

The new code, on this test, was more than 30 times faster! Of course this doesn't mean your code will be 30 times faster, after all your code doesn't consist of only creating NpgsqlConnection objects, but imagine a high traffic server which receives a lot of requests. When you sum up all the time spent creating NpgsqlConnection objects, this performance gain would make a difference.

Please, give it a try and let me know how it works for you. Just go to Npgsql git page and press the "Download ZIP" button and get a snapshot of the code. Open the Npgsql2010.sln solution file, build and test! 

Again, thank you very much, Kevin, for your patch!

Monday, June 17, 2013

Initial EF-6 support added to Npgsql

In my last post, I said there is a pull request by Pēteris Ņikiforovs to add support for EF-6 to Npgsql. Yesterday, I merged this pull request to the master branch of Npgsql.

With this merge, Npgsql has officially initial support for EF-6!

How to compile

For now, in order to compile Npgsql to use EF-6, you have to open the solution file NpgsqlEF6.sln. Later, as suggested by Pēteris Ņikiforovs, the idea is that we create a new configuration inside main project solution instead of maintain 2 separated projects.

Another thing you will need to compile Npgsql is the latest release of EntityFramework assembly through NuGet:

PM> Install-Package EntityFramework -Pre

That's it! Now you will be able to play with Npgsql and EF-6. Check out my previous post about how to use Npgsql with EntityFramework.

I'd like to thank Pēteris Ņikiforovs for his patch. And maxbundchen for his patch about Open/Close events needed for EF-6.

Please, give it a try and let me know how it works for you.

Saturday, June 08, 2013

Npgsql Code First Entity Framework 4.3.1 Sample

After reading the excellent article about entity framework on Postgresql by Brice Lambson, I decided to write this post to document my experience playing with Entity Framework 4.3.1 and Npgsql. This post will be an adaptation of the Code First To a New Database walkthrough in order to make it work with Npgsql. 

First Steps

You should follow the first 4 steps of Code First To a New Database. Go ahead, I''l wait for you.

Next steps

Here is where the adaptation of the walkthrough begins. As Brice noted in his post, Npgsql currently doesn't support database creation. ( I'm working on that and hope to get news about it soon.) So, for while, you have to create the database manually.

Those are the steps you have to do to create the database and the model:

First, run this command in the terminal to create the database (or you can use pgAdmin if you prefer a GUI):

> createdb ef_code_first_sample 

After that, you have to run the following commands inside the database you have just created (to simplify permissions, remember to run this script connected as the same user who is specified in your connection string):

create table "Blog" ("BlogId" serial, "Name" varchar(255));
create table "Post" ("PostId" serial, "Title" varchar(255), "Content" varchar(8000), "BlogId" int);

And here comes the first trick you have to use when working with EF and Npgsql: the table names as well as column names need to be double quoted

Entity Framework generates code with table and column names double quoted and, to Postgresql, using double quotes means you want to preserve the casing of the names. So you need to create the tables with the correct case or else, Postgresql will complain it can't find your tables.

With the database and tables created, let's make some more configuration before we can run the sample.

Entity Framework installation

Unfortunately Npgsql doesn't support EF 5. Currently it supports 4.3.1 and there is a pull request by Pēteris Ņikiforovs to add support for EF 6. Yeah, Npgsql will have support for latest EF version soon!

You will need to install the 4.3.1 version of EF. According to EF Nuget project page, this is done with the following command:

PM> Install-Package EntityFramework -Version 4.3.1

This is needed because if you don't specify the 4.3.1 version, Nuget will install the latest version which isn't supported by Npgsql yet.

Npgsql installation

If you don't have Npgsql installed already, you should install Npgsql from Nuget too:

PM> Install-Package Npgsql

And then you should configure Npgsql in your App.config to be found by the DbProviderFactory API: 

    <add name="Npgsql Data Provider"
          description="Data Provider for PostgreSQL"
          type="Npgsql.NpgsqlFactory, Npgsql" />

and configure your connection string in the same App.config file:

      <add name="BloggingContext"

Running the code

Now it's time to run the code. If you have everything configured and hit Ctrl+F5 you should get the code running and will be greeted with the Blog name question.

Unfortunately after answering the question and pressing enter, an exception will be thrown:

Unhandled Exception: System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---
> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Npgsql.NpgsqlException: ERROR: 3F000: schema "dbo" does not exist

This error occurs because by default, Entity Framework uses the schema dbo and Postgresql uses the schema public. This is the message you get in the Postgresql log:

INSERT INTO "dbo"."Blogs"("Name") VALUES (E'test');SELECT currval(pg_get_serial_sequence('"dbo"."Blogs"', 'BlogId')) AS "BlogId"

ERROR: schema "dbo" does not exist at character 13

As pointed out by Brice in his answer to a user question about this error, you have to tell Entity Framework to use a different schema. This is done by using Data Annotations and adding a Table attribute to the classes of the model:

[Table("Blog", Schema = "public")]
public class Blog


[Table("Post", Schema = "public")]
public class Post

To use those attributes, you have to import the Data Annotations namespace:

using System.ComponentModel.DataAnnotations;

More information about that can be found in the Code First To a New Database article linked at the beginning of this post.

That's it! After making those changes, you should now get the data correctly inserted in the database and everything should work ok:

LOG: statement: INSERT INTO "public"."Blog"("Name") VALUES (E'test');SELECT currval(pg_get_serial_sequence('"public"."Blog"', 'BlogId')) AS "BlogId"

I hope you enjoyed this post and could get started to Entity Framework and Npgsql. Please, let me know what you think in your comments. 

I'd like to thank Brice Lambson for his excellent article and the Microsoft Entity Framework team for their Code First To a New Database walkthrough and all the EF stuff.