Menu
Amazon Redshift
Database Developer Guide (API Version 2012-12-01)

PG_LAST_QUERY_ID()

Returns the query ID of the most recently executed query in the current session. If no queries have been executed in the current session, PG_LAST_QUERY_ID returns -1. PG_LAST_QUERY_ID does not return the query ID for queries that execute exclusively on the leader node. For more information, see Leader Node–Only Functions.

Syntax

Copy
pg_last_query_id()

Return Type

Returns an integer.

Example

The following query returns the ID of the latest query executed in the current session.

Copy
select pg_last_query_id(); pg_last_query_id ---------------- 5437 (1 row)

The following query returns the query ID and text of the most recently executed query in the current session.

Copy
select query, trim(querytxt) as sqlquery from stl_query where query = pg_last_query_id(); query | sqlquery ------+-------------------------------------------------- 5437 | select name, loadtime from stl_file_scan where loadtime > 1000000; (1 rows)