MCPcopy Create free account
hub / github.com/PostHog/duckgres / isDuckDBUtilityCommand

Function isDuckDBUtilityCommand

server/conn.go:742–790  ·  view source on GitHub ↗

isDuckDBUtilityCommand checks if a query is a DuckDB utility command that doesn't support EXPLAIN validation. These commands are passed through directly to DuckDB without pre-validation.

(query string)

Source from the content-addressed store, hash-verified

740
741// safeCleanupDB safely closes the database connection, handling the case where
742// the underlying connection (e.g., DuckLake's SSL connection to RDS) may be broken.
743//
744// This mitigates crashes from DuckDB throwing C++ exceptions during cleanup by:
745// 1. Detecting broken connections early via a health check query
746// 2. Explicitly rolling back transactions before Close() to avoid DuckDB's internal ROLLBACK
747// 3. Skipping SQL cleanup operations when the connection is known to be broken
748//
749// Note: If the connection breaks between our health check and Close(), DuckDB may still
750// throw a C++ exception. This is a best-effort mitigation, not a complete fix.
751func (c *clientConn) safeCleanupDB() {
752 // Recover from Go-level panics during database cleanup (e.g., nil pointer
753 // dereference if connection state is inconsistent after cancellation).
754 // Note: DuckDB C++ crashes (SIGABRT/SIGSEGV) are fatal signals that cannot be
755 // caught by recover() — process isolation mode is needed to survive those.
756 defer func() {
757 if r := recover(); r != nil {
758 c.logger().Error("Recovered from panic during database cleanup.", "panic", r)
759 }
760 }()
761
762 cleanupTimeout := 5 * time.Second
763
764 if c.sharedDB {
765 // Shared file-persistence pool: ROLLBACK any open transaction on the
766 // pinned connection, then return it to the pool. Skip DuckLake DETACH
767 // since the underlying DB is shared across connections.
768 if c.txStatus == txStatusTransaction || c.txStatus == txStatusError {
769 ctx, cancel := context.WithTimeout(context.Background(), cleanupTimeout)
770 _, err := c.executor.ExecContext(ctx, "ROLLBACK")
771 cancel()
772 if err != nil {
773 c.logger().Warn("Failed to rollback transaction during cleanup.", "error", err)
774 }
775 }
776 // Close returns the pinned *sql.Conn to the pool (does not close the DB).
777 if err := c.executor.Close(); err != nil {
778 c.logger().Warn("Failed to return connection to pool.", "error", err)
779 }
780 c.server.releaseFileDB(c.username)
781 return
782 }
783
784 connHealthy := true
785
786 // Check connection health. For DuckLake, we need to actually run a query that
787 // touches the metadata connection, not just ping the local DuckDB connection.
788 ctx, cancel := context.WithTimeout(context.Background(), cleanupTimeout)
789 if c.server.cfg.DuckLake.MetadataStore != "" {
790 // Probe the attached DuckLake catalog via DuckDB's catalog table function.
791 // This stays valid even though DuckLake does not expose an information_schema
792 // catalog that can be referenced as ducklake.information_schema.*.
793 _, err := c.executor.ExecContext(ctx, "SELECT 1 FROM duckdb_tables() WHERE database_name = 'ducklake' LIMIT 1")

Callers 2

validateWithDuckDBMethod · 0.85

Calls 2

stripLeadingCommentsFunction · 0.85
hasCommandPrefixFunction · 0.85

Tested by 1