You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reset the connection whenever it's closed inside of a TransactionScope #3735
Whenever a pooled connection is closed inside of a TransactionScope, the next open in the same scope will return the same physical connection with already created temporary tables, sequenced and many other things. We should reset the connection, doing the same thing as with prepared statements.
I know this is a few years old, but it seems like it is still a problem.
Here is some code to reproduce it:
usingSystem.Transactions;usingNpgsql;conststringconnectionString="Host=localhost;Username=postgres;Password=postgres;";awaitusingvards=NpgsqlDataSource.Create(connectionString);usingvartx=newTransactionScope(TransactionScopeOption.RequiresNew,TransactionScopeAsyncFlowOption.Enabled);awaitusing(varc=awaitds.OpenConnectionAsync()){awaitusingvarcmd=newNpgsqlCommand(""" create temp table temp_customer (id int, name text); insert into temp_customer (id, name) values (123, 'foo'); select * from temp_customer; """,c);awaitusingvarr=awaitcmd.ExecuteReaderAsync();while(awaitr.ReadAsync()){Console.WriteLine($"Customer: {r.GetInt32(0)}{r.GetString(1)}");}}awaitusing(varc=awaitds.OpenConnectionAsync()){// This fails with 'relation "temp_customer" already exists' if we are unlucky enough to get the same connection as above from the connection pool.awaitusingvarcmd=newNpgsqlCommand(""" create temp table temp_customer (id int, name text); insert into temp_customer (id, name) values (456, 'bar'); select * from temp_customer; """,c);awaitusingvarr=awaitcmd.ExecuteReaderAsync();while(awaitr.ReadAsync()){Console.WriteLine($"Customer: {r.GetInt32(0)}{r.GetString(1)}");}}tx.Complete();
Customer: 123 foo
Unhandled exception. Npgsql.PostgresException (0x80004005): 42P07: relation "temp_customer" already exists
at Npgsql.Internal.NpgsqlConnector.ReadMessageLong(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
at Program.<Main>$(String[] args) in /Users/bpursley/projects/npgsql-3735-repro/npgsql-3735-repro/Program.cs:line 34
at Program.<Main>$(String[] args) in /Users/bpursley/projects/npgsql-3735-repro/npgsql-3735-repro/Program.cs:line 35
at Program.<Main>$(String[] args) in /Users/bpursley/projects/npgsql-3735-repro/npgsql-3735-repro/Program.cs:line 39
at Program.<Main>$(String[] args) in /Users/bpursley/projects/npgsql-3735-repro/npgsql-3735-repro/Program.cs:line 41
at Program.<Main>(String[] args)
Exception data:
Severity: ERROR
SqlState: 42P07
MessageText: relation "temp_customer" already exists
File: heap.c
Line: 1160
Routine: heap_create_with_catalog
Adding discard temp in the second connection allows it to succeed.
usingSystem.Transactions;usingNpgsql;conststringconnectionString="Host=localhost;Username=postgres;Password=postgres;";awaitusingvards=NpgsqlDataSource.Create(connectionString);usingvartx=newTransactionScope(TransactionScopeOption.RequiresNew,TransactionScopeAsyncFlowOption.Enabled);awaitusing(varc=awaitds.OpenConnectionAsync()){awaitusingvarcmd=newNpgsqlCommand(""" create temp table temp_customer (id int, name text); insert into temp_customer (id, name) values (123, 'foo'); select * from temp_customer; """,c);awaitusingvarr=awaitcmd.ExecuteReaderAsync();while(awaitr.ReadAsync()){Console.WriteLine($"Customer: {r.GetInt32(0)}{r.GetString(1)}");}}awaitusing(varc=awaitds.OpenConnectionAsync()){awaitusingvarcmd=newNpgsqlCommand(""" discard temp; create temp table temp_customer (id int, name text); insert into temp_customer (id, name) values (456, 'bar'); select * from temp_customer; """,c);awaitusingvarr=awaitcmd.ExecuteReaderAsync();while(awaitr.ReadAsync()){Console.WriteLine($"Customer: {r.GetInt32(0)}{r.GetString(1)}");}}tx.Complete();
Activity
brianpursley commentedon Jan 24, 2026
I know this is a few years old, but it seems like it is still a problem.
Here is some code to reproduce it:
Adding
discard tempin the second connection allows it to succeed.It seems like the problem occurs somewhere around here:
https://github.com/brianpursley/npgsql/blob/e222c911bc25b0495e21d03df7fe6c63ef8b41e4/src/Npgsql/NpgsqlConnection.cs#L845-L887
When there is an enlisted transaction, it doesn't reset the connection when it is closed.
Reset connection when closing within TransactionScope
Reset connection when closing within TransactionScope
Reset connection when closing within TransactionScope (#6424)