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
7 changes: 7 additions & 0 deletions .github/workflows/ci-tap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ jobs:

- uses: ./.github/actions/setup-vcpkg

- name: Install uv
# uv is used by Arrow-IPC TAP tests (t/026, t/028) to run inline
# pyarrow scripts that decode the debug_arrow_dump_dir output.
uses: astral-sh/setup-uv@v6
with:
version: "latest"

- name: Cache PostgreSQL build
id: cache-pg
uses: actions/cache@v4
Expand Down
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 Int64 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. Signedness matches query_id so the two columns compare/join without explicit casts.',

Comment thread
JoshDreamland marked this conversation as resolved.
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
ADD COLUMN IF NOT EXISTS parent_query_id Int64 DEFAULT 0;
9 changes: 9 additions & 0 deletions src/export/arrow_batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ struct ArrowBatchBuilder::Impl {
arrow::StringBuilder trace_id_builder;
arrow::StringBuilder span_id_builder;
DictBuilder query_id_builder;
DictBuilder parent_query_id_builder;
DictBuilder db_name_builder;
DictBuilder db_user_builder;
DictBuilder db_operation_builder;
Expand Down Expand Up @@ -216,6 +217,7 @@ struct ArrowBatchBuilder::Impl {
arrow::field("trace_id", arrow::utf8()),
arrow::field("span_id", arrow::utf8()),
arrow::field("query_id", DictionaryUtf8Type()),
arrow::field("parent_query_id", DictionaryUtf8Type()),
arrow::field("db_name", DictionaryUtf8Type()),
arrow::field("db_user", DictionaryUtf8Type()),
arrow::field("db_operation", DictionaryUtf8Type()),
Expand Down Expand Up @@ -309,10 +311,15 @@ struct ArrowBatchBuilder::Impl {

char queryid_buf[24];
snprintf(queryid_buf, sizeof(queryid_buf), "%" PRIu64, static_cast<uint64_t>(event.queryid));
char parent_query_id_buf[24];
snprintf(parent_query_id_buf, sizeof(parent_query_id_buf), "%" PRIu64,
static_cast<uint64_t>(event.parent_query_id));
char pid_buf[12];
snprintf(pid_buf, sizeof(pid_buf), "%d", event.pid);

if (!AppendString(&query_id_builder, queryid_buf, "Arrow query_id append") ||
!AppendString(&parent_query_id_builder, parent_query_id_buf,
"Arrow parent_query_id append") ||
!AppendString(&db_name_builder, db_name, "Arrow db_name append") ||
!AppendString(&db_user_builder, db_user, "Arrow db_user append") ||
!AppendString(&db_operation_builder, PschCmdTypeToString(event.cmd_type),
Expand Down Expand Up @@ -475,6 +482,7 @@ struct ArrowBatchBuilder::Impl {
!add_array(&trace_id_builder, "Arrow trace_id finish") ||
!add_array(&span_id_builder, "Arrow span_id finish") ||
!add_dict_array(&query_id_builder, "Arrow query_id finish") ||
!add_dict_array(&parent_query_id_builder, "Arrow parent_query_id finish") ||
!add_dict_array(&db_name_builder, "Arrow db_name finish") ||
!add_dict_array(&db_user_builder, "Arrow db_user finish") ||
!add_dict_array(&db_operation_builder, "Arrow db_operation finish") ||
Expand Down Expand Up @@ -581,6 +589,7 @@ struct ArrowBatchBuilder::Impl {
trace_id_builder.Reset();
span_id_builder.Reset();
query_id_builder.ResetFull();
parent_query_id_builder.ResetFull();
db_name_builder.ResetFull();
db_user_builder.ResetFull();
db_operation_builder.ResetFull();
Expand Down
2 changes: 2 additions & 0 deletions src/export/stats_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ void ExportEventStatsInternal(const std::vector<PschEvent>& events, StatsExporte
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->RecordInt64("parent_query_id");
auto col_cmd_type = exporter->DbOperationColumn();
Comment thread
JoshDreamland marked this conversation as resolved.
auto col_rows = exporter->MetricUInt64("rows");
auto col_query = exporter->DbQueryTextColumn();
Expand Down Expand Up @@ -296,6 +297,7 @@ void ExportEventStatsInternal(const std::vector<PschEvent>& events, StatsExporte
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(PschCmdTypeToString(ev.cmd_type));
col_rows->Append(ev.rows);

Expand Down
Loading
Loading