InitSessionDatabaseMetadata installs session-local overrides for metadata surfaces (current_database, pg_database, information_schema views) so they reflect `catalog` — the real, attached catalog the session defaults to. The caller resolves `catalog` to "ducklake" (the name the catalog is actually a
(ctx context.Context, executor sqlcore.QueryExecutor, catalog string)
| 25 | type MetadataAccessPolicy struct { |
| 26 | AllowedSchemas []string |
| 27 | AllowedRelations []string |
| 28 | } |
| 29 | |
| 30 | // InitSessionDatabaseMetadata installs session-local overrides for metadata |
| 31 | // surfaces (current_database, pg_database, information_schema views) so they |
| 32 | // reflect `catalog` — the real, attached catalog the session defaults to. The |
| 33 | // caller resolves `catalog` to "ducklake" (the name the catalog is actually |
| 34 | // attached as); there is no logical→physical masking. |
| 35 | func InitSessionDatabaseMetadata(ctx context.Context, executor sqlcore.QueryExecutor, catalog string) error { |
| 36 | return InitSessionDatabaseMetadataWithAccess(ctx, executor, catalog, nil) |
| 37 | } |
| 38 | |
| 39 | // InitSessionDatabaseMetadataWithAccess installs metadata views filtered to |
| 40 | // the relations the current principal can query. |
| 41 | func InitSessionDatabaseMetadataWithAccess(ctx context.Context, executor sqlcore.QueryExecutor, catalog string, access *MetadataAccessPolicy) error { |
| 42 | if executor == nil { |
| 43 | return fmt.Errorf("session executor is required") |
| 44 | } |
| 45 | |
| 46 | catalog = strings.TrimSpace(catalog) |
| 47 | if catalog == "" { |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | if _, err := executor.ExecContext(ctx, fmt.Sprintf( |
| 52 | "CREATE OR REPLACE TEMP MACRO current_database() AS %s", |
| 53 | quoteSQLStringLiteral(catalog), |
| 54 | )); err != nil { |
| 55 | return fmt.Errorf("create current_database() macro: %w", err) |
| 56 | } |
| 57 | |
| 58 | duckLakeAttached, err := HasAttachedCatalog(ctx, executor, "ducklake") |
| 59 | if err != nil { |
| 60 | return fmt.Errorf("detect ducklake attachment: %w", err) |
| 61 | } |
| 62 | |
| 63 | if _, err := executor.ExecContext(ctx, "USE memory"); err != nil { |
| 64 | return fmt.Errorf("switch to memory catalog: %w", err) |
| 65 | } |
| 66 | defer func() { |
| 67 | // Leave the session in a real catalog (we entered `memory` to install the |
| 68 | // compat views there). For DuckLake sessions, restore `ducklake` here. Keep |
| 69 | // memory.main on the search_path so the pg_catalog compat macros stay |
| 70 | // resolvable after the switch. |
| 71 | if duckLakeAttached { |