Skip to content

Keep track of nested queries stack-style and emit parent_query_id#61

Open
JoshDreamland wants to merge 6 commits intomainfrom
nested-query-details
Open

Keep track of nested queries stack-style and emit parent_query_id#61
JoshDreamland wants to merge 6 commits intomainfrom
nested-query-details

Conversation

@JoshDreamland
Copy link
Copy Markdown
Contributor

Currently, we throw timing information for all queries and nested queries into the same stats table (invariably as log rows as of this moment). If you sum all of the query CPU loads for a fixed window, you'll likely end up with more core intervals than there are cores, because we currently count the window for every nested query.

Prior to this change, bool top_level; in the Event payload is written but never read. It's been replaced with parent_query_id, which allows forming a proper breakdown chart for CPU usage across nested queries.

Note: in this context, a "nested" query is a query that contains a plpgsql function issuing a separate query with its own execution plan, not SELECT foo WHERE bar in (SELECT...).

Because this introduces a new column, it is a BREAKING CHANGE for any existing user. I have added a migrations/ folder with SQL that should theoretically keep an existing CH table current with these (and future) schema changes, but this is a new practice for this project and does not have testing or automation.

JoshDreamland and others added 2 commits April 9, 2026 14:54
std::vector::push_back throws std::bad_alloc on memory exhaustion, which
is incompatible with PostgreSQL's longjmp-based error handling. Replace
with a fixed-size array and a separate depth counter so push/pop balance
is always maintained. Queries exceeding the cap emit zero CPU rather than
garbage or a crash.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 9, 2026 23:57
JoshDreamland and others added 2 commits April 9, 2026 23:01
Push sites now write directly into query_stack[depth] instead of
populating a local PschQueryFrame and assigning it to the array slot.

Pop sites now use a const pointer into the array (nullptr when depth
exceeded the cap) instead of copying out a zeroed PschQueryFrame.
CPU and start-time fields are only read through the pointer when
non-null, preserving the zero-CPU / zero-ts behavior for the overflow
case without the unnecessary struct copy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@JoshDreamland JoshDreamland requested review from Copilot and removed request for Copilot April 10, 2026 03:39
JoshDreamland and others added 2 commits April 10, 2026 10:47
Just for consistency with the other cast... I don't know why that's being done in the first place...
- Add QueryStackPush/QueryStackPop helpers to consolidate the push/pop
  logic (including rusage capture and CPU delta computation) that was
  duplicated across ExecutorStart, ExecutorEnd, and PschProcessUtility
- Add cpu_user_us/cpu_sys_us to PschQueryFrame; QueryStackPop computes
  the delta in-place, eliminating ComputeCpuDelta and its out-params
- Pass frame pointer directly into BuildEventFromQueryDesc and
  BuildEventForUtility instead of unpacking start_ts + CPU ints
- Reduce kMaxQueryNestingDepth from 64 to 8

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
-- 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)

@serprex
Copy link
Copy Markdown
Member

serprex commented Apr 10, 2026

can you explain how this stack is different from nesting_level, ie why you got rid of PG_TRY/PG_FINALLY to instead have push/pop seemingly scattered across different hook invocations

@serprex serprex mentioned this pull request Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants