aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/QueryLog.h
blob: 5bc80280eac9e0a8ebdf540df90f803cec4541ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once

#include <Common/ProfileEvents.h>
#include <Core/NamesAndTypes.h>
#include <Core/NamesAndAliases.h>
#include <Core/Settings.h>
#include <Interpreters/Cache/QueryCache.h>
#include <Interpreters/ClientInfo.h>
#include <Interpreters/SystemLog.h>
#include <Interpreters/TransactionVersionMetadata.h>
#include <IO/AsyncReadCounters.h>
#include <Parsers/IAST.h>


namespace ProfileEvents
{
    class Counters;
}


namespace DB
{


/** Allows to log information about queries execution:
  * - info about start of query execution;
  * - performance metrics (are set at the end of query execution);
  * - info about errors of query execution.
  */

/// A struct which will be inserted as row into query_log table
struct QueryLogElement
{
    using Type = QueryLogElementType;

    Type type = QUERY_START;

    /// Depending on the type of query and type of stage, not all the fields may be filled.

    time_t event_time{};
    Decimal64 event_time_microseconds{};
    time_t query_start_time{};
    Decimal64 query_start_time_microseconds{};
    UInt64 query_duration_ms{};

    /// The data fetched from DB to execute the query
    UInt64 read_rows{};
    UInt64 read_bytes{};

    /// The data written to DB
    UInt64 written_rows{};
    UInt64 written_bytes{};

    /// The data sent to the client
    UInt64 result_rows{};
    UInt64 result_bytes{};

    UInt64 memory_usage{};

    String current_database;
    String query;
    String formatted_query;
    UInt64 normalized_query_hash{};

    IAST::QueryKind query_kind{};
    std::set<String> query_databases;
    std::set<String> query_tables;
    std::set<String> query_columns;
    std::set<String> query_partitions;
    std::set<String> query_projections;
    std::set<String> query_views;

    std::unordered_set<String> used_aggregate_functions;
    std::unordered_set<String> used_aggregate_function_combinators;
    std::unordered_set<String> used_database_engines;
    std::unordered_set<String> used_data_type_families;
    std::unordered_set<String> used_dictionaries;
    std::unordered_set<String> used_formats;
    std::unordered_set<String> used_functions;
    std::unordered_set<String> used_storages;
    std::unordered_set<String> used_table_functions;
    std::set<String> used_row_policies;

    Int32 exception_code{}; // because ErrorCodes are int
    String exception;
    String stack_trace;
    std::string_view exception_format_string{};

    ClientInfo client_info;

    String log_comment;

    std::vector<UInt64> thread_ids;
    std::shared_ptr<ProfileEvents::Counters::Snapshot> profile_counters;
    std::shared_ptr<AsyncReadCounters> async_read_counters;
    std::shared_ptr<Settings> query_settings;

    TransactionID tid;

    QueryCache::Usage query_cache_usage = QueryCache::Usage::Unknown;

    static std::string name() { return "QueryLog"; }

    static NamesAndTypesList getNamesAndTypes();
    static NamesAndAliases getNamesAndAliases();
    void appendToBlock(MutableColumns & columns) const;
    static const char * getCustomColumnList() { return nullptr; }

    static void appendClientInfo(const ClientInfo & client_info, MutableColumns & columns, size_t & i);
};


/// Instead of typedef - to allow forward declaration.
class QueryLog : public SystemLog<QueryLogElement>
{
    using SystemLog<QueryLogElement>::SystemLog;
};

}