fix(duckdb): avoid extension install during startup

This commit is contained in:
langcaiye
2026-06-05 16:53:09 +08:00
committed by lyingbug
parent ea64a51129
commit 99f35f8dea
+12 -1
View File
@@ -1314,9 +1314,19 @@ func NewDuckDB() (*sql.DB, error) {
return nil, fmt.Errorf("failed to open duckdb: %w", err) return nil, fmt.Errorf("failed to open duckdb: %w", err)
} }
// Try to install and load required extensions. // Try to install and load required extensions unless explicitly disabled.
// - spatial: used for st_read_meta() to enumerate layer (sheet) names from .xlsx/.xls // - spatial: used for st_read_meta() to enumerate layer (sheet) names from .xlsx/.xls
// - excel: used for read_xlsx() which gives proper type inference per sheet // - excel: used for read_xlsx() which gives proper type inference per sheet
//
// INSTALL hits extensions.duckdb.org (public internet). In locked-down
// runtimes with no egress, set DUCKDB_SKIP_EXTENSION_LOAD=1 to avoid a
// startup hang; xlsx/xls ingest may fail later without these extensions.
if strings.EqualFold(os.Getenv("DUCKDB_SKIP_EXTENSION_LOAD"), "true") ||
os.Getenv("DUCKDB_SKIP_EXTENSION_LOAD") == "1" {
logger.Infof(context.Background(),
"[DuckDB] Skipping spatial/excel extension install/load "+
"(DUCKDB_SKIP_EXTENSION_LOAD is set; xlsx ingest may fail without them)")
} else {
bgCtx := context.Background() bgCtx := context.Background()
for _, ext := range []string{"spatial", "excel"} { for _, ext := range []string{"spatial", "excel"} {
if _, err := sqlDB.ExecContext(bgCtx, fmt.Sprintf("INSTALL %s;", ext)); err != nil { if _, err := sqlDB.ExecContext(bgCtx, fmt.Sprintf("INSTALL %s;", ext)); err != nil {
@@ -1326,6 +1336,7 @@ func NewDuckDB() (*sql.DB, error) {
logger.Warnf(bgCtx, "[DuckDB] Failed to load %s extension: %v", ext, err) logger.Warnf(bgCtx, "[DuckDB] Failed to load %s extension: %v", ext, err)
} }
} }
}
return sqlDB, nil return sqlDB, nil
} }