Skip to content

Commit

Permalink
add pg_stat_statements by default to NodeSet
Browse files Browse the repository at this point in the history
  • Loading branch information
skudasov committed Jan 20, 2025
1 parent 019f990 commit 8588d88
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
7 changes: 7 additions & 0 deletions book/src/framework/observability/postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ We run exporter for the first 5 nodes, that should be enough to debug performanc
## Data sources and raw queries

You can use `PostgreSQL X` datasources, first 5 nodes to select things for your [dashboards](http://localhost:3000/explore?panes=%7B%22qrr%22:%7B%22datasource%22:%22P4DD770FAD7295D26%22,%22queries%22:%5B%7B%22refId%22:%22A%22,%22datasource%22:%7B%22type%22:%22postgres%22,%22uid%22:%22P4DD770FAD7295D26%22%7D,%22format%22:%22table%22,%22rawSql%22:%22select%20%2A%20from%20sessions;%22,%22editorMode%22:%22code%22,%22sql%22:%7B%22columns%22:%5B%7B%22type%22:%22function%22,%22parameters%22:%5B%5D%7D%5D,%22groupBy%22:%5B%7B%22type%22:%22groupBy%22,%22property%22:%7B%22type%22:%22string%22%7D%7D%5D,%22limit%22:50%7D,%22rawQuery%22:true%7D%5D,%22range%22:%7B%22from%22:%22now-6h%22,%22to%22:%22now%22%7D%7D%7D&schemaVersion=1&orgId=1) or debug.

## Slow queries debug

Use `pg_stat_statements` extension, it is enabled for all databases in `NodeSet`
```
select * from pg_stat_statements;
```
1 change: 0 additions & 1 deletion framework/components/jd/jd.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func defaultJDDB() *postgres.Input {
Port: 14000,
Name: "jd-db",
VolumeName: "jd",
JDDatabase: true,
}
}

Expand Down
41 changes: 12 additions & 29 deletions framework/components/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ type Input struct {
Name string `toml:"name"`
VolumeName string `toml:"volume_name"`
Databases int `toml:"databases"`
JDDatabase bool `toml:"jd_database"`
PullImage bool `toml:"pull_image"`
Out *Output `toml:"out"`
}

type Output struct {
Url string `toml:"url"`
ContainerName string `toml:"container_name"`
DockerInternalURL string `toml:"docker_internal_url"`
JDUrl string `toml:"jd_url"`
JDDockerInternalURL string `toml:"jd_docker_internal_url"`
Url string `toml:"url"`
ContainerName string `toml:"container_name"`
DockerInternalURL string `toml:"docker_internal_url"`
}

func NewPostgreSQL(in *Input) (*Output, error) {
Expand All @@ -55,10 +52,11 @@ func NewPostgreSQL(in *Input) (*Output, error) {

var sqlCommands []string
for i := 0; i <= in.Databases; i++ {
sqlCommands = append(sqlCommands, fmt.Sprintf("CREATE DATABASE db_%d;", i))
}
if in.JDDatabase {
sqlCommands = append(sqlCommands, "CREATE DATABASE jd;")
sqlCommands = append(sqlCommands,
fmt.Sprintf("CREATE DATABASE db_%d;", i),
fmt.Sprintf("\\c db_%d", i),
"CREATE EXTENSION pg_stat_statements;",
)
}
sqlCommands = append(sqlCommands, "ALTER USER chainlink WITH SUPERUSER;")
initSQL := strings.Join(sqlCommands, "\n")
Expand Down Expand Up @@ -89,7 +87,10 @@ func NewPostgreSQL(in *Input) (*Output, error) {
"POSTGRES_DB": Database,
},
Cmd: []string{
"postgres", "-c", fmt.Sprintf("port=%s", Port),
"postgres", "-c",
fmt.Sprintf("port=%s", Port),
"-c", "shared_preload_libraries=pg_stat_statements",
"-c", "pg_stat_statements.track=all",
},
Files: []testcontainers.ContainerFile{
{
Expand Down Expand Up @@ -158,23 +159,5 @@ func NewPostgreSQL(in *Input) (*Output, error) {
Database,
),
}
if in.JDDatabase {
o.JDDockerInternalURL = fmt.Sprintf(
"postgresql://%s:%s@%s:%s/%s?sslmode=disable",
User,
Password,
containerName,
Port,
"jd",
)
o.JDUrl = fmt.Sprintf(
"postgresql://%s:%s@%s:%d/%s?sslmode=disable",
User,
Password,
host,
portToExpose,
"jd",
)
}
return o, nil
}

0 comments on commit 8588d88

Please sign in to comment.