

 Amazon Redshift will no longer support the creation of new Python UDFs starting Patch 198. Existing Python UDFs will continue to function until June 30, 2026. For more information, see the [ blog post ](https://amazonaws-china.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/). 

# STL\$1UTILITYTEXT
STL\$1UTILITYTEXT

Captures the text of non-SELECT SQL commands run on the database.

Query the STL\$1UTILITYTEXT view to capture the following subset of SQL statements that were run on the system:
+ ABORT, BEGIN, COMMIT, END, ROLLBACK
+ ANALYZE
+ CALL
+ CANCEL
+ COMMENT
+ CREATE, ALTER, DROP DATABASE
+ CREATE, ALTER, DROP USER
+ EXPLAIN
+ GRANT, REVOKE
+ LOCK
+ RESET
+ SET
+ SHOW
+ TRUNCATE

See also [STL\$1DDLTEXT](r_STL_DDLTEXT.md), [STL\$1QUERYTEXT](r_STL_QUERYTEXT.md), and [SVL\$1STATEMENTTEXT](r_SVL_STATEMENTTEXT.md).

Use the STARTTIME and ENDTIME columns to find out which statements were logged during a given time period. Long blocks of SQL text are broken into lines 200 characters long; the SEQUENCE column identifies fragments of text that belong to a single statement.

The STL\$1UTILITYTEXT system table supports both MANUAL and AUTO REFRESH operations of materialized views. To identify AUTO REFRESH on materialized views, find the column `label`. All AUTO REFRESH queries have a label with the value `maintenance`.

STL\$1UTILITYTEXT is visible to all users. Superusers can see all rows; regular users can see only their own data. For more information, see [Visibility of data in system tables and views](cm_chap_system-tables.md#c_visibility-of-data).

Some or all of the data in this table can also be found in the SYS monitoring view [SYS\$1QUERY\$1HISTORY](SYS_QUERY_HISTORY.md). The data in the SYS monitoring view is formatted to be easier to use and understand. We recommend that you use the SYS monitoring view for your queries.

## Table columns
Table columns

[\[See the AWS documentation website for more details\]](http://docs.amazonaws.cn/en_us/redshift/latest/dg/r_STL_UTILITYTEXT.html)

## Sample queries
Sample queries

The following query returns the text for "utility" commands that were run on January 26th, 2012. In this case, some SET commands and a SHOW ALL command were run: 

```
select starttime, sequence, rtrim(text)
from stl_utilitytext
where starttime like '2012-01-26%'
order by starttime, sequence;

starttime          | sequence |              rtrim
---------------------------+-----+----------------------------------
2012-01-26 13:05:52.529235 |   0 | show all;
2012-01-26 13:20:31.660255 |   0 | SET query_group to ''
2012-01-26 13:20:54.956131 |   0 | SET query_group to 'soldunsold.sql'
...
```

### Reconstructing Stored SQL
Reconstructing Stored SQL

To reconstruct the SQL stored in the `text` column of STL\$1UTILITYTEXT, run a SELECT statement to create SQL from 1 or more parts in the `text` column. Before running the reconstructed SQL, replace any (`\n`) special characters with a new line. The result of the following SELECT statement is rows of reconstructed SQL in the `query_statement` field.

```
SELECT LISTAGG(CASE WHEN LEN(RTRIM(text)) = 0 THEN text ELSE RTRIM(text) END) WITHIN GROUP (ORDER BY sequence) as query_statement 
FROM stl_utilitytext GROUP BY xid order by xid;
```

For example, the following query sets the query\$1group to a string of zeros. The query itself is longer than 200 characters and is stored in parts in STL\$1UTILITYTEXT.

```
set query_group to '00000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000                  000000';
```

In this example, the query is stored in 2 parts (rows) in the `text` column of STL\$1UTILITYTEXT.

```
select query, sequence, text
from stl_utilitytext where query=pg_last_query_id() order by query desc, sequence limit 10;
```

```
         starttime          | sequence |                                                                                                   text                                                                                                   
----------------------------+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 2019-07-23 22:55:34.926198 |        0 | set query_group to '00000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000
 2019-07-23 22:55:34.926198 |        1 |                   000000';
```

To reconstruct the SQL stored in STL\$1UTILITYTEXT, run the following SQL. 

```
select LISTAGG(CASE WHEN LEN(RTRIM(text)) = 0 THEN text ELSE RTRIM(text) END, '') within group (order by sequence) AS query_statement 
from stl_utilitytext where query=pg_last_query_id();
```

To use the resulting reconstructed SQL in your client, replace any (`\n`) special characters with a new line. 

```
                                                                                                                                      query_statement                                                                                                                                       
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 set query_group to '00000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000                  000000';
```