@@ -280,7 +280,7 @@ func (s *Server) createDBConnection(username string) (*sql.DB, error) {
280280 // Continue anyway - basic queries will still work
281281 }
282282
283- // Attach DuckLake catalog if configured
283+ // Attach DuckLake catalog if configured (but don't set as default yet)
284284 duckLakeMode := false
285285 if err := s .attachDuckLake (db ); err != nil {
286286 // If DuckLake was explicitly configured, fail the connection.
@@ -295,13 +295,22 @@ func (s *Server) createDBConnection(username string) (*sql.DB, error) {
295295 duckLakeMode = true
296296 }
297297
298- // Initialize information_schema compatibility views
299- // Must be done AFTER attaching DuckLake so views can reference ducklake.information_schema
298+ // Initialize information_schema compatibility views in memory.main
299+ // Must be done AFTER attaching DuckLake (so views can reference ducklake.information_schema)
300+ // but BEFORE setting DuckLake as default (so views are created in memory.main, not ducklake.main)
300301 if err := initInformationSchema (db , duckLakeMode ); err != nil {
301302 log .Printf ("Warning: failed to initialize information_schema for user %q: %v" , username , err )
302303 // Continue anyway - basic queries will still work
303304 }
304305
306+ // Now set DuckLake as the default catalog so all user queries use it
307+ if duckLakeMode {
308+ if err := setDuckLakeDefault (db ); err != nil {
309+ db .Close ()
310+ return nil , fmt .Errorf ("failed to set DuckLake as default: %w" , err )
311+ }
312+ }
313+
305314 return db , nil
306315}
307316
@@ -333,7 +342,8 @@ func (s *Server) loadExtensions(db *sql.DB) error {
333342 return lastErr
334343}
335344
336- // attachDuckLake attaches a DuckLake catalog if configured
345+ // attachDuckLake attaches a DuckLake catalog if configured (but does NOT set it as default).
346+ // Call setDuckLakeDefault after creating per-connection views in memory.main.
337347func (s * Server ) attachDuckLake (db * sql.DB ) error {
338348 if s .cfg .DuckLake .MetadataStore == "" {
339349 return nil // DuckLake not configured
@@ -355,10 +365,7 @@ func (s *Server) attachDuckLake(db *sql.DB) error {
355365 var count int
356366 err := db .QueryRow ("SELECT COUNT(*) FROM duckdb_databases() WHERE database_name = 'ducklake'" ).Scan (& count )
357367 if err == nil && count > 0 {
358- // Already attached, just set as default
359- if _ , err := db .Exec ("USE ducklake" ); err != nil {
360- return fmt .Errorf ("failed to set DuckLake as default catalog: %w" , err )
361- }
368+ // Already attached
362369 return nil
363370 }
364371
@@ -398,13 +405,17 @@ func (s *Server) attachDuckLake(db *sql.DB) error {
398405 return fmt .Errorf ("failed to attach DuckLake: %w" , err )
399406 }
400407
401- // Set DuckLake as the default catalog so all queries use it
402- // See: https://duckdb.org/docs/stable/core_extensions/ducklake#usage
408+ log .Printf ("Attached DuckLake catalog successfully" )
409+ return nil
410+ }
411+
412+ // setDuckLakeDefault sets the DuckLake catalog as the default so all queries use it.
413+ // This should be called AFTER creating per-connection views in memory.main.
414+ func setDuckLakeDefault (db * sql.DB ) error {
403415 if _ , err := db .Exec ("USE ducklake" ); err != nil {
404416 return fmt .Errorf ("failed to set DuckLake as default catalog: %w" , err )
405417 }
406-
407- log .Printf ("Attached DuckLake catalog successfully and set as default" )
418+ log .Printf ("Set DuckLake as default catalog" )
408419 return nil
409420}
410421
0 commit comments