Skip to content

Commit 94d5ab0

Browse files
committed
Add "--generate-helper-explain-analyze-role" argument for helper SQL
This optionally generates the SQL for the pganalyze.explain_analyze helper as part of the "--generate--helper-sql" command. The argument takes the role that the helper function should be owned by as a value. Note that the role creation, as well as any GRANT statements for the role must be taken care of separately.
1 parent 1530416 commit 94d5ab0

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

main.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func main() {
5050
var testExplain bool
5151
var testSection string
5252
var generateHelperSql string
53+
var generateHelperExplainAnalyzeRole string
5354
var forceStateUpdate bool
5455
var configFilename string
5556
var stateFilename string
@@ -75,6 +76,7 @@ func main() {
7576
flag.StringVar(&testSection, "test-section", "", "Tests a particular section of the config file, i.e. a specific server, and ignores all other config sections")
7677
flag.StringVar(&generateHelperSql, "generate-stats-helper-sql", "", "Deprecated alias for --generate-helper-sql")
7778
flag.StringVar(&generateHelperSql, "generate-helper-sql", "", "Generates a SQL script for the given server (name of section in the config file, or \"default\" for env variables), that can be run with \"psql -f\" for installing the collector helpers on all configured databases")
79+
flag.StringVar(&generateHelperExplainAnalyzeRole, "generate-helper-explain-analyze-role", "", "Includes pganalyze.explain_analyze helper in generated helper SQL, and sets owner role of helper function")
7880
flag.BoolVar(&reloadRun, "reload", false, "Reloads the collector daemon that's running on the host")
7981
flag.BoolVar(&noReload, "no-reload", false, "Disables automatic config reloading during a test run")
8082
flag.BoolVarP(&logger.Verbose, "verbose", "v", false, "Outputs additional debugging information, use this if you're encountering errors or other problems")
@@ -151,28 +153,29 @@ func main() {
151153
}
152154

153155
globalCollectionOpts := state.CollectionOpts{
154-
StartedAt: time.Now(),
155-
SubmitCollectedData: !benchmark && true,
156-
TestRun: testRun,
157-
TestRunLogs: testRunLogs || dryRunLogs,
158-
TestExplain: testExplain,
159-
TestSection: testSection,
160-
GenerateHelperSql: generateHelperSql,
161-
DebugLogs: debugLogs,
162-
DiscoverLogLocation: discoverLogLocation,
163-
CollectPostgresRelations: !noPostgresRelations,
164-
CollectPostgresSettings: !noPostgresSettings,
165-
CollectPostgresLocks: !noPostgresLocks,
166-
CollectPostgresFunctions: !noPostgresFunctions,
167-
CollectPostgresBloat: !noPostgresBloat,
168-
CollectPostgresViews: !noPostgresViews,
169-
CollectLogs: !noLogs,
170-
CollectExplain: !noExplain,
171-
CollectSystemInformation: !noSystemInformation,
172-
StateFilename: stateFilename,
173-
WriteStateUpdate: (!dryRun && !dryRunLogs && !testRun) || forceStateUpdate,
174-
ForceEmptyGrant: dryRun || dryRunLogs || testRunLogs || benchmark,
175-
OutputAsJson: !benchmark,
156+
StartedAt: time.Now(),
157+
SubmitCollectedData: !benchmark && true,
158+
TestRun: testRun,
159+
TestRunLogs: testRunLogs || dryRunLogs,
160+
TestExplain: testExplain,
161+
TestSection: testSection,
162+
GenerateHelperSql: generateHelperSql,
163+
GenerateHelperExplainAnalyzeRole: generateHelperExplainAnalyzeRole,
164+
DebugLogs: debugLogs,
165+
DiscoverLogLocation: discoverLogLocation,
166+
CollectPostgresRelations: !noPostgresRelations,
167+
CollectPostgresSettings: !noPostgresSettings,
168+
CollectPostgresLocks: !noPostgresLocks,
169+
CollectPostgresFunctions: !noPostgresFunctions,
170+
CollectPostgresBloat: !noPostgresBloat,
171+
CollectPostgresViews: !noPostgresViews,
172+
CollectLogs: !noLogs,
173+
CollectExplain: !noExplain,
174+
CollectSystemInformation: !noSystemInformation,
175+
StateFilename: stateFilename,
176+
WriteStateUpdate: (!dryRun && !dryRunLogs && !testRun) || forceStateUpdate,
177+
ForceEmptyGrant: dryRun || dryRunLogs || testRunLogs || benchmark,
178+
OutputAsJson: !benchmark,
176179
}
177180

178181
if reloadRun && !testRun {

runner/generate_helper_sql.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ func GenerateHelperSql(ctx context.Context, server *state.Server, globalCollecti
6363
for _, helper := range statsHelpers {
6464
output.WriteString(helper + "\n")
6565
}
66+
if globalCollectionOpts.GenerateHelperExplainAnalyzeRole != "" {
67+
output.WriteString(fmt.Sprintf("GRANT CREATE ON SCHEMA pganalyze TO %s;\n", globalCollectionOpts.GenerateHelperExplainAnalyzeRole))
68+
output.WriteString(fmt.Sprintf("SET ROLE %s;\n", globalCollectionOpts.GenerateHelperExplainAnalyzeRole))
69+
output.WriteString(util.ExplainAnalyzeHelper + "\n")
70+
output.WriteString("RESET ROLE;\n")
71+
output.WriteString(fmt.Sprintf("REVOKE CREATE ON SCHEMA pganalyze FROM %s;\n", globalCollectionOpts.GenerateHelperExplainAnalyzeRole))
72+
}
6673
output.WriteString("\n")
6774
}
6875

state/state.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,15 @@ type CollectionOpts struct {
212212

213213
DiffStatements bool
214214

215-
SubmitCollectedData bool
216-
TestRun bool
217-
TestRunLogs bool
218-
TestExplain bool
219-
TestSection string
220-
GenerateHelperSql string
221-
DebugLogs bool
222-
DiscoverLogLocation bool
215+
SubmitCollectedData bool
216+
TestRun bool
217+
TestRunLogs bool
218+
TestExplain bool
219+
TestSection string
220+
GenerateHelperSql string
221+
GenerateHelperExplainAnalyzeRole string
222+
DebugLogs bool
223+
DiscoverLogLocation bool
223224

224225
StateFilename string
225226
WriteStateUpdate bool

0 commit comments

Comments
 (0)