Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: allow variable show statement in sql editor read only mode for postgresql (#12387)

h3n4l171024971c67
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index d73579b5e3..f870a36047 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -41,6 +41,9 @@ var (
driverName = "pgx"
_ db.Driver = (*Driver)(nil)
+
+ variableSetStmtRegexp = regexp.MustCompile(`(?i)^SET\s+?`)
+ variableShowStmtRegexp = regexp.MustCompile(`(?i)^SHOW\s+?`)
)
func init() {
@@ -709,8 +712,9 @@ func getStatementWithResultLimit(stmt string, limit int) string {
func (*Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL base.SingleSQL, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
statement := strings.Trim(singleSQL.Text, " \n\t;")
- isSet, _ := regexp.MatchString(`(?i)^SET\s+?`, statement)
- if !isSet {
+ isSet := variableSetStmtRegexp.MatchString(statement)
+ isShow := variableShowStmtRegexp.MatchString(statement)
+ if !isSet && !isShow {
if queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
} else if queryContext.Limit > 0 {
diff --git a/backend/plugin/parser/pg/query.go b/backend/plugin/parser/pg/query.go
index 4b4081cfd5..cc9328bd82 100644
--- a/backend/plugin/parser/pg/query.go
+++ b/backend/plugin/parser/pg/query.go
@@ -40,7 +40,7 @@ func validateQuery(statement string) (bool, error) {
explainAnalyze := false
for _, stmt := range stmtList {
switch stmt := stmt.(type) {
- case *ast.SelectStmt, *ast.VariableSetStmt:
+ case *ast.SelectStmt, *ast.VariableSetStmt, *ast.VariableShowStmt:
case *ast.ExplainStmt:
if stmt.Analyze {
// We only support analyze select, because analyze will actually execute the statement.
diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.go
index bebf8289a5..88403f4941 100644
--- a/backend/plugin/parser/pg/query_span_extractor.go
+++ b/backend/plugin/parser/pg/query_span_extractor.go
@@ -117,8 +117,8 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba
Results: []base.QuerySpanResult{},
SourceColumns: base.SourceColumnSet{},
}, nil
- case *pgquery.Node_VariableSetStmt:
- // Skip the SET statement.
+ case *pgquery.Node_VariableSetStmt, *pgquery.Node_VariableShowStmt:
+ // Skip the SET statement and VARIABLE SHOW statement.
return &base.QuerySpan{
Results: []base.QuerySpanResult{},
SourceColumns: base.SourceColumnSet{},
diff --git a/backend/plugin/parser/sql/ast/variable_show_stmt.go b/backend/plugin/parser/sql/ast/variable_show_stmt.go
new file mode 100644
index 0000000000..99f6d51110
--- /dev/null
+++ b/backend/plugin/parser/sql/ast/variable_show_stmt.go
@@ -0,0 +1,7 @@
+package ast
+
+type VariableShowStmt struct {
+ node
+
+ Name string
+}
diff --git a/backend/plugin/parser/sql/engine/pg/convert.go b/backend/plugin/parser/sql/engine/pg/convert.go
index 5e75b5351d..679fc25d06 100644
--- a/backend/plugin/parser/sql/engine/pg/convert.go
+++ b/backend/plugin/parser/sql/engine/pg/convert.go
@@ -875,6 +875,11 @@ func convert(node *pgquery.Node, statement base.SingleSQL) (res ast.Node, err er
viewStmt.SetOriginalNode(in)
return viewStmt, nil
+ case *pgquery.Node_VariableShowStmt:
+ variableShowStmt := &ast.VariableShowStmt{
+ Name: in.VariableShowStmt.GetName(),
+ }
+ return variableShowStmt, nil
default:
return &ast.UnconvertedStmt{}, nil
}
diff --git a/backend/plugin/parser/sql/engine/pg/convert_test.go b/backend/plugin/parser/sql/engine/pg/convert_test.go
index 74dff429cc..d6240c9968 100644
--- a/backend/plugin/parser/sql/engine/pg/convert_test.go
+++ b/backend/plugin/parser/sql/engine/pg/convert_test.go
@@ -64,6 +64,42 @@ func newExpression(expression ast.ExpressionNode, text string) ast.ExpressionNod
return expression
}
+func TestPGVariableShowStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: `SHOW TIME ZONE`,
+ want: []ast.Node{
+ &ast.VariableShowStmt{
+ Name: "timezone",
+ },
+ },
+ statementList: []base.SingleSQL{
+ {
+ Text: `SHOW TIME ZONE`,
+ LastLine: 1,
+ },
+ },
+ },
+
+ {
+ stmt: `SHOW myvar`,
+ want: []ast.Node{
+ &ast.VariableShowStmt{
+ Name: "myvar",
+ },
+ },
+ statementList: []base.SingleSQL{
+ {
+ Text: `SHOW myvar`,
+ LastLine: 1,
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}
+
func TestPGConvertCreateViewStmt(t *testing.T) {
tests := []testData{
{
@@ -2294,11 +2330,11 @@ func TestCopyStmt(t *testing.T) {
func TestUnconvertStmt(t *testing.T) {
tests := []testData{
{
- stmt: "SHOW TABLES",
+ stmt: "UNLISTEN *",
want: []ast.Node{&ast.UnconvertedStmt{}},
statementList: []base.SingleSQL{
{
- Text: "SHOW TABLES",
+ Text: "UNLISTEN *",
LastLine: 1,
},
},