@@ -103,8 +103,6 @@ internal async Task LoadPostgresInfo(NpgsqlConnector conn, NpgsqlTimeout timeout
103103 /// Generates a raw SQL query string to select type information.
104104 /// </summary>
105105 /// <param name="withRange">True to load range types.</param>
106- /// <param name="withEnum">True to load enum types.</param>
107- /// <param name="withEnumSortOrder"></param>
108106 /// <param name="loadTableComposites">True to load table composites.</param>
109107 /// <returns>
110108 /// A raw SQL query string that selects type information.
@@ -116,11 +114,8 @@ internal async Task LoadPostgresInfo(NpgsqlConnector conn, NpgsqlTimeout timeout
116114 /// For arrays and ranges, join in the element OID and type (to filter out arrays of unhandled
117115 /// types).
118116 /// </remarks>
119- static string GenerateTypesQuery ( bool withRange , bool withEnum , bool withEnumSortOrder ,
120- bool loadTableComposites )
117+ static string GenerateLoadTypesQuery ( bool withRange , bool loadTableComposites )
121118 => $@ "
122- SELECT version();
123-
124119SELECT ns.nspname, typ_and_elem_type.*,
125120 CASE
126121 WHEN typtype IN ('b', 'e', 'p') THEN 0 -- First base types, enums, pseudo-types
@@ -165,8 +160,10 @@ elemtyptype IN ('b', 'r', 'e', 'd') OR -- Array of base, range, enum, domain
165160 (elemtyptype = 'p' AND elemtypname IN ('record', 'void')) OR -- Arrays of special supported pseudo-types
166161 (elemtyptype = 'c' AND { ( loadTableComposites ? "ns.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')" : "elemrelkind='c'" ) } ) -- Array of user-defined free-standing composites (not table composites) by default
167162 ))
168- ORDER BY ord;
163+ ORDER BY ord;" ;
169164
165+ static string GenerateLoadCompositeTypesQuery ( bool loadTableComposites )
166+ => $@ "
170167-- Load field definitions for (free-standing) composite types
171168SELECT typ.oid, att.attname, att.atttypid
172169FROM pg_type AS typ
@@ -177,15 +174,15 @@ JOIN pg_attribute AS att ON (att.attrelid = typ.typrelid)
177174 (typ.typtype = 'c' AND { ( loadTableComposites ? "ns.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')" : "cls.relkind='c'" ) } ) AND
178175 attnum > 0 AND -- Don't load system attributes
179176 NOT attisdropped
180- ORDER BY typ.oid, att.attnum;
177+ ORDER BY typ.oid, att.attnum;" ;
181178
182- { ( withEnum ? $@ "
179+ static string GenerateLoadEnumFieldsQuery ( bool withEnumSortOrder )
180+ => $@ "
183181-- Load enum fields
184182SELECT pg_type.oid, enumlabel
185183FROM pg_enum
186184JOIN pg_type ON pg_type.oid=enumtypid
187- ORDER BY oid{ ( withEnumSortOrder ? ", enumsortorder" : "" ) } ;" : "" ) }
188- " ;
185+ ORDER BY oid{ ( withEnumSortOrder ? ", enumsortorder" : "" ) } ;" ;
189186
190187 /// <summary>
191188 /// Loads type information from the backend specified by <paramref name="conn"/>.
@@ -204,13 +201,24 @@ internal async Task<List<PostgresType>> LoadBackendTypes(NpgsqlConnector conn, N
204201 if ( timeout . IsSet )
205202 commandTimeout = ( int ) timeout . CheckAndGetTimeLeft ( ) . TotalSeconds ;
206203
207- var typeLoadingQuery = GenerateTypesQuery ( SupportsRangeTypes , SupportsEnumTypes , HasEnumSortOrder , conn . Settings . LoadTableComposites ) ;
208- using var command = conn . CreateCommand ( typeLoadingQuery ) ;
209- command . CommandTimeout = commandTimeout ;
210- command . AllResultTypesAreUnknown = true ;
204+ using var batch = new NpgsqlBatch ( conn )
205+ {
206+ BatchCommands =
207+ {
208+ new ( "SELECT version()" ) ,
209+ new ( GenerateLoadTypesQuery ( SupportsRangeTypes , conn . Settings . LoadTableComposites ) ) ,
210+ new ( GenerateLoadCompositeTypesQuery ( conn . Settings . LoadTableComposites ) ) ,
211+ }
212+ } ;
213+
214+ if ( SupportsEnumTypes )
215+ batch . BatchCommands . Add ( new ( GenerateLoadEnumFieldsQuery ( HasEnumSortOrder ) ) ) ;
216+
217+ batch . Timeout = commandTimeout ;
218+ batch . AllResultTypesAreUnknown = true ;
211219
212220 timeout . CheckAndApply ( conn ) ;
213- var reader = async ? await command . ExecuteReaderAsync ( ) : command . ExecuteReader ( ) ;
221+ var reader = async ? await batch . ExecuteReaderAsync ( ) : batch . ExecuteReader ( ) ;
214222 try
215223 {
216224 var byOID = new Dictionary < uint , PostgresType > ( ) ;
0 commit comments