Skip to content

Commit c61e0e4

Browse files
committed
Query runner: Add parameter handling and Postgres settings support
1 parent 94d5ab0 commit c61e0e4

File tree

5 files changed

+203
-52
lines changed

5 files changed

+203
-52
lines changed

output/pganalyze_collector/server_message.pb.go

Lines changed: 152 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protobuf/server_message.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,18 @@ message ServerMessage {
3939
bool pause = 1;
4040
}
4141

42+
message QueryRunPostgresSetting {
43+
string name = 1;
44+
string value = 2;
45+
}
46+
4247
message QueryRun {
4348
int64 id = 1;
4449
QueryRunType type = 2;
4550
string database_name = 3;
4651
string query_text = 4;
52+
repeated NullString query_parameters = 5;
53+
repeated string query_parameter_types = 6;
54+
repeated QueryRunPostgresSetting postgres_settings = 7;
4755
}
4856
}

runner/query_run.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"time"
88

9+
"github.com/lib/pq"
910
"github.com/pganalyze/collector/input/postgres"
1011
"github.com/pganalyze/collector/output"
1112
"github.com/pganalyze/collector/output/pganalyze_collector"
@@ -65,11 +66,18 @@ func runQueryOnDatabase(ctx context.Context, server *state.Server, collectionOpt
6566
server.QueryRuns[id].BackendPid = pid
6667
server.QueryRunsMutex.Unlock()
6768

69+
for _, setting := range query.PostgresSettings {
70+
_, err = db.ExecContext(ctx, postgres.QueryMarkerSQL+"SET %s = %s", pq.QuoteIdentifier(setting.Name), pq.QuoteLiteral(setting.Value))
71+
if err != nil {
72+
return "", err
73+
}
74+
}
75+
6876
// We don't include QueryMarkerSQL so query runs are reported separately in pganalyze
6977
marker := fmt.Sprintf("/* pganalyze:no-alert,pganalyze-query-run:%d */ ", query.Id)
7078

7179
if query.Type == pganalyze_collector.QueryRunType_EXPLAIN {
72-
return postgres.RunExplainAnalyzeForQueryRun(ctx, db, query.QueryText, nil, nil, marker)
80+
return postgres.RunExplainAnalyzeForQueryRun(ctx, db, query.QueryText, query.QueryParameters, query.QueryParameterTypes, marker)
7381
} else {
7482
logger.PrintVerbose("Unhandled query run type %d for %d", query.Type, query.Id)
7583
return "", errors.New("Unhandled query run type")

runner/websocket.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/gorilla/websocket"
14+
"github.com/guregu/null"
1415
"github.com/pganalyze/collector/output/pganalyze_collector"
1516
"github.com/pganalyze/collector/state"
1617
"github.com/pganalyze/collector/util"
@@ -140,12 +141,23 @@ func connect(ctx context.Context, server *state.Server, globalCollectionOpts sta
140141
} else if message.GetQueryRun() != nil {
141142
q := message.GetQueryRun()
142143
logger.PrintVerbose("Query run %d received: %s", q.Id, q.QueryText)
144+
parameters := []null.String{}
145+
for _, p := range q.QueryParameters {
146+
parameters = append(parameters, null.NewString(p.Value, p.Valid))
147+
}
148+
settings := []state.QueryRunPostgresSetting{}
149+
for _, s := range q.PostgresSettings {
150+
settings = append(settings, state.QueryRunPostgresSetting{Name: s.Name, Value: s.Value})
151+
}
143152
server.QueryRunsMutex.Lock()
144153
server.QueryRuns[q.Id] = &state.QueryRun{
145-
Id: q.Id,
146-
Type: q.Type,
147-
DatabaseName: q.DatabaseName,
148-
QueryText: q.QueryText,
154+
Id: q.Id,
155+
Type: q.Type,
156+
DatabaseName: q.DatabaseName,
157+
QueryText: q.QueryText,
158+
QueryParameters: parameters,
159+
QueryParameterTypes: q.QueryParameterTypes,
160+
PostgresSettings: settings,
149161
}
150162
server.QueryRunsMutex.Unlock()
151163
}

state/state.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
raven "github.com/getsentry/raven-go"
1010
"github.com/gorilla/websocket"
11+
"github.com/guregu/null"
1112
"github.com/pganalyze/collector/config"
1213
"github.com/pganalyze/collector/output/pganalyze_collector"
1314
)
@@ -253,16 +254,24 @@ type CollectionStatus struct {
253254
LogSnapshotDisabledReason string
254255
}
255256

257+
type QueryRunPostgresSetting struct {
258+
Name string
259+
Value string
260+
}
261+
256262
type QueryRun struct {
257-
Id int64
258-
Type pganalyze_collector.QueryRunType
259-
DatabaseName string
260-
QueryText string
261-
Result string
262-
Error string
263-
StartedAt time.Time
264-
FinishedAt time.Time
265-
BackendPid int
263+
Id int64
264+
Type pganalyze_collector.QueryRunType
265+
DatabaseName string
266+
QueryText string
267+
QueryParameters []null.String
268+
QueryParameterTypes []string
269+
PostgresSettings []QueryRunPostgresSetting
270+
Result string
271+
Error string
272+
StartedAt time.Time
273+
FinishedAt time.Time
274+
BackendPid int
266275
}
267276

268277
type Server struct {

0 commit comments

Comments
 (0)