@@ -42,6 +42,9 @@ SELECT pg_sleep(1);
4242statement ok
4343EXPLAIN ANALYZE (DEBUG) SELECT * FROM t;
4444
45+ statement ok
46+ SELECT crdb_internal.request_statement_bundle('SELECT _, _, _ FROM _', 0::FLOAT, 0::INTERVAL, 0::INTERVAL);
47+
4548upgrade 0
4649
4750statement ok
@@ -50,10 +53,60 @@ SELECT pg_sleep(1);
5053statement ok
5154EXPLAIN ANALYZE (DEBUG) SELECT * FROM t;
5255
56+ statement ok
57+ GRANT SYSTEM VIEWACTIVITY TO testuser;
58+ GRANT SELECT ON t TO testuser;
59+
60+ user testuser
61+
62+ # Request a statement bundle as testuser. The user won't be captured since
63+ # the cluster setting is not yet active.
64+ statement ok
65+ SELECT crdb_internal.request_statement_bundle('SELECT _, _, _, _ FROM _', 0::FLOAT, 0::INTERVAL, 0::INTERVAL);
66+
67+ user root
68+
5369upgrade 2
5470
71+ # Block until upgrade is complete.
72+ statement ok
73+ SET CLUSTER SETTING version = crdb_internal.node_executable_version();
74+
75+ query B retry
76+ SELECT crdb_internal.is_at_least_version('25.1-08')
77+ ----
78+ true
79+
5580statement ok
5681SELECT pg_sleep(1);
5782
83+ user testuser
84+
85+ # Request a statement bundle as testuser. Since it's an explicit
86+ # EXPLAIN ANALYZE (DEBUG) invocation, we don't store the username in
87+ # system.statement_diagnostics_requests.
5888statement ok
5989EXPLAIN ANALYZE (DEBUG) SELECT * FROM t;
90+
91+ # Request a statement bundle as testuser. The user should be captured since
92+ # the cluster setting is now active.
93+ statement ok
94+ SELECT crdb_internal.request_statement_bundle('SELECT _, _, _, _, _ FROM _', 0::FLOAT, 0::INTERVAL, 0::INTERVAL);
95+
96+ user root
97+
98+ # Currently the user is not set for requests added with EXPLAIN ANALYZE (DEBUG).
99+ # For other requests, the user is only set once the cluster setting is active,
100+ # in this case for the final invocation of crdb_internal.request_statement_bundle.
101+ query TT
102+ SELECT statement_fingerprint, username FROM system.statement_diagnostics_requests
103+ ORDER BY statement_fingerprint;
104+ ----
105+ EXPLAIN ANALYZE (DEBUG) SELECT * FROM t ·
106+ EXPLAIN ANALYZE (DEBUG) SELECT * FROM t ·
107+ EXPLAIN ANALYZE (DEBUG) SELECT * FROM t ·
108+ EXPLAIN ANALYZE (DEBUG) SELECT * FROM t ·
109+ SELECT _, _ FROM _ ·
110+ SELECT _, _, _ FROM _ ·
111+ SELECT _, _, _, _ FROM _ ·
112+ SELECT _, _, _, _, _ FROM _ testuser
0 commit comments