@@ -538,6 +538,63 @@ public async Task SchemaOnly()
538538 await cmd . ExecuteScalarAsync ( ) ;
539539 }
540540
541+ [ Test , IssueLink ( "https://github.com/npgsql/npgsql/issues/6038" ) ]
542+ public async Task Auto_prepared_schema_only_correct_schema ( )
543+ {
544+ await using var dataSource = CreateDataSource ( csb =>
545+ {
546+ csb . MaxAutoPrepare = 1 ;
547+ csb . AutoPrepareMinUsages = 5 ;
548+ } ) ;
549+ await using var connection = await dataSource . OpenConnectionAsync ( ) ;
550+ var table1 = await CreateTempTable ( connection , "foo int" ) ;
551+ var table2 = await CreateTempTable ( connection , "bar int" ) ;
552+
553+ await using var cmd = connection . CreateCommand ( ) ;
554+ cmd . CommandText = $ "SELECT * FROM { table1 } ";
555+ for ( var i = 0 ; i < 5 ; i ++ )
556+ {
557+ // Make sure we prepare the first query
558+ await using ( await cmd . ExecuteReaderAsync ( CommandBehavior . SchemaOnly ) ) { }
559+ }
560+
561+ cmd . CommandText = $ "SELECT * FROM { table2 } ";
562+ // The second query will load RowDescription, which is a singleton on NpgsqlConnector
563+ // This shouldn't affect the first query, because we create a copy of RowDescription on prepare
564+ await using ( await cmd . ExecuteReaderAsync ( CommandBehavior . SchemaOnly ) ) { }
565+
566+ cmd . CommandText = $ "SELECT * FROM { table1 } ";
567+ // If we indeed made a copy of RowDescription on prepare, we should get the column for the first query and not for the second
568+ await using var reader = await cmd . ExecuteReaderAsync ( CommandBehavior . SchemaOnly | CommandBehavior . KeyInfo ) ;
569+ var columns = await reader . GetColumnSchemaAsync ( ) ;
570+ Assert . That ( columns . Count , Is . EqualTo ( 1 ) ) ;
571+ Assert . That ( columns [ 0 ] . ColumnName , Is . EqualTo ( "foo" ) ) ;
572+ }
573+
574+ [ Test ]
575+ public async Task Auto_prepared_schema_only_replace ( )
576+ {
577+ await using var dataSource = CreateDataSource ( csb =>
578+ {
579+ csb . MaxAutoPrepare = 1 ;
580+ csb . AutoPrepareMinUsages = 5 ;
581+ } ) ;
582+ await using var connection = await dataSource . OpenConnectionAsync ( ) ;
583+
584+ await using var cmd = connection . CreateCommand ( ) ;
585+ cmd . CommandText = "SELECT 1" ;
586+ for ( var i = 0 ; i < 5 ; i ++ )
587+ {
588+ await using ( await cmd . ExecuteReaderAsync ( CommandBehavior . SchemaOnly ) ) { }
589+ }
590+
591+ cmd . CommandText = "SELECT 2" ;
592+ for ( var i = 0 ; i < 5 ; i ++ )
593+ {
594+ await using ( await cmd . ExecuteReaderAsync ( CommandBehavior . SchemaOnly ) ) { }
595+ }
596+ }
597+
541598 [ Test ]
542599 public async Task Auto_prepared_statement_invalidation ( )
543600 {
0 commit comments