Categories: Varia

Get your Query History in Redshift

Because my Windows machine apparently decides to install updates over night (and thus reboot my machine), it has happened that I lost the query that I was writing for Redshift in the tool DBeaver. When you work with SQL Server Management Studio (SSMS), you typically don’t have this issue as a temporary copy is always saved. Close down SSMS, restart it and the queries are still there.

If you want to retrieve your query history, you can query the system table stl_query. You probably want to filter on a particular user (yourself). You can find the necessary info in the table pg_user.However, I got some weird errors when I tried to join the two tables together.

However, using a subquery does work:

SELECT userid, query,label,querytxt,starttime,endtime
FROM stl_query q
WHERE q.userid = (SELECT usesysid FROM pg_user u WHERE u.usename = 'myusername')
ORDER BY starttime DESC

The only problem is that the query from the result is not formatted. Luckily DBeaver has a formatting option that can do this for you. You don’t have this issue in SSMS, especially when you use SQLPrompt where you can retrieve closed tabs. Snowflake also has a nice query history table you can query (and it has a visual history tab as well, where you can get the formatted query).

Update:

I forgot to mention that another drawback of getting unformatted code is that single line comments

-- you know, like this one

really mess up everything. Because if you want to use your automatic formatter, it will likely error out over syntax issues since part of your SQL code will suddenly be commented out. All the more reason to use block comments

/* like this one */

in your SQL scripts.


------------------------------------------------
Do you like this blog post? You can thank me by buying me a beer 🙂
Koen Verbeeck

Koen Verbeeck is a Microsoft Business Intelligence consultant at AE, helping clients to get insight in their data. Koen has a comprehensive knowledge of the SQL Server BI stack, with a particular love for Integration Services. He's also a speaker at various conferences.

View Comments

Recent Posts

Free webinar – Tackling the Gaps and Islands Problem with T-SQL Window Functions

I'm hosting a free webinar at MSSQLTips.com at the 19th of December 2024, 6PM UTC.…

5 days ago

dataMinds Connect 2024 – Session Materials

The slides and scripts for my session "Tackling the Gaps & Islands Problem with T-SQL…

4 weeks ago

Connect to Power BI as a Guest User in another Tenant

Sometimes your Microsoft Entra ID account (formerly known as Azure Active Directory) is added as…

2 months ago

How to use a Script Activity in ADF as a Lookup

In Azure Data Factory (ADF, but also Synapse Pipelines and Fabric Pipelines), you have a…

4 months ago

Database Build Error – Incorrect syntax near DISTINCT

I wrote a piece of SQL that had some new T-SQL syntax in it: IS…

4 months ago

Speaking at dataMinds Connect 2024

I'm very excited to announce I've been selected as a speaker for dataMinds Connect 2024,…

5 months ago