Skip to content

Commit bd8504f

Browse files
committed
sql: add SHOW CREATE ALL ROUTINES statement
Implements a `SHOW CREATEA ALL ROUTINES` command. Syntax was added to the `SHOW CREATE` statement in the parser and the new statement implements the statement interface. A generator was added to get the IDs of functions and procedures and query the internal virtual tables based on IDs, so as to not have all statements in memory at once. This change is important for the purpose of allowing users to recreate all routines from a database by using the CREATE statements generated by the command. This is desirable for migrations, backups, etc. Release note (sql change): Added the SHOW CREATE ALL ROUTINES command, which can be used to show CREATE statements for all user-defined functions and procedures in the current database. Epic: CRDB-49582
1 parent 47672d2 commit bd8504f

File tree

24 files changed

+537
-0
lines changed

24 files changed

+537
-0
lines changed

docs/generated/sql/bnf/show_create_stmt.bnf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ show_create_stmt ::=
33
| 'SHOW' 'CREATE' 'ALL' 'SCHEMAS'
44
| 'SHOW' 'CREATE' 'ALL' 'TABLES'
55
| 'SHOW' 'CREATE' 'ALL' 'TYPES'
6+
| 'SHOW' 'CREATE' 'ALL' 'ROUTINES'

docs/generated/sql/bnf/stmt_block.bnf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ show_create_stmt ::=
855855
| 'SHOW' 'CREATE' 'ALL' 'SCHEMAS'
856856
| 'SHOW' 'CREATE' 'ALL' 'TABLES'
857857
| 'SHOW' 'CREATE' 'ALL' 'TYPES'
858+
| 'SHOW' 'CREATE' 'ALL' 'ROUTINES'
858859

859860
show_create_schedules_stmt ::=
860861
'SHOW' 'CREATE' 'ALL' 'SCHEDULES'

pkg/ccl/logictestccl/tests/3node-tenant/generated_test.go

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

pkg/ccl/logictestccl/tests/local-read-committed/generated_test.go

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

pkg/ccl/logictestccl/tests/local-repeatable-read/generated_test.go

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

pkg/sql/delegate/delegate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func TryDelegate(
7474
case *tree.ShowCreateAllTypes:
7575
return d.delegateShowCreateAllTypes()
7676

77+
case *tree.ShowCreateAllRoutines:
78+
return d.delegateShowCreateAllRoutines()
79+
7780
case *tree.ShowDatabaseIndexes:
7881
return d.delegateShowDatabaseIndexes(t)
7982

pkg/sql/delegate/show_function.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/cockroachdb/cockroach/pkg/sql/lexbase"
1212
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
13+
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
1314
"github.com/cockroachdb/errors"
1415
)
1516

@@ -85,3 +86,16 @@ AND %[1]s = %[4]s
8586
nameCol, tab, lexbase.EscapeSQLString(udfSchema), lexbase.EscapeSQLString(un.Parts[0]))
8687
return d.parse(fullQuery)
8788
}
89+
90+
func (d *delegator) delegateShowCreateAllRoutines() (tree.Statement, error) {
91+
sqltelemetry.IncrementShowCounter(sqltelemetry.Create)
92+
93+
const showCreateAllRoutinesQuery = `SELECT crdb_internal.show_create_all_routines(%[1]s) AS create_statement;`
94+
databaseLiteral := d.evalCtx.SessionData().Database
95+
96+
query := fmt.Sprintf(showCreateAllRoutinesQuery,
97+
lexbase.EscapeSQLString(databaseLiteral),
98+
)
99+
100+
return d.parse(query)
101+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
statement ok
2+
CREATE DATABASE d
3+
4+
statement ok
5+
USE d
6+
7+
query T colnames
8+
SHOW CREATE ALL ROUTINES;
9+
----
10+
create_statement
11+
12+
statement ok
13+
CREATE FUNCTION add_one(x INT) RETURNS INT AS 'SELECT x + 1' LANGUAGE SQL;
14+
15+
query T colnames
16+
SHOW CREATE ALL ROUTINES;
17+
----
18+
create_statement
19+
CREATE FUNCTION public.add_one(x INT8)
20+
RETURNS INT8
21+
VOLATILE
22+
NOT LEAKPROOF
23+
CALLED ON NULL INPUT
24+
LANGUAGE SQL
25+
SECURITY INVOKER
26+
AS $$
27+
SELECT x + 1;
28+
$$;
29+
30+
statement ok
31+
CREATE OR REPLACE PROCEDURE double_triple(INOUT double INT, OUT triple INT)
32+
AS $$
33+
BEGIN
34+
double := double * 2;
35+
triple := double * 3;
36+
END;
37+
$$ LANGUAGE PLpgSQL;
38+
39+
40+
query T colnames,nosort
41+
SHOW CREATE ALL ROUTINES;
42+
----
43+
create_statement
44+
CREATE FUNCTION public.add_one(x INT8)
45+
RETURNS INT8
46+
VOLATILE
47+
NOT LEAKPROOF
48+
CALLED ON NULL INPUT
49+
LANGUAGE SQL
50+
SECURITY INVOKER
51+
AS $$
52+
SELECT x + 1;
53+
$$;
54+
CREATE PROCEDURE public.double_triple(INOUT double INT8, OUT triple INT8)
55+
LANGUAGE plpgsql
56+
SECURITY INVOKER
57+
AS $$
58+
BEGIN
59+
double := double * 2;
60+
triple := double * 3;
61+
END;
62+
$$;
63+
64+
statement ok
65+
CREATE FUNCTION add_one(x FLOAT) RETURNS FLOAT AS 'SELECT x + 1' LANGUAGE SQL;
66+
67+
query T colnames,nosort
68+
SHOW CREATE ALL ROUTINES;
69+
----
70+
create_statement
71+
CREATE FUNCTION public.add_one(x INT8)
72+
RETURNS INT8
73+
VOLATILE
74+
NOT LEAKPROOF
75+
CALLED ON NULL INPUT
76+
LANGUAGE SQL
77+
SECURITY INVOKER
78+
AS $$
79+
SELECT x + 1;
80+
$$;
81+
CREATE FUNCTION public.add_one(x FLOAT8)
82+
RETURNS FLOAT8
83+
VOLATILE
84+
NOT LEAKPROOF
85+
CALLED ON NULL INPUT
86+
LANGUAGE SQL
87+
SECURITY INVOKER
88+
AS $$
89+
SELECT x + 1;
90+
$$;
91+
CREATE PROCEDURE public.double_triple(INOUT double INT8, OUT triple INT8)
92+
LANGUAGE plpgsql
93+
SECURITY INVOKER
94+
AS $$
95+
BEGIN
96+
double := double * 2;
97+
triple := double * 3;
98+
END;
99+
$$;
100+
101+
102+
#test dropping routines
103+
statement ok
104+
DROP FUNCTION add_one(x INT8);
105+
106+
statement ok
107+
DROP FUNCTION add_one(x FLOAT8);
108+
109+
query T colnames
110+
SHOW CREATE ALL ROUTINES;
111+
----
112+
create_statement
113+
CREATE PROCEDURE public.double_triple(INOUT double INT8, OUT triple INT8)
114+
LANGUAGE plpgsql
115+
SECURITY INVOKER
116+
AS $$
117+
BEGIN
118+
double := double * 2;
119+
triple := double * 3;
120+
END;
121+
$$;
122+
123+
statement ok
124+
DROP PROCEDURE double_triple;
125+
126+
query T colnames
127+
SHOW CREATE ALL ROUTINES;
128+
----
129+
create_statement
130+
131+
# test user defined schema
132+
statement ok
133+
CREATE SCHEMA s;
134+
135+
statement ok
136+
CREATE FUNCTION add_one(x INT) RETURNS INT AS 'SELECT x + 1' LANGUAGE SQL;
137+
138+
statement ok
139+
CREATE FUNCTION s.add_one(x INT) RETURNS INT AS 'SELECT x + 1' LANGUAGE SQL;
140+
141+
query T colnames,nosort
142+
SHOW CREATE ALL ROUTINES;
143+
----
144+
create_statement
145+
CREATE FUNCTION public.add_one(x INT8)
146+
RETURNS INT8
147+
VOLATILE
148+
NOT LEAKPROOF
149+
CALLED ON NULL INPUT
150+
LANGUAGE SQL
151+
SECURITY INVOKER
152+
AS $$
153+
SELECT x + 1;
154+
$$;
155+
CREATE FUNCTION s.add_one(x INT8)
156+
RETURNS INT8
157+
VOLATILE
158+
NOT LEAKPROOF
159+
CALLED ON NULL INPUT
160+
LANGUAGE SQL
161+
SECURITY INVOKER
162+
AS $$
163+
SELECT x + 1;
164+
$$;

pkg/sql/logictest/tests/fakedist-disk/generated_test.go

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

pkg/sql/logictest/tests/fakedist-vec-off/generated_test.go

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

0 commit comments

Comments
 (0)