프로파일 및 모니터링

프로파일 및 모니터링 (Profile and Monitor, Rust)

개요 (Overview)

Rust 클라이언트는 실행 중이거나 완료된 쿼리를 관찰할 수 있어요. 쿼리 실행 후 연산자별 프로파일링 메트릭을 검색할 수 있고, 다른 스레드에서 오래 실행되는 쿼리를 중단할 수 있어요. 둘 다 아래에 나와 있어요.

출처: 문서

본문

프로파일링 (Profiling)

메트릭을 수집하기 전에 연결에서 프로파일링을 활성화해야 해요. [enable_profilingprofiling_mode PRAGMA]({% link docs/current/dev/profiling.md %})로 활성화하고, 쿼리를 실행한 다음, get_profiling_info()로 결과를 읽어요:

use duckdb::Connection;

let conn = Connection::open_in_memory()?;

// 각 쿼리 후 프로파일링 출력을 stdout으로 출력하지 않도록 함 (프로파일링 활성화 시 기본 동작).
conn.execute("PRAGMA enable_profiling = 'no_output'", [])?;
// 각 연산자에 대한 포괄적인 메트릭 집합을 수집; 'detailed'도 사용 가능.
conn.execute("PRAGMA profiling_mode = 'standard'", [])?;

conn.execute("SELECT 42", [])?;

let info = conn.get_profiling_info().expect("profiling should be enabled");
println!("rows returned: {}", info.metrics["ROWS_RETURNED"]);

get_profiling_info()Option<ProfilingInfo>를 반환하는데, 프로파일링이 활성화되지 않았으면 None이에요. ProfilingInfo는 DuckDB의 쿼리 플랜을 그대로 반영한 트리예요. 루트(QUERY_ROOT)는 총 LATENCYROWS_RETURNED 같은 전체 쿼리 메트릭을 보유하고, 각 자식 노드는 OPERATOR_NAMEOPERATOR_CARDINALITY 같은 자체 메트릭을 가진 플랜 연산자를 나타내요. 노드의 메트릭은 metrics 맵으로, 자식은 children으로 읽어요.

쿼리 중단 (Interrupting Queries)

오래 실행되는 쿼리는 다른 스레드에서 취소할 수 있어요. 연결에서 interrupt_handle()을 호출해 Arc<InterruptHandle>을 얻어요. InterruptHandleSendSync를 모두 구현하므로, 그것이나 연결을 다른 스레드로 옮기고 편한 곳에서 interrupt()를 호출할 수 있어요. 중단된 쿼리는 오류로 실패해요:

use duckdb::{Connection, Result};

fn run_query(conn: Connection) -> Result<()> {
    let interrupt_handle = conn.interrupt_handle();

    let join_handle = std::thread::spawn(move || {
        conn.execute("⟨expensive query⟩", [])
    });

    // ... 나중에, 이 스레드에서 쿼리를 취소:
    interrupt_handle.interrupt();

    let query_result = join_handle.join().unwrap();
    assert!(query_result.is_err());
    Ok(())
}

interrupt()는 핸들을 얻은 연결에서 현재 실행 중인 쿼리를 취소해요. 연결이 이미 드롭됐다면 interrupt() 호출은 아무 일도 하지 않아요.

더 알아보기 (Learn more)

  • [Profiling]({% link docs/current/dev/profiling.md %}) — DuckDB의 쿼리 프로파일링 출력과 enable_profilingprofiling_mode PRAGMA.
  • [Run Queries]({% link docs/current/clients/rust/querying.md %}) — 이 페이지가 메트릭을 읽고 실행을 중단하는 쿼리 실행하기.
  • [Connect]({% link docs/current/clients/rust/connecting.md %}) — 프로파일링과 중단 핸들이 동작하는 Connection.