Question

I'm using Npgsql to connect to Amazon Redshift. This all works correctly, but it seems very slow at connecting.

Everything I'm doing seems pretty standard:

<add name="EventDatabase" connectionString="Server=...us-west-2.redshift.amazonaws.com; Database=analytics; UID=...; PWD=...; Port=...; Pooling=true; MinPoolSize=4; MaxPoolSize=12; ConnectionLifeTime=300;" />

Connection = new NpgsqlConnection(
    ConfigurationManager.ConnectionStrings["EventDB"].ConnectionString);
Connection.Open();

Benchmarking this a few times gives me an average time to open a connection of 0.9s. This is much higher than I would expect.

The connection should be pooled and I would have thought opening would be instant? Even if it's not pooled, 0.9s seems high. 40% of our query time is spent opening connections. That's not correct.

Am I missing something?

Was it helpful?

Solution

There was an issue with Beta1 using Redshift. With RC1 the connection opening is much faster.

OTHER TIPS

We found that Npgsql 2.2.5 wasn't releasing connections back into the connection pool, which is exacerbated by the expensive time to create a new connection (also ~1 second each).

This was tracked down to an error:

ERROR: 0A000: SQL command "UNLISTEN *" not supported.

Making the following change to the 2.2.5 NpgSql/NpgSql/NpgSqlConnector.cs code seems to rectify matters:

    internal void ReleaseRegisteredListen()
    {
        if (!IsRedshift) 
            NpgsqlCommand.ExecuteBlind(this, NpgsqlQuery.UnlistenAll);
    }

Disclaimer : This isn't fully tested, and there may be a different cleanup needed on RedShift altogether, which isn't done here.

Append "Pooling=False" to the connection string, and call this before opening the connection:

ThreadPool.SetMinThreads(512, 512);
ThreadPool.SetMaxThreads(1024, 1024);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top