Skip to content

Commit

Permalink
PMM-13288 Honour max-query-length parameter for pg_stat_statements. (#…
Browse files Browse the repository at this point in the history
…3119)

Co-authored-by: Alex Demidoff <[email protected]>
  • Loading branch information
BupycHuk and ademidoff authored Aug 27, 2024
1 parent d9d956c commit 7c580b4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions agent/agents/postgres/pgstatstatements/pgstatstatements.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (m *PGStatStatementsQAN) getNewBuckets(ctx context.Context, periodStart tim
return nil, err
}

buckets := makeBuckets(current, prev, m.disableCommentsParsing, m.l)
buckets := m.makeBuckets(current, prev)
startS := uint32(periodStart.Unix())
m.l.Debugf("Made %d buckets out of %d stat statements in %s+%d interval.",
len(buckets), len(current), periodStart.Format("15:04:05"), periodLengthSecs)
Expand All @@ -312,8 +312,9 @@ func (m *PGStatStatementsQAN) getNewBuckets(ctx context.Context, periodStart tim

// makeBuckets uses current state of pg_stat_statements table and accumulated previous state
// to make metrics buckets. It's a pure function for easier testing.
func makeBuckets(current, prev statementsMap, disableCommentsParsing bool, l *logrus.Entry) []*agentpb.MetricsBucket {
func (m *PGStatStatementsQAN) makeBuckets(current, prev statementsMap) []*agentpb.MetricsBucket {
res := make([]*agentpb.MetricsBucket, 0, len(current))
l := m.l

for queryID, currentPSS := range current {
prevPSS := prev[queryID]
Expand All @@ -339,10 +340,10 @@ func makeBuckets(current, prev statementsMap, disableCommentsParsing bool, l *lo
}

if len(currentPSS.Tables) == 0 {
currentPSS.Tables = extractTables(currentPSS.Query, l)
currentPSS.Tables = extractTables(currentPSS.Query, m.maxQueryLength, l)
}

if !disableCommentsParsing {
if !m.disableCommentsParsing {
comments, err := queryparser.PostgreSQLComments(currentPSS.Query)
if err != nil {
l.Errorf("failed to parse comments for query: %s", currentPSS.Query)
Expand Down
4 changes: 2 additions & 2 deletions agent/agents/postgres/pgstatstatements/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func queryUsernames(q *reform.Querier) map[int64]string {
return res
}

func extractTables(query string, l *logrus.Entry) []string {
func extractTables(query string, maxQueryLength int32, l *logrus.Entry) []string {
start := time.Now()
t, _ := truncate.Query(query, truncate.GetDefaultMaxQueryLength())
t, _ := truncate.Query(query, maxQueryLength)
tables, err := parser.ExtractTables(query)
if err != nil {
// log full query and error stack on debug level or more
Expand Down

0 comments on commit 7c580b4

Please sign in to comment.