Skip to content

[receiver/postgresql] get query plan for top query collection #39995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 4, 2025
Merged
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
27 changes: 27 additions & 0 deletions .chloggen/query-explain-for-postgresqlreceiver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: postgresqlreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: add the ability to obtain query plan for top n queries

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [39995]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
8 changes: 8 additions & 0 deletions receiver/postgresqlreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ We provide functionality to collect the most executed queries from postgresql. I
...
```

Along with those attributes, we will also report the query plan we gathered if it is possible.

By default, top query collection is disabled, also note, to use it, you will need
to create the extension to every database. Take the example from `testdata/integration/02-create-extension.sh`

Expand All @@ -95,6 +97,12 @@ The following options are available:
- `max_rows_per_query`: (optional, default=1000) The max number of rows would return from the query
against `pg_stat_statements`.
- `top_n_query`: (optional, default=1000) The maximum number of active queries to report (to the next consumer) in a single run.
- `max_explain_each_interval`: (optional, default=1000). The maximum number of explain query to be sent in each scrape interval. The top query
collection would not get the query plan directly. Instead, we need to mimic the query in the database and get the query plan from database
separately. This could lead some resources usage and limit this will reduce the impact on your database.
- `query_plan_cache_size`: (optional, default=1000). The query plan cache size. Once we got explain for one query, we will store it in the cache.
This defines the cache's size for query plan.
- `query_plan_cache_ttl`: (optional, default=1h). How long before the query plan cache got expired. Example values: `1m`, `1h`.

### Example Configuration

Expand Down
44 changes: 43 additions & 1 deletion receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,55 @@ type client interface {
getVersion(ctx context.Context) (string, error)
getQuerySamples(ctx context.Context, limit int64, logger *zap.Logger) ([]map[string]any, error)
getTopQuery(ctx context.Context, limit int64, logger *zap.Logger) ([]map[string]any, error)
explainQuery(query string, queryID string, logger *zap.Logger) (string, error)
}

type postgreSQLClient struct {
client *sql.DB
closeFn func() error
}

// explainQuery implements client.
func (c *postgreSQLClient) explainQuery(query string, queryID string, logger *zap.Logger) (string, error) {
normalizedQueryID := strings.ReplaceAll(queryID, "-", "_")
var queryBuilder strings.Builder
var nulls []string
counter := 1

for _, ch := range query {
if ch == '?' {
queryBuilder.WriteString(fmt.Sprintf("$%d", counter))
counter++
nulls = append(nulls, "null")
} else {
queryBuilder.WriteRune(ch)
}
}

preparedQuery := queryBuilder.String()

//nolint:errcheck
defer c.client.Exec(fmt.Sprintf("/* otel-collector-ignore */ DEALLOCATE PREPARE otel_%s", normalizedQueryID))

// if there is no parameter needed, we can not put an empty bracket
nullsString := ""
if len(nulls) > 0 {
nullsString = "(" + strings.Join(nulls, ", ") + ")"
}
setPlanCacheMode := "/* otel-collector-ignore */ SET plan_cache_mode = force_generic_plan;"
prepareStatement := fmt.Sprintf("PREPARE otel_%s AS %s;", normalizedQueryID, preparedQuery)
explainStatement := fmt.Sprintf("EXPLAIN(FORMAT JSON) EXECUTE otel_%s%s;", normalizedQueryID, nullsString)

wrappedDb := sqlquery.NewDbClient(sqlquery.DbWrapper{Db: c.client}, setPlanCacheMode+prepareStatement+explainStatement, logger, sqlquery.TelemetryConfig{})

result, err := wrappedDb.QueryRows(context.Background())
if err != nil {
logger.Error("failed to explain statement", zap.Error(err))
return "", err
}
return obfuscateSQLExecPlan(result[0]["QUERY PLAN"])
}

var _ client = (*postgreSQLClient)(nil)

type postgreSQLConfig struct {
Expand Down Expand Up @@ -892,7 +934,7 @@ func (c *postgreSQLClient) getTopQuery(ctx context.Context, limit int64, logger
for _, row := range rows {
hasConvention := map[string]string{
"datname": "db.namespace",
"query": "db.query.text",
"query": QueryTextAttributeName,
}

needConversion := map[string]func(string, string, *zap.Logger) (any, error){
Expand Down
9 changes: 6 additions & 3 deletions receiver/postgresqlreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ const (
)

type TopQueryCollection struct {
Enabled bool `mapstructure:"enabled"`
MaxRowsPerQuery int64 `mapstructure:"max_rows_per_query"`
TopNQuery int64 `mapstructure:"top_n_query"`
Enabled bool `mapstructure:"enabled"`
MaxRowsPerQuery int64 `mapstructure:"max_rows_per_query"`
TopNQuery int64 `mapstructure:"top_n_query"`
MaxExplainEachInterval int64 `mapstructure:"max_explain_each_interval"`
QueryPlanCacheSize int `mapstructure:"query_plan_cache_size"`
QueryPlanCacheTTL time.Duration `mapstructure:"query_plan_cache_ttl"`
}

type QuerySampleCollection struct {
Expand Down
1 change: 1 addition & 0 deletions receiver/postgresqlreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func TestLoadConfig(t *testing.T) {
expected.QuerySampleCollection.Enabled = true
expected.TopNQuery = 1234
expected.TopQueryCollection.Enabled = true
expected.QueryPlanCacheTTL = time.Second * 123
require.Equal(t, expected, cfg)
})

Expand Down
5 changes: 5 additions & 0 deletions receiver/postgresqlreceiver/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ const (
tempBlksReadColumnName = "temp_blks_read"
tempBlksWrittenColumnName = "temp_blks_written"
)

const (
QueryTextAttributeName = "db.query.text"
DatabaseAttributeName = "db.namespace"
)
28 changes: 20 additions & 8 deletions receiver/postgresqlreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

lru "github.com/hashicorp/golang-lru/v2"
"github.com/hashicorp/golang-lru/v2/expirable"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtls"
Expand All @@ -27,11 +28,19 @@ func newCache(size int) *lru.Cache[string, float64] {
if size <= 0 {
size = 1
}
// lru will only returns error when the size is less than 0
// lru will only return error when the size is less than 0
cache, _ := lru.New[string, float64](size)
return cache
}

func newTTLCache[v any](size int, ttl time.Duration) *expirable.LRU[string, v] {
if size <= 0 {
size = 1
}
cache := expirable.NewLRU[string, v](size, nil, ttl)
return cache
}

func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
Expand Down Expand Up @@ -61,9 +70,12 @@ func createDefaultConfig() component.Config {
MaxRowsPerQuery: 1000,
},
TopQueryCollection: TopQueryCollection{
Enabled: false,
TopNQuery: 1000,
MaxRowsPerQuery: 1000,
Enabled: false,
TopNQuery: 1000,
MaxRowsPerQuery: 1000,
MaxExplainEachInterval: 1000,
QueryPlanCacheSize: 1000,
QueryPlanCacheTTL: time.Hour,
},
}
}
Expand All @@ -83,7 +95,7 @@ func createMetricsReceiver(
clientFactory = newDefaultClientFactory(cfg)
}

ns := newPostgreSQLScraper(params, cfg, clientFactory, newCache(1))
ns := newPostgreSQLScraper(params, cfg, clientFactory, newCache(1), newTTLCache[string](1, time.Second))
s, err := scraper.NewMetrics(ns.scrape, scraper.WithShutdown(ns.shutdown))
if err != nil {
return nil, err
Expand Down Expand Up @@ -116,7 +128,7 @@ func createLogsReceiver(
if cfg.QuerySampleCollection.Enabled {
// query sample collection does not need cache, but we do not want to make it
// nil, so create one size 1 cache as a placeholder.
ns := newPostgreSQLScraper(params, cfg, clientFactory, newCache(1))
ns := newPostgreSQLScraper(params, cfg, clientFactory, newCache(1), newTTLCache[string](1, time.Second))
s, err := scraper.NewLogs(func(ctx context.Context) (plog.Logs, error) {
return ns.scrapeQuerySamples(ctx, cfg.QuerySampleCollection.MaxRowsPerQuery)
}, scraper.WithShutdown(ns.shutdown))
Expand All @@ -133,9 +145,9 @@ func createLogsReceiver(

if cfg.TopQueryCollection.Enabled {
// we have 10 updated only attributes. so we set the cache size accordingly.
ns := newPostgreSQLScraper(params, cfg, clientFactory, newCache(int(cfg.TopNQuery*10*2)))
ns := newPostgreSQLScraper(params, cfg, clientFactory, newCache(int(cfg.TopNQuery*10*2)), newTTLCache[string](cfg.QueryPlanCacheSize, cfg.QueryPlanCacheTTL))
s, err := scraper.NewLogs(func(ctx context.Context) (plog.Logs, error) {
return ns.scrapeTopQuery(ctx, cfg.TopQueryCollection.MaxRowsPerQuery, cfg.TopNQuery)
return ns.scrapeTopQuery(ctx, cfg.TopQueryCollection.MaxRowsPerQuery, cfg.TopNQuery, cfg.MaxExplainEachInterval)
}, scraper.WithShutdown(ns.shutdown))
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion receiver/postgresqlreceiver/generated_package_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 17 additions & 15 deletions receiver/postgresqlreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ require (

require (
dario.cat/mergo v1.0.1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
github.com/DataDog/datadog-go/v5 v5.6.0 // indirect
github.com/DataDog/go-sqllexer v0.1.3 // indirect
github.com/DataDog/go-sqllexer v0.1.4 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand All @@ -50,7 +50,7 @@ require (
github.com/cpuguy83/dockercfg v0.3.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v28.0.1+incompatible // indirect
github.com/docker/docker v28.1.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand All @@ -60,28 +60,30 @@ require (
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-tpm v0.9.5 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/knadh/koanf/maps v0.1.2 // indirect
github.com/knadh/koanf/providers/confmap v1.0.0 // indirect
github.com/knadh/koanf/v2 v2.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 // indirect
github.com/magiconair/properties v1.8.10 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/go-archive v0.1.0 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/sys/atomicwriter v0.1.0 // indirect
github.com/moby/sys/sequential v0.6.0 // indirect
github.com/moby/sys/user v0.4.0 // indirect
github.com/moby/sys/userns v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/moby/term v0.5.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
Expand All @@ -91,12 +93,12 @@ require (
github.com/outcaste-io/ristretto v0.2.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shirou/gopsutil/v4 v4.25.1 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/shirou/gopsutil/v4 v4.25.3 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/tklauser/go-sysconf v0.3.15 // indirect
github.com/tklauser/numcpus v0.10.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/consumer/consumererror v0.127.1-0.20250603105141-605011a1fea8 // indirect
Expand All @@ -107,7 +109,7 @@ require (
go.opentelemetry.io/collector/receiver/receiverhelper v0.127.1-0.20250603105141-605011a1fea8 // indirect
go.opentelemetry.io/collector/receiver/xreceiver v0.127.1-0.20250603105141-605011a1fea8 // indirect
go.opentelemetry.io/contrib/bridges/otelzap v0.11.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
go.opentelemetry.io/otel v1.36.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/log v0.12.2 // indirect
Expand All @@ -121,7 +123,7 @@ require (
golang.org/x/net v0.39.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250414145226-207652e42e2e // indirect
google.golang.org/grpc v1.72.2 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading