Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Allow auto_explain to be turned on in maintenance jobs
Browse files Browse the repository at this point in the history
Auto explain allows postgres to log the queries inside of the maintenance
jobs and their explain plans. This is a debugging feature.
  • Loading branch information
cevian committed Jul 2, 2021
1 parent 4d26951 commit 0aa7f12
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/migrations/migration_files_generated.go

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions pkg/migrations/sql/idempotent/base.sql
Expand Up @@ -1815,8 +1815,30 @@ CREATE OR REPLACE PROCEDURE SCHEMA_CATALOG.execute_maintenance_job(job_id int, c
AS $$
DECLARE
log_verbose boolean;
ae_key text;
ae_value text;
ae_load boolean := FALSE;
BEGIN
log_verbose := coalesce(config->>'log_verbose', 'false')::boolean;

--if auto_explain enabled in config, turn it on in a best-effort way
--i.e. if it fails (most likely due to lack of superuser priviliges) move on anyway.
BEGIN
FOR ae_key, ae_value IN
SELECT * FROM jsonb_each_text(config->'auto_explain')
LOOP
IF NOT ae_load THEN
ae_load := true;
LOAD 'auto_explain';
END IF;

PERFORM set_config('auto_explain.'|| ae_key, ae_value, FALSE);
END LOOP;
EXCEPTION WHEN OTHERS THEN
RAISE WARNING 'could not set auto_explain options';
END;


CALL SCHEMA_PROM.execute_maintenance(log_verbose=>log_verbose);
END
$$ LANGUAGE PLPGSQL;
Expand Down
21 changes: 15 additions & 6 deletions pkg/tests/end_to_end_tests/create_test.go
Expand Up @@ -1618,9 +1618,13 @@ func TestExecuteMaintJob(t *testing.T) {
t.Skip("test meaningless without Timescale 2")
}
withDB(t, *testDatabase, func(dbOwner *pgxpool.Pool, t testing.TB) {
db := dbOwner
dbSuper, err := pgxpool.Connect(context.Background(), testhelpers.PgConnectURL(*testDatabase, testhelpers.Superuser))
if err != nil {
t.Fatal(err)
}
defer dbSuper.Close()

execJob := func(config *string, configErr bool) {
execJob := func(db *pgxpool.Pool, config *string, configErr bool) {
_, err := db.Exec(context.Background(), "CALL _prom_catalog.execute_maintenance_job(2, $1)", config)
if err != nil {
if !configErr {
Expand All @@ -1634,13 +1638,18 @@ func TestExecuteMaintJob(t *testing.T) {

}

execJob(nil, false)
execJob(dbOwner, nil, false)
config := `{"log_verbose": true}`
execJob(&config, false)
execJob(dbOwner, &config, false)
config = `{"log_verbose": false}`
execJob(&config, false)
execJob(dbOwner, &config, false)
config = `{"log_verbose": "rr"}`
execJob(&config, true)
execJob(dbOwner, &config, true)
config = `{"auto_explain": {"log_min_duration": 0, "log_nested_statements": "true"}}`
//dbOwner will not have enough permissions for auto_explain but this should still succeed
execJob(dbOwner, &config, false)
//the superuser should be able to use auto_explain
execJob(dbSuper, &config, false)
})
}

Expand Down

0 comments on commit 0aa7f12

Please sign in to comment.