Skip to content

Conversation

gaantunes
Copy link
Contributor

@gaantunes gaantunes commented Sep 30, 2025

PR Description

Rework the Postgres query samples collector to buffer per-query execution state across scrapes, consolidate wait events, and emit finalized entries.

Key changes:

  • Introduce in-memory tracking keyed by PID/QueryID/XID to maintain sample state between scrapes and finalize when a query turns idle or disappears.
  • Track sequences of wait events (type, event, blocked_by PIDs) with normalization and last-seen timestamps, emitting OP_WAIT_EVENT per distinct occurrence.
  • Tests covering active/idle transitions, disappearance, parallel workers, wait event transitions and blocked_by PID normalization and CPU time sampling.

Which issue(s) this PR fixes

Notes to the Reviewer

PR Checklist

  • CHANGELOG.md updated
  • Documentation added
  • Tests updated
  • Config converters updated

@gaantunes gaantunes force-pushed the gaantunes/db-o11y-cache-pg-samples branch from fec512f to 5e19ebf Compare October 1, 2025 15:01
@gaantunes gaantunes marked this pull request as ready for review October 1, 2025 18:45
@gaantunes gaantunes requested a review from a team as a code owner October 1, 2025 19:48
@matthewnolf matthewnolf changed the title Db-o11y: Cache pg sample and wait events and send finalized entries database_observability: cache pg sample and wait events before sending finalised entries Oct 6, 2025

// emitAndDeleteSample builds final entries for a sample and removes it from memory.
func (c *QuerySamples) emitAndDeleteSample(key SampleKey, state *SampleState) {
// Build and emit OP_QUERY_SAMPLE
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like a lot of the godoc and inline comments are superflous

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like documenting the functions behavior, it makes the code easier to read. I can remove it if you are strongly against though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is a balance, a lot of the comments are saying what the code is doing. Their usefulness comes when they explain why it is doing it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed those that I judged redundant. Let me know if it looks better.

}
defer rows.Close()

activeKeys := map[SampleKey]struct{}{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it worth having a single map keys = map[SampleKey]SampleState?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are separated to make finalize logic straightforward, differentiating between samples that turned idle from samples that disappeared. If we put it all together more logic will be needed to classify samples within the same map.

@gaantunes gaantunes merged commit 00cb3ff into main Oct 9, 2025
41 checks passed
@gaantunes gaantunes deleted the gaantunes/db-o11y-cache-pg-samples branch October 9, 2025 13:36
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