Keep track of nested queries stack-style and emit parent_query_id#61
Open
JoshDreamland wants to merge 6 commits intomainfrom
Open
Keep track of nested queries stack-style and emit parent_query_id#61JoshDreamland wants to merge 6 commits intomainfrom
parent_query_id#61JoshDreamland wants to merge 6 commits intomainfrom
Conversation
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>
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>
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>
serprex
reviewed
Apr 10, 2026
| -- 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 |
Member
There was a problem hiding this comment.
there should be a 000 migration as seed (pretty much 00-schema.sql before this PR)
Member
|
can you explain how this stack is different from nesting_level, ie why you got rid of |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withparent_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
plpgsqlfunction issuing a separate query with its own execution plan, notSELECT 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.