-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --generate-stats-helper-sql command to generate helpers psql inst…
…all script (#535) Passing the new "--generate-stats-helper-sql" command to the collector will generate a SQL script that can be passed to "psql" to install stats helpers (e.g. for collecting column stats) on all configured databases for the specified server. The command takes the name of the configuration section of a specific server in the collector configuration (e.g. "server1"), or "default" when using environment variable-based configuration. It can be used manually by saving the output to a file, or by piping it to psql like this: pganalyze-collector --generate-stats-helper-sql=server1 | psql -f - Make sure to execute the generated SQL as an administrative user or database owner (not the pganalyze user!) in order for the SECURITY DEFINER helper functions to have sufficient privileges assigned.
- Loading branch information
Showing
5 changed files
with
119 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/lib/pq" | ||
"github.com/pganalyze/collector/input/postgres" | ||
"github.com/pganalyze/collector/state" | ||
"github.com/pganalyze/collector/util" | ||
) | ||
|
||
var statsHelpers = []string{ | ||
// Column stats | ||
`CREATE OR REPLACE FUNCTION pganalyze.get_column_stats() RETURNS SETOF pg_stats AS | ||
$$ | ||
/* pganalyze-collector */ SELECT schemaname, tablename, attname, inherited, null_frac, avg_width, | ||
n_distinct, NULL::anyarray, most_common_freqs, NULL::anyarray, correlation, NULL::anyarray, | ||
most_common_elem_freqs, elem_count_histogram | ||
FROM pg_catalog.pg_stats; | ||
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;`, | ||
|
||
// Extended stats | ||
`CREATE OR REPLACE FUNCTION pganalyze.get_relation_stats_ext() RETURNS TABLE( | ||
statistics_schemaname text, statistics_name text, | ||
inherited boolean, n_distinct pg_ndistinct, dependencies pg_dependencies, | ||
most_common_val_nulls boolean[], most_common_freqs float8[], most_common_base_freqs float8[] | ||
) AS | ||
$$ | ||
/* pganalyze-collector */ SELECT statistics_schemaname::text, statistics_name::text, | ||
(row_to_json(se.*)::jsonb ->> 'inherited')::boolean AS inherited, n_distinct, dependencies, | ||
most_common_val_nulls, most_common_freqs, most_common_base_freqs | ||
FROM pg_catalog.pg_stats_ext se; | ||
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;`} | ||
|
||
func GenerateStatsHelperSql(ctx context.Context, server *state.Server, globalCollectionOpts state.CollectionOpts, logger *util.Logger) (string, error) { | ||
db, err := postgres.EstablishConnection(ctx, server, logger, globalCollectionOpts, "") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer db.Close() | ||
|
||
version, err := postgres.GetPostgresVersion(ctx, logger, db) | ||
if err != nil { | ||
return "", fmt.Errorf("error collecting Postgres version: %s", err) | ||
} | ||
|
||
databases, _, err := postgres.GetDatabases(ctx, logger, db, version) | ||
if err != nil { | ||
return "", fmt.Errorf("error collecting pg_databases: %s", err) | ||
} | ||
|
||
output := strings.Builder{} | ||
for _, dbName := range postgres.GetDatabasesToCollect(server, databases) { | ||
output.WriteString(fmt.Sprintf("\\c %s\n", pq.QuoteIdentifier(dbName))) | ||
output.WriteString("CREATE SCHEMA IF NOT EXISTS pganalyze;\n") | ||
output.WriteString(fmt.Sprintf("GRANT USAGE ON SCHEMA pganalyze TO %s;\n", server.Config.GetDbUsername())) | ||
for _, helper := range statsHelpers { | ||
output.WriteString(helper + "\n") | ||
} | ||
output.WriteString("\n") | ||
} | ||
|
||
return output.String(), nil | ||
} |
This file contains 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