Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docker/init/00-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ CREATE TABLE pg_stat_ch.events_raw

query_id Int64 COMMENT '64-bit hash identifying normalized queries. Queries differing only in constants share the same query_id. Use for aggregating statistics across similar queries.',

parent_query_id UInt64 COMMENT 'query_id of the calling query (e.g. the plpgsql function that issued this SPI statement). 0 for top-level queries. Use WHERE parent_query_id = 0 to restrict aggregations to top-level queries and avoid double-counting CPU and duration.',

cmd_type LowCardinality(String) COMMENT 'Command type: SELECT, INSERT, UPDATE, DELETE, MERGE, UTILITY, or UNKNOWN. Use for workload characterization (read-heavy vs write-heavy).',

rows UInt64 COMMENT 'Rows returned (SELECT) or affected (INSERT/UPDATE/DELETE). HIGH: large result sets or bulk operations. LOW: point queries. Watch for unexpected HIGH values indicating missing WHERE clauses.',
Expand Down
12 changes: 12 additions & 0 deletions migrations/001_add_parent_query_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Migration: add parent_query_id column
--
-- Introduced in pg_stat_ch 0.4.x. Each event now carries the query_id of its
-- calling query (e.g. the plpgsql function that issued an SPI statement).
-- Top-level queries emit 0. Use WHERE parent_query_id = 0 in aggregations to
-- avoid double-counting CPU and duration across nested calls.
--
-- Run against your ClickHouse instance before upgrading the extension:
-- clickhouse-client < migrations/001_add_parent_query_id.sql

ALTER TABLE pg_stat_ch.events_raw
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a 000 migration as seed (pretty much 00-schema.sql before this PR)

ADD COLUMN IF NOT EXISTS parent_query_id UInt64 DEFAULT 0;
2 changes: 2 additions & 0 deletions src/export/stats_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void ExportEventStats(const std::vector<PschEvent>& events, StatsExporter* expor
auto col_username = exporter->DbUserColumn();
auto col_pid = exporter->RecordInt32("pid");
auto col_query_id = exporter->RecordInt64("query_id");
auto col_parent_query_id = exporter->RecordUInt64("parent_query_id");
auto col_cmd_type = exporter->DbOperationColumn();
auto col_rows = exporter->MetricUInt64("rows");
auto col_query = exporter->DbQueryTextColumn();
Expand Down Expand Up @@ -148,6 +149,7 @@ void ExportEventStats(const std::vector<PschEvent>& events, StatsExporter* expor
col_username->Append(std::string(ev.username, ev.username_len));
col_pid->Append(ev.pid);
col_query_id->Append(static_cast<int64_t>(ev.queryid));
col_parent_query_id->Append(static_cast<int64_t>(ev.parent_query_id));
col_cmd_type->Append(CmdTypeToString(ev.cmd_type));
col_rows->Append(ev.rows);

Expand Down
Loading
Loading